Skip to content

Randomize query_plan_optimize_join_order_randomize in stateles - #105995

Merged
KochetovNicolai merged 8 commits into
masterfrom
vdimir/randomize-join-order-stateless
Jul 3, 2026
Merged

KochetovNicolai merged 8 commits into
masterfrom
vdimir/randomize-join-order-stateless

Conversation

@vdimir

@vdimir vdimir commented May 27, 2026

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Build/Testing/Packaging Improvement

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

  • Randomize query_plan_optimize_join_order_randomize in stateless tests

@clickhouse-gh

clickhouse-gh Bot commented May 27, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [8beff7b]

Summary: ✅


AI Review

Summary

This PR moves query_plan_optimize_join_order_randomize into normal stateless random settings and pins or sorts many affected tests. The direction is fine, but the current patch still leaves several deterministic-test contracts exposed to the new randomizer, and the 1 sentinel seed remains non-replayable for automated random-settings diagnosis.

Findings

⚠️ Majors

  • [tests/clickhouse-test:1525] [dismissed by author -- https://github.com/ClickHouse/ClickHouse/pull/105995#discussion_r3475744756] Normal random settings can still emit query_plan_optimize_join_order_randomize=1, but QueryPlanOptimizationSettings expands 1 with randomSeed only inside the server while save_random_settings_artifact records the original value. The author noted that the materialized seed is printed in server logs, but --diagnose-random-settings replays the saved artifact, so automated reproduction and minimization still rerun with a different seed unless someone manually scrapes logs and rewrites the setting. Emit only concrete seeds (>= 2) from the normal randomizer, or persist the materialized seed into the artifact used by diagnosis.
  • [tests/queries/0_stateless/01720_country_intersection.sh:10] The outer ORDER BY c, d does not make this test deterministic because the inner LIMIT 100 still selects the first 100 rows before sorting. With randomized join stats, the CROSS JOIN can produce a different first 100 pairs and then sort that different subset. Move ORDER BY first.name, second.name before LIMIT 100 inside the subquery, or pin query_plan_optimize_join_order_randomize = 0 for the query. Existing thread: Randomize query_plan_optimize_join_order_randomize in stateles #105995 (comment)
  • [tests/queries/0_stateless/02731_analyzer_join_resolve_nested.sql.j2:145] The test still leaves multi-row FULL JOIN outputs over ttta/tttb and nnna/nnnb unsorted while allowing random join-order stats. Sorting only the preceding comma joins does not protect these references; a randomized swap can change emitted row order. Pin query_plan_optimize_join_order_randomize = 0 near the existing SET join_algorithm = 'hash', or add deterministic ordering to every multi-row join result. Existing thread: Randomize query_plan_optimize_join_order_randomize in stateles #105995 (comment)
  • [tests/queries/0_stateless/04300_dedup_subtrees_on_filter_merge.sql:14] The EXPLAIN assertion is side-sensitive: it counts text containing __table2, but the file does not pin query_plan_optimize_join_order_randomize or query_plan_join_swap_table. Once the setting is randomized normally, equal numbers(10000) inputs can receive different synthetic stats and flip sides, making the count fail even though the optimization still works. Pin the randomizer or disable join swapping for this test. Existing thread: Randomize query_plan_optimize_join_order_randomize in stateles #105995 (comment)
Missing context / blind spots
  • ⚠️ I did not run the affected stateless tests locally. The Praktika CI helper found no failed checks in the latest report, but that does not prove the unpinned random-seed cases above are covered.
Final Verdict

⚠️ Needs changes before approval. No new inline findings were posted in this run because the remaining issues already have review threads.

@clickhouse-gh clickhouse-gh Bot added the pr-build Pull request with build/testing/packaging improvement label May 27, 2026
@vdimir
vdimir force-pushed the vdimir/randomize-join-order-stateless branch from 4e485d2 to 7416f26 Compare May 28, 2026 09:00
Comment thread tests/clickhouse-test Resolved
@vdimir
vdimir force-pushed the vdimir/randomize-join-order-stateless branch from 7416f26 to 4b6a5a0 Compare June 25, 2026 15:43
Comment thread tests/clickhouse-test
@@ -1584,7 +1584,6 @@ class SettingsRandomizer:
}

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.

Moving query_plan_optimize_join_order_randomize out of stress_test_only_settings makes normal random-settings failures depend on this value being replayable. The generator still emits 1 10% of the time, and QueryPlanOptimizationSettings expands 1 with randomSeed, but save_random_settings_artifact only records the original setting value. A failed stateless test would therefore be saved as query_plan_optimize_join_order_randomize=1, and --diagnose-random-settings would rerun it with a different join-order seed, so the failure cannot be reliably reproduced or minimized. Please make the normal randomizer emit only concrete seeds (>= 2), or materialize the generated seed before saving/rerunning the test.

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.

We print randomization seed in server logs

@@ -1,3 +1,5 @@
SET query_plan_optimize_join_order_randomize = 0; -- TODO: TOTALS over CROSS JOIN with constant table loses build-side values when the join is swapped (pre-existing bug)

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.

Fixing separately in #108807

