forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
542 lines (476 loc) · 15.3 KB
/
types.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
// Copyright 2015 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package pgwire
import (
"context"
"encoding/binary"
"math"
"math/big"
"net"
"strconv"
"strings"
"time"
"github.com/cockroachdb/apd"
"github.com/cockroachdb/cockroach/pkg/server/telemetry"
"github.com/cockroachdb/cockroach/pkg/sql/lex"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgwirebase"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sessiondata"
"github.com/cockroachdb/cockroach/pkg/sql/sqltelemetry"
"github.com/cockroachdb/cockroach/pkg/sql/types"
"github.com/cockroachdb/cockroach/pkg/util/duration"
"github.com/cockroachdb/cockroach/pkg/util/errorutil/unimplemented"
"github.com/cockroachdb/cockroach/pkg/util/ipaddr"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/timeofday"
"github.com/cockroachdb/cockroach/pkg/util/timetz"
"github.com/cockroachdb/errors"
"github.com/lib/pq/oid"
)
// pgType contains type metadata used in RowDescription messages.
type pgType struct {
oid oid.Oid
// Variable-size types have size=-1.
// Note that the protocol has both int16 and int32 size fields,
// so this attribute is an unsized int and should be cast
// as needed.
// This field does *not* correspond to the encoded length of a
// data type, so it's unclear what, if anything, it is used for.
// To get the right value, "SELECT oid, typlen FROM pg_type"
// on a postgres server.
size int
}
func pgTypeForParserType(t *types.T) pgType {
size := -1
if s, variable := tree.DatumTypeSize(t); !variable {
size = int(s)
}
return pgType{
oid: t.Oid(),
size: size,
}
}
func (b *writeBuffer) writeTextDatum(
ctx context.Context, d tree.Datum, conv sessiondata.DataConversionConfig,
) {
if log.V(2) {
log.Infof(ctx, "pgwire writing TEXT datum of type: %T, %#v", d, d)
}
if d == tree.DNull {
// NULL is encoded as -1; all other values have a length prefix.
b.putInt32(-1)
return
}
switch v := tree.UnwrapDatum(nil, d).(type) {
case *tree.DBitArray:
b.textFormatter.FormatNode(v)
b.writeFromFmtCtx(b.textFormatter)
case *tree.DBool:
b.textFormatter.FormatNode(v)
b.writeFromFmtCtx(b.textFormatter)
case *tree.DInt:
// Start at offset 4 because `putInt32` clobbers the first 4 bytes.
s := strconv.AppendInt(b.putbuf[4:4], int64(*v), 10)
b.putInt32(int32(len(s)))
b.write(s)
case *tree.DFloat:
// Start at offset 4 because `putInt32` clobbers the first 4 bytes.
s := strconv.AppendFloat(b.putbuf[4:4], float64(*v), 'g', conv.GetFloatPrec(), 64)
b.putInt32(int32(len(s)))
b.write(s)
case *tree.DDecimal:
b.writeLengthPrefixedDatum(v)
case *tree.DBytes:
result := lex.EncodeByteArrayToRawBytes(
string(*v), conv.BytesEncodeFormat, false /* skipHexPrefix */)
b.putInt32(int32(len(result)))
b.write([]byte(result))
case *tree.DUuid:
// Start at offset 4 because `putInt32` clobbers the first 4 bytes.
s := b.putbuf[4 : 4+36]
v.UUID.StringBytes(s)
b.putInt32(int32(len(s)))
b.write(s)
case *tree.DIPAddr:
b.writeLengthPrefixedString(v.IPAddr.String())
case *tree.DString:
b.writeLengthPrefixedString(string(*v))
case *tree.DCollatedString:
b.writeLengthPrefixedString(v.Contents)
case *tree.DDate:
b.textFormatter.FormatNode(v)
b.writeFromFmtCtx(b.textFormatter)
case *tree.DTime:
// Start at offset 4 because `putInt32` clobbers the first 4 bytes.
s := formatTime(timeofday.TimeOfDay(*v), b.putbuf[4:4])
b.putInt32(int32(len(s)))
b.write(s)
case *tree.DTimeTZ:
// Start at offset 4 because `putInt32` clobbers the first 4 bytes.
s := formatTimeTZ(v.TimeTZ, b.putbuf[4:4])
b.putInt32(int32(len(s)))
b.write(s)
case *tree.DTimestamp:
// Start at offset 4 because `putInt32` clobbers the first 4 bytes.
s := formatTs(v.Time, nil, b.putbuf[4:4])
b.putInt32(int32(len(s)))
b.write(s)
case *tree.DTimestampTZ:
// Start at offset 4 because `putInt32` clobbers the first 4 bytes.
s := formatTs(v.Time, conv.Location, b.putbuf[4:4])
b.putInt32(int32(len(s)))
b.write(s)
case *tree.DInterval:
b.textFormatter.FormatNode(v)
b.writeFromFmtCtx(b.textFormatter)
case *tree.DJSON:
b.writeLengthPrefixedString(v.JSON.String())
case *tree.DTuple:
b.textFormatter.FormatNode(v)
b.writeFromFmtCtx(b.textFormatter)
case *tree.DArray:
// Arrays have custom formatting depending on their OID.
b.textFormatter.FormatNode(d)
b.writeFromFmtCtx(b.textFormatter)
case *tree.DOid:
b.writeLengthPrefixedDatum(v)
default:
b.setError(errors.Errorf("unsupported type %T", d))
}
}
// writeBinaryDatum writes d to the buffer. Oid must be specified for types
// that have various width encodings. It is ignored (and can be 0) for types
// with a 1:1 datum:oid mapping.
func (b *writeBuffer) writeBinaryDatum(
ctx context.Context, d tree.Datum, sessionLoc *time.Location, Oid oid.Oid,
) {
if log.V(2) {
log.Infof(ctx, "pgwire writing BINARY datum of type: %T, %#v", d, d)
}
if d == tree.DNull {
// NULL is encoded as -1; all other values have a length prefix.
b.putInt32(-1)
return
}
switch v := tree.UnwrapDatum(nil, d).(type) {
case *tree.DBitArray:
words, lastBitsUsed := v.EncodingParts()
if len(words) == 0 {
b.putInt32(4)
} else {
// Encode the length of the output bytes. It is computed here so we don't
// have to keep a buffer.
// 4: the int32 of the bitLen.
// 8*(len(words)-1): number of 8-byte words except the last one since it's
// partial.
// (lastBitsUsed+7)/8: number of bytes that will be written in the last
// partial word. The /8 rounds down, such that the +7 will cause 1-or-more
// bits to use a byte, but 0 will not.
b.putInt32(4 + int32(8*(len(words)-1)) + int32((lastBitsUsed+7)/8))
}
bitLen := v.BitLen()
b.putInt32(int32(bitLen))
var byteBuf [8]byte
for i := 0; i < len(words)-1; i++ {
w := words[i]
binary.BigEndian.PutUint64(byteBuf[:], w)
b.write(byteBuf[:])
}
if len(words) > 0 {
w := words[len(words)-1]
for i := uint(0); i < uint(lastBitsUsed); i += 8 {
c := byte(w >> (56 - i))
b.writeByte(c)
}
}
case *tree.DBool:
b.putInt32(1)
if *v {
b.writeByte(1)
} else {
b.writeByte(0)
}
case *tree.DInt:
switch Oid {
case oid.T_int2:
b.putInt32(2)
b.putInt16(int16(*v))
case oid.T_int4:
b.putInt32(4)
b.putInt32(int32(*v))
case oid.T_int8:
b.putInt32(8)
b.putInt64(int64(*v))
default:
b.setError(errors.Errorf("unsupported int oid: %v", Oid))
}
case *tree.DFloat:
switch Oid {
case oid.T_float4:
b.putInt32(4)
b.putInt32(int32(math.Float32bits(float32(*v))))
case oid.T_float8:
b.putInt32(8)
b.putInt64(int64(math.Float64bits(float64(*v))))
default:
b.setError(errors.Errorf("unsupported float oid: %v", Oid))
}
case *tree.DDecimal:
if v.Form != apd.Finite {
b.putInt32(8)
// 0 digits.
b.putInt32(0)
// https://github.com/postgres/postgres/blob/ffa4cbd623dd69f9fa99e5e92426928a5782cf1a/src/backend/utils/adt/numeric.c#L169
b.write([]byte{0xc0, 0, 0, 0})
if v.Form == apd.Infinite {
// TODO(mjibson): #32489
// The above encoding is not correct for Infinity, but since that encoding
// doesn't exist in postgres, it's unclear what to do. For now use the NaN
// encoding and count it to see if anyone even needs this.
telemetry.Inc(sqltelemetry.BinaryDecimalInfinityCounter)
}
return
}
alloc := struct {
pgNum pgwirebase.PGNumeric
bigI big.Int
}{
pgNum: pgwirebase.PGNumeric{
// Since we use 2000 as the exponent limits in tree.DecimalCtx, this
// conversion should not overflow.
Dscale: int16(-v.Exponent),
},
}
if v.Sign() >= 0 {
alloc.pgNum.Sign = pgwirebase.PGNumericPos
} else {
alloc.pgNum.Sign = pgwirebase.PGNumericNeg
}
isZero := func(r rune) bool {
return r == '0'
}
// Mostly cribbed from libpqtypes' str2num.
digits := strings.TrimLeftFunc(alloc.bigI.Abs(&v.Coeff).String(), isZero)
dweight := len(digits) - int(alloc.pgNum.Dscale) - 1
digits = strings.TrimRightFunc(digits, isZero)
if dweight >= 0 {
alloc.pgNum.Weight = int16((dweight+1+pgwirebase.PGDecDigits-1)/pgwirebase.PGDecDigits - 1)
} else {
alloc.pgNum.Weight = int16(-((-dweight-1)/pgwirebase.PGDecDigits + 1))
}
offset := (int(alloc.pgNum.Weight)+1)*pgwirebase.PGDecDigits - (dweight + 1)
alloc.pgNum.Ndigits = int16((len(digits) + offset + pgwirebase.PGDecDigits - 1) / pgwirebase.PGDecDigits)
if len(digits) == 0 {
offset = 0
alloc.pgNum.Ndigits = 0
alloc.pgNum.Weight = 0
}
digitIdx := -offset
nextDigit := func() int16 {
var ndigit int16
for nextDigitIdx := digitIdx + pgwirebase.PGDecDigits; digitIdx < nextDigitIdx; digitIdx++ {
ndigit *= 10
if digitIdx >= 0 && digitIdx < len(digits) {
ndigit += int16(digits[digitIdx] - '0')
}
}
return ndigit
}
b.putInt32(int32(2 * (4 + alloc.pgNum.Ndigits)))
b.putInt16(alloc.pgNum.Ndigits)
b.putInt16(alloc.pgNum.Weight)
b.putInt16(int16(alloc.pgNum.Sign))
b.putInt16(alloc.pgNum.Dscale)
for digitIdx < len(digits) {
b.putInt16(nextDigit())
}
case *tree.DBytes:
b.putInt32(int32(len(*v)))
b.write([]byte(*v))
case *tree.DUuid:
b.putInt32(16)
b.write(v.GetBytes())
case *tree.DIPAddr:
// We calculate the Postgres binary format for an IPAddr. For the spec see,
// https://github.com/postgres/postgres/blob/81c5e46c490e2426db243eada186995da5bb0ba7/src/backend/utils/adt/network.c#L144
// The pgBinary encoding is as follows:
// The int32 length of the following bytes.
// The family byte.
// The mask size byte.
// A 0 byte for is_cidr. It's ignored on the postgres frontend.
// The length of our IP bytes.
// The IP bytes.
const pgIPAddrBinaryHeaderSize = 4
if v.Family == ipaddr.IPv4family {
b.putInt32(net.IPv4len + pgIPAddrBinaryHeaderSize)
b.writeByte(pgwirebase.PGBinaryIPv4family)
b.writeByte(v.Mask)
b.writeByte(0)
b.writeByte(byte(net.IPv4len))
err := v.Addr.WriteIPv4Bytes(b)
if err != nil {
b.setError(err)
}
} else if v.Family == ipaddr.IPv6family {
b.putInt32(net.IPv6len + pgIPAddrBinaryHeaderSize)
b.writeByte(pgwirebase.PGBinaryIPv6family)
b.writeByte(v.Mask)
b.writeByte(0)
b.writeByte(byte(net.IPv6len))
err := v.Addr.WriteIPv6Bytes(b)
if err != nil {
b.setError(err)
}
} else {
b.setError(errors.Errorf("error encoding inet to pgBinary: %v", v.IPAddr))
}
case *tree.DString:
b.writeLengthPrefixedString(string(*v))
case *tree.DCollatedString:
b.writeLengthPrefixedString(v.Contents)
case *tree.DTimestamp:
b.putInt32(8)
b.putInt64(timeToPgBinary(v.Time, nil))
case *tree.DTimestampTZ:
b.putInt32(8)
b.putInt64(timeToPgBinary(v.Time, sessionLoc))
case *tree.DDate:
b.putInt32(4)
b.putInt32(v.PGEpochDays())
case *tree.DTime:
b.putInt32(8)
b.putInt64(int64(*v))
case *tree.DTimeTZ:
b.putInt32(12)
b.putInt64(int64(v.TimeOfDay))
b.putInt32(v.OffsetSecs)
case *tree.DInterval:
b.putInt32(16)
b.putInt64(v.Nanos() / int64(time.Microsecond/time.Nanosecond))
b.putInt32(int32(v.Days))
b.putInt32(int32(v.Months))
case *tree.DTuple:
// TODO(andrei): We shouldn't be allocating a new buffer for every array.
subWriter := newWriteBuffer(nil /* bytecount */)
// Put the number of datums.
subWriter.putInt32(int32(len(v.D)))
for _, elem := range v.D {
oid := elem.ResolvedType().Oid()
subWriter.putInt32(int32(oid))
subWriter.writeBinaryDatum(ctx, elem, sessionLoc, oid)
}
b.writeLengthPrefixedBuffer(&subWriter.wrapped)
case *tree.DArray:
if v.ParamTyp.Family() == types.ArrayFamily {
b.setError(unimplemented.NewWithIssueDetail(32552,
"binenc", "unsupported binary serialization of multidimensional arrays"))
return
}
// TODO(andrei): We shouldn't be allocating a new buffer for every array.
subWriter := newWriteBuffer(nil /* bytecount */)
// Put the number of dimensions. We currently support 1d arrays only.
var ndims int32 = 1
if v.Len() == 0 {
ndims = 0
}
subWriter.putInt32(ndims)
hasNulls := 0
if v.HasNulls {
hasNulls = 1
}
oid := v.ParamTyp.Oid()
subWriter.putInt32(int32(hasNulls))
subWriter.putInt32(int32(oid))
if v.Len() > 0 {
subWriter.putInt32(int32(v.Len()))
// Lower bound, we only support a lower bound of 1.
subWriter.putInt32(1)
for _, elem := range v.Array {
subWriter.writeBinaryDatum(ctx, elem, sessionLoc, oid)
}
}
b.writeLengthPrefixedBuffer(&subWriter.wrapped)
case *tree.DJSON:
s := v.JSON.String()
b.putInt32(int32(len(s) + 1))
// Postgres version number, as of writing, `1` is the only valid value.
b.writeByte(1)
b.writeString(s)
case *tree.DOid:
b.putInt32(4)
b.putInt32(int32(v.DInt))
default:
b.setError(errors.AssertionFailedf("unsupported type %T", d))
}
}
const (
pgTimeFormat = "15:04:05.999999"
pgTimeTZFormat = pgTimeFormat + "-07:00"
pgDateFormat = "2006-01-02"
pgTimeStampFormatNoOffset = pgDateFormat + " " + pgTimeFormat
pgTimeStampFormat = pgTimeStampFormatNoOffset + "-07:00"
)
// formatTime formats t into a format lib/pq understands, appending to the
// provided tmp buffer and reallocating if needed. The function will then return
// the resulting buffer.
func formatTime(t timeofday.TimeOfDay, tmp []byte) []byte {
return t.ToTime().AppendFormat(tmp, pgTimeFormat)
}
// formatTimeTZ formats t into a format lib/pq understands, appending to the
// provided tmp buffer and reallocating if needed. The function will then return
// the resulting buffer.
// Note it does not understand the "second" component of the offset as lib/pq
// cannot parse it.
func formatTimeTZ(t timetz.TimeTZ, tmp []byte) []byte {
return t.ToTime().AppendFormat(tmp, pgTimeTZFormat)
}
func formatTs(t time.Time, offset *time.Location, tmp []byte) (b []byte) {
var format string
if offset != nil {
format = pgTimeStampFormat
} else {
format = pgTimeStampFormatNoOffset
}
return formatTsWithFormat(format, t, offset, tmp)
}
// formatTsWithFormat formats t with an optional offset into a format
// lib/pq understands, appending to the provided tmp buffer and
// reallocating if needed. The function will then return the resulting
// buffer. formatTsWithFormat is mostly cribbed from github.com/lib/pq.
func formatTsWithFormat(format string, t time.Time, offset *time.Location, tmp []byte) (b []byte) {
// Need to send dates before 0001 A.D. with " BC" suffix, instead of the
// minus sign preferred by Go.
// Beware, "0000" in ISO is "1 BC", "-0001" is "2 BC" and so on
if offset != nil {
t = t.In(offset)
}
bc := false
if t.Year() <= 0 {
// flip year sign, and add 1, e.g: "0" will be "1", and "-10" will be "11"
t = t.AddDate((-t.Year())*2+1, 0, 0)
bc = true
}
b = t.AppendFormat(tmp, format)
if bc {
b = append(b, " BC"...)
}
return b
}
// timeToPgBinary calculates the Postgres binary format for a timestamp. The timestamp
// is represented as the number of microseconds between the given time and Jan 1, 2000
// (dubbed the PGEpochJDate), stored within an int64.
func timeToPgBinary(t time.Time, offset *time.Location) int64 {
if offset != nil {
t = t.In(offset)
} else {
t = t.UTC()
}
return duration.DiffMicros(t, pgwirebase.PGEpochJDate)
}