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

Classes

struct  HashRow
struct  EqualRow

Public Types

using RowSet = std::unordered_set<idx_t, HashRow, EqualRow>
Public Types inherited from duckdb::WindowAggregatorLocalState
using CollectionPtr = optional_ptr<WindowCollection>

Public Member Functions

 WindowNaiveLocalState (ExecutionContext &context, const WindowNaiveAggregator &aggregator)
void Finalize (ExecutionContext &context, WindowAggregatorGlobalState &gastate, CollectionPtr collection) override
void Evaluate (ExecutionContext &context, const WindowAggregatorGlobalState &gsink, const DataChunk &bounds, Vector &result, idx_t count, idx_t row_idx, InterruptState &interrupt)
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

Protected Member Functions

void FlushStates (const WindowAggregatorGlobalState &gsink)
 Flush the accumulated intermediate states into the result states.
size_t Hash (idx_t rid)
 Hashes a value for the hash table.
bool KeyEqual (const idx_t &lhs, const idx_t &rhs)
 Compares two values for the hash table.

Protected Attributes

const WindowNaiveAggregator & aggregator
 The global state.
vector< data_t > state
 Data pointer that contains a vector of states, used for row aggregation.
Vector statef
 Reused result state container for the aggregate.
Vector statep
 A vector of pointers to "state", used for buffering intermediate aggregates.
DataChunk leaves
 Input data chunk, used for leaf segment aggregation.
SelectionVector update_sel
 The rows beging updated.
idx_t flush_count
 Count of buffered values.
SubFrames frames
 The frame boundaries, used for EXCLUDE.
Vector hashes
 The optional hash table used for DISTINCT.
unique_ptr< WindowCursor > comparer
 The state used for comparing the collection across chunk boundaries.
unique_ptr< Sort > sort
 The state used for scanning ORDER BY values from the collection.
unique_ptr< WindowCursor > arg_orderer
 The order by collection.
DataChunk orderby_sink
 Reusable sort key chunk.
DataChunk orderby_scan
 Reusable sort payload chunk.
SelectionVector orderby_sel
 Reusable sort key slicer.

Additional Inherited Members

Static Public Member Functions inherited from duckdb::WindowAggregatorLocalState
static void InitSubFrames (SubFrames &frames, const WindowExcludeMode exclude_mode)
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.

Constructor & Destructor Documentation

◆ WindowNaiveLocalState()

duckdb::WindowNaiveLocalState::WindowNaiveLocalState ( ExecutionContext & context,
const WindowNaiveAggregator & aggregator )
256680 : WindowAggregatorLocalState(context), aggregator(aggregator), state(aggregator.state_size * STANDARD_VECTOR_SIZE),
256681 statef(LogicalType::POINTER), statep((LogicalType::POINTER)), flush_count(0), hashes(LogicalType::HASH) {
256682 InitSubFrames(frames, aggregator.exclude_mode);
256683
256684 update_sel.Initialize();
256685
256686 // Build the finalise vector that just points to the result states
256687 data_ptr_t state_ptr = state.data();
256688 D_ASSERT(statef.GetVectorType() == VectorType::FLAT_VECTOR);
256689 statef.SetVectorType(VectorType::CONSTANT_VECTOR);
256690 statef.Flatten(STANDARD_VECTOR_SIZE);
256691 auto fdata = FlatVector::GetData<data_ptr_t>(statef);
256692 for (idx_t i = 0; i < STANDARD_VECTOR_SIZE; ++i) {
256693 fdata[i] = state_ptr;
256694 state_ptr += aggregator.state_size;
256695 }
256696}
SelectionVector update_sel
The rows beging updated.
Definition duckdb.cpp:256657
const WindowNaiveAggregator & aggregator
The global state.
Definition duckdb.cpp:256647
idx_t flush_count
Count of buffered values.
Definition duckdb.cpp:256659
Vector statep
A vector of pointers to "state", used for buffering intermediate aggregates.
Definition duckdb.cpp:256653
Vector statef
Reused result state container for the aggregate.
Definition duckdb.cpp:256651
vector< data_t > state
Data pointer that contains a vector of states, used for row aggregation.
Definition duckdb.cpp:256649
Vector hashes
The optional hash table used for DISTINCT.
Definition duckdb.cpp:256663
SubFrames frames
The frame boundaries, used for EXCLUDE.
Definition duckdb.cpp:256661
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237

