Repository navigation
Adaptive lazy execution of short-circuit function arguments (continues #73776) - #114734
alexey-milovidov wants to merge 150 commits into
Conversation
…ouse into continue-pr-73776-adaptive-lazy Reconcile #73776 by @lgbo-ustc with the current `master`. The branch was ~17 months behind, and 27 files conflicted in the hottest central code, so besides resolving the conflict markers the change is reworked to fit the current interfaces: * The `enable_adaptive_short_circuit_lazy_execution` flag is no longer threaded through the `ExpressionStep`/`FilterStep` constructors, their serialization, the planner and the whole `QueryPlan/Optimizations` suite. It is a field of `ExpressionActionsSettings` next to `enable_lazy_columns_replication`, which is where the step already gets it from in `transformPipeline`. This drops ~20 files from the change. * `ExpressionActions::execute` and `executeOnColumns` stay `const`. The free `executeAction` became a member with three virtual hooks - `shouldExecuteLazily`, `executeFunction` and `finalizeBlockExecution` - which `AdaptiveExpressionActions` overrides. Its profiling state is `mutable`, which is safe because an adaptive instance is created per pipeline stream and is never shared between threads. * The `FunctionExecutionProfile` plumbing follows the current `IExecutableFunction` chain, including `executeWithoutReplicatedColumns`, which did not exist when the branch was written, and the `dry_run` argument which `ColumnFunction::reduce` gained. * `is_no_except` is computed only when the setting is enabled, so the default path pays nothing for it. * The setting is moved to the `26.8` block of `SettingsChangesHistory`, and the test is renumbered to `04893` because `03371` is taken. Verified locally: `04893_adaptive_short_circuit_execution` matches the reference, the adaptive decisions do fire (`Lazy execution is not beneficial for ...`), the lazy execution of the arguments which can throw is preserved, and the first query of `tests/performance/adaptive_short_circuit_execution.xml` runs 0.113 s -> 0.063 s.
AI review of a later commit does not approve the PR, see the AI Review comment.
…`clone` `ExpressionActions::clone` chose the clone's implementation from the settings, so copying a plain instance built with `enable_adaptive_short_circuit_lazy_execution` enabled (e.g. the table key expressions in `KeyDescription`, copied on `IStorage::setInMemoryMetadata`) silently produced a stateful `AdaptiveExpressionActions`, which could then be executed concurrently, e.g. by `MergeTreePatchReaderMergeOnKey::readPatch` through the cached patch sorting key. Make `clone` virtual: a plain instance is cloned into a plain one, and `AdaptiveExpressionActions` overrides it to create a fresh adaptive instance. Add `isAdaptive` and use it instead of the setting where the code decides whether an instance needs a per-stream copy. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Conflicts: * `ColumnFunction.cpp`: master added the reuse of the reduced shared nodes (`ReducedColumns`) to `reduceImpl`; this PR templated it on `with_profile`. The merged `reduceImpl<with_profile>` takes both the `ReducedColumns` and the profile. * `ExpressionActions.cpp`: kept both the PR's scope and master's `shared_node` argument. * `ActionsDAG.cpp`: master's `createLambdaExpressionActions`, wrapped into the PR's `ExpressionActionsPool`. Semantic changes: master added the `uncompiled_actions_dag` parameter to the `ExpressionActions` constructor; it is now passed through `ExpressionActions::create` and `AdaptiveExpressionActions`, and `createLambdaExpressionActions` uses `ExpressionActions::create`, so that a lambda body is adaptive with the setting (it is used only as the prototype of an `ExpressionActionsPool`). Co-Authored-By: Claude Opus 5.5 <[email protected]>
…e_short_circuit_lazy_execution` The table key expressions are plain `ExpressionActions` shared by the sinks of a parallel insert; their copies made by `IStorage::setInMemoryMetadata` must stay plain. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
🕵 Pushed
Locally, |
|
🕵 Pushed The only failing test in the previous run was |
…settings history The current version is now 26.12, and the `settings_changes_history` style check requires new settings to be recorded under the current version. https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=114734&sha=8aa764799c472ec1140ce899f353afcd2780ce90&name_0=PR&name_1=Style%20check #114734 Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
🕵 Pushed |
|
🕵 Pushed The only failure in the previous run is unrelated: |
`05444_adaptive_short_circuit_low_cardinality_rows` failed once out of 50 runs in the flaky check with `Test runs too long (> 180s)`. The randomized `MergeTree` settings chose `index_granularity = 2`, so the 200000 rows of the table were split into 100000 marks. The first read of the table (with a cold cache) did 200000 buffer allocations and 200000 uncompressed cache misses, and under the thread fuzzer this single query took 236 seconds of wall time for 39 seconds of CPU. The second query, which read from the cache, took 0.6 seconds. The slowness is in reading the table and not in the logic under test. Set `index_granularity` and `index_granularity_bytes` explicitly, as `05153_adaptive_short_circuit_profile_across_blocks` already does, in this test and in the other tests of the feature which fill a `MergeTree` table with hundreds of thousands of rows. https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=114734&sha=ea8bd84aa0ca1905a010869c143c906372286ebc&name_0=PR&name_1=Stateless%20tests%20%28amd_binary%2C%20flaky%20check%29 #114734 Co-Authored-By: Claude Fable 5.2 <[email protected]>
|
🕵 Pushed
The six edited tests pass locally on a build of the branch before the merge. |
# Conflicts: # src/Processors/QueryPlan/JoinStepLogical.cpp
|
🕵 Pushed CI on the previous head (
There are no unresolved review threads. |
|
🕵 No new commits were needed on this branch. On the current head The only red check was |
|
🕵 Pushed The only failure on the previous head I believe this is unrelated to the PR: everything it adds is gated by @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=114734&sha=4a23358fe2fc89ed7537434861f50ee25e3a8542&name_0=PR&name_1=Stress%20test%20%28amd_tsan%29 and provide a fix in a separate PR. If the fix is already in progress, link it here. |
|
Acknowledged. I'm investigating the |
|
This run left no artifacts, but the same export took 64-72 min in 3 other Fix: #133786 runs the export with |
|
The engine side from my previous comment: #134077 makes planning a |
`collect_query_and_trace_logs` exports the system logs with
`clickhouse-local --config-file=/etc/clickhouse-server/config.xml`, which loads
the stress `default` profile (`ast_fuzzer_runs = 5`, `ast_fuzzer_any_query =
true`). After each export query `clickhouse-local` therefore runs up to 5
fuzzed variants of it synchronously before exiting. Their results are
discarded, so the export never needed them, and one of them can take over an
hour: in three `Stress test (amd_tsan)` runs a variant like
`SELECT * FROM merge(REGEXP('(?i)SYSTEM'), 'metric_log')` spent 64-72 min
building the plan for the `system.metric_log` child, since
`max_execution_time` is only checked between `Merge` children. On PR ClickHouse#114734
(60 min stress phase) that pushed the job past its 3 h timeout and no result
was uploaded.
Pass `--ast_fuzzer_runs 0`, as `stress.py` and `setup_log_cluster.sh` already
do for harness statements. The stress run itself keeps the fuzzer.
Validated with the stress profile over a populated server data directory:
the function ran 20 fuzzed queries on master and 0 with this change, with
identical exported files; `--ast_fuzzer_runs 5` brings them back.
Related: ClickHouse#114734 (comment)
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This continues #73776 by @lgbo-ustc, whose branch was ~17 months behind
masterand could no longer be merged mechanically (27 conflicting files in the hottest central code). The author's commits are preserved; on top of them this reconciles the change with the currentmasterand reworks the plumbing.Short-circuit evaluation decides statically which arguments of
and,or,ifandmultiIfare executed lazily. Lazy execution is not free: the rows which are not needed are filtered out and the result is expanded back to the original size. When the filtered out part is small, or the argument itself is cheap, this overhead is larger than the work it saves.This adds an opt-in adaptive mode: the execution of every function is profiled (elapsed time and processed rows), and every 20000 rows the decision is revisited per action - lazy execution is turned off when the estimated cost of the full execution is lower than the measured cost of the lazy execution, and turned back on when the per-row cost of the action grows significantly. Actions whose function can throw an exception on invalid input values (for example
intDiv(1, 0)) are never switched to eager execution, so the short-circuit protection is preserved.The mode is disabled by default and enabled with the new experimental setting
enable_adaptive_short_circuit_lazy_execution. It currently takes effect only together withshort_circuit_function_evaluation = 'force_enable': with the defaultenablemode, none of the lazily executed arguments is safe to switch to eager execution.Changes on top of the original branch:
ExpressionStep/FilterStepconstructors, the query plan optimizations, the planner and the step serialization. It lives inExpressionActionsSettingsnext toenable_lazy_columns_replication, which removes ~20 files from the change and all the ripple throughQueryPlan/Optimizations.ExpressionActions::executeandexecuteOnColumnsstayconst; the freeexecuteActionbecame a member with three virtual hooks (shouldExecuteLazily,executeFunction,finalizeBlockExecution) whichAdaptiveExpressionActionsoverrides.IExecutableFunctionchain, includingexecuteWithoutReplicatedColumns, which did not exist when the branch was written.is_no_exceptis computed only when the setting is enabled, so nothing changes for the default path.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):
Added an experimental setting
enable_adaptive_short_circuit_lazy_execution, which enables dynamic evaluation of whether a short-circuit function's argument should be lazily executed. The execution of the functions is profiled at runtime, and the lazy execution of an argument is disabled when it costs more than executing the argument on all the rows.Related: #73776
Workflow [PR]
Sync PR [sync-upstream/pr/114734]