Skip to content

fix(transform): normalize nodes under a "constructor" key in normalizeTree - #367

Open
pyramation wants to merge 1 commit into
mainfrom
devin/1792900076-normalize-constructor-key
Open

pyramation wants to merge 1 commit into
mainfrom
devin/1792900076-normalize-constructor-key

Conversation

@pyramation

Copy link
Copy Markdown
Collaborator

Summary

normalizeTree (used by cleanTree and validateRoundTrip) found its handler with keyHandlers?.[key]. For the AST key constructor, used by JsonArrayAgg, JsonObjectAgg and other SQL/JSON aggregate nodes, that lookup returned the inherited Object.prototype.constructor. The code then called Object(node[key], recurse), which returns the subtree without normalizing it. As a result, location fields under constructor were never stripped, so validateRoundTrip / transformSql({ roundTrip: true }) reported false mismatches whenever whitespace moved them:

SELECT   JSON_ARRAYAGG(  t.name ORDER BY t.name ) FROM "my-schema".items t;
-- round-trip validation failed: ... JsonArrayAgg.constructor.agg_order[0].SortBy.node.ColumnRef.location (41 vs 37)

Fix: look up only the handler's own key (Object.prototype.hasOwnProperty.call(keyHandlers, key)).

Tests

New transform fixture __fixtures__/input/json-constructors.sql, with its golden output generated by pnpm fixtures:

  • Before the fix: ✕ passes round-trip validation (AST1 === AST2) (the mismatch above)
  • After the fix: @pgsql/transform has 255/255 tests passing and 4/4 snapshots.

Also present in @pgsql/transform 18.18.0, so this is not a regression in 18.18.1.

Link to Devin session: https://app.devin.ai/sessions/f8a1d342fa3b4580a90de9031ed4bd86
Open in Devin Desktop: https://app.devin.ai/desktop/session/f8a1d342fa3b4580a90de9031ed4bd86?variant=devin
Requested by: @pyramation

@devin-ai-integration

Copy link
Copy Markdown
Contributor

I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".

  • Disable automatic comment, CI, and merge conflict monitoring

@tenki-reviewer

tenki-reviewer Bot commented Oct 25, 2026 •

Copy link
Copy Markdown

Review complete. No issues found — approved ✅.


This PR extends the transform package's round-trip validation coverage to PostgreSQL JSON constructor statements: JSON_ARRAY, JSON_OBJECT, JSON_ARRAYAGG, and JSON_OBJECTAGG. To make these statements round-trip, the core comparison logic was adjusted for constructor-specific AST keys.

Files Change
packages/transform/src/round-trip-core.ts Adjusts round-trip comparison handling for JSON constructor AST keys so constructor statements validate cleanly.
packages/transform/__fixtures__/input/json-constructors.sql Adds input fixture covering JSON constructor forms, including qualified function calls and key:value pairs.
packages/transform/__fixtures__/output/json-constructors.sql Adds the corresponding expected deparsed output fixture.

Reviewed commit: 9aa53d8

@devin-ai-integration

Copy link
Copy Markdown
Contributor

Folded into #371

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