datafusion_expr_common/placement.rs
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership
3// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements
4
5//! Expression placement information for optimization decisions.
6
7/// Describes where an expression should be placed in the query plan for
8/// optimal execution. This is used by optimizers to make decisions about
9/// expression placement, such as whether to push expressions down through
10/// projections.
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
12pub enum ExpressionPlacement {
13 /// A constant literal value.
14 Literal,
15 /// A simple column reference.
16 Column,
17 /// A cheap expression that can be pushed to leaf nodes in the plan.
18 /// Examples include `get_field` for struct field access.
19 /// Pushing these expressions down in the plan can reduce data early
20 /// at low compute cost.
21 /// See [`ExpressionPlacement::should_push_to_leaves`] for details.
22 MoveTowardsLeafNodes,
23 /// An expensive expression that should stay where it is in the plan.
24 /// Examples include complex scalar functions or UDFs.
25 KeepInPlace,
26}
27
28impl ExpressionPlacement {
29 /// Returns true if the expression can be pushed down to leaf nodes
30 /// in the query plan.
31 ///
32 /// This returns true for:
33 /// - [`ExpressionPlacement::Column`]: Simple column references can be pushed down. They do no compute and do not increase or
34 /// decrease the amount of data being processed.
35 /// A projection that reduces the number of columns can eliminate unnecessary data early,
36 /// but this method only considers one expression at a time, not a projection as a whole.
37 /// - [`ExpressionPlacement::MoveTowardsLeafNodes`]: Cheap expressions can be pushed down to leaves to take advantage of
38 /// early computation and potential optimizations at the data source level.
39 /// For example `struct_col['field']` is cheap to compute (just an Arc clone of the nested array for `'field'`)
40 /// and thus can reduce data early in the plan at very low compute cost.
41 /// It may even be possible to eliminate the expression entirely if the data source can project only the needed field
42 /// (as e.g. Parquet can).
43 pub fn should_push_to_leaves(&self) -> bool {
44 matches!(
45 self,
46 ExpressionPlacement::Column | ExpressionPlacement::MoveTowardsLeafNodes
47 )
48 }
49}