-
Notifications
You must be signed in to change notification settings - Fork 29.3k
[SPARK-29918][SQL] RecordBinaryComparator should check endianness when compared by long #26548
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 1 commit
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 |
|---|---|---|
|
|
@@ -20,6 +20,8 @@ | |
| import org.apache.spark.unsafe.Platform; | ||
| import org.apache.spark.util.collection.unsafe.sort.RecordComparator; | ||
|
|
||
| import java.nio.ByteOrder; | ||
|
|
||
| public final class RecordBinaryComparator extends RecordComparator { | ||
|
|
||
| @Override | ||
|
|
@@ -49,9 +51,13 @@ public int compare( | |
| // for architectures that support unaligned accesses, chew it up 8 bytes at a time | ||
| if (Platform.unaligned() || (((leftOff + i) % 8 == 0) && ((rightOff + i) % 8 == 0))) { | ||
| while (i <= leftLen - 8) { | ||
| final long v1 = Platform.getLong(leftObj, leftOff + i); | ||
| final long v2 = Platform.getLong(rightObj, rightOff + i); | ||
| long v1 = Platform.getLong(leftObj, leftOff + i); | ||
| long v2 = Platform.getLong(rightObj, rightOff + i); | ||
| if (v1 != v2) { | ||
| if (ByteOrder.nativeOrder().equals(ByteOrder.LITTLE_ENDIAN)) { | ||
| v1 = Long.reverseBytes(v1); | ||
| v2 = Long.reverseBytes(v2); | ||
| } | ||
| return v1 > v2 ? 1 : -1; | ||
|
Member
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. Pulling together my comment with #25491 (comment) - maybe this also has to be
Contributor
Author
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. Yes, thanks for point out this. This is the same issue with #25491 (comment). And this can be reproduced in our cluster. |
||
| } | ||
| i += 8; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -273,7 +273,7 @@ public void testBinaryComparatorWhenSubtractionIsDivisibleByMaxIntValue() throws | |
| insertRow(row1); | ||
| insertRow(row2); | ||
|
|
||
| assert(compare(0, 1) < 0); | ||
| assert(compare(0, 1) > 0); | ||
|
Member
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. So, do you mean this is wrong before this PR?
Member
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. The change definitely changes the ordering, as bytes are compared in a different order.
Contributor
Author
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.
|
||
| } | ||
|
|
||
| @Test | ||
|
|
||
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.
Cant his check happen once and make it a constant rather than call each time?
This is also the unfortunately the common case, little-endian architectures. Hm, I'm trying to think of an alternative that would introduce overhead only for big-endian, but not sure if it's possible. that said
reverseBytesought to be very fast. And I guess this only happens once before this terminates.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.
To clarify the issue isn't endianness alone, but aligned access? varying either one could cause the result to change.