Skip to content

Commit bf6567d

Browse files
committed
Minor: improve Expr documentation
1 parent ae10754 commit bf6567d

File tree

2 files changed

+72
-21
lines changed

2 files changed

+72
-21
lines changed

datafusion/expr/src/expr.rs

+67-18
Original file line numberDiff line numberDiff line change
@@ -39,29 +39,64 @@ use datafusion_common::{
3939
};
4040
use 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
42+
/// `Expr` represent logical expressions such as `A + 1`, or `CAST(c1 AS
4443
/// int)`.
4544
///
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`.
4955
///
5056
/// # Examples
5157
///
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+
///
5366
/// ```
5467
/// # use datafusion_common::Column;
5568
/// # use datafusion_expr::{lit, col, Expr};
5669
/// let expr = col("c1");
5770
/// assert_eq!(expr, Expr::Column(Column::from_name("c1")));
5871
/// ```
5972
///
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+
///
6196
/// ```
6297
/// # use datafusion_expr::{lit, col, Operator, Expr};
98+
/// // Use the `+` operator to add two columns together
6399
/// let expr = col("c1") + col("c2");
64-
///
65100
/// assert!(matches!(expr, Expr::BinaryExpr { ..} ));
66101
/// if let Expr::BinaryExpr(binary_expr) = expr {
67102
/// assert_eq!(*binary_expr.left, col("c1"));
@@ -70,12 +105,13 @@ use sqlparser::ast::NullTreatment;
70105
/// }
71106
/// ```
72107
///
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+
///
74111
/// ```
75112
/// # use datafusion_common::ScalarValue;
76113
/// # use datafusion_expr::{lit, col, Operator, Expr};
77114
/// let expr = col("c1").eq(lit(42_i32));
78-
///
79115
/// assert!(matches!(expr, Expr::BinaryExpr { .. } ));
80116
/// if let Expr::BinaryExpr(binary_expr) = expr {
81117
/// assert_eq!(*binary_expr.left, col("c1"));
@@ -85,19 +121,23 @@ use sqlparser::ast::NullTreatment;
85121
/// }
86122
/// ```
87123
///
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+
///
89127
/// ```
90128
/// # use arrow::datatypes::{DataType, Field, Schema};
91129
/// # use datafusion_common::{DFSchema, Column};
92130
/// # use datafusion_expr::Expr;
93-
///
131+
/// // Create a schema c1(int, c2 float)
94132
/// let arrow_schema = Schema::new(vec![
95133
/// Field::new("c1", DataType::Int32, false),
96134
/// Field::new("c2", DataType::Float64, false),
97135
/// ]);
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();
99139
///
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
101141
/// let exprs: Vec<_> = df_schema.iter()
102142
/// .map(Expr::from)
103143
/// .collect();
@@ -227,6 +267,7 @@ impl<'a> From<(Option<&'a TableReference>, &'a FieldRef)> for Expr {
227267
}
228268
}
229269

270+
/// UNNEST expression.
230271
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
231272
pub struct Unnest {
232273
pub expr: Box<Expr>,
@@ -434,9 +475,13 @@ pub enum GetFieldAccess {
434475
},
435476
}
436477

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
440485
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
441486
pub struct GetIndexedField {
442487
/// The expression to take the field from
@@ -703,7 +748,7 @@ pub fn find_df_window_func(name: &str) -> Option<WindowFunctionDefinition> {
703748
}
704749
}
705750

706-
// Exists expression.
751+
/// EXISTS expression
707752
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
708753
pub struct Exists {
709754
/// subquery that will produce a single column of data
@@ -719,6 +764,9 @@ impl Exists {
719764
}
720765
}
721766

767+
/// User Defined Aggregate Function
768+
///
769+
/// See [`udaf::AggregateUDF`] for more information.
722770
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
723771
pub struct AggregateUDF {
724772
/// The function
@@ -812,6 +860,7 @@ impl Placeholder {
812860
}
813861

814862
/// Grouping sets
863+
///
815864
/// See <https://www.postgresql.org/docs/current/queries-table-expressions.html#QUERIES-GROUPING-SETS>
816865
/// for Postgres definition.
817866
/// See <https://spark.apache.org/docs/latest/sql-ref-syntax-qry-select-groupby.html>

datafusion/sql/src/unparser/expr.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ use std::{fmt::Display, vec};
2020

2121
use arrow_array::{Date32Array, Date64Array};
2222
use arrow_schema::DataType;
23+
use sqlparser::ast::{
24+
self, Expr as AstExpr, Function, FunctionArg, Ident, UnaryOperator,
25+
};
26+
2327
use datafusion_common::{
2428
internal_datafusion_err, internal_err, not_impl_err, plan_err, Column, Result,
2529
ScalarValue,
@@ -28,9 +32,6 @@ use datafusion_expr::{
2832
expr::{Alias, Exists, InList, ScalarFunction, Sort, WindowFunction},
2933
Between, BinaryExpr, Case, Cast, Expr, GroupingSet, Like, Operator, TryCast,
3034
};
31-
use sqlparser::ast::{
32-
self, Expr as AstExpr, Function, FunctionArg, Ident, UnaryOperator,
33-
};
3435

3536
use super::Unparser;
3637

@@ -931,6 +932,7 @@ mod tests {
931932

932933
use arrow::datatypes::{Field, Schema};
933934
use arrow_schema::DataType::Int8;
935+
934936
use datafusion_common::TableReference;
935937
use datafusion_expr::{
936938
case, col, cube, exists,

0 commit comments

Comments
 (0)