@@ -911,7 +911,7 @@ void GatherUsedWindows(const TExprNode::TPtr& window, const TExprNode::TPtr& pro
911911
912912TUsedColumns GatherUsedColumns (const TExprNode::TPtr& result, const TExprNode::TPtr& joinOps,
913913 const TExprNode::TPtr& filter, const TExprNode::TPtr& groupExprs, const TExprNode::TPtr& having, const TExprNode::TPtr& extraSortColumns,
914- const TExprNode::TPtr& window, const TWindowsCtx& winCtx) {
914+ const TExprNode::TPtr& window, const TWindowsCtx& winCtx, TVector<std::pair<TString, TString>>& joinUsingColumns ) {
915915 TUsedColumns usedColumns;
916916 for (const auto & x : result->Tail ().Children ()) {
917917 AddColumnsFromType (x->Child (1 )->GetTypeAnn (), usedColumns);
@@ -930,6 +930,8 @@ TUsedColumns GatherUsedColumns(const TExprNode::TPtr& result, const TExprNode::T
930930 auto lr = join->Child (3 )->Child (col);
931931 usedColumns.insert (std::make_pair (TString (lr->Child (0 )->Content ()), std::make_pair (Max<ui32>(), TString ())));
932932 usedColumns.insert (std::make_pair (TString (lr->Child (1 )->Content ()), std::make_pair (Max<ui32>(), TString ())));
933+ usedColumns.erase (TString (join->Child (2 )->Child (col)->Content ()));
934+ joinUsingColumns.emplace_back (ToString (groupNo), join->Child (2 )->Child (col)->Content ());
933935 }
934936 continue ;
935937 }
@@ -1442,7 +1444,7 @@ bool GatherJoinInputs(const TExprNode& root, const TExprNode& row, ui32 rightInp
14421444}
14431445
14441446TExprNode::TPtr BuildEquiJoin (TPositionHandle pos, TStringBuf joinType, const TExprNode::TPtr& left, const TExprNode::TPtr& right,
1445- const TExprNode::TListType& leftColumns, const TExprNode::TListType& rightColumns, TExprContext& ctx, bool isUsing = false ) {
1447+ const TExprNode::TListType& leftColumns, const TExprNode::TListType& rightColumns, TExprContext& ctx) {
14461448 auto join = ctx.Builder (pos)
14471449 .Callable (" EquiJoin" )
14481450 .List (0 )
@@ -1484,7 +1486,7 @@ TExprNode::TPtr BuildEquiJoin(TPositionHandle pos, TStringBuf joinType, const TE
14841486 .Seal ()
14851487 .Seal ()
14861488 .Build ();
1487- Y_UNUSED (isUsing);
1489+
14881490 return ctx.Builder (pos)
14891491 .Callable (" OrderedMap" )
14901492 .Add (0 , join)
@@ -1540,6 +1542,67 @@ std::tuple<TVector<ui32>, TExprNode::TListType> BuildJoinGroups(TPositionHandle
15401542 rightColumns.push_back (col->Child (1 ));
15411543 }
15421544 current = BuildEquiJoin (pos, joinType, left, right, leftColumns, rightColumns, ctx);
1545+ auto secondStruct = ctx.Builder (pos)
1546+ .Lambda ()
1547+ .Param (" row" )
1548+ .Callable (" AsStruct" )
1549+ .Do ([&](TExprNodeBuilder& parent) -> TExprNodeBuilder& {
1550+ for (size_t i = 0 ; i < leftColumns.size (); ++i) {
1551+ parent.List (i)
1552+ .Add (0 , join->Child (2 )->Child (i))
1553+ .Callable (1 , " Coalesce" )
1554+ .Callable (0 , " Member" )
1555+ .Arg (0 , " row" )
1556+ .Add (1 , leftColumns[i])
1557+ .Seal ()
1558+ .Callable (1 , " Member" )
1559+ .Arg (0 , " row" )
1560+ .Add (1 , rightColumns[i])
1561+ .Seal ()
1562+ .Seal ()
1563+ .Seal ();
1564+ }
1565+ return parent;
1566+ })
1567+ .Seal ()
1568+ .Seal ()
1569+ .Build ();
1570+ current = ctx.Builder (pos)
1571+ .Callable (" OrderedMap" )
1572+ .Add (0 , current)
1573+ .Lambda (1 )
1574+ .Param (" row" )
1575+ .Callable (" FlattenMembers" )
1576+ .List (0 )
1577+ .Atom (0 , " " )
1578+ .Arg (1 , " row" )
1579+ .Seal ()
1580+ .List (1 )
1581+ .Atom (0 , " " )
1582+ .Apply (1 , secondStruct).With (0 , " row" ).Seal ()
1583+ .Seal ()
1584+ .Seal ()
1585+ .Seal ()
1586+ .Seal ()
1587+ .Build ();/*
1588+ for (size_t i = 0; i < leftColumns.size(); ++i) {
1589+ current = ctx.Builder(pos)
1590+ .Callable("OrderedMap")
1591+ .Add(0, current)
1592+ .Lambda(1)
1593+ .Param("row")
1594+ .Callable("AddMember")
1595+ .Arg(0, "row")
1596+ .Atom(1, rightColumns[i]->Content())
1597+ .Callable(2, "Member")
1598+ .Arg(0, "row")
1599+ .Atom(1, leftColumns[i]->Content())
1600+ .Seal()
1601+ .Seal()
1602+ .Seal()
1603+ .Seal()
1604+ .Build();
1605+ }*/
15431606 continue ;
15441607 }
15451608
@@ -1732,7 +1795,7 @@ std::tuple<TVector<ui32>, TExprNode::TListType> BuildJoinGroups(TPositionHandle
17321795}
17331796
17341797TExprNode::TPtr BuildCrossJoinsBetweenGroups (TPositionHandle pos, const TExprNode::TListType& joinGroups,
1735- const TUsedColumns& usedColumns, const TVector<ui32>& groupForIndex, TExprContext& ctx) {
1798+ const TUsedColumns& usedColumns, const TVector<ui32>& groupForIndex, TExprContext& ctx, const TVector<std::pair<TString, TString>>& joinUsingColumns ) {
17361799 TExprNode::TListType args;
17371800 for (ui32 i = 0 ; i < joinGroups.size (); ++i) {
17381801 args.push_back (ctx.Builder (pos)
@@ -1765,6 +1828,16 @@ TExprNode::TPtr BuildCrossJoinsBetweenGroups(TPositionHandle pos, const TExprNod
17651828 .Build ());
17661829 }
17671830
1831+ for (const auto & x: joinUsingColumns) {
1832+ settings.push_back (ctx.Builder (pos)
1833+ .List ()
1834+ .Atom (0 , " rename" )
1835+ .Atom (1 , x.first + " ." + x.second )
1836+ .Atom (2 , x.second )
1837+ .Seal ()
1838+ .Build ());
1839+ }
1840+
17681841 auto settingsNode = ctx.NewList (pos, std::move (settings));
17691842 args.push_back (settingsNode);
17701843 return ctx.NewCallable (pos, " EquiJoin" , std::move (args));
@@ -3325,7 +3398,8 @@ TExprNode::TPtr ExpandPgSelectImpl(const TExprNode::TPtr& node, TExprContext& ct
33253398 cleanedInputs.push_back (list);
33263399 } else {
33273400 // extract all used columns
3328- auto usedColumns = GatherUsedColumns (result, joinOps, filter, groupExprs, having, extraSortColumns, window, winCtx);
3401+ TVector<std::pair<TString, TString>> joinUsingColumns;
3402+ auto usedColumns = GatherUsedColumns (result, joinOps, filter, groupExprs, having, extraSortColumns, window, winCtx, joinUsingColumns);
33293403
33303404 // fill index of input for each column
33313405 FillInputIndices (from, finalExtTypes, usedColumns, optCtx);
@@ -3341,7 +3415,7 @@ TExprNode::TPtr ExpandPgSelectImpl(const TExprNode::TPtr& node, TExprContext& ct
33413415 if (joinGroups.size () == 1 ) {
33423416 list = joinGroups.front ();
33433417 } else {
3344- list = BuildCrossJoinsBetweenGroups (node->Pos (), joinGroups, usedColumns, groupForIndex, ctx);
3418+ list = BuildCrossJoinsBetweenGroups (node->Pos (), joinGroups, usedColumns, groupForIndex, ctx, joinUsingColumns );
33453419 }
33463420 }
33473421 }
0 commit comments