@@ -317,6 +317,15 @@ void CountLabelsInputUsage(TExprNode::TPtr joinTree, THashMap<TString, int>& cou
317
317
}
318
318
}
319
319
320
+ void DecrementCountLabelsInputUsage (TExprNode::TPtr joinTree, THashMap<TString, int >& counters) {
321
+ if (joinTree->IsAtom ()) {
322
+ counters[joinTree->Content ()]--;
323
+ } else {
324
+ CountLabelsInputUsage (joinTree->ChildPtr (1 ), counters);
325
+ CountLabelsInputUsage (joinTree->ChildPtr (2 ), counters);
326
+ }
327
+ }
328
+
320
329
// returns the path to join child
321
330
std::pair<TExprNode::TPtr, TExprNode::TPtr> IsRightSideForLeftJoin (
322
331
const TExprNode::TPtr& joinTree, const TJoinLabels& labels, ui32 inputIndex, const TExprNode::TPtr& parent = nullptr
@@ -350,6 +359,16 @@ std::pair<TExprNode::TPtr, TExprNode::TPtr> IsRightSideForLeftJoin(
350
359
return {nullptr , nullptr };
351
360
}
352
361
362
+ TExprNode::TPtr CreateNewLabelList (THashMap<TString, int >& labels, TExprContext& ctx, const TPositionHandle& position) {
363
+ TExprNode::TListType newKeys;
364
+ for (const auto & label : labels) {
365
+ if (label.second == 0 ) {
366
+ newKeys.push_back (ctx.NewAtom (position, label.first ));
367
+ }
368
+ }
369
+ return ctx.NewList (position, std::move (newKeys));
370
+ }
371
+
353
372
TExprNode::TPtr FilterPushdownOverJoinOptionalSide (TExprNode::TPtr equiJoin, TExprNode::TPtr predicate,
354
373
const TSet<TStringBuf>& usedFields, TExprNode::TPtr args, const TJoinLabels& labels,
355
374
ui32 inputIndex, const TMap<TStringBuf, TVector<TStringBuf>>& renameMap, bool ordered, bool skipNulls, TExprContext& ctx,
@@ -401,8 +420,7 @@ TExprNode::TPtr FilterPushdownOverJoinOptionalSide(TExprNode::TPtr equiJoin, TEx
401
420
402
421
auto [leftJoinTree, parentJoinPtr] = IsRightSideForLeftJoin (joinTree, labels, inputIndex);
403
422
YQL_ENSURE (leftJoinTree);
404
- joinLabelCounters[leftJoinTree->Child (1 )->Content ()]--;
405
- joinLabelCounters[leftJoinTree->Child (2 )->Content ()]--;
423
+ DecrementCountLabelsInputUsage (leftJoinTree, joinLabelCounters);
406
424
407
425
auto leftJoinSettings = equiJoin->ChildPtr (equiJoin->ChildrenSize () - 1 );
408
426
@@ -509,11 +527,6 @@ TExprNode::TPtr FilterPushdownOverJoinOptionalSide(TExprNode::TPtr equiJoin, TEx
509
527
joinColumns.emplace (std::move (column));
510
528
}
511
529
512
- auto newJoinLabel = ctx.Builder (pos)
513
- .Atom (" __yql_right_side_pushdown_input_label" )
514
- .Build ();
515
-
516
-
517
530
TExprNode::TPtr remJoinKeys;
518
531
bool changedLeftSide = false ;
519
532
if (leftJoinTree == parentJoinPtr->ChildPtr (1 )) {
@@ -523,36 +536,16 @@ TExprNode::TPtr FilterPushdownOverJoinOptionalSide(TExprNode::TPtr equiJoin, TEx
523
536
remJoinKeys = parentJoinPtr->ChildPtr (4 );
524
537
}
525
538
526
- TExprNode::TListType newKeys;
527
- newKeys.reserve (remJoinKeys->ChildrenSize ());
528
-
529
- for (ui32 i = 0 ; i < remJoinKeys->ChildrenSize (); i += 2 ) {
530
- auto table = remJoinKeys->ChildPtr (i);
531
- auto column = remJoinKeys->ChildPtr (i + 1 );
532
-
533
- YQL_ENSURE (table->IsAtom ());
534
- YQL_ENSURE (column->IsAtom ());
535
-
536
- auto fcn = FullColumnName (table->Content (), column->Content ());
537
-
538
- if (joinColumns.contains (fcn)) {
539
- newKeys.push_back (newJoinLabel);
540
- newKeys.push_back (ctx.NewAtom (column->Pos (), fcn));
541
- } else {
542
- newKeys.push_back (table);
543
- newKeys.push_back (column);
544
- }
545
- }
546
-
547
- auto newKeysList = ctx.NewList (remJoinKeys->Pos (), std::move (newKeys));
539
+ auto parentJoinLabel = remJoinKeys->ChildPtr (0 );
540
+ auto joinLabelList = CreateNewLabelList (joinLabelCounters, ctx, pos);
548
541
549
542
auto newParentJoin = ctx.Builder (joinTree->Pos ())
550
543
.List ()
551
544
.Add (0 , parentJoinPtr->ChildPtr (0 ))
552
- .Add (1 , changedLeftSide ? newJoinLabel : parentJoinPtr->ChildPtr (1 ))
553
- .Add (2 , !changedLeftSide ? newJoinLabel : parentJoinPtr->ChildPtr (2 ))
554
- .Add (3 , changedLeftSide ? newKeysList : parentJoinPtr->ChildPtr (3 ))
555
- .Add (4 , !changedLeftSide ? newKeysList : parentJoinPtr->ChildPtr (4 ))
545
+ .Add (1 , changedLeftSide ? parentJoinLabel : parentJoinPtr->ChildPtr (1 ))
546
+ .Add (2 , !changedLeftSide ? parentJoinLabel : parentJoinPtr->ChildPtr (2 ))
547
+ .Add (3 , parentJoinPtr->ChildPtr (3 ))
548
+ .Add (4 , parentJoinPtr->ChildPtr (4 ))
556
549
.Add (5 , parentJoinPtr->ChildPtr (5 ))
557
550
.Seal ()
558
551
.Build ();
@@ -568,16 +561,6 @@ TExprNode::TPtr FilterPushdownOverJoinOptionalSide(TExprNode::TPtr equiJoin, TEx
568
561
}
569
562
return parent;
570
563
})
571
- .Do ([&](TExprNodeBuilder& parent) -> TExprNodeBuilder& {
572
- for (const auto & column : joinColumns) {
573
- parent.List (i++)
574
- .Atom (0 , " rename" )
575
- .Atom (1 , FullColumnName (" __yql_right_side_pushdown_input_label" , column))
576
- .Atom (2 , column)
577
- .Seal ();
578
- }
579
- return parent;
580
- })
581
564
.Seal ()
582
565
.Build ();
583
566
@@ -598,7 +581,7 @@ TExprNode::TPtr FilterPushdownOverJoinOptionalSide(TExprNode::TPtr equiJoin, TEx
598
581
})
599
582
.List (i++)
600
583
.Add (0 , unionAll)
601
- .Add (1 , newJoinLabel )
584
+ .Add (1 , joinLabelList )
602
585
.Seal ()
603
586
.Add (i++, newJoinTree)
604
587
.Add (i++, newJoinSettings)
0 commit comments