MCPcopy Create free account
hub / github.com/apache/datafusion / truncate_list_nulls

Function truncate_list_nulls

datafusion/common/src/utils/mod.rs:1133–1188  ·  view source on GitHub ↗

Create a new list array where all the nulls point to empty lists

(
    list: &GenericListArray<O>,
)

Source from the content-addressed store, hash-verified

1131
1132/// Create a new list array where all the nulls point to empty lists
1133fn truncate_list_nulls<O: OffsetSizeTrait>(
1134 list: &GenericListArray<O>,
1135) -> Result<GenericListArray<O>> {
1136 if let Some(nulls) = list.nulls()
1137 && nulls.null_count() > 0
1138 {
1139 let lengths = length(list)?;
1140 let zero: &dyn Datum = if lengths.data_type() == &DataType::Int32 {
1141 &Int32Array::new_scalar(0)
1142 } else {
1143 &Int64Array::new_scalar(0)
1144 };
1145
1146 let (mut valid_or_empty, _nulls) = eq(&lengths, zero)?.into_parts();
1147 valid_or_empty |= nulls.inner();
1148 let valid_or_empty = BooleanArray::from(valid_or_empty);
1149
1150 if valid_or_empty.has_false() {
1151 let array_data = list.values().to_data();
1152 let offsets = list.offsets();
1153 let capacity = offsets[offsets.len() - 1] - offsets[0];
1154 let mut mutable_array_data =
1155 MutableArrayData::new(vec![&array_data], false, capacity.as_usize());
1156
1157 let (valid_or_empty, _nulls) = valid_or_empty.into_parts();
1158
1159 for (start, end) in valid_or_empty.set_slices() {
1160 mutable_array_data.extend(
1161 0,
1162 offsets[start].as_usize(),
1163 offsets[end].as_usize(),
1164 );
1165 }
1166
1167 let lengths = std::iter::zip(offsets.lengths(), nulls)
1168 .map(|(length, is_valid)| if is_valid { length } else { 0 });
1169
1170 let offsets = OffsetBuffer::from_lengths(lengths);
1171 let values = make_array(mutable_array_data.freeze());
1172
1173 let field = match list.data_type() {
1174 DataType::List(field) => field,
1175 DataType::LargeList(field) => field,
1176 _ => unreachable!(),
1177 };
1178
1179 return Ok(GenericListArray::try_new(
1180 Arc::clone(field),
1181 offsets,
1182 values,
1183 list.nulls().cloned(),
1184 )?);
1185 }
1186 }
1187 Ok(list.clone())
1188}
1189
1190/// If `array` is a list or a map, returns a new array of the same length as it's inner values

Callers 1

remove_list_null_valuesFunction · 0.85

Calls 15

lengthFunction · 0.85
eqFunction · 0.85
newFunction · 0.85
make_arrayFunction · 0.85
null_countMethod · 0.80
into_partsMethod · 0.80
offsetsMethod · 0.80
nullsMethod · 0.45
data_typeMethod · 0.45
innerMethod · 0.45
valuesMethod · 0.45
lenMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…