OmniSciDB  2e3a973ef4
anonymous_namespace{ExternalExecutor.cpp} Namespace Reference

Classes

struct  DecodedString
 
struct  OmniSciCursor
 
struct  OmniSciVtab
 

Functions

int vt_destructor (sqlite3_vtab *pVtab)
 
int vt_create (sqlite3 *db, void *p_aux, int argc, const char *const *argv, sqlite3_vtab **pp_vt, char **pzErr)
 
int vt_connect (sqlite3 *db, void *p_aux, int argc, const char *const *argv, sqlite3_vtab **pp_vt, char **pzErr)
 
int vt_disconnect (sqlite3_vtab *pVtab)
 
int vt_destroy (sqlite3_vtab *pVtab)
 
int vt_open (sqlite3_vtab *pVTab, sqlite3_vtab_cursor **pp_cursor)
 
int vt_close (sqlite3_vtab_cursor *cur)
 
int vt_eof (sqlite3_vtab_cursor *cur)
 
int64_t get_num_rows (OmniSciCursor *p_cur)
 
int vt_next (sqlite3_vtab_cursor *cur)
 
template<class T >
DecodedString decode_string (const int8_t *column, const size_t cursor, StringDictionaryProxy *sdp)
 
int vt_column (sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int col_idx)
 
int vt_rowid (sqlite3_vtab_cursor *cur, sqlite_int64 *p_rowid)
 
int vt_filter (sqlite3_vtab_cursor *p_vtc, int idxNum, const char *idxStr, int argc, sqlite3_value **argv)
 
int vt_best_index (sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo)
 
std::vector< TargetMetaInfocreate_table_schema (const PlanState *plan_state)
 
int64_t * get_scan_output_slot (int64_t *output_buffer, const size_t output_buffer_entry_count, const size_t pos, const size_t row_size_quad)
 

Variables

sqlite3_module omnisci_module
 

Function Documentation

◆ create_table_schema()

std::vector<TargetMetaInfo> anonymous_namespace{ExternalExecutor.cpp}::create_table_schema ( const PlanState plan_state)

Definition at line 348 of file ExternalExecutor.cpp.

References CHECK, PlanState::executor_, get_temporary_table(), SQLTypeInfo::get_type_name(), PlanState::global_to_local_col_ids_, is_supported_type_for_extern_execution(), and serialize_column_ref().

Referenced by run_query_external().

348  {
349  std::map<size_t, TargetMetaInfo> schema_map;
350  const auto catalog = plan_state->executor_->getCatalog();
351  for (const auto& kv : plan_state->global_to_local_col_ids_) {
352  const int table_id = kv.first.getScanDesc().getTableId();
353  const int column_id = kv.first.getColId();
354  SQLTypeInfo column_type;
355  if (table_id < 0) {
356  const auto result_set =
357  get_temporary_table(plan_state->executor_->getTemporaryTables(), table_id);
358  column_type = result_set->getColType(column_id);
359  } else {
360  const auto cd = catalog->getMetadataForColumn(table_id, column_id);
361  column_type = cd->columnType;
362  }
363  if (!is_supported_type_for_extern_execution(column_type)) {
364  throw std::runtime_error("Type not supported yet for extern execution: " +
365  column_type.get_type_name());
366  }
367  const auto column_ref = serialize_column_ref(table_id, column_id, catalog);
368  const auto it_ok =
369  schema_map.emplace(kv.second, TargetMetaInfo(column_ref, column_type));
370  CHECK(it_ok.second);
371  }
372  std::vector<TargetMetaInfo> schema;
373  for (const auto& kv : schema_map) {
374  schema.push_back(kv.second);
375  }
376  return schema;
377 }
bool is_supported_type_for_extern_execution(const SQLTypeInfo &ti)
std::unordered_map< InputColDescriptor, size_t > global_to_local_col_ids_
Definition: PlanState.h:58
const Executor * executor_
Definition: PlanState.h:65
std::string serialize_column_ref(const int table_id, const int column_id, const Catalog_Namespace::Catalog *catalog)
const ResultSetPtr & get_temporary_table(const TemporaryTables *temporary_tables, const int table_id)
Definition: Execute.h:191
#define CHECK(condition)
Definition: Logger.h:197
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ decode_string()

template<class T >
DecodedString anonymous_namespace{ExternalExecutor.cpp}::decode_string ( const int8_t *  column,
const size_t  cursor,
StringDictionaryProxy sdp 
)

