1#include "duckdb/optimizer/filter_pushdown.hpp"
2#include "duckdb/planner/expression/bound_columnref_expression.hpp"
3#include "duckdb/planner/expression_iterator.hpp"
4#include "duckdb/planner/operator/logical_aggregate.hpp"
5#include "duckdb/planner/operator/logical_empty_result.hpp"
6#include "duckdb/planner/operator/logical_join.hpp"
7
8using namespace duckdb;
9using namespace std;
10
11using Filter = FilterPushdown::Filter;
12
13static unique_ptr<Expression> ReplaceGroupBindings(LogicalAggregate &proj, unique_ptr<Expression> expr) {
14 if (expr->type == ExpressionType::BOUND_COLUMN_REF) {
15 auto &colref = (BoundColumnRefExpression &)*expr;
16 assert(colref.binding.table_index == proj.group_index);
17 assert(colref.binding.column_index < proj.groups.size());
18 assert(colref.depth == 0);
19 // replace the binding with a copy to the expression at the referenced index
20 return proj.groups[colref.binding.column_index]->Copy();
21 }
22 ExpressionIterator::EnumerateChildren(*expr, [&](unique_ptr<Expression> child) -> unique_ptr<Expression> {
23 return ReplaceGroupBindings(proj, move(child));
24 });
25 return expr;
26}
27
28unique_ptr<LogicalOperator> FilterPushdown::PushdownAggregate(unique_ptr<LogicalOperator> op) {
29 assert(op->type == LogicalOperatorType::AGGREGATE_AND_GROUP_BY);
30 auto &aggr = (LogicalAggregate &)*op;
31
32 // pushdown into AGGREGATE and GROUP BY
33 // we cannot push expressions that refer to the aggregate
34 FilterPushdown child_pushdown(optimizer);
35 for (idx_t i = 0; i < filters.size(); i++) {
36 auto &f = *filters[i];
37 // check if the aggregate is in the set
38 if (f.bindings.find(aggr.aggregate_index) == f.bindings.end()) {
39 // no aggregate! we can push this down
40 // rewrite any group bindings within the filter
41 f.filter = ReplaceGroupBindings(aggr, move(f.filter));
42 // add the filter to the child node
43 if (child_pushdown.AddFilter(move(f.filter)) == FilterResult::UNSATISFIABLE) {
44 // filter statically evaluates to false, strip tree
45 return make_unique<LogicalEmptyResult>(move(op));
46 }
47 // erase the filter from here
48 filters.erase(filters.begin() + i);
49 i--;
50 }
51 }
52 child_pushdown.GenerateFilters();
53
54 op->children[0] = child_pushdown.Rewrite(move(op->children[0]));
55 return FinishPushdown(move(op));
56}
57