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::RadixPartitionedHashTable Class Reference
Collaboration diagram for duckdb::RadixPartitionedHashTable:

Public Member Functions

 RadixPartitionedHashTable (GroupingSet &grouping_set, const GroupedAggregateData &op, TupleDataValidityType group_validity)
unique_ptr< GroupedAggregateHashTable > CreateHT (ClientContext &context, const idx_t capacity, const idx_t radix_bits) const
unique_ptr< GlobalSinkState > GetGlobalSinkState (ClientContext &context) const
 Sink Interface.
unique_ptr< LocalSinkState > GetLocalSinkState (ExecutionContext &context) const
void Sink (ExecutionContext &context, DataChunk &chunk, OperatorSinkInput &input, DataChunk &aggregate_input_chunk, const unsafe_vector< idx_t > &filter) const
void Combine (ExecutionContext &context, GlobalSinkState &gstate, LocalSinkState &lstate) const
void Finalize (ClientContext &context, GlobalSinkState &gstate) const
unique_ptr< GlobalSourceState > GetGlobalSourceState (ClientContext &context) const
 Source interface.
unique_ptr< LocalSourceState > GetLocalSourceState (ExecutionContext &context) const
SourceResultType GetData (ExecutionContext &context, DataChunk &chunk, GlobalSinkState &sink, OperatorSourceInput &input) const
ProgressData GetProgress (ClientContext &context, GlobalSinkState &sink_p, GlobalSourceState &gstate) const
shared_ptr< TupleDataLayout > GetLayoutPtr () const
const TupleDataLayout & GetLayout () const
idx_t MaxThreads (GlobalSinkState &sink) const

Static Public Member Functions

static void SetMultiScan (GlobalSinkState &sink)

Public Attributes

GroupingSet & grouping_set
unsafe_vector< idx_t > null_groups
 The indices specified in the groups_count that do not appear in the grouping_set.
const GroupedAggregateData & op
vector< LogicalType > group_types
vector< Value > grouping_values
 The GROUPING values that belong to this hash table.
const TupleDataValidityType group_validity
 Whether there are no NULLs in the groups.

Private Member Functions

void SetGroupingValues ()
void PopulateGroupChunk (DataChunk &group_chunk, DataChunk &input_chunk) const

Private Attributes

shared_ptr< TupleDataLayout > layout_ptr

Constructor & Destructor Documentation

◆ RadixPartitionedHashTable()

duckdb::RadixPartitionedHashTable::RadixPartitionedHashTable ( GroupingSet & grouping_set,
const GroupedAggregateData & op,
TupleDataValidityType group_validity )
206985 : grouping_set(grouping_set_p), op(op_p), group_validity(group_validity_p) {
206986 auto groups_count = op.GroupCount();
206987 for (idx_t i = 0; i < groups_count; i++) {
206988 if (grouping_set.find(i) == grouping_set.end()) {
206989 null_groups.push_back(i);
206990 }
206991 }
206992 if (grouping_set.empty()) {
206993 // Fake a single group with a constant value for aggregation without groups
206994 group_types.emplace_back(LogicalType::TINYINT);
206995 }
206996 for (auto &entry : grouping_set) {
206997 D_ASSERT(entry < op.group_types.size());
206998 group_types.push_back(op.group_types[entry]);
206999 }
207000 SetGroupingValues();
207001
207002 auto group_types_copy = group_types;
207003 group_types_copy.emplace_back(LogicalType::HASH);
207004
207005 auto layout = make_shared_ptr<TupleDataLayout>();
207006 auto aggregate_objects = AggregateObject::CreateAggregateObjects(op.bindings);
207007 layout->Initialize(std::move(group_types_copy), std::move(aggregate_objects), group_validity);
207008 layout_ptr = std::move(layout);
207009}
const TupleDataValidityType group_validity
Whether there are no NULLs in the groups.
Definition duckdb.cpp:99130
unsafe_vector< idx_t > null_groups
The indices specified in the groups_count that do not appear in the grouping_set.
Definition duckdb.cpp:99124
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237

