From e786cdb7d0949499b96b84b8529a7ae2dbabc951 Mon Sep 17 00:00:00 2001 From: Pxl Date: Tue, 26 Mar 2024 19:19:09 +0800 Subject: [PATCH] [Bug](repeat) fix core dump coz output slot'order on repeat node not match with pre repeat exprs (#32662) fix core dump coz output slot'order on repeat node not match with pre repeat exprs --- .../translator/PhysicalPlanTranslator.java | 22 ++++----- .../test_dup_mv_repeat/test_dup_mv_repeat.out | 5 ++ .../test_dup_mv_repeat.groovy | 47 +++++++++++++++++++ 3 files changed, 61 insertions(+), 13 deletions(-) create mode 100644 regression-test/data/mv_p0/test_dup_mv_repeat/test_dup_mv_repeat.out create mode 100644 regression-test/suites/mv_p0/test_dup_mv_repeat/test_dup_mv_repeat.groovy diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/glue/translator/PhysicalPlanTranslator.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/glue/translator/PhysicalPlanTranslator.java index 1842e193152bcd..31b01b4af22345 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/glue/translator/PhysicalPlanTranslator.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/glue/translator/PhysicalPlanTranslator.java @@ -1932,19 +1932,15 @@ public PlanFragment visitPhysicalRepeat(PhysicalRepeat repeat, P .map(NamedExpression::toSlot) .collect(ImmutableList.toImmutableList()); - Set usedSlotInRepeat = ImmutableSet.builder() - .addAll(flattenGroupingSetExprs) - .addAll(aggregateFunctionUsedSlots) - .build(); - - List preRepeatExprs = usedSlotInRepeat.stream() - .map(expr -> ExpressionTranslator.translate(expr, context)) - .collect(ImmutableList.toImmutableList()); - - List outputSlots = repeat.getOutputExpressions() - .stream() - .map(NamedExpression::toSlot) - .collect(ImmutableList.toImmutableList()); + // keep flattenGroupingSetExprs comes first + List preRepeatExprs = Stream.concat(flattenGroupingSetExprs.stream(), aggregateFunctionUsedSlots.stream()) + .map(expr -> ExpressionTranslator.translate(expr, context)).collect(ImmutableList.toImmutableList()); + + // outputSlots's order need same with preRepeatExprs + List outputSlots = Stream.concat( + repeat.getOutputExpressions().stream().filter(output -> flattenGroupingSetExprs.contains(output)), + repeat.getOutputExpressions().stream().filter(output -> !flattenGroupingSetExprs.contains(output))) + .map(NamedExpression::toSlot).collect(ImmutableList.toImmutableList()); // NOTE: we should first translate preRepeatExprs, then generate output tuple, // or else the preRepeatExprs can not find the bottom slotRef and throw diff --git a/regression-test/data/mv_p0/test_dup_mv_repeat/test_dup_mv_repeat.out b/regression-test/data/mv_p0/test_dup_mv_repeat/test_dup_mv_repeat.out new file mode 100644 index 00000000000000..a958cf0b819cf5 --- /dev/null +++ b/regression-test/data/mv_p0/test_dup_mv_repeat/test_dup_mv_repeat.out @@ -0,0 +1,5 @@ +-- This file is automatically generated. You should know what you did if you want to edit this +-- !select_mv -- +abc 123.0 +def 456.0 + diff --git a/regression-test/suites/mv_p0/test_dup_mv_repeat/test_dup_mv_repeat.groovy b/regression-test/suites/mv_p0/test_dup_mv_repeat/test_dup_mv_repeat.groovy new file mode 100644 index 00000000000000..0a40c3cb050577 --- /dev/null +++ b/regression-test/suites/mv_p0/test_dup_mv_repeat/test_dup_mv_repeat.groovy @@ -0,0 +1,47 @@ +// 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. + +import org.codehaus.groovy.runtime.IOGroovyMethods + +suite ("test_dup_mv_repeat") { + + sql """ DROP TABLE IF EXISTS d_table; """ + + sql """ + CREATE TABLE `db1` ( + `dt` date NULL, + `s` varchar(128) NULL, + `n` bigint NULL + ) ENGINE=OLAP + DUPLICATE KEY(`dt`) + DISTRIBUTED BY HASH(`dt`) BUCKETS 1 + PROPERTIES ( + "replication_allocation" = "tag.location.default: 1", + "storage_format" = "V2" + ); + """ + + sql "insert into db1 values('2020-01-01','abc',123),('2020-01-02','def',456);" + + createMV ("create materialized view dbviwe as select dt,s,sum(n) as n from db1 group by dt,s;") + + explain { + sql("SELECT s AS s, sum(n) / count(DISTINCT dt) AS n FROM db1 GROUP BY GROUPING SETS((s)) order by 1;") + contains "(dbviwe)" + } + qt_select_mv "SELECT s AS s, sum(n) / count(DISTINCT dt) AS n FROM db1 GROUP BY GROUPING SETS((s)) order by 1;" +}