Skip to content

Commit 0186c8a

Browse files
[fix](infer-predicate) Preserve datetime precision in predicate inference (apache#67653)
### What problem does this PR solve? Related PR: apache#41731 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.
1 parent fb864fb commit 0186c8a

4 files changed

Lines changed: 86 additions & 1 deletion

File tree

‎fe/fe-core/src/main/java/org/apache/doris/nereids/util/PredicateInferUtils.java‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,9 @@ private static Optional<Expression> validForInfer(Expression expression, InferTy
156156
if (childType.isTimeStampNsType()) {
157157
return Optional.empty();
158158
}
159-
return validForInfer(child, inferType);
159+
if (!(childType instanceof DateTimeV2Type) || childType.isInjectiveCastTo(dataType)) {
160+
return validForInfer(child, inferType);
161+
}
160162
}
161163
} else if (inferType == InferType.STRING) {
162164
// avoid substring cast such as cast(char(3) as char(2))

‎fe/fe-core/src/test/java/org/apache/doris/nereids/rules/rewrite/InferPredicateByReplaceTest.java‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,32 @@ public void testInferWithPredicateDateTrunc() {
173173
Assertions.assertEquals(3, result.size());
174174
}
175175

176+
@Test
177+
public void testInferWithDateTimeV2CastPrecision() {
178+
for (int sourceScale : new int[] {0, 3, 6}) {
179+
SlotReference a = new SlotReference("a", DateTimeV2Type.of(sourceScale));
180+
SlotReference b = new SlotReference("b", DateTimeV2Type.of(sourceScale));
181+
InPredicate predicate = new InPredicate(a, ImmutableList.of(
182+
new DateTimeV2Literal(DateTimeV2Type.of(sourceScale), "2025-01-01 00:00:00"),
183+
new DateTimeV2Literal(DateTimeV2Type.of(sourceScale), "2025-01-01 00:00:01")));
184+
InPredicate expected = new InPredicate(b, predicate.getOptions());
185+
for (int targetScale : new int[] {0, 3, 6}) {
186+
EqualTo equality = new EqualTo(new Cast(a, DateTimeV2Type.of(targetScale)),
187+
new Cast(b, DateTimeV2Type.of(targetScale)));
188+
Set<Expression> inputs = new HashSet<>(ImmutableList.of(equality, predicate));
189+
Assertions.assertEquals(sourceScale <= targetScale,
190+
InferPredicateByReplace.infer(inputs).contains(expected),
191+
"Unexpected inference for scale " + sourceScale + " -> " + targetScale);
192+
}
193+
EqualTo nestedEquality = new EqualTo(
194+
new Cast(new Cast(a, DateTimeV2Type.of(0)), DateTimeV2Type.of(6)),
195+
new Cast(new Cast(b, DateTimeV2Type.of(0)), DateTimeV2Type.of(6)));
196+
Set<Expression> inputs = new HashSet<>(ImmutableList.of(nestedEquality, predicate));
197+
Assertions.assertEquals(sourceScale == 0, InferPredicateByReplace.infer(inputs).contains(expected),
198+
"Nested casts must not hide precision loss");
199+
}
200+
}
201+
176202
@Test
177203
public void testValidForInfer() {
178204
SlotReference a = new SlotReference("a", TinyIntType.INSTANCE);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-- This file is automatically generated. You should know what you did if you want to edit this
2+
-- !without_inference --
3+
1 1 10
4+
5+
-- !with_inference --
6+
1 1 10
7+
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
suite("infer_datetimev2_cast_precision", "p0") {
18+
sql "DROP TABLE IF EXISTS infer_datetimev2_cast_l"
19+
sql "DROP TABLE IF EXISTS infer_datetimev2_cast_r"
20+
sql """
21+
CREATE TABLE infer_datetimev2_cast_l (id INT, a DATETIMEV2(6))
22+
DUPLICATE KEY(id)
23+
DISTRIBUTED BY HASH(id) BUCKETS 1
24+
PROPERTIES ("replication_num" = "1")
25+
"""
26+
sql """
27+
CREATE TABLE infer_datetimev2_cast_r (id INT, b DATETIMEV2(6))
28+
DUPLICATE KEY(id)
29+
DISTRIBUTED BY HASH(id) BUCKETS 1
30+
PROPERTIES ("replication_num" = "1")
31+
"""
32+
sql "INSERT INTO infer_datetimev2_cast_l VALUES (1, '2025-01-01 00:00:00.100000')"
33+
sql "INSERT INTO infer_datetimev2_cast_r VALUES (10, '2025-01-01 00:00:00.200000')"
34+
35+
// Distinct raw values become equal after narrowing the fractional-second precision.
36+
sql "SET disable_nereids_rules = 'INFER_PREDICATES'"
37+
order_qt_without_inference """
38+
SELECT COUNT(*) AS cnt, MIN(l.id) AS left_id, MIN(r.id) AS right_id
39+
FROM infer_datetimev2_cast_l l JOIN infer_datetimev2_cast_r r
40+
ON CAST(l.a AS DATETIMEV2(0)) = CAST(r.b AS DATETIMEV2(0))
41+
WHERE l.a IN ('2025-01-01 00:00:00.100000', '2025-01-01 00:00:00.300000')
42+
"""
43+
sql "SET disable_nereids_rules = ''"
44+
order_qt_with_inference """
45+
SELECT COUNT(*) AS cnt, MIN(l.id) AS left_id, MIN(r.id) AS right_id
46+
FROM infer_datetimev2_cast_l l JOIN infer_datetimev2_cast_r r
47+
ON CAST(l.a AS DATETIMEV2(0)) = CAST(r.b AS DATETIMEV2(0))
48+
WHERE l.a IN ('2025-01-01 00:00:00.100000', '2025-01-01 00:00:00.300000')
49+
"""
50+
}

0 commit comments

Comments
 (0)