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

vortex-data / vortex / 16268681402

14 Jul 2025 01:49PM UTC coverage: 81.464% (+0.2%) from 81.235%
16268681402

Pull #3856

github

web-flow
Merge 3f50333da into 52555adbb
Pull Request #3856: fix: Drain prefetch buffer

15 of 27 new or added lines in 2 files covered. (55.56%)

115 existing lines in 13 files now uncovered.

46110 of 56602 relevant lines covered (81.46%)

146503.08 hits per line

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

86.75
/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
/// Attempt to cast an array to a desired DType.
15
///
16
/// Some array support the ability to narrow or upcast.
17
pub fn cast(array: &dyn Array, dtype: &DType) -> VortexResult<ArrayRef> {
19,232✔
18
    CAST_FN
19,232✔
19
        .invoke(&InvocationArgs {
19,232✔
20
            inputs: &[array.into(), dtype.into()],
19,232✔
21
            options: &(),
19,232✔
22
        })?
19,232✔
23
        .unwrap_array()
17,779✔
24
}
19,232✔
25

26
pub static CAST_FN: LazyLock<ComputeFn> = LazyLock::new(|| {
5,069✔
27
    let compute = ComputeFn::new("cast".into(), ArcRef::new_ref(&Cast));
5,069✔
28
    for kernel in inventory::iter::<CastKernelRef> {
53,248✔
29
        compute.register_kernel(kernel.0.clone());
48,179✔
30
    }
48,179✔
31
    compute
5,069✔
32
});
5,069✔
33

34
struct Cast;
35

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

44
        if array.dtype() == dtype {
19,232✔
45
            return Ok(array.to_array().into());
9,589✔
46
        }
9,643✔
47

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

50
        for kernel in kernels {
69,868✔
51
            if let Some(output) = kernel.invoke(args)? {
68,914✔
52
                return Ok(output);
7,958✔
53
            }
60,225✔
54
        }
55
        if let Some(output) = array.invoke(&CAST_FN, args)? {
954✔
56
            return Ok(output);
×
57
        }
954✔
58

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

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

880✔
76
        Ok(cast(array.to_canonical()?.as_ref(), dtype)?.into())
880✔
77
    }
19,232✔
78

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

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

89
    fn is_elementwise(&self) -> bool {
19,232✔
90
        true
19,232✔
91
    }
19,232✔
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> {
126,610✔
103
        if args.inputs.len() != 2 {
126,610✔
104
            vortex_bail!(
×
105
                "Cast function requires 2 arguments, but got {}",
×
UNCOV
106
                args.inputs.len()
×
UNCOV
107
            );
×
108
        }
126,610✔
109
        let array = args.inputs[0]
126,610✔
110
            .array()
126,610✔
111
            .ok_or_else(|| vortex_err!("Missing array argument"))?;
126,610✔
112
        let dtype = args.inputs[1]
126,610✔
113
            .dtype()
126,610✔
114
            .ok_or_else(|| vortex_err!("Missing dtype argument"))?;
126,610✔
115

116
        Ok(CastArgs { array, dtype })
126,610✔
117
    }
126,610✔
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 {
×
UNCOV
132
        CastKernelRef(ArcRef::new_ref(self))
×
UNCOV
133
    }
×
134
}
135

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

143
        Ok(V::cast(&self.0, array, dtype)?.map(|o| o.into()))
9,201✔
144
    }
68,074✔
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