[SPARK-57804][SQL] Add variant_set expression#57125
Conversation
variant_set expressionvariant_set expression
variant_set expressionvariant_set expression
shrirangmhalgi
left a comment
There was a problem hiding this comment.
Design question (non-blocking)
When createIfMissing is false, a type mismatch (e.g., object-key segment hitting a scalar) throws VARIANT_PATH_TYPE_MISMATCH. Should it instead leave the variant unchanged (same as missing-key behavior)?
PostgreSQL's jsonb_set(target, path, new_value, create_missing := false) returns target unchanged when a path is unreachable -- the docs state: "If these conditions are not met the target is returned unchanged." This covers both missing intermediates AND type mismatches.
The current behavior is internally consistent (createIfMissing=true also throws on type mismatch), but users migrating from PostgreSQL may expect silent passthrough when create_if_missing is false.
Not blocking - just flagging since it's a user-facing semantic difference from the ecosystem precedent.
What changes were proposed in this pull request?
Adds the SQL function
variant_set(v, path, val[, create_if_missing]), which sets (inserts or replaces) a value in a Variant value at a single JSONPath location.Details:
$.a): replaces the field if it exists; creates it whencreate_if_missingis true (the default);$[N]): replaces the element at indexN; whenNis at or past the end andcreate_if_missingis true, the array is padded with variant nulls up toN;create_if_missingis false, a missing leaf, a missing intermediate, or an out-of-range array index leavesvunchanged;VARIANT_PATH_TYPE_MISMATCHis raised when a path segment is applied to a value of an incompatible type; the root path$is rejected withINVALID_VARIANT_PATH, and results exceeding the size limit raiseVARIANT_SIZE_LIMIT.Why are the changes needed?
Without
variant_set, updating a variant means converting it to another datatype (e.g. map), mutating, and converting back.Does this PR introduce any user-facing change?
Yes, a new SQL function (and Scala/Python
functionsAPI).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