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

Public Member Functions

 PerfectAggregateHashTable (ClientContext &context, Allocator &allocator, const vector< LogicalType > &group_types, vector< LogicalType > payload_types_p, vector< AggregateObject > aggregate_objects, vector< Value > group_minima, vector< idx_t > required_bits)
void AddChunk (DataChunk &groups, DataChunk &payload)
 Add the given data to the HT.
void Combine (PerfectAggregateHashTable &other)
 Combines the target perfect aggregate HT into this one.
void Scan (idx_t &scan_position, DataChunk &result)
 Scan the HT starting from the scan_position.
Public Member Functions inherited from duckdb::BaseAggregateHashTable
 BaseAggregateHashTable (ClientContext &context, Allocator &allocator, const vector< AggregateObject > &aggregates, vector< LogicalType > payload_types)

Protected Attributes

Vector addresses
vector< idx_t > required_bits
 The required bits per group.
idx_t total_required_bits
 The total required bits for the HT (this determines the max capacity).
idx_t total_groups
 The total amount of groups.
idx_t tuple_size
 The tuple size.
idx_t grouping_columns
 The number of grouping columns.
data_ptr_t data
unsafe_unique_array< data_t > owned_data
 The owned data of the HT.
unsafe_unique_array< bool > group_is_set
 Information on whether or not a specific group has any entries.
vector< Value > group_minima
 The minimum values for each of the group columns.
SelectionVector sel
 Reused selection vector.
unique_ptr< ArenaAllocator > aggregate_allocator
 The active arena allocator used by the aggregates for their internal state.
vector< unique_ptr< ArenaAllocator > > stored_allocators
 Owning arena allocators that this HT has data from.
Protected Attributes inherited from duckdb::BaseAggregateHashTable
Allocator & allocator
BufferManager & buffer_manager
shared_ptr< TupleDataLayout > layout_ptr
 A helper for managing offsets into the data buffers.
vector< LogicalType > payload_types
 The types of the payload columns stored in the hashtable.
AggregateFilterDataSet filter_set
 Intermediate structures and data for aggregate filters.

Private Member Functions

void Destroy ()
 Destroy the perfect aggregate HT (called automatically by the destructor).

Constructor & Destructor Documentation

◆ PerfectAggregateHashTable()

duckdb::PerfectAggregateHashTable::PerfectAggregateHashTable ( ClientContext & context,
Allocator & allocator,
const vector< LogicalType > & group_types,
vector< LogicalType > payload_types_p,
vector< AggregateObject > aggregate_objects,
vector< Value > group_minima,
vector< idx_t > required_bits )
201100 : BaseAggregateHashTable(context, allocator, aggregate_objects_p, std::move(payload_types_p)),
201101 addresses(LogicalType::POINTER), required_bits(std::move(required_bits_p)), total_required_bits(0),
201102 group_minima(std::move(group_minima_p)), sel(STANDARD_VECTOR_SIZE),
201103 aggregate_allocator(make_uniq<ArenaAllocator>(allocator)) {
201104 for (auto &group_bits : required_bits) {
201105 total_required_bits += group_bits;
201106 }
201107 // the total amount of groups we allocate space for is 2^required_bits
201109 // we don't need to store the groups in a perfect hash table, since the group keys can be deduced by their location
201110 grouping_columns = group_types_p.size();
201111 layout_ptr->Initialize(std::move(aggregate_objects_p));
201112 tuple_size = layout_ptr->GetRowWidth();
201113
201114 // allocate and null initialize the data
201115 owned_data = make_unsafe_uniq_array_uninitialized<data_t>(tuple_size * total_groups);
201116 data = owned_data.get();
201117
201118 // set up the empty payloads for every tuple, and initialize the "occupied" flag to false
201119 group_is_set = make_unsafe_uniq_array_uninitialized<bool>(total_groups);
201120 memset(group_is_set.get(), 0, total_groups * sizeof(bool));
201121
201122 // initialize the hash table for each entry
201123 auto address_data = FlatVector::GetData<uintptr_t>(addresses);
201124 idx_t init_count = 0;
201125 for (idx_t i = 0; i < total_groups; i++) {
201126 address_data[init_count] = uintptr_t(data) + (tuple_size * i);
201127 init_count++;
201128 if (init_count == STANDARD_VECTOR_SIZE) {
201129 RowOperations::InitializeStates(*layout_ptr, addresses, *FlatVector::IncrementalSelectionVector(),
201130 init_count);
201131 init_count = 0;
201132 }
201133 }
201134 RowOperations::InitializeStates(*layout_ptr, addresses, *FlatVector::IncrementalSelectionVector(), init_count);
201135}
shared_ptr< TupleDataLayout > layout_ptr
A helper for managing offsets into the data buffers.
Definition duckdb.cpp:145677
unique_ptr< ArenaAllocator > aggregate_allocator
The active arena allocator used by the aggregates for their internal state.
Definition duckdb.cpp:161436
idx_t total_required_bits
The total required bits for the HT (this determines the max capacity).
Definition duckdb.cpp:161414
vector< Value > group_minima
The minimum values for each of the group columns.
Definition duckdb.cpp:161430
idx_t grouping_columns
The number of grouping columns.
Definition duckdb.cpp:161420
vector< idx_t > required_bits
The required bits per group.
Definition duckdb.cpp:161412
idx_t tuple_size
The tuple size.
Definition duckdb.cpp:161418
SelectionVector sel
Reused selection vector.
Definition duckdb.cpp:161433
unsafe_unique_array< bool > group_is_set
Information on whether or not a specific group has any entries.
Definition duckdb.cpp:161427
unsafe_unique_array< data_t > owned_data
The owned data of the HT.
Definition duckdb.cpp:161425
idx_t total_groups
The total amount of groups.
Definition duckdb.cpp:161416
::uint64_t uint64_t
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237
static void InitializeStates(TupleDataLayout &layout, Vector &addresses, const SelectionVector &sel, idx_t count)
initialize - unaligned addresses
Definition duckdb.cpp:99774

◆ ~PerfectAggregateHashTable()

duckdb::PerfectAggregateHashTable::~PerfectAggregateHashTable ( )
override
201137 {
201138 Destroy();
201139}
void Destroy()
Destroy the perfect aggregate HT (called automatically by the destructor).
Definition duckdb.cpp:201375

Member Function Documentation

◆ AddChunk()

void duckdb::PerfectAggregateHashTable::AddChunk ( DataChunk & groups,
DataChunk & payload )

Add the given data to the HT.

