@@ -146,14 +146,37 @@ TExprNode::TPtr OptimizeWideToBlocks(const TExprNode::TPtr& node, TExprContext&
146
146
.Build ();
147
147
}
148
148
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 ();
151
156
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);
155
173
}
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 ();
157
180
}
158
181
159
182
return node;
0 commit comments