Enum datafusion_expr::expr::Expr
source · [−]pub enum Expr {
Show 27 variants
Alias(Box<Expr>, String),
Column(Column),
ScalarVariable(DataType, Vec<String>),
Literal(ScalarValue),
BinaryExpr {
left: Box<Expr>,
op: Operator,
right: Box<Expr>,
},
Not(Box<Expr>),
IsNotNull(Box<Expr>),
IsNull(Box<Expr>),
Negative(Box<Expr>),
GetIndexedField {
expr: Box<Expr>,
key: ScalarValue,
},
Between {
expr: Box<Expr>,
negated: bool,
low: Box<Expr>,
high: Box<Expr>,
},
Case {
expr: Option<Box<Expr>>,
when_then_expr: Vec<(Box<Expr>, Box<Expr>)>,
else_expr: Option<Box<Expr>>,
},
Cast {
expr: Box<Expr>,
data_type: DataType,
},
TryCast {
expr: Box<Expr>,
data_type: DataType,
},
Sort {
expr: Box<Expr>,
asc: bool,
nulls_first: bool,
},
ScalarFunction {
fun: BuiltinScalarFunction,
args: Vec<Expr>,
},
ScalarUDF {
fun: Arc<ScalarUDF>,
args: Vec<Expr>,
},
AggregateFunction {
fun: AggregateFunction,
args: Vec<Expr>,
distinct: bool,
},
WindowFunction {
fun: WindowFunction,
args: Vec<Expr>,
partition_by: Vec<Expr>,
order_by: Vec<Expr>,
window_frame: Option<WindowFrame>,
},
AggregateUDF {
fun: Arc<AggregateUDF>,
args: Vec<Expr>,
},
InList {
expr: Box<Expr>,
list: Vec<Expr>,
negated: bool,
},
Exists {
subquery: Subquery,
negated: bool,
},
InSubquery {
expr: Box<Expr>,
subquery: Subquery,
negated: bool,
},
ScalarSubquery(Subquery),
Wildcard,
QualifiedWildcard {
qualifier: String,
},
GroupingSet(GroupingSet),
}
Expand description
Expr
is a central struct of DataFusion’s query API, and
represent logical expressions such as A + 1
, or CAST(c1 AS int)
.
An Expr
can compute its DataType
and nullability, and has functions for building up complex
expressions.
Examples
Create an expression c1
referring to column named “c1”
let expr = col("c1");
assert_eq!(expr, Expr::Column(Column::from_name("c1")));
Create the expression c1 + c2
to add columns “c1” and “c2” together
let expr = col("c1") + col("c2");
assert!(matches!(expr, Expr::BinaryExpr { ..} ));
if let Expr::BinaryExpr { left, right, op } = expr {
assert_eq!(*left, col("c1"));
assert_eq!(*right, col("c2"));
assert_eq!(op, Operator::Plus);
}
Create expression c1 = 42
to compare the value in column “c1” to the literal value 42
let expr = col("c1").eq(lit(42_i32));
assert!(matches!(expr, Expr::BinaryExpr { .. } ));
if let Expr::BinaryExpr { left, right, op } = expr {
assert_eq!(*left, col("c1"));
let scalar = ScalarValue::Int32(Some(42));
assert_eq!(*right, Expr::Literal(scalar));
assert_eq!(op, Operator::Eq);
}
Variants
Alias(Box<Expr>, String)
An expression with a specific name.
Column(Column)
A named reference to a qualified filed in a schema.
ScalarVariable(DataType, Vec<String>)
A named reference to a variable in a registry.
Literal(ScalarValue)
A constant value.
BinaryExpr
Fields
op: Operator
The comparison operator
A binary expression such as “age > 21”
Not(Box<Expr>)
Negation of an expression. The expression’s type must be a boolean to make sense.
IsNotNull(Box<Expr>)
Whether an expression is not Null. This expression is never null.
IsNull(Box<Expr>)
Whether an expression is Null. This expression is never null.
Negative(Box<Expr>)
arithmetic negation of an expression, the operand must be of a signed numeric data type
GetIndexedField
Fields
key: ScalarValue
The name of the field to take
Returns the field of a arrow::array::ListArray
or arrow::array::StructArray
by key
Between
Fields
negated: bool
Whether the expression is negated
Whether an expression is between a given range.
Case
Fields
The CASE expression is similar to a series of nested if/else and there are two forms that can be used. The first form consists of a series of boolean “when” expressions with corresponding “then” expressions, and an optional “else” expression.
CASE WHEN condition THEN result [WHEN …] [ELSE result] END
The second form uses a base expression and then a series of “when” clauses that match on a literal value.
CASE expression WHEN value THEN result [WHEN …] [ELSE result] END
Cast
Fields
data_type: DataType
The DataType
the expression will yield
Casts the expression to a given type and will return a runtime error if the expression cannot be cast. This expression is guaranteed to have a fixed type.
TryCast
Fields
data_type: DataType
The DataType
the expression will yield
Casts the expression to a given type and will return a null value if the expression cannot be cast. This expression is guaranteed to have a fixed type.
Sort
Fields
asc: bool
The direction of the sort
nulls_first: bool
Whether to put Nulls before all other data values
A sort expression, that can be used to sort values.
ScalarFunction
Fields
fun: BuiltinScalarFunction
The function
Represents the call of a built-in scalar function with a set of arguments.
ScalarUDF
Fields
Represents the call of a user-defined scalar function with arguments.
AggregateFunction
Fields
fun: AggregateFunction
Name of the function
distinct: bool
Whether this is a DISTINCT aggregation or not
Represents the call of an aggregate built-in function with arguments.
WindowFunction
Represents the call of a window function with arguments.
AggregateUDF
Fields
fun: Arc<AggregateUDF>
The function
aggregate function
InList
Fields
negated: bool
Whether the expression is negated
Returns whether the list contains the expr value.
Exists
Fields
subquery: Subquery
subquery that will produce a single column of data
negated: bool
Whether the expression is negated
EXISTS subquery
InSubquery
Fields
subquery: Subquery
subquery that will produce a single column of data to compare against
negated: bool
Whether the expression is negated
IN subquery
ScalarSubquery(Subquery)
Scalar subquery
Wildcard
Represents a reference to all fields in a schema.
QualifiedWildcard
Fields
qualifier: String
Represents a reference to all fields in a specific schema.
GroupingSet(GroupingSet)
List of grouping set expressions. Only valid in the context of an aggregate GROUP BY expression list
Implementations
sourceimpl Expr
impl Expr
sourcepub fn name(&self, input_schema: &DFSchema) -> Result<String>
pub fn name(&self, input_schema: &DFSchema) -> Result<String>
Returns the name of this expression based on datafusion_common::DFSchema.
This represents how a column with this expression is named when no alias is chosen
sourcepub fn variant_name(&self) -> &str
pub fn variant_name(&self) -> &str
Return String representation of the variant represented by self
Useful for non-rust based bindings
sourcepub fn modulus(self, other: Expr) -> Expr
pub fn modulus(self, other: Expr) -> Expr
Calculate the modulus of two expressions.
Return self % other
sourcepub fn in_list(self, list: Vec<Expr>, negated: bool) -> Expr
pub fn in_list(self, list: Vec<Expr>, negated: bool) -> Expr
Return self IN <list>
if negated
is false, otherwise
return self NOT IN <list>
.a
sourcepub fn is_not_null(self) -> Expr
pub fn is_not_null(self) -> Expr
Return `IsNotNull(Box(self))
sourcepub fn sort(self, asc: bool, nulls_first: bool) -> Expr
pub fn sort(self, asc: bool, nulls_first: bool) -> Expr
Create a sort expression from an existing expression.
let sort_expr = col("foo").sort(true, true); // SORT ASC NULLS_FIRST
pub fn try_into_col(&self) -> Result<Column>
Trait Implementations
sourceimpl ExprRewritable for Expr
impl ExprRewritable for Expr
sourcefn rewrite<R>(self, rewriter: &mut R) -> Result<Self> where
R: ExprRewriter<Self>,
fn rewrite<R>(self, rewriter: &mut R) -> Result<Self> where
R: ExprRewriter<Self>,
Performs a depth first walk of an expression and its children
to rewrite an expression, consuming self
producing a new
Expr
.
Implements a modified version of the visitor
pattern to
separate algorithms from the structure of the Expr
tree and
make it easier to write new, efficient expression
transformation algorithms.
For an expression tree such as
BinaryExpr (GT)
left: Column("foo")
right: Column("bar")
The nodes are visited using the following order
pre_visit(BinaryExpr(GT))
pre_visit(Column("foo"))
mutatate(Column("foo"))
pre_visit(Column("bar"))
mutate(Column("bar"))
mutate(BinaryExpr(GT))
If an Err result is returned, recursion is stopped immediately
If false
is returned on a call to pre_visit, no
children of that expression are visited, nor is mutate
called on that expression
sourceimpl ExprSchemable for Expr
impl ExprSchemable for Expr
sourcefn get_type<S: ExprSchema>(&self, schema: &S) -> Result<DataType>
fn get_type<S: ExprSchema>(&self, schema: &S) -> Result<DataType>
Returns the arrow::datatypes::DataType of the expression based on ExprSchema
Note: DFSchema implements ExprSchema.
Errors
This function errors when it is not possible to compute its
arrow::datatypes::DataType. This happens when e.g. the
expression refers to a column that does not exist in the
schema, or when the expression is incorrectly typed
(e.g. [utf8] + [bool]
).
sourcefn nullable<S: ExprSchema>(&self, input_schema: &S) -> Result<bool>
fn nullable<S: ExprSchema>(&self, input_schema: &S) -> Result<bool>
Returns the nullability of the expression based on ExprSchema.
Note: DFSchema implements ExprSchema.
Errors
This function errors when it is not possible to compute its nullability. This happens when the expression refers to a column that does not exist in the schema.
sourcefn to_field(&self, input_schema: &DFSchema) -> Result<DFField>
fn to_field(&self, input_schema: &DFSchema) -> Result<DFField>
Returns a arrow::datatypes::Field compatible with this expression.
sourcefn cast_to<S: ExprSchema>(
self,
cast_to_type: &DataType,
schema: &S
) -> Result<Expr>
fn cast_to<S: ExprSchema>(
self,
cast_to_type: &DataType,
schema: &S
) -> Result<Expr>
Wraps this expression in a cast to a target arrow::datatypes::DataType.
Errors
This function errors when it is impossible to cast the expression to the target arrow::datatypes::DataType.
sourceimpl ExprVisitable for Expr
impl ExprVisitable for Expr
sourcefn accept<V: ExpressionVisitor>(&self, visitor: V) -> Result<V>
fn accept<V: ExpressionVisitor>(&self, visitor: V) -> Result<V>
Performs a depth first walk of an expression and
its children, calling ExpressionVisitor::pre_visit
and
visitor.post_visit
.
Implements the visitor pattern to
separate expression algorithms from the structure of the
Expr
tree and make it easier to add new types of expressions
and algorithms that walk the tree.
For an expression tree such as
BinaryExpr (GT)
left: Column("foo")
right: Column("bar")
The nodes are visited using the following order
pre_visit(BinaryExpr(GT))
pre_visit(Column("foo"))
post_visit(Column("foo"))
pre_visit(Column("bar"))
post_visit(Column("bar"))
post_visit(BinaryExpr(GT))
If an Err result is returned, recursion is stopped immediately
If Recursion::Stop
is returned on a call to pre_visit, no
children of that expression are visited, nor is post_visit
called on that expression
sourceimpl PartialEq<Expr> for Expr
impl PartialEq<Expr> for Expr
sourceimpl PartialOrd<Expr> for Expr
impl PartialOrd<Expr> for Expr
sourcefn partial_cmp(&self, other: &Self) -> Option<Ordering>
fn partial_cmp(&self, other: &Self) -> Option<Ordering>
This method returns an ordering between self
and other
values if one exists. Read more
1.0.0 · sourcefn lt(&self, other: &Rhs) -> bool
fn lt(&self, other: &Rhs) -> bool
This method tests less than (for self
and other
) and is used by the <
operator. Read more
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
impl StructuralPartialEq for Expr
Auto Trait Implementations
impl !RefUnwindSafe for Expr
impl Send for Expr
impl Sync for Expr
impl Unpin for Expr
impl !UnwindSafe for Expr
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more