@vdimir
vdimir force-pushed the vdimir/randomize-join-order-stateless branch from 6cfb240 to ca2c8b8 Compare June 29, 2026 11:55
${CLICKHOUSE_CLIENT} -q "create table country_polygons(name String, p Array(Array(Tuple(Float64, Float64)))) engine=Memory()"
cat ${CURDIR}/country_polygons.tsv | ${CLICKHOUSE_CLIENT} -q "insert into country_polygons format TSV"
${CLICKHOUSE_CLIENT} -q "SELECT c, d, polygonsIntersectionSpherical(a, b) FROM (SELECT first.p AS a, second.p AS b, first.name AS c, second.name AS d FROM country_polygons AS first CROSS JOIN country_polygons AS second LIMIT 100) format TSV"
${CLICKHOUSE_CLIENT} -q "SELECT c, d, polygonsIntersectionSpherical(a, b) FROM (SELECT first.p AS a, second.p AS b, first.name AS c, second.name AS d FROM country_polygons AS first CROSS JOIN country_polygons AS second LIMIT 100) ORDER BY c, d format TSV"

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.

Placing ORDER BY outside this subquery does not make the test deterministic: the inner LIMIT 100 still selects whichever first 100 rows the CROSS JOIN produces. Once query_plan_optimize_join_order_randomize is injected in normal stateless runs, the join can be planned/probed in the opposite order, so a different 100 country pairs can be selected before the outer sort and the reference can still change. Please either pin query_plan_optimize_join_order_randomize = 0 for this query or move a deterministic ORDER BY first.name, second.name before LIMIT 100 inside the subquery.

@vdimir
vdimir force-pushed the vdimir/randomize-join-order-stateless branch from ca2c8b8 to 2bf018b Compare June 29, 2026 16:01
SELECT t FROM ttta, tttb;
SELECT t.t.t FROM ttta, tttb;
SELECT t.t.t.t.t FROM ttta, tttb;
SELECT t FROM ttta, tttb ORDER BY ALL;

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.

Adding ORDER BY only to these CROSS JOIN outputs does not make the test safe under the new normal randomizer. The same file still has later multi-row FULL JOIN outputs over ttta/tttb and nnna/nnnb without ordering, and optimizeJoin can still treat a two-relation FULL join as swap-only. In a randomized stateless run where query_plan_join_swap_table = 'auto', randomized relation stats from query_plan_optimize_join_order_randomize can flip the physical sides and change the emitted row order, so this test can still produce different references depending on the generated seed. Please either pin query_plan_optimize_join_order_randomize = 0 near the existing SET join_algorithm = 'hash', or sort every multi-row join result in the file.

Comment thread tests/clickhouse-test
@@ -1585,7 +1585,6 @@ class SettingsRandomizer:
}

stress_test_only_settings = {

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.

Moving query_plan_optimize_join_order_randomize into normal stateless random settings still leaves at least one side-sensitive EXPLAIN test unpinned. tests/queries/0_stateless/04300_dedup_subtrees_on_filter_merge.sql:14 expects the join action text to contain __table2; normal runs already randomize query_plan_join_swap_table to auto, and with this new randomized stats seed the two equal numbers(10000) inputs can get different synthetic row counts and flip the physical join side. That makes the countIf(... __table2 ...) assertion report 0 depending on the generated seed. Please pin query_plan_optimize_join_order_randomize = 0 or query_plan_join_swap_table = false in that test before enabling this setting globally.

@clickhouse-gh

clickhouse-gh Bot commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.82% 85.83% +0.01%
Functions 91.46% 91.46% +0.00%
Branches 78.27% 78.30% +0.03%

Changed lines: No C/C++ source files changed — skipping uncovered code analysis.

Newly covered by added/modified tests: 92 line(s), 1 function(s) across 40 file(s) · Details

Top files
  • src/Functions/array/arrayLevenshtein.cpp: 9 line(s)
  • src/Functions/FunctionsCodingIP.cpp: 7 line(s)
  • src/Functions/if.cpp: 7 line(s)
  • src/Server/DistributedQuery/StreamingExchangeSink.cpp: 6 line(s), 1 function(s)
  • src/Functions/dateName.cpp: 5 line(s)

Full report

@KochetovNicolai
KochetovNicolai added this pull request to the merge queue Jul 3, 2026
Merged via the queue into master with commit b54791c Jul 3, 2026
174 checks passed
@KochetovNicolai
KochetovNicolai deleted the vdimir/randomize-join-order-stateless branch July 3, 2026 12:44
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 3, 2026
@clickgapai

Copy link
Copy Markdown
Contributor

Hi @vdimir @KochetovNicolai — a couple of suggestions to make this PR easier for reviewers and the changelog reader.

Title (currently vague):

  • Current: Randomize query_plan_optimize_join_order_randomize in stateles
  • Why: Contains a truncated/misspelled word ('stateles' instead of 'stateless') and is awkwardly self-referential ('Randomize query_plan_optimize_join_order_randomize'). Does not convey that ~50 tests were pinned/ordered to preserve determinism.
  • Suggested: Randomize query_plan_optimize_join_order_randomize in stateless tests

Description (currently missing):

  • Why: PR description is essentially empty aside from the changelog category; it does not explain that the setting is moved out of stress-only randomization and that affected tests were pinned to 0 or given explicit ORDER BY.

  • Suggested addition:

    Moves query_plan_optimize_join_order_randomize out of stress_test_only_settings so it is randomized in regular stateless runs. Tests that assert on join plan/order via EXPLAIN are pinned with SET query_plan_optimize_join_order_randomize = 0; tests relying on join output order are given explicit ORDER BY.

Ignore this if the current title/body is intentional.

zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…-join-order-stateless

Randomize query_plan_optimize_join_order_randomize in stateles
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-build Pull request with build/testing/packaging improvement 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.

4 participants