Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@
1. SQL Parser: Fix parsing error for SQLServer session `SET QUOTED_IDENTIFIER` and `SET TEXTSIZE` statements - [#38005](https://github.com/apache/shardingsphere/pull/38005)
1. SQL Parser: Support '2'::int statement in PostgreSQL and openGauss - [#37962](https://github.com/apache/shardingsphere/pull/37962)
1. SQL Parser: Support range type constructor functions in PostgreSQL without quotes - [#37994](https://github.com/apache/shardingsphere/pull/37994)
1. SQL Parser: Support parsing MySQL stored procedure syntax - [#38017](https://github.com/apache/shardingsphere/pull/38017)
1. SQL Parser: Support parsing MySQL stored procedure syntax part8- [#38017](https://github.com/apache/shardingsphere/pull/38017)
1. SQL Parser: Support parsing MySQL stored procedure syntax part7- [#38029](https://github.com/apache/shardingsphere/pull/38029)
1. SQL Binder: Support to bind more SQL statements - [#36697](https://github.com/apache/shardingsphere/pull/36697)
1. SQL Binder: Add ALTER TABLE metadata check - [#35877](https://github.com/apache/shardingsphere/pull/35877)
1. SQL Binder: Support Grant statement SQL bind - [#36207](https://github.com/apache/shardingsphere/pull/36207)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@

package org.apache.shardingsphere.sql.parser.engine.mysql.parser;

import java.nio.CharBuffer;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CodePointBuffer;
import org.antlr.v4.runtime.CodePointCharStream;
import org.antlr.v4.runtime.misc.Interval;
import org.apache.shardingsphere.sql.parser.api.parser.SQLLexer;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementLexer;

Expand All @@ -27,6 +31,27 @@
public final class MySQLLexer extends MySQLStatementLexer implements SQLLexer {

public MySQLLexer(final CharStream input) {
super(input);
super(stripExecutableComment(input));
}

private static CharStream stripExecutableComment(final CharStream input) {
String sql = input.getText(Interval.of(0, input.size() - 1));
String trimmed = sql.trim();
if (trimmed.endsWith(";")) {
trimmed = trimmed.substring(0, trimmed.length() - 1).trim();
}
if (!trimmed.startsWith("/*!") || !trimmed.endsWith("*/")) {
return input;
}
String content = trimmed.substring(3, trimmed.length() - 2);
int index = 0;
while (index < content.length() && content.charAt(index) >= '0' && content.charAt(index) <= '9') {
index++;
}
String innerSQL = content.substring(index).trim();
if (innerSQL.isEmpty()) {
return input;
}
return CodePointCharStream.fromBuffer(CodePointBuffer.withChars(CharBuffer.wrap(innerSQL.toCharArray())));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@
import org.apache.shardingsphere.sql.parser.statement.core.statement.type.dml.SelectStatement;
import org.apache.shardingsphere.sql.parser.statement.core.statement.type.dml.UpdateStatement;
import org.apache.shardingsphere.sql.parser.statement.core.statement.type.dml.DoStatement;
import org.apache.shardingsphere.sql.parser.statement.core.statement.type.tcl.StartTransactionStatement;
import org.apache.shardingsphere.sql.parser.statement.core.value.collection.CollectionValue;
import org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue;
import org.apache.shardingsphere.sql.parser.statement.mysql.ddl.event.MySQLAlterEventStatement;
Expand All @@ -216,9 +217,12 @@ public final class MySQLDDLStatementVisitor extends MySQLStatementVisitor implem

private final MySQLDALStatementVisitor dalStatementVisitor;

private final MySQLTCLStatementVisitor tclStatementVisitor;

public MySQLDDLStatementVisitor(final DatabaseType databaseType) {
super(databaseType);
dalStatementVisitor = new MySQLDALStatementVisitor(databaseType);
tclStatementVisitor = new MySQLTCLStatementVisitor(databaseType);
}

@Override
Expand Down Expand Up @@ -864,6 +868,12 @@ private ValidStatementSegment createValidStatementSegment(final ValidStatementCo
sqlStatement = (DeleteStatement) visit(ctx.delete());
} else if (null != ctx.select()) {
sqlStatement = (SelectStatement) visit(ctx.select());
} else if (null != ctx.startTransaction()) {
sqlStatement = new StartTransactionStatement(getDatabaseType());
} else if (null != ctx.commit()) {
sqlStatement = (SQLStatement) tclStatementVisitor.visitCommit(ctx.commit());
} else if (null != ctx.rollback()) {
sqlStatement = (SQLStatement) tclStatementVisitor.visitRollback(ctx.rollback());
} else if (null != ctx.setVariable()) {
sqlStatement = (SQLStatement) dalStatementVisitor.visitSetVariable(ctx.setVariable());
} else if (null != ctx.doStatement()) {
Expand Down
10 changes: 10 additions & 0 deletions test/it/parser/src/main/resources/case/dal/set.xml
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,14 @@
<parameter name="time_zone" scope="PERSIST_ONLY" />
</parameter-assign>
</set-parameter>
<set-parameter sql-case-id="set_mysql_version_comment">
<parameter-assign value="123">
<parameter name="max_connections" scope="GLOBAL" />
</parameter-assign>
</set-parameter>
<set-parameter sql-case-id="set_mysql_version_comment_with_version">
<parameter-assign value="456">
<parameter name="max_connections" scope="GLOBAL" />
</parameter-assign>
</set-parameter>
</sql-parser-test-cases>
54 changes: 48 additions & 6 deletions test/it/parser/src/main/resources/case/ddl/create-procedure.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,12 @@
<create-procedure sql-case-id="create_procedure_with_transaction">
<procedure-name name="proc1" />
<sql-statements>
<sql-statement start-index="30" stop-index="50" statement-class-simple-name="StartTransactionStatement" />
<sql-statement start-index="52" stop-index="83" statement-class-simple-name="CreateTableStatement" />
<sql-statement start-index="85" stop-index="93" statement-class-simple-name="RollbackStatement" />
<sql-statement start-index="95" stop-index="115" statement-class-simple-name="StartTransactionStatement" />
<sql-statement start-index="117" stop-index="148" statement-class-simple-name="CreateTableStatement" />
<sql-statement start-index="150" stop-index="158" statement-class-simple-name="CommitStatement" />
<sql-statement start-index="31" stop-index="48" statement-class-simple-name="StartTransactionStatement" />
<sql-statement start-index="50" stop-index="74" statement-class-simple-name="CreateTableStatement" />
<sql-statement start-index="76" stop-index="84" statement-class-simple-name="RollbackStatement" />
<sql-statement start-index="86" stop-index="103" statement-class-simple-name="StartTransactionStatement" />
<sql-statement start-index="105" stop-index="129" statement-class-simple-name="CreateTableStatement" />
<sql-statement start-index="131" stop-index="137" statement-class-simple-name="CommitStatement" />
</sql-statements>
</create-procedure>
<create-procedure sql-case-id="create_procedure_with_charset_parameters_and_insert">
Expand Down Expand Up @@ -221,4 +221,46 @@
<create-procedure sql-case-id="create_procedure_lib_uses_both">
<procedure-name name="lib_uses_both" />
</create-procedure>
<create-procedure sql-case-id="create_procedure_batch_insert">
<procedure-name name="BatchInsert" />
<sql-statements>
<sql-statement start-index="53" stop-index="70" statement-class-simple-name="StartTransactionStatement" />
<sql-statement start-index="72" stop-index="82" statement-class-simple-name="SetStatement" />
<sql-statement start-index="91" stop-index="136" statement-class-simple-name="SetStatement" />
<sql-statement start-index="138" stop-index="188" statement-class-simple-name="InsertStatement" />
<sql-statement start-index="190" stop-index="205" statement-class-simple-name="SetStatement" />
<sql-statement start-index="240" stop-index="246" statement-class-simple-name="CommitStatement" />
</sql-statements>
</create-procedure>
<create-procedure sql-case-id="create_procedure_p1_select_set">
<procedure-name name="p1" />
<sql-statements>
<sql-statement start-index="33" stop-index="56" statement-class-simple-name="SelectStatement" />
<sql-statement start-index="58" stop-index="74" statement-class-simple-name="SelectStatement" />
<sql-statement start-index="76" stop-index="86" statement-class-simple-name="SetStatement" />
</sql-statements>
</create-procedure>
<create-procedure sql-case-id="create_procedure_test_if_commit">
<procedure-name name="test_if_commit" />
<sql-statements>
<sql-statement start-index="40" stop-index="48" statement-class-simple-name="RollbackStatement" />
<sql-statement start-index="50" stop-index="119" statement-class-simple-name="SelectStatement" />
<sql-statement start-index="121" stop-index="138" statement-class-simple-name="DeleteStatement" />
<sql-statement start-index="140" stop-index="146" statement-class-simple-name="CommitStatement" />
</sql-statements>
</create-procedure>
<create-procedure sql-case-id="create_procedure_p1_repeat_handlers">
<procedure-name name="p1" />
<sql-statements>
<sql-statement start-index="169" stop-index="186" statement-class-simple-name="StartTransactionStatement" />
<sql-statement start-index="215" stop-index="297" statement-class-simple-name="SelectStatement" />
<sql-statement start-index="326" stop-index="400" statement-class-simple-name="SelectStatement" />
<sql-statement start-index="429" stop-index="460" statement-class-simple-name="InsertStatement" />
<sql-statement start-index="489" stop-index="520" statement-class-simple-name="InsertStatement" />
<sql-statement start-index="549" stop-index="563" statement-class-simple-name="DeleteStatement" />
<sql-statement start-index="592" stop-index="606" statement-class-simple-name="DeleteStatement" />
<sql-statement start-index="635" stop-index="641" statement-class-simple-name="CommitStatement" />
<sql-statement start-index="651" stop-index="676" statement-class-simple-name="SetStatement" />
</sql-statements>
</create-procedure>
</sql-parser-test-cases>
2 changes: 2 additions & 0 deletions test/it/parser/src/main/resources/sql/supported/dal/set.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,6 @@
<sql-case id="set_session_authorization" value="SET SESSION AUTHORIZATION user1 PASSWORD 'password'" db-types="openGauss" />
<sql-case id="set_persist_system_variable_doris" value="SET PERSIST max_connections = 200" db-types="Doris" />
<sql-case id="set_persist_only_system_variable_doris" value="SET PERSIST_ONLY time_zone = '+08:00'" db-types="Doris" />
<sql-case id="set_mysql_version_comment" value="/*! SET GLOBAL max_connections=123 */" db-types="MySQL" />
<sql-case id="set_mysql_version_comment_with_version" value="/*!80029 SET GLOBAL max_connections=456 */" db-types="MySQL" />
</sql-cases>
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,8 @@
<sql-case id="create_procedure_keywords_labeled_while" value="CREATE PROCEDURE p2() BEGIN DECLARE n INT DEFAULT 2; general: WHILE n &gt; 0 DO SET n = n -1; END WHILE general; SET n = 2; slow: WHILE n &gt; 0 DO SET n = n -1; END WHILE slow; SET n = 2; ignore_server_ids: WHILE n &gt; 0 DO SET n = n -1; END WHILE ignore_server_ids; SET n = 2; source_heartbeat_period: WHILE n &gt; 0 DO SET n = n -1; END WHILE source_heartbeat_period; END" db-types="MySQL" />
<sql-case id="create_procedure_check_warnings" value="CREATE DEFINER=root@localhost PROCEDURE check_warnings(OUT result INT) BEGIN DECLARE `pos` bigint unsigned; SET SQL_LOG_BIN=0; UPDATE error_log el, global_suppressions gs SET suspicious=0 WHERE el.suspicious=1 AND el.line REGEXP gs.pattern; UPDATE error_log el, test_suppressions ts SET suspicious=0 WHERE el.suspicious=1 AND el.line REGEXP ts.pattern; SELECT COUNT(*) INTO @num_warnings FROM error_log WHERE suspicious=1; IF @num_warnings &gt; 0 THEN SELECT line FROM error_log WHERE suspicious=1; SELECT 2 INTO result; ELSE SELECT 0 INTO RESULT; END IF; TRUNCATE test_suppressions; DROP TABLE error_log; END" db-types="MySQL" />
<sql-case id="create_procedure_lib_uses_both" value="CREATE PROCEDURE lib_uses_both() LANGUAGE JAVASCRIPT AS $$ console.log(&quot;Hello world&quot;) $$" db-types="MySQL" />
<sql-case id="create_procedure_batch_insert" value="CREATE PROCEDURE BatchInsert(IN row_count int) BEGIN START TRANSACTION; SET @n = 1; REPEAT SET @str = (CONCAT('test', CAST(@n AS CHAR))); INSERT INTO product(code, name) VALUES(@str, @str); SET @n = @n + 1; UNTIL @n &gt; row_count END REPEAT; COMMIT; END;" db-types="MySQL" />
<sql-case id="create_procedure_p1_select_set" value="CREATE PROCEDURE p1(x int) BEGIN SELECT count(*) FROM t1; SELECT * FROM t1; SET @x = x; END;" db-types="MySQL" />
<sql-case id="create_procedure_test_if_commit" value="CREATE PROCEDURE test_if_commit() BEGIN ROLLBACK; SELECT IF (COUNT(*) &gt; 0, &quot;YES&quot;, &quot;NO&quot;) AS &quot;IMPLICIT COMMIT&quot; FROM trans; DELETE FROM trans; COMMIT; END;" db-types="MySQL" />
<sql-case id="create_procedure_p1_repeat_handlers" value="create procedure p1() begin declare counter integer default 0; declare continue handler for sqlexception begin set counter = counter + 10;end; repeat if rand()&gt;0.5 then start transaction; end if; if rand()&gt;0.5 then select var_samp(1), exists(select 1 from t1 lock in share mode) from t1 into @a,@b; end if; if rand()&gt;0.5 then select var_samp(1), exists(select 1 from t1 for update) from t1 into @a,@b; end if; if rand()&gt;0.5 then insert ignore into t1 values (); end if; if rand()&gt;0.5 then insert ignore into t2 values (); end if; if rand()&gt;0.5 then delete from t1; end if; if rand()&gt;0.5 then delete from t2; end if; if rand()&gt;0.5 then commit; end if; set counter = counter + 1; until counter &gt;= 100 end repeat; end" db-types="MySQL" />
</sql-cases>
Loading