Skip to content

fix: add edit-distance threshold and deterministic tie-breaking to su… - #27674

Merged
alamb merged 2 commits into
apache:mainfrom
rctruta:fix/suggest-valid-function-threshold-determinism
Nov 28, 2026
Merged

alamb merged 2 commits into
apache:mainfrom
rctruta:fix/suggest-valid-function-threshold-determinism

Conversation

@rctruta

@rctruta rctruta commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

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.

@github-actions github-actions Bot added sql SQL Planner sqllogictest SQL Logic Tests (.slt) labels Nov 23, 2026

@2010YOUY01 2010YOUY01 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a nice error message improvement. Thank you!

Comment thread datafusion/sql/src/expr/function.rs Resolved

@alamb alamb left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @rctruta and @2010YOUY01

Comment thread datafusion/sql/src/expr/function.rs Resolved
@alamb
alamb enabled auto-merge November 28, 2026 12:47
@alamb
alamb added this pull request to the merge queue Nov 28, 2026
Merged via the queue into apache:main with commit 9dfd461 Nov 28, 2026
42 checks passed
@rctruta
rctruta deleted the fix/suggest-valid-function-threshold-determinism branch November 28, 2026 18:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

sql SQL Planner sqllogictest SQL Logic Tests (.slt) v57.0.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

suggest_valid_function is non-deterministic and has no edit-distance threshold

3 participants