Skip to content

Commit c62a698

Browse files
authored
Optimize Coalesce(Just(...) ...) on peephole final stage. (#3957)
1 parent 50a24e3 commit c62a698

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

ydb/library/yql/core/peephole_opt/yql_opt_peephole_physical.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8147,16 +8147,23 @@ TExprNode::TPtr DropAssume(const TExprNode::TPtr& node, TExprContext&) {
81478147
}
81488148

81498149
TExprNode::TPtr OptimizeCoalesce(const TExprNode::TPtr& node, TExprContext& ctx) {
8150-
if (const auto& input = node->Head(); input.IsCallable("If") && (input.Child(1U)->IsComplete() || input.Child(2U)->IsComplete())) {
8150+
if (const auto& input = node->Head(); input.IsCallable("If") && input.ChildrenSize() == 3U &&
8151+
(input.Child(1U)->IsComplete() || input.Child(2U)->IsComplete())) {
81518152
YQL_CLOG(DEBUG, CorePeepHole) << "Dive " << node->Content() << " into " << input.Content();
81528153
return ctx.ChangeChildren(input, {
81538154
input.HeadPtr(),
81548155
ctx.ChangeChild(*node, 0U, input.ChildPtr(1U)),
81558156
ctx.ChangeChild(*node, 0U, input.ChildPtr(2U))
81568157
});
8157-
} else if (input.IsCallable("Nothing")) {
8158-
YQL_CLOG(DEBUG, CorePeepHole) << "Drop " << node->Content() << " over " << input.Content();
8159-
return node->TailPtr();
8158+
} else if (node->ChildrenSize() == 2U) {
8159+
if (input.IsCallable("Nothing")) {
8160+
YQL_CLOG(DEBUG, CorePeepHole) << "Drop " << node->Content() << " over " << input.Content();
8161+
return node->TailPtr();
8162+
} else if (input.IsCallable("Just")) {
8163+
YQL_CLOG(DEBUG, CorePeepHole) << "Drop " << node->Content() << " over " << input.Content();
8164+
return IsSameAnnotation(*node->GetTypeAnn(), *input.GetTypeAnn()) ?
8165+
node->HeadPtr() : input.HeadPtr();
8166+
}
81608167
}
81618168
return node;
81628169
}

0 commit comments

Comments
 (0)