Member Function Documentation

◆ Finalize()

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

Reimplemented from duckdb::WindowAggregatorLocalState.

256699 {
256700 WindowAggregatorLocalState::Finalize(context, gastate, collection);
256701
256702 // Set up the comparison scanner just in case
256703 if (!comparer) {
256704 comparer = make_uniq<WindowCursor>(*collection, aggregator.child_idx);
256705 }
256706
256707 // Set up the argument ORDER BY scanner if needed
256708 if (!aggregator.arg_order_idx.empty() && !arg_orderer) {
256709 arg_orderer = make_uniq<WindowCursor>(*collection, aggregator.arg_order_idx);
256710 auto input_types = arg_orderer->chunk.GetTypes();
256711 input_types.emplace_back(LogicalType::UBIGINT);
256712 orderby_sink.Initialize(BufferAllocator::Get(gastate.client), input_types);
256713
256714 // The sort expressions have already been computed, so we just need to reference them
256715 vector<BoundOrderByNode> orders;
256716 for (const auto &order_by : aggregator.wexpr.arg_orders) {
256717 auto order = order_by.Copy();
256718 const auto &type = order.expression->return_type;
256719 order.expression = make_uniq<BoundReferenceExpression>(type, orders.size());
256720 orders.emplace_back(std::move(order));
256721 }
256722
256723 // We only want the row numbers
256724 vector<idx_t> projection_map(1, input_types.size() - 1);
256725 orderby_scan.Initialize(BufferAllocator::Get(gastate.client), {input_types.back()});
256726 sort = make_uniq<Sort>(context.client, orders, input_types, projection_map);
256727
256728 orderby_sel.Initialize();
256729 }
256730
256731 // Initialise the chunks
256732 const auto types = cursor->chunk.GetTypes();
256733 if (leaves.ColumnCount() == 0 && !types.empty()) {
256734 leaves.Initialize(BufferAllocator::Get(context.client), types);
256735 }
256736}
unique_ptr< WindowCursor > cursor
The state used for reading the collection.
Definition duckdb.cpp:164094
unique_ptr< Sort > sort
The state used for scanning ORDER BY values from the collection.
Definition duckdb.cpp:256668
unique_ptr< WindowCursor > comparer
The state used for comparing the collection across chunk boundaries.
Definition duckdb.cpp:256665
DataChunk orderby_sink
Reusable sort key chunk.
Definition duckdb.cpp:256672
SelectionVector orderby_sel
Reusable sort key slicer.
Definition duckdb.cpp:256676
unique_ptr< WindowCursor > arg_orderer
The order by collection.
Definition duckdb.cpp:256670
DataChunk orderby_scan
Reusable sort payload chunk.
Definition duckdb.cpp:256674
DataChunk leaves
Input data chunk, used for leaf segment aggregation.
Definition duckdb.cpp:256655

◆ Evaluate()

