diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/Expr.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/Expr.java index 9ad70b1967..abd241d6dc 100755 --- a/fe/fe-core/src/main/java/org/apache/doris/analysis/Expr.java +++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/Expr.java @@ -47,6 +47,7 @@ import com.google.common.base.Preconditions; import com.google.common.base.Strings; import com.google.common.collect.Lists; import com.google.common.collect.Maps; +import org.apache.commons.collections.CollectionUtils; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -2197,5 +2198,30 @@ public abstract class Expr extends TreeNode implements ParseNode, Cloneabl return type; }).toArray(Type[]::new); } + + public boolean refToCountStar() { + if (this instanceof SlotRef) { + SlotRef slotRef = (SlotRef) this; + SlotDescriptor desc = slotRef.getDesc(); + List exprs = desc.getSourceExprs(); + return CollectionUtils.isNotEmpty(exprs) && exprs.stream().anyMatch(e -> { + if (e instanceof FunctionCallExpr) { + FunctionCallExpr funcExpr = (FunctionCallExpr) e; + Function f = funcExpr.fn; + if (f.getFunctionName().getFunction().equals("count") + && funcExpr.children.stream().anyMatch(Expr::isConstant)) { + return true; + } + } + return false; + }); + } + for (Expr expr : children) { + if (expr.refToCountStar()) { + return true; + } + } + return false; + } } diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/SelectStmt.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/SelectStmt.java index bfb6286b43..a8b572d040 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/analysis/SelectStmt.java +++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/SelectStmt.java @@ -896,7 +896,7 @@ public class SelectStmt extends QueryStmt { lateralViewRef.materializeRequiredSlots(baseTblSmap, analyzer); } } - boolean hasConstant = resultExprs.stream().anyMatch(Expr::isConstant); + boolean hasConstant = resultExprs.stream().anyMatch(e -> e.isConstant() || e.refToCountStar()); // In such case, agg output must be materialized whether outer query block required or not. if (tableRef instanceof InlineViewRef && hasConstant) { InlineViewRef inlineViewRef = (InlineViewRef) tableRef; diff --git a/regression-test/data/query_p0/subquery/test_subquery2.out b/regression-test/data/query_p0/subquery/test_subquery2.out index 1061df5495..ceaff8c994 100644 --- a/regression-test/data/query_p0/subquery/test_subquery2.out +++ b/regression-test/data/query_p0/subquery/test_subquery2.out @@ -5,3 +5,6 @@ abc -- !sql_2 -- bc +-- !sql_3 -- +1 + diff --git a/regression-test/suites/query_p0/subquery/test_subquery2.groovy b/regression-test/suites/query_p0/subquery/test_subquery2.groovy index d8476ebbdc..e572459cc7 100644 --- a/regression-test/suites/query_p0/subquery/test_subquery2.groovy +++ b/regression-test/suites/query_p0/subquery/test_subquery2.groovy @@ -31,6 +31,8 @@ suite("test_subquery2") { qt_sql_2 """select substring(i, 2) from (select 'abc' as i, sum(birth) as j from subquerytest2) as tmp""" + qt_sql_3 """select count(1) from (select 'abc' as i, sum(birth) as j from subquerytest2) as tmp""" + sql """DROP TABLE subquerytest2""" } \ No newline at end of file