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

vortex-data / vortex / 16594026488

29 Jul 2025 10:52AM UTC coverage: 82.149% (-0.1%) from 82.255%
16594026488

push

github

web-flow
fix: Forbid SequenceScheme for String and Float dictionary codes as well as Integer (#4048)

Signed-off-by: Robert Kruszewski <github@robertk.io>

2 of 2 new or added lines in 2 files covered. (100.0%)

57 existing lines in 10 files now uncovered.

44285 of 53908 relevant lines covered (82.15%)

168409.11 hits per line

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

76.07
/vortex-array/src/compute/take.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
use vortex_scalar::Scalar;
10

11
use crate::arrays::ConstantArray;
12
use crate::compute::{ComputeFn, ComputeFnVTable, InvocationArgs, Kernel, Output};
13
use crate::stats::{Precision, Stat, StatsProviderExt, StatsSet};
14
use crate::vtable::VTable;
15
use crate::{Array, ArrayRef, Canonical, IntoArray};
16

17
static TAKE_FN: LazyLock<ComputeFn> = LazyLock::new(|| {
1,624✔
18
    let compute = ComputeFn::new("take".into(), ArcRef::new_ref(&Take));
1,624✔
19
    for kernel in inventory::iter::<TakeKernelRef> {
24,070✔
20
        compute.register_kernel(kernel.0.clone());
22,446✔
21
    }
22,446✔
22
    compute
1,624✔
23
});
1,624✔
24

25
pub fn take(array: &dyn Array, indices: &dyn Array) -> VortexResult<ArrayRef> {
4,597✔
26
    if indices.is_empty() {
4,597✔
27
        return Ok(Canonical::empty(
2✔
28
            &array
2✔
29
                .dtype()
2✔
30
                .union_nullability(indices.dtype().nullability()),
2✔
31
        )
2✔
32
        .into_array());
2✔
33
    }
4,595✔
34

35
    TAKE_FN
4,595✔
36
        .invoke(&InvocationArgs {
4,595✔
37
            inputs: &[array.into(), indices.into()],
4,595✔
38
            options: &(),
4,595✔
39
        })?
4,595✔
40
        .unwrap_array()
4,557✔
41
}
4,597✔
42

43
pub struct Take;
44

45
impl ComputeFnVTable for Take {
46
    fn invoke(
4,595✔
47
        &self,
4,595✔
48
        args: &InvocationArgs,
4,595✔
49
        kernels: &[ArcRef<dyn Kernel>],
4,595✔
50
    ) -> VortexResult<Output> {
4,595✔
51
        let TakeArgs { array, indices } = TakeArgs::try_from(args)?;
4,595✔
52

53
        // TODO(ngates): if indices are sorted and unique (strict-sorted), then we should delegate to
54
        //  the filter function since they're typically optimised for this case.
55
        // TODO(ngates): if indices min is quite high, we could slice self and offset the indices
56
        //  such that canonicalize does less work.
57

58
        if indices.all_invalid()? {
4,595✔
59
            return Ok(ConstantArray::new(
41✔
60
                Scalar::null(array.dtype().as_nullable()),
41✔
61
                indices.len(),
41✔
62
            )
41✔
63
            .into_array()
41✔
64
            .into());
41✔
65
        }
4,554✔
66

67
        // We know that constant array don't need stats propagation, so we can avoid the overhead of
68
        // computing derived stats and merging them in.
69
        let derived_stats = (!array.is_constant()).then(|| derive_take_stats(array));
4,554✔
70

71
        let taken = take_impl(array, indices, kernels)?;
4,554✔
72

73
        if let Some(derived_stats) = derived_stats {
4,516✔
74
            let mut stats = taken.statistics().to_owned();
4,017✔
75
            stats.combine_sets(&derived_stats, array.dtype())?;
4,017✔
76
            for (stat, val) in stats.into_iter() {
4,217✔
77
                taken.statistics().set(stat, val)
708✔
78
            }
79
        }
499✔
80

81
        Ok(taken.into())
4,516✔
82
    }
4,595✔
83

84
    fn return_dtype(&self, args: &InvocationArgs) -> VortexResult<DType> {
4,595✔
85
        let TakeArgs { array, indices } = TakeArgs::try_from(args)?;
4,595✔
86

87
        if !indices.dtype().is_int() {
4,595✔
88
            vortex_bail!(
×
89
                "Take indices must be an integer type, got {}",
×
90
                indices.dtype()
×
91
            );
92
        }
4,595✔
93

94
        Ok(array
4,595✔
95
            .dtype()
4,595✔
96
            .union_nullability(indices.dtype().nullability()))
4,595✔
97
    }
4,595✔
98

99
    fn return_len(&self, args: &InvocationArgs) -> VortexResult<usize> {
4,595✔
100
        let TakeArgs { indices, .. } = TakeArgs::try_from(args)?;
4,595✔
101
        Ok(indices.len())
4,595✔
102
    }
4,595✔
103

104
    fn is_elementwise(&self) -> bool {
4,595✔
105
        false
4,595✔
106
    }
4,595✔
107
}
108

109
fn derive_take_stats(arr: &dyn Array) -> StatsSet {
4,055✔
110
    let stats = arr.statistics().to_owned();
4,055✔
111

112
    let is_constant = stats.get_as::<bool>(Stat::IsConstant);
4,055✔
113

114
    let mut stats = stats.keep_inexact_stats(&[
4,055✔
115
        // Cannot create values smaller than min or larger than max
4,055✔
116
        Stat::Min,
4,055✔
117
        Stat::Max,
4,055✔
118
    ]);
4,055✔
119

120
    if is_constant == Some(Precision::Exact(true)) {
4,055✔
121
        // Any combination of elements from a constant array is still const
×
122
        stats.set(Stat::IsConstant, Precision::exact(true));
×
123
    }
4,055✔
124

125
    stats
4,055✔
126
}
4,055✔
127

128
fn take_impl(
4,554✔
129
    array: &dyn Array,
4,554✔
130
    indices: &dyn Array,
4,554✔
131
    kernels: &[ArcRef<dyn Kernel>],
4,554✔
132
) -> VortexResult<ArrayRef> {
4,554✔
133
    let args = InvocationArgs {
4,554✔
134
        inputs: &[array.into(), indices.into()],
4,554✔
135
        options: &(),
4,554✔
136
    };
4,554✔
137

138
    // First look for a TakeFrom specialized on the indices.
139
    for kernel in TAKE_FROM_FN.kernels() {
4,554✔
140
        if let Some(output) = kernel.invoke(&args)? {
958✔
141
            return output.unwrap_array();
16✔
142
        }
942✔
143
    }
144
    if let Some(output) = indices.invoke(&TAKE_FROM_FN, &args)? {
4,538✔
145
        return output.unwrap_array();
×
146
    }
4,538✔
147

148
    // Then look for a Take kernel
149
    for kernel in kernels {
33,474✔
150
        if let Some(output) = kernel.invoke(&args)? {
33,474✔
151
            return output.unwrap_array();
4,500✔
152
        }
28,936✔
153
    }
UNCOV
154
    if let Some(output) = array.invoke(&TAKE_FN, &args)? {
×
155
        return output.unwrap_array();
×
UNCOV
156
    }
×
157

158
    // Otherwise, canonicalize and try again.
UNCOV
159
    if !array.is_canonical() {
×
UNCOV
160
        log::debug!("No take implementation found for {}", array.encoding_id());
×
UNCOV
161
        let canonical = array.to_canonical()?;
×
UNCOV
162
        return take(canonical.as_ref(), indices);
×
163
    }
×
164

165
    vortex_bail!("No take implementation found for {}", array.encoding_id());
×
166
}
4,554✔
167

168
struct TakeArgs<'a> {
169
    array: &'a dyn Array,
170
    indices: &'a dyn Array,
171
}
172

173
impl<'a> TryFrom<&InvocationArgs<'a>> for TakeArgs<'a> {
174
    type Error = VortexError;
175

176
    fn try_from(value: &InvocationArgs<'a>) -> Result<Self, Self::Error> {
48,217✔
177
        if value.inputs.len() != 2 {
48,217✔
178
            vortex_bail!("Expected 2 inputs, found {}", value.inputs.len());
×
179
        }
48,217✔
180
        let array = value.inputs[0]
48,217✔
181
            .array()
48,217✔
182
            .ok_or_else(|| vortex_err!("Expected first input to be an array"))?;
48,217✔
183
        let indices = value.inputs[1]
48,217✔
184
            .array()
48,217✔
185
            .ok_or_else(|| vortex_err!("Expected second input to be an array"))?;
48,217✔
186
        Ok(Self { array, indices })
48,217✔
187
    }
48,217✔
188
}
189

190
pub trait TakeKernel: VTable {
191
    /// Create a new array by taking the values from the `array` at the
192
    /// given `indices`.
193
    ///
194
    /// # Panics
195
    ///
196
    /// Using `indices` that are invalid for the given `array` will cause a panic.
197
    fn take(&self, array: &Self::Array, indices: &dyn Array) -> VortexResult<ArrayRef>;
198
}
199

