Skip to content

Commit

Permalink
Enhance oracle datetime sql parse logic (apache#28420)
Browse files Browse the repository at this point in the history
  • Loading branch information
strongduanmu authored Sep 12, 2023
1 parent 341dc3b commit 15e4677
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
6 changes: 3 additions & 3 deletions test/it/parser/src/main/resources/case/dml/insert.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down

0 comments on commit 15e4677

Please sign in to comment.