Skip to content

fix(arrow-cast): support unsafe casting with masked null values - #10726

Open
dhruvxvaishnav wants to merge 1 commit into
apache:mainfrom
dhruvxvaishnav:fix/cast-unsafe-masked-nulls
Open

fix(arrow-cast): support unsafe casting with masked null values#10726
dhruvxvaishnav wants to merge 1 commit into
apache:mainfrom
dhruvxvaishnav:fix/cast-unsafe-masked-nulls

Conversation

@dhruvxvaishnav

@dhruvxvaishnav dhruvxvaishnav commented Aug 18, 2026

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

When casting with safe: false, errors were raised for invalid values even if those values were masked by nulls in the array's null bitmap or child offsets (e.g. invalid UTF-8 in a null binary slot, invalid castable child items in a null fixed-size list, list, list view, struct, or map). Because masked values are logically null, the cast should succeed and preserve the null mask.

What changes are included in this PR?

  • arrow-cast/src/cast/string.rs: In cast_binary_to_string and cast_binary_view_to_string_view, if fast-path UTF-8 conversion fails with safe: false and the array contains nulls, only validate non-null values for valid UTF-8.
  • arrow-cast/src/cast/list.rs: Propagate parent list nulls to child arrays before casting in cast_single_element_fixed_size_list_to_values, cast_list_values, cast_list_view_values, cast_list, cast_list_view, and cast_list_to_list_view when the target field is nullable.
  • arrow-cast/src/cast/mod.rs: Propagate parent nulls for FixedSizeList and StructArray columns before calling cast_with_options when the target field is nullable.
  • arrow-cast/src/cast/map.rs: Propagate parent nulls to map key and value child arrays when target fields are nullable.
  • Added comprehensive unit tests in arrow-cast covering all container types and binary-to-string casts with masked null values.

Are these changes tested?

Yes, new unit tests added in arrow-cast/src/cast/mod.rs:

  • test_binary_to_string_unsafe_masked_nulls
  • test_binary_view_to_string_view_unsafe_masked_nulls
  • test_fixed_size_list_unsafe_masked_nulls
  • test_list_unsafe_masked_nulls
  • test_list_view_unsafe_masked_nulls
  • test_struct_unsafe_masked_nulls
  • test_map_unsafe_masked_nulls

All tests and lints run and pass cleanly:

  • cargo test -p arrow-cast
  • cargo +stable fmt --all -- --check
  • cargo clippy -p arrow-cast --all-targets --all-features -- -D warnings
  • git diff --check

Are there any user-facing changes?

No breaking changes to public APIs. safe: false casting now correctly succeeds on arrays where unconvertible values are masked by nulls instead of erroneously failing.

Do not fail safe: false casts on unconvertible values that are masked as nulls in binary, string, list, struct, and map arrays.

Fixes apache#10711
@Jefffrey

Copy link
Copy Markdown
Contributor

run benchmark cast_kernels

@Jefffrey Jefffrey added the bug label Aug 19, 2026
@adriangbot

This comment was marked as duplicate.

return Arc::clone(array.values());
}

let mut builder = BooleanBufferBuilder::new(array.values().len());

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.

i think we can use NullBuffer::expand here

@adriangbot

Copy link
Copy Markdown

🤖 Arrow criterion benchmark completed (GKE) | trigger

Instance: c4a-highmem-16 (12 vCPU / 65 GiB)

Comparing fix/cast-unsafe-masked-nulls (df27155) to bb1e6cd (merge-base) diff

Run configuration
run benchmark cast_kernels
CPU Details (lscpu)
Architecture:                            aarch64
CPU op-mode(s):                          64-bit
Byte Order:                              Little Endian
CPU(s):                                  16
On-line CPU(s) list:                     0-15
Vendor ID:                               ARM
Model name:                              Neoverse-V2
Model:                                   1
Thread(s) per core:                      1
Core(s) per cluster:                     16
Socket(s):                               -
Cluster(s):                              1
Stepping:                                r0p1
BogoMIPS:                                2000.00
Flags:                                   fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm jscvt fcma lrcpc dcpop sha3 sm3 sm4 asimddp sha512 sve asimdfhm dit uscat ilrcpc flagm sb paca pacg dcpodp sve2 sveaes svepmull svebitperm svesha3 svesm4 flagm2 frint svei8mm svebf16 i8mm bf16 dgh rng bti
L1d cache:                               1 MiB (16 instances)
L1i cache:                               1 MiB (16 instances)
L2 cache:                                32 MiB (16 instances)
L3 cache:                                80 MiB (1 instance)
NUMA node(s):                            1
NUMA node0 CPU(s):                       0-15
Vulnerability Gather data sampling:      Not affected
Vulnerability Indirect target selection: Not affected
Vulnerability Itlb multihit:             Not affected
Vulnerability L1tf:                      Not affected
Vulnerability Mds:                       Not affected
Vulnerability Meltdown:                  Not affected
Vulnerability Mmio stale data:           Not affected
Vulnerability Reg file data sampling:    Not affected
Vulnerability Retbleed:                  Not affected
Vulnerability Spec rstack overflow:      Not affected
Vulnerability Spec store bypass:         Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1:                Mitigation; __user pointer sanitization
Vulnerability Spectre v2:                Mitigation; CSV2, BHB
Vulnerability Srbds:                     Not affected
Vulnerability Tsa:                       Not affected
Vulnerability Tsx async abort:           Not affected
Vulnerability Vmscape:                   Not affected
Details