Definition at line 154 of file ExternalExecutor.cpp.

References StringDictionaryProxy::getStringBytes(), and run_benchmark_import::result.

156  {
157  const auto ids_column = reinterpret_cast<const T*>(column);
158  const auto val = ids_column[cursor];
159  DecodedString result{};
160  if (val == inline_int_null_value<T>()) {
161  result.is_null = true;
162  } else {
163  result.payload = sdp->getStringBytes(val);
164  }
165  return result;
166 }
std::pair< const char *, size_t > getStringBytes(int32_t string_id) const noexcept
+ Here is the call graph for this function:

◆ get_num_rows()

int64_t anonymous_namespace{ExternalExecutor.cpp}::get_num_rows ( OmniSciCursor p_cur)

Definition at line 127 of file ExternalExecutor.cpp.

References anonymous_namespace{ExternalExecutor.cpp}::OmniSciCursor::base, and CHECK_EQ.

Referenced by vt_column(), and vt_next().

127  {
128  auto p = reinterpret_cast<OmniSciVtab*>(p_cur->base.pVtab);
129  CHECK_EQ(p->external_query_table->fetch_result.num_rows.size(), size_t(1));
130  CHECK_EQ(p->external_query_table->fetch_result.num_rows.front().size(), size_t(1));
131  return p->external_query_table->fetch_result.num_rows.front().front();
132 }
#define CHECK_EQ(x, y)
Definition: Logger.h:205
+ Here is the caller graph for this function:

◆ get_scan_output_slot()

int64_t* anonymous_namespace{ExternalExecutor.cpp}::get_scan_output_slot ( int64_t *  output_buffer,
const size_t  output_buffer_entry_count,
const size_t  pos,
const size_t  row_size_quad 
)

Definition at line 404 of file ExternalExecutor.cpp.

References CHECK_LT.

407  {
408  const auto off = pos * row_size_quad;
409  CHECK_LT(pos, output_buffer_entry_count);
410  output_buffer[off] = off;
411  return output_buffer + off + 1;
412 }
#define CHECK_LT(x, y)
Definition: Logger.h:207

◆ vt_best_index()

int anonymous_namespace{ExternalExecutor.cpp}::vt_best_index ( sqlite3_vtab *  tab,
sqlite3_index_info *  pIdxInfo 
)

Definition at line 318 of file ExternalExecutor.cpp.

318  {
319  return SQLITE_OK;
320 }

◆ vt_close()

int anonymous_namespace{ExternalExecutor.cpp}::vt_close ( sqlite3_vtab_cursor *  cur)

Definition at line 116 of file ExternalExecutor.cpp.

116  {
117  auto p_cur = reinterpret_cast<OmniSciCursor*>(cur);
118  sqlite3_free(p_cur);
119 
120  return SQLITE_OK;
121 }

◆ vt_column()

int anonymous_namespace{ExternalExecutor.cpp}::vt_column ( sqlite3_vtab_cursor *  cur,
sqlite3_context *  ctx,
int  col_idx 
)

Definition at line 168 of file ExternalExecutor.cpp.

References CHECK, CHECK_LE, CHECK_LT, ChunkIter_get_nth(), logger::FATAL, get_num_rows(), inline_fp_null_value< double >(), inline_fp_null_value< float >(), VarlenDatum::is_null, anonymous_namespace{ExternalExecutor.cpp}::DecodedString::is_null, kBIGINT, kDOUBLE, kENCODING_DICT, kENCODING_NONE, kFLOAT, kINT, kSMALLINT, kTEXT, kTINYINT, VarlenDatum::length, LOG, num_rows, anonymous_namespace{ExternalExecutor.cpp}::DecodedString::payload, and VarlenDatum::pointer.

