@@ -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.
8182func (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