Null ordering
Each sort key carries an explicit position for nulls, first or last, independent of the sort direction. The compiler follows it literally, with the engine's own syntax where there is one and an equivalent form where there is not. The AG Grid adapter supplies a default, because the grid itself does not send one.
Rule. Every sortItem in the IR carries an explicit nullsPosition (first or last), independent of sortDirection — the IR never leaves null placement to be inferred from direction, and the compiler MUST honor nullsPosition literally rather than deriving it itself. Compilers emit native NULLS LAST/NULLS FIRST syntax where the dialect supports it, or an equivalent emulation where it doesn't.
The adapter's default (pinned). AG Grid's sortModel carries no null-position signal at all — it is purely { colId, sort }. Since $defs/sortItem requires nullsPosition on every sort key, the adapter must produce one on every sort key the grid sends, and it does so with a single, fixed rule: nullsPosition: "last" on asc, nullsPosition: "first" on desc. This is the one direction→position mapping this document pins — not because compilers are allowed to derive it themselves (they are not; see "must NOT" below), but because the adapter has to choose something deterministic when the wire protocol gives it nothing, and an arbitrary-but-consistent choice here is what makes G3/G4 achievable: every port's adapter applies the identical mapping, so every port's compiler receives the identical nullsPosition for the identical sortModel, and none of them ever compute it themselves.
Rationale. Database engines disagree about where NULLs sort by default: PostgreSQL and Oracle treat NULL as the "largest" value (NULLS LAST on ASC, NULLS FIRST on DESC, by default), while MySQL and SQLite treat NULL as the "smallest" value (NULLS FIRST on ASC by default) — and some engines' defaults are additionally subject to session/locale settings. If a compiler just emits ORDER BY col ASC and trusts the engine's default, the exact same IR produces a different row order depending on which SQL dialect executes it — a direct violation of G3/G4 (identical semantics across backends and language ports). Worse, it means results are technically legal-but-unpredictable per the SQL standard even on a single engine whose defaults change across a major version. Pinning nullsPosition explicitly in the IR, and pinning what value the adapter writes there in this document rather than leaving four ports to invent their own default — removes the ambiguity at the one place (the adapter, resolving AG Grid's request into IR) that should own the decision, and happens to match the PostgreSQL/Oracle default exactly (a coincidence of this particular choice, not a requirement that compilers rely on it — see "must NOT" below).
Worked example — sort amount ASC, nullsPosition: "last":
- PostgreSQL / Oracle / SQL:2003-compliant dialects — native syntax:
ORDER BY amount ASC NULLS LAST - MySQL — no native
NULLS LASTsyntax; emulated with anISNULL(col)prefix key (returns1for NULL,0otherwise), so ordering ascending by that key pushes NULLs after every non-null row, with the real column kept as the secondary key so non-null rows still sort correctly among themselves:ORDER BY ISNULL(amount) ASC, amount ASC - MongoDB — no native "nulls last"
$sortbehavior (BSON has its own type-ordering rules where null sorts before numbers); emulated with a synthetic null-bucket field added upstream in the pipeline, then sorted on the bucket first — and removed afterward, since the bucket is a compiler-internal emulation detail, not part of the row shape the grid should ever see:[ { "$addFields": { "__amountNullBucket": { "$cond": [{ "$eq": ["$amount", null] }, 1, 0] } } }, { "$sort": { "__amountNullBucket": 1, "amount": 1 } }, { "$unset": "__amountNullBucket" } ]The
$unsetMUST appear after every$sortstage that used a bucket, before the pipeline's final$skip/$limit/$group— a result row carrying__amountNullBucketalongside the real fields is a shape divergence from the Postgres/Search results for the identical IR, on top of leaking an implementation detail. If multiple sort keys each need a bucket, unset all of them in one stage ({ "$unset": ["__amountNullBucket", "__lastNameNullBucket"] }). - OpenSearch / Elasticsearch — the sort clause's native
missingparameter:{ "sort": [ { "amount": { "order": "asc", "missing": "_last" } } ] }
Must NOT.
- Must NOT rely on a dialect's implicit/default null ordering, even when it happens to match the desired output today — defaults vary across engines, across engine versions, and (for some engines) across session/locale configuration.
- Must NOT derive
nullsPositionfromsortDirectioninside the compiler (e.g. hardcoding "ASC always means nulls last") — even though that mapping happens to be this document's own pinned adapter default (above), the compiler must still read the IR'snullsPositionliterally rather than re-deriving it; the pinned mapping is the adapter's rule for producing the value, not the compiler's license to skip reading it. The IR already carriesnullsPositionexplicitly and independently for exactly this reason; a compiler that re-derives it is reintroducing the ambiguity the schema was designed to remove. - When emulating with a prefix-key trick (MySQL
ISNULL, the Mongo synthetic bucket), must NOT drop the original field as the secondary sort key — the emulation key only resolves null-vs-non-null placement, not the relative order of non-null values. - Mongo: must NOT leave a synthetic
__<field>NullBucketkey in the pipeline's output —$unsetit once it has served its$sortpurpose (see the worked example above). - Must NOT leave string-field collation implicit when sorting a case-insensitive field (SEM-1) — the same collation mechanism that governs
eq/inon that field governs itsORDER BY/$sort/sorttoo, on every backend, or row order (not just null placement) diverges across backends for identical IR.
Cross-links. $defs/sortItem, $defs/nullsPosition, $defs/sortDirection.