Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix cross join with list iterator. #4683

Merged
merged 5 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions ydb/library/yql/core/common_opt/yql_co_simple1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2074,6 +2074,12 @@ TExprNode::TPtr SimpleFlatMap(const TExprNode::TPtr& node, TExprContext& ctx, TO
}
}

if (lambdaBody.IsCallable("ForwardList")) {
const bool keepList = node->GetTypeAnn()->GetKind() == ETypeAnnotationKind::List && lambdaBody.Head().GetTypeAnn()->GetKind() == ETypeAnnotationKind::Flow;
YQL_CLOG(DEBUG, Core) << (keepList ? "Pull" : "Drop") << " out " << lambdaBody.Content() << " from " << node->Content() << " lambda root.";
return ctx.WrapByCallableIf(keepList, lambdaBody.Content(), ctx.ChangeChild(*node, 1U, ctx.DeepCopyLambda(node->Tail(), lambdaBody.HeadPtr())));
}

if (CanRewriteToEmptyContainer(*node)) {
const auto& inputToCheck = SkipCallables(node->Head(), SkippableCallables);
if (IsEmptyContainer(inputToCheck) || IsEmpty(inputToCheck, *optCtx.Types)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ TExprNode::TPtr ExpandEquiJoinImpl(const TExprNode& node, TExprContext& ctx) {
const auto& renames = GetRenames(node, ctx);
const auto& joinKind = node.Child(2)->Head().Content();
if (joinKind == "Cross") {
return ctx.Builder(node.Pos())
auto result = ctx.Builder(node.Pos())
.Callable("FlatMap")
.Add(0, std::move(list1))
.Lambda(1)
Expand Down Expand Up @@ -565,6 +565,20 @@ TExprNode::TPtr ExpandEquiJoinImpl(const TExprNode& node, TExprContext& ctx) {
.Seal()
.Seal()
.Seal().Build();

if (const auto iterator = FindNode(result->Tail().Tail().HeadPtr(),
[] (const TExprNode::TPtr& node) { return node->IsCallable("Iterator"); })) {
auto children = iterator->ChildrenList();
children.emplace_back(ctx.NewCallable(iterator->Pos(), "DependsOn", {result->Tail().Head().HeadPtr()}));
result = ctx.ReplaceNode(std::move(result), *iterator, ctx.ChangeChildren(*iterator, std::move(children)));
}

if (const auto forward = FindNode(result->Tail().Tail().HeadPtr(),
[] (const TExprNode::TPtr& node) { return node->IsCallable("ForwardList"); })) {
result = ctx.ReplaceNode(std::move(result), *forward, ctx.RenameNode(*forward, "Collect"));
}

return result;
}

const auto list1type = list1->GetTypeAnn()->Cast<TListExprType>()->GetItemType()->Cast<TStructExprType>();
Expand Down
Loading