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

Public Types

enum  FramePart : uint8_t { FULL = 0 , LEFT = 1 , RIGHT = 2 }
using RightEntry = std::pair<idx_t, idx_t>
 Right side nodes need to be cached and processed in reverse order.

Public Member Functions

 WindowSegmentTreePart (ArenaAllocator &allocator, const AggregateObject &aggr, unique_ptr< WindowCursor > cursor, const ValidityArray &filter_mask)
unique_ptr< WindowSegmentTreePart > Copy () const
void FlushStates (bool combining)
void ExtractFrame (idx_t begin, idx_t end, data_ptr_t current_state)
void WindowSegmentValue (const WindowSegmentTreeGlobalState &tree, idx_t l_idx, idx_t begin, idx_t end, data_ptr_t current_state)
void Finalize (Vector &result, idx_t count)
 Writes result and calls destructors.
void Combine (WindowSegmentTreePart &other, idx_t count)
void Evaluate (const WindowSegmentTreeGlobalState &tree, const idx_t *begins, const idx_t *ends, const idx_t *bounds, Vector &result, idx_t count, idx_t row_idx, FramePart frame_part)

Public Attributes

ArenaAllocator & allocator
 Allocator for aggregates.
const AggregateObject & aggr
 The aggregate function.
const bool order_insensitive
 Order insensitive aggregate (we can optimise internal combines).
const ValidityArray & filter_mask
 The filtered rows in inputs.
const idx_t state_size
 The size of a single aggregate state.
vector< data_t > state
 Data pointer that contains a vector of states, used for intermediate window segment aggregation.
unique_ptr< WindowCursor > cursor
 Scanned data state.
DataChunk leaves
 Input data chunk, used for leaf segment aggregation.
SelectionVector filter_sel
 The filtered rows in inputs.
Vector statep
 A vector of pointers to "state", used for intermediate window segment aggregation.
Vector statel
 Reused state pointers for combining segment tree levels.
Vector statef
 Reused result state container for the window functions.
idx_t flush_count
 Count of buffered values.
vector< RightEntry > right_stack
 Cache of right side tree ranges for ordered aggregates.

Protected Member Functions

void Initialize (idx_t count)
 Initialises the accumulation state vector (statef).
void EvaluateUpperLevels (const WindowSegmentTreeGlobalState &tree, const idx_t *begins, const idx_t *ends, const idx_t *bounds, idx_t count, idx_t row_idx, FramePart frame_part)
 Accumulate upper tree levels.
void EvaluateLeaves (const WindowSegmentTreeGlobalState &tree, const idx_t *begins, const idx_t *ends, const idx_t *bounds, idx_t count, idx_t row_idx, FramePart frame_part, FramePart leaf_part)

Static Protected Member Functions

static const idx_t * FrameBegins (const idx_t *begins, const idx_t *ends, const idx_t *bounds, FramePart frame_part)
static const idx_t * FrameEnds (const idx_t *begins, const idx_t *ends, const idx_t *bounds, FramePart frame_part)

Member Enumeration Documentation

◆ FramePart

enum duckdb::WindowSegmentTreePart::FramePart : uint8_t
257610: uint8_t { FULL = 0, LEFT = 1, RIGHT = 2 };
::uint8_t uint8_t

Constructor & Destructor Documentation

◆ WindowSegmentTreePart()

