From 86cbc469d0d458863d2de6ca180c81da6d7ae4ff Mon Sep 17 00:00:00 2001 From: 924060929 Date: Thu, 22 May 2025 11:30:32 +0800 Subject: [PATCH] [fix](nereids) fix subquery unnest can not found aggregate slot (#51086) when exists multiple subquery in a query block ```sql select case when t1.k1=1 then (select count(*) from t2 where t1.k2=t2.k2) -- count(*)#7 when t1.k1=2 then (select count(*) from t3 where t1.k2=t3.k2) -- count(*)#12 else 0 end as kk from t1 order by kk ``` the aggregate function output maybe bind failed and then throw exception ``` org.apache.doris.nereids.exceptions.AnalysisException: Input slot(s) not in child's output: count(*)#7 ``` because we only replace the last aggregate function slot(only replace the `#12`, but not replace the `#7` ): this pr fix subquery unnest can not found aggregate slot, introduced by #39471 --- .../nereids/rules/analysis/SubqueryToApply.java | 2 +- .../nereids_p0/subquery/subquery_unnesting.out | 16 ++++++++++++++++ .../subquery/subquery_unnesting.groovy | 9 +++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/SubqueryToApply.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/SubqueryToApply.java index d64c7f2baed6a4..cacd3d9cd6ce02 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/SubqueryToApply.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/SubqueryToApply.java @@ -381,7 +381,7 @@ private Pair> subqueryToApply( if (!ctx.subqueryIsAnalyzed(subqueryExpr)) { tmpPlan = addApply(subqueryExpr, tmpPlan.first, - subqueryToMarkJoinSlot, ctx, conjunct, + subqueryToMarkJoinSlot, ctx, tmpPlan.second, isProject, subqueryExprs.size() == 1, isMarkJoinSlotNotNull); } } diff --git a/regression-test/data/nereids_p0/subquery/subquery_unnesting.out b/regression-test/data/nereids_p0/subquery/subquery_unnesting.out index 5124b5f9700ed9..535e10059ad7ae 100644 --- a/regression-test/data/nereids_p0/subquery/subquery_unnesting.out +++ b/regression-test/data/nereids_p0/subquery/subquery_unnesting.out @@ -1519,3 +1519,19 @@ 20 2 22 3 24 4 + +-- !select64 -- +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +2 + diff --git a/regression-test/suites/nereids_p0/subquery/subquery_unnesting.groovy b/regression-test/suites/nereids_p0/subquery/subquery_unnesting.groovy index 397c5cfbf29bc7..8fa6524aefac68 100644 --- a/regression-test/suites/nereids_p0/subquery/subquery_unnesting.groovy +++ b/regression-test/suites/nereids_p0/subquery/subquery_unnesting.groovy @@ -133,4 +133,13 @@ suite ("subquery_unnesting") { qt_select61 """SELECT * FROM t1 AS t1 WHERE EXISTS (SELECT k1 FROM t1 AS t2 WHERE t1.k1 <> t2.k1 + 7 GROUP BY k1 HAVING k1 >= 100);""" qt_select62 """select * from t1 left semi join ( select * from t1 where t1.k1 < -1 ) l on true;""" qt_select63 """SELECT * FROM t1 AS t1 WHERE EXISTS (SELECT k1 FROM t1 AS t2 WHERE t1.k1 <> t2.k1 + 7 GROUP BY k1 HAVING sum(k2) >= 1) order by t1.k1, t1.k2;""" + + qt_select64 """ + select case + when t1.k1=1 then (select count(*) from t2 where t1.k2=t2.k2) + when t1.k1=2 then (select count(*) from t3 where t1.k2=t3.k2) + else 0 end as kk + from t1 + order by kk + """ }