Skip to content

Commit d1660c8

Browse files
mstaebleclaude
andcommitted
Use variant_combinations for query-level variant filtering
Replace array containment checks on large tables with subqueries against the small variant_combinations table (2K rows), then filter by integer variant_combination_id. Benchmarked on staging: - TestReportExcludeVariants: 1,167ms → 55ms (21x faster) - TestsByNURPAndStandardDeviation: 505ms → 223ms (2.3x faster) - PlatformInfraSuccess: 508ms → 346ms (1.5x faster) - Collapsed matview filter: 1,158ms → 500ms (2.3x faster) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 69816ed commit d1660c8

3 files changed

Lines changed: 34 additions & 19 deletions

File tree

pkg/db/query/misc_queries.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package query
22

33
import (
4+
"database/sql"
45
"fmt"
56
"time"
67

@@ -28,20 +29,25 @@ func PlatformInfraSuccess(dbc *db.DB, platforms sets.String, period string) (map
2829
return nil, fmt.Errorf("unknown period %s", period)
2930
}
3031

31-
raw := dbc.DB.Table(table).
32-
Select("*, unnest(variants) as variant").
33-
Where("name = ?", testidentification.NewInfrastructureTestName)
34-
3532
var sqlResults []struct {
3633
Variant string
3734
PassPercentage float64
3835
}
39-
q := dbc.DB.Table("(?) as results", raw).
40-
Select(`
41-
variant,
42-
SUM(current_successes) * 100.0 / NULLIF(SUM(current_runs), 0) AS pass_percentage`).
43-
Where("variant in ?", platforms.List()).
44-
Group("variant").Scan(&sqlResults)
36+
q := dbc.DB.Raw(fmt.Sprintf(`
37+
WITH target_variants AS (
38+
SELECT vc.id, v.variant
39+
FROM variant_combinations vc, unnest(vc.variants) AS v(variant)
40+
WHERE v.variant IN @platforms
41+
)
42+
SELECT tv.variant,
43+
SUM(m.current_successes) * 100.0 / NULLIF(SUM(m.current_runs), 0) AS pass_percentage
44+
FROM %s m
45+
JOIN target_variants tv ON m.variant_combination_id = tv.id
46+
WHERE m.name = @testname
47+
GROUP BY tv.variant`, table),
48+
sql.Named("platforms", platforms.List()),
49+
sql.Named("testname", testidentification.NewInfrastructureTestName),
50+
).Scan(&sqlResults)
4551

4652
for _, r := range sqlResults {
4753
results[r.Variant] = r.PassPercentage

pkg/db/query/test_queries.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,10 @@ func TestReportsByVariant(
113113
// Query and group by variant:
114114
var testReports []api.Test
115115
q := `
116-
WITH results AS (
116+
WITH excluded_vc AS (
117+
SELECT id FROM variant_combinations WHERE @excluded && variants
118+
),
119+
results AS (
117120
SELECT name,
118121
release,
119122
sum(current_runs) AS current_runs,
@@ -127,7 +130,7 @@ WITH results AS (
127130
unnest(variants) AS variant
128131
FROM prow_test_report_7d_matview
129132
WHERE release = @release AND name ~* @testsubstrings
130-
AND NOT(variants is not null and @excluded && variants)
133+
AND variant_combination_id NOT IN (SELECT id FROM excluded_vc)
131134
GROUP BY name, release, variant
132135
)
133136
SELECT *,
@@ -169,7 +172,10 @@ func TestReportExcludeVariants(dbc *db.DB, release, testName string, excludeVari
169172

170173
// Query and group by variant:
171174
var testReport api.Test
172-
q := `WITH results AS (
175+
q := `WITH excluded_vc AS (
176+
SELECT id FROM variant_combinations WHERE @excluded && variants
177+
),
178+
results AS (
173179
SELECT name,
174180
release,
175181
sum(current_runs) AS current_runs,
@@ -182,7 +188,7 @@ func TestReportExcludeVariants(dbc *db.DB, release, testName string, excludeVari
182188
sum(previous_flakes) AS previous_flakes
183189
FROM prow_test_report_7d_matview
184190
WHERE release = @release AND name = @testname
185-
AND NOT(variants is not null and @excluded && variants)
191+
AND variant_combination_id NOT IN (SELECT id FROM excluded_vc)
186192
GROUP BY name, release
187193
) SELECT *, %s FROM results;`
188194

@@ -243,7 +249,7 @@ func TestsByNURPAndStandardDeviation(dbc *db.DB, release, table string) *gorm.DB
243249
(current_pass_percentage - passing_average) AS delta_from_passing_average,
244250
(current_flake_percentage - flake_average) AS delta_from_flake_average`).
245251
Where(`release = ?`, release).
246-
Where(fmt.Sprintf("NOT ('never-stable'=any(%s.variants))", table))
252+
Where("variant_combination_id NOT IN (SELECT id FROM variant_combinations WHERE 'never-stable' = any(variants))")
247253
}
248254

249255
func TestOutputs(dbc *db.DB, release, test string, includedVariants, excludedVariants []string, quantity int) ([]api.TestOutput, error) {

pkg/db/views.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -372,10 +372,11 @@ var CollapsedVariantExclusions = []string{"never-stable", "aggregated"}
372372
var testReportCollapsedMatView = buildCollapsedMatViewSQL()
373373

374374
func buildCollapsedMatViewSQL() string {
375-
var clauses []string
376-
for _, v := range CollapsedVariantExclusions {
377-
clauses = append(clauses, fmt.Sprintf("NOT ('%s' = any(variants))", v))
375+
quotedExclusions := make([]string, len(CollapsedVariantExclusions))
376+
for i, v := range CollapsedVariantExclusions {
377+
quotedExclusions[i] = fmt.Sprintf("'%s'", v)
378378
}
379+
excludedArray := "ARRAY[" + strings.Join(quotedExclusions, ",") + "]"
379380
return `
380381
SELECT suite_name, name, id, jira_component, jira_component_id, release,
381382
SUM(current_runs)::bigint AS current_runs,
@@ -388,7 +389,9 @@ SELECT suite_name, name, id, jira_component, jira_component_id, release,
388389
SUM(previous_flakes)::bigint AS previous_flakes,
389390
(array_agg(open_bugs))[1] AS open_bugs
390391
FROM |||SOURCE|||
391-
WHERE ` + strings.Join(clauses, "\n AND ") + `
392+
WHERE variant_combination_id NOT IN (
393+
SELECT id FROM variant_combinations WHERE ` + excludedArray + ` && variants
394+
)
392395
GROUP BY suite_name, name, id, jira_component, jira_component_id, release
393396
`
394397
}

0 commit comments

Comments
 (0)