You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "comphead (via GitHub)" <gi...@apache.org> on 2026/05/06 20:04:08 UTC
[PR] feat: fix NTILE distribution logic [datafusion]
comphead opened a new pull request, #22051:
URL: https://github.com/apache/datafusion/pull/22051
## Which issue does this PR close?
<!--
We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123.
-->
- Closes #22049 .
## Rationale for this change
Root cause — datafusion/functions-window/src/ntile.rs:170-176 used the linear-interpolation formula i * n / num_rows to assign bucket numbers. That formula spreads the larger buckets evenly through the partition, but the SQL standard requires the first num_rows mod n buckets to be the larger ones. For
NTILE(4) over 10 rows it produced bucket sizes 3,2,3,2 instead of 3,3,2,2 — exactly the off-by-one you saw at row 6 of partition a=0.
Fix — replaced the formula with the front-loaded distribution: compute base = num_rows / n and remainder = num_rows % n, treat the first remainder * (base+1) rows as the "large bucket" region (each bucket of size base+1), and the remainder as size-base buckets. The n > num_rows case (existing test
NTILE(9223377) over 3 rows) still works because base = 0 forces every row into the "large" arm where each bucket holds one row.
<!--
Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.
-->
## What changes are included in this PR?
<!--
There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR.
-->
## Are these changes tested?
<!--
We typically require tests for all PRs in order to:
1. Prevent the code from being accidentally broken by subsequent changes
2. Serve as another way to document the expected behavior of the code
If tests are not included in your PR, please explain why (for example, are they covered by existing tests)?
-->
## Are there any user-facing changes?
<!--
If there are user-facing changes then we may require documentation to be updated before approving the PR.
-->
<!--
If there are any breaking changes to public APIs, please add the `api change` label.
-->
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] feat: fix NTILE distribution logic [datafusion]
Posted by "comphead (via GitHub)" <gi...@apache.org>.
comphead commented on PR #22051:
URL: https://github.com/apache/datafusion/pull/22051#issuecomment-4398669506
> is dead code then? We should remove it if it's not possible and replace with a `debug_assert` to make the invariant enforced in testing and invisible in release builds.
The evaluator can be simplified, however it is the existing code, thanks for helping to make it cleaner. Addressed
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] feat: fix NTILE distribution logic [datafusion]
Posted by "mbutrovich (via GitHub)" <gi...@apache.org>.
mbutrovich commented on code in PR #22051:
URL: https://github.com/apache/datafusion/pull/22051#discussion_r3198198218
##########
datafusion/functions-window/src/ntile.rs:
##########
Review Comment:
I think we need a guard for the unsigned path to match the signed path below for n == 0. The new code will panic if n is 0 in the unsigned case.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] feat: fix NTILE distribution logic [datafusion]
Posted by "mbutrovich (via GitHub)" <gi...@apache.org>.
mbutrovich commented on PR #22051:
URL: https://github.com/apache/datafusion/pull/22051#issuecomment-4393050212
Good thinking, @coderfender :)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] feat: fix NTILE distribution logic [datafusion]
Posted by "mbutrovich (via GitHub)" <gi...@apache.org>.
mbutrovich commented on PR #22051:
URL: https://github.com/apache/datafusion/pull/22051#issuecomment-4398278672
> Thanks @mbutrovich @coderfender non valid inputs, like 0, negative and NULL are covered in NTILE `partition_evaluator` (
>
> https://github.com/apache/datafusion/blob/2a14a93842bb44c31d07471afa1e886f158c7d6d/datafusion/functions-window/src/ntile.rs#L119
>
> ) which called through `create_udwf_window_expr` which DF and also Comet calling.
> But I'll add tests to assert it
>
> UPD: double checked, those tests already in place and passing.
So you're saying
```rust
if n <= 0 {
return exec_err!("NTILE requires a positive integer");
}
```
is dead code then? We should remove it if it's not possible and replace with a `debug_assert` to make the invariant enforced in testing and invisible in release builds.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] feat: fix NTILE distribution logic [datafusion]
Posted by "comphead (via GitHub)" <gi...@apache.org>.
comphead commented on PR #22051:
URL: https://github.com/apache/datafusion/pull/22051#issuecomment-4391726427
The entire CI is super fast now after #21941 thanks @blaginin
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] feat: fix NTILE distribution logic [datafusion]
Posted by "comphead (via GitHub)" <gi...@apache.org>.
comphead merged PR #22051:
URL: https://github.com/apache/datafusion/pull/22051
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] feat: fix NTILE distribution logic [datafusion]
Posted by "coderfender (via GitHub)" <gi...@apache.org>.
coderfender commented on code in PR #22051:
URL: https://github.com/apache/datafusion/pull/22051#discussion_r3198189033
##########
datafusion/functions-window/src/ntile.rs:
##########
@@ -167,12 +167,27 @@ impl PartitionEvaluator for NtileEvaluator {
_values: &[ArrayRef],
num_rows: usize,
) -> Result<ArrayRef> {
+ // SQL NTILE distributes rows "as equally as possible": with `base = num_rows / n`
+ // and `remainder = num_rows % n`, the first `remainder` buckets each contain
+ // `base + 1` rows and the rest contain `base` rows. The previous formula
+ // `i * n / num_rows` does not preserve those bucket sizes (e.g. NTILE(4) over
+ // 10 rows yielded sizes 3,2,3,2 instead of 3,3,2,2).
let num_rows = num_rows as u64;
- let mut vec: Vec<u64> = Vec::new();
- let n = u64::min(self.n, num_rows);
+ let n = self.n;
+ let mut vec: Vec<u64> = Vec::with_capacity(num_rows as usize);
+ let base = num_rows / n;
+ let remainder = num_rows % n;
+ let large_bucket_size = base + 1;
+ let large_rows = remainder * large_bucket_size;
for i in 0..num_rows {
- let res = i * n / num_rows;
- vec.push(res + 1)
+ let bucket = if i < large_rows {
+ i / large_bucket_size + 1
+ } else {
+ // base > 0 here: i >= large_rows is only reachable when remainder < n,
+ // which forces base >= 1 (otherwise large_rows would equal num_rows).
+ remainder + (i - large_rows) / base + 1
Review Comment:
Thank you ! perhaps a good idea to add tests for remainder = 0 ? Like say 10 rows distributed by 5
##########
datafusion/functions-window/src/ntile.rs:
##########
@@ -167,12 +167,27 @@ impl PartitionEvaluator for NtileEvaluator {
_values: &[ArrayRef],
num_rows: usize,
) -> Result<ArrayRef> {
+ // SQL NTILE distributes rows "as equally as possible": with `base = num_rows / n`
+ // and `remainder = num_rows % n`, the first `remainder` buckets each contain
+ // `base + 1` rows and the rest contain `base` rows. The previous formula
+ // `i * n / num_rows` does not preserve those bucket sizes (e.g. NTILE(4) over
+ // 10 rows yielded sizes 3,2,3,2 instead of 3,3,2,2).
let num_rows = num_rows as u64;
- let mut vec: Vec<u64> = Vec::new();
- let n = u64::min(self.n, num_rows);
+ let n = self.n;
Review Comment:
Should we also reject NTILE(0) directly ?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] feat: fix NTILE distribution logic [datafusion]
Posted by "comphead (via GitHub)" <gi...@apache.org>.
comphead commented on PR #22051:
URL: https://github.com/apache/datafusion/pull/22051#issuecomment-4393085589
Thanks @mbutrovich @coderfender non valid inputs, like 0, negative and NULL are covered in NTILE `partition_evaluator` which called through `create_udwf_window_expr` which DF and also Comet calling.
But I'll add tests to assert it
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org