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

Public Member Functions

 WindowLeadLagExecutor (BoundWindowExpression &wexpr, WindowSharedExpressions &shared)
unique_ptr< GlobalSinkState > GetGlobalState (ClientContext &client, const idx_t payload_count, const ValidityMask &partition_mask, const ValidityMask &order_mask) const override
unique_ptr< LocalSinkState > GetLocalState (ExecutionContext &context, const GlobalSinkState &gstate) const override
Public Member Functions inherited from duckdb::WindowValueExecutor
 WindowValueExecutor (BoundWindowExpression &wexpr, WindowSharedExpressions &shared)
void Finalize (ExecutionContext &context, CollectionPtr collection, OperatorSinkInput &sink) const override
unique_ptr< GlobalSinkState > GetGlobalState (ClientContext &client, const idx_t payload_count, const ValidityMask &partition_mask, const ValidityMask &order_mask) const override
unique_ptr< LocalSinkState > GetLocalState (ExecutionContext &context, const GlobalSinkState &gstate) const override
Public Member Functions inherited from duckdb::WindowExecutor
 WindowExecutor (BoundWindowExpression &wexpr, WindowSharedExpressions &shared)
virtual bool IgnoreNulls () const
virtual void Sink (ExecutionContext &context, DataChunk &sink_chunk, DataChunk &coll_chunk, const idx_t input_idx, OperatorSinkInput &sink) const
void Evaluate (ExecutionContext &context, idx_t row_idx, DataChunk &eval_chunk, Vector &result, OperatorSinkInput &sink) const

Protected Member Functions

void EvaluateInternal (ExecutionContext &context, DataChunk &eval_chunk, Vector &result, idx_t count, idx_t row_idx, OperatorSinkInput &sink) const override

Additional Inherited Members

Public Types inherited from duckdb::WindowExecutor
using CollectionPtr = optional_ptr<WindowCollection>
Public Attributes inherited from duckdb::WindowValueExecutor
column_t child_idx = DConstants::INVALID_INDEX
 The column index of the value column.
column_t nth_idx = DConstants::INVALID_INDEX
 The column index of the Nth column.
column_t offset_idx = DConstants::INVALID_INDEX
 The column index of the offset column.
column_t default_idx = DConstants::INVALID_INDEX
 The column index of the default value column.
vector< column_t > arg_order_idx
 The column indices of any ORDER BY argument expressions.
Public Attributes inherited from duckdb::WindowExecutor
const BoundWindowExpression & wexpr
column_t boundary_start_idx = DConstants::INVALID_INDEX
column_t boundary_end_idx = DConstants::INVALID_INDEX
optional_ptr< Expression > range_expr
column_t range_idx = DConstants::INVALID_INDEX

Constructor & Destructor Documentation

◆ WindowLeadLagExecutor()

duckdb::WindowLeadLagExecutor::WindowLeadLagExecutor ( BoundWindowExpression & wexpr,
WindowSharedExpressions & shared )
258644 : WindowValueExecutor(wexpr, shared) {
258645}

Member Function Documentation

◆ GetGlobalState()

unique_ptr< GlobalSinkState > duckdb::WindowLeadLagExecutor::GetGlobalState ( ClientContext & client,
const idx_t payload_count,
const ValidityMask & partition_mask,
const ValidityMask & order_mask ) const
overridevirtual

Reimplemented from duckdb::WindowExecutor.

258649 {
258650 return make_uniq<WindowLeadLagGlobalState>(client, *this, payload_count, partition_mask, order_mask);
258651}

◆ GetLocalState()

unique_ptr< LocalSinkState > duckdb::WindowLeadLagExecutor::GetLocalState ( ExecutionContext & context,
const GlobalSinkState & gstate ) const
overridevirtual

Reimplemented from duckdb::WindowExecutor.

258654 {
258655 const auto &glstate = gstate.Cast<WindowLeadLagGlobalState>();
258656 return make_uniq<WindowLeadLagLocalState>(context, glstate);
258657}

