From 0b3a71710fb6a604ef5a4dda356472e5c69c8d1d Mon Sep 17 00:00:00 2001 From: Tommy Reilly Date: Mon, 30 Jan 2023 15:31:59 -0500 Subject: [PATCH] sql: move RandString to util to break coldata->rangden->sem/tree dep Epic: CRDB-18892 Informs: #91831 Release note: None --- pkg/col/coldata/BUILD.bazel | 2 +- pkg/col/coldata/bytes_test.go | 4 ++-- pkg/internal/sqlsmith/BUILD.bazel | 1 + pkg/internal/sqlsmith/relational.go | 3 ++- pkg/sql/randgen/BUILD.bazel | 2 +- pkg/sql/randgen/schema.go | 3 ++- pkg/sql/randgen/string.go | 23 ----------------------- pkg/sql/rowenc/BUILD.bazel | 1 + pkg/sql/rowenc/index_encoding_test.go | 5 +++-- pkg/util/strings.go | 11 +++++++++++ 10 files changed, 24 insertions(+), 31 deletions(-) delete mode 100644 pkg/sql/randgen/string.go diff --git a/pkg/col/coldata/BUILD.bazel b/pkg/col/coldata/BUILD.bazel index 29c9e3908910..276ac3bfb791 100644 --- a/pkg/col/coldata/BUILD.bazel +++ b/pkg/col/coldata/BUILD.bazel @@ -48,8 +48,8 @@ go_test( deps = [ "//pkg/col/coldatatestutils", "//pkg/sql/colconv", - "//pkg/sql/randgen", "//pkg/sql/types", + "//pkg/util", "//pkg/util/leaktest", "//pkg/util/randutil", "@com_github_cockroachdb_errors//:errors", diff --git a/pkg/col/coldata/bytes_test.go b/pkg/col/coldata/bytes_test.go index ea955094ac83..ba85dd7d69f8 100644 --- a/pkg/col/coldata/bytes_test.go +++ b/pkg/col/coldata/bytes_test.go @@ -18,7 +18,7 @@ import ( "testing" "unsafe" - "github.com/cockroachdb/cockroach/pkg/sql/randgen" + "github.com/cockroachdb/cockroach/pkg/util" "github.com/cockroachdb/cockroach/pkg/util/leaktest" "github.com/cockroachdb/cockroach/pkg/util/randutil" "github.com/cockroachdb/errors" @@ -523,7 +523,7 @@ func TestArrowConversion(t *testing.T) { if rng.Float64() < nullChance { continue } - element := []byte(randgen.RandString(rng, 1+rng.Intn(maxStringLength), letters)) + element := []byte(util.RandString(rng, 1+rng.Intn(maxStringLength), letters)) b.Set(i, element) } diff --git a/pkg/internal/sqlsmith/BUILD.bazel b/pkg/internal/sqlsmith/BUILD.bazel index 2f19f652b932..76c60cdcd6c2 100644 --- a/pkg/internal/sqlsmith/BUILD.bazel +++ b/pkg/internal/sqlsmith/BUILD.bazel @@ -33,6 +33,7 @@ go_library( "//pkg/sql/sem/tree/treewindow", "//pkg/sql/sem/volatility", "//pkg/sql/types", + "//pkg/util", "//pkg/util/randident", "//pkg/util/randident/randidentcfg", "//pkg/util/syncutil", diff --git a/pkg/internal/sqlsmith/relational.go b/pkg/internal/sqlsmith/relational.go index 77c7177c6db3..081b90d8d9a1 100644 --- a/pkg/internal/sqlsmith/relational.go +++ b/pkg/internal/sqlsmith/relational.go @@ -15,6 +15,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree/treecmp" "github.com/cockroachdb/cockroach/pkg/sql/types" + "github.com/cockroachdb/cockroach/pkg/util" ) func (s *Smither) makeStmt() (stmt tree.Statement, ok bool) { @@ -1001,7 +1002,7 @@ func makeBegin(s *Smither) (tree.Statement, bool) { const letters = "abcdefghijklmnopqrstuvwxyz" func makeSavepoint(s *Smither) (tree.Statement, bool) { - savepointName := randgen.RandString(s.rnd, s.d9(), letters) + savepointName := util.RandString(s.rnd, s.d9(), letters) s.activeSavepoints = append(s.activeSavepoints, savepointName) return &tree.Savepoint{Name: tree.Name(savepointName)}, true } diff --git a/pkg/sql/randgen/BUILD.bazel b/pkg/sql/randgen/BUILD.bazel index 777c9b989e5d..aedd6889d440 100644 --- a/pkg/sql/randgen/BUILD.bazel +++ b/pkg/sql/randgen/BUILD.bazel @@ -10,7 +10,6 @@ go_library( "expr.go", "mutator.go", "schema.go", - "string.go", "type.go", ], importpath = "github.com/cockroachdb/cockroach/pkg/sql/randgen", @@ -39,6 +38,7 @@ go_library( "//pkg/sql/sem/volatility", "//pkg/sql/stats", "//pkg/sql/types", + "//pkg/util", "//pkg/util/bitarray", "//pkg/util/duration", "//pkg/util/encoding", diff --git a/pkg/sql/randgen/schema.go b/pkg/sql/randgen/schema.go index d081bb3f367f..fd0d8ba1db02 100644 --- a/pkg/sql/randgen/schema.go +++ b/pkg/sql/randgen/schema.go @@ -27,6 +27,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/sem/eval" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/types" + "github.com/cockroachdb/cockroach/pkg/util" "github.com/cockroachdb/cockroach/pkg/util/randutil" "github.com/cockroachdb/errors" "github.com/lib/pq/oid" @@ -53,7 +54,7 @@ func RandCreateType(rng *rand.Rand, name, alphabet string) tree.Statement { labelsMap := make(map[string]struct{}) i := 0 for i < numLabels { - s := RandString(rng, rng.Intn(6)+1, alphabet) + s := util.RandString(rng, rng.Intn(6)+1, alphabet) if _, ok := labelsMap[s]; !ok { labels[i] = tree.EnumValue(s) labelsMap[s] = struct{}{} diff --git a/pkg/sql/randgen/string.go b/pkg/sql/randgen/string.go deleted file mode 100644 index cc528b32ff95..000000000000 --- a/pkg/sql/randgen/string.go +++ /dev/null @@ -1,23 +0,0 @@ -// Copyright 2021 The Cockroach Authors. -// -// Use of this software is governed by the Business Source License -// included in the file licenses/BSL.txt. -// -// As of the Change Date specified in that file, in accordance with -// the Business Source License, use of this software will be governed -// by the Apache License, Version 2.0, included in the file -// licenses/APL.txt. - -package randgen - -import "math/rand" - -// RandString generates a random string of the desired length from the -// input alphabet. -func RandString(rng *rand.Rand, length int, alphabet string) string { - buf := make([]byte, length) - for i := range buf { - buf[i] = alphabet[rng.Intn(len(alphabet))] - } - return string(buf) -} diff --git a/pkg/sql/rowenc/BUILD.bazel b/pkg/sql/rowenc/BUILD.bazel index 0ba5b9748a94..e3eda4db8dd9 100644 --- a/pkg/sql/rowenc/BUILD.bazel +++ b/pkg/sql/rowenc/BUILD.bazel @@ -84,6 +84,7 @@ go_test( "//pkg/sql/types", "//pkg/testutils/datapathutils", "//pkg/testutils/serverutils", + "//pkg/util", "//pkg/util/encoding", "//pkg/util/json", "//pkg/util/leaktest", diff --git a/pkg/sql/rowenc/index_encoding_test.go b/pkg/sql/rowenc/index_encoding_test.go index 753bf513b00f..d1a969eb1041 100644 --- a/pkg/sql/rowenc/index_encoding_test.go +++ b/pkg/sql/rowenc/index_encoding_test.go @@ -33,6 +33,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/sem/eval" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/types" + "github.com/cockroachdb/cockroach/pkg/util" "github.com/cockroachdb/cockroach/pkg/util/json" "github.com/cockroachdb/cockroach/pkg/util/randutil" "github.com/cockroachdb/cockroach/pkg/util/trigram" @@ -1022,9 +1023,9 @@ func TestEncodeTrigramInvertedIndexSpans(t *testing.T) { // Generate two random strings and evaluate left % right, left LIKE right, // and left = right both via eval and via the span comparisons. - left := randgen.RandString(rng, 15, alphabet) + left := util.RandString(rng, 15, alphabet) length := 3 + rng.Intn(5) - right := randgen.RandString(rng, length, alphabet+"%") + right := util.RandString(rng, length, alphabet+"%") for _, searchType := range []trigramSearchType{like, eq, similar} { expr := makeTrigramBinOp(t, left, right, searchType) diff --git a/pkg/util/strings.go b/pkg/util/strings.go index 85100aa60677..c890fe5ea03e 100644 --- a/pkg/util/strings.go +++ b/pkg/util/strings.go @@ -14,6 +14,7 @@ import ( "bytes" "fmt" io "io" + "math/rand" "strings" "text/tabwriter" "unicode/utf8" @@ -156,3 +157,13 @@ func ExpandTabsInRedactableBytes(s redact.RedactableBytes) (redact.RedactableByt } return redact.RedactableBytes(buf.Bytes()), nil } + +// RandString generates a random string of the desired length from the +// input alphabet. +func RandString(rng *rand.Rand, length int, alphabet string) string { + buf := make([]byte, length) + for i := range buf { + buf[i] = alphabet[rng.Intn(len(alphabet))] + } + return string(buf) +}