Repository navigation
feat(core-spec): default NULL placement in window ORDER BY - #587
Conversation
Without NULLS FIRST or NULLS LAST, NULL sorts as the largest value: last for ASC, first for DESC. A mapping table lists which engines need the explicit clause to match. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
| RANK() OVER (ORDER BY revenue DESC NULLS LAST) | ||
| ``` | ||
|
|
||
| Without `NULLS FIRST` or `NULLS LAST`, NULL sorts as the largest value: last for `ASC`, first for `DESC`. Implementations MUST produce this order whatever the native default of the engine; see [Window ORDER BY NULL Placement](#window-order-by-null-placement). |
There was a problem hiding this comment.
Just a note: the MUST can't be met on engines with no NULLS FIRST/LAST syntax, such as MySQL/MariaDB and SQL Server (even if I know there are not the first target candidates 😄 ).
Both treat NULL as smallest, so ASC puts NULL first, the opposite of the Ossie default. Following the MUST there needs a rewrite like ORDER BY expr IS NULL, expr.
Maybe we could make the spec either describe that rewrite or say such items may be reported as unsupported? Otherwise conformance on those dialects is undefined.
There was a problem hiding this comment.
Good catch. The new section now covers engines without the clause: a leading CASE WHEN order_expr IS NULL THEN 1 ELSE 0 END key. It is ascending for NULLs last and descending for NULLs first. CASE is the form SQLGlot writes for both MySQL and T-SQL, so one rule covers both.
There was a problem hiding this comment.
| Without `NULLS FIRST` or `NULLS LAST`, NULL sorts as the largest value: last for `ASC`, first for `DESC`. Implementations MUST produce this order whatever the native default of the engine; see [Window ORDER BY NULL Placement](#window-order-by-null-placement). | |
| Without `NULLS FIRST` or `NULLS LAST`, NULL sorts as the largest value: last for `ASC`, first for `DESC`. This applies to `OSSIE_SQL_2026` expressions; other dialects keep their own semantics. Implementations MUST produce this order whatever the native default of the engine; see [Window ORDER BY NULL Placement](#window-order-by-null-placement). An implementation that converts a native expression into `OSSIE_SQL_2026` MUST add `NULLS FIRST` or `NULLS LAST` to each item whose native placement differs from this default. |
There was a problem hiding this comment.
Applied, except for the change to "MUST produce this order", explained in the thread on line 806. A question on the converter sentence. Snowflake and DuckDB let a session setting change the native placement, so a converter cannot always know it. Should it then write the clause on every item? If so, I'll add that.
|
|
||
| Sigma's table-calculation functions (`RowNumber`, `Rank`, `RunningSum`, `RunningAvg`, `Lag`, `Lead`, etc.) resolve their partition/order context from workbook UI configuration (which pivot/table the calculation is attached to) rather than from arguments passed in the formula text itself. Because that context isn't recoverable from the formula string alone, the Sigma converter (`converters/sigma/`) treats these as untranslatable to ANSI SQL and carries the original Sigma formula through in the `SIGMA` dialect only — see `converters/sigma/LIMITATIONS.md`. | ||
|
|
||
| ### Window ORDER BY NULL Placement |
There was a problem hiding this comment.
Because this ### directly after the Sigma table-calculation note, it reads as a Sigma subsection.
| ### Window ORDER BY NULL Placement | |
| ## Window ORDER BY NULL Placement |
There was a problem hiding this comment.
Applied: it is now its own ## section after the tool mappings.
| | :---- | :---- | :---- | :---- | | ||
| | Snowflake, PostgreSQL | NULLS LAST | NULLS FIRST | nothing | | ||
| | BigQuery, Databricks | NULLS FIRST | NULLS LAST | `ASC NULLS LAST`, `DESC NULLS FIRST` | | ||
| | ClickHouse, DuckDB | NULLS LAST | NULLS LAST | `DESC NULLS FIRST` | |
There was a problem hiding this comment.
Maybe we should add other engines (SQL Server, MySQL/MariaDB, Oracle, Redshift, Trino/Athena, ...).
Oracle and Trino default to NULLs largest like Postgres, so omission is safe there.
MySQL/SQL Server need the workaround.
To be honest, I don't know if we need that, so it's more an open question 😄
There was a problem hiding this comment.
Added Oracle, Redshift, Trino, MySQL and SQL Server, each checked against its current docs. One difference from your note: Trino puts NULLs last in both directions. Its docs say "The default null ordering is NULLS LAST, regardless of the ordering direction" (https://trino.io/docs/current/sql/select.html). I left MariaDB out because I couldn't confirm its syntax from the docs.
| | BigQuery, Databricks | NULLS FIRST | NULLS LAST | `ASC NULLS LAST`, `DESC NULLS FIRST` | | ||
| | ClickHouse, DuckDB | NULLS LAST | NULLS LAST | `DESC NULLS FIRST` | | ||
|
|
||
| Snowflake (`DEFAULT_NULL_ORDERING`) and DuckDB (`default_null_order`) let a session change the native default; an implementation that cannot rely on the setting emits the clause on every `ORDER BY` item. |
There was a problem hiding this comment.
I think this conflicts with the table. The table says Snowflake emits "nothing", but this paragraph says to emit the clause on every time if you can't rely on the session setting. A Snowflake or DuckDB converter that follows the table will silently reorder results under a non-default DEFAULT_NULL_ORDERING or default_null_order.
Maybe we should clearly state one rule, for example "omit only when the implementation controls the session setting, otherwise always emit". Also, the table has no entry for the Clickhouse and BigQuery settings that can change the native default, and it doesn't say whether Postgres is safe to omit because it has no such setting.
There was a problem hiding this comment.
Agreed. The section now has one rule: emit the clause. A default placement may be left out only if the native default already gives it. It also needs a session that can't change that default, or an implementation that sets it. The table no longer says when to omit. PostgreSQL may omit: I found no NULL-ordering setting in its configuration reference. Nor could I find one in ClickHouse (system.settings on 26.9) or the BigQuery query syntax reference. If you know one, I'll add it next to Snowflake and DuckDB.
…n section Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
@jbonofre I addressed your four comments in 6b08d8b on Oct 15 and replied inline. The dev@ thread has had no objections since then, so the default looks settled by lazy consensus: https://lists.apache.org/thread/z2jr7q4pllsk5nkzv7d18ro8844n5tgy. Could you take another look and merge if it reads well? |
| When an `ORDER BY` item omits the clause, the Ossie placement is `NULLS LAST` for `ASC` and `NULLS FIRST` for `DESC`. An implementation produces any placement, given or default, as follows: | ||
|
|
||
| - On an engine with `NULLS FIRST` and `NULLS LAST`, it emits the clause. It MAY omit a default placement that the native default already gives. This holds only if the session cannot change that default, or the implementation sets it. Snowflake (`DEFAULT_NULL_ORDERING`) and DuckDB (`default_null_order`) let a session change it. | ||
| - On an engine without them, it adds a leading sort key `CASE WHEN order_expr IS NULL THEN 1 ELSE 0 END`. The key is ascending to put NULLs last and descending to put them first. |
There was a problem hiding this comment.
With several ORDER BY items, "a leading sort key" can be read as putting every key at the front of the list. For ORDER BY d_year, d_moy (the shape of monthly_sales_change in the TPC-DS example) that gives:
ORDER BY CASE WHEN d_year IS NULL THEN 1 ELSE 0 END,
CASE WHEN d_moy IS NULL THEN 1 ELSE 0 END,
d_year, d_moy
which pushed every NULL-month raw after all other rows, instead of to the end of its own year. Each key has to go just before its own item:
| - On an engine without them, it adds a leading sort key `CASE WHEN order_expr IS NULL THEN 1 ELSE 0 END`. The key is ascending to put NULLs last and descending to put them first. | |
| - On an engine without them, it puts a sort key `CASE WHEN order_expr IS NULL THEN 1 ELSE 0 END` immediately before each `ORDER BY` item, built from that item's `order_expr`. The key is ascending to put NULLs last and descending to put them first. For example, `ORDER BY d_year, d_moy DESC` becomes `ORDER BY CASE WHEN d_year IS NULL THEN 1 ELSE 0 END, d_year, CASE WHEN d_moy IS NULL THEN 1 ELSE 0 END DESC, d_moy DESC`. |
There was a problem hiding this comment.
Applied, thanks. I tested it on six (year, month) rows on ClickHouse 26.9. Your version gives the same order as the clauses. With both keys in front, a NULL month of 2026 lands among the 2025 rows. I also added one sentence: "Where the native placement already matches, it MAY omit the key, on the same condition as in the bullet above." Without it, the next bullet would require an error where no key is needed.
| When an `ORDER BY` item omits the clause, the Ossie placement is `NULLS LAST` for `ASC` and `NULLS FIRST` for `DESC`. An implementation produces any placement, given or default, as follows: | ||
|
|
||
| - On an engine with `NULLS FIRST` and `NULLS LAST`, it emits the clause. It MAY omit a default placement that the native default already gives. This holds only if the session cannot change that default, or the implementation sets it. Snowflake (`DEFAULT_NULL_ORDERING`) and DuckDB (`default_null_order`) let a session change it. | ||
| - On an engine without them, it adds a leading sort key `CASE WHEN order_expr IS NULL THEN 1 ELSE 0 END`. The key is ascending to put NULLs last and descending to put them first. |
There was a problem hiding this comment.
Oh, by the way, on MySQL this key can't be added when the frame is RANGE with an offset. MySQL requires exactly one ORDER BY expression there, so it rejects:
SUM(amount) OVER (ORDER BY CASE WHEN order_day IS NULL THEN 1 ELSE 0 END, order_day
RANGE BETWEEN 6 PRECEDING AND CURRENT ROW)
The grammar allows any frame clause, so the spec needs to say what happens in the case. For example:
If an engine can express neither the clause nor the sort key for an item, the implementation MUST report an error instead of emitting SQL that places NULLs differently.
There was a problem hiding this comment.
Added as a third bullet, with MySQL as the example. MySQL documents the limit in error 3587: a RANGE N PRECEDING/FOLLOWING frame "requires exactly one ORDER BY expression". I narrowed your condition to "If an item needs the key and the engine cannot add it". With the original wording, ORDER BY d NULLS FIRST in a RANGE offset frame would be an error on MySQL. MySQL needs no key there, since its native ASC already puts NULLs first. Line 576 now says "or report the error that [Window ORDER BY NULL Placement] requires", so the two MUSTs agree.
| RANK() OVER (ORDER BY revenue DESC NULLS LAST) | ||
| ``` | ||
|
|
||
| Without `NULLS FIRST` or `NULLS LAST`, NULL sorts as the largest value: last for `ASC`, first for `DESC`. Implementations MUST produce this order whatever the native default of the engine; see [Window ORDER BY NULL Placement](#window-order-by-null-placement). |
There was a problem hiding this comment.
| Without `NULLS FIRST` or `NULLS LAST`, NULL sorts as the largest value: last for `ASC`, first for `DESC`. Implementations MUST produce this order whatever the native default of the engine; see [Window ORDER BY NULL Placement](#window-order-by-null-placement). | |
| Without `NULLS FIRST` or `NULLS LAST`, NULL sorts as the largest value: last for `ASC`, first for `DESC`. This applies to `OSSIE_SQL_2026` expressions; other dialects keep their own semantics. Implementations MUST produce this order whatever the native default of the engine; see [Window ORDER BY NULL Placement](#window-order-by-null-placement). An implementation that converts a native expression into `OSSIE_SQL_2026` MUST add `NULLS FIRST` or `NULLS LAST` to each item whose native placement differs from this default. |
…be added, OSSIE_SQL_2026 scope and converters Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
@ekut thanks! I'm doing a new pass. |
jbonofre
left a comment
There was a problem hiding this comment.
Thanks @ekut, this address my earlier comments.
- The
CASEsort key now sits immediately before eachORDER BYitem, so the multi-column ordering is correct. - The MySQL
RANGEframe case is covered by the error bullet. Narrowing it to "if an item needs the key and the engine cannot add it" is better than my wording. - The native-default table matches my understanding of each engine.
I approve.
I will hold the merge until the dev@ lazy consensus on the default completes, since @christianeu-db asked for the default to be settled by a separate vote.
christianeu-db
left a comment
There was a problem hiding this comment.
@ekut Thanks for putting this together. I agree this should merge once the lazy consensus period ends.
One thought that occurred to me based a on comment (possibly yours) on another spec PR: the gap around undefined NULL placement extends beyond this case. For example, ORDER BY in queries, PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY x), and other functions we haven't yet added to the expression language.
I wonder if it would make sense to have a central ossie_sql_defaults file (or similar) under core-spec/, where we define defaults that the underlying standard leaves unspecified. Other spec files could then inherit these defaults unless explicitly overridden.
Just an idea to avoid defining the same behavior in multiple places as the spec grows.
|
Thanks, @christianeu-db. I agree the gap goes beyond the window As #676 notes, A shared defaults file would let Section 7.5 and Appendix A.1 use one rule. Would the default from this PR move into that file, or stay here with the other files referring to it? |
@ekut Could you work with @jbonofre on #676? I don't want to add scope creep to this PR (esp. since it takes some time to get to consensus). Let's land this now for the window-function case first. Generalizing from there should be straightforward: mostly a matter of moving the logic down into a shared layer and referencing it in more places. |
apache/ossie#587 defines it: without NULLS FIRST | LAST, NULL sorts last for ASC and first for DESC, whatever the engine's default. parse() now gives each DESC item of a window's ORDER BY that has no clause NULLS FIRST; ASC is ClickHouse's default already. An explicit clause is kept as written. The rows of a query and ORDER BY inside WITHIN GROUP still sort NULLs last both ways. Tests pin the DESC values by hand (DuckDB disagrees with the spec), the spec's own ROW_NUMBER example, the planner SQL, and an end-to-end rank over TPC-DS sales without a date. Canaries cover SQLGlot's printing of the clause and the spec sentence. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
apache/ossie#587 defines it: without NULLS FIRST | LAST, NULL sorts last for ASC and first for DESC, whatever the engine's default. parse() now gives each DESC item of a window's ORDER BY that has no clause NULLS FIRST; ASC is ClickHouse's default already. An explicit clause is kept as written. The rows of a query and ORDER BY inside WITHIN GROUP still sort NULLs last both ways. Tests pin the DESC values by hand (DuckDB disagrees with the spec), the spec's own ROW_NUMBER example, the planner SQL, and an end-to-end rank over TPC-DS sales without a date. Canaries cover SQLGlot's printing of the clause and the spec sentence. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…ored dialect means it sqlglot's base dialect sorts NULL as the smallest value, the opposite of the OSSIE_SQL_2026 default (apache#587). The grader therefore marked RANK() OVER (ORDER BY x DESC NULLS LAST), which changes the order, as a cosmetic rewrite of RANK() OVER (ORDER BY x DESC), and the equivalent DESC NULLS FIRST as needing review. OSSIE_SQL_2026 expressions are now compared with NULL as the largest value; ANSI_SQL ones, whose default is the engine's, only count as cosmetic when equal under both orderings. Co-authored-by: Isaac <no-reply@databricks.com>
…ratio-to-total) M7 covers window functions with an ORDER BY: LAG, a running SUM with a ROWS frame, and RANK() OVER (ORDER BY ... DESC) without NULLS FIRST/LAST, which relies on the OSSIE_SQL_2026 default from apache#587. That default is defined for OSSIE_SQL_2026 only, so M7 is authored in it. M8 covers ratio-to-total, x / SUM(x) OVER () and OVER (PARTITION BY ...), with no ORDER BY. Both are field expressions over sibling columns, as M1. The manifest, the identity test and both READMEs list M0-M8; the hold note on apache#587 is gone. All 15 fixtures validate on main (f28692a) with sqlglot 30.21.0. Co-authored-by: Isaac <no-reply@databricks.com>
…ored dialect means it sqlglot's base dialect sorts NULL as the smallest value, the opposite of the OSSIE_SQL_2026 default (apache#587). The grader therefore marked RANK() OVER (ORDER BY x DESC NULLS LAST), which changes the order, as a cosmetic rewrite of RANK() OVER (ORDER BY x DESC), and the equivalent DESC NULLS FIRST as needing review. OSSIE_SQL_2026 expressions are now compared with NULL as the largest value; ANSI_SQL ones, whose default is the engine's, only count as cosmetic when equal under both orderings. Co-authored-by: Isaac <no-reply@databricks.com>
…ratio-to-total) M7 covers window functions with an ORDER BY: LAG, a running SUM with a ROWS frame, and RANK() OVER (ORDER BY ... DESC) without NULLS FIRST/LAST, which relies on the OSSIE_SQL_2026 default from apache#587. That default is defined for OSSIE_SQL_2026 only, so M7 is authored in it. M8 covers ratio-to-total, x / SUM(x) OVER () and OVER (PARTITION BY ...), with no ORDER BY. Both are field expressions over sibling columns, as M1. The manifest, the identity test and both READMEs list M0-M8; the hold note on apache#587 is gone. All 15 fixtures validate on main (f28692a) with sqlglot 30.21.0. Co-authored-by: Isaac <no-reply@databricks.com>
… audit (#711) * docs(spec-changes): correct and complete the 0.3.0.dev0 history Update the #577 and #591 rows, which later PRs superseded: #587 set the default NULL placement in window ORDER BY, and #633 lets dataset-scoped metrics reference source columns as source.column. State the #617 precedence of expression.sql as the MUST the spec makes it. Add the breaking changes merged in this window (#397 root dialects and vendors removed, #400 ontology uri renamed to iri, #486 name resolution), together with #587, #633 and the Layer 3 interface (#653). * fix(scripts): make the spec change audit see all of main Drop --first-parent: on a branch that has merged main, it followed the branch and skipped every main commit merged after the branch forked. Resolve the default history file and the pathspecs from the repository root, so running from a subdirectory no longer reports no candidates. Read --since as midnight UTC, matching the dates in the history, instead of the current local time on that date. Strip GIT_* variables and the user's git config from the test environment, so the tests cannot act on the developer's repository when run from a git hook, and run them in Validation CI.
…ored dialect means it sqlglot's base dialect sorts NULL as the smallest value, the opposite of the OSSIE_SQL_2026 default (apache#587). The grader therefore marked RANK() OVER (ORDER BY x DESC NULLS LAST), which changes the order, as a cosmetic rewrite of RANK() OVER (ORDER BY x DESC), and the equivalent DESC NULLS FIRST as needing review. OSSIE_SQL_2026 expressions are now compared with NULL as the largest value; ANSI_SQL ones, whose default is the engine's, only count as cosmetic when equal under both orderings. Co-authored-by: Isaac <no-reply@databricks.com>
…ratio-to-total) M7 covers window functions with an ORDER BY: LAG, a running SUM with a ROWS frame, and RANK() OVER (ORDER BY ... DESC) without NULLS FIRST/LAST, which relies on the OSSIE_SQL_2026 default from apache#587. That default is defined for OSSIE_SQL_2026 only, so M7 is authored in it. M8 covers ratio-to-total, x / SUM(x) OVER () and OVER (PARTITION BY ...), with no ORDER BY. Both are field expressions over sibling columns, as M1. The manifest, the identity test and both READMEs list M0-M8; the hold note on apache#587 is gone. All 15 fixtures validate on main (f28692a) with sqlglot 30.21.0. Co-authored-by: Isaac <no-reply@databricks.com>
…ratio-to-total) M7 covers window functions with an ORDER BY: LAG, a running SUM with a ROWS frame, and RANK() OVER (ORDER BY ... DESC) without NULLS FIRST/LAST, which relies on the OSSIE_SQL_2026 default from apache#587. That default is defined for OSSIE_SQL_2026 only, so M7 is authored in it. M8 covers ratio-to-total, x / SUM(x) OVER () and OVER (PARTITION BY ...), with no ORDER BY. Both are field expressions over sibling columns, as M1. The manifest, the identity test and both READMEs list M0-M8; the hold note on apache#587 is gone. All 15 fixtures validate on main (f28692a) with sqlglot 30.21.0. Co-authored-by: Isaac <no-reply@databricks.com>
…ored dialect means it sqlglot's base dialect sorts NULL as the smallest value, the opposite of the OSSIE_SQL_2026 default (apache#587). The grader therefore marked RANK() OVER (ORDER BY x DESC NULLS LAST), which changes the order, as a cosmetic rewrite of RANK() OVER (ORDER BY x DESC), and the equivalent DESC NULLS FIRST as needing review. OSSIE_SQL_2026 expressions are now compared with NULL as the largest value; ANSI_SQL ones, whose default is the engine's, only count as cosmetic when equal under both orderings. Co-authored-by: Isaac <no-reply@databricks.com>
…ored dialect means it sqlglot's base dialect sorts NULL as the smallest value, the opposite of the OSSIE_SQL_2026 default (apache#587). The grader therefore marked RANK() OVER (ORDER BY x DESC NULLS LAST), which changes the order, as a cosmetic rewrite of RANK() OVER (ORDER BY x DESC), and the equivalent DESC NULLS FIRST as needing review. OSSIE_SQL_2026 expressions are now compared with NULL as the largest value; ANSI_SQL ones, whose default is the engine's, only count as cosmetic when equal under both orderings. Co-authored-by: Isaac <no-reply@databricks.com>
…ored dialect means it (#756) sqlglot's base dialect sorts NULL as the smallest value, the opposite of the OSSIE_SQL_2026 default (#587). The grader therefore marked RANK() OVER (ORDER BY x DESC NULLS LAST), which changes the order, as a cosmetic rewrite of RANK() OVER (ORDER BY x DESC), and the equivalent DESC NULLS FIRST as needing review. OSSIE_SQL_2026 expressions are now compared with NULL as the largest value; ANSI_SQL ones, whose default is the engine's, only count as cosmetic when equal under both orderings. Co-authored-by: Isaac <no-reply@databricks.com>
…ratio-to-total) (#700) * test(compliance): add portability rungs M7 (window ORDER BY) and M8 (ratio-to-total) M7 covers window functions with an ORDER BY: LAG, a running SUM with a ROWS frame, and RANK() OVER (ORDER BY ... DESC) without NULLS FIRST/LAST, which relies on the OSSIE_SQL_2026 default from #587. That default is defined for OSSIE_SQL_2026 only, so M7 is authored in it. M8 covers ratio-to-total, x / SUM(x) OVER () and OVER (PARTITION BY ...), with no ORDER BY. Both are field expressions over sibling columns, as M1. The manifest, the identity test and both READMEs list M0-M8; the hold note on #587 is gone. All 15 fixtures validate on main (f28692a) with sqlglot 30.21.0. Co-authored-by: Isaac <no-reply@databricks.com> * test(compliance): move the M7 and M8 fixtures to spec version 0.3.0.dev0 Co-authored-by: Isaac <no-reply@databricks.com> --------- Co-authored-by: Isaac <no-reply@databricks.com>
Sets where NULLs sort in a window
ORDER BYwhenNULLS FIRSTorNULLS LASTis omitted: as the largest value, last forASCand first forDESC. Follows #577 and closes #498. On dev@ [1], @jbonofre proposed this default and @christianeu-db asked for it as a separate PR and vote.The rule is the Order By default of #246 (
foundational_semantics.md§5.1), so a window and a query order NULLs the same way. A new section after the tool mappings says how to produce it. Engines with the clause emit it. MySQL and SQL Server get a leadingCASEsort key instead. A table lists the native default of each engine. The two examples without the clause are the conformance cases JB asked for. They can move to the compliance suite once #237 lands.Impact on implementations: the Holistics (
sqlrefs.transpile) and Hex converters render Ossie expressions into warehouse SQL through SQLGlot, reading them with the base dialect, which treats NULLs as smallest. With SQLGlot 30.23.0,RANK() OVER (ORDER BY x DESC)comes out asDESC NULLS LASTfor Snowflake and PostgreSQL and unchanged for BigQuery and Databricks, the opposite of this default on all four. Reading with a dialect whoseNULL_ORDERINGisnulls_are_largefixes that. The Databricks converter already keeps windows out of the Ossie dialect. T-027 and D-029 in #237 expectNULLS LASTforDESCand would need to follow [2].[1] https://lists.apache.org/thread/z2jr7q4pllsk5nkzv7d18ro8844n5tgy
[2] #237 (comment)