-
-
Notifications
You must be signed in to change notification settings - Fork 480
/
set.go
251 lines (229 loc) · 7.64 KB
/
set.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// Copyright 2020 Dolthub, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package typeinfo
import (
"context"
"encoding/gob"
"fmt"
"strings"
"github.com/dolthub/go-mysql-server/sql"
"github.com/dolthub/dolt/go/store/types"
)
const (
setTypeParam_Collation = "collate"
setTypeParam_Values = "vals"
)
// This is a dolt implementation of the MySQL type Set, thus most of the functionality
// within is directly reliant on the go-mysql-server implementation.
type setType struct {
sqlSetType sql.SetType
}
var _ TypeInfo = (*setType)(nil)
func CreateSetTypeFromParams(params map[string]string) (TypeInfo, error) {
var collation sql.Collation
var err error
if collationStr, ok := params[setTypeParam_Collation]; ok {
collation, err = sql.ParseCollation(nil, &collationStr, false)
if err != nil {
return nil, err
}
} else {
return nil, fmt.Errorf(`create set type info is missing param "%v"`, setTypeParam_Collation)
}
var values []string
if valuesStr, ok := params[setTypeParam_Values]; ok {
dec := gob.NewDecoder(strings.NewReader(valuesStr))
err = dec.Decode(&values)
if err != nil {
return nil, err
}
} else {
return nil, fmt.Errorf(`create set type info is missing param "%v"`, setTypeParam_Values)
}
sqlSetType, err := sql.CreateSetType(values, collation)
if err != nil {
return nil, err
}
return &setType{sqlSetType}, nil
}
// ConvertNomsValueToValue implements TypeInfo interface.
func (ti *setType) ConvertNomsValueToValue(v types.Value) (interface{}, error) {
if val, ok := v.(types.Uint); ok {
res, err := ti.sqlSetType.Unmarshal(uint64(val))
if err != nil {
return nil, fmt.Errorf(`"%v" cannot convert "%v" to value`, ti.String(), val)
}
return res, nil
}
if _, ok := v.(types.Null); ok || v == nil {
return nil, nil
}
return nil, fmt.Errorf(`"%v" cannot convert NomsKind "%v" to a value`, ti.String(), v.Kind())
}
// ReadFrom reads a go value from a noms types.CodecReader directly
func (ti *setType) ReadFrom(_ *types.NomsBinFormat, reader types.CodecReader) (interface{}, error) {
k := reader.ReadKind()
switch k {
case types.UintKind:
val := reader.ReadUint()
res, err := ti.sqlSetType.Unmarshal(uint64(val))
if err != nil {
return nil, fmt.Errorf(`"%v" cannot convert "%v" to value`, ti.String(), val)
}
return res, nil
case types.NullKind:
return nil, nil
}
return nil, fmt.Errorf(`"%v" cannot convert NomsKind "%v" to a value`, ti.String(), k)
}
// ConvertValueToNomsValue implements TypeInfo interface.
func (ti *setType) ConvertValueToNomsValue(ctx context.Context, vrw types.ValueReadWriter, v interface{}) (types.Value, error) {
if v == nil {
return types.NullValue, nil
}
val, err := ti.sqlSetType.Marshal(v)
if err != nil {
return nil, err
}
return types.Uint(val), nil
}
// Equals implements TypeInfo interface.
func (ti *setType) Equals(other TypeInfo) bool {
if other == nil {
return false
}
if ti2, ok := other.(*setType); ok && ti.sqlSetType.NumberOfElements() == ti2.sqlSetType.NumberOfElements() {
tiVals := ti.sqlSetType.Values()
ti2Vals := ti2.sqlSetType.Values()
for i := range tiVals {
if tiVals[i] != ti2Vals[i] {
return false
}
}
return true
}
return false
}
// FormatValue implements TypeInfo interface.
func (ti *setType) FormatValue(v types.Value) (*string, error) {
if _, ok := v.(types.Null); ok || v == nil {
return nil, nil
}
strVal, err := ti.ConvertNomsValueToValue(v)
if err != nil {
return nil, err
}
val, ok := strVal.(string)
if !ok {
return nil, fmt.Errorf(`"%v" has unexpectedly encountered a value of type "%T" from embedded type`, ti.String(), v)
}
return &val, nil
}
// GetTypeIdentifier implements TypeInfo interface.
func (ti *setType) GetTypeIdentifier() Identifier {
return SetTypeIdentifier
}
// GetTypeParams implements TypeInfo interface.
func (ti *setType) GetTypeParams() map[string]string {
var sb strings.Builder
enc := gob.NewEncoder(&sb)
err := enc.Encode(ti.sqlSetType.Values())
// this should never error, encoding an array of strings should always succeed
if err != nil {
panic(err)
}
return map[string]string{
setTypeParam_Collation: ti.sqlSetType.Collation().String(),
setTypeParam_Values: sb.String(),
}
}
// IsValid implements TypeInfo interface.
func (ti *setType) IsValid(v types.Value) bool {
if val, ok := v.(types.Uint); ok {
_, err := ti.sqlSetType.Unmarshal(uint64(val))
if err != nil {
return false
}
return true
}
if _, ok := v.(types.Null); ok || v == nil {
return true
}
return false
}
// NomsKind implements TypeInfo interface.
func (ti *setType) NomsKind() types.NomsKind {
return types.UintKind
}
// Promote implements TypeInfo interface.
func (ti *setType) Promote() TypeInfo {
return &setType{ti.sqlSetType.Promote().(sql.SetType)}
}
// String implements TypeInfo interface.
func (ti *setType) String() string {
return fmt.Sprintf(`Set(Collation: %v, Values: %v)`, ti.sqlSetType.Collation().String(), strings.Join(ti.sqlSetType.Values(), ","))
}
// ToSqlType implements TypeInfo interface.
func (ti *setType) ToSqlType() sql.Type {
return ti.sqlSetType
}
// setTypeConverter is an internal function for GetTypeConverter that handles the specific type as the source TypeInfo.
func setTypeConverter(ctx context.Context, src *setType, destTi TypeInfo) (tc TypeConverter, needsConversion bool, err error) {
switch dest := destTi.(type) {
case *bitType:
return wrapConvertValueToNomsValue(dest.ConvertValueToNomsValue)
case *blobStringType:
return wrapConvertValueToNomsValue(dest.ConvertValueToNomsValue)
case *boolType:
return wrapConvertValueToNomsValue(dest.ConvertValueToNomsValue)
case *datetimeType:
return wrapConvertValueToNomsValue(dest.ConvertValueToNomsValue)
case *decimalType:
return wrapConvertValueToNomsValue(dest.ConvertValueToNomsValue)
case *enumType:
return wrapConvertValueToNomsValue(dest.ConvertValueToNomsValue)
case *floatType:
return wrapConvertValueToNomsValue(dest.ConvertValueToNomsValue)
case *geometryType:
return wrapConvertValueToNomsValue(dest.ConvertValueToNomsValue)
case *inlineBlobType:
return wrapConvertValueToNomsValue(dest.ConvertValueToNomsValue)
case *intType:
return wrapConvertValueToNomsValue(dest.ConvertValueToNomsValue)
case *jsonType:
return wrapConvertValueToNomsValue(dest.ConvertValueToNomsValue)
case *linestringType:
return wrapConvertValueToNomsValue(dest.ConvertValueToNomsValue)
case *pointType:
return wrapConvertValueToNomsValue(dest.ConvertValueToNomsValue)
case *polygonType:
return wrapConvertValueToNomsValue(dest.ConvertValueToNomsValue)
case *setType:
return wrapIsValid(dest.IsValid, src, dest)
case *timeType:
return wrapConvertValueToNomsValue(dest.ConvertValueToNomsValue)
case *uintType:
return wrapConvertValueToNomsValue(dest.ConvertValueToNomsValue)
case *uuidType:
return wrapConvertValueToNomsValue(dest.ConvertValueToNomsValue)
case *varBinaryType:
return wrapConvertValueToNomsValue(dest.ConvertValueToNomsValue)
case *varStringType:
return wrapConvertValueToNomsValue(dest.ConvertValueToNomsValue)
case *yearType:
return wrapConvertValueToNomsValue(dest.ConvertValueToNomsValue)
default:
return nil, false, UnhandledTypeConversion.New(src.String(), destTi.String())
}
}