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
7namespace duckdb {
8
9JoinRelation::JoinRelation(shared_ptr<Relation> left_p, shared_ptr<Relation> right_p,
10 unique_ptr<ParsedExpression> condition_p, JoinType type)
11 : Relation(left_p->context, RelationType::JOIN_RELATION), left(move(left_p)), right(move(right_p)),
12 condition(move(condition_p)), join_type(type) {
13 if (&left->context != &right->context) {
14 throw Exception("Cannot combine LEFT and RIGHT relations of different connections!");
15 }
16 context.TryBindRelation(*this, this->columns);
17}
18
19JoinRelation::JoinRelation(shared_ptr<Relation> left_p, shared_ptr<Relation> right_p, vector<string> using_columns_p,
20 JoinType type)
21 : Relation(left_p->context, RelationType::JOIN_RELATION), left(move(left_p)), right(move(right_p)),
22 using_columns(move(using_columns_p)), join_type(type) {
23 if (&left->context != &right->context) {
24 throw Exception("Cannot combine LEFT and RIGHT relations of different connections!");
25 }
26 context.TryBindRelation(*this, this->columns);
27}
28
29unique_ptr<QueryNode> JoinRelation::GetQueryNode() {
30 auto result = make_unique<SelectNode>();
31 result->select_list.push_back(make_unique<StarExpression>());
32 result->from_table = GetTableRef();
33 return move(result);
34}
35
36unique_ptr<TableRef> JoinRelation::GetTableRef() {
37 auto join_ref = make_unique<JoinRef>();
38 join_ref->left = left->GetTableRef();
39 join_ref->right = right->GetTableRef();
40 if (condition) {
41 join_ref->condition = condition->Copy();
42 }
43 join_ref->using_columns = using_columns;
44 join_ref->type = join_type;
45 return move(join_ref);
46}
47
48const vector<ColumnDefinition> &JoinRelation::Columns() {
49 return this->columns;
50}
51
52string JoinRelation::ToString(idx_t depth) {
53 string str = RenderWhitespace(depth);
54 str = "Join";
55 return str + "\n" + left->ToString(depth + 1) + right->ToString(depth + 1);
56}
57
58} // namespace duckdb
59