Support for the explain and do keywords in Stored Procedures#37376
Conversation
There was a problem hiding this comment.
Pull request overview
This PR adds support for the EXPLAIN and DO keywords in MySQL stored procedures by updating the ANTLR grammar and adding corresponding test cases.
- Updated the
validStatementgrammar rule to includedoStatementandEXPLAINas valid statements within stored procedure bodies - Added two new test SQL cases demonstrating EXPLAIN and DO usage in stored procedures
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| parser/sql/engine/dialect/mysql/src/main/antlr4/imports/mysql/DDLStatement.g4 | Extended the validStatement rule to support doStatement and EXPLAIN keywords in procedure bodies |
| test/it/parser/src/main/resources/sql/supported/ddl/create-procedure.xml | Added test SQL cases for procedures containing EXPLAIN and DO statements |
| test/it/parser/src/main/resources/case/ddl/create-procedure.xml | Added test case definitions for the new EXPLAIN and DO procedure tests |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
-
parser/sql/engine/dialect/mysql/src/main/antlr4/imports/mysql/DDLStatement.g4:680-687: EXPLAIN is added as a bare keyword to validStatement instead of using the explain rule. Inside BEGIN ... END, EXPLAIN SELECT ... will be parsed as two statements (EXPLAIN + SELECT), allowing an EXPLAIN with no target and losing the explain semantics. Downstream rewriting/analysis will treat it as a plain query — a behavior regression.
-
parser/sql/engine/dialect/mysql/src/main/java/org/apache/shardingsphere/sql/parser/engine/mysql/visitor/statement/type/MySQLDDLStatementVisitor.java:818-844: createValidStatementSegment still only emits table/DML/SELECT nodes. Newly allowed DO/EXPLAIN would return null and be dropped, so the routine body AST will not contain DoStatement/ExplainStatement; the feature isn’t actually wired through.
-
Tests are insufficient: test/it/parser/src/main/resources/sql/supported/ddl/create-procedure.xml and .../case/ddl/create-procedure.xml only add SQL cases, with no assertions on routine-body contents, so the above semantic gaps would not be caught.
Recommendation: wire validStatement to the explain rule and include doStatement, extend the visitor to return ExplainStatement/DoStatement, and add parser assertions ensuring routine bodies contain the correct ValidStatementSegment entries.
|
@terrymanu Hi, I've already made the revisions as requested. Hope that you can review it when you have some free time. |
There was a problem hiding this comment.
Nice direction adding EXPLAIN/DO in stored procedures! Two small tweaks to keep behavior consistent and avoid a regression:
- MySQLDDLStatementVisitor.visitExplain (parser/sql/engine/dialect/mysql/src/main/java/org/apache/shardingsphere/sql/parser/engine/mysql/visitor/statement/type/MySQLDDLStatementVisitor.java): when ctx.tableName()!=null it currently returns visitChildren(ctx), yielding
a SimpleTableSegment while createValidStatementSegment expects an ExplainStatement. This will throw ClassCastException and drop semantics for EXPLAIN/DESC table inside procedures. Please mirror the DAL visitor and build a MySQLDescribeStatement (including column wildcard handling) for the table-name branch. - Coverage: add a stored-proc case with EXPLAIN/DESC table (with table name) in test/it/parser/src/main/resources/sql/supported/ddl/create-procedure.xml, and annotate the corresponding sql-statement position/type in test/it/parser/src/main/resources/case/ddl/create-procedure.xml (expect MySQLDescribeStatement), so this branch is asserted and won’t regress.
Thanks for pushing this forward—looking forward to the update!
|
@terrymanu Hi, I have made the revisions as requested. Hope that you could take a look at it when you have a moment. |
terrymanu
left a comment
There was a problem hiding this comment.
- No correctness problems spotted. The patch extends the stored-procedure validStatement grammar to accept DO and EXPLAIN (parser/sql/engine/dialect/mysql/src/main/antlr4/imports/mysql/DDLStatement.g4:683) and teaches the DDL visitor to build DoStatement/
ExplainStatement/MySQLDescribeStatement nodes for them (parser/sql/engine/dialect/mysql/src/main/java/org/apache/shardingsphere/sql/parser/engine/mysql/visitor/statement/type/MySQLDDLStatementVisitor.java:857,874), aligning stored-procedure parsing with standalone statement handling. - New parser IT cases cover EXPLAIN SELECT, DO SLEEP(...), and table DESCRIBE/EXPLAIN inside procedures (test/it/parser/src/main/resources/sql/supported/ddl/create-procedure.xml:62, test/it/parser/src/main/resources/case/ddl/create-procedure.xml:143), so the added paths are exercised.
- Looks safe to merge; it addresses the root cause (missing DO/EXPLAIN in routine-body grammar) rather than papering over it.
…ngsphere/master * remotes/origin/master: Support for the explain and do keywords in Stored Procedures (apache#37376)
Fixes #35088
Changes proposed in this pull request:
Before committing this PR, I'm sure that I have checked the following options:
./mvnw clean install -B -T1C -Dmaven.javadoc.skip -Dmaven.jacoco.skip -e.