void duckdb::WindowNaiveLocalState::Evaluate ( ExecutionContext & context,
const WindowAggregatorGlobalState & gsink,
const DataChunk & bounds,
Vector & result,
idx_t count,
idx_t row_idx,
InterruptState & interrupt )
256804 {
256805 const auto &aggr = gsink.aggr;
256806 auto &filter_mask = gsink.filter_mask;
256807 const auto types = cursor->chunk.GetTypes();
256808
256809 auto fdata = FlatVector::GetData<data_ptr_t>(statef);
256810 auto pdata = FlatVector::GetData<data_ptr_t>(statep);
256811
256812 HashRow hash_row(*this);
256813 EqualRow equal_row(*this);
256814 RowSet row_set(STANDARD_VECTOR_SIZE, hash_row, equal_row);
256815
256816 WindowAggregator::EvaluateSubFrames(bounds, aggregator.exclude_mode, count, row_idx, frames, [&](idx_t rid) {
256817 auto agg_state = fdata[rid];
256818 aggr.function.GetStateInitCallback()(aggr.function, agg_state);
256819
256820 // Reset the DISTINCT hash table
256821 row_set.clear();
256822
256823 // Sort the input rows by the argument
256824 if (arg_orderer) {
256825 auto global_sink = sort->GetGlobalSinkState(context.client);
256826 auto local_sink = sort->GetLocalSinkState(context);
256827 OperatorSinkInput sink {*global_sink, *local_sink, interrupt};
256828
256829 idx_t orderby_count = 0;
256830 auto orderby_row = FlatVector::GetData<idx_t>(orderby_sink.data.back());
256831 for (const auto &frame : frames) {
256832 for (auto f = frame.start; f < frame.end; ++f) {
256833 // FILTER before the ORDER BY
256834 if (!filter_mask.RowIsValid(f)) {
256835 continue;
256836 }
256837
256838 if (!arg_orderer->RowIsVisible(f) || orderby_count >= STANDARD_VECTOR_SIZE) {
256839 if (orderby_count) {
256840 for (column_t c = 0; c < arg_orderer->chunk.ColumnCount(); ++c) {
256841 orderby_sink.data[c].Reference(arg_orderer->chunk.data[c]);
256842 }
256843 orderby_sink.Slice(orderby_sel, orderby_count);
256844 sort->Sink(context, orderby_sink, sink);
256845 orderby_sink.Reset();
256846 }
256847 orderby_count = 0;
256848 arg_orderer->Seek(f);
256849 // Fill in the row numbers
256850 for (idx_t i = 0; i < arg_orderer->chunk.size(); ++i) {
256851 orderby_row[i] = arg_orderer->state.current_row_index + i;
256852 }
256853 }
256854 orderby_sel.set_index(orderby_count++, arg_orderer->RowOffset(f));
256855 }
256856 }
256857 if (orderby_count) {
256858 for (column_t c = 0; c < arg_orderer->chunk.ColumnCount(); ++c) {
256859 orderby_sink.data[c].Reference(arg_orderer->chunk.data[c]);
256860 }
256861 orderby_sink.Slice(orderby_sel, orderby_count);
256862 sort->Sink(context, orderby_sink, sink);
256863 orderby_sink.Reset();
256864 }
256865
256866 OperatorSinkCombineInput combine {*global_sink, *local_sink, interrupt};
256867 sort->Combine(context, combine);
256868
256869 OperatorSinkFinalizeInput finalize {*global_sink, interrupt};
256870 sort->Finalize(context.client, finalize);
256871
256872 auto global_source = sort->GetGlobalSourceState(context.client, *global_sink);
256873 auto local_source = sort->GetLocalSourceState(context, *global_source);
256874 OperatorSourceInput source {*global_source, *local_source, interrupt};
256875 orderby_scan.Reset();
256876 for (; SourceResultType::FINISHED != sort->GetData(context, orderby_scan, source); orderby_scan.Reset()) {
256877 orderby_row = FlatVector::GetData<idx_t>(orderby_scan.data[0]);
256878 for (idx_t i = 0; i < orderby_scan.size(); ++i) {
256879 const auto f = orderby_row[i];
256880 // Seek to the current position
256881 if (!cursor->RowIsVisible(f)) {
256882 // We need to flush when we cross a chunk boundary
256883 FlushStates(gsink);
256884 cursor->Seek(f);
256885 }
256886
256887 // Filter out duplicates
256888 if (aggr.IsDistinct() && !row_set.insert(f).second) {
256889 continue;
256890 }
256891
256892 pdata[flush_count] = agg_state;
256893 update_sel[flush_count++] = cursor->RowOffset(f);
256894 if (flush_count >= STANDARD_VECTOR_SIZE) {
256895 FlushStates(gsink);
256896 }
256897 }
256898 }
256899 return;
256900 }
256901
256902 // Just update the aggregate with the unfiltered input rows
256903 for (const auto &frame : frames) {
256904 for (auto f = frame.start; f < frame.end; ++f) {
256905 if (!filter_mask.RowIsValid(f)) {
256906 continue;
256907 }
256908
256909 // Seek to the current position
256910 if (!cursor->RowIsVisible(f)) {
256911 // We need to flush when we cross a chunk boundary
256912 FlushStates(gsink);
256913 cursor->Seek(f);
256914 }
256915
256916 // Filter out duplicates
256917 if (aggr.IsDistinct() && !row_set.insert(f).second) {
256918 continue;
256919 }
256920
256921 pdata[flush_count] = agg_state;
256922 update_sel[flush_count++] = cursor->RowOffset(f);
256923 if (flush_count >= STANDARD_VECTOR_SIZE) {
256924 FlushStates(gsink);
256925 }
256926 }
256927 }
256928 });
256929
256930 // Flush the final states
256931 FlushStates(gsink);
256932
256933 // Finalise the result aggregates and write to the result
256934 AggregateInputData aggr_input_data(aggr.GetFunctionData(), allocator);
256935 aggr.function.GetStateFinalizeCallback()(statef, aggr_input_data, result, count, 0);
256936
256937 // Destruct the result aggregates
256938 if (aggr.function.HasStateDestructorCallback()) {
256939 aggr.function.GetStateDestructorCallback()(statef, aggr_input_data, count);
256940 }
256941}
ArenaAllocator allocator
Global allocator.
Definition duckdb.cpp:164091
void FlushStates(const WindowAggregatorGlobalState &gsink)
Flush the accumulated intermediate states into the result states.
Definition duckdb.cpp:256738
Definition duckdb.cpp:256617
Definition duckdb.cpp:256606

