github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/opt/xform/physical_props_test.go (about)

     1  // Copyright 2020 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package xform
    12  
    13  import (
    14  	"math"
    15  	"testing"
    16  
    17  	"github.com/cockroachdb/cockroach/pkg/util/leaktest"
    18  )
    19  
    20  // TestDistinctOnLimitHint verifies that distinctOnLimitHint never returns a
    21  // negative result.
    22  func TestDistinctOnLimitHint(t *testing.T) {
    23  	defer leaktest.AfterTest(t)()
    24  
    25  	// Generate a list of test values. We want to try the 0 value, very small
    26  	// values, very large values, and cases where the two values are ~1 off from
    27  	// each other, or very close to each other.
    28  	var values []float64
    29  	for _, v := range []float64{0, 1e-10, 1e-5, 0.1, 1, 2, 3, 10, 1e5, 1e10} {
    30  		values = append(values, v)
    31  		for _, noise := range []float64{1e-10, 1e-7, 0.1, 1} {
    32  			values = append(values, v+noise)
    33  			if v-noise >= 0 {
    34  				values = append(values, v-noise)
    35  			}
    36  		}
    37  	}
    38  
    39  	for _, distinctCount := range values {
    40  		for _, neededRows := range values {
    41  			hint := distinctOnLimitHint(distinctCount, neededRows)
    42  			if hint < 0 || math.IsNaN(hint) || math.IsInf(hint, +1) {
    43  				t.Fatalf("distinctOnLimitHint(%g,%g)=%g", distinctCount, neededRows, hint)
    44  			}
    45  		}
    46  	}
    47  }