From 15e4677da4f5254374b22f3df1d41da0bf3ddefe Mon Sep 17 00:00:00 2001 From: Zhengqiang Duan Date: Tue, 12 Sep 2023 14:23:52 +0800 Subject: [PATCH] Enhance oracle datetime sql parse logic (#28420) --- .../statement/OracleStatementVisitor.java | 18 ++++++++ .../literal/impl/DateTimeLiteralValue.java | 46 +++++++++++++++++++ .../src/main/resources/case/dml/insert.xml | 6 +-- .../case/dml/select-special-function.xml | 2 +- 4 files changed, 68 insertions(+), 4 deletions(-) create mode 100644 parser/sql/statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/value/literal/impl/DateTimeLiteralValue.java 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 bb999a1a0458a..c9629fe1213ca 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.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.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 final ASTNode visitLiterals(final LiteralsContext ctx) { 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 0000000000000..ee82f37a4e9b5 --- /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 { + + 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 b27c9ec4424a8..c32c2ef8e66e1 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 @@ - + - + - + 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 83d5fdb5c8c5a..44ef9346357b7 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 @@ - + EXTRACT(YEAR FROM TIMESTAMP '2001-02-16 20:38:40')