diff --git a/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/expression/rules/SimplifyComparisonPredicateTest.java b/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/expression/rules/SimplifyComparisonPredicateTest.java index dda78db577aae0..2feec78c737cb5 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/expression/rules/SimplifyComparisonPredicateTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/expression/rules/SimplifyComparisonPredicateTest.java @@ -1091,6 +1091,24 @@ void testTypeRangeLimitPreservesCastNullability() { ExpressionUtils.trueOrNull(nullableCast)); } + SlotReference nonNullableInt = new SlotReference("int_slot", IntegerType.INSTANCE, false); + DecimalV3Type narrowDecimal = DecimalV3Type.createDecimalV3Type(2, 0); + DecimalV3Literal decimalMin = new DecimalV3Literal(new BigDecimal("-99")); + DecimalV3Literal decimalMax = new DecimalV3Literal(new BigDecimal("99")); + List nullableDecimalCasts = ImmutableList.of( + new Cast(nonNullableInt, narrowDecimal), + new TryCast(nonNullableInt, narrowDecimal)); + for (Cast nullableCast : nullableDecimalCasts) { + assertRewrite(new LessThanEqual(nullableCast, decimalMax), + ExpressionUtils.trueOrNull(nullableCast)); + assertRewrite(new GreaterThanEqual(nullableCast, decimalMin), + ExpressionUtils.trueOrNull(nullableCast)); + assertRewrite(new GreaterThan(nullableCast, decimalMax), + ExpressionUtils.falseOrNull(nullableCast)); + assertRewrite(new LessThan(nullableCast, decimalMin), + ExpressionUtils.falseOrNull(nullableCast)); + } + SlotReference nonNullableTinyInt = new SlotReference("tinyint_slot", TinyIntType.INSTANCE, false); List safeCasts = ImmutableList.of( new Cast(nonNullableTinyInt, SmallIntType.INSTANCE), @@ -1099,6 +1117,15 @@ void testTypeRangeLimitPreservesCastNullability() { assertRewrite(new GreaterThan(safeCast, new SmallIntLiteral((short) 127)), BooleanLiteral.FALSE); } + + DecimalV3Type widerDecimal = DecimalV3Type.createDecimalV3Type(3, 0); + List safeDecimalCasts = ImmutableList.of( + new Cast(nonNullableTinyInt, widerDecimal), + new TryCast(nonNullableTinyInt, widerDecimal)); + for (Cast safeCast : safeDecimalCasts) { + assertRewrite(new GreaterThan(safeCast, new DecimalV3Literal(new BigDecimal("127"))), + BooleanLiteral.FALSE); + } } @Test diff --git a/regression-test/data/nereids_rules_p0/expression/test_narrow_decimal_cast_nullability.out b/regression-test/data/nereids_rules_p0/expression/test_narrow_decimal_cast_nullability.out new file mode 100644 index 00000000000000..67dce03f78211d --- /dev/null +++ b/regression-test/data/nereids_rules_p0/expression/test_narrow_decimal_cast_nullability.out @@ -0,0 +1,54 @@ +-- This file is automatically generated. You should know what you did if you want to edit this +-- !projection_enabled -- +-100 \N \N \N \N \N \N \N \N +-99 true true false false true true false false +0 true true false false true true false false +100 \N \N \N \N \N \N \N \N +99 true true false false true true false false + +-- !where_cast_upper_enabled -- +-99 +0 +99 + +-- !where_cast_lower_enabled -- +-99 +0 +99 + +-- !where_try_cast_upper_enabled -- +-99 +0 +99 + +-- !where_try_cast_lower_enabled -- +-99 +0 +99 + +-- !projection_disabled -- +-100 \N \N \N \N \N \N \N \N +-99 true true false false true true false false +0 true true false false true true false false +100 \N \N \N \N \N \N \N \N +99 true true false false true true false false + +-- !where_cast_upper_disabled -- +-99 +0 +99 + +-- !where_cast_lower_disabled -- +-99 +0 +99 + +-- !where_try_cast_upper_disabled -- +-99 +0 +99 + +-- !where_try_cast_lower_disabled -- +-99 +0 +99 diff --git a/regression-test/suites/nereids_rules_p0/expression/test_narrow_decimal_cast_nullability.groovy b/regression-test/suites/nereids_rules_p0/expression/test_narrow_decimal_cast_nullability.groovy new file mode 100644 index 00000000000000..e1675bf34f4d6c --- /dev/null +++ b/regression-test/suites/nereids_rules_p0/expression/test_narrow_decimal_cast_nullability.groovy @@ -0,0 +1,114 @@ +// 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("test_narrow_decimal_cast_nullability") { + sql "drop table if exists narrow_decimal_cast_nullability" + sql """ + create table narrow_decimal_cast_nullability ( + id int not null + ) duplicate key(id) + distributed by hash(id) buckets 1 + properties("replication_num" = "1") + """ + sql "insert into narrow_decimal_cast_nullability values (-100), (-99), (0), (99), (100)" + + sql "set enable_strict_cast = false" + sql "set detail_shape_nodes = 'PhysicalProject'" + try { + explain { + sql """ + shape plan + select cast(id as decimalv3(2, 0)) <= 99, + try_cast(id as decimalv3(2, 0)) > 99 + from narrow_decimal_cast_nullability + """ + contains "OR[( not cast(narrow_decimal_cast_nullability.id as DECIMALV3(2, 0)) IS NULL),NULL]" + contains "AND[tryCast(narrow_decimal_cast_nullability.id as DECIMALV3(2, 0)) IS NULL,NULL]" + } + + order_qt_projection_enabled """ + select id, + cast(id as decimalv3(2, 0)) <= 99, + cast(id as decimalv3(2, 0)) >= -99, + cast(id as decimalv3(2, 0)) > 99, + cast(id as decimalv3(2, 0)) < -99, + try_cast(id as decimalv3(2, 0)) <= 99, + try_cast(id as decimalv3(2, 0)) >= -99, + try_cast(id as decimalv3(2, 0)) > 99, + try_cast(id as decimalv3(2, 0)) < -99 + from narrow_decimal_cast_nullability + order by id + """ + order_qt_where_cast_upper_enabled """ + select id from narrow_decimal_cast_nullability + where cast(id as decimalv3(2, 0)) <= 99 + order by id + """ + order_qt_where_cast_lower_enabled """ + select id from narrow_decimal_cast_nullability + where cast(id as decimalv3(2, 0)) >= -99 + order by id + """ + order_qt_where_try_cast_upper_enabled """ + select id from narrow_decimal_cast_nullability + where try_cast(id as decimalv3(2, 0)) <= 99 + order by id + """ + order_qt_where_try_cast_lower_enabled """ + select id from narrow_decimal_cast_nullability + where try_cast(id as decimalv3(2, 0)) >= -99 + order by id + """ + + sql "set disable_nereids_expression_rules = 'SIMPLIFY_COMPARISON_PREDICATE'" + order_qt_projection_disabled """ + select id, + cast(id as decimalv3(2, 0)) <= 99, + cast(id as decimalv3(2, 0)) >= -99, + cast(id as decimalv3(2, 0)) > 99, + cast(id as decimalv3(2, 0)) < -99, + try_cast(id as decimalv3(2, 0)) <= 99, + try_cast(id as decimalv3(2, 0)) >= -99, + try_cast(id as decimalv3(2, 0)) > 99, + try_cast(id as decimalv3(2, 0)) < -99 + from narrow_decimal_cast_nullability + order by id + """ + order_qt_where_cast_upper_disabled """ + select id from narrow_decimal_cast_nullability + where cast(id as decimalv3(2, 0)) <= 99 + order by id + """ + order_qt_where_cast_lower_disabled """ + select id from narrow_decimal_cast_nullability + where cast(id as decimalv3(2, 0)) >= -99 + order by id + """ + order_qt_where_try_cast_upper_disabled """ + select id from narrow_decimal_cast_nullability + where try_cast(id as decimalv3(2, 0)) <= 99 + order by id + """ + order_qt_where_try_cast_lower_disabled """ + select id from narrow_decimal_cast_nullability + where try_cast(id as decimalv3(2, 0)) >= -99 + order by id + """ + } finally { + sql "set disable_nereids_expression_rules = ''" + } +}