Skip to content

Commit

Permalink
Remove common aggregate optimizer and instead eliminate duplicate agg…
Browse files Browse the repository at this point in the history
…regates during the binding phase
  • Loading branch information
Mytherin committed Dec 17, 2019
1 parent 2acc0f7 commit 75b1099
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 315 deletions.
40 changes: 0 additions & 40 deletions src/include/duckdb/optimizer/ca_optimizer.hpp

This file was deleted.

5 changes: 5 additions & 0 deletions src/include/duckdb/planner/query_node/bound_select_node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "duckdb/planner/bound_query_node.hpp"
#include "duckdb/planner/bound_tableref.hpp"
#include "duckdb/parser/expression_map.hpp"

namespace duckdb {

Expand Down Expand Up @@ -42,6 +43,10 @@ class BoundSelectNode : public BoundQueryNode {
index_t aggregate_index;
//! Aggregate functions to compute (only used if HasAggregation is true)
vector<unique_ptr<Expression>> aggregates;
//! Map from aggregate function to aggregate index (used to eliminate duplicate aggregates)
expression_map_t<index_t> aggregate_map;



//! Window index used by the LogicalWindow (only used if HasWindow is true)
index_t window_index;
Expand Down
1 change: 0 additions & 1 deletion src/optimizer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ add_subdirectory(rule)

add_library_unity(duckdb_optimizer
OBJECT
ca_optimizer.cpp
cse_optimizer.cpp
filter_combiner.cpp
filter_pushdown.cpp
Expand Down
122 changes: 0 additions & 122 deletions src/optimizer/ca_optimizer.cpp

This file was deleted.

6 changes: 0 additions & 6 deletions src/optimizer/optimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include "duckdb/execution/expression_executor.hpp"
#include "duckdb/main/client_context.hpp"
#include "duckdb/optimizer/ca_optimizer.hpp"
#include "duckdb/optimizer/cse_optimizer.hpp"
#include "duckdb/optimizer/filter_pushdown.hpp"
#include "duckdb/optimizer/index_scan.hpp"
Expand Down Expand Up @@ -94,11 +93,6 @@ unique_ptr<LogicalOperator> Optimizer::Optimize(unique_ptr<LogicalOperator> plan
JoinOrderOptimizer optimizer;
plan = optimizer.Optimize(move(plan));
context.profiler.EndPhase();
// next we make sure that multiple occurences of the same aggregation are only computed once
// context.profiler.StartPhase("common_aggregate_expressions");
// CommonAggregateOptimizer ca_optimizer;
// ca_optimizer.VisitOperator(*plan);
// context.profiler.EndPhase();

// then we extract common subexpressions inside the different operators
// context.profiler.StartPhase("common_subexpressions");
Expand Down
21 changes: 17 additions & 4 deletions src/planner/binder/expression/bind_aggregate_expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,24 @@ BindResult SelectBinder::BindAggregate(FunctionExpression &aggr, AggregateFuncti
// create the aggregate
auto aggregate = make_unique<BoundAggregateExpression>(GetInternalType(return_type), bound_function, aggr.distinct);
aggregate->children = move(children);
// now create a column reference referring to this aggregate

// check for all the aggregates if this aggregate already exists
index_t aggr_index;
auto entry = node.aggregate_map.find(aggregate.get());
if (entry == node.aggregate_map.end()) {
// new aggregate: insert into aggregate list
aggr_index = node.aggregates.size();
node.aggregate_map.insert(make_pair(aggregate.get(), aggr_index));
node.aggregates.push_back(move(aggregate));
} else {
// duplicate aggregate: simplify refer to this aggregate
aggr_index = entry->second;
}

// now create a column reference referring to the aggregate
auto colref = make_unique<BoundColumnRefExpression>(
aggr.alias.empty() ? aggregate->ToString() : aggr.alias, aggregate->return_type,
ColumnBinding(node.aggregate_index, node.aggregates.size()), depth);
aggr.alias.empty() ? node.aggregates[aggr_index]->ToString() : aggr.alias, node.aggregates[aggr_index]->return_type,
ColumnBinding(node.aggregate_index, aggr_index), depth);
// move the aggregate expression into the set of bound aggregates
node.aggregates.push_back(move(aggregate));
return BindResult(move(colref), return_type);
}
1 change: 0 additions & 1 deletion test/optimizer/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
add_library_unity(test_optimizer
OBJECT
test_arithmetic_simplification.cpp
test_ca_optimizer.cpp
test_case_simplification.cpp
test_comparison_simplification.cpp
test_conjunction_simplification.cpp
Expand Down
141 changes: 0 additions & 141 deletions test/optimizer/test_ca_optimizer.cpp

This file was deleted.

0 comments on commit 75b1099

Please sign in to comment.