@@ -39,29 +39,64 @@ 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
42
+ /// `Expr` represent logical expressions such as `A + 1`, or `CAST(c1 AS
44
43
/// int)`.
45
44
///
46
- /// An `Expr` can compute its [DataType]
47
- /// and nullability, and has functions for building up complex
48
- /// expressions.
45
+ /// # Creating Expressions
46
+ ///
47
+ /// `Expr`s can be created directly, but it is often easier and less verbose to
48
+ /// use the fluent APIs in [`crate::expr_fn`] such as [`col`] and [`lit`], or
49
+ /// methods such as [`Expr::alias`], [`Expr::cast_to`], and [`Expr::Like`]).
50
+ ///
51
+ /// # Schema Access
52
+ ///
53
+ /// See [`ExprSchemable::get_type`] to access the [`DataType`] and nullability
54
+ /// of an `Expr`.
49
55
///
50
56
/// # Examples
51
57
///
52
- /// ## Create an expression `c1` referring to column named "c1"
58
+ /// ## Column references and literals
59
+ ///
60
+ /// [`Expr::Column`] refer to the values of columns and are often created with
61
+ /// the [`col`] function. For example to create an expression `c1` referring to
62
+ /// column named "c1":
63
+ ///
64
+ /// [`col`]: crate::expr_fn::col
65
+ ///
53
66
/// ```
54
67
/// # use datafusion_common::Column;
55
68
/// # use datafusion_expr::{lit, col, Expr};
56
69
/// let expr = col("c1");
57
70
/// assert_eq!(expr, Expr::Column(Column::from_name("c1")));
58
71
/// ```
59
72
///
60
- /// ## Create the expression `c1 + c2` to add columns "c1" and "c2" together
73
+ /// [`Expr::Literal`] refer to literal, or constant, values. These are created
74
+ /// with the [`lit`] function. For example to create an expression `42`:
75
+ ///
76
+ /// [`lit`]: crate::lit
77
+ ///
78
+ /// ```
79
+ /// # use datafusion_common::{Column, ScalarValue};
80
+ /// # use datafusion_expr::{lit, col, Expr};
81
+ /// // All literals are strongly typed in DataFusion. To make an `i64` 42:
82
+ /// let expr = lit(42i64);
83
+ /// assert_eq!(expr, Expr::Literal(ScalarValue::Int64(Some(42))));
84
+ /// // To make a (typed) NULL:
85
+ /// let expr = Expr::Literal(ScalarValue::Int64(None));
86
+ /// // to make an (untyped) NULL (the optimizer will coerce this to the correct type):
87
+ /// let expr = lit(ScalarValue::Null);
88
+ /// ```
89
+ ///
90
+ /// ## Binary Expressions
91
+ ///
92
+ /// Exprs implement traits that allow easy to understand construction of more
93
+ /// complex expresions. For example, to create `c1 + c2` to add columns "c1" and
94
+ /// "c2" together
95
+ ///
61
96
/// ```
62
97
/// # use datafusion_expr::{lit, col, Operator, Expr};
98
+ /// // Use the `+` operator to add two columns together
63
99
/// let expr = col("c1") + col("c2");
64
- ///
65
100
/// assert!(matches!(expr, Expr::BinaryExpr { ..} ));
66
101
/// if let Expr::BinaryExpr(binary_expr) = expr {
67
102
/// assert_eq!(*binary_expr.left, col("c1"));
@@ -70,12 +105,13 @@ use sqlparser::ast::NullTreatment;
70
105
/// }
71
106
/// ```
72
107
///
73
- /// ## Create expression `c1 = 42` to compare the value in column "c1" to the literal value `42`
108
+ /// The expression `c1 = 42` to compares the value in column "c1" to the
109
+ /// literal value `42`:
110
+ ///
74
111
/// ```
75
112
/// # use datafusion_common::ScalarValue;
76
113
/// # use datafusion_expr::{lit, col, Operator, Expr};
77
114
/// let expr = col("c1").eq(lit(42_i32));
78
- ///
79
115
/// assert!(matches!(expr, Expr::BinaryExpr { .. } ));
80
116
/// if let Expr::BinaryExpr(binary_expr) = expr {
81
117
/// assert_eq!(*binary_expr.left, col("c1"));
@@ -85,19 +121,23 @@ use sqlparser::ast::NullTreatment;
85
121
/// }
86
122
/// ```
87
123
///
88
- /// ## Return a list of [`Expr::Column`] from a schema's columns
124
+ /// Here is how to implement the equivalent of `SELECT *` to select all
125
+ /// [`Expr::Column`] from a [`DFSchema`]'s columns:
126
+ ///
89
127
/// ```
90
128
/// # use arrow::datatypes::{DataType, Field, Schema};
91
129
/// # use datafusion_common::{DFSchema, Column};
92
130
/// # use datafusion_expr::Expr;
93
- ///
131
+ /// // Create a schema c1(int, c2 float)
94
132
/// let arrow_schema = Schema::new(vec![
95
133
/// Field::new("c1", DataType::Int32, false),
96
134
/// Field::new("c2", DataType::Float64, false),
97
135
/// ]);
98
- /// let df_schema = DFSchema::try_from_qualified_schema("t1", &arrow_schema).unwrap();
136
+ /// // DFSchema is a an Arrow schema with optional relation name
137
+ /// let df_schema = DFSchema::try_from_qualified_schema("t1", &arrow_schema)
138
+ /// .unwrap();
99
139
///
100
- /// // Form a list of expressions for each item in the schema
140
+ /// // Form Vec<Expr> with an expression for each column in the schema
101
141
/// let exprs: Vec<_> = df_schema.iter()
102
142
/// .map(Expr::from)
103
143
/// .collect();
@@ -227,6 +267,7 @@ impl<'a> From<(Option<&'a TableReference>, &'a FieldRef)> for Expr {
227
267
}
228
268
}
229
269
270
+ /// UNNEST expression.
230
271
#[ derive( Clone , PartialEq , Eq , Hash , Debug ) ]
231
272
pub struct Unnest {
232
273
pub expr : Box < Expr > ,
@@ -434,9 +475,13 @@ pub enum GetFieldAccess {
434
475
} ,
435
476
}
436
477
437
- /// Returns the field of a [`arrow::array::ListArray`] or
438
- /// [`arrow::array::StructArray`] by `key`. See [`GetFieldAccess`] for
439
- /// details.
478
+ /// Returns the field of a [`ListArray`] or
479
+ /// [`StructArray`] by `key`.
480
+ ///
481
+ /// See [`GetFieldAccess`] for details.
482
+ ///
483
+ /// [`ListArray`]: arrow::array::ListArray
484
+ /// [`StructArray`]: arrow::array::StructArray
440
485
#[ derive( Clone , PartialEq , Eq , Hash , Debug ) ]
441
486
pub struct GetIndexedField {
442
487
/// The expression to take the field from
@@ -703,7 +748,7 @@ pub fn find_df_window_func(name: &str) -> Option<WindowFunctionDefinition> {
703
748
}
704
749
}
705
750
706
- // Exists expression.
751
+ /// EXISTS expression
707
752
#[ derive( Clone , PartialEq , Eq , Hash , Debug ) ]
708
753
pub struct Exists {
709
754
/// subquery that will produce a single column of data
@@ -719,6 +764,9 @@ impl Exists {
719
764
}
720
765
}
721
766
767
+ /// User Defined Aggregate Function
768
+ ///
769
+ /// See [`udaf::AggregateUDF`] for more information.
722
770
#[ derive( Clone , PartialEq , Eq , Hash , Debug ) ]
723
771
pub struct AggregateUDF {
724
772
/// The function
@@ -812,6 +860,7 @@ impl Placeholder {
812
860
}
813
861
814
862
/// Grouping sets
863
+ ///
815
864
/// See <https://www.postgresql.org/docs/current/queries-table-expressions.html#QUERIES-GROUPING-SETS>
816
865
/// for Postgres definition.
817
866
/// See <https://spark.apache.org/docs/latest/sql-ref-syntax-qry-select-groupby.html>
0 commit comments