diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/analyzer/UnboundOneRowRelation.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/analyzer/UnboundOneRowRelation.java index 0f85172a7c..0021663cff 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/analyzer/UnboundOneRowRelation.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/analyzer/UnboundOneRowRelation.java @@ -62,6 +62,10 @@ public class UnboundOneRowRelation extends LogicalLeaf implements Unbound, OneRo this.projects = ImmutableList.copyOf(projects); } + public RelationId getId() { + return id; + } + @Override public R accept(PlanVisitor visitor, C context) { return visitor.visitUnboundOneRowRelation(this, context); diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/analyzer/UnboundTVFRelation.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/analyzer/UnboundTVFRelation.java index 73fa85494f..426ac23541 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/analyzer/UnboundTVFRelation.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/analyzer/UnboundTVFRelation.java @@ -26,6 +26,7 @@ import org.apache.doris.nereids.trees.expressions.Slot; import org.apache.doris.nereids.trees.expressions.TVFProperties; import org.apache.doris.nereids.trees.plans.Plan; import org.apache.doris.nereids.trees.plans.PlanType; +import org.apache.doris.nereids.trees.plans.RelationId; import org.apache.doris.nereids.trees.plans.logical.LogicalLeaf; import org.apache.doris.nereids.trees.plans.visitor.PlanVisitor; import org.apache.doris.nereids.util.Utils; @@ -38,14 +39,16 @@ import java.util.Optional; public class UnboundTVFRelation extends LogicalLeaf implements Relation, Unbound { private final String functionName; private final TVFProperties properties; + private final RelationId id; - public UnboundTVFRelation(String functionName, TVFProperties properties) { - this(functionName, properties, Optional.empty(), Optional.empty()); + public UnboundTVFRelation(RelationId id, String functionName, TVFProperties properties) { + this(id, functionName, properties, Optional.empty(), Optional.empty()); } - public UnboundTVFRelation(String functionName, TVFProperties properties, + public UnboundTVFRelation(RelationId id, String functionName, TVFProperties properties, Optional groupExpression, Optional logicalProperties) { super(PlanType.LOGICAL_UNBOUND_TVF_RELATION, groupExpression, logicalProperties); + this.id = id; this.functionName = Objects.requireNonNull(functionName, "functionName can not be null"); this.properties = Objects.requireNonNull(properties, "properties can not be null"); } @@ -58,6 +61,10 @@ public class UnboundTVFRelation extends LogicalLeaf implements Relation, Unbound return properties; } + public RelationId getId() { + return id; + } + @Override public LogicalProperties computeLogicalProperties() { return UnboundLogicalProperties.INSTANCE; @@ -80,12 +87,13 @@ public class UnboundTVFRelation extends LogicalLeaf implements Relation, Unbound @Override public Plan withGroupExpression(Optional groupExpression) { - return new UnboundTVFRelation(functionName, properties, groupExpression, Optional.of(getLogicalProperties())); + return new UnboundTVFRelation(id, functionName, properties, groupExpression, + Optional.of(getLogicalProperties())); } @Override public Plan withLogicalProperties(Optional logicalProperties) { - return new UnboundTVFRelation(functionName, properties, Optional.empty(), logicalProperties); + return new UnboundTVFRelation(id, functionName, properties, Optional.empty(), logicalProperties); } @Override diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java index 5b124b2271..951fbc7cc8 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java @@ -479,7 +479,8 @@ public class LogicalPlanBuilder extends DorisParserBaseVisitor { String value = parseTVFPropertyItem(argument.value); map.put(key, value); } - LogicalPlan relation = new UnboundTVFRelation(functionName, new TVFProperties(map.build())); + LogicalPlan relation = new UnboundTVFRelation(RelationUtil.newRelationId(), + functionName, new TVFProperties(map.build())); return withTableAlias(relation, ctx.tableAlias()); }); } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindFunction.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindFunction.java index 4f68812bed..8100f4f08b 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindFunction.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindFunction.java @@ -41,7 +41,6 @@ import org.apache.doris.nereids.trees.expressions.functions.generator.TableGener import org.apache.doris.nereids.trees.expressions.functions.table.TableValuedFunction; import org.apache.doris.nereids.trees.expressions.visitor.DefaultExpressionRewriter; import org.apache.doris.nereids.trees.plans.GroupPlan; -import org.apache.doris.nereids.trees.plans.RelationId; import org.apache.doris.nereids.trees.plans.logical.LogicalAggregate; import org.apache.doris.nereids.trees.plans.logical.LogicalFilter; import org.apache.doris.nereids.trees.plans.logical.LogicalHaving; @@ -206,9 +205,7 @@ public class BindFunction implements AnalysisRuleFactory { if (!(function instanceof TableValuedFunction)) { throw new AnalysisException(function.toSql() + " is not a TableValuedFunction"); } - - RelationId relationId = statementContext.getNextRelationId(); - return new LogicalTVFRelation(relationId, (TableValuedFunction) function); + return new LogicalTVFRelation(unboundTVFRelation.getId(), (TableValuedFunction) function); } /** diff --git a/regression-test/data/nereids_syntax_p0/function.out b/regression-test/data/nereids_syntax_p0/function.out index 7a85fb4c4a..7f0be9b3f1 100644 --- a/regression-test/data/nereids_syntax_p0/function.out +++ b/regression-test/data/nereids_syntax_p0/function.out @@ -27,3 +27,24 @@ -- !string_arithmetic -- 3.0 -1.0 +-- !subquery1 -- +1 + +-- !subquery2 -- +6 +7 +8 +9 + +-- !subquery3 -- +0 +1 +2 +3 +4 +5 +6 +7 +8 +9 + diff --git a/regression-test/suites/nereids_syntax_p0/function.groovy b/regression-test/suites/nereids_syntax_p0/function.groovy index c8ca608078..761fcd04a1 100644 --- a/regression-test/suites/nereids_syntax_p0/function.groovy +++ b/regression-test/suites/nereids_syntax_p0/function.groovy @@ -74,6 +74,10 @@ suite("nereids_function") { result([[0L], [1L], [2L], [3L], [4L], [5L], [6L], [7L], [8L], [9L]]) } + qt_subquery1 """ select * from numbers("number" = "10") where number = (select number from numbers("number" = "10") where number=1); """ + qt_subquery2 """ select * from numbers("number" = "10") where number in (select number from numbers("number" = "10") where number>5); """ + qt_subquery3 """ select a.number from numbers("number" = "10") a where number in (select number from numbers("number" = "10") b where a.number=b.number); """ + test { sql """select `number` from numbers("number" = -1, 'backend_num' = `1`)""" result([])