Member Function Documentation

◆ CreateHT()

unique_ptr< GroupedAggregateHashTable > duckdb::RadixPartitionedHashTable::CreateHT ( ClientContext & context,
const idx_t capacity,
const idx_t radix_bits ) const
207039 {
207040 return make_uniq<GroupedAggregateHashTable>(context, BufferAllocator::Get(context), group_types, op.payload_types,
207041 op.bindings, capacity, radix_bits, group_validity);
207042}

◆ GetGlobalSinkState()

unique_ptr< GlobalSinkState > duckdb::RadixPartitionedHashTable::GetGlobalSinkState ( ClientContext & context) const

Sink Interface.

207368 {
207369 return make_uniq<RadixHTGlobalSinkState>(context, *this);
207370}

◆ GetLocalSinkState()

unique_ptr< LocalSinkState > duckdb::RadixPartitionedHashTable::GetLocalSinkState ( ExecutionContext & context) const
207372 {
207373 return make_uniq<RadixHTLocalSinkState>(context.client, *this);
207374}

◆ Sink()

void duckdb::RadixPartitionedHashTable::Sink ( ExecutionContext & context,
DataChunk & chunk,
OperatorSinkInput & input,
DataChunk & aggregate_input_chunk,
const unsafe_vector< idx_t > & filter ) const
207498 {
207499 auto &gstate = input.global_state.Cast<RadixHTGlobalSinkState>();
207500 auto &lstate = input.local_state.Cast<RadixHTLocalSinkState>();
207501 if (!lstate.ht) {
207502 lstate.local_sink_capacity = gstate.config.sink_capacity;
207503 lstate.ht = CreateHT(context.client, lstate.local_sink_capacity, gstate.config.GetRadixBits());
207504 if (gstate.number_of_threads > RadixHTConfig::GROW_STRATEGY_THREAD_THRESHOLD) {
207505 // Not using grow strategy, so we enable the HLL to potentially adapt later
207506 lstate.ht->EnableHLL(true);
207507 } else {
207508 // Using grow strategy, so won't ever adapt
207509 lstate.adapted = true;
207510 }
207511 gstate.active_threads++;
207512 }
207513
207514 auto &group_chunk = lstate.group_chunk;
207515 PopulateGroupChunk(group_chunk, chunk);
207516
207517 auto &ht = *lstate.ht;
207518 ht.AddChunk(group_chunk, payload_input, filter);
207519
207520 // Decide whether we should adapt our strategy to the data
207521 if (!lstate.adapted && lstate.ht->GetSinkCount() >= RadixHTLocalSinkState::ADAPTIVITY_THRESHOLD) {
207522 DecideAdaptation(gstate, lstate);
207523 ht.EnableHLL(false); // Can be disabled now (costs 5-10% performance in worst case, single column distinct)
207524 lstate.adapted = true;
207525 }
207526
207527 if (ht.Count() + STANDARD_VECTOR_SIZE < GroupedAggregateHashTable::ResizeThreshold(lstate.local_sink_capacity)) {
207528 return; // We can fit another chunk
207529 }
207530
207531 if (gstate.number_of_threads > RadixHTConfig::GROW_STRATEGY_THREAD_THRESHOLD || gstate.external) {
207532 // 'Reset' the HT without taking its data, we can just keep appending to the same collection
207533 // This only works because we never resize the HT
207534 // We don't do this when running with 1 or 2 threads, it only makes sense when there's many threads
207535 ht.Abandon();
207536 }
207537
207538 // Check if we need to repartition
207539 const auto radix_bits_before = ht.GetRadixBits();
207540 MaybeRepartition(context.client, gstate, lstate, false);
207541 const auto repartitioned = radix_bits_before != ht.GetRadixBits();
207542
207543 if (repartitioned && ht.Count() != 0) {
207544 // We repartitioned, but we didn't clear the pointer table / reset the count because we're on 1 or 2 threads
207545 ht.Abandon();
207546 if (gstate.external) {
207547 ht.Resize(lstate.local_sink_capacity);
207548 }
207549 }
207550
207551 // TODO: combine early and often
207552}
idx_t ResizeThreshold() const
Threshold at which to resize the HT.
Definition duckdb.cpp:146215
static constexpr idx_t ADAPTIVITY_THRESHOLD
Definition duckdb.cpp:207349
void DecideAdaptation(RadixHTGlobalSinkState &gstate, RadixHTLocalSinkState &lstate)
Definition duckdb.cpp:207391
static constexpr idx_t GROW_STRATEGY_THREAD_THRESHOLD
If we have this many or less threads, we grow the HT, otherwise we abandon.
Definition duckdb.cpp:207113

