-
[OPIK-7307] [BE] feat: add id_week predicate to SpanDAO read paths (#7476)
发布于
2026-07-16 14:36:37 +00:00 - [OPIK-7307] [BE] feat: add id_week predicate to SpanDAO read paths
Engage weekly-partition pruning on user-facing spans-table read scans by
adding a toMonday(id_at) predicate alongside each existing span-id bound.
The planner cannot infer monotonicity through UUIDv7ToDateTime(toUUID(id)),
so the bound must be explicit. It is a strict consequence of the id-range
(never drops a row) and a no-op pre-cutover; once spans is PARTITION BY
toMonday(id_at) it prunes to the partitions in range.Spans counterpart of the traces work in OPIK-6895 (#7235); targets the
id_at-on-spans migration (000103) from OPIK-7306 (#7471), on which this is
stacked.Qualifying spans-table scans (mirror each id bound):
- SpanDAO: SELECT_BY_PROJECT_ID (span_id_prefilter, spans_deduped, page_wide
window re-read), COUNT_BY_PROJECT_ID, SELECT_SPANS_STATS,
SELECT_SPAN_FEEDBACK_SCORES_STATS - SpanMetricsQueries.spans_filtered (shared by ProjectMetricsDAO +
WorkspaceMetricsDAO) - ProjectMetricsDAO: GET_COST(_WITH_BREAKDOWN), GET_TOKEN_USAGE
(_WITH_BREAKDOWN), GET_THREAD_COST - KpiCardDAO: trace_costs, spans_filtered, thread_costs
- WorkspaceMetricsDAO: GET_COSTS_SUMMARY, GET_COSTS_DAILY(_BY_PROJECT)
Excluded (no safe id_week derivation): by-id lookups, trace_id/experiment-
linkage scans, created_at-windowed daily BI counters, EXISTS_BY_PROJECT_ID
and GET_PROJECT_TOKEN_USAGE_NAMES (no id bound), GET_TOTAL_COST (trace_id
range only). AiSpendQueryBuilder was removed in #7326 and is no longer a
candidate. StringTemplate vs raw-SQL escaping handled per query.Semantic no-op — exercised by pre-existing integration tests (span list/
search/stats time-filtering, metrics and KPI suites); no new tests, as in
#7235. EXPLAIN PARTITIONS validation deferred to post-cutover staging.Co-Authored-By: Claude Opus 4.8 (1M context) noreply@anthropic.com
- [OPIK-7307] [BE] test: cover page_wide id_week bounds (time-window + paginate + sort + exclude)
Adds a FindSpansResourceTest case that combines a UUID creation-time window
(from_time/to_time) with pagination, a non-default sort, and field exclusion,
asserting full page content per page against an independent sorted+excluded
reference. This exercises the toMonday(id_at) bounds added to span_id_prefilter,
spans_deduped, and the page_wide re-read: the window brackets every span so the
bounds are a strict no-op, and the byte-for-byte page assertions would catch any
week-bound / ordering / field-exclusion regression in the two-phase query.Addresses review feedback on the two-phase page query (per
.agents/skills/opik-backend/testing.md: assert whole page content, not just ids).Co-Authored-By: Claude Opus 4.8 (1M context) noreply@anthropic.com
- [OPIK-7307] [BE] test: assert page count in page_wide id_week test
Guard the time-window + pagination + sort + exclude test so it fails instead
of passing silently if the loop never iterates (assertThat(pages) > 1).
Addresses review feedback.Co-Authored-By: Claude Opus 4.8 (1M context) noreply@anthropic.com
Co-authored-by: Claude Opus 4.8 (1M context) noreply@anthropic.com
下载附件