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

Public Member Functions

 WindowBoundariesState (const BoundWindowExpression &wexpr, const idx_t input_size)
void PartitionBegin (DataChunk &bounds, idx_t row_idx, const idx_t count, bool is_jump, const ValidityMask &partition_mask)
void PartitionEnd (DataChunk &bounds, idx_t row_idx, const idx_t count, bool is_jump, const ValidityMask &partition_mask)
void PeerBegin (DataChunk &bounds, idx_t row_idx, const idx_t count, bool is_jump, const ValidityMask &partition_mask, const ValidityMask &order_mask)
void PeerEnd (DataChunk &bounds, idx_t row_idx, const idx_t count, const ValidityMask &partition_mask, const ValidityMask &order_mask)
void ValidBegin (DataChunk &bounds, idx_t row_idx, const idx_t count, bool is_jump, const ValidityMask &partition_mask, const ValidityMask &order_mask, optional_ptr< WindowCursor > range)
void ValidEnd (DataChunk &bounds, idx_t row_idx, const idx_t count, bool is_jump, const ValidityMask &partition_mask, const ValidityMask &order_mask, optional_ptr< WindowCursor > range)
void FrameBegin (DataChunk &bounds, idx_t row_idx, const idx_t count, WindowInputExpression &boundary_begin, const ValidityMask &order_mask, optional_ptr< WindowCursor > range)
void FrameEnd (DataChunk &bounds, idx_t row_idx, const idx_t count, WindowInputExpression &boundary_end, const ValidityMask &order_mask, optional_ptr< WindowCursor > range)
void Bounds (DataChunk &bounds, idx_t row_idx, optional_ptr< WindowCursor > range, const idx_t count, WindowInputExpression &boundary_start, WindowInputExpression &boundary_end, const ValidityMask &partition_mask, const ValidityMask &order_mask)

Static Public Member Functions

static bool HasPrecedingRange (const BoundWindowExpression &wexpr)
static bool HasFollowingRange (const BoundWindowExpression &wexpr)
static WindowBoundsSet GetWindowBounds (const BoundWindowExpression &wexpr)
static idx_t FindNextStart (const ValidityMask &mask, idx_t l, const idx_t r, idx_t &n)
static idx_t FindPrevStart (const ValidityMask &mask, const idx_t l, idx_t r, idx_t &n)
static void ClampFrame (const idx_t count, idx_t *values, const idx_t *begin, const idx_t *end)

Public Attributes

WindowBoundsSet required
const ExpressionType type
const idx_t input_size
const WindowBoundary start_boundary
const WindowBoundary end_boundary
const size_t partition_count
const size_t order_count
const OrderType range_sense
const bool has_preceding_range
const bool has_following_range
idx_t next_pos = 0
idx_t partition_start = 0
idx_t partition_end = 0
idx_t peer_start = 0
idx_t valid_start = 0
idx_t valid_end = 0
FrameBounds prev
optional_ptr< WindowCursor > range_lo
unique_ptr< WindowCursor > range_hi

Constructor & Destructor Documentation

◆ WindowBoundariesState()

duckdb::WindowBoundariesState::WindowBoundariesState ( const BoundWindowExpression & wexpr,
const idx_t input_size )
254165 : required(GetWindowBounds(wexpr)), type(wexpr.GetExpressionType()), input_size(input_size),
254166 start_boundary(wexpr.start), end_boundary(wexpr.end), partition_count(wexpr.partitions.size()),
254167 order_count(wexpr.orders.size()), range_sense(wexpr.orders.empty() ? OrderType::INVALID : wexpr.orders[0].type),
254168 has_preceding_range(HasPrecedingRange(wexpr)), has_following_range(HasFollowingRange(wexpr)) {
254169}

Member Function Documentation

◆ HasPrecedingRange()

bool duckdb::WindowBoundariesState::HasPrecedingRange ( const BoundWindowExpression & wexpr)
static
253964 {
253965 return (wexpr.start == WindowBoundary::EXPR_PRECEDING_RANGE || wexpr.end == WindowBoundary::EXPR_PRECEDING_RANGE);
253966}

◆ HasFollowingRange()

bool duckdb::WindowBoundariesState::HasFollowingRange ( const BoundWindowExpression & wexpr)
static
253968 {
253969 return (wexpr.start == WindowBoundary::EXPR_FOLLOWING_RANGE || wexpr.end == WindowBoundary::EXPR_FOLLOWING_RANGE);
253970}

◆ GetWindowBounds()

WindowBoundsSet duckdb::WindowBoundariesState::GetWindowBounds ( const BoundWindowExpression & wexpr)
static
253972 {
253973 const auto partition_count = wexpr.partitions.size();
253974 const auto order_count = wexpr.orders.size();
253975
253976 WindowBoundsSet result;
253977 switch (wexpr.GetExpressionType()) {
253978 case ExpressionType::WINDOW_ROW_NUMBER:
253979 if (wexpr.arg_orders.empty()) {
253980 result.insert(PARTITION_BEGIN);
253981 } else {
253982 // Secondary orders need to know where the frame is
253983 result.insert(FRAME_BEGIN);
253984 result.insert(FRAME_END);
253985 }
253986 break;
253987 case ExpressionType::WINDOW_NTILE:
253988 if (wexpr.arg_orders.empty()) {
253989 result.insert(PARTITION_BEGIN);
253990 result.insert(PARTITION_END);
253991 } else {
253992 // Secondary orders need to know where the frame is
253993 result.insert(FRAME_BEGIN);
253994 result.insert(FRAME_END);
253995 }
253996 break;
253997 case ExpressionType::WINDOW_RANK:
253998 if (wexpr.arg_orders.empty()) {
253999 result.insert(PARTITION_BEGIN);
254000 result.insert(PEER_BEGIN);
254001 } else {
254002 // Secondary orders need to know where the frame is
254003 result.insert(FRAME_BEGIN);
254004 result.insert(FRAME_END);
254005 result.insert(PEER_BEGIN);
254006 }
254007 break;
254008 case ExpressionType::WINDOW_RANK_DENSE:
254009 result.insert(PARTITION_BEGIN);
254010 result.insert(PEER_BEGIN);
254011 break;
254012 case ExpressionType::WINDOW_PERCENT_RANK:
254013 if (wexpr.arg_orders.empty()) {
254014 result.insert(PARTITION_BEGIN);
254015 result.insert(PARTITION_END);
254016 result.insert(PEER_BEGIN);
254017 } else {
254018 // Secondary orders need to know where the frame is
254019 result.insert(FRAME_BEGIN);
254020 result.insert(FRAME_END);
254021 result.insert(PEER_BEGIN);
254022 }
254023 break;
254024 case ExpressionType::WINDOW_CUME_DIST:
254025 if (wexpr.arg_orders.empty()) {
254026 result.insert(PARTITION_BEGIN);
254027 result.insert(PARTITION_END);
254028 result.insert(PEER_END);
254029 } else {
254030 // Secondary orders need to know where the frame is
254031 result.insert(FRAME_BEGIN);
254032 result.insert(FRAME_END);
254033 result.insert(PEER_END);
254034 }
254035 break;
254036 case ExpressionType::WINDOW_LEAD:
254037 case ExpressionType::WINDOW_LAG:
254038 if (wexpr.arg_orders.empty()) {
254039 result.insert(PARTITION_BEGIN);
254040 result.insert(PARTITION_END);
254041 } else {
254042 // Secondary orders need to know where the frame is
254043 result.insert(FRAME_BEGIN);
254044 result.insert(FRAME_END);
254045 }
254046 break;
254047 case ExpressionType::WINDOW_FILL:
254048 result.insert(FRAME_BEGIN);
254049 result.insert(FRAME_END);
254050 if (wexpr.arg_orders.empty()) {
254051 // FILL uses the validity ranges to quickly eliminate indexes that can't be interpolated.
254052 // This only works for non-secondary orderings
254053 result.insert(VALID_BEGIN);
254054 result.insert(VALID_END);
254055 }
254056 break;
254057 case ExpressionType::WINDOW_FIRST_VALUE:
254058 case ExpressionType::WINDOW_LAST_VALUE:
254059 case ExpressionType::WINDOW_NTH_VALUE:
254060 case ExpressionType::WINDOW_AGGREGATE:
254061 result.insert(FRAME_BEGIN);
254062 result.insert(FRAME_END);
254063 break;
254064 default:
254065 throw InternalException("Window expression type %s", ExpressionTypeToString(wexpr.GetExpressionType()));
254066 }
254067
254068 // Internal dependencies
254069 if (result.count(FRAME_BEGIN) || result.count(FRAME_END)) {
254070 result.insert(PARTITION_BEGIN);
254071 result.insert(PARTITION_END);
254072
254073 // if we have EXCLUDE GROUP / TIES, we also need peer boundaries
254074 if (wexpr.exclude_clause != WindowExcludeMode::NO_OTHER) {
254075 result.insert(PEER_BEGIN);
254076 result.insert(PEER_END);
254077 }
254078
254079 // If the frames are RANGE or GROUPS, then we need peer boundaries
254080 // If they are preceding or following, RANGE also needs to know
254081 // where the valid values begin or end.
254082 switch (wexpr.start) {
254083 case WindowBoundary::CURRENT_ROW_RANGE:
254084 case WindowBoundary::CURRENT_ROW_GROUPS:
254085 result.insert(PEER_BEGIN);
254086 break;
254087 case WindowBoundary::EXPR_PRECEDING_RANGE:
254088 result.insert(PEER_BEGIN);
254089 result.insert(VALID_BEGIN);
254090 result.insert(VALID_END);
254091 break;
254092 case WindowBoundary::EXPR_FOLLOWING_RANGE:
254093 result.insert(PEER_BEGIN);
254094 result.insert(VALID_END);
254095 break;
254096 case WindowBoundary::EXPR_PRECEDING_GROUPS:
254097 result.insert(PEER_BEGIN);
254098 break;
254099 case WindowBoundary::EXPR_FOLLOWING_GROUPS:
254100 result.insert(PEER_BEGIN);
254101 break;
254102 case WindowBoundary::UNBOUNDED_PRECEDING:
254103 case WindowBoundary::UNBOUNDED_FOLLOWING:
254104 case WindowBoundary::CURRENT_ROW_ROWS:
254105 case WindowBoundary::EXPR_PRECEDING_ROWS:
254106 case WindowBoundary::EXPR_FOLLOWING_ROWS:
254107 case WindowBoundary::INVALID:
254108 break;
254109 }
254110
254111 switch (wexpr.end) {
254112 case WindowBoundary::CURRENT_ROW_RANGE:
254113 case WindowBoundary::CURRENT_ROW_GROUPS:
254114 result.insert(PEER_END);
254115 break;
254116 case WindowBoundary::EXPR_PRECEDING_RANGE:
254117 result.insert(PEER_END);
254118 result.insert(VALID_BEGIN);
254119 break;
254120 case WindowBoundary::EXPR_FOLLOWING_RANGE:
254121 result.insert(PEER_END);
254122 result.insert(VALID_BEGIN);
254123 result.insert(VALID_END);
254124 break;
254125 case WindowBoundary::EXPR_PRECEDING_GROUPS:
254126 result.insert(PEER_END);
254127 break;
254128 case WindowBoundary::EXPR_FOLLOWING_GROUPS:
254129 result.insert(PEER_END);
254130 break;
254131 case WindowBoundary::UNBOUNDED_PRECEDING:
254132 case WindowBoundary::UNBOUNDED_FOLLOWING:
254133 case WindowBoundary::CURRENT_ROW_ROWS:
254134 case WindowBoundary::EXPR_PRECEDING_ROWS:
254135 case WindowBoundary::EXPR_FOLLOWING_ROWS:
254136 case WindowBoundary::INVALID:
254137 break;
254138 }
254139 }
254140
254141 if (result.count(VALID_END)) {
254142 result.insert(PARTITION_END);
254143 if (HasFollowingRange(wexpr)) {
254144 result.insert(VALID_BEGIN);
254145 }
254146 }
254147 if (result.count(VALID_BEGIN)) {
254148 result.insert(PARTITION_BEGIN);
254149 result.insert(PARTITION_END);
254150 }
254151 if (result.count(PEER_END)) {
254152 result.insert(PARTITION_END);
254153 if (order_count) {
254154 result.insert(PEER_BEGIN);
254155 }
254156 }
254157 if (result.count(PARTITION_END) && (partition_count + order_count)) {
254158 result.insert(PARTITION_BEGIN);
254159 }
254160
254161 return result;
254162}

