Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RSqliteDS.cxx
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#include <ROOT/RSqliteDS.hxx>
12#include <ROOT/RRawFile.hxx>
13
14#include "TError.h"
15#include "TRandom.h"
16#include "TSystem.h"
17
18#include <algorithm>
19#include <cctype>
20#include <cerrno>
21#include <cstring> // for memcpy
22#include <ctime>
23#include <memory> // for placement new
24#include <stdexcept>
25#include <utility>
26
27#include <sqlite3.h>
28
29namespace {
30
31// In order to provide direct access to remote sqlite files through HTTP and HTTPS, this datasource provides a custom
32// "SQlite VFS module" that uses Davix for data access. The SQlite VFS modules are roughly what TSystem is
33// for ROOT -- an abstraction of the operating system interface.
34//
35// SQlite allows for registering custom VFS modules, which are a set of C callback functions that SQlite invokes when
36// it needs to read from a file, write to a file, etc. More information is available under https://sqlite.org/vfs.html
37//
38// In the context of a data source, SQlite will only ever call reading functions from the VFS module, the sqlite
39// files are not modified. Therefore, only a subset of the callback functions provide a non-trivial implementation.
40// The custom VFS module uses a RRawFile for the byte access, thereby it can access local and remote files.
41
42////////////////////////////////////////////////////////////////////////////
43/// SQlite VFS modules are identified by string names. The name has to be unique for the entire application.
44constexpr char const *gSQliteVfsName = "ROOT-Davix-readonly";
45
46////////////////////////////////////////////////////////////////////////////
47/// Holds the state of an open sqlite database. Objects of this struct are created in VfsRdOnlyOpen()
48/// and then passed by sqlite to the file I/O callbacks (read, close, etc.). This uses C style inheritance
49/// where the struct starts with a sqlite3_file member (base class) which is extended by members related to
50/// this particular VFS module. Every callback here thus casts the sqlite3_file input parameter to its "derived"
51/// type VfsRootFile.
52struct VfsRootFile {
53 VfsRootFile() = default;
54
55 sqlite3_file pFile;
56 std::unique_ptr<ROOT::Internal::RRawFile> fRawFile;
57};
58
59// The following callbacks implement the I/O operations of an open database
60
61////////////////////////////////////////////////////////////////////////////
62/// Releases the resources associated to a file opened with davix
63int VfsRdOnlyClose(sqlite3_file *pFile)
64{
65 VfsRootFile *p = reinterpret_cast<VfsRootFile *>(pFile);
66 // We can't use delete because the storage for p is managed by sqlite
67 p->~VfsRootFile();
68 return SQLITE_OK;
69}
70
71////////////////////////////////////////////////////////////////////////////
72/// Issues a byte range request for a chunk to the raw file
73int VfsRdOnlyRead(sqlite3_file *pFile, void *zBuf, int count, sqlite_int64 offset)
74{
75 VfsRootFile *p = reinterpret_cast<VfsRootFile *>(pFile);
76 auto nbytes = p->fRawFile->ReadAt(zBuf, count, offset);
77 return (nbytes != static_cast<unsigned int>(count)) ? SQLITE_IOERR : SQLITE_OK;
78}
79
80////////////////////////////////////////////////////////////////////////////
81/// We do not write to a database in the RDataSource and therefore can simply return an error for this callback
82int VfsRdOnlyWrite(sqlite3_file * /*pFile*/, const void * /*zBuf*/, int /*iAmt*/, sqlite_int64 /*iOfst*/)
83{
84 return SQLITE_OPEN_READONLY;
85}
86
87////////////////////////////////////////////////////////////////////////////
88/// We do not write to a database in the RDataSource and therefore can simply return an error for this callback
89int VfsRdOnlyTruncate(sqlite3_file * /*pFile*/, sqlite_int64 /*size*/)
90{
91 return SQLITE_OPEN_READONLY;
92}
93
94////////////////////////////////////////////////////////////////////////////
95/// As the database is read-only, syncing data to disc is a no-op and always succeeds
96int VfsRdOnlySync(sqlite3_file * /*pFile*/, int /*flags*/)
97{
98 return SQLITE_OK;
99}
100
101////////////////////////////////////////////////////////////////////////////
102/// Returns the cached file size
103int VfsRdOnlyFileSize(sqlite3_file *pFile, sqlite_int64 *pSize)
104{
105 VfsRootFile *p = reinterpret_cast<VfsRootFile *>(pFile);
106 *pSize = p->fRawFile->GetSize();
107 return SQLITE_OK;
108}
109
110////////////////////////////////////////////////////////////////////////////
111/// As the database is read-only, locks for concurrent access are no-ops and always succeeds
112int VfsRdOnlyLock(sqlite3_file * /*pFile*/, int /*level*/)
113{
114 return SQLITE_OK;
115}
116
117////////////////////////////////////////////////////////////////////////////
118/// As the database is read-only, locks for concurrent access are no-ops and always succeeds
119int VfsRdOnlyUnlock(sqlite3_file * /*pFile*/, int /*level*/)
120{
121 return SQLITE_OK;
122}
123
124////////////////////////////////////////////////////////////////////////////
125/// As the database is read-only, locks for concurrent access are no-ops and always succeeds
126int VfsRdOnlyCheckReservedLock(sqlite3_file * /*pFile*/, int *pResOut)
127{
128 *pResOut = 0;
129 return SQLITE_OK;
130}
131
132////////////////////////////////////////////////////////////////////////////
133/// As the database is read-only, we know there are no additional control files such as a database journal
134int VfsRdOnlyFileControl(sqlite3_file * /*p*/, int /*op*/, void * /*pArg*/)
135{
136 return SQLITE_NOTFOUND;
137}
138
139////////////////////////////////////////////////////////////////////////////
140/// The database device's sector size is only needed for writing
141int VfsRdOnlySectorSize(sqlite3_file * /*pFile*/)
142{
143 return SQLITE_OPEN_READONLY;
144}
145
146////////////////////////////////////////////////////////////////////////////
147/// The database device's properties are only needed for writing
148int VfsRdOnlyDeviceCharacteristics(sqlite3_file * /*pFile*/)
149{
150 return SQLITE_OPEN_READONLY;
151}
152
153////////////////////////////////////////////////////////////////////////////
154/// Set the function pointers of the custom VFS I/O operations in a
155/// forward-compatible way
156sqlite3_io_methods GetSqlite3IoMethods()
157{
158 // The C style initialization is compatible with version 1 and later versions of the struct.
159 // Version 1 was introduced with sqlite 3.6, version 2 with sqlite 3.7.8, version 3 with sqlite 3.7.17
160 sqlite3_io_methods io_methods;
161 memset(&io_methods, 0, sizeof(io_methods));
162 io_methods.iVersion = 1;
163 io_methods.xClose = VfsRdOnlyClose;
164 io_methods.xRead = VfsRdOnlyRead;
165 io_methods.xWrite = VfsRdOnlyWrite;
166 io_methods.xTruncate = VfsRdOnlyTruncate;
167 io_methods.xSync = VfsRdOnlySync;
168 io_methods.xFileSize = VfsRdOnlyFileSize;
169 io_methods.xLock = VfsRdOnlyLock;
170 io_methods.xUnlock = VfsRdOnlyUnlock;
171 io_methods.xCheckReservedLock = VfsRdOnlyCheckReservedLock;
172 io_methods.xFileControl = VfsRdOnlyFileControl;
173 io_methods.xSectorSize = VfsRdOnlySectorSize;
174 io_methods.xDeviceCharacteristics = VfsRdOnlyDeviceCharacteristics;
175 return io_methods;
176}
177
178////////////////////////////////////////////////////////////////////////////
179/// Fills a new VfsRootFile struct enclosing a Davix file
180int VfsRdOnlyOpen(sqlite3_vfs * /*vfs*/, const char *zName, sqlite3_file *pFile, int flags, int * /*pOutFlags*/)
181{
182 // Storage for the VfsRootFile structure has been already allocated by sqlite, so we use placement new
183 VfsRootFile *p = new (pFile) VfsRootFile();
184 p->pFile.pMethods = nullptr;
185
186 // This global struct contains the function pointers to all the callback operations that act on an open database.
187 // It is passed via the pFile struct back to sqlite so that it can call back to the functions provided above.
188 static const sqlite3_io_methods io_methods = GetSqlite3IoMethods();
189
190 if (flags & (SQLITE_OPEN_READWRITE | SQLITE_OPEN_DELETEONCLOSE | SQLITE_OPEN_EXCLUSIVE))
191 return SQLITE_IOERR;
192
193 p->fRawFile = ROOT::Internal::RRawFile::Create(zName);
194 if (!p->fRawFile) {
195 ::Error("VfsRdOnlyOpen", "Cannot open %s\n", zName);
196 return SQLITE_IOERR;
197 }
198
199 if (!(p->fRawFile->GetFeatures() & ROOT::Internal::RRawFile::kFeatureHasSize)) {
200 ::Error("VfsRdOnlyOpen", "cannot determine file size of %s\n", zName);
201 return SQLITE_IOERR;
202 }
203
204 p->pFile.pMethods = &io_methods;
205 return SQLITE_OK;
206}
207
208// The following callbacks implement operating system specific functionality. In contrast to the previous callbacks,
209// there is no need to implement any customized logic for the following ones. An implementation has to be
210// provided nevertheless to have a fully functional VFS module.
211
212////////////////////////////////////////////////////////////////////////////
213/// This VFS module cannot remove files
214int VfsRdOnlyDelete(sqlite3_vfs * /*vfs*/, const char * /*zName*/, int /*syncDir*/)
215{
216 return SQLITE_IOERR_DELETE;
217}
218
219////////////////////////////////////////////////////////////////////////////
220/// Access control always allows read-only access to databases
221int VfsRdOnlyAccess(sqlite3_vfs * /*vfs*/, const char * /*zPath*/, int flags, int *pResOut)
222{
223 *pResOut = 0;
224 if (flags == SQLITE_ACCESS_READWRITE) {
225 return SQLITE_OPEN_READONLY;
226 }
227 return SQLITE_OK;
228}
229
230////////////////////////////////////////////////////////////////////////////
231/// No distinction between relative and full paths for URLs, returns the input path name
232int VfsRdOnlyFullPathname(sqlite3_vfs * /*vfs*/, const char *zPath, int nOut, char *zOut)
233{
234 zOut[nOut - 1] = '\0';
235 sqlite3_snprintf(nOut, zOut, "%s", zPath);
236 return SQLITE_OK;
237}
238
239////////////////////////////////////////////////////////////////////////////
240/// Let TRandom fill the buffer with random bytes
241int VfsRdOnlyRandomness(sqlite3_vfs * /*vfs*/, int nBuf, char *zBuf)
242{
243 for (int i = 0; i < nBuf; ++i) {
244 zBuf[i] = (char)gRandom->Integer(256);
245 }
246 return nBuf;
247}
248
249////////////////////////////////////////////////////////////////////////////
250/// Use ROOT's platform independent sleep wrapper
251int VfsRdOnlySleep(sqlite3_vfs * /*vfs*/, int microseconds)
252{
253 // Millisecond precision but sleep at least number of given microseconds as requested
254 gSystem->Sleep((microseconds + 1000 - 1) / 1000);
255 return microseconds;
256}
257
258////////////////////////////////////////////////////////////////////////////
259/// Use sqlite default implementation
260int VfsRdOnlyGetLastError(sqlite3_vfs * /*vfs*/, int /*not_used1*/, char * /*not_used2*/)
261{
262 return errno;
263}
264
265////////////////////////////////////////////////////////////////////////////
266/// Return UTC as being done in the sqlite unix VFS without gettimeofday()
267int VfsRdOnlyCurrentTimeInt64(sqlite3_vfs * /*vfs*/, sqlite3_int64 *piNow)
268{
269 static constexpr sqlite3_int64 unixEpoch = 24405875 * (sqlite3_int64)8640000;
270 time_t t;
271 time(&t);
272 *piNow = ((sqlite3_int64)t) * 1000 + unixEpoch;
273 return SQLITE_OK;
274}
275
276////////////////////////////////////////////////////////////////////////////
277/// Wrapper around VfsRdOnlyCurrentTimeInt64
278int VfsRdOnlyCurrentTime(sqlite3_vfs *vfs, double *prNow)
279{
280 sqlite3_int64 i = 0;
281 int rc = VfsRdOnlyCurrentTimeInt64(vfs, &i);
282 *prNow = i / 86400000.0;
283 return rc;
284}
285
286////////////////////////////////////////////////////////////////////////////
287/// Set the function pointers of the VFS implementation in a
288/// forward-compatible way
289sqlite3_vfs GetSqlite3Vfs()
290{
291 // The C style initialization is compatible with version 1 and later versions of the struct.
292 // Version 1 was introduced with sqlite 3.5, version 2 with sqlite 3.7, version 3 with sqlite 3.7.6
293 sqlite3_vfs vfs;
294 memset(&vfs, 0, sizeof(vfs));
295 vfs.iVersion = 1;
296 vfs.szOsFile = sizeof(VfsRootFile);
297 vfs.mxPathname = 2000;
298 vfs.zName = gSQliteVfsName;
299 vfs.xOpen = VfsRdOnlyOpen;
300 vfs.xDelete = VfsRdOnlyDelete;
301 vfs.xAccess = VfsRdOnlyAccess;
302 vfs.xFullPathname = VfsRdOnlyFullPathname;
303 vfs.xRandomness = VfsRdOnlyRandomness;
304 vfs.xSleep = VfsRdOnlySleep;
305 vfs.xCurrentTime = VfsRdOnlyCurrentTime;
306 vfs.xGetLastError = VfsRdOnlyGetLastError;
307 return vfs;
308}
309
310////////////////////////////////////////////////////////////////////////////
311/// A global struct of function pointers and details on the VfsRootFile class that together constitue a VFS module
312struct sqlite3_vfs kSqlite3Vfs = GetSqlite3Vfs();
313
314bool RegisterSqliteVfs()
315{
316 int retval;
317 retval = sqlite3_vfs_register(&kSqlite3Vfs, false);
318 return (retval == SQLITE_OK);
319}
320
321} // anonymous namespace
322
323namespace ROOT {
324
325namespace RDF {
326
327namespace Internal {
328////////////////////////////////////////////////////////////////////////////
329/// The state of an open dataset in terms of the sqlite3 C library.
331 sqlite3 *fDb = nullptr;
332 sqlite3_stmt *fQuery = nullptr;
333};
334}
335
337 : fType(type), fIsActive(false), fInteger(0), fReal(0.0), fText(), fBlob(), fNull(nullptr)
338{
339 switch (type) {
340 case ETypes::kInteger: fPtr = &fInteger; break;
341 case ETypes::kReal: fPtr = &fReal; break;
342 case ETypes::kText: fPtr = &fText; break;
343 case ETypes::kBlob: fPtr = &fBlob; break;
344 case ETypes::kNull: fPtr = &fNull; break;
345 default: throw std::runtime_error("Internal error");
346 }
347}
348
349constexpr char const *RSqliteDS::fgTypeNames[];
350
351////////////////////////////////////////////////////////////////////////////
352/// \brief Build the dataframe
353/// \param[in] fileName The path to an sqlite3 file, will be opened read-only
354/// \param[in] query A valid sqlite3 SELECT query
355///
356/// The constructor opens the sqlite file, prepares the query engine and determines the column names and types.
357RSqliteDS::RSqliteDS(const std::string &fileName, const std::string &query)
358 : fDataSet(std::make_unique<Internal::RSqliteDSDataSet>()), fNSlots(0), fNRow(0)
359{
360 static bool hasSqliteVfs = RegisterSqliteVfs();
361 if (!hasSqliteVfs)
362 throw std::runtime_error("Cannot register SQlite VFS in RSqliteDS");
363
364 int retval;
365
366 retval = sqlite3_open_v2(fileName.c_str(), &fDataSet->fDb, SQLITE_OPEN_READONLY | SQLITE_OPEN_NOMUTEX,
367 gSQliteVfsName);
368 if (retval != SQLITE_OK)
369 SqliteError(retval);
370
371 // Certain complex queries trigger creation of temporary tables. Depending on the build options of sqlite,
372 // sqlite may try to store such temporary tables on disk, using our custom VFS module to do so.
373 // Creation of new database files, however, is not supported by the custom VFS module. Thus we set the behavior
374 // of the database connection to "temp_store=2", meaning that temporary tables should always be maintained
375 // in memory.
376 retval = sqlite3_exec(fDataSet->fDb, "PRAGMA temp_store=2;", nullptr, nullptr, nullptr);
377 if (retval != SQLITE_OK)
378 SqliteError(retval);
379
380 retval = sqlite3_prepare_v2(fDataSet->fDb, query.c_str(), -1, &fDataSet->fQuery, nullptr);
381 if (retval != SQLITE_OK)
382 SqliteError(retval);
383
384 int colCount = sqlite3_column_count(fDataSet->fQuery);
385 retval = sqlite3_step(fDataSet->fQuery);
386 if ((retval != SQLITE_ROW) && (retval != SQLITE_DONE))
387 SqliteError(retval);
388
389 fValues.reserve(colCount);
390 for (int i = 0; i < colCount; ++i) {
391 fColumnNames.emplace_back(sqlite3_column_name(fDataSet->fQuery, i));
392 int type = SQLITE_NULL;
393 // Try first with the declared column type and then with the dynamic type
394 // for expressions
395 const char *declTypeCstr = sqlite3_column_decltype(fDataSet->fQuery, i);
396 if (declTypeCstr == nullptr) {
397 if (retval == SQLITE_ROW)
398 type = sqlite3_column_type(fDataSet->fQuery, i);
399 } else {
400 std::string declType(declTypeCstr);
401 std::transform(declType.begin(), declType.end(), declType.begin(), ::toupper);
402 if (declType == "INTEGER")
403 type = SQLITE_INTEGER;
404 else if (declType == "FLOAT")
405 type = SQLITE_FLOAT;
406 else if (declType == "TEXT")
407 type = SQLITE_TEXT;
408 else if (declType == "BLOB")
409 type = SQLITE_BLOB;
410 else
411 throw std::runtime_error("Unexpected column decl type");
412 }
413
414 switch (type) {
415 case SQLITE_INTEGER:
417 fValues.emplace_back(ETypes::kInteger);
418 break;
419 case SQLITE_FLOAT:
420 fColumnTypes.push_back(ETypes::kReal);
421 fValues.emplace_back(ETypes::kReal);
422 break;
423 case SQLITE_TEXT:
424 fColumnTypes.push_back(ETypes::kText);
425 fValues.emplace_back(ETypes::kText);
426 break;
427 case SQLITE_BLOB:
428 fColumnTypes.push_back(ETypes::kBlob);
429 fValues.emplace_back(ETypes::kBlob);
430 break;
431 case SQLITE_NULL:
432 // TODO: Null values in first rows are not well handled
433 fColumnTypes.push_back(ETypes::kNull);
434 fValues.emplace_back(ETypes::kNull);
435 break;
436 default: throw std::runtime_error("Unhandled data type");
437 }
438 }
439}
440
441////////////////////////////////////////////////////////////////////////////
442/// Frees the sqlite resources and closes the file.
444{
445 // sqlite3_finalize returns the error code of the most recent operation on fQuery.
446 sqlite3_finalize(fDataSet->fQuery);
447 // Closing can possibly fail with SQLITE_BUSY, in which case resources are leaked. This should not happen
448 // the way it is used in this class because we cleanup the prepared statement before.
449 sqlite3_close(fDataSet->fDb);
450}
451
452////////////////////////////////////////////////////////////////////////////
453/// Returns the SELECT queries names. The column names have been cached in the constructor.
454/// For expressions, the column name is the string of the expression unless the query defines a column name with as
455/// like in "SELECT 1 + 1 as mycolumn FROM table"
456const std::vector<std::string> &RSqliteDS::GetColumnNames() const
457{
458 return fColumnNames;
459}
460
461////////////////////////////////////////////////////////////////////////////
462/// Activates the given column's result value.
463RDataSource::Record_t RSqliteDS::GetColumnReadersImpl(std::string_view name, const std::type_info &ti)
464{
465 const auto index = std::distance(fColumnNames.begin(), std::find(fColumnNames.begin(), fColumnNames.end(), name));
466 const auto type = fColumnTypes[index];
467
468 if ((type == ETypes::kInteger && typeid(Long64_t) != ti) || (type == ETypes::kReal && typeid(double) != ti) ||
469 (type == ETypes::kText && typeid(std::string) != ti) ||
470 (type == ETypes::kBlob && typeid(std::vector<unsigned char>) != ti) ||
471 (type == ETypes::kNull && typeid(void *) != ti)) {
472 std::string errmsg = "The type selected for column \"";
473 errmsg += name;
474 errmsg += "\" does not correspond to column type, which is ";
475 errmsg += GetTypeName(name);
476 throw std::runtime_error(errmsg);
477 }
478
479 fValues[index].fIsActive = true;
480 return std::vector<void *>{fNSlots, &fValues[index].fPtr};
481}
482
483////////////////////////////////////////////////////////////////////////////
484/// Returns a range of size 1 as long as more rows are available in the SQL result set.
485/// This inherently serialized the RDF independent of the number of slots.
486std::vector<std::pair<ULong64_t, ULong64_t>> RSqliteDS::GetEntryRanges()
487{
488 std::vector<std::pair<ULong64_t, ULong64_t>> entryRanges;
489 int retval = sqlite3_step(fDataSet->fQuery);
490 switch (retval) {
491 case SQLITE_DONE: return entryRanges;
492 case SQLITE_ROW:
493 entryRanges.emplace_back(fNRow, fNRow + 1);
494 fNRow++;
495 return entryRanges;
496 default:
497 SqliteError(retval);
498 // Never here
499 abort();
500 }
501}
502
503////////////////////////////////////////////////////////////////////////////
504/// Returns the C++ type for a given column name, implemented as a linear search through all the columns.
505std::string RSqliteDS::GetTypeName(std::string_view colName) const
506{
507 unsigned N = fColumnNames.size();
508
509 for (unsigned i = 0; i < N; ++i) {
510 if (colName == fColumnNames[i]) {
511 return fgTypeNames[static_cast<int>(fColumnTypes[i])];
512 }
513 }
514 throw std::runtime_error("Unknown column: " + std::string(colName));
515}
516
517////////////////////////////////////////////////////////////////////////////
518/// A linear search through the columns for the given name
519bool RSqliteDS::HasColumn(std::string_view colName) const
520{
521 return std::find(fColumnNames.begin(), fColumnNames.end(), colName) != fColumnNames.end();
522}
523
524////////////////////////////////////////////////////////////////////////////
525/// Resets the SQlite query engine at the beginning of the event loop.
527{
528 fNRow = 0;
529 int retval = sqlite3_reset(fDataSet->fQuery);
530 if (retval != SQLITE_OK)
531 throw std::runtime_error("SQlite error, reset");
532}
533
535{
536 return "RSqliteDS";
537}
538
539////////////////////////////////////////////////////////////////////////////////////////////////
540/// \brief Factory method to create a SQlite RDataFrame.
541/// \param[in] fileName Path of the sqlite file.
542/// \param[in] query SQL query that defines the data set.
543RDataFrame FromSqlite(std::string_view fileName, std::string_view query)
544{
545 ROOT::RDataFrame rdf(std::make_unique<RSqliteDS>(std::string(fileName), std::string(query)));
546 return rdf;
547}
548
549////////////////////////////////////////////////////////////////////////////
550/// Stores the result of the current active sqlite query row as a C++ value.
551bool RSqliteDS::SetEntry(unsigned int /* slot */, ULong64_t entry)
552{
553 assert(entry + 1 == fNRow);
554 (void)entry;
555 unsigned N = fValues.size();
556 for (unsigned i = 0; i < N; ++i) {
557 if (!fValues[i].fIsActive)
558 continue;
559
560 int nbytes;
561 switch (fValues[i].fType) {
562 case ETypes::kInteger: fValues[i].fInteger = sqlite3_column_int64(fDataSet->fQuery, i); break;
563 case ETypes::kReal: fValues[i].fReal = sqlite3_column_double(fDataSet->fQuery, i); break;
564 case ETypes::kText:
565 nbytes = sqlite3_column_bytes(fDataSet->fQuery, i);
566 if (nbytes == 0) {
567 fValues[i].fText = "";
568 } else {
569 fValues[i].fText = reinterpret_cast<const char *>(sqlite3_column_text(fDataSet->fQuery, i));
570 }
571 break;
572 case ETypes::kBlob:
573 nbytes = sqlite3_column_bytes(fDataSet->fQuery, i);
574 fValues[i].fBlob.resize(nbytes);
575 if (nbytes > 0) {
576 std::memcpy(fValues[i].fBlob.data(), sqlite3_column_blob(fDataSet->fQuery, i), nbytes);
577 }
578 break;
579 case ETypes::kNull: break;
580 default: throw std::runtime_error("Unhandled column type");
581 }
582 }
583 return true;
584}
585
586////////////////////////////////////////////////////////////////////////////////////////////////
587/// Almost a no-op, many slots can in fact reduce the performance due to thread synchronization.
588void RSqliteDS::SetNSlots(unsigned int nSlots)
589{
590 if (nSlots > 1) {
591 ::Warning("SetNSlots", "Currently the SQlite data source faces performance degradation in multi-threaded mode. "
592 "Consider turning off IMT.");
593 }
594 fNSlots = nSlots;
595}
596
597////////////////////////////////////////////////////////////////////////////////////////////////
598/// Helper function to throw an exception if there is a fatal sqlite error, e.g. an I/O error.
599void RSqliteDS::SqliteError(int errcode)
600{
601 std::string errmsg = "SQlite error: ";
602#if SQLITE_VERSION_NUMBER < 3007015
603 errmsg += std::to_string(errcode);
604#else
605 errmsg += sqlite3_errstr(errcode);
606#endif
607 throw std::runtime_error(errmsg);
608}
609
610} // namespace RDF
611
612} // namespace ROOT
long long Long64_t
Definition RtypesCore.h:80
unsigned long long ULong64_t
Definition RtypesCore.h:81
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
Definition TError.cxx:185
void Warning(const char *location, const char *msgfmt,...)
Use this function in warning situations.
Definition TError.cxx:229
#define N
winID h TVirtualViewer3D TVirtualGLPainter p
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 offset
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t index
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
R__EXTERN TRandom * gRandom
Definition TRandom.h:62
R__EXTERN TSystem * gSystem
Definition TSystem.h:555
static std::unique_ptr< RRawFile > Create(std::string_view url, ROptions options=ROptions())
Factory method that returns a suitable concrete implementation according to the transport in the url.
Definition RRawFile.cxx:73
static constexpr int kFeatureHasSize
GetSize() does not return kUnknownFileSize.
Definition RRawFile.hxx:52
std::vector< void * > Record_t
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.
RSqliteDS(const std::string &fileName, const std::string &query)
Build the dataframe.
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 ,...
virtual UInt_t Integer(UInt_t imax)
Returns a random integer uniformly distributed on the interval [ 0, imax-1 ].
Definition TRandom.cxx:361
virtual void Sleep(UInt_t milliSec)
Sleep milliSec milli seconds.
Definition TSystem.cxx:437
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.
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