-
Notifications
You must be signed in to change notification settings - Fork 0
/
expr.go
1260 lines (1083 loc) · 35 KB
/
expr.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
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2015 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"
)
// Expr represents an expression.
type Expr interface {
fmt.Stringer
NodeFormatter
// Walk recursively walks all children using WalkExpr. If any children are changed, it returns a
// copy of this node updated to point to the new children. Otherwise the receiver is returned.
// For childless (leaf) Exprs, its implementation is empty.
Walk(Visitor) Expr
// TypeCheck transforms the Expr into a well-typed TypedExpr, which further permits
// evaluation and type introspection, or an error if the expression cannot be well-typed.
// When type checking is complete, if no error was reported, the expression and all
// sub-expressions will be guaranteed to be well-typed, meaning that the method effectively
// maps the Expr tree into a TypedExpr tree.
//
// The ctx parameter defines the context in which to perform type checking.
// The desired parameter hints the desired type that the method's caller wants from
// the resulting TypedExpr. It is not valid to call TypeCheck with a nil desired
// type. Instead, call it with wildcard type TypeAny if no specific type is
// desired. This restriction is also true of most methods and functions related
// to type checking.
TypeCheck(ctx *SemaContext, desired Type) (TypedExpr, error)
}
// TypedExpr represents a well-typed expression.
type TypedExpr interface {
Expr
// Eval evaluates an SQL expression. Expression evaluation is a
// mostly straightforward walk over the parse tree. The only
// significant complexity is the handling of types and implicit
// conversions. See binOps and cmpOps for more details. Note that
// expression evaluation returns an error if certain node types are
// encountered: Placeholder, VarName (and related UnqualifiedStar,
// UnresolvedName and AllColumnsSelector) or Subquery. These nodes
// should be replaced prior to expression evaluation by an
// appropriate WalkExpr. For example, Placeholder should be replace
// by the argument passed from the client.
Eval(*EvalContext) (Datum, error)
// ResolvedType provides the type of the TypedExpr, which is the type of Datum
// that the TypedExpr will return when evaluated.
ResolvedType() Type
}
// VariableExpr is an Expr that may change per row. It is used to
// signal the evaluation/simplification machinery that the underlying
// Expr is not constant.
type VariableExpr interface {
Expr
Variable()
}
// operatorExpr is used to identify expression types that involve operators;
// used by exprStrWithParen.
type operatorExpr interface {
Expr
operatorExpr()
}
var _ operatorExpr = &AndExpr{}
var _ operatorExpr = &OrExpr{}
var _ operatorExpr = &NotExpr{}
var _ operatorExpr = &BinaryExpr{}
var _ operatorExpr = &UnaryExpr{}
var _ operatorExpr = &ComparisonExpr{}
var _ operatorExpr = &RangeCond{}
var _ operatorExpr = &IsOfTypeExpr{}
// operator is used to identify operators; used in sql.y.
type operator interface {
operator()
}
var _ operator = UnaryOperator(0)
var _ operator = BinaryOperator(0)
var _ operator = ComparisonOperator(0)
// exprFmtWithParen is a variant of Format() which adds a set of outer parens
// if the expression involves an operator. It is used internally when the
// expression is part of another expression and we know it is preceded or
// followed by an operator.
func exprFmtWithParen(buf *bytes.Buffer, f FmtFlags, e Expr) {
if _, ok := e.(operatorExpr); ok {
buf.WriteByte('(')
FormatNode(buf, f, e)
buf.WriteByte(')')
} else {
FormatNode(buf, f, e)
}
}
// typeAnnotation is an embeddable struct to provide a TypedExpr with a dynamic
// type annotation.
type typeAnnotation struct {
typ Type
}
func (ta typeAnnotation) ResolvedType() Type {
ta.assertTyped()
return ta.typ
}
func (ta typeAnnotation) assertTyped() {
if ta.typ == nil {
panic("ReturnType called on TypedExpr with empty typeAnnotation. " +
"Was the underlying Expr type-checked before asserting a type of TypedExpr?")
}
}
// AndExpr represents an AND expression.
type AndExpr struct {
Left, Right Expr
typeAnnotation
}
func (*AndExpr) operatorExpr() {}
func binExprFmtWithParen(buf *bytes.Buffer, f FmtFlags, e1 Expr, op string, e2 Expr) {
binExprFmtWithParenAndSubOp(buf, f, e1, "", op, e2)
}
func binExprFmtWithParenAndSubOp(
buf *bytes.Buffer, f FmtFlags, e1 Expr, subOp, op string, e2 Expr,
) {
exprFmtWithParen(buf, f, e1)
buf.WriteByte(' ')
if subOp != "" {
buf.WriteString(subOp)
buf.WriteByte(' ')
}
buf.WriteString(op)
buf.WriteByte(' ')
exprFmtWithParen(buf, f, e2)
}
// Format implements the NodeFormatter interface.
func (node *AndExpr) Format(buf *bytes.Buffer, f FmtFlags) {
binExprFmtWithParen(buf, f, node.Left, "AND", node.Right)
}
// NewTypedAndExpr returns a new AndExpr that is verified to be well-typed.
func NewTypedAndExpr(left, right TypedExpr) *AndExpr {
node := &AndExpr{Left: left, Right: right}
node.typ = TypeBool
return node
}
// TypedLeft returns the AndExpr's left expression as a TypedExpr.
func (node *AndExpr) TypedLeft() TypedExpr {
return node.Left.(TypedExpr)
}
// TypedRight returns the AndExpr's right expression as a TypedExpr.
func (node *AndExpr) TypedRight() TypedExpr {
return node.Right.(TypedExpr)
}
// OrExpr represents an OR expression.
type OrExpr struct {
Left, Right Expr
typeAnnotation
}
func (*OrExpr) operatorExpr() {}
// Format implements the NodeFormatter interface.
func (node *OrExpr) Format(buf *bytes.Buffer, f FmtFlags) {
binExprFmtWithParen(buf, f, node.Left, "OR", node.Right)
}
// NewTypedOrExpr returns a new OrExpr that is verified to be well-typed.
func NewTypedOrExpr(left, right TypedExpr) *OrExpr {
node := &OrExpr{Left: left, Right: right}
node.typ = TypeBool
return node
}
// TypedLeft returns the OrExpr's left expression as a TypedExpr.
func (node *OrExpr) TypedLeft() TypedExpr {
return node.Left.(TypedExpr)
}
// TypedRight returns the OrExpr's right expression as a TypedExpr.
func (node *OrExpr) TypedRight() TypedExpr {
return node.Right.(TypedExpr)
}
// NotExpr represents a NOT expression.
type NotExpr struct {
Expr Expr
typeAnnotation
}
func (*NotExpr) operatorExpr() {}
// Format implements the NodeFormatter interface.
func (node *NotExpr) Format(buf *bytes.Buffer, f FmtFlags) {
buf.WriteString("NOT ")
exprFmtWithParen(buf, f, node.Expr)
}
// NewTypedNotExpr returns a new NotExpr that is verified to be well-typed.
func NewTypedNotExpr(expr TypedExpr) *NotExpr {
node := &NotExpr{Expr: expr}
node.typ = TypeBool
return node
}
// TypedInnerExpr returns the NotExpr's inner expression as a TypedExpr.
func (node *NotExpr) TypedInnerExpr() TypedExpr {
return node.Expr.(TypedExpr)
}
// ParenExpr represents a parenthesized expression.
type ParenExpr struct {
Expr Expr
typeAnnotation
}
// Format implements the NodeFormatter interface.
func (node *ParenExpr) Format(buf *bytes.Buffer, f FmtFlags) {
buf.WriteByte('(')
FormatNode(buf, f, node.Expr)
buf.WriteByte(')')
}
// TypedInnerExpr returns the ParenExpr's inner expression as a TypedExpr.
func (node *ParenExpr) TypedInnerExpr() TypedExpr {
return node.Expr.(TypedExpr)
}
// StripParens strips any parentheses surrounding an expression and
// returns the inner expression. For instance:
// 1 -> 1
// (1) -> 1
// ((1)) -> 1
func StripParens(expr Expr) Expr {
if p, ok := expr.(*ParenExpr); ok {
return StripParens(p.Expr)
}
return expr
}
// ComparisonOperator represents a binary operator.
type ComparisonOperator int
func (ComparisonOperator) operator() {}
// ComparisonExpr.Operator
const (
EQ ComparisonOperator = iota
LT
GT
LE
GE
NE
In
NotIn
Like
NotLike
ILike
NotILike
SimilarTo
NotSimilarTo
RegMatch
NotRegMatch
RegIMatch
NotRegIMatch
IsDistinctFrom
IsNotDistinctFrom
Is
IsNot
// The following operators will always be used with an associated SubOperator.
// If Go had algebraic data types they would be defined in a self-contained
// manner like:
//
// Any(ComparisonOperator)
// Some(ComparisonOperator)
// ...
//
// where the internal ComparisonOperator qualifies the behavior of the primary
// operator. Instead, a secondary ComparisonOperator is optionally included in
// ComparisonExpr for the cases where these operators are the primary op.
//
// ComparisonOperator.hasSubOperator returns true for ops in this group.
Any
Some
All
)
var comparisonOpName = [...]string{
EQ: "=",
LT: "<",
GT: ">",
LE: "<=",
GE: ">=",
NE: "!=",
In: "IN",
NotIn: "NOT IN",
Like: "LIKE",
NotLike: "NOT LIKE",
ILike: "ILIKE",
NotILike: "NOT ILIKE",
SimilarTo: "SIMILAR TO",
NotSimilarTo: "NOT SIMILAR TO",
RegMatch: "~",
NotRegMatch: "!~",
RegIMatch: "~*",
NotRegIMatch: "!~*",
IsDistinctFrom: "IS DISTINCT FROM",
IsNotDistinctFrom: "IS NOT DISTINCT FROM",
Is: "IS",
IsNot: "IS NOT",
Any: "ANY",
Some: "SOME",
All: "ALL",
}
func (i ComparisonOperator) String() string {
if i < 0 || i > ComparisonOperator(len(comparisonOpName)-1) {
return fmt.Sprintf("ComparisonOp(%d)", i)
}
return comparisonOpName[i]
}
// hasSubOperator returns if the ComparisonOperator is used with a sub-operator.
func (i ComparisonOperator) hasSubOperator() bool {
switch i {
case Any:
case Some:
case All:
default:
return false
}
return true
}
// ComparisonExpr represents a two-value comparison expression.
type ComparisonExpr struct {
Operator ComparisonOperator
SubOperator ComparisonOperator // used for array operators (when Operator is Any, Some, or All)
Left, Right Expr
typeAnnotation
fn CmpOp
}
func (*ComparisonExpr) operatorExpr() {}
// Format implements the NodeFormatter interface.
func (node *ComparisonExpr) Format(buf *bytes.Buffer, f FmtFlags) {
opStr := node.Operator.String()
if node.Operator.hasSubOperator() {
binExprFmtWithParenAndSubOp(buf, f, node.Left, node.SubOperator.String(), opStr, node.Right)
} else {
binExprFmtWithParen(buf, f, node.Left, opStr, node.Right)
}
}
// NewTypedComparisonExpr returns a new ComparisonExpr that is verified to be well-typed.
func NewTypedComparisonExpr(op ComparisonOperator, left, right TypedExpr) *ComparisonExpr {
node := &ComparisonExpr{Operator: op, Left: left, Right: right}
node.typ = TypeBool
node.memoizeFn()
return node
}
func (node *ComparisonExpr) memoizeFn() {
fOp, fLeft, fRight, _, _ := foldComparisonExpr(node.Operator, node.Left, node.Right)
leftRet, rightRet := fLeft.(TypedExpr).ResolvedType(), fRight.(TypedExpr).ResolvedType()
switch node.Operator {
case Is, IsNot, IsDistinctFrom, IsNotDistinctFrom:
// Is and related operators do not memoize a CmpOp.
return
case Any, Some, All:
// Array operators memoize the SubOperator's CmpOp.
fOp, _, _, _, _ = foldComparisonExpr(node.SubOperator, nil, nil)
rightRet = rightRet.(TArray).Typ
}
fn, ok := CmpOps[fOp].lookupImpl(leftRet, rightRet)
if !ok {
panic(fmt.Sprintf("lookup for ComparisonExpr %s's CmpOp failed",
AsStringWithFlags(node, FmtShowTypes)))
}
node.fn = fn
}
// TypedLeft returns the ComparisonExpr's left expression as a TypedExpr.
func (node *ComparisonExpr) TypedLeft() TypedExpr {
return node.Left.(TypedExpr)
}
// TypedRight returns the ComparisonExpr's right expression as a TypedExpr.
func (node *ComparisonExpr) TypedRight() TypedExpr {
return node.Right.(TypedExpr)
}
// IsMixedTypeComparison returns true when the two sides of
// a comparison operator have different types.
func (node *ComparisonExpr) IsMixedTypeComparison() bool {
switch node.Operator {
case In, NotIn:
tuple := node.TypedRight().ResolvedType().(TTuple)
for _, typ := range tuple {
if !sameTypeOrNull(node.TypedLeft().ResolvedType(), typ) {
return true
}
}
return false
case Any, Some, All:
array := node.TypedRight().ResolvedType().(TArray)
return !sameTypeOrNull(node.TypedLeft().ResolvedType(), array.Typ)
default:
return !sameTypeOrNull(node.TypedLeft().ResolvedType(), node.TypedRight().ResolvedType())
}
}
func sameTypeOrNull(left, right Type) bool {
return left == TypeNull || right == TypeNull || left.Equivalent(right)
}
// RangeCond represents a BETWEEN or a NOT BETWEEN expression.
type RangeCond struct {
Not bool
Left Expr
From, To Expr
typeAnnotation
}
func (*RangeCond) operatorExpr() {}
// Format implements the NodeFormatter interface.
func (node *RangeCond) Format(buf *bytes.Buffer, f FmtFlags) {
notStr := " BETWEEN "
if node.Not {
notStr = " NOT BETWEEN "
}
exprFmtWithParen(buf, f, node.Left)
buf.WriteString(notStr)
binExprFmtWithParen(buf, f, node.From, "AND", node.To)
}
// TypedLeft returns the RangeCond's left expression as a TypedExpr.
func (node *RangeCond) TypedLeft() TypedExpr {
return node.Left.(TypedExpr)
}
// TypedFrom returns the RangeCond's from expression as a TypedExpr.
func (node *RangeCond) TypedFrom() TypedExpr {
return node.From.(TypedExpr)
}
// TypedTo returns the RangeCond's to expression as a TypedExpr.
func (node *RangeCond) TypedTo() TypedExpr {
return node.To.(TypedExpr)
}
// IsOfTypeExpr represents an IS {,NOT} OF (type_list) expression.
type IsOfTypeExpr struct {
Not bool
Expr Expr
Types []ColumnType
typeAnnotation
}
func (*IsOfTypeExpr) operatorExpr() {}
// Format implements the NodeFormatter interface.
func (node *IsOfTypeExpr) Format(buf *bytes.Buffer, f FmtFlags) {
exprFmtWithParen(buf, f, node.Expr)
buf.WriteString(" IS")
if node.Not {
buf.WriteString(" NOT")
}
buf.WriteString(" OF (")
for i, t := range node.Types {
if i > 0 {
buf.WriteString(", ")
}
FormatNode(buf, f, t)
}
buf.WriteByte(')')
}
// ExistsExpr represents an EXISTS expression.
type ExistsExpr struct {
Subquery Expr
typeAnnotation
}
// Format implements the NodeFormatter interface.
func (node *ExistsExpr) Format(buf *bytes.Buffer, f FmtFlags) {
buf.WriteString("EXISTS ")
exprFmtWithParen(buf, f, node.Subquery)
}
// IfExpr represents an IF expression.
type IfExpr struct {
Cond Expr
True Expr
Else Expr
typeAnnotation
}
// TypedTrueExpr returns the IfExpr's True expression as a TypedExpr.
func (node *IfExpr) TypedTrueExpr() TypedExpr {
return node.True.(TypedExpr)
}
// TypedCondExpr returns the IfExpr's Cond expression as a TypedExpr.
func (node *IfExpr) TypedCondExpr() TypedExpr {
return node.Cond.(TypedExpr)
}
// TypedElseExpr returns the IfExpr's Else expression as a TypedExpr.
func (node *IfExpr) TypedElseExpr() TypedExpr {
return node.Else.(TypedExpr)
}
// Format implements the NodeFormatter interface.
func (node *IfExpr) Format(buf *bytes.Buffer, f FmtFlags) {
buf.WriteString("IF(")
FormatNode(buf, f, node.Cond)
buf.WriteString(", ")
FormatNode(buf, f, node.True)
buf.WriteString(", ")
FormatNode(buf, f, node.Else)
buf.WriteByte(')')
}
// NullIfExpr represents a NULLIF expression.
type NullIfExpr struct {
Expr1 Expr
Expr2 Expr
typeAnnotation
}
// Format implements the NodeFormatter interface.
func (node *NullIfExpr) Format(buf *bytes.Buffer, f FmtFlags) {
buf.WriteString("NULLIF(")
FormatNode(buf, f, node.Expr1)
buf.WriteString(", ")
FormatNode(buf, f, node.Expr2)
buf.WriteByte(')')
}
// CoalesceExpr represents a COALESCE or IFNULL expression.
type CoalesceExpr struct {
Name string
Exprs Exprs
typeAnnotation
}
// TypedExprAt returns the expression at the specified index as a TypedExpr.
func (node *CoalesceExpr) TypedExprAt(idx int) TypedExpr {
return node.Exprs[idx].(TypedExpr)
}
// Format implements the NodeFormatter interface.
func (node *CoalesceExpr) Format(buf *bytes.Buffer, f FmtFlags) {
buf.WriteString(node.Name)
buf.WriteByte('(')
FormatNode(buf, f, node.Exprs)
buf.WriteByte(')')
}
// DefaultVal represents the DEFAULT expression.
type DefaultVal struct{}
// Format implements the NodeFormatter interface.
func (node DefaultVal) Format(buf *bytes.Buffer, f FmtFlags) {
buf.WriteString("DEFAULT")
}
// ResolvedType implements the TypedExpr interface.
func (DefaultVal) ResolvedType() Type { return nil }
var _ VariableExpr = &Placeholder{}
// Placeholder represents a named placeholder.
type Placeholder struct {
Name string
typeAnnotation
}
// NewPlaceholder allocates a Placeholder.
func NewPlaceholder(name string) *Placeholder {
return &Placeholder{Name: name}
}
// Variable implements the VariableExpr interface.
func (*Placeholder) Variable() {}
// Format implements the NodeFormatter interface.
func (node *Placeholder) Format(buf *bytes.Buffer, f FmtFlags) {
buf.WriteByte('$')
buf.WriteString(node.Name)
}
// ResolvedType implements the TypedExpr interface.
func (node *Placeholder) ResolvedType() Type {
if node.typ == nil {
node.typ = &TPlaceholder{Name: node.Name}
}
return node.typ
}
// Tuple represents a parenthesized list of expressions.
type Tuple struct {
Exprs Exprs
row bool // indicates whether or not the tuple should be textually represented as a row.
types TTuple
}
// Format implements the NodeFormatter interface.
func (node *Tuple) Format(buf *bytes.Buffer, f FmtFlags) {
if node.row {
buf.WriteString("ROW")
}
buf.WriteByte('(')
FormatNode(buf, f, node.Exprs)
buf.WriteByte(')')
}
// ResolvedType implements the TypedExpr interface.
func (node *Tuple) ResolvedType() Type {
return node.types
}
// Array represents an array constructor.
type Array struct {
Exprs Exprs
typeAnnotation
}
// Format implements the NodeFormatter interface.
func (node *Array) Format(buf *bytes.Buffer, f FmtFlags) {
buf.WriteString("ARRAY[")
FormatNode(buf, f, node.Exprs)
buf.WriteByte(']')
}
// ArrayFlatten represents a subquery array constructor.
type ArrayFlatten struct {
Subquery Expr
typeAnnotation
}
// Format implements the NodeFormatter interface.
func (node *ArrayFlatten) Format(buf *bytes.Buffer, f FmtFlags) {
buf.WriteString("ARRAY")
exprFmtWithParen(buf, f, node.Subquery)
}
// Exprs represents a list of value expressions. It's not a valid expression
// because it's not parenthesized.
type Exprs []Expr
// Format implements the NodeFormatter interface.
func (node Exprs) Format(buf *bytes.Buffer, f FmtFlags) {
for i, n := range node {
if i > 0 {
buf.WriteString(", ")
}
FormatNode(buf, f, n)
}
}
// TypedExprs represents a list of well-typed value expressions. It's not a valid expression
// because it's not parenthesized.
type TypedExprs []TypedExpr
func (node TypedExprs) String() string {
var prefix string
var buf bytes.Buffer
for _, n := range node {
fmt.Fprintf(&buf, "%s%s", prefix, n)
prefix = ", "
}
return buf.String()
}
// Subquery represents a subquery.
type Subquery struct {
Select SelectStatement
}
// Variable implements the VariableExpr interface.
func (*Subquery) Variable() {}
// Format implements the NodeFormatter interface.
func (node *Subquery) Format(buf *bytes.Buffer, f FmtFlags) {
FormatNode(buf, f, node.Select)
}
// BinaryOperator represents a binary operator.
type BinaryOperator int
func (BinaryOperator) operator() {}
// BinaryExpr.Operator
const (
Bitand BinaryOperator = iota
Bitor
Bitxor
Plus
Minus
Mult
Div
FloorDiv
Mod
Pow
Concat
LShift
RShift
)
var binaryOpName = [...]string{
Bitand: "&",
Bitor: "|",
Bitxor: "#",
Plus: "+",
Minus: "-",
Mult: "*",
Div: "/",
FloorDiv: "//",
Mod: "%",
Pow: "^",
Concat: "||",
LShift: "<<",
RShift: ">>",
}
func (i BinaryOperator) String() string {
if i < 0 || i > BinaryOperator(len(binaryOpName)-1) {
return fmt.Sprintf("BinaryOp(%d)", i)
}
return binaryOpName[i]
}
// BinaryExpr represents a binary value expression.
type BinaryExpr struct {
Operator BinaryOperator
Left, Right Expr
typeAnnotation
fn BinOp
}
// TypedLeft returns the BinaryExpr's left expression as a TypedExpr.
func (node *BinaryExpr) TypedLeft() TypedExpr {
return node.Left.(TypedExpr)
}
// TypedRight returns the BinaryExpr's right expression as a TypedExpr.
func (node *BinaryExpr) TypedRight() TypedExpr {
return node.Right.(TypedExpr)
}
func (*BinaryExpr) operatorExpr() {}
func (node *BinaryExpr) memoizeFn() {
leftRet, rightRet := node.Left.(TypedExpr).ResolvedType(), node.Right.(TypedExpr).ResolvedType()
fn, ok := BinOps[node.Operator].lookupImpl(leftRet, rightRet)
if !ok {
panic(fmt.Sprintf("lookup for BinaryExpr %s's BinOp failed",
AsStringWithFlags(node, FmtShowTypes)))
}
node.fn = fn
}
// newBinExprIfValidOverload constructs a new BinaryExpr if and only
// if the pair of arguments have a valid implementation for the given
// BinaryOperator.
func newBinExprIfValidOverload(op BinaryOperator, left TypedExpr, right TypedExpr) *BinaryExpr {
leftRet, rightRet := left.ResolvedType(), right.ResolvedType()
fn, ok := BinOps[op].lookupImpl(leftRet, rightRet)
if ok {
expr := &BinaryExpr{
Operator: op,
Left: left,
Right: right,
fn: fn,
}
expr.typ = returnTypeToFixedType(fn.returnType())
return expr
}
return nil
}
// Format implements the NodeFormatter interface.
func (node *BinaryExpr) Format(buf *bytes.Buffer, f FmtFlags) {
binExprFmtWithParen(buf, f, node.Left, node.Operator.String(), node.Right)
}
// UnaryOperator represents a unary operator.
type UnaryOperator int
func (UnaryOperator) operator() {}
// UnaryExpr.Operator
const (
UnaryPlus UnaryOperator = iota
UnaryMinus
UnaryComplement
)
var unaryOpName = [...]string{
UnaryPlus: "+",
UnaryMinus: "-",
UnaryComplement: "~",
}
func (i UnaryOperator) String() string {
if i < 0 || i > UnaryOperator(len(unaryOpName)-1) {
return fmt.Sprintf("UnaryOp(%d)", i)
}
return unaryOpName[i]
}
// UnaryExpr represents a unary value expression.
type UnaryExpr struct {
Operator UnaryOperator
Expr Expr
typeAnnotation
fn UnaryOp
}
func (*UnaryExpr) operatorExpr() {}
// Format implements the NodeFormatter interface.
func (node *UnaryExpr) Format(buf *bytes.Buffer, f FmtFlags) {
buf.WriteString(node.Operator.String())
exprFmtWithParen(buf, f, node.Expr)
}
// TypedInnerExpr returns the UnaryExpr's inner expression as a TypedExpr.
func (node *UnaryExpr) TypedInnerExpr() TypedExpr {
return node.Expr.(TypedExpr)
}
// FuncExpr represents a function call.
type FuncExpr struct {
Func ResolvableFunctionReference
Type funcType
Exprs Exprs
// Filter is used for filters on aggregates: SUM(k) FILTER (WHERE k > 0)
Filter Expr
WindowDef *WindowDef
typeAnnotation
fn Builtin
}
// GetAggregateConstructor exposes the AggregateFunc field for use by
// the group node in package sql.
func (node *FuncExpr) GetAggregateConstructor() func(*EvalContext) AggregateFunc {
if node.fn.AggregateFunc == nil {
return nil
}
return func(evalCtx *EvalContext) AggregateFunc {
types := typesOfExprs(node.Exprs)
return node.fn.AggregateFunc(types, evalCtx)
}
}
// GetWindowConstructor returns a window function constructor if the
// FuncExpr is a built-in window function.
func (node *FuncExpr) GetWindowConstructor() func(*EvalContext) WindowFunc {
if node.fn.WindowFunc == nil {
return nil
}
return func(evalCtx *EvalContext) WindowFunc {
types := typesOfExprs(node.Exprs)
return node.fn.WindowFunc(types, evalCtx)
}
}
func typesOfExprs(exprs Exprs) []Type {
types := make([]Type, len(exprs))
for i, expr := range exprs {
types[i] = expr.(TypedExpr).ResolvedType()
}
return types
}
// IsWindowFunctionApplication returns if the function is being applied as a window function.
func (node *FuncExpr) IsWindowFunctionApplication() bool {
return node.WindowDef != nil
}
// IsImpure returns whether the function application is impure, meaning that it
// potentially returns a different value when called in the same statement with
// the same parameters.
func (node *FuncExpr) IsImpure() bool {
return node.fn.impure
}
// IsDistSQLBlacklist returns whether the function is not supported by DistSQL.
func (node *FuncExpr) IsDistSQLBlacklist() bool {
return node.fn.DistSQLBlacklist()
}
type funcType int
// FuncExpr.Type
const (
_ funcType = iota
DistinctFuncType
AllFuncType
)
var funcTypeName = [...]string{
DistinctFuncType: "DISTINCT",
AllFuncType: "ALL",
}
// Format implements the NodeFormatter interface.
func (node *FuncExpr) Format(buf *bytes.Buffer, f FmtFlags) {
var typ string
if node.Type != 0 {
typ = funcTypeName[node.Type] + " "
}
fmtDisableAnonymize := *f
fmtDisableAnonymize.anonymize = false
FormatNode(buf, &fmtDisableAnonymize, node.Func)
buf.WriteByte('(')
buf.WriteString(typ)
FormatNode(buf, f, node.Exprs)
buf.WriteByte(')')
if window := node.WindowDef; window != nil {
buf.WriteString(" OVER ")
if window.Name != "" {
FormatNode(buf, f, window.Name)
} else {
FormatNode(buf, f, window)
}
}
if node.Filter != nil {
buf.WriteString(" FILTER (WHERE ")
FormatNode(buf, f, node.Filter)
buf.WriteString(")")
}
}
// CaseExpr represents a CASE expression.
type CaseExpr struct {
Expr Expr
Whens []*When
Else Expr
typeAnnotation
}
// Format implements the NodeFormatter interface.
func (node *CaseExpr) Format(buf *bytes.Buffer, f FmtFlags) {
buf.WriteString("CASE ")
if node.Expr != nil {
FormatNode(buf, f, node.Expr)
buf.WriteByte(' ')
}
for _, when := range node.Whens {
FormatNode(buf, f, when)
buf.WriteByte(' ')