Skip to content

Allow DESCRIBE/SHOW metadata statements in common.ai SQLToolset#68102

Merged
kaxil merged 1 commit into
apache:mainfrom
astronomer:sqltoolset-allow-describe-show
Jun 6, 2026
Merged

Allow DESCRIBE/SHOW metadata statements in common.ai SQLToolset#68102
kaxil merged 1 commit into
apache:mainfrom
astronomer:sqltoolset-allow-describe-show

Conversation

@kaxil

@kaxil kaxil commented Jun 5, 2026

Copy link
Copy Markdown
Member

In read-only mode, the common.ai SQLToolset query tool only accepted SELECT-family statements (Select, Union, Intersect, Except). Read-only metadata statements such as DESCRIBE TABLE and SHOW TABLES / SHOW COLUMNS were rejected with SQLSafetyError. Agents frequently open with DESCRIBE to learn a table's columns, so a run that composed SELECTs directly succeeded while one that started with DESCRIBE failed outright, making agent runs nondeterministic.

The query and check_query tools now also accept read-only metadata statements (DESCRIBE/DESC and SHOW) when allow_writes=False (the default).

How it works

  • validate_sql() gains an opt-in allow_read_only_metadata flag that widens the read-only allow-list with exp.Describe and exp.Show. Only SQLToolset sets it, so LLMSQLQueryOperator keeps its SELECT-family-only contract.
  • SQLToolset passes the connection's dialect to the validator. SHOW only parses to a metadata statement on dialects that support it (Snowflake, MySQL); without a supporting dialect sqlglot falls back to a command statement that stays blocked. DESCRIBE parses to a metadata statement on every dialect.
  • The data-modifying deep scan still runs and now also rejects DDL nodes (CREATE/DROP/ALTER/TRUNCATE), so writes wrapped behind DESCRIBE/EXPLAIN (e.g. EXPLAIN DELETE ..., DESCRIBE DROP TABLE ...) remain blocked.
  • The SQLAlchemy-to-sqlglot dialect mapping is consolidated into a shared resolve_sqlglot_dialect() helper (reused by LLMSQLQueryOperator) that returns None for unknown dialects, so a misdetected dialect never breaks validation.

Usage

No API change. In the default read-only mode an agent can now run, for example, DESCRIBE TABLE my_table or (on databases that support it) SHOW COLUMNS FROM my_table.

Gotchas

  • SHOW is only recognized on databases whose dialect sqlglot supports; elsewhere it stays rejected (those databases generally do not support SHOW anyway).
  • Like SELECT, metadata statements are not scoped by allowed_tables (a documented visibility hint, not access control). Use database permissions to restrict access.

@kaxil kaxil requested a review from gopidesupavan as a code owner June 5, 2026 21:45
@kaxil kaxil force-pushed the sqltoolset-allow-describe-show branch 3 times, most recently from 5b6e8d7 to 365bfd7 Compare June 5, 2026 23:34
In read-only mode the SQLToolset query tool only accepted SELECT-family
statements, so an agent that opened with DESCRIBE (a common first move to
learn a table's columns) hard-failed with SQLSafetyError. That made agent
runs nondeterministic: a run composing SELECTs directly succeeded while one
starting with DESCRIBE failed outright.

The query and check_query tools now also accept read-only metadata statements
(DESCRIBE/DESC and SHOW) via an opt-in allow_read_only_metadata flag on
validate_sql(). The toolset passes the connection's dialect through, so SHOW is
recognized on databases that support it (Snowflake, MySQL); without a supporting
dialect it falls back to a blocked statement. Data-modifying statements stay
blocked, including ones wrapped behind DESCRIBE/EXPLAIN (e.g. EXPLAIN DELETE,
DESCRIBE DROP TABLE): the deep scan now also rejects DDL nodes that became
reachable through the metadata allowlist.

The SQLAlchemy-to-sqlglot dialect mapping is consolidated into a shared
resolve_sqlglot_dialect() helper (reused by LLMSQLQueryOperator) that returns
None for unknown dialects so a misdetected dialect never breaks validation.
@kaxil kaxil force-pushed the sqltoolset-allow-describe-show branch from 365bfd7 to e2995a8 Compare June 6, 2026 00:22
@kaxil kaxil merged commit a77dcb6 into apache:main Jun 6, 2026
94 checks passed
@kaxil kaxil deleted the sqltoolset-allow-describe-show branch June 6, 2026 01:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants