OmniSciDB  d2f719934e
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SqliteMemDatabase Class Reference

#include <ExternalExecutor.h>

+ Collaboration diagram for SqliteMemDatabase:

Public Member Functions

 SqliteMemDatabase (const ExternalQueryTable &external_query_table)
 
 ~SqliteMemDatabase ()
 
void run (const std::string &sql)
 
std::unique_ptr< ResultSetrunSelect (const std::string &sql, const ExternalQueryOutputSpec &output_spec)
 

Private Attributes

sqlite3 * db_
 
ExternalQueryTable external_query_table_
 

Static Private Attributes

static std::mutex session_mutex_
 

Detailed Description

Definition at line 48 of file ExternalExecutor.h.

Constructor & Destructor Documentation

SqliteMemDatabase::SqliteMemDatabase ( const ExternalQueryTable external_query_table)

Definition at line 381 of file ExternalExecutor.cpp.

References CHECK_EQ, db_, external_query_table_, and anonymous_namespace{ExternalExecutor.cpp}::omnisci_module.

382  : external_query_table_(external_query_table) {
383  int status = sqlite3_open(":memory:", &db_);
384  CHECK_EQ(status, SQLITE_OK);
385  status = sqlite3_create_module(db_, "omnisci", &omnisci_module, &external_query_table_);
386  CHECK_EQ(status, SQLITE_OK);
387 }
#define CHECK_EQ(x, y)
Definition: Logger.h:219
ExternalQueryTable external_query_table_
SqliteMemDatabase::~SqliteMemDatabase ( )

Definition at line 389 of file ExternalExecutor.cpp.

References CHECK_EQ, db_, and session_mutex_.

389  {
390  std::lock_guard session_lock(session_mutex_);
391  int status = sqlite3_close(db_);
392  CHECK_EQ(status, SQLITE_OK);
393 }
#define CHECK_EQ(x, y)
Definition: Logger.h:219
static std::mutex session_mutex_

Member Function Documentation

void SqliteMemDatabase::run ( const std::string &  sql)

Definition at line 395 of file ExternalExecutor.cpp.

References CHECK_EQ, db_, and session_mutex_.

Referenced by run_query_external().

395  {
396  std::lock_guard session_lock(session_mutex_);
397  char* msg;
398  int status = sqlite3_exec(db_, sql.c_str(), nullptr, nullptr, &msg);
399  CHECK_EQ(status, SQLITE_OK);
400 }
#define CHECK_EQ(x, y)
Definition: Logger.h:219
static std::mutex session_mutex_

+ Here is the caller graph for this function:

std::unique_ptr< ResultSet > SqliteMemDatabase::runSelect ( const std::string &  sql,
const ExternalQueryOutputSpec output_spec 
)

Definition at line 416 of file ExternalExecutor.cpp.

References CHECK, CHECK_EQ, SqliteConnector::columnTypes, CPU, db_, ExternalQueryOutputSpec::executor, logger::FATAL, get_scan_output_slot(), SqliteConnector::getData(), SqliteConnector::getNumCols(), SqliteConnector::getNumRows(), inline_fp_null_value< double >(), inline_fp_null_value< float >(), inline_int_max_min(), inline_int_null_val(), SqliteConnector::isNull(), kBIGINT, kBOOLEAN, kCHAR, kDOUBLE, kFLOAT, kINT, kSMALLINT, kTEXT, kTINYINT, kVARCHAR, LOG, SqliteConnector::query(), ExternalQueryOutputSpec::query_mem_desc, query_mem_desc, and ExternalQueryOutputSpec::target_infos.

Referenced by run_query_external().

