Repository navigation
Follow the input order for events that share a timestamp in sequenceMatch, sequenceCount, sequenceMatchEvents and sequenceNextNode - #124812
Conversation
…enceMatchEvents and sequenceNextNode The result of these functions depends on the order in which events that share a timestamp reach them, but a user could not control that order: - The four functions were registered without `is_order_dependent` (`sequenceNextNode` set it to `false` explicitly), so `removeRedundantSorting` dropped a subquery `ORDER BY` feeding them, while it keeps one under `groupArray`. - `sequenceMatch`, `sequenceCount` and `sequenceMatchEvents` sorted their events by timestamp with the unstable `::sort`, so even a kept `ORDER BY` did not decide the order of tied events (a debug build shuffles before sorting, so the result varied between runs). Flag the four registrations and sort with `::stableSort`; `merge` appends the other state after this one, so ties keep that order, as for `groupArray`. `sequenceNextNode` already sorts stably and still breaks timestamp ties by the event value. The docs said the order of tied events is undefined; they now say it follows the input order, which is defined when the input is sorted, and the `sequenceNextNode` section states how it breaks timestamp ties (by value, then by input order). The event was a `std::pair`, which is not trivially copyable, so `::stableSort` fell back to `std::stable_sort`, a merge sort that does not detect an already sorted input (2.26x slower than `::sort` on one sorted group of 20M rows). It is now a plain struct with the same `first` and `second` members, so `::stableSort` runs driftsort: 0.65x to 1.03x of the old `::sort` time (release, median of 7). The state is still serialized field by field, so its format is unchanged. Events that are already ordered by timestamp, for example after a subquery `ORDER BY t`, skip the sort: driftsort allocates its scratch buffer before it detects the existing run, while `::sort` did not allocate for them. The test runs one function per query, because the optimization keeps the sort for the whole aggregation as soon as one function in it is order-dependent. Requested in ClickHouse#124149 (comment) Co-Authored-By: Claude Opus 5.5 <[email protected]>
State the TimestampEvents trivially-copyable requirement as an invariant instead of a comparison with std::pair. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex) plus my own review; 8 findings over 4 rounds, the last round clean.
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-9:20261009-031700 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-4:20261009-024800 |
|
Workflow [PR], commit [e9e3b8b] Summary: ❌
AI ReviewSummaryThis PR makes Missing context / blind spots
Final Verdict✅ Looks good to me. No new inline findings to post. |
CI finish ledger - e9e3b8bEvery failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
All 163 check runs completed. The other three Session id: cron:our-pr-ci-monitor:20261009-050007 |
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
sequenceMatch,sequenceCount,sequenceMatchEventsandsequenceNextNodenow follow the input order for events that share a timestamp (forsequenceNextNode, events that also share the value), so sorting the input in a subquery, for exampleORDER BY time, id, decides how such events are matched.Description
Related: #124149 (follow-up asked for in #124149 (comment)).
On 26.9.9.28 (also 26.8, 26.3):
Two causes:
is_order_dependent(sequenceNextNodesets it tofalse), soremoveRedundantSortingdrops the subqueryORDER BY.sequenceMatch,sequenceCountandsequenceMatchEventssort events with the unstable::sort, so even a keptORDER BYdoes not decide tied events.Change: flag the four registrations;
AggregateFunctionSequenceMatchData::sortskips already ordered events and sorts the others with::stableSort, on a trivially copyable struct instead ofstd::pairso that driftsort runs, notstd::stable_sort.sequenceNextNodealready sorts stably but breaks timestamp ties by the event value first; I can drop that tie-break if you prefer.Released docs call the order of tied events undefined, hence
Improvement.Siblings:
windowFunnel(checked on 96 queries) andretentiondo not depend on the input order.Cost (release, median of 7, 20M rows): a subquery
ORDER BYfeeding these functions is now kept, so its sort is paid (3.58 s withORDER BY tvs 0.86 s without). Without it, vs before: one random group 0.63x, one sorted group 1.02x, 1M groups 0.85x, all-ties groups 0.99x. Sorting unordered events needs driftsort's scratch buffer (up to the event list's size): 1.22x peak memory on one 500K-event group.Test
05356_sequence_functions_tie_order, one function per query: every line differs from the reference on master; 51/51 with randomized settings. The four #124149 oracle-list entries go once it is merged.Workflow [PR]
Sync PR [sync-upstream/pr/124812]