vortex-array 0.88.0

Vortex in memory columnar data format
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors

//! Stats as they are stored on arrays.

use std::sync::Arc;
use std::sync::OnceLock;

use parking_lot::RwLock;
use vortex_array::ExecutionCtx;
use vortex_error::VortexError;
use vortex_error::VortexResult;
use vortex_error::vortex_panic;

use super::MutTypedStatsSetRef;
use super::StatsSet;
use super::StatsSetIntoIter;
use super::TypedStatsSetRef;
use crate::ArrayRef;
use crate::aggregate_fn::NumericalAggregateOpts;
use crate::aggregate_fn::fns::is_constant::is_constant;
use crate::aggregate_fn::fns::is_sorted::is_sorted;
use crate::aggregate_fn::fns::is_sorted::is_strict_sorted;
use crate::aggregate_fn::fns::min_max::MinMaxResult;
use crate::aggregate_fn::fns::min_max::min_max;
use crate::aggregate_fn::fns::nan_count::nan_count;
use crate::aggregate_fn::fns::sum::sum;
use crate::aggregate_fn::fns::uncompressed_size_in_bytes::uncompressed_size_in_bytes;
use crate::expr::stats::Precision;
use crate::expr::stats::Stat;
use crate::expr::stats::StatsProvider;
use crate::scalar::Scalar;
use crate::scalar::ScalarValue;

/// A shared [`StatsSet`] stored in an array. Can be shared by copies of the array and can also be mutated in place.
/// Stats are created lazily on read or write.
// TODO(adamg): This is a very bad name.
#[derive(Default, Debug)]
pub struct ArrayStats {
    inner: OnceLock<Arc<RwLock<StatsSet>>>,
}

impl Clone for ArrayStats {
    fn clone(&self) -> Self {
        Self {
            inner: OnceLock::from(Arc::clone(self.shared())),
        }
    }
}

/// Reference to an array's [`StatsSet`]. Can be used to get and mutate the underlying stats.
///
/// Constructed by calling [`ArrayStats::to_ref`].
pub struct StatsSetRef<'a> {
    // We need to reference back to the array
    dyn_array_ref: &'a ArrayRef,
    array_stats: &'a ArrayStats,
}

impl ArrayStats {
    pub fn to_ref<'a>(&'a self, array: &'a ArrayRef) -> StatsSetRef<'a> {
        StatsSetRef {
            dyn_array_ref: array,
            array_stats: self,
        }
    }

    fn shared(&self) -> &Arc<RwLock<StatsSet>> {
        self.inner.get_or_init(Default::default)
    }

    fn read<R>(&self, f: impl FnOnce(&StatsSet) -> R) -> R {
        match self.inner.get() {
            Some(shared) => f(&shared.read()),
            None => f(&StatsSet::default()),
        }
    }

    pub fn set(&self, stat: Stat, value: Precision<ScalarValue>) {
        self.shared().write().set(stat, value);
    }

    pub fn clear(&self, stat: Stat) {
        if let Some(shared) = self.inner.get() {
            shared.write().clear(stat);
        }
    }

    pub fn retain(&self, stats: &[Stat]) {
        if let Some(shared) = self.inner.get() {
            shared.write().retain_only(stats);
        }
    }
}

impl From<StatsSet> for ArrayStats {
    fn from(value: StatsSet) -> Self {
        Self {
            inner: OnceLock::from(Arc::new(RwLock::new(value))),
        }
    }
}

impl From<ArrayStats> for StatsSet {
    fn from(value: ArrayStats) -> Self {
        value.read(|stats| stats.clone())
    }
}

