This commit is contained in:
AKIRA
2023-02-10 11:00:01 +08:00
committed by GitHub
parent 885fe1516f
commit 0c20c607b2
4 changed files with 32 additions and 1 deletions

View File

@ -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;
}
}

View File

@ -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;

View File

@ -5,3 +5,6 @@ abc
-- !sql_2 --
bc
-- !sql_3 --
1

View File

@ -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"""
}