◆ FlushStates()

void duckdb::WindowNaiveLocalState::FlushStates ( const WindowAggregatorGlobalState & gsink)
protected

Flush the accumulated intermediate states into the result states.

256738 {
256739 if (!flush_count) {
256740 return;
256741 }
256742
256743 auto &scanned = cursor->chunk;
256744 leaves.Slice(scanned, update_sel, flush_count);
256745
256746 const auto &aggr = gsink.aggr;
256747 AggregateInputData aggr_input_data(aggr.GetFunctionData(), allocator);
256748 aggr.function.GetStateUpdateCallback()(leaves.data.data(), aggr_input_data, leaves.ColumnCount(), statep,
256749 flush_count);
256750
256751 flush_count = 0;
256752}

◆ Hash()

size_t duckdb::WindowNaiveLocalState::Hash ( idx_t rid)
protected

Hashes a value for the hash table.

256754 {
256755 D_ASSERT(cursor->RowIsVisible(rid));
256756 auto s = cursor->RowOffset(rid);
256757 auto &scanned = cursor->chunk;
256758 SelectionVector sel(&s);
256759 leaves.Slice(scanned, sel, 1);
256760 leaves.Hash(hashes);
256761
256762 return *FlatVector::GetData<hash_t>(hashes);
256763}

◆ KeyEqual()

bool duckdb::WindowNaiveLocalState::KeyEqual ( const idx_t & lhs,
const idx_t & rhs )
protected

Compares two values for the hash table.

256765 {
256766 // One of the indices will be scanned, so make it the left one
256767 auto lhs = lidx;
256768 auto rhs = ridx;
256769 if (!cursor->RowIsVisible(lhs)) {
256770 std::swap(lhs, rhs);
256771 D_ASSERT(cursor->RowIsVisible(lhs));
256772 }
256773
256774 auto &scanned = cursor->chunk;
256775 auto l = cursor->RowOffset(lhs);
256776 SelectionVector lsel(&l);
256777
256778 auto rreader = cursor.get();
256779 if (!cursor->RowIsVisible(rhs)) {
256780 // Values on different pages!
256781 rreader = comparer.get();
256782 rreader->Seek(rhs);
256783 }
256784 auto rscanned = &rreader->chunk;
256785 auto r = rreader->RowOffset(rhs);
256786 SelectionVector rsel(&r);
256787
256788 sel_t f = 0;
256789 SelectionVector fsel(&f);
256790
256791 for (column_t c = 0; c < scanned.ColumnCount(); ++c) {
256792 Vector left(scanned.data[c], lsel, 1);
256793 Vector right(rscanned->data[c], rsel, 1);
256794 if (!VectorOperations::NotDistinctFrom(left, right, nullptr, 1, nullptr, &fsel)) {
256795 return false;
256796 }
256797 }
256798
256799 return true;
256800}
idx_t column_t
Type used for column identifiers.
Definition duckdb.hpp:259
uint32_t sel_t
Type used for the selection vector.
Definition duckdb.hpp:251

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