-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
decode.go
307 lines (299 loc) · 9.8 KB
/
decode.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
// Copyright 2021 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 keyside
import (
"time"
"github.com/cockroachdb/apd/v3"
"github.com/cockroachdb/cockroach/pkg/geo"
"github.com/cockroachdb/cockroach/pkg/geo/geopb"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/types"
"github.com/cockroachdb/cockroach/pkg/util/bitarray"
"github.com/cockroachdb/cockroach/pkg/util/duration"
"github.com/cockroachdb/cockroach/pkg/util/encoding"
"github.com/cockroachdb/cockroach/pkg/util/ipaddr"
"github.com/cockroachdb/cockroach/pkg/util/json"
"github.com/cockroachdb/cockroach/pkg/util/timetz"
"github.com/cockroachdb/cockroach/pkg/util/timeutil/pgdate"
"github.com/cockroachdb/cockroach/pkg/util/uuid"
"github.com/cockroachdb/errors"
"github.com/lib/pq/oid"
)
// Decode decodes a value encoded by Encode from a key.
func Decode(
a *tree.DatumAlloc, valType *types.T, key []byte, dir encoding.Direction,
) (_ tree.Datum, remainingKey []byte, _ error) {
if (dir != encoding.Ascending) && (dir != encoding.Descending) {
return nil, nil, errors.Errorf("invalid direction: %d", dir)
}
var isNull bool
if key, isNull = encoding.DecodeIfNull(key); isNull {
return tree.DNull, key, nil
}
var rkey []byte
var err error
switch valType.Family() {
case types.ArrayFamily:
return decodeArrayKey(a, valType, key, dir)
case types.BitFamily:
var r bitarray.BitArray
if dir == encoding.Ascending {
rkey, r, err = encoding.DecodeBitArrayAscending(key)
} else {
rkey, r, err = encoding.DecodeBitArrayDescending(key)
}
return a.NewDBitArray(tree.DBitArray{BitArray: r}), rkey, err
case types.BoolFamily:
var i int64
if dir == encoding.Ascending {
rkey, i, err = encoding.DecodeVarintAscending(key)
} else {
rkey, i, err = encoding.DecodeVarintDescending(key)
}
// No need to chunk allocate DBool as MakeDBool returns either
// tree.DBoolTrue or tree.DBoolFalse.
return tree.MakeDBool(tree.DBool(i != 0)), rkey, err
case types.IntFamily:
var i int64
if dir == encoding.Ascending {
rkey, i, err = encoding.DecodeVarintAscending(key)
} else {
rkey, i, err = encoding.DecodeVarintDescending(key)
}
return a.NewDInt(tree.DInt(i)), rkey, err
case types.FloatFamily:
var f float64
if dir == encoding.Ascending {
rkey, f, err = encoding.DecodeFloatAscending(key)
} else {
rkey, f, err = encoding.DecodeFloatDescending(key)
}
return a.NewDFloat(tree.DFloat(f)), rkey, err
case types.DecimalFamily:
var d apd.Decimal
if dir == encoding.Ascending {
rkey, d, err = encoding.DecodeDecimalAscending(key, nil)
} else {
rkey, d, err = encoding.DecodeDecimalDescending(key, nil)
}
dd := a.NewDDecimal(tree.DDecimal{Decimal: d})
return dd, rkey, err
case types.StringFamily:
var r string
if dir == encoding.Ascending {
// Perform a deep copy so that r would never reference the key's
// memory which might keep the BatchResponse alive.
rkey, r, err = encoding.DecodeUnsafeStringAscendingDeepCopy(key, nil)
} else {
rkey, r, err = encoding.DecodeUnsafeStringDescending(key, nil)
}
if valType.Oid() == oid.T_name {
return a.NewDName(tree.DString(r)), rkey, err
}
return a.NewDString(tree.DString(r)), rkey, err
case types.CollatedStringFamily:
var r string
if dir == encoding.Ascending {
// Perform a deep copy so that r would never reference the key's
// memory which might keep the BatchResponse alive.
rkey, r, err = encoding.DecodeUnsafeStringAscendingDeepCopy(key, nil)
} else {
rkey, r, err = encoding.DecodeUnsafeStringDescending(key, nil)
}
if err != nil {
return nil, nil, err
}
d, err := a.NewDCollatedString(r, valType.Locale())
return d, rkey, err
case types.JsonFamily:
var json json.JSON
var buf []byte
var dJson tree.Datum
json, buf, err = decodeJSONKey(key, dir)
if err != nil {
return nil, nil, err
}
dJson, err = tree.MakeDJSON(json)
if err != nil {
return nil, nil, err
}
d := a.NewDJSON(*dJson.(*tree.DJSON))
return d, buf, err
case types.BytesFamily:
var r []byte
if dir == encoding.Ascending {
// No need to perform the deep copy since converting to string below
// will do that for us.
rkey, r, err = encoding.DecodeBytesAscending(key, nil)
} else {
rkey, r, err = encoding.DecodeBytesDescending(key, nil)
}
return a.NewDBytes(tree.DBytes(r)), rkey, err
case types.VoidFamily:
rkey, err = encoding.DecodeVoidAscendingOrDescending(key)
return a.NewDVoid(), rkey, err
case types.Box2DFamily:
var r geopb.BoundingBox
if dir == encoding.Ascending {
rkey, r, err = encoding.DecodeBox2DAscending(key)
} else {
rkey, r, err = encoding.DecodeBox2DDescending(key)
}
return a.NewDBox2D(tree.DBox2D{
CartesianBoundingBox: geo.CartesianBoundingBox{BoundingBox: r},
}), rkey, err
case types.GeographyFamily:
g := a.NewDGeographyEmpty()
so := g.Geography.SpatialObjectRef()
if dir == encoding.Ascending {
rkey, err = encoding.DecodeGeoAscending(key, so)
} else {
rkey, err = encoding.DecodeGeoDescending(key, so)
}
a.DoneInitNewDGeo(so)
return g, rkey, err
case types.GeometryFamily:
g := a.NewDGeometryEmpty()
so := g.Geometry.SpatialObjectRef()
if dir == encoding.Ascending {
rkey, err = encoding.DecodeGeoAscending(key, so)
} else {
rkey, err = encoding.DecodeGeoDescending(key, so)
}
a.DoneInitNewDGeo(so)
return g, rkey, err
case types.DateFamily:
var t int64
if dir == encoding.Ascending {
rkey, t, err = encoding.DecodeVarintAscending(key)
} else {
rkey, t, err = encoding.DecodeVarintDescending(key)
}
return a.NewDDate(tree.MakeDDate(pgdate.MakeCompatibleDateFromDisk(t))), rkey, err
case types.TimeFamily:
var t int64
if dir == encoding.Ascending {
rkey, t, err = encoding.DecodeVarintAscending(key)
} else {
rkey, t, err = encoding.DecodeVarintDescending(key)
}
return a.NewDTime(tree.DTime(t)), rkey, err
case types.TimeTZFamily:
var t timetz.TimeTZ
if dir == encoding.Ascending {
rkey, t, err = encoding.DecodeTimeTZAscending(key)
} else {
rkey, t, err = encoding.DecodeTimeTZDescending(key)
}
return a.NewDTimeTZ(tree.DTimeTZ{TimeTZ: t}), rkey, err
case types.TimestampFamily:
var t time.Time
if dir == encoding.Ascending {
rkey, t, err = encoding.DecodeTimeAscending(key)
} else {
rkey, t, err = encoding.DecodeTimeDescending(key)
}
return a.NewDTimestamp(tree.DTimestamp{Time: t}), rkey, err
case types.TimestampTZFamily:
var t time.Time
if dir == encoding.Ascending {
rkey, t, err = encoding.DecodeTimeAscending(key)
} else {
rkey, t, err = encoding.DecodeTimeDescending(key)
}
return a.NewDTimestampTZ(tree.DTimestampTZ{Time: t}), rkey, err
case types.IntervalFamily:
var d duration.Duration
if dir == encoding.Ascending {
rkey, d, err = encoding.DecodeDurationAscending(key)
} else {
rkey, d, err = encoding.DecodeDurationDescending(key)
}
return a.NewDInterval(tree.DInterval{Duration: d}), rkey, err
case types.UuidFamily:
var r []byte
if dir == encoding.Ascending {
// No need to perform the deep copy since converting to UUID below
// will do that for us.
rkey, r, err = encoding.DecodeBytesAscending(key, nil)
} else {
rkey, r, err = encoding.DecodeBytesDescending(key, nil)
}
if err != nil {
return nil, nil, err
}
u, err := uuid.FromBytes(r)
return a.NewDUuid(tree.DUuid{UUID: u}), rkey, err
case types.INetFamily:
var r []byte
if dir == encoding.Ascending {
// No need to perform the deep copy since converting to IPAddr below
// will do that for us.
rkey, r, err = encoding.DecodeBytesAscending(key, nil)
} else {
rkey, r, err = encoding.DecodeBytesDescending(key, nil)
}
if err != nil {
return nil, nil, err
}
var ipAddr ipaddr.IPAddr
_, err := ipAddr.FromBuffer(r)
return a.NewDIPAddr(tree.DIPAddr{IPAddr: ipAddr}), rkey, err
case types.OidFamily:
// TODO: This possibly should use DecodeUint32 (with corresponding changes
// to encoding) to ensure that the value fits in a DOid without any loss of
// precision. In practice, this may not matter, since everything at
// execution time uses a uint32 for OIDs. The extra safety may not be worth
// the loss of variable length encoding.
var i int64
if dir == encoding.Ascending {
rkey, i, err = encoding.DecodeVarintAscending(key)
} else {
rkey, i, err = encoding.DecodeVarintDescending(key)
}
return a.NewDOid(tree.MakeDOid(oid.Oid(i), valType)), rkey, err
case types.EnumFamily:
var r []byte
if dir == encoding.Ascending {
// No need to perform the deep copy since we only need r for a brief
// period of time.
rkey, r, err = encoding.DecodeBytesAscending(key, nil)
} else {
rkey, r, err = encoding.DecodeBytesDescending(key, nil)
}
if err != nil {
return nil, nil, err
}
phys, log, err := tree.GetEnumComponentsFromPhysicalRep(valType, r)
if err != nil {
return nil, nil, err
}
return a.NewDEnum(tree.DEnum{EnumTyp: valType, PhysicalRep: phys, LogicalRep: log}), rkey, nil
case types.EncodedKeyFamily:
// We don't actually decode anything; we wrap the raw key bytes into a
// DEncodedKey.
len, err := encoding.PeekLength(key)
if err != nil {
return nil, nil, err
}
rkey := key[len:]
return a.NewDEncodedKey(tree.DEncodedKey(key[:len])), rkey, nil
default:
return nil, nil, errors.Errorf("unable to decode table key: %s", valType)
}
}
// Skip skips one value of in a key, returning the remainder of the key.
func Skip(key []byte) (remainingKey []byte, _ error) {
skipLen, err := encoding.PeekLength(key)
if err != nil {
return nil, err
}
return key[skipLen:], nil
}