cboumalh commented on code in PR #54140:
URL: https://github.com/apache/spark/pull/54140#discussion_r2765189470


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/CrossJoinArrayContainsToInnerJoin.scala:
##########
@@ -0,0 +1,156 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.spark.sql.catalyst.optimizer
+
+import org.apache.spark.sql.catalyst.expressions._
+import org.apache.spark.sql.catalyst.plans._
+import org.apache.spark.sql.catalyst.plans.logical._
+import org.apache.spark.sql.catalyst.rules._
+import org.apache.spark.sql.catalyst.trees.TreePattern.JOIN
+import org.apache.spark.sql.types._
+
+/**
+ * Converts cross joins with array_contains filter into inner joins using 
explode.
+ *
+ * This optimization transforms queries of the form:
+ * {{{
+ * SELECT * FROM left, right WHERE array_contains(left.arr, right.elem)
+ * }}}
+ *
+ * Into a more efficient form using explode + inner join, reducing O(N*M) to 
O(N+M).
+ */
+object CrossJoinArrayContainsToInnerJoin extends Rule[LogicalPlan] with 
PredicateHelper {
+
+  override def apply(plan: LogicalPlan): LogicalPlan = 
plan.transformUpWithPruning(
+    _.containsPattern(JOIN), ruleId) {
+    // Case 1: array_contains in Filter on top of a cross/inner join without 
condition
+    case f @ Filter(cond, j @ Join(left, right, Cross | Inner, None, _)) =>
+      tryTransformFilter(f, cond, j, left, right).getOrElse(f)
+
+    // Case 2: array_contains already pushed into join condition (by 
PushPredicateThroughJoin)
+    case j @ Join(left, right, Inner, Some(cond), hint) =>
+      tryTransformJoin(j, cond, left, right, hint).getOrElse(j)
+  }
+
+  private def tryTransformFilter(
+      filter: Filter,
+      condition: Expression,
+      join: Join,
+      left: LogicalPlan,
+      right: LogicalPlan): Option[LogicalPlan] = {
+    val predicates = splitConjunctivePredicates(condition)
+    val leftOut = left.outputSet
+    val rightOut = right.outputSet
+
+    // Find first valid array_contains predicate
+    predicates.collectFirst {

Review Comment:
   Thanks for all the work @yaooqinn, qq do we want to consider cases where we 
have multiple `array_contains ` predicates? Potentially with the same array?



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/CrossJoinArrayContainsToInnerJoin.scala:
##########
@@ -0,0 +1,156 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.spark.sql.catalyst.optimizer
+
+import org.apache.spark.sql.catalyst.expressions._
+import org.apache.spark.sql.catalyst.plans._
+import org.apache.spark.sql.catalyst.plans.logical._
+import org.apache.spark.sql.catalyst.rules._
+import org.apache.spark.sql.catalyst.trees.TreePattern.JOIN
+import org.apache.spark.sql.types._
+
+/**
+ * Converts cross joins with array_contains filter into inner joins using 
explode.
+ *
+ * This optimization transforms queries of the form:
+ * {{{
+ * SELECT * FROM left, right WHERE array_contains(left.arr, right.elem)
+ * }}}
+ *
+ * Into a more efficient form using explode + inner join, reducing O(N*M) to 
O(N+M).
+ */
+object CrossJoinArrayContainsToInnerJoin extends Rule[LogicalPlan] with 
PredicateHelper {
+
+  override def apply(plan: LogicalPlan): LogicalPlan = 
plan.transformUpWithPruning(
+    _.containsPattern(JOIN), ruleId) {
+    // Case 1: array_contains in Filter on top of a cross/inner join without 
condition
+    case f @ Filter(cond, j @ Join(left, right, Cross | Inner, None, _)) =>
+      tryTransformFilter(f, cond, j, left, right).getOrElse(f)
+
+    // Case 2: array_contains already pushed into join condition (by 
PushPredicateThroughJoin)
+    case j @ Join(left, right, Inner, Some(cond), hint) =>
+      tryTransformJoin(j, cond, left, right, hint).getOrElse(j)
+  }
+
+  private def tryTransformFilter(
+      filter: Filter,
+      condition: Expression,
+      join: Join,
+      left: LogicalPlan,
+      right: LogicalPlan): Option[LogicalPlan] = {
+    val predicates = splitConjunctivePredicates(condition)
+    val leftOut = left.outputSet
+    val rightOut = right.outputSet
+
+    // Find first valid array_contains predicate
+    predicates.collectFirst {

Review Comment:
   Thanks for all the work @yaooqinn, qq do we want to consider cases where we 
have multiple `array_contains` predicates? Potentially with the same array?



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

Reply via email to