leanken commented on a change in pull request #29104:
URL: https://github.com/apache/spark/pull/29104#discussion_r456351760
##########
File path:
sql/core/src/main/scala/org/apache/spark/sql/execution/joins/BroadcastNestedLoopJoinExec.scala
##########
@@ -205,6 +224,108 @@ case class BroadcastNestedLoopJoinExec(
}
}
+ case class NotInSubquerySingleColumnOptimizeParams(
+ buildSideHashedRelation: HashedRelation,
+ isNullExists: Boolean,
+ isBuildRowsEmpty: Boolean)
+
+ private def notInSubquerySingleColumnOptimizeEnabled: Boolean = {
+ if (SQLConf.get.notInSubquerySingleColumnOptimizeEnabled &&
right.output.length == 1) {
+ // BuildSide must be single column, condition must be the following
pattern
+ // Or(EqualTo(a, b), IsNull(EqualTo(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 _ => false
+ }
+ } else {
+ false
+ }
+ }
+
+ private def notInSubquerySingleColumnOptimizeBuildParams(
+ buildRows: Array[InternalRow]): NotInSubquerySingleColumnOptimizeParams
= {
+ NotInSubquerySingleColumnOptimizeParams(
+ HashedRelation(buildRows.iterator,
+ BindReferences.bindReferences[Expression](
+ Seq(right.output.head), AttributeSeq(right.output)),
+ buildRows.length),
+ buildRows.exists(row => row.isNullAt(0)),
+ buildRows.isEmpty)
+ }
+
+ private def notInSubquerySingleColumnOptimizeSetStreamedKey(
+ leftAttr: AttributeReference,
+ rightAttr: AttributeReference): Unit = {
+ val leftNotInKeyFound = left.output.exists(_.semanticEquals(leftAttr))
+ if (leftNotInKeyFound) {
+ 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 =>
+ val keyGenerator =
+ UnsafeProjection.create(
+ BindReferences.bindReferences[Expression](
+ Seq(notInSubquerySingleColumnOptimizeStreamedKey),
+ AttributeSeq(left.output))
+ )
+ 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 lookupRow: UnsafeRow = keyGenerator(row)
+ val notInKeyEqual = params.buildSideHashedRelation.get(lookupRow)
match {
+ case null => false
+ case _ => true
+ }
+
+ if (!streamedRowIsNull && !params.isNullExists && !notInKeyEqual) {
Review comment:
done.
----------------------------------------------------------------
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]