@@ -39,29 +39,56 @@ 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`], 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
+ ///
53
64
/// ```
54
65
/// # use datafusion_common::Column;
55
66
/// # use datafusion_expr::{lit, col, Expr};
56
67
/// let expr = col("c1");
57
68
/// assert_eq!(expr, Expr::Column(Column::from_name("c1")));
58
69
/// ```
59
70
///
60
- /// ## Create the expression `c1 + c2` to add columns "c1" and "c2" together
71
+ /// [`Expr::Literal]` refer to literal, or constant, values. These are created
72
+ /// with the [`lit`] function. For example to create an expression `42`:
73
+ ///
74
+ /// ```
75
+ /// # use datafusion_common::{Column, ScalarValue};
76
+ /// # use datafusion_expr::{lit, col, Expr};
77
+ /// // All literals are strongly typed in DataFusion. To make an `i64` 42:
78
+ /// let expr = lit(42i64);
79
+ /// assert_eq!(expr, Expr::Literal(ScalarValue::Int64(Some(42))));
80
+ /// ```
81
+ ///
82
+ /// ## Binary Expressions
83
+ ///
84
+ /// Exprs implement traits that allow easy to understand construction of more
85
+ /// complex expresions. For example, to create `c1 + c2` to add columns "c1" and
86
+ /// "c2" together
87
+ ///
61
88
/// ```
62
89
/// # use datafusion_expr::{lit, col, Operator, Expr};
90
+ /// // Use the `+` operator to add two columns together
63
91
/// let expr = col("c1") + col("c2");
64
- ///
65
92
/// assert!(matches!(expr, Expr::BinaryExpr { ..} ));
66
93
/// if let Expr::BinaryExpr(binary_expr) = expr {
67
94
/// assert_eq!(*binary_expr.left, col("c1"));
@@ -70,7 +97,9 @@ use sqlparser::ast::NullTreatment;
70
97
/// }
71
98
/// ```
72
99
///
73
- /// ## Create expression `c1 = 42` to compare the value in column "c1" to the literal value `42`
100
+ /// The expression `c1 = 42` to compares the value in column "c1" to the
101
+ /// literal value `42`:
102
+ ///
74
103
/// ```
75
104
/// # use datafusion_common::ScalarValue;
76
105
/// # use datafusion_expr::{lit, col, Operator, Expr};
@@ -85,19 +114,23 @@ use sqlparser::ast::NullTreatment;
85
114
/// }
86
115
/// ```
87
116
///
88
- /// ## Return a list of [`Expr::Column`] from a schema's columns
117
+ /// Here is how to implement the equivalent of `SELECT *` (select all
118
+ /// [`Expr::Column`] from a [`DFSchema`]'s columns):
119
+ ///
89
120
/// ```
90
121
/// # use arrow::datatypes::{DataType, Field, Schema};
91
122
/// # use datafusion_common::{DFSchema, Column};
92
123
/// # use datafusion_expr::Expr;
93
- ///
124
+ /// // Create a schema c1(int, c2 float)
94
125
/// let arrow_schema = Schema::new(vec![
95
126
/// Field::new("c1", DataType::Int32, false),
96
127
/// Field::new("c2", DataType::Float64, false),
97
128
/// ]);
98
- /// let df_schema = DFSchema::try_from_qualified_schema("t1", &arrow_schema).unwrap();
129
+ /// // DFSchema is a an Arrow schema with optional relation name
130
+ /// let df_schema = DFSchema::try_from_qualified_schema("t1", &arrow_schema)
131
+ /// .unwrap();
99
132
///
100
- /// // Form a list of expressions for each item in the schema
133
+ /// // Form a list of expressions for each column in the schema
101
134
/// let exprs: Vec<_> = df_schema.iter()
102
135
/// .map(Expr::from)
103
136
/// .collect();
@@ -227,6 +260,7 @@ impl<'a> From<(Option<&'a TableReference>, &'a FieldRef)> for Expr {
227
260
}
228
261
}
229
262
263
+ /// UNNEST expression.
230
264
#[ derive( Clone , PartialEq , Eq , Hash , Debug ) ]
231
265
pub struct Unnest {
232
266
pub expr : Box < Expr > ,
@@ -434,9 +468,13 @@ pub enum GetFieldAccess {
434
468
} ,
435
469
}
436
470
437
- /// Returns the field of a [`arrow::array::ListArray`] or
438
- /// [`arrow::array::StructArray`] by `key`. See [`GetFieldAccess`] for
439
- /// details.
471
+ /// Returns the field of a [`ListArray`] or
472
+ /// [`StructArray`] by `key`.
473
+ ///
474
+ /// See [`GetFieldAccess`] for details.
475
+ ///
476
+ /// [`ListArray`]: arrow::array::ListArray
477
+ /// [`StructArray`]: arrow::array::StructArray
440
478
#[ derive( Clone , PartialEq , Eq , Hash , Debug ) ]
441
479
pub struct GetIndexedField {
442
480
/// The expression to take the field from
@@ -703,7 +741,7 @@ pub fn find_df_window_func(name: &str) -> Option<WindowFunctionDefinition> {
703
741
}
704
742
}
705
743
706
- // Exists expression.
744
+ /// EXISTS expression
707
745
#[ derive( Clone , PartialEq , Eq , Hash , Debug ) ]
708
746
pub struct Exists {
709
747
/// subquery that will produce a single column of data
@@ -719,6 +757,9 @@ impl Exists {
719
757
}
720
758
}
721
759
760
+ /// User Defined Aggregate Function
761
+ ///
762
+ /// See [`udaf::AggregateUDF`] for more information.
722
763
#[ derive( Clone , PartialEq , Eq , Hash , Debug ) ]
723
764
pub struct AggregateUDF {
724
765
/// The function
@@ -812,6 +853,7 @@ impl Placeholder {
812
853
}
813
854
814
855
/// Grouping sets
856
+ ///
815
857
/// See <https://www.postgresql.org/docs/current/queries-table-expressions.html#QUERIES-GROUPING-SETS>
816
858
/// for Postgres definition.
817
859
/// See <https://spark.apache.org/docs/latest/sql-ref-syntax-qry-select-groupby.html>
0 commit comments