Skip to content

Commit 81824e8

Browse files
authored
fix: report OneOf signature mismatches as planning errors (#26329)
## Which issue does this PR close? Part of #20501. This is a deliberately narrow slice of that issue, and it is the slice a maintainer previously asked for: #21032 attempted this fix as a large change (it also reworked `sum`/`avg` diagnostics and the public `first_value`/`last_value`/`nth_value` arity contracts), was asked to be split up, and then went stale. This PR is **one behaviour change and nothing else**. ## Rationale for this change `get_valid_types_with_udf` reports "no `OneOf` branch matched" with `internal_err!`. Every alternative signature failing means the *caller* passed the wrong number or types of arguments — a user mistake — yet the user is told: > This issue was likely caused by a bug in DataFusion's code. Please help us to resolve this by filing a bug report in our issue tracker: ... So an ordinary SQL typo invites a bug report, and the failure surfaces as `Internal error: ...` even though nothing internal went wrong. The maintainer comment on #20070 named exactly this ("Can try to fix the display + internal error reference"). ### Before / after `SELECT substr(1, 2)` Before: ```text Error during planning: Internal error: Function 'substr' failed to match any signature, errors: Error during planning: Function 'substr' requires String, but received Int64 (DataType: Int64).,Error during planning: Function 'substr' expects 3 arguments but received 2. This issue was likely caused by a bug in DataFusion's code. Please help us to resolve this by filing a bug report in our issue tracker: https://github.com/apache/datafusion/issues ``` After: ```text Error during planning: Function 'substr' failed to match any signature, errors: Error during planning: Function 'substr' requires String, but received Int64 (DataType: Int64).,Error during planning: Function 'substr' expects 3 arguments but received 2. ``` The message body, the candidate signatures, and the trailing "No function matches the given name and argument types ... You might need to add explicit type casts." line are all unchanged. Only the `Internal error` kind and the bug-report boilerplate are gone. ## What changes are included in this PR? - `datafusion/expr/src/type_coercion/functions.rs`: return `plan_err!` instead of `internal_err!` when every `OneOf` branch rejects the arguments. One line of behaviour change. - A unit test covering that failure. It mirrors `substr`'s real signature, so one branch fails on the argument type and the other on the arity. - Ten `sqllogictest` expectations that asserted the old user-visible text, in `array/array_range.slt`, `spark/math/round.slt`, `errors.slt` and `math.slt`. ## Are these changes tested? Yes. - The new unit test fails on `main` and passes with the change; `cargo test -p datafusion-expr --lib` is 282 passed, 0 failed. - `cargo fmt --all --check` is clean. - I audited every other expectation of this message. The remaining ones — `functions.slt`, `aggregate.slt`, `string_literal.slt`, `named_arguments.slt`, and one assertion in `datafusion/optimizer/src/analyzer/type_coercion.rs` — match on a substring that does not include the removed prefix, so they are unaffected. No `sqllogictest` file asserted the "This issue was likely caused by a bug..." boilerplate. I could not run the full `sqllogictest` suite locally (it needs a full `datafusion-core` build), so CI is the verification for the four updated `.slt` files. ## Scope Deliberately left out, to keep this reviewable: - the message is still verbose and still prefixes every branch error with `Error during planning:` — that is #19004; - `errors.join(",")` still runs the branch errors together without a separator, which is why the after-message reads `...(DataType: Int64).,Error during planning: ...`; - the arity phrasing suggested on #20070 (`log expects 1 to 2 arguments, got 3`) is a separate improvement. Happy to follow up on any of those in separate PRs.
1 parent 9f4b005 commit 81824e8

5 files changed

Lines changed: 16 additions & 12 deletions

File tree

‎datafusion/expr/src/type_coercion/functions.rs‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -559,9 +559,13 @@ fn get_valid_types_with_udf<F: UDFCoercionExt>(
559559
}
560560
}
561561

