1#include "duckdb/main/relation/cross_product_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
9CrossProductRelation::CrossProductRelation(shared_ptr<Relation> left_p, shared_ptr<Relation> right_p)
10 : Relation(left_p->context, RelationType::CROSS_PRODUCT_RELATION), left(std::move(left_p)),
11 right(std::move(right_p)) {
12 if (left->context.GetContext() != right->context.GetContext()) {
13 throw Exception("Cannot combine LEFT and RIGHT relations of different connections!");
14 }
15 context.GetContext()->TryBindRelation(relation&: *this, result_columns&: this->columns);
16}
17
18unique_ptr<QueryNode> CrossProductRelation::GetQueryNode() {
19 auto result = make_uniq<SelectNode>();
20 result->select_list.push_back(x: make_uniq<StarExpression>());
21 result->from_table = GetTableRef();
22 return std::move(result);
23}
24
25unique_ptr<TableRef> CrossProductRelation::GetTableRef() {
26 auto cross_product_ref = make_uniq<JoinRef>(args: JoinRefType::CROSS);
27 cross_product_ref->left = left->GetTableRef();
28 cross_product_ref->right = right->GetTableRef();
29 return std::move(cross_product_ref);
30}
31
32const vector<ColumnDefinition> &CrossProductRelation::Columns() {
33 return this->columns;
34}
35
36string CrossProductRelation::ToString(idx_t depth) {
37 string str = RenderWhitespace(depth);
38 str = "Cross Product";
39 return str + "\n" + left->ToString(depth: depth + 1) + right->ToString(depth: depth + 1);
40}
41
42} // namespace duckdb
43