[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:
AKIRA
2023-02-03 21:23:52 +08:00
committed by GitHub
parent a5d9aca7ba
commit 5e232a30d8
6 changed files with 56 additions and 15 deletions

View File

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

View File

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

View File

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

View File

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

View File

@ -0,0 +1,7 @@
-- This file is automatically generated. You should know what you did if you want to edit this
-- !sql_1 --
abc
-- !sql_2 --
bc

View File

@ -0,0 +1,36 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
suite("test_subquery2") {
sql """DROP TABLE IF EXISTS subquerytest2"""
sql """
CREATE TABLE subquerytest2 (birth int)UNIQUE KEY(birth)DISTRIBUTED BY HASH (birth)
BUCKETS 1 PROPERTIES("replication_allocation" = "tag.location.default: 1");
"""
sql """insert into subquerytest2 values (2)"""
qt_sql_1 """select i from (select 'abc' as i, sum(birth) as j from subquerytest2) as tmp"""
qt_sql_2 """select substring(i, 2) from (select 'abc' as i, sum(birth) as j from subquerytest2) as tmp"""
sql """DROP TABLE subquerytest2"""
}