• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

vortex-data / vortex / 16935267080

13 Aug 2025 11:00AM UTC coverage: 24.312% (-63.3%) from 87.658%
16935267080

Pull #4226

github

web-flow
Merge 81b48c7fb into baa6ea202
Pull Request #4226: Support converting TimestampTZ to and from duckdb

0 of 2 new or added lines in 1 file covered. (0.0%)

20666 existing lines in 469 files now uncovered.

8726 of 35892 relevant lines covered (24.31%)

147.74 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

72.73
/vortex-array/src/compute/cast.rs
1
// SPDX-License-Identifier: Apache-2.0
2
// SPDX-FileCopyrightText: Copyright the Vortex contributors
3

4
use std::sync::LazyLock;
5

6
use arcref::ArcRef;
7
use vortex_dtype::DType;
8
use vortex_error::{VortexError, VortexResult, vortex_bail, vortex_err};
9

10
use crate::compute::{ComputeFn, ComputeFnVTable, InvocationArgs, Kernel, Output};
11
use crate::vtable::VTable;
12
use crate::{Array, ArrayRef};
13

14
static CAST_FN: LazyLock<ComputeFn> = LazyLock::new(|| {
2✔
15
    let compute = ComputeFn::new("cast".into(), ArcRef::new_ref(&Cast));
2✔
16
    for kernel in inventory::iter::<CastKernelRef> {
56✔
17
        compute.register_kernel(kernel.0.clone());
54✔
18
    }
54✔
19
    compute
2✔
20
});
2✔
21

22
/// Attempt to cast an array to a desired DType.
23
///
24
/// Some array support the ability to narrow or upcast.
25
pub fn cast(array: &dyn Array, dtype: &DType) -> VortexResult<ArrayRef> {
4✔
26
    CAST_FN
4✔
27
        .invoke(&InvocationArgs {
4✔
28
            inputs: &[array.into(), dtype.into()],
4✔
29
            options: &(),
4✔
30
        })?
4✔
31
        .unwrap_array()
4✔
32
}
4✔
33

34
struct Cast;
35

36
impl ComputeFnVTable for Cast {
37
    fn invoke(
4✔
38
        &self,
4✔
39
        args: &InvocationArgs,
4✔
40
        kernels: &[ArcRef<dyn Kernel>],
4✔
41
    ) -> VortexResult<Output> {
4✔
42
        let CastArgs { array, dtype } = CastArgs::try_from(args)?;
4✔
43

44
        if array.dtype() == dtype {
4✔
UNCOV
45
            return Ok(array.to_array().into());
×
46
        }
4✔
47

48
        // TODO(ngates): check for null_count if dtype is non-nullable
49

50
        for kernel in kernels {
24✔
51
            if let Some(output) = kernel.invoke(args)? {
24✔
52
                return Ok(output);
4✔
53
            }
20✔
54
        }
UNCOV
55
        if let Some(output) = array.invoke(&CAST_FN, args)? {
×
56
            return Ok(output);
×
UNCOV
57
        }
×
58

59
        // Otherwise, we fall back to the canonical implementations.
UNCOV
60
        log::debug!(
×
61
            "Falling back to canonical cast for encoding {} and dtype {} to {}",
×
62
            array.encoding_id(),
×
63
            array.dtype(),
×
64
            dtype
65
        );
66

UNCOV
67
        if array.is_canonical() {
×
UNCOV
68
            vortex_bail!(
×
UNCOV
69
                "No compute kernel to cast array {} with dtype {} to {}",
×
UNCOV
70
                array.encoding_id(),
×
UNCOV
71
                array.dtype(),
×
72
                dtype
73
            );
UNCOV
74
        }
×
75

UNCOV
76
        Ok(cast(array.to_canonical()?.as_ref(), dtype)?.into())
×
77
    }
4✔
78

79
    fn return_dtype(&self, args: &InvocationArgs) -> VortexResult<DType> {
4✔
80
        let CastArgs { dtype, .. } = CastArgs::try_from(args)?;
4✔
81
        Ok(dtype.clone())
4✔
82
    }
4✔
83

84
    fn return_len(&self, args: &InvocationArgs) -> VortexResult<usize> {
4✔
85
        let CastArgs { array, .. } = CastArgs::try_from(args)?;
4✔
86
        Ok(array.len())
4✔
87
    }
4✔
88

89
    fn is_elementwise(&self) -> bool {
4✔
90
        true
4✔
91
    }
4✔
92
}
93

94
struct CastArgs<'a> {
95
    array: &'a dyn Array,
96
    dtype: &'a DType,
97
}
98

99
impl<'a> TryFrom<&InvocationArgs<'a>> for CastArgs<'a> {
100
    type Error = VortexError;
101

102
    fn try_from(args: &InvocationArgs<'a>) -> Result<Self, Self::Error> {
36✔
103
        if args.inputs.len() != 2 {
36✔
104
            vortex_bail!(
×
105
                "Cast function requires 2 arguments, but got {}",
×
106
                args.inputs.len()
×
107
            );
108
        }
36✔
109
        let array = args.inputs[0]
36✔
110
            .array()
36✔
111
            .ok_or_else(|| vortex_err!("Missing array argument"))?;
36✔
112
        let dtype = args.inputs[1]
36✔
113
            .dtype()
36✔
114
            .ok_or_else(|| vortex_err!("Missing dtype argument"))?;
36✔
115

116
        Ok(CastArgs { array, dtype })
36✔
117
    }
36✔
118
}
119

120
pub struct CastKernelRef(ArcRef<dyn Kernel>);
121
inventory::collect!(CastKernelRef);
122

123
pub trait CastKernel: VTable {
124
    fn cast(&self, array: &Self::Array, dtype: &DType) -> VortexResult<Option<ArrayRef>>;
125
}
126

127
#[derive(Debug)]
128
pub struct CastKernelAdapter<V: VTable>(pub V);
129

130
impl<V: VTable + CastKernel> CastKernelAdapter<V> {
131
    pub const fn lift(&'static self) -> CastKernelRef {
×
132
        CastKernelRef(ArcRef::new_ref(self))
×
133
    }
×
134
}
135

136
impl<V: VTable + CastKernel> Kernel for CastKernelAdapter<V> {
137
    fn invoke(&self, args: &InvocationArgs) -> VortexResult<Option<Output>> {
24✔
138
        let CastArgs { array, dtype } = CastArgs::try_from(args)?;
24✔
139
        let Some(array) = array.as_opt::<V>() else {
24✔
140
            return Ok(None);
20✔
141
        };
142

143
        Ok(V::cast(&self.0, array, dtype)?.map(|o| o.into()))
4✔
144
    }
24✔
145
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc