OmniSciDB  72c90bc290
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
InputMetadata.cpp File Reference
#include "InputMetadata.h"
#include "Execute.h"
#include "../Fragmenter/Fragmenter.h"
#include <tbb/parallel_for.h>
#include <tbb/task_arena.h>
#include <future>
+ Include dependency graph for InputMetadata.cpp:

Go to the source code of this file.

Namespaces

 

Functions

Fragmenter_Namespace::TableInfo anonymous_namespace{InputMetadata.cpp}::copy_table_info (const Fragmenter_Namespace::TableInfo &table_info)
 
Fragmenter_Namespace::TableInfo build_table_info (const std::vector< const TableDescriptor * > &shard_tables)
 
bool anonymous_namespace{InputMetadata.cpp}::uses_int_meta (const SQLTypeInfo &col_ti)
 
Fragmenter_Namespace::TableInfo anonymous_namespace{InputMetadata.cpp}::synthesize_table_info (const ResultSetPtr &rows)
 
void anonymous_namespace{InputMetadata.cpp}::collect_table_infos (std::vector< InputTableInfo > &table_infos, const std::vector< InputDescriptor > &input_descs, Executor *executor)
 
template<typename T >
void compute_table_function_col_chunk_stats (std::shared_ptr< ChunkMetadata > &chunk_metadata, const T *values_buffer, const size_t values_count, const T null_val)
 
ChunkMetadataMap synthesize_metadata_table_function (const ResultSet *rows)
 
ChunkMetadataMap synthesize_metadata (const ResultSet *rows)
 
size_t get_frag_count_of_table (const shared::TableKey &table_key, Executor *executor)
 
std::vector< InputTableInfoget_table_infos (const std::vector< InputDescriptor > &input_descs, Executor *executor)
 
std::vector< InputTableInfoget_table_infos (const RelAlgExecutionUnit &ra_exe_unit, Executor *executor)
 

Variables

bool g_enable_data_recycler
 
bool g_use_chunk_metadata_cache
 

Function Documentation

Fragmenter_Namespace::TableInfo build_table_info ( const std::vector< const TableDescriptor * > &  shard_tables)

Definition at line 44 of file InputMetadata.cpp.

References CHECK, Fragmenter_Namespace::TableInfo::fragments, and Fragmenter_Namespace::TableInfo::setPhysicalNumTuples().

Referenced by InputTableInfoCache::getTableInfo().

45  {
46  size_t total_number_of_tuples{0};
47  Fragmenter_Namespace::TableInfo table_info_all_shards;
48  for (const TableDescriptor* shard_table : shard_tables) {
49  CHECK(shard_table->fragmenter);
50  const auto& shard_metainfo = shard_table->fragmenter->getFragmentsForQuery();
51  total_number_of_tuples += shard_metainfo.getPhysicalNumTuples();
52  table_info_all_shards.fragments.reserve(table_info_all_shards.fragments.size() +
53  shard_metainfo.fragments.size());
54  table_info_all_shards.fragments.insert(table_info_all_shards.fragments.end(),
55  shard_metainfo.fragments.begin(),
56  shard_metainfo.fragments.end());
57  }
58  table_info_all_shards.setPhysicalNumTuples(total_number_of_tuples);
59  return table_info_all_shards;
60 }
std::vector< FragmentInfo > fragments
Definition: Fragmenter.h:171
#define CHECK(condition)
Definition: Logger.h:291
void setPhysicalNumTuples(const size_t physNumTuples)
Definition: Fragmenter.h:166

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

template<typename T >
void compute_table_function_col_chunk_stats ( std::shared_ptr< ChunkMetadata > &  chunk_metadata,
const T *  values_buffer,
const size_t  values_count,
const T  null_val 
)

Definition at line 142 of file InputMetadata.cpp.

References max_inputs_per_thread, threading_serial::parallel_for(), and heavydb.dtypes::T.

Referenced by synthesize_metadata_table_function().

