FlyingZC opened a new issue, #35089:
URL: https://github.com/apache/shardingsphere/issues/35089

   # 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:
   ```sql
   -- The original file and line number of the sql: 
generated_invisible_primary_key:567 
   CREATE PROCEDURE p1() BEGIN
   CREATE TABLE t1(f1 INT PRIMARY KEY, f2 INT);
   SHOW CREATE TABLE t1;
   CREATE TABLE t2 (f1 INT);
   SHOW CREATE TABLE t2;
   CREATE TABLE t3 AS SELECT * FROM t2;
   SHOW CREATE TABLE t3;
   DROP TABLE t1, t2, t3;
   end
   ```
   
   ```sql
   -- The original file and line number of the sql: 
generated_invisible_primary_key:662 
   CREATE PROCEDURE run_show_and_i_s_stmts() BEGIN
   SHOW CREATE TABLE t1;
   SHOW COLUMNS FROM t1;
   SHOW EXTENDED COLUMNS FROM t1;
   SHOW KEYS FROM t1;
   -- I_S.TABLES
   SELECT TABLE_NAME, AUTO_INCREMENT FROM INFORMATION_SCHEMA.TABLES
   WHERE TABLE_NAME='t1';
   -- I_S.COLUMNS
   SELECT COLUMN_NAME, COLUMN_TYPE, IS_NULLABLE, EXTRA
   FROM INFORMATION_SCHEMA.COLUMNS
   WHERE TABLE_NAME='t1' ORDER BY ORDINAL_POSITION;
   -- I_S.COLUMN_EXTENSIONS
   SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS_EXTENSIONS
   WHERE TABLE_NAME='t1' ORDER BY COLUMN_NAME;
   -- I_S.STATISTICS
   SELECT INDEX_NAME, COLUMN_NAME FROM INFORMATION_SCHEMA.STATISTICS
   WHERE TABLE_NAME='t1' ORDER BY INDEX_NAME, COLUMN_NAME;
   -- I_S.KEY_COLUMN_USAGE
   SELECT CONSTRAINT_NAME, COLUMN_NAME
   FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE TABLE_NAME='t1'
   ORDER BY CONSTRAINT_NAME, COLUMN_NAME;
   -- I_S.TABLE_CONSTRAINTS
   SELECT CONSTRAINT_NAME, CONSTRAINT_TYPE, ENFORCED
   FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE TABLE_NAME='t1'
   ORDER BY CONSTRAINT_NAME;
   -- I_S.TABLE_CONSTRAINTS_EXTENSIONS
   SELECT CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS_EXTENSIONS
   WHERE TABLE_NAME='t1' ORDER BY CONSTRAINT_NAME;
   END;
   ```
   
   ```sql
   -- The original file and line number of the sql: grant:1332 
   CREATE PROCEDURE p3() SHOW TABLES FROM mysqltest2
   ```
   
   ```sql
   -- The original file and line number of the sql: 
grant_revoke_not_existing_privilege_routine:80 
   REVOKE IF EXISTS GRANT OPTION ON PROCEDURE rngp_db.rngp_proc FROM rngp_role
   ```
   
   ```sql
   -- The original file and line number of the sql: 
grant_revoke_not_existing_privilege_routine:73 
   REVOKE IF EXISTS GRANT OPTION ON PROCEDURE rngp_db.rngp_proc FROM rngp_role
   ```
   
   
   # 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:
   ```shell
   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.
   
   
   1. Master JAVA language
   2. Have a basic understanding of Antlr g4 file
   3. Be familiar with MySQL SQLs


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: 
notifications-unsubscr...@shardingsphere.apache.org.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to