-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Cql.g
2453 lines (2224 loc) · 94 KB
/
Cql.g
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
/*
* SPDX-License-Identifier: Apache-2.0
*/
grammar Cql;
options {
language = Cpp;
}
@parser::namespace{cql3_parser}
@lexer::includes {
#include "cql3/error_collector.hh"
#include "cql3/error_listener.hh"
}
@parser::includes {
#include "cql3/statements/raw/parsed_statement.hh"
#include "cql3/statements/raw/select_statement.hh"
#include "cql3/statements/alter_keyspace_statement.hh"
#include "cql3/statements/alter_table_statement.hh"
#include "cql3/statements/alter_view_statement.hh"
#include "cql3/statements/alter_service_level_statement.hh"
#include "cql3/statements/create_keyspace_statement.hh"
#include "cql3/statements/drop_keyspace_statement.hh"
#include "cql3/statements/create_index_statement.hh"
#include "cql3/statements/create_table_statement.hh"
#include "cql3/statements/create_view_statement.hh"
#include "cql3/statements/create_type_statement.hh"
#include "cql3/statements/create_function_statement.hh"
#include "cql3/statements/create_aggregate_statement.hh"
#include "cql3/statements/create_service_level_statement.hh"
#include "cql3/statements/sl_prop_defs.hh"
#include "cql3/statements/attach_service_level_statement.hh"
#include "cql3/statements/drop_type_statement.hh"
#include "cql3/statements/alter_type_statement.hh"
#include "cql3/statements/property_definitions.hh"
#include "cql3/statements/drop_index_statement.hh"
#include "cql3/statements/drop_table_statement.hh"
#include "cql3/statements/drop_view_statement.hh"
#include "cql3/statements/drop_function_statement.hh"
#include "cql3/statements/drop_aggregate_statement.hh"
#include "cql3/statements/drop_service_level_statement.hh"
#include "cql3/statements/detach_service_level_statement.hh"
#include "cql3/statements/raw/truncate_statement.hh"
#include "cql3/statements/raw/update_statement.hh"
#include "cql3/statements/raw/insert_statement.hh"
#include "cql3/statements/raw/delete_statement.hh"
#include "cql3/statements/index_prop_defs.hh"
#include "cql3/statements/raw/use_statement.hh"
#include "cql3/statements/raw/batch_statement.hh"
#include "cql3/statements/raw/describe_statement.hh"
#include "cql3/statements/list_users_statement.hh"
#include "cql3/statements/grant_statement.hh"
#include "cql3/statements/revoke_statement.hh"
#include "cql3/statements/list_permissions_statement.hh"
#include "cql3/statements/alter_role_statement.hh"
#include "cql3/statements/list_roles_statement.hh"
#include "cql3/statements/list_service_level_statement.hh"
#include "cql3/statements/list_service_level_attachments_statement.hh"
#include "cql3/statements/grant_role_statement.hh"
#include "cql3/statements/revoke_role_statement.hh"
#include "cql3/statements/drop_role_statement.hh"
#include "cql3/statements/create_role_statement.hh"
#include "cql3/statements/index_target.hh"
#include "cql3/statements/ks_prop_defs.hh"
#include "cql3/selection/raw_selector.hh"
#include "cql3/selection/selectable-expr.hh"
#include "cql3/keyspace_element_name.hh"
#include "cql3/constants.hh"
#include "cql3/operation_impl.hh"
#include "cql3/error_listener.hh"
#include "cql3/index_name.hh"
#include "cql3/cql3_type.hh"
#include "cql3/cf_name.hh"
#include "cql3/maps.hh"
#include "cql3/sets.hh"
#include "cql3/lists.hh"
#include "cql3/role_name.hh"
#include "cql3/role_options.hh"
#include "cql3/user_types.hh"
#include "cql3/ut_name.hh"
#include "cql3/functions/function_name.hh"
#include "cql3/expr/expression.hh"
#include <seastar/core/sstring.hh>
#include "CqlLexer.hpp"
#include <algorithm>
#include <unordered_map>
#include <map>
}
@parser::traits {
using namespace cql3;
using namespace cql3::statements;
using namespace cql3::selection;
using namespace cql3::expr;
using cql3::cql3_type;
using operations_type = std::vector<std::pair<::shared_ptr<cql3::column_identifier::raw>, std::unique_ptr<cql3::operation::raw_update>>>;
// ANTLR forces us to define a default-initialized return value
// for every rule (e.g. [returns ut_name name]), but not every type
// can be naturally zero-initialized.
//
// The uninitialized<T> wrapper can be zero-initialized, and is convertible
// to T (after checking that it was assigned to) implicitly, eliminating the
// problem. It is up to the user to ensure it is actually assigned to.
template <typename T>
struct uninitialized {
std::optional<T> _val;
uninitialized() = default;
uninitialized(const uninitialized&) = default;
uninitialized(uninitialized&&) = default;
uninitialized(const T& val) : _val(val) {}
uninitialized(T&& val) : _val(std::move(val)) {}
uninitialized(std::convertible_to<T> auto&& x) : _val(std::forward<decltype(x)>(x)) {}
uninitialized& operator=(const uninitialized&) = default;
uninitialized& operator=(uninitialized&&) = default;
operator T&&() && { return check(), std::move(*_val); }
operator std::optional<T>&&() && { return check(), std::move(_val); }
void check() const { if (!_val) { throw std::runtime_error("not intitialized"); } }
};
template <typename T>
struct unwrap_uninitialized {
using type = T;
};
template <typename T>
struct unwrap_uninitialized<uninitialized<T>> {
using type = T;
};
template <typename T>
using unwrap_uninitialized_t = typename unwrap_uninitialized<T>::type;
using uexpression = uninitialized<expression>;
}
@context {
using collector_type = cql3::error_collector<ComponentType, ExceptionBaseType::TokenType, ExceptionBaseType>;
using listener_type = cql3::error_listener<ComponentType, ExceptionBaseType>;
listener_type* listener;
std::vector<::shared_ptr<cql3::column_identifier>> _bind_variables;
// index into _bind_variables
std::unordered_map<cql3::column_identifier, size_t> _named_bind_variables_indexes;
std::vector<std::unique_ptr<TokenType>> _missing_tokens;
// Can't use static variable, since it needs to be defined out-of-line
static const std::unordered_set<sstring>& _reserved_type_names() {
static std::unordered_set<sstring> s = {
"byte",
"smallint",
"complex",
"enum",
"date",
"interval",
"macaddr",
"bitstring",
};
return s;
}
bind_variable new_bind_variables(shared_ptr<cql3::column_identifier> name)
{
if (name && _named_bind_variables_indexes.contains(*name)) {
return bind_variable{_named_bind_variables_indexes[*name]};
}
auto marker = bind_variable{_bind_variables.size()};
_bind_variables.push_back(name);
if (name) {
_named_bind_variables_indexes[*name] = marker.bind_index;
}
return marker;
}
void set_error_listener(listener_type& listener) {
this->listener = &listener;
}
void displayRecognitionError(ANTLR_UINT8** token_names, ExceptionBaseType* ex)
{
listener->syntax_error(*this, token_names, ex);
}
void add_recognition_error(const sstring& msg) {
listener->syntax_error(*this, msg);
}
bool is_eof_token(CommonTokenType token) const
{
return token == CommonTokenType::TOKEN_EOF;
}
std::string token_text(const TokenType* token)
{
if (!token) {
return "";
}
return token->getText();
}
std::map<sstring, sstring> convert_property_map(const collection_constructor& map) {
if (map.elements.empty()) {
return std::map<sstring, sstring>{};
}
std::map<sstring, sstring> res;
for (auto&& entry : map.elements) {
auto entry_tuple = expr::as_if<tuple_constructor>(&entry);
// Because the parser tries to be smart and recover on error (to
// allow displaying more than one error I suppose), we have default-constructed
// entries in map.elements. Just skip those, a proper error will be thrown in the end.
if (!entry_tuple || entry_tuple->elements.size() != 2) {
break;
}
auto left = expr::as_if<untyped_constant>(&entry_tuple->elements[0]);
if (!left) {
sstring msg = fmt::format("Invalid property name: {}", entry_tuple->elements[0]);
if (expr::is<bind_variable>(entry_tuple->elements[0])) {
msg += " (bind variables are not supported in DDL queries)";
}
add_recognition_error(msg);
break;
}
auto right = expr::as_if<untyped_constant>(&entry_tuple->elements[1]);
if (!right) {
sstring msg = fmt::format("Invalid property value: {} for property: {}", entry_tuple->elements[0], entry_tuple->elements[1]);
if (expr::is<bind_variable>(entry_tuple->elements[1])) {
msg += " (bind variables are not supported in DDL queries)";
}
add_recognition_error(msg);
break;
}
if (!res.emplace(left->raw_text, right->raw_text).second) {
sstring msg = fmt::format("Multiple definition for property {}", left->raw_text);
add_recognition_error(msg);
break;
}
}
return res;
}
sstring to_lower(std::string_view s) {
sstring lower_s(s.size(), '\0');
std::transform(s.cbegin(), s.cend(), lower_s.begin(), &::tolower);
return lower_s;
}
bool convert_boolean_literal(std::string_view s) {
return to_lower(s) == "true";
}
TokenType* getMissingSymbol(IntStreamType* istream, ExceptionBaseType* e,
ANTLR_UINT32 expectedTokenType, BitsetListType* follow) {
auto token = BaseType::getMissingSymbol(istream, e, expectedTokenType, follow);
_missing_tokens.emplace_back(token);
return token;
}
}
@lexer::namespace{cql3_parser}
@lexer::traits {
class CqlLexer;
class CqlParser;
typedef antlr3::Traits<CqlLexer, CqlParser> CqlLexerTraits;
typedef CqlLexerTraits CqlParserTraits;
}
@lexer::header {
#pragma GCC diagnostic ignored "-Wunused-variable"
#pragma GCC diagnostic ignored "-Wunused-function"
}
@lexer::context {
using collector_type = cql3::error_collector<ComponentType, ExceptionBaseType::TokenType, ExceptionBaseType>;
using listener_type = cql3::error_listener<ComponentType, ExceptionBaseType>;
listener_type* listener;
void set_error_listener(listener_type& listener) {
this->listener = &listener;
}
void displayRecognitionError(ANTLR_UINT8** token_names, ExceptionBaseType* ex)
{
listener->syntax_error(*this, token_names, ex);
}
bool is_eof_token(CommonTokenType token) const
{
return token == CommonTokenType::TOKEN_EOF;
}
std::string token_text(const TokenType* token) const
{
if (!token) {
return "";
}
return std::to_string(int(*token));
}
}
/** STATEMENTS **/
queries returns [std::vector<std::unique_ptr<raw::parsed_statement>> stmts]
: st=cqlStatement { $stmts.emplace_back(std::move(st)); }
(';' st=cqlStatement { $stmts.emplace_back(std::move(st)); })*
(';')* EOF
;
query returns [std::unique_ptr<raw::parsed_statement> stmnt]
: st=cqlStatement (';')* EOF { $stmnt = std::move(st); }
;
cqlStatement returns [std::unique_ptr<raw::parsed_statement> stmt]
@after{ if (stmt) { stmt->set_bound_variables(_bind_variables); } }
: st1= selectStatement { $stmt = std::move(st1); }
| st2= insertStatement { $stmt = std::move(st2); }
| st3= updateStatement { $stmt = std::move(st3); }
| st4= batchStatement { $stmt = std::move(st4); }
| st5= deleteStatement { $stmt = std::move(st5); }
| st6= useStatement { $stmt = std::move(st6); }
| st7= truncateStatement { $stmt = std::move(st7); }
| st8= createKeyspaceStatement { $stmt = std::move(st8); }
| st9= createTableStatement { $stmt = std::move(st9); }
| st10=createIndexStatement { $stmt = std::move(st10); }
| st11=dropKeyspaceStatement { $stmt = std::move(st11); }
| st12=dropTableStatement { $stmt = std::move(st12); }
| st13=dropIndexStatement { $stmt = std::move(st13); }
| st14=alterTableStatement { $stmt = std::move(st14); }
| st15=alterKeyspaceStatement { $stmt = std::move(st15); }
| st16=grantStatement { $stmt = std::move(st16); }
| st17=revokeStatement { $stmt = std::move(st17); }
| st18=listPermissionsStatement { $stmt = std::move(st18); }
| st19=createUserStatement { $stmt = std::move(st19); }
| st20=alterUserStatement { $stmt = std::move(st20); }
| st21=dropUserStatement { $stmt = std::move(st21); }
| st22=listUsersStatement { $stmt = std::move(st22); }
#if 0
| st23=createTriggerStatement { $stmt = st23; }
| st24=dropTriggerStatement { $stmt = st24; }
#endif
| st25=createTypeStatement { $stmt = std::move(st25); }
| st26=alterTypeStatement { $stmt = std::move(st26); }
| st27=dropTypeStatement { $stmt = std::move(st27); }
| st28=createFunctionStatement { $stmt = std::move(st28); }
| st29=dropFunctionStatement { $stmt = std::move(st29); }
| st30=createAggregateStatement { $stmt = std::move(st30); }
| st31=dropAggregateStatement { $stmt = std::move(st31); }
| st32=createViewStatement { $stmt = std::move(st32); }
| st33=alterViewStatement { $stmt = std::move(st33); }
| st34=dropViewStatement { $stmt = std::move(st34); }
| st35=listRolesStatement { $stmt = std::move(st35); }
| st36=grantRoleStatement { $stmt = std::move(st36); }
| st37=revokeRoleStatement { $stmt = std::move(st37); }
| st38=dropRoleStatement { $stmt = std::move(st38); }
| st39=createRoleStatement { $stmt = std::move(st39); }
| st40=alterRoleStatement { $stmt = std::move(st40); }
| st41=createServiceLevelStatement { $stmt = std::move(st41); }
| st42=alterServiceLevelStatement { $stmt = std::move(st42); }
| st43=dropServiceLevelStatement { $stmt = std::move(st43); }
| st44=attachServiceLevelStatement { $stmt = std::move(st44); }
| st45=detachServiceLevelStatement { $stmt = std::move(st45); }
| st46=listServiceLevelStatement { $stmt = std::move(st46); }
| st47=listServiceLevelAttachStatement { $stmt = std::move(st47); }
| st48=pruneMaterializedViewStatement { $stmt = std::move(st48); }
| st49=describeStatement { $stmt = std::move(st49); }
;
/*
* USE <KEYSPACE>;
*/
useStatement returns [std::unique_ptr<raw::use_statement> stmt]
: K_USE ks=keyspaceName { $stmt = std::make_unique<raw::use_statement>(ks); }
;
/**
* SELECT [JSON] <expression>
* FROM <CF>
* WHERE KEY = "key1" AND COL > 1 AND COL < 100
* LIMIT <NUMBER>
* [USING TIMEOUT <duration>];
*/
selectStatement returns [std::unique_ptr<raw::select_statement> expr]
@init {
bool is_distinct = false;
std::optional<expression> limit;
std::optional<expression> per_partition_limit;
raw::select_statement::parameters::orderings_type orderings;
bool allow_filtering = false;
raw::select_statement::parameters::statement_subtype statement_subtype = raw::select_statement::parameters::statement_subtype::REGULAR;
bool bypass_cache = false;
auto attrs = std::make_unique<cql3::attributes::raw>();
expression wclause = conjunction{};
}
: K_SELECT (
( K_JSON { statement_subtype = raw::select_statement::parameters::statement_subtype::JSON; } )?
( K_DISTINCT { is_distinct = true; } )?
sclause=selectClause
)
K_FROM (
cf=columnFamilyName
| K_MUTATION_FRAGMENTS '(' cf=columnFamilyName ')' { statement_subtype = raw::select_statement::parameters::statement_subtype::MUTATION_FRAGMENTS; }
)
( K_WHERE w=whereClause { wclause = std::move(w); } )?
( K_GROUP K_BY gbcolumns=listOfIdentifiers)?
( K_ORDER K_BY orderByClause[orderings] ( ',' orderByClause[orderings] )* )?
( K_PER K_PARTITION K_LIMIT rows=intValue { per_partition_limit = std::move(rows); } )?
( K_LIMIT rows=intValue { limit = std::move(rows); } )?
( K_ALLOW K_FILTERING { allow_filtering = true; } )?
( K_BYPASS K_CACHE { bypass_cache = true; })?
( usingTimeoutClause[attrs] )?
{
auto params = make_lw_shared<raw::select_statement::parameters>(std::move(orderings), is_distinct, allow_filtering, statement_subtype, bypass_cache);
$expr = std::make_unique<raw::select_statement>(std::move(cf), std::move(params),
std::move(sclause), std::move(wclause), std::move(limit), std::move(per_partition_limit),
std::move(gbcolumns), std::move(attrs));
}
;
selectClause returns [std::vector<shared_ptr<raw_selector>> expr]
: t1=selector { $expr.push_back(t1); } (',' tN=selector { $expr.push_back(tN); })*
| '*' { }
;
selector returns [shared_ptr<raw_selector> s]
@init{ shared_ptr<cql3::column_identifier> alias; }
: us=unaliasedSelector (K_AS c=ident { alias = c; })? { $s = ::make_shared<raw_selector>(std::move(us), alias); }
;
unaliasedSelector returns [uexpression s]
@init { uexpression tmp; }
: ( c=cident { tmp = unresolved_identifier{std::move(c)}; }
| K_COUNT '(' countArgument ')' { tmp = make_count_rows_function_expression(); }
| K_WRITETIME '(' c=cident ')' { tmp = column_mutation_attribute{column_mutation_attribute::attribute_kind::writetime,
unresolved_identifier{std::move(c)}}; }
| K_TTL '(' c=cident ')' { tmp = column_mutation_attribute{column_mutation_attribute::attribute_kind::ttl,
unresolved_identifier{std::move(c)}}; }
| f=functionName args=selectionFunctionArgs { tmp = function_call{std::move(f), std::move(args)}; }
| K_CAST '(' arg=unaliasedSelector K_AS t=native_type ')' { tmp = cast{.style = cast::cast_style::sql, .arg = std::move(arg), .type = std::move(t)}; }
)
( '.' fi=cident { tmp = field_selection{std::move(tmp), std::move(fi)}; } )*
{ $s = tmp; }
;
selectionFunctionArgs returns [std::vector<expression> a]
: '(' ')'
| '(' s1=unaliasedSelector { a.push_back(std::move(s1)); }
( ',' sn=unaliasedSelector { a.push_back(std::move(sn)); } )*
')'
;
countArgument
: '*'
| i=INTEGER { if (i->getText() != "1") {
add_recognition_error("Only COUNT(1) is supported, got COUNT(" + i->getText() + ")");
} }
;
whereClause returns [uexpression clause]
@init { std::vector<expression> terms; }
: e1=relation { terms.push_back(std::move(e1)); } (K_AND en=relation { terms.push_back(std::move(en)); })*
{ clause = conjunction{std::move(terms)}; }
;
orderByClause[raw::select_statement::parameters::orderings_type& orderings]
@init{
raw::select_statement::ordering ordering = raw::select_statement::ordering::ascending;
}
: c=cident (K_ASC | K_DESC { ordering = raw::select_statement::ordering::descending; })? { orderings.emplace_back(c, ordering); }
;
jsonValue returns [uexpression value]
:
| s=STRING_LITERAL { $value = untyped_constant{untyped_constant::string, $s.text}; }
| m=marker { $value = std::move(m); }
;
/**
* INSERT INTO <CF> (<column>, <column>, <column>, ...)
* VALUES (<value>, <value>, <value>, ...)
* USING TIMESTAMP <long>;
*
*/
insertStatement returns [std::unique_ptr<raw::modification_statement> expr]
@init {
auto attrs = std::make_unique<cql3::attributes::raw>();
std::vector<::shared_ptr<cql3::column_identifier::raw>> column_names;
std::vector<expression> values;
bool if_not_exists = false;
bool default_unset = false;
std::optional<expression> json_value;
}
: K_INSERT K_INTO cf=columnFamilyName
('(' c1=cident { column_names.push_back(c1); } ( ',' cn=cident { column_names.push_back(cn); } )* ')'
K_VALUES
'(' v1=term { values.push_back(std::move(v1)); } ( ',' vn=term { values.push_back(std::move(vn)); } )* ')'
( K_IF K_NOT K_EXISTS { if_not_exists = true; } )?
( usingClause[attrs] )?
{
$expr = std::make_unique<raw::insert_statement>(std::move(cf),
std::move(attrs),
std::move(column_names),
std::move(values),
if_not_exists);
}
| K_JSON
json_token=jsonValue { json_value = std::move(json_token); }
( K_DEFAULT K_UNSET { default_unset = true; } | K_DEFAULT K_NULL )?
( K_IF K_NOT K_EXISTS { if_not_exists = true; } )?
( usingClause[attrs] )?
{
$expr = std::make_unique<raw::insert_json_statement>(std::move(cf),
std::move(attrs),
std::move(*json_value),
if_not_exists,
default_unset);
}
)
;
usingClause[std::unique_ptr<cql3::attributes::raw>& attrs]
: K_USING usingClauseObjective[attrs] ( K_AND usingClauseObjective[attrs] )*
;
usingClauseObjective[std::unique_ptr<cql3::attributes::raw>& attrs]
: K_TIMESTAMP ts=intValue { attrs->timestamp = std::move(ts); }
| K_TTL t=intValue { attrs->time_to_live = std::move(t); }
| K_TIMEOUT to=term { attrs->timeout = std::move(to); }
;
usingTimestampTimeoutClause[std::unique_ptr<cql3::attributes::raw>& attrs]
: K_USING usingTimestampTimeoutClauseObjective[attrs] ( K_AND usingTimestampTimeoutClauseObjective[attrs] )*
;
usingTimestampTimeoutClauseObjective[std::unique_ptr<cql3::attributes::raw>& attrs]
: K_TIMESTAMP ts=intValue { attrs->timestamp = std::move(ts); }
| K_TIMEOUT to=term { attrs->timeout = std::move(to); }
;
usingTimeoutClause[std::unique_ptr<cql3::attributes::raw>& attrs]
: K_USING K_TIMEOUT to=term { attrs->timeout = std::move(to); }
;
/**
* UPDATE <CF>
* USING TIMESTAMP <long>
* SET name1 = value1, name2 = value2
* WHERE key = value;
*/
updateStatement returns [std::unique_ptr<raw::update_statement> expr]
@init {
bool if_exists = false;
auto attrs = std::make_unique<cql3::attributes::raw>();
std::vector<std::pair<::shared_ptr<cql3::column_identifier::raw>, std::unique_ptr<cql3::operation::raw_update>>> operations;
std::optional<expression> cond_opt;
}
: K_UPDATE cf=columnFamilyName
( usingClause[attrs] )?
K_SET columnOperation[operations] (',' columnOperation[operations])*
K_WHERE wclause=whereClause
( K_IF (K_EXISTS{ if_exists = true; } | conditions=updateConditions { cond_opt = std::move(conditions); } ))?
{
return std::make_unique<raw::update_statement>(std::move(cf),
std::move(attrs),
std::move(operations),
std::move(wclause),
std::move(cond_opt),
if_exists);
}
;
updateConditions returns [uexpression cond]
@init {
std::vector<expression> conditions;
}
: c1=columnCondition { conditions.emplace_back(std::move(c1)); }
( K_AND cn=columnCondition { conditions.emplace_back(std::move(cn)); } )*
{
return conjunction{std::move(conditions)};
}
;
/**
* DELETE name1, name2
* FROM <CF>
* [USING (TIMESTAMP <long> | TIMEOUT <duration>) [AND ...]]
* WHERE KEY = keyname
[IF (EXISTS | name = value, ...)];
*/
deleteStatement returns [std::unique_ptr<raw::delete_statement> expr]
@init {
auto attrs = std::make_unique<cql3::attributes::raw>();
std::vector<std::unique_ptr<cql3::operation::raw_deletion>> column_deletions;
bool if_exists = false;
std::optional<expression> cond_opt;
}
: K_DELETE ( dels=deleteSelection { column_deletions = std::move(dels); } )?
K_FROM cf=columnFamilyName
( usingTimestampTimeoutClause[attrs] )?
K_WHERE wclause=whereClause
( K_IF ( K_EXISTS { if_exists = true; } | conditions=updateConditions { cond_opt = std::move(conditions); } ))?
{
return std::make_unique<raw::delete_statement>(cf,
std::move(attrs),
std::move(column_deletions),
std::move(wclause),
std::move(cond_opt),
if_exists);
}
;
deleteSelection returns [std::vector<std::unique_ptr<cql3::operation::raw_deletion>> operations]
: t1=deleteOp { $operations.emplace_back(std::move(t1)); }
(',' tN=deleteOp { $operations.emplace_back(std::move(tN)); })*
;
deleteOp returns [std::unique_ptr<cql3::operation::raw_deletion> op]
: c=cident { $op = std::make_unique<cql3::operation::column_deletion>(std::move(c)); }
| c=cident '[' t=term ']' { $op = std::make_unique<cql3::operation::element_deletion>(std::move(c), std::move(t)); }
| c=cident '.' field=ident { $op = std::make_unique<cql3::operation::field_deletion>(std::move(c), std::move(field)); }
;
pruneMaterializedViewStatement returns [std::unique_ptr<raw::select_statement> expr]
@init {
bool is_distinct = false;
std::optional<expression> limit;
std::optional<expression> per_partition_limit;
raw::select_statement::parameters::orderings_type orderings;
bool allow_filtering = false;
raw::select_statement::parameters::statement_subtype statement_subtype = raw::select_statement::parameters::statement_subtype::PRUNE_MATERIALIZED_VIEW;
bool bypass_cache = false;
auto attrs = std::make_unique<cql3::attributes::raw>();
expression wclause = conjunction{};
}
: K_PRUNE K_MATERIALIZED K_VIEW cf=columnFamilyName (K_WHERE w=whereClause { wclause = std::move(w); } )? ( usingClause[attrs] )?
{
auto params = make_lw_shared<raw::select_statement::parameters>(std::move(orderings), is_distinct, allow_filtering, statement_subtype, bypass_cache);
return std::make_unique<raw::select_statement>(std::move(cf), std::move(params),
std::vector<shared_ptr<raw_selector>>(), std::move(wclause), std::move(limit), std::move(per_partition_limit),
std::vector<::shared_ptr<cql3::column_identifier::raw>>(), std::move(attrs));
}
;
/**
* BEGIN BATCH
* UPDATE <CF> SET name1 = value1 WHERE KEY = keyname1;
* UPDATE <CF> SET name2 = value2 WHERE KEY = keyname2;
* UPDATE <CF> SET name3 = value3 WHERE KEY = keyname3;
* ...
* APPLY BATCH
*
* OR
*
* BEGIN BATCH
* INSERT INTO <CF> (KEY, <name>) VALUES ('<key>', '<value>');
* INSERT INTO <CF> (KEY, <name>) VALUES ('<key>', '<value>');
* ...
* APPLY BATCH
*
* OR
*
* BEGIN BATCH
* DELETE name1, name2 FROM <CF> WHERE key = <key>
* DELETE name3, name4 FROM <CF> WHERE key = <key>
* ...
* APPLY BATCH
*/
batchStatement returns [std::unique_ptr<cql3::statements::raw::batch_statement> expr]
@init {
using btype = cql3::statements::raw::batch_statement::type;
btype type = btype::LOGGED;
std::vector<std::unique_ptr<cql3::statements::raw::modification_statement>> statements;
auto attrs = std::make_unique<cql3::attributes::raw>();
}
: K_BEGIN
( K_UNLOGGED { type = btype::UNLOGGED; } | K_COUNTER { type = btype::COUNTER; } )?
K_BATCH ( usingClause[attrs] )?
( s=batchStatementObjective ';'? { statements.push_back(std::move(s)); } )*
K_APPLY K_BATCH
{
$expr = std::make_unique<cql3::statements::raw::batch_statement>(type, std::move(attrs), std::move(statements));
}
;
batchStatementObjective returns [std::unique_ptr<cql3::statements::raw::modification_statement> statement]
: i=insertStatement { $statement = std::move(i); }
| u=updateStatement { $statement = std::move(u); }
| d=deleteStatement { $statement = std::move(d); }
;
dropAggregateStatement returns [std::unique_ptr<cql3::statements::drop_aggregate_statement> expr]
@init {
bool if_exists = false;
std::vector<shared_ptr<cql3_type::raw>> arg_types;
bool args_present = false;
}
: K_DROP K_AGGREGATE
(K_IF K_EXISTS { if_exists = true; } )?
fn=functionName
(
'('
(
v=comparatorType { arg_types.push_back(v); }
( ',' v=comparatorType { arg_types.push_back(v); } )*
)?
')'
{ args_present = true; }
)?
{ $expr = std::make_unique<cql3::statements::drop_aggregate_statement>(std::move(fn), std::move(arg_types), args_present, if_exists); }
;
createAggregateStatement returns [std::unique_ptr<cql3::statements::create_aggregate_statement> expr]
@init {
bool or_replace = false;
bool if_not_exists = false;
std::vector<shared_ptr<cql3_type::raw>> arg_types;
std::optional<sstring> ffunc;
std::optional<expr::expression> ival;
std::optional<sstring> rfunc;
}
: K_CREATE (K_OR K_REPLACE { or_replace = true; })?
K_AGGREGATE
(K_IF K_NOT K_EXISTS { if_not_exists = true; })?
fn=functionName
'('
(
v=comparatorType { arg_types.push_back(v); }
( ',' v=comparatorType { arg_types.push_back(v); } )*
)?
')'
K_SFUNC sfunc = allowedFunctionName
K_STYPE stype = comparatorType
(
K_REDUCEFUNC reduce_name = allowedFunctionName { rfunc = reduce_name; }
)?
(
K_FINALFUNC final_func = allowedFunctionName { ffunc = final_func; }
)?
(
K_INITCOND init_val = term { ival = std::move(init_val); }
)?
{ $expr = std::make_unique<cql3::statements::create_aggregate_statement>(std::move(fn), std::move(arg_types), std::move(sfunc), std::move(stype), std::move(rfunc), std::move(ffunc), std::move(ival), or_replace, if_not_exists); }
;
createFunctionStatement returns [std::unique_ptr<cql3::statements::create_function_statement> expr]
@init {
bool or_replace = false;
bool if_not_exists = false;
std::vector<shared_ptr<cql3::column_identifier>> arg_names;
std::vector<shared_ptr<cql3_type::raw>> arg_types;
bool called_on_null_input = false;
}
: K_CREATE
// "OR REPLACE" and "IF NOT EXISTS" cannot be used together
((K_OR K_REPLACE { or_replace = true; } K_FUNCTION)
| (K_FUNCTION K_IF K_NOT K_EXISTS { if_not_exists = true; })
| K_FUNCTION)
fn=functionName
'('
(
k=ident v=comparatorType { arg_names.push_back(k); arg_types.push_back(v); }
( ',' k=ident v=comparatorType { arg_names.push_back(k); arg_types.push_back(v); } )*
)?
')'
( (K_RETURNS K_NULL) | (K_CALLED { called_on_null_input = true; })) K_ON K_NULL K_INPUT
K_RETURNS rt = comparatorType
K_LANGUAGE language = IDENT
K_AS body = STRING_LITERAL
{ $expr = std::make_unique<cql3::statements::create_function_statement>(std::move(fn), to_lower($language.text), $body.text, std::move(arg_names), std::move(arg_types), std::move(rt), called_on_null_input, or_replace, if_not_exists); }
;
dropFunctionStatement returns [std::unique_ptr<cql3::statements::drop_function_statement> expr]
@init {
bool if_exists = false;
std::vector<shared_ptr<cql3_type::raw>> arg_types;
bool args_present = false;
}
: K_DROP K_FUNCTION
(K_IF K_EXISTS { if_exists = true; } )?
fn=functionName
(
'('
(
v=comparatorType { arg_types.push_back(v); }
( ',' v=comparatorType { arg_types.push_back(v); } )*
)?
')'
{ args_present = true; }
)?
{ $expr = std::make_unique<cql3::statements::drop_function_statement>(std::move(fn), std::move(arg_types), args_present, if_exists); }
;
/**
* CREATE KEYSPACE [IF NOT EXISTS] <KEYSPACE> WITH attr1 = value1 AND attr2 = value2;
*/
createKeyspaceStatement returns [std::unique_ptr<cql3::statements::create_keyspace_statement> expr]
@init {
auto attrs = make_shared<cql3::statements::ks_prop_defs>();
bool if_not_exists = false;
}
: K_CREATE K_KEYSPACE (K_IF K_NOT K_EXISTS { if_not_exists = true; } )? ks=keyspaceName
K_WITH properties[*attrs] { $expr = std::make_unique<cql3::statements::create_keyspace_statement>(ks, attrs, if_not_exists); }
;
/**
* CREATE COLUMNFAMILY [IF NOT EXISTS] <CF> (
* <name1> <type>,
* <name2> <type>,
* <name3> <type>
* ) WITH <property> = <value> AND ...;
*/
createTableStatement returns [std::unique_ptr<cql3::statements::create_table_statement::raw_statement> expr]
@init { bool if_not_exists = false; }
: K_CREATE K_COLUMNFAMILY (K_IF K_NOT K_EXISTS { if_not_exists = true; } )?
cf=columnFamilyName { $expr = std::make_unique<cql3::statements::create_table_statement::raw_statement>(cf, if_not_exists); }
cfamDefinition[*expr]
;
cfamDefinition[cql3::statements::create_table_statement::raw_statement& expr]
: '(' cfamColumns[expr] ( ',' cfamColumns[expr]? )* ')'
( K_WITH cfamProperty[$expr.properties()] ( K_AND cfamProperty[$expr.properties()] )*)?
;
cfamColumns[cql3::statements::create_table_statement::raw_statement& expr]
@init { bool is_static=false; }
: k=ident v=comparatorType (K_STATIC {is_static = true;})? { $expr.add_definition(k, v, is_static); }
(K_PRIMARY K_KEY { $expr.add_key_aliases(std::vector<shared_ptr<cql3::column_identifier>>{k}); })?
| K_PRIMARY K_KEY '(' pkDef[expr] (',' c=ident { $expr.add_column_alias(c); } )* ')'
;
pkDef[cql3::statements::create_table_statement::raw_statement& expr]
@init { std::vector<shared_ptr<cql3::column_identifier>> l; }
: k=ident { $expr.add_key_aliases(std::vector<shared_ptr<cql3::column_identifier>>{k}); }
| '(' k1=ident { l.push_back(k1); } ( ',' kn=ident { l.push_back(kn); } )* ')' { $expr.add_key_aliases(l); }
;
cfamProperty[cql3::statements::cf_properties& expr]
: property[*$expr.properties()]
| K_COMPACT K_STORAGE { $expr.set_compact_storage(); }
| K_CLUSTERING K_ORDER K_BY '(' cfamOrdering[expr] (',' cfamOrdering[expr])* ')'
;
cfamOrdering[cql3::statements::cf_properties& expr]
@init{ bool reversed=false; }
: k=ident (K_ASC | K_DESC { reversed=true;} ) { $expr.set_ordering(k, reversed); }
;
/**
* CREATE TYPE foo (
* <name1> <type1>,
* <name2> <type2>,
* ....
* )
*/
createTypeStatement returns [std::unique_ptr<create_type_statement> expr]
@init { bool if_not_exists = false; }
: K_CREATE K_TYPE (K_IF K_NOT K_EXISTS { if_not_exists = true; } )?
tn=userTypeName { $expr = std::make_unique<create_type_statement>(ut_name(std::move(tn)), if_not_exists); }
'(' typeColumns[*expr] ( ',' typeColumns[*expr]? )* ')'
;
typeColumns[create_type_statement& expr]
: k=ident v=comparatorType { $expr.add_definition(k, v); }
;
/**
* CREATE INDEX [IF NOT EXISTS] [indexName] ON <columnFamily> (<columnName>);
* CREATE CUSTOM INDEX [IF NOT EXISTS] [indexName] ON <columnFamily> (<columnName>) USING <indexClass>;
*/
createIndexStatement returns [std::unique_ptr<create_index_statement> expr]
@init {
auto props = make_shared<index_prop_defs>();
bool if_not_exists = false;
auto name = ::make_shared<cql3::index_name>();
std::vector<::shared_ptr<index_target::raw>> targets;
}
: K_CREATE (K_CUSTOM { props->is_custom = true; })? K_INDEX (K_IF K_NOT K_EXISTS { if_not_exists = true; } )?
(idxName[*name])? K_ON cf=columnFamilyName '(' (target1=indexIdent { targets.emplace_back(target1); } (',' target2=indexIdent { targets.emplace_back(target2); } )*)? ')'
(K_USING cls=STRING_LITERAL { props->custom_class = sstring{$cls.text}; })?
(K_WITH properties[*props])?
{ $expr = std::make_unique<create_index_statement>(cf, name, targets, props, if_not_exists); }
;
indexIdent returns [::shared_ptr<index_target::raw> id]
@init {
std::vector<::shared_ptr<cql3::column_identifier::raw>> columns;
}
: c=cident { $id = index_target::raw::regular_values_of(c); }
| K_VALUES '(' c=cident ')' { $id = index_target::raw::collection_values_of(c); }
| K_KEYS '(' c=cident ')' { $id = index_target::raw::keys_of(c); }
| K_ENTRIES '(' c=cident ')' { $id = index_target::raw::keys_and_values_of(c); }
| K_FULL '(' c=cident ')' { $id = index_target::raw::full_collection(c); }
| '(' c1=cident { columns.push_back(c1); } ( ',' cn=cident { columns.push_back(cn); } )* ')' { $id = index_target::raw::columns(std::move(columns)); }
;
/**
* CREATE MATERIALIZED VIEW <viewName> AS
* SELECT <columns>
* FROM <CF>
* WHERE <pkColumns> IS NOT NULL
* PRIMARY KEY (<pkColumns>)
* WITH <property> = <value> AND ...;
*/
createViewStatement returns [std::unique_ptr<create_view_statement> expr]
@init {
bool if_not_exists = false;
std::vector<::shared_ptr<cql3::column_identifier::raw>> partition_keys;
std::vector<::shared_ptr<cql3::column_identifier::raw>> composite_keys;
expression wclause = conjunction{};
}
: K_CREATE K_MATERIALIZED K_VIEW (K_IF K_NOT K_EXISTS { if_not_exists = true; })? cf=columnFamilyName K_AS
K_SELECT sclause=selectClause K_FROM basecf=columnFamilyName
(K_WHERE w=whereClause { wclause = std::move(w); } )?
K_PRIMARY K_KEY (
'(' '(' k1=cident { partition_keys.push_back(k1); } ( ',' kn=cident { partition_keys.push_back(kn); } )* ')' ( ',' c1=cident { composite_keys.push_back(c1); } )* ')'
| '(' k1=cident { partition_keys.push_back(k1); } ( ',' cn=cident { composite_keys.push_back(cn); } )* ')'
)
{
$expr = std::make_unique<create_view_statement>(
std::move(cf),
std::move(basecf),
std::move(sclause),
std::move(wclause),
std::move(partition_keys),
std::move(composite_keys),
if_not_exists);
}
( K_WITH cfamProperty[{ $expr->properties() }] ( K_AND cfamProperty[{ $expr->properties() }] )*)?
;
#if 0
/**
* CREATE TRIGGER triggerName ON columnFamily USING 'triggerClass';
*/
createTriggerStatement returns [CreateTriggerStatement expr]
@init {
boolean ifNotExists = false;
}
: K_CREATE K_TRIGGER (K_IF K_NOT K_EXISTS { ifNotExists = true; } )? (name=cident)
K_ON cf=columnFamilyName K_USING cls=STRING_LITERAL
{ $expr = new CreateTriggerStatement(cf, name.toString(), $cls.text, ifNotExists); }
;
/**
* DROP TRIGGER [IF EXISTS] triggerName ON columnFamily;
*/
dropTriggerStatement returns [DropTriggerStatement expr]
@init { boolean ifExists = false; }
: K_DROP K_TRIGGER (K_IF K_EXISTS { ifExists = true; } )? (name=cident) K_ON cf=columnFamilyName
{ $expr = new DropTriggerStatement(cf, name.toString(), ifExists); }
;
#endif
/**
* ALTER KEYSPACE <KS> WITH <property> = <value>;
*/
alterKeyspaceStatement returns [std::unique_ptr<cql3::statements::alter_keyspace_statement> expr]
@init {
auto attrs = make_shared<cql3::statements::ks_prop_defs>();
}
: K_ALTER K_KEYSPACE ks=keyspaceName
K_WITH properties[*attrs] { $expr = std::make_unique<cql3::statements::alter_keyspace_statement>(ks, attrs); }
;
/**
* ALTER COLUMN FAMILY <CF> ALTER <column> TYPE <newtype>;
* ALTER COLUMN FAMILY <CF> ADD <column> <newtype>; | ALTER COLUMN FAMILY <CF> ADD (<column> <newtype>,<column1> <newtype1>..... <column n> <newtype n>)
* ALTER COLUMN FAMILY <CF> DROP <column>; | ALTER COLUMN FAMILY <CF> DROP ( <column>,<column1>.....<column n>)
* ALTER COLUMN FAMILY <CF> WITH <property> = <value>;
* ALTER COLUMN FAMILY <CF> RENAME <column> TO <column>;
*/
alterTableStatement returns [std::unique_ptr<alter_table_statement> expr]
@init {
alter_table_statement::type type;
auto props = cql3::statements::cf_prop_defs();
std::vector<alter_table_statement::column_change> column_changes;
std::vector<std::pair<shared_ptr<cql3::column_identifier::raw>, shared_ptr<cql3::column_identifier::raw>>> renames;
}
: K_ALTER K_COLUMNFAMILY cf=columnFamilyName
( K_ALTER id=cident K_TYPE v=comparatorType { type = alter_table_statement::type::alter; column_changes.emplace_back(alter_table_statement::column_change{id, v}); }
| K_ADD { type = alter_table_statement::type::add; }
( id=cident v=comparatorType s=cfisStatic { column_changes.emplace_back(alter_table_statement::column_change{id, v, s}); }
| '(' id1=cident v1=comparatorType s1=cfisStatic { column_changes.emplace_back(alter_table_statement::column_change{id1, v1, s1}); }
(',' idn=cident vn=comparatorType sn=cfisStatic { column_changes.emplace_back(alter_table_statement::column_change{idn, vn, sn}); } )* ')'
)