Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package org.apache.spark.sql.catalyst.util

import scala.util.hashing.MurmurHash3

import java.util.{Map => JavaMap}

/**
Expand All @@ -35,6 +37,29 @@ class ArrayBasedMapData(val keyArray: ArrayData, val valueArray: ArrayData) exte
override def toString: String = {
s"keys: $keyArray, values: $valueArray"
}

override def equals(obj: Any): Boolean = {
if (obj == null && this == null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how can this be null?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think IDEA can generate equals and hashCode implementations pretty well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, this can't be null. I'll remove this check.

The IDEA defaults in this case are not super helpful because they just defer to the parent class super.equals(obj).

return true
}

if (obj == null || !obj.isInstanceOf[ArrayBasedMapData]) {
return false
}

val other = obj.asInstanceOf[ArrayBasedMapData]

keyArray.equals(other.keyArray) && valueArray.equals(other.valueArray)
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this part is also causing ComplexDataSuite to fail the "inequality test for MapData" test case, where we use the triple inequality check for object equality. That should be calling equalsTo? This PR's implementation of equals follows the expectation of Literal. Can you comment on the expected behaviour for equals(), @cloud-fan ? Is it supposed to be object equality or element equality?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ideally equals should do element equality.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how we do it in other code, but there is Arrays.deepEquals for element-wise equality

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ArrayBasedMapData equals check in Literals does a == check between the keyArray and the valueArray: https://github.com/apache/spark/blob/master/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/literals.scala#L377-L390

To be consistent, I'll change it to use ==


// Hash this class as a Product of two hashCodes. We don't know the DataType which prevents us
// from getting individual rows for hashing as a Map.
override def hashCode(): Int = {
val seed = MurmurHash3.productSeed
val keyHash = scala.util.hashing.MurmurHash3.mix(seed, keyArray.hashCode())
val valueHash = scala.util.hashing.MurmurHash3.mix(keyHash, valueArray.hashCode())
scala.util.hashing.MurmurHash3.finalizeHash(valueHash, 2)
}
Comment on lines +50 to +55
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @cloud-fan , can you take a look? Do you think we need to also add an explicit hashCode() override to ArrayData (used by keyArray and valueArray)?

}

object ArrayBasedMapData {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,36 @@ class ArrayBasedMapBuilderSuite extends SparkFunSuite with SQLHelper {
Map(new GenericArrayData(Seq(1, 1)) -> 3, new GenericArrayData(Seq(2, 2)) -> 2))
}
}

test("SPARK-40315: simple equal() and hashCode() semantics") {
val dataToAdd: Map[Int, Int] = Map(0 -> -7, 1 -> 3, 10 -> 4, 20 -> 5)
val builder1 = new ArrayBasedMapBuilder(IntegerType, IntegerType)
val builder2 = new ArrayBasedMapBuilder(IntegerType, IntegerType)
val builder3 = new ArrayBasedMapBuilder(IntegerType, IntegerType)
dataToAdd.foreach { case (key, value) =>
builder1.put(key, value)
builder2.put(key, value)
// Replace the value by something slightly different in builder3 for one of the keys.
if (key == 20) {
builder3.put(key, value - 1)
} else {
builder3.put(key, value)
}
}
val arrayBasedMapData1 = builder1.build()
val arrayBasedMapData2 = builder2.build()
val arrayBasedMapData3 = builder3.build()

// We expect two objects to be equal and to have the same hashCode if they have the same
// elements.
assert(arrayBasedMapData1 == arrayBasedMapData2)
assert(arrayBasedMapData1.equals(arrayBasedMapData2))
assert(arrayBasedMapData1.hashCode() == arrayBasedMapData2.hashCode())

// If two objects have different elements, we expect them to not be equal and their hashCode
// to be different.
assert(arrayBasedMapData1 != arrayBasedMapData3)
assert(!arrayBasedMapData1.equals(arrayBasedMapData3))
assert(arrayBasedMapData1.hashCode() != arrayBasedMapData3.hashCode())
}
}