Add whitelist to block non-query statements in unified SQL path#5330
Conversation
Validate parsed SQL statements against Calcite's SqlKind.QUERY set before planning, rejecting DML (INSERT, DELETE, UPDATE, MERGE) and DDL statements. This brings the unified path in line with the legacy grammar's parser-level restriction to read-only queries. Whitelist: SqlKind.QUERY (SELECT, UNION, INTERSECT, EXCEPT, VALUES, WITH, ORDER_BY, EXPLICIT_TABLE). Note: EXPLAIN is also blocked because Calcite's SqlToRelConverter does not handle SqlExplain in convertQueryRecursive. Supporting EXPLAIN requires unwrapping the inner query and formatting the plan separately. Signed-off-by: Chen Dai <daichen@amazon.com>
PR Reviewer Guide 🔍Here are some key observations to aid the review process:
|
PR Code Suggestions ✨Explore these optional code suggestions:
|
| SqlNode parsed = planner.parse(query); | ||
| if (!parsed.isA(SqlKind.QUERY)) { | ||
| throw new UnsupportedOperationException( | ||
| "Only query statements are supported. Got: " + parsed.getKind()); |
There was a problem hiding this comment.
nit (clarity): Would a user know what this means?
Intuitively I'm guessing "query" means "non-modifying & non-explain," so maybe something like "Only read-only querying statements are supported, got an {explain} query" with some case-based substitution.
There was a problem hiding this comment.
Good point on clarity. But I think EXPLAIN, SHOW, and DESCRIBE are also read-only and not supported here either? I'll give more careful thought for the wording in this new api module later. Thanks!
Description
The unified SQL path uses Calcite's
SqlParser.parseQuery()which, despite its name, accepts any SQL statement type including DML and DDL. The SQL V2 grammar restricts input toSELECT/SHOW/DESCRIBEat the parser level, but the unified path had no equivalent protection.Changes
Validate the parsed
SqlNodeagainstSqlKind.QUERYinCalciteNativeStrategy.plan()before proceeding to planning. Non-query statements are rejected withUnsupportedOperationException./_explainpathRelated Issues
Part of #5248
Check List
--signoffor-s.By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.