Skip to content

Support for the explain and do keywords in Stored Procedures#37376

Merged
terrymanu merged 4 commits into
apache:masterfrom
ccxxxyy:issueNo
Dec 15, 2025
Merged

Support for the explain and do keywords in Stored Procedures#37376
terrymanu merged 4 commits into
apache:masterfrom
ccxxxyy:issueNo

Conversation

@ccxxxyy

@ccxxxyy ccxxxyy commented Dec 13, 2025

Copy link
Copy Markdown
Contributor

Fixes #35088

Changes proposed in this pull request:


Before committing this PR, I'm sure that I have checked the following options:

  • My code follows the code of conduct of this project.
  • I have self-reviewed the commit code.
  • I have (or in comment I request) added corresponding labels for the pull request.
  • I have passed maven check locally : ./mvnw clean install -B -T1C -Dmaven.javadoc.skip -Dmaven.jacoco.skip -e.
  • I have made corresponding changes to the documentation.
  • I have added corresponding unit tests for my changes.
  • I have updated the Release Notes of the current development version. For more details, see Update Release Note

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 validStatement grammar rule to include doStatement and EXPLAIN as 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.

Comment thread parser/sql/engine/dialect/mysql/src/main/antlr4/imports/mysql/DDLStatement.g4 Outdated
Comment thread parser/sql/engine/dialect/mysql/src/main/antlr4/imports/mysql/DDLStatement.g4 Outdated
Comment thread test/it/parser/src/main/resources/case/ddl/create-procedure.xml Outdated
Comment thread test/it/parser/src/main/resources/case/ddl/create-procedure.xml Outdated

@terrymanu terrymanu left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • 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.

@ccxxxyy
ccxxxyy marked this pull request as draft December 14, 2025 07:24
@ccxxxyy
ccxxxyy marked this pull request as ready for review December 14, 2025 07:47
@ccxxxyy

ccxxxyy commented Dec 14, 2025

Copy link
Copy Markdown
Contributor Author

@terrymanu Hi, I've already made the revisions as requested. Hope that you can review it when you have some free time.

@ccxxxyy
ccxxxyy requested a review from terrymanu December 14, 2025 09:02

@terrymanu terrymanu left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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!

@ccxxxyy

ccxxxyy commented Dec 15, 2025

Copy link
Copy Markdown
Contributor Author

@terrymanu Hi, I have made the revisions as requested. Hope that you could take a look at it when you have a moment.

@ccxxxyy
ccxxxyy requested a review from terrymanu December 15, 2025 02:19

@terrymanu terrymanu left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • 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.

@terrymanu
terrymanu merged commit f14047b into apache:master Dec 15, 2025
145 checks passed
@terrymanu terrymanu added this to the 5.5.3 milestone Dec 15, 2025
@ccxxxyy
ccxxxyy deleted the issueNo branch December 15, 2025 10:03
makssent pushed a commit to makssent/shardingsphere that referenced this pull request Apr 9, 2026
…ngsphere/master

* remotes/origin/master:
  Support for the explain and do keywords in Stored Procedures (apache#37376)
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.

Support parsing MySQL stored procedure syntax - part 4

3 participants