viirya commented on code in PR #41875:
URL: https://github.com/apache/spark/pull/41875#discussion_r1264611443


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/joins/ShuffledHashJoinEvaluatorFactory.scala:
##########
@@ -0,0 +1,355 @@
+/*
+ * 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.execution.joins
+
+import scala.concurrent.duration.NANOSECONDS
+
+import org.apache.spark.{PartitionEvaluator, PartitionEvaluatorFactory, 
TaskContext}
+import org.apache.spark.sql.catalyst.InternalRow
+import org.apache.spark.sql.catalyst.expressions.{Attribute, AttributeSet, 
BindReferences, Expression, GenericInternalRow, JoinedRow, UnsafeProjection}
+import org.apache.spark.sql.catalyst.optimizer.{BuildLeft, BuildRight, 
BuildSide}
+import org.apache.spark.sql.catalyst.plans.{FullOuter, JoinType, 
LeftExistence, LeftOuter, RightOuter}
+import org.apache.spark.sql.execution.RowIterator
+import org.apache.spark.sql.execution.metric.SQLMetric
+import org.apache.spark.util.collection.{BitSet, OpenHashSet}
+
+class ShuffledHashJoinEvaluatorFactory(
+    output: Seq[Attribute],
+    condition: Option[Expression],
+    buildKeys: Seq[Expression],
+    streamedKeys: Seq[Expression],
+    buildPlanOutput: Seq[Attribute],
+    streamedPlanOutput: Seq[Attribute],
+    joinType: JoinType,
+    buildSide: BuildSide,
+    streamedOutput: Seq[Attribute],
+    buildOutput: Seq[Attribute],
+    numOutputRows: SQLMetric,
+    buildDataSize: SQLMetric,
+    buildTime: SQLMetric)
+    extends PartitionEvaluatorFactory[InternalRow, InternalRow] {
+  override def createEvaluator(): PartitionEvaluator[InternalRow, InternalRow] 
=
+    new ShuffledHashJoinEvaluator
+
+  private class ShuffledHashJoinEvaluator extends 
PartitionEvaluator[InternalRow, InternalRow] {
+    override def eval(
+        partitionIndex: Int,
+        inputs: Iterator[InternalRow]*): Iterator[InternalRow] = {
+      assert(inputs.length == 2)
+      val streamIter = inputs(0)
+      val buildIter = inputs(1)
+      val hashed = buildHashedRelation(buildIter)
+      joinType match {
+        case FullOuter =>
+          buildSideOrFullOuterJoin(streamIter, hashed, numOutputRows, 
isFullOuterJoin = true)
+        case LeftOuter if buildSide.equals(BuildLeft) =>
+          buildSideOrFullOuterJoin(streamIter, hashed, numOutputRows, 
isFullOuterJoin = false)
+        case RightOuter if buildSide.equals(BuildRight) =>
+          buildSideOrFullOuterJoin(streamIter, hashed, numOutputRows, 
isFullOuterJoin = false)
+        case _ =>
+          HashJoin.join(
+            HashJoinParams(
+              streamIter,
+              hashed,
+              condition,
+              streamedKeys,
+              streamedOutput,
+              buildPlanOutput,
+              streamedPlanOutput,
+              output,
+              joinType,
+              buildSide,
+              numOutputRows))
+      }
+    }
+
+    private lazy val boundCondition: InternalRow => Boolean =
+      HashJoin.boundCondition(condition, joinType, buildSide, buildPlanOutput, 
streamedPlanOutput)
+
+    lazy val ignoreDuplicatedKey = joinType match {
+      case LeftExistence(_) =>
+        // For building hash relation, ignore duplicated rows with same join 
keys if:
+        // 1. Join condition is empty, or
+        // 2. Join condition only references streamed attributes and build 
join keys.
+        val streamedOutputAndBuildKeys = AttributeSet(streamedOutput ++ 
buildKeys)
+        condition.forall(_.references.subsetOf(streamedOutputAndBuildKeys))
+      case _ => false
+    }
+
+    private lazy val streamedBoundKeys =
+      BindReferences.bindReferences(HashJoin.rewriteKeyExpr(streamedKeys), 
streamedOutput)
+
+    private lazy val buildBoundKeys =
+      BindReferences.bindReferences(HashJoin.rewriteKeyExpr(buildKeys), 
buildOutput)
+
+    private def streamSideKeyGenerator(): UnsafeProjection =
+      UnsafeProjection.create(streamedBoundKeys)
+    private def buildHashedRelation(iter: Iterator[InternalRow]): 
HashedRelation = {

Review Comment:
   ```suggestion
       private def streamSideKeyGenerator(): UnsafeProjection =
         UnsafeProjection.create(streamedBoundKeys)
         
       private def buildHashedRelation(iter: Iterator[InternalRow]): 
HashedRelation = {
   ```



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