duckdb::WindowSegmentTreePart::WindowSegmentTreePart ( ArenaAllocator & allocator,
const AggregateObject & aggr,
unique_ptr< WindowCursor > cursor,
const ValidityArray & filter_mask )
257706 order_insensitive(aggr.function.GetOrderDependent() == AggregateOrderDependent::NOT_ORDER_DEPENDENT),
257707 filter_mask(filter_mask), state_size(aggr.function.GetStateSizeCallback()(aggr.function)),
257708 state(state_size * STANDARD_VECTOR_SIZE), cursor(std::move(cursor_p)), statep(LogicalType::POINTER),
257709 statel(LogicalType::POINTER), statef(LogicalType::POINTER), flush_count(0) {
257710 auto &inputs = cursor->chunk;
257711 if (inputs.ColumnCount() > 0) {
257712 leaves.Initialize(Allocator::DefaultAllocator(), inputs.GetTypes());
257713 filter_sel.Initialize();
257714 }
257715
257716 // Build the finalise vector that just points to the result states
257717 data_ptr_t state_ptr = state.data();
257718 D_ASSERT(statef.GetVectorType() == VectorType::FLAT_VECTOR);
257719 statef.SetVectorType(VectorType::CONSTANT_VECTOR);
257720 statef.Flatten(STANDARD_VECTOR_SIZE);
257721 auto fdata = FlatVector::GetData<data_ptr_t>(statef);
257722 for (idx_t i = 0; i < STANDARD_VECTOR_SIZE; ++i) {
257723 fdata[i] = state_ptr;
257724 state_ptr += state_size;
257725 }
257726}
DataChunk leaves
Input data chunk, used for leaf segment aggregation.
Definition duckdb.cpp:257666
const idx_t state_size
The size of a single aggregate state.
Definition duckdb.cpp:257660
unique_ptr< WindowCursor > cursor
Scanned data state.
Definition duckdb.cpp:257664
const bool order_insensitive
Order insensitive aggregate (we can optimise internal combines).
Definition duckdb.cpp:257656
Vector statel
Reused state pointers for combining segment tree levels.
Definition duckdb.cpp:257672
Vector statef
Reused result state container for the window functions.
Definition duckdb.cpp:257674
vector< data_t > state
Data pointer that contains a vector of states, used for intermediate window segment aggregation.
Definition duckdb.cpp:257662
SelectionVector filter_sel
The filtered rows in inputs.
Definition duckdb.cpp:257668
Vector statep
A vector of pointers to "state", used for intermediate window segment aggregation.
Definition duckdb.cpp:257670
const ValidityArray & filter_mask
The filtered rows in inputs.
Definition duckdb.cpp:257658
idx_t flush_count
Count of buffered values.
Definition duckdb.cpp:257676
const AggregateObject & aggr
The aggregate function.
Definition duckdb.cpp:257654
ArenaAllocator & allocator
Allocator for aggregates.
Definition duckdb.cpp:257652
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237

◆ ~WindowSegmentTreePart()

duckdb::WindowSegmentTreePart::~WindowSegmentTreePart ( )
257728 {
257729}

Member Function Documentation

◆ Copy()

unique_ptr< WindowSegmentTreePart > duckdb::WindowSegmentTreePart::Copy ( ) const
inline
257616 {
257617 return make_uniq<WindowSegmentTreePart>(allocator, aggr, cursor->Copy(), filter_mask);
257618 }

◆ FlushStates()

void duckdb::WindowSegmentTreePart::FlushStates ( bool combining)
257741 {
257742 if (!flush_count) {
257743 return;
257744 }
257745
257746 AggregateInputData aggr_input_data(aggr.GetFunctionData(), allocator);
257747 if (combining) {
257748 statel.Verify(flush_count);
257749 aggr.function.GetStateCombineCallback()(statel, statep, aggr_input_data, flush_count);
257750 } else {
257751 auto &scanned = cursor->chunk;
257752 leaves.Slice(scanned, filter_sel, flush_count);
257753 aggr.function.GetStateUpdateCallback()(&leaves.data[0], aggr_input_data, leaves.ColumnCount(), statep,
257754 flush_count);
257755 }
257756
257757 flush_count = 0;
257758}

◆ ExtractFrame()

void duckdb::WindowSegmentTreePart::ExtractFrame ( idx_t begin,
idx_t end,
data_ptr_t current_state )
257765 {
257766 const auto count = end - begin;
257767
257768 // If we are not filtering,
257769 // just update the shared dictionary selection to the range
257770 // Otherwise set it to the input rows that pass the filter
257771 auto states = FlatVector::GetData<data_ptr_t>(statep);
257772 if (filter_mask.AllValid()) {
257773 const auto offset = cursor->RowOffset(begin);
257774 for (idx_t i = 0; i < count; ++i) {
257775 states[flush_count] = state_ptr;
257776 filter_sel.set_index(flush_count++, offset + i);
257777 if (flush_count >= STANDARD_VECTOR_SIZE) {
257778 FlushStates(false);
257779 }
257780 }
257781 } else {
257782 for (idx_t i = begin; i < end; ++i) {
257783 if (filter_mask.RowIsValid(i)) {
257784 states[flush_count] = state_ptr;
257785 filter_sel.set_index(flush_count++, cursor->RowOffset(i));
257786 if (flush_count >= STANDARD_VECTOR_SIZE) {
257787 FlushStates(false);
257788 }
257789 }
257790 }
257791 }
257792}

◆ WindowSegmentValue()