◆ Combine()

void duckdb::RadixPartitionedHashTable::Combine ( ExecutionContext & context,
GlobalSinkState & gstate,
LocalSinkState & lstate ) const
207555 {
207556 auto &gstate = gstate_p.Cast<RadixHTGlobalSinkState>();
207557 auto &lstate = lstate_p.Cast<RadixHTLocalSinkState>();
207558 if (!lstate.ht) {
207559 return;
207560 }
207561
207562 // Set any_combined, then check one last time whether we need to repartition
207563 gstate.any_combined = true;
207564 MaybeRepartition(context.client, gstate, lstate, true);
207565
207566 auto &ht = *lstate.ht;
207567 auto lstate_data = ht.AcquirePartitionedData();
207568 if (lstate.abandoned_data) {
207569 D_ASSERT(gstate.external);
207570 D_ASSERT(lstate.abandoned_data->PartitionCount() == lstate.ht->GetPartitionedData().PartitionCount());
207571 D_ASSERT(lstate.abandoned_data->PartitionCount() ==
207572 RadixPartitioning::NumberOfPartitions(gstate.config.GetRadixBits()));
207573 lstate.abandoned_data->Combine(*lstate_data);
207574 } else {
207575 lstate.abandoned_data = std::move(lstate_data);
207576 }
207577
207578 auto aggregate_allocator = ht.GetAggregateAllocator();
207579
207580 // Eagerly destroy the HT
207581 lstate.ht.reset();
207582
207583 auto guard = gstate.Lock();
207584 D_ASSERT(!gstate.finalized);
207585 if (gstate.uncombined_data) {
207586 gstate.uncombined_data->Combine(*lstate.abandoned_data);
207587 } else {
207588 gstate.uncombined_data = std::move(lstate.abandoned_data);
207589 }
207590 gstate.stored_allocators.emplace_back(std::move(aggregate_allocator));
207591 gstate.stored_allocators_size += gstate.stored_allocators.back()->AllocationSize();
207592}
static constexpr idx_t NumberOfPartitions(idx_t radix_bits)
The number of partitions for a given number of radix bits.
Definition duckdb.cpp:93425

◆ Finalize()

