Skip to content

Commit 59167d7

Browse files
committed
Prune plan scans with zoned statistics
Signed-off-by: Joe Isaacs <joe.isaacs@live.co.uk>
1 parent ecff2cc commit 59167d7

15 files changed

Lines changed: 890 additions & 102 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vortex-layout/src/layouts/zoned/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,11 @@ impl ZonedLayout {
358358
}
359359

360360
impl ZonedData {
361-
fn aggregate_fns(&self) -> Arc<[AggregateFnRef]> {
361+
pub(crate) fn zone_len(&self) -> usize {
362+
self.zone_len
363+
}
364+
365+
pub(crate) fn aggregate_fns(&self) -> Arc<[AggregateFnRef]> {
362366
match &self.zone_map_schema {
363367
ZoneMapSchema::LegacyStats(stats) => stats
364368
.iter()

vortex-layout/src/layouts/zoned/zone_map.rs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use vortex_array::aggregate_fn::fns::bounded_max::BOUNDED_MAX_BOUND;
1818
use vortex_array::aggregate_fn::fns::bounded_max::BoundedMax;
1919
use vortex_array::aggregate_fn::fns::sum::Sum;
2020
use vortex_array::aggregate_fn::fns::sum::normalize_legacy_partial_array;
21+
use vortex_array::arrays::BoolArray;
2122
use vortex_array::arrays::ConstantArray;
2223
use vortex_array::arrays::PrimitiveArray;
2324
use vortex_array::arrays::StructArray;
@@ -92,7 +93,7 @@ impl ZoneMap {
9293
Ok(unsafe { Self::new_unchecked(column_dtype, array, aggregate_fns, zone_len, row_count) })
9394
}
9495

95-
pub(super) unsafe fn new_unchecked(
96+
pub(crate) unsafe fn new_unchecked(
9697
column_dtype: DType,
9798
array: StructArray,
9899
aggregate_fns: Arc<[AggregateFnRef]>,
@@ -152,19 +153,32 @@ impl ZoneMap {
152153
session: &VortexSession,
153154
) -> VortexResult<Mask> {
154155
let mut ctx = session.create_execution_ctx();
155-
let num_zones = self.array.len();
156-
let predicate = self.lower_stats(predicate.clone())?;
156+
self.applied_predicate(predicate)?
157+
.null_as_false()
158+
.execute(&mut ctx)
159+
}
157160

158-
let array = self.array.clone().into_array();
159-
let applied = array.apply_bound(&predicate)?;
161+
/// Evaluates a pruning predicate while preserving unknown (null) proof values.
162+
pub(crate) fn evaluate(
163+
&self,
164+
predicate: &BoundExpression,
165+
session: &VortexSession,
166+
) -> VortexResult<BoolArray> {
167+
let mut ctx = session.create_execution_ctx();
168+
self.applied_predicate(predicate)?
169+
.execute::<BoolArray>(&mut ctx)
170+
}
160171

172+
fn applied_predicate(&self, predicate: &BoundExpression) -> VortexResult<ArrayRef> {
173+
let num_zones = self.array.len();
174+
let predicate = self.lower_stats(predicate.clone())?;
175+
let applied = self.array.clone().into_array().apply_bound(&predicate)?;
161176
if !contains_row_count(&applied) {
162-
return applied.null_as_false().execute(&mut ctx);
177+
return Ok(applied);
163178
}
164179

165180
let row_count_array = row_count_array(self.zone_len, self.row_count, num_zones)?;
166-
let substituted = substitute_row_count(applied, &row_count_array)?;
167-
substituted.null_as_false().execute(&mut ctx)
181+
substitute_row_count(applied, &row_count_array)
168182
}
169183

170184
fn lower_stats(&self, predicate: BoundExpression) -> VortexResult<BoundExpression> {

vortex-layout/src/plan/lower.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,18 @@ fn lazy_children(layout: LayoutRef, slots: Vec<usize>) -> PlanChildren {
148148

149149
fn lower_zoned(layout: &LayoutRef) -> VortexResult<ZonedPlan> {
150150
// Zoned and legacy stats layouts share a child shape: transparent data, auxiliary zones.
151+
let metadata = if let Some(layout) = layout.as_opt::<Zoned>() {
152+
layout.data()
153+
} else if let Some(layout) = layout.as_opt::<LegacyStats>() {
154+
layout.data()
155+
} else {
156+
vortex_bail!("Zoned plan requires a zoned layout")
157+
};
151158
Ok(ZonedPlan::from_children(
152159
layout.dtype().clone(),
153160
layout.row_count(),
154161
lazy_children(Arc::clone(layout), vec![0, 1]),
162+
u64::try_from(metadata.zone_len())?,
163+
metadata.aggregate_fns(),
155164
))
156165
}

vortex-layout/src/plan/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ pub use plans::Take;
5858
pub use plans::TakeData;
5959
pub use plans::TakePlan;
6060
pub use plans::Zoned;
61+
pub use plans::ZonedData;
6162
pub use plans::ZonedPlan;
6263
pub use plans::row_idx_dtype;
6364
pub use typed::DynPlan;

vortex-layout/src/plan/optimizer/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ use super::Pack;
1616
use super::PlanRef;
1717
use super::RowIdx;
1818
use super::Take;
19+
use super::Zoned;
1920
use super::plans::ExpressionConcatRule;
2021
use super::plans::ExpressionPackRule;
2122
use super::plans::ExpressionRowIdxRule;
2223
use super::plans::ExpressionTakeRule;
24+
use super::plans::ExpressionZonedRule;
2325

2426
static EXPRESSION_CONCAT_RULE: PlanParentReduceRuleAdapter<Concat, ExpressionConcatRule> =
2527
PlanParentReduceRuleAdapter::new(ExpressionConcatRule);
@@ -29,12 +31,15 @@ static EXPRESSION_ROW_IDX_RULE: PlanParentReduceRuleAdapter<RowIdx, ExpressionRo
2931
PlanParentReduceRuleAdapter::new(ExpressionRowIdxRule);
3032
static EXPRESSION_PACK_RULE: PlanParentReduceRuleAdapter<Pack, ExpressionPackRule> =
3133
PlanParentReduceRuleAdapter::new(ExpressionPackRule);
34+
static EXPRESSION_ZONED_RULE: PlanParentReduceRuleAdapter<Zoned, ExpressionZonedRule> =
35+
PlanParentReduceRuleAdapter::new(ExpressionZonedRule);
3236

3337
static PARENT_RULES: PlanParentRuleSet = PlanParentRuleSet::new(&[
3438
&EXPRESSION_CONCAT_RULE,
3539
&EXPRESSION_TAKE_RULE,
3640
&EXPRESSION_ROW_IDX_RULE,
3741
&EXPRESSION_PACK_RULE,
42+
&EXPRESSION_ZONED_RULE,
3843
]);
3944

4045
/// Attempts a static rewrite for `parent` and its child at `child_idx`.

vortex-layout/src/plan/plans/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,7 @@ pub(crate) use take::ExpressionTakeRule;
4545
pub use take::Take;
4646
pub use take::TakeData;
4747
pub use take::TakePlan;
48+
pub(crate) use zoned::ExpressionZonedRule;
4849
pub use zoned::Zoned;
50+
pub use zoned::ZonedData;
4951
pub use zoned::ZonedPlan;

0 commit comments

Comments
 (0)