Skip to content

fix(deparser): ALTER STATISTICS IF EXISTS / DEFAULT and quoted operator schema names - #404

Merged
pyramation merged 4 commits into
mainfrom
fix/alter-stats-operator-name
Nov 23, 2026
Merged

pyramation merged 4 commits into
mainfrom
fix/alter-stats-operator-name

Conversation

@pyramation

@pyramation pyramation commented Nov 23, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Redoes the remaining pieces of #370 fresh on main (the stats renaming part already landed in #391).

  • AlterStatsStmt: emits IF EXISTS when missing_ok, and SET STATISTICS DEFAULT when stxstattarget is absent (PG 18 represents DEFAULT as a null target). Previously both were dropped.
  • deparseOperatorName: qualified operators now go through the existing qualifiedOperatorName helper (schema parts via QuoteUtils.quoteIdentifier, operator symbol as is) instead of a raw join('.'), so OPERATOR("My Schema".+) no longer deparses to OPERATOR(My Schema.+).
ALTER STATISTICS IF EXISTS s SET STATISTICS DEFAULT;   -- was: ALTER STATISTICS s SET STATISTICS
SELECT 1 OPERATOR("My Schema".+) 2;                    -- was: OPERATOR(My Schema.+)

Fixtures: __fixtures__/kitchen-sink/misc/{alter-statistics,operator-names}.sql with regenerated generated.json and kitchen-sink tests. Two regression-corpus cases now pass and were removed from known-failures.deparser.json.

Not in scope: separate pre-existing quoting bugs in ORDER BY … USING and exclusion-constraint operators.


libpg-query and parser opened this PR for Dan
Implemented in session Redo ALTER STATISTICS and operator name deparsing
Review with Devin · Open in Devin Desktop


Summary by CodeRabbit

  • Bug Fixes
    • SQL output now preserves qualified operator names, including operators with quoted or escaped schema names and operators used with ANY or ALL.
    • ALTER STATISTICS output now correctly includes IF EXISTS and DEFAULT where specified, including when the statistics target is zero.
  • Compatibility
    • PostgreSQL 16 ALTER STATISTICS statements now retain their target values, including zero and negative values, when transformed for PostgreSQL 17.

@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 commented Nov 23, 2026 •

Copy link
Copy Markdown

Review complete. No issues found — approved ✅.


The change extends the deparser with correct handling of two SQL constructs that previously failed to deparse. The operator-name path distinguishes single-part names (rendered raw, since PG operator symbols are never quoted) from multi-part names (schema parts quoted, emitted via the OPERATOR(schema.op) grammar), and the AlterStatsStmt handler fixes explicit stxstattarget = 0 being skipped and renders absent targets as DEFAULT.

Files Change
packages/deparser/src/deparser.ts Adds qualified-operator-name rendering and ALTER STATISTICS ... SET STATISTICS deparsing with correct IF EXISTS and DEFAULT/zero handling
__fixtures__/kitchen-sink/misc/operator-names.sql, alter-statistics.sql + generated fixtures New fixture SQL covering operator names and ALTER STATISTICS
packages/deparser/__tests__/kitchen-sink/misc-*.test.ts Generated round-trip tests for the new fixtures
__fixtures__/regress/known-failures.deparser.json Removes entries now passing

Reviewed commit: 0caf67d

@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 formats qualified operator names and emits IF EXISTS, zero targets, and DEFAULT in ALTER STATISTICS output under the specified conditions. The PostgreSQL 16-to-17 transformer converts ALTER STATISTICS nodes to the PostgreSQL 17 shape. Fixtures and tests cover these changes.

Changes: Qualified operator names

Layer / File(s) Summary
Operator formatting and fixtures
packages/deparser/src/deparser.ts, __fixtures__/kitchen-sink/misc/operator-names.sql, packages/deparser/__tests__/kitchen-sink/misc-operator-names.test.ts, __fixtures__/regress/known-failures.deparser.json
Qualified operator names use OPERATOR(...). Single operator names and qualified-name parts fall back to String.str when sval is unavailable. Fixtures cover quoted schemas and ANY/ALL operands. The unusual-operator known-failure mapping is removed.

ALTER STATISTICS output and AST conversion

Layer / File(s) Summary
ALTER STATISTICS formatting and fixtures
packages/deparser/src/deparser.ts, __fixtures__/kitchen-sink/misc/alter-statistics.sql, packages/deparser/__tests__/kitchen-sink/misc-alter-statistics.test.ts, __fixtures__/regress/known-failures.deparser.json
ALTER STATISTICS output includes IF EXISTS when missing_ok is true. It emits a target of zero and emits DEFAULT when the target is nullish. Fixtures cover quoted, schema-qualified, and unqualified names. One stats_ext known-failure entry is removed.
PostgreSQL 16-to-17 AST conversion
packages/transform-ast/src/transformers/v16-to-v17.ts, packages/transform-ast/__tests__/direct-transformers.test.ts
The transformer converts stxstattarget to an Integer node and defaults a missing or nullish target to zero. Tests compare transformed ASTs with PostgreSQL 17 parser output for targets 0, 42, and -1.

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

Change: Bug fix

Possibly related PRs

  • constructive-io/pgsql-parser#391: Adds deparsing for renaming statistics objects; this PR changes separate ALTER STATISTICS statement handling as part of the same statistics work.
Merge Risk: Minimal · up to af7c3 · assessed changes only

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

Security architecture risk: Minimal · up to af7c3

The changes correct SQL rendering and preserve numeric statistics targets during version conversion. They change what callers receive, but not who can execute SQL or what permissions they have. No material security or failure-containment regression was identified in the changed paths.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — The demonstrated scope is callers that render the affected AST shapes or consume the converted statistics node. Any database effect requires a downstream caller to execute the returned SQL under its own database permissions; that downstream scope was not established.

Trust Boundaries and Controls

  • observed — Schema components go through identifier quoting; the final operator component does not. The new legacy field fallback also applies to existing operator-definition and comment renderers, but the raw token position already accepted the current field. The helper is not a validator for arbitrary caller-supplied ASTs.
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 fixes that the changeset implements: AlterStatsStmt now emits IF EXISTS and DEFAULT, and qualified operator names use the qualifiedOperatorName helper so schema identifiers get quo…
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 fixes that the changeset implements: AlterStatsStmt now emits IF EXISTS and DEFAULT, and qualified operator names use the qualifiedOperatorName helper so schema identifiers get quoted. The title is specific and matches the main changes in deparser.ts, the fixtures, the tests, and the transformer. It is long at 84 characters, but it stays on topic.

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 ae79e51 and af7c39e.

Files ignored due to path filters: 2

  • __fixtures__/generated/generated.json is excluded by !**/generated/**, !__fixtures__/generated/**
  • packages/pgsql-types/src/field-metadata.json is excluded by !packages/pgsql-types/src/field-metadata.json
Files selected for processing: 2

  • __fixtures__/regress/known-failures.deparser.json
  • packages/deparser/src/deparser.ts

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

@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 found 1 finding. Devin automatically fixed it.

Fixed by Devin (1)
  • Legacy statistics zero becomes DEFAULT — v16-to-v17 now maps the legacy int target (absent = 0) to an Integer node, matching how PG17 parses SET STATISTICS 0, so DEFAULT is only emitted for a genuinely absent PG17+ target; covered by a direct transformer test.

View findings in Devin Review · Follow Devin's work

Devin Review

@pyramation
pyramation merged commit 9bcb8cd 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