lance_arrow/
cast.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright The Lance Authors
3
4use std::sync::Arc;
5
6use arrow_array::{Array, ArrayRef, FixedSizeListArray};
7use arrow_cast::CastOptions;
8use arrow_schema::{ArrowError, DataType};
9
10/// Customized [`arrow_cast::cast_with_options`] that handles cases not supported upstream yet.
11pub fn cast_with_options(
12    array: &dyn Array,
13    to_type: &DataType,
14    cast_options: &CastOptions,
15) -> Result<ArrayRef, ArrowError> {
16    use DataType::*;
17    match (array.data_type(), to_type) {
18        (FixedSizeList(_, size_from), FixedSizeList(to_field, size_to)) if size_from == size_to => {
19            let array = array.as_any().downcast_ref::<FixedSizeListArray>().unwrap();
20            let values = cast_with_options(array.values(), to_field.data_type(), cast_options)?;
21            Ok(Arc::new(FixedSizeListArray::try_new(
22                to_field.clone(),
23                *size_from,
24                values,
25                array.nulls().cloned(),
26            )?))
27        }
28        _ => arrow_cast::cast_with_options(array, to_type, cast_options),
29    }
30}