146  {
147  T min_val{std::numeric_limits<T>::max()};
148  T max_val{std::numeric_limits<T>::lowest()};
149  bool has_nulls{false};
150  constexpr size_t parallel_stats_compute_threshold = 20000UL;
151  if (values_count < parallel_stats_compute_threshold) {
152  for (size_t row_idx = 0; row_idx < values_count; ++row_idx) {
153  const T cell_val = values_buffer[row_idx];
154  if (cell_val == null_val) {
155  has_nulls = true;
156  continue;
157  }
158  if (cell_val < min_val) {
159  min_val = cell_val;
160  }
161  if (cell_val > max_val) {
162  max_val = cell_val;
163  }
164  }
165  } else {
166  const size_t max_thread_count = std::thread::hardware_concurrency();
167  const size_t max_inputs_per_thread = 20000;
168  const size_t min_grain_size = max_inputs_per_thread / 2;
169  const size_t num_threads =
170  std::min(max_thread_count,
171  ((values_count + max_inputs_per_thread - 1) / max_inputs_per_thread));
172 
173  std::vector<T> threads_local_mins(num_threads, std::numeric_limits<T>::max());
174  std::vector<T> threads_local_maxes(num_threads, std::numeric_limits<T>::lowest());
175  std::vector<bool> threads_local_has_nulls(num_threads, false);
176  tbb::task_arena limited_arena(num_threads);
177 
178  limited_arena.execute([&] {
180  tbb::blocked_range<size_t>(0, values_count, min_grain_size),
181  [&](const tbb::blocked_range<size_t>& r) {
182  const size_t start_idx = r.begin();
183  const size_t end_idx = r.end();
184  T local_min_val = std::numeric_limits<T>::max();
185  T local_max_val = std::numeric_limits<T>::lowest();
186  bool local_has_nulls = false;
187  for (size_t row_idx = start_idx; row_idx < end_idx; ++row_idx) {
188  const T cell_val = values_buffer[row_idx];
189  if (cell_val == null_val) {
190  local_has_nulls = true;
191  continue;
192  }
193  if (cell_val < local_min_val) {
194  local_min_val = cell_val;
195  }
196  if (cell_val > local_max_val) {
197  local_max_val = cell_val;
198  }
199  }
200  size_t thread_idx = tbb::this_task_arena::current_thread_index();
201  if (local_min_val < threads_local_mins[thread_idx]) {
202  threads_local_mins[thread_idx] = local_min_val;
203  }
204  if (local_max_val > threads_local_maxes[thread_idx]) {
205  threads_local_maxes[thread_idx] = local_max_val;
206  }
207  if (local_has_nulls) {
208  threads_local_has_nulls[thread_idx] = true;
209  }
210  },
211  tbb::simple_partitioner());
212  });
213 
214  for (size_t thread_idx = 0; thread_idx < num_threads; ++thread_idx) {
215  if (threads_local_mins[thread_idx] < min_val) {
216  min_val = threads_local_mins[thread_idx];
217  }
218  if (threads_local_maxes[thread_idx] > max_val) {
219  max_val = threads_local_maxes[thread_idx];
220  }
221  has_nulls |= threads_local_has_nulls[thread_idx];
222  }
223  }
224  chunk_metadata->fillChunkStats(min_val, max_val, has_nulls);
225 }
const size_t max_inputs_per_thread
void parallel_for(const blocked_range< Int > &range, const Body &body, const Partitioner &p=Partitioner())

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

size_t get_frag_count_of_table ( const shared::TableKey table_key,
Executor executor 
)

Definition at line 479 of file InputMetadata.cpp.

References CHECK, CHECK_GE, and shared::TableKey::table_id.

Referenced by RelAlgExecutor::getOuterFragmentCount().

479  {
480  const auto temporary_tables = executor->getTemporaryTables();
481  CHECK(temporary_tables);
482  auto it = temporary_tables->find(table_key.table_id);
483  if (it != temporary_tables->end()) {
484  CHECK_GE(int(0), table_key.table_id);
485  return size_t(1);
486  } else {
487  const auto table_info = executor->getTableInfo(table_key);
488  return table_info.fragments.size();
489  }
490 }
#define CHECK_GE(x, y)
Definition: Logger.h:306
#define CHECK(condition)
Definition: Logger.h:291