168  {
169  auto p_cur = reinterpret_cast<OmniSciCursor*>(cur);
170  const auto num_rows = get_num_rows(p_cur);
171 
172  auto p = reinterpret_cast<OmniSciVtab*>(p_cur->base.pVtab);
173  const auto& external_query_table = *(p->external_query_table);
174  CHECK_LT(static_cast<size_t>(col_idx),
175  external_query_table.fetch_result.col_buffers[0].size());
176  const auto column = external_query_table.fetch_result.col_buffers[0][col_idx];
177  const auto& col_ti = external_query_table.schema[col_idx].get_type_info();
178  switch (col_ti.get_type()) {
179  case kTINYINT: {
180  const auto val = column[p_cur->count - 1];
181  if (val == inline_int_null_value<int8_t>()) {
182  sqlite3_result_null(ctx);
183  } else {
184  sqlite3_result_int(ctx, val);
185  }
186  break;
187  }
188  case kSMALLINT: {
189  const auto int_column = reinterpret_cast<const int16_t*>(column);
190  const auto val = int_column[p_cur->count - 1];
191  if (val == inline_int_null_value<int16_t>()) {
192  sqlite3_result_null(ctx);
193  } else {
194  sqlite3_result_int(ctx, val);
195  }
196  break;
197  }
198  case kINT: {
199  const auto int_column = reinterpret_cast<const int32_t*>(column);
200  const auto val = int_column[p_cur->count - 1];
201  if (val == inline_int_null_value<int32_t>()) {
202  sqlite3_result_null(ctx);
203  } else {
204  sqlite3_result_int(ctx, val);
205  }
206  break;
207  }
208  case kBIGINT: {
209  const auto int_column = reinterpret_cast<const int64_t*>(column);
210  const auto val = int_column[p_cur->count - 1];
211  if (val == inline_int_null_value<int64_t>()) {
212  sqlite3_result_null(ctx);
213  } else {
214  sqlite3_result_int(ctx, val);
215  }
216  break;
217  }
218  case kFLOAT: {
219  const auto float_column = reinterpret_cast<const float*>(column);
220  const auto val = float_column[p_cur->count - 1];
221  if (val == inline_fp_null_value<float>()) {
222  sqlite3_result_null(ctx);
223  } else {
224  sqlite3_result_double(ctx, val);
225  }
226  break;
227  }
228  case kDOUBLE: {
229  const auto double_column = reinterpret_cast<const double*>(column);
230  const auto val = double_column[p_cur->count - 1];
231  if (val == inline_fp_null_value<double>()) {
232  sqlite3_result_null(ctx);
233  } else {
234  sqlite3_result_double(ctx, val);
235  }
236  break;
237  }
238  case kTEXT: {
239  if (col_ti.get_compression() == kENCODING_DICT) {
240  const auto executor = external_query_table.executor;
241  const auto sdp = executor->getStringDictionaryProxy(
242  col_ti.get_comp_param(), executor->getRowSetMemoryOwner(), true);
243  CHECK(sdp);
244  DecodedString decoded_string;
245  switch (col_ti.get_size()) {
246  case 1: {
247  decoded_string = decode_string<uint8_t>(column, p_cur->count - 1, sdp);
248  break;
249  }
250  case 2: {
251  decoded_string = decode_string<uint16_t>(column, p_cur->count - 1, sdp);
252  break;
253  }
254  case 4: {
255  decoded_string = decode_string<int32_t>(column, p_cur->count - 1, sdp);
256  break;
257  }
258  default: {
259  decoded_string = DecodedString{};
260  LOG(FATAL) << "Invalid encoding size: " << col_ti.get_size();
261  }
262  }
263  if (decoded_string.is_null) {
264  sqlite3_result_null(ctx);
265  } else {
266  sqlite3_result_text(
267  ctx, decoded_string.payload.first, decoded_string.payload.second, nullptr);
268  }
269  } else {
270  CHECK(col_ti.get_compression() == kENCODING_NONE);
271  const auto chunk_iter =
272  const_cast<ChunkIter*>(reinterpret_cast<const ChunkIter*>(column));
273  VarlenDatum vd;
274  bool is_end;
275  ChunkIter_get_nth(chunk_iter, p_cur->count - 1, false, &vd, &is_end);
276  if (vd.is_null) {
277  sqlite3_result_null(ctx);
278  } else {
279  sqlite3_result_text(
280  ctx, reinterpret_cast<const char*>(vd.pointer), vd.length, nullptr);
281  }
282  }
283  break;
284  }
285  default: {
286  LOG(FATAL) << "Unexpected type: " << col_ti.get_type_name();
287  break;
288  }
289  }
290  CHECK_LE(p_cur->count, num_rows);
291 
292  return SQLITE_OK;
293 }
const int8_t const int64_t * num_rows
bool is_null
Definition: sqltypes.h:76
#define LOG(tag)
Definition: Logger.h:188
int64_t get_num_rows(OmniSciCursor *p_cur)
DEVICE void ChunkIter_get_nth(ChunkIter *it, int n, bool uncompress, VarlenDatum *result, bool *is_end)
Definition: ChunkIter.cpp:181
int8_t * pointer
Definition: sqltypes.h:75
#define CHECK_LT(x, y)
Definition: Logger.h:207
Definition: sqltypes.h:54
#define CHECK_LE(x, y)
Definition: Logger.h:208
constexpr float inline_fp_null_value< float >()
constexpr double inline_fp_null_value< double >()
#define CHECK(condition)
Definition: Logger.h:197
Definition: sqltypes.h:47
size_t length
Definition: sqltypes.h:74
+ Here is the call graph for this function:

