This repository was archived by the owner on Sep 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 618
/
Copy pathpostgresparser.cpp
2024 lines (1856 loc) · 70.4 KB
/
postgresparser.cpp
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
//===----------------------------------------------------------------------===//
//
// Peloton
//
// postgresparser.cpp
//
// Identification: src/parser/postgresparser.cpp
//
// Copyright (c) 2015-2018, Carnegie Mellon University Database Group
//
//===----------------------------------------------------------------------===//
#include "parser/postgresparser.h"
#include "expression/aggregate_expression.h"
#include "expression/case_expression.h"
#include "expression/comparison_expression.h"
#include "expression/conjunction_expression.h"
#include "expression/constant_value_expression.h"
#include "expression/function_expression.h"
#include "expression/operator_expression.h"
#include "expression/star_expression.h"
#include "expression/subquery_expression.h"
#include "expression/tuple_value_expression.h"
#include "parser/pg_list.h"
#include "parser/pg_query.h"
#include "parser/pg_trigger.h"
namespace peloton {
namespace parser {
PostgresParser::PostgresParser() {}
PostgresParser::~PostgresParser() {}
// This function takes a Postgres Alias parsenode and extracts the name of
// the alias and return a duplicate of the string.
std::string PostgresParser::AliasTransform(Alias *root) {
if (root == nullptr) {
return "";
}
return root->aliasname;
}
// This function takes a Postgres JoinExpr parsenode and transfers it to a
// Peloton JoinDefinition object. Depends on AExprTransform and
// BoolExprTransform.
parser::JoinDefinition *PostgresParser::JoinTransform(JoinExpr *root) {
parser::JoinDefinition *result = nullptr;
// Natrual join is not supported
if ((root->jointype > 4) || (root->isNatural)) {
return nullptr;
}
LOG_TRACE("Join type is %d\n", root->jointype);
result = new parser::JoinDefinition();
switch (root->jointype) {
case JOIN_INNER: {
result->type = StringToJoinType("inner");
break;
}
case JOIN_LEFT: {
result->type = StringToJoinType("left");
break;
}
case JOIN_FULL: {
result->type = StringToJoinType("outer");
break;
}
case JOIN_RIGHT: {
result->type = StringToJoinType("right");
break;
}
case JOIN_SEMI: {
result->type = StringToJoinType("semi");
break;
}
default: {
delete result;
throw NotImplementedException(StringUtil::Format(
"Join type %d not supported yet...\n", root->jointype));
}
}
// Check the type of left arg and right arg before transform
if (root->larg->type == T_RangeVar) {
result->left.reset(
RangeVarTransform(reinterpret_cast<RangeVar *>(root->larg)));
} else if (root->larg->type == T_RangeSubselect) {
result->left.reset(RangeSubselectTransform(
reinterpret_cast<RangeSubselect *>(root->larg)));
} else if (root->larg->type == T_JoinExpr) {
TableRef *l_table_ref = new TableRef(TableReferenceType::JOIN);
l_table_ref->join.reset(
JoinTransform(reinterpret_cast<JoinExpr *>(root->larg)));
result->left.reset(l_table_ref);
} else {
delete result;
throw NotImplementedException(StringUtil::Format(
"Join arg type %d not supported yet...\n", root->larg->type));
}
if (root->rarg->type == T_RangeVar) {
result->right.reset(
RangeVarTransform(reinterpret_cast<RangeVar *>(root->rarg)));
} else if (root->rarg->type == T_RangeSubselect) {
result->right.reset(RangeSubselectTransform(
reinterpret_cast<RangeSubselect *>(root->rarg)));
} else if (root->rarg->type == T_JoinExpr) {
TableRef *r_table_ref = new TableRef(TableReferenceType::JOIN);
r_table_ref->join.reset(
JoinTransform(reinterpret_cast<JoinExpr *>(root->rarg)));
result->right.reset(r_table_ref);
} else {
delete result;
throw NotImplementedException(StringUtil::Format(
"Join arg type %d not supported yet...\n", root->larg->type));
return nullptr;
}
// transform the quals, depends on AExprTranform and BoolExprTransform
switch (root->quals->type) {
case T_A_Expr: {
result->condition.reset(
AExprTransform(reinterpret_cast<A_Expr *>(root->quals)));
break;
}
case T_BoolExpr: {
result->condition.reset(
BoolExprTransform(reinterpret_cast<BoolExpr *>(root->quals)));
break;
}
default: {
delete result;
throw NotImplementedException(StringUtil::Format(
"Join quals type %d not supported yet...\n", root->larg->type));
}
}
return result;
}
// This function takes in a single Postgres RangeVar parsenode and transfer
// it into a Peloton TableRef object.
parser::TableRef *PostgresParser::RangeVarTransform(RangeVar *root) {
parser::TableRef *result =
new parser::TableRef(StringToTableReferenceType("name"));
result->table_info_.reset(new parser::TableInfo());
// parse alias
result->alias = AliasTransform(root->alias);
// add table name
if (root->relname) {
result->table_info_->table_name = root->relname;
}
// add schema(namespace) name
if (root->schemaname) {
result->table_info_->schema_name = root->schemaname;
}
// add database name
if (root->catalogname) {
result->table_info_->database_name = root->catalogname;
}
return result;
}
// This function takes in a single Postgres RangeVar parsenode and transfer
// it into a Peloton TableRef object.
parser::TableRef *PostgresParser::RangeSubselectTransform(
RangeSubselect *root) {
auto result = new parser::TableRef(StringToTableReferenceType("select"));
result->select = reinterpret_cast<parser::SelectStatement *>(
SelectTransform(reinterpret_cast<SelectStmt *>(root->subquery)));
result->alias = AliasTransform(root->alias);
if (result->select == nullptr) {
delete result;
result = nullptr;
}
return result;
}
// Get in a target list and check if is with variables
bool IsTargetListWithVariable(List *target_list) {
// The only valid situation of a null from list is that all targets are
// constant
for (auto cell = target_list->head; cell != nullptr; cell = cell->next) {
ResTarget *target = reinterpret_cast<ResTarget *>(cell->data.ptr_value);
LOG_TRACE("Type: %d", target->type);
// Bypass the target nodes with type:
// constant("SELECT 1;"), expression ("SELECT 1 + 1"),
// and boolean ("SELECT 1!=2;");
// TODO: We may want to see if there are more types to check.
switch (target->val->type) {
case T_A_Const:
case T_A_Expr:
case T_BoolExpr:
continue;
default:
LOG_DEBUG("HERE");
return true;
}
}
return false;
}
// This fucntion takes in fromClause of a Postgres SelectStmt and transfers
// into a Peloton TableRef object.
// TODO: support select from multiple sources, nested queries, various joins
parser::TableRef *PostgresParser::FromTransform(SelectStmt *select_root) {
// now support select from only one sources
List *root = select_root->fromClause;
/* Statement like 'SELECT *;' cannot detect by postgres parser and would lead
* to
* a null list of from clause*/
if (root == nullptr) return nullptr;
parser::TableRef *result = nullptr;
Node *node;
if (root->length > 1) {
result = new TableRef(StringToTableReferenceType("CROSS_PRODUCT"));
for (auto cell = root->head; cell != nullptr; cell = cell->next) {
node = reinterpret_cast<Node *>(cell->data.ptr_value);
switch (node->type) {
case T_RangeVar: {
result->list.push_back(std::unique_ptr<TableRef>(
RangeVarTransform(reinterpret_cast<RangeVar *>(node))));
break;
}
case T_RangeSubselect: {
result->list.push_back(
std::unique_ptr<TableRef>(RangeSubselectTransform(
reinterpret_cast<RangeSubselect *>(node))));
break;
}
default: {
delete result;
throw NotImplementedException(StringUtil::Format(
"From Type %d not supported yet...", node->type));
}
}
}
return result;
}
node = reinterpret_cast<Node *>(root->head->data.ptr_value);
switch (node->type) {
case T_RangeVar: {
result = RangeVarTransform(reinterpret_cast<RangeVar *>(node));
break;
}
case T_JoinExpr: {
result = new parser::TableRef(StringToTableReferenceType("join"));
result->join.reset(JoinTransform(reinterpret_cast<JoinExpr *>(node)));
if (result->join == nullptr) {
delete result;
result = nullptr;
}
break;
}
case T_RangeSubselect: {
result =
RangeSubselectTransform(reinterpret_cast<RangeSubselect *>(node));
break;
}
default: {
throw NotImplementedException(
StringUtil::Format("From Type %d not supported yet...", node->type));
}
}
return result;
}
// This function takes in a Postgres ColumnRef parsenode and transfer into
// a Peloton tuple value expression.
expression::AbstractExpression *PostgresParser::ColumnRefTransform(
ColumnRef *root) {
expression::AbstractExpression *result = nullptr;
List *fields = root->fields;
switch ((reinterpret_cast<Node *>(fields->head->data.ptr_value))->type) {
case T_String: {
if (fields->length == 1) {
result = new expression::TupleValueExpression(std::string(
(reinterpret_cast<value *>(fields->head->data.ptr_value))
->val.str));
} else {
result = new expression::TupleValueExpression(
std::string(
(reinterpret_cast<value *>(fields->head->next->data.ptr_value))
->val.str),
std::string(
(reinterpret_cast<value *>(fields->head->data.ptr_value))
->val.str));
}
break;
}
case T_A_Star: {
result = new expression::StarExpression();
break;
}
default: {
throw NotImplementedException(StringUtil::Format(
"Type %d of ColumnRef not handled yet...\n",
(reinterpret_cast<Node *>(fields->head->data.ptr_value))->type));
}
}
return result;
}
// This function takes in a Postgres ParamRef parsenode and transfer into
// a Peloton tuple value expression.
expression::AbstractExpression *PostgresParser::ParamRefTransform(
ParamRef *root) {
// LOG_INFO("Parameter number: %d", root->number);
expression::AbstractExpression *result =
new expression::ParameterValueExpression(root->number - 1);
return result;
}
// This function takes in the Case Expression of a Postgres SelectStmt
// parsenode and transfers it into Peloton AbstractExpression.
expression::AbstractExpression *PostgresParser::CaseExprTransform(
CaseExpr *root) {
if (root == nullptr) {
return nullptr;
}
// Transform the CASE argument
auto arg_expr = ExprTransform(reinterpret_cast<Node *>(root->arg));
// Transform the WHEN conditions
std::vector<expression::CaseExpression::WhenClause> clauses;
for (auto cell = root->args->head; cell != nullptr; cell = cell->next) {
CaseWhen *w = reinterpret_cast<CaseWhen *>(cell->data.ptr_value);
// When condition
auto when_expr = ExprTransform(reinterpret_cast<Node *>(w->expr));
;
// Result
auto result_expr = ExprTransform(reinterpret_cast<Node *>(w->result));
// Build When Clause and add it to the list
clauses.push_back(expression::CaseExpression::WhenClause(
expression::CaseExpression::AbsExprPtr(when_expr),
expression::CaseExpression::AbsExprPtr(result_expr)));
}
// Transform the default result
auto defresult_expr =
ExprTransform(reinterpret_cast<Node *>(root->defresult));
// Build Case Expression
return arg_expr != nullptr
? new expression::CaseExpression(
clauses.at(0).second.get()->GetValueType(),
expression::CaseExpression::AbsExprPtr(arg_expr), clauses,
expression::CaseExpression::AbsExprPtr(defresult_expr))
: new expression::CaseExpression(
clauses.at(0).second.get()->GetValueType(), clauses,
expression::CaseExpression::AbsExprPtr(defresult_expr));
}
// This function takes in groupClause and havingClause of a Postgres SelectStmt
// transfers into a Peloton GroupByDescription object.
parser::GroupByDescription *PostgresParser::GroupByTransform(List *group,
Node *having) {
if (group == nullptr) {
return nullptr;
}
parser::GroupByDescription *result = new parser::GroupByDescription();
for (auto cell = group->head; cell != nullptr; cell = cell->next) {
Node *temp = reinterpret_cast<Node *>(cell->data.ptr_value);
try {
result->columns.push_back(
std::unique_ptr<expression::AbstractExpression>(ExprTransform(temp)));
} catch (NotImplementedException e) {
delete result;
throw NotImplementedException(StringUtil::Format(
"Exception thrown in group by expr:\n%s", e.what()));
}
}
// having clauses not implemented yet, depends on AExprTransform
if (having != nullptr) {
try {
result->having.reset(ExprTransform(having));
} catch (NotImplementedException e) {
delete result;
throw NotImplementedException(
StringUtil::Format("Exception thrown in having expr:\n%s", e.what()));
}
}
return result;
}
// This function takes in the sortClause part of a Postgres SelectStmt
// parsenode and transfers it into a list of Peloton OrderDescription objects
// std::vector<parser::OrderDescription>* PostgresParser::OrderByTransform(List*
// order) {
parser::OrderDescription *PostgresParser::OrderByTransform(List *order) {
if (order == nullptr) {
return nullptr;
}
parser::OrderDescription *result = new OrderDescription();
std::vector<OrderType> types = std::vector<OrderType>();
std::vector<std::unique_ptr<expression::AbstractExpression>> exprs =
std::vector<std::unique_ptr<expression::AbstractExpression>>();
for (auto cell = order->head; cell != nullptr; cell = cell->next) {
Node *temp = reinterpret_cast<Node *>(cell->data.ptr_value);
if (temp->type == T_SortBy) {
SortBy *sort = reinterpret_cast<SortBy *>(temp);
Node *target = sort->node;
if (sort->sortby_dir == SORTBY_ASC || sort->sortby_dir == SORTBY_DEFAULT)
types.push_back(parser::kOrderAsc);
if (sort->sortby_dir == SORTBY_DESC) types.push_back(parser::kOrderDesc);
expression::AbstractExpression *expr = nullptr;
try {
expr = ExprTransform(target);
} catch (NotImplementedException e) {
throw NotImplementedException(StringUtil::Format(
"Exception thrown in order by expr:\n%s", e.what()));
}
exprs.push_back(std::unique_ptr<expression::AbstractExpression>(expr));
} else {
throw NotImplementedException(
StringUtil::Format("ORDER BY list member type %d\n", temp->type));
}
}
result->exprs = std::move(exprs);
result->types = std::move(types);
return result;
}
// This function takes in a Posgres value parsenode and transfers it into
// a Peloton constant value expression.
expression::AbstractExpression *PostgresParser::ValueTransform(value val) {
expression::AbstractExpression *result = nullptr;
switch (val.type) {
case T_Integer:
result = new expression::ConstantValueExpression(
type::ValueFactory::GetIntegerValue((int32_t)val.val.ival));
break;
case T_String:
result = new expression::ConstantValueExpression(
type::ValueFactory::GetVarcharValue(std::string(val.val.str)));
break;
case T_Float:
result = new expression::ConstantValueExpression(
type::ValueFactory::GetDecimalValue(
std::stod(std::string(val.val.str))));
break;
case T_Null:
result = new expression::ConstantValueExpression(
type::ValueFactory::GetNullValueByType(type::TypeId::INTEGER));
break;
default:
throw NotImplementedException(
StringUtil::Format("Value type %d not supported yet...\n", val.type));
}
return result;
}
// This function takes in a Posgres A_Const parsenode and transfers it into
// a Peloton constant value expression.
expression::AbstractExpression *PostgresParser::ConstTransform(A_Const *root) {
return ValueTransform(root->val);
}
expression::AbstractExpression *PostgresParser::TypeCastTransform(
TypeCast *root) {
expression::AbstractExpression *result = nullptr;
type::Value source_value;
switch (root->arg->type) {
case T_A_Const: {
std::unique_ptr<expression::ConstantValueExpression> source_expr_ptr(
reinterpret_cast<expression::ConstantValueExpression *>(
ConstTransform(reinterpret_cast<A_Const *>(root->arg))));
source_value = source_expr_ptr.get()->GetValue();
break;
}
default:
throw NotImplementedException(StringUtil::Format(
"TypeCast Source of type %d not supported yet...\n",
root->arg->type));
}
TypeName *type_name = root->typeName;
char *name =
(reinterpret_cast<value *>(type_name->names->tail->data.ptr_value)
->val.str);
type::VarlenType temp(StringToTypeId("INVALID"));
result = new expression::ConstantValueExpression(
temp.CastAs(source_value, ColumnDefinition::StrToValueType(name)));
return result;
}
expression::AbstractExpression *PostgresParser::FuncCallTransform(
FuncCall *root) {
expression::AbstractExpression *result = nullptr;
std::string fun_name = StringUtil::Lower(
(reinterpret_cast<value *>(root->funcname->head->data.ptr_value))
->val.str);
if (!IsAggregateFunction(fun_name)) {
// Normal functions (i.e. built-in functions or UDFs)
fun_name = (reinterpret_cast<value *>(root->funcname->tail->data.ptr_value))
->val.str;
std::vector<expression::AbstractExpression *> children;
if (root->args != nullptr) {
for (auto cell = root->args->head; cell != nullptr; cell = cell->next) {
auto expr_node = (Node *)cell->data.ptr_value;
expression::AbstractExpression *child_expr = nullptr;
try {
child_expr = ExprTransform(expr_node);
} catch (NotImplementedException e) {
throw NotImplementedException(StringUtil::Format(
"Exception thrown in function expr:\n%s", e.what()));
}
children.push_back(child_expr);
}
}
result = new expression::FunctionExpression(fun_name.c_str(), children);
} else {
// Aggregate function
auto agg_fun_type = StringToExpressionType("AGGREGATE_" + fun_name);
if (root->agg_star) {
expression::AbstractExpression *children =
new expression::StarExpression();
result =
new expression::AggregateExpression(agg_fun_type, false, children);
} else {
if (root->args->length < 2) {
// auto children_expr_list = TargetTransform(root->args);
expression::AbstractExpression *child;
auto expr_node = (Node *)root->args->head->data.ptr_value;
try {
child = ExprTransform(expr_node);
} catch (NotImplementedException e) {
throw NotImplementedException(StringUtil::Format(
"Exception thrown in aggregation function:\n%s", e.what()));
}
result = new expression::AggregateExpression(agg_fun_type,
root->agg_distinct, child);
} else {
throw NotImplementedException(
"Aggregation over multiple columns not supported yet...\n");
}
}
}
return result;
}
// This function takes in the whereClause part of a Postgres SelectStmt
// parsenode and transfers it into the select_list of a Peloton SelectStatement.
// It checks the type of each target and call the corresponding helpers.
std::vector<std::unique_ptr<expression::AbstractExpression>>
*PostgresParser::TargetTransform(List *root) {
// Statement like 'SELECT;' cannot detect by postgres parser and would lead to
// null list
if (root == nullptr) {
throw ParserException("Error parsing SQL statement");
}
auto result =
new std::vector<std::unique_ptr<expression::AbstractExpression>>();
for (auto cell = root->head; cell != nullptr; cell = cell->next) {
ResTarget *target = reinterpret_cast<ResTarget *>(cell->data.ptr_value);
expression::AbstractExpression *expr = nullptr;
try {
expr = ExprTransform(target->val);
} catch (NotImplementedException e) {
throw NotImplementedException(
StringUtil::Format("Exception thrown in target val:\n%s", e.what()));
}
if (target->name != nullptr) expr->alias = target->name;
result->push_back(std::unique_ptr<expression::AbstractExpression>(expr));
}
return result;
}
// This function takes in a Postgres BoolExpr parsenode and transfers into
// a Peloton conjunction expression.
expression::AbstractExpression *PostgresParser::BoolExprTransform(
BoolExpr *root) {
expression::AbstractExpression *result = nullptr;
expression::AbstractExpression *next = nullptr;
for (auto cell = root->args->head; cell != nullptr; cell = cell->next) {
Node *node = reinterpret_cast<Node *>(cell->data.ptr_value);
try {
next = ExprTransform(node);
} catch (NotImplementedException e) {
throw NotImplementedException(StringUtil::Format(
"Exception thrown in boolean expr:\n%s", e.what()));
}
switch (root->boolop) {
case AND_EXPR: {
if (result == nullptr)
result = next;
else
result = new expression::ConjunctionExpression(
StringToExpressionType("CONJUNCTION_AND"), result, next);
break;
}
case OR_EXPR: {
if (result == nullptr)
result = next;
else
result = new expression::ConjunctionExpression(
StringToExpressionType("CONJUNCTION_OR"), result, next);
break;
}
case NOT_EXPR: {
result = new expression::OperatorExpression(
StringToExpressionType("OPERATOR_NOT"), StringToTypeId("INVALID"),
next, nullptr);
break;
}
}
}
return result;
}
expression::AbstractExpression *PostgresParser::ExprTransform(Node *node) {
if (node == nullptr) {
return nullptr;
}
expression::AbstractExpression *expr = nullptr;
switch (node->type) {
case T_ColumnRef: {
expr = ColumnRefTransform(reinterpret_cast<ColumnRef *>(node));
break;
}
case T_A_Const: {
expr = ConstTransform(reinterpret_cast<A_Const *>(node));
break;
}
case T_A_Expr: {
expr = AExprTransform(reinterpret_cast<A_Expr *>(node));
break;
}
case T_ParamRef: {
expr = ParamRefTransform(reinterpret_cast<ParamRef *>(node));
break;
}
case T_FuncCall: {
expr = FuncCallTransform(reinterpret_cast<FuncCall *>(node));
break;
}
case T_BoolExpr: {
expr = BoolExprTransform(reinterpret_cast<BoolExpr *>(node));
break;
}
case T_CaseExpr: {
expr = CaseExprTransform(reinterpret_cast<CaseExpr *>(node));
break;
}
case T_SubLink: {
expr = SubqueryExprTransform(reinterpret_cast<SubLink *>(node));
break;
}
case T_NullTest: {
expr = NullTestTransform(reinterpret_cast<NullTest *>(node));
break;
}
case T_TypeCast: {
expr = TypeCastTransform(reinterpret_cast<TypeCast *>(node));
break;
}
default: {
throw NotImplementedException(StringUtil::Format(
"Expr of type %d not supported yet...\n", node->type));
}
}
return expr;
}
// This function takes in a Postgres A_Expr parsenode and transfers
// it into Peloton AbstractExpression.
// TODO: the whole function, needs a function that transforms strings
// of operators to Peloton expression type (e.g. ">" to COMPARE_GREATERTHAN)
expression::AbstractExpression *PostgresParser::AExprTransform(A_Expr *root) {
if (root == nullptr) {
return nullptr;
}
LOG_TRACE("A_Expr type: %d\n", root->type);
UNUSED_ATTRIBUTE expression::AbstractExpression *result = nullptr;
UNUSED_ATTRIBUTE ExpressionType target_type;
const char *name =
(reinterpret_cast<value *>(root->name->head->data.ptr_value))->val.str;
if ((root->kind) != AEXPR_DISTINCT) {
target_type = StringToExpressionType(std::string(name));
} else {
target_type = StringToExpressionType("COMPARE_DISTINCT_FROM");
}
if (target_type == ExpressionType::INVALID) {
throw NotImplementedException(
StringUtil::Format("COMPARE type %s not supported yet...\n", name));
}
expression::AbstractExpression *left_expr = nullptr;
expression::AbstractExpression *right_expr = nullptr;
try {
left_expr = ExprTransform(root->lexpr);
} catch (NotImplementedException e) {
throw NotImplementedException(
StringUtil::Format("Exception thrown in left expr:\n%s", e.what()));
}
try {
right_expr = ExprTransform(root->rexpr);
} catch (NotImplementedException e) {
delete left_expr;
throw NotImplementedException(
StringUtil::Format("Exception thrown in right expr:\n%s", e.what()));
}
int type_id = static_cast<int>(target_type);
if (type_id <= 6) {
result = new expression::OperatorExpression(
target_type, StringToTypeId("INVALID"), left_expr, right_expr);
} else if (((10 <= type_id) && (type_id <= 17)) || (type_id == 20)) {
result = new expression::ComparisonExpression(target_type, left_expr,
right_expr);
} else {
delete left_expr;
delete right_expr;
throw NotImplementedException(StringUtil::Format(
"A_Expr Transform for type %d is not implemented yet...\n", type_id));
}
return result;
}
expression::AbstractExpression *PostgresParser::SubqueryExprTransform(
SubLink *node) {
if (node == nullptr) {
return nullptr;
}
expression::AbstractExpression *expr = nullptr;
auto select_stmt =
SelectTransform(reinterpret_cast<SelectStmt *>(node->subselect));
auto subquery_expr = new expression::SubqueryExpression();
subquery_expr->SetSubSelect(reinterpret_cast<SelectStatement *>(select_stmt));
switch (node->subLinkType) {
case ANY_SUBLINK: {
auto col_expr = ExprTransform(node->testexpr);
expr = new expression::ComparisonExpression(ExpressionType::COMPARE_IN,
col_expr, subquery_expr);
break;
}
case EXISTS_SUBLINK: {
expr = new expression::OperatorExpression(ExpressionType::OPERATOR_EXISTS,
type::TypeId::BOOLEAN,
subquery_expr, nullptr);
break;
}
case EXPR_SUBLINK: {
expr = subquery_expr;
break;
}
default: {
throw NotImplementedException(StringUtil::Format(
"Expr of type %d not supported yet...\n", node->subLinkType));
}
}
return expr;
}
// This function takes in a Postgres NullTest primnode and transfers
// it into Peloton AbstractExpression.
expression::AbstractExpression *PostgresParser::NullTestTransform(
NullTest *root) {
if (root == nullptr) {
return nullptr;
}
expression::AbstractExpression *result = nullptr;
ExpressionType target_type = ExpressionType::OPERATOR_IS_NULL;
if (root->nulltesttype == IS_NULL) {
target_type = ExpressionType::OPERATOR_IS_NULL;
} else if (root->nulltesttype == IS_NOT_NULL) {
target_type = ExpressionType::OPERATOR_IS_NOT_NULL;
}
expression::AbstractExpression *arg_expr = nullptr;
switch (root->arg->type) {
case T_ColumnRef: {
arg_expr = ColumnRefTransform(reinterpret_cast<ColumnRef *>(root->arg));
break;
}
case T_A_Const: {
arg_expr = ConstTransform(reinterpret_cast<A_Const *>(root->arg));
break;
}
case T_A_Expr: {
arg_expr = AExprTransform(reinterpret_cast<A_Expr *>(root->arg));
break;
}
case T_ParamRef: {
arg_expr = ParamRefTransform(reinterpret_cast<ParamRef *>(root->arg));
break;
}
default: {
throw NotImplementedException(StringUtil::Format(
"Arg expr of type %d not supported yet...\n", root->arg->type));
}
}
result = new expression::OperatorExpression(
target_type, type::TypeId::BOOLEAN, arg_expr, nullptr);
return result;
}
// This function takes in the whereClause part of a Postgres SelectStmt
// parsenode and transfers it into Peloton AbstractExpression.
expression::AbstractExpression *PostgresParser::WhereTransform(Node *root) {
if (root == nullptr) {
return nullptr;
}
expression::AbstractExpression *result = nullptr;
try {
result = ExprTransform(root);
} catch (NotImplementedException e) {
throw NotImplementedException(
StringUtil::Format("Exception thrown in WHERE:\n%s", e.what()));
}
return result;
}
// This function takes in the whenClause part of a Postgres CreateTrigStmt
// parsenode and transfers it into Peloton AbstractExpression.
expression::AbstractExpression *PostgresParser::WhenTransform(Node *root) {
if (root == nullptr) {
return nullptr;
}
expression::AbstractExpression *result = nullptr;
switch (root->type) {
case T_A_Expr: {
result = AExprTransform(reinterpret_cast<A_Expr *>(root));
break;
}
case T_BoolExpr: {
result = BoolExprTransform(reinterpret_cast<BoolExpr *>(root));
break;
}
default: {
throw NotImplementedException(StringUtil::Format(
"WHEN of type %d not supported yet...", root->type));
}
}
return result;
}
// This helper function takes in a Postgres ColumnDef object and transforms
// it into a Peloton ColumnDefinition object. The result of the transformation
// is also stored into the provided statement
void PostgresParser::ColumnDefTransform(ColumnDef *root,
parser::CreateStatement *stmt) {
TypeName *type_name = root->typeName;
char *name =
(reinterpret_cast<value *>(type_name->names->tail->data.ptr_value)
->val.str);
parser::ColumnDefinition *result = nullptr;
parser::ColumnDefinition::DataType data_type =
parser::ColumnDefinition::StrToDataType(name);
// Transform Varchar len
result = new ColumnDefinition(root->colname, data_type);
if (type_name->typmods) {
Node *node =
reinterpret_cast<Node *>(type_name->typmods->head->data.ptr_value);
if (node->type == T_A_Const) {
if (reinterpret_cast<A_Const *>(node)->val.type != T_Integer) {
delete result;
throw NotImplementedException(
StringUtil::Format("typmods of type %d not supported yet...\n",
reinterpret_cast<A_Const *>(node)->val.type));
}
result->varlen =
static_cast<size_t>(reinterpret_cast<A_Const *>(node)->val.val.ival);
} else {
delete result;
throw NotImplementedException(StringUtil::Format(
"typmods of type %d not supported yet...\n", node->type));
}
}
// Transform Per-column constraints
if (root->constraints) {
for (auto cell = root->constraints->head; cell != nullptr;
cell = cell->next) {
auto constraint = reinterpret_cast<Constraint *>(cell->data.ptr_value);
if (constraint->contype == CONSTR_PRIMARY)
result->primary = true;
else if (constraint->contype == CONSTR_NOTNULL)
result->not_null = true;
else if (constraint->contype == CONSTR_UNIQUE)
result->unique = true;
else if (constraint->contype == CONSTR_FOREIGN) {
// Transform foreign key attributes
// additionally, add a special ColumnDefinition to the list!
auto col = new ColumnDefinition(ColumnDefinition::DataType::FOREIGN);
col->foreign_key_source.emplace_back(root->colname);
if (constraint->pk_attrs != nullptr) {
auto attr_cell = constraint->pk_attrs->head;
value *attr_val =
reinterpret_cast<value *>(attr_cell->data.ptr_value);
col->foreign_key_sink.emplace_back(attr_val->val.str);
} else {
// Must specify the referenced columns
delete result;
delete col;
throw NotImplementedException(
StringUtil::Format("Foreign key columns not specified."));
}
// Update Reference Table
col->fk_sink_table_name = constraint->pktable->relname;
// Action type
col->foreign_key_delete_action =
CharToActionType(constraint->fk_del_action);
col->foreign_key_update_action =
CharToActionType(constraint->fk_upd_action);
// Match type
col->foreign_key_match_type = CharToMatchType(constraint->fk_matchtype);
stmt->foreign_keys.push_back(std::unique_ptr<ColumnDefinition>(col));
} else if (constraint->contype == CONSTR_DEFAULT) {
try {
result->default_value.reset(ExprTransform(constraint->raw_expr));
} catch (NotImplementedException e) {
delete result;
throw NotImplementedException(StringUtil::Format(
"Exception thrown in default expr:\n%s", e.what()));
}
} else if (constraint->contype == CONSTR_CHECK) {
try {
result->check_expression.reset(ExprTransform(constraint->raw_expr));
} catch (NotImplementedException e) {
delete result;
throw NotImplementedException(StringUtil::Format(
"Exception thrown in check expr:\n%s", e.what()));
}
}
}
}
stmt->columns.push_back(std::unique_ptr<ColumnDefinition>(result));
}
// This function takes in a Postgres CreateStmt parsenode
// and transfers into a Peloton CreateStatement parsenode.
// Please refer to parser/parsenode.h for the definition of
// CreateStmt parsenodes.
parser::SQLStatement *PostgresParser::CreateTransform(CreateStmt *root) {
UNUSED_ATTRIBUTE CreateStmt *temp = root;
parser::CreateStatement *result =
new CreateStatement(CreateStatement::CreateType::kTable);
RangeVar *relation = root->relation;
result->table_info_.reset(new parser::TableInfo());
if (relation->relname) {
result->table_info_->table_name = relation->relname;
}
if (relation->schemaname) {
result->table_info_->schema_name = relation->schemaname;
}
if (relation->catalogname) {
result->table_info_->database_name = relation->catalogname;
}
std::unordered_set<std::string> primary_keys;
for (auto cell = root->tableElts->head; cell != nullptr; cell = cell->next) {
Node *node = reinterpret_cast<Node *>(cell->data.ptr_value);
if ((node->type) == T_ColumnDef) {
// Transform Regular Column
try {
ColumnDefTransform(reinterpret_cast<ColumnDef *>(node), result);
} catch (NotImplementedException e) {
delete result;
throw e;
}
} else if (node->type == T_Constraint) {
// Transform Constraints
auto constraint = reinterpret_cast<Constraint *>(node);
if (constraint->contype == CONSTR_PRIMARY) {
for (auto key_cell = constraint->keys->head; key_cell != nullptr;
key_cell = key_cell->next) {
primary_keys.emplace(
reinterpret_cast<value *>(key_cell->data.ptr_value)->val.str);
}