-
Notifications
You must be signed in to change notification settings - Fork 154
Expand file tree
/
Copy pathrules.rs
More file actions
211 lines (186 loc) · 7.81 KB
/
rules.rs
File metadata and controls
211 lines (186 loc) · 7.81 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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors
use vortex_error::VortexResult;
use crate::ArrayEq;
use crate::ArrayRef;
use crate::IntoArray;
use crate::Precision;
use crate::array::ArrayView;
use crate::array::VTable;
use crate::arrays::Constant;
use crate::arrays::ConstantArray;
use crate::arrays::Dict;
use crate::arrays::DictArray;
use crate::arrays::ScalarFn;
use crate::arrays::ScalarFnArray;
use crate::arrays::dict::DictArraySlotsExt;
use crate::arrays::filter::FilterReduceAdaptor;
use crate::arrays::reversed::ReverseReduceAdaptor;
use crate::arrays::scalar_fn::AnyScalarFn;
use crate::arrays::scalar_fn::ScalarFnArrayExt;
use crate::arrays::slice::SliceReduceAdaptor;
use crate::builtins::ArrayBuiltins;
use crate::optimizer::ArrayOptimizer;
use crate::optimizer::rules::ArrayParentReduceRule;
use crate::optimizer::rules::ParentRuleSet;
use crate::scalar_fn::fns::cast::Cast;
use crate::scalar_fn::fns::cast::CastReduceAdaptor;
use crate::scalar_fn::fns::like::LikeReduceAdaptor;
use crate::scalar_fn::fns::mask::MaskReduceAdaptor;
use crate::scalar_fn::fns::pack::Pack;
use crate::validity::Validity;
pub(crate) const PARENT_RULES: ParentRuleSet<Dict> = ParentRuleSet::new(&[
ParentRuleSet::lift(&FilterReduceAdaptor(Dict)),
ParentRuleSet::lift(&CastReduceAdaptor(Dict)),
ParentRuleSet::lift(&MaskReduceAdaptor(Dict)),
ParentRuleSet::lift(&LikeReduceAdaptor(Dict)),
ParentRuleSet::lift(&DictionaryScalarFnValuesPushDownRule),
ParentRuleSet::lift(&DictionaryScalarFnCodesPullUpRule),
ParentRuleSet::lift(&ReverseReduceAdaptor(Dict)),
ParentRuleSet::lift(&SliceReduceAdaptor(Dict)),
]);
/// Push down a scalar function to run only over the values of a dictionary array.
#[derive(Debug)]
struct DictionaryScalarFnValuesPushDownRule;
impl ArrayParentReduceRule<Dict> for DictionaryScalarFnValuesPushDownRule {
type Parent = AnyScalarFn;
fn reduce_parent(
&self,
array: ArrayView<'_, Dict>,
parent: ArrayView<'_, ScalarFn>,
child_idx: usize,
) -> VortexResult<Option<ArrayRef>> {
// Check that the scalar function can actually be pushed down.
let sig = parent.scalar_fn().signature();
// Don't push down pack expressions since we might want to unpack them in exporters
// later.
if parent.scalar_fn().is::<Pack>() {
return Ok(None);
}
// Don't push down cast operations — CastReduceAdaptor handles these eagerly.
// If it declined (returned None), we must fall through to the canonical path
// rather than creating a lazy cast inside the dictionary values.
if parent.scalar_fn().is::<Cast>() {
return Ok(None);
}
// If the dictionary has less codes than values don't push down this might
// happen if the dictionary is sliced.
if array.values().len() > array.codes().len() {
return Ok(None);
}
// If the scalar function is fallible, we cannot push it down since it may fail over a
// value that isn't referenced by any code.
if !array.all_values_referenced && sig.is_fallible() {
tracing::trace!(
"Not pushing down fallible scalar function {} over dictionary with sparse codes {}",
parent.scalar_fn(),
Dict.id(),
);
return Ok(None);
}
// Check that all siblings are constant
// TODO(ngates): we can also support other dictionaries if the values are the same!
if !parent
.iter_children()
.enumerate()
.all(|(idx, c)| idx == child_idx || c.is::<Constant>())
{
return Ok(None);
}
// If the scalar function is null-sensitive, then we cannot push it down to values if
// we have any nulls in the codes.
if array.codes().dtype().is_nullable()
&& !matches!(
array.codes().validity()?,
Validity::NonNullable | Validity::AllValid
)
&& sig.is_null_sensitive()
{
tracing::trace!(
"Not pushing down null-sensitive scalar function {} over dictionary with null codes {}",
parent.scalar_fn(),
Dict.id(),
);
return Ok(None);
}
// Now we push the parent scalar function into the dictionary values.
let values_len = array.values().len();
let mut new_children = Vec::with_capacity(parent.nchildren());
for (idx, child) in parent.iter_children().enumerate() {
if idx == child_idx {
new_children.push(array.values().clone());
} else {
let scalar = child.as_::<Constant>().scalar().clone();
new_children.push(ConstantArray::new(scalar, values_len).into_array());
}
}
let new_values =
ScalarFnArray::try_new(parent.scalar_fn().clone(), new_children, values_len)?
.into_array()
.optimize()?;
// We can only push down null-sensitive functions when we have all-valid codes.
// In these cases, we cannot have the codes influence the nullability of the output DType.
// Therefore, we cast the codes to be non-nullable and then cast the dictionary output
// back to nullable if needed.
if sig.is_null_sensitive() && array.codes().dtype().is_nullable() {
let new_codes = array.codes().cast(array.codes().dtype().as_nonnullable())?;
let new_dict = unsafe { DictArray::new_unchecked(new_codes, new_values) }.into_array();
return Ok(Some(new_dict.cast(parent.dtype().clone())?));
}
Ok(Some(
unsafe { DictArray::new_unchecked(array.codes().clone(), new_values) }.into_array(),
))
}
}
#[derive(Debug)]
struct DictionaryScalarFnCodesPullUpRule;
impl ArrayParentReduceRule<Dict> for DictionaryScalarFnCodesPullUpRule {
type Parent = AnyScalarFn;
fn reduce_parent(
&self,
array: ArrayView<'_, Dict>,
parent: ArrayView<'_, ScalarFn>,
child_idx: usize,
) -> VortexResult<Option<ArrayRef>> {
// Don't attempt to pull up if there are less than 2 siblings.
if parent.nchildren() < 2 {
return Ok(None);
}
// Check that all siblings are dictionaries, and have the same number of values as us.
// This is a cheap first loop.
if !parent.iter_children().enumerate().all(|(idx, c)| {
idx == child_idx
|| c.as_opt::<Dict>()
.is_some_and(|c| c.values().len() == array.values().len())
}) {
return Ok(None);
}
// Now run the slightly more expensive check that all siblings have the same codes as us.
// We use the cheaper Precision::Ptr to avoid doing data comparisons.
if !parent.iter_children().enumerate().all(|(idx, c)| {
idx == child_idx
|| c.as_opt::<Dict>()
.is_some_and(|c| c.codes().array_eq(array.codes(), Precision::Value))
}) {
return Ok(None);
}
let mut new_children = Vec::with_capacity(parent.nchildren());
for (idx, child) in parent.iter_children().enumerate() {
if idx == child_idx {
new_children.push(array.values().clone());
} else {
new_children.push(child.as_::<Dict>().values().clone());
}
}
let new_values = ScalarFnArray::try_new(
parent.scalar_fn().clone(),
new_children,
array.values().len(),
)?
.into_array()
.optimize()?;
let new_dict =
unsafe { DictArray::new_unchecked(array.codes().clone(), new_values) }.into_array();
Ok(Some(new_dict))
}
}