Skip to content

fix(pg-core): correct nested/partial select null-collapse heuristic o… - #6041

Open
nightshift-labs2 wants to merge 1 commit into
drizzle-team:mainfrom
nightshift-labs2:fix/nested-partial-select-left-join-null
Open

fix(pg-core): correct nested/partial select null-collapse heuristic o…#6041
nightshift-labs2 wants to merge 1 commit into
drizzle-team:mainfrom
nightshift-labs2:fix/nested-partial-select-left-join-null

Conversation

@nightshift-labs2

Copy link
Copy Markdown

…n left joins

Fixes #1603

mapResultRow (drizzle-orm/src/utils.ts) decides whether to collapse a nested/partial-select object (e.g. .select({ branding: { logo, panelBackground } })) to null by tracking, per nested object, the table name of the first field it encounters that is null. A later field from the same table only cleared that tracked state if it belonged to a different table - a non-null value from the same table never cancelled it.

As a result, if the first column of a nested group happened to be null on an otherwise real, matched left-joined row (while a later column in the same group had a real value), the whole nested object was incorrectly collapsed to null. Swapping field order masked the bug, which was the reporter's clue that the first-field check, not the join logic itself, was at fault.

Fix: also clear the nullify candidacy as soon as any field belonging to the already-tracked table is non-null, not just when a different table appears. The heuristic now only nullifies a nested object when every observed field from that table is genuinely null, matching how full (non-partial) joins already detect an absent left-joined row.

Added tests/map-result-row.test.ts as a direct regression test for mapResultRow, verified to fail against the pre-fix code and pass after the fix. Full drizzle-orm vitest suite (569 tests) passes with no other regressions.

…n left joins

Fixes drizzle-team#1603

mapResultRow (drizzle-orm/src/utils.ts) decides whether to collapse a
nested/partial-select object (e.g. .select({ branding: { logo, panelBackground } }))
to null by tracking, per nested object, the table name of the first field it
encounters that is null. A later field from the same table only cleared that
tracked state if it belonged to a *different* table - a non-null value from the
*same* table never cancelled it.

As a result, if the first column of a nested group happened to be null on an
otherwise real, matched left-joined row (while a later column in the same group
had a real value), the whole nested object was incorrectly collapsed to null.
Swapping field order masked the bug, which was the reporter's clue that the
first-field check, not the join logic itself, was at fault.

Fix: also clear the nullify candidacy as soon as any field belonging to the
already-tracked table is non-null, not just when a different table appears.
The heuristic now only nullifies a nested object when every observed field from
that table is genuinely null, matching how full (non-partial) joins already
detect an absent left-joined row.

Added tests/map-result-row.test.ts as a direct regression test for mapResultRow,
verified to fail against the pre-fix code and pass after the fix. Full drizzle-orm
vitest suite (569 tests) passes with no other regressions.
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.

[BUG]: Nested Partial Select returns null on left join if first column value is null

1 participant