Skip to content

Commit a2784e2

Browse files
committed
Fix
1 parent b3b79e7 commit a2784e2

File tree

1 file changed

+27
-44
lines changed

1 file changed

+27
-44
lines changed

yql/essentials/core/common_opt/yql_flatmap_over_join.cpp

Lines changed: 27 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,15 @@ void CountLabelsInputUsage(TExprNode::TPtr joinTree, THashMap<TString, int>& cou
317317
}
318318
}
319319

320+
void DecrementCountLabelsInputUsage(TExprNode::TPtr joinTree, THashMap<TString, int>& counters) {
321+
if (joinTree->IsAtom()) {
322+
counters[joinTree->Content()]--;
323+
} else {
324+
DecrementCountLabelsInputUsage(joinTree->ChildPtr(1), counters);
325+
DecrementCountLabelsInputUsage(joinTree->ChildPtr(2), counters);
326+
}
327+
}
328+
320329
// returns the path to join child
321330
std::pair<TExprNode::TPtr, TExprNode::TPtr> IsRightSideForLeftJoin(
322331
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(
350359
return {nullptr, nullptr};
351360
}
352361

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+
353372
TExprNode::TPtr FilterPushdownOverJoinOptionalSide(TExprNode::TPtr equiJoin, TExprNode::TPtr predicate,
354373
const TSet<TStringBuf>& usedFields, TExprNode::TPtr args, const TJoinLabels& labels,
355374
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
401420

402421
auto [leftJoinTree, parentJoinPtr] = IsRightSideForLeftJoin(joinTree, labels, inputIndex);
403422
YQL_ENSURE(leftJoinTree);
404-
joinLabelCounters[leftJoinTree->Child(1)->Content()]--;
405-
joinLabelCounters[leftJoinTree->Child(2)->Content()]--;
423+
DecrementCountLabelsInputUsage(leftJoinTree, joinLabelCounters);
406424

407425
auto leftJoinSettings = equiJoin->ChildPtr(equiJoin->ChildrenSize() - 1);
408426

@@ -509,11 +527,6 @@ TExprNode::TPtr FilterPushdownOverJoinOptionalSide(TExprNode::TPtr equiJoin, TEx
509527
joinColumns.emplace(std::move(column));
510528
}
511529

512-
auto newJoinLabel = ctx.Builder(pos)
513-
.Atom("__yql_right_side_pushdown_input_label")
514-
.Build();
515-
516-
517530
TExprNode::TPtr remJoinKeys;
518531
bool changedLeftSide = false;
519532
if (leftJoinTree == parentJoinPtr->ChildPtr(1)) {
@@ -523,36 +536,16 @@ TExprNode::TPtr FilterPushdownOverJoinOptionalSide(TExprNode::TPtr equiJoin, TEx
523536
remJoinKeys = parentJoinPtr->ChildPtr(4);
524537
}
525538

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);
548541

549542
auto newParentJoin = ctx.Builder(joinTree->Pos())
550543
.List()
551544
.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))
556549
.Add(5, parentJoinPtr->ChildPtr(5))
557550
.Seal()
558551
.Build();
@@ -568,16 +561,6 @@ TExprNode::TPtr FilterPushdownOverJoinOptionalSide(TExprNode::TPtr equiJoin, TEx
568561
}
569562
return parent;
570563
})
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-
})
581564
.Seal()
582565
.Build();
583566

@@ -598,7 +581,7 @@ TExprNode::TPtr FilterPushdownOverJoinOptionalSide(TExprNode::TPtr equiJoin, TEx
598581
})
599582
.List(i++)
600583
.Add(0, unionAll)
601-
.Add(1, newJoinLabel)
584+
.Add(1, joinLabelList)
602585
.Seal()
603586
.Add(i++, newJoinTree)
604587
.Add(i++, newJoinSettings)

0 commit comments

Comments
 (0)