Repository navigation
feat(function): let scalar functions accept any arguments - #420
Open
alexskinner wants to merge 1 commit into
Open
alexskinner wants to merge 1 commit into
alexskinner wants to merge 1 commit into
Conversation
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 Report❌ Patch coverage is
Additional details and impacted files
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Item 1 of #419. Follow-up to #412 / #418.
What
Three default methods on
ScalarFunctionImpllet a function take any arguments:accepts_any_args(): the function takes its arguments as they are, of any type.accepts_arity(n): whether it takesnarguments (for examplen >= 1).return_type_for(&[LogicalType]): its result type for those argument types.All three default to today's behaviour (
false, the summary's arity, andreturn_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-endedCONCATtakes 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::resolvestill 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 withaccepts_any_args()and a matchingaccepts_arity(n). If two such functions accept the call it returnsAmbiguousFunction. A function that accepts any arguments is skipped in the first pass, so its summary never takes part in overload ranking.arg_typesis not used to resolve calls.evalhandles each argument type.ScalarFunction::return_type(arena)returnsreturn_type_for(..)for these functions, and is used for bothTypeCast::return_typeand evaluation, so the result type is consistent when binding the rest of the statement.FunctionArgstrait soresolvestays generic over scalar and table functions; table functions use the default (never any-args), so their behaviour is unchanged.Tests
test_resolve_any_args_after_declared_overloadsinexpression/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.test_load_function_accepts_any_argsintests/macros-test: agreatest(a, ...)function loaded withDatabase::load, used with 1 to 6 arguments of mixed types, NULL handling, result type used in aWHEREexpression, andgreatest()rejected byaccepts_arity..sltcase: 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.