ProjectionExprs

Struct ProjectionExprs 

Source
pub struct ProjectionExprs { /* private fields */ }
Expand description

A collection of projection expressions.

This struct encapsulates multiple ProjectionExpr instances, representing a complete projection operation and provides methods to manipulate and analyze the projection as a whole.

Implementations§

Source§

impl ProjectionExprs

Source

pub fn new<I>(exprs: I) -> ProjectionExprs
where I: IntoIterator<Item = ProjectionExpr>,

Source

pub fn from_indices(indices: &[usize], schema: &Arc<Schema>) -> ProjectionExprs

Creates a ProjectionExpr from a list of column indices.

This is a convenience method for creating simple column-only projections, where each projection expression is a reference to a column in the input schema.

§Behavior
  • Ordering: the output projection preserves the exact order of indices provided in the input slice For example, [2, 0, 1] will produce projections for columns 2, 0, then 1 in that order
  • Duplicates: Duplicate indices are allowed and will create multiple projection expressions referencing the same source column For example, [0, 0] creates 2 separate projections both referencing column 0
§Panics

Panics if any index in indices is out of bounds for the provided schema.

§Example
use arrow::datatypes::{DataType, Field, Schema};
use datafusion_physical_expr::projection::ProjectionExprs;
use std::sync::Arc;

// Create a schema with three columns
let schema = Arc::new(Schema::new(vec![
    Field::new("a", DataType::Int32, false),
    Field::new("b", DataType::Utf8, false),
    Field::new("c", DataType::Float64, false),
]));

// Project columns at indices 2 and 0 (c and a) - ordering is preserved
let projection = ProjectionExprs::from_indices(&[2, 0], &schema);

// This creates: SELECT c@2 AS c, a@0 AS a
assert_eq!(projection.as_ref().len(), 2);
assert_eq!(projection.as_ref()[0].alias, "c");
assert_eq!(projection.as_ref()[1].alias, "a");

// Duplicate indices are allowed
let projection_with_dups = ProjectionExprs::from_indices(&[0, 0, 1], &schema);
assert_eq!(projection_with_dups.as_ref().len(), 3);
assert_eq!(projection_with_dups.as_ref()[0].alias, "a");
assert_eq!(projection_with_dups.as_ref()[1].alias, "a"); // duplicate
assert_eq!(projection_with_dups.as_ref()[2].alias, "b");
Source

pub fn iter(&self) -> impl Iterator<Item = &ProjectionExpr>

Returns an iterator over the projection expressions

Source

pub fn projection_mapping( &self, input_schema: &Arc<Schema>, ) -> Result<ProjectionMapping, DataFusionError>

Creates a ProjectionMapping from this projection

Source

pub fn expr_iter(&self) -> impl Iterator<Item = Arc<dyn PhysicalExpr>>

Iterate over a clone of the projection expressions.

Source

pub fn try_merge( &self, other: &ProjectionExprs, ) -> Result<ProjectionExprs, DataFusionError>

Apply another projection on top of this projection, returning the combined projection. For example, if this projection is SELECT c@2 AS x, b@1 AS y, a@0 as z and the other projection is SELECT x@0 + 1 AS c1, y@1 + z@2 as c2, we return a projection equivalent to SELECT c@2 + 1 AS c1, b@1 + a@0 as c2.

§Example
use datafusion_common::{Result, ScalarValue};
use datafusion_expr::Operator;
use datafusion_physical_expr::expressions::{BinaryExpr, Column, Literal};
use datafusion_physical_expr::projection::{ProjectionExpr, ProjectionExprs};
use std::sync::Arc;

