Skip to content

Commit e2acc46

Browse files
igormunkinblinkov
authored andcommitted
YQL-19597: Fix swap of WideToBlocks with Extend in peephole optimizer
Follows up d56f1cbae00a964056a5a5216fe91918cbf73463 commit_hash:2436c8f5e305aa6961b3bad54fdebdbf908cdafa
1 parent c4d8fba commit e2acc46

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

yql/essentials/core/peephole_opt/yql_opt_peephole_physical.cpp

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -146,14 +146,37 @@ TExprNode::TPtr OptimizeWideToBlocks(const TExprNode::TPtr& node, TExprContext&
146146
.Build();
147147
}
148148

149-
if (input.IsCallable({"Extend", "OrderedExtend"})) {
150-
YQL_CLOG(DEBUG, CorePeepHole) << "Swap " << node->Content() << " with " << input.Content();
149+
if (input.IsCallable("FromFlow") && input.Head().IsCallable({"Extend", "OrderedExtend"})) {
150+
const auto& extend = input.Head();
151+
// Technically, the code below rewrites the following sequence
152+
// (WideToBlocks (FromFlow (Extend (<input>))))
153+
// into (Extend (WideToBlocks (FromFlow (<input>))), but
154+
// the logging is left intact, omitting the FromFlow barrier.
155+
YQL_CLOG(DEBUG, CorePeepHole) << "Swap " << node->Content() << " with " << extend.Content();
151156
TExprNodeList newChildren;
152-
newChildren.reserve(input.ChildrenSize());
153-
for (auto& child : input.ChildrenList()) {
154-
newChildren.emplace_back(ctx.ChangeChild(*node, 0, std::move(child)));
157+
newChildren.reserve(extend.ChildrenSize());
158+
for (const auto& child : extend.ChildrenList()) {
159+
// Extend callable can handle any sequential type, so
160+
// just wrap all its children with (ToStream (...)).
161+
// However, its *block* overload works only with WideFlow,
162+
// so the new child is wrapped with ToFlow callable.
163+
const auto newChild = ctx.Builder(node->Pos())
164+
.Callable("ToFlow")
165+
.Callable(0, "WideToBlocks")
166+
.Callable(0, "ToStream")
167+
.Add(0, child)
168+
.Seal()
169+
.Seal()
170+
.Seal()
171+
.Build();
172+
newChildren.emplace_back(newChild);
155173
}
156-
return ctx.NewCallable(input.Pos(), input.IsCallable("Extend") ? "BlockExtend" : "BlockOrderedExtend", std::move(newChildren));
174+
const auto newName = extend.IsCallable("Extend") ? "BlockExtend" : "BlockOrderedExtend";
175+
return ctx.Builder(node->Pos())
176+
.Callable("FromFlow")
177+
.Add(0, ctx.NewCallable(input.Pos(), newName, std::move(newChildren)))
178+
.Seal()
179+
.Build();
157180
}
158181

159182
return node;

yql/essentials/tests/sql/minirun/part5/canondata/result.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -375,9 +375,9 @@
375375
],
376376
"test.test[blocks-extend-default.txt-Peephole]": [
377377
{
378-
"checksum": "2bec63a1689cb0b100f82fec2b89cd3c",
379-
"size": 629,
380-
"uri": "https://{canondata_backend}/1809005/02f459fce1f16d89b3444e6e8728b9747bb52b53/resource.tar.gz#test.test_blocks-extend-default.txt-Peephole_/opt.yql"
378+
"checksum": "9d73defb3c7ec979ad15c0d105f3211e",
379+
"size": 664,
380+
"uri": "https://{canondata_backend}/1775059/85273762ecc854fd58fe1daec09cca032c02ccb1/resource.tar.gz#test.test_blocks-extend-default.txt-Peephole_/opt.yql"
381381
}
382382
],
383383
"test.test[blocks-extend-default.txt-Results]": [

0 commit comments

Comments
 (0)