Autonomy Software C++ 24.5.1
Welcome to the Autonomy Software repository of the Mars Rover Design Team (MRDT) at Missouri University of Science and Technology (Missouri S&T)! API reference contains the source code and other resources for the development of the autonomy software for our Mars rover. The Autonomy Software project aims to compete in the University Rover Challenge (URC) by demonstrating advanced autonomous capabilities and robust navigation algorithms.
Loading...
Searching...
No Matches
duckdb::PerfectHashJoinExecutor Class Reference

PhysicalHashJoin represents a hash loop join between two tables. More...

Collaboration diagram for duckdb::PerfectHashJoinExecutor:

Public Member Functions

 PerfectHashJoinExecutor (const PhysicalHashJoin &join, JoinHashTable &ht)
bool CanDoPerfectHashJoin (const PhysicalHashJoin &op, const Value &min, const Value &max)
unique_ptr< OperatorState > GetOperatorState (ExecutionContext &context)
OperatorResultType ProbePerfectHashTable (ExecutionContext &context, DataChunk &input, DataChunk &lhs_output_columns, DataChunk &chunk, OperatorState &state)
bool BuildPerfectHashTable (LogicalType &type)

Private Types

using PerfectHashTable = vector<buffer_ptr<VectorChildBuffer>>

Private Member Functions

void FillSelectionVectorSwitchProbe (Vector &source, SelectionVector &build_sel_vec, SelectionVector &probe_sel_vec, idx_t count, idx_t &probe_sel_count)
template<typename T>
void TemplatedFillSelectionVectorProbe (Vector &source, SelectionVector &build_sel_vec, SelectionVector &probe_sel_vec, idx_t count, idx_t &prob_sel_count)
bool FillSelectionVectorSwitchBuild (Vector &source, SelectionVector &sel_vec, SelectionVector &seq_sel_vec, idx_t count)
template<typename T>
bool TemplatedFillSelectionVectorBuild (Vector &source, SelectionVector &sel_vec, SelectionVector &seq_sel_vec, idx_t count)
bool FullScanHashTable (LogicalType &key_type)

Private Attributes

const PhysicalHashJoin & join
JoinHashTable & ht
PerfectHashTable perfect_hash_table
 Columnar perfect hash table.
PerfectHashJoinStats perfect_join_statistics
 Build statistics.
ValidityMask bitmap_build_idx
 Stores the occurrences of each value in the build side.
idx_t unique_keys = 0
 Stores the number of unique keys in the build side.

Detailed Description

PhysicalHashJoin represents a hash loop join between two tables.

Constructor & Destructor Documentation

◆ PerfectHashJoinExecutor()

duckdb::PerfectHashJoinExecutor::PerfectHashJoinExecutor ( const PhysicalHashJoin & join,
JoinHashTable & ht )
181200 : join(join_p), ht(ht_p) {
181201}

Member Function Documentation

◆ CanDoPerfectHashJoin()