void duckdb::WindowSegmentTreePart::WindowSegmentValue ( const WindowSegmentTreeGlobalState & tree,
idx_t l_idx,
idx_t begin,
idx_t end,
data_ptr_t current_state )
257795 {
257796 D_ASSERT(begin <= end);
257797 auto &inputs = cursor->chunk;
257798 if (begin == end || inputs.ColumnCount() == 0) {
257799 return;
257800 }
257801
257802 const auto count = end - begin;
257803 if (l_idx == 0) {
257804 // Check the leaves when they cross chunk boundaries
257805 while (begin < end) {
257806 if (!cursor->RowIsVisible(begin)) {
257807 FlushStates(false);
257808 cursor->Seek(begin);
257809 }
257810 auto next = MinValue(end, cursor->state.next_row_index);
257811 ExtractFrame(begin, next, state_ptr);
257812 begin = next;
257813 }
257814 } else {
257815 // find out where the states begin
257816 auto begin_ptr = tree.levels_flat_native.GetStatePtr(begin + tree.levels_flat_start[l_idx - 1]);
257817 // set up a vector of pointers that point towards the set of states
257818 auto ldata = FlatVector::GetData<const_data_ptr_t>(statel);
257819 auto pdata = FlatVector::GetData<data_ptr_t>(statep);
257820 for (idx_t i = 0; i < count; i++) {
257821 pdata[flush_count] = state_ptr;
257822 ldata[flush_count++] = begin_ptr;
257823 begin_ptr += state_size;
257824 if (flush_count >= STANDARD_VECTOR_SIZE) {
257825 FlushStates(true);
257826 }
257827 }
257828 }
257829}

◆ Finalize()

void duckdb::WindowSegmentTreePart::Finalize ( Vector & result,
idx_t count )

Writes result and calls destructors.

257830 {
257831 // Finalise the result aggregates and write to result if write_result is set
257832 AggregateInputData aggr_input_data(aggr.GetFunctionData(), allocator);
257833 aggr.function.GetStateFinalizeCallback()(statef, aggr_input_data, result, count, 0);
257834
257835 // Destruct the result aggregates
257836 if (aggr.function.HasStateDestructorCallback()) {
257837 aggr.function.GetStateDestructorCallback()(statef, aggr_input_data, count);
257838 }
257839}

◆ Combine()

void duckdb::WindowSegmentTreePart::Combine ( WindowSegmentTreePart & other,
idx_t count )
257760 {
257761 AggregateInputData aggr_input_data(aggr.GetFunctionData(), allocator);
257762 aggr.function.GetStateCombineCallback()(other.statef, statef, aggr_input_data, count);
257763}

◆ Evaluate()

void duckdb::WindowSegmentTreePart::Evaluate ( const WindowSegmentTreeGlobalState & tree,
const idx_t * begins,
const idx_t * ends,
const idx_t * bounds,
Vector & result,
idx_t count,
idx_t row_idx,
FramePart frame_part )
257988 {
257989 Initialize(count);
257990
257991 if (order_insensitive) {
257992 // First pass: aggregate the segment tree nodes with sharing
257993 EvaluateUpperLevels(tree, begins, ends, bounds, count, row_idx, frame_part);
257994
257995 // Second pass: aggregate the ragged leaves
257996 EvaluateLeaves(tree, begins, ends, bounds, count, row_idx, frame_part, FramePart::FULL);
257997 } else {
257998 // Evaluate leaves in order
257999 EvaluateLeaves(tree, begins, ends, bounds, count, row_idx, frame_part, FramePart::LEFT);
258000 EvaluateUpperLevels(tree, begins, ends, bounds, count, row_idx, frame_part);
258001 EvaluateLeaves(tree, begins, ends, bounds, count, row_idx, frame_part, FramePart::RIGHT);
258002 }
258003}
void EvaluateUpperLevels(const WindowSegmentTreeGlobalState &tree, const idx_t *begins, const idx_t *ends, const idx_t *bounds, idx_t count, idx_t row_idx, FramePart frame_part)
Accumulate upper tree levels.
Definition duckdb.cpp:258013
void Initialize(idx_t count)
Initialises the accumulation state vector (statef).
Definition duckdb.cpp:258005

◆ Initialize()

void duckdb::WindowSegmentTreePart::Initialize ( idx_t count)
protected

Initialises the accumulation state vector (statef).

