You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by pa...@apache.org on 2021/06/23 12:13:31 UTC
[shardingsphere] branch master updated: fix mysql assignment parse
error (#10947)
This is an automated email from the ASF dual-hosted git repository.
panjuan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 05f23a5 fix mysql assignment parse error (#10947)
05f23a5 is described below
commit 05f23a54c1c968a65b29a520aec328ae53fcf46d
Author: Zhengqiang Duan <st...@gmail.com>
AuthorDate: Wed Jun 23 20:13:05 2021 +0800
fix mysql assignment parse error (#10947)
---
.../src/main/antlr4/imports/mysql/BaseRule.g4 | 5 +++++
.../src/main/antlr4/imports/mysql/Symbol.g4 | 1 +
.../visitor/statement/impl/MySQLStatementSQLVisitor.java | 11 +++++++++++
.../src/main/resources/case/dml/select.xml | 10 ++++++++++
.../src/main/resources/sql/supported/dml/select.xml | 1 +
5 files changed, 28 insertions(+)
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/antlr4/imports/mysql/BaseRule.g4 b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/antlr4/imports/mysql/BaseRule.g4
index 26bc654..f6c39de 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/antlr4/imports/mysql/BaseRule.g4
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/antlr4/imports/mysql/BaseRule.g4
@@ -807,9 +807,14 @@ booleanPrimary
| booleanPrimary SAFE_EQ_ predicate
| booleanPrimary comparisonOperator predicate
| booleanPrimary comparisonOperator (ALL | ANY) subquery
+ | booleanPrimary assignmentOperator predicate
| predicate
;
+assignmentOperator
+ : EQ_ | ASSIGNMENT_
+ ;
+
comparisonOperator
: EQ_ | GTE_ | GT_ | LTE_ | LT_ | NEQ_
;
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/antlr4/imports/mysql/Symbol.g4 b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/antlr4/imports/mysql/Symbol.g4
index 1b99cee..ab8af14 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/antlr4/imports/mysql/Symbol.g4
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/antlr4/imports/mysql/Symbol.g4
@@ -57,5 +57,6 @@ BQ_: '`';
QUESTION_: '?';
AT_: '@';
SEMI_: ';';
+ASSIGNMENT_: ':=';
JSON_SEPARATOR: '->';
JSON_UNQUOTED_SEPARATOR: '->>';
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/statement/impl/MySQLStatementSQLVisitor.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/statement/impl/MySQLStatementSQLVisitor.java
index 172b27a..e3046a2 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/statement/impl/MySQLStatementSQLVisitor.java
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/statement/impl/MySQLStatementSQLVisitor.java
@@ -374,9 +374,20 @@ public abstract class MySQLStatementSQLVisitor extends MySQLStatementBaseVisitor
if (null != ctx.comparisonOperator() || null != ctx.SAFE_EQ_()) {
return createCompareSegment(ctx);
}
+ if (null != ctx.assignmentOperator()) {
+ return createAssignmentSegment(ctx);
+ }
return visit(ctx.predicate());
}
+ private ASTNode createAssignmentSegment(final BooleanPrimaryContext ctx) {
+ ExpressionSegment left = (ExpressionSegment) visit(ctx.booleanPrimary());
+ ExpressionSegment right = (ExpressionSegment) visit(ctx.predicate());
+ String operator = ctx.assignmentOperator().getText();
+ String text = ctx.start.getInputStream().getText(new Interval(ctx.start.getStartIndex(), ctx.stop.getStopIndex()));
+ return new BinaryOperationExpression(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), left, right, operator, text);
+ }
+
private ASTNode createCompareSegment(final BooleanPrimaryContext ctx) {
ExpressionSegment left = (ExpressionSegment) visit(ctx.booleanPrimary());
ExpressionSegment right;
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-test/src/main/resources/case/dml/select.xml b/shardingsphere-sql-parser/shardingsphere-sql-parser-test/src/main/resources/case/dml/select.xml
index fb3fc7b..f11da60 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-test/src/main/resources/case/dml/select.xml
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-test/src/main/resources/case/dml/select.xml
@@ -2905,5 +2905,15 @@
<expression-projection text="content_json::jsonb@@'$.keyword[*]=="ss"'" start-index="7" stop-index="47" />
</projections>
</select>
+
+ <select sql-case-id="select_with_assignment_operator">
+ <projections start-index="7" stop-index="31">
+ <expression-projection text="@rn := 1" start-index="7" stop-index="14" />
+ <expression-projection text="@now_code := ''" start-index="17" stop-index="31" />
+ </projections>
+ <from>
+ <simple-table name="t_order" start-index="38" stop-index="44" />
+ </from>
+ </select>
</sql-parser-test-cases>
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-test/src/main/resources/sql/supported/dml/select.xml b/shardingsphere-sql-parser/shardingsphere-sql-parser-test/src/main/resources/sql/supported/dml/select.xml
index 05c9128..9677647 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-test/src/main/resources/sql/supported/dml/select.xml
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-test/src/main/resources/sql/supported/dml/select.xml
@@ -79,4 +79,5 @@
<sql-case id="select_with_jsonb_path_delete" value="SELECT content_json::jsonb#-'{title}' FROM tb_content_json" db-types="PostgreSQL" />
<sql-case id="select_with_jsonb_path_contain_any_value" value="SELECT content_json::jsonb @?'$.keyword[*]?(@=="ss")' FROM tb_content_json" db-types="PostgreSQL" />
<sql-case id="select_with_jsonb_path_predicate_check" value="SELECT content_json::jsonb@@'$.keyword[*]=="ss"' FROM tb_content_json" db-types="PostgreSQL" />
+ <sql-case id="select_with_assignment_operator" value="SELECT @rn := 1, @now_code := '' FROM t_order" db-types="MySQL" />
</sql-cases>