Skip to content

Commit 2ec8f11

Browse files
committed
Minor: improve Expr documentation
1 parent 7c08a6f commit 2ec8f11

File tree

2 files changed

+64
-20
lines changed

2 files changed

+64
-20
lines changed

datafusion/expr/src/expr.rs

+59-17
Original file line numberDiff line numberDiff line change
@@ -39,29 +39,56 @@ 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`], 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+
///
5364
/// ```
5465
/// # use datafusion_common::Column;
5566
/// # use datafusion_expr::{lit, col, Expr};
5667
/// let expr = col("c1");
5768
/// assert_eq!(expr, Expr::Column(Column::from_name("c1")));
5869
/// ```
5970
///
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+
///
6188
/// ```
6289
/// # use datafusion_expr::{lit, col, Operator, Expr};
90+
/// // Use the `+` operator to add two columns together
6391
/// let expr = col("c1") + col("c2");
64-
///
6592
/// assert!(matches!(expr, Expr::BinaryExpr { ..} ));
6693
/// if let Expr::BinaryExpr(binary_expr) = expr {
6794
/// assert_eq!(*binary_expr.left, col("c1"));
@@ -70,7 +97,9 @@ use sqlparser::ast::NullTreatment;
7097
/// }
7198
/// ```
7299
///
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+
///
74103
/// ```
75104
/// # use datafusion_common::ScalarValue;
76105
/// # use datafusion_expr::{lit, col, Operator, Expr};
@@ -85,19 +114,23 @@ use sqlparser::ast::NullTreatment;
85114
/// }
86115
/// ```
87116
///
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+
///
89120
/// ```
90121
/// # use arrow::datatypes::{DataType, Field, Schema};
91122
/// # use datafusion_common::{DFSchema, Column};
92123
/// # use datafusion_expr::Expr;
93-
///
124+
/// // Create a schema c1(int, c2 float)
94125
/// let arrow_schema = Schema::new(vec![
95126
/// Field::new("c1", DataType::Int32, false),
96127
/// Field::new("c2", DataType::Float64, false),
97128
/// ]);
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();
99132
///
100-
/// // Form a list of expressions for each item in the schema
133+
/// // Form a list of expressions for each column in the schema
101134
/// let exprs: Vec<_> = df_schema.iter()
102135
/// .map(Expr::from)
103136
/// .collect();
@@ -227,6 +260,7 @@ impl<'a> From<(Option<&'a TableReference>, &'a FieldRef)> for Expr {
227260
}
228261
}
229262

263+
/// UNNEST expression.
230264
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
231265
pub struct Unnest {
232266
pub expr: Box<Expr>,
@@ -434,9 +468,13 @@ pub enum GetFieldAccess {
434468
},
435469
}
436470

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
440478
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
441479
pub struct GetIndexedField {
442480
/// The expression to take the field from
@@ -703,7 +741,7 @@ pub fn find_df_window_func(name: &str) -> Option<WindowFunctionDefinition> {
703741
}
704742
}
705743

706-
// Exists expression.
744+
/// EXISTS expression
707745
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
708746
pub struct Exists {
709747
/// subquery that will produce a single column of data
@@ -719,6 +757,9 @@ impl Exists {
719757
}
720758
}
721759

760+
/// User Defined Aggregate Function
761+
///
762+
/// See [`udaf::AggregateUDF`] for more information.
722763
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
723764
pub struct AggregateUDF {
724765
/// The function
@@ -812,6 +853,7 @@ impl Placeholder {
812853
}
813854

814855
/// Grouping sets
856+
///
815857
/// See <https://www.postgresql.org/docs/current/queries-table-expressions.html#QUERIES-GROUPING-SETS>
816858
/// for Postgres definition.
817859
/// 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)