Skip to content

Commit f3a5363

Browse files
committed
sql: add ltree value encoding tests
This was forgotten when we first implemented the ltree datum. Release note: None
1 parent cd43ab3 commit f3a5363

File tree

4 files changed

+62
-19
lines changed

4 files changed

+62
-19
lines changed

pkg/sql/randgen/datum.go

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -316,25 +316,7 @@ func RandDatumWithNullChance(
316316
case types.OidFamily:
317317
return tree.NewDOidWithType(oid.Oid(rng.Uint32()), typ)
318318
case types.LTreeFamily:
319-
length := rng.Intn(10)
320-
if length == 1 && rng.Intn(4) == 0 {
321-
return tree.NewDLTree(ltree.Empty)
322-
}
323-
labels := make([]string, length)
324-
for i := range labels {
325-
// Labels cannot be empty.
326-
labelLen := rng.Intn(9) + 1
327-
p := make([]byte, labelLen)
328-
for j := range p {
329-
p[j] = charSet[rng.Intn(len(charSet))]
330-
}
331-
labels[i] = string(p)
332-
}
333-
l, err := tree.ParseDLTree(strings.Join(labels, ltree.PathSeparator))
334-
if err != nil {
335-
return nil
336-
}
337-
return l
319+
return tree.NewDLTree(ltree.RandLTree(rng))
338320
case types.UnknownFamily:
339321
return tree.DNull
340322
case types.ArrayFamily:

pkg/util/encoding/encoding.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3644,6 +3644,13 @@ func PrettyPrintValueEncoded(b []byte) ([]byte, string, error) {
36443644
var s string
36453645
b, s, err = PrettyPrintTupleValueEncoded(b)
36463646
return b, s, err
3647+
case LTree:
3648+
var l ltree.T
3649+
b, l, err = DecodeLTreeValue(b)
3650+
if err != nil {
3651+
return b, "", err
3652+
}
3653+
return b, l.String(), nil
36473654
default:
36483655
return b, "", errors.Errorf("unknown type %s", typ)
36493656
}

pkg/util/encoding/encoding_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1725,6 +1725,10 @@ func (rd randData) ipAddr() ipaddr.IPAddr {
17251725
return ipaddr.RandIPAddr(rd.Rand)
17261726
}
17271727

1728+
func (rd randData) ltree() ltree.T {
1729+
return ltree.RandLTree(rd.Rand)
1730+
}
1731+
17281732
func BenchmarkEncodeUint32(b *testing.B) {
17291733
rng, _ := randutil.NewTestRand()
17301734

@@ -2251,6 +2255,25 @@ func TestValueEncodeDecodeDuration(t *testing.T) {
22512255
}
22522256
}
22532257

2258+
func TestValueEncodeDecodeLTree(t *testing.T) {
2259+
rng, seed := randutil.NewTestRand()
2260+
rd := randData{rng}
2261+
tests := make([]ltree.T, 1000)
2262+
for i := range tests {
2263+
tests[i] = rd.ltree()
2264+
}
2265+
for _, test := range tests {
2266+
buf := EncodeLTreeValue(nil, NoColumnID, test)
2267+
_, x, err := DecodeLTreeValue(buf)
2268+
if err != nil {
2269+
t.Fatal(err)
2270+
}
2271+
if x.Compare(test) != 0 {
2272+
t.Errorf("seed %d: expected %s got %s", seed, test.String(), x.String())
2273+
}
2274+
}
2275+
}
2276+
22542277
func BenchmarkEncodeNonsortingVarint(b *testing.B) {
22552278
bytes := make([]byte, 0, b.N*MaxNonsortingVarintLen)
22562279
rng, _ := randutil.NewTestRand()
@@ -2456,6 +2479,9 @@ func randValueEncode(rd randData, buf []byte, colID uint32, typ Type) ([]byte, i
24562479
case IPAddr:
24572480
x := rd.ipAddr()
24582481
return EncodeIPAddrValue(buf, colID, x), x, true
2482+
case LTree:
2483+
x := rd.ltree()
2484+
return EncodeLTreeValue(buf, colID, x), x, true
24592485
default:
24602486
return buf, nil, false
24612487
}
@@ -2611,6 +2637,8 @@ func TestValueEncodingRand(t *testing.T) {
26112637
buf, decoded, err = DecodeBitArrayValue(buf)
26122638
case IPAddr:
26132639
buf, decoded, err = DecodeIPAddrValue(buf)
2640+
case LTree:
2641+
buf, decoded, err = DecodeLTreeValue(buf)
26142642
default:
26152643
err = errors.Errorf("unknown type %s", typ)
26162644
}
@@ -2641,6 +2669,12 @@ func TestValueEncodingRand(t *testing.T) {
26412669
if bitarray.Compare(d, val) != 0 {
26422670
t.Fatalf("seed %d: %s got %v expected %v", seed, typ, decoded, value)
26432671
}
2672+
case LTree:
2673+
d := decoded.(ltree.T)
2674+
val := value.(ltree.T)
2675+
if d.Compare(val) != 0 {
2676+
t.Fatalf("seed %d: %s got %v expected %v", seed, typ, decoded, value)
2677+
}
26442678
default:
26452679
if decoded != value {
26462680
t.Fatalf("seed %d: %s got %v expected %v", seed, typ, decoded, value)

pkg/util/ltree/ltree.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package ltree
77

88
import (
99
"bytes"
10+
"math/rand"
1011
"strings"
1112

1213
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode"
@@ -144,6 +145,25 @@ func (lt T) Copy() T {
144145
return T{path: copiedLabels}
145146
}
146147

148+
func RandLTree(rng *rand.Rand) T {
149+
charSet := "-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"
150+
length := rng.Intn(10)
151+
if length == 0 {
152+
return Empty
153+
}
154+
labels := make([]string, length)
155+
for i := range labels {
156+
// Labels cannot be empty.
157+
labelLen := rng.Intn(9) + 1
158+
p := make([]byte, labelLen)
159+
for j := range p {
160+
p[j] = charSet[rng.Intn(len(charSet))]
161+
}
162+
labels[i] = string(p)
163+
}
164+
return T{path: labels}
165+
}
166+
147167
// validateLabel checks if a label is valid and returns an error if it is not,
148168
// otherwise, it returns nil.
149169
// A label is valid if it:

0 commit comments

Comments
 (0)