From e9d9265150e3b5fda9a4c6772c5985cbca9e01fa Mon Sep 17 00:00:00 2001 From: xueweizhang Date: Tue, 12 Mar 2024 17:59:04 +0800 Subject: [PATCH] [fix](planner) remove input slot for aggregate slot which is not materialized (#32092) introduced by #26886 run this sql: SELECT caseId FROM ( SELECT caseId, count(judgementDateId) FROM ( SELECT abs(caseId) AS caseId, id as judgementDateId FROM dr_user_test_t2 ) AGG_RESULT GROUP BY caseId ) TOTAL order by 1; will get: ERROR 1105 (HY000): errCode = 2, detailMessage = (172.17.0.1)[INTERNAL_ERROR]couldn't resolve slot descriptor 1, desc: tuples: Tuple(id=5 slots=[Slot(id=10 type=DOUBLE col=-1, colname=, nullable=1), Slot(id=11 type=VARCHAR col=-1, colname=id, nullable=1)] has_varlen_slots=1) Tuple(id=4 slots=[Slot(id=8 type=DOUBLE col=-1, colname=, nullable=1)] has_varlen_slots=0) Tuple(id=2 slots=[Slot(id=4 type=DOUBLE col=-1, colname=caseId, nullable=1)] has_varlen_slots=0) Tuple(id=0 slots=[Slot(id=0 type=VARCHAR col=-1, colname=caseId, nu --- .../apache/doris/planner/AggregationNode.java | 11 +++++ .../test_inlineview_with_project.out | 10 +++++ .../test_inlineview_with_project.groovy | 44 +++++++++++++++++++ 3 files changed, 65 insertions(+) diff --git a/fe/fe-core/src/main/java/org/apache/doris/planner/AggregationNode.java b/fe/fe-core/src/main/java/org/apache/doris/planner/AggregationNode.java index a842507673ffba..b0a4fcf2db35a8 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/planner/AggregationNode.java +++ b/fe/fe-core/src/main/java/org/apache/doris/planner/AggregationNode.java @@ -24,6 +24,7 @@ import org.apache.doris.analysis.Analyzer; import org.apache.doris.analysis.Expr; import org.apache.doris.analysis.FunctionCallExpr; +import org.apache.doris.analysis.SlotDescriptor; import org.apache.doris.analysis.SlotId; import org.apache.doris.analysis.TupleDescriptor; import org.apache.doris.common.NotImplementedException; @@ -363,6 +364,16 @@ public Set computeInputSlotIds(Analyzer analyzer) throws NotImplementedE result.add(tupleDesc.getMaterializedSlots().get(0).getId()); } } + // if some input slot for aggregate slot which is not materialized, we need to remove it from the result + TupleDescriptor tupleDescriptor = aggInfo.getOutputTupleDesc(); + ArrayList slots = tupleDescriptor.getSlots(); + for (SlotDescriptor slot : slots) { + if (!slot.isMaterialized()) { + List unRequestIds = Lists.newArrayList(); + Expr.getIds(slot.getSourceExprs(), null, unRequestIds); + unRequestIds.forEach(result::remove); + } + } return result; } diff --git a/regression-test/data/correctness_p0/test_inlineview_with_project.out b/regression-test/data/correctness_p0/test_inlineview_with_project.out index 550b958d4d9da0..20135da100ad25 100644 --- a/regression-test/data/correctness_p0/test_inlineview_with_project.out +++ b/regression-test/data/correctness_p0/test_inlineview_with_project.out @@ -13,3 +13,13 @@ -- !select5 -- 3 +-- !select5 -- +1 +2 +3 + +-- !select5 -- +1 +2 +3 + diff --git a/regression-test/suites/correctness_p0/test_inlineview_with_project.groovy b/regression-test/suites/correctness_p0/test_inlineview_with_project.groovy index be5dd9dddfd4df..8a70fb9e751fcb 100644 --- a/regression-test/suites/correctness_p0/test_inlineview_with_project.groovy +++ b/regression-test/suites/correctness_p0/test_inlineview_with_project.groovy @@ -518,6 +518,50 @@ suite("test_inlineview_with_project") { FROM test_01 ) TOTAL; """ + qt_select5 """ + SELECT + caseId + FROM + ( + SELECT + caseId, + count(judgementDateId) + FROM + ( + SELECT + abs(caseId) AS caseId, + id as judgementDateId + FROM + dr_user_test_t2 + ) AGG_RESULT + GROUP BY + caseId + ) TOTAL + order by 1; + """ + + qt_select5 """ + SELECT + caseId + FROM + ( + SELECT + caseId, + count(judgementDateId) + FROM + ( + SELECT + caseId AS caseId, + abs(id) as judgementDateId + FROM + dr_user_test_t2 + ) AGG_RESULT + GROUP BY + caseId + ) TOTAL + order by 1; + """ + sql """DROP TABLE IF EXISTS `dr_user_test_t1`;""" sql """DROP TABLE IF EXISTS `dr_user_test_t2`;""" }