Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sql: move RandString to util to break coldata->rangden->sem/tree dep #96229

Merged
merged 1 commit into from
Jan 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/col/coldata/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions pkg/col/coldata/bytes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}

Expand Down
1 change: 1 addition & 0 deletions pkg/internal/sqlsmith/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion pkg/internal/sqlsmith/relational.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/randgen/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ go_library(
"expr.go",
"mutator.go",
"schema.go",
"string.go",
"type.go",
],
importpath = "github.com/cockroachdb/cockroach/pkg/sql/randgen",
Expand Down Expand Up @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion pkg/sql/randgen/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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{}{}
Expand Down
23 changes: 0 additions & 23 deletions pkg/sql/randgen/string.go

This file was deleted.

1 change: 1 addition & 0 deletions pkg/sql/rowenc/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 3 additions & 2 deletions pkg/sql/rowenc/index_encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
11 changes: 11 additions & 0 deletions pkg/util/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"bytes"
"fmt"
io "io"
"math/rand"
"strings"
"text/tabwriter"
"unicode/utf8"
Expand Down Expand Up @@ -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)
}