Skip to content

Commit 86b8446

Browse files
committed
sql/stats: bring back guard against non-zero NumRange in forecasts
It occurred to me tonight that the code we removed in #143955 not only generated a sentry report, but also returned an error instead of producing a faulty histogram. I too hope that #93892 is now fixed, but it seems wise to at least keep some code that guards against faulty histograms, even if we don't think the sentry report is necessary any more. Informs: #93892 Epic: None Release note: None
1 parent c587e9a commit 86b8446

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

pkg/sql/stats/forecast.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ import (
1616
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
1717
"github.com/cockroachdb/cockroach/pkg/sql/opt/cat"
1818
"github.com/cockroachdb/cockroach/pkg/sql/sem/eval"
19+
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
1920
"github.com/cockroachdb/cockroach/pkg/sql/types"
21+
"github.com/cockroachdb/cockroach/pkg/util/errorutil"
2022
"github.com/cockroachdb/cockroach/pkg/util/log"
2123
"github.com/cockroachdb/errors"
2224
"github.com/cockroachdb/redact"
@@ -329,6 +331,24 @@ func forecastColumnStatistics(
329331
}
330332
forecast.HistogramData = &histData
331333
forecast.setHistogramBuckets(hist)
334+
335+
// Verify that the first two buckets (the initial NULL bucket and the first
336+
// non-NULL bucket) both have NumRange=0 and DistinctRange=0. (We must check
337+
// this after calling setHistogramBuckets to account for rounding.)
338+
for _, bucket := range forecast.Histogram {
339+
if bucket.NumRange != 0 || bucket.DistinctRange != 0 {
340+
err := errorutil.UnexpectedWithIssueErrorf(
341+
93892, "forecasted histogram for table %v had first bucket with non-zero NumRange or "+
342+
"DistinctRange", tableID,
343+
)
344+
log.Warningf(ctx, "%v", err)
345+
return nil, err
346+
}
347+
if bucket.UpperBound != tree.DNull {
348+
// Stop checking after the first non-NULL bucket.
349+
break
350+
}
351+
}
332352
}
333353

334354
return forecast, nil

0 commit comments

Comments
 (0)