Skip to content

Commit b332778

Browse files
fix(g2-followup): tiebreak newest-first journal scans on event_id (#159)
The bounded journal scan added in S4 (PR #158) ordered by `{time_column} DESC` with no secondary key, unlike the existing ascending path's `ASC, event_id ASC` total order. Two rows sharing an identical timestamp for the same entity resolved to whichever the backend happened to emit first — nondeterministic across backends and across reloads. Add `, event_id DESC` to restore a total order and match the old last-write-wins tie behavior deterministically. Found in an Opus review pass standing in for the unavailable Codex review step. Co-authored-by: JuliaEdom <uedomskikh@gmail.com>
1 parent 945fb00 commit b332778

2 files changed

Lines changed: 2 additions & 2 deletions

File tree

‎src/serving/semantic_layer/query/engine.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ def render(value: str) -> str:
222222
sql = f"{sql} WHERE {' AND '.join(where_clauses)}"
223223
order_column = time_column or "event_id"
224224
if newest_first:
225-
sql = f"{sql} ORDER BY {order_column} DESC"
225+
sql = f"{sql} ORDER BY {order_column} DESC, event_id DESC"
226226
else:
227227
sql = f"{sql} ORDER BY {order_column} ASC, event_id ASC"
228228
if limit is not None:

‎tests/unit/test_pipeline_events_scan.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def test_family_filters_and_limit_are_static_sql() -> None:
8484
((sql, _),) = backend.calls
8585
assert "topic = 'events.validated'" in sql
8686
assert "event_type IN ('click', 'page_view', 'add_to_cart')" in sql
87-
assert sql.endswith("ORDER BY processed_at DESC LIMIT 10")
87+
assert sql.endswith("ORDER BY processed_at DESC, event_id DESC LIMIT 10")
8888

8989

9090
def test_missing_journal_returns_empty_without_query() -> None:

0 commit comments

Comments
 (0)