258928 {
258929 auto &gvstate = sink.global_state.Cast<WindowValueGlobalState>();
258930 auto &lvstate = sink.local_state.Cast<WindowValueLocalState>();
258931 auto &cursor = *lvstate.cursor;
258932 auto &bounds = lvstate.bounds;
258933 auto &frames = lvstate.frames;
258934 auto &ignore_nulls = *gvstate.ignore_nulls;
258935 auto exclude_mode = gvstate.executor.wexpr.exclude_clause;
258936 D_ASSERT(cursor.chunk.ColumnCount() == 1);
258937 WindowInputExpression nth_col(eval_chunk,
nth_idx);
258938 WindowAggregator::EvaluateSubFrames(bounds, exclude_mode, count, row_idx, frames, [&](
idx_t i) {
258939
258940
258941 if (nth_col.CellIsNull(i)) {
258942 FlatVector::SetNull(result, i, true);
258943 return;
258944 }
258945 auto n_param = nth_col.GetCell<
int64_t>(i);
258946 if (n_param < 1) {
258947 FlatVector::SetNull(result, i, true);
258948 return;
258949 }
258950
258951
258952 auto n =
idx_t(n_param);
258953
258954 if (gvstate.value_tree) {
258955 idx_t frame_width = 0;
258956 for (const auto &frame : frames) {
258957 frame_width += frame.end - frame.start;
258958 }
258959
258960 if (--n < frame_width) {
258961 const auto nth_index = gvstate.value_tree->SelectNth(frames, n);
258962 if (nth_index.second || nth_index.first >= cursor.Count()) {
258963
258964 FlatVector::SetNull(result, i, true);
258965 } else {
258966 cursor.CopyCell(0, nth_index.first, result, i);
258967 }
258968 } else {
258969 FlatVector::SetNull(result, i, true);
258970 }
258971 return;
258972 }
258973
258974 for (const auto &frame : frames) {
258975 if (frame.start >= frame.end) {
258976 continue;
258977 }
258978
258979 const auto nth_index = WindowBoundariesState::FindNextStart(ignore_nulls, frame.start, frame.end, n);
258980 if (!n) {
258981 cursor.CopyCell(0, nth_index, result, i);
258982 return;
258983 }
258984 }
258985 FlatVector::SetNull(result, i, true);
258986 });
258987}
column_t nth_idx
The column index of the Nth column.
Definition duckdb.cpp:164348
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237