Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,20 @@ valueExpression
| left=valueExpression comparisonOperator right=valueExpression #comparison
;

datetimeUnit
: YEAR | MONTH
| WEEK | DAY
| HOUR | MINUTE | SECOND
;

primaryExpression
: CASE whenClause+ (ELSE elseExpression=expression)? END #searchedCase
: name=(TIMESTAMPDIFF | DATEDIFF)
LEFT_PAREN
unit=datetimeUnit COMMA
startTimestamp=valueExpression COMMA
endTimestamp=valueExpression
RIGHT_PAREN #timestampdiff
| CASE whenClause+ (ELSE elseExpression=expression)? END #searchedCase
| CASE value=expression whenClause+ (ELSE elseExpression=expression)? END #simpleCase
| name=CAST LEFT_PAREN expression AS identifier RIGHT_PAREN #cast
| constant #constantDefault
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,19 @@ private String paramsToSql() {
}
int len = children.size();
List<String> result = Lists.newArrayList();
//XXX_diff are used by nereids only
if (fnName.getFunction().equalsIgnoreCase("years_diff")
|| fnName.getFunction().equalsIgnoreCase("months_diff")
|| fnName.getFunction().equalsIgnoreCase("days_diff")
|| fnName.getFunction().equalsIgnoreCase("hours_diff")
|| fnName.getFunction().equalsIgnoreCase("minutes_diff")
|| fnName.getFunction().equalsIgnoreCase("seconds_diff")) {
sb.append(children.get(1).toSql()).append(", ");
sb.append(children.get(0).toSql()).append(")");
return sb.toString();
}
//used by nereids END

if (fnName.getFunction().equalsIgnoreCase("json_array")
|| fnName.getFunction().equalsIgnoreCase("json_object")) {
len = len - 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
import org.apache.doris.nereids.DorisParser.TableAliasContext;
import org.apache.doris.nereids.DorisParser.TableNameContext;
import org.apache.doris.nereids.DorisParser.TableValuedFunctionContext;
import org.apache.doris.nereids.DorisParser.TimestampdiffContext;
import org.apache.doris.nereids.DorisParser.TvfPropertyContext;
import org.apache.doris.nereids.DorisParser.TvfPropertyItemContext;
import org.apache.doris.nereids.DorisParser.TypeConstructorContext;
Expand Down Expand Up @@ -124,6 +125,12 @@
import org.apache.doris.nereids.trees.expressions.TVFProperties;
import org.apache.doris.nereids.trees.expressions.TimestampArithmetic;
import org.apache.doris.nereids.trees.expressions.WhenClause;
import org.apache.doris.nereids.trees.expressions.functions.scalar.DaysDiff;
import org.apache.doris.nereids.trees.expressions.functions.scalar.HoursDiff;
import org.apache.doris.nereids.trees.expressions.functions.scalar.MinutesDiff;
import org.apache.doris.nereids.trees.expressions.functions.scalar.MonthsDiff;
import org.apache.doris.nereids.trees.expressions.functions.scalar.SecondsDiff;
import org.apache.doris.nereids.trees.expressions.functions.scalar.YearsDiff;
import org.apache.doris.nereids.trees.expressions.literal.BigIntLiteral;
import org.apache.doris.nereids.trees.expressions.literal.BooleanLiteral;
import org.apache.doris.nereids.trees.expressions.literal.DateLiteral;
Expand Down Expand Up @@ -546,6 +553,28 @@ public Expression visitArithmeticBinary(ArithmeticBinaryContext ctx) {
});
}

@Override
public Expression visitTimestampdiff(TimestampdiffContext ctx) {
Expression start = (Expression) visit(ctx.startTimestamp);
Expression end = (Expression) visit(ctx.endTimestamp);
String unit = ctx.unit.getText();
if ("YEAR".equalsIgnoreCase(unit)) {
return new YearsDiff(end, start);
} else if ("MONTH".equalsIgnoreCase(unit)) {
return new MonthsDiff(end, start);
} else if ("DAY".equalsIgnoreCase(unit)) {
return new DaysDiff(end, start);
} else if ("HOUR".equalsIgnoreCase(unit)) {
return new HoursDiff(end, start);
} else if ("MINUTE".equalsIgnoreCase(unit)) {
return new MinutesDiff(end, start);
} else if ("SECOND".equalsIgnoreCase(unit)) {
return new SecondsDiff(end, start);
}
throw new ParseException("Unsupported time stamp diff time unit: " + unit, ctx);

}

/**
* Create a value based [[CaseWhen]] expression. This has the following SQL form:
* {{{
Expand Down
19 changes: 19 additions & 0 deletions regression-test/data/nereids_syntax_p0/test_timestampdiff.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-- This file is automatically generated. You should know what you did if you want to edit this
-- !select --
1

-- !select --
3

-- !select --
2

-- !select --
1

-- !select --
3

-- !select --
40

40 changes: 40 additions & 0 deletions regression-test/suites/nereids_syntax_p0/test_timestampdiff.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// 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.

suite("test_timestampdiff") {
sql "set enable_nereids_planner=true"
sql "set enable_fallback_to_original_planner=false"
qt_select """
SELECT TIMESTAMPDIFF(year,'2003-02-01','2004-05-01');
"""
qt_select """
SELECT TIMESTAMPDIFF(MONTH,'2003-02-01','2003-05-01');
"""
qt_select """
SELECT TIMESTAMPDIFF(day,'2003-02-01','2003-02-03');
"""
qt_select """
SELECT TIMESTAMPDIFF(hour,'2003-02-03 10:00:00','2003-02-03 11:00:00');
"""

qt_select """
SELECT TIMESTAMPDIFF(minute,'2003-02-03 11:00:00','2003-02-03 11:03:00');
"""
qt_select """
SELECT TIMESTAMPDIFF(second,'2003-02-03 11:00:00','2003-02-03 11:00:40');
"""
}