void duckdb::RadixPartitionedHashTable::Finalize ( ClientContext & context,
GlobalSinkState & gstate ) const
207594 {
207595 auto &gstate = gstate_p.Cast<RadixHTGlobalSinkState>();
207596 auto guard = gstate.Lock();
207597 D_ASSERT(!gstate.finalized);
207598
207599 if (gstate.uncombined_data) {
207600 auto &uncombined_data = *gstate.uncombined_data;
207601 gstate.count_before_combining = uncombined_data.Count();
207602
207603 // If true there is no need to combine, it was all done by a single thread in a single HT
207604 const auto single_ht = !gstate.external && gstate.active_threads == 1 && gstate.number_of_threads == 1;
207605
207606 auto &uncombined_partition_data = uncombined_data.GetPartitions();
207607 const auto n_partitions = uncombined_partition_data.size();
207608 gstate.partitions.reserve(n_partitions);
207609 for (idx_t i = 0; i < n_partitions; i++) {
207610 auto &partition = uncombined_partition_data[i];
207611 auto partition_size =
207612 partition->SizeInBytes() +
207613 GroupedAggregateHashTable::GetCapacityForCount(partition->Count()) * sizeof(ht_entry_t);
207614 gstate.max_partition_size = MaxValue(gstate.max_partition_size, partition_size);
207615
207616 gstate.partitions.emplace_back(make_uniq<AggregatePartition>(std::move(partition)));
207617 if (single_ht) {
207618 gstate.finalize_done++;
207619 gstate.partitions.back()->progress = 1;
207620 gstate.partitions.back()->state = AggregatePartitionState::READY_TO_SCAN;
207621 }
207622 }
207623 } else {
207624 gstate.count_before_combining = 0;
207625 }
207626
207627 // Minimum of combining one partition at a time
207628 gstate.temporary_memory_state->SetMinimumReservation(gstate.stored_allocators_size + gstate.max_partition_size);
207629 // Set size to 0 until the scan actually starts
207630 gstate.temporary_memory_state->SetZero();
207631 gstate.finalized = true;
207632}
static idx_t GetCapacityForCount(idx_t count)
Capacity that can hold 'count' entries without resizing.
Definition duckdb.cpp:146206
int partition(const std::vector< _Tp > &_vec, std::vector< int > &labels, _EqPredicate predicate=_EqPredicate())
@ READY_TO_SCAN
Finalized, ready to scan.
Definition duckdb.cpp:207053

◆ GetGlobalSourceState()

unique_ptr< GlobalSourceState > duckdb::RadixPartitionedHashTable::GetGlobalSourceState ( ClientContext & context) const

Source interface.

207728 {
207729 return make_uniq<RadixHTGlobalSourceState>(context, *this);
207730}

◆ GetLocalSourceState()

unique_ptr< LocalSourceState > duckdb::RadixPartitionedHashTable::GetLocalSourceState ( ExecutionContext & context) const
207732 {
207733 return make_uniq<RadixHTLocalSourceState>(context, *this);
207734}

◆ GetData()

