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 <string_view>
17
18#include <memory>
19#include <string>
20#include <vector>
21
22namespace ROOT {
23
24namespace RDF {
25
26namespace Internal {
27// Members are defined in RSqliteDS.cxx in order to not pullute this header file with sqlite3.h
28struct RSqliteDSDataSet;
29}
30
31// clang-format off
32/**
33\class ROOT::RDF::RSqliteDS
34\ingroup dataframe
35\brief RSqliteDS is an RDF data source implementation for SQL result sets from sqlite3 files.
36
37The RSqliteDS is able to feed an RDataFrame with data from a SQlite SELECT query. One can use it like
38
39 auto rdf = ROOT::RDF::FromSqlite("/path/to/file.sqlite", "select name from table");
40 auto h = rdf.Define("lName", "name.length()").Histo1D("lName");
41
42The data source has to provide column types for all the columns. Determining column types in SQlite is tricky
43as it is dynamically typed and in principle each row can have different column types. The following heuristics
44is used:
45
46 - If a table column is queried as is ("SELECT colname FROM table"), the default/declared column type is taken.
47 - For expressions ("SELECT 1+1 FROM table"), the type of the first row of the result set determines the column type.
48 That can result in a column to be of thought of type NULL where subsequent rows actually have meaningful values.
49 The provided SELECT query can be used to avoid such ambiguities.
50*/
51class RSqliteDS final : public ROOT::RDF::RDataSource {
52private:
53 // clang-format off
54 /// All the types known to SQlite. Changes require changing fgTypeNames, too.
55 enum class ETypes {
57 kReal,
58 kText,
59 kBlob,
60 kNull
61 };
62 // clang-format on
63
64 /// Used to hold a single "cell" of the SELECT query's result table. Can be changed to std::variant once available.
65 struct Value_t {
66 explicit Value_t(ETypes type);
67
69 bool fIsActive; ///< Not all columns of the query are necessarily used by the RDF. Allows for skipping them.
71 double fReal;
72 std::string fText;
73 std::vector<unsigned char> fBlob;
74 void *fNull;
75 void *fPtr; ///< Points to one of the values; an address to this pointer is returned by GetColumnReadersImpl.
76 };
77
78 void SqliteError(int errcode);
79
80 std::unique_ptr<Internal::RSqliteDSDataSet> fDataSet;
81 unsigned int fNSlots;
83 std::vector<std::string> fColumnNames;
84 std::vector<ETypes> fColumnTypes;
85 /// The data source is inherently single-threaded and returns only one row at a time. This vector holds the results.
86 std::vector<Value_t> fValues;
87
88 // clang-format off
89 /// Corresponds to the types defined in ETypes.
90 static constexpr char const *fgTypeNames[] = {
91 "Long64_t",
92 "double",
93 "std::string",
94 "std::vector<unsigned char>",
95 "void *"
96 };
97 // clang-format on
98
99public:
100 RSqliteDS(const std::string &fileName, const std::string &query);
101 ~RSqliteDS();
102 void SetNSlots(unsigned int nSlots) final;
103 const std::vector<std::string> &GetColumnNames() const final;
104 bool HasColumn(std::string_view colName) const final;
105 std::string GetTypeName(std::string_view colName) const final;
106 std::vector<std::pair<ULong64_t, ULong64_t>> GetEntryRanges() final;
107 bool SetEntry(unsigned int slot, ULong64_t entry) final;
108 void Initialize() final;
109 std::string GetLabel() final;
110
111protected:
112 Record_t GetColumnReadersImpl(std::string_view name, const std::type_info &) final;
113};
114
115RDataFrame FromSqlite(std::string_view fileName, std::string_view query);
116
117} // namespace RDF
118
119} // namespace ROOT
120
121#endif
long long Long64_t
Definition RtypesCore.h:69
unsigned long long ULong64_t
Definition RtypesCore.h:70
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
char name[80]
Definition TGX11.cxx:110
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:51
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:90
std::string GetLabel() final
Return a string representation of the datasource type.
unsigned int fNSlots
Definition RSqliteDS.hxx:81
std::vector< std::string > fColumnNames
Definition RSqliteDS.hxx:83
~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.
void Initialize() final
Resets the SQlite query engine at the beginning of the event loop.
std::vector< ETypes > fColumnTypes
Definition RSqliteDS.hxx:84
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:55
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:80
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:86
ROOT's RDataFrame offers a modern, high-level interface for analysis of data stored in TTree ,...
RDataFrame FromSqlite(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:65
void * fPtr
Points to one of the values; an address to this pointer is returned by GetColumnReadersImpl.
Definition RSqliteDS.hxx:75
std::vector< unsigned char > fBlob
Definition RSqliteDS.hxx:73
bool fIsActive
Not all columns of the query are necessarily used by the RDF. Allows for skipping them.
Definition RSqliteDS.hxx:69