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

Public Types

using HashGroupPtr = unique_ptr<ColumnDataCollection>
using OrderMasks = unordered_map<idx_t, ValidityMask>
using ExecutorGlobalStatePtr = unique_ptr<GlobalSinkState>
using ExecutorGlobalStates = vector<ExecutorGlobalStatePtr>
using ExecutorLocalStatePtr = unique_ptr<LocalSinkState>
using ExecutorLocalStates = vector<ExecutorLocalStatePtr>
using ThreadLocalStates = vector<ExecutorLocalStates>
using Task = WindowSourceTask
using TaskPtr = optional_ptr<Task>
using ScannerPtr = unique_ptr<WindowCollectionChunkScanner>
using ChunkRow = SortStrategy::ChunkRow

Public Member Functions

 WindowHashGroup (WindowGlobalSinkState &gsink, const ChunkRow &chunk_row, const idx_t hash_bin_p)
void AllocateMasks ()
void ComputeMasks (const idx_t begin_idx, const idx_t end_idx)
ExecutorGlobalStates & GetGlobalStates (ClientContext &client)
idx_t ChunkCount () const
 The number of chunks in the group.
idx_t GetTaskCount () const
idx_t GetThreadCount () const
idx_t InitTasks (idx_t per_thread)
ScannerPtr GetScanner (const idx_t begin_idx) const
void UpdateScanner (ScannerPtr &scanner, idx_t begin_idx) const
WindowGroupStage GetStage () const
bool TryPrepareNextStage ()
bool TryNextTask (Task &task)

Static Public Member Functions

template<typename T>
static T BinValue (T n, T val)

Public Attributes

WindowGlobalSinkState & gsink
 The shared global state from sinking.
HashGroupPtr rows
 The hash partition data.
idx_t count = 0
 The size of the group.
idx_t blocks = 0
 The number of blocks in the group.
ValidityMask partition_mask
 The partition boundary mask.
OrderMasks order_masks
 The order boundary mask.
unique_ptr< WindowCollection > collection
 The fully materialised data collection.
atomic< WindowGroupStage > stage
ExecutorGlobalStates gestates
 The function global states for this hash group.
ThreadLocalStates thread_states
 Executor local states, one per thread.
idx_t hash_bin
 The bin number.
mutex lock
 Single threading lock.
idx_t per_thread = 0
 The the number of blocks per thread.
idx_t group_threads = 0
 The the number of blocks per thread.
idx_t next_task = 0
 The next task to process.
std::atomic< idx_t > sorted
 Count of sorted run blocks.
std::atomic< idx_t > materialized
 Count of materialized run blocks.
std::atomic< idx_t > masked
 Count of masked blocks.
std::atomic< idx_t > sunk
 Count of sunk rows.
std::atomic< idx_t > finalized
 Count of finalized blocks.
std::atomic< idx_t > completed
 Count of completed tasks.
idx_t batch_base
 The output ordering batch index this hash group starts at.

Constructor & Destructor Documentation

◆ WindowHashGroup()

duckdb::WindowHashGroup::WindowHashGroup ( WindowGlobalSinkState & gsink,
const ChunkRow & chunk_row,
const idx_t hash_bin_p )
164895 : gsink(gsink), count(chunk_row.count), blocks(chunk_row.chunks), stage(WindowGroupStage::SORT),
164896 hash_bin(hash_bin_p), sorted(0), materialized(0), masked(0), sunk(0), finalized(0), completed(0), batch_base(0) {
164897 // There are three types of partitions:
164898 // 1. No partition (no sorting)
164899 // 2. One partition (sorting, but no hashing)
164900 // 3. Multiple partitions (sorting and hashing)
164901
164902 // Set up the collection for any fully materialised data
164903 const auto &shared = WindowSharedExpressions::GetSortedExpressions(gsink.shared.coll_shared);
164904 vector<LogicalType> types;
164905 for (auto &expr : shared) {
164906 types.emplace_back(expr->return_type);
164907 }
164908 auto &buffer_manager = BufferManager::GetBufferManager(gsink.client);
164909 collection = make_uniq<WindowCollection>(buffer_manager, count, types);
164910}
std::atomic< idx_t > materialized
Count of materialized run blocks.
Definition duckdb.cpp:164597
std::atomic< idx_t > masked
Count of masked blocks.
Definition duckdb.cpp:164599
idx_t count
The size of the group.
Definition duckdb.cpp:164568
unique_ptr< WindowCollection > collection
The fully materialised data collection.
Definition duckdb.cpp:164576
std::atomic< idx_t > finalized
Count of finalized blocks.
Definition duckdb.cpp:164603
std::atomic< idx_t > completed
Count of completed tasks.
Definition duckdb.cpp:164605
idx_t hash_bin
The bin number.
Definition duckdb.cpp:164585
WindowGlobalSinkState & gsink
The shared global state from sinking.
Definition duckdb.cpp:164564
std::atomic< idx_t > sorted
Count of sorted run blocks.
Definition duckdb.cpp:164595
idx_t blocks
The number of blocks in the group.
Definition duckdb.cpp:164570
idx_t batch_base
The output ordering batch index this hash group starts at.
Definition duckdb.cpp:164607
std::atomic< idx_t > sunk
Count of sunk rows.
Definition duckdb.cpp:164601
static vector< optional_ptr< const Expression > > GetSortedExpressions(Shared &shared)
Expression layout.
Definition duckdb.cpp:258195

