1 | #include "duckdb/common/exception.hpp" |
---|---|
2 | #include "duckdb/parser/expression/default_expression.hpp" |
3 | #include "duckdb/parser/transformer.hpp" |
4 | |
5 | using namespace duckdb; |
6 | using namespace std; |
7 | |
8 | unique_ptr<ParsedExpression> Transformer::TransformResTarget(PGResTarget *root) { |
9 | if (!root) { |
10 | return nullptr; |
11 | } |
12 | auto expr = TransformExpression(root->val); |
13 | if (!expr) { |
14 | return nullptr; |
15 | } |
16 | if (root->name) { |
17 | expr->alias = string(root->name); |
18 | } |
19 | return expr; |
20 | } |
21 | |
22 | unique_ptr<ParsedExpression> Transformer::TransformNamedArg(PGNamedArgExpr *root) { |
23 | if (!root) { |
24 | return nullptr; |
25 | } |
26 | auto expr = TransformExpression((PGNode *)root->arg); |
27 | if (root->name) { |
28 | expr->alias = string(root->name); |
29 | } |
30 | return expr; |
31 | } |
32 | |
33 | unique_ptr<ParsedExpression> Transformer::TransformExpression(PGNode *node) { |
34 | if (!node) { |
35 | return nullptr; |
36 | } |
37 | |
38 | switch (node->type) { |
39 | case T_PGColumnRef: |
40 | return TransformColumnRef(reinterpret_cast<PGColumnRef *>(node)); |
41 | case T_PGAConst: |
42 | return TransformConstant(reinterpret_cast<PGAConst *>(node)); |
43 | case T_PGAExpr: |
44 | return TransformAExpr(reinterpret_cast<PGAExpr *>(node)); |
45 | case T_PGFuncCall: |
46 | return TransformFuncCall(reinterpret_cast<PGFuncCall *>(node)); |
47 | case T_PGBoolExpr: |
48 | return TransformBoolExpr(reinterpret_cast<PGBoolExpr *>(node)); |
49 | case T_PGTypeCast: |
50 | return TransformTypeCast(reinterpret_cast<PGTypeCast *>(node)); |
51 | case T_PGCaseExpr: |
52 | return TransformCase(reinterpret_cast<PGCaseExpr *>(node)); |
53 | case T_PGSubLink: |
54 | return TransformSubquery(reinterpret_cast<PGSubLink *>(node)); |
55 | case T_PGCoalesceExpr: |
56 | return TransformCoalesce(reinterpret_cast<PGAExpr *>(node)); |
57 | case T_PGNullTest: |
58 | return TransformNullTest(reinterpret_cast<PGNullTest *>(node)); |
59 | case T_PGResTarget: |
60 | return TransformResTarget(reinterpret_cast<PGResTarget *>(node)); |
61 | case T_PGParamRef: |
62 | return TransformParamRef(reinterpret_cast<PGParamRef *>(node)); |
63 | case T_PGNamedArgExpr: |
64 | return TransformNamedArg(reinterpret_cast<PGNamedArgExpr *>(node)); |
65 | case T_PGSQLValueFunction: |
66 | return TransformSQLValueFunction(reinterpret_cast<PGSQLValueFunction *>(node)); |
67 | case T_PGSetToDefault: |
68 | return make_unique<DefaultExpression>(); |
69 | case T_PGCollateClause: |
70 | return TransformCollateExpr(reinterpret_cast<PGCollateClause*>(node)); |
71 | default: |
72 | throw NotImplementedException("Expr of type %d not implemented\n", (int)node->type); |
73 | } |
74 | } |
75 | |
76 | bool Transformer::TransformExpressionList(PGList *list, vector<unique_ptr<ParsedExpression>> &result) { |
77 | if (!list) { |
78 | return false; |
79 | } |
80 | for (auto node = list->head; node != nullptr; node = node->next) { |
81 | auto target = reinterpret_cast<PGNode *>(node->data.ptr_value); |
82 | if (!target) { |
83 | return false; |
84 | } |
85 | auto expr = TransformExpression(target); |
86 | if (!expr) { |
87 | return false; |
88 | } |
89 | result.push_back(move(expr)); |
90 | } |
91 | return true; |
92 | } |
93 |