201203 {
201204 // first we need to find the location in the HT of each of the groups
201205 auto address_data = FlatVector::GetData<uintptr_t>(addresses);
201206 // zero-initialize the address data
201207 memset(address_data, 0, groups.size() * sizeof(uintptr_t));
201208 D_ASSERT(groups.ColumnCount() == group_minima.size());
201209
201210 // then compute the actual group location by iterating over each of the groups
201211 idx_t current_shift = total_required_bits;
201212 for (idx_t i = 0; i < groups.ColumnCount(); i++) {
201213 current_shift -= required_bits[i];
201214 ComputeGroupLocation(groups.data[i], group_minima[i], address_data, current_shift, groups.size());
201215 }
201216 // now we have the HT entry number for every tuple
201217 // compute the actual pointer to the data by adding it to the base HT pointer and multiplying by the tuple size
201218 for (idx_t i = 0; i < groups.size(); i++) {
201219 const auto group = address_data[i];
201220 if (group >= total_groups) {
201221 throw InvalidInputException("Perfect hash aggregate: aggregate group %llu exceeded total groups %llu. This "
201222 "likely means that the statistics in your data source are corrupt.\n* PRAGMA "
201223 "disable_optimizer to disable optimizations that rely on correct statistics",
201224 group, total_groups);
201225 }
201226 group_is_set[group] = true;
201227 address_data[i] = uintptr_t(data) + group * tuple_size;
201228 }
201229
201230 // after finding the group location we update the aggregates
201231 idx_t payload_idx = 0;
201232 auto &aggregates = layout_ptr->GetAggregates();
201233 RowOperationsState row_state(*aggregate_allocator);
201234 for (idx_t aggr_idx = 0; aggr_idx < aggregates.size(); aggr_idx++) {
201235 auto &aggregate = aggregates[aggr_idx];
201236 auto input_count = (idx_t)aggregate.child_count;
201237 if (aggregate.filter) {
201238 RowOperations::UpdateFilteredStates(row_state, filter_set.GetFilterData(aggr_idx), aggregate, addresses,
201239 payload, payload_idx);
201240 } else {
201241 RowOperations::UpdateStates(row_state, aggregate, addresses, payload, payload_idx, payload.size());
201242 }
201243 // move to the next aggregate
201244 payload_idx += input_count;
201245 VectorOperations::AddInPlace(addresses, UnsafeNumericCast<int64_t>(aggregate.payload_size), payload.size());
201246 }
201247}
AggregateFilterDataSet filter_set
Intermediate structures and data for aggregate filters.
Definition duckdb.cpp:145681
vector< Vector > data
The vectors owned by the DataChunk.
Definition duckdb.hpp:11445
static void UpdateFilteredStates(RowOperationsState &state, AggregateFilterData &filter_data, AggregateObject &aggr, Vector &addresses, DataChunk &payload, idx_t arg_idx)
filtered update - aligned addresses
Definition duckdb.cpp:99821
static void UpdateStates(RowOperationsState &state, AggregateObject &aggr, Vector &addresses, DataChunk &payload, idx_t arg_idx, idx_t count)
update - aligned addresses
Definition duckdb.cpp:99814
static void AddInPlace(Vector &left, int64_t delta, idx_t count)
left += delta
Definition duckdb.cpp:143413
Here is the call graph for this function:

◆ Combine()

void duckdb::PerfectAggregateHashTable::Combine ( PerfectAggregateHashTable & other)

Combines the target perfect aggregate HT into this one.

201249 {
201250 D_ASSERT(total_groups == other.total_groups);
201251 D_ASSERT(tuple_size == other.tuple_size);
201252
201253 Vector source_addresses(LogicalType::POINTER);
201254 Vector target_addresses(LogicalType::POINTER);
201255 auto source_addresses_ptr = FlatVector::GetData<data_ptr_t>(source_addresses);
201256 auto target_addresses_ptr = FlatVector::GetData<data_ptr_t>(target_addresses);
201257
201258 // iterate over all entries of both hash tables and call combine for all entries that can be combined
201259 data_ptr_t source_ptr = other.data;
201260 data_ptr_t target_ptr = data;
201261 idx_t combine_count = 0;
201262 RowOperationsState row_state(*aggregate_allocator);
201263 for (idx_t i = 0; i < total_groups; i++) {
201264 auto has_entry_source = other.group_is_set[i];
201265 // we only have any work to do if the source has an entry for this group
201266 if (has_entry_source) {
201267 group_is_set[i] = true;
201268 source_addresses_ptr[combine_count] = source_ptr;
201269 target_addresses_ptr[combine_count] = target_ptr;
201270 combine_count++;
201271 if (combine_count == STANDARD_VECTOR_SIZE) {
201272 RowOperations::CombineStates(row_state, *layout_ptr, source_addresses, target_addresses, combine_count);
201273 combine_count = 0;
201274 }
201275 }
201276 source_ptr += tuple_size;
201277 target_ptr += tuple_size;
201278 }
201279 RowOperations::CombineStates(row_state, *layout_ptr, source_addresses, target_addresses, combine_count);
201280
201281 // FIXME: after moving the arena allocator, we currently have to ensure that the pointer is not nullptr, because the
201282 // FIXME: Destroy()-function of the hash table expects an allocator in some cases (e.g., for sorted aggregates)
201283 stored_allocators.push_back(std::move(other.aggregate_allocator));
201284 other.aggregate_allocator = make_uniq<ArenaAllocator>(allocator);
201285}
vector< unique_ptr< ArenaAllocator > > stored_allocators
Owning arena allocators that this HT has data from.
Definition duckdb.cpp:161438
static void CombineStates(RowOperationsState &state, TupleDataLayout &layout, Vector &sources, Vector &targets, idx_t count)
combine - unaligned addresses, updated
Definition duckdb.cpp:99834
Here is the call graph for this function:

