Skip to content

ClickHouse dialect: SHOW and EXPLAIN parse as Command (fallback for unknown syntax) #7548

Description

@borchkhadzetornike

Summary

In the clickhouse dialect, SHOW and EXPLAIN statements parse to exp.Command — sqlglot's catch-all for syntax it can't recognise — rather than to dedicated AST nodes. This makes them indistinguishable from genuinely-unknown statements like SYSTEM, RENAME, ATTACH, OPTIMIZE, etc., which also fall back to Command.

Reproduction (sqlglot 30.6.0)

import sqlglot

for s in ["SHOW TABLES",
          "SHOW CREATE TABLE t",
          "EXPLAIN SELECT 1",
          "EXPLAIN ESTIMATE SELECT 1"]:
    parsed = sqlglot.parse(s, dialect="clickhouse")
    print(s, "->", [type(p).__name__ for p in parsed])

Output:

'SHOW TABLES' contains unsupported syntax. Falling back to parsing as a 'Command'.
'SHOW CREATE TABLE t' contains unsupported syntax. Falling back to parsing as a 'Command'.
'EXPLAIN SELECT 1' contains unsupported syntax. Falling back to parsing as a 'Command'.
'EXPLAIN ESTIMATE SELECT 1' contains unsupported syntax. Falling back to parsing as a 'Command'.

SHOW TABLES -> ['Command']
SHOW CREATE TABLE t -> ['Command']
EXPLAIN SELECT 1 -> ['Command']
EXPLAIN ESTIMATE SELECT 1 -> ['Command']

For comparison, several other dialects do produce dedicated nodes for SHOW / EXPLAIN (e.g. exp.Show, exp.Explain).

Why it matters

In a downstream project we use sqlglot's ClickHouse dialect to validate that user-submitted SQL is read-only — accept SELECT / DESCRIBE, reject everything else. Because SHOW and EXPLAIN parse to the same Command node as SYSTEM / RENAME / ATTACH / OPTIMIZE, we can't safely allow them: accepting Command would also accept the write-capable statements. So we currently reject SHOW and EXPLAIN despite ClickHouse considering them read-only operations.

Folks doing access-control / SQL-firewall use cases with the ClickHouse dialect probably hit the same wall.

Suggested fix direction

Add ClickHouse-dialect parser support for SHOW and EXPLAIN so they produce exp.Show / exp.Explain (or dialect-specific subclasses) instead of Command. Both statements are well-defined in the ClickHouse docs — the grammar shouldn't be too painful, and dedicated nodes would unblock several validation/transformation use cases.

Happy to put up a PR if a maintainer can sketch the preferred shape.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions