Skip to content

Commit cd8866e

Browse files
authored
Merge pull request #155651 from michae2/backport25.2-155035
release-25.2: sql/stats: handle range counts when skipping dropped enum hist buckets
2 parents b645213 + 6201007 commit cd8866e

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

pkg/sql/opt/cat/table.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ type HistogramBucket struct {
295295

296296
// DistinctRange is the estimated number of distinct values between the upper
297297
// bound of the previous bucket and UpperBound (both boundaries are
298-
// exclusive).
298+
// exclusive). The first bucket should always have DistinctRange=0.
299299
DistinctRange float64
300300

301301
// UpperBound is the upper bound of the bucket.

pkg/sql/opt/props/histogram.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/cockroachdb/cockroach/pkg/sql/sem/eval"
2424
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
2525
"github.com/cockroachdb/cockroach/pkg/sql/types"
26+
"github.com/cockroachdb/cockroach/pkg/util/buildutil"
2627
"github.com/cockroachdb/cockroach/pkg/util/encoding"
2728
"github.com/cockroachdb/cockroach/pkg/util/timetz"
2829
"github.com/cockroachdb/errors"
@@ -79,6 +80,15 @@ func (h *Histogram) numEq(i int) float64 {
7980
// selectivity applied. i must be greater than or equal to 0 and less than
8081
// bucketCount.
8182
func (h *Histogram) numRange(i int) float64 {
83+
// The first bucket always has a zero value for NumRange, so the lower bound
84+
// of the histogram is the upper bound of the first bucket. We only check this
85+
// in test builds.
86+
if i == 0 && h.buckets[i].NumRange != 0 {
87+
if buildutil.CrdbTestBuild {
88+
panic(errors.AssertionFailedf("the first bucket should have NumRange=0"))
89+
}
90+
return 0
91+
}
8292
return h.buckets[i].NumRange * h.selectivity
8393
}
8494

@@ -89,6 +99,13 @@ func (h *Histogram) distinctRange(i int) float64 {
8999
n := h.buckets[i].NumRange
90100
d := h.buckets[i].DistinctRange
91101

102+
if i == 0 && d != 0 {
103+
if buildutil.CrdbTestBuild {
104+
panic(errors.AssertionFailedf("the first bucket should have DistinctRange=0"))
105+
}
106+
return 0
107+
}
108+
92109
if d == 0 {
93110
return 0
94111
}
@@ -207,11 +224,6 @@ func (h *Histogram) maxDistinctValuesCount() float64 {
207224
return 0
208225
}
209226

210-
// The first bucket always has a zero value for NumRange, so the lower bound
211-
// of the histogram is the upper bound of the first bucket.
212-
if h.numRange(0) != 0 {
213-
panic(errors.AssertionFailedf("the first bucket should have NumRange=0"))
214-
}
215227
previousUpperBound := h.upperBound(0)
216228

217229
var count float64
@@ -317,12 +329,6 @@ func (h *Histogram) filter(
317329
return filtered
318330
}
319331

320-
// The first bucket always has a zero value for NumRange, so the lower bound
321-
// of the histogram is the upper bound of the first bucket.
322-
if h.numRange(0) != 0 {
323-
panic(errors.AssertionFailedf("the first bucket should have NumRange=0"))
324-
}
325-
326332
var iter histogramIter
327333
iter.init(h, desc)
328334
spanIndex := 0

0 commit comments

Comments
 (0)