diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/ExpressionAnalyzer.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/ExpressionAnalyzer.java index 8db98362a9cc84..a95801472731f5 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/ExpressionAnalyzer.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/ExpressionAnalyzer.java @@ -290,7 +290,11 @@ public Expression visitUnboundSlot(UnboundSlot unboundSlot, ExpressionRewriteCon if (bindSlotInOuterScope && !foundInThisScope && outerScope.isPresent()) { boundedOpt = Optional.of(bindSlotByScope(unboundSlot, outerScope.get())); } + // it is heavy to deduplicate slots in scope. So we deduplicates bounded here List bounded = boundedOpt.get(); + if (bounded.size() > 1) { + bounded = bounded.stream().distinct().collect(Collectors.toList()); + } switch (bounded.size()) { case 0: String tableName = StringUtils.join(unboundSlot.getQualifier(), "."); diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/copier/LogicalPlanDeepCopier.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/copier/LogicalPlanDeepCopier.java index 5c5b98b26d797f..71984291d9cb3b 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/copier/LogicalPlanDeepCopier.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/copier/LogicalPlanDeepCopier.java @@ -188,7 +188,7 @@ public Plan visitLogicalAggregate(LogicalAggregate aggregate, De .collect(ImmutableList.toImmutableList()); LogicalAggregate copiedAggregate = aggregate.withChildGroupByAndOutput(groupByExpressions, outputExpressions, child); - Optional> childRepeat = + Optional> childRepeat = copiedAggregate.collectFirst(LogicalRepeat.class::isInstance); return childRepeat.isPresent() ? aggregate.withChildGroupByAndOutputAndSourceRepeat( groupByExpressions, outputExpressions, child, childRepeat) diff --git a/regression-test/suites/nereids_p0/slot_bind/test_bind_slot.groovy b/regression-test/suites/nereids_p0/slot_bind/test_bind_slot.groovy new file mode 100644 index 00000000000000..6d49c7fb49a2a3 --- /dev/null +++ b/regression-test/suites/nereids_p0/slot_bind/test_bind_slot.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. + + +suite("test_bind_slot") { + sql """ + drop table if exists t; + CREATE TABLE t ( + `id` int COMMENT '', + `status` string COMMENT '', + `time_created` string COMMENT '', + ) properties("replication_num" = "1"); + + """ + // if scope is not deduplicated, time_created wil be bound twice, causing a slot ambigious error + sql """select + from_unixtime(time_created / 1000, 'yyyyMMdd') as date1, + status, + count(distinct id) as cnt + from + ( + select + from_unixtime(time_created / 1001) as created_date, + time_created, + * + from + t + ) t1 + group by + from_unixtime(time_created / 1000, 'yyyyMMdd'), + status; + """ +}