1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
use vortex_dtype::DType;
use vortex_error::{vortex_err, VortexResult};

use crate::{Array, ArrayDType};

pub trait CastFn {
    fn cast(&self, dtype: &DType) -> VortexResult<Array>;
}

/// Attempt to cast an array to a desired DType.
///
/// Some array support the ability to narrow or upcast.
pub fn try_cast(array: &Array, dtype: &DType) -> VortexResult<Array> {
    if array.dtype() == dtype {
        return Ok(array.clone());
    }

    // TODO(ngates): check for null_count if dtype is non-nullable
    array.with_dyn(|a| {
        a.cast()
            .map(|f| f.cast(dtype))
            .unwrap_or_else(|| Err(vortex_err!(NotImplemented: "cast", array.encoding().id())))
    })
}