-
Notifications
You must be signed in to change notification settings - Fork 28.3k
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
[SPARK-24659][SQL] GenericArrayData.equals should respect element type differences #21643
Changes from 2 commits
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 |
---|---|---|
|
@@ -104,4 +104,38 @@ class ComplexDataSuite extends SparkFunSuite { | |
// The copied data should not be changed externally. | ||
assert(copied.getStruct(0, 1).getUTF8String(0).toString == "a") | ||
} | ||
|
||
test("SPARK-24659: GenericArrayData.equals should respect element type differences") { | ||
import scala.reflect.ClassTag | ||
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. nit: you can move this import to the head of this file. 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. Thanks for your suggestion! I'm used to making one-off imports inside a function when an import is only used within that function, so that the scope is as narrow as possible without being disturbing. |
||
|
||
// Expected positive cases | ||
def arraysShouldEqual[T: ClassTag](element: T*): Unit = { | ||
val array1 = new GenericArrayData(Array[T](element: _*)) | ||
val array2 = new GenericArrayData(Array[T](element: _*)) | ||
assert(array1.equals(array2)) | ||
} | ||
arraysShouldEqual(true, false) // Boolean | ||
arraysShouldEqual(0.toByte, 123.toByte, (-123).toByte) // Byte | ||
arraysShouldEqual(0.toShort, 123.toShort, (-256).toShort) // Short | ||
arraysShouldEqual(0, 123, -65536) // Int | ||
arraysShouldEqual(0L, 123L, -65536L) // Long | ||
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. It is not important but if you are checking corner cases, probably, it makes sense to pass values like 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 a good one. I can do that (and NaNs/Infinity for floating point types too) |
||
arraysShouldEqual(0.0F, 123.0F, -65536.0F) // Float | ||
arraysShouldEqual(0.0, 123.0, -65536.0) // Double | ||
arraysShouldEqual(Array[Byte](123.toByte), null) // Binary (Array[Byte]) | ||
arraysShouldEqual(UTF8String.fromString("foo"), null) // String (UTF8String) | ||
|
||
// Expected negative cases | ||
// Spark SQL considers cases like array<int> vs array<long> to be incompatible, | ||
// so an underlying implementation of array type should return false in such cases. | ||
def arraysShouldNotEqual[T: ClassTag, U: ClassTag](element1: T, element2: U): Unit = { | ||
val array1 = new GenericArrayData(Array[T](element1)) | ||
val array2 = new GenericArrayData(Array[U](element2)) | ||
assert(!array1.equals(array2)) | ||
} | ||
arraysShouldNotEqual(true, 1) // Boolean <-> Int | ||
arraysShouldNotEqual(123.toByte, 123) // Byte <-> Int | ||
arraysShouldNotEqual(123.toByte, 123L) // Byte <-> Long | ||
arraysShouldNotEqual(123.toShort, 123) // Short <-> Int | ||
arraysShouldNotEqual(123, 123L) // Int <-> Long | ||
} | ||
} |
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.
Are there any needs to handle
Array[Byte]
separately above?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.
in java
byte[]
or other primitive arrays doesn't have a properequals
implementation.