◆ FindNextStart()

idx_t duckdb::WindowBoundariesState::FindNextStart ( const ValidityMask & mask,
idx_t l,
const idx_t r,
idx_t & n )
static
253667 {
253668 if (mask.AllValid()) {
253669 auto start = MinValue(l + n - 1, r);
253670 n -= MinValue(n, r - l);
253671 return start;
253672 }
253673
253674 while (l < r) {
253675 // If l is aligned with the start of a block, and the block is blank, then skip forward one block.
253676 idx_t entry_idx;
253677 idx_t shift;
253678 mask.GetEntryIndex(l, entry_idx, shift);
253679
253680 const auto block = mask.GetValidityEntry(entry_idx);
253681 if (mask.NoneValid(block) && !shift) {
253682 l += ValidityMask::BITS_PER_VALUE;
253683 continue;
253684 }
253685
253686 // Loop over the block
253687 for (; shift < ValidityMask::BITS_PER_VALUE && l < r; ++shift, ++l) {
253688 if (mask.RowIsValid(block, shift) && --n == 0) {
253689 return MinValue(l, r);
253690 }
253691 }
253692 }
253693
253694 // Didn't find a start so return the end of the range
253695 return r;
253696}
GMat mask(const GMat &src, const GMat &mask)
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237

◆ FindPrevStart()

idx_t duckdb::WindowBoundariesState::FindPrevStart ( const ValidityMask & mask,
const idx_t l,
idx_t r,
idx_t & n )
static
253698 {
253699 if (mask.AllValid()) {
253700 auto start = (r <= l + n) ? l : r - n;
253701 n -= r - start;
253702 return start;
253703 }
253704
253705 while (l < r) {
253706 // If r is aligned with the start of a block, and the previous block is blank,
253707 // then skip backwards one block.
253708 idx_t entry_idx;
253709 idx_t shift;
253710 mask.GetEntryIndex(r - 1, entry_idx, shift);
253711
253712 const auto block = mask.GetValidityEntry(entry_idx);
253713 if (mask.NoneValid(block) && (shift + 1 == ValidityMask::BITS_PER_VALUE)) {
253714 // r is nonzero (> l) and word aligned, so this will not underflow.
253715 r -= ValidityMask::BITS_PER_VALUE;
253716 continue;
253717 }
253718
253719 // Loop backwards over the block
253720 // shift is probing r-1 >= l >= 0
253721 for (++shift; shift-- > 0 && l < r; --r) {
253722 // l < r ensures n == 1 if result is supposed to be NULL because of EXCLUDE
253723 if (mask.RowIsValid(block, shift) && --n == 0) {
253724 return MaxValue(l, r - 1);
253725 }
253726 }
253727 }
253728
253729 // Didn't find a start so return the start of the range
253730 return l;
253731}

◆ PartitionBegin()

void duckdb::WindowBoundariesState::PartitionBegin ( DataChunk & bounds,
idx_t row_idx,
const idx_t count,
bool is_jump,
const ValidityMask & partition_mask )
254214 {
254215 auto partition_begin_data = FlatVector::GetData<idx_t>(bounds.data[PARTITION_BEGIN]);
254216
254217 // OVER()
254218 if (partition_count + order_count == 0) {
254219 for (idx_t chunk_idx = 0; chunk_idx < count; ++chunk_idx, ++row_idx) {
254220 partition_begin_data[chunk_idx] = 0;
254221 }
254222 return;
254223 }
254224
254225 for (idx_t chunk_idx = 0; chunk_idx < count; ++chunk_idx, ++row_idx) {
254226 // determine partition and peer group boundaries to ultimately figure out window size
254227 const auto is_same_partition = !partition_mask.RowIsValidUnsafe(row_idx);
254228
254229 // when the partition changes, recompute the boundaries
254230 if (!is_same_partition || is_jump) {
254231 if (is_jump) {
254232 idx_t n = 1;
254233 partition_start = FindPrevStart(partition_mask, 0, row_idx + 1, n);
254234 is_jump = false;
254235 } else {
254236 partition_start = row_idx;
254237 }
254238 }
254239
254240 partition_begin_data[chunk_idx] = partition_start;
254241 }
254242}
vector< Vector > data
The vectors owned by the DataChunk.
Definition duckdb.hpp:11445

◆ PartitionEnd()

void duckdb::WindowBoundariesState::PartitionEnd ( DataChunk & bounds,
idx_t row_idx,
const idx_t count,
bool is_jump,
const ValidityMask & partition_mask )
254245 {
254246 auto partition_end_data = FlatVector::GetData<idx_t>(bounds.data[PARTITION_END]);
254247
254248 // OVER()
254249 if (partition_count + order_count == 0) {
254250 for (idx_t chunk_idx = 0; chunk_idx < count; ++chunk_idx, ++row_idx) {
254251 partition_end_data[chunk_idx] = input_size;
254252 }
254253 return;
254254 }
254255
254256 auto partition_begin_data = FlatVector::GetData<const idx_t>(bounds.data[PARTITION_BEGIN]);
254257 for (idx_t chunk_idx = 0; chunk_idx < count; ++chunk_idx, ++row_idx) {
254258 // determine partition and peer group boundaries to ultimately figure out window size
254259 const auto is_same_partition = !partition_mask.RowIsValidUnsafe(row_idx);
254260
254261 // when the partition changes, recompute the boundaries
254262 if (!is_same_partition || is_jump) {
254263 // find end of partition
254264 partition_end = input_size;
254265 if (partition_count) {
254266 const auto partition_begin = partition_begin_data[chunk_idx];
254267 idx_t n = 1;
254268 partition_end = FindNextStart(partition_mask, partition_begin + 1, input_size, n);
254269 }
254270 is_jump = false;
254271 }
254272
254273 partition_end_data[chunk_idx] = partition_end;
254274 }
254275}

◆ PeerBegin()

void duckdb::WindowBoundariesState::PeerBegin ( DataChunk & bounds,
idx_t row_idx,
const idx_t count,
bool is_jump,
const ValidityMask & partition_mask,
const ValidityMask & order_mask )
254278 {
254279 auto peer_begin_data = FlatVector::GetData<idx_t>(bounds.data[PEER_BEGIN]);
254280
254281 // OVER()
254282 if (partition_count + order_count == 0) {
254283 for (idx_t chunk_idx = 0; chunk_idx < count; ++chunk_idx, ++row_idx) {
254284 peer_begin_data[chunk_idx] = 0;
254285 }
254286 return;
254287 }
254288
254289 for (idx_t chunk_idx = 0; chunk_idx < count; ++chunk_idx, ++row_idx) {
254290 // determine partition and peer group boundaries to ultimately figure out window size
254291 const auto is_same_partition = !partition_mask.RowIsValidUnsafe(row_idx);
254292 const auto is_peer = !order_mask.RowIsValidUnsafe(row_idx);
254293
254294 // when the partition changes, recompute the boundaries
254295 if (!is_same_partition || is_jump) {
254296 // find end of partition
254297 if (is_jump) {
254298 idx_t n = 1;
254299 peer_start = FindPrevStart(order_mask, 0, row_idx + 1, n);
254300 } else {
254301 peer_start = row_idx;
254302 }
254303 is_jump = false;
254304 } else if (!is_peer) {
254305 peer_start = row_idx;
254306 }
254307
254308 peer_begin_data[chunk_idx] = peer_start;
254309 }
254310}

◆ PeerEnd()

void duckdb::WindowBoundariesState::PeerEnd ( DataChunk & bounds,
idx_t row_idx,
const idx_t count,
const ValidityMask & partition_mask,
const ValidityMask & order_mask )
254313 {
254314 // OVER()
254315 if (!order_count) {
254316 bounds.data[PEER_END].Reference(bounds.data[PARTITION_END]);
254317 return;
254318 }
254319
254320 auto partition_end_data = FlatVector::GetData<const idx_t>(bounds.data[PARTITION_END]);
254321 auto peer_begin_data = FlatVector::GetData<const idx_t>(bounds.data[PEER_BEGIN]);
254322 auto peer_end_data = FlatVector::GetData<idx_t>(bounds.data[PEER_END]);
254323 auto prev_end = peer_begin_data[0];
254324 for (idx_t chunk_idx = 0; chunk_idx < count; ++chunk_idx, ++row_idx) {
254325 const auto peer_start = peer_begin_data[chunk_idx];
254326 if (peer_start >= prev_end) {
254327 idx_t n = 1;
254328 const auto partition_end = partition_end_data[chunk_idx];
254329 prev_end = FindNextStart(order_mask, peer_start + 1, partition_end, n);
254330 }
254331 peer_end_data[chunk_idx] = prev_end;
254332 }
254333}

◆ ValidBegin()

