Skip to content

Commit

Permalink
make build_predicate_expression public
Browse files Browse the repository at this point in the history
  • Loading branch information
adriangb committed Oct 9, 2024
1 parent c4405df commit 807e295
Showing 1 changed file with 20 additions and 32 deletions.
52 changes: 20 additions & 32 deletions datafusion/core/src/physical_optimizer/pruning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -526,26 +526,14 @@ impl PruningPredicate {
///
/// See the struct level documentation on [`PruningPredicate`] for more
/// details.
pub fn try_new(expr: Arc<dyn PhysicalExpr>, schema: SchemaRef) -> Result<Self> {
pub fn try_new(
expr: Arc<dyn PhysicalExpr>,
schema: SchemaRef,
) -> Result<Self> {
let unhandled_hook = Arc::new(ConstantUnhandledPredicateHook::new(Arc::new(
phys_expr::Literal::new(ScalarValue::Boolean(Some(true))),
)));
Self::try_new_with_unhandled_hook(expr, schema, unhandled_hook)
}

/// Try to create a new instance of [`PruningPredicate`] with a custom
/// unhandled hook.
///
/// This is the same as [`PruningPredicate::try_new`] but allows for a custom
/// hook to be used when a predicate can not be handled by DataFusion's
/// transformation rules or is referencing a column that is not in the schema.
///
/// By default, a constant `true` is returned for unhandled predicates.
pub fn try_new_with_unhandled_hook(
expr: Arc<dyn PhysicalExpr>,
schema: SchemaRef,
unhandled_hook: Arc<dyn UnhandledPredicateHook>,
) -> Result<Self> {
// build predicate expression once
let mut required_columns = RequiredColumns::new();
let predicate_expr = build_predicate_expression(
Expand Down Expand Up @@ -1363,11 +1351,13 @@ const MAX_LIST_VALUE_SIZE_REWRITE: usize = 20;
/// 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.
///
/// Any predicates that can not be translated will be passed to `unhandled_hook`.
///
/// Returns the pruning predicate as an [`PhysicalExpr`]
///
/// Notice: Does not handle [`phys_expr::InListExpr`] greater than 20, which will be rewritten to TRUE
fn build_predicate_expression(
/// Notice: Does not handle [`phys_expr::InListExpr`] greater than 20, which will fall back to calling `unhandled_hook`
pub fn build_predicate_expression(
expr: &Arc<dyn PhysicalExpr>,
schema: &Schema,
required_columns: &mut RequiredColumns,
Expand Down Expand Up @@ -3465,20 +3455,18 @@ mod tests {
if let Some(column) =
left.as_any().downcast_ref::<phys_expr::Column>()
{
if column.name() == "b"
&& right
.as_any()
.downcast_ref::<phys_expr::Literal>()
.is_some()
{
let new_column =
Arc::new(phys_expr::Column::new("c", column.index()))
as _;
return Arc::new(phys_expr::BinaryExpr::new(
new_column,
*expr.op(),
right.clone(),
));
if column.name() == "b" {
if right.as_any().downcast_ref::<phys_expr::Literal>().is_some()
{
let new_column =
Arc::new(phys_expr::Column::new("c", column.index()))
as _;
return Arc::new(phys_expr::BinaryExpr::new(
new_column,
*expr.op(),
right.clone(),
));
}
}
}
}
Expand Down

0 comments on commit 807e295

Please sign in to comment.