SourceResultType duckdb::RadixPartitionedHashTable::GetData ( ExecutionContext & context,
DataChunk & chunk,
GlobalSinkState & sink,
OperatorSourceInput & input ) const
207924 {
207925 auto &sink = sink_p.Cast<RadixHTGlobalSinkState>();
207926 D_ASSERT(sink.finalized);
207927
207928 auto &gstate = input.global_state.Cast<RadixHTGlobalSourceState>();
207929 auto &lstate = input.local_state.Cast<RadixHTLocalSourceState>();
207930 D_ASSERT(sink.scan_pin_properties == TupleDataPinProperties::UNPIN_AFTER_DONE ||
207931 sink.scan_pin_properties == TupleDataPinProperties::DESTROY_AFTER_DONE);
207932
207933 if (gstate.finished) {
207934 return SourceResultType::FINISHED;
207935 }
207936
207937 if (sink.count_before_combining == 0) {
207938 if (grouping_set.empty()) {
207939 // Special case hack to sort out aggregating from empty intermediates for aggregations without groups
207940 D_ASSERT(chunk.ColumnCount() == null_groups.size() + op.aggregates.size() + op.grouping_functions.size());
207941 // For each column in the aggregates, set to initial state
207942 chunk.SetCardinality(1);
207943 for (auto null_group : null_groups) {
207944 chunk.data[null_group].SetVectorType(VectorType::CONSTANT_VECTOR);
207945 ConstantVector::SetNull(chunk.data[null_group], true);
207946 }
207947 ArenaAllocator allocator(BufferAllocator::Get(context.client));
207948 for (idx_t i = 0; i < op.aggregates.size(); i++) {
207949 D_ASSERT(op.aggregates[i]->GetExpressionClass() == ExpressionClass::BOUND_AGGREGATE);
207950 auto &aggr = op.aggregates[i]->Cast<BoundAggregateExpression>();
207951 auto aggr_state =
207952 make_unsafe_uniq_array_uninitialized<data_t>(aggr.function.GetStateSizeCallback()(aggr.function));
207953 aggr.function.GetStateInitCallback()(aggr.function, aggr_state.get());
207954
207955 AggregateInputData aggr_input_data(aggr.bind_info.get(), allocator);
207956 Vector state_vector(Value::POINTER(CastPointerToValue(aggr_state.get())));
207957 aggr.function.GetStateFinalizeCallback()(state_vector, aggr_input_data,
207958 chunk.data[null_groups.size() + i], 1, 0);
207959 if (aggr.function.HasStateDestructorCallback()) {
207960 aggr.function.GetStateDestructorCallback()(state_vector, aggr_input_data, 1);
207961 }
207962 }
207963 // Place the grouping values (all the groups of the grouping_set condensed into a single value)
207964 // Behind the null groups + aggregates
207965 for (idx_t i = 0; i < op.grouping_functions.size(); i++) {
207966 chunk.data[null_groups.size() + op.aggregates.size() + i].Reference(grouping_values[i]);
207967 }
207968 }
207969 gstate.finished = true;
207970 return SourceResultType::FINISHED;
207971 }
207972
207973 while (!gstate.finished && chunk.size() == 0) {
207974 if (lstate.TaskFinished()) {
207975 const auto res = gstate.AssignTask(sink, lstate, input.interrupt_state);
207976 if (res != SourceResultType::HAVE_MORE_OUTPUT) {
207977 D_ASSERT(res == SourceResultType::FINISHED || res == SourceResultType::BLOCKED);
207978 return res;
207979 }
207980 }
207981 lstate.ExecuteTask(sink, gstate, chunk);
207982 }
207983
207984 if (chunk.size() != 0) {
207985 return SourceResultType::HAVE_MORE_OUTPUT;
207986 } else {
207987 return SourceResultType::FINISHED;
207988 }
207989}
vector< Vector > data
The vectors owned by the DataChunk.
Definition duckdb.hpp:11445
vector< Value > grouping_values
The GROUPING values that belong to this hash table.
Definition duckdb.cpp:99128
static DUCKDB_API Value POINTER(uintptr_t value)
Create a pointer Value from a specified value.
Definition duckdb.cpp:132331
@ DESTROY_AFTER_DONE
Destroys blocks after they are done (for reading only).
Definition duckdb.cpp:56603
@ UNPIN_AFTER_DONE
Unpins blocks after they are done (for both reading/writing).
Definition duckdb.cpp:56601

◆ GetProgress()

ProgressData duckdb::RadixPartitionedHashTable::GetProgress ( ClientContext & context,
GlobalSinkState & sink_p,
GlobalSourceState & gstate ) const
207992 {
207993 auto &sink = sink_p.Cast<RadixHTGlobalSinkState>();
207994 auto &gstate = gstate_p.Cast<RadixHTGlobalSourceState>();
207995
207996 // Get partition combine progress, weigh it 2x
207997 ProgressData progress;
207998 for (auto &partition : sink.partitions) {
207999 progress.done += 2.0 * partition->progress;
208000 }
208001
208002 // Get scan progress, weigh it 1x
208003 progress.done += 1.0 * double(gstate.task_done);
208004
208005 // Divide by 3x for the weights, and the number of partitions to get a value between 0 and 1 again
208006 progress.total += 3.0 * double(sink.partitions.size());
208007
208008 return progress;
208009}

◆ GetLayoutPtr()

shared_ptr< TupleDataLayout > duckdb::RadixPartitionedHashTable::GetLayoutPtr ( ) const
207030 {
207031 return layout_ptr;
207032}

◆ GetLayout()

