Skip to content

feat(function): let scalar functions accept any arguments - #420

Open
alexskinner wants to merge 1 commit into
KipData:mainfrom
alexskinner:feat/function-accepts-any-args
Open

alexskinner wants to merge 1 commit into
KipData:mainfrom
alexskinner:feat/function-accepts-any-args

Conversation

@alexskinner

Copy link
Copy Markdown
Collaborator

Item 1 of #419. Follow-up to #412 / #418.

What

Three default methods on ScalarFunctionImpl let a function take any arguments:

  • accepts_any_args(): the function takes its arguments as they are, of any type.
  • accepts_arity(n): whether it takes n arguments (for example n >= 1).
  • return_type_for(&[LogicalType]): its result type for those argument types.

All three default to today's behaviour (false, the summary's arity, and return_type()), so existing functions and the built-ins are unchanged.

Why

#418 resolves a call by the parameter types a function declares, so it covers fixed arities. A function such as GREATEST(a, b, ...), LEAST, CONCAT_WS(sep, ...) or an open-ended CONCAT takes any number of arguments of mixed types, and its result type follows its arguments (GREATEST(int, double) is a double). Today these cannot be registered from outside the crate; a downstream dialect can only register a fixed set of arities. Keeping this as a trait extension means the core stays dialect-neutral and no function is added to core.

How

  • FunctionRegistry::resolve still scans the declared overloads first, exactly as in feat(function): resolve function arguments by implicit cast #418. Only when none of them accepts the call does it look for a function of that name with accepts_any_args() and a matching accepts_arity(n). If two such functions accept the call it returns AmbiguousFunction. A function that accepts any arguments is skipped in the first pass, so its summary never takes part in overload ranking.
  • The summary of such a function only identifies it (it is what a decoded plan looks up); arg_types is not used to resolve calls.
  • The binder does not cast the arguments of an any-args function; eval handles each argument type.
  • ScalarFunction::return_type(arena) returns return_type_for(..) for these functions, and is used for both TypeCast::return_type and evaluation, so the result type is consistent when binding the rest of the statement.
  • The registry gets a small crate-private FunctionArgs trait so resolve stays generic over scalar and table functions; table functions use the default (never any-args), so their behaviour is unchanged.

Tests

  • Unit test test_resolve_any_args_after_declared_overloads in expression/function: declared overload wins when it accepts the call, any-args fallback otherwise (any types, any count), arity checked by the function, ambiguity between two any-args functions.
  • Integration test test_load_function_accepts_any_args in tests/macros-test: a greatest(a, ...) function loaded with Database::load, used with 1 to 6 arguments of mixed types, NULL handling, result type used in a WHERE expression, and greatest() rejected by accepts_arity.
  • No .slt case: no built-in function is variadic, so there is nothing to exercise from SQL alone.

Run locally on this branch: make fmt, make clippy, make test (667 passed, 0 failed), make test-async (608 passed), make test-slt (198 files passed).

No hurry on this one. If you would prefer a different shape (for example a single enum on the function instead of three methods), I am happy to rework it.

Function lookup (KipData#418) resolves a call by the parameter types a function
declares, so a function such as GREATEST(a, b, ...) or CONCAT_WS(sep, ...)
that takes any number of arguments of mixed types cannot be registered, and
its result type cannot follow its arguments.

Add three default methods to `ScalarFunctionImpl`:

- `accepts_any_args()`: the function takes its arguments as they are.
- `accepts_arity(n)`: whether it takes `n` arguments.
- `return_type_for(&[LogicalType])`: its result type for those arguments.

`FunctionRegistry::resolve` still tries the declared overloads first, so
existing functions resolve as before. Only when none accepts the call is a
function of that name that accepts any arguments used; two such functions
that both accept the call are reported as `AmbiguousFunction`. The binder
does not cast the arguments of such a function, and the result type is read
through `ScalarFunction::return_type` when binding and evaluating.

Item 1 of KipData#419.
@codecov

codecov Bot commented Dec 6, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 90.90909% with 9 lines in your changes missing coverage. Please review.
✅ Project coverage is 93.03%. Comparing base (522eb00) to head (61821bf).

Files with missing lines Patch % Lines
src/expression/function/scala.rs 76.00% 6 Missing ⚠️
src/expression/function/mod.rs 95.58% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #420      +/-   ##
==========================================
- Coverage   93.03%   93.03%   -0.01%     
==========================================
  Files         283      283              
  Lines       40489    40583      +94     
==========================================
+ Hits        37670    37755      +85     
- Misses       2819     2828       +9     
Flag Coverage Δ
rust 93.03% <90.90%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
src/binder/expr.rs 94.11% <100.00%> (+0.01%) ⬆️
src/expression/evaluator.rs 93.18% <100.00%> (+0.03%) ⬆️
src/expression/function/table.rs 47.61% <ø> (ø)
src/expression/mod.rs 85.61% <100.00%> (ø)
src/expression/function/mod.rs 98.51% <95.58%> (-0.60%) ⬇️
src/expression/function/scala.rs 65.78% <76.00%> (+19.63%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant