OmniSciDB  0fdbebe030
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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 47 of file ExternalExecutor.h.

Constructor & Destructor Documentation

SqliteMemDatabase::SqliteMemDatabase ( const ExternalQueryTable external_query_table)

Definition at line 380 of file ExternalExecutor.cpp.

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

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

Definition at line 388 of file ExternalExecutor.cpp.

References CHECK_EQ, db_, and session_mutex_.

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

Member Function Documentation

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

Definition at line 394 of file ExternalExecutor.cpp.

References CHECK_EQ, db_, and session_mutex_.

Referenced by run_query_external().

394  {
395  std::lock_guard session_lock(session_mutex_);
396  char* msg;
397  int status = sqlite3_exec(db_, sql.c_str(), nullptr, nullptr, &msg);
398  CHECK_EQ(status, SQLITE_OK);
399 }
#define CHECK_EQ(x, y)
Definition: Logger.h:205
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 415 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, num_rows, SqliteConnector::query(), ExternalQueryOutputSpec::query_mem_desc, query_mem_desc, and ExternalQueryOutputSpec::target_infos.

Referenced by run_query_external().

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

+ 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 58 of file ExternalExecutor.h.

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

ExternalQueryTable SqliteMemDatabase::external_query_table_
private

Definition at line 59 of file ExternalExecutor.h.

Referenced by SqliteMemDatabase().

std::mutex SqliteMemDatabase::session_mutex_
staticprivate

Definition at line 60 of file ExternalExecutor.h.

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


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