maropu commented on a change in pull request #29104:
URL: https://github.com/apache/spark/pull/29104#discussion_r454743716



##########
File path: 
sql/core/src/main/scala/org/apache/spark/sql/execution/joins/BroadcastNestedLoopJoinExec.scala
##########
@@ -205,6 +226,117 @@ case class BroadcastNestedLoopJoinExec(
     }
   }
 
+  case class NotInSubquerySingleColumnOptimizeParams(
+      buildSideHashSet: mutable.HashSet[AnyRef],
+      isNullExists: Boolean,
+      isBuildRowsEmpty: Boolean)
+
+  private def notInSubquerySingleColumnOptimizeEnabled: Boolean = {
+    if (SQLConf.get.notInSubquerySingleColumnOptimizeEnabled && 
right.output.length == 1) {
+      // buildSide must be single column
+      // and condition must be either of following pattern
+      // or(a=b,isnull(a=b))
+      // or(isnull(a=b),a=b)
+      condition.get match {
+        case _@Or(_@EqualTo(leftAttr: AttributeReference, rightAttr: 
AttributeReference),
+            _@IsNull(_@EqualTo(tmpLeft: AttributeReference, tmpRight: 
AttributeReference)))
+            if leftAttr.semanticEquals(tmpLeft) && 
rightAttr.semanticEquals(tmpRight) =>
+          notInSubquerySingleColumnOptimizeSetStreamedKey(leftAttr, rightAttr)
+          if (notInSubquerySingleColumnOptimizeStreamedKeyIndex != -1) {
+            true
+          } else {
+            logWarning(s"failed to find 
notInSubquerySingleColumnOptimizeStreamedKeyIndex," +
+              s" fallback to leftExistenceJoin.")
+            false
+          }
+        case _@Or(_@IsNull(_@EqualTo(tmpLeft: AttributeReference, tmpRight: 
AttributeReference)),
+            _@EqualTo(leftAttr: AttributeReference, rightAttr: 
AttributeReference))
+            if leftAttr.semanticEquals(tmpLeft) && 
rightAttr.semanticEquals(tmpRight) =>
+          notInSubquerySingleColumnOptimizeSetStreamedKey(leftAttr, rightAttr)
+          if (notInSubquerySingleColumnOptimizeStreamedKeyIndex != -1) {
+            true
+          } else {
+            logWarning(s"failed to find 
notInSubquerySingleColumnOptimizeStreamedKeyIndex," +
+              s" fallback to leftExistenceJoin.")
+            false
+          }
+        case _ => false
+      }
+    } else {
+      false
+    }
+  }
+
+  private def notInSubquerySingleColumnOptimizeBuildParams(
+      buildRows: Array[InternalRow]): NotInSubquerySingleColumnOptimizeParams 
= {
+    val buildRowsHashSet = mutable.HashSet[AnyRef]()
+    val dataType = right.output.head.dataType
+    var isNullExist = false
+    buildRows.foreach(row => {
+      if (row.isNullAt(0)) {
+        isNullExist = true
+      }
+      // put null as value, because we only leverage HashMap keys
+      buildRowsHashSet.add(row.get(0, dataType))
+    })
+    NotInSubquerySingleColumnOptimizeParams(buildRowsHashSet, isNullExist, 
buildRows.isEmpty)
+  }
+
+  private def notInSubquerySingleColumnOptimizeSetStreamedKey(
+      leftAttr: AttributeReference,
+      rightAttr: AttributeReference): Unit = {
+    val leftNotInKeyFound = left.output.find(_.semanticEquals(leftAttr))
+    if (leftNotInKeyFound.isDefined) {
+      notInSubquerySingleColumnOptimizeStreamedKey = leftAttr
+      notInSubquerySingleColumnOptimizeStreamedKeyIndex =
+        AttributeSeq(left.output).indexOf(leftAttr.exprId)
+    } else {
+      notInSubquerySingleColumnOptimizeStreamedKey = rightAttr
+      notInSubquerySingleColumnOptimizeStreamedKeyIndex =
+        AttributeSeq(left.output).indexOf(rightAttr.exprId)
+    }
+  }
+
+  /**
+   * Optimized version implementation for LeftAnti
+   * which converted from single column NotInSubquery
+   * @param relation
+   * @return
+   */
+  private def notInSubquerySingleColumnOptimize(
+      relation: Broadcast[Array[InternalRow]]): RDD[InternalRow] = {
+    assert(buildSide == BuildRight)
+    val buildRows = relation.value
+    if (buildRows.length > 
SQLConf.get.notInSubquerySingleColumnOptimizeRowCountThreshold) {
+      logWarning(s"BuildSide row count ${buildRows.length} exceeded " +
+        s"notInSubquerySingleColumnOptimizeRowCountThreshold ${SQLConf.get
+          .notInSubquerySingleColumnOptimizeRowCountThreshold}, fallback to 
leftExistenceJoin.")
+      leftExistenceJoin(relation, false)
+    } else {
+      val params = notInSubquerySingleColumnOptimizeBuildParams(buildRows)
+      streamed.execute().mapPartitionsInternal { streamedIter =>
+        streamedIter.filter(row => {
+          // See. not-in-unit-tests-single-column.sql for detail filter rules
+          if (params.isBuildRowsEmpty) {
+            true
+          } else {
+            val streamedRowIsNull = 
row.isNullAt(notInSubquerySingleColumnOptimizeStreamedKeyIndex)
+            val streamedRowNotInKey = row.get(
+              notInSubquerySingleColumnOptimizeStreamedKeyIndex,
+              notInSubquerySingleColumnOptimizeStreamedKey.dataType
+            )
+            val notInKeyEqual = 
params.buildSideHashSet.contains(streamedRowNotInKey)

Review comment:
       Could we reuse `[Unsafe|Long]HashedRelation` here?




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

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