bool duckdb::PerfectHashJoinExecutor::CanDoPerfectHashJoin ( const PhysicalHashJoin & op,
const Value & min,
const Value & max )
181257 {
181258 if (perfect_join_statistics.is_build_small) {
181259 return true; // Already true based on static statistics
181260 }
181261
181262 // We only do this optimization for inner joins with one integer equality condition
181263 const auto key_type = op.conditions[0].left->return_type;
181264 if (op.join_type != JoinType::INNER || op.conditions.size() != 1 ||
181265 op.conditions[0].comparison != ExpressionType::COMPARE_EQUAL || !TypeIsInteger(key_type.InternalType())) {
181266 return false;
181267 }
181268
181269 // We bail out if there are nested types on the RHS
181270 for (auto &type : op.children[1].get().GetTypes()) {
181271 switch (type.InternalType()) {
181273 case PhysicalType::LIST:
181274 case PhysicalType::ARRAY:
181275 return false;
181276 default:
181277 break;
181278 }
181279 }
181280
181281 // And when the build range is smaller than the threshold
181282 perfect_join_statistics.build_min = min;
181283 perfect_join_statistics.build_max = max;
181284 hugeint_t min_value, max_value;
181285 if (!ExtractNumericValue(perfect_join_statistics.build_min, min_value) ||
181286 !ExtractNumericValue(perfect_join_statistics.build_max, max_value)) {
181287 return false;
181288 }
181289 if (max_value < min_value) {
181290 return false; // Empty table
181291 }
181292
181293 hugeint_t build_range;
181294 if (!TrySubtractOperator::Operation(max_value, min_value, build_range)) {
181295 return false;
181296 }
181297
181298 // The max size our build must have to run the perfect HJ
181299 static constexpr idx_t MAX_BUILD_SIZE = 1048576;
181300 if (build_range > Hugeint::Convert(MAX_BUILD_SIZE)) {
181301 return false;
181302 }
181303 perfect_join_statistics.build_range = NumericCast<idx_t>(build_range);
181304
181305 // If count is larger than range (duplicates), we bail out
181306 if (ht.Count() > perfect_join_statistics.build_range) {
181307 return false;
181308 }
181309
181310 perfect_join_statistics.is_build_small = true;
181311 return true;
181312}
PerfectHashJoinStats perfect_join_statistics
Build statistics.
Definition duckdb.cpp:181074
static softfloat min()
static softfloat max()
@ LIST
A list of some logical data type.
Definition duckdb.hpp:2813
@ STRUCT
Struct of logical types.
Definition duckdb.hpp:2816
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237

◆ GetOperatorState()

unique_ptr< OperatorState > duckdb::PerfectHashJoinExecutor::GetOperatorState ( ExecutionContext & context)
181455 {
181456 auto state = make_uniq<PerfectHashJoinState>(context.client, join);
181457 return std::move(state);
181458}

◆ ProbePerfectHashTable()

OperatorResultType duckdb::PerfectHashJoinExecutor::ProbePerfectHashTable ( ExecutionContext & context,
DataChunk & input,
DataChunk & lhs_output_columns,
DataChunk & chunk,
OperatorState & state )
181462 {
181463 auto &state = state_p.Cast<PerfectHashJoinState>();
181464 // keeps track of how many probe keys have a match
181465 idx_t probe_sel_count = 0;
181466
181467 // fetch the join keys from the chunk
181468 state.join_keys.Reset();
181469 state.probe_executor.Execute(input, state.join_keys);
181470 // select the keys that are in the min-max range
181471 auto &keys_vec = state.join_keys.data[0];
181472 auto keys_count = state.join_keys.size();
181473 // todo: add check for fast pass when probe is part of build domain
181474 FillSelectionVectorSwitchProbe(keys_vec, state.build_sel_vec, state.probe_sel_vec, keys_count, probe_sel_count);
181475
181476 // If build is dense and probe is in build's domain, just reference probe
181477 if (perfect_join_statistics.is_build_dense && keys_count == probe_sel_count) {
181478 result.Reference(lhs_output_columns);
181479 } else {
181480 // otherwise, filter it out the values that do not match
181481 result.Slice(lhs_output_columns, state.probe_sel_vec, probe_sel_count, 0);
181482 }
181483 // on the build side, we need to fetch the data and build dictionary vectors with the sel_vec
181484 for (idx_t i = 0; i < join.rhs_output_columns.col_types.size(); i++) {
181485 auto &result_vector = result.data[lhs_output_columns.ColumnCount() + i];
181486 D_ASSERT(result_vector.GetType() == ht.layout_ptr->GetTypes()[ht.output_columns[i]]);
181487 result_vector.Dictionary(perfect_hash_table[i], state.build_sel_vec);
181488 }
181489 return OperatorResultType::NEED_MORE_INPUT;
181490}
PerfectHashTable perfect_hash_table
Columnar perfect hash table.
Definition duckdb.cpp:181072

◆ BuildPerfectHashTable()

