@@ -16,6 +16,7 @@ struct TJoinTableAliases {
1616};
1717
1818THashSet<TString> SupportedAggregationFunctions{" sum" , " min" , " max" , " count" };
19+ ui64 KqpUniqueAggColumnId{0 };
1920
2021TJoinTableAliases GatherJoinAliasesLeftSideMultiInputs (const TVector<TInfoUnit> &joinKeys, const THashSet<TString> &processedInputs) {
2122 TJoinTableAliases joinAliases;
@@ -388,6 +389,7 @@ TExprNode::TPtr RewritePgSelect(const TExprNode::TPtr &node, TExprContext &ctx,
388389 }
389390
390391 // FIXME: Group by key can be an expression, we need to handle this case
392+ TVector<std::pair<TInfoUnit, TInfoUnit>> aggRenamesMap;
391393 TVector<TInfoUnit> groupByKeys;
392394 auto groupOps = GetSetting (setItem->Tail (), " group_exprs" );
393395 if (groupOps) {
@@ -398,6 +400,9 @@ TExprNode::TPtr RewritePgSelect(const TExprNode::TPtr &node, TExprContext &ctx,
398400 TVector<TInfoUnit> keys;
399401 GetAllMembers (body, keys);
400402 groupByKeys.insert (groupByKeys.end (), keys.begin (), keys.end ());
403+ for (const auto &infoUnit : keys) {
404+ aggRenamesMap.push_back ({infoUnit, infoUnit});
405+ }
401406 }
402407 }
403408
@@ -407,6 +412,8 @@ TExprNode::TPtr RewritePgSelect(const TExprNode::TPtr &node, TExprContext &ctx,
407412
408413 // This is a hack to enable convertion for aggregation columns.
409414 THashSet<TString> aggregationColumns;
415+ THashSet<TString> columnNames;
416+ bool needToRenameAggFields = false ;
410417 // Collect PgAgg for each result item at first pass.
411418 TVector<TKqpOpAggregationTraits> aggTraitsList;
412419 for (ui32 i = 0 ; i < result->Child (1 )->ChildrenSize (); ++i) {
@@ -418,6 +425,21 @@ TExprNode::TPtr RewritePgSelect(const TExprNode::TPtr &node, TExprContext &ctx,
418425 TVector<TInfoUnit> originalColNames;
419426 GetAllMembers (pgAgg, originalColNames);
420427 Y_ENSURE (originalColNames.size () == 1 , " Invalid column size for aggregation columns." );
428+ const auto & originalColName = originalColNames.front ();
429+ auto renamedColName = originalColName;
430+
431+ // Rename agg column we will add a map to map same column to different renames.
432+ if (columnNames.count (originalColName.GetFullName ())) {
433+ TStringBuilder strBuilder;
434+ strBuilder << " _kqp_agg_input_" ;
435+ strBuilder << originalColName.ColumnName ;
436+ strBuilder << " _" ;
437+ strBuilder << ToString (KqpUniqueAggColumnId++);
438+ renamedColName = TInfoUnit (originalColName.Alias , strBuilder);
439+ needToRenameAggFields = true ;
440+ }
441+ aggRenamesMap.push_back ({originalColName, renamedColName});
442+ columnNames.insert (renamedColName.GetFullName ());
421443
422444 // Result column name.
423445 auto resultColName = TString (resultItem->Child (0 )->Content ());
@@ -429,7 +451,7 @@ TExprNode::TPtr RewritePgSelect(const TExprNode::TPtr &node, TExprContext &ctx,
429451 // clang-format off
430452 auto aggregationTraits = Build<TKqpOpAggregationTraits>(ctx, node->Pos ())
431453 .OriginalColName <TCoAtom>()
432- .Value (originalColNames. front () .GetFullName ())
454+ .Value (renamedColName .GetFullName ())
433455 .Build ()
434456 .AggregationFunction <TCoAtom>()
435457 .Value (aggFuncName)
@@ -446,6 +468,35 @@ TExprNode::TPtr RewritePgSelect(const TExprNode::TPtr &node, TExprContext &ctx,
446468
447469 TExprNode::TPtr resultExpr = filterExpr;
448470 if (!aggTraitsList.empty ()) {
471+ if (needToRenameAggFields) {
472+ TVector<TExprNode::TPtr> mapElements;
473+ for (const auto &[colName, newColName] : aggRenamesMap) {
474+ // clang-format off
475+ mapElements.push_back (Build<TKqpOpMapElementRename>(ctx, node->Pos ())
476+ .Input (resultExpr)
477+ .Variable ()
478+ .Value (newColName.GetFullName ())
479+ .Build ()
480+ .From ()
481+ .Value (colName.GetFullName ())
482+ .Build ()
483+ .Done ().Ptr ());
484+ // clang-format on
485+ }
486+
487+ // clang-format off
488+ resultExpr = Build<TKqpOpMap>(ctx, node->Pos ())
489+ .Input (resultExpr)
490+ .MapElements ()
491+ .Add (mapElements)
492+ .Build ()
493+ .Project ()
494+ .Value (" false" )
495+ .Build ()
496+ .Done ().Ptr ();
497+ // clang-format on
498+ }
499+
449500 TVector<TCoAtom> keyColumns;
450501 for (const auto &column : groupByKeys) {
451502 // clang-format off
0 commit comments