-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
fold_constants.go
583 lines (531 loc) · 19.7 KB
/
fold_constants.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
// Copyright 2019 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 norm
import (
"github.com/cockroachdb/cockroach/pkg/sql/opt"
"github.com/cockroachdb/cockroach/pkg/sql/opt/cat"
"github.com/cockroachdb/cockroach/pkg/sql/opt/memo"
"github.com/cockroachdb/cockroach/pkg/sql/parser"
"github.com/cockroachdb/cockroach/pkg/sql/privilege"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/types"
)
// FoldNullUnary replaces the unary operator with a typed null value having the
// same type as the unary operator would have.
func (c *CustomFuncs) FoldNullUnary(op opt.Operator, input opt.ScalarExpr) opt.ScalarExpr {
return c.f.ConstructNull(memo.InferUnaryType(op, input.DataType()))
}
// FoldNullBinary replaces the binary operator with a typed null value having
// the same type as the binary operator would have.
func (c *CustomFuncs) FoldNullBinary(op opt.Operator, left, right opt.ScalarExpr) opt.ScalarExpr {
return c.f.ConstructNull(memo.InferBinaryType(op, left.DataType(), right.DataType()))
}
// IsListOfConstants returns true if elems is a list of constant values or
// tuples.
func (c *CustomFuncs) IsListOfConstants(elems memo.ScalarListExpr) bool {
for _, elem := range elems {
if !c.IsConstValueOrTuple(elem) {
return false
}
}
return true
}
// FoldArray evaluates an Array expression with constant inputs. It returns the
// array as a Const datum with type TArray.
func (c *CustomFuncs) FoldArray(elems memo.ScalarListExpr, typ *types.T) opt.ScalarExpr {
elemType := typ.ArrayContents()
a := tree.NewDArray(elemType)
a.Array = make(tree.Datums, len(elems))
for i := range a.Array {
a.Array[i] = memo.ExtractConstDatum(elems[i])
if a.Array[i] == tree.DNull {
a.HasNulls = true
} else {
a.HasNonNulls = true
}
}
return c.f.ConstructConst(a, typ)
}
// IsConstValueOrTuple returns true if the input is a constant or a tuple of
// constants.
func (c *CustomFuncs) IsConstValueOrTuple(input opt.ScalarExpr) bool {
return memo.CanExtractConstDatum(input)
}
// HasOneNullElement returns true if the input tuple has one constant, null
// element. Note that it only returns true if one element is known to be null.
// For example, given the tuple (1, x), it will return false because x is not
// guaranteed to be null.
func (c *CustomFuncs) HasOneNullElement(input opt.ScalarExpr) bool {
tup := input.(*memo.TupleExpr)
for _, e := range tup.Elems {
if e.Op() == opt.NullOp {
return true
}
}
return false
}
// HasAllNullElements returns true if the input tuple has only constant, null
// elements. Note that it only returns true if all elements are known to be
// null. For example, given the tuple (NULL, x), it will return false because x
// is not guaranteed to be null.
func (c *CustomFuncs) HasAllNullElements(input opt.ScalarExpr) bool {
tup := input.(*memo.TupleExpr)
for _, e := range tup.Elems {
if e.Op() != opt.NullOp {
return false
}
}
return true
}
// HasOneNonNullElement returns true if the input tuple has one constant,
// non-null element. Note that it only returns true if one element is known to
// be non-null. For example, given the tuple (NULL, x), it will return false
// because x is not guaranteed to be non-null.
func (c *CustomFuncs) HasOneNonNullElement(input opt.ScalarExpr) bool {
tup := input.(*memo.TupleExpr)
for _, e := range tup.Elems {
if e.Op() != opt.NullOp && (opt.IsConstValueOp(e) || e.Op() == opt.TupleOp || e.Op() == opt.ArrayOp) {
return true
}
}
return false
}
// HasAllNonNullElements returns true if the input tuple has all constant,
// non-null elements. Note that it only returns true if all elements are known
// to be non-null. For example, given the tuple (1, x), it will return false
// because x is not guaranteed to be non-null.
func (c *CustomFuncs) HasAllNonNullElements(input opt.ScalarExpr) bool {
tup := input.(*memo.TupleExpr)
for _, e := range tup.Elems {
if e.Op() == opt.NullOp || !(opt.IsConstValueOp(e) || e.Op() == opt.TupleOp || e.Op() == opt.ArrayOp) {
return false
}
}
return true
}
// FoldBinary evaluates a binary expression with constant inputs. It returns
// a constant expression as long as it finds an appropriate overload function
// for the given operator and input types, and the evaluation causes no error.
func (c *CustomFuncs) FoldBinary(op opt.Operator, left, right opt.ScalarExpr) opt.ScalarExpr {
lDatum, rDatum := memo.ExtractConstDatum(left), memo.ExtractConstDatum(right)
o, ok := memo.FindBinaryOverload(op, left.DataType(), right.DataType())
if !ok {
return nil
}
result, err := o.Fn(c.f.evalCtx, lDatum, rDatum)
if err != nil {
return nil
}
return c.f.ConstructConstVal(result, o.ReturnType)
}
// FoldUnary evaluates a unary expression with a constant input. It returns
// a constant expression as long as it finds an appropriate overload function
// for the given operator and input type, and the evaluation causes no error.
func (c *CustomFuncs) FoldUnary(op opt.Operator, input opt.ScalarExpr) opt.ScalarExpr {
datum := memo.ExtractConstDatum(input)
o, ok := memo.FindUnaryOverload(op, input.DataType())
if !ok {
return nil
}
result, err := o.Fn(c.f.evalCtx, datum)
if err != nil {
return nil
}
return c.f.ConstructConstVal(result, o.ReturnType)
}
// foldStringToRegclassCast resolves a string that is a table name into an OID
// by resolving the table name and returning its table ID. This permits the
// optimizer to do intelligent things like push down filters that look like:
// ... WHERE oid = 'my_table'::REGCLASS
func (c *CustomFuncs) foldStringToRegclassCast(
input opt.ScalarExpr, typ *types.T,
) (opt.ScalarExpr, error) {
// Special case: we're casting a string to a REGCLASS oid, which is a
// table id lookup.
flags := cat.Flags{AvoidDescriptorCaches: false, NoTableStats: true}
datum := memo.ExtractConstDatum(input)
s := tree.MustBeDString(datum)
tn, err := parser.ParseQualifiedTableName(string(s))
if err != nil {
return nil, err
}
ds, resName, err := c.f.catalog.ResolveDataSource(c.f.evalCtx.Context, flags, tn)
if err != nil {
return nil, err
}
c.mem.Metadata().AddDependency(opt.DepByName(&resName), ds, privilege.SELECT)
regclassOid := tree.NewDOidWithName(tree.DInt(ds.PostgresDescriptorID()), types.RegClass, string(tn.ObjectName))
return c.f.ConstructConstVal(regclassOid, typ), nil
}
// FoldCast evaluates a cast expression with a constant input. It returns
// a constant expression as long as the evaluation causes no error.
func (c *CustomFuncs) FoldCast(input opt.ScalarExpr, typ *types.T) opt.ScalarExpr {
if typ.Family() == types.OidFamily {
if typ.Oid() == types.RegClass.Oid() && input.DataType().Family() == types.StringFamily {
expr, err := c.foldStringToRegclassCast(input, typ)
if err == nil {
return expr
}
}
// Save this cast for the execbuilder.
return nil
}
datum := memo.ExtractConstDatum(input)
texpr := tree.NewTypedCastExpr(datum, typ)
result, err := texpr.Eval(c.f.evalCtx)
if err != nil {
return nil
}
return c.f.ConstructConstVal(result, typ)
}
// isMonotonicConversion returns true if conversion of a value from FROM to
// TO is monotonic.
// That is, if a and b are values of type FROM, then
//
// 1. a = b implies a::TO = b::TO and
// 2. a < b implies a::TO <= b::TO
//
// Property (1) can be violated by cases like:
//
// '-0'::FLOAT = '0'::FLOAT, but '-0'::FLOAT::STRING != '0'::FLOAT::STRING
//
// Property (2) can be violated by cases like:
//
// 2 < 10, but 2::STRING > 10::STRING.
//
// Note that the stronger version of (2),
//
// a < b implies a::TO < b::TO
//
// is not required, for instance this is not generally true of conversion from
// a TIMESTAMP to a DATE, but certain such conversions can still generate spans
// in some cases where values under FROM and TO are "the same" (such as where a
// TIMESTAMP precisely falls on a date boundary). We don't need this property
// because we will subsequently check that the values can round-trip to ensure
// that we don't lose any information by doing the conversion.
// TODO(justin): fill this out with the complete set of such conversions.
func isMonotonicConversion(from, to *types.T) bool {
switch from.Family() {
case types.TimestampFamily, types.TimestampTZFamily, types.DateFamily:
switch to.Family() {
case types.TimestampFamily, types.TimestampTZFamily, types.DateFamily:
return true
}
return false
case types.IntFamily, types.FloatFamily, types.DecimalFamily:
switch to.Family() {
case types.IntFamily, types.FloatFamily, types.DecimalFamily:
return true
}
return false
}
return false
}
// UnifyComparison attempts to convert a constant expression to the type of the
// variable expression, if that conversion can round-trip and is monotonic.
func (c *CustomFuncs) UnifyComparison(left, right opt.ScalarExpr) opt.ScalarExpr {
v := left.(*memo.VariableExpr)
cnst := right.(*memo.ConstExpr)
desiredType := v.DataType()
originalType := cnst.DataType()
// Don't bother if they're already the same.
if desiredType.Equivalent(originalType) {
return nil
}
if !isMonotonicConversion(originalType, desiredType) {
return nil
}
// Check that the datum can round-trip between the types. If this is true, it
// means we don't lose any information needed to generate spans, and combined
// with monotonicity means that it's safe to convert the RHS to the type of
// the LHS.
convertedDatum, err := tree.PerformCast(c.f.evalCtx, cnst.Value, desiredType)
if err != nil {
return nil
}
convertedBack, err := tree.PerformCast(c.f.evalCtx, convertedDatum, originalType)
if err != nil {
return nil
}
if convertedBack.Compare(c.f.evalCtx, cnst.Value) != 0 {
return nil
}
return c.f.ConstructConst(convertedDatum, desiredType)
}
// FoldComparison evaluates a comparison expression with constant inputs. It
// returns a constant expression as long as it finds an appropriate overload
// function for the given operator and input types, and the evaluation causes
// no error.
func (c *CustomFuncs) FoldComparison(op opt.Operator, left, right opt.ScalarExpr) opt.ScalarExpr {
lDatum, rDatum := memo.ExtractConstDatum(left), memo.ExtractConstDatum(right)
var flipped, not bool
o, flipped, not, ok := memo.FindComparisonOverload(op, left.DataType(), right.DataType())
if !ok {
return nil
}
if flipped {
lDatum, rDatum = rDatum, lDatum
}
result, err := o.Fn(c.f.evalCtx, lDatum, rDatum)
if err != nil {
return nil
}
if b, ok := result.(*tree.DBool); ok && not {
result = tree.MakeDBool(!*b)
}
return c.f.ConstructConstVal(result, types.Bool)
}
// FoldIndirection evaluates an array indirection operator with constant inputs.
// It returns the referenced array element as a constant value, or nil if the
// evaluation results in an error.
func (c *CustomFuncs) FoldIndirection(input, index opt.ScalarExpr) opt.ScalarExpr {
// Index is 1-based, so convert to 0-based.
indexD := memo.ExtractConstDatum(index)
indexI := int(*indexD.(*tree.DInt)) - 1
// Case 1: The input is a static array constructor.
if arr, ok := input.(*memo.ArrayExpr); ok {
if indexI >= 0 && indexI < len(arr.Elems) {
return arr.Elems[indexI]
}
return c.f.ConstructNull(arr.Typ.ArrayContents())
}
// Case 2: The input is a constant DArray.
if memo.CanExtractConstDatum(input) {
inputD := memo.ExtractConstDatum(input)
texpr := tree.NewTypedIndirectionExpr(inputD, indexD, input.DataType().ArrayContents())
result, err := texpr.Eval(c.f.evalCtx)
if err == nil {
return c.f.ConstructConstVal(result, texpr.ResolvedType())
}
}
return nil
}
// FoldColumnAccess tries to evaluate a tuple column access operator with a
// constant tuple input (though tuple field values do not need to be constant).
// It returns the referenced tuple field value, or nil if folding is not
// possible or results in an error.
func (c *CustomFuncs) FoldColumnAccess(input opt.ScalarExpr, idx memo.TupleOrdinal) opt.ScalarExpr {
// Case 1: The input is a static tuple constructor.
if tup, ok := input.(*memo.TupleExpr); ok {
return tup.Elems[idx]
}
// Case 2: The input is a constant DTuple.
if memo.CanExtractConstDatum(input) {
datum := memo.ExtractConstDatum(input)
texpr := tree.NewTypedColumnAccessExpr(datum, "" /* by-index access */, int(idx))
result, err := texpr.Eval(c.f.evalCtx)
if err == nil {
return c.f.ConstructConstVal(result, texpr.ResolvedType())
}
}
return nil
}
// FoldFunction evaluates a function expression with constant inputs. It
// returns a constant expression as long as the function is contained in the
// FoldFunctionWhitelist, and the evaluation causes no error.
func (c *CustomFuncs) FoldFunction(
args memo.ScalarListExpr, private *memo.FunctionPrivate,
) opt.ScalarExpr {
if _, ok := FoldFunctionWhitelist[private.Name]; !ok {
return nil
}
exprs := make(tree.TypedExprs, len(args))
for i := range exprs {
exprs[i] = memo.ExtractConstDatum(args[i])
}
funcRef := tree.WrapFunction(private.Name)
fn := tree.NewTypedFuncExpr(
funcRef,
0, /* aggQualifier */
exprs,
nil, /* filter */
nil, /* windowDef */
private.Typ,
private.Properties,
private.Overload,
)
result, err := fn.Eval(c.f.evalCtx)
if err != nil {
return nil
}
return c.f.ConstructConstVal(result, private.Typ)
}
// FoldFunctionWhitelist contains functions that are known to always produce
// the same result given the same set of arguments. This excludes impure
// functions and functions that rely on context such as locale or current user.
// See sql/sem/builtins/builtins.go for the function definitions.
// TODO(rytaft): This is a stopgap until #26582 is completed to identify
// functions as immutable, stable or volatile.
var FoldFunctionWhitelist = map[string]struct{}{
"length": {},
"char_length": {},
"character_length": {},
"bit_length": {},
"octet_length": {},
"lower": {},
"upper": {},
"substr": {},
"substring": {},
"concat": {},
"concat_ws": {},
"convert_from": {},
"convert_to": {},
"to_uuid": {},
"from_uuid": {},
"abbrev": {},
"broadcast": {},
"family": {},
"host": {},
"hostmask": {},
"masklen": {},
"netmask": {},
"set_masklen": {},
"text": {},
"inet_same_family": {},
"inet_contained_by_or_equals": {},
"inet_contains_or_equals": {},
"from_ip": {},
"to_ip": {},
"split_part": {},
"repeat": {},
"encode": {},
"decode": {},
"ascii": {},
"chr": {},
"md5": {},
"sha1": {},
"sha256": {},
"sha512": {},
"fnv32": {},
"fnv32a": {},
"fnv64": {},
"fnv64a": {},
"crc32ieee": {},
"crc32c": {},
"to_hex": {},
"to_english": {},
"strpos": {},
"overlay": {},
"lpad": {},
"rpad": {},
"btrim": {},
"ltrim": {},
"rtrim": {},
"reverse": {},
"replace": {},
"translate": {},
"regexp_extract": {},
"regexp_replace": {},
"like_escape": {},
"not_like_escape": {},
"ilike_escape": {},
"not_ilike_escape": {},
"similar_to_escape": {},
"not_similar_to_escape": {},
"initcap": {},
"quote_ident": {},
"left": {},
"right": {},
"greatest": {},
"least": {},
"abs": {},
"acos": {},
"asin": {},
"atan": {},
"atan2": {},
"cbrt": {},
"ceil": {},
"ceiling": {},
"cos": {},
"cot": {},
"degrees": {},
"div": {},
"exp": {},
"floor": {},
"isnan": {},
"ln": {},
"log": {},
"mod": {},
"pi": {},
"pow": {},
"power": {},
"radians": {},
"round": {},
"sin": {},
"sign": {},
"sqrt": {},
"tan": {},
"trunc": {},
"string_to_array": {},
"array_to_string": {},
"array_length": {},
"array_lower": {},
"array_upper": {},
"array_append": {},
"array_prepend": {},
"array_cat": {},
"json_remove_path": {},
"json_extract_path": {},
"jsonb_extract_path": {},
"json_set": {},
"jsonb_set": {},
"jsonb_insert": {},
"jsonb_pretty": {},
"json_typeof": {},
"jsonb_typeof": {},
"array_to_json": {},
"to_json": {},
"to_jsonb": {},
"json_build_array": {},
"jsonb_build_array": {},
"json_build_object": {},
"jsonb_build_object": {},
"json_object": {},
"jsonb_object": {},
"json_strip_nulls": {},
"jsonb_strip_nulls": {},
"json_array_length": {},
"jsonb_array_length": {},
"crdb_internal.locality_value": {},
"st_geomfromtext": {},
"st_geometryfromtext": {},
"st_geomfromewkt": {},
"st_geomfromwkb": {},
"st_geomfromewkb": {},
"st_geomfromgeojson": {},
"st_geogfromtext": {},
"st_geographyfromtext": {},
"st_geogfromewkt": {},
"st_geogfromwkb": {},
"st_geogfromewkb": {},
"st_geogfromgeojson": {},
"st_astext": {},
"st_asewkt": {},
"st_asbinary": {},
"st_asewkb": {},
"st_ashexwkb": {},
"st_ashexewkb": {},
"st_askml": {},
"st_asgeojson": {},
"st_area": {},
"st_length": {},
"st_perimeter": {},
"st_distance": {},
"st_covers": {},
"st_coveredby": {},
"st_contains": {},
"st_containsproperly": {},
"st_crosses": {},
"st_equals": {},
"st_intersects": {},
"st_overlaps": {},
"st_touches": {},
"st_within": {},
}