From 332cd6ae370a23cf3eb0b6e56e6ce86c19b9048c Mon Sep 17 00:00:00 2001 From: Adrian Garcia Badaracco <1755071+adriangb@users.noreply.github.com> Date: Sun, 29 Nov 2026 23:30:35 -0800 Subject: [PATCH] refactor: pass a build context to build_predicate_expression build_predicate_expression takes six arguments. Three of them (the schema, the unhandled hook, and the max IN list size) do not change while it rewrites one predicate. Every recursive call passes them through again. Group the three in a private PredicateBuildContext struct and pass one reference to it. This commit does not change behavior. Co-Authored-By: Claude Fable 5.2 --- datafusion/pruning/src/pruning_predicate.rs | 93 ++++++++++----------- 1 file changed, 44 insertions(+), 49 deletions(-) diff --git a/datafusion/pruning/src/pruning_predicate.rs b/datafusion/pruning/src/pruning_predicate.rs index b7325d90d5672..12fa7759f6f82 100644 --- a/datafusion/pruning/src/pruning_predicate.rs +++ b/datafusion/pruning/src/pruning_predicate.rs @@ -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(); @@ -1738,17 +1741,28 @@ impl PredicateRewriter { ) -> Arc { 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, + /// 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. @@ -1763,12 +1777,15 @@ impl PredicateRewriter { /// checks. Longer lists fall back to `unhandled_hook`. fn build_predicate_expression( expr: &Arc, - schema: &SchemaRef, + ctx: &PredicateBuildContext, required_columns: &mut RequiredColumns, - unhandled_hook: &Arc, - max_in_list_size: usize, properties: &mut PruningExpressionProperties, ) -> Arc { + 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. @@ -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 { @@ -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 _ @@ -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) @@ -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]