[fix](planner) Doris returns empty sets when select from a inline view (#16370)
Doris always delays the execution of expressions as possible as it can, so as the expansion of constant expression. Given below SQL: ```sql select i from (select 'abc' as i, sum(birth) as j from subquerytest2) as tmp ``` The aggregation would be eliminated, since its output is not required by the outer block, but the expasion for constant expression would be done in the final result expr, and since aggreagete output has been eliminate, the expasion would actually do nothing, and finally cause a empty results. To fix this, we materialize the results expr in the inner block for such SQL, it may affect performance, but better than let system produce a mistaken result.
This commit is contained in:
@ -444,7 +444,6 @@ public class SelectStmt extends QueryStmt {
|
||||
super.analyze(analyzer);
|
||||
fromClause.setNeedToSql(needToSql);
|
||||
fromClause.analyze(analyzer);
|
||||
|
||||
// Generate !empty() predicates to filter out empty collections.
|
||||
// Skip this step when analyzing a WITH-clause because CollectionTableRefs
|
||||
// do not register collection slots in their parent in that context
|
||||
@ -887,6 +886,13 @@ public class SelectStmt extends QueryStmt {
|
||||
lateralViewRef.materializeRequiredSlots(baseTblSmap, analyzer);
|
||||
}
|
||||
}
|
||||
boolean hasConstant = resultExprs.stream().anyMatch(Expr::isConstant);
|
||||
// In such case, agg output must be materialized whether outer query block required or not.
|
||||
if (tableRef instanceof InlineViewRef && hasConstant) {
|
||||
InlineViewRef inlineViewRef = (InlineViewRef) tableRef;
|
||||
QueryStmt queryStmt = inlineViewRef.getQueryStmt();
|
||||
queryStmt.resultExprs.forEach(Expr::materializeSrcExpr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -34,6 +34,7 @@ import org.apache.doris.thrift.TSlotRef;
|
||||
import com.google.common.base.MoreObjects;
|
||||
import com.google.common.base.Objects;
|
||||
import com.google.common.base.Preconditions;
|
||||
import org.apache.commons.collections.CollectionUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
@ -354,6 +355,10 @@ public class SlotRef extends Expr {
|
||||
|
||||
@Override
|
||||
protected boolean isConstantImpl() {
|
||||
if (desc != null) {
|
||||
List<Expr> exprs = desc.getSourceExprs();
|
||||
return CollectionUtils.isNotEmpty(exprs) && exprs.stream().allMatch(Expr::isConstant);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@ -210,7 +210,7 @@ public class QueryStmtTest {
|
||||
Assert.assertEquals(5, exprsMap.size());
|
||||
constMap.clear();
|
||||
constMap = getConstantExprMap(exprsMap, analyzer);
|
||||
Assert.assertEquals(1, constMap.size());
|
||||
Assert.assertEquals(2, constMap.size());
|
||||
|
||||
// expr in subquery associate with column in grandparent level
|
||||
sql = "WITH aa AS\n"
|
||||
|
||||
@ -1781,19 +1781,6 @@ public class QueryPlanTest extends TestWithFeService {
|
||||
Assert.assertTrue(explainString.contains("PREDICATES: `k11` > '2021-06-01 00:00:00'"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullColumnViewOrderBy() throws Exception {
|
||||
FeConstants.runningUnitTest = true;
|
||||
connectContext.setDatabase("default_cluster:test");
|
||||
String sql = "select * from tbl_null_column_view where add_column is not null;";
|
||||
String explainString1 = getSQLPlanOrErrorMsg("EXPLAIN " + sql);
|
||||
Assert.assertTrue(explainString1.contains("EMPTYSET"));
|
||||
|
||||
String sql2 = "select * from tbl_null_column_view where add_column is not null order by query_id;";
|
||||
String explainString2 = getSQLPlanOrErrorMsg("EXPLAIN " + sql2);
|
||||
Assert.assertTrue(explainString2.contains("EMPTYSET"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCompoundPredicateWriteRule() throws Exception {
|
||||
connectContext.setDatabase("default_cluster:test");
|
||||
|
||||
Reference in New Issue
Block a user