-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParseStatement.hpp
1267 lines (1103 loc) · 40.9 KB
/
ParseStatement.hpp
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
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
**/
#ifndef QUICKSTEP_PARSER_PARSE_STATEMENT_HPP_
#define QUICKSTEP_PARSER_PARSE_STATEMENT_HPP_
#include <memory>
#include <string>
#include <vector>
#include "parser/ParseAssignment.hpp"
#include "parser/ParseAttributeDefinition.hpp"
#include "parser/ParseBasicExpressions.hpp"
#include "parser/ParseBlockProperties.hpp"
#include "parser/ParseIndexProperties.hpp"
#include "parser/ParseKeyValue.hpp"
#include "parser/ParsePartitionClause.hpp"
#include "parser/ParsePredicate.hpp"
#include "parser/ParsePriority.hpp"
#include "parser/ParseSelect.hpp"
#include "parser/ParseSetOperation.hpp"
#include "parser/ParseString.hpp"
#include "parser/ParseSubqueryTableReference.hpp"
#include "parser/ParseTreeNode.hpp"
#include "storage/StorageBlockInfo.hpp"
#include "utility/Macros.hpp"
#include "utility/PtrList.hpp"
#include "utility/PtrVector.hpp"
#include "glog/logging.h"
namespace quickstep {
/** \addtogroup Parser
* @{
*/
/**
* @brief Abstract base class for all complete SQL commands.
**/
class ParseStatement : public ParseTreeNode {
public:
/**
* @brief The possible types of SQL statements.
**/
enum StatementType {
kCommand = 0,
kCopy,
kCreateIndex,
kCreateTable,
kDelete,
kDropTable,
kInsert,
kQuit,
kSetOperation,
kUpdate
};
/**
* @brief Virtual destructor.
**/
~ParseStatement() override {
}
/**
* @brief Identify the type of this SQL statement.
*
* @return The type of this statement.
**/
virtual StatementType getStatementType() const = 0;
/**
* @brief Get the priority of the SQL statement. Note that the priority is
* an unsigned non-zero integer.
*
* @return The priority of the SQL statement. The default priority is 1.
**/
virtual const std::uint64_t getPriority() const {
return 1;
}
protected:
ParseStatement(const int line_number, const int column_number)
: ParseTreeNode(line_number, column_number) {
}
private:
DISALLOW_COPY_AND_ASSIGN(ParseStatement);
};
/**
* @brief The parsed representation of a CREATE TABLE statement.
**/
class ParseStatementCreateTable : public ParseStatement {
public:
/**
* @brief Constructor.
*
* @param relation_name The name of the relation to create.
* @param attribute_definition_list The list of definitions for the
* attributes in the new relation, which becomes owned by this
* ParseStatementCreateTable.
* @param opt_block_properties Optional physical properties of the block.
**/
ParseStatementCreateTable(const int line_number,
const int column_number,
ParseString *relation_name,
PtrList<ParseAttributeDefinition> *attribute_definition_list,
ParseBlockProperties *opt_block_properties,
ParsePartitionClause *opt_partition_clause)
: ParseStatement(line_number, column_number),
relation_name_(relation_name),
attribute_definition_list_(attribute_definition_list),
opt_block_properties_(opt_block_properties),
opt_partition_clause_(opt_partition_clause) {
}
~ParseStatementCreateTable() override {
}
StatementType getStatementType() const override {
return kCreateTable;
}
std::string getName() const override { return "CreateTableStatement"; }
/**
* @brief Get the name of the relation to create.
*
* @return The new relation's name.
**/
const ParseString* relation_name() const {
return relation_name_.get();
}
/**
* @brief Get the list of attribute definitions.
*
* @return The list of attribute definitions for the new relation.
**/
const PtrList<ParseAttributeDefinition>& attribute_definition_list() const {
return *attribute_definition_list_;
}
/**
* @brief Get a pointer to the BlockProperties.
*
* @return The BlockProperties or nullptr if not specified.
**/
const ParseBlockProperties* opt_block_properties() const {
return opt_block_properties_.get();
}
/**
* @brief Get a pointer to the PartitionClause.
*
* @return The PartitionClause or nullptr if not specified.
**/
const ParsePartitionClause* opt_partition_clause() const {
return opt_partition_clause_.get();
}
protected:
void getFieldStringItems(
std::vector<std::string> *inline_field_names,
std::vector<std::string> *inline_field_values,
std::vector<std::string> *non_container_child_field_names,
std::vector<const ParseTreeNode*> *non_container_child_fields,
std::vector<std::string> *container_child_field_names,
std::vector<std::vector<const ParseTreeNode*>> *container_child_fields) const override {
inline_field_names->push_back("relation_name");
inline_field_values->push_back(relation_name_->value());
container_child_field_names->push_back("attribute_list");
container_child_fields->emplace_back();
for (const ParseAttributeDefinition& attribute_definition : *attribute_definition_list_) {
container_child_fields->back().push_back(&attribute_definition);
}
if (opt_block_properties_) {
container_child_field_names->push_back("block_properties");
container_child_fields->emplace_back();
container_child_fields->back().push_back(opt_block_properties_.get());
}
if (opt_partition_clause_) {
container_child_field_names->push_back("partition_clause");
container_child_fields->emplace_back();
container_child_fields->back().push_back(opt_partition_clause_.get());
}
}
private:
std::unique_ptr<ParseString> relation_name_;
std::unique_ptr<PtrList<ParseAttributeDefinition> > attribute_definition_list_;
std::unique_ptr<ParseBlockProperties> opt_block_properties_;
std::unique_ptr<ParsePartitionClause> opt_partition_clause_;
DISALLOW_COPY_AND_ASSIGN(ParseStatementCreateTable);
};
/**
* @brief The parsed representation of a CREATE INDEX statement.
**/
class ParseStatementCreateIndex : public ParseStatement {
public:
/**
* @brief Constructor.
*
* @param index_name The name of the index to create.
* @param relation_name The name of the relation to create index upon.
* @param attribute_name_list A list of attributes of the relation
* on which the index has to be created. If specified as null,
* then index is created on all the attributes.
* @param index_type The type of index to create.
**/
ParseStatementCreateIndex(const int line_number,
const int column_number,
ParseString *index_name,
ParseString *relation_name,
PtrList<ParseAttribute> *attribute_list,
ParseString *index_type)
: ParseStatement(line_number, column_number),
index_name_(index_name),
relation_name_(relation_name),
attribute_list_(attribute_list),
index_type_(index_type) {
initializeIndexType();
}
/**
* @brief Constructor.
*
* @param index_name The name of the index to create.
* @param relation_name The name of the relation to create index upon.
* @param attribute_name_list A list of attributes of the relation
* on which the index has to be created. If specified as null,
* then index is created on all the attributes.
* @param index_type The type of index to create.
* @param index_properties_line_number
* @param index_properties_column_number
* @param opt_index_properties Optional index properties that were specified.
**/
ParseStatementCreateIndex(const int line_number,
const int column_number,
ParseString *index_name,
ParseString *relation_name,
PtrList<ParseAttribute> *attribute_list,
ParseString *index_type,
const int index_properties_line_number,
const int index_properties_column_number,
PtrList<ParseKeyValue> *opt_index_properties)
: ParseStatement(line_number, column_number),
index_name_(index_name),
relation_name_(relation_name),
attribute_list_(attribute_list),
index_type_(index_type) {
initializeIndexType();
custom_properties_node_.reset(new ParseIndexProperties(index_properties_line_number,
index_properties_column_number,
opt_index_properties));
index_properties_->addCustomProperties(custom_properties_node_->getKeyValueList());
}
~ParseStatementCreateIndex() override {
}
StatementType getStatementType() const override {
return kCreateIndex;
}
std::string getName() const override { return "CreateIndexStatement"; }
/**
* @brief Get the name of the index to create.
*
* @return The index's name.
**/
const ParseString* index_name() const {
return index_name_.get();
}
/**
* @brief Get the name of the relation to create index upon.
*
* @return The relation's name.
**/
const ParseString* relation_name() const {
return relation_name_.get();
}
/**
* @brief Get the list of attributes on which index is supposed to be defined.
*
* @return The list of attributes on which index is to be built.
**/
const PtrList<ParseAttribute>* attribute_list() const {
return attribute_list_.get();
}
/**
* @brief Get the type of the index to be created.
*
* @return The index's type.
**/
const ParseString* index_type() const {
return index_type_.get();
}
/**
* @brief Get the index properties associated with this index type.
*
* @return The index properties for this type.
**/
const IndexProperties* getIndexProperties() const {
return index_properties_.get();
}
const ParseIndexProperties* getCustomPropertiesNode() const {
return custom_properties_node_.get();
}
bool hasCustomProperties() const {
return custom_properties_node_ != nullptr;
}
protected:
void getFieldStringItems(
std::vector<std::string> *inline_field_names,
std::vector<std::string> *inline_field_values,
std::vector<std::string> *non_container_child_field_names,
std::vector<const ParseTreeNode*> *non_container_child_fields,
std::vector<std::string> *container_child_field_names,
std::vector<std::vector<const ParseTreeNode*>> *container_child_fields) const override {
inline_field_names->push_back("index_name");
inline_field_values->push_back(index_name_->value());
inline_field_names->push_back("relation_name");
inline_field_values->push_back(relation_name_->value());
inline_field_names->push_back("index_type");
const int index_type_enum_val = std::stoi(index_type_->value());
switch (index_type_enum_val) {
case IndexSubBlockType::kBitWeavingV: // Fall through.
case IndexSubBlockType::kBitWeavingH:
inline_field_values->push_back("bitweaving");
break;
case IndexSubBlockType::kCSBTree:
inline_field_values->push_back("cs_b_tree");
break;
case IndexSubBlockType::kBloomFilter:
inline_field_values->push_back("bloom_filter");
break;
case IndexSubBlockType::kSMA:
inline_field_values->push_back("sma");
break;
default:
inline_field_values->push_back("unknown");
}
if (attribute_list_ != nullptr) {
container_child_field_names->push_back("attribute_list");
container_child_fields->emplace_back();
for (const ParseAttribute &attribute : *attribute_list_) {
container_child_fields->back().push_back(&attribute);
}
}
if (custom_properties_node_ != nullptr) {
container_child_field_names->push_back("index_property_list");
container_child_fields->emplace_back();
container_child_fields->back().push_back(custom_properties_node_.get());
}
}
private:
std::unique_ptr<ParseString> index_name_;
std::unique_ptr<ParseString> relation_name_;
std::unique_ptr<PtrList<ParseAttribute>> attribute_list_;
std::unique_ptr<ParseString> index_type_;
std::unique_ptr<IndexProperties> index_properties_;
// Optional custom properties for the index can be specified during creation.
std::unique_ptr<const ParseIndexProperties> custom_properties_node_;
void initializeIndexType() {
const int index_type_enum_val = std::stoi(index_type_->value());
switch (index_type_enum_val) {
case IndexSubBlockType::kBitWeavingV: // Fall through.
case IndexSubBlockType::kBitWeavingH:
index_properties_.reset(new BitWeavingIndexProperties());
break;
case IndexSubBlockType::kBloomFilter:
index_properties_.reset(new BloomFilterIndexProperties());
break;
case IndexSubBlockType::kCSBTree:
index_properties_.reset(new CSBTreeIndexProperties());
break;
case IndexSubBlockType::kSMA:
index_properties_.reset(new SMAIndexProperties());
break;
default:
LOG(FATAL) << "Unknown index subblock type.";
break;
}
}
DISALLOW_COPY_AND_ASSIGN(ParseStatementCreateIndex);
};
/**
* @brief The parsed representation of a DROP TABLE statement.
**/
class ParseStatementDropTable : public ParseStatement {
public:
/**
* @brief Constructor.
*
* @param line_number Line number of the first token of this node in the SQL statement.
* @param column_number Column number of the first token of this node in the SQL statement.
* @param relation_name The name of the relation to drop.
**/
ParseStatementDropTable(const int line_number, const int column_number, ParseString *relation_name)
: ParseStatement(line_number, column_number),
relation_name_(relation_name) {
}
/**
* @brief Destructor.
*/
~ParseStatementDropTable() override {
}
StatementType getStatementType() const override {
return kDropTable;
}
std::string getName() const override { return "DropTableStatement"; }
/**
* @brief Get the name of the relation to drop.
*
* @return The name of the relation to drop.
**/
const ParseString* relation_name() const {
return relation_name_.get();
}
protected:
void getFieldStringItems(
std::vector<std::string> *inline_field_names,
std::vector<std::string> *inline_field_values,
std::vector<std::string> *non_container_child_field_names,
std::vector<const ParseTreeNode*> *non_container_child_fields,
std::vector<std::string> *container_child_field_names,
std::vector<std::vector<const ParseTreeNode*>> *container_child_fields) const override {
inline_field_names->push_back("relation_name");
inline_field_values->push_back(relation_name_->value());
}
private:
std::unique_ptr<ParseString> relation_name_;
DISALLOW_COPY_AND_ASSIGN(ParseStatementDropTable);
};
/**
* @brief The parsed representation of an UNION/INTERSECT/SELECT statement.
**/
class ParseStatementSetOperation : public ParseStatement {
public:
/**
* @brief Constructor.
* @note Takes ownership of all pointers.
*
* @param line_number Line number of the first token of this node in the SQL statement.
* @param column_number Column number of the first token of this node in the SQL statement.
* @param set_operation_query The top level set operation query
* @param with_clause The WITH clause of common table query expressions.
* @param priority_clause The PRIORITY clause of this query. If not valid or
* not present, this is NULL.
**/
ParseStatementSetOperation(const int line_number,
const int column_number,
ParseSetOperation *set_operation_query,
PtrVector<ParseSubqueryTableReference> *with_clause,
ParsePriority *priority_clause)
: ParseStatement(line_number, column_number),
set_operation_query_(set_operation_query),
with_clause_(with_clause),
priority_clause_(priority_clause) {
}
/**
* @brief Destructor.
*/
~ParseStatementSetOperation() override {
}
StatementType getStatementType() const override {
return kSetOperation;
}
std::string getName() const override { return "SetOperationStatement"; }
/**
* @return Get the top-level set operation query.
*/
const ParseSetOperation* set_operation_query() const {
return set_operation_query_.get();
}
/**
* @brief Get the WITH table queries.
*
* @return The parsed WITH table list.
*/
const PtrVector<ParseSubqueryTableReference>* with_clause() const {
return with_clause_.get();
}
const std::uint64_t getPriority() const override {
if (priority_clause_ != nullptr) {
DCHECK(priority_clause_->priority_expression() != nullptr);
return priority_clause_->priority_expression()->long_value();
}
return 1;
}
protected:
void getFieldStringItems(
std::vector<std::string> *inline_field_names,
std::vector<std::string> *inline_field_values,
std::vector<std::string> *non_container_child_field_names,
std::vector<const ParseTreeNode*> *non_container_child_fields,
std::vector<std::string> *container_child_field_names,
std::vector<std::vector<const ParseTreeNode*>> *container_child_fields) const override {
non_container_child_field_names->push_back("set_operation_query");
non_container_child_fields->push_back(set_operation_query_.get());
if (with_clause_ != nullptr && !with_clause_->empty()) {
container_child_field_names->push_back("with_clause");
container_child_fields->emplace_back();
for (const ParseSubqueryTableReference &common_subquery : *with_clause_) {
container_child_fields->back().push_back(&common_subquery);
}
}
if (priority_clause_ != nullptr) {
non_container_child_field_names->push_back("priority");
non_container_child_fields->push_back(priority_clause_.get());
}
}
private:
std::unique_ptr<ParseSetOperation> set_operation_query_;
std::unique_ptr<PtrVector<ParseSubqueryTableReference>> with_clause_;
std::unique_ptr<ParsePriority> priority_clause_;
DISALLOW_COPY_AND_ASSIGN(ParseStatementSetOperation);
};
/**
* @brief The parsed representation of an INSERT statement.
*
* This is an abstract class where each of its subclass represents a concrete
* type of insert operation.
**/
class ParseStatementInsert : public ParseStatement {
public:
enum class InsertType {
kTuple = 0,
kSelection
};
/**
* @brief Constructor.
*
* @param line_number Line number of the first token of this node in the SQL statement.
* @param column_number Column number of the first token of this node in the SQL statement.
* @param relation_name The name of the relation to insert into.
**/
ParseStatementInsert(const int line_number,
const int column_number,
const ParseString *relation_name)
: ParseStatement(line_number, column_number),
relation_name_(relation_name) {
}
/**
* @brief Get the insert type of this insert statement.
*
* @return The insert type of this insert statement.
*/
virtual InsertType getInsertType() const = 0;
std::string getName() const override { return "InsertStatement"; }
StatementType getStatementType() const override {
return kInsert;
}
/**
* @brief Get the name of the relation to insert into.
*
* @return The name of the relation to insert into.
**/
const ParseString* relation_name() const {
return relation_name_.get();
}
private:
std::unique_ptr<const ParseString> relation_name_;
DISALLOW_COPY_AND_ASSIGN(ParseStatementInsert);
};
/**
* @brief The parsed representation of an INSERT ... VALUES ... statement.
**/
class ParseStatementInsertTuple : public ParseStatementInsert {
public:
/**
* @brief Constructor.
*
* @param line_number Line number of the first token of this node in the SQL statement.
* @param column_number Column number of the first token of this node in the SQL statement.
* @param relation_name The name of the relation to insert into.
* @param literal_values A list of literal values (in attribute-definition
* order) to insert into the specified relation as a new tuple.
* Becomes owned by this ParseStatementInsert.
**/
ParseStatementInsertTuple(const int line_number,
const int column_number,
const ParseString *relation_name,
PtrList<PtrList<ParseScalarLiteral>> *literal_values_list)
: ParseStatementInsert(line_number, column_number, relation_name),
literal_values_(literal_values_list) {
}
~ParseStatementInsertTuple() override {
}
InsertType getInsertType() const override {
return InsertType::kTuple;
}
/**
* @brief Get the list of list of parsed literal attribute values to insert.
*
* @return The list of lists of literal values to insert.
**/
const PtrList<PtrList<ParseScalarLiteral>>& getLiteralValues() const {
return *literal_values_;
}
protected:
void getFieldStringItems(
std::vector<std::string> *inline_field_names,
std::vector<std::string> *inline_field_values,
std::vector<std::string> *non_container_child_field_names,
std::vector<const ParseTreeNode*> *non_container_child_fields,
std::vector<std::string> *container_child_field_names,
std::vector<std::vector<const ParseTreeNode*>> *container_child_fields) const override {
inline_field_names->push_back("relation_name");
inline_field_values->push_back(relation_name()->value());
for (const PtrList<ParseScalarLiteral>& literal_values_single_tuple : *literal_values_) {
container_child_field_names->push_back("tuple");
container_child_fields->emplace_back();
for (const ParseScalarLiteral& literal_value : literal_values_single_tuple) {
container_child_fields->back().push_back(&literal_value);
}
}
}
private:
std::unique_ptr<PtrList<PtrList<ParseScalarLiteral>>> literal_values_;
DISALLOW_COPY_AND_ASSIGN(ParseStatementInsertTuple);
};
/**
* @brief The parsed representation of an INSERT ... SELECT ... statement.
**/
class ParseStatementInsertSelection : public ParseStatementInsert {
public:
/**
* @brief Constructor.
*
* @param line_number Line number of the first token of this node in the SQL statement.
* @param column_number Column number of the first token of this node in the SQL statement.
* @param relation_name The name of the relation to insert into.
* @param select_query The SELECT query for generating insertion tuples.
* @param with_clause The WITH clause of common table query expressions.
**/
ParseStatementInsertSelection(const int line_number,
const int column_number,
const ParseString *relation_name,
ParseSelect *select_query,
PtrVector<ParseSubqueryTableReference> *with_clause)
: ParseStatementInsert(line_number, column_number, relation_name),
select_query_(select_query),
with_clause_(with_clause) {
}
~ParseStatementInsertSelection() override {
}
InsertType getInsertType() const override {
return InsertType::kSelection;
}
/**
* @return Gets the SELECT query.
*/
const ParseSelect* select_query() const {
return select_query_.get();
}
/**
* @brief Gets the WITH table queries.
*
* @return The parsed WITH table list.
*/
const PtrVector<ParseSubqueryTableReference>* with_clause() const {
return with_clause_.get();
}
protected:
void getFieldStringItems(
std::vector<std::string> *inline_field_names,
std::vector<std::string> *inline_field_values,
std::vector<std::string> *non_container_child_field_names,
std::vector<const ParseTreeNode*> *non_container_child_fields,
std::vector<std::string> *container_child_field_names,
std::vector<std::vector<const ParseTreeNode*>> *container_child_fields) const override {
inline_field_names->push_back("relation_name");
inline_field_values->push_back(relation_name()->value());
non_container_child_field_names->push_back("select_query");
non_container_child_fields->push_back(select_query_.get());
if (with_clause_ != nullptr && !with_clause_->empty()) {
container_child_field_names->push_back("with_clause");
container_child_fields->emplace_back();
for (const ParseSubqueryTableReference &common_subquery : *with_clause_) {
container_child_fields->back().push_back(&common_subquery);
}
}
}
private:
std::unique_ptr<ParseSelect> select_query_;
std::unique_ptr<PtrVector<ParseSubqueryTableReference>> with_clause_;
DISALLOW_COPY_AND_ASSIGN(ParseStatementInsertSelection);
};
/**
* @brief The parsed representation of a COPY FROM/COPY TO statement.
**/
class ParseStatementCopy : public ParseStatement {
public:
/**
* @brief Copy direction (FROM text file/TO text file).
*/
enum CopyDirection {
kFrom = 0,
kTo
};
/**
* @brief Constructor for a copy statement that copies a stored relation FROM
* a text file (or multiple text files, in the case that the file name
* is a GLOB pattern) / TO a text file.
*
* @param line_number Line number of the first token of this node in the SQL statement.
* @param column_number Column number of the first token of this node in the SQL statement.
* @param direction The copy direction (FROM/TO).
* @param relation_name The name of the relation.
* @param file_name The name of the file.
* @param params The optional parameters of the COPY statement.
**/
ParseStatementCopy(const int line_number,
const int column_number,
const CopyDirection direction,
ParseString *relation_name,
ParseString *file_name,
PtrList<ParseKeyValue> *params)
: ParseStatement(line_number, column_number),
direction_(direction),
relation_name_(relation_name),
file_name_(file_name),
params_(params) {
}
/**
* @brief Constructor for a copy statement that copies the result table of a
* query TO a text file.
*
* @param line_number Line number of the first token of this node in the SQL statement.
* @param column_number Column number of the first token of this node in the SQL statement.
* @param set_operation_query The set operation query.
* @param with_clause The WITH clause of common table query expressions.
* @param file_name The name of the file.
* @param params The optional parameters of the COPY statement.
**/
ParseStatementCopy(const int line_number,
const int column_number,
ParseSetOperation *set_operation_query,
PtrVector<ParseSubqueryTableReference> *with_clause,
ParseString *file_name,
PtrList<ParseKeyValue> *params)
: ParseStatement(line_number, column_number),
direction_(kTo),
set_operation_query_(set_operation_query),
with_clause_(with_clause),
file_name_(file_name),
params_(params) {
}
/**
* @brief Destructor.
*/
~ParseStatementCopy() override {
}
StatementType getStatementType() const override {
return kCopy;
}
std::string getName() const override {
return "CopyStatement";
}
/**
* @brief Get the direction (FROM text file/TO text file) of the COPY statement.
*
* return The direction of the COPY statement.
*/
const CopyDirection getCopyDirection() const {
return direction_;
}
/**
* @brief Get the name of the relation.
*
* @return The name of the relation.
**/
const ParseString* relation_name() const {
return relation_name_.get();
}
/**
* @brief Get the set operation query.
*
* @return The set operation query.
*/
const ParseSetOperation* set_operation_query() const {
return set_operation_query_.get();
}
/**
* @brief Get the WITH table queries.
*
* @return The parsed WITH table list.
*/
const PtrVector<ParseSubqueryTableReference>* with_clause() const {
return with_clause_.get();
}
/**
* @brief Get the name of the text file to import from/export to.
*
* @return The name of the text file.
**/
const ParseString* file_name() const {
return file_name_.get();
}
/**
* @brief Get the additional COPY parameters.
*
* @return The additional COPY parameters.
**/
const PtrList<ParseKeyValue>* params() const {
return params_.get();
}
protected:
void getFieldStringItems(
std::vector<std::string> *inline_field_names,
std::vector<std::string> *inline_field_values,
std::vector<std::string> *non_container_child_field_names,
std::vector<const ParseTreeNode*> *non_container_child_fields,
std::vector<std::string> *container_child_field_names,
std::vector<std::vector<const ParseTreeNode*>> *container_child_fields) const override {
inline_field_names->push_back("direction");
inline_field_values->push_back(direction_ == kFrom ? "FROM" : "TO");
inline_field_names->push_back("file");
inline_field_values->push_back(file_name_->value());
if (relation_name_ != nullptr) {
inline_field_names->push_back("relation_name");
inline_field_values->push_back(relation_name_->value());
}
if (set_operation_query_ != nullptr) {
non_container_child_field_names->push_back("set_operation_query");
non_container_child_fields->push_back(set_operation_query_.get());
}
if (with_clause_ != nullptr && !with_clause_->empty()) {
container_child_field_names->push_back("with_clause");
container_child_fields->emplace_back();
for (const ParseSubqueryTableReference &common_subquery : *with_clause_) {
container_child_fields->back().push_back(&common_subquery);
}
}
if (params_ != nullptr) {
container_child_field_names->push_back("params");
container_child_fields->emplace_back();
for (const ParseKeyValue ¶m : *params_) {
container_child_fields->back().push_back(¶m);
}
}
}
private:
const CopyDirection direction_;
// NOTE(jianqiao):
// (1) Either relation_name_ or set_operation_query_ has non-null value.
// (2) set_operation_query_ must be null for COPY FROM statement.
std::unique_ptr<ParseString> relation_name_;
std::unique_ptr<ParseSetOperation> set_operation_query_;
std::unique_ptr<PtrVector<ParseSubqueryTableReference>> with_clause_;
std::unique_ptr<ParseString> file_name_;
std::unique_ptr<PtrList<ParseKeyValue>> params_;
DISALLOW_COPY_AND_ASSIGN(ParseStatementCopy);
};
/**
* @brief The parsed representation of an UPDATE statement.
**/
class ParseStatementUpdate : public ParseStatement {
public:
/**
* @brief Constructor.
*
* @param line_number Line number of the first token of this node in the SQL statement.
* @param column_number Column number of the first token of this node in the SQL statement.
* @param relation_name The name of the relation to update.
* @param assignments A list of assignment for the attributes in the
* relation, which becomes owned by this ParseStatementUpdate.
* @param where_predicate An optional predicate from a WHERE clause in the
* UPDATE statement (may be NULL if no predicate). Becomes owned by
* this ParseStatementUpdate if non-NULL.
**/
ParseStatementUpdate(const int line_number,
const int column_number,
ParseString *relation_name,
PtrList<ParseAssignment> *assignments,
ParsePredicate *where_predicate)
: ParseStatement(line_number, column_number),
relation_name_(relation_name),
assignments_(assignments),
where_predicate_(where_predicate) {
}
/**
* @brief Destructor.