+ Here is the caller graph for this function:

std::vector<InputTableInfo> get_table_infos ( const std::vector< InputDescriptor > &  input_descs,
Executor executor 
)

Definition at line 492 of file InputMetadata.cpp.

References anonymous_namespace{InputMetadata.cpp}::collect_table_infos().

Referenced by RelAlgExecutor::computeWindow(), RelAlgExecutor::createAggregateWorkUnit(), RelAlgExecutor::createCompoundWorkUnit(), RelAlgExecutor::createFilterWorkUnit(), RelAlgExecutor::createProjectWorkUnit(), RelAlgExecutor::createTableFunctionWorkUnit(), RelAlgExecutor::createUnionWorkUnit(), RelAlgExecutor::executeDelete(), RelAlgExecutor::executeTableFunction(), RelAlgExecutor::executeUpdate(), RelAlgExecutor::executeWorkUnit(), TableOptimizer::getDeletedColumnStats(), RelAlgExecutor::getFilteredCountAll(), RelAlgExecutor::getFilterSelectivity(), RelAlgExecutor::getNDVEstimation(), RelAlgExecutor::handleOutOfMemoryRetry(), TableOptimizer::recomputeColumnMetadata(), and RelAlgExecutor::selectFiltersToBePushedDown().

494  {
495  std::vector<InputTableInfo> table_infos;
496  collect_table_infos(table_infos, input_descs, executor);
497  return table_infos;
498 }
void collect_table_infos(std::vector< InputTableInfo > &table_infos, const std::vector< InputDescriptor > &input_descs, Executor *executor)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::vector<InputTableInfo> get_table_infos ( const RelAlgExecutionUnit ra_exe_unit,
Executor executor 
)

Definition at line 500 of file InputMetadata.cpp.

References anonymous_namespace{InputMetadata.cpp}::collect_table_infos(), and RelAlgExecutionUnit::input_descs.

501  {
502  std::vector<InputTableInfo> table_infos;
503  collect_table_infos(table_infos, ra_exe_unit.input_descs, executor);
504  return table_infos;
505 }
std::vector< InputDescriptor > input_descs
void collect_table_infos(std::vector< InputTableInfo > &table_infos, const std::vector< InputDescriptor > &input_descs, Executor *executor)

+ Here is the call graph for this function:

ChunkMetadataMap synthesize_metadata ( const ResultSet rows)

Definition at line 361 of file InputMetadata.cpp.

References CHECK, CHECK_LT, cpu_threads(), Encoder::Create(), DEBUG_TIMER, inline_fp_null_val(), inline_int_null_val(), kDOUBLE, kFLOAT, threading_serial::parallel_for(), report::rows, synthesize_metadata_table_function(), TableFunction, result_set::use_parallel_algorithms(), and anonymous_namespace{InputMetadata.cpp}::uses_int_meta().

Referenced by Fragmenter_Namespace::FragmentInfo::getChunkMetadataMap().