200
/// A kernel that implements the filter function.
201
pub struct TakeKernelRef(pub ArcRef<dyn Kernel>);
202
inventory::collect!(TakeKernelRef);
203

204
#[derive(Debug)]
205
pub struct TakeKernelAdapter<V: VTable>(pub V);
206

207
impl<V: VTable + TakeKernel> TakeKernelAdapter<V> {
208
    pub const fn lift(&'static self) -> TakeKernelRef {
×
209
        TakeKernelRef(ArcRef::new_ref(self))
×
210
    }
×
211
}
212

213
impl<V: VTable + TakeKernel> Kernel for TakeKernelAdapter<V> {
214
    fn invoke(&self, args: &InvocationArgs) -> VortexResult<Option<Output>> {
31,843✔
215
        let inputs = TakeArgs::try_from(args)?;
31,843✔
216
        let Some(array) = inputs.array.as_opt::<V>() else {
31,843✔
217
            return Ok(None);
28,342✔
218
        };
219
        Ok(Some(V::take(&self.0, array, inputs.indices)?.into()))
3,501✔
220
    }
31,843✔
221
}
222

223
static TAKE_FROM_FN: LazyLock<ComputeFn> = LazyLock::new(|| {
1,584✔
224
    let compute = ComputeFn::new("take_from".into(), ArcRef::new_ref(&TakeFrom));
1,584✔
225
    for kernel in inventory::iter::<TakeFromKernelRef> {
1,966✔
226
        compute.register_kernel(kernel.0.clone());
382✔
227
    }
382✔
228
    compute
1,584✔
229
});
1,584✔
230

231
pub struct TakeFrom;
232

233
impl ComputeFnVTable for TakeFrom {
234
    fn invoke(
×
235
        &self,
×
236
        _args: &InvocationArgs,
×
237
        _kernels: &[ArcRef<dyn Kernel>],
×
238
    ) -> VortexResult<Output> {
×
239
        vortex_bail!(
×
240
            "TakeFrom should not be invoked directly. Its kernels are used to accelerated the Take function"
×
241
        )
242
    }
×
243

244
    fn return_dtype(&self, args: &InvocationArgs) -> VortexResult<DType> {
×
245
        Take.return_dtype(args)
×
246
    }
×
247

248
    fn return_len(&self, args: &InvocationArgs) -> VortexResult<usize> {
×
249
        Take.return_len(args)
×
250
    }
×
251

252
    fn is_elementwise(&self) -> bool {
×
253
        Take.is_elementwise()
×
254
    }
×
255
}
256

257
pub trait TakeFromKernel: VTable {
258
    /// Create a new array by taking the values from the `array` at the
259
    /// given `indices`.
260
    fn take_from(&self, indices: &Self::Array, array: &dyn Array)
261
    -> VortexResult<Option<ArrayRef>>;
262
}
263

264
pub struct TakeFromKernelRef(pub ArcRef<dyn Kernel>);
265
inventory::collect!(TakeFromKernelRef);
266

267
#[derive(Debug)]
268
pub struct TakeFromKernelAdapter<V: VTable>(pub V);
269

270
impl<V: VTable + TakeFromKernel> TakeFromKernelAdapter<V> {
271
    pub const fn lift(&'static self) -> TakeFromKernelRef {
×
272
        TakeFromKernelRef(ArcRef::new_ref(self))
×
273
    }
×
274
}
275

276
impl<V: VTable + TakeFromKernel> Kernel for TakeFromKernelAdapter<V> {
277
    fn invoke(&self, args: &InvocationArgs) -> VortexResult<Option<Output>> {
535✔
278
        let inputs = TakeArgs::try_from(args)?;
535✔
279
        let Some(indices) = inputs.indices.as_opt::<V>() else {
535✔
280
            return Ok(None);
519✔
281
        };
282
        Ok(V::take_from(&self.0, indices, inputs.array)?.map(Output::from))
16✔
283
    }
535✔
284
}
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