[SPARK-52153][SQL] Fix from_json and to_json with variant #50901
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What changes were proposed in this pull request?
It fixes two minor issues with
from_json(variant)
andto_json(variant)
.from_json(variant)
currently ignores any JSON options. This is inconsistent withfrom_json(nested type containing variant)
, which respects JSON options.to_json(variant)
, when the variant contains special floating-point values (Infinity, NaN), the output is currently not wrapped in quotes. This is inconsistent withto_json(nested type containing floating points)
.to_json(named_struct('a', cast('NaN' as double)))
is{"a":"NaN"}
, while the result ofto_json(to_variant_object(named_struct('a', cast('NaN' as double))))
is{"a":NaN}
{"a":NaN}
can be parsed byfrom_json
when theallowNonNumericNumbers
option is true, it is still not a valid JSON according to the spec.to_json
should produce valid JSON.Why are the changes needed?
This makes variant-related JSON handling more consistent with non-variant JSON handling.
Does this PR introduce any user-facing change?
Yes, as stated above.
How was this patch tested?
Unit test.
Was this patch authored or co-authored using generative AI tooling?
No.