Skip to content

Commit 6973f76

Browse files
authored
Merge pull request #155648 from michae2/backport25.4-155035
release-25.4: sql/stats: handle range counts when skipping dropped enum hist buckets
2 parents 8059a81 + 4037bc5 commit 6973f76

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
@@ -300,7 +300,7 @@ type HistogramBucket struct {
300300

301301
// DistinctRange is the estimated number of distinct values between the upper
302302
// bound of the previous bucket and UpperBound (both boundaries are
303-
// exclusive).
303+
// exclusive). The first bucket should always have DistinctRange=0.
304304
DistinctRange float64
305305

306306
// 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
@@ -21,6 +21,7 @@ import (
2121
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
2222
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree/datumrange"
2323
"github.com/cockroachdb/cockroach/pkg/sql/types"
24+
"github.com/cockroachdb/cockroach/pkg/util/buildutil"
2425
"github.com/cockroachdb/errors"
2526
"github.com/olekukonko/tablewriter"
2627
)
@@ -75,6 +76,15 @@ func (h *Histogram) numEq(i int) float64 {
7576
// selectivity applied. i must be greater than or equal to 0 and less than
7677
// bucketCount.
7778
func (h *Histogram) numRange(i int) float64 {
79+
// The first bucket always has a zero value for NumRange, so the lower bound
80+
// of the histogram is the upper bound of the first bucket. We only check this
81+
// in test builds.
82+
if i == 0 && h.buckets[i].NumRange != 0 {
83+
if buildutil.CrdbTestBuild {
84+
panic(errors.AssertionFailedf("the first bucket should have NumRange=0"))
85+
}
86+
return 0
87+
}
7888
return h.buckets[i].NumRange * h.selectivity
7989
}
8090

@@ -85,6 +95,13 @@ func (h *Histogram) distinctRange(i int) float64 {
8595
n := h.buckets[i].NumRange
8696
d := h.buckets[i].DistinctRange
8797

98+
if i == 0 && d != 0 {
99+
if buildutil.CrdbTestBuild {
100+
panic(errors.AssertionFailedf("the first bucket should have DistinctRange=0"))
101+
}
102+
return 0
103+
}
104+
88105
if d == 0 {
89106
return 0
90107
}
@@ -203,11 +220,6 @@ func (h *Histogram) maxDistinctValuesCount() float64 {
203220
return 0
204221
}
205222

206-
// The first bucket always has a zero value for NumRange, so the lower bound
207-
// of the histogram is the upper bound of the first bucket.
208-
if h.numRange(0) != 0 {
209-
panic(errors.AssertionFailedf("the first bucket should have NumRange=0"))
210-
}
211223
previousUpperBound := h.upperBound(0)
212224

213225
var count float64
@@ -322,12 +334,6 @@ func (h *Histogram) filter(
322334
return filtered
323335
}
324336

325-
// The first bucket always has a zero value for NumRange, so the lower bound
326-
// of the histogram is the upper bound of the first bucket.
327-
if h.numRange(0) != 0 {
328-
panic(errors.AssertionFailedf("the first bucket should have NumRange=0"))
329-
}
330-
331337
var iter histogramIter
332338
iter.init(h, desc)
333339
spanIndex := 0

0 commit comments

Comments
 (0)