mkql_node_printer_ut.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #include "mkql_node_printer.h"
  2. #include "mkql_node_builder.h"
  3. #include "mkql_node_serialization.h"
  4. #include <library/cpp/testing/unittest/registar.h>
  5. namespace NKikimr {
  6. namespace NMiniKQL {
  7. namespace {
  8. TNode* BuildGraph(const TTypeEnvironment& env) {
  9. TStructLiteralBuilder structBuilder(env);
  10. TDataType* dtype1 = TDataType::Create(NUdf::TDataType<ui32>::Id, env);
  11. TDataType* dtype2 = TDataType::Create(NUdf::TDataType<char*>::Id, env);
  12. TVector<TType*> callableTypes;
  13. callableTypes.push_back(env.GetVoidLazy()->GetGenericType());
  14. TCallableType* ctype1 = TCallableType::Create("ret data", dtype1, callableTypes.size(), callableTypes.data(), nullptr, env);
  15. TListType* ltype1 = TListType::Create(dtype1, env);
  16. TOptionalType* otype1 = TOptionalType::Create(dtype1, env);
  17. structBuilder.Add("01", TRuntimeNode(env.GetTypeOfTypeLazy(), true));
  18. structBuilder.Add("02", TRuntimeNode(env.GetTypeOfVoidLazy(), true));
  19. structBuilder.Add("03", TRuntimeNode(dtype1, true));
  20. structBuilder.Add("04", TRuntimeNode(TDataType::Create(NUdf::TDataType<ui32>::Id, env), true));
  21. structBuilder.Add("12", TRuntimeNode(env.GetEmptyStructLazy()->GetType(), true));
  22. TVector<std::pair<TString, TType*>> smallMembers;
  23. smallMembers.push_back(std::make_pair("Embedded member", env.GetVoidLazy()->GetGenericType()));
  24. structBuilder.Add("13", TRuntimeNode(TStructType::Create(smallMembers.data(), smallMembers.size(), env), true));
  25. structBuilder.Add("14", TRuntimeNode(TListType::Create(dtype1, env), true));
  26. structBuilder.Add("15", TRuntimeNode(TOptionalType::Create(dtype2, env), true));
  27. structBuilder.Add("16", TRuntimeNode(TDictType::Create(dtype1, dtype2, env), true));
  28. TVector<TType*> smallTypes;
  29. smallTypes.push_back(dtype1);
  30. structBuilder.Add("17", TRuntimeNode(TCallableType::Create("My callable",
  31. dtype2, smallTypes.size(), smallTypes.data(), nullptr, env), true));
  32. structBuilder.Add("18", TRuntimeNode(env.GetVoidLazy(), true));
  33. ui32 u = 345;
  34. structBuilder.Add("19", TRuntimeNode(TDataLiteral::Create(NUdf::TUnboxedValuePod(u), dtype1, env), true));
  35. auto v = TRuntimeNode(env.GetVoidLazy(), true);
  36. structBuilder.Add("26", TRuntimeNode(TListLiteral::Create(nullptr, 0, ltype1, env), true));
  37. TVector<TRuntimeNode> litems;
  38. litems.push_back(TRuntimeNode(TDataLiteral::Create(NUdf::TUnboxedValuePod(u), dtype1, env), true));
  39. u = 789;
  40. litems.push_back(TRuntimeNode(TDataLiteral::Create(NUdf::TUnboxedValuePod(u), dtype1, env), true));
  41. structBuilder.Add("27", TRuntimeNode(TListLiteral::Create(litems.data(), litems.size(), ltype1, env), true));
  42. structBuilder.Add("28", TRuntimeNode(TOptionalLiteral::Create(otype1, env), true));
  43. structBuilder.Add("29", TRuntimeNode(TOptionalLiteral::Create(litems[0], otype1, env), true));
  44. auto ditype1 = TDictType::Create(dtype1, dtype2, env);
  45. TVector<std::pair<TRuntimeNode, TRuntimeNode>> ditems;
  46. ditems.push_back(std::make_pair(TRuntimeNode(TDataLiteral::Create(NUdf::TUnboxedValuePod((ui32)456), dtype1, env), true),
  47. TRuntimeNode(TDataLiteral::Create(NUdf::TUnboxedValuePod::Embedded("aaaa"), dtype2, env), true)));
  48. structBuilder.Add("30", TRuntimeNode(TDictLiteral::Create(ditems.size(), ditems.data(), ditype1, env), true));
  49. TVector<TRuntimeNode> callableArgs;
  50. callableArgs.push_back(TRuntimeNode(env.GetVoidLazy(), true));
  51. TCallable* callable = TCallable::Create(callableArgs.size(), callableArgs.data(), ctype1, env);
  52. callable->SetResult(TRuntimeNode(TDataLiteral::Create(NUdf::TUnboxedValuePod(u), dtype1, env), true), env);
  53. structBuilder.Add("31", TRuntimeNode(callable, true));
  54. structBuilder.Add("32", TRuntimeNode(env.GetAnyTypeLazy(), true));
  55. structBuilder.Add("33", TRuntimeNode(TAny::Create(env), true));
  56. auto anyWithData = TAny::Create(env);
  57. anyWithData->SetItem(TRuntimeNode(env.GetVoidLazy(), true));
  58. structBuilder.Add("34", TRuntimeNode(anyWithData, true));
  59. structBuilder.Add("35", TRuntimeNode(TCallableType::Create("My callable 2",
  60. dtype2, callableArgs.size(), smallTypes.data(), env.GetVoidLazy(), env), true));
  61. TVector<TType*> tupleTypes;
  62. tupleTypes.push_back(dtype1);
  63. TVector<TRuntimeNode> tupleValues;
  64. tupleValues.push_back(TRuntimeNode(TDataLiteral::Create(NUdf::TUnboxedValuePod((ui32)456), dtype1, env), true));
  65. auto tupleType = TTupleType::Create(tupleTypes.size(), tupleTypes.data(), env);
  66. structBuilder.Add("36", TRuntimeNode(tupleType, true));
  67. structBuilder.Add("37", TRuntimeNode(TTupleLiteral::Create(tupleValues.size(), tupleValues.data(), tupleType, env), true));
  68. structBuilder.Add("38", TRuntimeNode(TResourceType::Create("myres", env), true));
  69. structBuilder.Add("39", TRuntimeNode(TStreamType::Create(dtype1, env), true));
  70. structBuilder.Add("40", TRuntimeNode(env.GetNullLazy(), true));
  71. structBuilder.Add("41", TRuntimeNode(env.GetEmptyListLazy(), true));
  72. structBuilder.Add("42", TRuntimeNode(env.GetEmptyDictLazy(), true));
  73. structBuilder.Add("43", TRuntimeNode(TTaggedType::Create(dtype1, "mytag", env), true));
  74. structBuilder.Add("44", TRuntimeNode(TBlockType::Create(dtype1, TBlockType::EShape::Scalar, env), true));
  75. structBuilder.Add("45", TRuntimeNode(TBlockType::Create(dtype2, TBlockType::EShape::Many, env), true));
  76. structBuilder.Add("46", TRuntimeNode(TPgType::Create(23, env), true)); // int4 type
  77. return structBuilder.Build();
  78. }
  79. }
  80. Y_UNIT_TEST_SUITE(TMiniKQLNodePrinterTest) {
  81. Y_UNIT_TEST(TestPrintWithoutSchema) {
  82. TScopedAlloc alloc(__LOCATION__);
  83. TTypeEnvironment env(alloc);
  84. auto node = BuildGraph(env);
  85. TString s = PrintNode(node);
  86. //Cout << s << Endl;
  87. auto serialized = SerializeNode(node, env);
  88. TNode* node2 = DeserializeNode(serialized, env);
  89. TString s2 = PrintNode(node2, false);
  90. UNIT_ASSERT_EQUAL(s2, s);
  91. }
  92. Y_UNIT_TEST(RuntimeNodeSerialization) {
  93. TScopedAlloc alloc(__LOCATION__);
  94. TTypeEnvironment env(alloc);
  95. TDataType* dtype1 = TDataType::Create(NUdf::TDataType<ui32>::Id, env);
  96. TCallableType* ctype1 = TCallableType::Create("ctype1", dtype1, 0, nullptr, nullptr, env);
  97. TCallable* c1 = TCallable::Create(0, nullptr, ctype1, env);
  98. TRuntimeNode r1(c1, false);
  99. auto s1 = SerializeRuntimeNode(r1, env);
  100. TRuntimeNode r2 = DeserializeRuntimeNode(s1, env);
  101. UNIT_ASSERT(r1 == r2);
  102. TRuntimeNode r3(c1, true);
  103. auto s2 = SerializeRuntimeNode(r3, env);
  104. TRuntimeNode r4 = DeserializeRuntimeNode(s2, env);
  105. UNIT_ASSERT(r3 == r4);
  106. UNIT_ASSERT(r1 != r3);
  107. }
  108. }
  109. }
  110. }