[SPARK-57641] Add variant_insert expression#56703
Open
bojana-db wants to merge 7 commits into
Open
Conversation
variant_insert expressionvariant_insert expression
harshmotw-db
reviewed
Jul 1, 2026
harshmotw-db
left a comment
Contributor
There was a problem hiding this comment.
Thanks for the feature! I have gone through the scala implementation and it looks good to me from an implementation PoV. So far, my only point of contention is whether we want to implicitly cast structs/maps to variant using a to_variant_object transformation which is implicitly lossy. Tagging @srielau to think about this.
Alternatives I can think of are:
- Force users to pass variant as the input argument. They could cast it themselves
- Allow users to pass any data that doesn't contain structs/maps and if they want to use the
to_variant_objecttransformation, they could do that manually and pass variant as the inserted argument instead.
I will go over the Python code and the tests tomorrow
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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?
Adds the SQL function
variant_insert(v, path, val), which inserts a value into a Variant value at a single JSONPath location.Details:
$.b): adds a new field; errors withVARIANT_DUPLICATE_KEYif the key already exists;
$[N]): inserts at indexN, shifting later elements right; anindex past the end pads the gap with variant nulls;
VARIANT_PATH_TYPE_MISMATCHis raised when a path segmentis applied to an incompatible value (e.g. an object key on an array); the root
path
$is rejected withINVALID_VARIANT_PATH, and results exceeding the sizelimit raise
VARIANT_SIZE_LIMIT.Why are the changes needed?
There is no way to add a field or element to a variant; today the only option is to convert it to another datatype (e.g. map), mutate, and convert back.
Does this PR introduce any user-facing change?
Yes, a new SQL function (and Scala/Python
functionsAPI) plus a new error conditionVARIANT_PATH_TYPE_MISMATCH.How was this patch tested?
Unit tests.
Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Code with Claude Opus 4.8