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
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) {
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
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
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
258699 const auto n = NumericCast<idx_t>(val_idx);
258700 const auto nth_index = glstate.value_tree->SelectNth(frames, n);
258701
258702 if (nth_index.second) {
258703
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
258721 if (glstate.use_framing) {
258722 partition_begin = frame_begin;
258723 partition_end = frame_end;
258724 }
258725
258726
258727
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;) {
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 }
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
258757 if (val_idx < (int64_t)row_idx) {
258758
258760 val_idx =
int64_t(WindowBoundariesState::FindPrevStart(*ignore_nulls, partition_begin[i], row_idx, delta));
258761 } else if (val_idx > (int64_t)row_idx) {
258763 val_idx =
258764 int64_t(WindowBoundariesState::FindNextStart(*ignore_nulls, row_idx + 1, partition_end[i], delta));
258765 }
258766
258767
258768 if (can_shift) {
258769 const auto target_limit = MinValue(partition_end[i], row_end) - row_idx;
258770 if (!delta) {
258771
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
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;
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
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237