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

Public Member Functions

 WindowDistinctAggregatorLocalState (ExecutionContext &context, const WindowDistinctAggregatorGlobalState &aggregator)
void Sink (ExecutionContext &context, DataChunk &sink_chunk, DataChunk &coll_chunk, idx_t input_idx, optional_ptr< SelectionVector > filter_sel, idx_t filtered, InterruptState &interrupt)
void Finalize (ExecutionContext &context, WindowAggregatorGlobalState &gastate, CollectionPtr collection) override
void Sorted ()
void ExecuteTask (ExecutionContext &context, WindowDistinctAggregatorGlobalState &gdstate)
void Evaluate (ExecutionContext &context, const WindowDistinctAggregatorGlobalState &gdstate, const DataChunk &bounds, Vector &result, idx_t count, idx_t row_idx)
Public Member Functions inherited from duckdb::WindowAggregatorLocalState
 WindowAggregatorLocalState (ExecutionContext &context)
void Sink (ExecutionContext &context, WindowAggregatorGlobalState &gastate, DataChunk &sink_chunk, DataChunk &coll_chunk, idx_t row_idx)
Public Member Functions inherited from duckdb::LocalSinkState
template<class TARGET>
TARGET & Cast ()
template<class TARGET>
const TARGET & Cast () const

Public Attributes

optional_ptr< LocalSinkState > local_sink
 Thread-local sorting data.
WindowDistinctSortStage stage = WindowDistinctSortStage::INIT
 Finalize stage.
idx_t block_idx
 Finalize scan block index.
Vector update_v
 Thread-local tree aggregation.
Vector source_v
Vector target_v
DataChunk leaves
SelectionVector sel
Public Attributes inherited from duckdb::WindowAggregatorLocalState
ArenaAllocator allocator
 Global allocator.
unique_ptr< WindowCursor > cursor
 The state used for reading the collection.
Public Attributes inherited from duckdb::LocalSinkState
SourcePartitionInfo partition_info
 Source partition info.

Protected Member Functions

void FlushStates ()
 Flush the accumulated intermediate states into the result states.

Protected Attributes

const WindowDistinctAggregatorGlobalState & gdstate
 The aggregator we are working with.
DataChunk sort_chunk
 The sort input chunk.
WindowAggregateStates statef
 Reused result state container for the window functions.
Vector statep
 A vector of pointers to "state", used for buffering intermediate aggregates.
Vector statel
 Reused state pointers for combining tree elements.
idx_t flush_count
 Count of buffered values.
SubFrames frames
 The frame boundaries, used for the window functions.

Additional Inherited Members

Public Types inherited from duckdb::WindowAggregatorLocalState
using CollectionPtr = optional_ptr<WindowCollection>
Static Public Member Functions inherited from duckdb::WindowAggregatorLocalState
static void InitSubFrames (SubFrames &frames, const WindowExcludeMode exclude_mode)

Constructor & Destructor Documentation

◆ WindowDistinctAggregatorLocalState()

duckdb::WindowDistinctAggregatorLocalState::WindowDistinctAggregatorLocalState ( ExecutionContext & context,
const WindowDistinctAggregatorGlobalState & aggregator )
255648 : WindowAggregatorLocalState(context), update_v(LogicalType::POINTER), source_v(LogicalType::POINTER),
255649 target_v(LogicalType::POINTER), gdstate(gdstate), statef(context.client, gdstate.aggr),
255650 statep(LogicalType::POINTER), statel(LogicalType::POINTER), flush_count(0) {
255651 InitSubFrames(frames, gdstate.aggregator.exclude_mode);
255652
255653 sort_chunk.Initialize(context.client, gdstate.sort_types);
255654
255655 gdstate.locals++;
255656}
WindowAggregateStates statef
Reused result state container for the window functions.
Definition duckdb.cpp:255635
SubFrames frames
The frame boundaries, used for the window functions.
Definition duckdb.cpp:255643
Vector statel
Reused state pointers for combining tree elements.
Definition duckdb.cpp:255639
idx_t flush_count
Count of buffered values.
Definition duckdb.cpp:255641
const WindowDistinctAggregatorGlobalState & gdstate
The aggregator we are working with.
Definition duckdb.cpp:255631
Vector update_v
Thread-local tree aggregation.
Definition duckdb.cpp:255620
Vector statep
A vector of pointers to "state", used for buffering intermediate aggregates.
Definition duckdb.cpp:255637
DataChunk sort_chunk
The sort input chunk.
Definition duckdb.cpp:255633

