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 @@ -156,7 +156,9 @@ private static Optional<Expression> 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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(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)),
new Cast(b, DateTimeV2Type.of(targetScale)));
Set<Expression> 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<Expression> 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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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

Original file line number Diff line number Diff line change
@@ -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')
"""
}
Loading