forked from cloudspannerecosystem/memefish
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathast.go
2840 lines (2280 loc) · 65.3 KB
/
ast.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
// Package ast provides AST nodes definitions.
//
// The definitions of ASTs are based on the following document.
//
// - <https://cloud.google.com/spanner/docs/reference/standard-sql/data-definition-language>
// - <https://cloud.google.com/spanner/docs/query-syntax>
//
// Each `Node`'s documentation describes its syntax (SQL representation) in a `text/template`
// fashion with thw following custom functions.
//
// - `sql node`: Returns the SQL representation of `node`.
// - `sqlOpt node`: Like `sql node`, but returns the empty string if `node` is `nil`.
// - `sqlJoin sep nodes`: Concatenates the SQL representations of `nodes` with `sep`.
// - `sqlIdentQuote x`: Quotes the given identifier string if needed.
// - `sqlStringQuote s`: Returns the SQL quoted string of `s`.
// - `sqlBytesQuote bs`: Returns the SQL quotes bytes of `bs`.
// - `isnil v`: Checks whether `v` is `nil` or others.
//
// Each `Node`s documentation has `pos` and `end` information using the following EBNF.
//
// PosChoice -> PosExpr ("||" PosExpr)*
// PosExpr -> PosAtom ("+" IntAtom)?
// PosAtom -> PosVar | NodeExpr "." ("pos" | "end")
// NodeExpr -> NodeAtom | "(" NodeAtom ("??" NodeAtom)* ")"
// NodeAtom -> NodeVar | NodeSliceVar "[" (IntAtom | "$") "]"
// IntAtom -> IntVal
// | "len" "(" StringVar ")"
// | "(" BoolVar "?" IntAtom ":" IntAtom ")"
// IntVal -> "0" | "1" | ...
//
// (PosVar, NodeVar, NodeSliceVar, and BoolVar are derived by its `struct` definition.)
package ast
import (
"github.com/cloudspannerecosystem/memefish/token"
)
// Node is base interface of Spanner SQL AST nodes.
type Node interface {
Pos() token.Pos
End() token.Pos
// Convert AST node into SQL source string (a.k.a. Unparse).
SQL() string
}
// Statement represents toplevel statement node of Spanner SQL.
type Statement interface {
Node
isStatement()
}
func (QueryStatement) isStatement() {}
func (CreateDatabase) isStatement() {}
func (CreateTable) isStatement() {}
func (CreateSequence) isStatement() {}
func (CreateView) isStatement() {}
func (CreateIndex) isStatement() {}
func (CreateVectorIndex) isStatement() {}
func (CreateRole) isStatement() {}
func (AlterTable) isStatement() {}
func (AlterIndex) isStatement() {}
func (AlterSequence) isStatement() {}
func (DropTable) isStatement() {}
func (DropIndex) isStatement() {}
func (DropVectorIndex) isStatement() {}
func (DropSequence) isStatement() {}
func (DropRole) isStatement() {}
func (Insert) isStatement() {}
func (Delete) isStatement() {}
func (Update) isStatement() {}
func (Grant) isStatement() {}
func (Revoke) isStatement() {}
func (CreateChangeStream) isStatement() {}
func (AlterChangeStream) isStatement() {}
func (DropChangeStream) isStatement() {}
func (CreatePropertyGraph) isStatement() {}
func (DropPropertyGraph) isStatement() {}
// QueryExpr represents set operator operands.
type QueryExpr interface {
Node
isQueryExpr()
}
func (Select) isQueryExpr() {}
func (SubQuery) isQueryExpr() {}
func (CompoundQuery) isQueryExpr() {}
// SelectItem represents expression in SELECT clause result columns list.
type SelectItem interface {
Node
isSelectItem()
}
func (Star) isSelectItem() {}
func (DotStar) isSelectItem() {}
func (Alias) isSelectItem() {}
func (ExprSelectItem) isSelectItem() {}
// SelectAs represents AS VALUE/STRUCT/typename clause in SELECT clause.
type SelectAs interface {
Node
isSelectAs()
}
func (AsStruct) isSelectAs() {}
func (AsValue) isSelectAs() {}
func (AsTypeName) isSelectAs() {}
// TableExpr represents JOIN operands.
type TableExpr interface {
Node
isTableExpr()
}
func (Unnest) isTableExpr() {}
func (TableName) isTableExpr() {}
func (SubQueryTableExpr) isTableExpr() {}
func (ParenTableExpr) isTableExpr() {}
func (Join) isTableExpr() {}
// JoinCondition represents condition part of JOIN expression.
type JoinCondition interface {
Node
isJoinCondition()
}
func (On) isJoinCondition() {}
func (Using) isJoinCondition() {}
// Expr repersents an expression in SQL.
type Expr interface {
Node
isExpr()
}
func (BinaryExpr) isExpr() {}
func (UnaryExpr) isExpr() {}
func (InExpr) isExpr() {}
func (IsNullExpr) isExpr() {}
func (IsBoolExpr) isExpr() {}
func (BetweenExpr) isExpr() {}
func (SelectorExpr) isExpr() {}
func (IndexExpr) isExpr() {}
func (CallExpr) isExpr() {}
func (CountStarExpr) isExpr() {}
func (CastExpr) isExpr() {}
func (ExtractExpr) isExpr() {}
func (CaseExpr) isExpr() {}
func (ParenExpr) isExpr() {}
func (ScalarSubQuery) isExpr() {}
func (ArraySubQuery) isExpr() {}
func (ExistsSubQuery) isExpr() {}
func (Param) isExpr() {}
func (Ident) isExpr() {}
func (Path) isExpr() {}
func (ArrayLiteral) isExpr() {}
func (StructLiteral) isExpr() {}
func (NullLiteral) isExpr() {}
func (BoolLiteral) isExpr() {}
func (IntLiteral) isExpr() {}
func (FloatLiteral) isExpr() {}
func (StringLiteral) isExpr() {}
func (BytesLiteral) isExpr() {}
func (DateLiteral) isExpr() {}
func (TimestampLiteral) isExpr() {}
func (NumericLiteral) isExpr() {}
func (JSONLiteral) isExpr() {}
// Arg represents argument of function call.
type Arg interface {
Node
isArg()
}
func (ExprArg) isArg() {}
func (IntervalArg) isArg() {}
func (SequenceArg) isArg() {}
// NullHandlingModifier represents IGNORE/RESPECT NULLS of aggregate function calls
type NullHandlingModifier interface {
Node
isNullHandlingModifier()
}
func (IgnoreNulls) isNullHandlingModifier() {}
func (RespectNulls) isNullHandlingModifier() {}
// HavingModifier represents HAVING clause of aggregate function calls.
type HavingModifier interface {
Node
isHavingModifier()
}
func (HavingMax) isHavingModifier() {}
func (HavingMin) isHavingModifier() {}
// InCondition is right-side value of IN operator.
type InCondition interface {
Node
isInCondition()
}
func (UnnestInCondition) isInCondition() {}
func (SubQueryInCondition) isInCondition() {}
func (ValuesInCondition) isInCondition() {}
// Type represents type node.
type Type interface {
Node
isType()
}
func (SimpleType) isType() {}
func (ArrayType) isType() {}
func (StructType) isType() {}
func (NamedType) isType() {}
// IntValue represents integer values in SQL.
type IntValue interface {
Node
isIntValue()
}
func (Param) isIntValue() {}
func (IntLiteral) isIntValue() {}
func (CastIntValue) isIntValue() {}
// NumValue represents number values in SQL.
type NumValue interface {
Node
isNumValue()
}
func (Param) isNumValue() {}
func (IntLiteral) isNumValue() {}
func (FloatLiteral) isNumValue() {}
func (CastNumValue) isNumValue() {}
// StringValue represents string value in SQL.
type StringValue interface {
Node
isStringValue()
}
func (Param) isStringValue() {}
func (StringLiteral) isStringValue() {}
// DDL represents data definition language in SQL.
//
// https://cloud.google.com/spanner/docs/data-definition-language
type DDL interface {
Statement
isDDL()
}
func (CreateDatabase) isDDL() {}
func (CreateTable) isDDL() {}
func (CreateView) isDDL() {}
func (CreateSequence) isDDL() {}
func (AlterTable) isDDL() {}
func (DropTable) isDDL() {}
func (CreateIndex) isDDL() {}
func (CreateVectorIndex) isDDL() {}
func (AlterIndex) isDDL() {}
func (AlterSequence) isDDL() {}
func (DropIndex) isDDL() {}
func (DropVectorIndex) isDDL() {}
func (DropSequence) isDDL() {}
func (CreateRole) isDDL() {}
func (DropRole) isDDL() {}
func (Grant) isDDL() {}
func (Revoke) isDDL() {}
func (CreateChangeStream) isDDL() {}
func (AlterChangeStream) isDDL() {}
func (DropChangeStream) isDDL() {}
func (CreatePropertyGraph) isDDL() {}
func (DropPropertyGraph) isDDL() {}
// Constraint represents table constraint of CONSTARINT clause.
type Constraint interface {
Node
isConstraint()
}
func (ForeignKey) isConstraint() {}
func (Check) isConstraint() {}
// TableAlteration represents ALTER TABLE action.
type TableAlteration interface {
Node
isTableAlteration()
}
func (AddColumn) isTableAlteration() {}
func (AddTableConstraint) isTableAlteration() {}
func (AddRowDeletionPolicy) isTableAlteration() {}
func (DropColumn) isTableAlteration() {}
func (DropConstraint) isTableAlteration() {}
func (DropRowDeletionPolicy) isTableAlteration() {}
func (ReplaceRowDeletionPolicy) isTableAlteration() {}
func (SetOnDelete) isTableAlteration() {}
func (AlterColumn) isTableAlteration() {}
func (AlterColumnSet) isTableAlteration() {}
// Privilege represents privileges specified by GRANT and REVOKE.
type Privilege interface {
Node
isPrivilege()
}
func (PrivilegeOnTable) isPrivilege() {}
func (SelectPrivilegeOnView) isPrivilege() {}
func (ExecutePrivilegeOnTableFunction) isPrivilege() {}
func (RolePrivilege) isPrivilege() {}
// TablePrivilege represents privileges on table.
type TablePrivilege interface {
Node
isTablePrivilege()
}
func (SelectPrivilege) isTablePrivilege() {}
func (InsertPrivilege) isTablePrivilege() {}
func (UpdatePrivilege) isTablePrivilege() {}
func (DeletePrivilege) isTablePrivilege() {}
// SchemaType represents types for schema.
type SchemaType interface {
Node
isSchemaType()
}
func (ScalarSchemaType) isSchemaType() {}
func (SizedSchemaType) isSchemaType() {}
func (ArraySchemaType) isSchemaType() {}
func (NamedType) isSchemaType() {}
// IndexAlteration represents ALTER INDEX action.
type IndexAlteration interface {
Node
isIndexAlteration()
}
func (AddStoredColumn) isIndexAlteration() {}
func (DropStoredColumn) isIndexAlteration() {}
// DML represents data manipulation language in SQL.
//
// https://cloud.google.com/spanner/docs/data-definition-language
type DML interface {
Statement
isDML()
}
func (Insert) isDML() {}
func (Delete) isDML() {}
func (Update) isDML() {}
// InsertInput represents input values of INSERT statement.
type InsertInput interface {
Node
isInsertInput()
}
func (ValuesInput) isInsertInput() {}
func (SubQueryInput) isInsertInput() {}
// ChangeStreamFor represents FOR clause in CREATE/ALTER CHANGE STREAM statement.
type ChangeStreamFor interface {
Node
isChangeStreamFor()
}
func (ChangeStreamForAll) isChangeStreamFor() {}
func (ChangeStreamForTables) isChangeStreamFor() {}
// ChangeStreamAlteration represents ALTER CHANGE STREAM action.
type ChangeStreamAlteration interface {
Node
isChangeStreamAlteration()
}
func (ChangeStreamSetFor) isChangeStreamAlteration() {}
func (ChangeStreamDropForAll) isChangeStreamAlteration() {}
func (ChangeStreamSetOptions) isChangeStreamAlteration() {}
// ================================================================================
//
// SELECT
//
// ================================================================================
// QueryStatement is query statement node.
//
// {{if .Hint}}{{.Hint | sql}}{{end}}
// {{.Expr | sql}}
//
// https://cloud.google.com/spanner/docs/query-syntax
type QueryStatement struct {
// pos = (Hint ?? With ?? Expr).pos
// end = Expr.end
Hint *Hint // optional
With *With // optional
Query QueryExpr
}
// Hint is hint node.
//
// @{{"{"}}{{.Records | sqlJoin ","}}{{"}"}}
type Hint struct {
// pos = Atmark
// end = Rbrace + 1
Atmark token.Pos // position of "@"
Rbrace token.Pos // position of "}"
Records []*HintRecord // len(Records) > 0
}
// HintRecord is hint record node.
//
// {{.Key | sql}}={{.Value | sql}}
type HintRecord struct {
// pos = Key.pos
// end = Value.end
Key *Ident
Value Expr
}
// With is with clause node.
//
// WITH {{.CTEs | sqlJoin ","}}
type With struct {
// pos = With
// end = CTEs[$].end
With token.Pos // position of "WITH" keyword
CTEs []*CTE
}
// CTE is common table expression node.
//
// {{.Name}} AS ({{.QueryExpr}})
type CTE struct {
// pos = Name.pos
// end = Rparen + 1
Rparen token.Pos // position of ")"
Name *Ident
QueryExpr QueryExpr
}
// Select is SELECT statement node.
//
// SELECT
// {{if .Distinct}}DISTINCT{{end}}
// {{.As | sqlOpt}}
// {{.Results | sqlJoin ","}}
// {{.From | sqlOpt}}
// {{.Where | sqlOpt}}
// {{.GroupBy | sqlOpt}}
// {{.Having | sqlOpt}}
// {{.OrderBy | sqlOpt}}
// {{.Limit | sqlOpt}}
type Select struct {
// pos = Select
// end = (Limit ?? OrderBy ?? Having ?? GroupBy ?? Where ?? From ?? Results[$]).end
Select token.Pos // position of "select" keyword
Distinct bool
As SelectAs // optional
Results []SelectItem // len(Results) > 0
From *From // optional
Where *Where // optional
GroupBy *GroupBy // optional
Having *Having // optional
OrderBy *OrderBy // optional
Limit *Limit // optional
}
// AsStruct represents AS STRUCT node in SELECT clause.
//
// AS STRUCT
type AsStruct struct {
// pos = As
// end = Struct + 6
As token.Pos
Struct token.Pos
}
// AsValue represents AS VALUE node in SELECT clause.
//
// AS VALUE
type AsValue struct {
// pos = As
// end = Value + 5
As token.Pos
Value token.Pos
}
// AsTypeName represents AS typename node in SELECT clause.
//
// AS {{.TypeName | sql}}
type AsTypeName struct {
// pos = As
// end = TypeName.end
As token.Pos
TypeName *NamedType
}
// CompoundQuery is query statement node compounded by set operators.
//
// {{.Queries | sqlJoin (printf "%s %s" .Op or(and(.Distinct, "DISTINCT"), "ALL"))}}
// {{.OrderBy | sqlOpt}}
// {{.Limit | sqlOpt}}
type CompoundQuery struct {
// pos = Queries[0].pos
// end = (Limit ?? OrderBy ?? Queries[$]).end
Op SetOp
Distinct bool
Queries []QueryExpr // len(List) >= 2
OrderBy *OrderBy // optional
Limit *Limit // optional
}
// SubQuery is subquery statement node.
//
// ({{.Expr | sql}} {{.OrderBy | sqlOpt}} {{.Limit | sqlOpt}})
type SubQuery struct {
// pos = Lparen
// end = (Query ?? Limit).end || Rparen + 1
Lparen, Rparen token.Pos // position of "(" and ")"
Query QueryExpr
OrderBy *OrderBy // optional
Limit *Limit // optional
}
// Star is a single * in SELECT result columns list.
//
// *
type Star struct {
// pos = Star
// end = Star + 1
Star token.Pos // position of "*"
}
// DotStar is expression with * in SELECT result columns list.
//
// {{.Expr | sql}}.*
type DotStar struct {
// pos = Expr.pos
// end = Star + 1
Star token.Pos // position of "*"
Expr Expr
}
// Alias is aliased expression by AS clause in SELECT result columns list.
//
// {{.Expr | sql}} {{.As | sql}}
type Alias struct {
// pos = Expr.pos
// end = As.end
Expr Expr
As *AsAlias
}
// AsAlias is AS clause node for general purpose.
//
// It is used in Alias node and some JoinExpr nodes.
//
// NOTE: Sometime keyword AS can be omited.
//
// In this case, it.token.Pos() == it.Alias.token.Pos(), so we can detect this.
//
// AS {{.Alias | sql}}
type AsAlias struct {
// pos = As || Alias.pos
// end = Alias.end
As token.Pos // position of "AS" keyword
Alias *Ident
}
// ExprSelectItem is Expr wrapper for SelectItem.
//
// {{.Expr | sql}}
type ExprSelectItem struct {
// pos = Expr.pos
// end = Expr.end
Expr Expr
}
// From is FROM clause node.
//
// FROM {{.Source | sql}}
type From struct {
// pos = From
// end = Source.end
From token.Pos // position of "FROM" keyword
Source TableExpr
}
// Where is WHERE clause node.
//
// WHERE {{.Expr | sql}}
type Where struct {
// pos = Where
// end = Expr.end
Where token.Pos // position of "WHERE" keyword
Expr Expr
}
// GroupBy is GROUP BY clause node.
//
// GROUP BY {{.Exprs | sqlJoin ","}}
type GroupBy struct {
// pos = Group
// end = Exprs[$].end
Group token.Pos // position of "GROUP" keyword
Exprs []Expr // len(Exprs) > 0
}
// Having is HAVING clause node.
//
// HAVING {{.Expr | sql}}
type Having struct {
// pos = Having
// end = Expr.end
Having token.Pos // position of "HAVING" keyword
Expr Expr
}
// OrderBy is ORDER BY clause node.
//
// ORDER BY {{.Items | sqlJoin ","}}
type OrderBy struct {
// pos = Order
// end = Items[$].end
Order token.Pos // position of "ORDER" keyword
Items []*OrderByItem // len(Items) > 0
}
// OrderByItem is expression node in ORDER BY clause list.
//
// {{.Expr | sql}} {{.Collate | sqlOpt}} {{.Direction}}
type OrderByItem struct {
// pos = Expr.pos
// end = DirPos + len(Dir) || (Collate ?? Expr).end
DirPos token.Pos // position of Dir
Expr Expr
Collate *Collate // optional
Dir Direction // optional
}
// Collate is COLLATE clause node in ORDER BY item.
//
// COLLATE {{.Value | sql}}
type Collate struct {
// pos = Collate
// end = Value.end
Collate token.Pos // position of "COLLATE" keyword
Value StringValue
}
// Limit is LIMIT clause node.
//
// LIMIT {{.Count | sql}} {{.Offset | sqlOpt}}
type Limit struct {
// pos = Limit
// end = (Offset ?? Count).end
Limit token.Pos // position of "LIMIT" keyword
Count IntValue
Offset *Offset // optional
}
// Offset is OFFSET clause node in LIMIT clause.
//
// OFFSET {{.Value | sql}}
type Offset struct {
// pos = Offset
// end = Value.end
Offset token.Pos // position of "OFFSET" keyword
Value IntValue
}
// ================================================================================
//
// JOIN
//
// ================================================================================
// Unnest is UNNEST call in FROM clause.
//
// {{if .Implicit}}{{.Expr | sql}}{{else}}UNNEST({{.Expr | sql}}){{end}}
// {{.Hint | sqlOpt}}
// {{.As | sqlOpt}}
// {{.WithOffset | sqlOpt}}
// {{.Sample | sqlOpt}}
type Unnest struct {
// pos = Unnest || Expr.pos
// end = (Sample ?? WithOffset ?? As ?? Hint).end || Rparen + 1 || Expr.end
Unnest token.Pos // position of "UNNEST"
Rparen token.Pos // position of ")"
Implicit bool
Expr Expr // Path or Ident when Implicit is true
Hint *Hint // optional
As *AsAlias // optional
WithOffset *WithOffset // optional
Sample *TableSample // optional
}
// WithOffset is WITH OFFSET clause node after UNNEST call.
//
// WITH OFFSET {{.As | sqlOpt}}
type WithOffset struct {
// pos = With
// end = As.end || Offset + 6
With, Offset token.Pos // position of "WITH" and "OFFSET" keywords
As *AsAlias // optional
}
// TableName is table name node in FROM clause.
//
// {{.Table | sql}} {{.Hint | sqlOpt}} {{.As | sqlOpt}} {{.Sample | sqlOpt}}
type TableName struct {
// pos = Table.pos
// end = (Sample ?? As ?? Hint ?? Table).end
Table *Ident
Hint *Hint // optional
As *AsAlias // optional
Sample *TableSample // optional
}
// SubQueryTableExpr is subquery inside JOIN expression.
//
// ({{.Query | sql}}) {{.As | sqlOpt}} {{.Sample | sqlOpt}}
type SubQueryTableExpr struct {
// pos = Lparen
// end = (Sample ?? As).end || Rparen + 1
Lparen, Rparen token.Pos // position of "(" and ")"
Query QueryExpr
As *AsAlias // optional
Sample *TableSample // optional
}
// ParenTableExpr is parenthesized JOIN expression.
//
// ({{.Source | sql}}) {{.Sample | sqlOpt}}
type ParenTableExpr struct {
// pos = Lparen
// end = Sample.end || Rparen + 1
Lparen, Rparen token.Pos // position of "(" and ")"
Source TableExpr // SubQueryJoinExpr (without As) or Join
Sample *TableSample // optional
}
// Join is JOIN expression.
//
// {{.Left | sql}}
// {{.Op}} {{.Method}} {{.Hint | sqlOpt}}
// {{.Right | sql}}
// {{.Cond | sqlOpt}}
type Join struct {
// pos = Left.pos
// end = (Cond ?? Right).pos
Op JoinOp
Method JoinMethod
Hint *Hint // optional
Left, Right TableExpr
Cond JoinCondition // nil when Op is CrossJoin, otherwise it must be set.
}
// On is ON condition of JOIN expression.
//
// ON {{.Expr | sql}}
type On struct {
// pos = On
// end = Expr.end
On token.Pos // position of "ON" keyword
Expr Expr
}
// Using is Using condition of JOIN expression.
//
// USING ({{Idents | sqlJoin ","}})
type Using struct {
// pos = Using
// end = Rparen + 1
Using token.Pos // position of "USING" keyword
Rparen token.Pos // position of ")"
Idents []*Ident // len(Idents) > 0
}
// TableSample is TABLESAMPLE clause node.
//
// TABLESAMPLE {{.Method}} {{.Size | sql}}
type TableSample struct {
// pos = TableSample
// end = Size.end
TableSample token.Pos // position of "TABLESAMPLE" keyword
Method TableSampleMethod
Size *TableSampleSize
}
// TableSampleSize is size part of TABLESAMPLE clause.
//
// ({{.Value | sql}} {{.Unit}})
type TableSampleSize struct {
// pos = Lparen
// end = Rparen + 1
Lparen, Rparen token.Pos // position of "(" and ")"
Value NumValue
Unit TableSampleUnit
}
// ================================================================================
//
// Expr
//
// ================================================================================
// BinaryExpr is binary operator expression node.
//
// {{.Left | sql}} {{.Op}} {{.Right | sql}}
type BinaryExpr struct {
// pos = Left.pos
// end = Right.pos
Op BinaryOp
Left, Right Expr
}
// UnaryExpr is unary operator expression node.
//
// {{.Op}} {{.Expr | sql}}
type UnaryExpr struct {
// pos = OpPos
// end = Expr.end
OpPos token.Pos // position of Op
Op UnaryOp
Expr Expr
}
// InExpr is IN expression node.
//
// {{.Left | sql}} {{if .Not}}NOT{{end}} IN {{.Right | sql}}
type InExpr struct {
// pos = Left.pos
// end = Right.end
Not bool
Left Expr
Right InCondition
}
// UnnestInCondition is UNNEST call at IN condition.
//
// UNNEST({{.Expr | sql}})
type UnnestInCondition struct {
// pos = Unnest
// end = Rparen + 1
Unnest token.Pos
Rparen token.Pos
Expr Expr
}
// SubQueryInCondition is subquery at IN condition.
//
// ({{.Query | sql}})
type SubQueryInCondition struct {
// pos = Lparen
// end = Rparen + 1
Lparen, Rparen token.Pos // position of "(" and ")"
Query QueryExpr
}
// ValuesInCondition is parenthesized values at IN condition.
//
// ({{.Exprs | sqlJoin ","}})
type ValuesInCondition struct {
// pos = Lparen
// end = Rparen + 1
Lparen, Rparen token.Pos // position of "(" and ")"
Exprs []Expr // len(Exprs) > 0
}
// IsNullExpr is IS NULL expression node.
//
// {{.Left | sql}} IS {{if .Not}}NOT{{end}} NULL
type IsNullExpr struct {
// pos = Expr.pos
// end = Null + 4
Null token.Pos // position of "NULL"
Not bool
Left Expr
}
// IsBoolExpr is IS TRUE/FALSE expression node.
//
// {{.Left | sql}} IS {{if .Not}}NOT{{end}} {{if .Right}}TRUE{{else}}FALSE{{end}}
type IsBoolExpr struct {
// pos = Expr.pos
// end = RightPos + (Right ? 4 : 5)
RightPos token.Pos // position of Right
Not bool
Left Expr
Right bool
}
// BetweenExpr is BETWEEN expression node.
//
// {{.Left | sql}} {{if .Not}}NOT{{end}} BETWEEN {{.RightStart | sql}} AND {{.RightEnd | sql}}
type BetweenExpr struct {
// pos = Left.pos
// end = RightEnd.end
Not bool
Left, RightStart, RightEnd Expr
}
// SelectorExpr is struct field access expression node.
//
// {{.Expr | sql}}.{{.Ident | sql}}
type SelectorExpr struct {
// pos = Expr.pos
// end = Ident.pos
Expr Expr
Ident *Ident
}
// IndexExpr is array item access expression node.
//