258005 {
258006 auto fdata = FlatVector::GetData<data_ptr_t>(statef);
258007 for (idx_t rid = 0; rid < count; ++rid) {
258008 auto state_ptr = fdata[rid];
258009 aggr.function.GetStateInitCallback()(aggr.function, state_ptr);
258010 }
258011}

◆ EvaluateUpperLevels()

void duckdb::WindowSegmentTreePart::EvaluateUpperLevels ( const WindowSegmentTreeGlobalState & tree,
const idx_t * begins,
const idx_t * ends,
const idx_t * bounds,
idx_t count,
idx_t row_idx,
FramePart frame_part )
protected

Accumulate upper tree levels.

258015 {
258016 auto fdata = FlatVector::GetData<data_ptr_t>(statef);
258017
258018 const auto exclude_mode = tree.tree.exclude_mode;
258019 const bool begin_on_curr_row = frame_part == FramePart::RIGHT && exclude_mode == WindowExcludeMode::CURRENT_ROW;
258020 const bool end_on_curr_row = frame_part == FramePart::LEFT && exclude_mode == WindowExcludeMode::CURRENT_ROW;
258021
258022 // We need the full range of the frame to clamp
258023 auto frame_begins = FrameBegins(begins, ends, bounds, frame_part);
258024 auto frame_ends = FrameEnds(begins, ends, bounds, frame_part);
258025
258026 const auto max_level = tree.levels_flat_start.size() + 1;
258027 right_stack.resize(max_level, {0, 0});
258028
258029 // Share adjacent identical states
258030 // We do this first because we want to share only tree aggregations
258031 idx_t prev_begin = 1;
258032 idx_t prev_end = 0;
258033 auto ldata = FlatVector::GetData<data_ptr_t>(statel);
258034 auto pdata = FlatVector::GetData<data_ptr_t>(statep);
258035 data_ptr_t prev_state = nullptr;
258036 for (idx_t rid = 0, cur_row = row_idx; rid < count; ++rid, ++cur_row) {
258037 auto state_ptr = fdata[rid];
258038
258039 auto begin = MaxValue(begin_on_curr_row ? cur_row + 1 : begins[rid], frame_begins[rid]);
258040 auto end = MinValue(end_on_curr_row ? cur_row : ends[rid], frame_ends[rid]);
258041 if (begin >= end) {
258042 continue;
258043 }
258044
258045 // Skip level 0
258046 idx_t l_idx = 0;
258047 idx_t right_max = 0;
258048 for (; l_idx < max_level; l_idx++) {
258049 idx_t parent_begin = begin / tree.TREE_FANOUT;
258050 idx_t parent_end = end / tree.TREE_FANOUT;
258051 if (prev_state && l_idx == 1 && begin == prev_begin && end == prev_end) {
258052 // Just combine the previous top level result
258053 ldata[flush_count] = prev_state;
258054 pdata[flush_count] = state_ptr;
258055 if (++flush_count >= STANDARD_VECTOR_SIZE) {
258056 FlushStates(true);
258057 }
258058 break;
258059 }
258060
258061 if (order_insensitive && l_idx == 1) {
258062 prev_state = state_ptr;
258063 prev_begin = begin;
258064 prev_end = end;
258065 }
258066
258067 if (parent_begin == parent_end) {
258068 if (l_idx) {
258069 WindowSegmentValue(tree, l_idx, begin, end, state_ptr);
258070 }
258071 break;
258072 }
258073 idx_t group_begin = parent_begin * tree.TREE_FANOUT;
258074 if (begin != group_begin) {
258075 if (l_idx) {
258076 WindowSegmentValue(tree, l_idx, begin, group_begin + tree.TREE_FANOUT, state_ptr);
258077 }
258078 parent_begin++;
258079 }
258080 idx_t group_end = parent_end * tree.TREE_FANOUT;
258081 if (end != group_end) {
258082 if (l_idx) {
258083 if (order_insensitive) {
258084 WindowSegmentValue(tree, l_idx, group_end, end, state_ptr);
258085 } else {
258086 right_stack[l_idx] = {group_end, end};
258087 right_max = l_idx;
258088 }
258089 }
258090 }
258091 begin = parent_begin;
258092 end = parent_end;
258093 }
258094
258095 // Flush the right side values from left to right for order_sensitive aggregates
258096 // As we go up the tree, the right side ranges move left,
258097 // so we just cache them in a fixed size, preallocated array.
258098 // Then we can just reverse scan the array and append the cached ranges.
258099 for (l_idx = right_max; l_idx > 0; --l_idx) {
258100 auto &right_entry = right_stack[l_idx];
258101 const auto group_end = right_entry.first;
258102 const auto end = right_entry.second;
258103 if (end) {
258104 WindowSegmentValue(tree, l_idx, group_end, end, state_ptr);
258105 right_entry = {0, 0};
258106 }
258107 }
258108 }
258109 FlushStates(true);
258110}
vector< RightEntry > right_stack
Cache of right side tree ranges for ordered aggregates.
Definition duckdb.cpp:257678

