Skip to content

[Variant] Safe cast_to_variant failures produce Variant null instead of Arrow null #11939

Open

Description

@sdf-jkl

Describe the bug

cast_to_variant_with_options with CastOptions { safe: true, .. } encodes conversion failures as Variant::Null while leaving the output row valid. Arrow's cast kernel marks failed conversions null in the output array, as documented by CastOptions::safe.

Found while implementing Spark-compatible SQL TRY_CAST(... AS VARIANT) in datafusion-variant. Spark documents TRY_CAST as returning SQL NULL on runtime conversion errors, including overflow. Downstream code must currently convert these failed results to SQL NULL explicitly.

To Reproduce

Reproduced with arrow = "60.0.0" and parquet-variant-compute = "60.0.0":

use arrow::array::{Array, TimestampSecondArray};
use arrow::compute::{CastOptions, cast_with_options};
use arrow::datatypes::{DataType, TimeUnit};
use parquet_variant_compute::cast_to_variant_with_options;

fn main() {
    let input = TimestampSecondArray::from(vec![Some(i64::MAX), Some(0), None]);
    let options = CastOptions { safe: true, ..Default::default() };

    let arrow = cast_with_options(
        &input,
        &DataType::Timestamp(TimeUnit::Microsecond, None),
        &options,
    ).unwrap();
    let variant = cast_to_variant_with_options(&input, &options).unwrap();

    println!("Arrow overflow is null: {}", arrow.is_null(0));
    println!("Variant overflow is null: {}", variant.is_null(0));
    println!("Variant overflow value: {:?}", variant.value(0));
    assert!(!variant.is_null(1));
    assert!(variant.is_null(2));
    assert!(arrow.is_null(0));
    assert!(variant.is_null(0)); // Fails: overflow is encoded as Variant::Null.
}

Output before the assertion failure:

Arrow overflow is null: true
Variant overflow is null: false
Variant overflow value: Null

Expected behavior

For a failed top-level conversion with safe: true, mark the output row null, matching Arrow's cast kernel. Valid conversions and input nulls should retain their current behavior; safe: false should continue to return an error.

Additional context

Source inspection of main at 5746b443f7 confirms the same behavior: the failure branch appends Variant::Null, and the timestamp overflow test expects it.

The original discussion in #8059 proposed setting the array element null on overflow, following Arrow's cast model. A later discussion in #8233 suggested Variant::Null instead of None. #8984 subsequently unified the options with Arrow's CastOptions while preserving behavior.

Activity

  1. added
    bug
    on Oct 27, 2026
  2. rokadepiyush49-rgb commented on Nov 23, 2026

    @rokadepiyush49-rgb

    With safe: true, failed conversions get written as Variant::Null, so the row stays valid. Arrow's own cast kernels mark them as real nulls instead:

    flowchart TD
      A[cast_to_variant, safe = true] --> B{value converts?}
      B -- yes --> C[Variant value]
      B -- no, today --> D[Variant::Null, row valid]
      B -. no, fix .-> E[Arrow null, validity unset]
    
    Loading

    The fix looks like appending a null on the builder in the safe-failure branches of arrow_to_variant.rs, keeping safe: false as an error and real JSON nulls as Variant::Null. Should this be called out as a breaking change in the changelog?

  3. Mola-maker commented on Nov 26, 2026

    @Mola-maker
    Contributor

    Hi @rokadepiyush49-rgb, thanks for the detailed analysis and proposal. Are you planning to open a PR for this?

    If not, I'd be happy to pick it up, since I've recently been working in arrow_to_variant.rs (#12211). I'll wait for your reply before starting so we don't duplicate work.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions