Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Improve solution
  • Loading branch information
planga82 committed May 13, 2021
commit 1de9c3d6f15fe230b61fc30dee6c539248f0e403
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ abstract class HashExpression[E] extends Expression {

protected def genHashFloat(input: String, result: String): String = {
s"""
|if(Float.floatToIntBits($input) == Float.floatToIntBits(-0.0f)) {
|if($input == -0.0f) {
| ${genHashInt(s"Float.floatToIntBits(0.0f)", result)}
Copy link
Member

Choose a reason for hiding this comment

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

Although this has the semantic, shall we use simply "0" instead of s"Float.floatToIntBits(0.0f)"? We may add some comment for the semantic instead.

jshell> Float.floatToIntBits(0.0f)
$1 ==> 0

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You are right, I have tested it to be sure.
Murmur3HashFunction.hashInt(java.lang.Float.floatToIntBits(0.0f), 42) == Murmur3HashFunction.hashInt(0, 42)
It is simpler.

|} else {
| ${genHashInt(s"Float.floatToIntBits($input)", result)}
Expand All @@ -381,7 +381,7 @@ abstract class HashExpression[E] extends Expression {

protected def genHashDouble(input: String, result: String): String = {
s"""
|if(Double.doubleToLongBits($input) == Double.doubleToLongBits(-0.0d)) {
|if($input == -0.0d) {
| ${genHashLong(s"Double.doubleToLongBits(0.0d)", result)}
Copy link
Member

Choose a reason for hiding this comment

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

ditto. We had better use the simplest constant here instead of s"Double.doubleToLongBits(0.0d)".

Copy link
Member

Choose a reason for hiding this comment

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

In this case, 0L?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The same as the previous point, thanks

|} else {
| ${genHashLong(s"Double.doubleToLongBits($input)", result)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -710,12 +710,13 @@ class HashExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {

test("SPARK-35207: Compute hash consistent between -0.0 and 0.0") {
def checkResult(exprs1: Expression, exprs2: Expression): Unit = {
assert(Murmur3Hash(Seq(exprs1), 42).eval() == Murmur3Hash(Seq(exprs2), 42).eval())
assert(XxHash64(Seq(exprs1), 42).eval() == XxHash64(Seq(exprs2), 42).eval())
assert(HiveHash(Seq(exprs1)).eval() == HiveHash(Seq(exprs2)).eval())
checkEvaluation(Murmur3Hash(Seq(exprs1), 42), Murmur3Hash(Seq(exprs2), 42).eval())
checkEvaluation(XxHash64(Seq(exprs1), 42), XxHash64(Seq(exprs2), 42).eval())
checkEvaluation(HiveHash(Seq(exprs1)), HiveHash(Seq(exprs2)).eval())
}
checkResult(Literal.create(0D, DoubleType), Literal.create(-0D, DoubleType))
checkResult(Literal.create(0L, LongType), Literal.create(-0L, LongType))

checkResult(Literal.create(-0D, DoubleType), Literal.create(0D, DoubleType))
checkResult(Literal.create(-0F, FloatType), Literal.create(0F, FloatType))
}

private def testHash(inputSchema: StructType): Unit = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -708,30 +708,4 @@ class WholeStageCodegenSuite extends QueryTest with SharedSparkSession
}
}
}

test("SPARK-35207: Compute hash consistent between -0.0 and 0.0 doubles with Codegen") {
val data = Seq((0.0d, -1.0d, 1.0d))
withTempPath { dir =>
val path = dir.getCanonicalPath
data.toDF("col1", "col2", "col3").write.parquet(path)
sql(s"create table testHash(col1 double, col2 double, col3 double) " +
s"using parquet location '$path'")
sql("select hash(col1 / col2) == hash(col1 / col3) from testHash").collect()
.foreach(row => assert(row.getBoolean(0) == true))
sql("drop table testHash")
}
}

test("SPARK-35207: Compute hash consistent between -0.0 and 0.0 floats with Codegen") {
val data = Seq((0.0f, -1.0f, 1.0f))
withTempPath { dir =>
val path = dir.getCanonicalPath
data.toDF("col1", "col2", "col3").write.parquet(path)
sql(s"create table testHash(col1 float, col2 float, col3 float) " +
s"using parquet location '$path'")
sql("select hash(col1 / col2) == hash(col1 / col3) from testHash").collect()
.foreach(row => assert(row.getBoolean(0) == true))
sql("drop table testHash")
}
}
}