Skip to main content

create_physical_expr

Function create_physical_expr 

Source
pub fn create_physical_expr(
    e: &Expr,
    input_dfschema: &DFSchema,
    execution_props: &ExecutionProps,
    planning_ctx: &PhysicalPlanningContext,
) -> Result<Arc<dyn PhysicalExpr>>
Expand description

PhysicalExpr evaluate DataFusion expressions such as A + 1, or CAST(c1 AS int).

PhysicalExpr are the physical counterpart to Expr used in logical planning, and can be evaluated directly on a RecordBatch. They are normally created from Expr by a PhysicalPlanner and can be created directly using create_physical_expr.

A Physical expression knows its type, nullability and how to evaluate itself.

§Example: Create PhysicalExpr from Expr

// For a logical expression `a = 1`, we can create a physical expression
let expr = col("a").eq(lit(1));
// To create a PhysicalExpr we need 1. a schema
let schema = Schema::new(vec![Field::new("a", DataType::Int32, true)]);
let df_schema = DFSchema::try_from(schema).unwrap();
// 2. ExecutionProps
let props = ExecutionProps::new();
// We can now create a PhysicalExpr. Expressions with no scalar
// subqueries use an empty `PhysicalPlanningContext`:
let physical_expr =
    create_physical_expr(&expr, &df_schema, &props, &PhysicalPlanningContext::default())
        .unwrap();

§Example: Executing a PhysicalExpr to obtain ColumnarValue

// Given a PhysicalExpr, for `a = 1` we can evaluate it against a RecordBatch like this:
let physical_expr =
    create_physical_expr(&expr, &df_schema, &props, &PhysicalPlanningContext::default())
        .unwrap();
// Input of [1,2,3]
let input_batch = RecordBatch::try_from_iter(vec![
  ("a", Arc::new(Int32Array::from(vec![1, 2, 3])) as _)
]).unwrap();
// The result is a ColumnarValue (either an Array or a Scalar)
let result = physical_expr.evaluate(&input_batch).unwrap();
// In this case, a BooleanArray with the result of the comparison
let ColumnarValue::Array(arr) = result else {
 panic!("Expected an array")
};
assert_eq!(arr.as_boolean(), &BooleanArray::from(vec![true, false, false]));

Create a physical expression from a logical expression (Expr).

§Arguments

  • e - The logical expression
  • input_dfschema - The DataFusion schema for the input, used to resolve Column references to qualified or unqualified fields by name.
  • execution_props - Per-execution properties such as the query start time.
  • planning_ctx - The PhysicalPlanningContext used to resolve Expr::ScalarSubquery and Expr::LambdaVariable nodes. The physical planner threads the subquery index map and shared results container from its ScalarSubqueryExec construction into calls to create_physical_expr; the lambda variable qualifiers are added by this function itself as it descends into lambda bodies. Callers creating physical expressions outside of physical planning should pass &PhysicalPlanningContext::default(); converting a scalar subquery then returns a planning error.