Skip to content

Commit ecf610f

Browse files
committed
fix: configurable comparison mode (auto/cost/buffers)
1 parent a6e5d5d commit ecf610f

2 files changed

Lines changed: 40 additions & 1 deletion

File tree

regresql/config.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ type (
2121

2222
AnalyzeConfig struct {
2323
Enabled bool `yaml:"enabled"`
24+
Comparison string `yaml:"comparison,omitempty"` // "auto" | "cost" | "buffers"
2425
BufferThreshold float64 `yaml:"buffer_threshold,omitempty"` // default: 2.0
2526
CostThreshold float64 `yaml:"cost_threshold,omitempty"` // default: 10.0
2627
}
@@ -181,16 +182,21 @@ func GetAnalyzeConfig() *AnalyzeConfig {
181182
if cachedConfig == nil || cachedConfig.Analyze == nil {
182183
return &AnalyzeConfig{
183184
Enabled: false,
185+
Comparison: "auto",
184186
BufferThreshold: 2.0,
185187
CostThreshold: 10.0,
186188
}
187189
}
188190
cfg := cachedConfig.Analyze
189191
result := &AnalyzeConfig{
190192
Enabled: cfg.Enabled,
193+
Comparison: cfg.Comparison,
191194
BufferThreshold: cfg.BufferThreshold,
192195
CostThreshold: cfg.CostThreshold,
193196
}
197+
if result.Comparison == "" {
198+
result.Comparison = "auto"
199+
}
194200
if result.BufferThreshold == 0 {
195201
result.BufferThreshold = 2.0
196202
}
@@ -211,3 +217,7 @@ func GetBufferThreshold() float64 {
211217
func GetCostThreshold() float64 {
212218
return GetAnalyzeConfig().CostThreshold
213219
}
220+
221+
func GetComparisonMode() string {
222+
return GetAnalyzeConfig().Comparison
223+
}

regresql/tap.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,19 @@ func (p *Plan) compareBaseline(baselineDir, bindingName string, bindings map[str
208208
return result
209209
}
210210

211-
useBufferComparison := baseline.AnalyzeMode && baseline.Buffers != nil
211+
// Warn if analyze.enabled but baseline lacks buffer data
212+
if IsAnalyzeEnabled() && !baseline.AnalyzeMode {
213+
fmt.Fprintf(os.Stderr, "Warning: analyze.enabled=true but baseline '%s' lacks buffer data\n", baselinePath)
214+
}
215+
216+
useBufferComparison, err := getComparisonMode(baseline, baselinePath)
217+
if err != nil {
218+
result.Status = "failed"
219+
result.Error = err.Error()
220+
result.Duration = time.Since(start).Seconds()
221+
return result
222+
}
223+
212224
explainPlan, err := p.runExplainWithMode(q, bindings, useBufferComparison)
213225
if err != nil {
214226
result.Status = "failed"
@@ -317,3 +329,20 @@ func hasCriticalRegression(regressions []PlanRegression) bool {
317329
}
318330
return false
319331
}
332+
333+
func getComparisonMode(baseline *Baseline, baselinePath string) (bool, error) {
334+
mode := GetComparisonMode()
335+
hasBuffers := baseline.AnalyzeMode && baseline.Buffers != nil
336+
337+
switch mode {
338+
case "buffers":
339+
if !hasBuffers {
340+
return false, fmt.Errorf("comparison: buffers requires baseline with buffer data - regenerate '%s' with --analyze", baselinePath)
341+
}
342+
return true, nil
343+
case "cost":
344+
return false, nil
345+
default: // "auto"
346+
return hasBuffers, nil
347+
}
348+
}

0 commit comments

Comments
 (0)