Skip to content

Include the webassembly_udf_* settings in the query condition cache key - #120050

Closed
alexey-milovidov wants to merge 1 commit into
masterfrom
qcc-salt-webassembly-udf-settings
Closed

alexey-milovidov wants to merge 1 commit into
masterfrom
qcc-salt-webassembly-udf-settings

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Sep 14, 2026 •

Copy link
Copy Markdown
Member

Related: #117803

Follow-up to #117803, which folds function settings that do not reach the condition's ActionsDAG into the query condition cache key. The AI review on that PR pointed out one more family after it was merged: a LANGUAGE WASM function declared DETERMINISTIC is admitted to the cache, and FunctionUserDefinedWasm snapshots webassembly_udf_max_fuel / webassembly_udf_max_memory in its compartment pool config when it is built and reads webassembly_udf_max_input_block_size / webassembly_udf_input_split_memory_ratio per call. They decide between a result and a WASM_ERROR / BAD_ARGUMENTS exception without changing the function name or the result type, so a "no marks match" verdict primed under a lenient budget was served to a session with a stricter budget instead of its exception.

This registers the four settings in queryConditionCacheSettingsSalt under the same rule as the rest of the salt. The regression test primes the cache with an unlimited budget and then checks that one unit of fuel, a memory cap below the module's minimum memory, and an out-of-range split ratio each still raise on the same predicate. Verified against a binary without the fix: the fuel line "succeeded but the server error 770 was expected".

Changelog category (leave one):

  • Experimental Feature

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

The query condition cache key now includes the webassembly_udf_* execution budget settings, so a "no rows match" verdict from a deterministic LANGUAGE WASM function evaluated under a lenient fuel or memory budget is no longer served to a query with a stricter budget that is supposed to raise an exception.

🤖 Generated with Claude Code


Workflow [PR]
Sync PR [sync-upstream/pr/120050]

… key

A `LANGUAGE WASM` function declared `DETERMINISTIC` is admitted to the
query condition cache like any other function, but `FunctionUserDefinedWasm`
snapshots its execution budget when it is built (`webassembly_udf_max_fuel`,
`webassembly_udf_max_memory`) and reads `webassembly_udf_max_input_block_size`
and `webassembly_udf_input_split_memory_ratio` per call, none of which reaches
`ActionsDAG::Node::updateHash`. A "no marks match" verdict primed under a
lenient budget was served to a session with a stricter budget instead of its
`WASM_ERROR` / `BAD_ARGUMENTS` exception. Fold the four settings into
`queryConditionCacheSettingsSalt`, following the registration rule
introduced in #117803.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [f375e84]

Summary: ❌

job_name test_name status info comment
Stress test (arm_release) FAIL
Logical error: Invalid number of columns in chunk pushed to OutputPort. Expected A, found B (STID: 1815-379e) FAIL cidb
LLVM Coverage FAIL
Generate LLVM Coverage Diff Report FAIL cidb
Print Uncovered Code FAIL cidb Uncovered code analysis did not run: bash ci/jobs/scripts/generate_diff_coverage_report.sh failed (its output is on the Generate LLVM Coverage Diff Report result).

AI Review

Summary

This PR adds the query-scoped webassembly_udf_* execution-budget settings to queryConditionCacheSettingsSalt and introduces a stateless regression around deterministic LANGUAGE WASM predicates. The general direction is right, but the cache key is still incomplete for deterministic WASM UDFs whose persisted definition changes under the same SQL name, and the new regression test currently targets the wrong memory-limit failure mode while leaving webassembly_udf_max_input_block_size unproven.

Findings

❌ Blockers

  • [src/Interpreters/Cache/QueryConditionCache.cpp:234] The cache key still is not partitioned by all semantics-affecting WASM state. FunctionUserDefinedWasm latches the persisted webassembly_udf_enable_fuel setting into getWasmModuleConfig and other persisted UDF properties such as serialization_format into the executable object, but ActionsDAG::Node::updateHash hashes only the SQL function name / result type and CREATE OR REPLACE FUNCTION does not clear the query condition cache. A deterministic WASM UDF can therefore prime a "no marks match" entry, be replaced under the same SQL name with a stricter persisted definition, and still suppress the expected rows or WASM_ERROR.
    Suggested fix: either invalidate the query condition cache on WASM UDF create/replace/drop, or include a stable fingerprint of the persisted WASM definition in the hash used for deterministic WASM predicates.

⚠️ Majors

  • [tests/queries/0_stateless/05213_query_condition_cache_wasm_udf_settings.sh:45] One of the four new salt inputs is untested. The regression never changes webassembly_udf_max_input_block_size, so hash.update(settings[Setting::webassembly_udf_max_input_block_size].value); can be removed without making this test fail.
    Suggested fix: add a case where the prime query uses a small fixed block size that fits under the fuel/memory budget, then rerun the same predicate with webassembly_udf_max_input_block_size = 0 or a larger block size so the uncached execution is supposed to raise.
  • [tests/queries/0_stateless/05213_query_condition_cache_wasm_udf_settings.sh:51] The memory branch here is asserting the wrong failure mode. FunctionUserDefinedWasm::executeImpl rejects any guest-memory ABI below one WASM page with BAD_ARGUMENTS before instantiation, so webassembly_udf_max_memory = 1024 does not test the "module minimum memory" WASM_ERROR path described in the PR body and comment.
    Suggested fix: either change this expectation to BAD_ARGUMENTS, or use a one-page cap against a module whose declared minimum is larger (as in 05088_wasm_abi_sub_page_memory_limit.sh) if the goal is to prove the instantiation failure remains uncached.
Final Verdict

Changes requested. The new salt covers the obvious query-scoped budgets, but the review surface for deterministic WASM UDFs is still incomplete and one of the new regression assertions is mis-targeted.

LLVM Coverage Report

⚠️ No coverage measurement for commit f375e84: bash ci/jobs/scripts/generate_diff_coverage_report.sh failed (its output is on the Generate LLVM Coverage Diff Report result).

@clickhouse-gh clickhouse-gh Bot added the pr-experimental Experimental Feature label Sep 14, 2026
/// (a stricter budget runs out of fuel or memory on the same rows, an out-of-range ratio is rejected), and
/// none of them changes the function name or the result type, so a verdict primed under a lenient budget
/// must not be served to a session with a stricter one.
hash.update(settings[Setting::webassembly_udf_max_fuel].value);

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.

FunctionUserDefinedWasm still has semantics-affecting state outside this salt. In particular, it latches the persisted webassembly_udf_enable_fuel UDF setting into getWasmModuleConfig(... getFuelMode()) and captures other persisted WASM-definition properties like serialization_format in the executable object, but ActionsDAG::Node::updateHash() only hashes the SQL function name / result type and CREATE OR REPLACE FUNCTION does not clear the query condition cache. That means CREATE OR REPLACE FUNCTION f ... SETTINGS webassembly_udf_enable_fuel = false, prime a no-match entry, then replace the same f with fuel enabled and rerun under webassembly_udf_max_fuel = 1 will still reuse the stale verdict instead of surfacing the WASM_ERROR. Either invalidate the query condition cache on WASM UDF create/replace/drop, or include a stable fingerprint of the persisted WASM definition in the deterministic-function hash.


-- Prime the cache with an unlimited budget: the identity function never returns -1, so no mark matches.
SYSTEM DROP QUERY CONDITION CACHE;
SELECT count() FROM t_qcc_wasm WHERE identity_qcc_det(x) = -1 SETTINGS webassembly_udf_max_fuel = 0, webassembly_udf_max_memory = 134217728, webassembly_udf_input_split_memory_ratio = 0.5;

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.

The title/body say all four execution-budget settings are now part of the cache key, but this regression only varies three of them. Nothing here changes webassembly_udf_max_input_block_size, so the new hash.update(settings[Setting::webassembly_udf_max_input_block_size].value); line can be deleted and this test still passes. Please add a case where the prime query uses a small fixed block size to fit under the same fuel/memory budget and the rerun uses webassembly_udf_max_input_block_size = 0 (or a larger block) so the uncached execution is supposed to raise.

-- one unit of fuel is exhausted by the first call,
SELECT count() FROM t_qcc_wasm WHERE identity_qcc_det(x) = -1 SETTINGS webassembly_udf_max_fuel = 1; -- { serverError WASM_ERROR }
-- a memory limit below the module's minimum memory cannot even instantiate it,
SELECT count() FROM t_qcc_wasm WHERE identity_qcc_det(x) = -1 SETTINGS webassembly_udf_max_memory = 1024; -- { serverError WASM_ERROR }

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.

This assertion is targeting a different branch than the comment above and the PR body describe. FunctionUserDefinedWasm::executeImpl rejects any guest-memory ABI below one WASM_PAGE_SIZE with BAD_ARGUMENTS before instantiation, so webassembly_udf_max_memory = 1024 does not reach the module-minimum-memory WASM_ERROR path. Either make the expected error BAD_ARGUMENTS, or switch this to a one-page cap against a module whose declared minimum is larger (like 05088_wasm_abi_sub_page_memory_limit.sh) if you want to validate the instantiation failure stays uncached.

@clickhouse-gh clickhouse-gh Bot added the comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. label Sep 14, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 14, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing f375e842d with master 8f68f32a0 (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of 6b982cacb; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

7 translation units recompiled, 7 s compile time in total, 7 of them have a recent master baseline.

Job report

@rschu1ze

Copy link
Copy Markdown
Member

Will be fixed properly by #120690

@rschu1ze rschu1ze closed this Sep 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. pr-experimental Experimental Feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants