Skip to content

PostgreSQL: Add support for CREATE STATISTICS - #3040

Open
lei-jia-xing wants to merge 3 commits into
apache:mainfrom
lei-jia-xing:postgres-create-statistics
Open

lei-jia-xing wants to merge 3 commits into
apache:mainfrom
lei-jia-xing:postgres-create-statistics

Conversation

@lei-jia-xing

Copy link
Copy Markdown

Closes #3035.

CREATE STATISTICS s ON a, b FROM t; now parses in the PostgreSQL and Generic dialects. The AST preserves the statistics name, kinds, expressions, and target table. Tests cover SQL round trips and a missing FROM clause.

PostgreSQL syntax reference

AI assistance was used to draft the implementation and tests.

@LucaCappelletti94 LucaCappelletti94 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.

Please add the fuzzer seeds, then run the differential fuzzer harness with pg_query to identify whether you are fully covering this syntax. Do ping me when that is done.

@LucaCappelletti94 LucaCappelletti94 added enhancement New feature or request api change waiting on contributor The review needs further refinements by its author labels Nov 24, 2026
@lei-jia-xing
lei-jia-xing force-pushed the postgres-create-statistics branch from 60c3676 to 032b489 Compare November 25, 2026 01:58
@lei-jia-xing

lei-jia-xing commented Nov 25, 2026 •

Copy link
Copy Markdown
Author

@LucaCappelletti94 Fixed two gaps against PostgreSQL's gram.y https://github.com/postgres/postgres/blob/REL_17_7/src/backend/parser/gram.y#L4593-L4620:

fuzz test passed. Ready to review

@codecov-commenter

codecov-commenter commented Nov 25, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 83.61%. Comparing base (c3f0d8e) to head (e2792ea).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3040      +/-   ##
==========================================
+ Coverage   83.57%   83.61%   +0.03%     
==========================================
  Files          42       42              
  Lines       35719    35787      +68     
  Branches    35719    35787      +68     
==========================================
+ Hits        29853    29922      +69     
+ Misses       2425     2424       -1     
  Partials     3441     3441              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment thread tests/sqlparser_postgres.rs Outdated
"CREATE STATISTICS s ON row_number() OVER () FROM t",
"CREATE STATISTICS IF NOT EXISTS ON a FROM t",
] {
pg_and_generic()

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.

Actually assert against the expected error type so we avoid these strings failing on the wrong errors.

@lei-jia-xing
lei-jia-xing force-pushed the postgres-create-statistics branch from 4295f97 to e2792ea Compare November 25, 2026 12:31
@lei-jia-xing

Copy link
Copy Markdown
Author

@LucaCappelletti94 impove the code coverage & do assert like previous test

Comment thread src/parser/mod.rs
} else if self.dialect.supports_create_statistics()
&& self.parse_keyword(Keyword::STATISTICS)
{
if has_modifier {

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.

Could you please open an issue / if you have time also a PR to try and refactor these has_modifier checks into a single standardized error? I see it repeats several times.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Ah, I see.There were already three has_modifier guards; this PR just adds a fourth. I'll open a tracking issue to consolidate them into one helper with a single standardized error, and send the refactor as a separate PR after this PR lands.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

But the guard is still opt-in: CREATE TEMP POLICY/DOMAIN/RULE parse and lose TEMP on Display. Should the tracking issue cover that structural fix too, or just the helper?

@lei-jia-xing lei-jia-xing Nov 25, 2026 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I dug into the has_modifier guard a bit more. It's a single bool checked only in four branches (STATISTICS, USER MAPPING, FOREIGN TABLE, FOREIGN DATA WRAPPER). Other branches whose AST cannot store modifiers don't check it, and silently drop them

@lei-jia-xing lei-jia-xing Nov 25, 2026 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

for a such illegal test.sql

CREATE TEMP POLICY p ON t USING (true);

run with

cargo run --quiet --example cli -- ./test.sql --postgres

Round-trip:
'CREATE POLICY p ON t USING (true)'
It parses and remove the temp. But it can't be parsed

https://www.postgresql.org/docs/17/sql-createpolicy.html

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Hi @LucaCappelletti94, I've opened a follow-up issue to track this.

Before opening a PR, could you please let me know if the CreateModifiers struct approach (consuming valid modifiers per target and rejecting any unconsumed ones via ensure_empty()) aligns with what you have in mind?

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.

Yes a struct approach is most likely the cleanest refactoring.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api change enhancement New feature or request PostgreSQL waiting on reviewer PR needs to be reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PostgreSqlDialect rejects PostgreSQL's CREATE STATISTICS statement for defining extended statistics on a table.

3 participants