Skip to content

Commit 1673868

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

15 files changed

Lines changed: 876 additions & 100 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
@@ -16,6 +16,7 @@ use vortex_array::aggregate_fn::fns::all_non_null::AllNonNull;
1616
use vortex_array::aggregate_fn::fns::all_null::AllNull;
1717
use vortex_array::aggregate_fn::fns::bounded_max::BOUNDED_MAX_BOUND;
1818
use vortex_array::aggregate_fn::fns::bounded_max::BoundedMax;
19+
use vortex_array::arrays::BoolArray;
1920
use vortex_array::arrays::ConstantArray;
2021
use vortex_array::arrays::PrimitiveArray;
2122
use vortex_array::arrays::StructArray;
@@ -84,7 +85,7 @@ impl ZoneMap {
8485
Ok(unsafe { Self::new_unchecked(column_dtype, array, aggregate_fns, zone_len, row_count) })
8586
}
8687

87-
pub(super) unsafe fn new_unchecked(
88+
pub(crate) unsafe fn new_unchecked(
8889
column_dtype: DType,
8990
array: StructArray,
9091
aggregate_fns: Arc<[AggregateFnRef]>,
@@ -144,19 +145,32 @@ impl ZoneMap {
144145
session: &VortexSession,
145146
) -> VortexResult<Mask> {
146147
let mut ctx = session.create_execution_ctx();
147-
let num_zones = self.array.len();
148-
let predicate = self.lower_stats(predicate.clone())?;
148+
self.applied_predicate(predicate)?
149+
.null_as_false()
150+
.execute(&mut ctx)
151+
}
149152

150-
let array = self.array.clone().into_array();
151-
let applied = array.apply_bound(&predicate)?;
153+
/// Evaluates a pruning predicate while preserving unknown (null) proof values.
154+
pub(crate) fn evaluate(
155+
&self,
156+
predicate: &BoundExpression,
157+
session: &VortexSession,
158+
) -> VortexResult<BoolArray> {
159+
let mut ctx = session.create_execution_ctx();
160+
self.applied_predicate(predicate)?
161+
.execute::<BoolArray>(&mut ctx)
162+
}
152163

164+
fn applied_predicate(&self, predicate: &BoundExpression) -> VortexResult<ArrayRef> {
165+
let num_zones = self.array.len();
166+
let predicate = self.lower_stats(predicate.clone())?;
167+
let applied = self.array.clone().into_array().apply_bound(&predicate)?;
153168
if !contains_row_count(&applied) {
154-
return applied.null_as_false().execute(&mut ctx);
169+
return Ok(applied);
155170
}
156171

157172
let row_count_array = row_count_array(self.zone_len, self.row_count, num_zones)?;
158-
let substituted = substitute_row_count(applied, &row_count_array)?;
159-
substituted.null_as_false().execute(&mut ctx)
173+
substitute_row_count(applied, &row_count_array)
160174
}
161175

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

vortex-layout/src/plan/lower.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,5 +166,17 @@ fn lower_zoned(layout: &LayoutRef) -> VortexResult<ZonedPlan> {
166166
.slot(1)?
167167
.ok_or_else(|| vortex_err!("Zoned zones child is absent"))?,
168168
)?;
169-
Ok(ZonedPlan::new(data, zones))
169+
let metadata = if let Some(layout) = layout.as_opt::<Zoned>() {
170+
layout.data()
171+
} else if let Some(layout) = layout.as_opt::<LegacyStats>() {
172+
layout.data()
173+
} else {
174+
vortex_bail!("Zoned plan requires a zoned layout")
175+
};
176+
Ok(ZonedPlan::new(
177+
data,
178+
zones,
179+
u64::try_from(metadata.zone_len())?,
180+
metadata.aggregate_fns(),
181+
))
170182
}

vortex-layout/src/plan/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ pub use plans::Take;
5555
pub use plans::TakeData;
5656
pub use plans::TakePlan;
5757
pub use plans::Zoned;
58+
pub use plans::ZonedData;
5859
pub use plans::ZonedPlan;
5960
pub use plans::row_idx_dtype;
6061
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)