◆ Scan()

void duckdb::PerfectAggregateHashTable::Scan ( idx_t & scan_position,
DataChunk & result )

Scan the HT starting from the scan_position.

201341 {
201342 auto data_pointers = FlatVector::GetData<data_ptr_t>(addresses);
201343 uint32_t group_values[STANDARD_VECTOR_SIZE];
201344
201345 // iterate over the HT until we either have exhausted the entire HT, or
201346 idx_t entry_count = 0;
201347 for (; scan_position < total_groups; scan_position++) {
201348 if (group_is_set[scan_position]) {
201349 // this group is set: add it to the set of groups to extract
201350 data_pointers[entry_count] = data + tuple_size * scan_position;
201351 group_values[entry_count] = NumericCast<uint32_t>(scan_position);
201352 entry_count++;
201353 if (entry_count == STANDARD_VECTOR_SIZE) {
201354 scan_position++;
201355 break;
201356 }
201357 }
201358 }
201359 if (entry_count == 0) {
201360 // no entries found
201361 return;
201362 }
201363 // first reconstruct the groups from the group index
201364 idx_t shift = total_required_bits;
201365 for (idx_t i = 0; i < grouping_columns; i++) {
201366 shift -= required_bits[i];
201367 ReconstructGroupVector(group_values, group_minima[i], required_bits[i], shift, entry_count, result.data[i]);
201368 }
201369 // then construct the payloads
201370 result.SetCardinality(entry_count);
201371 RowOperationsState row_state(*aggregate_allocator);
201372 RowOperations::FinalizeStates(row_state, *layout_ptr, addresses, result, grouping_columns);
201373}
::uint32_t uint32_t
static void FinalizeStates(RowOperationsState &state, TupleDataLayout &layout, Vector &addresses, DataChunk &result, idx_t aggr_idx)
finalize - unaligned addresses, updated
Definition duckdb.cpp:99866
Here is the call graph for this function:

◆ Destroy()

void duckdb::PerfectAggregateHashTable::Destroy ( )
private

Destroy the perfect aggregate HT (called automatically by the destructor).

201375 {
201376 // check if there is any destructor to call
201377 bool has_destructor = false;
201378 for (auto &aggr : layout_ptr->GetAggregates()) {
201379 if (aggr.function.HasStateDestructorCallback()) {
201380 has_destructor = true;
201381 }
201382 }
201383 if (!has_destructor) {
201384 return;
201385 }
201386 // there are aggregates with destructors: loop over the hash table
201387 // and call the destructor method for each of the aggregates
201388 auto data_pointers = FlatVector::GetData<data_ptr_t>(addresses);
201389 idx_t count = 0;
201390
201391 // iterate over all initialised slots of the hash table
201392 RowOperationsState row_state(*aggregate_allocator);
201393 data_ptr_t payload_ptr = data;
201394 for (idx_t i = 0; i < total_groups; i++) {
201395 data_pointers[count++] = payload_ptr;
201396 if (count == STANDARD_VECTOR_SIZE) {
201397 RowOperations::DestroyStates(row_state, *layout_ptr, addresses, count);
201398 count = 0;
201399 }
201400 payload_ptr += tuple_size;
201401 }
201402 RowOperations::DestroyStates(row_state, *layout_ptr, addresses, count);
201403}
static void DestroyStates(RowOperationsState &state, TupleDataLayout &layout, Vector &addresses, idx_t count)
destructor - unaligned addresses, updated
Definition duckdb.cpp:99798
Here is the call graph for this function:

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