-
Notifications
You must be signed in to change notification settings - Fork 28.7k
[SPARK-8432] [SQL] fix hashCode() and equals() of BinaryType in Row #6876
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
53c38b1
0fff25d
5819d33
6ad2a90
d96929b
41caec6
bd20780
89c2432
a0626ed
32d9811
429c2c0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,15 +18,78 @@ | |
package org.apache.spark.sql.catalyst | ||
|
||
import org.apache.spark.sql.Row | ||
import org.apache.spark.sql.catalyst.expressions.GenericRow | ||
import org.apache.spark.sql.catalyst.expressions._ | ||
|
||
/** | ||
* An abstract class for row used internal in Spark SQL, which only contain the columns as | ||
* internal types. | ||
*/ | ||
abstract class InternalRow extends Row { | ||
// A default implementation to change the return type | ||
override def copy(): InternalRow = {this} | ||
override def copy(): InternalRow = this | ||
|
||
override def equals(o: Any): Boolean = { | ||
if (!o.isInstanceOf[Row]) { | ||
return false | ||
} | ||
|
||
val other = o.asInstanceOf[Row] | ||
if (length != other.length) { | ||
return false | ||
} | ||
|
||
var i = 0 | ||
while (i < length) { | ||
if (isNullAt(i) != other.isNullAt(i)) { | ||
return false | ||
} | ||
if (!isNullAt(i)) { | ||
val o1 = apply(i) | ||
val o2 = other.apply(i) | ||
if (o1.isInstanceOf[Array[Byte]]) { | ||
// handle equality of Array[Byte] | ||
val b1 = o1.asInstanceOf[Array[Byte]] | ||
if (!o2.isInstanceOf[Array[Byte]] || | ||
!java.util.Arrays.equals(b1, o2.asInstanceOf[Array[Byte]])) { | ||
return false | ||
} | ||
} else if (o1 != o2) { | ||
return false | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what about (o1, o2) match {
case (b1: Array[Byte], b2: Array[Byte]) => java.util.Arrays.equals(b1, b2)
case _ => o1 == o2
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
i += 1 | ||
} | ||
true | ||
} | ||
|
||
// Custom hashCode function that matches the efficient code generated version. | ||
override def hashCode: Int = { | ||
var result: Int = 37 | ||
var i = 0 | ||
while (i < length) { | ||
val update: Int = | ||
if (isNullAt(i)) { | ||
0 | ||
} else { | ||
apply(i) match { | ||
case b: Boolean => if (b) 0 else 1 | ||
case b: Byte => b.toInt | ||
case s: Short => s.toInt | ||
case i: Int => i | ||
case l: Long => (l ^ (l >>> 32)).toInt | ||
case f: Float => java.lang.Float.floatToIntBits(f) | ||
case d: Double => | ||
val b = java.lang.Double.doubleToLongBits(d) | ||
(b ^ (b >>> 32)).toInt | ||
case a: Array[Byte] => java.util.Arrays.hashCode(a) | ||
case other => other.hashCode() | ||
} | ||
} | ||
result = 37 * result + update | ||
i += 1 | ||
} | ||
result | ||
} | ||
} | ||
|
||
object InternalRow { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -127,6 +127,7 @@ object GenerateProjection extends CodeGenerator[Seq[Expression], Projection] { | |
case FloatType => s"Float.floatToIntBits($col)" | ||
case DoubleType => | ||
s"(int)(Double.doubleToLongBits($col) ^ (Double.doubleToLongBits($col) >>> 32))" | ||
case BinaryType => s"java.util.Arrays.hashCode($col)" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we also update There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's already done There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah I see, |
||
case _ => s"$col.hashCode()" | ||
} | ||
s"isNullAt($i) ? 0 : ($nonNull)" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will we change it to
isInstanceOf[InternalRow]
after #6869?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes