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 168cacc106c939eca12c6e8f83af49ded4d647ad
Author: AKIRA <[email protected]>
AuthorDate: Fri Feb 3 21:23:52 2023 +0800

    [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.
---
 .../java/org/apache/doris/analysis/SelectStmt.java |  8 ++++-
 .../java/org/apache/doris/analysis/SlotRef.java    |  5 +++
 .../org/apache/doris/analysis/QueryStmtTest.java   |  2 +-
 .../org/apache/doris/planner/QueryPlanTest.java    | 13 --------
 .../data/query_p0/subquery/test_subquery2.out      |  7 +++++
 .../suites/query_p0/subquery/test_subquery2.groovy | 36 ++++++++++++++++++++++
 6 files changed, 56 insertions(+), 15 deletions(-)

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 8f47a9ed04..921af16eec 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
@@ -407,7 +407,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
@@ -757,6 +756,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);
+            }
         }
     }
 
diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/SlotRef.java 
b/fe/fe-core/src/main/java/org/apache/doris/analysis/SlotRef.java
index 0650acfb28..bfd9ab9ccb 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/analysis/SlotRef.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/SlotRef.java
@@ -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;
@@ -339,6 +340,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;
     }
 
diff --git 
a/fe/fe-core/src/test/java/org/apache/doris/analysis/QueryStmtTest.java 
b/fe/fe-core/src/test/java/org/apache/doris/analysis/QueryStmtTest.java
index 130c23d427..03acde255d 100644
--- a/fe/fe-core/src/test/java/org/apache/doris/analysis/QueryStmtTest.java
+++ b/fe/fe-core/src/test/java/org/apache/doris/analysis/QueryStmtTest.java
@@ -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"
diff --git 
a/fe/fe-core/src/test/java/org/apache/doris/planner/QueryPlanTest.java 
b/fe/fe-core/src/test/java/org/apache/doris/planner/QueryPlanTest.java
index 115cdfcc9a..f2085315ef 100644
--- a/fe/fe-core/src/test/java/org/apache/doris/planner/QueryPlanTest.java
+++ b/fe/fe-core/src/test/java/org/apache/doris/planner/QueryPlanTest.java
@@ -1798,19 +1798,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");
diff --git a/regression-test/data/query_p0/subquery/test_subquery2.out 
b/regression-test/data/query_p0/subquery/test_subquery2.out
new file mode 100644
index 0000000000..1061df5495
--- /dev/null
+++ b/regression-test/data/query_p0/subquery/test_subquery2.out
@@ -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
+
diff --git a/regression-test/suites/query_p0/subquery/test_subquery2.groovy 
b/regression-test/suites/query_p0/subquery/test_subquery2.groovy
new file mode 100644
index 0000000000..d8476ebbdc
--- /dev/null
+++ b/regression-test/suites/query_p0/subquery/test_subquery2.groovy
@@ -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"""
+    
+}
\ No newline at end of file


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

Reply via email to