1#include "duckdb/optimizer/rule/ordered_aggregate_optimizer.hpp"
2
3#include "duckdb/optimizer/matcher/expression_matcher.hpp"
4#include "duckdb/planner/expression/bound_aggregate_expression.hpp"
5
6namespace duckdb {
7
8OrderedAggregateOptimizer::OrderedAggregateOptimizer(ExpressionRewriter &rewriter) : Rule(rewriter) {
9 // we match on an OR expression within a LogicalFilter node
10 root = make_uniq<ExpressionMatcher>();
11 root->expr_class = ExpressionClass::BOUND_AGGREGATE;
12}
13
14unique_ptr<Expression> OrderedAggregateOptimizer::Apply(LogicalOperator &op, vector<reference<Expression>> &bindings,
15 bool &changes_made, bool is_root) {
16 auto &aggr = bindings[0].get().Cast<BoundAggregateExpression>();
17 if (!aggr.order_bys) {
18 // no ORDER BYs defined
19 return nullptr;
20 }
21 if (aggr.function.order_dependent == AggregateOrderDependent::NOT_ORDER_DEPENDENT) {
22 // not an order dependent aggregate but we have an ORDER BY clause - remove it
23 aggr.order_bys.reset();
24 changes_made = true;
25 return nullptr;
26 }
27 return nullptr;
28}
29
30} // namespace duckdb
31