418  {
419  SqliteConnector connector(db_);
420  connector.query(sql);
421  auto query_mem_desc = output_spec.query_mem_desc;
422  const auto num_rows = connector.getNumRows();
423  query_mem_desc.setEntryCount(num_rows);
424  auto rs = std::make_unique<ResultSet>(output_spec.target_infos,
427  output_spec.executor->getRowSetMemoryOwner(),
428  nullptr,
429  0,
430  0);
431  const auto storage = rs->allocateStorage();
432  auto output_buffer = storage->getUnderlyingBuffer();
433  CHECK(!num_rows || output_buffer);
434  for (size_t row_idx = 0; row_idx < num_rows; ++row_idx) {
435  auto row = get_scan_output_slot(reinterpret_cast<int64_t*>(output_buffer),
436  num_rows,
437  row_idx,
438  query_mem_desc.getRowSize() / sizeof(int64_t));
439  CHECK_EQ(output_spec.target_infos.size(), connector.getNumCols());
440  size_t slot_idx = 0;
441  for (size_t col_idx = 0; col_idx < connector.getNumCols(); ++col_idx, ++slot_idx) {
442  const auto& col_type = output_spec.target_infos[col_idx].sql_type;
443  const int sqlite_col_type = connector.columnTypes[col_idx];
444  switch (col_type.get_type()) {
445  case kBOOLEAN:
446  case kTINYINT:
447  case kSMALLINT:
448  case kINT:
449  case kBIGINT: {
450  static const std::string overflow_message{"Overflow or underflow"};
451  if (sqlite_col_type != SQLITE_INTEGER && sqlite_col_type != SQLITE_NULL) {
452  throw std::runtime_error(overflow_message);
453  }
454  if (!connector.isNull(row_idx, col_idx)) {
455  const auto limits = inline_int_max_min(col_type.get_logical_size());
456  const auto val = connector.getData<int64_t>(row_idx, col_idx);
457  if (val > limits.first || val < limits.second) {
458  throw std::runtime_error(overflow_message);
459  }
460  row[slot_idx] = val;
461  } else {
462  row[slot_idx] = inline_int_null_val(col_type);
463  }
464  break;
465  }
466  case kFLOAT: {
467  CHECK(sqlite_col_type == SQLITE_FLOAT || sqlite_col_type == SQLITE_NULL);
468  if (!connector.isNull(row_idx, col_idx)) {
469  reinterpret_cast<double*>(row)[slot_idx] =
470  connector.getData<double>(row_idx, col_idx);
471  } else {
472  reinterpret_cast<double*>(row)[slot_idx] = inline_fp_null_value<float>();
473  }
474  break;
475  }
476  case kDOUBLE: {
477  CHECK(sqlite_col_type == SQLITE_FLOAT || sqlite_col_type == SQLITE_NULL);
478  if (!connector.isNull(row_idx, col_idx)) {
479  reinterpret_cast<double*>(row)[slot_idx] =
480  connector.getData<double>(row_idx, col_idx);
481  } else {
482  reinterpret_cast<double*>(row)[slot_idx] = inline_fp_null_value<double>();
483  }
484  break;
485  }
486  case kCHAR:
487  case kTEXT:
488  case kVARCHAR: {
489  CHECK(sqlite_col_type == SQLITE_TEXT || sqlite_col_type == SQLITE_NULL);
490  if (!connector.isNull(row_idx, col_idx)) {
491  const auto str = connector.getData<std::string>(row_idx, col_idx);
492  const auto owned_str =
493  output_spec.executor->getRowSetMemoryOwner()->addString(str);
494  row[slot_idx] = reinterpret_cast<int64_t>(owned_str->c_str());
495  row[++slot_idx] = str.size();
496  } else {
497  row[slot_idx] = 0;
498  row[++slot_idx] = 0;
499  }
500  break;
501  }
502  default: {
503  LOG(FATAL) << "Unexpected type: " << col_type.get_type_name();
504  break;
505  }
506  }
507  }
508  }
509  return rs;
510 }
#define CHECK_EQ(x, y)
Definition: Logger.h:219
RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t * get_scan_output_slot(int64_t *output_buffer, const uint32_t output_buffer_entry_count, const uint32_t pos, const int64_t offset_in_fragment, const uint32_t row_size_quad)
#define LOG(tag)
Definition: Logger.h:205
const Executor * executor
std::vector< TargetInfo > target_infos
Definition: sqltypes.h:52
QueryMemoryDescriptor query_mem_desc
constexpr float inline_fp_null_value< float >()
constexpr double inline_fp_null_value< double >()
Definition: sqltypes.h:41
#define CHECK(condition)
Definition: Logger.h:211
int64_t inline_int_null_val(const SQL_TYPE_INFO &ti)
std::pair< int64_t, int64_t > inline_int_max_min(const size_t byte_width)
Definition: sqltypes.h:45

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Member Data Documentation

sqlite3* SqliteMemDatabase::db_
private

Definition at line 59 of file ExternalExecutor.h.

Referenced by run(), runSelect(), SqliteMemDatabase(), and ~SqliteMemDatabase().

ExternalQueryTable SqliteMemDatabase::external_query_table_
private

Definition at line 60 of file ExternalExecutor.h.

Referenced by SqliteMemDatabase().

std::mutex SqliteMemDatabase::session_mutex_
staticprivate

Definition at line 61 of file ExternalExecutor.h.

Referenced by run(), and ~SqliteMemDatabase().


The documentation for this class was generated from the following files: