Skip to content

fix(deparser): quote without/nulls/format column names before types - #400

Merged
pyramation merged 4 commits into
mainfrom
devin/1795421895-quote-keyword-columns
Nov 23, 2026
Merged

pyramation merged 4 commits into
mainfrom
devin/1795421895-quote-keyword-columns

Conversation

@pyramation

@pyramation pyramation commented Nov 23, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

CREATE TABLE t ("without" time) deparsed as CREATE TABLE t (without time), which PG's lexer lookahead reads as WITHOUT TIME and fails to reparse. Same for nulls (→ NULLS FIRST/LAST) and format (→ FORMAT JSON).

Adds quoteTypedIdentifier(name, typeName): when a name is followed by a type and is one of without/nulls/format, it is always quoted; otherwise falls back to QuoteUtils.quoteIdentifier. Used for ColumnDef (CREATE TABLE, ALTER ... ADD COLUMN, pretty-printed column lists), FunctionParameter, and XMLTABLE columns. General identifier quoting is unchanged.

Tests: new kitchen-sink fixture __fixtures__/kitchen-sink/misc/keyword-column-names.sql (CREATE TABLE, arrays, ALTER ADD COLUMN, XMLTABLE, RETURNS TABLE) with regenerated generated.json and round-trip test file.


libpg-query and parser opened this PR for Dan
Implemented in session Quote keyword column names before types in deparser
Review with Devin · Open in Devin Desktop


Summary by CodeRabbit

  • Bug Fixes
    • Fixed SQL formatting for column and parameter names that match keywords such as WITHOUT, NULLS, and FORMAT. These names are now quoted appropriately in table definitions, table alterations, function declarations, and XML table column declarations, helping preserve valid SQL syntax when keyword-like names appear alongside type and clause syntax.

@luxor-code-review

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

Copy link
Copy Markdown

Review complete. 🟡 1 medium

💬 Inline comments (1)

  • 🟡 Case-sensitive check misses mixed-case Without before type — deparser.ts:2789
🧹 Nitpicks (1) — 🟢 1 low
  • 🟢 quoteTypedIdentifier ignores the type name (deparser.ts:2789) — quoteTypedIdentifier quotes without, nulls, and format whenever any typeName is present, without inspecting the type name itself (packages/deparser/src/deparser.ts:2789).

This PR fixes deparse output for columns whose names collide with PostgreSQL keywords in a pre-type context, plus the fixtures and test wiring to lock the behavior in. The fix is narrowly scoped to the deparser's identifier quoting path, with the rest of the change being generated fixture data and a new kitchen-sink test file.

Files Change
packages/deparser/src/deparser.ts Adds quoteTypedIdentifier, which quotes without/nulls/format column names when a type name follows, and wires it into the ColumnDef deparse path.
__fixtures__/kitchen-sink/misc/keyword-column-names.sql, __fixtures__/generated/generated.json Adds six fixture statements exercising the keyword-named columns.
packages/deparser/__tests__/kitchen-sink/misc-keyword-column-names.test.ts New kitchen-sink test asserting parse-deparse-parse round-trips for the six fixtures.

Reviewed commit: 07301aa

@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 uses type-aware quoting for typed column and parameter names in table creation, column alteration, function parameters, and XMLTABLE. The quote utility adds rules for without, nulls, and format. New fixtures and tests cover these cases.

Changes: Typed keyword names

Layer / File(s) Summary
Typed identifier quote rules
packages/quotes/src/quote-utils.ts, packages/quotes/__tests__/quote-utils.test.ts
quoteIdentifierBeforeType always quotes without, nulls, and format. Tests also cover regular identifier quoting behavior.
Typed deparser paths and fixture coverage
packages/deparser/src/deparser.ts, __fixtures__/kitchen-sink/misc/keyword-column-names.sql, packages/deparser/__tests__/kitchen-sink/misc-keyword-column-names.test.ts
Column definitions, both column-add paths, function parameters, and XMLTABLE columns use type-aware quoting when a type is present. Fixtures and a test cover these SQL cases.

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

Change: Bug fix

Possibly related PRs

Merge Risk: Minimal · up to bba99 · assessed changes only

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

Security architecture risk: Minimal · up to bba99

The change prevents three column or parameter names from being misread as SQL syntax before a type. It preserves ordinary identifier escaping and does not add a way to execute SQL.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — The directly changed behavior reaches callers of the new helper and consumers of SQL text from the affected deparser branches. Those branches do not execute SQL, acquire credentials, or write database state; downstream execution is outside the inspected scope.

Trust Boundaries and Controls

  • observed — At the AST-to-SQL rendering boundary, the new branch only adds delimiters for three fixed names. Every other name takes the existing quoting path. This is a lexical control, not an authorization check on the SQL statement.
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 fix (quoting without/nulls/format column names before types) and the affected area (deparser). The raw summary shows the change adds quoteIdentifierBeforeType in QuoteUtils and app…
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 fix (quoting without/nulls/format column names before types) and the affected area (deparser). The raw summary shows the change adds quoteIdentifierBeforeType in QuoteUtils and applies it in ColumnDef, AT_AddColumn, FunctionParameter, and RangeTableFuncCol. The title covers the main change, though it omits the quotes package and the test additions. The title is specific and conveys the primary change.

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: 3
  • Waived: $0.75 (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 b90ebfb and bba99ad.

Files selected for processing: 3

  • packages/deparser/src/deparser.ts
  • packages/quotes/__tests__/quote-utils.test.ts
  • packages/quotes/src/quote-utils.ts

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

@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

@luxor-code-review luxor-code-review Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Adds six keyword-named-column fixtures to the deparser kitchen-sink suite and a quoteTypedIdentifier guard in packages/deparser/src/deparser.ts that quotes without, nulls, and format column names when followed by a type name, fixing parse conflicts in deparse output.

Key findings

  • 🟡 Case-sensitive check misses mixed-case Without before type — deparser.ts:2789

Comment thread packages/deparser/src/deparser.ts Outdated
Comment on lines +2789 to +2791
if (typeName && (name === 'without' || name === 'nulls' || name === 'format')) {
return `"${name}"`;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 bug · medium

Case-sensitive check misses mixed-case Without before type

quoteTypedIdentifier compares the name exactly against lowercase 'without'/'nulls'/'format' (packages/deparser/src/deparser.ts:2789), but PostgreSQL matches keywords case-insensitively in the lexer. A column named Without, NULLS, or Format followed by a type name deparses unquoted, because the fallback QuoteUtils.quoteIdentifier also leaves unreserved keywords bare via the lowercasing keywordKindOf lookup. The mixed-case round-trip therefore hits exactly the parse conflict this helper was added to fix for lowercase names.

📋 Prompt for AI Agents

In packages/deparser/src/deparser.ts, quoteTypedIdentifier (around lines 2787-2793), change the condition name === 'without' || name === 'nulls' || name === 'format' to compare name.toLowerCase() against the same keyword set, so mixed-case identifiers like Without/NULLS/Format are also force-quoted when a typeName follows. PostgreSQL's lexer matches keywords case-insensitively, so unquoted mixed-case output reproduces the same parse failure this helper exists to prevent.

…e-keyword-columns

# Conflicts:
#	__fixtures__/generated/generated.json

@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

coderabbitai[bot]

This comment was marked as resolved.

@pyramation
pyramation merged commit 879314a 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