Skip to content

Commit

Permalink
[SPARK-10325] Override hashCode() for public Row
Browse files Browse the repository at this point in the history
This commit fixes an issue where the public SQL `Row` class did not override `hashCode`, causing it to violate the hashCode() + equals() contract. To fix this, I simply ported the `hashCode` implementation from the 1.4.x version of `Row`.

Author: Josh Rosen <joshrosen@databricks.com>

Closes #8500 from JoshRosen/SPARK-10325 and squashes the following commits:

51ffea1 [Josh Rosen] Override hashCode() for public Row.

(cherry picked from commit d3f87dc)
Signed-off-by: Michael Armbrust <michael@databricks.com>

Conflicts:
	sql/catalyst/src/main/scala/org/apache/spark/sql/Row.scala
  • Loading branch information
JoshRosen authored and marmbrus committed Aug 28, 2015
1 parent 0abbc18 commit ccda27a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
15 changes: 14 additions & 1 deletion sql/catalyst/src/main/scala/org/apache/spark/sql/Row.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@

package org.apache.spark.sql

import org.apache.spark.sql.catalyst.InternalRow
import scala.util.hashing.MurmurHash3

import org.apache.spark.sql.catalyst.expressions.GenericRow
import org.apache.spark.sql.types.StructType

Expand Down Expand Up @@ -410,6 +411,18 @@ trait Row extends Serializable {
true
}

override def hashCode: Int = {
// Using Scala's Seq hash code implementation.
var n = 0
var h = MurmurHash3.seqSeed
val len = length
while (n < len) {
h = MurmurHash3.mix(h, apply(n).##)
n += 1
}
MurmurHash3.finalizeHash(h, n)
}

/* ---------------------- utility methods for Scala ---------------------- */

/**
Expand Down
9 changes: 9 additions & 0 deletions sql/core/src/test/scala/org/apache/spark/sql/RowSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,13 @@ class RowSuite extends SparkFunSuite with SharedSQLContext {
val r2 = Row(Double.NaN)
assert(r1 === r2)
}

test("equals and hashCode") {
val r1 = Row("Hello")
val r2 = Row("Hello")
assert(r1 === r2)
assert(r1.hashCode() === r2.hashCode())
val r3 = Row("World")
assert(r3.hashCode() != r1.hashCode())
}
}

0 comments on commit ccda27a

Please sign in to comment.