Skip to content

[SPARK-52233][SQL] Fix map_zip_with for Floating Point Types #50967

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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 @@ -1124,23 +1124,40 @@ case class MapZipWith(left: Expression, right: Expression, function: Expression)

private def getKeysWithIndexesFast(keys1: ArrayData, keys2: ArrayData) = {
val hashMap = new mutable.LinkedHashMap[Any, Array[Option[Int]]]
val NaNTracker = Array[Option[Int]](None, None)
for ((z, array) <- Array((0, keys1), (1, keys2))) {
var i = 0
while (i < array.numElements()) {
val key = array.get(i, keyType)
hashMap.get(key) match {
case Some(indexes) =>
if (indexes(z).isEmpty) {
indexes(z) = Some(i)
keyType match {
case FloatType if key.asInstanceOf[Float].isNaN =>
NaNTracker(z) = Some(i)
case DoubleType if key.asInstanceOf[Double].isNaN =>
NaNTracker(z) = Some(i)
case _ =>
hashMap.get(key) match {
case Some(indexes) =>
if (indexes(z).isEmpty) {
indexes(z) = Some(i)
}
case None =>
val indexes = Array[Option[Int]](None, None)
indexes(z) = Some(i)
hashMap.put(key, indexes)
}
case None =>
val indexes = Array[Option[Int]](None, None)
indexes(z) = Some(i)
hashMap.put(key, indexes)
Copy link
Contributor

Choose a reason for hiding this comment

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

My proposal is to simply do

val normalized = if (key == Float.NaN) {
  Float.NaN
} else if (key == Double.NaN) {
  Double.NaN
} else {
  key
}
hashMap.put(key, indexes)

Copy link
Contributor

Choose a reason for hiding this comment

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

The idea is to use the same NaN instance, so that the hash map can work out of the box.

}
i += 1
}
}
if (NaNTracker(0).isDefined || NaNTracker(1).isDefined) {
keyType match {
case FloatType =>
hashMap.put(Float.NaN, NaNTracker)
case DoubleType =>
hashMap.put(Double.NaN, NaNTracker)
case _ =>
}
}
hashMap
}

Expand Down
Loading