You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by du...@apache.org on 2023/03/14 10:53:20 UTC

[shardingsphere] branch master updated: support for CAST Function (#24532)

This is an automated email from the ASF dual-hosted git repository.

duanzhengqiang 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 c3ada162fa0 support for CAST Function (#24532)
c3ada162fa0 is described below

commit c3ada162fa0d39dc169c0e05c7362f681defd3a1
Author: kanha gupta <92...@users.noreply.github.com>
AuthorDate: Tue Mar 14 16:23:12 2023 +0530

    support for CAST Function (#24532)
---
 .../mysql/src/main/antlr4/imports/mysql/BaseRule.g4   |  3 ++-
 .../src/main/antlr4/imports/mysql/MySQLKeyword.g4     |  4 ++++
 .../statement/impl/MySQLStatementSQLVisitor.java      | 14 ++++++++------
 .../resources/case/dml/select-special-function.xml    | 19 +++++++++++++++++++
 .../sql/supported/dml/select-special-function.xml     |  1 +
 .../main/resources/sql/unsupported/unsupported.xml    |  1 -
 6 files changed, 34 insertions(+), 8 deletions(-)

diff --git a/sql-parser/dialect/mysql/src/main/antlr4/imports/mysql/BaseRule.g4 b/sql-parser/dialect/mysql/src/main/antlr4/imports/mysql/BaseRule.g4
index d2041722089..d4dc4e6cbdf 100644
--- a/sql-parser/dialect/mysql/src/main/antlr4/imports/mysql/BaseRule.g4
+++ b/sql-parser/dialect/mysql/src/main/antlr4/imports/mysql/BaseRule.g4
@@ -1004,8 +1004,9 @@ repairType
     
 castFunction
     : CAST LP_ expr AS dataType RP_
+    | CAST LP_ expr AT TIME ZONE expr AS DATETIME typeDatetimePrecision? RP_
     ;
-    
+
 convertFunction
     : CONVERT LP_ expr COMMA_ castType RP_
     | CONVERT LP_ expr USING charsetName RP_
diff --git a/sql-parser/dialect/mysql/src/main/antlr4/imports/mysql/MySQLKeyword.g4 b/sql-parser/dialect/mysql/src/main/antlr4/imports/mysql/MySQLKeyword.g4
index 32fe5e3deb4..3294d8ba836 100644
--- a/sql-parser/dialect/mysql/src/main/antlr4/imports/mysql/MySQLKeyword.g4
+++ b/sql-parser/dialect/mysql/src/main/antlr4/imports/mysql/MySQLKeyword.g4
@@ -2958,3 +2958,7 @@ JSON_VALID
     : J S O N UL_ V A L I D
     ;
 
+ZONE
+    : Z O N E
+    ;
+
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 b408bb24e3e..73b0d7f3c81 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
@@ -887,13 +887,15 @@ public abstract class MySQLStatementSQLVisitor extends MySQLStatementBaseVisitor
     
     @Override
     public final ASTNode visitCastFunction(final CastFunctionContext ctx) {
-        calculateParameterCount(Collections.singleton(ctx.expr()));
+        calculateParameterCount(ctx.expr());
         FunctionSegment result = new FunctionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), ctx.CAST().getText(), getOriginalText(ctx));
-        ASTNode exprSegment = visit(ctx.expr());
-        if (exprSegment instanceof ColumnSegment) {
-            result.getParameters().add((ColumnSegment) exprSegment);
-        } else if (exprSegment instanceof LiteralExpressionSegment) {
-            result.getParameters().add((LiteralExpressionSegment) exprSegment);
+        for (ExprContext each : ctx.expr()) {
+            ASTNode expr = visit(each);
+            if (expr instanceof ColumnSegment) {
+                result.getParameters().add((ColumnSegment) expr);
+            } else if (expr instanceof LiteralExpressionSegment) {
+                result.getParameters().add((LiteralExpressionSegment) expr);
+            }
         }
         result.getParameters().add((DataTypeSegment) visit(ctx.dataType()));
         return result;
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 8ed2bb7123c..8155cf5f2d2 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
@@ -62,6 +62,25 @@
             </expression-projection>
         </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">
+                <expr>
+                    <function function-name="CAST" start-index="7" stop-index="42" text="CAST(c AT TIME ZONE 'UTC' AS DATETIME)">
+                        <parameter>
+                            <literal-expression value="c" start-index="12" stop-index="12" />
+                        </parameter>
+                        <parameter>
+                            <literal-expression value="UTC" start-index="24" stop-index="29" />
+                        </parameter>
+                        <parameter>
+                            <data-type value="DATETIME" start-index="34" stop-index="41" />
+                        </parameter>
+                    </function>
+                </expr>
+            </expression-projection>
+        </projections>
+    </select>
     <select sql-case-id="select_convert_function">
         <projections start-index="7" stop-index="33">
             <expression-projection text="CONVERT('2020-10-01', DATE)" start-index="7" stop-index="33">
diff --git a/test/it/parser/src/main/resources/sql/supported/dml/select-special-function.xml b/test/it/parser/src/main/resources/sql/supported/dml/select-special-function.xml
index 2931333abb9..13793d00095 100644
--- a/test/it/parser/src/main/resources/sql/supported/dml/select-special-function.xml
+++ b/test/it/parser/src/main/resources/sql/supported/dml/select-special-function.xml
@@ -20,6 +20,7 @@
     <sql-case id="select_group_concat" value="SELECT GROUP_CONCAT(status) FROM t_order" db-types="MySQL" />
     <sql-case id="select_window_function" value="SELECT order_id, ROW_NUMBER() OVER() FROM t_order" db-types="MySQL" />
     <sql-case id="select_cast_function" value="SELECT CAST('1' AS UNSIGNED)" db-types="MySQL" />
+    <sql-case id="select_cast" value="SELECT CAST(c AT TIME ZONE 'UTC' AS DATETIME)" db-types="MySQL" />
     <sql-case id="select_convert_function" value="SELECT CONVERT('2020-10-01', DATE)" db-types="MySQL" />
     <sql-case id="select_position" value="SELECT POSITION('bar' IN 'foobarbar')" db-types="MySQL" />
     <sql-case id="select_substring" value="SELECT SUBSTRING('foobarbar' from 4)" db-types="MySQL" />
diff --git a/test/it/parser/src/main/resources/sql/unsupported/unsupported.xml b/test/it/parser/src/main/resources/sql/unsupported/unsupported.xml
index 0536a7b47aa..76d07bfa0a6 100644
--- a/test/it/parser/src/main/resources/sql/unsupported/unsupported.xml
+++ b/test/it/parser/src/main/resources/sql/unsupported/unsupported.xml
@@ -24,7 +24,6 @@
     <sql-case id="assert_dist_SQL_show_rule_parse_conflict" value="SHOW REPLICA_QUERY RULE FROM schema_name" />
     <sql-case id="with_select" value="WITH cte AS (SELECT 0 /*! ) */ SELECT * FROM cte a, cte b;" db-types="MySQL" />
     <sql-case id="with_select_comment" value="WITH cte AS /*! ( */ SELECT 0) SELECT * FROM cte a, cte b;" db-types="MySQL" />
-    <sql-case id="select_cast" value="SELECT cast( NULL AT TIME ZONE 'UTC' AS DATETIME );" db-types="MySQL" />
     <sql-case id="create_table_as_select" value="create table agg_data_2k as select g from generate_series(0, 1999) g;" db-types="PostgreSQL" />
     <sql-case id="create_temp_table" value="create temp table old_oids as select relname, oid as oldoid, relfilenode as oldfilenode from pg_class where relname like 'at_partitioned%'" db-types="PostgreSQL" />
     <sql-case id="select_case_when" value="select relname,c.oid = oldoid as orig_oid,case relfilenode when 0 then 'none' when c.oid then 'own' when oldfilenode then 'orig' else 'OTHER' end as storage, obj_description(c.oid, 'pg_class') as desc from pg_class c left join old_oids using (relname) where relname like 'at_partitioned%' order by relname" db-types="PostgreSQL" />