325579 {
325580 auto &aggr = op->Cast<LogicalAggregate>();
325581 if (!aggr.groups.empty()) {
325582 return;
325583 }
325584
325585 unordered_set<idx_t> rewrote_map;
325586 vector<unique_ptr<Expression>> constants;
325587 idx_t aggr_count = aggr.expressions.size();
325588 for (
idx_t i = 0; i < aggr_count; ++i) {
325589 auto &expr = aggr.expressions[i];
325590 vector<reference<Expression>> bindings;
325591 if (!sum_matcher->Match(*expr, bindings)) {
325592 continue;
325593 }
325594
325595 auto &
sum = bindings[0].get().Cast<BoundAggregateExpression>();
325596 auto &addition = bindings[1].get().Cast<BoundFunctionExpression>();
325597 idx_t const_idx = addition.children[0]->GetExpressionType() == ExpressionType::VALUE_CONSTANT ? 0 : 1;
325598 auto const_expr = std::move(addition.children[const_idx]);
325599 auto main_expr = std::move(addition.children[1 - const_idx]);
325600
325601
325602 sum.children[0] = main_expr->Copy();
325603
325604
325605 FunctionBinder function_binder(optimizer.context);
325606
325607 auto count_fun = CountFunctionBase::GetFunction();
325608 vector<unique_ptr<Expression>> children;
325609 children.push_back(std::move(main_expr));
325610 auto count_aggr =
325611 function_binder.BindAggregateFunction(count_fun, std::move(children), nullptr, AggregateType::NON_DISTINCT);
325612 aggr.expressions.push_back(std::move(count_aggr));
325613 constants.push_back(std::move(const_expr));
325614 rewrote_map.insert(i);
325615 }
325616 if (rewrote_map.empty()) {
325617 return;
325618 }
325619 vector<unique_ptr<Expression>> projection_expressions;
325620
325621 idx_t rewritten_index = 0;
325622 auto proj_index = optimizer.binder.GenerateTableIndex();
325623 for (
idx_t i = 0; i < aggr_count; i++) {
325624 ColumnBinding aggregate_binding(aggr.aggregate_index, i);
325625 aggregate_map[aggregate_binding] = ColumnBinding(proj_index, i);
325626 auto &aggr_type = aggr.expressions[i]->return_type;
325627 auto aggr_ref = make_uniq<BoundColumnRefExpression>(aggr_type, aggregate_binding);
325628 if (rewrote_map.find(i) == rewrote_map.end()) {
325629
325630 projection_expressions.push_back(std::move(aggr_ref));
325631 continue;
325632 }
325633
325634 idx_t count_idx = aggr_count + rewritten_index;
325635 ColumnBinding count_binding(aggr.aggregate_index, count_idx);
325636 auto count_ref = make_uniq<BoundColumnRefExpression>(aggr.expressions[count_idx]->return_type, count_binding);
325637
325638
325640 auto const_expr =
325642
325643
325644 auto multiply = optimizer.BindScalarFunction(
"*", std::move(cast_count), std::move(const_expr));
325645
325646
325647 auto final_result = optimizer.BindScalarFunction("+", std::move(aggr_ref), std::move(multiply));
325648 projection_expressions.push_back(std::move(final_result));
325649
325650 rewritten_index++;
325651 }
325652
325653
325654 auto proj = make_uniq<LogicalProjection>(proj_index, std::move(projection_expressions));
325655 if (op->has_estimated_cardinality) {
325656 proj->SetEstimatedCardinality(op->estimated_cardinality);
325657 }
325658 proj->children.push_back(std::move(op));
325659 op = std::move(proj);
325660}
static DUCKDB_API unique_ptr< Expression > AddCastToType(ClientContext &context, unique_ptr< Expression > expr, const LogicalType &target_type, bool try_cast=false)
Cast an expression to the specified SQL type if required.
Definition duckdb.cpp:375869
Scalar sum(InputArray src)
void multiply(InputArray src1, InputArray src2, OutputArray dst, double scale=1, int dtype=-1)
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237