|
16 | 16 | #include <ydb/library/yql/utils/plan/plan_utils.h>
|
17 | 17 | #include <ydb/library/yql/providers/dq/common/yql_dq_settings.h>
|
18 | 18 |
|
| 19 | +#include <ydb/public/lib/ydb_cli/common/format.h> |
| 20 | + |
19 | 21 | #include <library/cpp/json/writer/json.h>
|
20 | 22 | #include <library/cpp/json/json_reader.h>
|
21 | 23 | #include <library/cpp/protobuf/json/proto2json.h>
|
@@ -1811,6 +1813,217 @@ void SetNonZero(NJson::TJsonValue& node, const TStringBuf& name, T value) {
|
1811 | 1813 | }
|
1812 | 1814 | }
|
1813 | 1815 |
|
| 1816 | +void BuildPlanIndex(NJson::TJsonValue& plan, THashMap<int, NJson::TJsonValue>& planIndex, THashMap<TString, NJson::TJsonValue>& precomputes) { |
| 1817 | + if (plan.GetMapSafe().contains("PlanNodeId")){ |
| 1818 | + auto id = plan.GetMapSafe().at("PlanNodeId").GetIntegerSafe(); |
| 1819 | + planIndex[id] = plan; |
| 1820 | + } |
| 1821 | + |
| 1822 | + if (plan.GetMapSafe().contains("Subplan Name")) { |
| 1823 | + const auto& precomputeName = plan.GetMapSafe().at("Subplan Name").GetStringSafe(); |
| 1824 | + |
| 1825 | + auto pos = precomputeName.find("precompute"); |
| 1826 | + if (pos != TString::npos) { |
| 1827 | + precomputes[precomputeName.substr(pos)] = plan; |
| 1828 | + } |
| 1829 | + } |
| 1830 | + |
| 1831 | + if (plan.GetMapSafe().contains("Plans")) { |
| 1832 | + for (auto p : plan.GetMapSafe().at("Plans").GetArraySafe()) { |
| 1833 | + BuildPlanIndex(p, planIndex, precomputes); |
| 1834 | + } |
| 1835 | + } |
| 1836 | +} |
| 1837 | + |
| 1838 | +TVector<NJson::TJsonValue> RemoveRedundantNodes(NJson::TJsonValue& plan, const THashSet<TString>& redundantNodes) { |
| 1839 | + auto& planMap = plan.GetMapSafe(); |
| 1840 | + |
| 1841 | + TVector<NJson::TJsonValue> children; |
| 1842 | + if (planMap.contains("Plans") && planMap.at("Plans").IsArray()) { |
| 1843 | + for (auto& child : planMap.at("Plans").GetArraySafe()) { |
| 1844 | + auto newChildren = RemoveRedundantNodes(child, redundantNodes); |
| 1845 | + children.insert(children.end(), newChildren.begin(), newChildren.end()); |
| 1846 | + } |
| 1847 | + } |
| 1848 | + |
| 1849 | + planMap.erase("Plans"); |
| 1850 | + if (!children.empty()) { |
| 1851 | + auto& plans = planMap["Plans"]; |
| 1852 | + for (auto& child : children) { |
| 1853 | + plans.AppendValue(child); |
| 1854 | + } |
| 1855 | + } |
| 1856 | + |
| 1857 | + const auto typeName = planMap.at("Node Type").GetStringSafe(); |
| 1858 | + if (redundantNodes.contains(typeName) || typeName.find("Precompute") != TString::npos) { |
| 1859 | + return children; |
| 1860 | + } |
| 1861 | + |
| 1862 | + return {plan}; |
| 1863 | +} |
| 1864 | + |
| 1865 | +NJson::TJsonValue ReconstructQueryPlanRec(const NJson::TJsonValue& plan, |
| 1866 | + int operatorIndex, |
| 1867 | + const THashMap<int, NJson::TJsonValue>& planIndex, |
| 1868 | + const THashMap<TString, NJson::TJsonValue>& precomputes, |
| 1869 | + int& nodeCounter) { |
| 1870 | + |
| 1871 | + int currentNodeId = nodeCounter++; |
| 1872 | + |
| 1873 | + NJson::TJsonValue result; |
| 1874 | + result["PlanNodeId"] = currentNodeId; |
| 1875 | + if (plan.GetMapSafe().contains("PlanNodeType")) { |
| 1876 | + result["PlanNodeType"] = plan.GetMapSafe().at("PlanNodeType").GetStringSafe(); |
| 1877 | + } |
| 1878 | + |
| 1879 | + if (plan.GetMapSafe().contains("Stats")) { |
| 1880 | + result["Stats"] = plan.GetMapSafe().at("Stats"); |
| 1881 | + } |
| 1882 | + |
| 1883 | + if (!plan.GetMapSafe().contains("Operators")) { |
| 1884 | + NJson::TJsonValue planInputs; |
| 1885 | + |
| 1886 | + result["Node Type"] = plan.GetMapSafe().at("Node Type").GetStringSafe(); |
| 1887 | + |
| 1888 | + if (!plan.GetMapSafe().contains("Plans")) { |
| 1889 | + return result; |
| 1890 | + } |
| 1891 | + |
| 1892 | + if (plan.GetMapSafe().at("Node Type") == "TableLookup") { |
| 1893 | + NJson::TJsonValue newOps; |
| 1894 | + NJson::TJsonValue op; |
| 1895 | + |
| 1896 | + op["Name"] = "TableLookup"; |
| 1897 | + op["Columns"] = plan.GetMapSafe().at("Columns"); |
| 1898 | + op["LookupKeyColumns"] = plan.GetMapSafe().at("LookupKeyColumns"); |
| 1899 | + op["Table"] = plan.GetMapSafe().at("Table"); |
| 1900 | + |
| 1901 | + newOps.AppendValue(op); |
| 1902 | + |
| 1903 | + result["Operators"] = newOps; |
| 1904 | + return result; |
| 1905 | + } |
| 1906 | + |
| 1907 | + for (auto p : plan.GetMapSafe().at("Plans").GetArraySafe()) { |
| 1908 | + if (p.GetMapSafe().at("Node Type").GetStringSafe().find("Precompute") == TString::npos) { |
| 1909 | + planInputs.AppendValue(ReconstructQueryPlanRec(p, 0, planIndex, precomputes, nodeCounter)); |
| 1910 | + } |
| 1911 | + } |
| 1912 | + result["Plans"] = planInputs; |
| 1913 | + return result; |
| 1914 | + } |
| 1915 | + |
| 1916 | + if (plan.GetMapSafe().contains("CTE Name") && plan.GetMapSafe().at("Node Type") == "ConstantExpr") { |
| 1917 | + auto precompute = plan.GetMapSafe().at("CTE Name").GetStringSafe(); |
| 1918 | + if (!precomputes.contains(precompute)) { |
| 1919 | + result["Node Type"] = "ConstantExpr"; |
| 1920 | + return result; |
| 1921 | + } |
| 1922 | + return ReconstructQueryPlanRec(precomputes.at(precompute), 0, planIndex, precomputes, nodeCounter); |
| 1923 | + } |
| 1924 | + |
| 1925 | + auto ops = plan.GetMapSafe().at("Operators").GetArraySafe(); |
| 1926 | + auto op = ops[operatorIndex]; |
| 1927 | + |
| 1928 | + TVector<NJson::TJsonValue> planInputs; |
| 1929 | + |
| 1930 | + auto opName = op.GetMapSafe().at("Name").GetStringSafe(); |
| 1931 | + |
| 1932 | + for (auto opInput : op.GetMapSafe().at("Inputs").GetArraySafe()) { |
| 1933 | + if (opInput.GetMapSafe().contains("ExternalPlanNodeId")) { |
| 1934 | + auto inputPlanKey = opInput.GetMapSafe().at("ExternalPlanNodeId").GetIntegerSafe(); |
| 1935 | + auto inputPlan = planIndex.at(inputPlanKey); |
| 1936 | + planInputs.push_back( ReconstructQueryPlanRec(inputPlan, 0, planIndex, precomputes, nodeCounter)); |
| 1937 | + } else if (opInput.GetMapSafe().contains("InternalOperatorId")) { |
| 1938 | + auto inputPlanId = opInput.GetMapSafe().at("InternalOperatorId").GetIntegerSafe(); |
| 1939 | + planInputs.push_back( ReconstructQueryPlanRec(plan, inputPlanId, planIndex, precomputes, nodeCounter)); |
| 1940 | + } |
| 1941 | + // temp hack |
| 1942 | + if (opName == "Filter") { |
| 1943 | + break; |
| 1944 | + } |
| 1945 | + } |
| 1946 | + |
| 1947 | + if (op.GetMapSafe().contains("Inputs")) { |
| 1948 | + op.GetMapSafe().erase("Inputs"); |
| 1949 | + } |
| 1950 | + |
| 1951 | + if (op.GetMapSafe().contains("Input") || op.GetMapSafe().contains("ToFlow")) { |
| 1952 | + TString maybePrecompute = ""; |
| 1953 | + if (op.GetMapSafe().contains("Input")) { |
| 1954 | + maybePrecompute = op.GetMapSafe().at("Input").GetStringSafe(); |
| 1955 | + } else if (op.GetMapSafe().contains("ToFlow")) { |
| 1956 | + maybePrecompute = op.GetMapSafe().at("ToFlow").GetStringSafe(); |
| 1957 | + } |
| 1958 | + |
| 1959 | + if (precomputes.contains(maybePrecompute)) { |
| 1960 | + planInputs.push_back(ReconstructQueryPlanRec(precomputes.at(maybePrecompute), 0, planIndex, precomputes, nodeCounter)); |
| 1961 | + } |
| 1962 | + } |
| 1963 | + |
| 1964 | + result["Node Type"] = opName; |
| 1965 | + NJson::TJsonValue newOps; |
| 1966 | + newOps.AppendValue(op); |
| 1967 | + result["Operators"] = newOps; |
| 1968 | + |
| 1969 | + if (planInputs.size()){ |
| 1970 | + NJson::TJsonValue plans; |
| 1971 | + for( auto i : planInputs) { |
| 1972 | + plans.AppendValue(i); |
| 1973 | + } |
| 1974 | + result["Plans"] = plans; |
| 1975 | + } |
| 1976 | + |
| 1977 | + return result; |
| 1978 | +} |
| 1979 | + |
| 1980 | +NJson::TJsonValue SimplifyQueryPlan(NJson::TJsonValue& plan) { |
| 1981 | + static const THashSet<TString> redundantNodes = { |
| 1982 | + "UnionAll", |
| 1983 | + "Broadcast", |
| 1984 | + "Map", |
| 1985 | + "HashShuffle", |
| 1986 | + "Merge", |
| 1987 | + "Collect", |
| 1988 | + "Stage", |
| 1989 | + "Iterator", |
| 1990 | + "PartitionByKey", |
| 1991 | + "ToFlow" |
| 1992 | + }; |
| 1993 | + |
| 1994 | + THashMap<int, NJson::TJsonValue> planIndex; |
| 1995 | + THashMap<TString, NJson::TJsonValue> precomputes; |
| 1996 | + |
| 1997 | + |
| 1998 | + BuildPlanIndex(plan, planIndex, precomputes); |
| 1999 | + |
| 2000 | + int nodeCounter = 0; |
| 2001 | + plan = ReconstructQueryPlanRec(plan, 0, planIndex, precomputes, nodeCounter); |
| 2002 | + RemoveRedundantNodes(plan, redundantNodes); |
| 2003 | + return plan; |
| 2004 | +} |
| 2005 | + |
| 2006 | +TString AddSimplifiedPlan(const TString& planText, bool analyzeMode) { |
| 2007 | + Y_UNUSED(analyzeMode); |
| 2008 | + NJson::TJsonValue planJson; |
| 2009 | + NJson::ReadJsonTree(planText, &planJson, true); |
| 2010 | + if (!planJson.GetMapSafe().contains("Plan")){ |
| 2011 | + return planText; |
| 2012 | + } |
| 2013 | + |
| 2014 | + NJson::TJsonValue planCopy; |
| 2015 | + NJson::ReadJsonTree(planText, &planCopy, true); |
| 2016 | + |
| 2017 | + planJson["SimplifiedPlan"] = SimplifyQueryPlan(planCopy.GetMapSafe().at("Plan")); |
| 2018 | + |
| 2019 | + // Don't print the OLAP plan yet, there are some non UTF-8 symbols there that need to be fixed |
| 2020 | + //TTempBufOutput stringStream; |
| 2021 | + //NYdb::NConsoleClient::TQueryPlanPrinter printer(NYdb::NConsoleClient::EOutputFormat::PrettyTable, analyzeMode, stringStream); |
| 2022 | + //printer.Print(planJson.GetStringRobust()); |
| 2023 | + //planJson["OLAPText"] = stringStream.Data(); |
| 2024 | + return planJson.GetStringRobust(); |
| 2025 | +} |
| 2026 | + |
1814 | 2027 | TString SerializeTxPlans(const TVector<const TString>& txPlans, const TString commonPlanInfo = "") {
|
1815 | 2028 | NJsonWriter::TBuf writer;
|
1816 | 2029 | writer.SetIndentSpaces(2);
|
@@ -1862,7 +2075,8 @@ TString SerializeTxPlans(const TVector<const TString>& txPlans, const TString co
|
1862 | 2075 | writer.EndObject();
|
1863 | 2076 | writer.EndObject();
|
1864 | 2077 |
|
1865 |
| - return writer.Str(); |
| 2078 | + auto resultPlan = writer.Str(); |
| 2079 | + return AddSimplifiedPlan(resultPlan, false); |
1866 | 2080 | }
|
1867 | 2081 |
|
1868 | 2082 | } // namespace
|
@@ -2250,7 +2464,8 @@ TString AddExecStatsToTxPlan(const TString& txPlanJson, const NYql::NDqProto::TD
|
2250 | 2464 |
|
2251 | 2465 | NJsonWriter::TBuf txWriter;
|
2252 | 2466 | txWriter.WriteJsonValue(&root, true);
|
2253 |
| - return txWriter.Str(); |
| 2467 | + auto resultPlan = txWriter.Str(); |
| 2468 | + return AddSimplifiedPlan(resultPlan, true); |
2254 | 2469 | }
|
2255 | 2470 |
|
2256 | 2471 | TString SerializeAnalyzePlan(const NKqpProto::TKqpStatsQuery& queryStats) {
|
@@ -2294,6 +2509,9 @@ TString SerializeScriptPlan(const TVector<const TString>& queryPlans) {
|
2294 | 2509 | if (auto dqPlan = planMap.FindPtr("Plan")) {
|
2295 | 2510 | writer.WriteKey("Plan");
|
2296 | 2511 | writer.WriteJsonValue(dqPlan);
|
| 2512 | + writer.WriteKey("SimplifiedPlan"); |
| 2513 | + auto simplifiedPlan = SimplifyQueryPlan(*dqPlan); |
| 2514 | + writer.WriteJsonValue(&simplifiedPlan); |
2297 | 2515 | }
|
2298 | 2516 | writer.EndObject();
|
2299 | 2517 | }
|
|
0 commit comments