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

Function general_array_element

datafusion/functions-nested/src/extract.rs:213–278  ·  view source on GitHub ↗
(
    array: &GenericListArray<O>,
    indexes: &Int64Array,
)

Source from the content-addressed store, hash-verified

211}
212
213fn general_array_element<O: OffsetSizeTrait>(
214 array: &GenericListArray<O>,
215 indexes: &Int64Array,
216) -> Result<ArrayRef>
217where
218 i64: TryInto<O>,
219{
220 let values = array.values();
221 if values.data_type().is_null() {
222 return Ok(Arc::new(NullArray::new(array.len())));
223 }
224
225 let original_data = values.to_data();
226 let capacity = Capacities::Array(original_data.len());
227
228 // use_nulls: true, we don't construct List for array_element, so we need explicit nulls.
229 let mut mutable =
230 MutableArrayData::with_capacities(vec![&original_data], true, capacity);
231
232 fn adjusted_array_index<O: OffsetSizeTrait>(index: i64, len: O) -> Result<Option<O>>
233 where
234 i64: TryInto<O>,
235 {
236 let index: O = index.try_into().map_err(|_| {
237 exec_datafusion_err!("array_element got invalid index: {index}")
238 })?;
239 // 0 ~ len - 1
240 let adjusted_zero_index = if index < O::usize_as(0) {
241 index + len
242 } else {
243 index - O::usize_as(1)
244 };
245
246 if O::usize_as(0) <= adjusted_zero_index && adjusted_zero_index < len {
247 Ok(Some(adjusted_zero_index))
248 } else {
249 // Out of bounds
250 Ok(None)
251 }
252 }
253
254 for (row_index, offset_window) in array.offsets().windows(2).enumerate() {
255 let start = offset_window[0];
256 let end = offset_window[1];
257 let len = end - start;
258
259 // array is null
260 if array.is_null(row_index) {
261 mutable.extend_nulls(1);
262 continue;
263 }
264
265 let index = adjusted_array_index::<O>(indexes.value(row_index), len)?;
266
267 if let Some(index) = index {
268 let start = start.as_usize() + index.as_usize();
269 mutable.extend(0, start, start + 1_usize);
270 } else {

Callers 1

Calls 10

newFunction · 0.85
make_arrayFunction · 0.85
offsetsMethod · 0.80
valuesMethod · 0.45
is_nullMethod · 0.45
data_typeMethod · 0.45
lenMethod · 0.45
valueMethod · 0.45
as_usizeMethod · 0.45
extendMethod · 0.45

Tested by 1

Used in the wild real call sites across dependent graphs

searching dependent graphs…