-
Notifications
You must be signed in to change notification settings - Fork 203
Expand file tree
/
Copy pathbind.rs
More file actions
217 lines (187 loc) · 7.21 KB
/
Copy pathbind.rs
File metadata and controls
217 lines (187 loc) · 7.21 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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors
//! Bind abstract `vortex.stat` expressions to a concrete stats representation.
//!
//! Stats rewrite rules describe pruning in terms of `vortex.stat(input, aggregate_fn)` placeholders
//! so the rewrite is independent of where statistics are stored. These stat placeholders are
//! abstract because they name the statistic needed for a proof, but not how that statistic is
//! represented by a specific layout or reader.
//!
//! Binding is the later pass that replaces each abstract placeholder with the representation used
//! by a caller: zone-map field references, file-level stat literals, or typed nulls for missing
//! stats. This lets all callers share the same falsification rules while keeping layout-specific
//! stat storage behind [`StatBinder`].
use vortex_error::VortexResult;
use crate::aggregate_fn::AggregateFnRef;
use crate::dtype::DType;
use crate::expr::BoundExpression;
use crate::expr::bound::lit;
use crate::expr::traversal::NodeExt;
use crate::expr::traversal::Transformed;
use crate::scalar::Scalar;
use crate::scalar_fn::fns::stat::StatFn;
/// A target that can bind abstract statistics to concrete expressions.
///
/// Implementations define how a pruning proof should read stats from a specific backing
/// representation. For example, a zone-map binder can translate a `max(col)` placeholder into a
/// field reference in the per-zone stats table, while a file-stats binder can translate the same
/// placeholder into a literal value from the file footer.
pub trait StatBinder {
/// Bind `aggregate_fn(input)` to a concrete expression.
///
/// Implementations should return `Ok(None)` when the requested aggregate
/// statistic is unavailable in their backing representation.
fn bind_aggregate(
&self,
input: &BoundExpression,
aggregate_fn: &AggregateFnRef,
stat_dtype: &DType,
) -> VortexResult<Option<BoundExpression>>;
/// Expression to use when a stat is unavailable.
///
/// The default is a nullable null literal, which preserves three-valued
/// pruning semantics for stats-table execution.
fn missing_stat(&self, dtype: DType) -> VortexResult<BoundExpression> {
null_expr(dtype)
}
}
/// Bind all `vortex.stat` expressions in `predicate`.
///
/// The predicate is usually the output of a stats rewrite rule. Rewrite rules
/// are responsible for expressing stat semantics; binding maps aggregate-backed
/// stat requests to the concrete stats representation supported by the binder.
pub fn bind_stats<B: StatBinder + ?Sized>(
predicate: BoundExpression,
binder: &B,
) -> VortexResult<BoundExpression> {
Ok(predicate
.transform_down(|expr| {
if !expr.is::<StatFn>() {
return Ok(Transformed::no(expr));
}
match bind_stat_fn(&expr, binder)? {
Some(bound) => Ok(Transformed::yes(bound)),
None => Ok(Transformed::yes(binder.missing_stat(expr.dtype().clone())?)),
}
})?
.into_inner())
}
fn bind_stat_fn(
expr: &BoundExpression,
binder: &(impl StatBinder + ?Sized),
) -> VortexResult<Option<BoundExpression>> {
let options = expr.as_::<StatFn>();
let aggregate_fn = options.aggregate_fn();
// `StatFn` has exactly one child: the expression the aggregate statistic is computed over.
let input = expr.child(0);
binder.bind_aggregate(input, aggregate_fn, expr.dtype())
}
fn null_expr(dtype: DType) -> VortexResult<BoundExpression> {
Ok(lit(Scalar::null(dtype.as_nullable())))
}
#[cfg(test)]
mod tests {
use vortex_error::VortexResult;
use super::*;
use crate::dtype::Nullability;
use crate::dtype::PType;
use crate::dtype::StructFields;
use crate::expr::and;
use crate::expr::col;
use crate::expr::get_item;
use crate::expr::is_null;
use crate::expr::lit;
use crate::expr::or;
use crate::expr::root;
use crate::expr::stats::Stat;
use crate::stats::all_non_nan;
use crate::stats::nan_count;
struct TestBinder {
input_scope: DType,
stats_scope: DType,
bind_nan_count: bool,
}
impl TestBinder {
fn new(bind_nan_count: bool) -> Self {
Self {
input_scope: DType::Struct(
StructFields::from_iter([(
"f",
DType::Primitive(PType::F32, Nullability::NonNullable),
)]),
Nullability::NonNullable,
),
stats_scope: DType::Struct(
StructFields::from_iter([(
"f_nan_count",
DType::Primitive(PType::U64, Nullability::NonNullable),
)]),
Nullability::NonNullable,
),
bind_nan_count,
}
}
}
impl StatBinder for TestBinder {
fn bind_aggregate(
&self,
_input: &BoundExpression,
aggregate_fn: &AggregateFnRef,
_stat_dtype: &DType,
) -> VortexResult<Option<BoundExpression>> {
let Some(stat) = Stat::from_aggregate_fn(aggregate_fn) else {
return Ok(None);
};
if stat == Stat::NaNCount && self.bind_nan_count {
Ok(Some(
get_item("f_nan_count", root()).bind(&self.stats_scope)?,
))
} else {
Ok(None)
}
}
}
#[test]
fn nan_count_binds_to_direct_stat_slot() -> VortexResult<()> {
let binder = TestBinder::new(true);
let bound = bind_stats(nan_count(col("f")).bind(&binder.input_scope)?, &binder)?;
assert_eq!(bound, col("f_nan_count").bind(&binder.stats_scope)?);
Ok(())
}
#[test]
fn all_non_nan_does_not_derive_from_nan_count() -> VortexResult<()> {
let binder = TestBinder::new(true);
let bound = bind_stats(all_non_nan(col("f")).bind(&binder.input_scope)?, &binder)?;
assert_eq!(
bound,
lit(Scalar::null(DType::Bool(Nullability::Nullable))).bind(&binder.stats_scope)?
);
Ok(())
}
#[test]
fn missing_stats_bind_to_null_without_reducing() -> VortexResult<()> {
let binder = TestBinder::new(false);
let null_bool = lit(Scalar::null(DType::Bool(Nullability::Nullable)));
let bound = bind_stats(
and(lit(false), all_non_nan(col("f"))).bind(&binder.input_scope)?,
&binder,
)?;
assert_eq!(
bound,
and(lit(false), null_bool.clone()).bind(&binder.stats_scope)?
);
let bound = bind_stats(
or(lit(true), all_non_nan(col("f"))).bind(&binder.input_scope)?,
&binder,
)?;
assert_eq!(bound, or(lit(true), null_bool).bind(&binder.stats_scope)?);
Ok(())
}
#[test]
fn unrelated_expressions_do_not_request_nan_count() -> VortexResult<()> {
let binder = TestBinder::new(false);
let bound = bind_stats(is_null(col("f")).bind(&binder.input_scope)?, &binder)?;
assert_eq!(bound, is_null(col("f")).bind(&binder.input_scope)?);
Ok(())
}
}