forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
/
constant.go
589 lines (545 loc) · 17.9 KB
/
constant.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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
// Copyright 2016 The Cockroach Authors.
//
// 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 parser
import (
"bytes"
"fmt"
"go/constant"
"go/token"
"strings"
"time"
"unicode/utf8"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
"github.com/pkg/errors"
)
// Constant is an constant literal expression which may be resolved to more than one type.
type Constant interface {
Expr
// AvailableTypes returns the ordered set of types that the Constant is able to
// be resolved into. The order of the type slice provides a notion of precedence,
// with the first element in the ordering being the Constant's "natural type".
AvailableTypes() []Type
// DesirableTypes returns the ordered set of types that the constant would
// prefer to be resolved into. As in AvailableTypes, the order of the returned
// type slice provides a notion of precedence, with the first element in the
// ordering being the Constant's "natural type." The function is meant to be
// differentiated from AvailableTypes in that it will exclude certain types
// that are possible, but not desirable.
//
// An example of this is a floating point numeric constant without a value
// past the decimal point. It is possible to resolve this constant as a
// decimal, but it is not desirable.
DesirableTypes() []Type
// ResolveAsType resolves the Constant as the Datum type specified, or returns an
// error if the Constant could not be resolved as that type. The method should only
// be passed a type returned from AvailableTypes and should never be called more than
// once for a given Constant.
ResolveAsType(*SemaContext, Type) (Datum, error)
}
var _ Constant = &NumVal{}
var _ Constant = &StrVal{}
func isConstant(expr Expr) bool {
_, ok := expr.(Constant)
return ok
}
func typeCheckConstant(c Constant, ctx *SemaContext, desired Type) (TypedExpr, error) {
avail := c.AvailableTypes()
if desired != TypeAny {
for _, typ := range avail {
if desired.Equivalent(typ) {
return c.ResolveAsType(ctx, desired)
}
}
}
// If a numeric constant will be promoted to a DECIMAL because it was out
// of range of an INT, but an INT is desired, throw an error here so that
// the error message specifically mentions the overflow.
if desired.FamilyEqual(TypeInt) {
if n, ok := c.(*NumVal); ok {
_, err := n.AsInt64()
switch err {
case errConstOutOfRange:
return nil, err
case errConstNotInt:
default:
panic(fmt.Sprintf("unexpected error %v", err))
}
}
}
natural := avail[0]
return c.ResolveAsType(ctx, natural)
}
func naturalConstantType(c Constant) Type {
return c.AvailableTypes()[0]
}
// canConstantBecome returns whether the provided Constant can become resolved
// as the provided type.
func canConstantBecome(c Constant, typ Type) bool {
avail := c.AvailableTypes()
for _, availTyp := range avail {
if availTyp.Equivalent(typ) {
return true
}
}
return false
}
// NumVal represents a constant numeric value.
type NumVal struct {
constant.Value
// We preserve the "original" string representation (before folding).
OrigString string
// The following fields are used to avoid allocating Datums on type resolution.
resInt DInt
resFloat DFloat
resDecimal DDecimal
}
// Format implements the NodeFormatter interface.
func (expr *NumVal) Format(buf *bytes.Buffer, f FmtFlags) {
s := expr.OrigString
if s == "" {
s = expr.Value.String()
}
buf.WriteString(s)
}
// canBeInt64 checks if it's possible for the value to become an int64:
// 1 = yes
// 1.0 = yes
// 1.1 = no
// 123...overflow...456 = no
func (expr *NumVal) canBeInt64() bool {
_, err := expr.AsInt64()
return err == nil
}
// ShouldBeInt64 checks if the value naturally is an int64:
// 1 = yes
// 1.0 = no
// 1.1 = no
// 123...overflow...456 = no
func (expr *NumVal) ShouldBeInt64() bool {
return expr.Kind() == constant.Int && expr.canBeInt64()
}
// These errors are statically allocated, because they are returned in the
// common path of AsInt64.
var errConstNotInt = errors.New("cannot represent numeric constant as an int")
var errConstOutOfRange = errors.New("numeric constant out of int64 range")
// AsInt64 returns the value as a 64-bit integer if possible, or returns an
// error if not possible. The method will set expr.resInt to the value of
// this int64 if it is successful, avoiding the need to call the method again.
func (expr *NumVal) AsInt64() (int64, error) {
intVal, ok := expr.asConstantInt()
if !ok {
return 0, errConstNotInt
}
i, exact := constant.Int64Val(intVal)
if !exact {
return 0, errConstOutOfRange
}
expr.resInt = DInt(i)
return i, nil
}
// asConstantInt returns the value as an constant.Int if possible, along
// with a flag indicating whether the conversion was possible.
func (expr *NumVal) asConstantInt() (constant.Value, bool) {
intVal := constant.ToInt(expr.Value)
if intVal.Kind() == constant.Int {
return intVal, true
}
return nil, false
}
var (
intLikeTypes = []Type{TypeInt, TypeOid}
decimalLikeTypes = []Type{TypeDecimal, TypeFloat}
numValAvailInteger = append(intLikeTypes, decimalLikeTypes...)
numValAvailDecimalNoFraction = append(decimalLikeTypes, intLikeTypes...)
numValAvailDecimalWithFraction = decimalLikeTypes
)
// AvailableTypes implements the Constant interface.
func (expr *NumVal) AvailableTypes() []Type {
switch {
case expr.canBeInt64():
if expr.Kind() == constant.Int {
return numValAvailInteger
}
return numValAvailDecimalNoFraction
default:
return numValAvailDecimalWithFraction
}
}
// DesirableTypes implements the Constant interface.
func (expr *NumVal) DesirableTypes() []Type {
if expr.ShouldBeInt64() {
return numValAvailInteger
}
return numValAvailDecimalWithFraction
}
// ResolveAsType implements the Constant interface.
func (expr *NumVal) ResolveAsType(ctx *SemaContext, typ Type) (Datum, error) {
switch typ {
case TypeInt:
// We may have already set expr.resInt in AsInt64.
if expr.resInt == 0 {
if _, err := expr.AsInt64(); err != nil {
return nil, err
}
}
return &expr.resInt, nil
case TypeFloat:
f, _ := constant.Float64Val(expr.Value)
expr.resFloat = DFloat(f)
return &expr.resFloat, nil
case TypeDecimal:
dd := &expr.resDecimal
s := expr.OrigString
if s == "" {
// TODO(nvanbenschoten): We should propagate width through constant folding so that we
// can control precision on folded values as well.
s = expr.ExactString()
}
if idx := strings.IndexRune(s, '/'); idx != -1 {
// Handle constant.ratVal, which will return a rational string
// like 6/7. If only we could call big.Rat.FloatString() on it...
num, den := s[:idx], s[idx+1:]
if err := dd.SetString(num); err != nil {
return nil, errors.Wrapf(err, "could not evaluate numerator of %v as Datum type DDecimal "+
"from string %q", expr, num)
}
// TODO(nvanbenschoten): Should we try to avoid this allocation?
denDec, err := ParseDDecimal(den)
if err != nil {
return nil, errors.Wrapf(err, "could not evaluate denominator %v as Datum type DDecimal "+
"from string %q", expr, den)
}
if _, err := DecimalCtx.Quo(&dd.Decimal, &dd.Decimal, &denDec.Decimal); err != nil {
return nil, err
}
} else {
if err := dd.SetString(s); err != nil {
return nil, errors.Wrapf(err, "could not evaluate %v as Datum type DDecimal from "+
"string %q", expr, s)
}
}
return dd, nil
case TypeOid,
TypeRegClass,
TypeRegNamespace,
TypeRegProc,
TypeRegProcedure,
TypeRegType:
d, err := expr.ResolveAsType(ctx, TypeInt)
if err != nil {
return nil, err
}
oid := NewDOid(*d.(*DInt))
oid.semanticType = oidTypeToColType(typ)
return oid, nil
default:
return nil, pgerror.NewErrorf(pgerror.CodeInternalError,
"could not resolve %T %v into a %T", expr, expr, typ)
}
}
func intersectTypeSlices(xs, ys []Type) (out []Type) {
for _, x := range xs {
for _, y := range ys {
if x == y {
out = append(out, x)
}
}
}
return out
}
// commonConstantType returns the most constrained type which is mutually
// resolvable between a set of provided constants.
//
// The function takes a slice of Exprs and indexes, but expects all the indexed
// Exprs to wrap a Constant. The reason it does no take a slice of Constants
// instead is to avoid forcing callers to allocate separate slices of Constant.
func commonConstantType(vals []Expr, idxs []int) (Type, bool) {
var candidates []Type
for _, i := range idxs {
availableTypes := vals[i].(Constant).DesirableTypes()
if candidates == nil {
candidates = availableTypes
} else {
candidates = intersectTypeSlices(candidates, availableTypes)
}
}
if len(candidates) > 0 {
return candidates[0], true
}
return nil, false
}
// StrVal represents a constant string value.
type StrVal struct {
// We could embed a constant.Value here (like NumVal) and use the stringVal implementation,
// but that would have extra overhead without much of a benefit. However, it would make
// constant folding (below) a little more straightforward.
s string
bytesEsc bool
// The following fields are used to avoid allocating Datums on type resolution.
resString DString
resBytes DBytes
}
// NewStrVal constructs a StrVal instance.
func NewStrVal(s string) *StrVal {
return &StrVal{s: s}
}
// Format implements the NodeFormatter interface.
func (expr *StrVal) Format(buf *bytes.Buffer, f FmtFlags) {
if expr.bytesEsc {
encodeSQLBytes(buf, expr.s)
} else {
encodeSQLStringWithFlags(buf, expr.s, f)
}
}
var (
strValAvailAllParsable = []Type{
TypeString,
TypeBytes,
TypeBool,
TypeInt,
TypeFloat,
TypeDecimal,
TypeDate,
TypeTimestamp,
TypeTimestampTZ,
TypeInterval,
TypeUUID,
}
strValAvailBytesString = []Type{TypeBytes, TypeString, TypeUUID}
strValAvailBytes = []Type{TypeBytes, TypeUUID}
)
// AvailableTypes implements the Constant interface.
//
// To fully take advantage of literal type inference, this method would
// determine exactly which types are available for a given string. This would
// entail attempting to parse the literal string as a date, a timestamp, an
// interval, etc. and having more fine-grained results than strValAvailAllParsable.
// However, this is not feasible in practice because of the associated parsing
// overhead.
//
// Conservative approaches like checking the string's length have been investigated
// to reduce ambiguity and improve type inference in some cases. When doing so, the
// length of the string literal was compared against all valid date and timestamp
// formats to quickly gain limited insight into whether parsing the string as the
// respective datum types could succeed. The hope was to eliminate impossibilities
// and constrain the returned type sets as much as possible. Unfortunately, two issues
// were found with this approach:
// - date and timestamp formats do not always imply a fixed-length valid input. For
// instance, timestamp formats that take fractional seconds can successfully parse
// inputs of varied length.
// - the set of date and timestamp formats are not disjoint, which means that ambiguity
// can not be eliminated when inferring the type of string literals that use these
// shared formats.
// While these limitations still permitted improved type inference in many cases, they
// resulted in behavior that was ultimately incomplete, resulted in unpredictable levels
// of inference, and occasionally failed to eliminate ambiguity. Further heuristics could
// have been applied to improve the accuracy of the inference, like checking that all
// or some characters were digits, but it would not have circumvented the fundamental
// issues here. Fully parsing the literal into each type would be the only way to
// concretely avoid the issue of unpredictable inference behavior.
func (expr *StrVal) AvailableTypes() []Type {
if !expr.bytesEsc {
return strValAvailAllParsable
}
if utf8.ValidString(expr.s) {
return strValAvailBytesString
}
return strValAvailBytes
}
// DesirableTypes implements the Constant interface.
func (expr *StrVal) DesirableTypes() []Type {
return expr.AvailableTypes()
}
// ResolveAsType implements the Constant interface.
func (expr *StrVal) ResolveAsType(ctx *SemaContext, typ Type) (Datum, error) {
switch typ {
case TypeString:
expr.resString = DString(expr.s)
return &expr.resString, nil
case TypeName:
expr.resString = DString(expr.s)
return NewDNameFromDString(&expr.resString), nil
case TypeBytes:
s, err := ParseDByte(expr.s)
if err == nil {
expr.resBytes = *s
}
return &expr.resBytes, err
case TypeBool:
return ParseDBool(expr.s)
case TypeInt:
return ParseDInt(expr.s)
case TypeFloat:
return ParseDFloat(expr.s)
case TypeDecimal:
return ParseDDecimal(expr.s)
case TypeDate:
return ParseDDate(expr.s, ctx.getLocation())
case TypeTimestamp:
return ParseDTimestamp(expr.s, time.Microsecond)
case TypeTimestampTZ:
return ParseDTimestampTZ(expr.s, ctx.getLocation(), time.Microsecond)
case TypeInterval:
return ParseDInterval(expr.s)
case TypeUUID:
if expr.bytesEsc {
return ParseDUuidFromBytes([]byte(expr.s))
}
return ParseDUuidFromString(expr.s)
default:
return nil, pgerror.NewErrorf(pgerror.CodeInternalError,
"could not resolve %T %v into a %T", expr, expr, typ)
}
}
type constantFolderVisitor struct{}
var _ Visitor = constantFolderVisitor{}
func (constantFolderVisitor) VisitPre(expr Expr) (recurse bool, newExpr Expr) {
return true, expr
}
var unaryOpToToken = map[UnaryOperator]token.Token{
UnaryPlus: token.ADD,
UnaryMinus: token.SUB,
}
var unaryOpToTokenIntOnly = map[UnaryOperator]token.Token{
UnaryComplement: token.XOR,
}
var binaryOpToToken = map[BinaryOperator]token.Token{
Plus: token.ADD,
Minus: token.SUB,
Mult: token.MUL,
Div: token.QUO,
}
var binaryOpToTokenIntOnly = map[BinaryOperator]token.Token{
FloorDiv: token.QUO_ASSIGN,
Mod: token.REM,
Bitand: token.AND,
Bitor: token.OR,
Bitxor: token.XOR,
}
var binaryShiftOpToToken = map[BinaryOperator]token.Token{
LShift: token.SHL,
RShift: token.SHR,
}
var comparisonOpToToken = map[ComparisonOperator]token.Token{
EQ: token.EQL,
NE: token.NEQ,
LT: token.LSS,
LE: token.LEQ,
GT: token.GTR,
GE: token.GEQ,
}
func (constantFolderVisitor) VisitPost(expr Expr) (retExpr Expr) {
defer func() {
// go/constant operations can panic for a number of reasons (like division
// by zero), but it's difficult to preemptively detect when they will. It's
// safest to just recover here without folding the expression and let
// normalization or evaluation deal with error handling.
if r := recover(); r != nil {
retExpr = expr
}
}()
switch t := expr.(type) {
case *ParenExpr:
switch cv := t.Expr.(type) {
case *NumVal, *StrVal:
return cv
}
case *UnaryExpr:
switch cv := t.Expr.(type) {
case *NumVal:
if token, ok := unaryOpToToken[t.Operator]; ok {
return &NumVal{Value: constant.UnaryOp(token, cv.Value, 0)}
}
if token, ok := unaryOpToTokenIntOnly[t.Operator]; ok {
if intVal, ok := cv.asConstantInt(); ok {
return &NumVal{Value: constant.UnaryOp(token, intVal, 0)}
}
}
}
case *BinaryExpr:
switch l := t.Left.(type) {
case *NumVal:
if r, ok := t.Right.(*NumVal); ok {
if token, ok := binaryOpToToken[t.Operator]; ok {
return &NumVal{Value: constant.BinaryOp(l.Value, token, r.Value)}
}
if token, ok := binaryOpToTokenIntOnly[t.Operator]; ok {
if lInt, ok := l.asConstantInt(); ok {
if rInt, ok := r.asConstantInt(); ok {
return &NumVal{Value: constant.BinaryOp(lInt, token, rInt)}
}
}
}
if token, ok := binaryShiftOpToToken[t.Operator]; ok {
if lInt, ok := l.asConstantInt(); ok {
if rInt64, err := r.AsInt64(); err == nil && rInt64 >= 0 {
return &NumVal{Value: constant.Shift(lInt, token, uint(rInt64))}
}
}
}
}
case *StrVal:
if r, ok := t.Right.(*StrVal); ok {
switch t.Operator {
case Concat:
// When folding string-like constants, if either was byte-escaped,
// the result is also considered byte escaped.
return &StrVal{s: l.s + r.s, bytesEsc: l.bytesEsc || r.bytesEsc}
}
}
}
case *ComparisonExpr:
switch l := t.Left.(type) {
case *NumVal:
if r, ok := t.Right.(*NumVal); ok {
if token, ok := comparisonOpToToken[t.Operator]; ok {
return MakeDBool(DBool(constant.Compare(l.Value, token, r.Value)))
}
}
case *StrVal:
// ComparisonExpr folding for String-like constants is not significantly different
// from constant evalutation during normalization (because both should be exact,
// unlike numeric comparisons). Still, folding these comparisons when possible here
// can reduce the amount of work performed during type checking, can reduce necessary
// allocations, and maintains symmetry with numeric constants.
if r, ok := t.Right.(*StrVal); ok {
switch t.Operator {
case EQ:
return MakeDBool(DBool(l.s == r.s))
case NE:
return MakeDBool(DBool(l.s != r.s))
case LT:
return MakeDBool(DBool(l.s < r.s))
case LE:
return MakeDBool(DBool(l.s <= r.s))
case GT:
return MakeDBool(DBool(l.s > r.s))
case GE:
return MakeDBool(DBool(l.s >= r.s))
}
}
}
}
return expr
}
// foldConstantLiterals folds all constant literals using exact arithmetic.
//
// TODO(nvanbenschoten): Can this visitor be preallocated (like normalizeVisitor)?
// TODO(nvanbenschoten): Investigate normalizing associative operations to group
// constants together and permit further numeric constant folding.
func foldConstantLiterals(expr Expr) (Expr, error) {
v := constantFolderVisitor{}
expr, _ = WalkExpr(v, expr)
return expr, nil
}