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

tuichenchuxin 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 15e4677da4f Enhance oracle datetime sql parse logic (#28420)
15e4677da4f is described below

commit 15e4677da4f5254374b22f3df1d41da0bf3ddefe
Author: Zhengqiang Duan <[email protected]>
AuthorDate: Tue Sep 12 14:23:52 2023 +0800

    Enhance oracle datetime sql parse logic (#28420)
---
 .../visitor/statement/OracleStatementVisitor.java  | 18 +++++++++
 .../value/literal/impl/DateTimeLiteralValue.java   | 46 ++++++++++++++++++++++
 .../parser/src/main/resources/case/dml/insert.xml  |  6 +--
 .../resources/case/dml/select-special-function.xml |  2 +-
 4 files changed, 68 insertions(+), 4 deletions(-)

diff --git 
a/parser/sql/dialect/oracle/src/main/java/org/apache/shardingsphere/sql/parser/oracle/visitor/statement/OracleStatementVisitor.java
 
b/parser/sql/dialect/oracle/src/main/java/org/apache/shardingsphere/sql/parser/oracle/visitor/statement/OracleStatementVisitor.java
index bb999a1a045..c9629fe1213 100644
--- 
a/parser/sql/dialect/oracle/src/main/java/org/apache/shardingsphere/sql/parser/oracle/visitor/statement/OracleStatementVisitor.java
+++ 
b/parser/sql/dialect/oracle/src/main/java/org/apache/shardingsphere/sql/parser/oracle/visitor/statement/OracleStatementVisitor.java
@@ -41,6 +41,7 @@ import 
org.apache.shardingsphere.sql.parser.autogen.OracleStatementParser.Cursor
 import 
org.apache.shardingsphere.sql.parser.autogen.OracleStatementParser.DataTypeContext;
 import 
org.apache.shardingsphere.sql.parser.autogen.OracleStatementParser.DataTypeLengthContext;
 import 
org.apache.shardingsphere.sql.parser.autogen.OracleStatementParser.DataTypeNameContext;
+import 
org.apache.shardingsphere.sql.parser.autogen.OracleStatementParser.DateTimeLiteralsContext;
 import 
org.apache.shardingsphere.sql.parser.autogen.OracleStatementParser.DatetimeExprContext;
 import 
org.apache.shardingsphere.sql.parser.autogen.OracleStatementParser.ExprContext;
 import 
org.apache.shardingsphere.sql.parser.autogen.OracleStatementParser.ExtractFunctionContext;
@@ -153,6 +154,7 @@ import 
org.apache.shardingsphere.sql.parser.sql.common.value.collection.Collecti
 import 
org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue;
 import 
org.apache.shardingsphere.sql.parser.sql.common.value.keyword.KeywordValue;
 import 
org.apache.shardingsphere.sql.parser.sql.common.value.literal.impl.BooleanLiteralValue;
+import 
org.apache.shardingsphere.sql.parser.sql.common.value.literal.impl.DateTimeLiteralValue;
 import 
org.apache.shardingsphere.sql.parser.sql.common.value.literal.impl.NullLiteralValue;
 import 
org.apache.shardingsphere.sql.parser.sql.common.value.literal.impl.NumberLiteralValue;
 import 
org.apache.shardingsphere.sql.parser.sql.common.value.literal.impl.OtherLiteralValue;
@@ -209,6 +211,22 @@ public abstract class OracleStatementVisitor extends 
OracleStatementBaseVisitor<
         throw new IllegalStateException("Literals must have string, number, 
dateTime, hex, bit, interval, boolean or null.");
     }
     
+    @Override
+    public ASTNode visitDateTimeLiterals(final DateTimeLiteralsContext ctx) {
+        if (null != ctx.LBE_()) {
+            return new DateTimeLiteralValue(ctx.identifier().getText(), 
((StringLiteralValue) visit(ctx.stringLiterals())).getValue(), true);
+        }
+        String dateTimeType;
+        if (null != ctx.DATE()) {
+            dateTimeType = ctx.DATE().getText();
+        } else if (null != ctx.TIME()) {
+            dateTimeType = ctx.TIME().getText();
+        } else {
+            dateTimeType = ctx.TIMESTAMP().getText();
+        }
+        return new DateTimeLiteralValue(dateTimeType, ((StringLiteralValue) 
visit(ctx.stringLiterals())).getValue(), false);
+    }
+    
     @Override
     public final ASTNode visitStringLiterals(final StringLiteralsContext ctx) {
         return new StringLiteralValue(ctx.getText());
diff --git 
a/parser/sql/statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/value/literal/impl/DateTimeLiteralValue.java
 
b/parser/sql/statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/value/literal/impl/DateTimeLiteralValue.java
new file mode 100644
index 00000000000..ee82f37a4e9
--- /dev/null
+++ 
b/parser/sql/statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/value/literal/impl/DateTimeLiteralValue.java
@@ -0,0 +1,46 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.sql.parser.sql.common.value.literal.impl;
+
+import 
org.apache.shardingsphere.sql.parser.sql.common.value.literal.LiteralValue;
+
+/**
+ * Date time literal value.
+ */
+public final class DateTimeLiteralValue implements LiteralValue<String> {
+    
+    private final String dateTimeType;
+    
+    private final String dateTimeValue;
+    
+    private final boolean containsBrace;
+    
+    public DateTimeLiteralValue(final String dateTimeType, final String 
dateTimeValue, final boolean containsBrace) {
+        this.dateTimeType = dateTimeType;
+        this.dateTimeValue = containsBrace ? dateTimeValue.substring(1, 
dateTimeValue.length() - 1) : dateTimeValue;
+        this.containsBrace = containsBrace;
+    }
+    
+    @Override
+    public String getValue() {
+        if (containsBrace) {
+            return "{" + dateTimeType + " " + dateTimeValue + "}";
+        }
+        return dateTimeType + " " + dateTimeValue;
+    }
+}
diff --git a/test/it/parser/src/main/resources/case/dml/insert.xml 
b/test/it/parser/src/main/resources/case/dml/insert.xml
index b27c9ec4424..c32c2ef8e66 100644
--- a/test/it/parser/src/main/resources/case/dml/insert.xml
+++ b/test/it/parser/src/main/resources/case/dml/insert.xml
@@ -2310,13 +2310,13 @@
         <values>
             <value>
                 <assignment-value>
-                    <literal-expression value="1999-12-01 10:00:00" 
start-index="30" stop-index="59" />
+                    <common-expression text="TIMESTAMP'1999-12-01 10:00:00'" 
start-index="30" stop-index="59" />
                 </assignment-value>
                 <assignment-value>
-                    <literal-expression value="1999-12-01 10:00:00" 
start-index="62" stop-index="91" />
+                    <common-expression text="TIMESTAMP'1999-12-01 10:00:00'" 
start-index="62" stop-index="91" />
                 </assignment-value>
                 <assignment-value>
-                    <literal-expression value="1999-12-01 10:00:00" 
start-index="94" stop-index="123" />
+                    <common-expression text="TIMESTAMP'1999-12-01 10:00:00'" 
start-index="94" stop-index="123" />
                 </assignment-value>
             </value>
         </values>
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 83d5fdb5c8c..44ef9346357 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
@@ -431,7 +431,7 @@
                 <expr>
                     <function function-name="EXTRACT" text="EXTRACT(YEAR FROM 
TIMESTAMP '2001-02-16 20:38:40')" start-index="7" stop-index="56" 
literal-start-index="7" literal-stop-index="56">
                         <parameter>
-                            <literal-expression value="2001-02-16 20:38:40" 
start-index="25" stop-index="55" literal-start-index="25" 
literal-stop-index="55" />
+                            <common-expression text="TIMESTAMP '2001-02-16 
20:38:40'" start-index="25" stop-index="55" literal-start-index="25" 
literal-stop-index="55" />
                         </parameter>
                         <literalText>EXTRACT(YEAR FROM TIMESTAMP '2001-02-16 
20:38:40')</literalText>
                     </function>

Reply via email to