Skip to content

Commit 2439979

Browse files
alambfindepi
andauthored
Rename TypeSignature::NullAry --> TypeSignature::Nullary and improve comments (#13817)
* Rename `TypeSignature::NullAry` --> `TypeSignature::Nullary` and improve comments * Apply suggestions from code review Co-authored-by: Piotr Findeisen <[email protected]> * improve docs --------- Co-authored-by: Piotr Findeisen <[email protected]>
1 parent 46101f3 commit 2439979

File tree

5 files changed

+62
-30
lines changed

5 files changed

+62
-30
lines changed

datafusion/expr-common/src/signature.rs

Lines changed: 57 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ pub enum Volatility {
7474
/// adds a cast such as `cos(CAST int_column AS DOUBLE)` during planning.
7575
///
7676
/// # Data Types
77+
///
78+
/// ## Timestamps
79+
///
7780
/// Types to match are represented using Arrow's [`DataType`]. [`DataType::Timestamp`] has an optional variable
7881
/// timezone specification. To specify a function can handle a timestamp with *ANY* timezone, use
7982
/// the [`TIMEZONE_WILDCARD`]. For example:
@@ -93,60 +96,89 @@ pub enum Volatility {
9396
pub enum TypeSignature {
9497
/// One or more arguments of a common type out of a list of valid types.
9598
///
99+
/// For functions that take no arguments (e.g. `random()` see [`TypeSignature::Nullary`]).
100+
///
96101
/// # Examples
97-
/// A function such as `concat` is `Variadic(vec![DataType::Utf8, DataType::LargeUtf8])`
102+
///
103+
/// A function such as `concat` is `Variadic(vec![DataType::Utf8,
104+
/// DataType::LargeUtf8])`
98105
Variadic(Vec<DataType>),
99106
/// The acceptable signature and coercions rules to coerce arguments to this
100107
/// signature are special for this function. If this signature is specified,
101108
/// DataFusion will call `ScalarUDFImpl::coerce_types` to prepare argument types.
102109
UserDefined,
103110
/// One or more arguments with arbitrary types
104111
VariadicAny,
105-
/// Fixed number of arguments of an arbitrary but equal type out of a list of valid types.
112+
/// One or more arguments of an arbitrary but equal type out of a list of valid types.
106113
///
107114
/// # Examples
115+
///
108116
/// 1. A function of one argument of f64 is `Uniform(1, vec![DataType::Float64])`
109117
/// 2. A function of one argument of f64 or f32 is `Uniform(1, vec![DataType::Float32, DataType::Float64])`
110118
Uniform(usize, Vec<DataType>),
111-
/// Exact number of arguments of an exact type
119+
/// One or more arguments with exactly the specified types in order.
120+
///
121+
/// For functions that take no arguments (e.g. `random()`) use [`TypeSignature::Nullary`].
112122
Exact(Vec<DataType>),
113-
/// The number of arguments that can be coerced to in order
123+
/// One or more arguments belonging to the [`TypeSignatureClass`], in order.
124+
///
114125
/// For example, `Coercible(vec![logical_float64()])` accepts
115126
/// arguments like `vec![DataType::Int32]` or `vec![DataType::Float32]`
116-
/// since i32 and f32 can be casted to f64
127+
/// since i32 and f32 can be cast to f64
128+
///
129+
/// For functions that take no arguments (e.g. `random()`) see [`TypeSignature::Nullary`].
117130
Coercible(Vec<TypeSignatureClass>),
118-
/// The arguments will be coerced to a single type based on the comparison rules.
119-
/// For example, i32 and i64 has coerced type Int64.
131+
/// One or more arguments that can be "compared"
132+
///
133+
/// Each argument will be coerced to a single type based on comparison rules.
134+
/// For example a function called with `i32` and `i64` has coerced type `Int64` so
135+
/// each argument will be coerced to `Int64` before the function is invoked.
120136
///
121137
/// Note:
122-
/// - If compares with numeric and string, numeric is preferred for numeric string cases. For example, nullif('2', 1) has coerced types Int64.
138+
/// - If compares with numeric and string, numeric is preferred for numeric string cases. For example, `nullif('2', 1)` has coerced types `Int64`.
123139
/// - If the result is Null, it will be coerced to String (Utf8View).
140+
/// - See [`comparison_coercion`] for more details.
141+
/// - For functions that take no arguments (e.g. `random()` see [`TypeSignature::Nullary`]).
124142
///
125-
/// See `comparison_coercion_numeric` for more details.
143+
/// [`comparison_coercion`]: crate::type_coercion::binary::comparison_coercion
126144
Comparable(usize),
127-
/// Fixed number of arguments of arbitrary types, number should be larger than 0
145+
/// One or more arguments of arbitrary types.
146+
///
147+
/// For functions that take no arguments (e.g. `random()`) use [`TypeSignature::Nullary`].
128148
Any(usize),
129-
/// Matches exactly one of a list of [`TypeSignature`]s. Coercion is attempted to match
130-
/// the signatures in order, and stops after the first success, if any.
149+
/// Matches exactly one of a list of [`TypeSignature`]s.
150+
///
151+
/// Coercion is attempted to match the signatures in order, and stops after
152+
/// the first success, if any.
131153
///
132154
/// # Examples
133-
/// Function `make_array` takes 0 or more arguments with arbitrary types, its `TypeSignature`
155+
///
156+
/// Since `make_array` takes 0 or more arguments with arbitrary types, its `TypeSignature`
134157
/// is `OneOf(vec![Any(0), VariadicAny])`.
135158
OneOf(Vec<TypeSignature>),
136-
/// Specifies Signatures for array functions
159+
/// A function that has an [`ArrayFunctionSignature`]
137160
ArraySignature(ArrayFunctionSignature),
138-
/// Fixed number of arguments of numeric types.
161+
/// One or more arguments of numeric types.
162+
///
139163
/// See [`NativeType::is_numeric`] to know which type is considered numeric
140164
///
141-
/// [`NativeType::is_numeric`]: datafusion_common
165+
/// For functions that take no arguments (e.g. `random()`) use [`TypeSignature::Nullary`].
166+
///
167+
/// [`NativeType::is_numeric`]: datafusion_common::types::NativeType::is_numeric
142168
Numeric(usize),
143-
/// Fixed number of arguments of all the same string types.
169+
/// One or arguments of all the same string types.
170+
///
144171
/// The precedence of type from high to low is Utf8View, LargeUtf8 and Utf8.
145172
/// Null is considered as `Utf8` by default
146173
/// Dictionary with string value type is also handled.
174+
///
175+
/// For example, if a function is called with (utf8, large_utf8), all
176+
/// arguments will be coerced to `LargeUtf8`
177+
///
178+
/// For functions that take no arguments (e.g. `random()` use [`TypeSignature::Nullary`]).
147179
String(usize),
148-
/// Zero argument
149-
NullAry,
180+
/// No arguments
181+
Nullary,
150182
}
151183

152184
impl TypeSignature {
@@ -243,7 +275,7 @@ impl Display for ArrayFunctionSignature {
243275
impl TypeSignature {
244276
pub fn to_string_repr(&self) -> Vec<String> {
245277
match self {
246-
TypeSignature::NullAry => {
278+
TypeSignature::Nullary => {
247279
vec!["NullAry()".to_string()]
248280
}
249281
TypeSignature::Variadic(types) => {
@@ -302,7 +334,7 @@ impl TypeSignature {
302334
pub fn supports_zero_argument(&self) -> bool {
303335
match &self {
304336
TypeSignature::Exact(vec) => vec.is_empty(),
305-
TypeSignature::NullAry => true,
337+
TypeSignature::Nullary => true,
306338
TypeSignature::OneOf(types) => types
307339
.iter()
308340
.any(|type_sig| type_sig.supports_zero_argument()),
@@ -368,7 +400,7 @@ impl TypeSignature {
368400
// TODO: Implement for other types
369401
TypeSignature::Any(_)
370402
| TypeSignature::Comparable(_)
371-
| TypeSignature::NullAry
403+
| TypeSignature::Nullary
372404
| TypeSignature::VariadicAny
373405
| TypeSignature::ArraySignature(_)
374406
| TypeSignature::UserDefined => vec![],
@@ -502,7 +534,7 @@ impl Signature {
502534

503535
pub fn nullary(volatility: Volatility) -> Self {
504536
Signature {
505-
type_signature: TypeSignature::NullAry,
537+
type_signature: TypeSignature::Nullary,
506538
volatility,
507539
}
508540
}
@@ -579,10 +611,10 @@ mod tests {
579611
TypeSignature::Exact(vec![]),
580612
TypeSignature::OneOf(vec![
581613
TypeSignature::Exact(vec![DataType::Int8]),
582-
TypeSignature::NullAry,
614+
TypeSignature::Nullary,
583615
TypeSignature::Uniform(1, vec![DataType::Int8]),
584616
]),
585-
TypeSignature::NullAry,
617+
TypeSignature::Nullary,
586618
];
587619

588620
for case in positive_cases {

datafusion/expr-common/src/type_coercion/binary.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,7 @@ pub fn comparison_coercion(lhs_type: &DataType, rhs_type: &DataType) -> Option<D
642642
.or_else(|| struct_coercion(lhs_type, rhs_type))
643643
}
644644

645-
// Similar to comparison_coercion but prefer numeric if compares with numeric and string
645+
/// Similar to [`comparison_coercion`] but prefer numeric if compares with numeric and string
646646
pub fn comparison_coercion_numeric(
647647
lhs_type: &DataType,
648648
rhs_type: &DataType,

datafusion/expr/src/type_coercion/functions.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ fn is_well_supported_signature(type_signature: &TypeSignature) -> bool {
207207
| TypeSignature::String(_)
208208
| TypeSignature::Coercible(_)
209209
| TypeSignature::Any(_)
210-
| TypeSignature::NullAry
210+
| TypeSignature::Nullary
211211
| TypeSignature::Comparable(_)
212212
)
213213
}
@@ -715,7 +715,7 @@ fn get_valid_types(
715715
}
716716
}
717717
},
718-
TypeSignature::NullAry => {
718+
TypeSignature::Nullary => {
719719
if !current_types.is_empty() {
720720
return plan_err!(
721721
"The function expected zero argument but received {}",

datafusion/functions-aggregate/src/count.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ impl Count {
123123
pub fn new() -> Self {
124124
Self {
125125
signature: Signature::one_of(
126-
vec![TypeSignature::VariadicAny, TypeSignature::NullAry],
126+
vec![TypeSignature::VariadicAny, TypeSignature::Nullary],
127127
Volatility::Immutable,
128128
),
129129
}

datafusion/functions-nested/src/make_array.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ impl MakeArray {
6464
pub fn new() -> Self {
6565
Self {
6666
signature: Signature::one_of(
67-
vec![TypeSignature::NullAry, TypeSignature::UserDefined],
67+
vec![TypeSignature::Nullary, TypeSignature::UserDefined],
6868
Volatility::Immutable,
6969
),
7070
aliases: vec![String::from("make_list")],

0 commit comments

Comments
 (0)