Skip to content

Commit 57d1309

Browse files
authored
Fix ScalarValue::to_array_of_size for DenseUnion (#13797)
1 parent e665115 commit 57d1309

File tree

1 file changed

+12
-6
lines changed
  • datafusion/common/src/scalar

1 file changed

+12
-6
lines changed

datafusion/common/src/scalar/mod.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2444,7 +2444,7 @@ impl ScalarValue {
24442444
e,
24452445
size
24462446
),
2447-
ScalarValue::Union(value, fields, _mode) => match value {
2447+
ScalarValue::Union(value, fields, mode) => match value {
24482448
Some((v_id, value)) => {
24492449
let mut new_fields = Vec::with_capacity(fields.len());
24502450
let mut child_arrays = Vec::<ArrayRef>::with_capacity(fields.len());
@@ -2453,15 +2453,23 @@ impl ScalarValue {
24532453
value.to_array_of_size(size)?
24542454
} else {
24552455
let dt = field.data_type();
2456-
new_null_array(dt, size)
2456+
match mode {
2457+
UnionMode::Sparse => new_null_array(dt, size),
2458+
// In a dense union, only the child with values needs to be
2459+
// allocated
2460+
UnionMode::Dense => new_null_array(dt, 0),
2461+
}
24572462
};
24582463
let field = (**field).clone();
24592464
child_arrays.push(ar);
24602465
new_fields.push(field.clone());
24612466
}
24622467
let type_ids = repeat(*v_id).take(size);
24632468
let type_ids = ScalarBuffer::<i8>::from_iter(type_ids);
2464-
let value_offsets: Option<ScalarBuffer<i32>> = None;
2469+
let value_offsets = match mode {
2470+
UnionMode::Sparse => None,
2471+
UnionMode::Dense => Some(ScalarBuffer::from_iter(0..size as i32)),
2472+
};
24652473
let ar = UnionArray::try_new(
24662474
fields.clone(),
24672475
type_ids,
@@ -5642,14 +5650,12 @@ mod tests {
56425650
// null array
56435651
Arc::new(NullArray::new(3)),
56445652
// dense union
5645-
/* Dense union fails due to https://github.com/apache/datafusion/issues/13762
56465653
{
56475654
let mut builder = UnionBuilder::new_dense();
56485655
builder.append::<Int32Type>("a", 1).unwrap();
56495656
builder.append::<Float64Type>("b", 3.4).unwrap();
56505657
Arc::new(builder.build().unwrap())
5651-
}
5652-
*/
5658+
},
56535659
// sparse union
56545660
{
56555661
let mut builder = UnionBuilder::new_sparse();

0 commit comments

Comments
 (0)