bool duckdb::PerfectHashJoinExecutor::BuildPerfectHashTable ( LogicalType & type)
181317 {
181318 // First, allocate memory for each build column
181319 const auto build_size = perfect_join_statistics.build_range + 1;
181320 for (const auto &type : join.rhs_output_columns.col_types) {
181321 perfect_hash_table.emplace_back(DictionaryVector::CreateReusableDictionary(type, build_size));
181322 }
181323
181324 // and for duplicate_checking
181325 bitmap_build_idx.Initialize(build_size);
181326 bitmap_build_idx.SetAllInvalid(build_size);
181327
181328 // Now fill columns with build data
181329 return FullScanHashTable(key_type);
181330}
ValidityMask bitmap_build_idx
Stores the occurrences of each value in the build side.
Definition duckdb.cpp:181076

◆ FillSelectionVectorSwitchProbe()

void duckdb::PerfectHashJoinExecutor::FillSelectionVectorSwitchProbe ( Vector & source,
SelectionVector & build_sel_vec,
SelectionVector & probe_sel_vec,
idx_t count,
idx_t & probe_sel_count )
private
181494 {
181495 switch (source.GetType().InternalType()) {
181496 case PhysicalType::INT8:
181497 TemplatedFillSelectionVectorProbe<int8_t>(source, build_sel_vec, probe_sel_vec, count, probe_sel_count);
181498 break;
181499 case PhysicalType::INT16:
181500 TemplatedFillSelectionVectorProbe<int16_t>(source, build_sel_vec, probe_sel_vec, count, probe_sel_count);
181501 break;
181502 case PhysicalType::INT32:
181503 TemplatedFillSelectionVectorProbe<int32_t>(source, build_sel_vec, probe_sel_vec, count, probe_sel_count);
181504 break;
181505 case PhysicalType::INT64:
181506 TemplatedFillSelectionVectorProbe<int64_t>(source, build_sel_vec, probe_sel_vec, count, probe_sel_count);
181507 break;
181508 case PhysicalType::INT128:
181509 TemplatedFillSelectionVectorProbe<hugeint_t>(source, build_sel_vec, probe_sel_vec, count, probe_sel_count);
181510 break;
181511 case PhysicalType::UINT8:
181512 TemplatedFillSelectionVectorProbe<uint8_t>(source, build_sel_vec, probe_sel_vec, count, probe_sel_count);
181513 break;
181515 TemplatedFillSelectionVectorProbe<uint16_t>(source, build_sel_vec, probe_sel_vec, count, probe_sel_count);
181516 break;
181518 TemplatedFillSelectionVectorProbe<uint32_t>(source, build_sel_vec, probe_sel_vec, count, probe_sel_count);
181519 break;
181521 TemplatedFillSelectionVectorProbe<uint64_t>(source, build_sel_vec, probe_sel_vec, count, probe_sel_count);
181522 break;
181523 case PhysicalType::UINT128:
181524 TemplatedFillSelectionVectorProbe<uhugeint_t>(source, build_sel_vec, probe_sel_vec, count, probe_sel_count);
181525 break;
181526 default:
181527 throw NotImplementedException("Type not supported");
181528 }
181529}
@ UINT32
Unsigned 32-bit little-endian integer.
Definition duckdb.hpp:2758
@ UINT16
Unsigned 16-bit little-endian integer.
Definition duckdb.hpp:2752
@ INT64
Signed 64-bit little-endian integer.
Definition duckdb.hpp:2767
@ INT16
Signed 16-bit little-endian integer.
Definition duckdb.hpp:2755
@ INT32
Signed 32-bit little-endian integer.
Definition duckdb.hpp:2761
@ UINT64
Unsigned 64-bit little-endian integer.
Definition duckdb.hpp:2764
@ UINT8
Unsigned 8-bit little-endian integer.
Definition duckdb.hpp:2746
@ INT8
Signed 8-bit little-endian integer.
Definition duckdb.hpp:2749

◆ TemplatedFillSelectionVectorProbe()

