Skip to content

[SPARK-58222][SQL] Add try_variant_array_append expression#57378

Closed
bojana-db wants to merge 3 commits into
apache:masterfrom
bojana-db:try-variant-array-append
Closed

[SPARK-58222][SQL] Add try_variant_array_append expression#57378
bojana-db wants to merge 3 commits into
apache:masterfrom
bojana-db:try-variant-array-append

Conversation

@bojana-db

@bojana-db bojana-db commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

What changes were proposed in this pull request?

Adds the SQL function try_variant_array_append(v, path, val), the error-tolerant counterpart of variant_array_append. It appends a value to the array in a Variant value at a single JSONPath location, returning NULL instead of failing the query when the operation hits a recoverable error.

Details:

  • On a valid append, behaves exactly like variant_array_append: appends val as a new last element of the array at path (e.g. $.a, $[0], $.a[1]); the root path $ targets the whole Variant, which must itself be an array; a missing key or out-of-range array index along the path leaves v unchanged;
  • Recoverable errors are caught and return NULL instead of throwing: a path type mismatch (VARIANT_PATH_TYPE_MISMATCH), raised when a path segment is applied to a value of an incompatible type or when the target is not an array;
  • Non-recoverable errors still throw, matching variant_array_append: a malformed path is rejected with INVALID_VARIANT_PATH (the path is parsed before the append), and a result exceeding the size limit raises VARIANT_SIZE_LIMIT;
  • Any NULL argument returns NULL;
  • The value may be any expression castable to variant. An array value is appended as a single nested element rather than flattened, and a variant null appends a null element.

Why are the changes needed?

Error-tolerant counterpart of variant_array_append.

Does this PR introduce any user-facing change?

Yes, a new SQL function try_variant_array_append (and the corresponding Scala/Python functions API).

How was this patch tested?

Unit tests.

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

Generated-by: Cursor with Claude Opus 4.8

@bojana-db bojana-db changed the title Add try_variant_array_append expression [SPARK-58222] Add try_variant_array_append expression Jul 20, 2026

@harshmotw-db harshmotw-db left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM, thanks for the feature!

@uros-b uros-b left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you @bojana-db and @harshmotw-db!

@uros-b uros-b changed the title [SPARK-58222] Add try_variant_array_append expression [SPARK-58222][SQL] Add try_variant_array_append expression Jul 21, 2026
@uros-b uros-b closed this in f0cbaf7 Jul 21, 2026
uros-b pushed a commit that referenced this pull request Jul 21, 2026
### What changes were proposed in this pull request?
Adds the SQL function `try_variant_array_append(v, path, val)`, the error-tolerant counterpart of `variant_array_append`. It appends a value to the array in a Variant value at a single JSONPath location, returning NULL instead of failing the query when the operation hits a recoverable error.

Details:

- On a valid append, behaves exactly like `variant_array_append`: appends `val` as a new last element of the array at `path` (e.g. `$.a`, `$[0]`, `$.a[1]`); the root path `$` targets the whole Variant, which must itself be an array; a missing key or out-of-range array index along the path leaves `v` unchanged;
- Recoverable errors are caught and return NULL instead of throwing: a path type mismatch (`VARIANT_PATH_TYPE_MISMATCH`), raised when a path segment is applied to a value of an incompatible type or when the target is not an array;
- Non-recoverable errors still throw, matching `variant_array_append`: a malformed path is rejected with `INVALID_VARIANT_PATH` (the path is parsed before the append), and a result exceeding the size limit raises `VARIANT_SIZE_LIMIT`;
- Any NULL argument returns NULL;
- The value may be any expression castable to variant. An array value is appended as a single nested element rather than flattened, and a variant null appends a null element.

### Why are the changes needed?
Error-tolerant counterpart of `variant_array_append`.

### Does this PR introduce _any_ user-facing change?
Yes, a new SQL function `try_variant_array_append` (and the corresponding Scala/Python `functions` API).

### How was this patch tested?
Unit tests.

### Was this patch authored or co-authored using generative AI tooling?
Generated-by: Cursor with Claude Opus 4.8

Closes #57378 from bojana-db/try-variant-array-append.

Authored-by: bojana-db <bojana.zecevic@databricks.com>
Signed-off-by: Uros Bojanic <221401595+uros-b@users.noreply.github.com>
(cherry picked from commit f0cbaf7)
Signed-off-by: Uros Bojanic <221401595+uros-b@users.noreply.github.com>
@uros-b

uros-b commented Jul 21, 2026

Copy link
Copy Markdown
Member

Merge Summary:

Posted by merge_spark_pr.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants