morrySnow commented on code in PR #24598:
URL: https://github.com/apache/doris/pull/24598#discussion_r1332423419


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/SubqueryToApply.java:
##########
@@ -164,11 +169,84 @@ public List<Rule> buildRules() {
                             oneRowRelation.withProjects(
                                     ImmutableList.of(new 
Alias(BooleanLiteral.of(true),
                                             
ctx.statementContext.generateColumnName()))));
-                }
-            ))
+                })),
+            RuleType.JOIN_SUBQUERY_TO_APPLY
+                .build(logicalJoin()
+                .when(join -> join.getHashJoinConjuncts().isEmpty() && 
!join.getOtherJoinConjuncts().isEmpty())
+                .thenApply(ctx -> {
+                    LogicalJoin<Plan, Plan> join = ctx.root;
+                    Map<Boolean, List<Expression>> joinConjuncts = 
join.getOtherJoinConjuncts().stream()
+                            .collect(Collectors.groupingBy(conjunct -> 
conjunct.containsType(SubqueryExpr.class),
+                                    Collectors.toList()));
+                    List<Expression> subqueryConjuncts = 
joinConjuncts.get(true);
+                    if (subqueryConjuncts == null || subqueryConjuncts.stream()
+                            .anyMatch(expr -> !isValidSubqueryConjunct(expr, 
join.left()))) {
+                        return join;
+                    }
+
+                    ImmutableList<Set<SubqueryExpr>> subqueryExprsList = 
subqueryConjuncts.stream()
+                            .map(e -> (Set<SubqueryExpr>) 
e.collect(SubqueryExpr.class::isInstance))
+                            .collect(ImmutableList.toImmutableList());
+                    List<Expression> newConjuncts = Lists.newArrayList();

Review Comment:
   use ImmutableList.Builder<Expression>



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/SubqueryToApply.java:
##########
@@ -164,11 +169,84 @@ public List<Rule> buildRules() {
                             oneRowRelation.withProjects(
                                     ImmutableList.of(new 
Alias(BooleanLiteral.of(true),
                                             
ctx.statementContext.generateColumnName()))));
-                }
-            ))
+                })),
+            RuleType.JOIN_SUBQUERY_TO_APPLY
+                .build(logicalJoin()
+                .when(join -> join.getHashJoinConjuncts().isEmpty() && 
!join.getOtherJoinConjuncts().isEmpty())
+                .thenApply(ctx -> {
+                    LogicalJoin<Plan, Plan> join = ctx.root;
+                    Map<Boolean, List<Expression>> joinConjuncts = 
join.getOtherJoinConjuncts().stream()
+                            .collect(Collectors.groupingBy(conjunct -> 
conjunct.containsType(SubqueryExpr.class),
+                                    Collectors.toList()));
+                    List<Expression> subqueryConjuncts = 
joinConjuncts.get(true);
+                    if (subqueryConjuncts == null || subqueryConjuncts.stream()
+                            .anyMatch(expr -> !isValidSubqueryConjunct(expr, 
join.left()))) {
+                        return join;
+                    }
+
+                    ImmutableList<Set<SubqueryExpr>> subqueryExprsList = 
subqueryConjuncts.stream()
+                            .map(e -> (Set<SubqueryExpr>) 
e.collect(SubqueryExpr.class::isInstance))
+                            .collect(ImmutableList.toImmutableList());
+                    List<Expression> newConjuncts = Lists.newArrayList();
+                    LogicalPlan applyPlan = null;
+                    LogicalPlan tmpPlan = (LogicalPlan) join.left();

Review Comment:
   tmpPlan is not a good name, use a more meaningful name



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/SubqueryToApply.java:
##########
@@ -164,11 +169,84 @@ public List<Rule> buildRules() {
                             oneRowRelation.withProjects(
                                     ImmutableList.of(new 
Alias(BooleanLiteral.of(true),
                                             
ctx.statementContext.generateColumnName()))));
-                }
-            ))
+                })),
+            RuleType.JOIN_SUBQUERY_TO_APPLY
+                .build(logicalJoin()
+                .when(join -> join.getHashJoinConjuncts().isEmpty() && 
!join.getOtherJoinConjuncts().isEmpty())
+                .thenApply(ctx -> {
+                    LogicalJoin<Plan, Plan> join = ctx.root;
+                    Map<Boolean, List<Expression>> joinConjuncts = 
join.getOtherJoinConjuncts().stream()
+                            .collect(Collectors.groupingBy(conjunct -> 
conjunct.containsType(SubqueryExpr.class),
+                                    Collectors.toList()));
+                    List<Expression> subqueryConjuncts = 
joinConjuncts.get(true);
+                    if (subqueryConjuncts == null || subqueryConjuncts.stream()
+                            .anyMatch(expr -> !isValidSubqueryConjunct(expr, 
join.left()))) {
+                        return join;
+                    }
+
+                    ImmutableList<Set<SubqueryExpr>> subqueryExprsList = 
subqueryConjuncts.stream()
+                            .map(e -> (Set<SubqueryExpr>) 
e.collect(SubqueryExpr.class::isInstance))

Review Comment:
   ```suggestion
                               .Set<SubqueryExpr>map(e -> 
e.collect(SubqueryExpr.class::isInstance))
   ```



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/SubqueryToApply.java:
##########
@@ -164,11 +169,84 @@ public List<Rule> buildRules() {
                             oneRowRelation.withProjects(
                                     ImmutableList.of(new 
Alias(BooleanLiteral.of(true),
                                             
ctx.statementContext.generateColumnName()))));
-                }
-            ))
+                })),
+            RuleType.JOIN_SUBQUERY_TO_APPLY
+                .build(logicalJoin()
+                .when(join -> join.getHashJoinConjuncts().isEmpty() && 
!join.getOtherJoinConjuncts().isEmpty())
+                .thenApply(ctx -> {
+                    LogicalJoin<Plan, Plan> join = ctx.root;
+                    Map<Boolean, List<Expression>> joinConjuncts = 
join.getOtherJoinConjuncts().stream()
+                            .collect(Collectors.groupingBy(conjunct -> 
conjunct.containsType(SubqueryExpr.class),
+                                    Collectors.toList()));
+                    List<Expression> subqueryConjuncts = 
joinConjuncts.get(true);
+                    if (subqueryConjuncts == null || subqueryConjuncts.stream()
+                            .anyMatch(expr -> !isValidSubqueryConjunct(expr, 
join.left()))) {
+                        return join;
+                    }
+
+                    ImmutableList<Set<SubqueryExpr>> subqueryExprsList = 
subqueryConjuncts.stream()
+                            .map(e -> (Set<SubqueryExpr>) 
e.collect(SubqueryExpr.class::isInstance))
+                            .collect(ImmutableList.toImmutableList());
+                    List<Expression> newConjuncts = Lists.newArrayList();
+                    LogicalPlan applyPlan = null;
+                    LogicalPlan tmpPlan = (LogicalPlan) join.left();
+
+                    // Subquery traversal with the conjunct of and as the 
granularity.
+                    for (int i = 0; i < subqueryExprsList.size(); ++i) {
+                        Set<SubqueryExpr> subqueryExprs = 
subqueryExprsList.get(i);
+                        if (subqueryExprs.size() > 1) {
+                            // only support the conjunct contains one subquery 
expr
+                            return join;
+                        }
+
+                        // first step: Replace the subquery of predicate in 
LogicalFilter
+                        // second step: Replace subquery with LogicalApply
+                        ReplaceSubquery replaceSubquery = new 
ReplaceSubquery(ctx.statementContext, true);
+                        SubqueryContext context = new 
SubqueryContext(subqueryExprs);
+                        Expression conjunct = 
replaceSubquery.replace(subqueryConjuncts.get(i), context);
+
+                        applyPlan = subqueryToApply(
+                                
subqueryExprs.stream().collect(ImmutableList.toImmutableList()),
+                                tmpPlan, context.getSubqueryToMarkJoinSlot(),
+                                ctx.cascadesContext, Optional.of(conjunct), 
false);
+                        tmpPlan = applyPlan;
+                        newConjuncts.add(conjunct);
+                    }
+                    List<Expression> simpleConjuncts = 
joinConjuncts.get(false);
+                    if (simpleConjuncts != null) {
+                        newConjuncts.addAll(simpleConjuncts);
+                    }
+                    Plan newJoin = 
join.withConjunctsChildren(join.getHashJoinConjuncts(),
+                            newConjuncts, applyPlan, join.right());
+                    return newJoin;
+                }))
         );
     }
 
+    private static boolean isValidSubqueryConjunct(Expression expression, Plan 
leftChild) {
+        // the subquery must be uncorrelated subquery or only correlated to 
the left child
+        // currently only support the following 3 simple scenarios

Review Comment:
   ```suggestion
           // currently only support the following 4 simple scenarios
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org
For additional commands, e-mail: commits-h...@doris.apache.org

Reply via email to