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 f1757a0 fix lexer for string (#7594)
f1757a0 is described below
commit f1757a00966fd499d790db180b6fed7f13443b45
Author: JingShang Lu <[email protected]>
AuthorDate: Fri Sep 25 11:23:12 2020 +0800
fix lexer for string (#7594)
* fix lexer for string
* fix
* fix
---
.../src/main/antlr4/imports/mysql/DDLStatement.g4 | 6 +++---
.../src/main/antlr4/imports/mysql/Literals.g4 | 3 +--
.../sql/parser/mysql/visitor/MySQLVisitor.java | 24 +++++++++-------------
.../src/main/resources/case/dml/select.xml | 7 +++++++
.../main/resources/sql/supported/dml/select.xml | 1 +
5 files changed, 22 insertions(+), 19 deletions(-)
diff --git
a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/antlr4/imports/mysql/DDLStatement.g4
b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/antlr4/imports/mysql/DDLStatement.g4
index 647327d..a62871a 100644
---
a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/antlr4/imports/mysql/DDLStatement.g4
+++
b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/antlr4/imports/mysql/DDLStatement.g4
@@ -322,7 +322,7 @@ columnDefinition
storageOption
: dataTypeGenericOption
| AUTO_INCREMENT
- | DEFAULT (literals | expr)
+ | DEFAULT expr
| COLUMN_FORMAT (FIXED | DYNAMIC | DEFAULT)
| STORAGE (DISK | MEMORY | DEFAULT)
;
@@ -414,7 +414,7 @@ alterSpecification
| DROP CHECK ignoredIdentifier_
| ALTER CHECK ignoredIdentifier_ NOT? ENFORCED
| ALGORITHM EQ_? (DEFAULT | INSTANT | INPLACE | COPY)
- | ALTER COLUMN? columnName (SET DEFAULT (literals | LP_ expr RP_) | DROP
DEFAULT)
+ | ALTER COLUMN? columnName (SET DEFAULT expr | DROP DEFAULT)
| ALTER INDEX indexName (VISIBLE | INVISIBLE)
| changeColumnSpecification
| modifyColumnSpecification
@@ -549,7 +549,7 @@ partitionLessThanValue_
;
partitionValueList_
- : literals (COMMA_ literals)*
+ : expr (COMMA_ expr)*
;
partitionDefinitionOption_
diff --git
a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/antlr4/imports/mysql/Literals.g4
b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/antlr4/imports/mysql/Literals.g4
index c85e845..28121fe 100644
---
a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/antlr4/imports/mysql/Literals.g4
+++
b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/antlr4/imports/mysql/Literals.g4
@@ -34,14 +34,13 @@ FILESIZE_LITERAL
IDENTIFIER_
: [A-Za-z_$0-9]*?[A-Za-z_$]+?[A-Za-z_$0-9]*
| BQ_ ~'`'+ BQ_
- | (DQ_ ( '\\'. | '""' | ~('"'| '\\') )* DQ_)
;
Y_N_
: SQ_ ('Y' | 'N') SQ_
;
-STRING_
+STRING_
: (DQ_ ( '\\'. | '""' | ~('"'| '\\') )* DQ_)
| (SQ_ ('\\'. | '\'\'' | ~('\'' | '\\'))* SQ_)
;
diff --git
a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/MySQLVisitor.java
b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/MySQLVisitor.java
index c6e3d0c..1d9db5d 100644
---
a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/MySQLVisitor.java
+++
b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/MySQLVisitor.java
@@ -19,7 +19,6 @@ package org.apache.shardingsphere.sql.parser.mysql.visitor;
import lombok.AccessLevel;
import lombok.Getter;
-import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.misc.Interval;
import org.antlr.v4.runtime.tree.TerminalNode;
import org.apache.shardingsphere.sql.parser.api.ASTNode;
@@ -386,7 +385,7 @@ public abstract class MySQLVisitor extends
MySQLStatementBaseVisitor<ASTNode> {
@Override
public final ASTNode visitBitExpr(final BitExprContext ctx) {
if (null != ctx.simpleExpr()) {
- return createExpressionSegment(visit(ctx.simpleExpr()), ctx);
+ return visit(ctx.simpleExpr());
}
ExpressionSegment left = (ExpressionSegment) visit(ctx.getChild(0));
ExpressionSegment right = (ExpressionSegment) visit(ctx.getChild(2));
@@ -396,7 +395,8 @@ public abstract class MySQLVisitor extends
MySQLStatementBaseVisitor<ASTNode> {
return result;
}
- private ASTNode createExpressionSegment(final ASTNode astNode, final
ParserRuleContext context) {
+ private ExpressionSegment createLiteralExpression(final LiteralsContext
context) {
+ ASTNode astNode = visit(context);
if (astNode instanceof StringLiteralValue) {
return new LiteralExpressionSegment(context.start.getStartIndex(),
context.stop.getStopIndex(), ((StringLiteralValue) astNode).getValue());
}
@@ -406,28 +406,24 @@ public abstract class MySQLVisitor extends
MySQLStatementBaseVisitor<ASTNode> {
if (astNode instanceof BooleanLiteralValue) {
return new LiteralExpressionSegment(context.start.getStartIndex(),
context.stop.getStopIndex(), ((BooleanLiteralValue) astNode).getValue());
}
- if (astNode instanceof ParameterMarkerValue) {
- return new
ParameterMarkerExpressionSegment(context.start.getStartIndex(),
context.stop.getStopIndex(), ((ParameterMarkerValue) astNode).getValue());
- }
- if (astNode instanceof SubquerySegment) {
- return new SubqueryExpressionSegment((SubquerySegment) astNode);
- }
if (astNode instanceof OtherLiteralValue) {
return new
CommonExpressionSegment(context.getStart().getStartIndex(),
context.getStop().getStopIndex(), ((OtherLiteralValue) astNode).getValue());
}
- return astNode;
+ return new CommonExpressionSegment(context.getStart().getStartIndex(),
context.getStop().getStopIndex(),
+ context.start.getInputStream().getText(new
Interval(context.start.getStartIndex(), context.stop.getStopIndex())));
}
@Override
public final ASTNode visitSimpleExpr(final SimpleExprContext ctx) {
if (null != ctx.subquery()) {
- return new SubquerySegment(ctx.getStart().getStartIndex(),
ctx.getStop().getStopIndex(), (MySQLSelectStatement) visit(ctx.subquery()));
+ SubquerySegment subquerySegment = new
SubquerySegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(),
(MySQLSelectStatement) visit(ctx.subquery()));
+ return new SubqueryExpressionSegment(subquerySegment);
}
if (null != ctx.parameterMarker()) {
- return visit(ctx.parameterMarker());
+ return new
ParameterMarkerExpressionSegment(ctx.start.getStartIndex(),
ctx.stop.getStopIndex(), ((ParameterMarkerValue)
visit(ctx.parameterMarker())).getValue());
}
if (null != ctx.literals()) {
- return visit(ctx.literals());
+ return createLiteralExpression(ctx.literals());
}
if (null != ctx.intervalExpression()) {
return visit(ctx.intervalExpression());
@@ -603,7 +599,7 @@ public abstract class MySQLVisitor extends
MySQLStatementBaseVisitor<ASTNode> {
if (null != ctx.caseExpression()) {
visit(ctx.caseExpression());
String text = ctx.start.getInputStream().getText(new
Interval(ctx.start.getStartIndex(), ctx.stop.getStopIndex()));
- return new OtherLiteralValue(text);
+ return new CommonExpressionSegment(ctx.getStart().getStartIndex(),
ctx.getStop().getStopIndex(), text);
}
for (ExprContext each : ctx.expr()) {
visit(each);
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 8a738c3..86a4e82 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
@@ -471,6 +471,13 @@
</where>
</select>
+ <select sql-case-id="select_like_with_single_quotes" >
+ <projections start-index="7" stop-index="8">
+ <column-projection name="id" start-index="7" stop-index="8"/>
+ </projections>
+ <where></where>
+ </select>
+
<select sql-case-id="select_count_tilde_concat" parameters="'init', 1, 2,
9, 10">
<table-reference>
<table-factor>
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 2813045..000b5c2 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
@@ -32,6 +32,7 @@
<sql-case id="select_equal_with_same_sharding_column" value="SELECT * FROM
t_order WHERE order_id = ? AND order_id = ?" />
<sql-case id="select_in_with_same_sharding_column" value="SELECT * FROM
t_order WHERE order_id IN (?, ?) AND order_id IN (?, ?) ORDER BY order_id" />
<sql-case id="select_count_like_concat" value="SELECT count(0) as
orders_count FROM t_order o WHERE o.status LIKE CONCAT('%%', ?, '%%') AND
o.user_id IN (?, ?) AND o.order_id BETWEEN ? AND ?" />
+ <sql-case id="select_like_with_single_quotes" value="select id from admin
where fullname like 'a%'" db-types="MySQL"/>
<sql-case id="select_count_tilde_concat" value="SELECT count(0) as
orders_count FROM t_order o WHERE o.status ~~ CONCAT('%%', ?, '%%') AND
o.user_id IN (?, ?) AND o.order_id BETWEEN ? AND ?" db-types="PostgreSQL" />
<sql-case id="select_sharding_route_with_binding_tables" value="SELECT i.*
FROM t_order o JOIN t_order_item i ON o.user_id = i.user_id AND o.order_id =
i.order_id WHERE o.user_id IN (?, ?) AND o.order_id BETWEEN ? AND ? ORDER BY
i.item_id" />
<sql-case id="select_full_route_with_binding_tables" value="SELECT i.*
FROM t_order o JOIN t_order_item i ON o.user_id = i.user_id AND o.order_id =
i.order_id ORDER BY i.item_id" />