fix(traverse): dispatch visitors for untagged typed embedded fields#313
Merged
Conversation
…ia runtime schema
Contributor
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
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.
Summary
walk()used the runtime schema (src/18/runtime-schema.ts) for descent but not for dispatch: concrete typed embedded fields (e.g.CreatePolicyStmt.table: RangeVar) are bare untagged objects in libpg_query JSON ({schemaname, relname, ...}— no{RangeVar: {...}}wrapper), so the single-capitalized-key tag detection never fired and theRangeVarvisitor was never called for them. Concretely, walkingCREATE POLICY p ON app.posts FOR SELECT USING (true);with aRangeVarvisitor never visitedapp.posts.Fix — synthesize the tag from the runtime schema when descending into a concrete typed field with an untagged value:
walkNode(extracted from the old tagged branch) constructs theNodePath, honors thefalse= skip-children return contract, and continues descending using the node's ownNodeSpec. Both array (field.isArray, e.g.ParseResult.stmts: RawStmt[]) and scalar cases are handled.visit()had the same gap (it only recursed into single-key objects); it now consults the runtime schema per field and synthesizes the type for untagged concrete-typed values, both scalar and array.Affected typed fields include
CreatePolicyStmt.table,IndexStmt.relation,CreateTrigStmt.relation/.constrrel,AlterTableStmt.relation,IntoClause.rel,ColumnDef.typeName,ParseResult.stmts, etc.Impact
Visitors now fire on nodes they previously never saw — downstream visitors will see additional (correct) node visits (e.g.
RangeVarfor policy/index/trigger/ALTER TABLE targets). In-repo consumers audited:plpgsql-parser(walkSqlExpressions/sqlVisitoruses@pgsql/traversewalk, benefits directly; all 303 tests pass);@pgsql/transformandpgsql-deparserdo not use traverse. Downstream workarounds like manual per-node extraction in constructive-db's facts classifier can be removed.Tests
New
packages/traverse/__tests__/typed-fields.test.ts(real libpg_query PG18 AST shapes):CreatePolicyStmt.table,IndexStmt.relation,CreateTrigStmt.relation,AlterTableStmt.relation,ColumnDef.typeName, array case (ParseResult.stmts→RawStmt),false-return skip-children on a synthesized tag, and tagged-node behavior unchanged — for bothwalkandvisit. Full workspacepnpm run build && pnpm run testpasses (deparser 714, transform 1437, plpgsql-parser 303, traverse 23, etc.).Link to Devin session: https://app.devin.ai/sessions/0ce0d0a13c624fb8b8eb22c77d448fb6
Requested by: @pyramation