github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/colexec/hash_utils_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 colexec 12 13 import ( 14 "context" 15 "fmt" 16 "testing" 17 18 "github.com/cockroachdb/cockroach/pkg/col/coldata" 19 "github.com/cockroachdb/cockroach/pkg/sql/sqlbase" 20 "github.com/cockroachdb/cockroach/pkg/sql/types" 21 "github.com/cockroachdb/cockroach/pkg/util/leaktest" 22 ) 23 24 // TestHashFunctionFamily verifies the assumption that our vectorized hashing 25 // function (the combination of initHash, rehash, and finalizeHash) actually 26 // defines a function family and that changing the initial hash value is 27 // sufficient to get a "different" hash function. 28 func TestHashFunctionFamily(t *testing.T) { 29 defer leaktest.AfterTest(t)() 30 31 ctx := context.Background() 32 bucketsA, bucketsB := make([]uint64, coldata.BatchSize()), make([]uint64, coldata.BatchSize()) 33 nKeys := coldata.BatchSize() 34 keyTypes := []*types.T{types.Int} 35 keys := []coldata.Vec{testAllocator.NewMemColumn(keyTypes[0], coldata.BatchSize())} 36 for i := int64(0); i < int64(coldata.BatchSize()); i++ { 37 keys[0].Int64()[i] = i 38 } 39 numBuckets := uint64(16) 40 var ( 41 cancelChecker CancelChecker 42 overloadHelperVar overloadHelper 43 datumAlloc sqlbase.DatumAlloc 44 ) 45 46 for initHashValue, buckets := range [][]uint64{bucketsA, bucketsB} { 47 // We need +1 here because 0 is not a valid initial hash value. 48 initHash(buckets, nKeys, uint64(initHashValue+1)) 49 for _, keysCol := range keys { 50 rehash(ctx, buckets, keysCol, nKeys, nil /* sel */, cancelChecker, overloadHelperVar, &datumAlloc) 51 } 52 finalizeHash(buckets, nKeys, numBuckets) 53 } 54 55 numKeysInSameBucket := 0 56 for key := range bucketsA { 57 if bucketsA[key] == bucketsB[key] { 58 numKeysInSameBucket++ 59 } 60 } 61 // We expect that about 1/numBuckets keys remained in the same bucket, so if 62 // the actual number deviates by more than a factor of 3, we fail the test. 63 if nKeys*3/int(numBuckets) < numKeysInSameBucket { 64 t.Fatal(fmt.Sprintf("too many keys remained in the same bucket: expected about %d, actual %d", 65 nKeys/int(numBuckets), numKeysInSameBucket)) 66 } 67 }