@@ -39,29 +39,73 @@ use datafusion_common::{
39
39
} ;
40
40
use sqlparser:: ast:: NullTreatment ;
41
41
42
- /// `Expr` is a central struct of DataFusion's query API, and
43
- /// represent logical expressions such as `A + 1`, or `CAST(c1 AS
44
- /// int)`.
42
+ /// Represents logical expressions such as `A + 1`, or `CAST(c1 AS int)`.
45
43
///
46
- /// An `Expr` can compute its [DataType]
47
- /// and nullability, and has functions for building up complex
48
- /// expressions.
44
+ /// For example the expression `A + 1` will be represented as
45
+ ///
46
+ ///```text
47
+ /// BinaryExpr {
48
+ /// left: Expr::Column("A"),
49
+ /// op: Operator::Plus,
50
+ /// right: Expr::Literal(ScalarValue::Int32(Some(1)))
51
+ /// }
52
+ /// ```
53
+ ///
54
+ /// # Creating Expressions
55
+ ///
56
+ /// `Expr`s can be created directly, but it is often easier and less verbose to
57
+ /// use the fluent APIs in [`crate::expr_fn`] such as [`col`] and [`lit`], or
58
+ /// methods such as [`Expr::alias`], [`Expr::cast_to`], and [`Expr::Like`]).
59
+ ///
60
+ /// # Schema Access
61
+ ///
62
+ /// See [`ExprSchemable::get_type`] to access the [`DataType`] and nullability
63
+ /// of an `Expr`.
49
64
///
50
65
/// # Examples
51
66
///
52
- /// ## Create an expression `c1` referring to column named "c1"
67
+ /// ## Column references and literals
68
+ ///
69
+ /// [`Expr::Column`] refer to the values of columns and are often created with
70
+ /// the [`col`] function. For example to create an expression `c1` referring to
71
+ /// column named "c1":
72
+ ///
73
+ /// [`col`]: crate::expr_fn::col
74
+ ///
53
75
/// ```
54
76
/// # use datafusion_common::Column;
55
77
/// # use datafusion_expr::{lit, col, Expr};
56
78
/// let expr = col("c1");
57
79
/// assert_eq!(expr, Expr::Column(Column::from_name("c1")));
58
80
/// ```
59
81
///
60
- /// ## Create the expression `c1 + c2` to add columns "c1" and "c2" together
82
+ /// [`Expr::Literal`] refer to literal, or constant, values. These are created
83
+ /// with the [`lit`] function. For example to create an expression `42`:
84
+ ///
85
+ /// [`lit`]: crate::lit
86
+ ///
87
+ /// ```
88
+ /// # use datafusion_common::{Column, ScalarValue};
89
+ /// # use datafusion_expr::{lit, col, Expr};
90
+ /// // All literals are strongly typed in DataFusion. To make an `i64` 42:
91
+ /// let expr = lit(42i64);
92
+ /// assert_eq!(expr, Expr::Literal(ScalarValue::Int64(Some(42))));
93
+ /// // To make a (typed) NULL:
94
+ /// let expr = Expr::Literal(ScalarValue::Int64(None));
95
+ /// // to make an (untyped) NULL (the optimizer will coerce this to the correct type):
96
+ /// let expr = lit(ScalarValue::Null);
97
+ /// ```
98
+ ///
99
+ /// ## Binary Expressions
100
+ ///
101
+ /// Exprs implement traits that allow easy to understand construction of more
102
+ /// complex expresions. For example, to create `c1 + c2` to add columns "c1" and
103
+ /// "c2" together
104
+ ///
61
105
/// ```
62
106
/// # use datafusion_expr::{lit, col, Operator, Expr};
107
+ /// // Use the `+` operator to add two columns together
63
108
/// let expr = col("c1") + col("c2");
64
- ///
65
109
/// assert!(matches!(expr, Expr::BinaryExpr { ..} ));
66
110
/// if let Expr::BinaryExpr(binary_expr) = expr {
67
111
/// assert_eq!(*binary_expr.left, col("c1"));
@@ -70,12 +114,13 @@ use sqlparser::ast::NullTreatment;
70
114
/// }
71
115
/// ```
72
116
///
73
- /// ## Create expression `c1 = 42` to compare the value in column "c1" to the literal value `42`
117
+ /// The expression `c1 = 42` to compares the value in column "c1" to the
118
+ /// literal value `42`:
119
+ ///
74
120
/// ```
75
121
/// # use datafusion_common::ScalarValue;
76
122
/// # use datafusion_expr::{lit, col, Operator, Expr};
77
123
/// let expr = col("c1").eq(lit(42_i32));
78
- ///
79
124
/// assert!(matches!(expr, Expr::BinaryExpr { .. } ));
80
125
/// if let Expr::BinaryExpr(binary_expr) = expr {
81
126
/// assert_eq!(*binary_expr.left, col("c1"));
@@ -85,19 +130,23 @@ use sqlparser::ast::NullTreatment;
85
130
/// }
86
131
/// ```
87
132
///
88
- /// ## Return a list of [`Expr::Column`] from a schema's columns
133
+ /// Here is how to implement the equivalent of `SELECT *` to select all
134
+ /// [`Expr::Column`] from a [`DFSchema`]'s columns:
135
+ ///
89
136
/// ```
90
137
/// # use arrow::datatypes::{DataType, Field, Schema};
91
138
/// # use datafusion_common::{DFSchema, Column};
92
139
/// # use datafusion_expr::Expr;
93
- ///
140
+ /// // Create a schema c1(int, c2 float)
94
141
/// let arrow_schema = Schema::new(vec![
95
142
/// Field::new("c1", DataType::Int32, false),
96
143
/// Field::new("c2", DataType::Float64, false),
97
144
/// ]);
98
- /// let df_schema = DFSchema::try_from_qualified_schema("t1", &arrow_schema).unwrap();
145
+ /// // DFSchema is a an Arrow schema with optional relation name
146
+ /// let df_schema = DFSchema::try_from_qualified_schema("t1", &arrow_schema)
147
+ /// .unwrap();
99
148
///
100
- /// // Form a list of expressions for each item in the schema
149
+ /// // Form Vec<Expr> with an expression for each column in the schema
101
150
/// let exprs: Vec<_> = df_schema.iter()
102
151
/// .map(Expr::from)
103
152
/// .collect();
@@ -227,6 +276,7 @@ impl<'a> From<(Option<&'a TableReference>, &'a FieldRef)> for Expr {
227
276
}
228
277
}
229
278
279
+ /// UNNEST expression.
230
280
#[ derive( Clone , PartialEq , Eq , Hash , Debug ) ]
231
281
pub struct Unnest {
232
282
pub expr : Box < Expr > ,
@@ -434,9 +484,13 @@ pub enum GetFieldAccess {
434
484
} ,
435
485
}
436
486
437
- /// Returns the field of a [`arrow::array::ListArray`] or
438
- /// [`arrow::array::StructArray`] by `key`. See [`GetFieldAccess`] for
439
- /// details.
487
+ /// Returns the field of a [`ListArray`] or
488
+ /// [`StructArray`] by `key`.
489
+ ///
490
+ /// See [`GetFieldAccess`] for details.
491
+ ///
492
+ /// [`ListArray`]: arrow::array::ListArray
493
+ /// [`StructArray`]: arrow::array::StructArray
440
494
#[ derive( Clone , PartialEq , Eq , Hash , Debug ) ]
441
495
pub struct GetIndexedField {
442
496
/// The expression to take the field from
@@ -712,7 +766,7 @@ pub fn find_df_window_func(name: &str) -> Option<WindowFunctionDefinition> {
712
766
}
713
767
}
714
768
715
- // Exists expression.
769
+ /// EXISTS expression
716
770
#[ derive( Clone , PartialEq , Eq , Hash , Debug ) ]
717
771
pub struct Exists {
718
772
/// subquery that will produce a single column of data
@@ -728,6 +782,9 @@ impl Exists {
728
782
}
729
783
}
730
784
785
+ /// User Defined Aggregate Function
786
+ ///
787
+ /// See [`udaf::AggregateUDF`] for more information.
731
788
#[ derive( Clone , PartialEq , Eq , Hash , Debug ) ]
732
789
pub struct AggregateUDF {
733
790
/// The function
@@ -821,6 +878,7 @@ impl Placeholder {
821
878
}
822
879
823
880
/// Grouping sets
881
+ ///
824
882
/// See <https://www.postgresql.org/docs/current/queries-table-expressions.html#QUERIES-GROUPING-SETS>
825
883
/// for Postgres definition.
826
884
/// See <https://spark.apache.org/docs/latest/sql-ref-syntax-qry-select-groupby.html>
0 commit comments