◆ EvaluateLeaves()

void duckdb::WindowSegmentTreePart::EvaluateLeaves ( const WindowSegmentTreeGlobalState & tree,
const idx_t * begins,
const idx_t * ends,
const idx_t * bounds,
idx_t count,
idx_t row_idx,
FramePart frame_part,
FramePart leaf_part )
protected
258114 {
258115 auto fdata = FlatVector::GetData<data_ptr_t>(statef);
258116
258117 // For order-sensitive aggregates, we have to process the ragged leaves in two pieces.
258118 // The left side have to be added before the main tree followed by the ragged right sides.
258119 // The current row is the leftmost value of the right hand side.
258120 const bool compute_left = leaf_part != FramePart::RIGHT;
258121 const bool compute_right = leaf_part != FramePart::LEFT;
258122 const auto exclude_mode = tree.tree.exclude_mode;
258123 const bool begin_on_curr_row = frame_part == FramePart::RIGHT && exclude_mode == WindowExcludeMode::CURRENT_ROW;
258124 const bool end_on_curr_row = frame_part == FramePart::LEFT && exclude_mode == WindowExcludeMode::CURRENT_ROW;
258125 // with EXCLUDE TIES, in addition to the frame part right of the peer group's end, we also need to consider the
258126 // current row
258127 const bool add_curr_row = compute_left && frame_part == FramePart::RIGHT && exclude_mode == WindowExcludeMode::TIES;
258128
258129 // We need the full range of the frame to clamp
258130 auto frame_begins = FrameBegins(begins, ends, bounds, frame_part);
258131 auto frame_ends = FrameEnds(begins, ends, bounds, frame_part);
258132
258133 for (idx_t rid = 0, cur_row = row_idx; rid < count; ++rid, ++cur_row) {
258134 auto state_ptr = fdata[rid];
258135
258136 const auto frame_begin = frame_begins[rid];
258137 auto begin = MaxValue(begin_on_curr_row ? cur_row + 1 : begins[rid], frame_begin);
258138
258139 const auto frame_end = frame_ends[rid];
258140 auto end = MinValue(end_on_curr_row ? cur_row : ends[rid], frame_end);
258141 if (add_curr_row && frame_begin <= cur_row && cur_row < frame_end) {
258142 WindowSegmentValue(tree, 0, cur_row, cur_row + 1, state_ptr);
258143 }
258144 if (begin >= end) {
258145 continue;
258146 }
258147
258148 idx_t parent_begin = begin / tree.TREE_FANOUT;
258149 idx_t parent_end = end / tree.TREE_FANOUT;
258150 if (parent_begin == parent_end) {
258151 if (compute_left) {
258152 WindowSegmentValue(tree, 0, begin, end, state_ptr);
258153 }
258154 continue;
258155 }
258156
258157 idx_t group_begin = parent_begin * tree.TREE_FANOUT;
258158 if (begin != group_begin && compute_left) {
258159 WindowSegmentValue(tree, 0, begin, group_begin + tree.TREE_FANOUT, state_ptr);
258160 }
258161 idx_t group_end = parent_end * tree.TREE_FANOUT;
258162 if (end != group_end && compute_right) {
258163 WindowSegmentValue(tree, 0, group_end, end, state_ptr);
258164 }
258165 }
258166 FlushStates(false);
258167}

◆ FrameBegins()

const idx_t * duckdb::WindowSegmentTreePart::FrameBegins ( const idx_t * begins,
const idx_t * ends,
const idx_t * bounds,
FramePart frame_part )
inlinestaticprotected
257642 {
257643 return frame_part == FramePart::RIGHT ? bounds : begins;
257644 }

◆ FrameEnds()

const idx_t * duckdb::WindowSegmentTreePart::FrameEnds ( const idx_t * begins,
const idx_t * ends,
const idx_t * bounds,
FramePart frame_part )
inlinestaticprotected
257646 {
257647 return frame_part == FramePart::LEFT ? bounds : ends;
257648 }

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