Skip to content

fix(deparser): choose TypeCast syntax from the parse tree - #398

Merged
pyramation merged 1 commit into
mainfrom
feat/typecast-parse-tree
Nov 23, 2026
Merged

pyramation merged 1 commit into
mainfrom
feat/typecast-parse-tree

Conversation

@pyramation

@pyramation pyramation commented Nov 23, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Fresh, minimal redo of #235 on current main.

TypeCast deparsing now picks the cast form (expr::type vs CAST(expr AS type), and whether the argument needs parentheses) from the argument's AST node type instead of inspecting the deparsed string, and detects a qualified pg_catalog.bpchar from the TypeName.names nodes. The existing built-in type spelling policy is unchanged.

Tests: packages/deparser/__tests__/misc/typecast.test.ts adds 17 PG 18 cases asserting both emitted SQL and AST round-trip.

Left out from #235:

  • Its larger test file and long JSDoc, replaced by focused cases.
  • Its unary-minus expectations, which were wrong for PG 18: -1::integer parses as negation of a cast, (-1)::integer as a cast of a negative constant; the new tests assert the actual parse.

No version changes. #235 will be closed pointing here once this merges.


libpg-query and parser opened this PR for Dan
Implemented in session Redo parse-tree-based TypeCast deparsing
Review with Devin · Open in Devin Desktop


Summary by CodeRabbit

  • Bug Fixes
    • Improved SQL output for type casts, including negative numeric values, expressions, and schema-qualified types. Casts involving pg_catalog.bpchar and function calls now use appropriate SQL syntax.

@luxor-code-review

luxor-code-review Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Review complete. No issues found — approved ✅.


Refines the deparser's TypeCast handling: a new isNegativeConstant helper detects signed A_Const values so bare :: syntax is not emitted where a leading minus would bind outside the cast, and the bpchar special case is narrowed to qualified pg_catalog.bpchar, falling back to CAST(...) otherwise. New tests cover casts of strings, negative integers/numerics, function calls, and bpchar variants.

Files Change
packages/deparser/src/deparser.ts Adds isNegativeConstant and simplifies TypeCast to choose :: vs CAST syntax based on argument shape and pg_catalog qualification.
packages/deparser/__tests__/misc/typecast.test.ts New round-trip test suite covering 17 cast forms including negative constants and bpchar typmod/qualified variants.

Reviewed commit: 8fc8028

@coderabbitai

coderabbitai Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The deparser now detects negative constants and selects cast syntax based on the type and expression shape. Parameterized tests check deparsed output for text and numeric casts, expressions, pg_catalog.bpchar, and schema-qualified types.

Changes: Type cast deparsing

Layer / File(s) Summary
Cast syntax selection
packages/deparser/src/deparser.ts, packages/deparser/__tests__/misc/typecast.test.ts
TypeCast retains CAST(...) for qualified pg_catalog.bpchar casts and negative constants. Catalog-type casts of function calls use parenthesized postfix syntax. Columns and nonnegative constants use postfix casts directly. Parameterized tests check deparsed output across cast cases.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Suggested reviewers: luxor-code-review

Merge Risk: Minimal · up to 8fc80

This change makes the deparser choose cast syntax from the parse tree instead of from rendered text. It adds tests for negative constants, bpchar and schema-qualified types. No concrete merge-blocking risk was found.

Security architecture risk: Minimal · up to 8fc80

The deparser now uses the shape of the parsed expression to choose cast syntax. This changes formatting without adding an input path, granting privileges, or executing SQL.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: TypeCast deparsing now picks its syntax (postfix :: versus CAST(... AS ...)) from the parse tree. The summary and objectives confirm this is the core change. The t…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: TypeCast deparsing now picks its syntax (postfix :: versus CAST(... AS ...)) from the parse tree. The summary and objectives confirm this is the core change. The title uses the conventional prefix and stays specific, so it is related to the changeset. The title is a single sentence of 57 characters and 8 words, which is concise.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 890b03b and 8fc8028.

Files selected for processing: 2

  • packages/deparser/__tests__/misc/typecast.test.ts
  • packages/deparser/src/deparser.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 3 remain after this review.

@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

@pyramation
pyramation merged commit 6d8b46f into main Nov 23, 2026
23 checks passed
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