-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathn1ql.y
2390 lines (2170 loc) · 33.4 KB
/
n1ql.y
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 n1ql
import "fmt"
import "strings"
import "github.com/couchbaselabs/clog"
import "github.com/couchbaselabs/query/algebra"
import "github.com/couchbaselabs/query/datastore"
import "github.com/couchbaselabs/query/expression"
import "github.com/couchbaselabs/query/value"
func logDebugGrammar(format string, v ...interface{}) {
clog.To("PARSER", format, v...)
}
%}
%union {
s string
n int
f float64
b bool
ss []string
expr expression.Expression
exprs expression.Expressions
whenTerm *expression.WhenTerm
whenTerms expression.WhenTerms
binding *expression.Binding
bindings expression.Bindings
node algebra.Node
statement algebra.Statement
fullselect *algebra.Select
subresult algebra.Subresult
subselect *algebra.Subselect
fromTerm algebra.FromTerm
keyspaceTerm *algebra.KeyspaceTerm
subqueryTerm *algebra.SubqueryTerm
path expression.Path
group *algebra.Group
resultTerm *algebra.ResultTerm
resultTerms algebra.ResultTerms
projection *algebra.Projection
order *algebra.Order
sortTerm *algebra.SortTerm
sortTerms algebra.SortTerms
keyspaceRef *algebra.KeyspaceRef
pairs algebra.Pairs
set *algebra.Set
unset *algebra.Unset
setTerm *algebra.SetTerm
setTerms algebra.SetTerms
unsetTerm *algebra.UnsetTerm
unsetTerms algebra.UnsetTerms
updateFor *algebra.UpdateFor
mergeActions *algebra.MergeActions
mergeUpdate *algebra.MergeUpdate
mergeDelete *algebra.MergeDelete
mergeInsert *algebra.MergeInsert
createIndex *algebra.CreateIndex
dropIndex *algebra.DropIndex
alterIndex *algebra.AlterIndex
indexType datastore.IndexType
val value.Value
}
%token ALL
%token ALTER
%token ANALYZE
%token AND
%token ANY
%token ARRAY
%token AS
%token ASC
%token BEGIN
%token BETWEEN
%token BINARY
%token BOOLEAN
%token BREAK
%token BUCKET
%token BUILD
%token BY
%token CALL
%token CASE
%token CAST
%token CLUSTER
%token COLLATE
%token COLLECTION
%token COMMIT
%token CONNECT
%token CONTINUE
%token CREATE
%token DATABASE
%token DATASET
%token DATASTORE
%token DECLARE
%token DECREMENT
%token DELETE
%token DERIVED
%token DESC
%token DESCRIBE
%token DISTINCT
%token DO
%token DROP
%token EACH
%token ELEMENT
%token ELSE
%token END
%token EVERY
%token EXCEPT
%token EXCLUDE
%token EXECUTE
%token EXISTS
%token EXPLAIN
%token FALSE
%token FIRST
%token FLATTEN
%token FOR
%token FROM
%token FUNCTION
%token GRANT
%token GROUP
%token GSI
%token HAVING
%token IF
%token IN
%token INCLUDE
%token INCREMENT
%token INDEX
%token INLINE
%token INNER
%token INSERT
%token INTERSECT
%token INTO
%token IS
%token JOIN
%token KEY
%token KEYS
%token KEYSPACE
%token LAST
%token LEFT
%token LET
%token LETTING
%token LIKE
%token LIMIT
%token LSM
%token MAP
%token MAPPING
%token MATCHED
%token MATERIALIZED
%token MERGE
%token MINUS
%token MISSING
%token NAMESPACE
%token NEST
%token NOT
%token NULL
%token NUMBER
%token OBJECT
%token OFFSET
%token ON
%token OPTION
%token OR
%token ORDER
%token OUTER
%token OVER
%token PARTITION
%token PASSWORD
%token PATH
%token POOL
%token PREPARE
%token PRIMARY
%token PRIVATE
%token PRIVILEGE
%token PROCEDURE
%token PUBLIC
%token RAW
%token REALM
%token REDUCE
%token RENAME
%token RETURN
%token RETURNING
%token REVOKE
%token RIGHT
%token ROLE
%token ROLLBACK
%token SATISFIES
%token SCHEMA
%token SELECT
%token SELF
%token SET
%token SHOW
%token SOME
%token START
%token STATISTICS
%token STRING
%token SYSTEM
%token THEN
%token TO
%token TRANSACTION
%token TRIGGER
%token TRUE
%token TRUNCATE
%token UNDER
%token UNION
%token UNIQUE
%token UNNEST
%token UNSET
%token UPDATE
%token UPSERT
%token USE
%token USER
%token USING
%token VALUE
%token VALUED
%token VALUES
%token VIEW
%token WHEN
%token WHERE
%token WHILE
%token WITH
%token WITHIN
%token WORK
%token XOR
%token INT NUMBER STRING IDENTIFIER IDENTIFIER_ICASE NAMED_PARAM POSITIONAL_PARAM NEXT_PARAM
%token LPAREN RPAREN
%token LBRACE RBRACE LBRACKET RBRACKET RBRACKET_ICASE
%token COMMA COLON
/* Precedence: lowest to highest */
%left ORDER
%left UNION INTERESECT EXCEPT
%left JOIN NEST UNNEST FLATTEN INNER LEFT
%left OR
%left AND
%right NOT
%nonassoc EQ DEQ NE
%nonassoc LT GT LE GE
%nonassoc LIKE
%nonassoc BETWEEN
%nonassoc IN WITHIN
%nonassoc EXISTS
%nonassoc IS /* IS NULL, IS MISSING, IS VALUED, IS NOT NULL, etc. */
%left CONCAT
%left PLUS MINUS
%left STAR DIV MOD
/* Unary operators */
%right UMINUS
%left DOT LBRACKET RBRACKET
/* Override precedence */
%left LPAREN RPAREN
/* Types */
%type <s> STRING
%type <s> IDENTIFIER IDENTIFIER_ICASE
%type <s> NAMED_PARAM
%type <f> NUMBER
%type <n> INT
%type <n> POSITIONAL_PARAM NEXT_PARAM
%type <expr> literal construction_expr object array
%type <expr> param_expr
%type <binding> member
%type <bindings> members opt_members
%type <expr> expr c_expr b_expr
%type <exprs> exprs opt_exprs
%type <binding> binding
%type <bindings> bindings
%type <s> alias as_alias opt_as_alias variable
%type <expr> case_expr simple_or_searched_case simple_case searched_case opt_else
%type <whenTerms> when_thens
%type <expr> collection_expr collection_cond collection_xform
%type <binding> coll_binding
%type <bindings> coll_bindings
%type <expr> satisfies
%type <expr> opt_when
%type <expr> function_expr
%type <s> function_name
%type <expr> paren_or_subquery_expr paren_or_subquery
%type <fullselect> fullselect
%type <subresult> subselects
%type <subselect> subselect
%type <subselect> select_from
%type <subselect> from_select
%type <fromTerm> from_term from opt_from
%type <keyspaceTerm> keyspace_term join_term
%type <subqueryTerm> subquery_term
%type <b> opt_join_type
%type <path> path opt_subpath
%type <s> namespace_name keyspace_name
%type <expr> use_keys opt_use_keys on_keys
%type <bindings> opt_let let
%type <expr> opt_where where
%type <group> opt_group group
%type <bindings> opt_letting letting
%type <expr> opt_having having
%type <resultTerm> project
%type <resultTerms> projects
%type <projection> projection select_clause
%type <order> order_by opt_order_by
%type <sortTerm> sort_term
%type <sortTerms> sort_terms
%type <expr> limit opt_limit
%type <expr> offset opt_offset
%type <b> dir opt_dir
%type <statement> stmt explain prepare execute select_stmt dml_stmt ddl_stmt
%type <statement> insert upsert delete update merge
%type <statement> index_stmt create_index drop_index alter_index build_index
%type <keyspaceRef> keyspace_ref
%type <pairs> values values_list
%type <expr> key_expr opt_value_expr
%type <projection> returns returning opt_returning
%type <binding> update_binding
%type <bindings> update_bindings
%type <expr> path_expr
%type <set> set
%type <setTerm> set_term
%type <setTerms> set_terms
%type <unset> unset
%type <unsetTerm> unset_term
%type <unsetTerms> unset_terms
%type <updateFor> update_for opt_update_for
%type <binding> update_binding
%type <bindings> update_bindings
%type <mergeActions> merge_actions opt_merge_delete_insert
%type <mergeUpdate> merge_update
%type <mergeDelete> merge_delete
%type <mergeInsert> merge_insert opt_merge_insert
%type <s> index_name opt_primary_name
%type <ss> index_names
%type <keyspaceRef> named_keyspace_ref
%type <expr> index_partition
%type <indexType> index_using opt_index_using
%type <val> index_with opt_index_with
%type <s> rename
%type <expr> index_expr index_where
%type <exprs> index_exprs
%start input
%%
input:
stmt
{
yylex.(*lexer).setStatement($1)
}
|
expr
{
yylex.(*lexer).setExpression($1)
}
;
stmt:
explain
|
prepare
|
execute
|
select_stmt
|
dml_stmt
|
ddl_stmt
;
explain:
EXPLAIN stmt
{
$$ = algebra.NewExplain($2)
}
;
prepare:
PREPARE stmt
{
$$ = algebra.NewPrepare($2)
}
;
execute:
EXECUTE object
{
$$ = algebra.NewExecute($2)
}
;
select_stmt:
fullselect
{
$$ = $1
}
;
dml_stmt:
insert
|
upsert
|
delete
|
update
|
merge
;
ddl_stmt:
index_stmt
;
index_stmt:
create_index
|
drop_index
|
alter_index
|
build_index
;
fullselect:
subselects opt_order_by
{
$$ = algebra.NewSelect($1, $2, nil, nil) /* OFFSET precedes LIMIT */
}
|
subselects opt_order_by limit opt_offset
{
$$ = algebra.NewSelect($1, $2, $4, $3) /* OFFSET precedes LIMIT */
}
|
subselects opt_order_by offset opt_limit
{
$$ = algebra.NewSelect($1, $2, $3, $4) /* OFFSET precedes LIMIT */
}
;
subselects:
subselect
{
$$ = $1
}
|
subselects UNION subselect
{
$$ = algebra.NewUnion($1, $3)
}
|
subselects UNION ALL subselect
{
$$ = algebra.NewUnionAll($1, $4)
}
|
subselects INTERSECT subselect
{
$$ = algebra.NewIntersect($1, $3)
}
|
subselects INTERSECT ALL subselect
{
$$ = algebra.NewIntersectAll($1, $4)
}
|
subselects EXCEPT subselect
{
$$ = algebra.NewExcept($1, $3)
}
|
subselects EXCEPT ALL subselect
{
$$ = algebra.NewExceptAll($1, $4)
}
;
subselect:
from_select
|
select_from
;
from_select:
from opt_let opt_where opt_group select_clause
{
$$ = algebra.NewSubselect($1, $2, $3, $4, $5)
}
;
select_from:
select_clause opt_from opt_let opt_where opt_group
{
$$ = algebra.NewSubselect($2, $3, $4, $5, $1)
}
;
/*************************************************
*
* SELECT clause
*
*************************************************/
select_clause:
SELECT
projection
{
$$ = $2
}
;
projection:
projects
{
$$ = algebra.NewProjection(false, $1)
}
|
DISTINCT projects
{
$$ = algebra.NewProjection(true, $2)
}
|
ALL projects
{
$$ = algebra.NewProjection(false, $2)
}
|
raw expr opt_as_alias
{
$$ = algebra.NewRawProjection(false, $2, $3)
}
|
DISTINCT raw expr opt_as_alias
{
$$ = algebra.NewRawProjection(true, $3, $4)
}
;
raw:
RAW
|
ELEMENT
;
projects:
project
{
$$ = algebra.ResultTerms{$1}
}
|
projects COMMA project
{
$$ = append($1, $3)
}
;
project:
STAR
{
$$ = algebra.NewResultTerm(nil, true, "")
}
|
expr DOT STAR
{
$$ = algebra.NewResultTerm($1, true, "")
}
|
expr opt_as_alias
{
$$ = algebra.NewResultTerm($1, false, $2)
}
;
opt_as_alias:
/* empty */
{
$$ = ""
}
|
as_alias
;
as_alias:
alias
|
AS alias
{
$$ = $2
}
;
alias:
IDENTIFIER
;
/*************************************************
*
* FROM clause
*
*************************************************/
opt_from:
/* empty */
{
$$ = nil
}
|
from
;
from:
FROM from_term
{
$$ = $2
}
;
from_term:
keyspace_term
{
$$ = $1
}
|
subquery_term
{
$$ = $1
}
|
from_term opt_join_type JOIN join_term
{
$$ = algebra.NewJoin($1, $2, $4)
}
|
from_term opt_join_type NEST join_term
{
$$ = algebra.NewNest($1, $2, $4)
}
|
from_term opt_join_type unnest expr opt_as_alias
{
$$ = algebra.NewUnnest($1, $2, $4, $5)
}
;
unnest:
UNNEST
|
FLATTEN
;
keyspace_term:
keyspace_name opt_subpath opt_as_alias opt_use_keys
{
$$ = algebra.NewKeyspaceTerm("", $1, $2, $3, $4)
}
|
namespace_name COLON keyspace_name opt_subpath opt_as_alias opt_use_keys
{
$$ = algebra.NewKeyspaceTerm($1, $3, $4, $5, $6)
}
|
SYSTEM COLON keyspace_name opt_subpath opt_as_alias opt_use_keys
{
$$ = algebra.NewKeyspaceTerm("#system", $3, $4, $5, $6)
}
;
subquery_term:
LPAREN fullselect RPAREN opt_as_alias
{
if $4 == "" {
yylex.Error("Subquery in FROM clause must have an alias.")
} else {
$$ = algebra.NewSubqueryTerm($2, $4)
}
}
;
join_term:
keyspace_name opt_subpath opt_as_alias on_keys
{
$$ = algebra.NewKeyspaceTerm("", $1, $2, $3, $4)
}
|
namespace_name COLON keyspace_name opt_subpath opt_as_alias on_keys
{
$$ = algebra.NewKeyspaceTerm($1, $3, $4, $5, $6)
}
|
SYSTEM COLON keyspace_name opt_subpath opt_as_alias on_keys
{
$$ = algebra.NewKeyspaceTerm("#system", $3, $4, $5, $6)
}
;
namespace_name:
IDENTIFIER
;
keyspace_name:
IDENTIFIER
;
opt_subpath:
/* empty */
{
$$ = nil
}
|
DOT path
{
$$ = $2
}
;
opt_use_keys:
/* empty */
{
$$ = nil
}
|
use_keys
;
use_keys:
USE opt_primary KEYS expr
{
$$ = $4
}
;
opt_primary:
/* empty */
{
}
|
PRIMARY
;
opt_join_type:
/* empty */
{
$$ = false
}
|
INNER
{
$$ = false
}
|
LEFT opt_outer
{
$$ = true
}
;
opt_outer:
/* empty */
|
OUTER
;
on_keys:
ON opt_primary KEYS expr
{
$$ = $4
}
;
/*************************************************
*
* LET clause
*
*************************************************/
opt_let:
/* empty */
{
$$ = nil
}
|
let
;
let:
LET bindings
{
$$ = $2
}
;
bindings:
binding
{
$$ = expression.Bindings{$1}
}
|
bindings COMMA binding
{
$$ = append($1, $3)
}
;
binding:
alias EQ expr
{
$$ = expression.NewBinding($1, $3)
}
;
/*************************************************
*
* WHERE clause
*
*************************************************/
opt_where:
/* empty */
{
$$ = nil
}
|
where
;
where:
WHERE expr
{
$$ = $2
}
;
/*************************************************
*
* GROUP BY clause
*
*************************************************/
opt_group:
/* empty */
{
$$ = nil
}
|
group
;
group:
GROUP BY exprs opt_letting opt_having
{
$$ = algebra.NewGroup($3, $4, $5)
}
|
letting
{
$$ = algebra.NewGroup(nil, $1, nil)
}
;
exprs:
expr
{
$$ = expression.Expressions{$1}
}
|
exprs COMMA expr
{
$$ = append($1, $3)
}
;
opt_letting:
/* empty */
{
$$ = nil
}
|
letting
;
letting:
LETTING bindings
{
$$ = $2
}
;
opt_having:
/* empty */
{
$$ = nil
}
|
having
;
having:
HAVING expr
{
$$ = $2
}
;
/*************************************************
*
* ORDER BY clause
*
*************************************************/
opt_order_by:
/* empty */
{
$$ = nil
}
|
order_by
;
order_by:
ORDER BY sort_terms
{
$$ = algebra.NewOrder($3)
}
;
sort_terms:
sort_term
{
$$ = algebra.SortTerms{$1}
}
|
sort_terms COMMA sort_term
{
$$ = append($1, $3)
}
;
sort_term:
expr opt_dir
{
$$ = algebra.NewSortTerm($1, $2)
}
;
opt_dir:
/* empty */
{
$$ = false
}
|
dir
;
dir:
ASC
{
$$ = false
}
|
DESC
{
$$ = true
}
;
/*************************************************
*
* LIMIT clause
*
*************************************************/
opt_limit:
/* empty */
{
$$ = nil
}
|
limit