257492 {
257493 auto &grstate = sink.global_state.Cast<WindowRowNumberGlobalState>();
257494 auto &lrstate = sink.local_state.Cast<WindowRowNumberLocalState>();
257495 auto partition_begin = FlatVector::GetData<const idx_t>(lrstate.bounds.data[PARTITION_BEGIN]);
257496 auto partition_end = FlatVector::GetData<const idx_t>(lrstate.bounds.data[PARTITION_END]);
257497 if (grstate.use_framing) {
257498
257499 partition_begin = FlatVector::GetData<const idx_t>(lrstate.bounds.data[FRAME_BEGIN]);
257500 partition_end = FlatVector::GetData<const idx_t>(lrstate.bounds.data[FRAME_END]);
257501 }
257502 auto rdata = FlatVector::GetData<int64_t>(result);
257503 WindowInputExpression ntile_col(eval_chunk,
ntile_idx);
257504 for (
idx_t i = 0; i < count; ++i, ++row_idx) {
257505 if (ntile_col.CellIsNull(i)) {
257506 FlatVector::SetNull(result, i, true);
257507 } else {
257508 auto n_param = ntile_col.GetCell<
int64_t>(i);
257509 if (n_param < 1) {
257510 throw InvalidInputException("Argument for ntile must be greater than zero");
257511 }
257512
257513 auto n_total = NumericCast<int64_t>(partition_end[i] - partition_begin[i]);
257514 if (n_param > n_total) {
257515
257516
257517 n_param = n_total;
257518 }
257519 int64_t n_size = (n_total / n_param);
257520
257521 D_ASSERT(row_idx >= partition_begin[i]);
257522 idx_t partition_idx = 0;
257523 if (grstate.token_tree) {
257524 partition_idx = grstate.token_tree->Rank(partition_begin[i], partition_end[i], row_idx) - 1;
257525 } else {
257526 partition_idx = row_idx - partition_begin[i];
257527 }
257528 auto adjusted_row_idx = NumericCast<int64_t>(partition_idx);
257529
257530
257531 int64_t n_large = n_total - n_param * n_size;
257532 int64_t i_small = n_large * (n_size + 1);
257534
257535 D_ASSERT((n_large * (n_size + 1) + (n_param - n_large) * n_size) == n_total);
257536
257537 if (adjusted_row_idx < i_small) {
257538 result_ntile = 1 + adjusted_row_idx / (n_size + 1);
257539 } else {
257540 result_ntile = 1 + n_large + (adjusted_row_idx - i_small) / n_size;
257541 }
257542
257543 D_ASSERT(result_ntile >= 1 && result_ntile <= n_param);
257544 rdata[i] = result_ntile;
257545 }
257546 }
257547}
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237