rctruta opened a new pull request, #27674:
URL: https://github.com/apache/datafusion/pull/27674
## Which issue does this PR close?
- Closes #27673
## Rationale for this change
`suggest_valid_function` / `find_closest_match` in
`datafusion/sql/src/expr/function.rs` currently:
1. Has no maximum edit-distance cutoff, so completely unrelated function
names still emit misleading `"Did you mean ..."` suggestions (e.g.,
`zzzzzzzzzz()` -> `'list_zip'`).
2. Iterates over `HashSet`-backed function names with
`Iterator::min_by_key`, making tie-breaking non-deterministic across processes
(e.g., `date('1995-03-15')` randomly suggests `tanh`, `atan`, `rand`, `abs`,
`cot`, `map`, `range`, or `to_date`).
3. Treats `None` from `suggest_valid_function` at the call site as
`internal_err!("No functions registered with this context.")`, preventing
`None` from representing "no close match".
## What changes are included in this PR?
- `datafusion/sql/src/expr/function.rs`:
- Update `find_closest_match` to use `levenshtein_with_buffer`, reject
candidates where `max_len == 0 || distance * 2 >= max_len` (analogous to
`closest_valid_field` in `datafusion/common/src/error.rs`, strictly requiring
`< 50%` edits so exact-50% false positives like `year` -> `mean` are also
filtered out), and break ties deterministically by `(distance,
Reverse(max_len), candidate_lower)`.
- Update the call site to check for an empty function registry before
calling `suggest_valid_function`, returning `PlanError("Invalid function
'<name>'.")` (with diagnostic span and no note) when no candidate is close
enough.
- Add unit tests in
`expr::function::tests::test_find_closest_match_threshold_and_determinism`.
- `datafusion/sql/tests/cases/diagnostic.rs`:
- Split `test_invalid_function` into a close-typo case (`conca` ->
`concat`) and a far-distance case (`concat_not_exist` -> empty notes).
- `datafusion/sqllogictest/test_files/functions.slt`:
- Add end-to-end tests for `SELECT date('1995-03-15')` (deterministically
suggesting `to_date`) and `SELECT zzzzzzzzzz()` (no suggestion).
## Are these changes tested?
Yes:
- `cargo test -p datafusion-sql` (104 unit tests, 613 integration tests, 12
doc-tests)
- `cargo test -p datafusion-sqllogictest --test sqllogictests --
functions.slt errors.slt`
- `cargo clippy -p datafusion-sql --all-targets --no-deps -- -D warnings`
- `cargo fmt -p datafusion-sql -- --check`
## Are there any user-facing changes?
Unknown SQL function errors no longer emit non-deterministic or unrelated
`"Did you mean ..."` suggestions when no registered function is within the
edit-distance threshold.
--
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]