Skip to content

Sort NULLs the way Postgres does on the SQL front end (ATSCALE-52093) - #2

Open
dave-mariani-atscale wants to merge 1 commit into
mainfrom
dm-atscale-52093-postgres-null-placement
Open

dave-mariani-atscale wants to merge 1 commit into
mainfrom
dm-atscale-52093-postgres-null-placement

Conversation

@dave-mariani-atscale

@dave-mariani-atscale dave-mariani-atscale commented Aug 23, 2026

Copy link
Copy Markdown

Turns on query.language.sql.ordering.postgresNullPlacement, added in engine PR #9968 for ATSCALE-52093.

Why

The engine normalises every warehouse to "NULL sorts smallest" — nulls first ascending, last descending. That is Hive's rule, and it is deliberate: a cube gives one answer whatever backend sits underneath.

It is also the opposite of Postgres in both directions.

ASC DESC
AtScale default NULLs first NULLs last
Postgres NULLs last NULLs first

The difference is invisible until it isn't. Any ORDER BY, LIMIT or window ranking over a nullable column returns a different set of rows through the SQL front end than the same query run against Postgres directly, with nothing in the result to say so.

What this changes

With the setting on, an ORDER BY on the SQL front end that states no placement gets Postgres's. Scoped to the SQL front end, so MDX and DAX are unaffected. A NULLS FIRST / NULLS LAST written into the query still wins over it.

Verified on a local engine carrying the fix:

query before the setting with NULLS LAST written
ORDER BY tei ASC [null, null, null]
ORDER BY tei ASC NULLS LAST [null, null, null] (clause was inert) [0.092, 0.0924, 0.1058]

Caveat

Turning this on changes the row order of existing queries that sort over nullable columns. That is the point, but worth knowing before it lands somewhere with saved reports built on the current order.

Requires an engine carrying PR #9968; on an older engine the key is simply unknown and ignored.

Turns on query.language.sql.ordering.postgresNullPlacement, added in engine
PR #9968.

The engine normalises every warehouse to "NULL sorts smallest" -- nulls first
ascending, last descending. That is Hive's rule and it is deliberate, so that a
cube gives one answer whatever the backend sits underneath. It is also the
opposite of Postgres in both directions: Postgres sorts NULL as the largest
value, so nulls come last ascending and first descending.

That difference is invisible until it isn't. Any ORDER BY, LIMIT or window
ranking over a nullable column returns a different set of rows through the SQL
front end than the same query run against Postgres directly, with nothing in the
result to say so.

This setting makes the front end emit Postgres's own placement when a query
states none. It is scoped to the SQL front end, so MDX and DAX are unaffected,
and a NULLS FIRST/LAST written into the query still wins over it.

Turning it on changes the row order of existing queries that sort over nullable
columns, which is the point, but worth knowing before it lands somewhere with
saved reports.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant