Repository navigation
Description
Hi @KKould,
A follow-up to #403. With #404-#407 and #410 merged, the last thing KiteSQL-MySQL needs from core, besides #408 and #409, is a way to add its own functions from outside the crate, for example CONCAT_WS, a three-argument REPLACE, or LAST_INSERT_ID().
What already works
I checked main (34a2123) first, and registering functions from another crate already works, so I don't think a new registration API is needed:
Database::load(CatalogKind::ScalarFunction(..))andCatalogKind::TableFunction(..)(src/db.rs) add to the database-wide function maps.ScalarFunctionImpl,TableFunctionImplandFunctionSummaryare public (src/expression/function/). The exportedscala_function!andtable_function!macros (src/macros/mod.rs) are already used fromtests/macros-test.- A function used in stored DDL, such as a column
DEFAULT, is encoded by itsFunctionSummaryand resolved again on decode. So it survives a reopen, as long as the function is loaded before the table.
The gap: lookup needs an exact signature match
bind_function_call (src/binder/expr.rs) builds a FunctionSummary from the argument types and calls scala_functions.get(&summary), so any argument whose type isn't exactly the declared parameter type misses. Here is a small example on main, with twice(VARCHAR) and plus1(BIGINT) registered through Database::load:
create table t (id int primary key, s varchar(20), u varchar, n int);
select twice(u) from t; -- ok
select twice(s) from t; -- function `twice` not found (VARCHAR(20) vs VARCHAR)
select upper(s) from t; -- function `upper` not found (built-in, same reason)
select twice(null); -- not found (SqlNull, the existing TODO)
select plus1(n) from t; -- not found (INT vs BIGINT)
select plus1(5); -- not found
select plus1(cast(5 as bigint)); -- okThe upper(s) case shows this isn't only a downstream problem. For a dialect crate, it means registering every function once for each possible argument type and length, which doesn't scale.
Proposal (one PR)
Keep the exact map hit as the fast path. Only on a miss:
- Collect the registered functions with the same name and number of arguments.
- A candidate matches if every argument meets one of these:
- it is exactly the parameter type;
- it is
SqlNull; - it is the same string type with a length the parameter accepts (
VARCHAR(20)->VARCHAR); LogicalType::can_implicit_cast(arg, param)is true.
- If several candidates match, prefer the most specific one: a candidate whose parameter types all implicitly cast to the other's. For example,
f(BIGINT)beatsf(DOUBLE)for anINTargument. If two are still tied, return an error. If none match, keep the currentFunctionNotFound. - Wrap each argument that isn't an exact match in a
TypeCastto the parameter type, so an implementation always sees the types it declared.
Properties:
- Dialect-neutral: no new
SqlDialectmethod, and the rules reusecan_implicit_cast. - No change to registration,
FunctionSummaryor the storage format. - Nothing changes when a lookup hits. The extra work only happens at bind time after a miss. I'll include TPC-C numbers in the PR.
- Tests: a few cases in an existing slt file (
upper()on aVARCHAR(n)column,upper(null), integer widening), plus one unit test insrc/binder/expr.rsfor the tie-break. No new test files.
Dialects
I'd keep functions database-wide, as they are today. A dialect crate would ship its functions with a small helper that loads them, and the application calls that helper when it sets up the dialect. A per-dialect function set, so that switching through DialectRegistry also changes which functions are visible, could come later if you think it's useful. KiteSQL-MySQL doesn't need it.
Not in this proposal
- Aggregates.
AggKindis a closed enum, so an aggregate can't be added from outside. I have a UDAF trait on the fork, but KiteSQL-MySQL doesn't need it now, so I'd leave it unless you want one. - Variadic functions (
CONCAT,CONCAT_WS). Registering one function per argument count works for now. - Registering functions on
DataBaseBuilder, so they always exist before any table is loaded. It would be nice to have, butDatabase::loadis enough.
Questions
- Do you want implicit argument resolution in core at all, or would you rather keep exact matching and have callers cast explicitly?
- If you do want it, are the matching rules above right, especially string length widening and reusing
can_implicit_cast? Should a tie be an error, or should it pick one? - Should the binder insert the casts, or should casting stay inside each implementation, as
scala_function!already does? - Are database-wide functions fine, or would you prefer a per-dialect hook from the start?
- Would you ever want an extension point for user-defined aggregates, or should aggregates stay internal?
I'm happy to change the shape, naming or scope based on your feedback before I write the PR. If this isn't a direction you want for KiteSQL, just say so and I'll handle it downstream.
Activity