Skip to content

Commit 7e555f7

Browse files
authored
[KQP RBO] Add avg aggregation function (#29395)
1 parent 05ce178 commit 7e555f7

File tree

9 files changed

+346
-92
lines changed

9 files changed

+346
-92
lines changed

ydb/core/kqp/expr_nodes/kqp_expr_nodes.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -910,8 +910,7 @@
910910
"Match": {"Type": "Tuple"},
911911
"Children": [
912912
{"Index": 0, "Name": "OriginalColName", "Type": "TCoAtom"},
913-
{"Index": 1, "Name": "AggregationFunction", "Type": "TCoAtom"},
914-
{"Index": 2, "Name": "ResultColName", "Type": "TCoAtom"}
913+
{"Index": 1, "Name": "AggregationFunction", "Type": "TCoAtom"}
915914
]
916915
},
917916
{

ydb/core/kqp/opt/kqp_type_ann.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2498,12 +2498,11 @@ TStatus AnnotateOpAggregate(const TExprNode::TPtr& input, TExprContext& ctx) {
24982498
const auto* structType = inputType->Cast<TListExprType>()->GetItemType()->Cast<TStructExprType>();
24992499
auto opAggregate = TKqpOpAggregate(input);
25002500

2501-
THashMap<TString, std::pair<TString, TString>> aggTraitsMap;
2501+
THashMap<TString, TString> aggTraitsMap;
25022502
for (const auto& traits : opAggregate.AggregationTraitsList()) {
25032503
const auto originalColName = TString(traits.OriginalColName());
25042504
const auto aggFuncName = TString(traits.AggregationFunction());
2505-
const auto resultColName = TString(traits.ResultColName());
2506-
aggTraitsMap[originalColName] = {resultColName, aggFuncName};
2505+
aggTraitsMap[originalColName] = aggFuncName;
25072506
}
25082507

25092508
THashSet<TString> keyColumns;
@@ -2516,17 +2515,22 @@ TStatus AnnotateOpAggregate(const TExprNode::TPtr& input, TExprContext& ctx) {
25162515
// The type of the column could be changed after aggregation.
25172516
const auto itemName = itemType->GetName();
25182517
if (auto it = aggTraitsMap.find(itemName); it != aggTraitsMap.end()) {
2519-
const auto& resultColName = it->second.first;
2520-
const auto& aggFunction = it->second.second;
2518+
const auto& colName = it->first;
2519+
const auto& aggFunction = it->second;
25212520
const TTypeAnnotationNode* aggFieldType = itemType->GetItemType();
2521+
TPositionHandle dummyPos;
2522+
25222523
if (aggFunction == "count") {
25232524
aggFieldType = ctx.MakeType<TDataExprType>(EDataSlot::Uint64);
25242525
} else if (aggFunction == "sum") {
2525-
TPositionHandle dummyPos;
25262526
Y_ENSURE(GetSumResultType(dummyPos, *itemType->GetItemType(), aggFieldType, ctx),
25272527
"Unsupported type for sum aggregation function");
2528+
} else if (aggFunction == "avg") {
2529+
Y_ENSURE(GetAvgResultType(dummyPos, *itemType->GetItemType(), aggFieldType, ctx),
2530+
"Unsupported type for avg aggregation function");
25282531
}
2529-
newItemTypes.push_back(ctx.MakeType<TItemExprType>(resultColName, aggFieldType));
2532+
2533+
newItemTypes.push_back(ctx.MakeType<TItemExprType>(colName, aggFieldType));
25302534
} else if (keyColumns.contains(itemName)) {
25312535
newItemTypes.push_back(itemType);
25322536
}

0 commit comments

Comments
 (0)