pub enum LogicalPlan {
Show 27 variants Projection(Projection), Filter(Filter), Window(Window), Aggregate(Aggregate), Sort(Sort), Join(Join), CrossJoin(CrossJoin), Repartition(Repartition), Union(Union), TableScan(TableScan), EmptyRelation(EmptyRelation), Subquery(Subquery), SubqueryAlias(SubqueryAlias), Limit(Limit), Statement(Statement), Values(Values), Explain(Explain), Analyze(Analyze), Extension(Extension), Distinct(Distinct), Prepare(Prepare), Dml(DmlStatement), Ddl(DdlStatement), Copy(CopyTo), DescribeTable(DescribeTable), Unnest(Unnest), RecursiveQuery(RecursiveQuery),
}
Expand description

A LogicalPlan represents the different types of relational operators (such as Projection, Filter, etc) and can be created by the SQL query planner and the DataFrame API.

A LogicalPlan represents transforming an input relation (table) to an output relation (table) with a (potentially) different schema. A plan represents a dataflow tree where data flows from leaves up to the root to produce the query result.

Variants§

§

Projection(Projection)

Evaluates an arbitrary list of expressions (essentially a SELECT with an expression list) on its input.

§

Filter(Filter)

Filters rows from its input that do not match an expression (essentially a WHERE clause with a predicate expression).

Semantically, <predicate> is evaluated for each row of the input; If the value of <predicate> is true, the input row is passed to the output. If the value of <predicate> is false (or null), the row is discarded.

§

Window(Window)

Windows input based on a set of window spec and window function (e.g. SUM or RANK). This is used to implement SQL window functions, and the OVER clause.

§

Aggregate(Aggregate)

Aggregates its input based on a set of grouping and aggregate expressions (e.g. SUM). This is used to implement SQL aggregates and GROUP BY.

§

Sort(Sort)

Sorts its input according to a list of sort expressions. This is used to implement SQL ORDER BY

§

Join(Join)

Join two logical plans on one or more join columns. This is used to implement SQL JOIN

§

CrossJoin(CrossJoin)

Apply Cross Join to two logical plans. This is used to implement SQL CROSS JOIN

§

Repartition(Repartition)

Repartitions the input based on a partitioning scheme. This is used to add parallelism and is sometimes referred to as an “exchange” operator in other systems

§

Union(Union)

Union multiple inputs with the same schema into a single output stream. This is used to implement SQL UNION [ALL] and INTERSECT [ALL].

§

TableScan(TableScan)

Produces rows from a TableSource, used to implement SQL FROM tables or views.

§

EmptyRelation(EmptyRelation)

Produces no rows: An empty relation with an empty schema that produces 0 or 1 row. This is used to implement SQL SELECT that has no values in the FROM clause.

§

Subquery(Subquery)

Produces the output of running another query. This is used to implement SQL subqueries

§

SubqueryAlias(SubqueryAlias)

Aliased relation provides, or changes, the name of a relation.

§

Limit(Limit)

Skip some number of rows, and then fetch some number of rows.

§

Statement(Statement)

A DataFusion Statement such as SET VARIABLE or START TRANSACTION

§

Values(Values)

Values expression. See Postgres VALUES documentation for more details. This is used to implement SQL such as VALUES (1, 2), (3, 4)

§

Explain(Explain)

Produces a relation with string representations of various parts of the plan. This is used to implement SQL EXPLAIN.

§

Analyze(Analyze)

Runs the input, and prints annotated physical plan as a string with execution metric. This is used to implement SQL EXPLAIN ANALYZE.

§

Extension(Extension)

Extension operator defined outside of DataFusion. This is used to extend DataFusion with custom relational operations that

§

Distinct(Distinct)

Remove duplicate rows from the input. This is used to implement SQL SELECT DISTINCT ....

§

Prepare(Prepare)

Prepare a statement and find any bind parameters (e.g. ?). This is used to implement SQL-prepared statements.

§

Dml(DmlStatement)

Data Manipulaton Language (DML): Insert / Update / Delete

§

Ddl(DdlStatement)

Data Definition Language (DDL): CREATE / DROP TABLES / VIEWS / SCHEMAS

§

Copy(CopyTo)

COPY TO for writing plan results to files

§

DescribeTable(DescribeTable)

Describe the schema of the table. This is used to implement the SQL DESCRIBE command from MySQL.

§

Unnest(Unnest)

Unnest a column that contains a nested list type such as an ARRAY. This is used to implement SQL UNNEST

§

RecursiveQuery(RecursiveQuery)

A variadic query (e.g. “Recursive CTEs”)

Implementations§

source§

impl LogicalPlan

source

pub fn schema(&self) -> &DFSchemaRef

Get a reference to the logical plan’s schema

source

pub fn fallback_normalize_schemas(&self) -> Vec<&DFSchema>

Used for normalizing columns, as the fallback schemas to the main schema of the plan.

source

pub fn explain_schema() -> SchemaRef

Returns the (fixed) output schema for explain plans

source

pub fn describe_schema() -> Schema

Returns the (fixed) output schema for DESCRIBE plans

source

pub fn expressions(self: &LogicalPlan) -> Vec<Expr>

Returns all expressions (non-recursively) evaluated by the current logical plan node. This does not include expressions in any children

The returned expressions do not necessarily represent or even contributed to the output schema of this node. For example, LogicalPlan::Filter returns the filter expression even though the output of a Filter has the same columns as the input.

The expressions do contain all the columns that are used by this plan, so if there are columns not referenced by these expressions then DataFusion’s optimizer attempts to optimize them away.

source

pub fn all_out_ref_exprs(self: &LogicalPlan) -> Vec<Expr>

Returns all the out reference(correlated) expressions (recursively) in the current logical plan nodes and all its descendant nodes.

source

pub fn inspect_expressions<F, E>(self: &LogicalPlan, f: F) -> Result<(), E>
where F: FnMut(&Expr) -> Result<(), E>,

Calls f on all expressions (non-recursively) in the current logical plan node. This does not include expressions in any children.

source

pub fn inputs(&self) -> Vec<&LogicalPlan>

returns all inputs of this LogicalPlan node. Does not include inputs to inputs, or subqueries.

source

pub fn using_columns(&self) -> Result<Vec<HashSet<Column>>, DataFusionError>

returns all Using join columns in a logical plan

source

pub fn head_output_expr(&self) -> Result<Option<Expr>>

returns the first output expression of this LogicalPlan node.

source

pub fn with_new_inputs(&self, inputs: &[LogicalPlan]) -> Result<LogicalPlan>

👎Deprecated since 35.0.0: please use with_new_exprs instead

Returns a copy of this LogicalPlan with the new inputs

source

pub fn with_new_exprs( &self, expr: Vec<Expr>, inputs: Vec<LogicalPlan> ) -> Result<LogicalPlan>

Returns a new LogicalPlan based on self with inputs and expressions replaced.

The exprs correspond to the same order of expressions returned by Self::expressions. This function is used by optimizers to rewrite plans using the following pattern:

let new_inputs = optimize_children(..., plan, props);

// get the plans expressions to optimize
let exprs = plan.expressions();

// potentially rewrite plan expressions
let rewritten_exprs = rewrite_exprs(exprs);

// create new plan using rewritten_exprs in same position
let new_plan = plan.new_with_exprs(rewritten_exprs, new_inputs);
source

pub fn with_param_values( self, param_values: impl Into<ParamValues> ) -> Result<LogicalPlan>

Replaces placeholder param values (like $1, $2) in LogicalPlan with the specified param_values.

LogicalPlan::Prepare are converted to their inner logical plan for execution.

§Example
use datafusion_common::ScalarValue;
// Build SELECT * FROM t1 WHRERE id = $1
let plan = table_scan(Some("t1"), &schema, None).unwrap()
    .filter(col("id").eq(placeholder("$1"))).unwrap()
    .build().unwrap();

assert_eq!(
  "Filter: t1.id = $1\
  \n  TableScan: t1",
  plan.display_indent().to_string()
);

// Fill in the parameter $1 with a literal 3
let plan = plan.with_param_values(vec![
  ScalarValue::from(3i32) // value at index 0 --> $1
]).unwrap();

assert_eq!(
   "Filter: t1.id = Int32(3)\
   \n  TableScan: t1",
   plan.display_indent().to_string()
 );

// Note you can also used named parameters
// Build SELECT * FROM t1 WHRERE id = $my_param
let plan = table_scan(Some("t1"), &schema, None).unwrap()
    .filter(col("id").eq(placeholder("$my_param"))).unwrap()
    .build().unwrap()
    // Fill in the parameter $my_param with a literal 3
    .with_param_values(vec![
      ("my_param", ScalarValue::from(3i32)),
    ]).unwrap();

assert_eq!(
   "Filter: t1.id = Int32(3)\
   \n  TableScan: t1",
   plan.display_indent().to_string()
 );
source

pub fn max_rows(self: &LogicalPlan) -> Option<usize>

Returns the maximum number of rows that this plan can output, if known.

If None, the plan can return any number of rows. If Some(n) then the plan can return at most n rows but may return fewer.

source§

impl LogicalPlan

source

pub fn replace_params_with_values( &self, param_values: &ParamValues ) -> Result<LogicalPlan>

Return a LogicalPlan with all placeholders (e.g $1 $2, …) replaced with corresponding values provided in params_values

See Self::with_param_values for examples and usage

source

pub fn get_parameter_types( &self ) -> Result<HashMap<String, Option<DataType>>, DataFusionError>

Walk the logical plan, find any Placeholder tokens, and return a map of their IDs and DataTypes

source§

impl LogicalPlan

source

pub fn display_indent(&self) -> impl Display + '_

Return a formatable structure that produces a single line per node.

§Example
Projection: employee.id
   Filter: employee.state Eq Utf8(\"CO\")\
      CsvScan: employee projection=Some([0, 3])
use arrow::datatypes::{Field, Schema, DataType};
use datafusion_expr::{lit, col, LogicalPlanBuilder, logical_plan::table_scan};
let schema = Schema::new(vec![
    Field::new("id", DataType::Int32, false),
]);
let plan = table_scan(Some("t1"), &schema, None).unwrap()
    .filter(col("id").eq(lit(5))).unwrap()
    .build().unwrap();

// Format using display_indent
let display_string = format!("{}", plan.display_indent());

assert_eq!("Filter: t1.id = Int32(5)\n  TableScan: t1",
            display_string);
source

pub fn display_indent_schema(&self) -> impl Display + '_

Return a formatable structure that produces a single line per node that includes the output schema. For example:

Projection: employee.id [id:Int32]\
   Filter: employee.state = Utf8(\"CO\") [id:Int32, state:Utf8]\
     TableScan: employee projection=[0, 3] [id:Int32, state:Utf8]";
use arrow::datatypes::{Field, Schema, DataType};
use datafusion_expr::{lit, col, LogicalPlanBuilder, logical_plan::table_scan};
let schema = Schema::new(vec![
    Field::new("id", DataType::Int32, false),
]);
let plan = table_scan(Some("t1"), &schema, None).unwrap()
    .filter(col("id").eq(lit(5))).unwrap()
    .build().unwrap();

// Format using display_indent_schema
let display_string = format!("{}", plan.display_indent_schema());

assert_eq!("Filter: t1.id = Int32(5) [id:Int32]\
            \n  TableScan: t1 [id:Int32]",
            display_string);
source

pub fn display_graphviz(&self) -> impl Display + '_

Return a formatable structure that produces lines meant for graphical display using the DOT language. This format can be visualized using software from graphviz

This currently produces two graphs – one with the basic structure, and one with additional details such as schema.

use arrow::datatypes::{Field, Schema, DataType};
use datafusion_expr::{lit, col, LogicalPlanBuilder, logical_plan::table_scan};
let schema = Schema::new(vec![
    Field::new("id", DataType::Int32, false),
]);
let plan = table_scan(Some("t1"), &schema, None).unwrap()
    .filter(col("id").eq(lit(5))).unwrap()
    .build().unwrap();

// Format using display_graphviz
let graphviz_string = format!("{}", plan.display_graphviz());

If graphviz string is saved to a file such as /tmp/example.dot, the following commands can be used to render it as a pdf:

  dot -Tpdf < /tmp/example.dot  > /tmp/example.pdf
source

pub fn display(&self) -> impl Display + '_

Return a formatable structure with the a human readable description of this LogicalPlan node per node, not including children. For example:

Projection: id
use arrow::datatypes::{Field, Schema, DataType};
use datafusion_expr::{lit, col, LogicalPlanBuilder, logical_plan::table_scan};
let schema = Schema::new(vec![
    Field::new("id", DataType::Int32, false),
]);
let plan = table_scan(Some("t1"), &schema, None).unwrap()
    .build().unwrap();

// Format using display
let display_string = format!("{}", plan.display());

assert_eq!("TableScan: t1", display_string);

Trait Implementations§

source§

impl Clone for LogicalPlan

source§

fn clone(&self) -> LogicalPlan

Returns a copy 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 LogicalPlan

source§

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

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

impl Hash for LogicalPlan

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl PartialEq for LogicalPlan

source§

fn eq(&self, other: &LogicalPlan) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl ToStringifiedPlan for LogicalPlan

source§

fn to_stringified(&self, plan_type: PlanType) -> StringifiedPlan

Create a stringified plan with the specified type
source§

impl TreeNode for LogicalPlan

source§

fn visit<V: TreeNodeVisitor<Node = Self>>( &self, visitor: &mut V ) -> Result<TreeNodeRecursion>

To use, define a struct that implements the trait TreeNodeVisitor and then invoke LogicalPlan::visit.

For example, for a logical plan like:

Projection: id
   Filter: state Eq Utf8(\"CO\")\
      CsvScan: employee.csv projection=Some([0, 3])";

The sequence of visit operations would be:

visitor.pre_visit(Projection)
visitor.pre_visit(Filter)
visitor.pre_visit(CsvScan)
visitor.post_visit(CsvScan)
visitor.post_visit(Filter)
visitor.post_visit(Projection)
source§

fn apply<F: FnMut(&Self) -> Result<TreeNodeRecursion>>( &self, f: &mut F ) -> Result<TreeNodeRecursion>

Applies f to the node and its children. f is applied in a pre-order way, and it is controlled by TreeNodeRecursion, which means result of the f on a node can cause an early return. Read more
source§

fn apply_children<F: FnMut(&Self) -> Result<TreeNodeRecursion>>( &self, f: &mut F ) -> Result<TreeNodeRecursion>

Apply the closure F to the node’s children.
source§

fn map_children<F>(self, f: F) -> Result<Transformed<Self>>
where F: FnMut(Self) -> Result<Transformed<Self>>,

Apply transform F to the node’s children. Note that the transform F might have a direction (pre-order or post-order).
source§

fn rewrite<R>( self, rewriter: &mut R ) -> Result<Transformed<Self>, DataFusionError>
where R: TreeNodeRewriter<Node = Self>,

Implements the visitor pattern for recursively transforming TreeNodes. Read more
source§

fn transform<F>(self, f: &F) -> Result<Transformed<Self>, DataFusionError>
where F: Fn(Self) -> Result<Transformed<Self>, DataFusionError>,

Convenience utility for writing optimizer rules: Recursively apply the given function f to the tree in a bottom-up (post-order) fashion. When f does not apply to a given node, it is left unchanged.
source§

fn transform_down<F>(self, f: &F) -> Result<Transformed<Self>, DataFusionError>
where F: Fn(Self) -> Result<Transformed<Self>, DataFusionError>,

Convenience utility for writing optimizer rules: Recursively apply the given function f to a node and then to its children (pre-order traversal). When f does not apply to a given node, it is left unchanged.
source§

fn transform_down_mut<F>( self, f: &mut F ) -> Result<Transformed<Self>, DataFusionError>
where F: FnMut(Self) -> Result<Transformed<Self>, DataFusionError>,

Convenience utility for writing optimizer rules: Recursively apply the given mutable function f to a node and then to its children (pre-order traversal). When f does not apply to a given node, it is left unchanged.
source§

fn transform_up<F>(self, f: &F) -> Result<Transformed<Self>, DataFusionError>
where F: Fn(Self) -> Result<Transformed<Self>, DataFusionError>,

Convenience utility for writing optimizer rules: Recursively apply the given function f to all children of a node, and then to the node itself (post-order traversal). When f does not apply to a given node, it is left unchanged.
source§

fn transform_up_mut<F>( self, f: &mut F ) -> Result<Transformed<Self>, DataFusionError>
where F: FnMut(Self) -> Result<Transformed<Self>, DataFusionError>,

Convenience utility for writing optimizer rules: Recursively apply the given mutable function f to all children of a node, and then to the node itself (post-order traversal). When f does not apply to a given node, it is left unchanged.
source§

fn transform_down_up<FD, FU>( self, f_down: &mut FD, f_up: &mut FU ) -> Result<Transformed<Self>, DataFusionError>
where FD: FnMut(Self) -> Result<Transformed<Self>, DataFusionError>, FU: FnMut(Self) -> Result<Transformed<Self>, DataFusionError>,

Transforms the tree using f_down while traversing the tree top-down (pre-order), and using f_up while traversing the tree bottom-up (post-order). Read more
source§

impl Eq for LogicalPlan

source§

impl StructuralPartialEq for LogicalPlan

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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
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> ToOwned for T
where T: Clone,

§

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, U> TryFrom<U> for T
where U: Into<T>,

§

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>,

§

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.