Skip to content
Open
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 @@ -40,11 +40,11 @@ public class DateDiff extends ScalarFunction
implements BinaryExpression, ExplicitlyCastableSignature, PropagateNullable {

public static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
FunctionSignature.ret(IntegerType.INSTANCE)
.args(TimeStampTzType.WILDCARD, TimeStampTzType.WILDCARD),
FunctionSignature.ret(IntegerType.INSTANCE)
.args(DateTimeV2Type.WILDCARD, DateTimeV2Type.WILDCARD),
FunctionSignature.ret(IntegerType.INSTANCE).args(DateV2Type.INSTANCE, DateV2Type.INSTANCE));
FunctionSignature.ret(IntegerType.INSTANCE).args(DateV2Type.INSTANCE, DateV2Type.INSTANCE),
FunctionSignature.ret(IntegerType.INSTANCE)
.args(TimeStampTzType.WILDCARD, TimeStampTzType.WILDCARD));

/**
* constructor with 2 arguments.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ public class DaysDiff extends ScalarFunction implements BinaryExpression, Explic
PropagateNullable, DateDiffMonotonic {

private static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
FunctionSignature.ret(BigIntType.INSTANCE).args(TimeStampTzType.WILDCARD, TimeStampTzType.WILDCARD),
FunctionSignature.ret(BigIntType.INSTANCE).args(DateTimeV2Type.WILDCARD, DateTimeV2Type.WILDCARD),
FunctionSignature.ret(BigIntType.INSTANCE).args(DateV2Type.INSTANCE, DateV2Type.INSTANCE));
FunctionSignature.ret(BigIntType.INSTANCE).args(DateV2Type.INSTANCE, DateV2Type.INSTANCE),
FunctionSignature.ret(BigIntType.INSTANCE).args(TimeStampTzType.WILDCARD, TimeStampTzType.WILDCARD));

/**
* constructor with 2 arguments.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ public class HoursDiff extends ScalarFunction implements BinaryExpression, Expli
PropagateNullable, DateDiffMonotonic {

public static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
FunctionSignature.ret(BigIntType.INSTANCE)
.args(TimeStampTzType.WILDCARD, TimeStampTzType.WILDCARD),
FunctionSignature.ret(BigIntType.INSTANCE)
.args(DateTimeV2Type.WILDCARD, DateTimeV2Type.WILDCARD),
FunctionSignature.ret(BigIntType.INSTANCE).args(DateV2Type.INSTANCE, DateV2Type.INSTANCE));
FunctionSignature.ret(BigIntType.INSTANCE).args(DateV2Type.INSTANCE, DateV2Type.INSTANCE),
FunctionSignature.ret(BigIntType.INSTANCE)
.args(TimeStampTzType.WILDCARD, TimeStampTzType.WILDCARD));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P1] Preserve instant semantics for all accepted zoned literal forms

The timezone score is summed across both arguments, so a recognized zoned literal paired with a zone-less literal scores +1 - 1 = 0 and ties DATETIMEV2. With time_zone='America/Los_Angeles', hours_diff('2021-03-14 03:30:00-07:00', '2021-03-14 01:30:00') therefore changes from 1 elapsed hour on the old TIMESTAMPTZ winner to 2 civil hours after moving this overload last. There is a second instance of the same policy gap where both inputs have explicit zones: the cast grammar accepts and normalizes compact -0700, but DateTimeChecker requires a colon before offset minutes, so pairing 2021-03-15 00:00:00-0700 with 2021-03-14 00:00:00-08:00 again ties and changes 23 elapsed hours to 24 civil hours. Please preserve TIMESTAMPTZ whenever an inspectable literal supplies any accepted explicit zone, using the same accepted grammar as the cast path, without reintroducing the all-uninspectable-VARCHAR fallback; add regressions for both mixed cases.


/**
* constructor with 2 arguments.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,9 @@ public class MicroSecondsDiff extends ScalarFunction implements BinaryExpression

private static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
FunctionSignature.ret(BigIntType.INSTANCE)
.args(TimeStampTzType.WILDCARD, TimeStampTzType.WILDCARD),
.args(DateTimeV2Type.WILDCARD, DateTimeV2Type.WILDCARD),
FunctionSignature.ret(BigIntType.INSTANCE)
.args(DateTimeV2Type.WILDCARD, DateTimeV2Type.WILDCARD)
);
.args(TimeStampTzType.WILDCARD, TimeStampTzType.WILDCARD));

/**
* constructor with 2 arguments.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,9 @@ public class MilliSecondsDiff extends ScalarFunction implements BinaryExpression

private static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
FunctionSignature.ret(BigIntType.INSTANCE)
.args(TimeStampTzType.WILDCARD, TimeStampTzType.WILDCARD),
.args(DateTimeV2Type.WILDCARD, DateTimeV2Type.WILDCARD),
FunctionSignature.ret(BigIntType.INSTANCE)
.args(DateTimeV2Type.WILDCARD, DateTimeV2Type.WILDCARD)
);
.args(TimeStampTzType.WILDCARD, TimeStampTzType.WILDCARD));

/**
* constructor with 2 arguments.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ public class MinutesDiff extends ScalarFunction implements BinaryExpression, Exp
PropagateNullable, DateDiffMonotonic {

private static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
FunctionSignature.ret(BigIntType.INSTANCE)
.args(TimeStampTzType.WILDCARD, TimeStampTzType.WILDCARD),
FunctionSignature.ret(BigIntType.INSTANCE)
.args(DateTimeV2Type.WILDCARD, DateTimeV2Type.WILDCARD),
FunctionSignature.ret(BigIntType.INSTANCE).args(DateV2Type.INSTANCE, DateV2Type.INSTANCE));
FunctionSignature.ret(BigIntType.INSTANCE).args(DateV2Type.INSTANCE, DateV2Type.INSTANCE),
FunctionSignature.ret(BigIntType.INSTANCE)
.args(TimeStampTzType.WILDCARD, TimeStampTzType.WILDCARD));

/**
* constructor with 2 arguments.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ public class MonthsDiff extends ScalarFunction implements BinaryExpression, Expl
PropagateNullable, DateDiffMonotonic {

private static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
FunctionSignature.ret(BigIntType.INSTANCE)
.args(TimeStampTzType.WILDCARD, TimeStampTzType.WILDCARD),
FunctionSignature.ret(BigIntType.INSTANCE)
.args(DateTimeV2Type.WILDCARD, DateTimeV2Type.WILDCARD),
FunctionSignature.ret(BigIntType.INSTANCE).args(DateV2Type.INSTANCE, DateV2Type.INSTANCE));
FunctionSignature.ret(BigIntType.INSTANCE).args(DateV2Type.INSTANCE, DateV2Type.INSTANCE),
FunctionSignature.ret(BigIntType.INSTANCE)
.args(TimeStampTzType.WILDCARD, TimeStampTzType.WILDCARD));

/**
* constructor with 2 arguments.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,11 @@ public class QuartersDiff extends ScalarFunction implements BinaryExpression, Ex
PropagateNullable, DateDiffMonotonic {

private static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
FunctionSignature.ret(BigIntType.INSTANCE)
.args(TimeStampTzType.WILDCARD, TimeStampTzType.WILDCARD),
FunctionSignature.ret(BigIntType.INSTANCE)
.args(DateTimeV2Type.WILDCARD, DateTimeV2Type.WILDCARD),
FunctionSignature.ret(BigIntType.INSTANCE).args(DateV2Type.INSTANCE, DateV2Type.INSTANCE)
);
FunctionSignature.ret(BigIntType.INSTANCE).args(DateV2Type.INSTANCE, DateV2Type.INSTANCE),
FunctionSignature.ret(BigIntType.INSTANCE)
.args(TimeStampTzType.WILDCARD, TimeStampTzType.WILDCARD));

/**
* constructor with 2 arguments.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ public class SecondsDiff extends ScalarFunction implements BinaryExpression, Exp
PropagateNullable, DateDiffMonotonic {

private static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
FunctionSignature.ret(BigIntType.INSTANCE)
.args(TimeStampTzType.WILDCARD, TimeStampTzType.WILDCARD),
FunctionSignature.ret(BigIntType.INSTANCE)
.args(DateTimeV2Type.WILDCARD, DateTimeV2Type.WILDCARD),
FunctionSignature.ret(BigIntType.INSTANCE).args(DateV2Type.INSTANCE, DateV2Type.INSTANCE));
FunctionSignature.ret(BigIntType.INSTANCE).args(DateV2Type.INSTANCE, DateV2Type.INSTANCE),
FunctionSignature.ret(BigIntType.INSTANCE)
.args(TimeStampTzType.WILDCARD, TimeStampTzType.WILDCARD));

/**
* constructor with 2 arguments.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ public class TimeDiff extends ScalarFunction
implements BinaryExpression, ExplicitlyCastableSignature, PropagateNullable {

private static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
FunctionSignature.ret(TimeV2Type.WILDCARD)
.args(TimeStampTzType.WILDCARD, TimeStampTzType.WILDCARD),
FunctionSignature.ret(TimeV2Type.WILDCARD)
.args(DateTimeV2Type.WILDCARD, DateTimeV2Type.WILDCARD),
FunctionSignature.ret(TimeV2Type.SYSTEM_DEFAULT).args(DateV2Type.INSTANCE, DateV2Type.INSTANCE));
FunctionSignature.ret(TimeV2Type.SYSTEM_DEFAULT).args(DateV2Type.INSTANCE, DateV2Type.INSTANCE),
FunctionSignature.ret(TimeV2Type.WILDCARD)
.args(TimeStampTzType.WILDCARD, TimeStampTzType.WILDCARD));

/**
* constructor with 2 arguments.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ public class WeeksDiff extends ScalarFunction implements BinaryExpression, Expli
PropagateNullable, DateDiffMonotonic {

private static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
FunctionSignature.ret(BigIntType.INSTANCE)
.args(TimeStampTzType.WILDCARD, TimeStampTzType.WILDCARD),
FunctionSignature.ret(BigIntType.INSTANCE)
.args(DateTimeV2Type.WILDCARD, DateTimeV2Type.WILDCARD),
FunctionSignature.ret(BigIntType.INSTANCE).args(DateV2Type.INSTANCE, DateV2Type.INSTANCE));
FunctionSignature.ret(BigIntType.INSTANCE).args(DateV2Type.INSTANCE, DateV2Type.INSTANCE),
FunctionSignature.ret(BigIntType.INSTANCE)
.args(TimeStampTzType.WILDCARD, TimeStampTzType.WILDCARD));

/**
* constructor with 2 arguments.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ public class YearsDiff extends ScalarFunction implements BinaryExpression, Expli
PropagateNullable, DateDiffMonotonic {

private static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
FunctionSignature.ret(BigIntType.INSTANCE)
.args(TimeStampTzType.WILDCARD, TimeStampTzType.WILDCARD),
FunctionSignature.ret(BigIntType.INSTANCE)
.args(DateTimeV2Type.WILDCARD, DateTimeV2Type.WILDCARD),
FunctionSignature.ret(BigIntType.INSTANCE).args(DateV2Type.INSTANCE, DateV2Type.INSTANCE));
FunctionSignature.ret(BigIntType.INSTANCE).args(DateV2Type.INSTANCE, DateV2Type.INSTANCE),
FunctionSignature.ret(BigIntType.INSTANCE)
.args(TimeStampTzType.WILDCARD, TimeStampTzType.WILDCARD));

/**
* constructor with 2 arguments.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// 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.doris.nereids.trees.expressions.functions.scalar;

import org.apache.doris.catalog.FunctionSignature;
import org.apache.doris.nereids.trees.expressions.Expression;
import org.apache.doris.nereids.trees.expressions.SlotReference;
import org.apache.doris.nereids.trees.expressions.functions.ComputeSignature;
import org.apache.doris.nereids.types.DateTimeV2Type;
import org.apache.doris.nereids.types.TimeStampTzType;
import org.apache.doris.nereids.types.VarcharType;

import com.google.common.collect.ImmutableList;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.function.BiFunction;

/**
* Regression tests for apache/doris#66120.
*
* <p>All 12 Nereids {@code *_diff} scalar functions used to list their TIMESTAMPTZ signature
* FIRST in SIGNATURES. {@code SearchSignature}'s timezone-coercion tie-break only fires for
* inspectable literals ({@code ExpressionUtils.getLiteralAfterUnwrapNullable}), so a
* non-literal VARCHAR-typed argument (varchar column, subquery slot, UNION slot) ties across
* every candidate signature and the tie-break falls through to "keep the first-listed
* candidate" - which was always TIMESTAMPTZ. Casting a plain varchar through timestamptz(6)
* silently UTC-shifts the value whenever the session {@code time_zone} is not UTC.
*
* <p>The fix moves each file's TimeStampTz signature to LAST in SIGNATURES, so the tie
* resolves to DATETIMEV2 instead. Non-literal TIMESTAMPTZ-typed arguments must still bind to
* the TimeStampTzType signature - this class also guards against that regressing.
*/
public class DiffFunctionSignatureTest {

private static final List<NamedDiffConstructor> DIFF_CONSTRUCTORS = ImmutableList.of(
new NamedDiffConstructor("DateDiff", DateDiff::new),
new NamedDiffConstructor("DaysDiff", DaysDiff::new),
new NamedDiffConstructor("HoursDiff", HoursDiff::new),
new NamedDiffConstructor("MicroSecondsDiff", MicroSecondsDiff::new),
new NamedDiffConstructor("MilliSecondsDiff", MilliSecondsDiff::new),
new NamedDiffConstructor("MinutesDiff", MinutesDiff::new),
new NamedDiffConstructor("MonthsDiff", MonthsDiff::new),
new NamedDiffConstructor("QuartersDiff", QuartersDiff::new),
new NamedDiffConstructor("SecondsDiff", SecondsDiff::new),
new NamedDiffConstructor("TimeDiff", TimeDiff::new),
new NamedDiffConstructor("WeeksDiff", WeeksDiff::new),
new NamedDiffConstructor("YearsDiff", YearsDiff::new));

@Test
public void testVarcharSlotsBindToDateTimeV2NotTimeStampTz() {
SlotReference left = SlotReference.of("a", VarcharType.SYSTEM_DEFAULT);
SlotReference right = SlotReference.of("b", VarcharType.SYSTEM_DEFAULT);
for (NamedDiffConstructor c : DIFF_CONSTRUCTORS) {
FunctionSignature signature = c.constructor.apply(left, right).getSignature();
Assertions.assertInstanceOf(DateTimeV2Type.class, signature.getArgType(0),
c.name + ": varchar arg0 should bind to DateTimeV2Type, not TimeStampTzType (issue #66120)");
Assertions.assertInstanceOf(DateTimeV2Type.class, signature.getArgType(1),
c.name + ": varchar arg1 should bind to DateTimeV2Type, not TimeStampTzType (issue #66120)");
}
}

@Test
public void testTimeStampTzSlotsStillBindToTimeStampTz() {
SlotReference left = SlotReference.of("a", TimeStampTzType.of(6));
SlotReference right = SlotReference.of("b", TimeStampTzType.of(6));
for (NamedDiffConstructor c : DIFF_CONSTRUCTORS) {
FunctionSignature signature = c.constructor.apply(left, right).getSignature();
Assertions.assertInstanceOf(TimeStampTzType.class, signature.getArgType(0),
c.name + ": timestamptz arg0 should still bind to TimeStampTzType");
Assertions.assertInstanceOf(TimeStampTzType.class, signature.getArgType(1),
c.name + ": timestamptz arg1 should still bind to TimeStampTzType");
}
}

private static final class NamedDiffConstructor {
private final String name;
private final BiFunction<Expression, Expression, ComputeSignature> constructor;

private NamedDiffConstructor(String name, BiFunction<Expression, Expression, ComputeSignature> constructor) {
this.name = name;
this.constructor = constructor;
}
}
}