Member Function Documentation

◆ BinValue()

template<typename T>
T duckdb::WindowHashGroup::BinValue ( T n,
T val )
inlinestatic
164464 {
164465 return ((n + (val - 1)) / val);
164466 }

◆ AllocateMasks()

void duckdb::WindowHashGroup::AllocateMasks ( )
164930 {
164931 // Single-threaded building as this is mostly memory allocation
164932 lock_guard<mutex> gestate_guard(lock);
164933 if (partition_mask.IsMaskSet()) {
164934 return;
164935 }
164936
164937 // Allocate masks inside the lock
164938 partition_mask.Initialize(count);
164939
164940 const auto &executors = gsink.executors;
164941 for (auto &wexec : executors) {
164942 auto &wexpr = wexec->wexpr;
164943 auto &order_mask = order_masks[wexpr.partitions.size() + wexpr.orders.size()];
164944 if (order_mask.IsMaskSet()) {
164945 continue;
164946 }
164947 order_mask.Initialize(count);
164948 }
164949}
mutex lock
Single threading lock.
Definition duckdb.cpp:164587
ValidityMask partition_mask
The partition boundary mask.
Definition duckdb.cpp:164572
OrderMasks order_masks
The order boundary mask.
Definition duckdb.cpp:164574

◆ ComputeMasks()

