Repository navigation
Restore SQL user-defined functions that an earlier version accepted - #122912
Conversation
`RESTORE TABLE system.functions` registers every restored definition through `registerFunction`, whose `checkCanBeRegistered` runs `validateSQLFunction`, the check for a new `CREATE FUNCTION`. Each time that check was tightened, definitions stored under the older rules became unrestorable, and the first of them aborted the whole restore: - ClickHouse#80431 (25.6) requires the argument list to be a `tuple`, so a backup taken on 25.5 or earlier that holds `CREATE FUNCTION f AS lambda(identity(x), x)` fails on 25.6 and later with `Lambda must have valid arguments`. - ClickHouse#120483 (master) requires the core to be a `lambda` and the tuple to have no parameters, so a Cloud upgrade from 26.4 failed with `Expected lambda expression, got: JSONExtractString((x, y), x): While restoring data of table system.functions`: ClickHouse#120483 (comment) Loading the same definitions from disk or Keeper at startup never validates them, so an in-place upgrade keeps them and `BACKUP` writes them out unchanged. `restore` now passes `is_restore`, and `validateSQLFunction` then skips exactly those two rules. The structural rules stay (the core is a function with two arguments, the argument list holds unique identifiers, the body exists, no self-recursion): no version accepted a definition that fails them, and the DDL expansion in `UserDefinedSQLFunctionVisitor::tryToReplaceFunction` relies on them without checks. `CREATE FUNCTION` is unchanged. Tables are re-created by `RESTORE` under `LoadingStrictnessLevel::SECONDARY_CREATE`, which skips some sanity checks for the same reason. The new backward-compatibility test creates three such definitions and a control on 25.3, backs them up, upgrades, and restores; it fails on master and on 26.9.1.1629 at the first restore. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Shorten the validateSQLFunction comment to one line and write `lambda` and `tuple` as code. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 2 findings over one full pass and one re-check of the final description), plus my own cold review of the diff: no blocker, no major.
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-10:20260929-152300 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-5:20260929-140400 |
|
Workflow [PR], commit [49b7e8c] Summary: ✅
AI ReviewSummaryThis PR separates SQL UDF validation for new definitions from restore-time registration, so Final Verdict✅ No remaining findings. LLVM Coverage ReportMeasured on commit 49b7e8c.
Changed lines: Changed C/C++ lines covered: 26/26 (100.00%) · Uncovered code |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units31 translation units recompiled, 228 s compile time in total, 31 of them have a recent master baseline. |
| throw Exception(ErrorCodes::BAD_ARGUMENTS, "Expected function, got: {}", function->formatForErrorMessage()); | ||
|
|
||
| if (lambda_function->name != "lambda") | ||
| if (!is_restore && lambda_function->name != "lambda") |
There was a problem hiding this comment.
@groeneai are you sure there are no other cases missing?
There was a problem hiding this comment.
No, there were more. I checked every rule on the restore path against the versions that could store a definition:
- 21.10 to 22.1 accepted a core with more than two arguments, e.g.
lambda(tuple(x), x, 1), and the 22.2 arity check rejected it on restore. Covered now. - Reject lambda arguments a SQL user defined function cannot use #121370's rules (single-part argument names, nested lambda argument lists) would have done the same once merged. They go into
validateSQLFunction, whichRESTOREno longer runs, so neither PR needs another change. - The shape that
RESTOREstill checks held in every version: a definition without it threw or crashed the server inCREATE FUNCTION(fixed in 22.2 and 22.5), so none was stored. - Not covered: a function whose name later became a built-in (RESTORE fails with FUNCTION_ALREADY_EXISTS when a backup contains a UDF whose name is a built-in in the restoring version #121340). That is a name conflict, not a definition rule. Restoring it would shadow the built-in (a stored
stddevUDF wins over thestddevaggregate, measured), andDROP FUNCTIONrefuses to remove it (Cannot drop system function). Tell me if you want it handled in this PR.
The test now also restores a stored three-argument core and #121370's two shapes, and checks that RESTORE still refuses f(g(1), 2): 5e6d67d.
|
|
||
| /// Register function for function_name in factory for specified create_function_query. | ||
| bool registerFunction(const ContextMutablePtr & current_context, const String & function_name, ASTPtr create_function_query, bool throw_if_exists, bool replace_if_exists); | ||
| bool registerFunction(const ContextMutablePtr & current_context, const String & function_name, ASTPtr create_function_query, bool throw_if_exists, bool replace_if_exists, bool is_restore = false); |
There was a problem hiding this comment.
@groeneai I don't like the bool is_restore parameter. Is there a better way to do this?
There was a problem hiding this comment.
Replaced in ca702c0. CREATE FUNCTION now calls a new createFunction, which runs validateSQLFunction with all its rules and then registerFunction. registerFunction, which restore also uses, keeps the name checks and checks only the shape that UserDefinedSQLFunctionVisitor dereferences without checks: a core with at least two arguments, the first a function whose arguments are identifiers. There is no flag, and a rule added to CREATE FUNCTION later cannot affect RESTORE.
…ions Replace the `is_restore` flag with a separate entry point for new definitions. `createFunction`, called by `CREATE FUNCTION`, applies every rule of `validateSQLFunction`. `registerFunction`, used by both `CREATE FUNCTION` and `RESTORE`, checks only the shape that `UserDefinedSQLFunctionVisitor` dereferences without checks: a function core with at least two arguments, the first a function whose arguments are identifiers. A rule added to `CREATE FUNCTION` later can no longer make stored definitions unrestorable. This also covers a core with more than two arguments, which versions 21.10 to 22.1 accepted before the arity check of 22.2, and the argument rules pending in ClickHouse#121370. The test gains rows for both. Review: ClickHouse#122912 (comment) Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…nctions Every version since 21.10 rejected duplicate lambda parameters in `CREATE FUNCTION`, so only a hand-written storage file can hold such a definition. `RESTORE` rejected it until the previous change, and should keep doing so: `UserDefinedSQLFunctionVisitor` maps parameters to arguments with `emplace`, so `(x, x) -> x` silently ignores its second argument when expanded, e.g. `DEFAULT f(a, 7)` is stored as `DEFAULT a`. The test now also restores a backup holding `(x, x) -> x` and expects `BAD_ARGUMENTS`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
@fm4v the PR looks good to me. Can you give a review? |
Related: #120483 (comment)
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed
RESTOREofsystem.functionsfailing, and aborting the whole restore, when the backup holds a SQL user-defined function whose definition the version that created it accepted butCREATE FUNCTIONnow rejects, for exampleCREATE FUNCTION f AS lambda(identity(x), x)created on 25.5 or earlier.Description
On 25.6 and later,
RESTOREof a backup taken on 25.5 or earlier fails withBAD_ARGUMENTSand aborts the whole restore when it holds a function such asCREATE FUNCTION f AS lambda(identity(x), x)(measured on 26.9.1.1629). On master the same happens forCREATE FUNCTION f AS JSONExtractString((x, y), x)created on 26.4 (the linked Cloud upgrade).Root cause.
UserDefinedSQLFunctionFactory::restoreregisters every restored definition through the check for a newCREATE FUNCTION. Each tightening of that check (22.2: exactly two lambda arguments; #80431: the argument list is atuple; #120483: the core is alambda, the tuple has no parameters) made definitions stored under older rules unrestorable, although startup loading never validates them.Change.
CREATE FUNCTIONnow callscreateFunction, which applies every rule. Registration, shared withRESTORE, checks only what DDL expansion relies on: a core with at least two arguments, the first a function of distinct identifiers (a storedf(g(1), 2)makesCREATE TABLE ... DEFAULThit undefined behavior,(x, x) -> xsilently drops an argument). So a laterCREATE FUNCTIONrule, #121370's included, cannot break restores again. Restored functions behave as after an in-place upgrade (a query calling a non-lambda one raises the existingLOGICAL_ERROR).Validation. A new backward-compatibility test creates such functions and a control on 25.3, stores one that only 21.10 to 22.1 accepted, backs up, upgrades, then restores the old backup onto existing and dropped functions, and a new backup;
CREATE FUNCTIONstill rejects the legacy ones. It fails on master and 26.9 at the first restore, passes with the fix; requiring exactly two arguments, rejecting #121370's dotted arguments on restore, or skipping the rules onCREATEfails it.Not changed: a name that became a built-in (#121340): it would shadow the built-in, and
DROP FUNCTIONrefuses it.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/122912]
Version info
26.10.1.1203-master(included in26.10and later)