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::StreamingWindowState::LeadLagState Struct Reference
Collaboration diagram for duckdb::StreamingWindowState::LeadLagState:

Public Member Functions

 LeadLagState (ClientContext &context, BoundWindowExpression &wexpr)
void Execute (ExecutionContext &context, DataChunk &input, DataChunk &delayed, Vector &result)
void ExecuteLag (ExecutionContext &context, DataChunk &input, Vector &result)
void ExecuteLead (ExecutionContext &context, DataChunk &input, DataChunk &delayed, Vector &result)

Static Public Member Functions

static bool ComputeOffset (ClientContext &context, BoundWindowExpression &wexpr, int64_t &offset)
static bool ComputeDefault (ClientContext &context, BoundWindowExpression &wexpr, Value &result)

Public Attributes

BoundWindowExpression & wexpr
 The aggregate expression.
ExpressionExecutor executor
 Cache the executor to cut down on memory allocation.
int64_t offset
 The constant offset.
idx_t buffered
 The number of rows we have buffered.
Value dflt
 The constant default value.
DataChunk curr_chunk
 The current set of values.
Vector prev
 The previous set of values.
Vector temp
 The copy buffer.

Static Public Attributes

static constexpr int64_t MAX_BUFFER = 2048

Constructor & Destructor Documentation

◆ LeadLagState()

duckdb::StreamingWindowState::LeadLagState::LeadLagState ( ClientContext & context,
BoundWindowExpression & wexpr )
inline
161889 : wexpr(wexpr), executor(context, *wexpr.children[0]), prev(wexpr.return_type), temp(wexpr.return_type) {
161890 ComputeOffset(context, wexpr, offset);
161891 ComputeDefault(context, wexpr, dflt);
161892
161893 buffered = idx_t(std::abs(offset));
161894 prev.Reference(dflt);
161895 prev.Flatten(buffered);
161896 temp.Initialize(false, buffered);
161897 }
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237
Vector prev
The previous set of values.
Definition duckdb.cpp:161991
BoundWindowExpression & wexpr
The aggregate expression.
Definition duckdb.cpp:161979
Vector temp
The copy buffer.
Definition duckdb.cpp:161993
int64_t offset
The constant offset.
Definition duckdb.cpp:161983
idx_t buffered
The number of rows we have buffered.
Definition duckdb.cpp:161985
Value dflt
The constant default value.
Definition duckdb.cpp:161987
ExpressionExecutor executor
Cache the executor to cut down on memory allocation.
Definition duckdb.cpp:161981

Member Function Documentation

◆ ComputeOffset()

bool duckdb::StreamingWindowState::LeadLagState::ComputeOffset ( ClientContext & context,
BoundWindowExpression & wexpr,
int64_t & offset )
inlinestatic
161851 {
161852 offset = 1;
161853 if (wexpr.offset_expr) {
161854 if (wexpr.offset_expr->HasParameter() || !wexpr.offset_expr->IsFoldable()) {
161855 return false;
161856 }
161857 auto offset_value = ExpressionExecutor::EvaluateScalar(context, *wexpr.offset_expr);
161858 if (offset_value.IsNull()) {
161859 return false;
161860 }
161861 Value bigint_value;
161862 if (!offset_value.DefaultTryCastAs(LogicalType::BIGINT, bigint_value, nullptr, false)) {
161863 return false;
161864 }
161865 offset = bigint_value.GetValue<int64_t>();
161866 }
161867
161868 // We can only support LEAD and LAG values within one standard vector
161869 if (wexpr.GetExpressionType() == ExpressionType::WINDOW_LEAD) {
161870 offset = -offset;
161871 }
161872 return std::abs(offset) < MAX_BUFFER;
161873 }
static DUCKDB_API Value EvaluateScalar(ClientContext &context, const Expression &expr, bool allow_unfoldable=false)
Evaluate a scalar expression and fold it into a single value.
Definition duckdb.cpp:149993
::int64_t int64_t

◆ ComputeDefault()

bool duckdb::StreamingWindowState::LeadLagState::ComputeDefault ( ClientContext & context,
BoundWindowExpression & wexpr,
Value & result )
inlinestatic
161875 {
161876 if (!wexpr.default_expr) {
161877 result = Value(wexpr.return_type);
161878 return true;
161879 }
161880
161881 if (wexpr.default_expr && (wexpr.default_expr->HasParameter() || !wexpr.default_expr->IsFoldable())) {
161882 return false;
161883 }
161884 auto dflt_value = ExpressionExecutor::EvaluateScalar(context, *wexpr.default_expr);
161885 return dflt_value.DefaultTryCastAs(wexpr.return_type, result, nullptr, false);
161886 }

