summaryrefslogtreecommitdiffstats
path: root/yql/essentials/public/purecalc/common/transformations/utils.cpp
diff options
context:
space:
mode:
authorvvvv <[email protected]>2024-11-26 12:00:01 +0300
committervvvv <[email protected]>2024-11-26 12:11:55 +0300
commit376f6f42244428b36cde31eaa8c7e89a90d7fd1b (patch)
treeae023e0a2285cfbf8ad4441f6e1a146c2dd1383f /yql/essentials/public/purecalc/common/transformations/utils.cpp
parentd7c900fd2ee9f48165550376be3c798be3299a59 (diff)
Moved yql/public/purecalc YQL-19206
init commit_hash:abf729827c312980464da21824f86ea1defe094c
Diffstat (limited to 'yql/essentials/public/purecalc/common/transformations/utils.cpp')
-rw-r--r--yql/essentials/public/purecalc/common/transformations/utils.cpp179
1 files changed, 179 insertions, 0 deletions
diff --git a/yql/essentials/public/purecalc/common/transformations/utils.cpp b/yql/essentials/public/purecalc/common/transformations/utils.cpp
new file mode 100644
index 00000000000..4e2da41835c
--- /dev/null
+++ b/yql/essentials/public/purecalc/common/transformations/utils.cpp
@@ -0,0 +1,179 @@
+#include "utils.h"
+
+#include <yql/essentials/public/purecalc/common/names.h>
+#include <yql/essentials/core/yql_expr_type_annotation.h>
+
+using namespace NYql;
+using namespace NYql::NPureCalc;
+
+TExprNode::TPtr NYql::NPureCalc::NodeFromBlocks(
+ const TPositionHandle& pos,
+ const TStructExprType* structType,
+ TExprContext& ctx
+) {
+ const auto items = structType->GetItems();
+ Y_ENSURE(items.size() > 0);
+ return ctx.Builder(pos)
+ .Lambda()
+ .Param("stream")
+ .Callable(0, "FromFlow")
+ .Callable(0, "NarrowMap")
+ .Callable(0, "WideFromBlocks")
+ .Callable(0, "ExpandMap")
+ .Callable(0, "ToFlow")
+ .Arg(0, "stream")
+ .Seal()
+ .Lambda(1)
+ .Param("item")
+ .Do([&](TExprNodeBuilder& lambda) -> TExprNodeBuilder& {
+ ui32 i = 0;
+ for (const auto& item : items) {
+ lambda.Callable(i++, "Member")
+ .Arg(0, "item")
+ .Atom(1, item->GetName())
+ .Seal();
+ }
+ lambda.Callable(i, "Member")
+ .Arg(0, "item")
+ .Atom(1, PurecalcBlockColumnLength)
+ .Seal();
+ return lambda;
+ })
+ .Seal()
+ .Seal()
+ .Seal()
+ .Lambda(1)
+ .Params("fields", items.size())
+ .Callable("AsStruct")
+ .Do([&](TExprNodeBuilder& parent) -> TExprNodeBuilder& {
+ ui32 i = 0;
+ for (const auto& item : items) {
+ parent.List(i)
+ .Atom(0, item->GetName())
+ .Arg(1, "fields", i++)
+ .Seal();
+ }
+ return parent;
+ })
+ .Seal()
+ .Seal()
+ .Seal()
+ .Seal()
+ .Seal()
+ .Build();
+}
+
+TExprNode::TPtr NYql::NPureCalc::NodeToBlocks(
+ const TPositionHandle& pos,
+ const TStructExprType* structType,
+ TExprContext& ctx
+) {
+ const auto items = structType->GetItems();
+ Y_ENSURE(items.size() > 0);
+ return ctx.Builder(pos)
+ .Lambda()
+ .Param("stream")
+ .Callable("FromFlow")
+ .Callable(0, "NarrowMap")
+ .Callable(0, "WideToBlocks")
+ .Callable(0, "ExpandMap")
+ .Callable(0, "ToFlow")
+ .Arg(0, "stream")
+ .Seal()
+ .Lambda(1)
+ .Param("item")
+ .Do([&](TExprNodeBuilder& lambda) -> TExprNodeBuilder& {
+ ui32 i = 0;
+ for (const auto& item : items) {
+ lambda.Callable(i++, "Member")
+ .Arg(0, "item")
+ .Atom(1, item->GetName())
+ .Seal();
+ }
+ return lambda;
+ })
+ .Seal()
+ .Seal()
+ .Seal()
+ .Lambda(1)
+ .Params("fields", items.size() + 1)
+ .Callable("AsStruct")
+ .Do([&](TExprNodeBuilder& parent) -> TExprNodeBuilder& {
+ ui32 i = 0;
+ for (const auto& item : items) {
+ parent.List(i)
+ .Atom(0, item->GetName())
+ .Arg(1, "fields", i++)
+ .Seal();
+ }
+ parent.List(i)
+ .Atom(0, PurecalcBlockColumnLength)
+ .Arg(1, "fields", i)
+ .Seal();
+ return parent;
+ })
+ .Seal()
+ .Seal()
+ .Seal()
+ .Seal()
+ .Seal()
+ .Build();
+}
+
+TExprNode::TPtr NYql::NPureCalc::ApplyToIterable(
+ const TPositionHandle& pos,
+ const TExprNode::TPtr iterable,
+ const TExprNode::TPtr lambda,
+ bool wrapLMap,
+ TExprContext& ctx
+) {
+ if (wrapLMap) {
+ return ctx.Builder(pos)
+ .Callable("LMap")
+ .Add(0, iterable)
+ .Lambda(1)
+ .Param("stream")
+ .Apply(lambda)
+ .With(0, "stream")
+ .Seal()
+ .Seal()
+ .Seal()
+ .Build();
+ } else {
+ return ctx.Builder(pos)
+ .Apply(lambda)
+ .With(0, iterable)
+ .Seal()
+ .Build();
+ }
+}
+
+const TStructExprType* NYql::NPureCalc::WrapBlockStruct(
+ const TStructExprType* structType,
+ TExprContext& ctx
+) {
+ TVector<const TItemExprType*> members;
+ for (const auto& item : structType->GetItems()) {
+ const auto blockItemType = ctx.MakeType<TBlockExprType>(item->GetItemType());
+ members.push_back(ctx.MakeType<TItemExprType>(item->GetName(), blockItemType));
+ }
+ const auto scalarItemType = ctx.MakeType<TScalarExprType>(ctx.MakeType<TDataExprType>(EDataSlot::Uint64));
+ members.push_back(ctx.MakeType<TItemExprType>(PurecalcBlockColumnLength, scalarItemType));
+ return ctx.MakeType<TStructExprType>(members);
+}
+
+const TStructExprType* NYql::NPureCalc::UnwrapBlockStruct(
+ const TStructExprType* structType,
+ TExprContext& ctx
+) {
+ TVector<const TItemExprType*> members;
+ for (const auto& item : structType->GetItems()) {
+ if (item->GetName() == PurecalcBlockColumnLength) {
+ continue;
+ }
+ bool isScalarUnused;
+ const auto blockItemType = GetBlockItemType(*item->GetItemType(), isScalarUnused);
+ members.push_back(ctx.MakeType<TItemExprType>(item->GetName(), blockItemType));
+ }
+ return ctx.MakeType<TStructExprType>(members);
+}