This is an automated email from the ASF dual-hosted git repository.

morningman pushed a commit to branch branch-1.2-lts
in repository https://gitbox.apache.org/repos/asf/doris.git

commit 58268569633dec3c933202a608639aa0362829ad
Author: AKIRA <[email protected]>
AuthorDate: Fri Feb 10 11:00:01 2023 +0800

    fix stats (#16556)
---
 .../main/java/org/apache/doris/analysis/Expr.java  | 26 ++++++++++++++++++++++
 .../java/org/apache/doris/analysis/SelectStmt.java |  2 +-
 .../data/query_p0/subquery/test_subquery2.out      |  3 +++
 .../suites/query_p0/subquery/test_subquery2.groovy |  2 ++
 4 files changed, 32 insertions(+), 1 deletion(-)

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 1dc9740aae..b7915d207e 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
@@ -45,6 +45,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;
 
@@ -2120,5 +2121,30 @@ public abstract class Expr extends TreeNode<Expr> 
implements ParseNode, Cloneabl
                     return type;
                 }).toArray(Type[]::new);
     }
+
+    public boolean refToCountStar() {
+        if (this instanceof SlotRef) {
+            SlotRef slotRef = (SlotRef) this;
+            SlotDescriptor desc = slotRef.getDesc();
+            List<Expr> 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 921af16eec..6ad90e47fb 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
@@ -756,7 +756,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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to