OmniSciDB  a5dc49c757
 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 384 of file ExternalExecutor.cpp.

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

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

Definition at line 392 of file ExternalExecutor.cpp.

References CHECK_EQ, db_, and session_mutex_.

392  {
393  std::lock_guard session_lock(session_mutex_);
394  int status = sqlite3_close(db_);
395  CHECK_EQ(status, SQLITE_OK);
396 }
std::lock_guard< T > lock_guard
#define CHECK_EQ(x, y)
Definition: Logger.h:301
static std::mutex session_mutex_

Member Function Documentation

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

Definition at line 398 of file ExternalExecutor.cpp.

References CHECK_EQ, db_, and session_mutex_.

Referenced by run_query_external().

398  {
399  std::lock_guard session_lock(session_mutex_);
400  char* msg;
401  int status = sqlite3_exec(db_, sql.c_str(), nullptr, nullptr, &msg);
402  CHECK_EQ(status, SQLITE_OK);
403 }
std::lock_guard< T > lock_guard
#define CHECK_EQ(x, y)
Definition: Logger.h:301
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 419 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().

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

+ 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: