Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 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 @@ -120,6 +120,9 @@ public static float getFloat(Object object, long offset) {
}

public static void putFloat(Object object, long offset, float value) {
if(value == -0.0f) {
Copy link
Contributor

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?

value = 0.0f;
}
_UNSAFE.putFloat(object, offset, value);
}

Expand All @@ -128,6 +131,9 @@ public static double getDouble(Object object, long offset) {
}

public static void putDouble(Object object, long offset, double value) {
if(value == -0.0d) {
value = 0.0d;
}
_UNSAFE.putDouble(object, offset, value);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import org.junit.Assert;
import org.junit.Test;

import java.nio.ByteBuffer;

public class PlatformUtilSuite {

@Test
Expand Down Expand Up @@ -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()));
Copy link
Contributor

Choose a reason for hiding this comment

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

are you sure this test fails before the fix? IIUC 0.0 == -0.0 is ture, but they have different binary format

Copy link
Contributor

Choose a reason for hiding this comment

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

BTW thanks for adding the unit test! It's a good complementary to the end-to-end test.

Copy link
Author

Choose a reason for hiding this comment

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

yeah, it fails. Indeed 0.0 == -0.0 so I'm using Double.compare == 0 to test this.

Assert.assertEquals(0, Float.compare(0.0f, ByteBuffer.wrap(floatBytes).getFloat()));
Copy link
Contributor

Choose a reason for hiding this comment

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

can we use Platform.getFloat to read the value back? to match how we write it.

Copy link
Contributor

Choose a reason for hiding this comment

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

and would be better to directly check that, the binary of 0.0 and -0.0 are same.

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of checking the result, I prefer the code snippet in the JIRA ticket, which is more obvious about where is the problem.

Let's run a group-by query, with both 0.0 and -0.0 in the input. Then we check the number of result rows, as ideally 0.0 and -0.0 is same, so we should only have one group(one result row).

Copy link
Author

Choose a reason for hiding this comment

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

I'm not sure I follow, below this I'm constructing Seqs with 0 and -0 like in the JIRA and in the assertResult helper I'm checking that there's only 1 line like you said.
Do you mean the check that the key is indeed 0.0 and not -0.0 is redundant?

Copy link
Contributor

Choose a reason for hiding this comment

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

ah sorry I misread the code.

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))
Copy link
Contributor

Choose a reason for hiding this comment

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

why we have to turn off whole-stage-codegen?

Copy link
Author

Choose a reason for hiding this comment

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

looks like leftovers from a different solution. Also there's no need to test the boxed version now that it's not in the codegen. I'll simplify the test.

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)
}
}