Skip to content

Commit 786998c

Browse files
committed
set and enum
1 parent b570e91 commit 786998c

File tree

4 files changed

+143
-8
lines changed

4 files changed

+143
-8
lines changed

sql/types/decimal.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package types
1616

1717
import (
1818
"fmt"
19+
"github.com/dolthub/go-mysql-server/sql/encodings"
1920
"math/big"
2021
"reflect"
2122
"strings"
@@ -312,7 +313,7 @@ func (t DecimalType_) SQL(ctx *sql.Context, dest []byte, v interface{}) (sqltype
312313
return sqltypes.Value{}, err
313314
}
314315

315-
val := AppendAndSliceString(dest, t.DecimalValueStringFixed(value.Decimal))
316+
val := encodings.StringToBytes(t.DecimalValueStringFixed(value.Decimal))
316317

317318
return sqltypes.MakeTrusted(sqltypes.Decimal, val), nil
318319
}

sql/types/enum.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ var (
4949
type EnumType struct {
5050
collation sql.CollationID
5151
hashedValToIndex map[uint64]int
52+
valToIdx map[string]int
5253
indexToVal []string
5354
maxResponseByteLength uint32
5455
}
@@ -70,7 +71,8 @@ func CreateEnumType(values []string, collation sql.CollationID) (sql.EnumType, e
7071
// including accounting for multibyte character representations.
7172
var maxResponseByteLength uint32
7273
maxCharLength := collation.Collation().CharacterSet.MaxLength()
73-
valToIndex := make(map[uint64]int)
74+
hashedValToIndex := make(map[uint64]int)
75+
valToIdx := make(map[string]int)
7476
for i, value := range values {
7577
if !collation.Equals(sql.Collation_binary) {
7678
// Trailing spaces are automatically deleted from ENUM member values in the table definition when a table
@@ -82,11 +84,12 @@ func CreateEnumType(values []string, collation sql.CollationID) (sql.EnumType, e
8284
if err != nil {
8385
return nil, err
8486
}
85-
if _, ok := valToIndex[hashedVal]; ok {
87+
if _, ok := hashedValToIndex[hashedVal]; ok {
8688
return nil, fmt.Errorf("duplicate entry: %v", value)
8789
}
8890
// The elements listed in the column specification are assigned index numbers, beginning with 1.
89-
valToIndex[hashedVal] = i + 1
91+
hashedValToIndex[hashedVal] = i + 1
92+
valToIdx[value] = i + 1
9093

9194
byteLength := uint32(utf8.RuneCountInString(value) * int(maxCharLength))
9295
if byteLength > maxResponseByteLength {
@@ -95,8 +98,9 @@ func CreateEnumType(values []string, collation sql.CollationID) (sql.EnumType, e
9598
}
9699
return EnumType{
97100
collation: collation,
98-
hashedValToIndex: valToIndex,
101+
hashedValToIndex: hashedValToIndex,
99102
indexToVal: values,
103+
valToIdx: valToIdx,
100104
maxResponseByteLength: maxResponseByteLength,
101105
}, nil
102106
}
@@ -309,6 +313,9 @@ func (t EnumType) Collation() sql.CollationID {
309313

310314
// IndexOf implements EnumType interface.
311315
func (t EnumType) IndexOf(v string) int {
316+
if idx, ok := t.valToIdx[v]; ok {
317+
return idx
318+
}
312319
hashedVal, err := t.collation.HashToUint(v)
313320
if err == nil {
314321
if index, ok := t.hashedValToIndex[hashedVal]; ok {

sql/types/set.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ type SetType struct {
4444
collation sql.CollationID
4545
hashedValToBit map[uint64]uint64
4646
bitToVal map[uint64]string
47+
valToBit map[string]uint64
4748
maxResponseByteLength uint32
4849
}
4950

@@ -63,6 +64,7 @@ func CreateSetType(values []string, collation sql.CollationID) (sql.SetType, err
6364

6465
hashedValToBit := make(map[uint64]uint64)
6566
bitToVal := make(map[uint64]string)
67+
valToBit := make(map[string]uint64)
6668
var maxByteLength uint32
6769
maxCharLength := collation.Collation().CharacterSet.MaxLength()
6870
for i, value := range values {
@@ -84,6 +86,7 @@ func CreateSetType(values []string, collation sql.CollationID) (sql.SetType, err
8486
}
8587
bit := uint64(1 << uint64(i))
8688
hashedValToBit[hashedVal] = bit
89+
valToBit[value] = bit
8790
bitToVal[bit] = value
8891
maxByteLength = maxByteLength + uint32(utf8.RuneCountInString(value)*int(maxCharLength))
8992
if i != 0 {
@@ -94,6 +97,7 @@ func CreateSetType(values []string, collation sql.CollationID) (sql.SetType, err
9497
collation: collation,
9598
hashedValToBit: hashedValToBit,
9699
bitToVal: bitToVal,
100+
valToBit: valToBit,
97101
maxResponseByteLength: maxByteLength,
98102
}, nil
99103
}
@@ -366,16 +370,29 @@ func (t SetType) convertStringToBitField(str string) (uint64, error) {
366370
return 0, nil
367371
}
368372
var bitField uint64
369-
vals := strings.Split(str, ",")
370-
for _, val := range vals {
373+
lastI := 0
374+
var val string
375+
for i := 0; i < len(str)+1; i++ {
376+
if i < len(str) && str[i] != ',' {
377+
continue
378+
}
379+
371380
// empty string should hash to 0, so just skip
372-
if val == "" {
381+
if lastI == i {
382+
lastI = i + 1
373383
continue
374384
}
385+
val = str[lastI:i]
386+
lastI = i + 1
387+
375388
compareVal := val
376389
if t.collation != sql.Collation_binary {
377390
compareVal = strings.TrimRight(compareVal, " ")
378391
}
392+
if bit, ok := t.valToBit[compareVal]; ok {
393+
bitField |= bit
394+
continue
395+
}
379396
hashedVal, err := t.collation.HashToUint(compareVal)
380397
if err == nil {
381398
if bit, ok := t.hashedValToBit[hashedVal]; ok {

sql/types/sql_test.go

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package types
2+
3+
import (
4+
"github.com/dolthub/go-mysql-server/sql"
5+
"github.com/dolthub/vitess/go/sqltypes"
6+
"github.com/shopspring/decimal"
7+
"testing"
8+
)
9+
10+
var result_ sqltypes.Value
11+
12+
func BenchmarkNumI64SQL(b *testing.B) {
13+
var res sqltypes.Value
14+
t := Int64
15+
ctx := sql.NewEmptyContext()
16+
for i := 0; i < b.N; i++ {
17+
res, _ = t.SQL(ctx, nil, i)
18+
}
19+
result_ = res
20+
}
21+
22+
func BenchmarkVarchar10SQL(b *testing.B) {
23+
var res sqltypes.Value
24+
t := MustCreateStringWithDefaults(sqltypes.VarChar, 10)
25+
ctx := sql.NewEmptyContext()
26+
for i := 0; i < b.N; i++ {
27+
res, _ = t.SQL(ctx, nil, "char")
28+
}
29+
result_ = res
30+
}
31+
32+
func BenchmarkTimespanSQL(b *testing.B) {
33+
var res sqltypes.Value
34+
t := TimespanType_{}
35+
ctx := sql.NewEmptyContext()
36+
for i := 0; i < b.N; i++ {
37+
res, _ = t.SQL(ctx, nil, i%60)
38+
}
39+
result_ = res
40+
}
41+
42+
func BenchmarkTimestampSQL(b *testing.B) {
43+
var res sqltypes.Value
44+
t := Timestamp
45+
ctx := sql.NewEmptyContext()
46+
for i := 0; i < b.N; i++ {
47+
res, _ = t.SQL(ctx, nil, "2019-12-31T12:00:00Z")
48+
}
49+
result_ = res
50+
}
51+
52+
func BenchmarkDatetimeSQL(b *testing.B) {
53+
var res sqltypes.Value
54+
t := Datetime
55+
ctx := sql.NewEmptyContext()
56+
for i := 0; i < b.N; i++ {
57+
res, _ = t.SQL(ctx, nil, "2019-12-31T12:00:00Z")
58+
}
59+
result_ = res
60+
}
61+
62+
func BenchmarkEnumSQL(b *testing.B) {
63+
var res sqltypes.Value
64+
t, _ := CreateEnumType([]string{"a", "b", "c"}, sql.Collation_Default)
65+
ctx := sql.NewEmptyContext()
66+
for i := 0; i < b.N; i++ {
67+
res, _ = t.SQL(ctx, nil, "a")
68+
}
69+
result_ = res
70+
}
71+
72+
func BenchmarkSetSQL(b *testing.B) {
73+
var res sqltypes.Value
74+
t, _ := CreateSetType([]string{"a", "b", "c"}, sql.Collation_Default)
75+
ctx := sql.NewEmptyContext()
76+
for i := 0; i < b.N; i++ {
77+
res, _ = t.SQL(ctx, nil, "a")
78+
}
79+
result_ = res
80+
}
81+
82+
func BenchmarkBitSQL(b *testing.B) {
83+
var res sqltypes.Value
84+
t := BitType_{numOfBits: 8}
85+
ctx := sql.NewEmptyContext()
86+
for i := 0; i < b.N; i++ {
87+
res, _ = t.SQL(ctx, nil, i%8)
88+
}
89+
result_ = res
90+
}
91+
92+
func BenchmarkDecimalSQL(b *testing.B) {
93+
var res sqltypes.Value
94+
t, _ := CreateColumnDecimalType(2, 2)
95+
ctx := sql.NewEmptyContext()
96+
for i := 0; i < b.N; i++ {
97+
res, _ = t.SQL(ctx, nil, decimal.New(int64(i), 2))
98+
}
99+
result_ = res
100+
}
101+
102+
func BenchmarkNumF64SQL(b *testing.B) {
103+
var res sqltypes.Value
104+
t := Float64
105+
ctx := sql.NewEmptyContext()
106+
for i := 0; i < b.N; i++ {
107+
res, _ = t.SQL(ctx, nil, i)
108+
}
109+
result_ = res
110+
}

0 commit comments

Comments
 (0)