diff options
author | aozeritsky <aozeritsky@ydb.tech> | 2023-08-30 17:07:09 +0300 |
---|---|---|
committer | aozeritsky <aozeritsky@ydb.tech> | 2023-08-30 17:50:17 +0300 |
commit | 869faeec69e4aa549f3f73d3ec01ba5a5b6e423f (patch) | |
tree | cd83bf8c333cffec3ccf15e22b49c852c036d055 | |
parent | a9f92e7ccfda9fb289c8212b4f54ba41730b164a (diff) | |
download | ydb-869faeec69e4aa549f3f73d3ec01ba5a5b6e423f.tar.gz |
Print compact json for CostsOf
-rw-r--r-- | ydb/library/yql/core/peephole_opt/yql_opt_peephole_physical.cpp | 62 |
1 files changed, 36 insertions, 26 deletions
diff --git a/ydb/library/yql/core/peephole_opt/yql_opt_peephole_physical.cpp b/ydb/library/yql/core/peephole_opt/yql_opt_peephole_physical.cpp index 62b4c057df3..9485386b8f3 100644 --- a/ydb/library/yql/core/peephole_opt/yql_opt_peephole_physical.cpp +++ b/ydb/library/yql/core/peephole_opt/yql_opt_peephole_physical.cpp @@ -5670,46 +5670,56 @@ TExprNode::TPtr ExpandConstraintsOf(const TExprNode::TPtr& node, TExprContext& c TExprNode::TPtr ExpandCostsOf(const TExprNode::TPtr& node, TExprContext& ctx, TTypeAnnotationContext& typesCtx) { YQL_CLOG(DEBUG, CorePeepHole) << "Expand " << node->Content(); - TString json; - TStringOutput out(json); - NJson::TJsonWriter jsonWriter(&out, true); + TNodeMap<TString> visitedNodes; + std::function<TString(const TExprNode::TPtr& node)> printNode = [&](const TExprNode::TPtr& node) -> TString { + auto [it, emplaced] = visitedNodes.emplace(node.Get(), ""); + if (!emplaced) { + return it->second; + } - VisitExpr(node, [&](const TExprNode::TPtr& node) { - auto stat = typesCtx.GetStats(node.Get()); + std::vector<TString> chInfo; + for (const auto& child : node->ChildrenList()) { + auto res = printNode(child); + if (res) { + chInfo.emplace_back(std::move(res)); + } + } - if (stat || node->ChildrenSize()) { + auto stat = typesCtx.GetStats(node.Get()); + if (!chInfo.empty() || stat) { + TStringOutput out(it->second); + NJson::TJsonWriter jsonWriter(&out, false); jsonWriter.OpenMap(); - jsonWriter.WriteKey("Name"); - jsonWriter.Write(node->Content()); + if (node->Content()) { + jsonWriter.Write("Name", node->Content()); + } if (stat) { if (stat->Cost) { - jsonWriter.WriteKey("Cost"); - jsonWriter.Write(*stat->Cost); + jsonWriter.Write("Cost", *stat->Cost); } - jsonWriter.WriteKey("Cols"); - jsonWriter.Write(stat->Ncols); - jsonWriter.WriteKey("Rows"); - jsonWriter.Write(stat->Nrows); + jsonWriter.Write("Cols", stat->Ncols); + jsonWriter.Write("Rows", stat->Nrows); } - if (node->ChildrenSize()) { + if (!chInfo.empty()) { jsonWriter.WriteKey("Children"); jsonWriter.OpenArray(); - } - } - return true; - }, [&](const TExprNode::TPtr& node) { - auto stat = typesCtx.GetStats(node.Get()); - - if (stat || node->ChildrenSize()) { - if (node->ChildrenSize()) { + for (const auto& info : chInfo) { + jsonWriter.UnsafeWrite(info); + } jsonWriter.CloseArray(); } jsonWriter.CloseMap(); + jsonWriter.Flush(); } - return true; - }); - jsonWriter.Flush(); + return it->second; + }; + + TString json = printNode(node); + + if (!json) { + json = "{}"; + } return ctx.Builder(node->Pos()) .Callable("Json") |