-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
xid.go
253 lines (222 loc) · 6.76 KB
/
xid.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
252
253
// Copyright 2024 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 types
import (
"bytes"
"encoding/binary"
"fmt"
"reflect"
"strconv"
"github.com/dolthub/go-mysql-server/sql"
"github.com/dolthub/go-mysql-server/sql/types"
"github.com/dolthub/vitess/go/sqltypes"
"github.com/dolthub/vitess/go/vt/proto/query"
"github.com/lib/pq/oid"
"github.com/shopspring/decimal"
)
// Xid is a data type used for internal transaction IDs. It is implemented as an unsigned 32 bit integer.
var Xid = XidType{}
// XidType is the extended type implementation of the PostgreSQL xid.
type XidType struct{}
var _ DoltgresType = XidType{}
// BaseID implements the DoltgresType interface.
func (b XidType) BaseID() DoltgresTypeBaseID {
return DoltgresTypeBaseID_Xid
}
// CollationCoercibility implements the DoltgresType interface.
func (b XidType) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
return sql.Collation_binary, 5
}
// Compare implements the DoltgresType interface.
func (b XidType) Compare(v1 any, v2 any) (int, error) {
return compareUint32(b, v1, v2)
}
// Convert implements the DoltgresType interface.
func (b XidType) Convert(val any) (any, sql.ConvertInRange, error) {
switch val := val.(type) {
case bool:
if val {
return uint32(1), sql.InRange, nil
}
return uint32(0), sql.InRange, nil
case int:
return uint32(val), sql.InRange, nil
case uint:
return uint32(val), sql.InRange, nil
case int8:
return uint32(val), sql.InRange, nil
case uint8:
return uint32(val), sql.InRange, nil
case int16:
return uint32(val), sql.InRange, nil
case uint16:
return uint32(val), sql.InRange, nil
case int32:
return uint32(val), sql.InRange, nil
case uint32:
return uint32(val), sql.InRange, nil
case int64:
return uint32(val), sql.InRange, nil
case uint64:
return uint32(val), sql.InRange, nil
case float32:
return uint32(val), sql.InRange, nil
case float64:
return uint32(val), sql.InRange, nil
case decimal.NullDecimal:
if !val.Valid {
return nil, sql.InRange, nil
}
return b.Convert(val.Decimal)
case decimal.Decimal:
return uint32(val.IntPart()), sql.InRange, nil
case string:
i, err := strconv.ParseInt(val, 10, 64)
if err != nil {
return 0, sql.InRange, nil
}
return uint32(i), sql.InRange, nil
case []byte:
return b.Convert(string(val))
case nil:
return nil, sql.InRange, nil
default:
return nil, sql.OutOfRange, fmt.Errorf("%s: unhandled type: %T", b.String(), val)
}
}
// Equals implements the DoltgresType interface.
func (b XidType) Equals(otherType sql.Type) bool {
if otherExtendedType, ok := otherType.(types.ExtendedType); ok {
return bytes.Equal(MustSerializeType(b), MustSerializeType(otherExtendedType))
}
return false
}
// FormatSerializedValue implements the DoltgresType interface.
func (b XidType) FormatSerializedValue(val []byte) (string, error) {
deserialized, err := b.DeserializeValue(val)
if err != nil {
return "", err
}
return b.FormatValue(deserialized)
}
// FormatValue implements the DoltgresType interface.
func (b XidType) FormatValue(val any) (string, error) {
if val == nil {
return "", nil
}
converted, _, err := b.Convert(val)
if err != nil {
return "", err
}
return strconv.FormatInt(int64(converted.(uint32)), 10), nil
}
// GetSerializationID implements the DoltgresType interface.
func (b XidType) GetSerializationID() SerializationID {
return SerializationID_Xid
}
// IsUnbounded implements the DoltgresType interface.
func (b XidType) IsUnbounded() bool {
return false
}
// MaxSerializedWidth implements the DoltgresType interface.
func (b XidType) MaxSerializedWidth() types.ExtendedTypeSerializedWidth {
return types.ExtendedTypeSerializedWidth_64K
}
// MaxTextResponseByteLength implements the DoltgresType interface.
func (b XidType) MaxTextResponseByteLength(ctx *sql.Context) uint32 {
return 4
}
// OID implements the DoltgresType interface.
func (b XidType) OID() uint32 {
return uint32(oid.T_xid)
}
// Promote implements the DoltgresType interface.
func (b XidType) Promote() sql.Type {
return b
}
// SerializedCompare implements the DoltgresType interface.
func (b XidType) SerializedCompare(v1 []byte, v2 []byte) (int, error) {
if len(v1) == 0 && len(v2) == 0 {
return 0, nil
} else if len(v1) > 0 && len(v2) == 0 {
return 1, nil
} else if len(v1) == 0 && len(v2) > 0 {
return -1, nil
}
return bytes.Compare(v1, v2), nil
}
// SQL implements the DoltgresType interface.
func (b XidType) SQL(ctx *sql.Context, dest []byte, v any) (sqltypes.Value, error) {
if v == nil {
return sqltypes.NULL, nil
}
value, err := b.FormatValue(v)
if err != nil {
return sqltypes.Value{}, err
}
return sqltypes.MakeTrusted(sqltypes.Text, types.AppendAndSliceBytes(dest, []byte(value))), nil
}
// String implements the DoltgresType interface.
func (b XidType) String() string {
return "xid"
}
// ToArrayType implements the DoltgresType interface.
func (b XidType) ToArrayType() DoltgresArrayType {
return XidArray
}
// Type implements the DoltgresType interface.
func (b XidType) Type() query.Type {
return sqltypes.Uint32
}
// ValueType implements the DoltgresType interface.
func (b XidType) ValueType() reflect.Type {
return reflect.TypeOf(uint32(0))
}
// Zero implements the DoltgresType interface.
func (b XidType) Zero() any {
return uint32(0)
}
// SerializeType implements the DoltgresType interface.
func (b XidType) SerializeType() ([]byte, error) {
return SerializationID_Xid.ToByteSlice(0), nil
}
// deserializeType implements the DoltgresType interface.
func (b XidType) deserializeType(version uint16, metadata []byte) (DoltgresType, error) {
switch version {
case 0:
return Xid, nil
default:
return nil, fmt.Errorf("version %d is not yet supported for %s", version, b.String())
}
}
// SerializeValue implements the DoltgresType interface.
func (b XidType) SerializeValue(val any) ([]byte, error) {
if val == nil {
return nil, nil
}
converted, _, err := b.Convert(val)
if err != nil {
return nil, err
}
retVal := make([]byte, 4)
binary.BigEndian.PutUint32(retVal, uint32(converted.(uint32)))
return retVal, nil
}
// DeserializeValue implements the DoltgresType interface.
func (b XidType) DeserializeValue(val []byte) (any, error) {
if len(val) == 0 {
return nil, nil
}
return uint32(binary.BigEndian.Uint32(val)), nil
}