template<typename T>
void duckdb::PerfectHashJoinExecutor::TemplatedFillSelectionVectorProbe ( Vector & source,
SelectionVector & build_sel_vec,
SelectionVector & probe_sel_vec,
idx_t count,
idx_t & prob_sel_count )
private
181534 {
181535 auto min_value = perfect_join_statistics.build_min.GetValueUnsafe<T>();
181536 auto max_value = perfect_join_statistics.build_max.GetValueUnsafe<T>();
181537
181538 UnifiedVectorFormat vector_data;
181539 source.ToUnifiedFormat(count, vector_data);
181540 auto data = reinterpret_cast<T *>(vector_data.data);
181541 auto validity_mask = &vector_data.validity;
181542 // build selection vector for non-dense build
181543 if (validity_mask->AllValid()) {
181544 for (idx_t i = 0, sel_idx = 0; i < count; ++i) {
181545 // retrieve value from vector
181546 auto data_idx = vector_data.sel->get_index(i);
181547 auto input_value = data[data_idx];
181548 // add index to selection vector if value in the range
181549 if (min_value <= input_value && input_value <= max_value) {
181550 auto idx = UnsafeNumericCast<idx_t>(input_value - min_value); // subtract min value to get the idx
181551 // position check for matches in the build
181552 if (bitmap_build_idx.RowIsValid(idx)) {
181553 build_sel_vec.set_index(sel_idx, idx);
181554 probe_sel_vec.set_index(sel_idx++, i);
181555 probe_sel_count++;
181556 }
181557 }
181558 }
181559 } else {
181560 for (idx_t i = 0, sel_idx = 0; i < count; ++i) {
181561 // retrieve value from vector
181562 auto data_idx = vector_data.sel->get_index(i);
181563 if (!validity_mask->RowIsValid(data_idx)) {
181564 continue;
181565 }
181566 auto input_value = data[data_idx];
181567 // add index to selection vector if value in the range
181568 if (min_value <= input_value && input_value <= max_value) {
181569 auto idx = UnsafeNumericCast<idx_t>(input_value - min_value); // subtract min value to get the idx
181570 // position check for matches in the build
181571 if (bitmap_build_idx.RowIsValid(idx)) {
181572 build_sel_vec.set_index(sel_idx, idx);
181573 probe_sel_vec.set_index(sel_idx++, i);
181574 probe_sel_count++;
181575 }
181576 }
181577 }
181578 }
181579}

◆ FillSelectionVectorSwitchBuild()

bool duckdb::PerfectHashJoinExecutor::FillSelectionVectorSwitchBuild ( Vector & source,
SelectionVector & sel_vec,
SelectionVector & seq_sel_vec,
idx_t count )
private
181375 {
181376 switch (source.GetType().InternalType()) {
181377 case PhysicalType::INT8:
181378 return TemplatedFillSelectionVectorBuild<int8_t>(source, sel_vec, seq_sel_vec, count);
181379 case PhysicalType::INT16:
181380 return TemplatedFillSelectionVectorBuild<int16_t>(source, sel_vec, seq_sel_vec, count);
181381 case PhysicalType::INT32:
181382 return TemplatedFillSelectionVectorBuild<int32_t>(source, sel_vec, seq_sel_vec, count);
181383 case PhysicalType::INT64:
181384 return TemplatedFillSelectionVectorBuild<int64_t>(source, sel_vec, seq_sel_vec, count);
181385 case PhysicalType::INT128:
181386 return TemplatedFillSelectionVectorBuild<hugeint_t>(source, sel_vec, seq_sel_vec, count);
181387 case PhysicalType::UINT8:
181388 return TemplatedFillSelectionVectorBuild<uint8_t>(source, sel_vec, seq_sel_vec, count);
181390 return TemplatedFillSelectionVectorBuild<uint16_t>(source, sel_vec, seq_sel_vec, count);
181392 return TemplatedFillSelectionVectorBuild<uint32_t>(source, sel_vec, seq_sel_vec, count);
181394 return TemplatedFillSelectionVectorBuild<uint64_t>(source, sel_vec, seq_sel_vec, count);
181395 case PhysicalType::UINT128:
181396 return TemplatedFillSelectionVectorBuild<uhugeint_t>(source, sel_vec, seq_sel_vec, count);
181397 default:
181398 throw NotImplementedException("Type not supported for perfect hash join");
181399 }
181400}

