forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
349 lines (317 loc) · 10.5 KB
/
util.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
// Copyright 2013 The Go-MySQL-Driver Authors. All rights reserved.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at http://mozilla.org/MPL/2.0/.
// The MIT License (MIT)
//
// Copyright (c) 2014 wandoulabs
// Copyright (c) 2014 siddontang
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// Copyright 2015 PingCAP, 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,
// See the License for the specific language governing permissions and
// limitations under the License.
package server
import (
"bytes"
"encoding/binary"
"io"
"math"
"strconv"
"time"
"github.com/juju/errors"
"github.com/pingcap/tidb/mysql"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/hack"
)
func parseLengthEncodedInt(b []byte) (num uint64, isNull bool, n int) {
switch b[0] {
// 251: NULL
case 0xfb:
n = 1
isNull = true
return
// 252: value of following 2
case 0xfc:
num = uint64(b[1]) | uint64(b[2])<<8
n = 3
return
// 253: value of following 3
case 0xfd:
num = uint64(b[1]) | uint64(b[2])<<8 | uint64(b[3])<<16
n = 4
return
// 254: value of following 8
case 0xfe:
num = uint64(b[1]) | uint64(b[2])<<8 | uint64(b[3])<<16 |
uint64(b[4])<<24 | uint64(b[5])<<32 | uint64(b[6])<<40 |
uint64(b[7])<<48 | uint64(b[8])<<56
n = 9
return
}
// 0-250: value of first byte
num = uint64(b[0])
n = 1
return
}
func dumpLengthEncodedInt(buffer []byte, n uint64) []byte {
switch {
case n <= 250:
return append(buffer, tinyIntCache[n]...)
case n <= 0xffff:
return append(buffer, 0xfc, byte(n), byte(n>>8))
case n <= 0xffffff:
return append(buffer, 0xfd, byte(n), byte(n>>8), byte(n>>16))
case n <= 0xffffffffffffffff:
return append(buffer, 0xfe, byte(n), byte(n>>8), byte(n>>16), byte(n>>24),
byte(n>>32), byte(n>>40), byte(n>>48), byte(n>>56))
}
return buffer
}
func parseLengthEncodedBytes(b []byte) ([]byte, bool, int, error) {
// Get length
num, isNull, n := parseLengthEncodedInt(b)
if num < 1 {
return nil, isNull, n, nil
}
n += int(num)
// Check data length
if len(b) >= n {
return b[n-int(num) : n], false, n, nil
}
return nil, false, n, io.EOF
}
func dumpLengthEncodedString(buffer []byte, bytes []byte) []byte {
buffer = dumpLengthEncodedInt(buffer, uint64(len(bytes)))
buffer = append(buffer, bytes...)
return buffer
}
func dumpUint16(buffer []byte, n uint16) []byte {
buffer = append(buffer, byte(n))
buffer = append(buffer, byte(n>>8))
return buffer
}
func dumpUint32(buffer []byte, n uint32) []byte {
buffer = append(buffer, byte(n))
buffer = append(buffer, byte(n>>8))
buffer = append(buffer, byte(n>>16))
buffer = append(buffer, byte(n>>24))
return buffer
}
func dumpUint64(buffer []byte, n uint64) []byte {
buffer = append(buffer, byte(n))
buffer = append(buffer, byte(n>>8))
buffer = append(buffer, byte(n>>16))
buffer = append(buffer, byte(n>>24))
buffer = append(buffer, byte(n>>32))
buffer = append(buffer, byte(n>>40))
buffer = append(buffer, byte(n>>48))
buffer = append(buffer, byte(n>>56))
return buffer
}
var tinyIntCache [251][]byte
func init() {
for i := 0; i < len(tinyIntCache); i++ {
tinyIntCache[i] = []byte{byte(i)}
}
}
func dumpBinaryTime(dur time.Duration) (data []byte) {
if dur == 0 {
data = tinyIntCache[0]
return
}
data = make([]byte, 13)
data[0] = 12
if dur < 0 {
data[1] = 1
dur = -dur
}
days := dur / (24 * time.Hour)
dur -= days * 24 * time.Hour
data[2] = byte(days)
hours := dur / time.Hour
dur -= hours * time.Hour
data[6] = byte(hours)
minutes := dur / time.Minute
dur -= minutes * time.Minute
data[7] = byte(minutes)
seconds := dur / time.Second
dur -= seconds * time.Second
data[8] = byte(seconds)
if dur == 0 {
data[0] = 8
return data[:9]
}
binary.LittleEndian.PutUint32(data[9:13], uint32(dur/time.Microsecond))
return
}
func dumpBinaryDateTime(data []byte, t types.Time, loc *time.Location) ([]byte, error) {
if t.Type == mysql.TypeTimestamp && loc != nil {
// TODO: Consider time_zone variable.
t1, err := t.Time.GoTime(time.Local)
if err != nil {
return nil, errors.Errorf("FATAL: convert timestamp %v go time return error!", t.Time)
}
t.Time = types.FromGoTime(t1.In(loc))
}
year, mon, day := t.Time.Year(), t.Time.Month(), t.Time.Day()
if t.IsZero() {
year, mon, day = 1, int(time.January), 1
}
switch t.Type {
case mysql.TypeTimestamp, mysql.TypeDatetime:
data = append(data, 11)
data = dumpUint16(data, uint16(year))
data = append(data, byte(mon), byte(day), byte(t.Time.Hour()), byte(t.Time.Minute()), byte(t.Time.Second()))
data = dumpUint32(data, uint32(t.Time.Microsecond()))
case mysql.TypeDate:
data = append(data, 4)
data = dumpUint16(data, uint16(year)) //year
data = append(data, byte(mon), byte(day))
}
return data, nil
}
func dumpBinaryRow(buffer []byte, columns []*ColumnInfo, row types.Row) ([]byte, error) {
buffer = append(buffer, mysql.OKHeader)
nullBitmapOff := len(buffer)
numBytes4Null := (len(columns) + 7 + 2) / 8
for i := 0; i < numBytes4Null; i++ {
buffer = append(buffer, 0)
}
for i := range columns {
if row.IsNull(i) {
bytePos := (i + 2) / 8
bitPos := byte((i + 2) % 8)
buffer[nullBitmapOff+bytePos] |= 1 << bitPos
continue
}
switch columns[i].Type {
case mysql.TypeTiny:
buffer = append(buffer, byte(row.GetInt64(i)))
case mysql.TypeShort, mysql.TypeYear:
buffer = dumpUint16(buffer, uint16(row.GetInt64(i)))
case mysql.TypeInt24, mysql.TypeLong:
buffer = dumpUint32(buffer, uint32(row.GetInt64(i)))
case mysql.TypeLonglong:
buffer = dumpUint64(buffer, row.GetUint64(i))
case mysql.TypeFloat:
buffer = dumpUint32(buffer, math.Float32bits(row.GetFloat32(i)))
case mysql.TypeDouble:
buffer = dumpUint64(buffer, math.Float64bits(row.GetFloat64(i)))
case mysql.TypeNewDecimal:
buffer = dumpLengthEncodedString(buffer, hack.Slice(row.GetMyDecimal(i).String()))
case mysql.TypeString, mysql.TypeVarString, mysql.TypeVarchar, mysql.TypeBit,
mysql.TypeTinyBlob, mysql.TypeMediumBlob, mysql.TypeLongBlob, mysql.TypeBlob:
buffer = dumpLengthEncodedString(buffer, row.GetBytes(i))
case mysql.TypeDate, mysql.TypeDatetime, mysql.TypeTimestamp:
var err error
buffer, err = dumpBinaryDateTime(buffer, row.GetTime(i), nil)
if err != nil {
return buffer, errors.Trace(err)
}
case mysql.TypeDuration:
buffer = append(buffer, dumpBinaryTime(row.GetDuration(i).Duration)...)
case mysql.TypeEnum:
buffer = dumpLengthEncodedString(buffer, hack.Slice(row.GetEnum(i).String()))
case mysql.TypeSet:
buffer = dumpLengthEncodedString(buffer, hack.Slice(row.GetSet(i).String()))
case mysql.TypeJSON:
buffer = dumpLengthEncodedString(buffer, hack.Slice(row.GetJSON(i).String()))
default:
return nil, errInvalidType.Gen("invalid type %v", columns[i].Type)
}
}
return buffer, nil
}
func dumpTextRow(buffer []byte, columns []*ColumnInfo, row types.Row) ([]byte, error) {
tmp := make([]byte, 0, 20)
for i, col := range columns {
if row.IsNull(i) {
buffer = append(buffer, 0xfb)
continue
}
switch col.Type {
case mysql.TypeTiny, mysql.TypeShort, mysql.TypeYear, mysql.TypeInt24, mysql.TypeLong:
tmp = strconv.AppendInt(tmp[:0], row.GetInt64(i), 10)
buffer = dumpLengthEncodedString(buffer, tmp)
case mysql.TypeLonglong:
if mysql.HasUnsignedFlag(uint(columns[i].Flag)) {
tmp = strconv.AppendUint(tmp[:0], row.GetUint64(i), 10)
} else {
tmp = strconv.AppendInt(tmp[:0], row.GetInt64(i), 10)
}
buffer = dumpLengthEncodedString(buffer, tmp)
case mysql.TypeFloat:
prec := -1
if columns[i].Decimal > 0 && int(col.Decimal) != mysql.NotFixedDec {
prec = int(col.Decimal)
}
tmp = appendFormatFloat(tmp[:0], float64(row.GetFloat32(i)), prec, 32)
buffer = dumpLengthEncodedString(buffer, tmp)
case mysql.TypeDouble:
prec := types.UnspecifiedLength
if col.Decimal > 0 && int(col.Decimal) != mysql.NotFixedDec {
prec = int(col.Decimal)
}
tmp = appendFormatFloat(tmp[:0], row.GetFloat64(i), prec, 64)
buffer = dumpLengthEncodedString(buffer, tmp)
case mysql.TypeNewDecimal:
buffer = dumpLengthEncodedString(buffer, hack.Slice(row.GetMyDecimal(i).String()))
case mysql.TypeString, mysql.TypeVarString, mysql.TypeVarchar, mysql.TypeBit,
mysql.TypeTinyBlob, mysql.TypeMediumBlob, mysql.TypeLongBlob, mysql.TypeBlob:
buffer = dumpLengthEncodedString(buffer, row.GetBytes(i))
case mysql.TypeDate, mysql.TypeDatetime, mysql.TypeTimestamp:
buffer = dumpLengthEncodedString(buffer, hack.Slice(row.GetTime(i).String()))
case mysql.TypeDuration:
buffer = dumpLengthEncodedString(buffer, hack.Slice(row.GetDuration(i).String()))
case mysql.TypeEnum:
buffer = dumpLengthEncodedString(buffer, hack.Slice(row.GetEnum(i).String()))
case mysql.TypeSet:
buffer = dumpLengthEncodedString(buffer, hack.Slice(row.GetSet(i).String()))
case mysql.TypeJSON:
buffer = dumpLengthEncodedString(buffer, hack.Slice(row.GetJSON(i).String()))
default:
return nil, errInvalidType.Gen("invalid type %v", columns[i].Type)
}
}
return buffer, nil
}
const (
expFormatBig = 1e15
expFormatSmall = 1e-15
)
func appendFormatFloat(in []byte, fVal float64, prec, bitSize int) []byte {
absVal := math.Abs(fVal)
var out []byte
if prec == types.UnspecifiedLength && (absVal >= expFormatBig || (absVal != 0 && absVal < expFormatSmall)) {
out = strconv.AppendFloat(in, fVal, 'e', prec, bitSize)
valStr := out[len(in):]
// remove the '+' from the string for compatibility.
plusPos := bytes.IndexByte(valStr, '+')
if plusPos > 0 {
plusPosInOut := len(in) + plusPos
out = append(out[:plusPosInOut], out[plusPosInOut+1:]...)
}
} else {
out = strconv.AppendFloat(in, fVal, 'f', prec, bitSize)
}
return out
}