shardingsphere icon indicating copy to clipboard operation
shardingsphere copied to clipboard

Support parsing MySQL stored procedure syntax - part 4

Open FlyingZC opened this issue 9 months ago • 3 comments

Background

Hi community,

ShardingSphere's parser engine enables users to parse SQL into AST (Abstract Syntax Tree) and convert it into SQLStatement objects. This issue focuses on enhancing MySQL stored procedure parsing capabilities to support complex database programming logic.

More details: https://shardingsphere.apache.org/document/current/en/reference/sharding/parse/

Issue Background Explanation

These SQL statements are from the official MySQL test set. You can find the corresponding stored procedure SQL statements in the following website: https://github.com/mysql/mysql-server/tree/8.0/mysql-test/t These SQL cases are then passed to ShardingSphere's parsing engine for analysis. For SQL cases that fail to be parsed successfully, every 3 to 5 SQL cases are grouped together as an issue.

  1. It cannot be guaranteed that all SQL cases are correct. Please follow the following process to handle this pull request (PR).
  2. Some SQL cases may have already been fixed in other PRs. For cases that can already be executed successfully, simply leave a comment to ignore them.
  3. If a SQL case can be executed successfully without any code changes, there is no need to add a corresponding test assert file.

Task

This issue requires adding support for the following MySQL stored procedure syntax:

-- The original file and line number of the sql: events_bugs:530 
create procedure p_16 () alter event e_16 on schedule every @a second
-- The original file and line number of the sql: func_math:1123 
CREATE PROCEDURE test_round(in arg bigint)
BEGIN
DECLARE i int;
SET i = 0;
WHILE (i >= -20) DO
BEGIN
# Ignore SQLSTATE 22003 numeric value out of range
DECLARE CONTINUE HANDLER FOR SQLSTATE '22003' SHOW ERRORS;
SELECT arg, i, round(arg, i);
END;
SET i = i - 1;
END WHILE;
END
-- The original file and line number of the sql: func_math:1139 
CREATE PROCEDURE test_round_unsigned(in arg bigint unsigned)
BEGIN
DECLARE i int;
SET i = 0;
WHILE (i >= -20) DO
BEGIN
# Ignore SQLSTATE 22003 numeric value out of range
DECLARE CONTINUE HANDLER FOR SQLSTATE '22003' SHOW ERRORS;
SELECT arg, i, round(arg, i);
END;
SET i = i - 1;
END WHILE;
END
-- The original file and line number of the sql: func_str:1868 
CREATE PROCEDURE sub1(id CHAR(10) CHARACTER SET utf8mb3)
BEGIN
SELECT * FROM view1 WHERE table_name=id COLLATE utf8mb3_tolower_ci;
EXPLAIN SELECT * FROM view1 WHERE table_name=id COLLATE utf8mb3_tolower_ci;
SELECT * FROM view2 WHERE table_name=id;
EXPLAIN SELECT * FROM view2 WHERE table_name=id;
SELECT * FROM dd_table WHERE name COLLATE utf8mb3_tolower_ci = id;
EXPLAIN SELECT * FROM dd_table WHERE name COLLATE utf8mb3_tolower_ci = id;
SELECT * FROM view1 WHERE id COLLATE utf8mb3_tolower_ci=table_name;
EXPLAIN SELECT * FROM view1 WHERE id COLLATE utf8mb3_tolower_ci=table_name;
SELECT * FROM view2 WHERE id=table_name;
EXPLAIN SELECT * FROM view2 WHERE id=table_name;
SELECT * FROM dd_table WHERE id = name COLLATE utf8mb3_tolower_ci;
EXPLAIN SELECT * FROM dd_table WHERE id = name COLLATE utf8mb3_tolower_ci;
SELECT * FROM view1 WHERE table_name COLLATE utf8mb3_tolower_ci = id;
EXPLAIN SELECT * FROM view1 WHERE table_name COLLATE utf8mb3_tolower_ci = id;
END
-- The original file and line number of the sql: func_time:621 
create procedure t_sysdate()
begin
select sysdate() into @a;
do sleep(2);
select sysdate() into @b;
select @a != @b;
end;

Overall Procedure

If you intend to participate in fixing this issue, please feel free to leave a comment below the issue. Community members will assign the issue accordingly. For example, you can leave a comment like this: "Hi, please assign this issue to me. Thank you!"

Once you have claimed the issue, please review the syntax of the SQL on the official website of the corresponding database. Execute the SQL on the respective database to ensure the correctness of the SQL syntax.

You can check the corresponding source of each SQL case on the official database website. Next, execute the problematic SQL cases mentioned above in the database (you can quickly start the corresponding database using the Docker image for that database, and then connect to it using a client you are familiar with), to ensure that the SQL syntax itself is correct.

Once you have confirmed the correctness of the SQL syntax, you can validate and fix the grammar parsing issue in ShardingSphere.

If you are using IntelliJ IDEA, you will need to install the ANTLR plugin before proceeding. If it is an ANTLR parsing error message, try to repair the .g4 file by comparing it with the official database syntax until the SQL can be correctly parsed by ANTLR. When there is no error message in the ANTLR Preview window, it means that ANTLR can correctly parse the SQL.

After ANTLR parses SQL into an abstract syntax tree, ShardingSphere will access the abstract syntax tree through Visitor and extract the required information. If you need to extract Segments, you need to first execute:

mvn -T 2C clean install -DskipTests

Under the shardingsphere-parser module to compile the entire parser module. Then rewrite the corresponding visit method in SQLStatementVisitorr as needed to extract the corresponding Segment.

After the above SQL parsing problem is repaired, the corresponding Test needs to be added. The steps are as follows:

  1. Add the corresponding sql-case in the sql/supported directory.

  2. Add case assertions in the case directory of the shardingsphere-test-it-parser module.

  3. Run org.apache.shardingsphere.test.it.sql.parser.internal.InternalSQLParserIT After SQL Parser IT runs successfully, you can submit a PR.

  4. Master JAVA language

  5. Have a basic understanding of Antlr g4 file

  6. Be familiar with MySQL SQLs

FlyingZC avatar Mar 27 '25 11:03 FlyingZC

Could you please assign this issue to me? Thank you

ccxxxyy avatar Apr 22 '25 07:04 ccxxxyy

@ccxxxyy , welcome, assigned to you.

yx9o avatar Apr 23 '25 08:04 yx9o

OK,Thanks!

---Original--- From: @.> Date: Wed, Apr 23, 2025 16:10 PM To: @.>; Cc: @.@.>; Subject: Re: [apache/shardingsphere] Support parsing MySQL stored proceduresyntax - part 4 (Issue #35088)

Assigned #35088 to @ccxxxyy.

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you were assigned.Message ID: @.***>

ccxxxyy avatar Apr 23 '25 08:04 ccxxxyy