Skip to content

Commit

Permalink
sql: use test rand in tests
Browse files Browse the repository at this point in the history
This should make tests more deterministic.

Release note: None
  • Loading branch information
yuzefovich committed Nov 13, 2024
1 parent 87420f6 commit 6694645
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 41 deletions.
1 change: 0 additions & 1 deletion pkg/sql/rowexec/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,6 @@ go_test(
"//pkg/util/randutil",
"//pkg/util/stop",
"//pkg/util/syncutil",
"//pkg/util/timeutil",
"//pkg/util/tracing",
"//pkg/util/tracing/tracingpb",
"@com_github_axiomhq_hyperloglog//:hyperloglog",
Expand Down
9 changes: 4 additions & 5 deletions pkg/sql/rowexec/sorter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package rowexec
import (
"context"
"fmt"
"math/rand"
"testing"

"github.com/cockroachdb/cockroach/pkg/base"
Expand All @@ -27,7 +26,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/util/encoding"
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/cockroachdb/cockroach/pkg/util/randutil"
)

func TestSorter(t *testing.T) {
Expand Down Expand Up @@ -407,7 +406,7 @@ func BenchmarkSortAll(b *testing.B) {
DiskMonitor: diskMonitor,
}

rng := rand.New(rand.NewSource(timeutil.Now().UnixNano()))
rng, _ := randutil.NewTestRand()
spec := execinfrapb.SorterSpec{OutputOrdering: twoColOrdering}
post := execinfrapb.PostProcessSpec{}

Expand Down Expand Up @@ -451,7 +450,7 @@ func BenchmarkSortLimit(b *testing.B) {
DiskMonitor: diskMonitor,
}

rng := rand.New(rand.NewSource(timeutil.Now().UnixNano()))
rng, _ := randutil.NewTestRand()
spec := execinfrapb.SorterSpec{OutputOrdering: twoColOrdering}

const numRows = 1 << 16
Expand Down Expand Up @@ -500,7 +499,7 @@ func BenchmarkSortChunks(b *testing.B) {
DiskMonitor: diskMonitor,
}

rng := rand.New(rand.NewSource(timeutil.Now().UnixNano()))
rng, _ := randutil.NewTestRand()
spec := execinfrapb.SorterSpec{
OutputOrdering: twoColOrdering,
OrderingMatchLen: 1,
Expand Down
4 changes: 2 additions & 2 deletions pkg/sql/run_control_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"context"
gosql "database/sql"
"fmt"
"math/rand"
"net/url"
"strings"
"sync"
Expand Down Expand Up @@ -37,6 +36,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/util/buildutil"
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/randutil"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/cockroachdb/errors"
"github.com/lib/pq"
Expand All @@ -58,7 +58,7 @@ func TestCancelDistSQLQuery(t *testing.T) {

var queryLatency *time.Duration
sem := make(chan struct{}, 1)
rng := rand.New(rand.NewSource(timeutil.Now().UnixNano()))
rng, _ := randutil.NewTestRand()
tc := serverutils.StartCluster(t, 2, /* numNodes */
base.TestClusterArgs{
ReplicationMode: base.ReplicationManual,
Expand Down
10 changes: 5 additions & 5 deletions pkg/sql/schema_changer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4991,10 +4991,10 @@ CREATE TABLE t.test (k INT PRIMARY KEY, v JSON);

tableDesc = desctestutils.TestingGetPublicTableDescriptor(kvDB, keys.SystemSQLCodec, "t", "test")

r := rand.New(rand.NewSource(timeutil.Now().UnixNano()))
rng, _ := randutil.NewTestRand()
// Insert enough rows to exceed the chunk size.
for i := 0; i < maxValue+1; i++ {
jsonVal, err := json.Random(20, r)
jsonVal, err := json.Random(20, rng)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -5038,14 +5038,14 @@ CREATE TABLE t.test (a INT, b INT, c JSON, d JSON);
t.Fatal(err)
}

r := rand.New(rand.NewSource(timeutil.Now().UnixNano()))
rng, _ := randutil.NewTestRand()
// Insert enough rows to exceed the chunk size.
for i := 0; i < maxValue+1; i++ {
jsonVal1, err := json.Random(20, r)
jsonVal1, err := json.Random(20, rng)
if err != nil {
t.Fatal(err)
}
jsonVal2, err := json.Random(20, r)
jsonVal2, err := json.Random(20, rng)
if err != nil {
t.Fatal(err)
}
Expand Down
11 changes: 5 additions & 6 deletions pkg/sql/sem/builtins/datums_to_bytes_builtin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package builtins_test
import (
"context"
"fmt"
"math/rand"
"strings"
"testing"

Expand All @@ -21,7 +20,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/util"
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/cockroachdb/cockroach/pkg/util/randutil"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -51,7 +50,7 @@ func TestCrdbInternalDatumsToBytes(t *testing.T) {
"STRING[]",
"INT[]",
}
r := rand.New(rand.NewSource(timeutil.Now().UnixNano()))
rng, _ := randutil.NewTestRand()
createTable := func(t *testing.T, tdb *sqlutils.SQLRunner, typ []string) (columnNames []string) {
columnNames = make([]string, len(typ))
columnSpecs := make([]string, len(typ))
Expand Down Expand Up @@ -89,7 +88,7 @@ func TestCrdbInternalDatumsToBytes(t *testing.T) {
d = tree.DNull
} else {
const nullOk = false
d = randgen.RandDatum(r, col.GetType(), nullOk)
d = randgen.RandDatum(rng, col.GetType(), nullOk)
}
row = append(row, tree.AsStringWithFlags(d, tree.FmtParsable))
}
Expand Down Expand Up @@ -127,10 +126,10 @@ SELECT (SELECT count(DISTINCT (cols)) FROM t) -
const numCombinations = 10
for i := 0; i < numCombinations; i++ {
t.Run("", func(t *testing.T) {
numColumns := r.Intn(len(types)*3) + 1 // arbitrary, at least 1
numColumns := rng.Intn(len(types)*3) + 1 // arbitrary, at least 1
colTypes := make([]string, numColumns)
for i := range colTypes {
colTypes[i] = types[r.Intn(len(types))]
colTypes[i] = types[rng.Intn(len(types))]
}
testTableWithColumnTypes(t, colTypes...)
})
Expand Down
1 change: 0 additions & 1 deletion pkg/sql/sem/builtins/pgformat/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ go_test(
"//pkg/util/leaktest",
"//pkg/util/log",
"//pkg/util/randutil",
"//pkg/util/timeutil",
"@com_github_lib_pq//oid",
"@com_github_stretchr_testify//require",
],
Expand Down
8 changes: 4 additions & 4 deletions pkg/sql/sem/builtins/pgformat/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/util"
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/cockroachdb/cockroach/pkg/util/randutil"
"github.com/lib/pq/oid"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -67,10 +67,10 @@ func TestFormat(t *testing.T) {
}
}

seed := rand.New(rand.NewSource(timeutil.Now().UnixNano()))
rng, _ := randutil.NewTestRand()
createTable := func(t *testing.T, tdb *sqlutils.SQLRunner, typ *types.T) (tableNamer func(string) string) {
columnSpec := fmt.Sprintf("c %s", typ.SQLString())
tableName := fmt.Sprintf("%s_table_%d", strings.Replace(typ.String(), "\"", "", -1), seed.Int())
tableName := fmt.Sprintf("%s_table_%d", strings.Replace(typ.String(), "\"", "", -1), rng.Int())
tableName = strings.Replace(tableName, `[]`, `_array`, -1)

// Create the table.
Expand All @@ -96,7 +96,7 @@ func TestFormat(t *testing.T) {
d = tree.DNull
} else {
const nullOk = false
d = randgen.RandDatum(seed, col.GetType(), nullOk)
d = randgen.RandDatum(rng, col.GetType(), nullOk)
}
row = append(row, tree.AsStringWithFlags(d, tree.FmtParsable))
}
Expand Down
20 changes: 10 additions & 10 deletions pkg/sql/sem/eval/window_funcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/sql/types"
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/cockroachdb/cockroach/pkg/util/randutil"
)

const minOffset = 0
Expand Down Expand Up @@ -266,11 +266,11 @@ func testEndFollowing(t *testing.T, evalCtx *Context, wfr *WindowFrameRun, offse

func makeIntSortedPartition(count int) indexedRows {
partition := indexedRows{rows: make([]indexedRow, count)}
r := rand.New(rand.NewSource(timeutil.Now().UnixNano()))
rng, _ := randutil.NewTestRand()
number := 0
for idx := 0; idx < count; idx++ {
if r.Float64() < probabilityOfNewNumber {
number += r.Intn(10)
if rng.Float64() < probabilityOfNewNumber {
number += rng.Intn(10)
}
partition.rows[idx] = indexedRow{idx: idx, row: tree.Datums{tree.NewDInt(tree.DInt(number))}}
}
Expand All @@ -279,11 +279,11 @@ func makeIntSortedPartition(count int) indexedRows {

func makeFloatSortedPartition(count int) indexedRows {
partition := indexedRows{rows: make([]indexedRow, count)}
r := rand.New(rand.NewSource(timeutil.Now().UnixNano()))
rng, _ := randutil.NewTestRand()
number := 0.0
for idx := 0; idx < count; idx++ {
if r.Float64() < probabilityOfNewNumber {
number += r.Float64() * 10
if rng.Float64() < probabilityOfNewNumber {
number += rng.Float64() * 10
}
partition.rows[idx] = indexedRow{idx: idx, row: tree.Datums{tree.NewDFloat(tree.DFloat(number))}}
}
Expand All @@ -292,12 +292,12 @@ func makeFloatSortedPartition(count int) indexedRows {

func makeDecimalSortedPartition(t *testing.T, count int) indexedRows {
partition := indexedRows{rows: make([]indexedRow, count)}
r := rand.New(rand.NewSource(timeutil.Now().UnixNano()))
rng, _ := randutil.NewTestRand()
number := &tree.DDecimal{}
for idx := 0; idx < count; idx++ {
tmp := apd.Decimal{}
if r.Float64() < probabilityOfNewNumber {
_, err := tmp.SetFloat64(r.Float64() * 10)
if rng.Float64() < probabilityOfNewNumber {
_, err := tmp.SetFloat64(rng.Float64() * 10)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/sql/tests/inverted_index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/util/json"
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/cockroachdb/cockroach/pkg/util/randutil"
)

const numRandomJSONs = 1000
Expand All @@ -37,14 +37,14 @@ func TestInvertedIndex(t *testing.T) {
db.Exec(t, "CREATE DATABASE IF NOT EXISTS test")
db.Exec(t, "CREATE TABLE test.jsons (i INT PRIMARY KEY, j JSONB)")

r := rand.New(rand.NewSource(timeutil.Now().UnixNano()))
rng, _ := randutil.NewTestRand()

// Grab a bunch of random JSONs. We insert half before we add the inverted
// index and half after.
jsons := make([]json.JSON, numRandomJSONs)
for i := 0; i < numRandomJSONs; i++ {
var err error
jsons[i], err = json.Random(jsonComplexity, r)
jsons[i], err = json.Random(jsonComplexity, rng)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -82,7 +82,7 @@ func TestInvertedIndex(t *testing.T) {
perm := rand.Perm(len(jsons))
for i := 0; i < docsToUpdate; i++ {
var err error
jsons[perm[i]], err = json.Random(jsonComplexity, r)
jsons[perm[i]], err = json.Random(jsonComplexity, rng)
if err != nil {
t.Fatal(err)
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/sql/tests/random_schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
gosql "database/sql"
"fmt"
"math"
"math/rand"
"testing"

"github.com/cockroachdb/cockroach/pkg/base"
Expand All @@ -22,7 +21,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/testutils/serverutils"
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/cockroachdb/cockroach/pkg/util/randutil"
)

func setDb(t *testing.T, db *gosql.DB, name string) {
Expand Down Expand Up @@ -56,7 +55,7 @@ func TestCreateRandomSchema(t *testing.T) {
return tree.AsStringWithFlags(c, tree.FmtParsable)
}

rng := rand.New(rand.NewSource(timeutil.Now().UnixNano()))
rng, _ := randutil.NewTestRand()
for i := 0; i < 100; i++ {
createTable := randgen.RandCreateTable(ctx, rng, "table", i, randgen.TableOptNone)
setDb(t, db, "test")
Expand Down

0 comments on commit 6694645

Please sign in to comment.