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

vortex-data / vortex / 16339747936

17 Jul 2025 08:09AM UTC coverage: 80.702% (-0.002%) from 80.704%
16339747936

push

github

web-flow
Use ptr::default in Rust 1.88 (#3896)

Signed-off-by: Nicholas Gates <nick@nickgates.com>

0 of 14 new or added lines in 5 files covered. (0.0%)

2 existing lines in 1 file now uncovered.

41864 of 51875 relevant lines covered (80.7%)

157546.66 hits per line

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

75.0
/vortex-array/src/compute/invert.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, vortex_panic};
9

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

14
/// Logically invert a boolean array, preserving its validity.
15
pub fn invert(array: &dyn Array) -> VortexResult<ArrayRef> {
110✔
16
    INVERT_FN
110✔
17
        .invoke(&InvocationArgs {
110✔
18
            inputs: &[array.into()],
110✔
19
            options: &(),
110✔
20
        })?
110✔
21
        .unwrap_array()
110✔
22
}
110✔
23

24
struct Invert;
25

26
impl ComputeFnVTable for Invert {
27
    fn invoke(
110✔
28
        &self,
110✔
29
        args: &InvocationArgs,
110✔
30
        kernels: &[ArcRef<dyn Kernel>],
110✔
31
    ) -> VortexResult<Output> {
110✔
32
        let UnaryArgs { array, .. } = UnaryArgs::<()>::try_from(args)?;
110✔
33

34
        for kernel in kernels {
110✔
35
            if let Some(output) = kernel.invoke(args)? {
110✔
36
                return Ok(output);
110✔
UNCOV
37
            }
×
38
        }
39
        if let Some(output) = array.invoke(&INVERT_FN, args)? {
×
40
            return Ok(output);
×
41
        }
×
42

43
        // Otherwise, we canonicalize into a boolean array and invert.
44
        log::debug!(
×
45
            "No invert implementation found for encoding {}",
×
46
            array.encoding_id(),
×
47
        );
48
        if array.is_canonical() {
×
49
            vortex_panic!("Canonical bool array does not implement invert");
×
50
        }
×
51
        Ok(invert(&array.to_bool()?.into_array())?.into())
×
52
    }
110✔
53

54
    fn return_dtype(&self, args: &InvocationArgs) -> VortexResult<DType> {
110✔
55
        let UnaryArgs { array, .. } = UnaryArgs::<()>::try_from(args)?;
110✔
56

57
        if !matches!(array.dtype(), DType::Bool(..)) {
110✔
58
            vortex_bail!("Expected boolean array, got {}", array.dtype());
×
59
        }
110✔
60
        Ok(array.dtype().clone())
110✔
61
    }
110✔
62

63
    fn return_len(&self, args: &InvocationArgs) -> VortexResult<usize> {
110✔
64
        let UnaryArgs { array, .. } = UnaryArgs::<()>::try_from(args)?;
110✔
65
        Ok(array.len())
110✔
66
    }
110✔
67

68
    fn is_elementwise(&self) -> bool {
110✔
69
        true
110✔
70
    }
110✔
71
}
72

73
struct InvertArgs<'a> {
74
    array: &'a dyn Array,
75
}
76

77
impl<'a> TryFrom<&InvocationArgs<'a>> for InvertArgs<'a> {
78
    type Error = VortexError;
79

80
    fn try_from(value: &InvocationArgs<'a>) -> Result<Self, Self::Error> {
110✔
81
        if value.inputs.len() != 1 {
110✔
82
            vortex_bail!("Invert expects exactly one argument",);
×
83
        }
110✔
84
        let array = value.inputs[0]
110✔
85
            .array()
110✔
86
            .ok_or_else(|| vortex_err!("Invert expects an array argument"))?;
110✔
87
        Ok(InvertArgs { array })
110✔
88
    }
110✔
89
}
90

91
pub struct InvertKernelRef(ArcRef<dyn Kernel>);
92
inventory::collect!(InvertKernelRef);
93

94
pub trait InvertKernel: VTable {
95
    /// Logically invert a boolean array. Converts true -> false, false -> true, null -> null.
96
    fn invert(&self, array: &Self::Array) -> VortexResult<ArrayRef>;
97
}
98

99
#[derive(Debug)]
100
pub struct InvertKernelAdapter<V: VTable>(pub V);
101

102
impl<V: VTable + InvertKernel> InvertKernelAdapter<V> {
103
    pub const fn lift(&'static self) -> InvertKernelRef {
×
104
        InvertKernelRef(ArcRef::new_ref(self))
×
105
    }
×
106
}
107

108
impl<V: VTable + InvertKernel> Kernel for InvertKernelAdapter<V> {
109
    fn invoke(&self, args: &InvocationArgs) -> VortexResult<Option<Output>> {
110✔
110
        let args = InvertArgs::try_from(args)?;
110✔
111
        let Some(array) = args.array.as_opt::<V>() else {
110✔
UNCOV
112
            return Ok(None);
×
113
        };
114
        Ok(Some(V::invert(&self.0, array)?.into()))
110✔
115
    }
110✔
116
}
117

118
pub static INVERT_FN: LazyLock<ComputeFn> = LazyLock::new(|| {
109✔
119
    let compute = ComputeFn::new("invert".into(), ArcRef::new_ref(&Invert));
109✔
120
    for kernel in inventory::iter::<InvertKernelRef> {
508✔
121
        compute.register_kernel(kernel.0.clone());
399✔
122
    }
399✔
123
    compute
109✔
124
});
109✔
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

© 2025 Coveralls, Inc