impl StatsSetRef<'_> {
    pub(crate) fn replace(&self, stats: StatsSet) {
        if stats.is_empty() && self.array_stats.inner.get().is_none() {
            return;
        }
        *self.array_stats.shared().write() = stats;
    }

    pub fn set_iter(&self, iter: StatsSetIntoIter) {
        let mut iter = iter.peekable();
        if iter.peek().is_none() {
            return;
        }
        let mut guard = self.array_stats.shared().write();
        for (stat, value) in iter {
            guard.set(stat, value);
        }
    }

    pub fn inherit_from(&self, stats: StatsSetRef<'_>) {
        let Some(source) = stats.array_stats.inner.get() else {
            return;
        };
        // Only inherit if the underlying stats are different
        if self
            .array_stats
            .inner
            .get()
            .is_none_or(|shared| !Arc::ptr_eq(shared, source))
        {
            stats.with_iter(|iter| self.inherit(iter));
        }
    }

    pub fn inherit<'a>(&self, iter: impl Iterator<Item = &'a (Stat, Precision<ScalarValue>)>) {
        let mut iter = iter.peekable();
        if iter.peek().is_none() {
            return;
        }
        let mut guard = self.array_stats.shared().write();
        for (stat, value) in iter {
            if !value.is_exact() {
                if !guard.get(*stat).is_exact() {
                    guard.set(*stat, value.clone());
                }
            } else {
                guard.set(*stat, value.clone());
            }
        }
    }

    pub fn with_typed_stats_set<U, F: FnOnce(TypedStatsSetRef) -> U>(&self, apply: F) -> U {
        self.array_stats
            .read(|stats| apply(stats.as_typed_ref(self.dyn_array_ref.dtype())))
    }

    pub fn with_mut_typed_stats_set<U, F: FnOnce(MutTypedStatsSetRef) -> U>(&self, apply: F) -> U {
        apply(
            self.array_stats
                .shared()
                .write()
                .as_mut_typed_ref(self.dyn_array_ref.dtype()),
        )
    }

    pub fn to_owned(&self) -> StatsSet {
        self.array_stats.read(|stats| stats.clone())
    }

    /// Returns a clone of the underlying [`ArrayStats`].
    pub fn to_array_stats(&self) -> ArrayStats {
        self.array_stats.clone()
    }

    /// Share underlying stats if they exist or return a lazy instance
    pub(crate) fn share_existing(&self) -> ArrayStats {
        ArrayStats {
            inner: match self.array_stats.inner.get() {
                Some(shared) => OnceLock::from(Arc::clone(shared)),
                None => OnceLock::new(),
            },
        }
    }

    pub fn with_iter<
        F: for<'a> FnOnce(&mut dyn Iterator<Item = &'a (Stat, Precision<ScalarValue>)>) -> R,
        R,
    >(
        &self,
        f: F,
    ) -> R {
        self.array_stats.read(|stats| f(&mut stats.iter()))
    }

    /// Returns the value of `stat` by either fetching it from cache if it exists and is [`Precision::Exact`], or falling back to
    /// computation. The underlying compute kernels will cache the computed stat in the latter case.
    pub fn compute_stat(&self, stat: Stat, ctx: &mut ExecutionCtx) -> VortexResult<Option<Scalar>> {
        // If it's already computed and exact, we can return it.
        if let Precision::Exact(s) = self.get(stat) {
            return Ok(Some(s));
        }

        Ok(match stat {
            Stat::Min => min_max(self.dyn_array_ref, ctx, NumericalAggregateOpts::default())?
                .map(|MinMaxResult { min, max: _ }| min),
            Stat::Max => min_max(self.dyn_array_ref, ctx, NumericalAggregateOpts::default())?
                .map(|MinMaxResult { min: _, max }| max),
            Stat::Sum => {
                Stat::Sum
                    .dtype(self.dyn_array_ref.dtype())
                    .is_some()
                    .then(|| {
                        // Sum is supported for this dtype.
                        sum(self.dyn_array_ref, ctx)
                    })
                    .transpose()?
            }
            Stat::NullCount => self.dyn_array_ref.invalid_count(ctx).ok().map(Into::into),
            Stat::IsConstant => {
                if self.dyn_array_ref.is_empty() {
                    None
                } else {
                    Some(is_constant(self.dyn_array_ref, ctx)?.into())
                }
            }
            Stat::IsSorted => Some(is_sorted(self.dyn_array_ref, ctx)?.into()),
            Stat::IsStrictSorted => Some(is_strict_sorted(self.dyn_array_ref, ctx)?.into()),
            Stat::UncompressedSizeInBytes => Stat::UncompressedSizeInBytes
                .dtype(self.dyn_array_ref.dtype())
                .is_some()
                .then(|| uncompressed_size_in_bytes(self.dyn_array_ref, ctx))
                .transpose()?
                .map(|s| s.into()),
            Stat::NaNCount => {
                Stat::NaNCount
                    .dtype(self.dyn_array_ref.dtype())
                    .is_some()
                    .then(|| {
                        // NaNCount is supported for this dtype.
                        nan_count(self.dyn_array_ref, ctx)
                    })
                    .transpose()?
                    .map(|s| s.into())
            }
        })
    }

    pub fn compute_all(&self, stats: &[Stat], ctx: &mut ExecutionCtx) -> VortexResult<StatsSet> {
        let mut stats_set = StatsSet::default();
        for &stat in stats {
            if let Some(s) = self.compute_stat(stat, ctx)?
                && let Some(value) = s.into_value()
            {
                stats_set.set(stat, Precision::exact(value));
            }
        }
        Ok(stats_set)
    }
}