void duckdb::WindowHashGroup::ComputeMasks ( const idx_t begin_idx,
const idx_t end_idx )
164951 {
164952 D_ASSERT(count > 0);
164953
164954 // Initialise our range
164955 AllocateMasks();
164956 const auto begin_entry = partition_mask.EntryCount(block_begin * STANDARD_VECTOR_SIZE);
164957 const auto end_entry = partition_mask.EntryCount(MinValue<idx_t>(block_end * STANDARD_VECTOR_SIZE, count));
164958
164959 // If the data is unsorted, then the chunk sizes may be < STANDARD_VECTOR_SIZE,
164960 // and the entry range may be empty.
164961 if (begin_entry >= end_entry) {
164962 return;
164963 }
164964
164965 partition_mask.SetRangeInvalid(count, begin_entry, end_entry);
164966 if (!block_begin) {
164967 partition_mask.SetValidUnsafe(0);
164968 }
164969 for (auto &order_mask : order_masks) {
164970 order_mask.second.SetRangeInvalid(count, begin_entry, end_entry);
164971 if (!block_begin) {
164972 order_mask.second.SetValidUnsafe(0);
164973 }
164974 }
164975
164976 // If we are not sorting, then only the partition boundaries are needed.
164977 const auto &wexpr = gsink.op.select_list[gsink.op.order_idx]->Cast<BoundWindowExpression>();
164978 auto &partitions = wexpr.partitions;
164979 if (partitions.empty() && wexpr.orders.empty()) {
164980 return;
164981 }
164982
164983 // Set up the partition compare structs
164984 const auto key_count = partitions.size();
164985
164986 // Set up the order data structures
164987 auto &collection = *rows;
164988 auto &scan_cols = gsink.sort_strategy->sort_ids;
164989 WindowCollectionChunkScanner scanner(collection, scan_cols, block_begin);
164990 unordered_map<idx_t, DataChunk> prefixes;
164991 for (auto &order_mask : order_masks) {
164992 D_ASSERT(order_mask.first >= key_count);
164993 auto order_type = scanner.PrefixStructType(order_mask.first, key_count);
164994 vector<LogicalType> types(2, order_type);
164995 auto &keys = prefixes[order_mask.first];
164996 // We can't use InitializeEmpty here because it doesn't set up all of the STRUCT internals...
164997 keys.Initialize(collection.GetAllocator(), types);
164998 }
164999
165000 WindowDeltaScanner(collection, block_begin, block_end, scan_cols, key_count,
165001 [&](const idx_t row_idx, DataChunk &prev, DataChunk &curr, const idx_t ndistinct,
165002 SelectionVector &distinct, const SelectionVector &matching) {
165003 // Process the partition boundaries
165004 for (idx_t i = 0; i < ndistinct; ++i) {
165005 const idx_t curr_index = row_idx + distinct.get_index(i);
165006 partition_mask.SetValidUnsafe(curr_index);
165007 for (auto &order_mask : order_masks) {
165008 order_mask.second.SetValidUnsafe(curr_index);
165009 }
165010 }
165011
165012 // Process the peers with each partition
165013 const auto count = MinValue<idx_t>(prev.size(), curr.size());
165014 const auto nmatch = count - ndistinct;
165015 if (!nmatch) {
165016 return;
165017 }
165018
165019 for (auto &order_mask : order_masks) {
165020 // If there are no order columns, then all the partition elements are peers and we are
165021 // done
165022 if (partitions.size() == order_mask.first) {
165023 continue;
165024 }
165025 auto &prefix = prefixes[order_mask.first];
165026 prefix.Reset();
165027 auto &order_prev = prefix.data[0];
165028 auto &order_curr = prefix.data[1];
165029 scanner.ReferenceStructColumns(prev, order_prev, order_mask.first, partitions.size());
165030 scanner.ReferenceStructColumns(curr, order_curr, order_mask.first, partitions.size());
165031 if (ndistinct) {
165032 prefix.Slice(matching, nmatch);
165033 } else {
165034 prefix.SetCardinality(nmatch);
165035 }
165036 const auto m = VectorOperations::DistinctFrom(order_curr, order_prev, nullptr, nmatch,
165037 &distinct, nullptr);
165038 for (idx_t i = 0; i < m; ++i) {
165039 const idx_t curr_index = row_idx + matching.get_index(distinct.get_index(i));
165040 order_mask.second.SetValidUnsafe(curr_index);
165041 }
165042 }
165043 });
165044}
HashGroupPtr rows
The hash partition data.
Definition duckdb.cpp:164566
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237

◆ GetGlobalStates()

WindowHashGroup::ExecutorGlobalStates & duckdb::WindowHashGroup::GetGlobalStates ( ClientContext & client)
165168 {
165169 // Single-threaded building as this is mostly memory allocation
165170 lock_guard<mutex> gestate_guard(lock);
165171 const auto &executors = gsink.executors;
165172 if (gestates.size() == executors.size()) {
165173 return gestates;
165174 }
165175
165176 // These can be large so we defer building them until we are ready.
165177 for (auto &wexec : executors) {
165178 auto &wexpr = wexec->wexpr;
165179 auto &order_mask = order_masks[wexpr.partitions.size() + wexpr.orders.size()];
165180 gestates.emplace_back(wexec->GetGlobalState(client, count, partition_mask, order_mask));
165181 }
165182
165183 return gestates;
165184}
ExecutorGlobalStates gestates
The function global states for this hash group.
Definition duckdb.cpp:164580

◆ ChunkCount()

idx_t duckdb::WindowHashGroup::ChunkCount ( ) const
inline

The number of chunks in the group.

164476 {
164477 return blocks;
164478 }

◆ GetTaskCount()

idx_t duckdb::WindowHashGroup::GetTaskCount ( ) const
inline
164481 {
164482 return GetThreadCount() * (uint8_t(WindowGroupStage::DONE) - uint8_t(WindowGroupStage::SORT));
164483 }
::uint8_t uint8_t

