1#include "duckdb/main/relation/filter_relation.hpp"
2#include "duckdb/main/client_context.hpp"
3#include "duckdb/parser/query_node/select_node.hpp"
4#include "duckdb/parser/query_node/set_operation_node.hpp"
5#include "duckdb/parser/expression/conjunction_expression.hpp"
6#include "duckdb/parser/expression/star_expression.hpp"
7
8namespace duckdb {
9
10FilterRelation::FilterRelation(shared_ptr<Relation> child_p, unique_ptr<ParsedExpression> condition_p)
11 : Relation(child_p->context, RelationType::FILTER_RELATION), condition(move(condition_p)), child(move(child_p)) {
12 vector<ColumnDefinition> dummy_columns;
13 context.TryBindRelation(*this, dummy_columns);
14}
15
16unique_ptr<QueryNode> FilterRelation::GetQueryNode() {
17 auto child_ptr = child.get();
18 while (child_ptr->InheritsColumnBindings()) {
19 child_ptr = child_ptr->ChildRelation();
20 }
21 if (child_ptr->type == RelationType::JOIN_RELATION) {
22 // child node is a join: push filter into WHERE clause of select node
23 auto child_node = child->GetQueryNode();
24 assert(child_node->type == QueryNodeType::SELECT_NODE);
25 auto &select_node = (SelectNode &)*child_node;
26 if (!select_node.where_clause) {
27 select_node.where_clause = condition->Copy();
28 } else {
29 select_node.where_clause = make_unique<ConjunctionExpression>(
30 ExpressionType::CONJUNCTION_AND, move(select_node.where_clause), condition->Copy());
31 }
32 return child_node;
33 } else {
34 auto result = make_unique<SelectNode>();
35 result->select_list.push_back(make_unique<StarExpression>());
36 result->from_table = child->GetTableRef();
37 result->where_clause = condition->Copy();
38 return move(result);
39 }
40}
41
42string FilterRelation::GetAlias() {
43 return child->GetAlias();
44}
45
46const vector<ColumnDefinition> &FilterRelation::Columns() {
47 return child->Columns();
48}
49
50string FilterRelation::ToString(idx_t depth) {
51 string str = RenderWhitespace(depth) + "Filter [" + condition->ToString() + "]\n";
52 return str + child->ToString(depth + 1);
53}
54
55} // namespace duckdb
56