Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Commit a5f69ea

Browse files
committed
Merge branch 'master' of ssh://github.com/kshvakov/clickhouse
2 parents e4ba95f + d6cb182 commit a5f69ea

File tree

2 files changed

+66
-14
lines changed

2 files changed

+66
-14
lines changed

lib/column/uuid.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ import (
99
"github.com/kshvakov/clickhouse/lib/binary"
1010
)
1111

12-
const UUIDLen = 16
12+
const (
13+
UUIDLen = 16
14+
NullUUID = "00000000-0000-0000-0000-000000000000"
15+
)
1316

1417
var ErrInvalidUUIDFormat = errors.New("invalid UUID format")
1518

@@ -84,6 +87,12 @@ func swap(src []byte) []byte {
8487

8588
func uuid2bytes(str string) ([]byte, error) {
8689
var uuid [16]byte
90+
strLength := len(str)
91+
if strLength == 0 {
92+
str = NullUUID
93+
} else if strLength != 36 {
94+
return nil, ErrInvalidUUIDFormat
95+
}
8796
if str[8] != '-' || str[13] != '-' || str[18] != '-' || str[23] != '-' {
8897
return nil, ErrInvalidUUIDFormat
8998
}

lib/column/uuid_test.go

Lines changed: 56 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,21 @@ import (
77
"testing"
88
)
99

10+
func bytes2uuid(src []byte) string {
11+
var uuid [36]byte
12+
hex.Encode(uuid[:], src[:4])
13+
uuid[8] = '-'
14+
hex.Encode(uuid[9:13], src[4:6])
15+
uuid[13] = '-'
16+
hex.Encode(uuid[14:18], src[6:8])
17+
uuid[18] = '-'
18+
hex.Encode(uuid[19:23], src[8:10])
19+
uuid[23] = '-'
20+
hex.Encode(uuid[24:], src[10:])
21+
return string(uuid[:])
22+
}
23+
1024
func Test_UUID2Bytes(t *testing.T) {
11-
bytes2uuid := func(src []byte) string {
12-
var uuid [36]byte
13-
hex.Encode(uuid[:], src[:4])
14-
uuid[8] = '-'
15-
hex.Encode(uuid[9:13], src[4:6])
16-
uuid[13] = '-'
17-
hex.Encode(uuid[14:18], src[6:8])
18-
uuid[18] = '-'
19-
hex.Encode(uuid[19:23], src[8:10])
20-
uuid[23] = '-'
21-
hex.Encode(uuid[24:], src[10:])
22-
return string(uuid[:])
23-
}
2425
origin := "00000000-0000-0000-0000-000000000000"
2526
if uuid, err := uuid2bytes(origin); assert.NoError(t, err) {
2627
assert.Equal(t, origin, bytes2uuid(uuid))
@@ -35,3 +36,45 @@ func Benchmark_UUID2Bytes(b *testing.B) {
3536
}
3637
}
3738
}
39+
40+
func Test_EmptyStringToNullUUID(t *testing.T) {
41+
origin := ""
42+
if uuid, err := uuid2bytes(origin); assert.NoError(t, err) {
43+
assert.Equal(t, "00000000-0000-0000-0000-000000000000", bytes2uuid(uuid))
44+
}
45+
}
46+
47+
func Test_ErrInvalidUUIDFormat(t *testing.T) {
48+
cases := []struct {
49+
origin string
50+
exceptedError error
51+
}{
52+
{
53+
"",
54+
nil,
55+
},
56+
{
57+
"a",
58+
ErrInvalidUUIDFormat,
59+
},
60+
{
61+
"00000000-0000-0000-00000000000000000",
62+
ErrInvalidUUIDFormat,
63+
},
64+
{
65+
"00000000-0000-0000-0000-0000000000000",
66+
ErrInvalidUUIDFormat,
67+
},
68+
}
69+
70+
for _, Case := range cases {
71+
_, err := uuid2bytes(Case.origin)
72+
if Case.exceptedError != nil {
73+
assert.Error(t, err)
74+
assert.EqualError(t, Case.exceptedError, err.Error())
75+
} else {
76+
assert.NoError(t, err)
77+
}
78+
79+
}
80+
}

0 commit comments

Comments
 (0)