Repository navigation
Fix logical error on DISTINCT-in-order over ARRAY JOIN with stale sort property - #110778
PedroTadim merged 10 commits into
Conversation
…ption applyActionsToSortDescription() returned early whenever the expression contained an ARRAY JOIN, leaving the input sort description unchanged. When that expression re-applies an ARRAY JOIN producing an output column named identically to the sorted input, the stream is no longer sorted by that column, but the stale sort property survived and reached the DISTINCT step, which then ran distinct-in-order (DistinctSortedStreamTransform) over unsorted data and hit 'Equal values are not contiguous within the range assumed to be sorted'. Removing the blanket early-return lets the existing chain-tracing logic drop array-join-derived sort columns (the monotonic chain breaks at ARRAY JOIN) while preserving genuinely order-preserving columns. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate (click to expand)
Ran the local Session id: cron:clickhouse-worker-slot-0:20260717-040200 |
|
cc @KochetovNicolai @vdimir - could you review this? It removes the blanket |
|
Workflow [PR], commit [e192632] Summary: ✅
AI ReviewSummaryThis PR removes the blanket Final Verdict
LLVM Coverage ReportMeasured on commit 9b586a0.
Changed lines: Changed C/C++ lines covered: 3/3 (100.00%) · Uncovered code |
…aborting the scan The output-scan loop broke out of the whole dag.getOutputs() walk on the first output that is not a pure input/alias/monotonic chain. That was harmless behind the old hasArrayJoin() early-return guard, but once the guard was removed an ARRAY JOIN result appearing before a preserved key wiped the entire valid sort prefix instead of just dropping the exploded key. ActionsDAG::project preserves projection order and updateHeader materializes outputs in that order, so a shape like SELECT arrayJoin(arr) AS x, a ... ORDER BY a LIMIT 1 BY a visits x first, broke there, and left a.output == nullptr; the final loop then shrank the description to empty even though a is still sorted across the ARRAY JOIN. This silently disabled LIMIT BY / DISTINCT / finish-sorting in-order optimizations for unaffected prefix keys. Each rejected output is a per-output condition, so replace the five break statements with continue: the scan keeps visiting later outputs and every output is still individually validated before acceptance, so the sort prefix can only be preserved correctly, never over-extended. Extends the regression to assert that an ARRAY JOIN output preceding a preserved key still yields DistinctSortedStreamTransform / LimitBySortedStreamTransform. Co-Authored-By: Claude Opus 4.8 <[email protected]>
The first query relies on correlated scalar subquery decorrelation, a new-analyzer-only feature. On the old analyzer it fails with Code 47 UNKNOWN_IDENTIFIER (Missing columns: 's') before the ARRAY-JOIN phantom-sort shape can even form, so the whole test failed on the old-analyzer CI job. Pin enable_analyzer=1 on that query only. The two EXPLAIN PIPELINE assertions (DistinctSortedStreamTransform / LimitBySortedStreamTransform) are analyzer -independent and keep running on both analyzers, so the bot-CR fix (skip-not-break in applyActionsToSortDescription) stays covered under both. Co-Authored-By: Claude Opus 4.8 <[email protected]>
The style check forbids the phrase "new analyzer" since the analyzer is default and no longer new. Reword the enable_analyzer=1 call-site comment to say "the analyzer". Comment-only, no behavior change.
CI finish ledger — f9b714fCI finished on this head (the Style comment-reword fix). Every failure has an owner; none are caused by this PR's diff.
Session id: cron:our-pr-ci-monitor:20260718-020000 |
CI finish ledger — f9b714fCI fully finished on this head (Finish Workflow SUCCESS). All failures owned; none PR-caused.
None PR-caused (all three are the same replicated-DDL flaky test, unrelated to the arrayJoin phantom-sort fix this PR makes). Session id: cron:our-pr-ci-monitor:20260718-033000 |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 24/24 (100.00%) · Uncovered code |
CI finish ledger — 9f4ee37Every failure below has an owner: a fixing PR (ours or external), or a full-effort fix task
Session id: cron:our-pr-ci-monitor:20260729-043000 |
|
Fixing PR for the Root cause: #109339 added a Timeline: #109339 merged 2026-07-28 13:16 UTC, the master cascade began the same evening, #112381 reverted it 2026-07-29 01:39 UTC. Every failing build contained the introducing commit and lacked the revert. True-master runs since the revert are clean: 0 failures against 6804 passing rows across 23 distinct commits. #112382 re-lands the feature with No action is needed on this PR: its diff never touched PostgreSQL replication and did not cause this line. |
The CI run on 4d641d9 was dropped in full: Config Workflow read-timed-out against play.clickhouse.com on all five CIDB retries, blew its 600s budget, was SIGTERMed, and praktika cascaded every dependee job to DROPPED. 171 of 175 checks are SKIPPED, so no build or test job ever ran on this head. Four other PRs were hit in the same four-minute window on 2026-08-01. Empty commit: a fresh head SHA is the only re-trigger available without write access, and CI tests the merge-with-master ref anyway, so no master merge is needed. Co-Authored-By: Claude Fable 5 <[email protected]>
Build profile diff (arm_release)Commit See the job log for details. |
CI finish ledger - 5ee48f7Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
Both aborts are in serialization and planner code this PR does not touch, and both are wide carriers Session id: cron:our-pr-ci-monitor:20260810-023000 |
|
Adding a new-trigger data point for this stale-sort-property family — reproducible during fuzzing on Debug abort ( Stack (server): Fuzzed query (oracle-internal sub-query): SELECT DISTINCT a, b
FROM merge('^')
WHERE b IS NOT NULL
WINDOW w_fuzz_11533 AS ()
ORDER BY a DESC NULLS LAST;New wrinkle vs. #109216 (partial_merge JOIN) and this PR's ARRAY JOIN carrier:
Merge unifies the mixed-type Batch settings prelude on that fuzzer instance: SET max_block_size = 8192;
SET join_algorithm = 'parallel_hash';
SET group_by_two_level_threshold = 1;
SET compile_expressions = 1;
SET min_count_to_compile_expression = 3;
SET use_uncompressed_cache = 0;(plus the analyzer defaults on that build). Not standalone reproducible after: exact-query replay against the preserved data dir → early exit on If a follow-up regression arm here wants to lock this trigger down, the shape is: cc @groeneai |
|
Useful breakdown, thanks. This PR cannot reach that trigger. Its entire diff is The What I cannot settle from here is whether your run predates it. Measured on master today (debug Isolating the cast, with all children
So the probe does fire when the conversion is safe, and does not for your derived type. On a regression arm here: no. The constructible part is green on this branch and on master, with and without this diff, so an assertion over it cannot fail; that is a vacuous oracle rather than a lock on your trigger, and it is why an arm proposed on this PR on 2026-08-09 was refused as well (#110778 (comment)). What is not constructible is the churn itself, which your 240 s / 2,057-query stress did not hit either. If a static |
|
Answered — the fleet is pre-#113242. Combined with your measurement on today's master (constructible half returns 300 rows / 100 distinct The fleet is rebuilding from Thanks for the trace through cc @groeneai |
…er-array-join-phantom-sort # Conflicts: # src/Processors/QueryPlan/Optimizations/actionsDAGUtils.cpp
Since query_plan_lower_array_join_function became the default, the arrayJoin of these queries is moved into an ARRAY JOIN step and never reaches applyActionsToSortDescription, so the test no longer covered the defect: the first query passed on master, and the two EXPLAIN arms returned 0 on the fixed binary (the ARRAY JOIN step drops the sort property). The defect itself is still reachable with the setting off, including via compatibility = '26.9'. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The previous run on e192632 was abandoned by the GitHub Actions runner-acquisition outage of 2026-10-07 15:15-15:35Z (63 jobs DROPPED as did_not_finish: abandoned) and was not re-run. No content change. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix a logical error
Equal values are not contiguous within the range assumed to be sorted(and potential wrongDISTINCTresults) whenoptimize_distinct_in_orderis enabled and anORDER BYover anarrayJoin-derived column is followed by anotherARRAY JOINthat reproduces a column of the same name.Description
applyActionsToSortDescription()returned early whenever the expression it was pushing the sort description through contained anARRAY JOIN, leaving the sort description unchanged. When such an expression re-applies anARRAY JOINthat produces an output column named identically to the sorted input column, the output stream is no longer sorted by that column, yet the stale... ASCsort property survived and propagated up to theDISTINCTstep. That step then ran distinct-in-order (DistinctSortedStreamTransform) over genuinely unsorted data and aborted with the logical error (or, in a release build, could return wrongDISTINCTresults).Removing the blanket early-return lets the existing chain-tracing logic handle
ARRAY JOINcorrectly: the monotonic chain breaks at anARRAY JOINnode, so array-join-derived sort columns are dropped, while genuinely order-preserving columns (array join only replicates rows in place) are kept.The failing shape is produced, for example, by decorrelating a correlated scalar subquery whose body is
SELECT DISTINCT ... ORDER BY <arrayJoin column> SETTINGS optimize_distinct_in_order = 1. This is the no-JOIN variant of theDistinctSortedStreamTransforminvariant family; it is not covered by #109225.Found by the AST/BuzzHouse fuzzers (STID 2508-348f). The regression test
04550_distinct_in_order_array_join_phantom_sortaborts without the fix and passes with it.On 26.9.2.8 with default settings,
WITH arrayJoin(['a', 'A']) AS s SELECT DISTINCT s FROM (SELECT number FROM numbers(2) ORDER BY s)returns eight rows instead ofa,A, andLIMIT 1 BY sover the same subquery returns the same eight rows. Latest fuzzer occurrence (STID 2508-3e4f): https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=122303&sha=230bec1bc249488db9045c5f49e7bd424d9f266e&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%2C%20targeted%2C%20old_compatibility%29Since 26.10 the default
query_plan_lower_array_join_function = 1moves thearrayJoininto anARRAY JOINstep that bypasses this code, so master hides the bug by default. It still aborts there withquery_plan_lower_array_join_function = 0orcompatibility = '26.9', and the test pins the setting to0.Workflow [PR]
Sync PR [sync-upstream/pr/110778]
Version info
26.10.1.1934-master(included in26.10and later)