Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 44 additions & 49 deletions datafusion/pruning/src/pruning_predicate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,12 +533,15 @@ impl<'a> PruningPredicateBuilder<'a> {
// build predicate expression once
let mut required_columns = RequiredColumns::new();
let mut properties = PruningExpressionProperties::default();
let ctx = PredicateBuildContext {
schema: &file_schema,
unhandled_hook: &unhandled_hook,
max_in_list_size: self.max_in_list_size,
};
let predicate_expr = build_predicate_expression(
&predicate,
&file_schema,
&ctx,
&mut required_columns,
&unhandled_hook,
self.max_in_list_size,
&mut properties,
);
let predicate_schema = required_columns.schema();
Expand Down Expand Up @@ -1738,17 +1741,28 @@ impl PredicateRewriter {
) -> Arc<dyn PhysicalExpr> {
let mut required_columns = RequiredColumns::new();
let mut properties = PruningExpressionProperties::default();
build_predicate_expression(
expr,
&Arc::new(schema.clone()),
&mut required_columns,
&self.unhandled_hook,
self.max_in_list_size,
&mut properties,
)
let schema = Arc::new(schema.clone());
let ctx = PredicateBuildContext {
schema: &schema,
unhandled_hook: &self.unhandled_hook,
max_in_list_size: self.max_in_list_size,
};
build_predicate_expression(expr, &ctx, &mut required_columns, &mut properties)
}
}

/// The settings that do not change while [`build_predicate_expression`]
/// rewrites one predicate.
#[derive(Clone, Copy)]
struct PredicateBuildContext<'a> {
/// The schema of the predicate
schema: &'a SchemaRef,
/// Rewrites the predicates that pruning cannot use
unhandled_hook: &'a Arc<dyn UnhandledPredicateHook>,
/// The largest `IN (...)` list that pruning uses
max_in_list_size: usize,
}

/// Translate logical filter expression into pruning predicate
/// expression that will evaluate to FALSE if it can be determined no
/// rows between the min/max values could pass the predicates.
Expand All @@ -1763,12 +1777,15 @@ impl PredicateRewriter {
/// checks. Longer lists fall back to `unhandled_hook`.
fn build_predicate_expression(
expr: &Arc<dyn PhysicalExpr>,
schema: &SchemaRef,
ctx: &PredicateBuildContext,
required_columns: &mut RequiredColumns,
unhandled_hook: &Arc<dyn UnhandledPredicateHook>,
max_in_list_size: usize,
properties: &mut PruningExpressionProperties,
) -> Arc<dyn PhysicalExpr> {
let PredicateBuildContext {
schema,
unhandled_hook,
max_in_list_size,
} = *ctx;
if is_always_false(expr) {
// Shouldn't return `unhandled_hook.handle(expr)`
// Because it will transfer false to true.
Expand Down Expand Up @@ -1838,10 +1855,8 @@ fn build_predicate_expression(
.unwrap();
return build_predicate_expression(
&change_expr,
schema,
ctx,
required_columns,
unhandled_hook,
max_in_list_size,
properties,
);
} else {
Expand Down Expand Up @@ -1873,14 +1888,7 @@ fn build_predicate_expression(
.map(|(_, then)| then)
.chain(case.else_expr())
.map(|branch| {
build_predicate_expression(
branch,
schema,
required_columns,
unhandled_hook,
max_in_list_size,
properties,
)
build_predicate_expression(branch, ctx, required_columns, properties)
})
.reduce(|acc, branch| {
Arc::new(phys_expr::BinaryExpr::new(acc, Operator::Or, branch)) as _
Expand Down Expand Up @@ -1916,22 +1924,10 @@ fn build_predicate_expression(
};

if op == Operator::And || op == Operator::Or {
let left_expr = build_predicate_expression(
&left,
schema,
required_columns,
unhandled_hook,
max_in_list_size,
properties,
);
let right_expr = build_predicate_expression(
&right,
schema,
required_columns,
unhandled_hook,
max_in_list_size,
properties,
);
let left_expr =
build_predicate_expression(&left, ctx, required_columns, properties);
let right_expr =
build_predicate_expression(&right, ctx, required_columns, properties);
// simplify boolean expression if applicable
let expr = match (&left_expr, op, &right_expr) {
(left, Operator::And, right)
Expand Down Expand Up @@ -7498,14 +7494,13 @@ mod tests {
let expr = logical2physical(expr, schema);
let unhandled_hook = Arc::new(ConstantUnhandledPredicateHook::default()) as _;
let mut properties = PruningExpressionProperties::default();
build_predicate_expression(
&expr,
&Arc::new(schema.clone()),
required_columns,
&unhandled_hook,
MAX_IN_LIST_SIZE,
&mut properties,
)
let schema = Arc::new(schema.clone());
let ctx = PredicateBuildContext {
schema: &schema,
unhandled_hook: &unhandled_hook,
max_in_list_size: MAX_IN_LIST_SIZE,
};
build_predicate_expression(&expr, &ctx, required_columns, &mut properties)
}

#[test]
Expand Down