rctruta opened a new issue, #27673:
URL: https://github.com/apache/datafusion/issues/27673
### Describe the bug
When a SQL query calls an unknown function, `suggest_valid_function` /
`find_closest_match` in `datafusion/sql/src/expr/function.rs` has two issues:
1. **No similarity threshold:** `find_closest_match` uses
`candidates.into_iter().min_by_key(|candidate| levenshtein(...))`, which always
returns `Some(...)` as long as any function is registered — even when no
candidate is remotely close:
```sql
SELECT zzzzzzzzzz();
-- Error during planning: Invalid function 'zzzzzzzzzz'.
-- Did you mean 'list_zip'?
```
2. **Non-deterministic tie-breaking:** `ctx.udf_names()` /
`ctx.udaf_names()` are backed by `HashSet` (per-process randomized hash order),
and `Iterator::min_by_key` picks the first minimum encountered. For short names
with ties at equal Levenshtein distance, the suggestion changes across
processes.
- Running `SELECT date('1995-03-15')` across 20 fresh processes on
`datafusion 55.0.0` produced 8 different suggestions (all at Levenshtein
distance 3): `tanh` (8x), `atan` (3x), `rand` (2x), `abs` (2x), `to_date` (2x),
`cot` (1x), `range` (1x), `map` (1x).
- Across a 75-query probe of common cross-dialect SQL functions (3 fresh
processes each), **28 of 67** invalid functions returned different suggestions
across runs.
Additionally, the caller in `datafusion/sql/src/expr/function.rs` currently
treats `None` from `suggest_valid_function` as `internal_err!("No functions
registered with this context.")`, so returning `None` when no candidate is
close enough requires separating the empty-registry check from the "no close
match" case.
### To Reproduce
Using `datafusion-cli`:
```bash
# 1. No edit-distance threshold
datafusion-cli -q -c "SELECT zzzzzzzzzz();"
# Error: Error during planning: Invalid function 'zzzzzzzzzz'.
# Did you mean 'list_zip'?
# 2. Non-deterministic tie-breaking across fresh processes
for i in {1..10}; do
datafusion-cli -q -c "SELECT date('1995-03-15');" 2>&1 | grep "Did you
mean"
done
# Output varies across runs (e.g. 'tanh', 'atan', 'rand', 'abs', 'cot',
'map', 'range', 'to_date')
```
### Expected behavior
Consistent with `closest_valid_field` in `datafusion/common/src/error.rs`:
1. Omit `"Did you mean ..."` when `distance * 2 >= max_len` (at least half
of the longer name would have to be edited), returning `PlanError("Invalid
function '<name>'.")` while preserving `internal_err!("No functions registered
with this context.")` when the registry is empty.
2. Break ties deterministically by `(distance, Reverse(max_len),
candidate_lower)`.
3. Reuse a buffer via `levenshtein_with_buffer`.
### Additional context
I have a tested fix ready and will open a PR referencing this issue.
### Willingness to contribute
I plan to contribute a fix for this bug independently
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]