Skip to content

Commit 933f025

Browse files
gatorsmilemarmbrus
authored andcommitted
[SPARK-8658][SQL][FOLLOW-UP] AttributeReference's equals method compares all the members
Based on the comment of cloud-fan in #9216, update the AttributeReference's hashCode function by including the hashCode of the other attributes including name, nullable and qualifiers. Here, I am not 100% sure if we should include name in the hashCode calculation, since the original hashCode calculation does not include it. marmbrus cloud-fan Please review if the changes are good. Author: gatorsmile <gatorsmile@gmail.com> Closes #9761 from gatorsmile/hashCodeNamedExpression. (cherry picked from commit 0158ff7) Signed-off-by: Michael Armbrust <michael@databricks.com>
1 parent 167ea61 commit 933f025

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/namedExpressions.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,12 @@ case class AttributeReference(
212212
override def hashCode: Int = {
213213
// See http://stackoverflow.com/questions/113511/hash-code-implementation
214214
var h = 17
215-
h = h * 37 + exprId.hashCode()
215+
h = h * 37 + name.hashCode()
216216
h = h * 37 + dataType.hashCode()
217+
h = h * 37 + nullable.hashCode()
217218
h = h * 37 + metadata.hashCode()
219+
h = h * 37 + exprId.hashCode()
220+
h = h * 37 + qualifiers.hashCode()
218221
h
219222
}
220223

sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/SubexpressionEliminationSuite.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,18 @@ class SubexpressionEliminationSuite extends SparkFunSuite {
2525
val a: AttributeReference = AttributeReference("name", IntegerType)()
2626
val b1 = a.withName("name2").withExprId(id)
2727
val b2 = a.withExprId(id)
28+
val b3 = a.withQualifiers("qualifierName" :: Nil)
2829

2930
assert(b1 != b2)
3031
assert(a != b1)
3132
assert(b1.semanticEquals(b2))
3233
assert(!b1.semanticEquals(a))
3334
assert(a.hashCode != b1.hashCode)
34-
assert(b1.hashCode == b2.hashCode)
35+
assert(b1.hashCode != b2.hashCode)
3536
assert(b1.semanticHash() == b2.semanticHash())
37+
assert(a != b3)
38+
assert(a.hashCode != b3.hashCode)
39+
assert(a.semanticEquals(b3))
3640
}
3741

3842
test("Expression Equivalence - basic") {

0 commit comments

Comments
 (0)