fix stats (#16556)
This commit is contained in:
@ -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<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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -5,3 +5,6 @@ abc
|
||||
-- !sql_2 --
|
||||
bc
|
||||
|
||||
-- !sql_3 --
|
||||
1
|
||||
|
||||
|
||||
@ -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"""
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user