-
Notifications
You must be signed in to change notification settings - Fork 200
Expand file tree
/
Copy patharray.rs
More file actions
300 lines (264 loc) · 9.49 KB
/
Copy patharray.rs
File metadata and controls
300 lines (264 loc) · 9.49 KB
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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors
//! Stats as they are stored on arrays.
use std::sync::Arc;
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.
// TODO(adamg): This is a very bad name.
#[derive(Clone, Default, Debug)]
pub struct ArrayStats {
inner: Arc<RwLock<StatsSet>>,
}
/// 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,
}
}
pub fn set(&self, stat: Stat, value: Precision<ScalarValue>) {
self.inner.write().set(stat, value);
}
pub fn clear(&self, stat: Stat) {
self.inner.write().clear(stat);
}
pub fn retain(&self, stats: &[Stat]) {
self.inner.write().retain_only(stats);
}
}
impl From<StatsSet> for ArrayStats {
fn from(value: StatsSet) -> Self {
Self {
inner: Arc::new(RwLock::new(value)),
}
}
}
impl From<ArrayStats> for StatsSet {
fn from(value: ArrayStats) -> Self {
value.inner.read().clone()
}
}
impl StatsSetRef<'_> {
pub(crate) fn replace(&self, stats: StatsSet) {
*self.array_stats.inner.write() = stats;
}
pub fn set_iter(&self, iter: StatsSetIntoIter) {
let mut guard = self.array_stats.inner.write();
for (stat, value) in iter {
guard.set(stat, value);
}
}
pub fn inherit_from(&self, stats: StatsSetRef<'_>) {
// Only inherit if the underlying stats are different
if !Arc::ptr_eq(&self.array_stats.inner, &stats.array_stats.inner) {
stats.with_iter(|iter| self.inherit(iter));
}
}
pub fn inherit<'a>(&self, iter: impl Iterator<Item = &'a (Stat, Precision<ScalarValue>)>) {
let mut guard = self.array_stats.inner.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 {
apply(
self.array_stats
.inner
.read()
.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
.inner
.write()
.as_mut_typed_ref(self.dyn_array_ref.dtype()),
)
}
pub fn to_owned(&self) -> StatsSet {
self.array_stats.inner.read().clone()
}
/// Returns a clone of the underlying [`ArrayStats`].
///
/// Since [`ArrayStats`] uses `Arc` internally, this is a cheap reference-count increment.
pub fn to_array_stats(&self) -> ArrayStats {
self.array_stats.clone()
}
pub fn with_iter<
F: for<'a> FnOnce(&mut dyn Iterator<Item = &'a (Stat, Precision<ScalarValue>)>) -> R,
R,
>(
&self,
f: F,
) -> R {
let lock = self.array_stats.inner.read();
f(&mut lock.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_as(Stat::IsSorted, ctx)
}
pub fn compute_is_strict_sorted(&self, ctx: &mut ExecutionCtx) -> Option<bool> {
self.compute_as(Stat::IsStrictSorted, ctx)
}
pub fn compute_is_constant(&self, ctx: &mut ExecutionCtx) -> Option<bool> {
self.compute_as(Stat::IsConstant, ctx)
}
pub fn compute_null_count(&self, ctx: &mut ExecutionCtx) -> Option<usize> {
self.compute_as(Stat::NullCount, ctx)
}
pub fn compute_uncompressed_size_in_bytes(&self, ctx: &mut ExecutionCtx) -> Option<usize> {
self.compute_as(Stat::UncompressedSizeInBytes, ctx)
}
}
impl StatsProvider for StatsSetRef<'_> {
fn get(&self, stat: Stat) -> Precision<Scalar> {
self.array_stats
.inner
.read()
.as_typed_ref(self.dyn_array_ref.dtype())
.get(stat)
}
fn len(&self) -> usize {
self.array_stats.inner.read().len()
}
}