361  {
362  auto timer = DEBUG_TIMER(__func__);
363  ChunkMetadataMap metadata_map;
364 
365  // If the ResultSet has no rows, fill with dummy metadata and return early.
366  if (rows->definitelyHasNoRows()) {
367  // resultset has no valid storage, so we fill dummy metadata and return early
368  std::vector<std::unique_ptr<Encoder>> decoders;
369  for (size_t i = 0; i < rows->colCount(); ++i) {
370  decoders.emplace_back(Encoder::Create(nullptr, rows->getColType(i)));
371  const auto it_ok =
372  metadata_map.emplace(i, decoders.back()->getMetadata(rows->getColType(i)));
373  CHECK(it_ok.second);
374  }
375  return metadata_map;
376  }
377 
378  // Create a vector of Encoder vectors for each worker.
379  std::vector<std::vector<std::unique_ptr<Encoder>>> dummy_encoders;
380  const size_t worker_count =
382  for (size_t worker_idx = 0; worker_idx < worker_count; ++worker_idx) {
383  dummy_encoders.emplace_back();
384  for (size_t i = 0; i < rows->colCount(); ++i) {
385  const auto& col_ti = rows->getColType(i);
386  dummy_encoders.back().emplace_back(Encoder::Create(nullptr, col_ti));
387  }
388  }
389 
390  // For TableFunctions, call the optimized function we have for this format.
391  if (rows->getQueryMemDesc().getQueryDescriptionType() ==
394  }
395  rows->moveToBegin();
396 
397  // Code in the do_work lambda runs for and processes each row.
398  const auto do_work = [rows](const std::vector<TargetValue>& crt_row,
399  std::vector<std::unique_ptr<Encoder>>& dummy_encoders) {
400  for (size_t i = 0; i < rows->colCount(); ++i) {
401  const auto& col_ti = rows->getColType(i);
402  const auto& col_val = crt_row[i];
403  const auto scalar_col_val = boost::get<ScalarTargetValue>(&col_val);
404  CHECK(scalar_col_val);
405  if (uses_int_meta(col_ti)) {
406  const auto i64_p = boost::get<int64_t>(scalar_col_val);
407  CHECK(i64_p);
408  dummy_encoders[i]->updateStats(*i64_p, *i64_p == inline_int_null_val(col_ti));
409  } else if (col_ti.is_fp()) {
410  switch (col_ti.get_type()) {
411  case kFLOAT: {
412  const auto float_p = boost::get<float>(scalar_col_val);
413  CHECK(float_p);
414  dummy_encoders[i]->updateStats(*float_p,
415  *float_p == inline_fp_null_val(col_ti));
416  break;
417  }
418  case kDOUBLE: {
419  const auto double_p = boost::get<double>(scalar_col_val);
420  CHECK(double_p);
421  dummy_encoders[i]->updateStats(*double_p,
422  *double_p == inline_fp_null_val(col_ti));
423  break;
424  }
425  default:
426  CHECK(false);
427  }
428  } else {
429  throw std::runtime_error(col_ti.get_type_name() +
430  " is not supported in temporary table.");
431  }
432  }
433  };
434 
435  // Parallelize the processing using TBB if parallel algorithms are enabled.
437  const size_t entry_count = rows->entryCount();
439  tbb::blocked_range<size_t>(0, entry_count),
440  [&do_work, &rows, &dummy_encoders](const tbb::blocked_range<size_t>& range) {
441  const size_t worker_idx = tbb::this_task_arena::current_thread_index();
442  for (size_t i = range.begin(); i < range.end(); ++i) {
443  const auto crt_row = rows->getRowAtNoTranslations(i);
444  if (!crt_row.empty()) {
445  do_work(crt_row, dummy_encoders[worker_idx]);
446  }
447  }
448  });
449 
450  } else {
451  // If parallel algorithms are not enabled, process the rows sequentially.
452  while (true) {
453  auto crt_row = rows->getNextRow(false, false);
454  if (crt_row.empty()) {
455  break;
456  }
457  do_work(crt_row, dummy_encoders[0]);
458  }
459  }
460  rows->moveToBegin();
461 
462  // Reduce the results from each worker.
463  for (size_t worker_idx = 1; worker_idx < worker_count; ++worker_idx) {
464  CHECK_LT(worker_idx, dummy_encoders.size());
465  const auto& worker_encoders = dummy_encoders[worker_idx];
466  for (size_t i = 0; i < rows->colCount(); ++i) {
467  dummy_encoders[0][i]->reduceStats(*worker_encoders[i]);
468  }
469  }
470  // Add each column's results to the metadata map.
471  for (size_t i = 0; i < rows->colCount(); ++i) {
472  const auto it_ok =
473  metadata_map.emplace(i, dummy_encoders[0][i]->getMetadata(rows->getColType(i)));
474  CHECK(it_ok.second);
475  }
476  return metadata_map;
477 }
ChunkMetadataMap synthesize_metadata_table_function(const ResultSet *rows)
static Encoder * Create(Data_Namespace::AbstractBuffer *buffer, const SQLTypeInfo sqlType)
Definition: Encoder.cpp:26
double inline_fp_null_val(const SQL_TYPE_INFO &ti)
std::map< int, std::shared_ptr< ChunkMetadata >> ChunkMetadataMap
bool use_parallel_algorithms(const ResultSet &rows)
Definition: ResultSet.cpp:1600
tuple rows
Definition: report.py:114
bool uses_int_meta(const SQLTypeInfo &col_ti)
#define CHECK_LT(x, y)
Definition: Logger.h:303
void parallel_for(const blocked_range< Int > &range, const Body &body, const Partitioner &p=Partitioner())
#define CHECK(condition)
Definition: Logger.h:291
#define DEBUG_TIMER(name)
Definition: Logger.h:412
int64_t inline_int_null_val(const SQL_TYPE_INFO &ti)
int cpu_threads()
Definition: thread_count.h:25

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

ChunkMetadataMap synthesize_metadata_table_function ( const ResultSet rows)

Definition at line 227 of file InputMetadata.cpp.

References CHECK, CHECK_EQ, compute_table_function_col_chunk_stats(), FlatBufferManager::getBufferSize(), inline_fixed_encoding_null_val(), inline_fp_null_value< double >(), inline_fp_null_value< float >(), FlatBufferManager::isFlatBuffer(), kBIGINT, kBOOLEAN, kDOUBLE, kENCODING_DICT, kENCODING_NONE, kFLOAT, kINT, kLINESTRING, kMULTILINESTRING, kMULTIPOLYGON, kPOINT, kPOLYGON, kSMALLINT, kTEXT, kTIMESTAMP, kTINYINT, TableFunction, and UNREACHABLE.

Referenced by synthesize_metadata().

227  {
228  CHECK(rows->getQueryMemDesc().getQueryDescriptionType() ==
230  CHECK(rows->didOutputColumnar());
231  CHECK(!(rows->areAnyColumnsLazyFetched()));
232  const size_t col_count = rows->colCount();
233  const auto row_count = rows->entryCount();
234 
235  ChunkMetadataMap chunk_metadata_map;
236 
237  for (size_t col_idx = 0; col_idx < col_count; ++col_idx) {
238  std::shared_ptr<ChunkMetadata> chunk_metadata = std::make_shared<ChunkMetadata>();
239  const int8_t* columnar_buffer = const_cast<int8_t*>(rows->getColumnarBuffer(col_idx));
240  const auto col_sql_type_info = rows->getColType(col_idx);
241  // Here, min/max of a column of arrays, col, is defined as
242  // min/max(unnest(col)). That is, if is_array is true, the
243  // metadata is supposed to be syntesized for a query like `SELECT
244  // UNNEST(col_of_arrays) ... GROUP BY ...`. How can we verify that
245  // here?
246 
247  // min/max of a column of a geotype is defined as the min/max of
248  // all x and y coordinate values
249  bool is_array = col_sql_type_info.is_array();
250  bool is_geometry = col_sql_type_info.is_geometry();
251  const auto col_type =
252  (is_array ? col_sql_type_info.get_subtype()
253  : (is_geometry ? col_sql_type_info.get_elem_type().get_type()
254  : col_sql_type_info.get_type()));
255  const auto col_type_info =
256  ((is_array || is_geometry) ? col_sql_type_info.get_elem_type()
257  : col_sql_type_info);
258 
259  chunk_metadata->sqlType = col_type_info;
260  chunk_metadata->numElements = row_count;
261 
262  const int8_t* values_buffer{nullptr};
263  size_t values_count{0};
264  if (FlatBufferManager::isFlatBuffer(columnar_buffer)) {
265  CHECK(FlatBufferManager::isFlatBuffer(columnar_buffer));
266  FlatBufferManager m{const_cast<int8_t*>(columnar_buffer)};
267  chunk_metadata->numBytes = m.getBufferSize();
268  if (is_geometry) {
269  switch (col_sql_type_info.get_type()) {
270  case kPOINT:
271  // a geometry value is a pair of coordinates but its element
272  // type value is a int or double, hence multiplication by 2:
273  values_count = row_count * 2;
274  values_buffer = m.get_values();
275  break;
276  case kLINESTRING:
277  case kPOLYGON:
278  case kMULTILINESTRING:
279  case kMULTIPOLYGON: {
280  values_count = m.getValuesCount();
281  values_buffer = m.getValuesBuffer();
282  } break;
283  default:
284  UNREACHABLE();
285  }
286  } else {
287  CHECK(is_array);
288  CHECK(m.isNestedArray());
289  values_count = m.getValuesCount();
290  values_buffer = m.getValuesBuffer();
291  }
292  } else {
293  chunk_metadata->numBytes = row_count * col_type_info.get_size();
294  values_count = row_count;
295  values_buffer = columnar_buffer;
296  }
297 
298  if (col_type != kTEXT) {
299  CHECK(col_type_info.get_compression() == kENCODING_NONE);
300  } else {
301  CHECK(col_type_info.get_compression() == kENCODING_DICT);
302  CHECK_EQ(col_type_info.get_size(), sizeof(int32_t));
303  }
304 
305  switch (col_type) {
306  case kBOOLEAN:
307  case kTINYINT:
309  chunk_metadata,
310  values_buffer,
311  values_count,
312  static_cast<int8_t>(inline_fixed_encoding_null_val(col_type_info)));
313  break;
314  case kSMALLINT:
316  chunk_metadata,
317  reinterpret_cast<const int16_t*>(values_buffer),
318  values_count,
319  static_cast<int16_t>(inline_fixed_encoding_null_val(col_type_info)));
320  break;
321  case kINT:
322  case kTEXT:
324  chunk_metadata,
325  reinterpret_cast<const int32_t*>(values_buffer),
326  values_count,
327  static_cast<int32_t>(inline_fixed_encoding_null_val(col_type_info)));
328  break;
329  case kBIGINT:
330  case kTIMESTAMP:
332  chunk_metadata,
333  reinterpret_cast<const int64_t*>(values_buffer),
334  values_count,
335  static_cast<int64_t>(inline_fixed_encoding_null_val(col_type_info)));
336  break;
337  case kFLOAT:
338  // For float use the typed null accessor as the generic one converts to double,
339  // and do not want to risk loss of precision
341  chunk_metadata,
342  reinterpret_cast<const float*>(values_buffer),
343  values_count,
345  break;
346  case kDOUBLE:
348  chunk_metadata,
349  reinterpret_cast<const double*>(values_buffer),
350  values_count,
352  break;
353  default:
354  UNREACHABLE();
355  }
356  chunk_metadata_map.emplace(col_idx, chunk_metadata);
357  }
358  return chunk_metadata_map;
359 }
#define CHECK_EQ(x, y)
Definition: Logger.h:301
#define UNREACHABLE()
Definition: Logger.h:338
std::map< int, std::shared_ptr< ChunkMetadata >> ChunkMetadataMap
tuple rows
Definition: report.py:114
Definition: sqltypes.h:79
constexpr float inline_fp_null_value< float >()
constexpr double inline_fp_null_value< double >()
#define CHECK(condition)
Definition: Logger.h:291
void compute_table_function_col_chunk_stats(std::shared_ptr< ChunkMetadata > &chunk_metadata, const T *values_buffer, const size_t values_count, const T null_val)
int64_t inline_fixed_encoding_null_val(const SQL_TYPE_INFO &ti)
Definition: sqltypes.h:72
HOST static DEVICE bool isFlatBuffer(const void *buffer)
Definition: FlatBuffer.h:528
static int64_t getBufferSize(const void *buffer)
Definition: FlatBuffer.h:553

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Variable Documentation

bool g_enable_data_recycler

Definition at line 154 of file Execute.cpp.

bool g_use_chunk_metadata_cache

Definition at line 157 of file Execute.cpp.