-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-26021][SQL] replace minus zero with zero in Platform.putDouble/Float #23043
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 3 commits
ee0ef91
63b7f59
f48d4ef
28bd429
20d56eb
a07e614
03408d3
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 |
|---|---|---|
|
|
@@ -24,6 +24,8 @@ | |
| import org.junit.Assert; | ||
| import org.junit.Test; | ||
|
|
||
| import java.nio.ByteBuffer; | ||
|
|
||
| public class PlatformUtilSuite { | ||
|
|
||
| @Test | ||
|
|
@@ -157,4 +159,15 @@ public void heapMemoryReuse() { | |
| Assert.assertEquals(onheap4.size(), 1024 * 1024 + 7); | ||
| Assert.assertEquals(obj3, onheap4.getBaseObject()); | ||
| } | ||
|
|
||
| @Test | ||
| // SPARK-26021 | ||
| public void writeMinusZeroIsReplacedWithZero() { | ||
| byte[] doubleBytes = new byte[Double.BYTES]; | ||
| byte[] floatBytes = new byte[Float.BYTES]; | ||
| Platform.putDouble(doubleBytes, Platform.BYTE_ARRAY_OFFSET, -0.0d); | ||
| Platform.putFloat(floatBytes, Platform.BYTE_ARRAY_OFFSET, -0.0f); | ||
| Assert.assertEquals(0, Double.compare(0.0d, ByteBuffer.wrap(doubleBytes).getDouble())); | ||
|
||
| Assert.assertEquals(0, Float.compare(0.0f, ByteBuffer.wrap(floatBytes).getFloat())); | ||
|
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -723,4 +723,32 @@ class DataFrameAggregateSuite extends QueryTest with SharedSQLContext { | |
| "grouping expressions: [current_date(None)], value: [key: int, value: string], " + | ||
| "type: GroupBy]")) | ||
| } | ||
|
|
||
| test("SPARK-26021: Double and Float 0.0/-0.0 should be equal when grouping") { | ||
| val colName = "i" | ||
| def groupByCollect(df: DataFrame): Array[Row] = { | ||
| df.groupBy(colName).count().collect() | ||
| } | ||
| def assertResult[T](result: Array[Row], zero: T)(implicit ordering: Ordering[T]): Unit = { | ||
| assert(result.length == 1) | ||
| // using compare since 0.0 == -0.0 is true | ||
| assert(ordering.compare(result(0).getAs[T](0), zero) == 0) | ||
|
||
| assert(result(0).getLong(1) == 3) | ||
| } | ||
|
|
||
| spark.conf.set("spark.sql.codegen.wholeStage", "false") | ||
| val doubles = | ||
| groupByCollect(Seq(0.0d, 0.0d, -0.0d).toDF(colName)) | ||
| val doublesBoxed = | ||
| groupByCollect(Seq(Double.box(0.0d), Double.box(0.0d), Double.box(-0.0d)).toDF(colName)) | ||
| val floats = | ||
| groupByCollect(Seq(0.0f, -0.0f, 0.0f).toDF(colName)) | ||
|
||
| val floatsBoxed = | ||
| groupByCollect(Seq(Float.box(0.0f), Float.box(-0.0f), Float.box(0.0f)).toDF(colName)) | ||
|
|
||
| assertResult(doubles, 0.0d) | ||
| assertResult(doublesBoxed, 0.0d) | ||
| assertResult(floats, 0.0f) | ||
| assertResult(floatsBoxed, 0.0f) | ||
| } | ||
| } | ||
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.
I'm fine to put this trick here, shall we also move the IsNaN logic to here as well?