forked from sensorbee/sensorbee
/
stack.go
1415 lines (1261 loc) · 41.9 KB
/
stack.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 parser
import (
"fmt"
"gopkg.in/sensorbee/sensorbee.v0/data"
)
// parseStack is a standard stack implementation, but also holds
// methods for transforming the top k elements into a new element.
type parseStack struct {
top *stackElement
size int
}
// stackElement is a stack-internal data structure that is used
// as a wrapper for the actual data.
type stackElement struct {
value *ParsedComponent
next *stackElement
}
// ParsedComponent is an element of the parse stack that represents
// a section of the input string that was successfully parsed.
type ParsedComponent struct {
// begin is the index of the first character that belongs to
// the parsed statement
begin int
// end is the index of the last character that belongs to the
// parsed statement + 1
end int
// comp stores the struct that the string was parsed into
comp interface{}
}
// Len return the stack's size.
func (ps *parseStack) Len() int {
return ps.size
}
// Push pushes a new element onto the stack.
func (ps *parseStack) Push(value *ParsedComponent) {
ps.top = &stackElement{value, ps.top}
ps.size++
}
// Pop removes the top element from the stack and returns its value.
// If the stack is empty, returns nil.
func (ps *parseStack) Pop() (value *ParsedComponent) {
if ps.size > 0 {
value, ps.top = ps.top.value, ps.top.next
ps.size--
return
}
return nil
}
// Peek returns the top element from the stack but doesn't remove it.
// If the stack is empty, returns nil.
func (ps *parseStack) Peek() (value *ParsedComponent) {
if ps.size > 0 {
return ps.top.value
}
return nil
}
// AssembleSelect takes the topmost elements from the stack, assuming
// they are components of a SELECT statement, and replaces them by
// a single SelectStmt element.
//
// EmitterAST
// HavingAST
// GroupingAST
// FilterAST
// WindowedFromAST
// ProjectionsAST
// =>
// SelectStmt{EmitterAST, ProjectionsAST, WindowedFromAST, FilterAST, GroupingAST, HavingAST}
func (ps *parseStack) AssembleSelect() {
// pop the components from the stack in reverse order
_having, _grouping, _filter, _from, _projections, _emitter := ps.pop6()
// extract and convert the contained structure
// (if this fails, this is a fundamental parser bug => panic ok)
having := _having.comp.(HavingAST)
grouping := _grouping.comp.(GroupingAST)
filter := _filter.comp.(FilterAST)
from := _from.comp.(WindowedFromAST)
projections := _projections.comp.(ProjectionsAST)
emitter := _emitter.comp.(EmitterAST)
// assemble the SelectStmt and push it back
s := SelectStmt{emitter, projections, from, filter, grouping, having}
se := ParsedComponent{_emitter.begin, _having.end, s}
ps.Push(&se)
}
// AssembleSelectUnion takes the elements from the stack that
// correspond to the input[begin:end] string and wraps a
// SelectUnionStmt struct around them.
//
// SelectStmt
// SelectStmt
// SelectStmt
// =>
// SelectUnionStmt{[SelectStmt, SelectStmt, SelectStmt]}
func (ps *parseStack) AssembleSelectUnion(begin int, end int) {
elems := ps.collectElements(begin, end)
selects := make([]SelectStmt, len(elems))
for i := range elems {
selects[i] = elems[i].(SelectStmt)
}
// push the grouped list back
ps.PushComponent(begin, end, SelectUnionStmt{selects})
}
// AssembleCreateStreamAsSelect takes the topmost elements from the stack,
// assuming they are components of a CREATE STREAM statement, and
// replaces them by a single CreateStreamAsSelectStmt element.
//
// SelectStmt
// StreamIdentifier
// =>
// CreateStreamAsSelectStmt{StreamIdentifier, SelectStmt}
func (ps *parseStack) AssembleCreateStreamAsSelect() {
// now pop the components from the stack in reverse order
_select, _name := ps.pop2()
// extract and convert the contained structure
// (if this fails, this is a fundamental parser bug => panic ok)
s := _select.comp.(SelectStmt)
name := _name.comp.(StreamIdentifier)
// assemble the SelectStmt and push it back
css := CreateStreamAsSelectStmt{name, s}
se := ParsedComponent{_name.begin, _select.end, css}
ps.Push(&se)
}
// AssembleCreateStreamAsSelectUnion takes the topmost elements from the
// stack, assuming they are components of a CREATE STREAM statement, and
// replaces them by a single CreateStreamAsSelectUnionStmt element.
//
// SelectUnionStmt
// StreamIdentifier
// =>
// CreateStreamAsSelectUnionStmt{StreamIdentifier, SelectUnionStmt}
func (ps *parseStack) AssembleCreateStreamAsSelectUnion() {
// now pop the components from the stack in reverse order
_selectUnion, _name := ps.pop2()
// extract and convert the contained structure
// (if this fails, this is a fundamental parser bug => panic ok)
selectUnion := _selectUnion.comp.(SelectUnionStmt)
name := _name.comp.(StreamIdentifier)
// assemble the SelectUnionStmt and push it back
css := CreateStreamAsSelectUnionStmt{name, selectUnion}
se := ParsedComponent{_name.begin, _selectUnion.end, css}
ps.Push(&se)
}
// AssembleCreateSource takes the topmost elements from the stack,
// assuming they are components of a CREATE SOURCE statement, and
// replaces them by a single CreateSourceStmt element.
//
// BinaryKeyword
// SourceSinkSpecsAST
// SourceSinkType
// StreamIdentifier
// =>
// CreateSourceStmt{BinaryKeyword, StreamIdentifier, SourceSinkType,
// SourceSinkSpecsAST}
func (ps *parseStack) AssembleCreateSource() {
// pop the components from the stack in reverse order
_specs, _sourceType, _name, _paused := ps.pop4()
// extract and convert the contained structure
// (if this fails, this is a fundamental parser bug => panic ok)
specs := _specs.comp.(SourceSinkSpecsAST)
sourceType := _sourceType.comp.(SourceSinkType)
name := _name.comp.(StreamIdentifier)
paused := _paused.comp.(BinaryKeyword)
// assemble the CreateSourceStmt and push it back
s := CreateSourceStmt{paused, name, sourceType, specs}
se := ParsedComponent{_paused.begin, _specs.end, s}
ps.Push(&se)
}
// AssembleCreateSink takes the topmost elements from the stack,
// assuming they are components of a CREATE SINK statement, and
// replaces them by a single CreateSinkStmt element.
//
// SourceSinkSpecsAST
// SourceSinkType
// StreamIdentifier
// =>
// CreateSinkStmt{StreamIdentifier, SourceSinkType, SourceSinkSpecsAST}
func (ps *parseStack) AssembleCreateSink() {
_specs, _sinkType, _name := ps.pop3()
specs := _specs.comp.(SourceSinkSpecsAST)
sinkType := _sinkType.comp.(SourceSinkType)
name := _name.comp.(StreamIdentifier)
s := CreateSinkStmt{name, sinkType, specs}
se := ParsedComponent{_name.begin, _specs.end, s}
ps.Push(&se)
}
// AssembleCreateState takes the topmost elements from the stack,
// assuming they are components of a CREATE STATE statement, and
// replaces them by a single CreateStateStmt element.
//
// SourceSinkSpecsAST
// SourceSinkType
// StreamIdentifier
// =>
// CreateStateStmt{StreamIdentifier, SourceSinkType, SourceSinkSpecsAST}
func (ps *parseStack) AssembleCreateState() {
_specs, _sinkType, _name := ps.pop3()
specs := _specs.comp.(SourceSinkSpecsAST)
sinkType := _sinkType.comp.(SourceSinkType)
name := _name.comp.(StreamIdentifier)
s := CreateStateStmt{name, sinkType, specs}
se := ParsedComponent{_name.begin, _specs.end, s}
ps.Push(&se)
}
// AssembleUpdateState takes the topmost elements from the stack,
// assuming they are components of a UPDATE STATE statement, and
// replaces them by a single UpdateStateStmt element.
//
// SourceSinkSpecsAST
// StreamIdentifier
// =>
// UpdateStateStmt{StreamIdentifier, SourceSinkSpecsAST}
func (ps *parseStack) AssembleUpdateState() {
_specs, _name := ps.pop2()
specs := _specs.comp.(SourceSinkSpecsAST)
name := _name.comp.(StreamIdentifier)
s := UpdateStateStmt{name, specs}
se := ParsedComponent{_name.begin, _specs.end, s}
ps.Push(&se)
}
// AssembleUpdateSource takes the topmost elements from the stack,
// assuming they are components of a UPDATE SOURCE statement, and
// replaces them by a single UpdateSourceStmt element.
//
// SourceSinkSpecsAST
// StreamIdentifier
// =>
// UpdateSourceStmt{StreamIdentifier, SourceSinkSpecsAST}
func (ps *parseStack) AssembleUpdateSource() {
_specs, _name := ps.pop2()
specs := _specs.comp.(SourceSinkSpecsAST)
name := _name.comp.(StreamIdentifier)
s := UpdateSourceStmt{name, specs}
se := ParsedComponent{_name.begin, _specs.end, s}
ps.Push(&se)
}
// AssembleUpdateSink takes the topmost elements from the stack,
// assuming they are components of a UPDATE SINK statement, and
// replaces them by a single UpdateSinkStmt element.
//
// SourceSinkSpecsAST
// StreamIdentifier
// =>
// UpdateSinkStmt{StreamIdentifier, SourceSinkSpecsAST}
func (ps *parseStack) AssembleUpdateSink() {
_specs, _name := ps.pop2()
specs := _specs.comp.(SourceSinkSpecsAST)
name := _name.comp.(StreamIdentifier)
s := UpdateSinkStmt{name, specs}
se := ParsedComponent{_name.begin, _specs.end, s}
ps.Push(&se)
}
// AssembleInsertIntoFrom takes the topmost elements from the stack,
// assuming they are components of a INSERT ... FROM ... statement, and
// replaces them by a single InsertIntoFromStmt element.
//
// StreamIdentifier
// StreamIdentifier
// =>
// InsertIntoFromStmt{StreamIdentifier, StreamIdentifier}
func (ps *parseStack) AssembleInsertIntoFrom() {
_input, _sink := ps.pop2()
input := _input.comp.(StreamIdentifier)
sink := _sink.comp.(StreamIdentifier)
s := InsertIntoFromStmt{sink, input}
se := ParsedComponent{_sink.begin, _input.end, s}
ps.Push(&se)
}
// AssemblePauseSource takes the topmost elements from the stack,
// assuming they are components of a PAUSE SOURCE statement, and
// replaces them by a single PauseSourceStmt element.
//
// StreamIdentifier
// =>
// PauseSourceStmt{StreamIdentifier}
func (ps *parseStack) AssemblePauseSource() {
// pop the components from the stack in reverse order
_name := ps.Pop()
name := _name.comp.(StreamIdentifier)
se := ParsedComponent{_name.begin, _name.end, PauseSourceStmt{name}}
ps.Push(&se)
}
// AssembleResumeSource takes the topmost elements from the stack,
// assuming they are components of a RESUME SOURCE statement, and
// replaces them by a single ResumeSourceStmt element.
//
// StreamIdentifier
// =>
// ResumeSourceStmt{StreamIdentifier}
func (ps *parseStack) AssembleResumeSource() {
// pop the components from the stack in reverse order
_name := ps.Pop()
name := _name.comp.(StreamIdentifier)
se := ParsedComponent{_name.begin, _name.end, ResumeSourceStmt{name}}
ps.Push(&se)
}
// AssembleRewindSource takes the topmost elements from the stack,
// assuming they are components of a REWIND SOURCE statement, and
// replaces them by a single RewindSourceStmt element.
//
// StreamIdentifier
// =>
// RewindSourceStmt{StreamIdentifier}
func (ps *parseStack) AssembleRewindSource() {
// pop the components from the stack in reverse order
_name := ps.Pop()
name := _name.comp.(StreamIdentifier)
se := ParsedComponent{_name.begin, _name.end, RewindSourceStmt{name}}
ps.Push(&se)
}
// AssembleDropSource takes the topmost elements from the stack,
// assuming they are components of a DROP SOURCE statement, and
// replaces them by a single DropSourceStmt element.
//
// StreamIdentifier
// =>
// DropSourceStmt{StreamIdentifier}
func (ps *parseStack) AssembleDropSource() {
// pop the components from the stack in reverse order
_name := ps.Pop()
name := _name.comp.(StreamIdentifier)
se := ParsedComponent{_name.begin, _name.end, DropSourceStmt{name}}
ps.Push(&se)
}
// AssembleDropStream takes the topmost elements from the stack,
// assuming they are components of a DROP STREAM statement, and
// replaces them by a single DropStreamStmt element.
//
// StreamIdentifier
// =>
// DropStreamStmt{StreamIdentifier}
func (ps *parseStack) AssembleDropStream() {
// pop the components from the stack in reverse order
_name := ps.Pop()
name := _name.comp.(StreamIdentifier)
se := ParsedComponent{_name.begin, _name.end, DropStreamStmt{name}}
ps.Push(&se)
}
// AssembleDropSink takes the topmost elements from the stack,
// assuming they are components of a DROP SINK statement, and
// replaces them by a single DropSinkStmt element.
//
// StreamIdentifier
// =>
// DropSinkStmt{StreamIdentifier}
func (ps *parseStack) AssembleDropSink() {
// pop the components from the stack in reverse order
_name := ps.Pop()
name := _name.comp.(StreamIdentifier)
se := ParsedComponent{_name.begin, _name.end, DropSinkStmt{name}}
ps.Push(&se)
}
// AssembleDropState takes the topmost elements from the stack,
// assuming they are components of a DROP STATE statement, and
// replaces them by a single DropStateStmt element.
//
// StreamIdentifier
// =>
// DropStateStmt{StreamIdentifier}
func (ps *parseStack) AssembleDropState() {
// pop the components from the stack in reverse order
_name := ps.Pop()
name := _name.comp.(StreamIdentifier)
se := ParsedComponent{_name.begin, _name.end, DropStateStmt{name}}
ps.Push(&se)
}
// AssembleLoadState takes the topmost elements from the stack,
// assuming they are components of a LOAD STATE statement, and
// replaces them by a single LoadStateStmt element.
//
// SourceSinkSpecsAST
// Identifier
// SourceSinkType
// StreamIdentifier
// =>
// LoadStateStmt{StreamIdentifier, SourceSinkType,
// string, SourceSinkSpecsAST}
func (ps *parseStack) AssembleLoadState() {
// pop the components from the stack in reverse order
_specs, _tag, _sinkType, _name := ps.pop4()
specs := _specs.comp.(SourceSinkSpecsAST)
tag := _tag.comp.(Identifier)
sinkType := _sinkType.comp.(SourceSinkType)
name := _name.comp.(StreamIdentifier)
s := LoadStateStmt{name, sinkType, string(tag), specs}
se := ParsedComponent{_name.begin, _specs.end, s}
ps.Push(&se)
}
// AssembleLoadStateOrCreate takes the topmost elements from the stack,
// assuming they are components of a LOAD STATE OR CREATE statement, and
// replaces them by a single LoadStateOrCreateStmt element.
//
// SourceSinkSpecsAST
// LoadStateStmt
// =>
// LoadStateOrCreateStmt{StreamIdentifier, SourceSinkType,
// string, SourceSinkSpecsAST, SourceSinkSpecsAST}
func (ps *parseStack) AssembleLoadStateOrCreate() {
// pop the components from the stack in reverse order
_createSpecs, _loadStateStmt := ps.pop2()
loadStateStmt := _loadStateStmt.comp.(LoadStateStmt)
createSpecs := _createSpecs.comp.(SourceSinkSpecsAST)
specs := loadStateStmt.SourceSinkSpecsAST
tag := loadStateStmt.Tag
sinkType := loadStateStmt.Type
name := loadStateStmt.Name
s := LoadStateOrCreateStmt{name, sinkType, tag, specs, createSpecs}
se := ParsedComponent{_loadStateStmt.begin, _createSpecs.end, s}
ps.Push(&se)
}
// AssembleSaveState takes the topmost elements from the stack,
// assuming they are components of a SAVE STATE statement, and
// replaces them by a single SaveStateStmt element.
//
// Identifier
// StreamIdentifier
// =>
// SaveStateStmt{StreamIdentifier, string}
func (ps *parseStack) AssembleSaveState() {
// pop the components from the stack in reverse order
_tag, _name := ps.pop2()
tag := _tag.comp.(Identifier)
name := _name.comp.(StreamIdentifier)
se := ParsedComponent{_name.begin, _tag.end, SaveStateStmt{name, string(tag)}}
ps.Push(&se)
}
// AssembleEval takes the topmost one or two elements from the
// stack, assuming they are components of an EVAL statement, and
// replaces them by a single EvalStmt element.
//
// Expression
// Expression
// =>
// EvalStmt{Expression, Expression}
// or
// Expression
// =>
// EvalStmt{Expression, nil}
func (ps *parseStack) AssembleEval(begin, end int) {
var exprBegin int
var expr Expression
var inputRow *MapAST
// pop a different number of items depending on whether we have the
// optional ON clause or not
if begin == end {
// the `... ON input` clause is empty
_expr := ps.Pop()
exprBegin = _expr.begin
expr = _expr.comp.(Expression)
} else {
_input, _expr := ps.pop2()
exprBegin = _expr.begin
expr = _expr.comp.(Expression)
input := _input.comp.(MapAST)
inputRow = &input
}
se := ParsedComponent{exprBegin, end, EvalStmt{expr, inputRow}}
ps.Push(&se)
}
/* Projections/Columns */
// AssembleEmitter takes the topmost elements from the stack, assuming
// they are components of a emitter clause, and replaces them by
// a single EmitterAST element.
//
// Emitter
// ...
// =>
// EmitterAST{Emitter}
func (ps *parseStack) AssembleEmitter() {
// pop the components from the stack in reverse order
_options, _emitter := ps.pop2()
emitter := _emitter.comp.(Emitter)
options := _options.comp.([]interface{})
ps.PushComponent(_emitter.begin, _options.end, EmitterAST{emitter, options})
}
// AssembleEmitterOptions takes the elements from the stack that
// correspond to the input[begin:end] string and pushes a slice
// with all of them back to the stack.
//
// Any
// Any
// Any
// =>
// []{Any, Any, Any}
func (ps *parseStack) AssembleEmitterOptions(begin int, end int) {
elems := ps.collectElements(begin, end)
if len(elems) == 0 {
elems = nil
}
// push the grouped list back
ps.PushComponent(begin, end, elems)
}
// AssembleEmitterLimit takes the topmost elements from the stack,
// assuming they are components of a emitter LIMIT option, and replaces
// them by a single EmitterLimit element.
//
// NumericLiteral
// ...
// =>
// EmitterLimit{NumericLiteral}
func (ps *parseStack) AssembleEmitterLimit() {
_limit := ps.Pop()
limit := _limit.comp.(NumericLiteral)
ps.PushComponent(_limit.begin, _limit.end, EmitterLimit{limit.Value})
}
// AssembleEmitterSampling takes the topmost elements from the stack,
// assuming they are components of a emitter EVERY/SAMPLE option, and replaces
// them by a single EmitterSampling element.
//
// NumericLiteral
// ...
// =>
// EmitterSampling{NumericLiteral, EmitterSamplingType}
func (ps *parseStack) AssembleEmitterSampling(samplingType EmitterSamplingType, factor float64) {
_value := ps.Pop()
var value float64
if num, ok := _value.comp.(NumericLiteral); ok {
value = float64(num.Value)
} else {
num := _value.comp.(FloatLiteral)
value = num.Value
}
ps.PushComponent(_value.begin, _value.end, EmitterSampling{value * factor, samplingType})
}
// AssembleProjections takes the elements from the stack that
// correspond to the input[begin:end] string and wraps a
// ProjectionsAST struct around them.
//
// Any
// Any
// Any
// =>
// ProjectionsAST{[Any, Any, Any]}
func (ps *parseStack) AssembleProjections(begin int, end int) {
elems := ps.collectElements(begin, end)
exprs := make([]Expression, len(elems))
for i := range elems {
exprs[i] = elems[i].(Expression)
}
// push the grouped list back
ps.PushComponent(begin, end, ProjectionsAST{exprs})
}
// AssembleAlias takes the topmost elements from the stack, assuming
// they are components of an AS clause, and replaces them by
// a single AliasAST element.
//
// Identifier
// Any
// =>
// AliasAST{Any, Identifier}
func (ps *parseStack) AssembleAlias() {
// pop the components from the stack in reverse order
_name, _expr := ps.pop2()
name := _name.comp.(Identifier)
expr := _expr.comp.(Expression)
ps.PushComponent(_expr.begin, _name.end, AliasAST{expr, string(name)})
}
/* FROM clause */
// AssembleWindowedFrom takes the elements from the stack that
// correspond to the input[begin:end] string, makes sure they are all
// AliasedStreamWindowAST elements and wraps a WindowedFromAST struct
// around them. If there are no such elements, adds an
// empty WindowedFromAST struct to the stack.
//
// AliasedStreamWindowAST
// AliasedStreamWindowAST
// =>
// WindowedFromAST{[AliasedStreamWindowAST, AliasedStreamWindowAST]}
func (ps *parseStack) AssembleWindowedFrom(begin int, end int) {
if begin == end {
// push an empty FROM clause
ps.PushComponent(begin, end, WindowedFromAST{})
} else {
elems := ps.collectElements(begin, end)
rels := make([]AliasedStreamWindowAST, len(elems), len(elems))
for i, elem := range elems {
// (if this conversion fails, this is a fundamental parser bug)
e := elem.(AliasedStreamWindowAST)
rels[i] = e
}
// push the grouped list back
ps.PushComponent(begin, end, WindowedFromAST{rels})
}
}
// AssembleInterval takes the topmost elements from the stack, assuming
// they are components of a RANGE clause, and replaces them by
// a single IntervalAST element.
//
// IntervalUnit
// NumericLiteral
// =>
// IntervalAST{FloatLiteral, IntervalUnit}
// or
// IntervalUnit
// FloatLiteral
// =>
// IntervalAST{FloatLiteral, IntervalUnit}
func (ps *parseStack) AssembleInterval() {
// pop the components from the stack in reverse order
_unit, _num := ps.pop2()
// extract and convert the contained structure
// (if this fails, this is a fundamental parser bug => panic ok)
unit := _unit.comp.(IntervalUnit)
var val float64
if num, ok := _num.comp.(NumericLiteral); ok {
val = float64(num.Value)
} else {
num := _num.comp.(FloatLiteral)
val = num.Value
}
// assemble the IntervalAST and push it back
ps.PushComponent(_num.begin, _unit.end, IntervalAST{FloatLiteral{val}, unit})
}
/* WHERE clause */
// AssembleFilter takes the expression on top of the stack
// (if there is a WHERE clause) and wraps a FilterAST struct
// around it. If there is no WHERE clause, an empty FilterAST
// struct is used.
//
// Any
// =>
// FilterAST{Any}
func (ps *parseStack) AssembleFilter(begin int, end int) {
if begin == end {
// push an empty from clause
ps.PushComponent(begin, end, FilterAST{})
} else {
// if the stack is empty at this point, this is
// a serious parser bug
f := ps.Pop()
if begin > f.begin || end < f.end {
panic("the item on top of the stack is not within given range")
}
ps.PushComponent(begin, end, FilterAST{f.comp.(Expression)})
}
}
/* GROUP BY clause */
// AssembleGrouping takes the elements from the stack that
// correspond to the input[begin:end] string and wraps a
// GroupingAST struct around them. If there are no such elements,
// adds an empty GroupingAST struct to the stack.
//
// Any
// Any
// Any
// =>
// GroupingAST{[Any, Any, Any]}
func (ps *parseStack) AssembleGrouping(begin int, end int) {
elems := ps.collectElements(begin, end)
var exprs []Expression
if len(elems) > 0 {
exprs = make([]Expression, len(elems))
}
for i := range elems {
exprs[i] = elems[i].(Expression)
}
// push the grouped list back
ps.PushComponent(begin, end, GroupingAST{exprs})
}
/* HAVING clause */
// AssembleHaving takes the expression on top of the stack
// (if there is a HAVING clause) and wraps a HavingAST struct
// around it. If there is no HAVING clause, an empty HavingAST
// struct is used.
//
// Any
// =>
// HavingAST{Any}
func (ps *parseStack) AssembleHaving(begin int, end int) {
if begin == end {
// push an empty from clause
ps.PushComponent(begin, end, HavingAST{})
} else {
// if the stack is empty at this point, this is
// a serious parser bug
h := ps.Pop()
if begin > h.begin || end < h.end {
panic("the item on top of the stack is not within given range")
}
ps.PushComponent(begin, end, HavingAST{h.comp.(Expression)})
}
}
// AssembleAliasedStreamWindow takes the topmost elements from the stack, assuming
// they are components of an AS clause, and replaces them by
// a single AliasedStreamWindowAST element.
//
// Identifier
// StreamWindowAST
// =>
// AliasedStreamWindowAST{StreamWindowAST, Identifier}
func (ps *parseStack) AssembleAliasedStreamWindow() {
// pop the components from the stack in reverse order
_name, _rel := ps.pop2()
name := _name.comp.(Identifier)
rel := _rel.comp.(StreamWindowAST)
ps.PushComponent(_rel.begin, _name.end, AliasedStreamWindowAST{rel, string(name)})
}
// EnsureAliasedStreamWindow takes the top element from the stack. If it is a
// StreamWindowAST element, it wraps it into an AliasedStreamWindowAST struct; if it
// is already an AliasedStreamWindowAST it just pushes it back. This helps to
// ensure we only deal with AliasedStreamWindowAST objects in the collection step.
func (ps *parseStack) EnsureAliasedStreamWindow() {
_elem := ps.Pop()
elem := _elem.comp
var aliasRel AliasedStreamWindowAST
e, ok := elem.(AliasedStreamWindowAST)
if ok {
aliasRel = e
} else {
e := elem.(StreamWindowAST)
aliasRel = AliasedStreamWindowAST{e, ""}
}
ps.PushComponent(_elem.begin, _elem.end, aliasRel)
}
// AssembleStreamWindow takes the topmost elements from the stack, assuming
// they are components of an AS clause, and replaces them by
// a single StreamWindowAST element.
//
// IntervalAST
// Stream
// =>
// StreamWindowAST{Stream, IntervalAST}
func (ps *parseStack) AssembleStreamWindow() {
// pop the components from the stack in reverse order
_shedding, _capacity, _range, _rel := ps.pop4()
rel := _rel.comp.(Stream)
rangeAst := _range.comp.(IntervalAST)
capacity := _capacity.comp.(NumericLiteral)
shedding := _shedding.comp.(SheddingOption)
ps.PushComponent(_rel.begin, _shedding.end, StreamWindowAST{rel, rangeAst,
capacity.Value, shedding})
}
// AssembleUDSFFuncApp takes the topmost elements from the stack,
// assuming they are components of a UDSF clause, and
// replaces them by a single Stream element.
//
// FuncAppAST{Function, ExpressionsAST}
// =>
// Stream{UDSFStream, Function, ExpressionAST.Expressions}
func (ps *parseStack) AssembleUDSFFuncApp() {
_fun := ps.Pop()
fun := _fun.comp.(FuncAppAST)
se := ParsedComponent{_fun.begin, _fun.end,
Stream{UDSFStream, string(fun.Function), fun.Expressions}}
ps.Push(&se)
}
// EnsureCapacitySpec makes sure that the top element of the stack
// is a NumericLiteral element.
func (ps *parseStack) EnsureCapacitySpec(begin int, end int) {
top := ps.Peek()
if top == nil || top.end <= begin {
// there is no item in the given range
ps.PushComponent(begin, end, NumericLiteral{UnspecifiedCapacity})
} else {
// there is an item in the given range
_, ok := top.comp.(NumericLiteral)
if !ok {
panic(fmt.Sprintf("begin (%d) != end (%d), but there "+
"was a %T on the stack", begin, end, top.comp))
}
}
}
// EnsureSheddingSpec makes sure that the top element of the stack
// is a SheddingOption element.
func (ps *parseStack) EnsureSheddingSpec(begin int, end int) {
top := ps.Peek()
if top == nil || top.end <= begin {
// there is no item in the given range
ps.PushComponent(begin, end, UnspecifiedSheddingOption)
} else {
// there is an item in the given range
_, ok := top.comp.(SheddingOption)
if !ok {
panic(fmt.Sprintf("begin (%d) != end (%d), but there "+
"was a %T on the stack", begin, end, top.comp))
}
}
}
// AssembleSourceSinkSpecs takes the elements from the stack that
// correspond to the input[begin:end] string, makes sure
// they are all SourceSinkParamAST elements and wraps a SourceSinkSpecsAST
// struct around them. If there are no such elements, adds an
// empty SourceSpecAST struct to the stack.
//
// SourceSinkParamAST
// SourceSinkParamAST
// SourceSinkParamAST
// =>
// SourceSinkSpecsAST{[SourceSpecAST, SourceSpecAST, SourceSpecAST]}
func (ps *parseStack) AssembleSourceSinkSpecs(begin int, end int) {
if begin == end {
// push an empty from clause
ps.PushComponent(begin, end, SourceSinkSpecsAST{})
} else {
elems := ps.collectElements(begin, end)
params := make([]SourceSinkParamAST, len(elems), len(elems))
for i, elem := range elems {
// (if this conversion fails, this is a fundamental parser bug)
e := elem.(SourceSinkParamAST)
params[i] = e
}
// push the grouped list back
ps.PushComponent(begin, end, SourceSinkSpecsAST{params})
}
}
// AssembleSourceSinkParam takes the topmost elements from the
// stack, assuming they are part of a WITH clause in a CREATE SOURCE
// statement and replaces them by a single SourceSinkParamAST element.
//
// Any
// SourceSinkParamKey
// =>
// SourceSinkParamAST{SourceSinkParamKey, data.Value}
func (ps *parseStack) AssembleSourceSinkParam() {
_value, _key := ps.pop2()
var toValue func(obj interface{}) data.Value
toValue = func(obj interface{}) data.Value {
var value data.Value
switch lit := obj.(type) {
default:
panic(fmt.Sprintf("cannot deal with a %T here", lit))
case StringLiteral:
value = data.String(lit.Value)
case BoolLiteral:
value = data.Bool(lit.Value)
case NumericLiteral:
value = data.Int(lit.Value)
case FloatLiteral:
value = data.Float(lit.Value)
case ArrayAST:
arr := make(data.Array, len(lit.Expressions))
for i, item := range lit.Expressions {
arr[i] = toValue(item)
}
value = arr
case MapAST:
m := data.Map{}
for _, item := range lit.Entries {
m[item.Key] = toValue(item.Value)
}
value = m
}
return value
}
value := toValue(_value.comp)
key := _key.comp.(SourceSinkParamKey)
ss := SourceSinkParamAST{key, value}
ps.PushComponent(_key.begin, _value.end, ss)
}
// EnsureIdentifier makes sure that the top element of the stack
// is an Identifier element. If there was no Identifier on top,
// an empty Identifier is pushed there. Note that since regular
// identifiers must always be empty, an empty identifier is
// semantically equivalent to "not given".
func (ps *parseStack) EnsureIdentifier(begin int, end int) {
top := ps.Peek()
if top == nil || top.end <= begin {
// there is no item in the given range
ps.PushComponent(begin, end, Identifier(""))
} else {
// there is an item in the given range, don't touch it
_, ok := top.comp.(Identifier)
if !ok {
panic(fmt.Sprintf("begin (%d) != end (%d), but there "+
"was a %T on the stack", begin, end, top.comp))
}
}
}
// EnsureKeywordPresent makes sure that the top element of the stack
// is a BinaryKeyword element.
func (ps *parseStack) EnsureKeywordPresent(begin int, end int) {
top := ps.Peek()
if top == nil || top.end <= begin {
// there is no item in the given range
ps.PushComponent(begin, end, UnspecifiedKeyword)
} else {
// there is an item in the given range
_, ok := top.comp.(BinaryKeyword)
if !ok {
panic(fmt.Sprintf("begin (%d) != end (%d), but there "+
"was a %T on the stack", begin, end, top.comp))
}
}