signalsLatest/lastSeen via signal_latest projection#284
Open
signalsLatest/lastSeen via signal_latest projection#284
Conversation
Rewrites getLatestQuery to emit aggregate shapes that ClickHouse's projection matcher picks up against the signal_latest_by_subject_source_name projection added in model-garage. Non-location signals use argMax + max(timestamp); location signals use argMaxIf + maxIf with the same non-(0, 0) filter the projection carries. Mixed queries UNION ALL the two shapes so each branch matches cleanly. Drops DEFAULT_LOOKBACK / Service.lookbackFrom and the timestamp WHERE clause on both getLatestQuery and getLastSeenQuery. The timestamp filter prevents projection matching, and the projection itself makes the lookback unnecessary -- queries now read a handful of pre-aggregated rows per (subject, source, name) regardless of history depth. Verified on ClickHouse 25.3 with a 20M-row ReplacingMergeTree signal table: all four query shapes (non-location latest, location latest, mixed UNION ALL with source filter, lastSeen) match the projection via system.query_log.projections, and results are byte-identical to force-base (optimize_use_projections=0) runs. Must deploy only after DIMO-Network/model-garage#257 is materialized in the target environment; otherwise these queries fall back to full-history scans.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
getLatestQueryso the aggregate shapes match thesignal_latest_by_subject_source_nameprojection added in DIMO-Network/model-garage#257 — non-location signals useargMax+max(timestamp), location signals useargMaxIf+maxIfwith the same non-(0, 0)filter the projection carries, mixed queriesUNION ALLthe two branches.DEFAULT_LOOKBACK/Service.lookbackFromand the timestampWHEREclause on bothgetLatestQueryandgetLastSeenQuery. The timestamp filter prevents projection matching, and the projection makes the lookback unnecessary — queries now read a handful of pre-aggregated rows per(subject, source, name)regardless of history depth.Why
signalsLatest/lastSeenpreviously needed a 60-day lookback to keep scans bounded. Vehicles that hadn't reported in the last 60 days returned empty fromsignalsLatest. The projection stores one pre-aggregated row per(subject, source, name), so the lookback is no longer load-bearing and the full history is cheap to query.Verification
ClickHouse 25.3 (podman), 20M-row
ReplacingMergeTreesignaltable, 200 subjects, 8 partitions, with the new projection materialized:system.query_log.projectionsmatchspeed,isIgnitionOn)argMaxIf+maxIfnon-(0,0))lastSeen(max(timestamp))Results byte-identical between projection and force-base runs (
SETTINGS optimize_use_projections = 0).Deploy order
Must deploy only after DIMO-Network/model-garage#257 is materialized in the target environment. Without the projection, the now-unbounded queries will do full-history scans — regression vs today's 60-day lookback. Hold the deploy until
system.mutationsshows theMATERIALIZE PROJECTIONmutation is done onsignalandevent.Removed config
DEFAULT_LOOKBACKenv var (was 60 in bothvalues.yamlandvalues-prod.yaml)DefaultLookbackDaysfield inconfig.SettingsService.defaultLookbackfield,Service.lookbackFrom(),defaultLookbackDaysconstsettings.sample.yamlTest plan
go test ./...passes (unit + e2e testcontainers)system.query_log.projectionson live traffic shows the projection being hitsignalsLatestp99signalsLatest