-
Notifications
You must be signed in to change notification settings - Fork 3.9k
[fix](function) Prefer DATETIMEV2 over TIMESTAMPTZ when binding *_diff functions on non-literal string args #67238
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lets-order-some-fries
wants to merge
1
commit into
apache:master
Choose a base branch
from
lets-order-some-fries:fix-diff-varchar-tz-order
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+134
−36
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
...rg/apache/doris/nereids/trees/expressions/functions/scalar/DiffFunctionSignatureTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 = 0and ties DATETIMEV2. Withtime_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, butDateTimeCheckerrequires a colon before offset minutes, so pairing2021-03-15 00:00:00-0700with2021-03-14 00:00:00-08:00again 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.