Github user nongli commented on a diff in the pull request:
https://github.com/apache/spark/pull/11065#discussion_r51954890
--- Diff:
sql/core/src/main/scala/org/apache/spark/sql/execution/joins/HashedRelation.scala
---
@@ -408,6 +453,210 @@ private[joins] object UnsafeHashedRelation {
}
}
+ // TODO: create UniqueUnsafeRelation
new UnsafeHashedRelation(hashTable)
}
}
+
+/**
+ * An interface for a hashed relation that the key is a Long.
+ */
+private[joins] trait LongHashedRelation extends HashedRelation {
+ override def get(key: InternalRow): Seq[InternalRow] = {
+ get(key.getLong(0))
+ }
+}
+
+private[joins] final class GeneralLongHashedRelation(
+ private var hashTable: JavaHashMap[Long, CompactBuffer[UnsafeRow]])
+ extends LongHashedRelation with Externalizable {
+
+ // Needed for serialization (it is public to make Java serialization
work)
+ def this() = this(null)
+
+ override def get(key: Long): Seq[InternalRow] = hashTable.get(key)
+
+ override def writeExternal(out: ObjectOutput): Unit = {
+ writeBytes(out, SparkSqlSerializer.serialize(hashTable))
+ }
+
+ override def readExternal(in: ObjectInput): Unit = {
+ hashTable = SparkSqlSerializer.deserialize(readBytes(in))
+ }
+}
+
+private[joins] final class UniqueLongHashedRelation(
+ private var hashTable: JavaHashMap[Long, UnsafeRow])
+ extends UniqueHashedRelation with LongHashedRelation with Externalizable
{
+
+ // Needed for serialization (it is public to make Java serialization
work)
+ def this() = this(null)
+
+ override def getValue(key: InternalRow): InternalRow = {
+ getValue(key.getLong(0))
+ }
+
+ override def getValue(key: Long): InternalRow = {
+ hashTable.get(key)
+ }
+
+ override def writeExternal(out: ObjectOutput): Unit = {
+ writeBytes(out, SparkSqlSerializer.serialize(hashTable))
+ }
+
+ override def readExternal(in: ObjectInput): Unit = {
+ hashTable = SparkSqlSerializer.deserialize(readBytes(in))
+ }
+}
+
+/**
+ * A relation that pack all the rows into a byte array, together with
offsets and sizes.
+ */
+private[joins] final class LongArrayRelation(
+ private var numFields: Int,
+ private var start: Long,
+ private var offsets: Array[Int],
+ private var sizes: Array[Int],
+ private var bytes: Array[Byte]
+ ) extends UniqueHashedRelation with LongHashedRelation with
Externalizable {
+
+ // Needed for serialization (it is public to make Java serialization
work)
+ def this() = this(0, 0L, null, null, null)
+
+ override def getValue(key: InternalRow): InternalRow = {
+ getValue(key.getLong(0))
+ }
+
+ override def getMemorySize: Long = {
+ offsets.length * 4 + sizes.length * 4 + bytes.length
+ }
+
+ override def getValue(key: Long): InternalRow = {
+ val idx = (key - start).toInt
+ if (idx >= 0 && idx < sizes.length && sizes(idx) > 0) {
+ val result = new UnsafeRow(numFields)
+ result.pointTo(bytes, Platform.BYTE_ARRAY_OFFSET + offsets(idx),
sizes(idx))
+ result
+ } else {
+ null
+ }
+ }
+
+ override def writeExternal(out: ObjectOutput): Unit = {
+ out.writeInt(numFields)
+ out.writeLong(start)
+ out.writeInt(sizes.length)
+ var i = 0
+ while (i < sizes.length) {
+ out.writeInt(sizes(i))
+ i += 1
+ }
+ out.writeInt(bytes.length)
+ out.write(bytes)
+ }
+
+ override def readExternal(in: ObjectInput): Unit = {
+ numFields = in.readInt()
+ start = in.readLong()
+ val length = in.readInt()
+ // read sizes of rows
+ sizes = new Array[Int](length)
+ offsets = new Array[Int](length)
+ var i = 0
+ var offset = 0
+ while (i < length) {
+ offsets(i) = offset
+ sizes(i) = in.readInt()
+ offset += sizes(i)
+ i += 1
+ }
+ // read all the bytes
+ val total = in.readInt()
+ assert(total == offset)
+ bytes = new Array[Byte](total)
+ in.readFully(bytes)
+ }
+}
+
+/**
+ * Create hashed relation with key that is long.
+ */
+private[joins] object LongHashedRelation {
+ def apply(
+ input: Iterator[InternalRow],
+ numInputRows: LongSQLMetric,
+ keyGenerator: Projection,
+ sizeEstimate: Int): HashedRelation = {
+
+ // Use a Java hash table here because unsafe maps expect fixed size
records
+ val hashTable = new JavaHashMap[Long,
CompactBuffer[UnsafeRow]](sizeEstimate)
+
+ // Create a mapping of buildKeys -> rows
+ var numFields = 0
+ var keyIsUnique = true
+ var minKey = Long.MaxValue
+ var maxKey = Long.MinValue
+ while (input.hasNext) {
+ val unsafeRow = input.next().asInstanceOf[UnsafeRow]
+ numFields = unsafeRow.numFields()
+ numInputRows += 1
+ val rowKey = keyGenerator(unsafeRow)
+ if (!rowKey.anyNull) {
+ val key = rowKey.getLong(0)
+ minKey = math.min(minKey, key)
+ maxKey = math.max(maxKey, key)
+ val existingMatchList = hashTable.get(key)
+ val matchList = if (existingMatchList == null) {
+ val newMatchList = new CompactBuffer[UnsafeRow]()
+ hashTable.put(key, newMatchList)
+ newMatchList
+ } else {
+ keyIsUnique = false
+ existingMatchList
+ }
+ matchList += unsafeRow.copy()
+ }
+ }
+
+ if (keyIsUnique) {
+ if (maxKey - minKey <= hashTable.size() * 5) {
--- End diff --
how did you pick this? Can we make this a constant named variable at the
top of this class
---
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 [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]