You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by GitBox <gi...@apache.org> on 2022/09/02 12:38:25 UTC
[GitHub] [spark] c27kwan opened a new pull request, #37771: [SPARK-40315] Add equals() and hashCode() to ArrayBasedMapData
c27kwan opened a new pull request, #37771:
URL: https://github.com/apache/spark/pull/37771
<!--
Thanks for sending a pull request! Here are some tips for you:
1. If this is your first time, please read our contributor guidelines: https://spark.apache.org/contributing.html
2. Ensure you have added or run the appropriate tests for your PR: https://spark.apache.org/developer-tools.html
3. If the PR is unfinished, add '[WIP]' in your PR title, e.g., '[WIP][SPARK-XXXX] Your PR title ...'.
4. Be sure to keep the PR description updated to reflect all changes.
5. Please write your PR title to summarize what this PR proposes.
6. If possible, provide a concise example to reproduce the issue for a faster review.
7. If you want to add a new configuration, please read the guideline first for naming configurations in
'core/src/main/scala/org/apache/spark/internal/config/ConfigEntry.scala'.
8. If you want to add or modify an error type or message, please read the guideline first in
'core/src/main/resources/error/README.md'.
-->
### What changes were proposed in this pull request?
There is no explicit `hashCode()` function override for the `ArrayBasedMapData` LogicalPlan. As a result, there is a non-deterministic error where the `hashCode()` computed for `ArrayBasedMapData` can be different for two equal objects (objects with equal keys and values).
In this PR, we override the `hashCode` function so that it works exactly as we expect. We also have an explicit `equals()` function for consistency with how `Literal`s check for equality of `ArrayBasedMapData`.
### Why are the changes needed?
This is a bug fix for a non-deterministic error. It is also more consistent with the rest of Spark if we implement the `hashCode` and `equals` methods instead of relying on defaults.
### Does this PR introduce _any_ user-facing change?
No
### How was this patch tested?
A simple unit test was added.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] c27kwan commented on a diff in pull request #37771: [SPARK-40315][SQL] Add equals() and hashCode() to ArrayBasedMapData
Posted by GitBox <gi...@apache.org>.
c27kwan commented on code in PR #37771:
URL: https://github.com/apache/spark/pull/37771#discussion_r961653435
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/ArrayBasedMapData.scala:
##########
@@ -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) {
+ return true
+ }
+
+ if (obj == null || !obj.isInstanceOf[ArrayBasedMapData]) {
+ return false
+ }
+
+ val other = obj.asInstanceOf[ArrayBasedMapData]
+
+ keyArray.equals(other.keyArray) && valueArray.equals(other.valueArray)
+ }
+
+ // 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)
+ }
Review Comment:
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`)?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] c27kwan commented on pull request #37771: [SPARK-40315][SQL] Add equals() and hashCode() to ArrayBasedMapData
Posted by GitBox <gi...@apache.org>.
c27kwan commented on PR #37771:
URL: https://github.com/apache/spark/pull/37771#issuecomment-1238126742
Oops, here's the correct PR link: https://github.com/apache/spark/pull/37807
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] cloud-fan commented on a diff in pull request #37771: [SPARK-40315][SQL] Add equals() and hashCode() to ArrayBasedMapData
Posted by GitBox <gi...@apache.org>.
cloud-fan commented on code in PR #37771:
URL: https://github.com/apache/spark/pull/37771#discussion_r961806515
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/ArrayBasedMapData.scala:
##########
@@ -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) {
+ return true
+ }
+
+ if (obj == null || !obj.isInstanceOf[ArrayBasedMapData]) {
+ return false
+ }
+
+ val other = obj.asInstanceOf[ArrayBasedMapData]
+
+ keyArray.equals(other.keyArray) && valueArray.equals(other.valueArray)
+ }
Review Comment:
ideally `equals` should do element equality.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] c27kwan commented on a diff in pull request #37771: [SPARK-40315][SQL] Add equals() and hashCode() to ArrayBasedMapData
Posted by GitBox <gi...@apache.org>.
c27kwan commented on code in PR #37771:
URL: https://github.com/apache/spark/pull/37771#discussion_r963024550
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/ArrayBasedMapData.scala:
##########
@@ -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) {
+ return true
+ }
+
+ if (obj == null || !obj.isInstanceOf[ArrayBasedMapData]) {
+ return false
+ }
+
+ val other = obj.asInstanceOf[ArrayBasedMapData]
+
+ keyArray.equals(other.keyArray) && valueArray.equals(other.valueArray)
+ }
Review Comment:
The `ArrayBasedMapData` `equals` check in `Literal`s 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 `==`
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/ArrayBasedMapData.scala:
##########
@@ -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) {
Review Comment:
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)`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] cloud-fan commented on pull request #37771: [SPARK-40315][SQL] Add equals() and hashCode() to ArrayBasedMapData
Posted by GitBox <gi...@apache.org>.
cloud-fan commented on PR #37771:
URL: https://github.com/apache/spark/pull/37771#issuecomment-1238107463
There are 2 things:
1. SQL semantic: map type is not orderable and can't be used as join/group keys
2. MapData needs to implement equals/hashCode as we need to look up plans (e.g. in CacheManager) and plans may contain map literals.
In long term, we need to support equality check of map values, so that we can use map type as join/group keys. In short term, we need to correctly implement `QueryPlan.semanticEquals` which requires correct `hashCode/equals` of map literals.
I'm +1 to this idea: `I'm going to make another variant of this PR and fix the hashCode only for Literal of ArrayBasedMapData`
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] c27kwan commented on pull request #37771: [SPARK-40315][SQL] Add equals() and hashCode() to ArrayBasedMapData
Posted by GitBox <gi...@apache.org>.
c27kwan commented on PR #37771:
URL: https://github.com/apache/spark/pull/37771#issuecomment-1237905787
I've been going through old PRs and see that the `equals` and `hashCode` methods were actually purposely removed from this class: https://github.com/apache/spark/pull/13847
We've also chosen to honour this removal by adding the checks in other places like `Literal`: https://github.com/apache/spark/pull/30246
I also saw a newer PR (not merged) attempting to add these methods back in: https://github.com/apache/spark/pull/32552
Is this going to break an invariant if we now provide an equals and hashCode function only for ArrayBasedMapData? I'm going to make another variant of this PR and fix the `hashCode` only for `Literal` of `ArrayBasedMapData`, since that's the only place that's inconsistent right now between the `equals` and the `hashCode`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] c27kwan closed pull request #37771: [SPARK-40315][SQL] Add equals() and hashCode() to ArrayBasedMapData
Posted by GitBox <gi...@apache.org>.
c27kwan closed pull request #37771: [SPARK-40315][SQL] Add equals() and hashCode() to ArrayBasedMapData
URL: https://github.com/apache/spark/pull/37771
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] srowen commented on a diff in pull request #37771: [SPARK-40315][SQL] Add equals() and hashCode() to ArrayBasedMapData
Posted by GitBox <gi...@apache.org>.
srowen commented on code in PR #37771:
URL: https://github.com/apache/spark/pull/37771#discussion_r962166466
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/ArrayBasedMapData.scala:
##########
@@ -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) {
+ return true
+ }
+
+ if (obj == null || !obj.isInstanceOf[ArrayBasedMapData]) {
+ return false
+ }
+
+ val other = obj.asInstanceOf[ArrayBasedMapData]
+
+ keyArray.equals(other.keyArray) && valueArray.equals(other.valueArray)
+ }
Review Comment:
Not sure how we do it in other code, but there is Arrays.deepEquals for element-wise equality
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] AmplabJenkins commented on pull request #37771: [SPARK-40315][SQL] Add equals() and hashCode() to ArrayBasedMapData
Posted by GitBox <gi...@apache.org>.
AmplabJenkins commented on PR #37771:
URL: https://github.com/apache/spark/pull/37771#issuecomment-1236246575
Can one of the admins verify this patch?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] srowen commented on a diff in pull request #37771: [SPARK-40315][SQL] Add equals() and hashCode() to ArrayBasedMapData
Posted by GitBox <gi...@apache.org>.
srowen commented on code in PR #37771:
URL: https://github.com/apache/spark/pull/37771#discussion_r962166373
##########
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/util/ArrayBasedMapBuilderSuite.scala:
##########
@@ -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)
Review Comment:
I think this is redundant with the next line? they aren't the same object, so this isn't ref equality
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] c27kwan commented on a diff in pull request #37771: [SPARK-40315][SQL] Add equals() and hashCode() to ArrayBasedMapData
Posted by GitBox <gi...@apache.org>.
c27kwan commented on code in PR #37771:
URL: https://github.com/apache/spark/pull/37771#discussion_r961663549
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/ArrayBasedMapData.scala:
##########
@@ -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) {
+ return true
+ }
+
+ if (obj == null || !obj.isInstanceOf[ArrayBasedMapData]) {
+ return false
+ }
+
+ val other = obj.asInstanceOf[ArrayBasedMapData]
+
+ keyArray.equals(other.keyArray) && valueArray.equals(other.valueArray)
+ }
Review Comment:
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?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] cloud-fan commented on pull request #37771: [SPARK-40315][SQL] Add equals() and hashCode() to ArrayBasedMapData
Posted by GitBox <gi...@apache.org>.
cloud-fan commented on PR #37771:
URL: https://github.com/apache/spark/pull/37771#issuecomment-1238111033
@c27kwan did you put the wrong link of your new PR?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] cloud-fan commented on a diff in pull request #37771: [SPARK-40315][SQL] Add equals() and hashCode() to ArrayBasedMapData
Posted by GitBox <gi...@apache.org>.
cloud-fan commented on code in PR #37771:
URL: https://github.com/apache/spark/pull/37771#discussion_r961805586
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/ArrayBasedMapData.scala:
##########
@@ -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) {
Review Comment:
I think IDEA can generate equals and hashCode implementations pretty well.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] cloud-fan commented on a diff in pull request #37771: [SPARK-40315][SQL] Add equals() and hashCode() to ArrayBasedMapData
Posted by GitBox <gi...@apache.org>.
cloud-fan commented on code in PR #37771:
URL: https://github.com/apache/spark/pull/37771#discussion_r961805057
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/ArrayBasedMapData.scala:
##########
@@ -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) {
Review Comment:
how can `this` be null?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] c27kwan commented on pull request #37771: [SPARK-40315][SQL] Add equals() and hashCode() to ArrayBasedMapData
Posted by GitBox <gi...@apache.org>.
c27kwan commented on PR #37771:
URL: https://github.com/apache/spark/pull/37771#issuecomment-1237957742
I made a new PR that I think works better because it has a more narrow area of impact: https://github.com/apache/spark/pull/3780
I'm going to close this PR because the proposed solution in this PR isn't as good.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org