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

Conversation

mihailom-db
Copy link
Contributor

What changes were proposed in this pull request?

Fix to map_zip_with expression while handling floating point numbers.

Why are the changes needed?

Previously we would run getKeysWithIndexesFast which would use LinkedHashMap, which does not use proper equality on keys for floating point numbers. All NaNs would be treated in a different way. This PR aims to fix this behaviour.

Example:

select map_zip_with(map(float('NaN'), 1), map(float('NaN'), 2), (k, v1, v2) -> (v1, v2))

Output before:

{"NaN":{"v1":1,"v2":null},"NaN":{"v1":null,"v2":2}}

Output after:

{"NaN":{"v1":1,"v2":2}}

Does this PR introduce any user-facing change?

Yes, fixing the way expression works.

How was this patch tested?

Added tests to golden files.

Was this patch authored or co-authored using generative AI tooling?

No.

@github-actions github-actions bot added the SQL label May 21, 2025
getKeysWithIndexesBruteForce
keyType match {
case properEqualsType if TypeUtils.typeWithProperEquals(properEqualsType) =>
getKeysWithIndexesFast
Copy link
Contributor

Choose a reason for hiding this comment

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

shall we simply changge getKeysWithIndexesFast to normalize the float/double values before putting into the hashmap?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed the code now, could you take one more look?

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.

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 problem is the way we are constructing the indexes. We first input the value from first map, and then check if the value is in the map. The hash map is not using the proper equals on NaN values, so we get into the same problem.
Screenshot 2025-05-23 at 09 40 50

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So either we abstract separate method for Float/Double or we improve getKeysWithIndexesFast in this way.

Copy link
Contributor

Choose a reason for hiding this comment

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

we can also normalize the key before lookup?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants