Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Window ordering is not enforced for ntile and ranking window functions #24163

Open
kasiafi opened this issue Nov 18, 2024 · 0 comments
Open

Window ordering is not enforced for ntile and ranking window functions #24163

kasiafi opened this issue Nov 18, 2024 · 0 comments

Comments

@kasiafi
Copy link
Member

kasiafi commented Nov 18, 2024

According to SQL specification ISO/IEC 9075-2:2023(E) 9.23 Evaluationandtransformationof<windowfunction>, p. 665:

5) If <ntile function>, <lead or lag function>, <rank function type> or ROW_NUMBER is specified, then:
     a) If <ntile function>, <lead or lag function>, or <rank function type> is specified, then the window ordering clause WOC of WDX shall be present.

We check for the window ordering clause only for the lead() and lag() functions.

Here's how the remaining functions behave without window ordering:

  • rank() and dense_rank() return 1 for all rows. This is expected and kind of intuitive, as in the absence of ordering, all rows are peers.
  • percent_rank() returns 0.0 for every row, which is consistent with the function's semantics where all rows are peers.
  • cume_dist() returns 1.0 for every row, which is consistent with the function's semantics where all rows are peers.
  • ntile() assigns buckets to rows in their input order. It might be useful, but the name ntile is not relevant.
trino> select a, ntile(2) over() from (values 1, 5, 3, 4, 2) t(a);
 a | _col1
---+-------
 1 |     1
 5 |     1
 3 |     1
 4 |     2
 2 |     2
(5 rows)

I suggest that we:

  • enforce window ordering for ntile, and add another ntile-like function for random bucketing
  • leave the remaining functions as they are, and treat it as extension to SQL specification

or do not enforce window ordering at all, but instead document explicitly the unordered behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant