| 1 | #include "duckdb/common/exception.hpp" |
|---|---|
| 2 | #include "duckdb/parser/tableref/basetableref.hpp" |
| 3 | #include "duckdb/parser/tableref/crossproductref.hpp" |
| 4 | #include "duckdb/parser/tableref/joinref.hpp" |
| 5 | #include "duckdb/parser/transformer.hpp" |
| 6 | |
| 7 | using namespace duckdb; |
| 8 | using namespace std; |
| 9 | |
| 10 | unique_ptr<TableRef> Transformer::TransformJoin(PGJoinExpr *root) { |
| 11 | auto result = make_unique<JoinRef>(); |
| 12 | switch (root->jointype) { |
| 13 | case PG_JOIN_INNER: { |
| 14 | result->type = JoinType::INNER; |
| 15 | break; |
| 16 | } |
| 17 | case PG_JOIN_LEFT: { |
| 18 | result->type = JoinType::LEFT; |
| 19 | break; |
| 20 | } |
| 21 | case PG_JOIN_FULL: { |
| 22 | result->type = JoinType::OUTER; |
| 23 | break; |
| 24 | } |
| 25 | case PG_JOIN_RIGHT: { |
| 26 | result->type = JoinType::RIGHT; |
| 27 | break; |
| 28 | } |
| 29 | case PG_JOIN_SEMI: { |
| 30 | result->type = JoinType::SEMI; |
| 31 | break; |
| 32 | } |
| 33 | default: { throw NotImplementedException("Join type %d not supported yet...\n", root->jointype); } |
| 34 | } |
| 35 | |
| 36 | // Check the type of left arg and right arg before transform |
| 37 | result->left = TransformTableRefNode(root->larg); |
| 38 | result->right = TransformTableRefNode(root->rarg); |
| 39 | |
| 40 | if (root->usingClause && root->usingClause->length > 0) { |
| 41 | // usingClause is a list of strings |
| 42 | for (auto node = root->usingClause->head; node != nullptr; node = node->next) { |
| 43 | auto target = reinterpret_cast<PGNode *>(node->data.ptr_value); |
| 44 | assert(target->type == T_PGString); |
| 45 | auto column_name = string(reinterpret_cast<PGValue *>(target)->val.str); |
| 46 | result->using_columns.push_back(column_name); |
| 47 | } |
| 48 | return move(result); |
| 49 | } |
| 50 | |
| 51 | if (!root->quals && result->using_columns.size() == 0) { // CROSS PRODUCT |
| 52 | auto cross = make_unique<CrossProductRef>(); |
| 53 | cross->left = move(result->left); |
| 54 | cross->right = move(result->right); |
| 55 | return move(cross); |
| 56 | } |
| 57 | |
| 58 | result->condition = TransformExpression(root->quals); |
| 59 | return move(result); |
| 60 | } |
| 61 |