fix(pg-core): correct nested/partial select null-collapse heuristic o… - #6041
Open
nightshift-labs2 wants to merge 1 commit into
Open
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
…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.