Skip to content

feat: Add VariantStatistics materialized view#7093

Open
AntoineToussaint wants to merge 6 commits intomainfrom
feat/variant-statistics
Open

feat: Add VariantStatistics materialized view#7093
AntoineToussaint wants to merge 6 commits intomainfrom
feat/variant-statistics

Conversation

@AntoineToussaint
Copy link
Copy Markdown
Member

Summary

  • Adds VariantStatistics aggregation tables that group metrics by (function_name, variant_name, minute) — the variant-level counterpart of ModelProviderStatistics
  • ClickHouse (migration 0053): AggregatingMergeTree table fed by three materialized views — VariantStatisticsModelView (tokens/cost from ModelInference via InferenceById JOIN), VariantStatisticsChatView (latency quantiles + count from ChatInference), and VariantStatisticsJsonView (same from JsonInference)
  • Postgres: variant_statistics table with refresh_variant_statistics_incremental() function joining chat_inferences UNION ALL json_inferences with model_inferences
  • Adds prepare_variant_statistics() test helper for both backends
  • Adds E2E aggregation test verifying token sums, cost aggregation (with NULL exclusion), inference count, and count_with_cost across multiple model inferences per inference

Closes #6980

Test plan

  • cargo check --all-targets --all-features
  • cargo clippy --all-targets --all-features -- -D warnings
  • cargo fmt
  • cargo test-unit-fast (12 pre-existing failures, no new ones)
  • Pre-commit hooks pass
  • E2E: test_variant_statistics_aggregation_clickhouse — inserts chat + model inferences, verifies aggregated row
  • E2E: test_variant_statistics_aggregation_postgres — same test on Postgres backend
  • ClickHouse migration e2e: migration 0053 applies cleanly on fresh DB (covered by existing test_rollback_up_to_migration_index_46)

🤖 Generated with Claude Code

Create variant-level aggregation tables that group metrics by
(function_name, variant_name, minute), analogous to the existing
ModelProviderStatistics which groups by (model_name, model_provider_name).

ClickHouse: AggregatingMergeTree table fed by three MVs:
- VariantStatisticsModelView (ModelInference → tokens/cost via InferenceById JOIN)
- VariantStatisticsChatView (ChatInference → latency quantiles + count)
- VariantStatisticsJsonView (JsonInference → latency quantiles + count)

Postgres: variant_statistics table with incremental refresh function
that joins chat_inferences + json_inferences with model_inferences.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
AntoineToussaint and others added 4 commits March 27, 2026 13:57
Tests cover: basic aggregation with retries, multiple variants isolation,
cross-minute bucketing, mixed chat+json inferences, high-volume (20 inferences
with retry patterns), and orphan inferences without model data. All tests
run against both ClickHouse and Postgres via make_db_test! macro.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Fix cross_minute test: count_with_cost assertion was 3 but should be 4
  (a1, a2, b1, b3 all have cost; b2 and c1 don't)
- Fix mixed_chat_json test: Postgres json_inferences requires non-null
  output_schema — provide `{"type": "object"}` in make_json_inference

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
ClickHouse cannot parse `null` for JSON string columns like `input`.
Use `Some(StoredInput::default())` and `Some(vec![])` for output,
matching the pattern used in evaluation_queries tests.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
ClickHouse rejects null for JSON string columns (input, inference_params,
tool_params, extra_body, auxiliary_content). Provide default values
matching the pattern used in evaluation_queries tests.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Create a materialized view VariantStatistics that includes the same statistics as ModelProviderStatistics

1 participant