OmniSciDB  cde582ebc3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
BaselineJoinHashTable.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2022 HEAVY.AI, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
18 
19 #include <future>
20 
24 #include "QueryEngine/Execute.h"
31 
32 // let's only consider CPU hashtable recycler at this moment
33 // todo (yoonmin): support GPU hashtable cache without regression
34 std::unique_ptr<HashtableRecycler> BaselineJoinHashTable::hash_table_cache_ =
35  std::make_unique<HashtableRecycler>(CacheItemType::BASELINE_HT,
37 std::unique_ptr<HashingSchemeRecycler> BaselineJoinHashTable::hash_table_layout_cache_ =
38  std::make_unique<HashingSchemeRecycler>();
39 
41 std::shared_ptr<BaselineJoinHashTable> BaselineJoinHashTable::getInstance(
42  const std::shared_ptr<Analyzer::BinOper> condition,
43  const std::vector<InputTableInfo>& query_infos,
44  const Data_Namespace::MemoryLevel memory_level,
45  const JoinType join_type,
46  const HashType preferred_hash_type,
47  const int device_count,
48  ColumnCacheMap& column_cache,
49  Executor* executor,
50  const HashTableBuildDagMap& hashtable_build_dag_map,
51  const TableIdToNodeMap& table_id_to_node_map) {
52  decltype(std::chrono::steady_clock::now()) ts1, ts2;
53 
54  if (VLOGGING(1)) {
55  VLOG(1) << "Building keyed hash table " << getHashTypeString(preferred_hash_type)
56  << " for qual: " << condition->toString();
57  ts1 = std::chrono::steady_clock::now();
58  }
59  auto inner_outer_pairs = HashJoin::normalizeColumnPairs(
60  condition.get(), *executor->getCatalog(), executor->getTemporaryTables());
61  const auto& inner_outer_cols = inner_outer_pairs.first;
62  const auto& col_pairs_string_op_infos = inner_outer_pairs.second;
63  auto join_hash_table = std::shared_ptr<BaselineJoinHashTable>(
64  new BaselineJoinHashTable(condition,
65  join_type,
66  query_infos,
67  memory_level,
68  column_cache,
69  executor,
70  inner_outer_cols,
71  col_pairs_string_op_infos,
72  device_count,
73  hashtable_build_dag_map,
74  table_id_to_node_map));
75 
76  try {
77  join_hash_table->reify(preferred_hash_type);
78  } catch (const TableMustBeReplicated& e) {
79  // Throw a runtime error to abort the query
80  join_hash_table->freeHashBufferMemory();
81  throw std::runtime_error(e.what());
82  } catch (const HashJoinFail& e) {
83  // HashJoinFail exceptions log an error and trigger a retry with a join loop (if
84  // possible)
85  join_hash_table->freeHashBufferMemory();
86  throw HashJoinFail(std::string("Could not build a 1-to-1 correspondence for columns "
87  "involved in equijoin | ") +
88  e.what());
89  } catch (const ColumnarConversionNotSupported& e) {
90  throw HashJoinFail(std::string("Could not build hash tables for equijoin | ") +
91  e.what());
92  } catch (const OutOfMemory& e) {
93  throw HashJoinFail(
94  std::string("Ran out of memory while building hash tables for equijoin | ") +
95  e.what());
96  } catch (const std::exception& e) {
97  throw std::runtime_error(
98  std::string("Fatal error while attempting to build hash tables for join: ") +
99  e.what());
100  }
101  if (VLOGGING(1)) {
102  ts2 = std::chrono::steady_clock::now();
103  VLOG(1) << "Built keyed hash table "
104  << getHashTypeString(join_hash_table->getHashType()) << " in "
105  << std::chrono::duration_cast<std::chrono::milliseconds>(ts2 - ts1).count()
106  << " ms";
107  }
108  return join_hash_table;
109 }
110 
112  const std::shared_ptr<Analyzer::BinOper> condition,
113  const JoinType join_type,
114  const std::vector<InputTableInfo>& query_infos,
115  const Data_Namespace::MemoryLevel memory_level,
116  ColumnCacheMap& column_cache,
117  Executor* executor,
118  const std::vector<InnerOuter>& inner_outer_pairs,
119  const std::vector<InnerOuterStringOpInfos>& col_pairs_string_op_infos,
120  const int device_count,
121  const HashTableBuildDagMap& hashtable_build_dag_map,
122  const TableIdToNodeMap& table_id_to_node_map)
123  : condition_(condition)
124  , join_type_(join_type)
125  , query_infos_(query_infos)
126  , memory_level_(memory_level)
127  , executor_(executor)
128  , column_cache_(column_cache)
129  , inner_outer_pairs_(inner_outer_pairs)
130  , inner_outer_string_op_infos_pairs_(col_pairs_string_op_infos)
131  , catalog_(executor->getCatalog())
132  , device_count_(device_count)
133  , needs_dict_translation_(false)
134  , hashtable_build_dag_map_(hashtable_build_dag_map)
135  , table_id_to_node_map_(table_id_to_node_map) {
137  hash_tables_for_device_.resize(std::max(device_count_, 1));
138 }
139 
141  const Analyzer::BinOper* condition,
142  const Executor* executor,
143  const std::vector<InnerOuter>& inner_outer_pairs) {
144  for (const auto& inner_outer_pair : inner_outer_pairs) {
145  const auto pair_shard_count = get_shard_count(inner_outer_pair, executor);
146  if (pair_shard_count) {
147  return pair_shard_count;
148  }
149  }
150  return 0;
151 }
152 
154  const int device_id,
155  bool raw) const {
156  auto buffer = getJoinHashBuffer(device_type, device_id);
157  CHECK_LT(static_cast<size_t>(device_id), hash_tables_for_device_.size());
158  auto hash_table = hash_tables_for_device_[device_id];
159  CHECK(hash_table);
160  auto buffer_size = hash_table->getHashTableBufferSize(device_type);
161 #ifdef HAVE_CUDA
162  std::unique_ptr<int8_t[]> buffer_copy;
163  if (device_type == ExecutorDeviceType::GPU) {
164  buffer_copy = std::make_unique<int8_t[]>(buffer_size);
165 
166  auto& data_mgr = catalog_->getDataMgr();
167  auto device_allocator = std::make_unique<CudaAllocator>(
168  &data_mgr, device_id, getQueryEngineCudaStreamForDevice(device_id));
169  device_allocator->copyFromDevice(buffer_copy.get(), buffer, buffer_size);
170  }
171  auto ptr1 = buffer_copy ? buffer_copy.get() : buffer;
172 #else
173  auto ptr1 = buffer;
174 #endif // HAVE_CUDA
175  auto ptr2 = ptr1 + offsetBufferOff();
176  auto ptr3 = ptr1 + countBufferOff();
177  auto ptr4 = ptr1 + payloadBufferOff();
178  CHECK(hash_table);
179  const auto layout = getHashType();
180  return HashTable::toString(
181  "keyed",
182  getHashTypeString(layout),
183  getKeyComponentCount() + (layout == HashType::OneToOne ? 1 : 0),
185  hash_table->getEntryCount(),
186  ptr1,
187  ptr2,
188  ptr3,
189  ptr4,
190  buffer_size,
191  raw);
192 }
193 
194 std::set<DecodedJoinHashBufferEntry> BaselineJoinHashTable::toSet(
195  const ExecutorDeviceType device_type,
196  const int device_id) const {
197  auto buffer = getJoinHashBuffer(device_type, device_id);
198  auto hash_table = getHashTableForDevice(device_id);
199  CHECK(hash_table);
200  auto buffer_size = hash_table->getHashTableBufferSize(device_type);
201 #ifdef HAVE_CUDA
202  std::unique_ptr<int8_t[]> buffer_copy;
203  if (device_type == ExecutorDeviceType::GPU) {
204  buffer_copy = std::make_unique<int8_t[]>(buffer_size);
205  auto& data_mgr = catalog_->getDataMgr();
206  auto device_allocator = std::make_unique<CudaAllocator>(
207  &data_mgr, device_id, getQueryEngineCudaStreamForDevice(device_id));
208  device_allocator->copyFromDevice(buffer_copy.get(), buffer, buffer_size);
209  }
210  auto ptr1 = buffer_copy ? buffer_copy.get() : buffer;
211 #else
212  auto ptr1 = buffer;
213 #endif // HAVE_CUDA
214  auto ptr2 = ptr1 + offsetBufferOff();
215  auto ptr3 = ptr1 + countBufferOff();
216  auto ptr4 = ptr1 + payloadBufferOff();
217  const auto layout = hash_table->getLayout();
218  return HashTable::toSet(getKeyComponentCount() + (layout == HashType::OneToOne ? 1 : 0),
220  hash_table->getEntryCount(),
221  ptr1,
222  ptr2,
223  ptr3,
224  ptr4,
225  buffer_size);
226 }
227 
229  const std::vector<InnerOuter>& inner_outer_pairs,
230  const std::vector<InnerOuterStringOpInfos>& inner_outer_string_op_infos_pairs,
231  const Executor* executor) {
232  const auto num_col_pairs = inner_outer_pairs.size();
233  CHECK_EQ(num_col_pairs, inner_outer_string_op_infos_pairs.size());
234  for (size_t col_pair_idx = 0; col_pair_idx < num_col_pairs; ++col_pair_idx) {
235  if (needs_dictionary_translation(inner_outer_pairs[col_pair_idx],
236  inner_outer_string_op_infos_pairs[col_pair_idx],
237  executor)) {
238  return true;
239  }
240  }
241  return false;
242 }
243 
244 void BaselineJoinHashTable::reify(const HashType preferred_layout) {
245  auto timer = DEBUG_TIMER(__func__);
247  const auto composite_key_info = HashJoin::getCompositeKeyInfo(
251 
256  executor_);
257 
258  auto layout = preferred_layout;
259  if (condition_->is_overlaps_oper()) {
260  CHECK_EQ(inner_outer_pairs_.size(), size_t(1));
261 
262  if (inner_outer_pairs_[0].second->get_type_info().is_array()) {
263  layout = HashType::ManyToMany;
264  } else {
265  layout = HashType::OneToMany;
266  }
267  try {
268  reifyWithLayout(layout);
269  return;
270  } catch (const std::exception& e) {
271  VLOG(1) << "Caught exception while building overlaps baseline hash table: "
272  << e.what();
273  throw;
274  }
275  }
276 
277  // Automatically prefer One-To-Many layouts when string operations are involved as these
278  // tend to be cardinality-reducing operations.
279  // Todo(todd): Ostensibly only string ops on the rhs/inner expression cause rhs dups and
280  // so we may be too conservative here, but validate
281 
282  for (const auto& inner_outer_string_op_infos : inner_outer_string_op_infos_pairs_) {
283  if (inner_outer_string_op_infos.first.size() ||
284  inner_outer_string_op_infos.second.size()) {
285  layout = HashType::OneToMany;
286  break;
287  }
288  }
289 
290  try {
291  reifyWithLayout(layout);
292  } catch (const std::exception& e) {
293  VLOG(1) << "Caught exception while building baseline hash table: " << e.what();
294  // In perfect hash we CHECK that the layout is not OnetoMany here, but for baseline
295  // we are catching all exceptions, so should determine if that is safe first
296  // before we would CHECK and not throw an exception here
297  if (layout == HashType::OneToMany) {
298  throw(e);
299  }
302  }
303 }
304 
306  const auto& query_info = get_inner_query_info(getInnerTableId(), query_infos_).info;
307  if (query_info.fragments.empty()) {
308  return;
309  }
310 
311  const auto total_entries = 2 * query_info.getNumTuplesUpperBound();
312  if (total_entries > static_cast<size_t>(std::numeric_limits<int32_t>::max())) {
313  throw TooManyHashEntries();
314  }
315 
316  std::vector<std::unique_ptr<CudaAllocator>> dev_buff_owners;
317  std::vector<std::vector<Fragmenter_Namespace::FragmentInfo>> fragments_per_device;
318  std::vector<ColumnsForDevice> columns_per_device;
319  const auto shard_count = shardCount();
320  auto entries_per_device =
321  get_entries_per_device(total_entries, shard_count, device_count_, memory_level_);
322  auto data_mgr = executor_->getDataMgr();
323  // cached hash table lookup logic is similar with perfect join hash table
324  // first, prepare fragment lists per device
325  std::vector<ChunkKey> chunk_key_per_device;
326  for (int device_id = 0; device_id < device_count_; ++device_id) {
327  fragments_per_device.emplace_back(
328  shard_count
329  ? only_shards_for_device(query_info.fragments, device_id, device_count_)
330  : query_info.fragments);
332  dev_buff_owners.emplace_back(std::make_unique<CudaAllocator>(
333  data_mgr, device_id, getQueryEngineCudaStreamForDevice(device_id)));
334  }
335  const auto chunk_key = genChunkKey(fragments_per_device[device_id]);
336  chunk_key_per_device.emplace_back(std::move(chunk_key));
337  }
338 
339  // prepare per-device cache key
340  auto inner_outer_pairs = HashJoin::normalizeColumnPairs(
341  condition_.get(), *executor_->getCatalog(), executor_->getTemporaryTables());
342  const auto& inner_outer_cols = inner_outer_pairs.first;
343  const auto& col_pairs_string_op_infos = inner_outer_pairs.second;
344  auto hashtable_access_path_info =
346  col_pairs_string_op_infos,
347  condition_->get_optype(),
348  join_type_,
351  shard_count,
352  fragments_per_device,
353  executor_);
354  hashtable_cache_key_ = hashtable_access_path_info.hashed_query_plan_dag;
355  hashtable_cache_meta_info_ = hashtable_access_path_info.meta_info;
356  table_keys_ = hashtable_access_path_info.table_keys;
357 
358  // the actual chunks fetched per device can be different but they constitute the same
359  // table in the same db, so we can exploit this to create an alternative table key
360  if (table_keys_.empty()) {
362  chunk_key_per_device,
363  executor_->getCatalog()->getDatabaseId(),
364  getInnerTableId());
365  }
366  CHECK(!table_keys_.empty());
367 
368  if (HashtableRecycler::isInvalidHashTableCacheKey(hashtable_cache_key_) &&
369  getInnerTableId() > 0) {
370  // sometimes we cannot retrieve query plan dag, so try to recycler cache
371  // with the old-passioned cache key if we deal with hashtable of non-temporary table
372  for (int device_id = 0; device_id < device_count_; ++device_id) {
373  const auto num_tuples = std::accumulate(fragments_per_device[device_id].begin(),
374  fragments_per_device[device_id].end(),
375  size_t(0),
376  [](const auto& sum, const auto& fragment) {
377  return sum + fragment.getNumTuples();
378  });
381  num_tuples,
382  condition_->get_optype(),
383  join_type_,
384  chunk_key_per_device[device_id]};
385  hashtable_cache_key_[device_id] = getAlternativeCacheKey(cache_key);
386  }
387  }
388 
389  // register a mapping between cache key and input tables of the hash table
390  const auto invalid_cache_key =
391  HashtableRecycler::isInvalidHashTableCacheKey(hashtable_cache_key_);
392  if (!invalid_cache_key) {
393  if (!shard_count) {
394  hash_table_cache_->addQueryPlanDagForTableKeys(hashtable_cache_key_.front(),
395  table_keys_);
396  } else {
397  std::for_each(hashtable_cache_key_.cbegin(),
398  hashtable_cache_key_.cend(),
399  [this](QueryPlanHash key) {
400  hash_table_cache_->addQueryPlanDagForTableKeys(key, table_keys_);
401  });
402  }
403  }
404 
405  // now, let's try to check whether we have a cached hash table for this join qual
406  const auto effective_memory_level = getEffectiveMemoryLevel(inner_outer_pairs_);
407 
408  // todo (yoonmin) : support dictionary proxy cache for join including string op(s)
409  if (effective_memory_level == Data_Namespace::CPU_LEVEL) {
410  std::unique_lock<std::mutex> str_proxy_translation_lock(str_proxy_translation_mutex_);
411  if (str_proxy_translation_maps_.empty()) {
412  const auto composite_key_info = HashJoin::getCompositeKeyInfo(
415  composite_key_info, inner_outer_string_op_infos_pairs_, executor_);
417  }
418  }
419 
420  auto allow_hashtable_recycling =
425  bool has_invalid_cached_hash_table = false;
426  if (effective_memory_level == Data_Namespace::CPU_LEVEL &&
428  allow_hashtable_recycling, invalid_cache_key, join_type_)) {
429  // build a hash table on CPU, and we have a chance to recycle the cached one if
430  // available
431  for (int device_id = 0; device_id < device_count_; ++device_id) {
432  auto hash_table =
433  initHashTableOnCpuFromCache(hashtable_cache_key_[device_id],
436  if (hash_table) {
437  hash_tables_for_device_[device_id] = hash_table;
438  } else {
439  has_invalid_cached_hash_table = true;
440  break;
441  }
442  }
443 
444  if (has_invalid_cached_hash_table) {
445  hash_tables_for_device_.clear();
446  hash_tables_for_device_.resize(device_count_);
447  } else {
449 #ifdef HAVE_CUDA
450  auto data_mgr = executor_->getDataMgr();
451  for (int device_id = 0; device_id < device_count_; ++device_id) {
452  auto cpu_hash_table = std::dynamic_pointer_cast<BaselineHashTable>(
453  hash_tables_for_device_[device_id]);
454  copyCpuHashTableToGpu(cpu_hash_table, device_id, data_mgr);
455  }
456 #else
457  UNREACHABLE();
458 #endif
459  }
460  return;
461  }
462  }
463 
464  // we have no cached hash table for this qual
465  // so, start building the hash table by fetching columns for devices
466  for (int device_id = 0; device_id < device_count_; ++device_id) {
467  const auto columns_for_device =
468  fetchColumnsForDevice(fragments_per_device[device_id],
469  device_id,
471  ? dev_buff_owners[device_id].get()
472  : nullptr);
473  columns_per_device.push_back(columns_for_device);
474  }
475 
476  auto hashtable_layout_type = layout;
477  size_t emitted_keys_count = 0;
478  if (hashtable_layout_type == HashType::OneToMany) {
479  CHECK(!columns_per_device.front().join_columns.empty());
480  emitted_keys_count = columns_per_device.front().join_columns.front().num_elems;
481  size_t tuple_count;
482  std::tie(tuple_count, std::ignore) = approximateTupleCount(columns_per_device);
483  const auto entry_count = 2 * std::max(tuple_count, size_t(1));
484 
485  // reset entries per device with one to many info
486  entries_per_device =
487  get_entries_per_device(entry_count, shard_count, device_count_, memory_level_);
488  }
489  std::vector<std::future<void>> init_threads;
490  for (int device_id = 0; device_id < device_count_; ++device_id) {
491  const auto fragments =
492  shard_count
493  ? only_shards_for_device(query_info.fragments, device_id, device_count_)
494  : query_info.fragments;
495  init_threads.push_back(std::async(std::launch::async,
497  this,
498  columns_per_device[device_id],
499  hashtable_layout_type,
500  device_id,
501  entries_per_device,
502  emitted_keys_count,
503  logger::thread_id()));
504  }
505  for (auto& init_thread : init_threads) {
506  init_thread.wait();
507  }
508  for (auto& init_thread : init_threads) {
509  init_thread.get();
510  }
511 }
512 
514  const std::vector<ColumnsForDevice>& columns_per_device) const {
515  const auto effective_memory_level = getEffectiveMemoryLevel(inner_outer_pairs_);
516  CountDistinctDescriptor count_distinct_desc{
518  0,
519  11,
520  true,
521  effective_memory_level == Data_Namespace::MemoryLevel::GPU_LEVEL
524  1};
525  const auto padded_size_bytes = count_distinct_desc.bitmapPaddedSizeBytes();
526 
527  CHECK(!columns_per_device.empty() && !columns_per_device.front().join_columns.empty());
528 
529  if (effective_memory_level == Data_Namespace::MemoryLevel::CPU_LEVEL) {
530  int thread_count = cpu_threads();
531  std::vector<uint8_t> hll_buffer_all_cpus(thread_count * padded_size_bytes);
532  auto hll_result = &hll_buffer_all_cpus[0];
533 
534  approximate_distinct_tuples(hll_result,
535  count_distinct_desc.bitmap_sz_bits,
536  padded_size_bytes,
537  columns_per_device.front().join_columns,
538  columns_per_device.front().join_column_types,
539  thread_count);
540  for (int i = 1; i < thread_count; ++i) {
541  hll_unify(hll_result,
542  hll_result + i * padded_size_bytes,
543  1 << count_distinct_desc.bitmap_sz_bits);
544  }
545  return std::make_pair(hll_size(hll_result, count_distinct_desc.bitmap_sz_bits), 0);
546  }
547 #ifdef HAVE_CUDA
548  auto data_mgr = executor_->getDataMgr();
549  std::vector<std::vector<uint8_t>> host_hll_buffers(device_count_);
550  for (auto& host_hll_buffer : host_hll_buffers) {
551  host_hll_buffer.resize(count_distinct_desc.bitmapPaddedSizeBytes());
552  }
553  std::vector<std::future<void>> approximate_distinct_device_threads;
554  for (int device_id = 0; device_id < device_count_; ++device_id) {
555  approximate_distinct_device_threads.emplace_back(std::async(
557  [device_id,
558  &columns_per_device,
559  &count_distinct_desc,
560  data_mgr,
561  &host_hll_buffers] {
562  auto allocator = std::make_unique<CudaAllocator>(
563  data_mgr, device_id, getQueryEngineCudaStreamForDevice(device_id));
564  auto device_hll_buffer =
565  allocator->alloc(count_distinct_desc.bitmapPaddedSizeBytes());
566  data_mgr->getCudaMgr()->zeroDeviceMem(
567  device_hll_buffer,
568  count_distinct_desc.bitmapPaddedSizeBytes(),
569  device_id,
571  const auto& columns_for_device = columns_per_device[device_id];
572  auto join_columns_gpu = transfer_vector_of_flat_objects_to_gpu(
573  columns_for_device.join_columns, *allocator);
574  auto join_column_types_gpu = transfer_vector_of_flat_objects_to_gpu(
575  columns_for_device.join_column_types, *allocator);
576  const auto key_handler =
577  GenericKeyHandler(columns_for_device.join_columns.size(),
578  true,
579  join_columns_gpu,
580  join_column_types_gpu,
581  nullptr,
582  nullptr);
583  const auto key_handler_gpu =
584  transfer_flat_object_to_gpu(key_handler, *allocator);
586  reinterpret_cast<uint8_t*>(device_hll_buffer),
587  count_distinct_desc.bitmap_sz_bits,
588  key_handler_gpu,
589  columns_for_device.join_columns[0].num_elems);
590 
591  auto& host_hll_buffer = host_hll_buffers[device_id];
592  allocator->copyFromDevice(&host_hll_buffer[0],
593  device_hll_buffer,
594  count_distinct_desc.bitmapPaddedSizeBytes());
595  }));
596  }
597  for (auto& child : approximate_distinct_device_threads) {
598  child.get();
599  }
600  CHECK_EQ(Data_Namespace::MemoryLevel::GPU_LEVEL, effective_memory_level);
601  auto& result_hll_buffer = host_hll_buffers.front();
602  auto hll_result = reinterpret_cast<int32_t*>(&result_hll_buffer[0]);
603  for (int device_id = 1; device_id < device_count_; ++device_id) {
604  auto& host_hll_buffer = host_hll_buffers[device_id];
605  hll_unify(hll_result,
606  reinterpret_cast<int32_t*>(&host_hll_buffer[0]),
607  1 << count_distinct_desc.bitmap_sz_bits);
608  }
609  return std::make_pair(hll_size(hll_result, count_distinct_desc.bitmap_sz_bits), 0);
610 #else
611  UNREACHABLE();
612  return {0, 0};
613 #endif // HAVE_CUDA
614 }
615 
617  const std::vector<Fragmenter_Namespace::FragmentInfo>& fragments,
618  const int device_id,
619  DeviceAllocator* dev_buff_owner) {
620  const auto effective_memory_level =
622 
623  std::vector<JoinColumn> join_columns;
624  std::vector<std::shared_ptr<Chunk_NS::Chunk>> chunks_owner;
625  std::vector<JoinColumnTypeInfo> join_column_types;
626  std::vector<JoinBucketInfo> join_bucket_info;
627  std::vector<std::shared_ptr<void>> malloc_owner;
628  for (const auto& inner_outer_pair : inner_outer_pairs_) {
629  const auto inner_col = inner_outer_pair.first;
630  const auto inner_cd = get_column_descriptor_maybe(
631  inner_col->get_column_id(), inner_col->get_table_id(), *catalog_);
632  if (inner_cd && inner_cd->isVirtualCol) {
634  }
635  join_columns.emplace_back(fetchJoinColumn(inner_col,
636  fragments,
637  effective_memory_level,
638  device_id,
639  chunks_owner,
640  dev_buff_owner,
641  malloc_owner,
642  executor_,
643  &column_cache_));
644  const auto& ti = inner_col->get_type_info();
645  join_column_types.emplace_back(JoinColumnTypeInfo{static_cast<size_t>(ti.get_size()),
646  0,
647  0,
649  isBitwiseEq(),
650  0,
652  }
653  return {join_columns, join_column_types, chunks_owner, join_bucket_info, malloc_owner};
654 }
655 
657  const HashType layout,
658  const int device_id,
659  const size_t entry_count,
660  const size_t emitted_keys_count,
661  const logger::ThreadId parent_thread_id) {
662  DEBUG_TIMER_NEW_THREAD(parent_thread_id);
663  const auto effective_memory_level =
665  const auto err = initHashTableForDevice(columns_for_device.join_columns,
666  columns_for_device.join_column_types,
667  columns_for_device.join_buckets,
668  layout,
669  effective_memory_level,
670  entry_count,
671  emitted_keys_count,
672  device_id);
673  if (err) {
674  throw HashJoinFail(
675  std::string("Unrecognized error when initializing baseline hash table (") +
676  std::to_string(err) + std::string(")"));
677  }
678 }
679 
682  return 0;
683  }
686 }
687 
689  for (const auto& inner_outer_pair : inner_outer_pairs_) {
690  const auto inner_col = inner_outer_pair.first;
691  const auto& inner_col_ti = inner_col->get_type_info();
692  if (inner_col_ti.get_logical_size() > 4) {
693  CHECK_EQ(8, inner_col_ti.get_logical_size());
694  return 8;
695  }
696  }
697  return 4;
698 }
699 
701  return inner_outer_pairs_.size();
702 }
703 
705  const std::vector<InnerOuter>& inner_outer_pairs) const {
707  inner_outer_pairs, inner_outer_string_op_infos_pairs_, executor_)) {
710  }
711  return memory_level_;
712 }
713 
715  std::shared_ptr<BaselineHashTable>& cpu_hash_table,
716  const int device_id,
717  Data_Namespace::DataMgr* data_mgr) {
719 
720  builder.allocateDeviceMemory(cpu_hash_table->getLayout(),
723  cpu_hash_table->getEntryCount(),
724  cpu_hash_table->getEmittedKeysCount(),
725  device_id,
726  executor_);
727  auto gpu_target_hash_table = builder.getHashTable();
728  CHECK(gpu_target_hash_table);
729 
730  const auto gpu_buff = gpu_target_hash_table->getGpuBuffer();
731  CHECK(gpu_buff);
732  auto allocator = std::make_unique<CudaAllocator>(
733  data_mgr, device_id, getQueryEngineCudaStreamForDevice(device_id));
734  allocator->copyToDevice(
735  gpu_buff,
736  cpu_hash_table->getCpuBuffer(),
737  cpu_hash_table->getHashTableBufferSize(ExecutorDeviceType::CPU));
738  hash_tables_for_device_[device_id] = std::move(gpu_target_hash_table);
739 }
740 
742  const std::vector<const StringDictionaryProxy::IdMap*>& str_proxy_translation_maps) {
743  StrProxyTranslationMapsPtrsAndOffsets translation_map_ptrs_and_offsets;
744  // First element of pair is vector of int32_t* pointing to translation map "vector"
745  // Second element of pair is vector of int32_t of min inner dictionary ids (offsets)
746  const size_t num_translation_maps = str_proxy_translation_maps.size();
747  translation_map_ptrs_and_offsets.first.reserve(num_translation_maps);
748  translation_map_ptrs_and_offsets.second.reserve(num_translation_maps);
749  for (const auto& str_proxy_translation_map : str_proxy_translation_maps) {
750  if (str_proxy_translation_map) {
751  translation_map_ptrs_and_offsets.first.emplace_back(
752  str_proxy_translation_map->data());
753  translation_map_ptrs_and_offsets.second.emplace_back(
754  str_proxy_translation_map->domainStart());
755  } else {
756  // dummy values
757  translation_map_ptrs_and_offsets.first.emplace_back(nullptr);
758  translation_map_ptrs_and_offsets.second.emplace_back(0);
759  }
760  }
761  return translation_map_ptrs_and_offsets;
762 }
763 
765  const std::vector<JoinColumn>& join_columns,
766  const std::vector<JoinColumnTypeInfo>& join_column_types,
767  const std::vector<JoinBucketInfo>& join_bucket_info,
768  const HashType layout,
769  const Data_Namespace::MemoryLevel effective_memory_level,
770  const size_t entry_count,
771  const size_t emitted_keys_count,
772  const int device_id) {
773  auto timer = DEBUG_TIMER(__func__);
774  const auto key_component_count = getKeyComponentCount();
775  int err = 0;
776  decltype(std::chrono::steady_clock::now()) ts1, ts2;
777  ts1 = std::chrono::steady_clock::now();
778  auto allow_hashtable_recycling =
783  HashType hashtable_layout = layout;
784  if (effective_memory_level == Data_Namespace::CPU_LEVEL) {
785  std::lock_guard<std::mutex> cpu_hash_table_buff_lock(cpu_hash_table_buff_mutex_);
786 
787  const auto composite_key_info = HashJoin::getCompositeKeyInfo(
789 
790  CHECK(!join_columns.empty());
791 
793  CHECK_EQ(device_id, 0);
794  }
795  CHECK_LT(static_cast<size_t>(device_id), hash_tables_for_device_.size());
796  std::shared_ptr<HashTable> hash_table{nullptr};
797  const auto str_proxy_translation_map_ptrs_and_offsets =
800 
801  const auto key_handler =
802  GenericKeyHandler(key_component_count,
803  true,
804  &join_columns[0],
805  &join_column_types[0],
806  &str_proxy_translation_map_ptrs_and_offsets.first[0],
807  &str_proxy_translation_map_ptrs_and_offsets.second[0]);
808  err = builder.initHashTableOnCpu(&key_handler,
809  composite_key_info,
810  join_columns,
811  join_column_types,
812  join_bucket_info,
813  str_proxy_translation_map_ptrs_and_offsets,
814  entry_count,
815  join_columns.front().num_elems,
816  hashtable_layout,
817  join_type_,
820  hash_tables_for_device_[device_id] = builder.getHashTable();
821  ts2 = std::chrono::steady_clock::now();
822  auto hashtable_build_time =
823  std::chrono::duration_cast<std::chrono::milliseconds>(ts2 - ts1).count();
824  if (!err && allow_hashtable_recycling && hash_tables_for_device_[device_id]) {
825  // add ht-related items to cache iff we have a valid hashtable
828  hash_tables_for_device_[device_id],
830  hashtable_build_time);
831 
832  hash_table_layout_cache_->putItemToCache(
833  hashtable_cache_key_[device_id],
834  hash_tables_for_device_[device_id]->getLayout(),
837  0,
838  0,
839  {});
840  }
841  // Transfer the hash table on the GPU if we've only built it on CPU
842  // but the query runs on GPU (join on dictionary encoded columns).
843  // Don't transfer the buffer if there was an error since we'll bail anyway.
844  if (memory_level_ == Data_Namespace::GPU_LEVEL && !err) {
845 #ifdef HAVE_CUDA
846  auto cpu_hash_table = std::dynamic_pointer_cast<BaselineHashTable>(
847  hash_tables_for_device_[device_id]);
848  copyCpuHashTableToGpu(cpu_hash_table, device_id, executor_->getDataMgr());
849 #else
850  CHECK(false);
851 #endif
852  }
853  } else {
854 #ifdef HAVE_CUDA
856 
857  auto data_mgr = executor_->getDataMgr();
858  CudaAllocator allocator(
859  data_mgr, device_id, getQueryEngineCudaStreamForDevice(device_id));
860  auto join_column_types_gpu =
861  transfer_vector_of_flat_objects_to_gpu(join_column_types, allocator);
862  auto join_columns_gpu =
863  transfer_vector_of_flat_objects_to_gpu(join_columns, allocator);
864  const auto key_handler = GenericKeyHandler(key_component_count,
865  true,
866  join_columns_gpu,
867  join_column_types_gpu,
868  nullptr,
869  nullptr);
870 
871  err = builder.initHashTableOnGpu(&key_handler,
872  join_columns,
873  hashtable_layout,
874  join_type_,
877  entry_count,
878  emitted_keys_count,
879  device_id,
880  executor_);
881  CHECK_LT(static_cast<size_t>(device_id), hash_tables_for_device_.size());
882  hash_tables_for_device_[device_id] = builder.getHashTable();
883  if (!err && allow_hashtable_recycling && hash_tables_for_device_[device_id]) {
884  // add layout to cache iff we have a valid hashtable
885  hash_table_layout_cache_->putItemToCache(
886  hashtable_cache_key_[device_id],
887  hash_tables_for_device_[device_id]->getLayout(),
890  0,
891  0,
892  {});
893  }
894 #else
895  UNREACHABLE();
896 #endif
897  }
898  return err;
899 }
900 
901 #define LL_CONTEXT executor_->cgen_state_->context_
902 #define LL_BUILDER executor_->cgen_state_->ir_builder_
903 #define LL_INT(v) executor_->cgen_state_->llInt(v)
904 #define LL_FP(v) executor_->cgen_state_->llFp(v)
905 #define ROW_FUNC executor_->cgen_state_->row_func_
906 
908  const size_t index) {
909  AUTOMATIC_IR_METADATA(executor_->cgen_state_.get());
911  const auto key_component_width = getKeyComponentWidth();
912  CHECK(key_component_width == 4 || key_component_width == 8);
913  auto key_buff_lv = codegenKey(co);
914  const auto hash_ptr = hashPtr(index);
915  const auto key_ptr_lv =
916  LL_BUILDER.CreatePointerCast(key_buff_lv, llvm::Type::getInt8PtrTy(LL_CONTEXT));
917  const auto key_size_lv = LL_INT(getKeyComponentCount() * key_component_width);
918  const auto hash_table = getHashTableForDevice(size_t(0));
919  return executor_->cgen_state_->emitExternalCall(
920  "baseline_hash_join_idx_" + std::to_string(key_component_width * 8),
922  {hash_ptr, key_ptr_lv, key_size_lv, LL_INT(hash_table->getEntryCount())});
923 }
924 
926  const CompilationOptions& co,
927  const size_t index) {
928  AUTOMATIC_IR_METADATA(executor_->cgen_state_.get());
929  const auto hash_table = getHashTableForDevice(size_t(0));
930  CHECK(hash_table);
931  const auto key_component_width = getKeyComponentWidth();
932  CHECK(key_component_width == 4 || key_component_width == 8);
933  auto key_buff_lv = codegenKey(co);
935  auto hash_ptr = HashJoin::codegenHashTableLoad(index, executor_);
936  const auto composite_dict_ptr_type =
937  llvm::Type::getIntNPtrTy(LL_CONTEXT, key_component_width * 8);
938  const auto composite_key_dict =
939  hash_ptr->getType()->isPointerTy()
940  ? LL_BUILDER.CreatePointerCast(hash_ptr, composite_dict_ptr_type)
941  : LL_BUILDER.CreateIntToPtr(hash_ptr, composite_dict_ptr_type);
942  const auto key_component_count = getKeyComponentCount();
943  const auto key = executor_->cgen_state_->emitExternalCall(
944  "get_composite_key_index_" + std::to_string(key_component_width * 8),
946  {key_buff_lv,
947  LL_INT(key_component_count),
948  composite_key_dict,
949  LL_INT(hash_table->getEntryCount())});
950  auto one_to_many_ptr = hash_ptr;
951  if (one_to_many_ptr->getType()->isPointerTy()) {
952  one_to_many_ptr =
953  LL_BUILDER.CreatePtrToInt(hash_ptr, llvm::Type::getInt64Ty(LL_CONTEXT));
954  } else {
955  CHECK(one_to_many_ptr->getType()->isIntegerTy(64));
956  }
957  const auto composite_key_dict_size = offsetBufferOff();
958  one_to_many_ptr =
959  LL_BUILDER.CreateAdd(one_to_many_ptr, LL_INT(composite_key_dict_size));
961  {one_to_many_ptr, key, LL_INT(int64_t(0)), LL_INT(hash_table->getEntryCount() - 1)},
962  false,
963  false,
964  false,
966  executor_);
967 }
968 
969 size_t BaselineJoinHashTable::offsetBufferOff() const noexcept {
970  return getKeyBufferSize();
971 }
972 
973 size_t BaselineJoinHashTable::countBufferOff() const noexcept {
976  } else {
977  return getKeyBufferSize();
978  }
979 }
980 
984  } else {
985  return getKeyBufferSize();
986  }
987 }
988 
990  const auto key_component_width = getKeyComponentWidth();
991  CHECK(key_component_width == 4 || key_component_width == 8);
992  const auto key_component_count = getKeyComponentCount();
993  auto hash_table = getHashTableForDevice(size_t(0));
994  CHECK(hash_table);
995  if (layoutRequiresAdditionalBuffers(hash_table->getLayout())) {
996  return hash_table->getEntryCount() * key_component_count * key_component_width;
997  } else {
998  return hash_table->getEntryCount() * (key_component_count + 1) * key_component_width;
999  }
1000 }
1001 
1003  const auto hash_table = getHashTableForDevice(size_t(0));
1004  return hash_table->getEntryCount() * sizeof(int32_t);
1005 }
1006 
1008  AUTOMATIC_IR_METADATA(executor_->cgen_state_.get());
1009  const auto key_component_width = getKeyComponentWidth();
1010  CHECK(key_component_width == 4 || key_component_width == 8);
1011  const auto key_size_lv = LL_INT(getKeyComponentCount() * key_component_width);
1012  llvm::Value* key_buff_lv{nullptr};
1013  switch (key_component_width) {
1014  case 4:
1015  key_buff_lv =
1016  LL_BUILDER.CreateAlloca(llvm::Type::getInt32Ty(LL_CONTEXT), key_size_lv);
1017  break;
1018  case 8:
1019  key_buff_lv =
1020  LL_BUILDER.CreateAlloca(llvm::Type::getInt64Ty(LL_CONTEXT), key_size_lv);
1021  break;
1022  default:
1023  CHECK(false);
1024  }
1025 
1026  CodeGenerator code_generator(executor_);
1027  for (size_t i = 0; i < getKeyComponentCount(); ++i) {
1028  const auto key_comp_dest_lv = LL_BUILDER.CreateGEP(
1029  key_buff_lv->getType()->getScalarType()->getPointerElementType(),
1030  key_buff_lv,
1031  LL_INT(i));
1032  const auto& inner_outer_pair = inner_outer_pairs_[i];
1033  const auto outer_col = inner_outer_pair.second;
1034  const auto key_col_var = dynamic_cast<const Analyzer::ColumnVar*>(outer_col);
1035  const auto val_col_var =
1036  dynamic_cast<const Analyzer::ColumnVar*>(inner_outer_pair.first);
1037  if (key_col_var && val_col_var &&
1039  key_col_var,
1040  val_col_var,
1041  get_max_rte_scan_table(executor_->cgen_state_->scan_idx_to_hash_pos_))) {
1042  throw std::runtime_error(
1043  "Query execution fails because the query contains not supported self-join "
1044  "pattern. We suspect the query requires multiple left-deep join tree due to "
1045  "the join condition of the self-join and is not supported for now. Please "
1046  "consider rewriting table order in "
1047  "FROM clause.");
1048  }
1049  auto key_lv = HashJoin::codegenColOrStringOper(
1050  outer_col, inner_outer_string_op_infos_pairs_[i].second, code_generator, co);
1051  const auto key_lv_ext =
1052  LL_BUILDER.CreateSExt(key_lv, get_int_type(key_component_width * 8, LL_CONTEXT));
1053  LL_BUILDER.CreateStore(key_lv_ext, key_comp_dest_lv);
1054  }
1055  return key_buff_lv;
1056 }
1057 
1058 llvm::Value* BaselineJoinHashTable::hashPtr(const size_t index) {
1059  AUTOMATIC_IR_METADATA(executor_->cgen_state_.get());
1060  auto hash_ptr = HashJoin::codegenHashTableLoad(index, executor_);
1061  const auto pi8_type = llvm::Type::getInt8PtrTy(LL_CONTEXT);
1062  return hash_ptr->getType()->isPointerTy()
1063  ? LL_BUILDER.CreatePointerCast(hash_ptr, pi8_type)
1064  : LL_BUILDER.CreateIntToPtr(hash_ptr, pi8_type);
1065 }
1066 
1067 #undef ROW_FUNC
1068 #undef LL_INT
1069 #undef LL_BUILDER
1070 #undef LL_CONTEXT
1071 
1073  try {
1075  } catch (...) {
1076  CHECK(false);
1077  }
1078  return 0;
1079 }
1080 
1082  CHECK(!inner_outer_pairs_.empty());
1083  const auto first_inner_col = inner_outer_pairs_.front().first;
1084  return first_inner_col->get_rte_idx();
1085 }
1086 
1088  auto hash_table = getHashTableForDevice(size_t(0));
1089  CHECK(hash_table);
1090  if (layout_override_) {
1091  return *layout_override_;
1092  } else {
1093  return hash_table->getLayout();
1094  }
1095 }
1096 
1098  const std::vector<InnerOuter>& inner_outer_pairs) {
1099  CHECK(!inner_outer_pairs.empty());
1100  const auto first_inner_col = inner_outer_pairs.front().first;
1101  return first_inner_col->get_table_id();
1102 }
1103 
1105  QueryPlanHash key,
1106  CacheItemType item_type,
1107  DeviceIdentifier device_identifier) {
1108  auto timer = DEBUG_TIMER(__func__);
1109  VLOG(1) << "Checking CPU hash table cache.";
1111  return hash_table_cache_->getItemFromCache(key, item_type, device_identifier);
1112 }
1113 
1115  QueryPlanHash key,
1116  CacheItemType item_type,
1117  std::shared_ptr<HashTable> hashtable_ptr,
1118  DeviceIdentifier device_identifier,
1119  size_t hashtable_building_time) {
1121  CHECK(hashtable_ptr && !hashtable_ptr->getGpuBuffer());
1122  hash_table_cache_->putItemToCache(
1123  key,
1124  hashtable_ptr,
1125  item_type,
1126  device_identifier,
1127  hashtable_ptr->getHashTableBufferSize(ExecutorDeviceType::CPU),
1128  hashtable_building_time);
1129 }
1130 
1132  return condition_->get_optype() == kBW_EQ;
1133 }
1134 
1136  const std::vector<Fragmenter_Namespace::FragmentInfo>& fragments) const {
1137  std::vector<int> fragment_ids;
1138  std::for_each(
1139  fragments.cbegin(), fragments.cend(), [&fragment_ids](const auto& fragment) {
1140  fragment_ids.push_back(fragment.fragmentId);
1141  });
1142  return fragment_ids;
1143 }
size_t offsetBufferOff() const noexceptoverride
catalog_(nullptr)
std::set< DecodedJoinHashBufferEntry > toSet(const ExecutorDeviceType device_type, const int device_id) const override
#define CHECK_EQ(x, y)
Definition: Logger.h:230
std::vector< int > ChunkKey
Definition: types.h:36
void putHashTableOnCpuToCache(QueryPlanHash key, CacheItemType item_type, std::shared_ptr< HashTable > hashtable_ptr, DeviceIdentifier device_identifier, size_t hashtable_building_time)
size_t DeviceIdentifier
Definition: DataRecycler.h:129
virtual HashJoinMatchingSet codegenMatchingSet(const CompilationOptions &, const size_t)=0
JoinType
Definition: sqldefs.h:151
Fragmenter_Namespace::TableInfo info
Definition: InputMetadata.h:35
std::string toString(const ExecutorDeviceType device_type, const int device_id=0, bool raw=false) const override
static llvm::Value * codegenHashTableLoad(const size_t table_idx, Executor *executor)
Definition: HashJoin.cpp:257
bool self_join_not_covered_by_left_deep_tree(const Analyzer::ColumnVar *key_side, const Analyzer::ColumnVar *val_side, const int max_rte_covered)
std::vector< QueryPlanHash > hashtable_cache_key_
static bool isInvalidHashTableCacheKey(const std::vector< QueryPlanHash > &cache_keys)
Data_Namespace::MemoryLevel getEffectiveMemoryLevel(const std::vector< InnerOuter > &inner_outer_pairs) const
static bool canAccessHashTable(bool allow_hash_table_recycling, bool invalid_cache_key, JoinType join_type)
Definition: HashJoin.cpp:1026
ExecutorDeviceType
Data_Namespace::DataMgr & getDataMgr() const
Definition: Catalog.h:243
int initHashTableOnCpu(KEY_HANDLER *key_handler, const CompositeKeyInfo &composite_key_info, const std::vector< JoinColumn > &join_columns, const std::vector< JoinColumnTypeInfo > &join_column_types, const std::vector< JoinBucketInfo > &join_bucket_info, const StrProxyTranslationMapsPtrsAndOffsets &str_proxy_translation_maps_ptrs_and_offsets, const size_t keyspace_entry_count, const size_t keys_for_all_rows, const HashType layout, const JoinType join_type, const size_t key_component_width, const size_t key_component_count)
T * transfer_flat_object_to_gpu(const T &object, DeviceAllocator &allocator)
static std::pair< std::vector< InnerOuter >, std::vector< InnerOuterStringOpInfos > > normalizeColumnPairs(const Analyzer::BinOper *condition, const Catalog_Namespace::Catalog &cat, const TemporaryTables *temporary_tables)
Definition: HashJoin.cpp:989
HashJoinMatchingSet codegenMatchingSet(const CompilationOptions &, const size_t) override
void hll_unify(T1 *lhs, T2 *rhs, const size_t m)
Definition: HyperLogLog.h:107
JoinColumn fetchJoinColumn(const Analyzer::ColumnVar *hash_col, const std::vector< Fragmenter_Namespace::FragmentInfo > &fragment_info, const Data_Namespace::MemoryLevel effective_memory_level, const int device_id, std::vector< std::shared_ptr< Chunk_NS::Chunk >> &chunks_owner, DeviceAllocator *dev_buff_owner, std::vector< std::shared_ptr< void >> &malloc_owner, Executor *executor, ColumnCacheMap *column_cache)
Definition: HashJoin.cpp:58
std::vector< std::shared_ptr< HashTable > > hash_tables_for_device_
Definition: HashJoin.h:351
BaselineJoinHashTable(const std::shared_ptr< Analyzer::BinOper > condition, const JoinType join_type, const std::vector< InputTableInfo > &query_infos, const Data_Namespace::MemoryLevel memory_level, ColumnCacheMap &column_cache, Executor *executor, const std::vector< InnerOuter > &inner_outer_pairs, const std::vector< InnerOuterStringOpInfos > &col_pairs_string_op_infos, const int device_count, const HashTableBuildDagMap &hashtable_build_dag_map, const TableIdToNodeMap &table_id_to_node_map)
#define UNREACHABLE()
Definition: Logger.h:266
Data_Namespace::MemoryLevel get_effective_memory_level(const Data_Namespace::MemoryLevel memory_level, const bool needs_dict_translation)
HashTableBuildDagMap hashtable_build_dag_map_
size_t getKeyBufferSize() const noexcept
#define DEBUG_TIMER_NEW_THREAD(parent_thread_id)
Definition: Logger.h:376
const TableIdToNodeMap table_id_to_node_map_
size_t getComponentBufferSize() const noexceptoverride
bool needs_dictionary_translation(const std::vector< InnerOuter > &inner_outer_pairs, const std::vector< InnerOuterStringOpInfos > &inner_outer_string_op_infos_pairs, const Executor *executor)
static llvm::Value * codegenColOrStringOper(const Analyzer::Expr *col_or_string_oper, const std::vector< StringOps_Namespace::StringOpInfo > &string_op_infos, CodeGenerator &code_generator, const CompilationOptions &co)
Definition: HashJoin.cpp:539
void freeHashBufferMemory()
Definition: HashJoin.h:311
llvm::Type * get_int_type(const int width, llvm::LLVMContext &context)
size_t hll_size(const T *M, const size_t bitmap_sz_bits)
Definition: HyperLogLog.h:88
#define CHECK_GT(x, y)
Definition: Logger.h:234
const int get_max_rte_scan_table(std::unordered_map< int, llvm::Value * > &scan_idx_to_hash_pos)
int getInnerTableRteIdx() const noexceptoverride
std::string to_string(char const *&&v)
std::unordered_set< size_t > table_keys_
virtual void reifyForDevice(const ColumnsForDevice &columns_for_device, const HashType layout, const int device_id, const size_t entry_count, const size_t emitted_keys_count, const logger::ThreadId parent_thread_id)
virtual ColumnsForDevice fetchColumnsForDevice(const std::vector< Fragmenter_Namespace::FragmentInfo > &fragments, const int device_id, DeviceAllocator *dev_buff_owner)
const std::vector< InputTableInfo > & query_infos_
virtual llvm::Value * codegenKey(const CompilationOptions &)
std::shared_ptr< HashTable > initHashTableOnCpuFromCache(QueryPlanHash key, CacheItemType item_type, DeviceIdentifier device_identifier)
size_t payloadBufferOff() const noexceptoverride
std::vector< InnerOuter > inner_outer_pairs_
const std::vector< JoinColumnTypeInfo > join_column_types
Definition: HashJoin.h:101
void reify(const HashType preferred_layout)
void approximate_distinct_tuples(uint8_t *hll_buffer_all_cpus, const uint32_t b, const size_t padded_size_bytes, const std::vector< JoinColumn > &join_column_per_key, const std::vector< JoinColumnTypeInfo > &type_info_per_key, const int thread_count)
future< Result > async(Fn &&fn, Args &&...args)
std::unordered_map< size_t, HashTableBuildDag > HashTableBuildDagMap
HashType getHashType() const noexceptoverride
static QueryPlanHash getAlternativeCacheKey(AlternativeCacheKeyForBaselineHashJoin &info)
const ColumnDescriptor * get_column_descriptor_maybe(const int col_id, const int table_id, const Catalog_Namespace::Catalog &cat)
Definition: Execute.h:220
CacheItemType
Definition: DataRecycler.h:38
std::vector< InnerOuterStringOpInfos > inner_outer_string_op_infos_pairs_
static std::unique_ptr< HashtableRecycler > hash_table_cache_
ColumnCacheMap & column_cache_
#define LL_INT(v)
std::unordered_map< int, const RelAlgNode * > TableIdToNodeMap
std::vector< Fragmenter_Namespace::FragmentInfo > only_shards_for_device(const std::vector< Fragmenter_Namespace::FragmentInfo > &fragments, const int device_id, const int device_count)
int8_t * getJoinHashBuffer(const ExecutorDeviceType device_type, const int device_id) const
Definition: HashJoin.h:288
DEVICE auto accumulate(ARGS &&...args)
Definition: gpu_enabled.h:42
virtual std::pair< size_t, size_t > approximateTupleCount(const std::vector< ColumnsForDevice > &) const
static std::vector< const StringDictionaryProxy::IdMap * > translateCompositeStrDictProxies(const CompositeKeyInfo &composite_key_info, const std::vector< InnerOuterStringOpInfos > &string_op_infos_for_keys, const Executor *executor)
Definition: HashJoin.cpp:503
HashtableCacheMetaInfo hashtable_cache_meta_info_
int initHashTableOnGpu(KEY_HANDLER *key_handler, const std::vector< JoinColumn > &join_columns, const HashType layout, const JoinType join_type, const size_t key_component_width, const size_t key_component_count, const size_t keyspace_entry_count, const size_t emitted_keys_count, const int device_id, const Executor *executor)
#define LL_CONTEXT
#define AUTOMATIC_IR_METADATA(CGENSTATE)
static void checkHashJoinReplicationConstraint(const int table_id, const size_t shard_count, const Executor *executor)
Definition: HashJoin.cpp:771
virtual void reifyWithLayout(const HashType layout)
std::unordered_map< int, std::unordered_map< int, std::shared_ptr< const ColumnarResults >>> ColumnCacheMap
static std::shared_ptr< BaselineJoinHashTable > getInstance(const std::shared_ptr< Analyzer::BinOper > condition, const std::vector< InputTableInfo > &query_infos, const Data_Namespace::MemoryLevel memory_level, const JoinType join_type, const HashType preferred_hash_type, const int device_count, ColumnCacheMap &column_cache, Executor *executor, const HashTableBuildDagMap &hashtable_build_dag_map, const TableIdToNodeMap &table_id_to_node_map)
Make hash table from an in-flight SQL query&#39;s parse tree etc.
HashTable * getHashTableForDevice(const size_t device_id) const
Definition: HashJoin.h:269
#define VLOGGING(n)
Definition: Logger.h:220
const InputTableInfo & get_inner_query_info(const int inner_table_id, const std::vector< InputTableInfo > &query_infos)
#define CHECK_LT(x, y)
Definition: Logger.h:232
int getInnerTableId() const noexceptoverride
std::pair< std::vector< const int32_t * >, std::vector< int32_t >> StrProxyTranslationMapsPtrsAndOffsets
#define LL_BUILDER
std::unique_ptr< BaselineHashTable > getHashTable()
static std::string getHashTypeString(HashType ht) noexcept
Definition: HashJoin.h:154
static std::unordered_set< size_t > getAlternativeTableKeys(const std::vector< ChunkKey > &chunk_keys, int db_id, int inner_table_id)
Definition: DataRecycler.h:154
std::optional< HashType > layout_override_
static std::string toString(const std::string &type, const std::string &layout_type, size_t key_component_count, size_t key_component_width, size_t entry_count, const int8_t *ptr1, const int8_t *ptr2, const int8_t *ptr3, const int8_t *ptr4, size_t buffer_size, bool raw=false)
Decode hash table into a human-readable string.
Definition: HashTable.cpp:226
size_t get_entries_per_device(const size_t total_entries, const size_t shard_count, const size_t device_count, const Data_Namespace::MemoryLevel memory_level)
const Catalog_Namespace::Catalog * catalog_
uint64_t ThreadId
Definition: Logger.h:364
size_t QueryPlanHash
CUstream getQueryEngineCudaStreamForDevice(int device_num)
Definition: QueryEngine.cpp:7
const Data_Namespace::MemoryLevel memory_level_
llvm::Value * hashPtr(const size_t index)
ThreadId thread_id()
Definition: Logger.cpp:820
void allocateDeviceMemory(const HashType layout, const size_t key_component_width, const size_t key_component_count, const size_t keyspace_entry_count, const size_t emitted_keys_count, const int device_id, const Executor *executor)
virtual int initHashTableForDevice(const std::vector< JoinColumn > &join_columns, const std::vector< JoinColumnTypeInfo > &join_column_types, const std::vector< JoinBucketInfo > &join_buckets, const HashType layout, const Data_Namespace::MemoryLevel effective_memory_level, const size_t entry_count, const size_t emitted_keys_count, const int device_id)
void approximate_distinct_tuples_on_device(uint8_t *hll_buffer, const uint32_t b, const GenericKeyHandler *key_handler, const int64_t num_elems)
ColumnType get_join_column_type_kind(const SQLTypeInfo &ti)
bool g_enable_watchdog false
Definition: Execute.cpp:79
llvm::Value * codegenSlot(const CompilationOptions &, const size_t) override
bool isBitwiseEq() const override
#define CHECK(condition)
Definition: Logger.h:222
#define DEBUG_TIMER(name)
Definition: Logger.h:371
Definition: sqldefs.h:30
virtual size_t getKeyComponentCount() const
int64_t inline_fixed_encoding_null_val(const SQL_TYPE_INFO &ti)
void copyCpuHashTableToGpu(std::shared_ptr< BaselineHashTable > &cpu_hash_table, const int device_id, Data_Namespace::DataMgr *data_mgr)
ChunkKey genChunkKey(const std::vector< Fragmenter_Namespace::FragmentInfo > &fragments) const
std::vector< const StringDictionaryProxy::IdMap * > str_proxy_translation_maps_
virtual size_t getKeyComponentWidth() const
static DecodedJoinHashBufferSet toSet(size_t key_component_count, size_t key_component_width, size_t entry_count, const int8_t *ptr1, const int8_t *ptr2, const int8_t *ptr3, const int8_t *ptr4, size_t buffer_size)
Decode hash table into a std::set for easy inspection and validation.
Definition: HashTable.cpp:139
T * transfer_vector_of_flat_objects_to_gpu(const std::vector< T > &vec, DeviceAllocator &allocator)
Allocate GPU memory using GpuBuffers via DataMgr.
static size_t getShardCountForCondition(const Analyzer::BinOper *condition, const Executor *executor, const std::vector< InnerOuter > &inner_outer_pairs)
std::vector< JoinBucketInfo > join_buckets
Definition: HashJoin.h:103
static std::unique_ptr< HashingSchemeRecycler > hash_table_layout_cache_
static constexpr DeviceIdentifier CPU_DEVICE_IDENTIFIER
Definition: DataRecycler.h:136
int cpu_threads()
Definition: thread_count.h:24
size_t get_shard_count(const Analyzer::BinOper *join_condition, const Executor *executor)
Definition: HashJoin.cpp:1045
StrProxyTranslationMapsPtrsAndOffsets decomposeStrDictTranslationMaps(const std::vector< const StringDictionaryProxy::IdMap * > &str_proxy_translation_maps)
static HashtableAccessPathInfo getHashtableAccessPathInfo(const std::vector< InnerOuter > &inner_outer_pairs, const std::vector< InnerOuterStringOpInfos > &inner_outer_string_op_infos_pairs, const SQLOps op_type, const JoinType join_type, const HashTableBuildDagMap &hashtable_build_dag_map, int device_count, int shard_count, const std::vector< std::vector< Fragmenter_Namespace::FragmentInfo >> &frags_for_device, Executor *executor)
HashType
Definition: HashTable.h:19
const std::vector< JoinColumn > join_columns
Definition: HashJoin.h:100
#define VLOG(n)
Definition: Logger.h:316
static bool layoutRequiresAdditionalBuffers(HashType layout) noexcept
Definition: HashJoin.h:150
const std::shared_ptr< Analyzer::BinOper > condition_
static bool isSafeToCacheHashtable(const TableIdToNodeMap &table_id_to_node_map, bool need_dict_translation, const std::vector< InnerOuterStringOpInfos > &inner_outer_string_op_info_pairs, const int table_id)
size_t countBufferOff() const noexceptoverride
static CompositeKeyInfo getCompositeKeyInfo(const std::vector< InnerOuter > &inner_outer_pairs, const Executor *executor, const std::vector< InnerOuterStringOpInfos > &inner_outer_string_op_infos_pairs={})
Definition: HashJoin.cpp:455