Skip to content

Commit ecdefa8

Browse files
rename variant -> child array
1 parent cb9ef07 commit ecdefa8

File tree

2 files changed

+20
-20
lines changed

2 files changed

+20
-20
lines changed

arrow-cast/src/cast/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ pub fn can_cast_types(from_type: &DataType, to_type: &DataType) -> bool {
110110
can_cast_types(from_value_type, to_value_type)
111111
}
112112
(Dictionary(_, value_type), _) => can_cast_types(value_type, to_type),
113-
(Union(fields, _), _) => union::resolve_variant(fields, to_type).is_some(),
113+
(Union(fields, _), _) => union::resolve_child_array(fields, to_type).is_some(),
114114
(_, Union(_, _)) => false,
115115
(RunEndEncoded(_, value_type), _) => can_cast_types(value_type.data_type(), to_type),
116116
(_, RunEndEncoded(_, value_type)) => can_cast_types(from_type, value_type.data_type()),

arrow-cast/src/cast/union.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ use arrow_select::union_extract::union_extract;
2525

2626
use super::CastOptions;
2727

28-
// this is used during variant selection to prefer a "close" type over a distant cast
29-
// for example: when targeting Utf8View, a Utf8 variant is preferred over Int32 despite both being castable
28+
// this is used during child array selection to prefer a "close" type over a distant cast
29+
// for example: when targeting Utf8View, a Utf8 child is preferred over Int32 despite both being castable
3030
fn same_type_family(a: &DataType, b: &DataType) -> bool {
3131
use DataType::*;
3232
matches!(
@@ -61,7 +61,7 @@ fn same_type_family(a: &DataType, b: &DataType) -> bool {
6161
/// nulls, which can conflict with non-nullable inner fields
6262
///
6363
/// Each pass greedily picks the first matching field by type_id order
64-
pub(crate) fn resolve_variant<'a>(
64+
pub(crate) fn resolve_child_array<'a>(
6565
fields: &'a UnionFields,
6666
target_type: &DataType,
6767
) -> Option<&'a FieldRef> {
@@ -74,7 +74,7 @@ pub(crate) fn resolve_variant<'a>(
7474
.find(|(_, f)| same_type_family(f.data_type(), target_type))
7575
})
7676
.or_else(|| {
77-
// skip nested types in pass 3 union extraction introduces nulls,
77+
// skip nested types in pass 3 because union extraction introduces nulls,
7878
// and casting nullable arrays to nested types like List/Struct/Map can fail
7979
// when inner fields are non-nullable.
8080
if target_type.is_nested() {
@@ -90,8 +90,8 @@ pub(crate) fn resolve_variant<'a>(
9090
/// Extracts the best-matching child array from a [`UnionArray`] for a given target type,
9191
/// and casts it to that type.
9292
///
93-
/// Rows where a different variant is active become NULL.
94-
/// If no variant matches, returns a null array.
93+
/// Rows where a different child array is active become NULL.
94+
/// If no child array matches, returns an error.
9595
///
9696
/// # Example
9797
///
@@ -120,7 +120,7 @@ pub(crate) fn resolve_variant<'a>(
120120
/// )
121121
/// .unwrap();
122122
///
123-
/// // extract the Utf8 variant and cast to Utf8View
123+
/// // extract the Utf8 child array and cast to Utf8View
124124
/// let result = union_extract_by_type(&union, &DataType::Utf8View, &CastOptions::default()).unwrap();
125125
/// assert_eq!(result.data_type(), &DataType::Utf8View);
126126
/// assert!(result.is_null(0)); // Int32 row -> NULL
@@ -137,7 +137,7 @@ pub fn union_extract_by_type(
137137
_ => unreachable!("union_extract_by_type called on non-union array"),
138138
};
139139

140-
let Some(field) = resolve_variant(fields, target_type) else {
140+
let Some(field) = resolve_child_array(fields, target_type) else {
141141
return Err(ArrowError::CastError(format!(
142142
"cannot cast Union with fields {} to {}",
143143
fields
@@ -182,7 +182,7 @@ mod tests {
182182
}
183183

184184
// pass 1: exact type match.
185-
// Union(Int32, Utf8) targeting Utf8 — the Utf8 variant matches exactly.
185+
// Union(Int32, Utf8) targeting Utf8. The Utf8 child matches exactly.
186186
// Int32 rows become NULL. tested for both sparse and dense.
187187
#[test]
188188
fn test_exact_type_match() {
@@ -237,8 +237,8 @@ mod tests {
237237
}
238238

239239
// pass 2: same type family match.
240-
// Union(Int32, Utf8) targeting Utf8View — no exact match, but Utf8 and Utf8View
241-
// are in the same family. picks the Utf8 variant and casts to Utf8View.
240+
// Union(Int32, Utf8) targeting Utf8View. No exact match, but Utf8 and Utf8View
241+
// are in the same family. picks the Utf8 child array and casts to Utf8View.
242242
// this is the bug that motivated this work: without pass 2, pass 3 would
243243
// greedily pick Int32 (since can_cast_types(Int32, Utf8View) is true).
244244
#[test]
@@ -301,7 +301,7 @@ mod tests {
301301

302302
// pass 3: one-directional cast across type families.
303303
// Union(Int32, Utf8) targeting Boolean — no exact match, no family match.
304-
// pass 3 picks Int32 (first variant where can_cast_types is true) and
304+
// pass 3 picks Int32 (first child array where can_cast_types is true) and
305305
// casts to Boolean (0 → false, nonzero → true). Utf8 rows become NULL.
306306
#[test]
307307
fn test_one_directional_cast() {
@@ -355,7 +355,7 @@ mod tests {
355355
assert!(!arr.value(2));
356356
}
357357

358-
// no matching variant — all three passes fail.
358+
// no matching child array, all three passes fail.
359359
// Union(Int32, Utf8) targeting Struct({x: Int32}). neither Int32 nor Utf8
360360
// can be cast to a Struct, so both can_cast_types and cast return errors.
361361
#[test]
@@ -382,7 +382,7 @@ mod tests {
382382
}
383383

384384
// priority: exact match (pass 1) wins over family match (pass 2).
385-
// Union(Utf8, Utf8View) targeting Utf8View — both variants are in the string
385+
// Union(Utf8, Utf8View) targeting Utf8View. Both child arrays are in the string
386386
// family, but Utf8View is an exact match. pass 1 should pick it, not Utf8.
387387
#[test]
388388
fn test_exact_match_preferred_over_family() {
@@ -421,17 +421,17 @@ mod tests {
421421
assert_eq!(result.data_type(), &target);
422422
let arr = result.as_any().downcast_ref::<StringViewArray>().unwrap();
423423

424-
// pass 1 picks variant "b" (Utf8View), so variant "a" rows become NULL
424+
// pass 1 picks child "b" (Utf8View), so child "a" rows become NULL
425425
assert!(arr.is_null(0));
426426
assert_eq!(arr.value(1), "from_b");
427427
assert!(arr.is_null(2));
428428
}
429429

430-
// null values within the selected variant stay null.
431-
// this is distinct from "wrong variant → NULL": here the correct variant
430+
// null values within the selected child array stay null.
431+
// this is distinct from "wrong child array -> NULL": here the correct child array
432432
// is active but its value is null.
433433
#[test]
434-
fn test_null_in_selected_variant() {
434+
fn test_null_in_selected_child_array() {
435435
let target = DataType::Utf8;
436436

437437
assert!(can_cast_types(
@@ -440,7 +440,7 @@ mod tests {
440440
));
441441

442442
// ["hello", NULL(str), "world"]
443-
// all rows are the Utf8 variant, but row 1 has a null value
443+
// all rows are the Utf8 child array, but row 1 has a null value
444444
let union = UnionArray::try_new(
445445
int_str_fields(),
446446
vec![1_i8, 1, 1].into(),

0 commit comments

Comments
 (0)