Github user marmbrus commented on a diff in the pull request:

    https://github.com/apache/spark/pull/837#discussion_r13262687
  
    --- Diff: 
sql/core/src/main/scala/org/apache/spark/sql/execution/joins.scala ---
    @@ -144,6 +144,150 @@ case class HashJoin(
      * :: DeveloperApi ::
      */
     @DeveloperApi
    +case class LeftSemiJoinHash(
    +                     leftKeys: Seq[Expression],
    +                     rightKeys: Seq[Expression],
    +                     buildSide: BuildSide,
    +                     left: SparkPlan,
    +                     right: SparkPlan) extends BinaryNode {
    +
    +  override def outputPartitioning: Partitioning = left.outputPartitioning
    +
    +  override def requiredChildDistribution =
    +    ClusteredDistribution(leftKeys) :: ClusteredDistribution(rightKeys) :: 
Nil
    +
    +  val (buildPlan, streamedPlan) = buildSide match {
    +    case BuildLeft => (left, right)
    +    case BuildRight => (right, left)
    +  }
    +
    +  val (buildKeys, streamedKeys) = buildSide match {
    +    case BuildLeft => (leftKeys, rightKeys)
    +    case BuildRight => (rightKeys, leftKeys)
    +  }
    +
    +  def output = left.output
    +
    +  @transient lazy val buildSideKeyGenerator = new Projection(buildKeys, 
buildPlan.output)
    +  @transient lazy val streamSideKeyGenerator =
    +    () => new MutableProjection(streamedKeys, streamedPlan.output)
    +
    +  def execute() = {
    +
    +    buildPlan.execute().zipPartitions(streamedPlan.execute()) { 
(buildIter, streamIter) =>
    +    // TODO: Use Spark's HashMap implementation.
    +      val hashTable = new java.util.HashMap[Row, ArrayBuffer[Row]]()
    +      var currentRow: Row = null
    +
    +      // Create a mapping of buildKeys -> rows
    +      while (buildIter.hasNext) {
    +        currentRow = buildIter.next()
    +        val rowKey = buildSideKeyGenerator(currentRow)
    +        if(!rowKey.anyNull) {
    +          val existingMatchList = hashTable.get(rowKey)
    +          val matchList = if (existingMatchList == null) {
    +            val newMatchList = new ArrayBuffer[Row]()
    +            hashTable.put(rowKey, newMatchList)
    +            newMatchList
    +          } else {
    +            existingMatchList
    +          }
    +          matchList += currentRow.copy()
    +        }
    +      }
    +
    +      new Iterator[Row] {
    +        private[this] var currentStreamedRow: Row = _
    +        private[this] var currentHashMatched: Boolean = false
    +
    +        private[this] val joinKeys = streamSideKeyGenerator()
    +
    +        override final def hasNext: Boolean =
    +          streamIter.hasNext && fetchNext()
    +
    +        override final def next() = {
    +          currentStreamedRow
    --- End diff --
    
    Is this correct if the operator is created with BuildLeft instead of 
BuildRight?  I think that would turn it into a RightSemiJoin.  Perhaps we 
should just remove the option to build on the other side.  I think you can then 
also safely simplify this to use a HashSet instead of a HashMap, which will 
reduce memory consumption significantly.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to