Craticula

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":

Must NOT.

Cross-links. $defs/sortItem, $defs/nullsPosition, $defs/sortDirection.