Logo ROOT  
Reference Guide
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 {
57 kInteger,
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 Initialize() 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
@ kText
Definition: Buttons.h:30
long long Long64_t
Definition: RtypesCore.h:80
unsigned long long ULong64_t
Definition: RtypesCore.h:81
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:52
void SetNSlots(unsigned int nSlots) final
Almost a no-op, many slots can in fact reduce the performance due to thread synchronization.
Definition: RSqliteDS.cxx:591
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.
Definition: RSqliteDS.cxx:537
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.
Definition: RSqliteDS.cxx:446
bool HasColumn(std::string_view colName) const final
A linear search through the columns for the given name.
Definition: RSqliteDS.cxx:522
void Initialize() final
Resets the SQlite query engine at the beginning of the event loop.
Definition: RSqliteDS.cxx:529
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.
Definition: RSqliteDS.cxx:508
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.
Definition: RSqliteDS.cxx:466
RSqliteDS(const std::string &fileName, const std::string &query)
Build the dataframe.
Definition: RSqliteDS.cxx:360
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.
Definition: RSqliteDS.cxx:489
const std::vector< std::string > & GetColumnNames() const final
Returns the SELECT queries names.
Definition: RSqliteDS.cxx:459
bool SetEntry(unsigned int slot, ULong64_t entry) final
Stores the result of the current active sqlite query row as a C++ value.
Definition: RSqliteDS.cxx:554
void SqliteError(int errcode)
Helper function to throw an exception if there is a fatal sqlite error, e.g. an I/O error.
Definition: RSqliteDS.cxx:602
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 modern, high-level interface for analysis of data stored in TTree ,...
Definition: RDataFrame.hxx:41
RDataFrame MakeSqliteDataFrame(std::string_view fileName, std::string_view query)
Factory method to create a SQlite RDataFrame.
Definition: RSqliteDS.cxx:546
This file contains a specialised ROOT message handler to test for diagnostic in unit tests.
The state of an open dataset in terms of the sqlite3 C library.
Definition: RSqliteDS.cxx:333
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