Skip to content

Parse PostgreSQL parenthesized EXPLAIN ( option [, ...] )#4

Open
alexey-milovidov wants to merge 1 commit into
mainfrom
pg-explain-options
Open

Parse PostgreSQL parenthesized EXPLAIN ( option [, ...] )#4
alexey-milovidov wants to merge 1 commit into
mainfrom
pg-explain-options

Conversation

@alexey-milovidov

Copy link
Copy Markdown
Member

What

Adds support for PostgreSQL's parenthesized EXPLAIN option syntax:

EXPLAIN (VERBOSE, COSTS OFF, FORMAT JSON) SELECT ...
EXPLAIN (ANALYZE, BUFFERS) SELECT ...

This form is also accepted by the PostgreSQL-compatible dialects CockroachDB, RisingWave and Materialize.

Why

Before this change the parenthesized form failed to parse:

Parse error at line 1, column 10: Expected identifier, got "LParen"

While transpiling PostgreSQL's regression test suite (src/test/regress, ~53k statements) to ClickHouse via transpile, this single gap caused the majority of EXPLAIN transpilation errors (~2.7k statements): every EXPLAIN (...) in the suite failed before its inner statement could be reached.

How

The options (ANALYZE, VERBOSE, COSTS, BUFFERS, SETTINGS, WAL, TIMING, SUMMARY, MEMORY, GENERIC_PLAN, SERIALIZE, FORMAT) are planner/output directives with no cross-dialect meaning, so they are parsed and discarded — consistent with the existing normalization of EXPLAIN to DESCRIBE. The ANALYZE option is folded into the DESCRIBE style so that EXPLAIN (ANALYZE) ... behaves like bare EXPLAIN ANALYZE .... A parenthesized subquery (EXPLAIN (SELECT ...)) is still parsed as a subquery target, not an option list.

The new parsing is gated to the PostgreSQL-family dialects (PostgreSQL, CockroachDB, RisingWave, Materialize), so no other dialect's behavior changes.

Tests

Adds crates/polyglot-sql/tests/postgres_explain_options.rs (7 tests) covering option discard, ANALYZE folding (including ANALYZE false/off), subquery disambiguation, PostgreSQL→ClickHouse transpilation, and the PG-compatible dialects. The full polyglot-sql suite passes; the only failing test, tpch_query2_transpile_postgres_to_fabric, is pre-existing on main and unrelated to this change.


Upstream PR: tobilg#315

PostgreSQL and the PostgreSQL-compatible dialects CockroachDB, RisingWave
and Materialize accept the parenthesized EXPLAIN option form:

    EXPLAIN (VERBOSE, COSTS OFF, FORMAT JSON) SELECT ...

Previously this failed to parse ("Expected identifier, got LParen"), which
blocked a large fraction of real PostgreSQL EXPLAIN statements from being
transpiled.

The options are planner/output directives with no cross-dialect meaning, so
they are parsed and discarded here - consistent with the existing
normalization of EXPLAIN to DESCRIBE. The ANALYZE option is folded into the
DESCRIBE style so that "EXPLAIN (ANALYZE) ..." matches bare
"EXPLAIN ANALYZE ...". A parenthesized subquery ("EXPLAIN (SELECT ...)") is
still parsed as a subquery target.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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