Skip to content
Snippets Groups Projects
Commit d3f87dc3 authored by Josh Rosen's avatar Josh Rosen Committed by Michael Armbrust
Browse files

[SPARK-10325] Override hashCode() for public Row

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.
parent 499e8e15
No related branches found
No related tags found
No related merge requests found
......@@ -18,6 +18,7 @@
package org.apache.spark.sql
import scala.collection.JavaConverters._
import scala.util.hashing.MurmurHash3
import org.apache.spark.sql.catalyst.InternalRow
import org.apache.spark.sql.catalyst.expressions.GenericRow
......@@ -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 ---------------------- */
/**
......
......@@ -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% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment