From 6dcdb03a970cdaf99c455acb6929574ce2d3c39b Mon Sep 17 00:00:00 2001 From: LiBinfeng <46676950+LiBinfeng-01@users.noreply.github.com> Date: Fri, 20 Sep 2024 10:14:16 +0800 Subject: [PATCH 1/2] [fix](Nereids) fix fold constant by be return type mismatched (#39723) when using fold constant by be, the return type of substring('123456', 1, 3) would changed to be text, which we want it to be 3 --- .../rules/expression/rules/FoldConstantRuleOnBE.java | 11 ++++++++++- .../fold_constant/fold_constant_by_be.groovy | 7 ++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/FoldConstantRuleOnBE.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/FoldConstantRuleOnBE.java index 1739c147d8678d..40c06b76036c17 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/FoldConstantRuleOnBE.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/FoldConstantRuleOnBE.java @@ -109,7 +109,16 @@ private Expression replace(Expression root, Map constMap, Ma if (newChild != child) { hasNewChildren = true; } - newChildren.add(newChild); + if (!newChild.getDataType().equals(child.getDataType())) { + try { + newChildren.add(newChild.castTo(child.getDataType())); + } catch (Exception e) { + LOG.warn("expression of type {} cast to {} failed. ", newChild.getDataType(), child.getDataType()); + newChildren.add(newChild); + } + } else { + newChildren.add(newChild); + } } return hasNewChildren ? root.withChildren(newChildren) : root; } diff --git a/regression-test/suites/nereids_p0/expression/fold_constant/fold_constant_by_be.groovy b/regression-test/suites/nereids_p0/expression/fold_constant/fold_constant_by_be.groovy index e1162c662b9124..2a59de195a635e 100644 --- a/regression-test/suites/nereids_p0/expression/fold_constant/fold_constant_by_be.groovy +++ b/regression-test/suites/nereids_p0/expression/fold_constant/fold_constant_by_be.groovy @@ -40,4 +40,9 @@ suite("fold_constant_by_be") { sql """ INSERT INTO str_tb VALUES (2, repeat("test1111", 10000)); """ qt_sql_1 """ select length(v1) from str_tb; """ -} \ No newline at end of file + + explain { + sql("verbose select substring('123456', 1, 3)") + contains "varchar(3)" + } +} From dc5036aacc54dcaa5b143c71426ee1eb618da21a Mon Sep 17 00:00:00 2001 From: LiBinfeng <46676950+LiBinfeng-01@users.noreply.github.com> Date: Fri, 27 Sep 2024 19:46:26 +0800 Subject: [PATCH 2/2] [Fix](Nereids) fix fold const be return type (#41164) remove windowframe in window expression to avoid folding constant on be --- .../trees/expressions/WindowExpression.java | 4 ++- .../fold_constant/fold_constant_by_be.groovy | 25 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/WindowExpression.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/WindowExpression.java index c9531d61b67187..34026e4414ecbb 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/WindowExpression.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/WindowExpression.java @@ -66,7 +66,6 @@ public WindowExpression(Expression function, List partitionKeys, Lis .add(function) .addAll(partitionKeys) .addAll(orderKeys) - .add(windowFrame) .build()); this.function = function; this.partitionKeys = ImmutableList.copyOf(partitionKeys); @@ -147,6 +146,9 @@ public WindowExpression withChildren(List children) { if (index < children.size()) { return new WindowExpression(func, partitionKeys, orderKeys, (WindowFrame) children.get(index)); } + if (windowFrame.isPresent()) { + return new WindowExpression(func, partitionKeys, orderKeys, windowFrame.get()); + } return new WindowExpression(func, partitionKeys, orderKeys); } diff --git a/regression-test/suites/nereids_p0/expression/fold_constant/fold_constant_by_be.groovy b/regression-test/suites/nereids_p0/expression/fold_constant/fold_constant_by_be.groovy index 2a59de195a635e..f6a67d5e8bb503 100644 --- a/regression-test/suites/nereids_p0/expression/fold_constant/fold_constant_by_be.groovy +++ b/regression-test/suites/nereids_p0/expression/fold_constant/fold_constant_by_be.groovy @@ -45,4 +45,29 @@ suite("fold_constant_by_be") { sql("verbose select substring('123456', 1, 3)") contains "varchar(3)" } + + sql "drop table if exists table_200_undef_partitions2_keys3_properties4_distributed_by53" + sql """create table table_200_undef_partitions2_keys3_properties4_distributed_by53 ( + pk int, + col_char_255__undef_signed char(255) null , + col_char_100__undef_signed char(100) null , + col_char_255__undef_signed_not_null char(255) not null , + col_char_100__undef_signed_not_null char(100) not null , + col_varchar_255__undef_signed varchar(255) null , + col_varchar_255__undef_signed_not_null varchar(255) not null , + col_varchar_1000__undef_signed varchar(1000) null , + col_varchar_1000__undef_signed_not_null varchar(1000) not null , + col_varchar_1001__undef_signed varchar(1001) null , + col_varchar_1001__undef_signed_not_null varchar(1001) not null + ) engine=olap + DUPLICATE KEY(pk, col_char_255__undef_signed, col_char_100__undef_signed) + distributed by hash(pk) buckets 10 + properties("replication_num" = "1");""" + explain { + sql("select LAST_VALUE(col_char_255__undef_signed_not_null, false) over (partition by " + + "concat('GkIPbzAZSu', col_char_100__undef_signed), mask('JrqFkEDqeA') " + + "order by pk rows between unbounded preceding and 6 following) AS col_alias26947 " + + "from table_200_undef_partitions2_keys3_properties4_distributed_by53;") + notContains("mask") + } }