diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/planner/SubqueryPlanner.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/planner/SubqueryPlanner.java index 17422f80247b..ebc32b6a247e 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/planner/SubqueryPlanner.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/planner/SubqueryPlanner.java @@ -115,32 +115,39 @@ public PlanBuilder handleSubqueries( public PlanBuilder handleSubqueries( PlanBuilder builder, Expression expression, Analysis.SubqueryAnalysis subqueries) { - for (Cluster cluster : - cluster( - builder.getScope(), - selectSubqueries(builder, expression, subqueries.getInPredicatesSubqueries()))) { - builder = planInPredicate(builder, cluster, subqueries); - } - for (Cluster cluster : - cluster( - builder.getScope(), - selectSubqueries(builder, expression, subqueries.getSubqueries()))) { - builder = planScalarSubquery(builder, cluster); + List inPredicates = subqueries.getInPredicatesSubqueries(); + if (!inPredicates.isEmpty()) { + for (Cluster cluster : + cluster(builder.getScope(), selectSubqueries(builder, expression, inPredicates))) { + builder = planInPredicate(builder, cluster, subqueries); + } } - for (Cluster cluster : - cluster( - builder.getScope(), - selectSubqueries(builder, expression, subqueries.getExistsSubqueries()))) { - builder = planExists(builder, cluster); + + List scalarSubqueries = subqueries.getSubqueries(); + if (!scalarSubqueries.isEmpty()) { + for (Cluster cluster : + cluster(builder.getScope(), selectSubqueries(builder, expression, scalarSubqueries))) { + builder = planScalarSubquery(builder, cluster); + } } - for (Cluster cluster : - cluster( - builder.getScope(), - selectSubqueries( - builder, expression, subqueries.getQuantifiedComparisonSubqueries()))) { - builder = planQuantifiedComparison(builder, cluster, subqueries); + + List existsPredicates = subqueries.getExistsSubqueries(); + if (!existsPredicates.isEmpty()) { + for (Cluster cluster : + cluster(builder.getScope(), selectSubqueries(builder, expression, existsPredicates))) { + builder = planExists(builder, cluster); + } } + List quantifiedComparisons = + subqueries.getQuantifiedComparisonSubqueries(); + if (!quantifiedComparisons.isEmpty()) { + for (Cluster cluster : + cluster( + builder.getScope(), selectSubqueries(builder, expression, quantifiedComparisons))) { + builder = planQuantifiedComparison(builder, cluster, subqueries); + } + } return builder; }