|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// SPDX-FileCopyrightText: Copyright the Vortex contributors |
| 3 | +#include "aggregate_fn_pushdown.hpp" |
| 4 | +#include "duckdb/planner/expression/bound_aggregate_expression.hpp" |
| 5 | +#include "duckdb/planner/operator/logical_aggregate.hpp" |
| 6 | +#include "scalar_fn_pushdown.hpp" |
| 7 | +#include "table_function.hpp" |
| 8 | + |
| 9 | +using enum LogicalOperatorType; |
| 10 | + |
| 11 | +LogicalOperatorPtr TryPushdownAggregateFunctions(ClientContext &context, LogicalOperatorPtr plan) { |
| 12 | + Analyses analyses; |
| 13 | + Projections projections; |
| 14 | + FindGetsAndProjections(*plan, analyses, projections); |
| 15 | + if (analyses.empty()) { |
| 16 | + return plan; |
| 17 | + } |
| 18 | + return RewriteAggregates(context, std::move(plan), analyses, projections); |
| 19 | +} |
| 20 | + |
| 21 | +LogicalOperatorPtr RewriteAggregates(ClientContext &context, |
| 22 | + LogicalOperatorPtr op, |
| 23 | + Analyses &analyses, |
| 24 | + const Projections &projections) { |
| 25 | + for (auto &child : op->children) { |
| 26 | + child = RewriteAggregates(context, std::move(child), analyses, projections); |
| 27 | + } |
| 28 | + if (op->type == LOGICAL_AGGREGATE_AND_GROUP_BY) { |
| 29 | + return TryReplaceAggregate(context, std::move(op), analyses, projections); |
| 30 | + } |
| 31 | + return op; |
| 32 | +} |
| 33 | + |
| 34 | +static bool IsUngrouped(const LogicalAggregate &agg) { |
| 35 | + return agg.groups.empty() && agg.grouping_sets.empty() && agg.grouping_functions.empty() && |
| 36 | + !agg.expressions.empty(); |
| 37 | +} |
| 38 | + |
| 39 | +constexpr inline idx_t COUNT_STAR_PROJ_IDX = std::numeric_limits<TableColumnStorageIndex>::max(); |
| 40 | + |
| 41 | +LogicalOperatorPtr TryReplaceAggregate(ClientContext &context, |
| 42 | + LogicalOperatorPtr op, |
| 43 | + Analyses &analyses, |
| 44 | + const Projections &projections) { |
| 45 | + LogicalAggregate &agg = op->Cast<LogicalAggregate>(); |
| 46 | + if (!IsUngrouped(agg)) { |
| 47 | + return op; |
| 48 | + } |
| 49 | + |
| 50 | + LogicalGet *const get = GetChildGet(agg); |
| 51 | + if (get == nullptr) { |
| 52 | + return op; |
| 53 | + } |
| 54 | + |
| 55 | + vector<std::pair<TableColumnStorageIndex, const Expression &>> input; |
| 56 | + const idx_t N = agg.expressions.size(); |
| 57 | + input.reserve(N); |
| 58 | + |
| 59 | + for (const auto &expr : agg.expressions) { |
| 60 | + if (expr->GetExpressionClass() != ExpressionClass::BOUND_AGGREGATE) { |
| 61 | + return op; |
| 62 | + } |
| 63 | + const auto &bound_aggr = expr->Cast<BoundAggregateExpression>(); |
| 64 | + if (bound_aggr.IsDistinct() || bound_aggr.filter != nullptr || bound_aggr.order_bys != nullptr) { |
| 65 | + return op; |
| 66 | + } |
| 67 | + |
| 68 | + if (bound_aggr.function.name == "count_star") { |
| 69 | + input.emplace_back(COUNT_STAR_PROJ_IDX, *expr); |
| 70 | + continue; |
| 71 | + } |
| 72 | + |
| 73 | + if (bound_aggr.children.size() != 1 || |
| 74 | + bound_aggr.children[0]->GetExpressionType() != ExpressionType::BOUND_COLUMN_REF) { |
| 75 | + return op; |
| 76 | + } |
| 77 | + const auto &bound_col = bound_aggr.children[0]->Cast<BoundColumnRefExpression>(); |
| 78 | + const auto binding = Resolve(bound_col.binding, analyses, projections); |
| 79 | + if (!binding || &binding->analysis.get != get) { |
| 80 | + return op; |
| 81 | + } |
| 82 | + const TableColumnStorageIndex storage_index = binding->analysis.StorageIndex(binding->column_index); |
| 83 | + input.emplace_back(storage_index, *expr); |
| 84 | + } |
| 85 | + |
| 86 | + if (!aggregate_pushdown(context, {*get, input})) { |
| 87 | + return op; |
| 88 | + } |
| 89 | + |
| 90 | + // GET now returns one column per aggregate. Expand column into multiple |
| 91 | + // column if there are many aggregates per column. |
| 92 | + auto &column_ids = get->GetMutableColumnIds(); |
| 93 | + get->types.resize(N); |
| 94 | + get->returned_types.resize(N); |
| 95 | + column_ids.resize(N); |
| 96 | + |
| 97 | + vector<string> names(N); // need a copy because we reference original names |
| 98 | + |
| 99 | + for (idx_t i = 0; i < N; i++) { |
| 100 | + const auto &[storage_index, expr] = input[i]; |
| 101 | + names[i] = storage_index == COUNT_STAR_PROJ_IDX ? "count_star()" : get->names[storage_index]; |
| 102 | + get->types[i] = expr.return_type; |
| 103 | + get->returned_types[i] = expr.return_type; |
| 104 | + column_ids[i] = ColumnIndex {i}; |
| 105 | + } |
| 106 | + get->names = std::move(names); |
| 107 | + get->projection_ids.clear(); |
| 108 | + get->table_index = agg.aggregate_index; |
| 109 | + |
| 110 | + unique_ptr<LogicalOperator> &child = agg.children[0]; |
| 111 | + if (child->type == LOGICAL_GET) { |
| 112 | + return std::move(child); |
| 113 | + } |
| 114 | + D_ASSERT(child->type == LOGICAL_PROJECTION); |
| 115 | + D_ASSERT(child->children.size() == 1); |
| 116 | + D_ASSERT(child->children[0]->type == LOGICAL_GET); |
| 117 | + return std::move(child->children[0]); |
| 118 | +} |
| 119 | + |
| 120 | +LogicalGet *GetChildGet(const LogicalAggregate &agg) { |
| 121 | + if (agg.children.size() != 1) { |
| 122 | + return nullptr; |
| 123 | + } |
| 124 | + LogicalOperator &child = *agg.children[0]; |
| 125 | + LogicalOperator *op; |
| 126 | + if (child.type == LOGICAL_GET) { |
| 127 | + op = &child; |
| 128 | + } else if (child.type == LOGICAL_PROJECTION && child.children.size() == 1 && |
| 129 | + child.children[0]->type == LOGICAL_GET) { |
| 130 | + op = child.children[0].get(); |
| 131 | + } else { |
| 132 | + return nullptr; |
| 133 | + } |
| 134 | + LogicalGet &get = op->Cast<LogicalGet>(); |
| 135 | + return get.function.bind == duckdb_vx_table_function_bind ? &get : nullptr; |
| 136 | +} |
0 commit comments