void duckdb::WindowBoundariesState::ValidBegin ( DataChunk & bounds,
idx_t row_idx,
const idx_t count,
bool is_jump,
const ValidityMask & partition_mask,
const ValidityMask & order_mask,
optional_ptr< WindowCursor > range )
254337 {
254338 auto partition_begin_data = FlatVector::GetData<const idx_t>(bounds.data[PARTITION_BEGIN]);
254339 auto partition_end_data = FlatVector::GetData<const idx_t>(bounds.data[PARTITION_END]);
254340 auto valid_begin_data = FlatVector::GetData<idx_t>(bounds.data[VALID_BEGIN]);
254341
254342 // OVER()
254343 D_ASSERT(partition_count + order_count != 0);
254344 D_ASSERT(range);
254345
254346 for (idx_t chunk_idx = 0; chunk_idx < count; ++chunk_idx, ++row_idx) {
254347 const auto is_same_partition = !partition_mask.RowIsValidUnsafe(row_idx);
254348
254349 if (!is_same_partition || is_jump) {
254350 // Find valid ordering values for the new partition
254351 // so we can exclude NULLs from RANGE expression computations
254352 valid_start = partition_begin_data[chunk_idx];
254353 const auto valid_end = partition_end_data[chunk_idx];
254354
254355 if (valid_start < valid_end) {
254356 // Exclude any leading NULLs
254357 if (range->CellIsNull(0, valid_start)) {
254358 idx_t n = 1;
254359 valid_start = FindNextStart(order_mask, valid_start + 1, valid_end, n);
254360 }
254361 }
254362 }
254363
254364 valid_begin_data[chunk_idx] = valid_start;
254365 }
254366}

◆ ValidEnd()

void duckdb::WindowBoundariesState::ValidEnd ( DataChunk & bounds,
idx_t row_idx,
const idx_t count,
bool is_jump,
const ValidityMask & partition_mask,
const ValidityMask & order_mask,
optional_ptr< WindowCursor > range )
254370 {
254371 auto partition_end_data = FlatVector::GetData<const idx_t>(bounds.data[PARTITION_END]);
254372 auto valid_begin_data = FlatVector::GetData<const idx_t>(bounds.data[VALID_BEGIN]);
254373 auto valid_end_data = FlatVector::GetData<idx_t>(bounds.data[VALID_END]);
254374
254375 // OVER()
254376 D_ASSERT(partition_count + order_count != 0);
254377 D_ASSERT(range);
254378
254379 for (idx_t chunk_idx = 0; chunk_idx < count; ++chunk_idx, ++row_idx) {
254380 const auto is_same_partition = !partition_mask.RowIsValidUnsafe(row_idx);
254381
254382 if (!is_same_partition || is_jump) {
254383 // Find valid ordering values for the new partition
254384 // so we can exclude NULLs from RANGE expression computations
254385 const auto valid_start = valid_begin_data[chunk_idx];
254386 valid_end = partition_end_data[chunk_idx];
254387
254388 if (valid_start < valid_end) {
254389 // Exclude any trailing NULLs
254390 if (range->CellIsNull(0, valid_end - 1)) {
254391 idx_t n = 1;
254392 valid_end = FindPrevStart(order_mask, valid_start, valid_end, n);
254393 }
254394 }
254395 }
254396
254397 valid_end_data[chunk_idx] = valid_end;
254398 }
254399}

◆ FrameBegin()

