From 6bf2e6b2891a0386c3d25001eae41a2a904d81fb Mon Sep 17 00:00:00 2001 From: feiniaofeiafei Date: Tue, 8 Sep 2026 16:18:35 +0800 Subject: [PATCH 1/2] [fix](fe) Preserve datetime precision in predicate inference Problem Summary: Predicate inference unwraps narrowing DATETIMEV2 casts and incorrectly treats distinct raw join keys as equal. An IN predicate on one side can then filter valid matches on the other side. Require injective DATETIMEV2 casts before unwrapping, including nested casts, while preserving same-scale and widening inference. Add one unit test and a regression case comparing inference enabled and disabled. Fix missing join results caused by predicate inference across narrowing DATETIMEV2 casts. - Test: Checkstyle passed; remote unit and regression validation pending - Behavior changed: Yes, preserve valid matches for narrowing datetime casts - Does this need documentation: No --- .../nereids/util/PredicateInferUtils.java | 4 +- .../rewrite/InferPredicateByReplaceTest.java | 26 ++++++++++ .../infer_datetimev2_cast_precision.groovy | 50 +++++++++++++++++++ 3 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 regression-test/suites/nereids_rules_p0/infer_predicate/infer_datetimev2_cast_precision.groovy diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/util/PredicateInferUtils.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/util/PredicateInferUtils.java index d6f7dfc145c732..2a3ac016c49819 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/util/PredicateInferUtils.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/util/PredicateInferUtils.java @@ -156,7 +156,9 @@ private static Optional validForInfer(Expression expression, InferTy if (childType.isTimeStampNsType()) { return Optional.empty(); } - return validForInfer(child, inferType); + if (!(childType instanceof DateTimeV2Type) || childType.isInjectiveCastTo(dataType)) { + return validForInfer(child, inferType); + } } } else if (inferType == InferType.STRING) { // avoid substring cast such as cast(char(3) as char(2)) diff --git a/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/rewrite/InferPredicateByReplaceTest.java b/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/rewrite/InferPredicateByReplaceTest.java index 87f88157a0f348..499f9fc08834f9 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/rewrite/InferPredicateByReplaceTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/rewrite/InferPredicateByReplaceTest.java @@ -173,6 +173,32 @@ public void testInferWithPredicateDateTrunc() { Assertions.assertEquals(3, result.size()); } + @Test + public void testInferWithDateTimeV2CastPrecision() { + for (int sourceScale : new int[] {0, 3, 6}) { + SlotReference a = new SlotReference("a", DateTimeV2Type.of(sourceScale)); + SlotReference b = new SlotReference("b", DateTimeV2Type.of(sourceScale)); + InPredicate predicate = new InPredicate(a, ImmutableList.of( + new DateTimeV2Literal("2025-01-01 00:00:00"), + new DateTimeV2Literal("2025-01-01 00:00:01"))); + InPredicate expected = new InPredicate(b, predicate.getOptions()); + for (int targetScale : new int[] {0, 3, 6}) { + EqualTo equality = new EqualTo(new Cast(a, DateTimeV2Type.of(targetScale)), + new Cast(b, DateTimeV2Type.of(targetScale))); + Set inputs = new HashSet<>(ImmutableList.of(equality, predicate)); + Assertions.assertEquals(sourceScale <= targetScale, + InferPredicateByReplace.infer(inputs).contains(expected), + "Unexpected inference for scale " + sourceScale + " -> " + targetScale); + } + EqualTo nestedEquality = new EqualTo( + new Cast(new Cast(a, DateTimeV2Type.of(0)), DateTimeV2Type.of(6)), + new Cast(new Cast(b, DateTimeV2Type.of(0)), DateTimeV2Type.of(6))); + Set inputs = new HashSet<>(ImmutableList.of(nestedEquality, predicate)); + Assertions.assertEquals(sourceScale == 0, InferPredicateByReplace.infer(inputs).contains(expected), + "Nested casts must not hide precision loss"); + } + } + @Test public void testValidForInfer() { SlotReference a = new SlotReference("a", TinyIntType.INSTANCE); diff --git a/regression-test/suites/nereids_rules_p0/infer_predicate/infer_datetimev2_cast_precision.groovy b/regression-test/suites/nereids_rules_p0/infer_predicate/infer_datetimev2_cast_precision.groovy new file mode 100644 index 00000000000000..5047cd9a0e7d0f --- /dev/null +++ b/regression-test/suites/nereids_rules_p0/infer_predicate/infer_datetimev2_cast_precision.groovy @@ -0,0 +1,50 @@ +// 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("infer_datetimev2_cast_precision", "p0") { + sql "DROP TABLE IF EXISTS infer_datetimev2_cast_l" + sql "DROP TABLE IF EXISTS infer_datetimev2_cast_r" + sql """ + CREATE TABLE infer_datetimev2_cast_l (id INT, a DATETIMEV2(6)) + DUPLICATE KEY(id) + DISTRIBUTED BY HASH(id) BUCKETS 1 + PROPERTIES ("replication_num" = "1") + """ + sql """ + CREATE TABLE infer_datetimev2_cast_r (id INT, b DATETIMEV2(6)) + DUPLICATE KEY(id) + DISTRIBUTED BY HASH(id) BUCKETS 1 + PROPERTIES ("replication_num" = "1") + """ + sql "INSERT INTO infer_datetimev2_cast_l VALUES (1, '2025-01-01 00:00:00.100000')" + sql "INSERT INTO infer_datetimev2_cast_r VALUES (10, '2025-01-01 00:00:00.200000')" + + // Distinct raw values become equal after narrowing the fractional-second precision. + sql "SET disable_nereids_rules = 'INFER_PREDICATES'" + order_qt_without_inference """ + SELECT COUNT(*) AS cnt, MIN(l.id) AS left_id, MIN(r.id) AS right_id + FROM infer_datetimev2_cast_l l JOIN infer_datetimev2_cast_r r + ON CAST(l.a AS DATETIMEV2(0)) = CAST(r.b AS DATETIMEV2(0)) + WHERE l.a IN ('2025-01-01 00:00:00.100000', '2025-01-01 00:00:00.300000') + """ + sql "SET disable_nereids_rules = ''" + order_qt_with_inference """ + SELECT COUNT(*) AS cnt, MIN(l.id) AS left_id, MIN(r.id) AS right_id + FROM infer_datetimev2_cast_l l JOIN infer_datetimev2_cast_r r + ON CAST(l.a AS DATETIMEV2(0)) = CAST(r.b AS DATETIMEV2(0)) + WHERE l.a IN ('2025-01-01 00:00:00.100000', '2025-01-01 00:00:00.300000') + """ +} From db0ed53251f1ef5951ff81b1fb77998e9916f04b Mon Sep 17 00:00:00 2001 From: feiniaofeiafei Date: Tue, 8 Sep 2026 16:31:38 +0800 Subject: [PATCH 2/2] [test](fe) Verify datetime cast predicate inference results ### What problem does this PR solve? Problem Summary: Record generated regression output proving that narrowing DATETIMEV2 join casts preserve matching rows with predicate inference both enabled and disabled. Match unit-test literal precision to the source slots so expression comparisons test inference without unrelated type coercion. ### Release note None ### Check List (For Author) - Test: Unit Test / Regression test / Manual test - Checkstyle passed - DISABLE_BUILD_UI=ON ./build.sh --fe passed on dev - ./run-fe-ut.sh --run org.apache.doris.nereids.rules.rewrite.InferPredicateByReplaceTest: 11 passed - infer_datetimev2_cast_precision regression suite: 1 passed - Old FE returned 0 rows with inference enabled; updated FE returns the expected match - EXPLAIN confirms the right scan has no incorrect raw IN predicate - Behavior changed: No, test-only changes - Does this need documentation: No --- .../nereids/rules/rewrite/InferPredicateByReplaceTest.java | 4 ++-- .../infer_predicate/infer_datetimev2_cast_precision.out | 7 +++++++ 2 files changed, 9 insertions(+), 2 deletions(-) create mode 100644 regression-test/data/nereids_rules_p0/infer_predicate/infer_datetimev2_cast_precision.out diff --git a/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/rewrite/InferPredicateByReplaceTest.java b/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/rewrite/InferPredicateByReplaceTest.java index 499f9fc08834f9..5c174cb6348f10 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/rewrite/InferPredicateByReplaceTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/rewrite/InferPredicateByReplaceTest.java @@ -179,8 +179,8 @@ public void testInferWithDateTimeV2CastPrecision() { SlotReference a = new SlotReference("a", DateTimeV2Type.of(sourceScale)); SlotReference b = new SlotReference("b", DateTimeV2Type.of(sourceScale)); InPredicate predicate = new InPredicate(a, ImmutableList.of( - new DateTimeV2Literal("2025-01-01 00:00:00"), - new DateTimeV2Literal("2025-01-01 00:00:01"))); + new DateTimeV2Literal(DateTimeV2Type.of(sourceScale), "2025-01-01 00:00:00"), + new DateTimeV2Literal(DateTimeV2Type.of(sourceScale), "2025-01-01 00:00:01"))); InPredicate expected = new InPredicate(b, predicate.getOptions()); for (int targetScale : new int[] {0, 3, 6}) { EqualTo equality = new EqualTo(new Cast(a, DateTimeV2Type.of(targetScale)), diff --git a/regression-test/data/nereids_rules_p0/infer_predicate/infer_datetimev2_cast_precision.out b/regression-test/data/nereids_rules_p0/infer_predicate/infer_datetimev2_cast_precision.out new file mode 100644 index 00000000000000..d037fd99f05b56 --- /dev/null +++ b/regression-test/data/nereids_rules_p0/infer_predicate/infer_datetimev2_cast_precision.out @@ -0,0 +1,7 @@ +-- This file is automatically generated. You should know what you did if you want to edit this +-- !without_inference -- +1 1 10 + +-- !with_inference -- +1 1 10 +