◆ EvaluateInternal()

void duckdb::WindowLeadLagExecutor::EvaluateInternal ( ExecutionContext & context,
DataChunk & eval_chunk,
Vector & result,
idx_t count,
idx_t row_idx,
OperatorSinkInput & sink ) const
overrideprotectedvirtual

Implements duckdb::WindowExecutor.

258660 {
258661 auto &glstate = sink.global_state.Cast<WindowLeadLagGlobalState>();
258662 auto &llstate = sink.local_state.Cast<WindowLeadLagLocalState>();
258663 auto &cursor = *llstate.cursor;
258664
258665 WindowInputExpression leadlag_offset(eval_chunk, offset_idx);
258666 WindowInputExpression leadlag_default(eval_chunk, default_idx);
258667
258668 auto frame_begin = FlatVector::GetData<const idx_t>(llstate.bounds.data[FRAME_BEGIN]);
258669 auto frame_end = FlatVector::GetData<const idx_t>(llstate.bounds.data[FRAME_END]);
258670
258671 if (glstate.row_tree) {
258672 // TODO: Handle subframes (SelectNth can handle it but Rank can't)
258673 auto &frames = llstate.frames;
258674 frames.resize(1);
258675 auto &frame = frames[0];
258676 for (idx_t i = 0; i < count; ++i, ++row_idx) {
258677 int64_t offset = 1;
258678 if (wexpr.offset_expr) {
258679 if (leadlag_offset.CellIsNull(i)) {
258680 FlatVector::SetNull(result, i, true);
258681 continue;
258682 }
258683 offset = leadlag_offset.GetCell<int64_t>(i);
258684 }
258685
258686 // (1) compute the ROW_NUMBER of the own row
258687 frame = FrameBounds(frame_begin[i], frame_end[i]);
258688 const auto own_row = glstate.row_tree->Rank(frame.start, frame.end, row_idx) - 1;
258689 // (2) adjust the row number by adding or subtracting an offset
258690 auto val_idx = NumericCast<int64_t>(own_row);
258691 if (wexpr.GetExpressionType() == ExpressionType::WINDOW_LEAD) {
258692 val_idx = AddOperatorOverflowCheck::Operation<int64_t, int64_t, int64_t>(val_idx, offset);
258693 } else {
258694 val_idx = SubtractOperatorOverflowCheck::Operation<int64_t, int64_t, int64_t>(val_idx, offset);
258695 }
258696 const auto frame_width = NumericCast<int64_t>(frame.end - frame.start);
258697 if (val_idx >= 0 && val_idx < frame_width) {
258698 // (3) find the row at that offset
258699 const auto n = NumericCast<idx_t>(val_idx);
258700 const auto nth_index = glstate.value_tree->SelectNth(frames, n);
258701 // (4) evaluate the expression provided to LEAD/LAG on this row.
258702 if (nth_index.second) {
258703 // Overflow
258704 FlatVector::SetNull(result, i, true);
258705 } else {
258706 cursor.CopyCell(0, nth_index.first, result, i);
258707 }
258708 } else if (wexpr.default_expr) {
258709 leadlag_default.CopyCell(result, i);
258710 } else {
258711 FlatVector::SetNull(result, i, true);
258712 }
258713 }
258714 return;
258715 }
258716
258717 auto partition_begin = FlatVector::GetData<const idx_t>(llstate.bounds.data[PARTITION_BEGIN]);
258718 auto partition_end = FlatVector::GetData<const idx_t>(llstate.bounds.data[PARTITION_END]);
258719
258720 // Only shift within the frame if we are using a shared ordering clause.
258721 if (glstate.use_framing) {
258722 partition_begin = frame_begin;
258723 partition_end = frame_end;
258724 }
258725
258726 // We can't shift if we are ignoring NULLs (the rows may not be contiguous)
258727 // or if we are using framing (the frame may change on each row)
258728 auto &ignore_nulls = glstate.ignore_nulls;
258729 bool can_shift = ignore_nulls->AllValid() && !glstate.use_framing;
258730 if (wexpr.offset_expr) {
258731 can_shift = can_shift && wexpr.offset_expr->IsFoldable();
258732 }
258733 if (wexpr.default_expr) {
258734 can_shift = can_shift && wexpr.default_expr->IsFoldable();
258735 }
258736
258737 const auto row_end = row_idx + count;
258738 for (idx_t i = 0; i < count;) {
258739 int64_t offset = 1;
258740 if (wexpr.offset_expr) {
258741 if (leadlag_offset.CellIsNull(i)) {
258742 FlatVector::SetNull(result, i, true);
258743 ++i;
258744 ++row_idx;
258745 continue;
258746 }
258747 offset = leadlag_offset.GetCell<int64_t>(i);
258748 }
258749 int64_t val_idx = (int64_t)row_idx;
258750 if (wexpr.GetExpressionType() == ExpressionType::WINDOW_LEAD) {
258751 val_idx = AddOperatorOverflowCheck::Operation<int64_t, int64_t, int64_t>(val_idx, offset);
258752 } else {
258753 val_idx = SubtractOperatorOverflowCheck::Operation<int64_t, int64_t, int64_t>(val_idx, offset);
258754 }
258755
258756 idx_t delta = 0;
258757 if (val_idx < (int64_t)row_idx) {
258758 // Count backwards
258759 delta = idx_t(row_idx - idx_t(val_idx));
258760 val_idx = int64_t(WindowBoundariesState::FindPrevStart(*ignore_nulls, partition_begin[i], row_idx, delta));
258761 } else if (val_idx > (int64_t)row_idx) {
258762 delta = idx_t(idx_t(val_idx) - row_idx);
258763 val_idx =
258764 int64_t(WindowBoundariesState::FindNextStart(*ignore_nulls, row_idx + 1, partition_end[i], delta));
258765 }
258766 // else offset is zero, so don't move.
258767
258768 if (can_shift) {
258769 const auto target_limit = MinValue(partition_end[i], row_end) - row_idx;
258770 if (!delta) {
258771 // Copy source[index:index+width] => result[i:]
258772 auto index = NumericCast<idx_t>(val_idx);
258773 const auto source_limit = partition_end[i] - index;
258774 auto width = MinValue(source_limit, target_limit);
258775 // We may have to scan multiple blocks here, so loop until we have copied everything
258776 const idx_t col_idx = 0;
258777 while (width) {
258778 const auto source_offset = cursor.Seek(index);
258779 auto &source = cursor.chunk.data[col_idx];
258780 const auto copied = MinValue<idx_t>(cursor.chunk.size() - source_offset, width);
258781 VectorOperations::Copy(source, result, source_offset + copied, source_offset, i);
258782 i += copied;
258783 row_idx += copied;
258784 index += copied;
258785 width -= copied;
258786 }
258787 } else if (wexpr.default_expr) {
258788 const auto width = MinValue(delta, target_limit);
258789 leadlag_default.CopyCell(result, i, width);
258790 i += width;
258791 row_idx += width;
258792 } else {
258793 for (idx_t nulls = MinValue(delta, target_limit); nulls--; ++i, ++row_idx) {
258794 FlatVector::SetNull(result, i, true);
258795 }
258796 }
258797 } else {
258798 if (!delta) {
258799 cursor.CopyCell(0, NumericCast<idx_t>(val_idx), result, i);
258800 } else if (wexpr.default_expr) {
258801 leadlag_default.CopyCell(result, i);
258802 } else {
258803 FlatVector::SetNull(result, i, true);
258804 }
258805 ++i;
258806 ++row_idx;
258807 }
258808 }
258809}
column_t default_idx
The column index of the default value column.
Definition duckdb.cpp:164352
column_t offset_idx
The column index of the offset column.
Definition duckdb.cpp:164350
index
::int64_t int64_t
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237

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