[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)
This commit is contained in:
starocean999
2023-09-14 17:22:08 +08:00
committed by GitHub
parent 0be0b8ff58
commit d035a58374
4 changed files with 42 additions and 1 deletions

View File

@ -122,6 +122,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),

View File

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

View File

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

View File

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