Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RSqliteDS.hxx
Go to the documentation of this file.
1// Author: Jakob Blomer CERN 07/2018
2
3/*************************************************************************
4 * Copyright (C) 1995-2017, Rene Brun and Fons Rademakers. *
5 * All rights reserved. *
6 * *
7 * For the licensing terms see $ROOTSYS/LICENSE. *
8 * For the list of contributors see $ROOTSYS/README/CREDITS. *
9 *************************************************************************/
10
11#ifndef ROOT_RSQLITEDS
12#define ROOT_RSQLITEDS
13
14#include "ROOT/RDataFrame.hxx"
15#include "ROOT/RDataSource.hxx"
16#include "ROOT/RStringView.hxx"
17
18#include <memory>
19#include <mutex>
20#include <string>
21#include <vector>
22
23namespace ROOT {
24
25namespace RDF {
26
27namespace Internal {
28// Members are defined in RSqliteDS.cxx in order to not pullute this header file with sqlite3.h
29struct RSqliteDSDataSet;
30}
31
32// clang-format off
33/**
34\class ROOT::RDF::RSqliteDS
35\ingroup dataframe
36\brief RSqliteDS is an RDF data source implementation for SQL result sets from sqlite3 files.
37
38The RSqliteDS is able to feed an RDataFrame with data from a SQlite SELECT query. One can use it like
39
40 auto rdf = ROOT::RDF::MakeSqliteDataFrame("/path/to/file.sqlite", "select name from table");
41 auto h = rdf.Define("lName", "name.length()").Histo1D("lName");
42
43The data source has to provide column types for all the columns. Determining column types in SQlite is tricky
44as it is dynamically typed and in principle each row can have different column types. The following heuristics
45is used:
46
47 - If a table column is queried as is ("SELECT colname FROM table"), the default/declared column type is taken.
48 - For expressions ("SELECT 1+1 FROM table"), the type of the first row of the result set determines the column type.
49 That can result in a column to be of thought of type NULL where subsequent rows actually have meaningful values.
50 The provided SELECT query can be used to avoid such ambiguities.
51*/
52class RSqliteDS final : public ROOT::RDF::RDataSource {
53private:
54 // clang-format off
55 /// All the types known to SQlite. Changes require changing fgTypeNames, too.
56 enum class ETypes {
58 kReal,
59 kText,
60 kBlob,
61 kNull
62 };
63 // clang-format on
64
65 /// Used to hold a single "cell" of the SELECT query's result table. Can be changed to std::variant once available.
66 struct Value_t {
67 explicit Value_t(ETypes type);
68
70 bool fIsActive; ///< Not all columns of the query are necessarily used by the RDF. Allows for skipping them.
72 double fReal;
73 std::string fText;
74 std::vector<unsigned char> fBlob;
75 void *fNull;
76 void *fPtr; ///< Points to one of the values; an address to this pointer is returned by GetColumnReadersImpl.
77 };
78
79 void SqliteError(int errcode);
80
81 std::unique_ptr<Internal::RSqliteDSDataSet> fDataSet;
82 unsigned int fNSlots;
84 std::vector<std::string> fColumnNames;
85 std::vector<ETypes> fColumnTypes;
86 /// The data source is inherently single-threaded and returns only one row at a time. This vector holds the results.
87 std::vector<Value_t> fValues;
88
89 // clang-format off
90 /// Corresponds to the types defined in ETypes.
91 static constexpr char const *fgTypeNames[] = {
92 "Long64_t",
93 "double",
94 "std::string",
95 "std::vector<unsigned char>",
96 "void *"
97 };
98 // clang-format on
99
100public:
101 RSqliteDS(const std::string &fileName, const std::string &query);
102 ~RSqliteDS();
103 void SetNSlots(unsigned int nSlots) final;
104 const std::vector<std::string> &GetColumnNames() const final;
105 bool HasColumn(std::string_view colName) const final;
106 std::string GetTypeName(std::string_view colName) const final;
107 std::vector<std::pair<ULong64_t, ULong64_t>> GetEntryRanges() final;
108 bool SetEntry(unsigned int slot, ULong64_t entry) final;
109 void Initialise() final;
110 std::string GetLabel() final;
111
112protected:
113 Record_t GetColumnReadersImpl(std::string_view name, const std::type_info &) final;
114};
115
116RDataFrame MakeSqliteDataFrame(std::string_view fileName, std::string_view query);
117
118} // namespace RDF
119
120} // namespace ROOT
121
122#endif
long long Long64_t
Definition RtypesCore.h:80
unsigned long long ULong64_t
Definition RtypesCore.h:81
char name[80]
Definition TGX11.cxx:110
int type
Definition TGX11.cxx:121
RDataSource defines an API that RDataFrame can use to read arbitrary data formats.
std::vector< void * > Record_t
RSqliteDS is an RDF data source implementation for SQL result sets from sqlite3 files.
Definition RSqliteDS.hxx:52
void SetNSlots(unsigned int nSlots) final
Almost a no-op, many slots can in fact reduce the performance due to thread synchronization.
static constexpr char const * fgTypeNames[]
Corresponds to the types defined in ETypes.
Definition RSqliteDS.hxx:91
std::string GetLabel() final
Return a string representation of the datasource type.
void Initialise() final
Resets the SQlite query engine at the beginning of the event loop.
unsigned int fNSlots
Definition RSqliteDS.hxx:82
std::vector< std::string > fColumnNames
Definition RSqliteDS.hxx:84
~RSqliteDS()
Frees the sqlite resources and closes the file.
bool HasColumn(std::string_view colName) const final
A linear search through the columns for the given name.
std::vector< ETypes > fColumnTypes
Definition RSqliteDS.hxx:85
std::string GetTypeName(std::string_view colName) const final
Returns the C++ type for a given column name, implemented as a linear search through all the columns.
ETypes
All the types known to SQlite. Changes require changing fgTypeNames, too.
Definition RSqliteDS.hxx:56
Record_t GetColumnReadersImpl(std::string_view name, const std::type_info &) final
Activates the given column's result value.
std::unique_ptr< Internal::RSqliteDSDataSet > fDataSet
Definition RSqliteDS.hxx:81
std::vector< std::pair< ULong64_t, ULong64_t > > GetEntryRanges() final
Returns a range of size 1 as long as more rows are available in the SQL result set.
const std::vector< std::string > & GetColumnNames() const final
Returns the SELECT queries names.
bool SetEntry(unsigned int slot, ULong64_t entry) final
Stores the result of the current active sqlite query row as a C++ value.
void SqliteError(int errcode)
Helper function to throw an exception if there is a fatal sqlite error, e.g. an I/O error.
std::vector< Value_t > fValues
The data source is inherently single-threaded and returns only one row at a time. This vector holds t...
Definition RSqliteDS.hxx:87
ROOT's RDataFrame offers a high level interface for analyses of data stored in TTree,...
RDataFrame MakeSqliteDataFrame(std::string_view fileName, std::string_view query)
Factory method to create a SQlite RDataFrame.
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
The state of an open dataset in terms of the sqlite3 C library.
Used to hold a single "cell" of the SELECT query's result table. Can be changed to std::variant once ...
Definition RSqliteDS.hxx:66
void * fPtr
Points to one of the values; an address to this pointer is returned by GetColumnReadersImpl.
Definition RSqliteDS.hxx:76
std::vector< unsigned char > fBlob
Definition RSqliteDS.hxx:74
bool fIsActive
Not all columns of the query are necessarily used by the RDF. Allows for skipping them.
Definition RSqliteDS.hxx:70