◆ ~WindowDistinctAggregatorLocalState()

duckdb::WindowDistinctAggregatorLocalState::~WindowDistinctAggregatorLocalState ( )
inlineoverride
255601 {
255602 statef.Destroy();
255603 }

Member Function Documentation

◆ Sink()

void duckdb::WindowDistinctAggregatorLocalState::Sink ( ExecutionContext & context,
DataChunk & sink_chunk,
DataChunk & coll_chunk,
idx_t input_idx,
optional_ptr< SelectionVector > filter_sel,
idx_t filtered,
InterruptState & interrupt )
255674 {
255675 // 3: for i ← 0 to in.size do
255676 // 4: sorted[i] ← (in[i], i)
255677 const auto count = sink_chunk.size();
255678 sort_chunk.Reset();
255679 auto &sorted_vec = sort_chunk.data.back();
255680 auto sorted = FlatVector::GetData<idx_t>(sorted_vec);
255681 std::iota(sorted, sorted + count, input_idx);
255682
255683 // Our arguments are being fully materialised,
255684 // but we also need them as sort keys.
255685 auto &child_idx = gdstate.aggregator.child_idx;
255686 for (column_t c = 0; c < child_idx.size(); ++c) {
255687 sort_chunk.data[c].Reference(coll_chunk.data[child_idx[c]]);
255688 }
255689 sort_chunk.SetCardinality(sink_chunk);
255690
255691 // Apply FILTER clause, if any
255692 if (filter_sel) {
255693 sort_chunk.Slice(*filter_sel, filtered);
255694 }
255695
255696 if (!local_sink) {
255697 local_sink = gdstate.InitializeLocalSort(context);
255698 }
255699
255700 OperatorSinkInput sink {*gdstate.global_sink, *local_sink, interrupt};
255701 gdstate.sort->Sink(context, sort_chunk, sink);
255702}
vector< Vector > data
The vectors owned by the DataChunk.
Definition duckdb.hpp:11445
optional_ptr< LocalSinkState > local_sink
Thread-local sorting data.
Definition duckdb.cpp:255614
idx_t column_t
Type used for column identifiers.
Definition duckdb.hpp:259

◆ Finalize()

void duckdb::WindowDistinctAggregatorLocalState::Finalize ( ExecutionContext & context,
WindowAggregatorGlobalState & gastate,
CollectionPtr collection )
overridevirtual

Input data chunk, used for leaf segment aggregation

Reimplemented from duckdb::WindowAggregatorLocalState.

255705 {
255706 WindowAggregatorLocalState::Finalize(context, gastate, collection);
255707
255709 leaves.Initialize(context.client, cursor->chunk.GetTypes());
255710 sel.Initialize();
255711}
unique_ptr< WindowCursor > cursor
The state used for reading the collection.
Definition duckdb.cpp:164094

◆ Sorted()