void duckdb::WindowBoundariesState::FrameBegin ( DataChunk & bounds,
idx_t row_idx,
const idx_t count,
WindowInputExpression & boundary_begin,
const ValidityMask & order_mask,
optional_ptr< WindowCursor > range )
254403 {
254404 auto partition_begin_data = FlatVector::GetData<idx_t>(bounds.data[PARTITION_BEGIN]);
254405 auto partition_end_data = FlatVector::GetData<const idx_t>(bounds.data[PARTITION_END]);
254406 auto peer_begin_data = FlatVector::GetData<idx_t>(bounds.data[PEER_BEGIN]);
254407 auto valid_begin_data = FlatVector::GetData<const idx_t>(bounds.data[VALID_BEGIN]);
254408 auto valid_end_data = FlatVector::GetData<const idx_t>(bounds.data[VALID_END]);
254409 auto frame_begin_data = FlatVector::GetData<idx_t>(bounds.data[FRAME_BEGIN]);
254410
254411 idx_t window_start = NumericLimits<idx_t>::Maximum();
254412
254413 // Reset previous range hints
254414 idx_t prev_partition = partition_begin_data[0];
254415 prev.start = valid_begin_data[0];
254416 prev.end = valid_end_data[0];
254417
254418 if (has_preceding_range || has_following_range) {
254419 if (range_lo.get() != range.get()) {
254420 range_lo = range.get();
254421 range_hi = range_lo->Copy();
254422 }
254423 }
254424
254425 switch (start_boundary) {
254426 case WindowBoundary::UNBOUNDED_PRECEDING:
254427 bounds.data[FRAME_BEGIN].Reference(bounds.data[PARTITION_BEGIN]);
254428 // No need to clamp
254429 return;
254430 case WindowBoundary::CURRENT_ROW_ROWS:
254431 for (idx_t chunk_idx = 0; chunk_idx < count; ++chunk_idx, ++row_idx) {
254432 frame_begin_data[chunk_idx] = row_idx;
254433 }
254434 break;
254435 case WindowBoundary::CURRENT_ROW_RANGE:
254436 case WindowBoundary::CURRENT_ROW_GROUPS:
254437 // in RANGE or GROUPS mode it means that the frame starts or ends with the current row's
254438 // first or last peer in the ORDER BY ordering
254439 bounds.data[FRAME_BEGIN].Reference(bounds.data[PEER_BEGIN]);
254440 frame_begin_data = peer_begin_data;
254441 break;
254442 case WindowBoundary::EXPR_PRECEDING_ROWS:
254443 for (idx_t chunk_idx = 0; chunk_idx < count; ++chunk_idx, ++row_idx) {
254444 int64_t computed_start;
254445 if (!TrySubtractOperator::Operation(static_cast<int64_t>(row_idx),
254446 boundary_begin.GetCell<int64_t>(chunk_idx), computed_start)) {
254447 window_start = partition_begin_data[chunk_idx];
254448 } else {
254449 window_start = UnsafeNumericCast<idx_t>(MaxValue<int64_t>(computed_start, 0));
254450 }
254451 frame_begin_data[chunk_idx] = window_start;
254452 }
254453 break;
254454 case WindowBoundary::EXPR_FOLLOWING_ROWS:
254455 for (idx_t chunk_idx = 0; chunk_idx < count; ++chunk_idx, ++row_idx) {
254456 int64_t computed_start;
254457 if (!TryAddOperator::Operation(static_cast<int64_t>(row_idx), boundary_begin.GetCell<int64_t>(chunk_idx),
254458 computed_start)) {
254459 window_start = partition_begin_data[chunk_idx];
254460 } else {
254461 window_start = UnsafeNumericCast<idx_t>(MaxValue<int64_t>(computed_start, 0));
254462 }
254463 frame_begin_data[chunk_idx] = window_start;
254464 }
254465 break;
254466 case WindowBoundary::EXPR_PRECEDING_RANGE:
254467 for (idx_t chunk_idx = 0; chunk_idx < count; ++chunk_idx, ++row_idx) {
254468 if (boundary_begin.CellIsNull(chunk_idx)) {
254469 window_start = peer_begin_data[chunk_idx];
254470 } else {
254471 const auto valid_start = valid_begin_data[chunk_idx];
254472 prev.end = valid_end_data[chunk_idx];
254473 const auto cur_partition = partition_begin_data[chunk_idx];
254474 if (cur_partition != prev_partition) {
254475 prev.start = valid_start;
254476 prev_partition = cur_partition;
254477 }
254478 window_start = FindOrderedRangeBound<true>(*range_lo, *range_hi, range_sense, valid_start, row_idx + 1,
254479 start_boundary, boundary_begin, chunk_idx, prev);
254480 prev.start = window_start;
254481 }
254482 frame_begin_data[chunk_idx] = window_start;
254483 }
254484 break;
254485 case WindowBoundary::EXPR_FOLLOWING_RANGE:
254486 for (idx_t chunk_idx = 0; chunk_idx < count; ++chunk_idx, ++row_idx) {
254487 if (boundary_begin.CellIsNull(chunk_idx)) {
254488 window_start = peer_begin_data[chunk_idx];
254489 } else {
254490 const auto valid_end = valid_end_data[chunk_idx];
254491 prev.end = valid_end;
254492 const auto cur_partition = partition_begin_data[chunk_idx];
254493 if (cur_partition != prev_partition) {
254494 prev.start = valid_begin_data[chunk_idx];
254495 prev_partition = cur_partition;
254496 }
254497 window_start = FindOrderedRangeBound<true>(*range_lo, *range_hi, range_sense, row_idx, valid_end,
254498 start_boundary, boundary_begin, chunk_idx, prev);
254499 prev.start = window_start;
254500 }
254501 frame_begin_data[chunk_idx] = window_start;
254502 }
254503 break;
254504 case WindowBoundary::EXPR_PRECEDING_GROUPS:
254505 // In GROUPS mode, the offset is an integer indicating that the frame starts or ends that many peer groups
254506 // before or after the current row's peer group, where a peer group is a group of rows that are equivalent
254507 // according to the window's ORDER BY clause.
254508 for (idx_t chunk_idx = 0; chunk_idx < count; ++chunk_idx, ++row_idx) {
254509 if (boundary_begin.CellIsNull(chunk_idx)) {
254510 window_start = peer_begin_data[chunk_idx];
254511 } else {
254512 // Count peer groups backwards.
254513 const auto peer_begin = peer_begin_data[chunk_idx];
254514 const auto partition_begin = partition_begin_data[chunk_idx];
254515 const auto boundary = boundary_begin.GetCell<int64_t>(chunk_idx);
254516 if (boundary < 0) {
254517 throw OutOfRangeException("Invalid GROUPS PRECEDING value");
254518 } else if (!boundary) {
254519 window_start = peer_begin;
254520 } else {
254521 auto n = UnsafeNumericCast<idx_t>(boundary);
254522 window_start = FindPrevStart(order_mask, partition_begin, peer_begin, n);
254523 }
254524 }
254525 frame_begin_data[chunk_idx] = window_start;
254526 }
254527 break;
254528 case WindowBoundary::EXPR_FOLLOWING_GROUPS:
254529 for (idx_t chunk_idx = 0; chunk_idx < count; ++chunk_idx, ++row_idx) {
254530 if (boundary_begin.CellIsNull(chunk_idx)) {
254531 window_start = peer_begin_data[chunk_idx];
254532 } else {
254533 // Count peer groups forward.
254534 const auto peer_begin = peer_begin_data[chunk_idx];
254535 const auto partition_end = partition_end_data[chunk_idx];
254536 const auto boundary = boundary_begin.GetCell<int64_t>(chunk_idx);
254537 if (boundary < 0) {
254538 throw OutOfRangeException("Invalid GROUPS FOLLOWING value");
254539 } else if (!boundary) {
254540 window_start = peer_begin;
254541 } else {
254542 auto n = UnsafeNumericCast<idx_t>(boundary);
254543 window_start = FindNextStart(order_mask, peer_begin + 1, partition_end, n);
254544 }
254545 }
254546 frame_begin_data[chunk_idx] = window_start;
254547 }
254548 break;
254549 case WindowBoundary::UNBOUNDED_FOLLOWING:
254550 case WindowBoundary::INVALID:
254551 throw InternalException("Unsupported window start boundary");
254552 }
254553
254554 ClampFrame(count, frame_begin_data, partition_begin_data, partition_end_data);
254555}
::int64_t int64_t

