This is an automated email from the ASF dual-hosted git repository.

kxiao pushed a commit to branch branch-2.0
in repository https://gitbox.apache.org/repos/asf/doris.git

commit 6ffdd8cf2d43e87030abdcee57f8beb77bf93e72
Author: starocean999 <[email protected]>
AuthorDate: Thu Sep 14 17:22:08 2023 +0800

    [feature](nereids) support unnest subquery in LogicalOneRowRelation (#24355)
    
    select (select 1);
    before :
    ERROR 1105 (HY000): errCode = 2, detailMessage = Subquery is not supported 
in the select list.
    after:
    mysql> select (select 1);
    +---------------------------------------------------------------------+
    |  (SCALARSUBQUERY) (LogicalOneRowRelation ( projects=[1 AS `1`#0] )) |
    +---------------------------------------------------------------------+
    |                                                                   1 |
    +---------------------------------------------------------------------+
    1 row in set (0.61 sec)
---
 .../org/apache/doris/nereids/rules/RuleType.java    |  1 +
 .../nereids/rules/analysis/SubqueryToApply.java     | 18 +++++++++++++++++-
 .../data/nereids_p0/subquery/test_subquery.out      |  3 +++
 .../suites/nereids_p0/subquery/test_subquery.groovy | 21 +++++++++++++++++++++
 4 files changed, 42 insertions(+), 1 deletion(-)

diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/RuleType.java 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/RuleType.java
index 3ede58eaab..e6ce120bdd 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/RuleType.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/RuleType.java
@@ -121,6 +121,7 @@ public enum RuleType {
     // subquery analyze
     FILTER_SUBQUERY_TO_APPLY(RuleTypeClass.REWRITE),
     PROJECT_SUBQUERY_TO_APPLY(RuleTypeClass.REWRITE),
+    ONE_ROW_RELATION_SUBQUERY_TO_APPLY(RuleTypeClass.REWRITE),
     // subquery rewrite rule
     ELIMINATE_LIMIT_UNDER_APPLY(RuleTypeClass.REWRITE),
     ELIMINATE_SORT_UNDER_APPLY(RuleTypeClass.REWRITE),
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/SubqueryToApply.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/SubqueryToApply.java
index c28e82f680..de67fea935 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/SubqueryToApply.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/SubqueryToApply.java
@@ -21,6 +21,7 @@ import org.apache.doris.nereids.CascadesContext;
 import org.apache.doris.nereids.StatementContext;
 import org.apache.doris.nereids.rules.Rule;
 import org.apache.doris.nereids.rules.RuleType;
+import org.apache.doris.nereids.trees.expressions.Alias;
 import org.apache.doris.nereids.trees.expressions.BinaryOperator;
 import org.apache.doris.nereids.trees.expressions.Exists;
 import org.apache.doris.nereids.trees.expressions.Expression;
@@ -37,6 +38,7 @@ import org.apache.doris.nereids.trees.plans.Plan;
 import org.apache.doris.nereids.trees.plans.algebra.Aggregate;
 import org.apache.doris.nereids.trees.plans.logical.LogicalApply;
 import org.apache.doris.nereids.trees.plans.logical.LogicalFilter;
+import org.apache.doris.nereids.trees.plans.logical.LogicalOneRowRelation;
 import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;
 import org.apache.doris.nereids.trees.plans.logical.LogicalProject;
 
@@ -149,7 +151,21 @@ public class SubqueryToApply implements 
AnalysisRuleFactory {
                 }
 
                 return project.withProjectsAndChild(newProjects.build(), 
childPlan);
-            }))
+            })),
+            
RuleType.ONE_ROW_RELATION_SUBQUERY_TO_APPLY.build(logicalOneRowRelation()
+                .when(ctx -> ctx.getProjects().stream()
+                        .anyMatch(project -> 
project.containsType(SubqueryExpr.class)))
+                .thenApply(ctx -> {
+                    LogicalOneRowRelation oneRowRelation = ctx.root;
+                    // create a LogicalProject node with the same project 
lists above LogicalOneRowRelation
+                    // create a LogicalOneRowRelation with a dummy output 
column
+                    // so PROJECT_SUBQUERY_TO_APPLY rule can handle the 
subquery unnest thing
+                    return new 
LogicalProject<Plan>(oneRowRelation.getProjects(),
+                            oneRowRelation.withProjects(
+                                    ImmutableList.of(new 
Alias(BooleanLiteral.of(true),
+                                            
ctx.statementContext.generateColumnName()))));
+                }
+            ))
         );
     }
 
diff --git a/regression-test/data/nereids_p0/subquery/test_subquery.out 
b/regression-test/data/nereids_p0/subquery/test_subquery.out
index 2e13e02930..284361624e 100644
--- a/regression-test/data/nereids_p0/subquery/test_subquery.out
+++ b/regression-test/data/nereids_p0/subquery/test_subquery.out
@@ -20,3 +20,6 @@
 -- !uncorrelated_scalar_with_sort_and_limit --
 true   15      1992    3021    11011920        0.000   true    9999-12-12      
2015-04-02T00:00                3.141592653     20.456  string12345     
701411834604692317
 
+-- !sql_subquery_one_row_relation --
+1
+
diff --git a/regression-test/suites/nereids_p0/subquery/test_subquery.groovy 
b/regression-test/suites/nereids_p0/subquery/test_subquery.groovy
index 5e3e4bfb30..2f9962d921 100644
--- a/regression-test/suites/nereids_p0/subquery/test_subquery.groovy
+++ b/regression-test/suites/nereids_p0/subquery/test_subquery.groovy
@@ -64,4 +64,25 @@ suite("test_subquery") {
     qt_uncorrelated_scalar_with_sort_and_limit """
             select * from nereids_test_query_db.baseall where k1 = (select k1 
from nereids_test_query_db.baseall order by k1 desc limit 1)
         """
+
+    sql """drop table if exists test_one_row_relation;"""
+    sql """
+        CREATE TABLE `test_one_row_relation` (
+        `user_id` int(11) NULL 
+        )
+        UNIQUE KEY(`user_id`)
+        COMMENT 'test'
+        DISTRIBUTED BY HASH(`user_id`) BUCKETS 1
+        PROPERTIES (
+        "replication_allocation" = "tag.location.default: 1"
+        );
+    """
+
+    sql """ set enable_nereids_dml=true; """
+    
+    sql """insert into test_one_row_relation select (select 1);"""
+
+    qt_sql_subquery_one_row_relation """select * from test_one_row_relation;"""
+
+    sql """drop table if exists test_one_row_relation;"""
 }


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

Reply via email to