group                                                              fix_cast-unsafe-masked-nulls           main
-----                                                              ----------------------------           ----
"cast decimal128 to float64"                                       1.00     27.1±0.01µs        ? ?/sec    1.00     27.1±0.02µs        ? ?/sec
"cast decimal128 to int64"                                         1.00     48.3±0.76µs        ? ?/sec    1.00     48.3±0.88µs        ? ?/sec
"cast decimal128 to int8"                                          1.00     60.5±0.63µs        ? ?/sec    1.01     60.8±0.73µs        ? ?/sec
"cast decimal256 to float64"                                       1.00     68.6±0.08µs        ? ?/sec    1.00     68.6±0.10µs        ? ?/sec
"cast decimal256 to int64"                                         1.00    153.0±0.34µs        ? ?/sec    1.02    155.4±0.82µs        ? ?/sec
"cast float64 to decimal128(32, 3)"                                1.00     35.4±0.28µs        ? ?/sec    1.00     35.3±0.28µs        ? ?/sec
"cast invalid float64 to to decimal128(32, 3)"                     1.01     23.7±1.15µs        ? ?/sec    1.00     23.4±1.13µs        ? ?/sec
"cast string to decimal128(38, 3)"                                 1.00    110.8±0.63µs        ? ?/sec    1.01    111.7±0.48µs        ? ?/sec
"cast string to decimal256(76, 3)"                                 1.00    148.5±0.35µs        ? ?/sec    1.00    148.5±0.37µs        ? ?/sec
cast binary dict to string view (sparse)                           1.00     46.2±0.10µs        ? ?/sec    1.08     49.9±1.38µs        ? ?/sec
cast binary view to string                                         1.00     70.6±1.04µs        ? ?/sec    1.01     71.6±0.76µs        ? ?/sec
cast binary view to string view                                    1.00     63.5±1.39µs        ? ?/sec    1.00     63.8±1.80µs        ? ?/sec
cast binary view to wide string                                    1.00     69.3±0.83µs        ? ?/sec    1.00     69.6±0.92µs        ? ?/sec
cast date32 to date64 512                                          1.01    336.8±0.46ns        ? ?/sec    1.00    332.4±0.84ns        ? ?/sec
cast date64 to date32 512                                          1.03   1496.8±5.06ns        ? ?/sec    1.00   1446.4±4.18ns        ? ?/sec
cast decimal128 to decimal128 512                                  1.00      6.9±0.00µs        ? ?/sec    1.00      6.9±0.00µs        ? ?/sec
cast decimal128 to decimal128 512 lower precision                  1.00     19.9±0.08µs        ? ?/sec    1.01     20.0±0.02µs        ? ?/sec
cast decimal128 to decimal128 512 with lower scale (infallible)    1.00     45.8±0.06µs        ? ?/sec    1.00     45.8±0.04µs        ? ?/sec
cast decimal128 to decimal128 512 with same scale                  1.00     75.0±0.25ns        ? ?/sec    1.00     74.7±0.27ns        ? ?/sec
cast decimal128 to decimal256 512                                  1.00     26.2±0.01µs        ? ?/sec    1.00     26.2±0.02µs        ? ?/sec
cast decimal256 to decimal128 512                                  1.00    317.9±0.24µs        ? ?/sec    1.00    317.8±0.91µs        ? ?/sec
cast decimal256 to decimal256 512                                  1.00     81.9±0.05µs        ? ?/sec    1.00     81.9±0.06µs        ? ?/sec
cast decimal256 to decimal256 512 with same scale                  1.01     76.6±2.12ns        ? ?/sec    1.00     76.0±1.11ns        ? ?/sec
cast dict to string view                                           1.02     15.3±0.20µs        ? ?/sec    1.00     15.0±0.14µs        ? ?/sec
cast dict to string view (sparse)                                  1.00      5.2±0.06µs        ? ?/sec    1.05      5.5±0.08µs        ? ?/sec
cast f32 to string 512                                             1.00     11.9±0.04µs        ? ?/sec    1.01     12.1±0.05µs        ? ?/sec
cast f64 to string 512                                             1.00     15.4±0.03µs        ? ?/sec    1.02     15.6±0.05µs        ? ?/sec
cast float32 to int32 512                                          1.02  1390.4±13.51ns        ? ?/sec    1.00   1357.1±5.61ns        ? ?/sec
cast float64 to float32 512                                        1.06    718.6±1.30ns        ? ?/sec    1.00    680.6±4.27ns        ? ?/sec
cast float64 to uint64 512                                         1.05  1496.5±56.36ns        ? ?/sec    1.00   1421.0±2.97ns        ? ?/sec
cast i64 to string 512                                             1.00      8.8±0.03µs        ? ?/sec    1.00      8.7±0.05µs        ? ?/sec
cast int32 to float32 512                                          1.06    746.6±8.86ns        ? ?/sec    1.00    704.9±3.85ns        ? ?/sec
cast int32 to float64 512                                          1.07    758.8±3.22ns        ? ?/sec    1.00    707.1±2.16ns        ? ?/sec
cast int32 to int32 512                                            1.00    173.8±2.09ns        ? ?/sec    1.02    176.5±3.20ns        ? ?/sec
cast int32 to int64 512                                            1.05    704.4±4.06ns        ? ?/sec    1.00    670.2±3.16ns        ? ?/sec
cast int32 to uint32 512                                           1.00   1394.0±0.97ns        ? ?/sec    1.00   1387.9±1.53ns        ? ?/sec
cast int64 to int32 512                                            1.04   1528.0±2.58ns        ? ?/sec    1.00   1468.8±1.41ns        ? ?/sec
cast nested dict to dict                                           1.00      4.5±0.03µs        ? ?/sec    1.02      4.6±0.02µs        ? ?/sec
cast no runs of int32s to ree<int32>                               1.01     58.7±1.84µs        ? ?/sec    1.00     58.0±0.92µs        ? ?/sec
cast runs of 10 string to ree<int32>                               1.00      8.8±0.08µs        ? ?/sec    1.00      8.8±0.02µs        ? ?/sec
cast runs of 1000 int32s to ree<int32>                             1.00      3.4±0.02µs        ? ?/sec    1.01      3.4±0.01µs        ? ?/sec
cast string single run to ree<int32>                               1.00     27.4±0.04µs        ? ?/sec    1.00     27.4±0.02µs        ? ?/sec
cast string to binary view 512                                     1.04      2.3±0.01µs        ? ?/sec    1.00      2.2±0.03µs        ? ?/sec
cast string view to binary view                                    1.00     83.8±0.90ns        ? ?/sec    1.00     83.7±1.26ns        ? ?/sec
cast string view to dict                                           1.01    155.6±0.42µs        ? ?/sec    1.00    154.8±0.43µs        ? ?/sec
cast string view to string                                         1.00     43.2±0.92µs        ? ?/sec    1.00     43.4±0.77µs        ? ?/sec
cast string view to wide string                                    1.00     43.0±0.79µs        ? ?/sec    1.01     43.2±0.88µs        ? ?/sec
cast time32s to time32ms 512                                       1.02   1485.0±3.10ns        ? ?/sec    1.00   1461.3±2.94ns        ? ?/sec
cast time32s to time64us 512                                       1.01    336.6±0.48ns        ? ?/sec    1.00    331.7±0.67ns        ? ?/sec
cast time64ns to time32s 512                                       1.02    421.1±0.27ns        ? ?/sec    1.00    412.1±0.37ns        ? ?/sec
cast timestamp_ms to i64 512                                       1.00    246.7±0.69ns        ? ?/sec    1.00    246.9±2.20ns        ? ?/sec
cast timestamp_ms to timestamp_ns 512                              1.00   1867.7±3.13ns        ? ?/sec    1.00   1862.2±2.56ns        ? ?/sec
cast timestamp_ns to timestamp_s 512                               1.00    170.6±1.34ns        ? ?/sec    1.01    173.1±0.87ns        ? ?/sec
cast utf8 to date32 512                                            1.01      7.0±0.05µs        ? ?/sec    1.00      6.9±0.03µs        ? ?/sec
cast utf8 to date64 512                                            1.01     32.2±0.11µs        ? ?/sec    1.00     32.0±0.12µs        ? ?/sec
cast utf8 to f32                                                   1.00      5.6±0.03µs        ? ?/sec    1.00      5.6±0.02µs        ? ?/sec
cast utf8 to i32                                                   1.00      5.3±0.05µs        ? ?/sec    1.00      5.3±0.04µs        ? ?/sec
cast wide string to binary view 512                                1.00      3.9±0.06µs        ? ?/sec    1.01      4.0±0.07µs        ? ?/sec

Resource Usage

base (merge-base)

Metric Value
Wall time 575.1s
Peak memory 15.5 MiB
Avg memory 10.2 MiB
CPU user 572.4s
CPU sys 0.1s
Peak spill 0 B

branch

Metric Value
Wall time 565.1s
Peak memory 16.8 MiB
Avg memory 14.3 MiB
CPU user 562.6s
CPU sys 0.1s
Peak spill 0 B

File an issue against this benchmark runner

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

safe: false casting doesn't account for masked null values

3 participants