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
8namespace duckdb {
9
10using Filter = FilterPushdown::Filter;
11
12static void ExtractFilterBindings(Expression &expr, vector<ColumnBinding> &bindings) {
13 if (expr.type == ExpressionType::BOUND_COLUMN_REF) {
14 auto &colref = expr.Cast<BoundColumnRefExpression>();
15 bindings.push_back(x: colref.binding);
16 }
17 ExpressionIterator::EnumerateChildren(expression&: expr, callback: [&](Expression &child) { ExtractFilterBindings(expr&: child, bindings); });
18}
19
20static unique_ptr<Expression> ReplaceGroupBindings(LogicalAggregate &proj, unique_ptr<Expression> expr) {
21 if (expr->type == ExpressionType::BOUND_COLUMN_REF) {
22 auto &colref = expr->Cast<BoundColumnRefExpression>();
23 D_ASSERT(colref.binding.table_index == proj.group_index);
24 D_ASSERT(colref.binding.column_index < proj.groups.size());
25 D_ASSERT(colref.depth == 0);
26 // replace the binding with a copy to the expression at the referenced index
27 return proj.groups[colref.binding.column_index]->Copy();
28 }
29 ExpressionIterator::EnumerateChildren(
30 expression&: *expr, callback: [&](unique_ptr<Expression> &child) { child = ReplaceGroupBindings(proj, expr: std::move(child)); });
31 return expr;
32}
33
34unique_ptr<LogicalOperator> FilterPushdown::PushdownAggregate(unique_ptr<LogicalOperator> op) {
35 D_ASSERT(op->type == LogicalOperatorType::LOGICAL_AGGREGATE_AND_GROUP_BY);
36 auto &aggr = op->Cast<LogicalAggregate>();
37
38 // pushdown into AGGREGATE and GROUP BY
39 // we cannot push expressions that refer to the aggregate
40 FilterPushdown child_pushdown(optimizer);
41 for (idx_t i = 0; i < filters.size(); i++) {
42 auto &f = *filters[i];
43 if (f.bindings.find(x: aggr.aggregate_index) != f.bindings.end()) {
44 // filter on aggregate: cannot pushdown
45 continue;
46 }
47 if (f.bindings.find(x: aggr.groupings_index) != f.bindings.end()) {
48 // filter on GROUPINGS function: cannot pushdown
49 continue;
50 }
51 // no aggregate! we are filtering on a group
52 // we can only push this down if the filter is in all grouping sets
53 vector<ColumnBinding> bindings;
54 ExtractFilterBindings(expr&: *f.filter, bindings);
55
56 bool can_pushdown_filter = true;
57 if (aggr.grouping_sets.empty()) {
58 // empty grouping set - we cannot pushdown the filter
59 can_pushdown_filter = false;
60 }
61 for (auto &grp : aggr.grouping_sets) {
62 // check for each of the grouping sets if they contain all groups
63 if (bindings.empty()) {
64 // we can never push down empty grouping sets
65 can_pushdown_filter = false;
66 break;
67 }
68 for (auto &binding : bindings) {
69 if (grp.find(x: binding.column_index) == grp.end()) {
70 can_pushdown_filter = false;
71 break;
72 }
73 }
74 if (!can_pushdown_filter) {
75 break;
76 }
77 }
78 if (!can_pushdown_filter) {
79 continue;
80 }
81 // no aggregate! we can push this down
82 // rewrite any group bindings within the filter
83 f.filter = ReplaceGroupBindings(proj&: aggr, expr: std::move(f.filter));
84 // add the filter to the child node
85 if (child_pushdown.AddFilter(expr: std::move(f.filter)) == FilterResult::UNSATISFIABLE) {
86 // filter statically evaluates to false, strip tree
87 return make_uniq<LogicalEmptyResult>(args: std::move(op));
88 }
89 // erase the filter from here
90 filters.erase(position: filters.begin() + i);
91 i--;
92 }
93 child_pushdown.GenerateFilters();
94
95 op->children[0] = child_pushdown.Rewrite(op: std::move(op->children[0]));
96 return FinishPushdown(op: std::move(op));
97}
98
99} // namespace duckdb
100