alamb commented on code in PR #22612:
URL: https://github.com/apache/datafusion/pull/22612#discussion_r3326648963
##########
datafusion/optimizer/src/eliminate_cross_join.rs:
##########
@@ -207,6 +218,62 @@ impl OptimizerRule for EliminateCrossJoin {
}
}
+/// Returns `true` if `plan` contains at least one [`LogicalPlan::Join`]
+/// node, either directly in its tree *or* inside an embedded subquery
+/// plan reachable through `Expr::ScalarSubquery` / `Expr::InSubquery`
+/// / `Expr::Exists` / `Expr::SetComparison`.
+///
+/// Used as a fast-path gate at the top of [`EliminateCrossJoin::rewrite`]
+/// so that join-free plans skip the full recursive rewrite. The
+/// expression-side recursion is required because `rewrite_children`
+/// also dives into uncorrelated subqueries via
+/// `map_uncorrelated_subqueries`; ignoring them here would skip
+/// optimizing a `CROSS JOIN` that sits only inside an
+/// `IN (SELECT ... FROM a, b)`-style predicate.
+///
+/// Read-only `apply` walk with early stop on first match, no
+/// allocations.
+fn plan_has_joins(plan: &LogicalPlan) -> bool {
+ let mut found = false;
+ let _ = plan.apply(|node| {
+ if matches!(node, LogicalPlan::Join(_)) {
+ found = true;
+ return Ok(TreeNodeRecursion::Stop);
+ }
+ // Recurse into subquery plans referenced from this node's
+ // expressions. `LogicalPlan::apply` does not descend into the
+ // subquery plan, so we have to do it explicitly.
+ let _ = node.apply_expressions(|expr| {
+ if found {
+ return Ok(TreeNodeRecursion::Stop);
+ }
+ let _ = expr.apply(|e| {
Review Comment:
I am surprised there isn'y already soem tranversal code that recurses into
subqueries
Perhaps this one?
https://docs.rs/datafusion/latest/datafusion/logical_expr/enum.LogicalPlan.html#method.apply_with_subqueries
(though maybe you can't use that because it is for LogicalPlans not Exprs 🤔 )
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]