◆ FrameEnd()

void duckdb::WindowBoundariesState::FrameEnd ( DataChunk & bounds,
idx_t row_idx,
const idx_t count,
WindowInputExpression & boundary_end,
const ValidityMask & order_mask,
optional_ptr< WindowCursor > range )
254559 {
254560 auto partition_begin_data = FlatVector::GetData<const idx_t>(bounds.data[PARTITION_BEGIN]);
254561 auto partition_end_data = FlatVector::GetData<idx_t>(bounds.data[PARTITION_END]);
254562 auto peer_end_data = FlatVector::GetData<idx_t>(bounds.data[PEER_END]);
254563 auto valid_begin_data = FlatVector::GetData<const idx_t>(bounds.data[VALID_BEGIN]);
254564 auto valid_end_data = FlatVector::GetData<const idx_t>(bounds.data[VALID_END]);
254565 auto frame_end_data = FlatVector::GetData<idx_t>(bounds.data[FRAME_END]);
254566
254567 idx_t window_end = NumericLimits<idx_t>::Maximum();
254568
254569 // Reset previous range hints
254570 idx_t prev_partition = partition_begin_data[0];
254571 prev.start = valid_begin_data[0];
254572 prev.end = valid_end_data[0];
254573
254574 if (has_preceding_range || has_following_range) {
254575 if (range_lo.get() != range.get()) {
254576 range_lo = range.get();
254577 range_hi = range_lo->Copy();
254578 }
254579 }
254580
254581 switch (end_boundary) {
254582 case WindowBoundary::CURRENT_ROW_ROWS:
254583 for (idx_t chunk_idx = 0; chunk_idx < count; ++chunk_idx, ++row_idx) {
254584 frame_end_data[chunk_idx] = row_idx + 1;
254585 }
254586 break;
254587 case WindowBoundary::CURRENT_ROW_RANGE:
254588 case WindowBoundary::CURRENT_ROW_GROUPS:
254589 // in RANGE or GROUPS mode it means that the frame starts or ends with the current row's
254590 // first or last peer in the ORDER BY ordering
254591 bounds.data[FRAME_END].Reference(bounds.data[PEER_END]);
254592 frame_end_data = peer_end_data;
254593 break;
254594 case WindowBoundary::UNBOUNDED_FOLLOWING:
254595 bounds.data[FRAME_END].Reference(bounds.data[PARTITION_END]);
254596 // No need to clamp
254597 return;
254598 case WindowBoundary::EXPR_PRECEDING_ROWS: {
254599 for (idx_t chunk_idx = 0; chunk_idx < count; ++chunk_idx, ++row_idx) {
254600 int64_t computed_start;
254601 if (!TrySubtractOperator::Operation(int64_t(row_idx + 1), boundary_end.GetCell<int64_t>(chunk_idx),
254602 computed_start)) {
254603 window_end = partition_end_data[chunk_idx];
254604 } else {
254605 window_end = UnsafeNumericCast<idx_t>(MaxValue<int64_t>(computed_start, 0));
254606 }
254607 frame_end_data[chunk_idx] = window_end;
254608 }
254609 break;
254610 }
254611 case WindowBoundary::EXPR_FOLLOWING_ROWS:
254612 for (idx_t chunk_idx = 0; chunk_idx < count; ++chunk_idx, ++row_idx) {
254613 int64_t computed_start;
254614 if (!TryAddOperator::Operation(int64_t(row_idx + 1), boundary_end.GetCell<int64_t>(chunk_idx),
254615 computed_start)) {
254616 window_end = partition_end_data[chunk_idx];
254617 } else {
254618 window_end = UnsafeNumericCast<idx_t>(MaxValue<int64_t>(computed_start, 0));
254619 }
254620 frame_end_data[chunk_idx] = window_end;
254621 }
254622 break;
254623 case WindowBoundary::EXPR_PRECEDING_RANGE:
254624 for (idx_t chunk_idx = 0; chunk_idx < count; ++chunk_idx, ++row_idx) {
254625 if (boundary_end.CellIsNull(chunk_idx)) {
254626 window_end = peer_end_data[chunk_idx];
254627 } else {
254628 const auto valid_start = valid_begin_data[chunk_idx];
254629 prev.start = valid_start;
254630 const auto cur_partition = partition_begin_data[chunk_idx];
254631 if (cur_partition != prev_partition) {
254632 prev.end = valid_end;
254633 prev_partition = cur_partition;
254634 }
254635 window_end = FindOrderedRangeBound<false>(*range_lo, *range_hi, range_sense, valid_start, row_idx + 1,
254636 end_boundary, boundary_end, chunk_idx, prev);
254637 prev.end = window_end;
254638 }
254639 frame_end_data[chunk_idx] = window_end;
254640 }
254641 break;
254642 case WindowBoundary::EXPR_FOLLOWING_RANGE:
254643 for (idx_t chunk_idx = 0; chunk_idx < count; ++chunk_idx, ++row_idx) {
254644 if (boundary_end.CellIsNull(chunk_idx)) {
254645 window_end = peer_end_data[chunk_idx];
254646 } else {
254647 const auto valid_end = valid_end_data[chunk_idx];
254648 prev.start = valid_begin_data[chunk_idx];
254649 const auto cur_partition = partition_begin_data[chunk_idx];
254650 if (cur_partition != prev_partition) {
254651 prev.end = valid_end;
254652 prev_partition = cur_partition;
254653 }
254654 window_end = FindOrderedRangeBound<false>(*range_lo, *range_hi, range_sense, row_idx, valid_end,
254655 end_boundary, boundary_end, chunk_idx, prev);
254656 prev.end = window_end;
254657 }
254658 frame_end_data[chunk_idx] = window_end;
254659 }
254660 break;
254661 case WindowBoundary::EXPR_PRECEDING_GROUPS:
254662 // In GROUPS mode, the offset is an integer indicating that the frame starts or ends that many peer groups
254663 // before or after the current row's peer group, where a peer group is a group of rows that are equivalent
254664 // according to the window's ORDER BY clause.
254665 for (idx_t chunk_idx = 0; chunk_idx < count; ++chunk_idx, ++row_idx) {
254666 if (boundary_end.CellIsNull(chunk_idx)) {
254667 window_end = peer_end_data[chunk_idx];
254668 } else {
254669 // Count peer groups backwards.
254670 const auto peer_end = peer_end_data[chunk_idx];
254671 const auto partition_begin = partition_begin_data[chunk_idx];
254672 const auto boundary = boundary_end.GetCell<int64_t>(chunk_idx);
254673 if (boundary < 0) {
254674 throw OutOfRangeException("Invalid GROUPS PRECEDING value");
254675 } else if (!boundary) {
254676 window_end = peer_end;
254677 } else {
254678 auto n = UnsafeNumericCast<idx_t>(boundary);
254679 window_end = FindPrevStart(order_mask, partition_begin, peer_end, n);
254680 }
254681 }
254682 frame_end_data[chunk_idx] = window_end;
254683 }
254684 break;
254685 case WindowBoundary::EXPR_FOLLOWING_GROUPS:
254686 for (idx_t chunk_idx = 0; chunk_idx < count; ++chunk_idx, ++row_idx) {
254687 if (boundary_end.CellIsNull(chunk_idx)) {
254688 window_end = peer_end_data[chunk_idx];
254689 } else {
254690 // Count peer groups forward.
254691 const auto peer_end = peer_end_data[chunk_idx];
254692 const auto partition_end = partition_end_data[chunk_idx];
254693 const auto boundary = boundary_end.GetCell<int64_t>(chunk_idx);
254694 if (boundary < 0) {
254695 throw OutOfRangeException("Invalid GROUPS FOLLOWING value");
254696 } else if (!boundary) {
254697 window_end = peer_end;
254698 } else {
254699 auto n = UnsafeNumericCast<idx_t>(boundary);
254700 window_end = FindNextStart(order_mask, peer_end + 1, partition_end, n);
254701 }
254702 }
254703 frame_end_data[chunk_idx] = window_end;
254704 }
254705 break;
254706 case WindowBoundary::UNBOUNDED_PRECEDING:
254707 case WindowBoundary::INVALID:
254708 throw InternalException("Unsupported window end boundary");
254709 }
254710
254711 ClampFrame(count, frame_end_data, partition_begin_data, partition_end_data);
254712}

