Skip to content

fix(deparser): ORDER BY USING OPERATOR(...) and keep pg_catalog."char" - #412

Merged
devin-ai-integration[bot] merged 2 commits into
mainfrom
fix/deparser-order-by-using-char
Nov 28, 2026
Merged

devin-ai-integration[bot] merged 2 commits into
mainfrom
fix/deparser-order-by-using-char

Conversation

@pyramation

@pyramation pyramation commented Nov 28, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Two deparser bugs that broke round-trips:

  • ORDER BY x USING pg_catalog.< was emitted verbatim, which is a syntax error. SortBy.useOp now goes through the existing operator formatter: qualified → USING OPERATOR(pg_catalog.<), bare → USING <.
  • pg_catalog."char" lost its schema ('x'::char), so it reparsed as bpchar. Internal "char" is now quoted via QuoteUtils, keeps its schema when present (qualified casts use the existing CAST(... AS pg_catalog."char") form), and keeps array bounds.

Tests: kitchen-sink fixtures __fixtures__/kitchen-sink/misc/{operator-names,typecast}.sql with regenerated round-trip suites, plus an exact-output assertion in packages/deparser/__tests__/misc/pg-catalog.test.ts.

Before the fix (commit 7d17c3f, tests only)

❌ AST_MISMATCH: misc/typecast-18.sql
❌ DEPARSED SQL: SELECT 'x'::char
❌ DEPARSED SQL: SELECT x FROM ( VALUES (1), (2) ) AS t(x) ORDER BY x USING pg_catalog.<
PARSE ERROR: syntax error at or near "pg_catalog"
Test Suites: 3 failed, 3 total

After (commit 1825d93)

pgsql-deparser:   Test Suites: 341 passed; Tests: 1312 passed
plpgsql-deparser: Test Suites: 11 passed;  Tests: 127 passed

Regress known-failures ratchets unchanged.


libpg-query and parser opened this PR for Dan
Implemented in session Fix deparser ORDER BY USING and "char" output
Review with Devin · Open in Devin Desktop


Summary by CodeRabbit

  • Bug Fixes
    • Improved SQL output for char type casts and column definitions, preserving the correct quoting for qualified and unqualified type names.
    • Corrected formatting of ORDER BY clauses that use operator-qualified ordering operators, including cases with NULLS FIRST or NULLS LAST.
    • Added coverage for these SQL formatting cases.

@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

@coderabbitai

coderabbitai Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The deparser now quotes char type names in additional contexts and uses deparseOperatorName for SORTBY_USING operators. New SQL fixtures and tests cover these formatting cases.

Changes: Type name formatting

Layer / File(s) Summary
Format char type names
packages/deparser/src/deparser.ts, __fixtures__/kitchen-sink/misc/typecast.sql, packages/deparser/__tests__/kitchen-sink/misc-typecast.test.ts, packages/deparser/__tests__/misc/pg-catalog.test.ts
TypeName quotes char in all contexts and quotes the type in two-part pg_catalog names. Fixtures and tests cover casts and column definitions using qualified and unqualified "char".

ORDER BY operator formatting

Layer / File(s) Summary
Format ORDER BY operators
packages/deparser/src/deparser.ts, __fixtures__/kitchen-sink/misc/operator-names.sql, packages/deparser/__tests__/kitchen-sink/misc-operator-names.test.ts
SortBy uses deparseOperatorName for SORTBY_USING. Fixtures cover qualified and unqualified operators, including NULLS FIRST and NULLS LAST.

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

Change: Bug fix

Possibly related PRs

Suggested reviewers: luxor-code-review

Merge Risk: Minimal · up to 1825d · assessed changes only

Path filters excluded changed files. This rating applies only to the assessed changes.

Security architecture risk: Minimal · up to 1825d

The changes correct how SQL is written without adding a way to execute it or changing who can access data. The reviewed change did not establish a new security risk.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — The directly affected surface is callers that consume SQL rendered from the affected AST forms. The reviewed change does not add an entrypoint or extend database privileges; downstream execution contexts were not established.

Trust Boundaries and Controls

  • observed — Operator formatting is not input validation. The base already emitted operator symbol text directly; the head still does so while adding identifier quoting for schema components of qualified names.
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 two changes: ORDER BY USING OPERATOR(...) handling and keeping pg_catalog."char" in the deparser. The changeset matches both. The deparser.ts diff routes SortBy.useOp through deparseOp…
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 two changes: ORDER BY USING OPERATOR(...) handling and keeping pg_catalog."char" in the deparser. The changeset matches both. The deparser.ts diff routes SortBy.useOp through deparseOperatorName and changes how TypeName quotes char and pg_catalog.char. The fixtures and tests cover the same two areas. The title is specific and describes the primary changes, though it lists two topics in one line.

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

Usage-based review receipt

  • Mode: Continue automatically
  • Reviewed files: 6
  • Waived: $1.50 (charged $0.00)
  • View usage details

Note

This review exceeded your plan’s limits and used usage-based reviews—free during trial, billed after paid activation unless disabled. Manage usage-based reviews.

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between a5a946f and 1825d93.

Files ignored due to path filters: 1

  • __fixtures__/generated/generated.json is excluded by !**/generated/**, !__fixtures__/generated/**
Files selected for processing: 6

  • __fixtures__/kitchen-sink/misc/operator-names.sql
  • __fixtures__/kitchen-sink/misc/typecast.sql
  • packages/deparser/__tests__/kitchen-sink/misc-operator-names.test.ts
  • packages/deparser/__tests__/kitchen-sink/misc-typecast.test.ts
  • packages/deparser/__tests__/misc/pg-catalog.test.ts
  • packages/deparser/src/deparser.ts

Limit details: You’ve used all 10 included reviews currently available.

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Devin Review

@devin-ai-integration
devin-ai-integration Bot merged commit 4d1dc95 into main Nov 28, 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