@@ -39,29 +39,73 @@ use datafusion_common::{
3939} ;
4040use sqlparser:: ast:: NullTreatment ;
4141
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)`.
4543///
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`.
4964///
5065/// # Examples
5166///
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+ ///
5375/// ```
5476/// # use datafusion_common::Column;
5577/// # use datafusion_expr::{lit, col, Expr};
5678/// let expr = col("c1");
5779/// assert_eq!(expr, Expr::Column(Column::from_name("c1")));
5880/// ```
5981///
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+ ///
61105/// ```
62106/// # use datafusion_expr::{lit, col, Operator, Expr};
107+ /// // Use the `+` operator to add two columns together
63108/// let expr = col("c1") + col("c2");
64- ///
65109/// assert!(matches!(expr, Expr::BinaryExpr { ..} ));
66110/// if let Expr::BinaryExpr(binary_expr) = expr {
67111/// assert_eq!(*binary_expr.left, col("c1"));
@@ -70,12 +114,13 @@ use sqlparser::ast::NullTreatment;
70114/// }
71115/// ```
72116///
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+ ///
74120/// ```
75121/// # use datafusion_common::ScalarValue;
76122/// # use datafusion_expr::{lit, col, Operator, Expr};
77123/// let expr = col("c1").eq(lit(42_i32));
78- ///
79124/// assert!(matches!(expr, Expr::BinaryExpr { .. } ));
80125/// if let Expr::BinaryExpr(binary_expr) = expr {
81126/// assert_eq!(*binary_expr.left, col("c1"));
@@ -85,19 +130,23 @@ use sqlparser::ast::NullTreatment;
85130/// }
86131/// ```
87132///
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+ ///
89136/// ```
90137/// # use arrow::datatypes::{DataType, Field, Schema};
91138/// # use datafusion_common::{DFSchema, Column};
92139/// # use datafusion_expr::Expr;
93- ///
140+ /// // Create a schema c1(int, c2 float)
94141/// let arrow_schema = Schema::new(vec![
95142/// Field::new("c1", DataType::Int32, false),
96143/// Field::new("c2", DataType::Float64, false),
97144/// ]);
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();
99148///
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
101150/// let exprs: Vec<_> = df_schema.iter()
102151/// .map(Expr::from)
103152/// .collect();
@@ -227,6 +276,7 @@ impl<'a> From<(Option<&'a TableReference>, &'a FieldRef)> for Expr {
227276 }
228277}
229278
279+ /// UNNEST expression.
230280#[ derive( Clone , PartialEq , Eq , Hash , Debug ) ]
231281pub struct Unnest {
232282 pub expr : Box < Expr > ,
@@ -434,9 +484,13 @@ pub enum GetFieldAccess {
434484 } ,
435485}
436486
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
440494#[ derive( Clone , PartialEq , Eq , Hash , Debug ) ]
441495pub struct GetIndexedField {
442496 /// The expression to take the field from
@@ -712,7 +766,7 @@ pub fn find_df_window_func(name: &str) -> Option<WindowFunctionDefinition> {
712766 }
713767}
714768
715- // Exists expression.
769+ /// EXISTS expression
716770#[ derive( Clone , PartialEq , Eq , Hash , Debug ) ]
717771pub struct Exists {
718772 /// subquery that will produce a single column of data
@@ -728,6 +782,9 @@ impl Exists {
728782 }
729783}
730784
785+ /// User Defined Aggregate Function
786+ ///
787+ /// See [`udaf::AggregateUDF`] for more information.
731788#[ derive( Clone , PartialEq , Eq , Hash , Debug ) ]
732789pub struct AggregateUDF {
733790 /// The function
@@ -821,6 +878,7 @@ impl Placeholder {
821878}
822879
823880/// Grouping sets
881+ ///
824882/// See <https://www.postgresql.org/docs/current/queries-table-expressions.html#QUERIES-GROUPING-SETS>
825883/// for Postgres definition.
826884/// See <https://spark.apache.org/docs/latest/sql-ref-syntax-qry-select-groupby.html>
0 commit comments