impl StatsSetRef<'_> {
    pub fn compute_as<U: for<'a> TryFrom<&'a Scalar, Error = VortexError>>(
        &self,
        stat: Stat,
        ctx: &mut ExecutionCtx,
    ) -> Option<U> {
        self.compute_stat(stat, ctx)
            .inspect_err(|e| tracing::warn!("Failed to compute stat {stat}: {e}"))
            .ok()
            .flatten()
            .map(|s| U::try_from(&s))
            .transpose()
            .unwrap_or_else(|err| {
                vortex_panic!(
                    err,
                    "Failed to compute stat {} as {}",
                    stat,
                    std::any::type_name::<U>()
                )
            })
    }

    pub fn set(&self, stat: Stat, value: Precision<ScalarValue>) {
        self.array_stats.set(stat, value);
    }

    pub fn clear(&self, stat: Stat) {
        self.array_stats.clear(stat);
    }

    pub fn compute_min<U: for<'a> TryFrom<&'a Scalar, Error = VortexError>>(
        &self,
        ctx: &mut ExecutionCtx,
    ) -> Option<U> {
        self.compute_as(Stat::Min, ctx)
    }

    pub fn compute_max<U: for<'a> TryFrom<&'a Scalar, Error = VortexError>>(
        &self,
        ctx: &mut ExecutionCtx,
    ) -> Option<U> {
        self.compute_as(Stat::Max, ctx)
    }

    pub fn compute_is_sorted(&self, ctx: &mut ExecutionCtx) -> Option<bool> {
        self.compute_bool(Stat::IsSorted, ctx)
    }

    pub fn compute_is_strict_sorted(&self, ctx: &mut ExecutionCtx) -> Option<bool> {
        self.compute_bool(Stat::IsStrictSorted, ctx)
    }

    pub fn compute_is_constant(&self, ctx: &mut ExecutionCtx) -> Option<bool> {
        self.compute_bool(Stat::IsConstant, ctx)
    }

    pub fn compute_null_count(&self, ctx: &mut ExecutionCtx) -> Option<usize> {
        self.compute_usize(Stat::NullCount, ctx)
    }

    pub fn compute_uncompressed_size_in_bytes(&self, ctx: &mut ExecutionCtx) -> Option<usize> {
        self.compute_usize(Stat::UncompressedSizeInBytes, ctx)
    }

    /// Like [`Self::compute_as`], but reads a cached exact value without building a [`Scalar`].
    fn compute_bool(&self, stat: Stat, ctx: &mut ExecutionCtx) -> Option<bool> {
        // Bind the cached value first, so the read lock is released before any computation.
        let cached = self.array_stats.read(|stats| stats.get_bool(stat));
        if let Precision::Exact(value) = cached {
            return Some(value);
        }

        self.compute_as(stat, ctx)
    }

    /// Like [`Self::compute_as`], but reads a cached exact value without building a [`Scalar`].
    fn compute_usize(&self, stat: Stat, ctx: &mut ExecutionCtx) -> Option<usize> {
        // Bind the cached value first, so the read lock is released before any computation.
        let cached = self.array_stats.read(|stats| stats.get_usize(stat));
        if let Precision::Exact(value) = cached {
            return Some(value);
        }

        self.compute_as(stat, ctx)
    }
}

impl StatsProvider for StatsSetRef<'_> {
    fn get(&self, stat: Stat) -> Precision<Scalar> {
        self.array_stats
            .read(|stats| stats.as_typed_ref(self.dyn_array_ref.dtype()).get(stat))
    }

    fn len(&self) -> usize {
        self.array_stats.read(|stats| stats.len())
    }
}

#[cfg(test)]
mod tests {
    use super::ArrayStats;
    use super::StatsSet;
    use crate::expr::stats::Precision;
    use crate::expr::stats::Stat;
    use crate::scalar::ScalarValue;

    #[test]
    fn empty_stats() {
        let stats = ArrayStats::default();
        assert_eq!(StatsSet::from(stats).len(), 0);
    }

    #[test]
    fn clone_stats() {
        let stats = ArrayStats::default();
        let copy = stats.clone();

        stats.set(Stat::NullCount, Precision::exact(ScalarValue::from(3u64)));
        let through_copy = StatsSet::from(copy);
        assert_eq!(
            through_copy.get(Stat::NullCount),
            Precision::exact(ScalarValue::from(3u64))
        );
    }

    #[test]
    fn clone_writes() {
        let stats = ArrayStats::default();
        let copy = stats.clone();

        copy.set(Stat::NullCount, Precision::exact(ScalarValue::from(7u64)));
        assert_eq!(
            StatsSet::from(stats).get(Stat::NullCount),
            Precision::exact(ScalarValue::from(7u64))
        );
    }
}