This is an automated email from the ASF dual-hosted git repository. github-merge-queue[bot] pushed a commit to branch gh-readonly-queue/main/pr-12246-e3167536ad6059589a9e9fc58ca48534d3e229ff in repository https://gitbox.apache.org/repos/asf/arrow-rs.git
commit 7a1e79d0c2115f7dd1f7443c79203c30f9b098c1 Author: dayan02dev <[email protected]> AuthorDate: Mon Nov 16 20:23:44 2026 +0000 fix(parquet-variant-compute): reject unsupported types in empty and all-null cast_to_variant input (#12246) # Which issue does this PR close? - Closes #11946 - Part of #10546 # Rationale for this change `cast_to_variant` returned `Ok` for duration/interval types (directly or nested) when the input was empty or all null, and the unsupported-type error as soon as one row held a value. There are two causes: 1. The all-null fast path in `cast_to_variant_with_options` returned before `make_arrow_to_variant_row_builder`, which is the only place types are checked. 2. `UnionArrowToVariantBuilder::new` looped over the per-row `type_ids()` buffer, so it only built (and type-checked) the children some row selects, and rebuilt that child's builder once per row. Moving the type check before the shortcut, as the issue suggests, fixes the first cause but not the second: an empty union with a duration child, or one whose rows never select that child, is still accepted. So the union loop also changes. # What changes are included in this PR? - Build the row builder before the all-null fast path, so the existing recursive type check runs first. Supported types still take the bulk `append_nulls` fast path. - Create one union child builder per declared field (`union_array.fields().iter()`) instead of per row. This matches the comment above the loop and the behaviour before #8299. #12211 also edits the body of the same union loop, so one of the two will need a small rebase. # Are these changes tested? Yes. `test_cast_to_variant_duration_or_interval_errors` now also checks empty and all-null arrays of each of the seven duration/interval types, directly and inside a list, a struct and a sparse union, plus supported controls. Without the fix it fails with `List(Duration(s)) with 0 rows`; with only the fast-path reorder it still fails on the empty union case. # Are there any significant user-facing changes? - Empty or all-null arrays of unsupported types (including nested ones), and unions that declare an unsupported child, now return the same `InvalidArgumentError` as populated arrays instead of `Ok`. No signatures change. - All-null map and dictionary inputs now pay builder construction (for maps, casting the keys to Utf8; for dictionaries, normalising the keys). Because a map slice's keys are the parent's whole entries array, an all-null 1000-row slice of a 1M-entry map goes from ~0.01 ms to ~17 ms, the same as a populated slice of that map already costs. Other all-null types show no measurable change. - Valid unions convert ~1.8× faster, since child builders are no longer rebuilt per row. If you'd rather keep the fast path cost-free for maps, the alternatives are narrowing the map key cast to the offsets range, or a `DataType`-only recursive check before the shortcut. # AI usage disclosure The patch, test, verification and the benchmark numbers above were produced with an AI coding agent (Claude Code), and a separate agent session re-checked them before submission. --- parquet-variant-compute/src/arrow_to_variant.rs | 3 +- parquet-variant-compute/src/cast_to_variant.rs | 70 +++++++++++++++++++------ 2 files changed, 55 insertions(+), 18 deletions(-) diff --git a/parquet-variant-compute/src/arrow_to_variant.rs b/parquet-variant-compute/src/arrow_to_variant.rs index 4c2ed3a49f..8fb5a92b9a 100644 --- a/parquet-variant-compute/src/arrow_to_variant.rs +++ b/parquet-variant-compute/src/arrow_to_variant.rs @@ -663,11 +663,10 @@ pub(crate) struct UnionArrowToVariantBuilder<'a> { impl<'a> UnionArrowToVariantBuilder<'a> { pub(crate) fn new(array: &'a dyn Array, options: &'a CastOptions) -> Result<Self, ArrowError> { let union_array = array.as_union(); - let type_ids = union_array.type_ids(); // Create child builders for each union field let mut child_builders = HashMap::new(); - for &type_id in type_ids { + for (type_id, _) in union_array.fields().iter() { let child_array = union_array.child(type_id); let child_builder = make_arrow_to_variant_row_builder( child_array.data_type(), diff --git a/parquet-variant-compute/src/cast_to_variant.rs b/parquet-variant-compute/src/cast_to_variant.rs index 27e367532d..c974f75b6e 100644 --- a/parquet-variant-compute/src/cast_to_variant.rs +++ b/parquet-variant-compute/src/cast_to_variant.rs @@ -45,6 +45,9 @@ pub fn cast_to_variant_with_options( input: &dyn Array, options: &CastOptions, ) -> Result<VariantArray, ArrowError> { + // Build before the all-null fast path so unsupported types fail regardless of contents. + let mut row_builder = make_arrow_to_variant_row_builder(input.data_type(), input, options)?; + // Fast path: any all-null input maps to an all-null VariantArray. if input.null_count() == input.len() { let mut array_builder = VariantArrayBuilder::new(input.len()); @@ -52,9 +55,6 @@ pub fn cast_to_variant_with_options( return Ok(array_builder.build()); } - // Create row builder for the input array type - let mut row_builder = make_arrow_to_variant_row_builder(input.data_type(), input, options)?; - // Create output array builder let mut array_builder = VariantArrayBuilder::new(input.len()); @@ -96,7 +96,7 @@ mod tests { StructArray, Time32MillisecondArray, Time32SecondArray, Time64MicrosecondArray, Time64NanosecondArray, TimestampMicrosecondArray, TimestampMillisecondArray, TimestampNanosecondArray, TimestampSecondArray, UInt8Array, UInt16Array, UInt32Array, - UInt64Array, UnionArray, + UInt64Array, UnionArray, new_empty_array, new_null_array, }; use arrow::buffer::{NullBuffer, OffsetBuffer, ScalarBuffer}; use arrow::datatypes::{ @@ -107,7 +107,7 @@ mod tests { use arrow_schema::{ DECIMAL32_MAX_PRECISION, DECIMAL64_MAX_PRECISION, DECIMAL128_MAX_PRECISION, }; - use arrow_schema::{DataType, Field, Fields, UnionFields}; + use arrow_schema::{DataType, Field, Fields, UnionFields, UnionMode}; use chrono::{DateTime, NaiveDate, NaiveTime}; use half::f16; use parquet_variant::{ @@ -1127,20 +1127,58 @@ mod tests { ]; for array in arrays { - let result = cast_to_variant(array.as_ref()); - assert!(result.is_err()); - match result.unwrap_err() { - ArrowError::InvalidArgumentError(msg) => { - assert!( - msg.contains("Casting duration/interval types to Variant is not supported") - ); - assert!( - msg.contains("The Variant format does not define duration/interval types") - ); + let data_type = array.data_type().clone(); + let field = Field::new("value", data_type.clone(), true); + let union_fields = UnionFields::from_fields(vec![ + Field::new("int", DataType::Int32, true), + field.clone(), + ]); + let mut cases: Vec<ArrayRef> = vec![array.into()]; + for data_type in [ + DataType::new_list(data_type.clone(), true), + DataType::Struct(Fields::from(vec![field])), + // new_null_array points every union row at the first (Int32) child + DataType::Union(union_fields, UnionMode::Sparse), + data_type, + ] { + cases.push(new_empty_array(&data_type)); + cases.push(new_null_array(&data_type, 2)); + } + + for array in cases { + let result = cast_to_variant(array.as_ref()); + assert!( + result.is_err(), + "{} with {} rows", + array.data_type(), + array.len() + ); + match result.unwrap_err() { + ArrowError::InvalidArgumentError(msg) => { + assert!(msg.contains( + "Casting duration/interval types to Variant is not supported" + )); + assert!(msg.contains( + "The Variant format does not define duration/interval types" + )); + } + _ => panic!("Expected InvalidArgumentError"), } - _ => panic!("Expected InvalidArgumentError"), } } + + let union_fields = UnionFields::from_fields(vec![ + Field::new("int", DataType::Int32, true), + Field::new("string", DataType::Utf8, true), + ]); + for data_type in [ + DataType::Int64, + DataType::new_list(DataType::Int64, true), + DataType::Union(union_fields, UnionMode::Sparse), + ] { + run_test(new_empty_array(&data_type), vec![]); + run_test(new_null_array(&data_type, 2), vec![None, None]); + } } #[test]
