1#include "duckdb/execution/operator/aggregate/physical_hash_aggregate.hpp"
2#include "duckdb/execution/operator/aggregate/physical_simple_aggregate.hpp"
3#include "duckdb/execution/physical_plan_generator.hpp"
4#include "duckdb/catalog/catalog_entry/aggregate_function_catalog_entry.hpp"
5#include "duckdb/planner/expression/bound_aggregate_expression.hpp"
6#include "duckdb/planner/operator/logical_aggregate.hpp"
7
8using namespace duckdb;
9using namespace std;
10
11unique_ptr<PhysicalOperator> PhysicalPlanGenerator::CreatePlan(LogicalAggregate &op) {
12 assert(op.children.size() == 1);
13
14 auto plan = CreatePlan(*op.children[0]);
15 if (op.groups.size() == 0) {
16 // no groups, check if we can use a simple aggregation
17 // special case: aggregate entire columns together
18 bool use_simple_aggregation = true;
19 for (idx_t i = 0; i < op.expressions.size(); i++) {
20 auto &aggregate = (BoundAggregateExpression &)*op.expressions[i];
21 if (!aggregate.function.simple_update || aggregate.distinct) {
22 // unsupported aggregate for simple aggregation: use hash aggregation
23 use_simple_aggregation = false;
24 break;
25 }
26 }
27 if (use_simple_aggregation) {
28 auto groupby = make_unique<PhysicalSimpleAggregate>(op.types, move(op.expressions));
29 groupby->children.push_back(move(plan));
30 return move(groupby);
31 } else {
32 auto groupby = make_unique<PhysicalHashAggregate>(op.types, move(op.expressions));
33 groupby->children.push_back(move(plan));
34 return move(groupby);
35 }
36 } else {
37 // groups! create a GROUP BY aggregator
38 auto groupby = make_unique<PhysicalHashAggregate>(op.types, move(op.expressions), move(op.groups));
39 groupby->children.push_back(move(plan));
40 return move(groupby);
41 }
42}
43