sdf-jkl opened a new issue, #12513: URL: https://github.com/apache/arrow-rs/issues/12513
## Is your feature request related to a problem or challenge? Variant kernels decode each row with `Variant::new` ([variant_array.rs#L72](https://github.com/apache/arrow-rs/blob/76d0ac0b01698381430719cb173e06da800af881/parquet-variant-compute/src/variant_array.rs#L72)), which checks only the headers, and then read it through infallible accessors that `expect()` everything else ([object.rs#L391](https://github.com/apache/arrow-rs/blob/76d0ac0b01698381430719cb173e06da800af881/parquet-variant/src/variant/object.rs#L391), [list.rs#L331](https://github.com/apache/arrow-rs/blob/76d0ac0b01698381430719cb173e06da800af881/parquet-variant/src/variant/list.rs#L331)). So malformed bytes in an unshredded `value` column panic every kernel, including `variant_get` in safe mode, and `VariantArray::try_value` panics as well ([documented](https://github.com/apache/arrow-rs/blob/76d0ac0b01698381430719cb173e06da800af881/parquet-variant-compute/src/variant_array.rs#L392-L395)). On main (76d0ac0b01), with the value `{"a": 1}` corrupted two ways: | Corruption | `try_value` | `variant_get` (safe) | `variant_to_json` | `shred_variant` | |---|---|---|---|---| | field offset points past the buffer | Ok | panic | panic | panic | | `num_elements` set to 200 | panic | panic | panic | panic | The only way to avoid this today is full validation (`Variant::try_new`), which walks the whole value on every row, so kernels skip it (see #7711). #12309 added header-only views ([`VariantView`](https://github.com/apache/arrow-rs/blob/76d0ac0b01698381430719cb173e06da800af881/parquet-variant/src/variant/view.rs#L182) and its object, list and metadata counterparts) that read and bounds-check only the bytes an access uses, and return an error instead of panicking. This is the model Spark uses: its `Variant` is a lazy view over the buffer ([Variant.java#L145](https://github.com/apache/spark/blob/c470f3dce544ad46ffb748949b6476d3cbc8d443/common/variant/src/main/java/org/apache/spark/types/variant/Variant.java#L145)), and validation is a separate opt-in check ([VariantUtil.java#L617](https://github.com/apache/spark/blob/c470f3dce544ad46ffb748949b6476d3cbc8d443/common/variant/src/main/java/org/apache/spark/types/variant/VariantUtil.java#L617)). ## Describe the solution you'd like Move every kernel that reads Variant bytes to views, then simplify the owned types. **1. Kernels read through views**, so malformed bytes become an error instead of a panic: | Reader | Status | |---|---| | `variant_get` on unshredded columns | #12310 (approved) | | `shred_variant` | #12311 (approved, draft) copies leftover fields by id, reads are still owned | | `variant_to_json` | todo | | `variant_to_arrow` row builders | todo | | `unshred_variant` | todo, fully validates each row today | | `VariantArray::try_value` | todo, can use `VariantView::try_to_variant` | | builder copy (`append_variant`) | todo, needs a fallible copy from a view | This also needs iteration on `VariantObjectView`/`VariantListView`, and a depth limit in recursive readers such as `to_json` and the builder copy, since views do no up-front walk (related: #10360). **2. Owned values are always fully validated.** Once kernels stop using shallow owned values, `new` and `try_new` can run the same checks, as in arrow-array ([byte_array.rs#L101-L149](https://github.com/apache/arrow-rs/blob/76d0ac0b01698381430719cb173e06da800af881/arrow-array/src/array/byte_array.rs#L101-L149)): | Constructor | Today | Proposed | |---|---|---| | `try_new` | full validation, returns `Err` | unchanged | | `new` | shallow validation, panics | full validation, panics | | `unsafe new_unchecked` | n/a | no checks, for builder output and bytes validated earlier | `new_unchecked` has to be `unsafe` because validated metadata skips UTF-8 checks (#10879, [metadata.rs#L364](https://github.com/apache/arrow-rs/blob/76d0ac0b01698381430719cb173e06da800af881/parquet-variant/src/variant/metadata.rs#L364)). This removes the `validated` flag, `with_full_validation`, `is_fully_validated` and the `try_*` accessors, and makes `iter()`/`get()` panic-free. Since `Variant::new` would go from constant time to a full walk, it may be better to deprecate it for a release first. ## Describe alternatives you've considered - **Views only, as in Spark**, with validation reduced to a `validate()` check. This drops the validated owned types, which give an infallible API, `PartialEq`, and checks Spark does not make: Spark's `isValidVariant` accepts duplicate and unsorted field names and invalid UTF-8, and overflows the stack on deeply nested input. - **A "validated" flag on `VariantArray`.** It is lost through take/concat/IPC/FFI, and setting it on unchecked bytes would be unsound because of #10879. - **Full validation in each kernel.** Walks every value on every row, which is what views avoid. ## Additional context Views do not check that a child lies inside its parent, which also matches Spark (`getFieldByKey` builds the child at `dataStart + offset`, checked only against the buffer length). So on malformed offsets a view-based kernel can return another value's bytes where the owned types return an error. Open question: should malformed bytes become NULL in safe mode, or always error? Spark's `try_variant_get` returns NULL only for failed casts and raises `MALFORMED_VARIANT` for malformed input ([variantExpressions.scala#L379-L394](https://github.com/apache/spark/blob/c470f3dce544ad46ffb748949b6476d3cbc8d443/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/variant/variantExpressions.scala#L379-L394)). Follow-up to the discussion on #12309. I plan to work on this, unless @cetra3 would like to take it since the views are theirs. cc @scovich @alamb ## Willingness to contribute I plan to contribute to this feature independently -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