562-
// Every signature failed, return the joined error
562+
// Every signature failed. That means the caller passed the wrong
563+
// number or types of arguments, which is a user mistake rather than
564+
// a bug in DataFusion, so report it as a planning error. Reporting it
565+
// as an internal error told users to file a bug report for their own
566+
// typo, and the caller appends the candidate signatures anyway.
563567
if res.is_empty() {
564-
return internal_err!(
568+
return plan_err!(
565569
"Function '{}' failed to match any signature, errors: {}",
566570
func.name(),
567571
errors.join(",")

‎datafusion/sqllogictest/test_files/array/array_range.slt‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -244,17 +244,17 @@ select generate_series(arrow_cast('2021-01-01T00:00:00', 'Timestamp(Nanosecond,
244244
[2021-01-01T00:00:00-05:00, 2021-01-01T01:29:54.500-05:00, 2021-01-01T02:59:49-05:00, 2021-01-01T04:29:43.500-05:00, 2021-01-01T05:59:38-05:00]
245245

246246
## mixing types for timestamps is not supported
247-
query error DataFusion error: Error during planning: Internal error: Function 'generate_series' failed to match any signature
247+
query error DataFusion error: Error during planning: Function 'generate_series' failed to match any signature
248248
select generate_series(arrow_cast('2021-01-01T00:00:00', 'Timestamp(Nanosecond, Some("-05:00"))'), DATE '2021-01-02', INTERVAL '1' HOUR);
249249

250250
## mixing types not allowed even if an argument is null
251-
query error DataFusion error: Error during planning: Internal error: Function 'generate_series' failed to match any signature
251+
query error DataFusion error: Error during planning: Function 'generate_series' failed to match any signature
252252
select generate_series(TIMESTAMP '1992-09-01', DATE '1993-03-01', NULL);
253253

254-
query error DataFusion error: Error during planning: Internal error: Function 'generate_series' failed to match any signature
254+
query error DataFusion error: Error during planning: Function 'generate_series' failed to match any signature
255255
select generate_series(1, '2024-01-01', '2025-01-02');
256256

257-
query error DataFusion error: Error during planning: Internal error: Function 'generate_series' failed to match any signature
257+
query error DataFusion error: Error during planning: Function 'generate_series' failed to match any signature
258258
select generate_series('2024-01-01'::timestamp, '2025-01-02', interval '1 day');
259259

260260
## should return NULL

‎datafusion/sqllogictest/test_files/errors.slt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ from aggregate_test_100
124124
order by c9
125125

126126
# WindowFunction wrong signature
127-
statement error DataFusion error: Error during planning: Internal error: Function 'nth_value' failed to match any signature
127+
statement error DataFusion error: Error during planning: Function 'nth_value' failed to match any signature
128128
select
129129
c9,
130130
nth_value(c5, 2, 3) over (order by c9) as nv1

‎datafusion/sqllogictest/test_files/math.slt‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,10 @@ NULL
175175
query error DataFusion error: Error during planning: 'round' does not support zero arguments
176176
SELECT round();
177177

178-
query error Error during planning: Internal error: Function 'round' failed to match any signature
178+
query error Error during planning: Function 'round' failed to match any signature
179179
SELECT round(1, 2, 3);
180180

181-
query error Error during planning: Internal error: Function 'round' failed to match any signature
181+
query error Error during planning: Function 'round' failed to match any signature
182182
SELECT round('x');
183183

184184
query error round decimal_places 2147483648 is out of supported i32 range

‎datafusion/sqllogictest/test_files/spark/math/round.slt‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -593,13 +593,13 @@ set datafusion.execution.enable_ansi_mode = false;
593593
# -------------------------------------------------------------------
594594

595595
# round(string) should fail
596-
query error Error during planning: Internal error: Function 'round' failed to match any signature
596+
query error Error during planning: Function 'round' failed to match any signature
597597
SELECT round('hello'::varchar);
598598

599599
# round(boolean) should fail
600-
query error Error during planning: Internal error: Function 'round' failed to match any signature
600+
query error Error during planning: Function 'round' failed to match any signature
601601
SELECT round(true);
602602

603603
# round(timestamp) should fail
604-
query error Error during planning: Internal error: Function 'round' failed to match any signature
604+
query error Error during planning: Function 'round' failed to match any signature
605605
SELECT round('2023-01-01T00:00:00'::timestamp);

0 commit comments

Comments
 (0)