void duckdb::WindowDistinctAggregatorLocalState::Sorted ( )
255827 {
255828 using ZippedTuple = WindowDistinctAggregatorGlobalState::ZippedTuple;
255829 auto &collection = *gdstate.sorted;
255830 auto &prev_idcs = gdstate.zipped_tree.LowestLevel();
255831 auto &aggregator = gdstate.aggregator;
255832
255833 // Find our chunk range
255834 const auto block_begin = (block_idx * collection.ChunkCount()) / gdstate.total_tasks;
255835 const auto block_end = ((block_idx + 1) * collection.ChunkCount()) / gdstate.total_tasks;
255836
255837 const auto &scan_cols = gdstate.sort_cols;
255838 const auto key_count = aggregator.arg_types.size();
255839
255840 // Setting up the first row is a bit tricky - we have to scan the first value ourselves
255841 WindowCollectionChunkScanner scanner(collection, scan_cols, block_begin ? block_begin - 1 : 0);
255842 auto &scanned = scanner.chunk;
255843 if (!scanner.Scan()) {
255844 return;
255845 }
255846
255847 idx_t prev_i = 0;
255848 if (!block_begin) {
255849 // First block, so set up initial sentinel
255850 auto input_idx = FlatVector::GetData<idx_t>(scanned.data.back());
255851 prev_i = input_idx[0];
255852 prev_idcs[prev_i] = ZippedTuple(0, prev_i);
255853 } else {
255854 // Move to the to end of the previous block
255855 // so we can record the comparison result for the first row
255856 auto input_idx = FlatVector::GetData<idx_t>(scanned.data.back());
255857 auto scan_idx = scanned.size() - 1;
255858 prev_i = input_idx[scan_idx];
255859 }
255860
255861 // 8: for i ← 1 to in.size do
255862 WindowDeltaScanner(collection, block_begin, block_end, scan_cols, key_count,
255863 [&](const idx_t row_idx, DataChunk &prev, DataChunk &curr, const idx_t ndistinct,
255864 SelectionVector &distinct, const SelectionVector &matching) {
255865 const auto count = MinValue<idx_t>(prev.size(), curr.size());
255866
255867 // The input index has probably been sliced.
255868 UnifiedVectorFormat input_format;
255869 curr.data.back().ToUnifiedFormat(count, input_format);
255870 auto input_idx = UnifiedVectorFormat::GetData<idx_t>(input_format);
255871
255872 const auto nmatch = count - ndistinct;
255873 // 9: if sorted[i].first == sorted[i-1].first then
255874 // 10: prevIdcs[i] ← sorted[i-1].second
255875 for (idx_t j = 0; j < nmatch; ++j) {
255876 auto scan_idx = matching.get_index(j);
255877 auto i = input_idx[input_format.sel->get_index(scan_idx)];
255878 auto second = scan_idx ? input_idx[input_format.sel->get_index(scan_idx - 1)] : prev_i;
255879 prev_idcs[i] = ZippedTuple(second + 1, i);
255880 }
255881 // 11: else
255882 // 12: prevIdcs[i] ← “-”
255883 for (idx_t j = 0; j < ndistinct; ++j) {
255884 auto scan_idx = distinct.get_index(j);
255885 auto i = input_idx[input_format.sel->get_index(scan_idx)];
255886 prev_idcs[i] = ZippedTuple(0, i);
255887 }
255888
255889 // Remember the last input_idx of this chunk.
255890 prev_i = input_idx[input_format.sel->get_index(count - 1)];
255891 });
255892
255893 // 13: return prevIdcs
255894}
idx_t block_idx
Finalize scan block index.
Definition duckdb.cpp:255618
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237

◆ ExecuteTask()