◆ TemplatedFillSelectionVectorBuild()

template<typename T>
bool duckdb::PerfectHashJoinExecutor::TemplatedFillSelectionVectorBuild ( Vector & source,
SelectionVector & sel_vec,
SelectionVector & seq_sel_vec,
idx_t count )
private
181404 {
181405 if (perfect_join_statistics.build_min.IsNull() || perfect_join_statistics.build_max.IsNull()) {
181406 return false;
181407 }
181408 auto min_value = perfect_join_statistics.build_min.GetValueUnsafe<T>();
181409 auto max_value = perfect_join_statistics.build_max.GetValueUnsafe<T>();
181410 UnifiedVectorFormat vector_data;
181411 source.ToUnifiedFormat(count, vector_data);
181412 const auto data = vector_data.GetData<T>();
181413 // generate the selection vector
181414 for (idx_t i = 0, sel_idx = 0; i < count; ++i) {
181415 auto data_idx = vector_data.sel->get_index(i);
181416 auto input_value = data[data_idx];
181417 // add index to selection vector if value in the range
181418 if (min_value <= input_value && input_value <= max_value) {
181419 auto idx = UnsafeNumericCast<idx_t>(input_value - min_value); // subtract min value to get the idx position
181420 sel_vec.set_index(sel_idx, idx);
181421 if (bitmap_build_idx.RowIsValidUnsafe(idx)) {
181422 return false;
181423 } else {
181424 bitmap_build_idx.SetValidUnsafe(idx);
181425 unique_keys++;
181426 }
181427 seq_sel_vec.set_index(sel_idx++, i);
181428 }
181429 }
181430 return true;
181431}
idx_t unique_keys
Stores the number of unique keys in the build side.
Definition duckdb.cpp:181078

◆ FullScanHashTable()

bool duckdb::PerfectHashJoinExecutor::FullScanHashTable ( LogicalType & key_type)
private
181332 {
181333 auto &data_collection = ht.GetDataCollection();
181334
181335 // TODO: In a parallel finalize: One should exclusively lock and each thread should do one part of the code below.
181336 Vector tuples_addresses(LogicalType::POINTER, ht.Count()); // allocate space for all the tuples
181337 Vector build_vector(key_type, ht.Count());
181338 auto key_count = ht.ScanKeyColumn(tuples_addresses, build_vector, 0);
181339
181340 // Now fill the selection vector using the build keys and create a sequential vector
181341 // TODO: add check for fast pass when probe is part of build domain
181342 SelectionVector sel_build(key_count + 1);
181343 SelectionVector sel_tuples(key_count + 1);
181344 bool success = FillSelectionVectorSwitchBuild(build_vector, sel_build, sel_tuples, key_count);
181345
181346 // early out
181347 if (!success) {
181348 return false;
181349 }
181350
181351 const auto build_size = perfect_join_statistics.build_range + 1;
181352 if (unique_keys == build_size && !ht.has_null) {
181353 perfect_join_statistics.is_build_dense = true;
181354 bitmap_build_idx.Reset(build_size); // All valid
181355 }
181356 key_count = unique_keys; // do not consider keys out of the range
181357
181358 // Full scan the remaining build columns and fill the perfect hash table
181359
181360 for (idx_t i = 0; i < join.rhs_output_columns.col_types.size(); i++) {
181361 auto &vector = perfect_hash_table[i]->data;
181362 const auto output_col_idx = ht.output_columns[i];
181363 D_ASSERT(vector.GetType() == ht.layout_ptr->GetTypes()[output_col_idx]);
181364 auto &col_mask = FlatVector::Validity(vector);
181365 col_mask.Reset(build_size);
181366 data_collection.Gather(tuples_addresses, sel_tuples, key_count, output_col_idx, vector, sel_build, nullptr);
181367 // This ensures the empty entries are set to NULL, so that the emitted dictionary vectors make sense
181368 col_mask.Combine(bitmap_build_idx, build_size);
181369 }
181370
181371 return true;
181372}

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