fn main() -> Result<()> {
    // Example from the docstring:
    // Base projection: SELECT c@2 AS x, b@1 AS y, a@0 AS z
    let base = ProjectionExprs::new(vec![
        ProjectionExpr {
            expr: Arc::new(Column::new("c", 2)),
            alias: "x".to_string(),
        },
        ProjectionExpr {
            expr: Arc::new(Column::new("b", 1)),
            alias: "y".to_string(),
        },
        ProjectionExpr {
            expr: Arc::new(Column::new("a", 0)),
            alias: "z".to_string(),
        },
    ]);

    // Top projection: SELECT x@0 + 1 AS c1, y@1 + z@2 AS c2
    let top = ProjectionExprs::new(vec![
        ProjectionExpr {
            expr: Arc::new(BinaryExpr::new(
                Arc::new(Column::new("x", 0)),
                Operator::Plus,
                Arc::new(Literal::new(ScalarValue::Int32(Some(1)))),
            )),
            alias: "c1".to_string(),
        },
        ProjectionExpr {
            expr: Arc::new(BinaryExpr::new(
                Arc::new(Column::new("y", 1)),
                Operator::Plus,
                Arc::new(Column::new("z", 2)),
            )),
            alias: "c2".to_string(),
        },
    ]);

    // Expected result: SELECT c@2 + 1 AS c1, b@1 + a@0 AS c2
    let result = base.try_merge(&top)?;

    assert_eq!(result.as_ref().len(), 2);
    assert_eq!(result.as_ref()[0].alias, "c1");
    assert_eq!(result.as_ref()[1].alias, "c2");

    Ok(())
}
§Errors

This function returns an error if any expression in the other projection cannot be applied on top of this projection.

Source

pub fn column_indices(&self) -> Vec<usize>

Extract the column indices used in this projection. For example, for a projection SELECT a AS x, b + 1 AS y, where a is at index 0 and b is at index 1, this function would return [0, 1]. Repeated indices are returned only once, and the order is ascending.

Source

pub fn ordered_column_indices(&self) -> Vec<usize>

Extract the ordered column indices for a column-only projection.

This function assumes that all expressions in the projection are simple column references. It returns the column indices in the order they appear in the projection.

§Panics

Panics if any expression in the projection is not a simple column reference. This includes:

  • Computed expressions (e.g., a + 1, CAST(a AS INT))
  • Function calls (e.g., UPPER(name), SUM(amount))
  • Literals (e.g., 42, 'hello')
  • Complex nested expressions (e.g., CASE WHEN ... THEN ... END)
§Returns

A vector of column indices in projection order. Unlike column_indices(), this function:

  • Preserves the projection order (does not sort)
  • Preserves duplicates (does not deduplicate)
§Example

For a projection SELECT c, a, c where a is at index 0 and c is at index 2, this function would return [2, 0, 2].

Use column_indices() instead if the projection may contain non-column expressions or if you need a deduplicated sorted list.

Source

pub fn project_schema( &self, input_schema: &Schema, ) -> Result<Schema, DataFusionError>

Project a schema according to this projection. For example, for a projection SELECT a AS x, b + 1 AS y, where a is at index 0 and b is at index 1, if the input schema is [a: Int32, b: Int32, c: Int32], the output schema would be [x: Int32, y: Int32]. Fields’ metadata are preserved from the input schema.

Source

pub fn project_statistics( &self, stats: Statistics, input_schema: &Schema, ) -> Result<Statistics, DataFusionError>

Project statistics according to this projection. For example, for a projection SELECT a AS x, b + 1 AS y, where a is at index 0 and b is at index 1, if the input statistics has column statistics for columns a, b, and c, the output statistics would have column statistics for columns x and y.

Trait Implementations§

Source§

impl AsRef<[ProjectionExpr]> for ProjectionExprs

Source§

fn as_ref(&self) -> &[ProjectionExpr]

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Clone for ProjectionExprs

Source§

fn clone(&self) -> ProjectionExprs

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ProjectionExprs

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Display for ProjectionExprs

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl From<&[ProjectionExpr]> for ProjectionExprs

Source§

fn from(value: &[ProjectionExpr]) -> ProjectionExprs

Converts to this type from the input type.
Source§

impl From<Vec<ProjectionExpr>> for ProjectionExprs

Source§

fn from(value: Vec<ProjectionExpr>) -> ProjectionExprs

Converts to this type from the input type.
Source§

impl FromIterator<ProjectionExpr> for ProjectionExprs

Source§

fn from_iter<T>(exprs: T) -> ProjectionExprs
where T: IntoIterator<Item = ProjectionExpr>,

Creates a value from an iterator. Read more
Source§

impl<'a> IntoIterator for &'a ProjectionExprs

Source§

type Item = &'a ProjectionExpr

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, ProjectionExpr>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> <&'a ProjectionExprs as IntoIterator>::IntoIter

Creates an iterator from a value. Read more
Source§

impl IntoIterator for ProjectionExprs

Source§

type Item = ProjectionExpr

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<ProjectionExpr>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> <ProjectionExprs as IntoIterator>::IntoIter

Creates an iterator from a value. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> ErasedDestructor for T
where T: 'static,