-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
timestamp.go
257 lines (222 loc) · 7.07 KB
/
timestamp.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
254
255
256
257
// 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"
"fmt"
"reflect"
"time"
"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"
)
// Timestamp is the timestamp without a time zone. Precision is unbounded.
var Timestamp = TimestampType{-1}
// TimestampType is the extended type implementation of the PostgreSQL timestamp without time zone.
type TimestampType struct {
// TODO: implement precision
Precision int8
}
var _ DoltgresType = TimestampType{}
// BaseID implements the DoltgresType interface.
func (b TimestampType) BaseID() DoltgresTypeBaseID {
return DoltgresTypeBaseID_Timestamp
}
// CollationCoercibility implements the DoltgresType interface.
func (b TimestampType) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
return sql.Collation_binary, 5
}
// Compare implements the DoltgresType interface.
func (b TimestampType) Compare(v1 any, v2 any) (int, error) {
if v1 == nil && v2 == nil {
return 0, nil
} else if v1 != nil && v2 == nil {
return 1, nil
} else if v1 == nil && v2 != nil {
return -1, nil
}
ac, _, err := b.Convert(v1)
if err != nil {
return 0, err
}
bc, _, err := b.Convert(v2)
if err != nil {
return 0, err
}
ab := ac.(time.Time)
bb := bc.(time.Time)
return ab.Compare(bb), nil
}
// Convert implements the DoltgresType interface.
func (b TimestampType) Convert(val any) (any, sql.ConvertInRange, error) {
switch val := val.(type) {
case time.Time:
return val, sql.InRange, nil
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 TimestampType) 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 TimestampType) 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 TimestampType) FormatValue(val any) (string, error) {
if val == nil {
return "", nil
}
return b.IoOutput(val)
}
// GetSerializationID implements the DoltgresType interface.
func (b TimestampType) GetSerializationID() SerializationID {
return SerializationID_Timestamp
}
// IoInput implements the DoltgresType interface.
func (b TimestampType) IoInput(input string) (any, error) {
if t, err := time.Parse("2006-01-02 15:04:05", input); err == nil {
return t.UTC(), nil
} else if t, err = time.Parse("January 01 15:04:05 2006", input); err == nil {
return t.UTC(), nil
}
return nil, fmt.Errorf("invalid format for timestamp")
}
// IoOutput implements the DoltgresType interface.
func (b TimestampType) IoOutput(output any) (string, error) {
converted, _, err := b.Convert(output)
if err != nil {
return "", err
}
return converted.(time.Time).Format("2006-01-02 15:04:05"), nil
}
// IsUnbounded implements the DoltgresType interface.
func (b TimestampType) IsUnbounded() bool {
return false
}
// MaxSerializedWidth implements the DoltgresType interface.
func (b TimestampType) MaxSerializedWidth() types.ExtendedTypeSerializedWidth {
return types.ExtendedTypeSerializedWidth_64K
}
// MaxTextResponseByteLength implements the DoltgresType interface.
func (b TimestampType) MaxTextResponseByteLength(ctx *sql.Context) uint32 {
return 32
}
// OID implements the DoltgresType interface.
func (b TimestampType) OID() uint32 {
return uint32(oid.T_timestamp)
}
// Promote implements the DoltgresType interface.
func (b TimestampType) Promote() sql.Type {
return Timestamp
}
// SerializedCompare implements the DoltgresType interface.
func (b TimestampType) 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
}
// The marshalled time format is byte-comparable
return bytes.Compare(v1, v2), nil
}
// SQL implements the DoltgresType interface.
func (b TimestampType) 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 TimestampType) String() string {
if b.Precision == -1 {
return "timestamp"
}
return fmt.Sprintf("timestamp(%d)", b.Precision)
}
// ToArrayType implements the DoltgresType interface.
func (b TimestampType) ToArrayType() DoltgresArrayType {
return createArrayType(b, SerializationID_TimestampArray, oid.T__timestamp)
}
// Type implements the DoltgresType interface.
func (b TimestampType) Type() query.Type {
return sqltypes.Text
}
// ValueType implements the DoltgresType interface.
func (b TimestampType) ValueType() reflect.Type {
return reflect.TypeOf(time.Time{})
}
// Zero implements the DoltgresType interface.
func (b TimestampType) Zero() any {
return time.Time{}
}
// SerializeType implements the DoltgresType interface.
func (b TimestampType) SerializeType() ([]byte, error) {
t := make([]byte, serializationIDHeaderSize+1)
copy(t, SerializationID_Timestamp.ToByteSlice(0))
t[serializationIDHeaderSize] = byte(b.Precision)
return t, nil
}
// deserializeType implements the DoltgresType interface.
func (b TimestampType) deserializeType(version uint16, metadata []byte) (DoltgresType, error) {
switch version {
case 0:
return TimestampType{
Precision: int8(metadata[0]),
}, nil
default:
return nil, fmt.Errorf("version %d is not yet supported for %s", version, b.String())
}
}
// SerializeValue implements the DoltgresType interface.
func (b TimestampType) SerializeValue(val any) ([]byte, error) {
if val == nil {
return nil, nil
}
converted, _, err := b.Convert(val)
if err != nil {
return nil, err
}
return converted.(time.Time).MarshalBinary()
}
// DeserializeValue implements the DoltgresType interface.
func (b TimestampType) DeserializeValue(val []byte) (any, error) {
if len(val) == 0 {
return nil, nil
}
t := time.Time{}
if err := t.UnmarshalBinary(val); err != nil {
return nil, err
}
return t, nil
}