Skip to content

Commit 405b99c

Browse files
authored
Improve error messages for incorrect zero argument signatures (apache#13881)
* Improve error messages for incorrect zero argument signatures * fix errors * fix fmt
1 parent 8fd792f commit 405b99c

File tree

2 files changed

+43
-37
lines changed

2 files changed

+43
-37
lines changed

datafusion/expr-common/src/signature.rs

+9
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,15 @@ impl TypeSignature {
351351
}
352352
}
353353

354+
/// Returns true if the signature currently supports or used to supported 0
355+
/// input arguments in a previous version of DataFusion.
356+
pub fn used_to_support_zero_arguments(&self) -> bool {
357+
match &self {
358+
TypeSignature::Any(num) => *num == 0,
359+
_ => self.supports_zero_argument(),
360+
}
361+
}
362+
354363
/// get all possible types for the given `TypeSignature`
355364
pub fn get_possible_types(&self) -> Vec<Vec<DataType>> {
356365
match self {

datafusion/expr/src/type_coercion/functions.rs

+34-37
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,21 @@ pub fn data_types_with_scalar_udf(
5050
func: &ScalarUDF,
5151
) -> Result<Vec<DataType>> {
5252
let signature = func.signature();
53+
let type_signature = &signature.type_signature;
5354

5455
if current_types.is_empty() {
55-
if signature.type_signature.supports_zero_argument() {
56+
if type_signature.supports_zero_argument() {
5657
return Ok(vec![]);
58+
} else if type_signature.used_to_support_zero_arguments() {
59+
// Special error to help during upgrade: https://github.com/apache/datafusion/issues/13763
60+
return plan_err!("{} does not support zero arguments. Use TypeSignature::Nullary for zero arguments.", func.name());
5761
} else {
5862
return plan_err!("{} does not support zero arguments.", func.name());
5963
}
6064
}
6165

6266
let valid_types =
63-
get_valid_types_with_scalar_udf(&signature.type_signature, current_types, func)?;
67+
get_valid_types_with_scalar_udf(type_signature, current_types, func)?;
6468

6569
if valid_types
6670
.iter()
@@ -69,12 +73,7 @@ pub fn data_types_with_scalar_udf(
6973
return Ok(current_types.to_vec());
7074
}
7175

72-
try_coerce_types(
73-
func.name(),
74-
valid_types,
75-
current_types,
76-
&signature.type_signature,
77-
)
76+
try_coerce_types(func.name(), valid_types, current_types, type_signature)
7877
}
7978

8079
/// Performs type coercion for aggregate function arguments.
@@ -89,33 +88,29 @@ pub fn data_types_with_aggregate_udf(
8988
func: &AggregateUDF,
9089
) -> Result<Vec<DataType>> {
9190
let signature = func.signature();
91+
let type_signature = &signature.type_signature;
9292

9393
if current_types.is_empty() {
94-
if signature.type_signature.supports_zero_argument() {
94+
if type_signature.supports_zero_argument() {
9595
return Ok(vec![]);
96+
} else if type_signature.used_to_support_zero_arguments() {
97+
// Special error to help during upgrade: https://github.com/apache/datafusion/issues/13763
98+
return plan_err!("{} does not support zero arguments. Use TypeSignature::Nullary for zero arguments.", func.name());
9699
} else {
97100
return plan_err!("{} does not support zero arguments.", func.name());
98101
}
99102
}
100103

101-
let valid_types = get_valid_types_with_aggregate_udf(
102-
&signature.type_signature,
103-
current_types,
104-
func,
105-
)?;
104+
let valid_types =
105+
get_valid_types_with_aggregate_udf(type_signature, current_types, func)?;
106106
if valid_types
107107
.iter()
108108
.any(|data_type| data_type == current_types)
109109
{
110110
return Ok(current_types.to_vec());
111111
}
112112

113-
try_coerce_types(
114-
func.name(),
115-
valid_types,
116-
current_types,
117-
&signature.type_signature,
118-
)
113+
try_coerce_types(func.name(), valid_types, current_types, type_signature)
119114
}
120115

121116
/// Performs type coercion for window function arguments.
@@ -130,30 +125,29 @@ pub fn data_types_with_window_udf(
130125
func: &WindowUDF,
131126
) -> Result<Vec<DataType>> {
132127
let signature = func.signature();
128+
let type_signature = &signature.type_signature;
133129

134130
if current_types.is_empty() {
135-
if signature.type_signature.supports_zero_argument() {
131+
if type_signature.supports_zero_argument() {
136132
return Ok(vec![]);
133+
} else if type_signature.used_to_support_zero_arguments() {
134+
// Special error to help during upgrade: https://github.com/apache/datafusion/issues/13763
135+
return plan_err!("{} does not support zero arguments. Use TypeSignature::Nullary for zero arguments.", func.name());
137136
} else {
138137
return plan_err!("{} does not support zero arguments.", func.name());
139138
}
140139
}
141140

142141
let valid_types =
143-
get_valid_types_with_window_udf(&signature.type_signature, current_types, func)?;
142+
get_valid_types_with_window_udf(type_signature, current_types, func)?;
144143
if valid_types
145144
.iter()
146145
.any(|data_type| data_type == current_types)
147146
{
148147
return Ok(current_types.to_vec());
149148
}
150149

151-
try_coerce_types(
152-
func.name(),
153-
valid_types,
154-
current_types,
155-
&signature.type_signature,
156-
)
150+
try_coerce_types(func.name(), valid_types, current_types, type_signature)
157151
}
158152

159153
/// Performs type coercion for function arguments.
@@ -168,31 +162,34 @@ pub fn data_types(
168162
current_types: &[DataType],
169163
signature: &Signature,
170164
) -> Result<Vec<DataType>> {
165+
let type_signature = &signature.type_signature;
166+
171167
if current_types.is_empty() {
172-
if signature.type_signature.supports_zero_argument() {
168+
if type_signature.supports_zero_argument() {
173169
return Ok(vec![]);
170+
} else if type_signature.used_to_support_zero_arguments() {
171+
// Special error to help during upgrade: https://github.com/apache/datafusion/issues/13763
172+
return plan_err!(
173+
"signature {:?} does not support zero arguments. Use TypeSignature::Nullary for zero arguments.",
174+
type_signature
175+
);
174176
} else {
175177
return plan_err!(
176178
"signature {:?} does not support zero arguments.",
177-
&signature.type_signature
179+
type_signature
178180
);
179181
}
180182
}
181183

182-
let valid_types = get_valid_types(&signature.type_signature, current_types)?;
184+
let valid_types = get_valid_types(type_signature, current_types)?;
183185
if valid_types
184186
.iter()
185187
.any(|data_type| data_type == current_types)
186188
{
187189
return Ok(current_types.to_vec());
188190
}
189191

190-
try_coerce_types(
191-
function_name,
192-
valid_types,
193-
current_types,
194-
&signature.type_signature,
195-
)
192+
try_coerce_types(function_name, valid_types, current_types, type_signature)
196193
}
197194

198195
fn is_well_supported_signature(type_signature: &TypeSignature) -> bool {

0 commit comments

Comments
 (0)