aboutsummaryrefslogtreecommitdiff
path: root/sql/core/src/main/scala/org/apache/spark/sql/execution/joins/HashJoin.scala
blob: ecbb1ac64b7c08e2574571b7f83a3116671d5ee9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
 * 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 java.util.NoSuchElementException

import org.apache.spark.sql.catalyst.InternalRow
import org.apache.spark.sql.catalyst.expressions._
import org.apache.spark.sql.execution.SparkPlan
import org.apache.spark.sql.execution.metric.LongSQLMetric
import org.apache.spark.sql.types.{IntegralType, LongType}

trait HashJoin {
  self: SparkPlan =>

  val leftKeys: Seq[Expression]
  val rightKeys: Seq[Expression]
  val buildSide: BuildSide
  val condition: Option[Expression]
  val left: SparkPlan
  val right: SparkPlan

  protected lazy val (buildPlan, streamedPlan) = buildSide match {
    case BuildLeft => (left, right)
    case BuildRight => (right, left)
  }

  protected lazy val (buildKeys, streamedKeys) = buildSide match {
    case BuildLeft => (leftKeys, rightKeys)
    case BuildRight => (rightKeys, leftKeys)
  }

  override def output: Seq[Attribute] = left.output ++ right.output

  /**
    * Try to rewrite the key as LongType so we can use getLong(), if they key can fit with a long.
    *
    * If not, returns the original expressions.
    */
  def rewriteKeyExpr(keys: Seq[Expression]): Seq[Expression] = {
    var keyExpr: Expression = null
    var width = 0
    keys.foreach { e =>
      e.dataType match {
        case dt: IntegralType if dt.defaultSize <= 8 - width =>
          if (width == 0) {
            if (e.dataType != LongType) {
              keyExpr = Cast(e, LongType)
            } else {
              keyExpr = e
            }
            width = dt.defaultSize
          } else {
            val bits = dt.defaultSize * 8
            keyExpr = BitwiseOr(ShiftLeft(keyExpr, Literal(bits)),
              BitwiseAnd(Cast(e, LongType), Literal((1L << bits) - 1)))
            width -= bits
          }
        // TODO: support BooleanType, DateType and TimestampType
        case other =>
          return keys
      }
    }
    keyExpr :: Nil
  }

  protected val canJoinKeyFitWithinLong: Boolean = {
    val sameTypes = buildKeys.map(_.dataType) == streamedKeys.map(_.dataType)
    val key = rewriteKeyExpr(buildKeys)
    sameTypes && key.length == 1 && key.head.dataType.isInstanceOf[LongType]
  }

  protected def buildSideKeyGenerator: Projection =
    UnsafeProjection.create(rewriteKeyExpr(buildKeys), buildPlan.output)

  protected def streamSideKeyGenerator: Projection =
    UnsafeProjection.create(rewriteKeyExpr(streamedKeys), streamedPlan.output)

  @transient private[this] lazy val boundCondition = if (condition.isDefined) {
    newPredicate(condition.getOrElse(Literal(true)), left.output ++ right.output)
  } else {
    (r: InternalRow) => true
  }

  protected def hashJoin(
      streamIter: Iterator[InternalRow],
      numStreamRows: LongSQLMetric,
      hashedRelation: HashedRelation,
      numOutputRows: LongSQLMetric): Iterator[InternalRow] =
  {
    new Iterator[InternalRow] {
      private[this] var currentStreamedRow: InternalRow = _
      private[this] var currentHashMatches: Seq[InternalRow] = _
      private[this] var currentMatchPosition: Int = -1

      // Mutable per row objects.
      private[this] val joinRow = new JoinedRow
      private[this] val resultProjection: (InternalRow) => InternalRow =
        UnsafeProjection.create(self.schema)

      private[this] val joinKeys = streamSideKeyGenerator

      override final def hasNext: Boolean = {
        while (true) {
          // check if it's end of current matches
          if (currentHashMatches != null && currentMatchPosition == currentHashMatches.length) {
            currentHashMatches = null
            currentMatchPosition = -1
          }

          // find the next match
          while (currentHashMatches == null && streamIter.hasNext) {
            currentStreamedRow = streamIter.next()
            numStreamRows += 1
            val key = joinKeys(currentStreamedRow)
            if (!key.anyNull) {
              currentHashMatches = hashedRelation.get(key)
              if (currentHashMatches != null) {
                currentMatchPosition = 0
              }
            }
          }
          if (currentHashMatches == null) {
            return false
          }

          // found some matches
          buildSide match {
            case BuildRight => joinRow(currentStreamedRow, currentHashMatches(currentMatchPosition))
            case BuildLeft => joinRow(currentHashMatches(currentMatchPosition), currentStreamedRow)
          }
          if (boundCondition(joinRow)) {
            return true
          } else {
            currentMatchPosition += 1
          }
        }
        false  // unreachable
      }

      override final def next(): InternalRow = {
        // next() could be called without calling hasNext()
        if (hasNext) {
          currentMatchPosition += 1
          numOutputRows += 1
          resultProjection(joinRow)
        } else {
          throw new NoSuchElementException
        }
      }
    }
  }
}