const TupleDataLayout & duckdb::RadixPartitionedHashTable::GetLayout ( ) const
207034 {
207035 return *layout_ptr;
207036}

◆ MaxThreads()

idx_t duckdb::RadixPartitionedHashTable::MaxThreads ( GlobalSinkState & sink) const
207637 {
207638 auto &sink = sink_p.Cast<RadixHTGlobalSinkState>();
207639 if (sink.partitions.empty()) {
207640 return 0;
207641 }
207642
207643 const auto max_threads = MinValue<idx_t>(
207644 NumericCast<idx_t>(TaskScheduler::GetScheduler(sink.context).NumberOfThreads()), sink.partitions.size());
207645 sink.temporary_memory_state->SetRemainingSizeAndUpdateReservation(
207646 sink.context, sink.stored_allocators_size + max_threads * sink.max_partition_size);
207647
207648 // we cannot spill aggregate state memory
207649 const auto usable_memory = sink.temporary_memory_state->GetReservation() > sink.stored_allocators_size
207650 ? sink.temporary_memory_state->GetReservation() - sink.stored_allocators_size
207651 : 0;
207652 // This many partitions will fit given our reservation (at least 1))
207653 const auto partitions_fit = MaxValue<idx_t>(usable_memory / sink.max_partition_size, 1);
207654
207655 // Mininum of the two
207656 return MinValue<idx_t>(partitions_fit, max_threads);
207657}

◆ SetMultiScan()

void duckdb::RadixPartitionedHashTable::SetMultiScan ( GlobalSinkState & sink)
static
207659 {
207660 auto &sink = sink_p.Cast<RadixHTGlobalSinkState>();
207661 sink.scan_pin_properties = TupleDataPinProperties::UNPIN_AFTER_DONE;
207662}

◆ SetGroupingValues()

void duckdb::RadixPartitionedHashTable::SetGroupingValues ( )
private
207011 {
207012 // Compute the GROUPING values:
207013 // For each parameter to the GROUPING clause, we check if the hash table groups on this particular group
207014 // If it does, we return 0, otherwise we return 1
207015 // We then use bitshifts to combine these values
207016 auto &grouping_functions = op.GetGroupingFunctions();
207017 for (auto &grouping : grouping_functions) {
207018 int64_t grouping_value = 0;
207019 D_ASSERT(grouping.size() < sizeof(int64_t) * 8);
207020 for (idx_t i = 0; i < grouping.size(); i++) {
207021 if (grouping_set.find(grouping[i]) == grouping_set.end()) {
207022 // We don't group on this value!
207023 grouping_value += 1LL << (grouping.size() - (i + 1));
207024 }
207025 }
207026 grouping_values.push_back(Value::BIGINT(grouping_value));
207027 }
207028}
static DUCKDB_API Value BIGINT(int64_t value)
Create a bigint Value from a specified value.
Definition duckdb.cpp:132146
::int64_t int64_t

◆ PopulateGroupChunk()

void duckdb::RadixPartitionedHashTable::PopulateGroupChunk ( DataChunk & group_chunk,
DataChunk & input_chunk ) const
private
207376 {
207377 idx_t chunk_index = 0;
207378 // Populate the group_chunk
207379 for (auto &group_idx : grouping_set) {
207380 // Retrieve the expression containing the index in the input chunk
207381 auto &group = op.groups[group_idx];
207382 D_ASSERT(group->GetExpressionType() == ExpressionType::BOUND_REF);
207383 auto &bound_ref_expr = group->Cast<BoundReferenceExpression>();
207384 // Reference from input_chunk[group.index] -> group_chunk[chunk_index]
207385 group_chunk.data[chunk_index++].Reference(input_chunk.data[bound_ref_expr.index]);
207386 }
207387 group_chunk.SetCardinality(input_chunk.size());
207388 group_chunk.Verify();
207389}
DUCKDB_API void Verify()
Definition duckdb.cpp:117115

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