You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by ji...@apache.org on 2023/03/20 02:26:52 UTC
[shardingsphere] branch master updated: Fix mysql cast function parsing error (#24637) (#24667)
This is an automated email from the ASF dual-hosted git repository.
jianglongtao 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 4886d9a1878 Fix mysql cast function parsing error (#24637) (#24667)
4886d9a1878 is described below
commit 4886d9a1878a03418d3af4aa16642921d7aac10f
Author: jiangtao <ji...@gmail.com>
AuthorDate: Mon Mar 20 10:26:44 2023 +0800
Fix mysql cast function parsing error (#24637) (#24667)
* Fix mysql cast function parsing error (#24637)
* Optimize code
* Optimize code
---
.../statement/impl/MySQLStatementSQLVisitor.java | 24 +++++++++++++++++++++-
.../resources/case/dml/select-special-function.xml | 12 +++++------
2 files changed, 29 insertions(+), 7 deletions(-)
diff --git a/sql-parser/dialect/mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/statement/impl/MySQLStatementSQLVisitor.java b/sql-parser/dialect/mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/statement/impl/MySQLStatementSQLVisitor.java
index 98f792ff75c..14536143dd7 100644
--- a/sql-parser/dialect/mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/statement/impl/MySQLStatementSQLVisitor.java
+++ b/sql-parser/dialect/mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/statement/impl/MySQLStatementSQLVisitor.java
@@ -129,6 +129,7 @@ import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.TableRe
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.TableStatementContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.TemporalLiteralsContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.TrimFunctionContext;
+import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.TypeDatetimePrecisionContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.UpdateContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.ValuesFunctionContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.ViewNameContext;
@@ -907,7 +908,19 @@ public abstract class MySQLStatementSQLVisitor extends MySQLStatementBaseVisitor
result.getParameters().add((LiteralExpressionSegment) expr);
}
}
- result.getParameters().add((DataTypeSegment) visit(ctx.dataType()));
+ if (null != ctx.dataType()) {
+ result.getParameters().add((DataTypeSegment) visit(ctx.dataType()));
+ }
+ if (null != ctx.DATETIME()) {
+ DataTypeSegment dataType = new DataTypeSegment();
+ dataType.setDataTypeName(ctx.DATETIME().getText());
+ dataType.setStartIndex(ctx.DATETIME().getSymbol().getStartIndex());
+ dataType.setStopIndex(ctx.DATETIME().getSymbol().getStopIndex());
+ if (null != ctx.typeDatetimePrecision()) {
+ dataType.setDataLength((DataTypeLengthSegment) visit(ctx.typeDatetimePrecision()));
+ }
+ result.getParameters().add(dataType);
+ }
return result;
}
@@ -1081,6 +1094,15 @@ public abstract class MySQLStatementSQLVisitor extends MySQLStatementBaseVisitor
return result;
}
+ @Override
+ public ASTNode visitTypeDatetimePrecision(final TypeDatetimePrecisionContext ctx) {
+ DataTypeLengthSegment result = new DataTypeLengthSegment();
+ result.setStartIndex(ctx.start.getStartIndex());
+ result.setStopIndex(ctx.stop.getStartIndex());
+ result.setPrecision(Integer.parseInt(ctx.NUMBER_().getText()));
+ return result;
+ }
+
@Override
public final ASTNode visitOrderByClause(final OrderByClauseContext ctx) {
Collection<OrderByItemSegment> items = new LinkedList<>();
diff --git a/test/it/parser/src/main/resources/case/dml/select-special-function.xml b/test/it/parser/src/main/resources/case/dml/select-special-function.xml
index 8155cf5f2d2..80ba46cc7d7 100644
--- a/test/it/parser/src/main/resources/case/dml/select-special-function.xml
+++ b/test/it/parser/src/main/resources/case/dml/select-special-function.xml
@@ -63,18 +63,18 @@
</projections>
</select>
<select sql-case-id="select_cast">
- <projections start-index="7" stop-index="42">
- <expression-projection text="CAST(c AT TIME ZONE 'UTC' AS DATETIME)" start-index="7" stop-index="42">
+ <projections start-index="7" stop-index="44">
+ <expression-projection text="CAST(c AT TIME ZONE 'UTC' AS DATETIME)" start-index="7" stop-index="44">
<expr>
- <function function-name="CAST" start-index="7" stop-index="42" text="CAST(c AT TIME ZONE 'UTC' AS DATETIME)">
+ <function function-name="CAST" start-index="7" stop-index="44" text="CAST(c AT TIME ZONE 'UTC' AS DATETIME)">
<parameter>
- <literal-expression value="c" start-index="12" stop-index="12" />
+ <column name="c" start-index="12" stop-index="12" />
</parameter>
<parameter>
- <literal-expression value="UTC" start-index="24" stop-index="29" />
+ <literal-expression value="UTC" start-index="27" stop-index="31" />
</parameter>
<parameter>
- <data-type value="DATETIME" start-index="34" stop-index="41" />
+ <data-type value="DATETIME" start-index="36" stop-index="43" />
</parameter>
</function>
</expr>