Skip to content

Fix join reorder applying type changes at wrong step - #102351

Merged
alexey-milovidov merged 21 commits into
masterfrom
fix/join-reorder-type-changes
May 10, 2026
Merged

alexey-milovidov merged 21 commits into
masterfrom
fix/join-reorder-type-changes

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Apr 10, 2026 •

Copy link
Copy Markdown
Member

This PR is a follow-up to #102516, which already fixed the "Cannot fold actions for projection" exception in join reorder optimization with type-changing joins.

#102516 added the regression test as 04097_join_reorder_type_changes.sh. This PR replaces it with the equivalent .sql form (04095_join_reorder_type_changes.sql), which is preferable per project convention — it does not require bash and runs through the standard query path. Same schema, data, query, settings, and expected output; only the file format changes.

Original stress test (arm_msan) report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=101292&sha=2d129c6df44b0ed28a05397e55abe0cbbff9bb1e&name_0=PR&name_1=Stress%20test%20%28arm_msan%29

Upstream PR: #101292
Original fix PR: #102516

Changelog category (leave one):

  • Not for changelog

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

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.5.1.496

When the join optimizer reorders a multi-table join, type changes
(e.g., `toNullable` from a LEFT JOIN) were applied at the first step
involving the affected relation, regardless of whether the join that
causes the type change was actually being executed at that step.

For example, with `t1 LEFT JOIN t2 INNER JOIN t3`, if the optimizer
reorders to join t2-t3 first, the Nullable wrapping for t2 (from the
LEFT JOIN with t1) was incorrectly applied at the t2-t3 step. This
replaced original input nodes in `current_input_nodes`, so when the
t1-t2 LEFT JOIN expression was later processed, it could not find the
original `t2.id` INPUT node — causing the exception "Cannot fold
actions for projection".

The fix checks the `join_kinds` dependency before applying type changes:
a type change for a relation is only applied when the other side of the
type-changing join is also present in the current step's joined set.

https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=101292&sha=2d129c6df44b0ed28a05397e55abe0cbbff9bb1e&name_0=PR&name_1=Stress%20test%20%28arm_msan%29
#101292

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Apr 10, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [83b77da]

Summary: ✅


AI Review

Summary

This PR converts the regression coverage for the join-reorder/type-change exception from 04097_join_reorder_type_changes.sh to an equivalent 04095_join_reorder_type_changes.sql test and renames the matching reference file. The change is scoped, consistent with test conventions, and does not introduce review findings.

ClickHouse Rules
Item Status Notes
Deletion logging ➖
Serialization versioning ➖
Core-area scrutiny ✅
No test removal ✅
Experimental gate ➖
No magic constants ✅
Backward compatibility ✅
SettingsChangesHistory.cpp ➖
PR metadata quality ✅
Safe rollout ➖
Compilation time ✅
No large/binary files ✅
Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Apr 10, 2026
Do not flatten joins that have type-changing sides (e.g., LEFT JOIN
with `join_use_nulls` making right-side columns Nullable) into the
parent join graph for reordering. Flattening such joins allows the
optimizer to reorder them, which can separate a relation from the
join that causes its type change, leading to the exception "Cannot
fold actions for projection" when the original INPUT node is no
longer found in `current_inputs`.

The fix adds a check in `addChildQueryGraph`: if the child join step
has any type-changing sides (detected via `typeChangingSides`), it
is not decomposed into the parent graph. Instead, it is kept as a
single opaque relation. This preserves the type-change semantics
while still allowing reordering of other joins in the query.

https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=101292&sha=2d129c6df44b0ed28a05397e55abe0cbbff9bb1e&name_0=PR&name_1=Stress%20test%20%28arm_msan%29
#101292

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
The previous form of the test used 3 tables with a `LEFT JOIN` + `INNER
JOIN` but did not actually reproduce the `Cannot fold actions for
projection` exception on the base version (without the fix), so the
`Bugfix validation (functional tests)` job reported it as not
reproducing the bug - see
https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=102351&sha=889a9199aa898d43e0909c98c523b0e6aa003e8e&name_0=PR&name_1=Bugfix%20validation%20%28functional%20tests%29

The new form uses a minimal 4-table reproducer with two chained
`LEFT JOIN`s followed by an `INNER JOIN` whose `ON` condition references
a column from the nested `LEFT JOIN` together with an `IS NOT NULL`
predicate. With `join_use_nulls = 1`, the optimizer flattens the child
`LEFT JOIN` into the join graph and reorders relations so that a
type-changing step is applied at the wrong point, triggering the
exception on unfixed servers.
@clickhouse-gh

clickhouse-gh Bot commented Apr 25, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 83.80% 83.80% +0.00%
Functions 91.10% 91.10% +0.00%
Branches 76.20% 76.20% +0.00%

Changed lines: 100.00% (6/6) | lost baseline coverage: 1 line(s) · Uncovered code

Full report · Diff report

The `04097_join_reorder_type_changes.sh` test added in #102516 covers
the same regression as `04095_join_reorder_type_changes.sql`: same
schema, data, query, settings, and expected output. The `.sql` form is
preferable per project convention — it does not require bash and runs
through the standard query path. Drop the duplicate.
@clickhouse-gh clickhouse-gh Bot added pr-not-for-changelog This PR should not be mentioned in the changelog and removed pr-bugfix Pull request with bugfix, not backported by default labels May 6, 2026
`f1873a9bdb0` accidentally re-introduced the pre-revert version of
`.github/workflows/backfill_distroless.yml`, which master had already
reverted in #103567. This commit re-aligns the file with master so the
PR diff is limited to the `04095_join_reorder_type_changes.sql`
regression test, as described in the PR body.

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good, test-only change.

@alexey-milovidov alexey-milovidov self-assigned this May 10, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue May 10, 2026
Merged via the queue into master with commit fd6a717 May 10, 2026
165 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix/join-reorder-type-changes branch May 10, 2026 17:10
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label May 10, 2026
@alexey-milovidov alexey-milovidov mentioned this pull request May 12, 2026
1 task done
groeneai added a commit to groeneai/ClickHouse that referenced this pull request May 14, 2026
Before this change, `tests/docker_scripts/stress_runner.sh` exported
`THREAD_POOL_FAULT_INJECTION=1` before `configure`, so install.sh
symlinked `tests/config/config.d/cannot_allocate_thread_injection.xml`
into the server's `config.d/` and the server started with
`cannot_allocate_thread_fault_injection_probability=0.0001` already
active. `stress.py` then ran a small smoke check (two canary tests)
to validate the setup. The smoke check did, among other things:

    CREATE DATABASE IF NOT EXISTS test_N
        ON CLUSTER test_cluster_database_replicated
        ENGINE = Replicated(...)

When thread-pool fault injection fired during the async load of the
Replicated database (`Code: 439. Cannot schedule a task: fault
injected`), the server raised the exception after it had already
started streaming the HTTP response, so
`WriteBufferFromHTTPServerResponse` deliberately broke the chunked
encoding. The client (`clickhouse-test` → `clickhouse_execute_http`)
saw `http.client.IncompleteRead`, the per-test retry was exhausted,
and the smoke check aborted the whole stress test job.

The protocol break is by design — the server uses it to signal that a
streamed response is incomplete and must not be consumed as if it were
successful. Retrying it on the client (the previous attempt) would mask
genuine server failures.

Instead, this commit applies @azat's prescription: run the smoke check
against a server that does *not* have the thread-pool fault injector
installed, then enable it only once the smoke check has passed.

- `tests/docker_scripts/stress_runner.sh` no longer exports
  `THREAD_POOL_FAULT_INJECTION=1` before `configure`. install.sh leaves
  `cannot_allocate_thread_injection.xml` uninstalled. The server starts
  with `cannot_allocate_thread_fault_injection_probability=0` (default),
  so the smoke check runs deterministically.
- `stress.py` gains `--enable-thread-pool-fault-injection`. When set,
  `run_func_test` calls a new `install_thread_pool_fault_injection()`
  helper after the smoke-check loop. The helper symlinks the canonical
  config and runs `SYSTEM RELOAD CONFIG` with retries (the reload can
  itself transiently fail under ZooKeeper fault injection and
  ThreadFuzzer). `stress_runner.sh` passes the flag; the upgrade runner
  does not. If the smoke check raises, the install path is not reached
  and the post-stress cleanup `rm -f`s any stale symlink.
- The obsolete `CANNOT_SCHEDULE_TASK` / `Fault injection` entries are
  removed from the smoke-check `ignored_errors` list. Tolerating them
  would now mask a genuine setup failure since the injector is not
  active during the smoke check.

The failure mode recurs ~weekly across unrelated master PRs (e.g.
ClickHouse#104058, ClickHouse#103490, ClickHouse#102351, ClickHouse#102610, ClickHouse#102203, and master CI on
2026-04-28). Tracked by @alexey-milovidov's directive on PR ClickHouse#96844:
ClickHouse#96844 (comment)

Failing report example (PR ClickHouse#96844, commit 9c4f6f5, MSan stress):
https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=96844&sha=9c4f6f5a34d4bb45fcf73f20c464ba8d855a0c1f&name_0=PR&name_1=Stress%20test%20%28arm_msan%29

Validation: `tmp/test_install_thread_pool_fault_injection.py` (not
committed) covers (a) the symlink+reload mechanics, (b) fail-close when
the source config is missing, (c) flag off → no install, (d) flag on →
install runs once after smoke check, (e) install does not run when
smoke check raises an unrecoverable error, (f) the obsolete entries
are gone from `ignored_errors`. All six pass.
@groeneai groeneai mentioned this pull request May 17, 2026
1 task done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-not-for-changelog This PR should not be mentioned in the changelog pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants