Try to execute aggregates using only the statistics if possible.
Can't get a storage index for this column, so it doesn't have stats we can use This happens when we're dealing with a generated column for example
323971 {
323972 if (!aggr.groups.empty()) {
323973
323974 return;
323975 }
323976
323977 vector<idx_t> count_star_idxs;
323978 vector<ColumnBinding> min_max_bindings;
323979 vector<unique_ptr<ValueComparator>> comparators;
323980
323981 for (
idx_t i = 0; i < aggr.expressions.size(); i++) {
323982 auto &aggr_ref = aggr.expressions[i];
323983 if (aggr_ref->GetExpressionClass() != ExpressionClass::BOUND_AGGREGATE) {
323984
323985 return;
323986 }
323987 auto &aggr_expr = aggr_ref->Cast<BoundAggregateExpression>();
323988 if (aggr_expr.filter) {
323989
323990 return;
323991 }
323992 const string &fun_name = aggr_expr.function.name;
323993 if (fun_name == "min" || fun_name == "max") {
323994 if (aggr_expr.children.size() != 1 || aggr_expr.children[0]->type != ExpressionType::BOUND_COLUMN_REF) {
323995 return;
323996 }
323997 const auto &col_ref = aggr_expr.children[0]->Cast<BoundColumnRefExpression>();
323998 min_max_bindings.push_back(col_ref.binding);
323999 auto comparator = GetComparator(fun_name, col_ref.return_type);
324000 if (!comparator) {
324001
324002 return;
324003 }
324004 comparators.push_back(std::move(comparator));
324005 } else if (fun_name == "count_star") {
324006 count_star_idxs.push_back(i);
324007 } else {
324008
324009 return;
324010 }
324011 }
324012
324013
324014 reference<LogicalOperator> child_ref = *aggr.children[0];
324015 while (child_ref.get().type == LogicalOperatorType::LOGICAL_PROJECTION) {
324016 for (auto &binding : min_max_bindings) {
324017 auto &expr = child_ref.get().expressions[binding.column_index];
324018 if (expr->type != ExpressionType::BOUND_COLUMN_REF) {
324019 return;
324020 }
324021 binding = expr->Cast<BoundColumnRefExpression>().binding;
324022 }
324023 child_ref = *child_ref.get().children[0];
324024 }
324025
324026 if (child_ref.get().type != LogicalOperatorType::LOGICAL_GET) {
324027
324028 return;
324029 }
324030 auto &
get = child_ref.get().Cast<LogicalGet>();
324031 if (!
get.function.get_partition_stats) {
324032
324033 return;
324034 }
324035 if (
get.extra_info.sample_options) {
324036
324037 return;
324038 }
324039
324040
324041 GetPartitionStatsInput input(
get.function,
get.bind_data.get());
324042 auto partition_stats =
get.function.get_partition_stats(context, input);
324043 if (partition_stats.empty()) {
324044
324045 return;
324046 }
324047
324048 vector<StorageIndex> min_max_storage_indexes(min_max_bindings.size());
324049 for (
idx_t i = 0; i < min_max_bindings.size(); i++) {
324050 auto &binding = min_max_bindings[i];
324051 auto &column_index =
get.GetColumnIds()[binding.column_index];
324052 if (!
get.TryGetStorageIndex(column_index, min_max_storage_indexes[i])) {
324055 return;
324056 }
324057 }
324058
324059 vector<LogicalType> types;
324060 vector<unique_ptr<Expression>> agg_results;
324061
324062 if (!
get.table_filters.filters.empty()) {
324063 map<StorageIndex, reference<unique_ptr<TableFilter>>> filter_storage_index_map;
324064 for (
auto &entry :
get.table_filters.filters) {
324065 auto col_idx = entry.first;
324066 auto &
filter = entry.second;
324067 auto column_index = ColumnIndex(col_idx);
324068 StorageIndex storage_index;
324069 if (!
get.TryGetStorageIndex(column_index, storage_index)) {
324070 return;
324071 }
324072 filter_storage_index_map.emplace(storage_index, filter);
324073 }
324074 vector<PartitionStatistics> remaining_partition_stats;
324075 for (auto &stats : partition_stats) {
324076 if (!stats.partition_row_group) {
324077 return;
324078 }
324079 auto filter_result = FilterPropagateResult::FILTER_ALWAYS_TRUE;
324080 for (auto &entry : filter_storage_index_map) {
324081 auto &storage_index = entry.first;
324082 auto &
filter = entry.second;
324083 auto prg = stats.partition_row_group;
324084 if (!prg) {
324085 return;
324086 }
324087 auto column_stats = prg->GetColumnStatistics(storage_index);
324088 if (!column_stats) {
324089 return;
324090 }
324091 auto col_filter_result =
filter.get()->CheckStatistics(*column_stats);
324092 if (col_filter_result == FilterPropagateResult::FILTER_ALWAYS_FALSE) {
324093
324094 filter_result = FilterPropagateResult::FILTER_ALWAYS_FALSE;
324095 break;
324096 }
324097 if (col_filter_result != FilterPropagateResult::FILTER_ALWAYS_TRUE) {
324098 filter_result = col_filter_result;
324099 }
324100 }
324101 switch (filter_result) {
324102 case FilterPropagateResult::FILTER_ALWAYS_TRUE:
324103
324104 remaining_partition_stats.push_back(std::move(stats));
324105 break;
324106 case FilterPropagateResult::FILTER_ALWAYS_FALSE:
324107 break;
324108 default:
324109
324110 return;
324111 }
324112 }
324113 partition_stats = std::move(remaining_partition_stats);
324114 }
324115
324116 if (!min_max_bindings.empty()) {
324117
324118 for (
idx_t agg_idx = 0; agg_idx < min_max_storage_indexes.size(); agg_idx++) {
324119 const auto &storage_index = min_max_storage_indexes[agg_idx];
324120 auto &comparator = comparators[agg_idx];
324121
324122 Value agg_result;
324123 if (!TryGetValueFromStats(partition_stats[0], storage_index, *comparator, agg_result)) {
324124 return;
324125 }
324126 for (
idx_t partition_idx = 1; partition_idx < partition_stats.size(); partition_idx++) {
324127 Value rhs;
324128 if (!TryGetValueFromStats(partition_stats[partition_idx], storage_index, *comparator, rhs)) {
324129 return;
324130 }
324131 if (!comparator->Compare(agg_result, rhs)) {
324132 agg_result = rhs;
324133 }
324134 }
324135 types.push_back(agg_result.GetTypeMutable());
324136 auto expr = make_uniq<BoundConstantExpression>(agg_result);
324137 agg_results.push_back(std::move(expr));
324138 }
324139 }
324140 if (!count_star_idxs.empty()) {
324141
324143 for (const auto &stats : partition_stats) {
324144 if (stats.count_type == CountType::COUNT_APPROXIMATE) {
324145
324146 return;
324147 }
324148 count += stats.count;
324149 }
324150 for (const auto count_star_idx : count_star_idxs) {
324151 auto count_result = make_uniq<BoundConstantExpression>(
Value::BIGINT(NumericCast<int64_t>(count)));
324152 agg_results.emplace(agg_results.begin() + NumericCast<int64_t>(count_star_idx), std::move(count_result));
324153 types.insert(types.begin() + NumericCast<int64_t>(count_star_idx), LogicalType::BIGINT);
324154 }
324155 }
324156
324157
324158 for (
idx_t expr_idx = 0; expr_idx < agg_results.size(); expr_idx++) {
324159 agg_results[expr_idx]->SetAlias(aggr.expressions[expr_idx]->GetAlias());
324160 }
324161
324162 vector<vector<unique_ptr<Expression>>> expressions;
324163 expressions.push_back(std::move(agg_results));
324164 auto expression_get =
324165 make_uniq<LogicalExpressionGet>(aggr.aggregate_index, std::move(types), std::move(expressions));
324166 expression_get->children.push_back(make_uniq<LogicalDummyScan>(aggr.group_index));
324167 node_ptr = std::move(expression_get);
324168}
static DUCKDB_API Value BIGINT(int64_t value)
Create a bigint Value from a specified value.
Definition duckdb.cpp:132146