◆ vt_connect()

int anonymous_namespace{ExternalExecutor.cpp}::vt_connect ( sqlite3 *  db,
void *  p_aux,
int  argc,
const char *const *  argv,
sqlite3_vtab **  pp_vt,
char **  pzErr 
)

Definition at line 86 of file ExternalExecutor.cpp.

References vt_create().

91  {
92  return vt_create(db, p_aux, argc, argv, pp_vt, pzErr);
93 }
int vt_create(sqlite3 *db, void *p_aux, int argc, const char *const *argv, sqlite3_vtab **pp_vt, char **pzErr)
+ Here is the call graph for this function:

◆ vt_create()

int anonymous_namespace{ExternalExecutor.cpp}::vt_create ( sqlite3 *  db,
void *  p_aux,
int  argc,
const char *const *  argv,
sqlite3_vtab **  pp_vt,
char **  pzErr 
)

Definition at line 45 of file ExternalExecutor.cpp.

References anonymous_namespace{ExternalExecutor.cpp}::OmniSciVtab::db, join(), and vt_destructor().

Referenced by vt_connect().

50  {
51  // Allocate the sqlite3_vtab/OmniSciVtab structure itself.
52  auto p_vt = static_cast<OmniSciVtab*>(sqlite3_malloc(sizeof(OmniSciVtab)));
53  if (!p_vt) {
54  return SQLITE_NOMEM;
55  }
56 
57  p_vt->db = db;
58  p_vt->external_query_table = reinterpret_cast<const ExternalQueryTable*>(p_aux);
59 
60  std::vector<std::string> col_defs;
61  std::transform(p_vt->external_query_table->schema.begin(),
62  p_vt->external_query_table->schema.end(),
63  std::back_inserter(col_defs),
64  [](const TargetMetaInfo& target_metainfo) {
65  return target_metainfo.get_resname() + " " +
66  target_metainfo.get_type_info().get_type_name();
67  });
68  const auto col_defs_str = boost::algorithm::join(col_defs, ", ");
69  const auto create_statement =
70  "create table vtable (" + (col_defs_str.empty() ? "dummy int" : col_defs_str) + ")";
71 
72  // Declare the vtable's structure.
73  int rc = sqlite3_declare_vtab(db, create_statement.c_str());
74 
75  if (rc != SQLITE_OK) {
76  vt_destructor(reinterpret_cast<sqlite3_vtab*>(p_vt));
77  return SQLITE_ERROR;
78  }
79 
80  // Success. Set *pp_vt and return.
81  *pp_vt = &p_vt->base;
82 
83  return SQLITE_OK;
84 }
std::string join(T const &container, std::string const &delim)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ vt_destroy()

int anonymous_namespace{ExternalExecutor.cpp}::vt_destroy ( sqlite3_vtab *  pVtab)

Definition at line 99 of file ExternalExecutor.cpp.

References vt_destructor().

99  {
100  int rc = SQLITE_OK;
101 
102  if (rc == SQLITE_OK) {
103  rc = vt_destructor(pVtab);
104  }
105 
106  return rc;
107 }
+ Here is the call graph for this function:

◆ vt_destructor()

int anonymous_namespace{ExternalExecutor.cpp}::vt_destructor ( sqlite3_vtab *  pVtab)

Definition at line 38 of file ExternalExecutor.cpp.

Referenced by vt_create(), vt_destroy(), and vt_disconnect().

38  {
39  OmniSciVtab* p = reinterpret_cast<OmniSciVtab*>(pVtab);
40  sqlite3_free(p);
41 
42  return 0;
43 }
+ Here is the caller graph for this function:

◆ vt_disconnect()