◆ ClampFrame()

void duckdb::WindowBoundariesState::ClampFrame ( const idx_t count,
idx_t * values,
const idx_t * begin,
const idx_t * end )
inlinestatic
163790 {
163791 for (idx_t i = 0; i < count; ++i) {
163792 values[i] = MinValue(MaxValue(values[i], begin[i]), end[i]);
163793 }
163794 }

◆ Bounds()

void duckdb::WindowBoundariesState::Bounds ( DataChunk & bounds,
idx_t row_idx,
optional_ptr< WindowCursor > range,
const idx_t count,
WindowInputExpression & boundary_start,
WindowInputExpression & boundary_end,
const ValidityMask & partition_mask,
const ValidityMask & order_mask )
254174 {
254175 bounds.Reset();
254176 D_ASSERT(bounds.ColumnCount() == 8);
254177
254178 // Have we jumped from the previous position?
254179 const auto is_jump = (next_pos != row_idx);
254180 if (is_jump) {
254181 next_pos = row_idx;
254182 }
254183
254184 if (required.count(PARTITION_BEGIN)) {
254185 PartitionBegin(bounds, row_idx, count, is_jump, partition_mask);
254186 }
254187 if (required.count(PARTITION_END)) {
254188 PartitionEnd(bounds, row_idx, count, is_jump, partition_mask);
254189 }
254190 if (required.count(PEER_BEGIN)) {
254191 PeerBegin(bounds, row_idx, count, is_jump, partition_mask, order_mask);
254192 }
254193 if (required.count(PEER_END)) {
254194 PeerEnd(bounds, row_idx, count, partition_mask, order_mask);
254195 }
254196 if (required.count(VALID_BEGIN)) {
254197 ValidBegin(bounds, row_idx, count, is_jump, partition_mask, order_mask, range);
254198 }
254199 if (required.count(VALID_END)) {
254200 ValidEnd(bounds, row_idx, count, is_jump, partition_mask, order_mask, range);
254201 }
254202 if (required.count(FRAME_BEGIN)) {
254203 FrameBegin(bounds, row_idx, count, boundary_start, order_mask, range);
254204 }
254205 if (required.count(FRAME_END)) {
254206 FrameEnd(bounds, row_idx, count, boundary_end, order_mask, range);
254207 }
254208 next_pos += count;
254209
254210 bounds.SetCardinality(count);
254211}
DUCKDB_API void Reset()
Definition duckdb.cpp:116840

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