void duckdb::WindowDistinctAggregatorLocalState::ExecuteTask ( ExecutionContext & context,
WindowDistinctAggregatorGlobalState & gdstate )
255714 {
255715 PostIncrement<atomic<idx_t>> on_done(gdstate.tasks_completed);
255716
255717 switch (stage) {
255718 case WindowDistinctSortStage::COMBINE: {
255719 auto &local_sink = *gdstate.local_sinks[block_idx];
255720 InterruptState interrupt_state;
255721 OperatorSinkCombineInput combine {*gdstate.global_sink, local_sink, interrupt_state};
255722 gdstate.sort->Combine(context, combine);
255723 break;
255724 }
255725 case WindowDistinctSortStage::FINALIZE: {
255726 // 5: Sort sorted lexicographically increasing
255727 auto &sort = *gdstate.sort;
255728 InterruptState interrupt;
255729 OperatorSinkFinalizeInput finalize {*gdstate.global_sink, interrupt};
255730 sort.Finalize(context.client, finalize);
255731 auto sort_global = sort.GetGlobalSourceState(context.client, *gdstate.global_sink);
255732 auto sort_local = sort.GetLocalSourceState(context, *sort_global);
255733 OperatorSourceInput source {*sort_global, *sort_local, interrupt};
255734 sort.MaterializeColumnData(context, source);
255735 gdstate.sorted = sort.GetColumnData(source);
255736 break;
255737 }
255738 case WindowDistinctSortStage::SORTED:
255739 Sorted();
255740 break;
255741 default:
255742 break;
255743 }
255744}
WindowDistinctSortStage stage
Finalize stage.
Definition duckdb.cpp:255616
void sort(InputArray src, OutputArray dst, int flags)
cv::GKernelPackage combine(const cv::GKernelPackage &lhs, const cv::GKernelPackage &rhs)

◆ Evaluate()

void duckdb::WindowDistinctAggregatorLocalState::Evaluate ( ExecutionContext & context,
const WindowDistinctAggregatorGlobalState & gdstate,
const DataChunk & bounds,
Vector & result,
idx_t count,
idx_t row_idx )
256068 {
256069 auto ldata = FlatVector::GetData<const_data_ptr_t>(statel);
256070 auto pdata = FlatVector::GetData<data_ptr_t>(statep);
256071
256072 const auto &merge_sort_tree = gdstate.merge_sort_tree;
256073 const auto &levels_flat_native = gdstate.levels_flat_native;
256074 const auto exclude_mode = gdstate.aggregator.exclude_mode;
256075
256076 // Build the finalise vector that just points to the result states
256077 statef.Initialize(count);
256078
256079 WindowAggregator::EvaluateSubFrames(bounds, exclude_mode, count, row_idx, frames, [&](idx_t rid) {
256080 auto agg_state = statef.GetStatePtr(rid);
256081
256082 // TODO: Extend AggregateLowerBound to handle subframes, just like SelectNth.
256083 const auto lower = frames[0].start;
256084 const auto upper = frames[0].end;
256085 merge_sort_tree.AggregateLowerBound(lower, upper, lower + 1,
256086 [&](idx_t level, const idx_t run_begin, const idx_t run_pos) {
256087 if (run_pos != run_begin) {
256088 // Find the source aggregate
256089 // Buffer a merge of the indicated state into the current state
256090 const auto agg_idx = gdstate.levels_flat_start[level] + run_pos - 1;
256091 const auto running_agg = levels_flat_native.GetStatePtr(agg_idx);
256092 pdata[flush_count] = agg_state;
256093 ldata[flush_count++] = running_agg;
256094 if (flush_count >= STANDARD_VECTOR_SIZE) {
256095 FlushStates();
256096 }
256097 }
256098 });
256099 });
256100
256101 // Flush the final states
256102 FlushStates();
256103
256104 // Finalise the result aggregates and write to the result
256105 statef.Finalize(result);
256106
256107 // Destruct any non-POD state
256108 statef.Destroy();
256109}
void FlushStates()
Flush the accumulated intermediate states into the result states.
Definition duckdb.cpp:256053

◆ FlushStates()

void duckdb::WindowDistinctAggregatorLocalState::FlushStates ( )
protected

Flush the accumulated intermediate states into the result states.

256053 {
256054 if (!flush_count) {
256055 return;
256056 }
256057
256058 const auto &aggr = gdstate.aggr;
256059 AggregateInputData aggr_input_data(aggr.GetFunctionData(), allocator);
256060 statel.Verify(flush_count);
256061 aggr.function.GetStateCombineCallback()(statel, statep, aggr_input_data, flush_count);
256062
256063 flush_count = 0;
256064}
ArenaAllocator allocator
Global allocator.
Definition duckdb.cpp:164091

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