micromegas_analytics/dfext/
predicate.rs

1use datafusion::{
2    arrow::datatypes::SchemaRef,
3    catalog::Session,
4    common::DFSchema,
5    logical_expr::utils::conjunction,
6    physical_plan::{PhysicalExpr, expressions},
7    prelude::*,
8};
9use std::sync::Arc;
10
11// from datafusion/datafusion-examples/examples/advanced_parquet_index.rs
12/// Converts a list of DataFusion `Expr` filters into a single `PhysicalExpr` predicate.
13pub fn filters_to_predicate(
14    schema: SchemaRef,
15    state: &dyn Session,
16    filters: &[Expr],
17) -> datafusion::error::Result<Arc<dyn PhysicalExpr>> {
18    let df_schema = DFSchema::try_from(schema)?;
19    let predicate = conjunction(filters.to_vec());
20    let predicate = predicate
21        .map(|predicate| state.create_physical_expr(predicate, &df_schema))
22        .transpose()?
23        // if there are no filters, use a literal true to have a predicate
24        // that always evaluates to true we can pass to the index
25        .unwrap_or_else(|| expressions::lit(true));
26
27    Ok(predicate)
28}