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

vortex-data / vortex / 16199940901

10 Jul 2025 03:54PM UTC coverage: 81.084% (+2.9%) from 78.188%
16199940901

Pull #3822

github

web-flow
Merge ec099962a into 325722ee9
Pull Request #3822: chore: TPC-H CI/coverage improvements

45627 of 56271 relevant lines covered (81.08%)

145002.85 hits per line

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

75.0
/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
pub fn take(array: &dyn Array, indices: &dyn Array) -> VortexResult<ArrayRef> {
4,354✔
18
    if indices.is_empty() {
4,354✔
19
        return Ok(Canonical::empty(
2✔
20
            &array
2✔
21
                .dtype()
2✔
22
                .union_nullability(indices.dtype().nullability()),
2✔
23
        )
2✔
24
        .into_array());
2✔
25
    }
4,352✔
26

4,352✔
27
    TAKE_FN
4,352✔
28
        .invoke(&InvocationArgs {
4,352✔
29
            inputs: &[array.into(), indices.into()],
4,352✔
30
            options: &(),
4,352✔
31
        })?
4,352✔
32
        .unwrap_array()
4,316✔
33
}
4,354✔
34

35
pub static TAKE_FN: LazyLock<ComputeFn> = LazyLock::new(|| {
1,537✔
36
    let compute = ComputeFn::new("take".into(), ArcRef::new_ref(&Take));
1,537✔
37
    for kernel in inventory::iter::<TakeKernelRef> {
22,786✔
38
        compute.register_kernel(kernel.0.clone());
21,249✔
39
    }
21,249✔
40
    compute
1,537✔
41
});
1,537✔
42

43
pub struct Take;
44

45
impl ComputeFnVTable for Take {
46
    fn invoke(
4,352✔
47
        &self,
4,352✔
48
        args: &InvocationArgs,
4,352✔
49
        kernels: &[ArcRef<dyn Kernel>],
4,352✔
50
    ) -> VortexResult<Output> {
4,352✔
51
        let TakeArgs { array, indices } = TakeArgs::try_from(args)?;
4,352✔
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,352✔
59
            return Ok(ConstantArray::new(
39✔
60
                Scalar::null(array.dtype().as_nullable()),
39✔
61
                indices.len(),
39✔
62
            )
39✔
63
            .into_array()
39✔
64
            .into());
39✔
65
        }
4,313✔
66

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

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

73
        if let Some(derived_stats) = derived_stats {
4,277✔
74
            let mut stats = taken.statistics().to_owned();
3,808✔
75
            stats.combine_sets(&derived_stats, array.dtype())?;
3,808✔
76
            for (stat, val) in stats.into_iter() {
4,008✔
77
                taken.statistics().set(stat, val)
700✔
78
            }
79
        }
469✔
80

81
        Ok(taken.into())
4,277✔
82
    }
4,352✔
83

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

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

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

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

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

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

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

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

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

125
    stats
3,844✔
126
}
3,844✔
127

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

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

148
    // Then look for a Take kernel
149
    for kernel in kernels {
34,377✔
150
        if let Some(output) = kernel.invoke(&args)? {
34,377✔
151
            return output.unwrap_array();
4,261✔
152
        }
30,080✔
153
    }
154
    if let Some(output) = array.invoke(&TAKE_FN, &args)? {
×
155
        return output.unwrap_array();
×
156
    }
×
157

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

×
165
    vortex_bail!("No take implementation found for {}", array.encoding_id());
×
166
}
4,313✔
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,361✔
177
        if value.inputs.len() != 2 {
48,361✔
178
            vortex_bail!("Expected 2 inputs, found {}", value.inputs.len());
×
179
        }
48,361✔
180
        let array = value.inputs[0]
48,361✔
181
            .array()
48,361✔
182
            .ok_or_else(|| vortex_err!("Expected first input to be an array"))?;
48,361✔
183
        let indices = value.inputs[1]
48,361✔
184
            .array()
48,361✔
185
            .ok_or_else(|| vortex_err!("Expected second input to be an array"))?;
48,361✔
186
        Ok(Self { array, indices })
48,361✔
187
    }
48,361✔
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>> {
32,980✔
215
        let inputs = TakeArgs::try_from(args)?;
32,980✔
216
        let Some(array) = inputs.array.as_opt::<V>() else {
32,980✔
217
            return Ok(None);
29,662✔
218
        };
219
        Ok(Some(V::take(&self.0, array, inputs.indices)?.into()))
3,318✔
220
    }
32,980✔
221
}
222

223
pub static TAKE_FROM_FN: LazyLock<ComputeFn> = LazyLock::new(|| {
1,499✔
224
    let compute = ComputeFn::new("take_from".into(), ArcRef::new_ref(&TakeFrom));
1,499✔
225
    for kernel in inventory::iter::<TakeFromKernelRef> {
1,861✔
226
        compute.register_kernel(kernel.0.clone());
362✔
227
    }
362✔
228
    compute
1,499✔
229
});
1,499✔
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>> {
529✔
278
        let inputs = TakeArgs::try_from(args)?;
529✔
279
        let Some(indices) = inputs.indices.as_opt::<V>() else {
529✔
280
            return Ok(None);
513✔
281
        };
282
        Ok(V::take_from(&self.0, indices, inputs.array)?.map(Output::from))
16✔
283
    }
529✔
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