◆ GetThreadCount()

idx_t duckdb::WindowHashGroup::GetThreadCount ( ) const
inline
164485 {
164486 return group_threads;
164487 }
idx_t group_threads
The the number of blocks per thread.
Definition duckdb.cpp:164591

◆ InitTasks()

idx_t duckdb::WindowHashGroup::InitTasks ( idx_t per_thread)
165111 {
165112 per_thread = per_thread_p;
165113 group_threads = BinValue(ChunkCount(), per_thread);
165114 thread_states.resize(GetThreadCount());
165115
165116 return GetTaskCount();
165117}
ThreadLocalStates thread_states
Executor local states, one per thread.
Definition duckdb.cpp:164582
idx_t ChunkCount() const
The number of chunks in the group.
Definition duckdb.cpp:164476
idx_t per_thread
The the number of blocks per thread.
Definition duckdb.cpp:164589

◆ GetScanner()

unique_ptr< WindowCollectionChunkScanner > duckdb::WindowHashGroup::GetScanner ( const idx_t begin_idx) const
164912 {
164913 if (!rows) {
164914 return nullptr;
164915 }
164916
164917 auto &scan_ids = gsink.sort_strategy->scan_ids;
164918 return make_uniq<WindowCollectionChunkScanner>(*rows, scan_ids, begin_idx);
164919}

◆ UpdateScanner()

void duckdb::WindowHashGroup::UpdateScanner ( ScannerPtr & scanner,
idx_t begin_idx ) const
164921 {
164922 if (!scanner || &scanner->collection != rows.get()) {
164923 scanner.reset();
164924 scanner = GetScanner(begin_idx);
164925 } else {
164926 scanner->Seek(begin_idx);
164927 }
164928}

◆ GetStage()

WindowGroupStage duckdb::WindowHashGroup::GetStage ( ) const
inline
164496 {
164497 return stage;
164498 }

◆ TryPrepareNextStage()

bool duckdb::WindowHashGroup::TryPrepareNextStage ( )
inline
164500 {
164501 lock_guard<mutex> prepare_guard(lock);
164502 switch (stage.load()) {
164503 case WindowGroupStage::SORT:
164504 if (sorted == blocks) {
164505 stage = WindowGroupStage::MATERIALIZE;
164506 return true;
164507 }
164508 return false;
164509 case WindowGroupStage::MATERIALIZE:
164510 if (materialized == blocks && rows.get()) {
164511 stage = WindowGroupStage::MASK;
164512 return true;
164513 }
164514 return false;
164515 case WindowGroupStage::MASK:
164516 if (masked == blocks) {
164517 stage = WindowGroupStage::SINK;
164518 return true;
164519 }
164520 return false;
164521 case WindowGroupStage::SINK:
164522 if (sunk == count) {
164523 stage = WindowGroupStage::FINALIZE;
164524 return true;
164525 }
164526 return false;
164527 case WindowGroupStage::FINALIZE:
164528 if (finalized == blocks) {
164529 stage = WindowGroupStage::GETDATA;
164530 return true;
164531 }
164532 return false;
164533 case WindowGroupStage::GETDATA:
164534 case WindowGroupStage::DONE:
164535 // never block in GETDATA
164536 return true;
164537 }
164538
164539 // Stop Linux whinging about control flow...
164540 return true;
164541 }

◆ TryNextTask()

bool duckdb::WindowHashGroup::TryNextTask ( Task & task)
inline
164543 {
164544 if (next_task >= GetTaskCount()) {
164545 return false;
164546 }
164547 const auto group_stage = GetStage();
164548 const auto group_threads = GetThreadCount();
164549 task.stage = WindowGroupStage(next_task / group_threads);
164550 if (task.stage == group_stage) {
164551 task.thread_idx = next_task % group_threads;
164552 task.group_idx = hash_bin;
164553 task.begin_idx = task.thread_idx * per_thread;
164554 task.max_idx = ChunkCount();
164555 task.end_idx = MinValue<idx_t>(task.begin_idx + per_thread, task.max_idx);
164556 ++next_task;
164557 return true;
164558 }
164559
164560 return false;
164561 }
idx_t next_task
The next task to process.
Definition duckdb.cpp:164593

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