◆ Execute()

void duckdb::StreamingWindowState::LeadLagState::Execute ( ExecutionContext & context,
DataChunk & input,
DataChunk & delayed,
Vector & result )
inline
161899 {
161900 if (!curr_chunk.ColumnCount()) {
161901 curr_chunk.Initialize(context.client, {result.GetType()}, delayed.GetCapacity());
161902 }
161903
161904 if (offset >= 0) {
161905 ExecuteLag(context, input, result);
161906 } else {
161907 ExecuteLead(context, input, delayed, result);
161908 }
161909 }
DataChunk delayed
A buffer for delayed input.
Definition duckdb.cpp:162080
DataChunk curr_chunk
The current set of values.
Definition duckdb.cpp:161989

◆ ExecuteLag()

void duckdb::StreamingWindowState::LeadLagState::ExecuteLag ( ExecutionContext & context,
DataChunk & input,
Vector & result )
inline
161911 {
161912 D_ASSERT(offset >= 0);
161913 auto &curr = curr_chunk.data[0];
161914 curr_chunk.Reset();
161915 executor.Execute(input, curr_chunk);
161916 const idx_t count = input.size();
161917 // Copy prev[0, buffered] => result[0, buffered]
161918 idx_t source_count = MinValue<idx_t>(buffered, count);
161919 VectorOperations::Copy(prev, result, source_count, 0, 0);
161920 // Special case when we have buffered enough values for the output
161921 if (count < buffered) {
161922 // Shift down incomplete buffers
161923 // Copy prev[count, buffered] => temp[0, buffered-count]
161924 source_count = buffered - count;
161925 FlatVector::Validity(temp).Reset();
161926 VectorOperations::Copy(prev, temp, buffered, count, 0);
161927
161928 // Copy temp[0, buffered-count] => prev[0, buffered-count]
161929 FlatVector::Validity(prev).Reset();
161930 VectorOperations::Copy(temp, prev, source_count, 0, 0);
161931 // Copy curr[0, count] => prev[buffered-count, buffered]
161932 VectorOperations::Copy(curr, prev, count, 0, source_count);
161933 } else {
161934 // Copy input values beyond what we have buffered
161935 source_count = count - buffered;
161936 // Copy curr[0, count-buffered] => result[buffered, count]
161937 VectorOperations::Copy(curr, result, source_count, 0, buffered);
161938 // Copy curr[count-buffered, count] => prev[0, buffered]
161939 FlatVector::Validity(prev).Reset();
161940 VectorOperations::Copy(curr, prev, count, source_count, 0);
161941 }
161942 }

◆ ExecuteLead()

void duckdb::StreamingWindowState::LeadLagState::ExecuteLead ( ExecutionContext & context,
DataChunk & input,
DataChunk & delayed,
Vector & result )
inline
161944 {
161945 // We treat input || delayed as a logical unified buffer
161946 D_ASSERT(offset < 0);
161947 // Input has been set up with the number of rows we CAN produce.
161948 const idx_t count = input.size();
161949 auto &curr = curr_chunk.data[0];
161950 // Copy unified[buffered:count] => result[pos:]
161951 idx_t pos = 0;
161952 idx_t unified_offset = buffered;
161953 if (unified_offset < count) {
161954 Reset(curr_chunk);
161955 executor.Execute(input, curr_chunk);
161956 VectorOperations::Copy(curr, result, count, unified_offset, pos);
161957 pos += count - unified_offset;
161958 unified_offset = count;
161959 }
161960 // Copy unified[unified_offset:] => result[pos:]
161961 idx_t unified_count = count + delayed.size();
161962 if (unified_offset < unified_count) {
161963 Reset(curr_chunk);
161964 executor.Execute(delayed, curr_chunk);
161965 idx_t delayed_offset = unified_offset - count;
161966 // Only copy as many values as we need
161967 idx_t delayed_count = MinValue<idx_t>(delayed.size(), delayed_offset + (count - pos));
161968 VectorOperations::Copy(curr, result, delayed_count, delayed_offset, pos);
161969 pos += delayed_count - delayed_offset;
161970 }
161971 // Copy default[:count-pos] => result[pos:]
161972 if (pos < count) {
161973 const idx_t defaulted = count - pos;
161974 VectorOperations::Copy(prev, result, defaulted, 0, pos);
161975 }
161976 }

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