int anonymous_namespace{ExternalExecutor.cpp}::vt_disconnect ( sqlite3_vtab *  pVtab)

Definition at line 95 of file ExternalExecutor.cpp.

References vt_destructor().

95  {
96  return vt_destructor(pVtab);
97 }
+ Here is the call graph for this function:

◆ vt_eof()

int anonymous_namespace{ExternalExecutor.cpp}::vt_eof ( sqlite3_vtab_cursor *  cur)

Definition at line 123 of file ExternalExecutor.cpp.

123  {
124  return reinterpret_cast<OmniSciCursor*>(cur)->eof;
125 }

◆ vt_filter()

int anonymous_namespace{ExternalExecutor.cpp}::vt_filter ( sqlite3_vtab_cursor *  p_vtc,
int  idxNum,
const char *  idxStr,
int  argc,
sqlite3_value **  argv 
)

Definition at line 302 of file ExternalExecutor.cpp.

References anonymous_namespace{ExternalExecutor.cpp}::OmniSciCursor::count, and vt_next().

306  {
307  // Initialize the cursor structure.
308  auto p_cur = reinterpret_cast<OmniSciCursor*>(p_vtc);
309  // Zero rows returned thus far.
310  p_cur->count = 0;
311  // Have not reached end of set.
312  p_cur->eof = 0;
313  // Move cursor to first row.
314  return vt_next(p_vtc);
315 }
+ Here is the call graph for this function:

◆ vt_next()

int anonymous_namespace{ExternalExecutor.cpp}::vt_next ( sqlite3_vtab_cursor *  cur)

Definition at line 134 of file ExternalExecutor.cpp.

References get_num_rows(), and num_rows.

Referenced by vt_filter().

134  {
135  auto p_cur = reinterpret_cast<OmniSciCursor*>(cur);
136  const auto num_rows = get_num_rows(p_cur);
137 
138  if (p_cur->count == num_rows) {
139  p_cur->eof = 1;
140  }
141 
142  // Increment the current row count.
143  ++p_cur->count;
144 
145  return SQLITE_OK;
146 }
const int8_t const int64_t * num_rows
int64_t get_num_rows(OmniSciCursor *p_cur)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ vt_open()

int anonymous_namespace{ExternalExecutor.cpp}::vt_open ( sqlite3_vtab *  pVTab,
sqlite3_vtab_cursor **  pp_cursor 
)

Definition at line 109 of file ExternalExecutor.cpp.

109  {
110  auto p_cur = static_cast<OmniSciCursor*>(sqlite3_malloc(sizeof(OmniSciCursor)));
111  *pp_cursor = reinterpret_cast<sqlite3_vtab_cursor*>(p_cur);
112 
113  return (p_cur ? SQLITE_OK : SQLITE_NOMEM);
114 }

◆ vt_rowid()

int anonymous_namespace{ExternalExecutor.cpp}::vt_rowid ( sqlite3_vtab_cursor *  cur,
sqlite_int64 *  p_rowid 
)

Definition at line 295 of file ExternalExecutor.cpp.

References anonymous_namespace{ExternalExecutor.cpp}::OmniSciCursor::count.

295  {
296  auto p_cur = reinterpret_cast<OmniSciCursor*>(cur);
297  // Just use the current row count as the rowid.
298  *p_rowid = p_cur->count;
299  return SQLITE_OK;
300 }

Variable Documentation

◆ omnisci_module

sqlite3_module anonymous_namespace{ExternalExecutor.cpp}::omnisci_module
Initial value:
= {
0,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr
}
int vt_connect(sqlite3 *db, void *p_aux, int argc, const char *const *argv, sqlite3_vtab **pp_vt, char **pzErr)
int vt_filter(sqlite3_vtab_cursor *p_vtc, int idxNum, const char *idxStr, int argc, sqlite3_value **argv)
int vt_rowid(sqlite3_vtab_cursor *cur, sqlite_int64 *p_rowid)
int vt_close(sqlite3_vtab_cursor *cur)
int vt_open(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **pp_cursor)
int vt_column(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int col_idx)
int vt_create(sqlite3 *db, void *p_aux, int argc, const char *const *argv, sqlite3_vtab **pp_vt, char **pzErr)
int vt_best_index(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo)

Definition at line 322 of file ExternalExecutor.cpp.

Referenced by SqliteMemDatabase::SqliteMemDatabase().