MCPcopy Create free account
hub / github.com/cockroachdb/cockroachdb-parser / MaxDistinctCount

Function MaxDistinctCount

pkg/sql/sem/tree/datum.go:6247–6331  ·  view source on GitHub ↗

MaxDistinctCount returns the maximum number of distinct values between the given datums (inclusive). This is possible if: a. the types of the datums are equivalent and countable, or b. the datums have the same value (in which case the distinct count is 1). If neither of these conditions hold, Ma

(
	ctx context.Context, evalCtx CompareContext, first, last Datum,
)

Source from the content-addressed store, hash-verified

6245// Additionally, it must be the case that first <= last, otherwise
6246// MaxDistinctCount returns ok=false.
6247func MaxDistinctCount(
6248 ctx context.Context, evalCtx CompareContext, first, last Datum,
6249) (_ int64, ok bool) {
6250 if !first.ResolvedType().Equivalent(last.ResolvedType()) {
6251 // The datums must be of the same type.
6252 return 0, false
6253 }
6254 if cmp, err := first.Compare(ctx, evalCtx, last); err != nil {
6255 panic(err)
6256 } else if cmp == 0 {
6257 // If the datums are equal, the distinct count is 1.
6258 return 1, true
6259 }
6260
6261 // If the datums are a countable type, return the distinct count between them.
6262 var start, end int64
6263
6264 switch t := first.(type) {
6265 case *DInt:
6266 otherDInt, otherOk := AsDInt(last)
6267 if otherOk {
6268 start = int64(*t)
6269 end = int64(otherDInt)
6270 }
6271
6272 case *DOid:
6273 otherDOid, otherOk := AsDOid(last)
6274 if otherOk {
6275 start = int64(t.Oid)
6276 end = int64(otherDOid.Oid)
6277 }
6278
6279 case *DDate:
6280 otherDDate, otherOk := last.(*DDate)
6281 if otherOk {
6282 if !t.IsFinite() || !otherDDate.IsFinite() {
6283 // One of the DDates isn't finite, so we can't extract a distinct count.
6284 return 0, false
6285 }
6286 start = int64((*t).PGEpochDays())
6287 end = int64(otherDDate.PGEpochDays())
6288 }
6289
6290 case *DEnum:
6291 otherDEnum, otherOk := last.(*DEnum)
6292 if otherOk {
6293 startIdx, err := t.EnumTyp.EnumGetIdxOfPhysical(t.PhysicalRep)
6294 if err != nil {
6295 panic(err)
6296 }
6297 endIdx, err := t.EnumTyp.EnumGetIdxOfPhysical(otherDEnum.PhysicalRep)
6298 if err != nil {
6299 panic(err)
6300 }
6301 start, end = int64(startIdx), int64(endIdx)
6302 }
6303
6304 case *DBool:

Callers

nothing calls this directly

Calls 8

AsDIntFunction · 0.85
AsDOidFunction · 0.85
EquivalentMethod · 0.80
IsFiniteMethod · 0.80
PGEpochDaysMethod · 0.80
EnumGetIdxOfPhysicalMethod · 0.80
ResolvedTypeMethod · 0.65
CompareMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…