1 | #include "duckdb/main/relation/join_relation.hpp" |
2 | #include "duckdb/main/client_context.hpp" |
3 | #include "duckdb/parser/query_node/select_node.hpp" |
4 | #include "duckdb/parser/expression/star_expression.hpp" |
5 | #include "duckdb/parser/tableref/joinref.hpp" |
6 | #include "duckdb/common/enum_util.hpp" |
7 | |
8 | namespace duckdb { |
9 | |
10 | JoinRelation::JoinRelation(shared_ptr<Relation> left_p, shared_ptr<Relation> right_p, |
11 | unique_ptr<ParsedExpression> condition_p, JoinType type) |
12 | : Relation(left_p->context, RelationType::JOIN_RELATION), left(std::move(left_p)), right(std::move(right_p)), |
13 | condition(std::move(condition_p)), join_type(type) { |
14 | if (left->context.GetContext() != right->context.GetContext()) { |
15 | throw Exception("Cannot combine LEFT and RIGHT relations of different connections!" ); |
16 | } |
17 | context.GetContext()->TryBindRelation(relation&: *this, result_columns&: this->columns); |
18 | } |
19 | |
20 | JoinRelation::JoinRelation(shared_ptr<Relation> left_p, shared_ptr<Relation> right_p, vector<string> using_columns_p, |
21 | JoinType type) |
22 | : Relation(left_p->context, RelationType::JOIN_RELATION), left(std::move(left_p)), right(std::move(right_p)), |
23 | using_columns(std::move(using_columns_p)), join_type(type) { |
24 | if (left->context.GetContext() != right->context.GetContext()) { |
25 | throw Exception("Cannot combine LEFT and RIGHT relations of different connections!" ); |
26 | } |
27 | context.GetContext()->TryBindRelation(relation&: *this, result_columns&: this->columns); |
28 | } |
29 | |
30 | unique_ptr<QueryNode> JoinRelation::GetQueryNode() { |
31 | auto result = make_uniq<SelectNode>(); |
32 | result->select_list.push_back(x: make_uniq<StarExpression>()); |
33 | result->from_table = GetTableRef(); |
34 | return std::move(result); |
35 | } |
36 | |
37 | unique_ptr<TableRef> JoinRelation::GetTableRef() { |
38 | auto join_ref = make_uniq<JoinRef>(args: JoinRefType::REGULAR); |
39 | join_ref->left = left->GetTableRef(); |
40 | join_ref->right = right->GetTableRef(); |
41 | if (condition) { |
42 | join_ref->condition = condition->Copy(); |
43 | } |
44 | join_ref->using_columns = using_columns; |
45 | join_ref->type = join_type; |
46 | return std::move(join_ref); |
47 | } |
48 | |
49 | const vector<ColumnDefinition> &JoinRelation::Columns() { |
50 | return this->columns; |
51 | } |
52 | |
53 | string JoinRelation::ToString(idx_t depth) { |
54 | string str = RenderWhitespace(depth); |
55 | str += "Join " + EnumUtil::ToString(value: join_type); |
56 | if (condition) { |
57 | str += " " + condition->GetName(); |
58 | } |
59 | |
60 | return str + "\n" + left->ToString(depth: depth + 1) + "\n" + right->ToString(depth: depth + 1); |
61 | } |
62 | |
63 | } // namespace duckdb |
64 | |