Repository navigation
Fix sequenceMatchEvents returning an event chain that does not match the pattern - #118497
Conversation
…tern
sequenceMatchEvents could return an event chain that its own pattern does not
match, and that sequenceMatch rejects on the same input. With a time gate:
SELECT sequenceMatch('(?1)(?t<=10)(?2)(?3)')(t, e='A', e='B', e='C'),
sequenceMatchEvents('(?1)(?t<=10)(?2)(?3)')(t, e='A', e='B', e='C')
FROM values('t UInt32, e String', (0,'A'),(5,'B'),(100,'B'),(101,'C'));
returned 0 and [0,100,101], where 100 - 0 > 10 violates the gate. It now
returns [0,5], the longest chain that does satisfy the pattern.
The template parameter remember_matched_events was meant to add bookkeeping,
but it also changed the search. do_push_event pushed an extra backtrack frame
for every matched SpecificEvent, and a frame means "retry this action at the
next event", so in that instantiation alone a SpecificEvent could skip forward
while earlier matches stayed in place. The pattern authorises such a gap only
through .* or a (?t...) action, each of which pushes its own frame. After
SpecificEvent(2) failed at B@100, backtracking restored action_it to
SpecificEvent(1) and skipped the TimeLessOrEqual action, so the gate was never
re-evaluated for the new position and A@0, B@100, C@101 was accepted.
Removing that frame makes do_revert_event_if_needed's single pop insufficient,
since one backtrack can now rewind past several matched actions, so it becomes
a loop. Without that, sequenceMatchEvents('(?1)(?1)(?1)(?1)(?1)') would leave
stale entries and assemble a chain from two different start positions.
The defect also affects patterns with no time gate at all: on (0,'A'),(1,'B'),
(2,'B'),(3,'C') the pattern (?1)(?2)(?3) returned [0,2,3], a length-3 array
for a three-condition pattern, while sequenceMatch returned 0. Since fullness
is tested by comparing the array size to the number of conditions, a caller
concluded a full match had occurred when none had.
Both edited lambdas are called only under if constexpr (remember_matched_events),
so sequenceMatch and sequenceCount are unchanged by construction. Their 46
assertions and the 23 pre-existing sequenceMatchEvents assertions in 00222 are
byte-identical before and after.
Closes ClickHouse#118120
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex) plus a separate cold read of the
Checked independently of the gates: all 23 pre-existing Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-8:20260907-001501 |
|
cc @Algunenano @rschu1ze, could you review this? |
|
Workflow [PR], commit [1f22303] Summary: ✅
AI ReviewSummaryThis PR removes the extra per- Findings
Final VerdictThe matcher fix is correct for the bug this PR targets, but the LLVM Coverage ReportMeasured on commit 1f22303.
Changed lines: Changed C/C++ lines covered: 10/10 (100.00%) · Uncovered code |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units8 translation units recompiled, 20 s compile time in total, 8 of them have a recent master baseline. |
CI finish ledger - 6e6cbb2Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
Session id: cron:our-pr-ci-monitor:20260907-060021 |
…events-invalid-chain
Closes: #118120
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed
sequenceMatchEventsreturning an event chain that the pattern does not authorize, for example one that skips a(?t...)time gate between two of the returned events. The returned chain now always matches a prefix of the pattern. Closes #118120.Description
100 - 0 > 10violates the gate, andsequenceMatchrejects that chain. It now returns[0,5], the longest chain the pattern does match.remember_matched_eventswas meant to add bookkeeping, but it also changed the search:do_push_eventpushed a backtrack frame for every matchedSpecificEvent, and a frame means "retry this action at the next event". So in that instantiation alone aSpecificEventcould skip forward while earlier matches stayed in place, a gap the pattern authorizes only through.*or a(?t...)action. Here backtracking restoredaction_itpastTimeLessOrEqual, so the gate was never re-evaluated. Removing the frame makesdo_revert_event_if_needed's single pop insufficient, since one backtrack can now rewind past several matched actions, so it becomes a loop.Gate-free patterns are affected too: on
(0,'A'),(1,'B'),(2,'B'),(3,'C'),(?1)(?2)(?3)returned[0,2,3], a length-3 array for a three-condition pattern, whilesequenceMatchreturned 0. Since fullness is tested by comparing array size to condition count, a caller saw a full match where none occurred.remember_matched_eventsis pure bookkeeping again: both edited lambdas are reached only underif constexpr (remember_matched_events), sosequenceMatchandsequenceCountare unchanged by construction and the two instantiations walk the same search. Their 46 assertions and the 23 pre-existingsequenceMatchEventsones in00222stay byte-identical; 7 are added, 2 of them failing without this change, and 50 randomized repeats are green.Output can change for existing queries, but every chain that disappears is one
sequenceMatchalready reports as no-match, and the docs promise the "longest event chains that matched the pattern".Pre-existing, not fixed here: array length still cannot prove a full match, since a path may match every
(?N)and then fail a trailing(?t...).Workflow [PR]
Sync PR [sync-upstream/pr/118497]