mkql_node_builder_ut.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #include "mkql_node_builder.h"
  2. #include "mkql_node_printer.h"
  3. #include <yql/essentials/public/udf/udf_type_inspection.h>
  4. #include "mkql_type_builder.h"
  5. #include <library/cpp/testing/unittest/registar.h>
  6. namespace NKikimr {
  7. namespace NMiniKQL {
  8. Y_UNIT_TEST_SUITE(TMiniKQLNodeBuilderTest) {
  9. Y_UNIT_TEST(TestDataBuild) {
  10. TScopedAlloc alloc(__LOCATION__);
  11. TTypeEnvironment env(alloc);
  12. auto d2 = BuildDataLiteral(NUdf::TUnboxedValuePod((ui32)456), NUdf::EDataSlot::Uint32, env);
  13. UNIT_ASSERT_EQUAL(d2->GetType()->GetSchemeType(), NUdf::TDataType<ui32>::Id);
  14. }
  15. Y_UNIT_TEST(TestTupleBuilder) {
  16. TScopedAlloc alloc(__LOCATION__);
  17. TTypeEnvironment env(alloc);
  18. auto builder = TTupleLiteralBuilder(env);
  19. const auto tuple = builder
  20. .Add(TRuntimeNode(BuildDataLiteral(NUdf::TUnboxedValuePod(true), NUdf::EDataSlot::Bool, env), true))
  21. .Add(TRuntimeNode(BuildDataLiteral(NUdf::TUnboxedValuePod(132), NUdf::EDataSlot::Uint32, env), true))
  22. .Build();
  23. UNIT_ASSERT_EQUAL(tuple->GetType()->GetKind(), TType::EKind::Tuple);
  24. UNIT_ASSERT_VALUES_EQUAL(tuple->GetValuesCount(), 2);
  25. builder.Clear();
  26. UNIT_ASSERT_VALUES_EQUAL(builder.Build()->GetValuesCount(), 0);
  27. }
  28. Y_UNIT_TEST(TestStructTypeBuilder) {
  29. TScopedAlloc alloc(__LOCATION__);
  30. TTypeEnvironment env(alloc);
  31. auto type = TStructTypeBuilder(env)
  32. .Add("Field1", env.GetVoidLazy()->GetGenericType())
  33. .Add("Field2", TDataType::Create(NUdf::TDataType<ui32>::Id, env))
  34. .Build();
  35. UNIT_ASSERT_EQUAL(type->GetKind(), TType::EKind::Struct);
  36. }
  37. Y_UNIT_TEST(TestStructLiteralBuilder) {
  38. TScopedAlloc alloc(__LOCATION__);
  39. TTypeEnvironment env(alloc);
  40. auto structObj = TStructLiteralBuilder(env)
  41. .Add("Field1", TRuntimeNode(env.GetVoidLazy(), true))
  42. .Add("Field2", TRuntimeNode(BuildDataLiteral(NUdf::TUnboxedValuePod((ui32)234), NUdf::EDataSlot::Uint32, env), true))
  43. .Build();
  44. UNIT_ASSERT_EQUAL(structObj->GetType()->GetKind(), TType::EKind::Struct);
  45. auto structObj2 = TStructLiteralBuilder(env)
  46. .Add("Field2", TRuntimeNode(BuildDataLiteral(NUdf::TUnboxedValuePod((ui32)234), NUdf::EDataSlot::Uint32, env), true))
  47. .Add("Field1", TRuntimeNode(env.GetVoidLazy(), true))
  48. .Build();
  49. UNIT_ASSERT(structObj->GetType()->IsSameType(*structObj2->GetType()));
  50. }
  51. Y_UNIT_TEST(TestListLiteralBuilder) {
  52. TScopedAlloc alloc(__LOCATION__);
  53. TTypeEnvironment env(alloc);
  54. auto list = TListLiteralBuilder(env, TDataType::Create(NUdf::TDataType<ui32>::Id, env))
  55. .Add(TRuntimeNode(BuildDataLiteral(NUdf::TUnboxedValuePod((ui32)132), NUdf::EDataSlot::Uint32, env), true))
  56. .Add(TRuntimeNode(BuildDataLiteral(NUdf::TUnboxedValuePod((ui32)234), NUdf::EDataSlot::Uint32, env), true))
  57. .Build();
  58. UNIT_ASSERT_EQUAL(list->GetType()->GetKind(), TType::EKind::List);
  59. UNIT_ASSERT_EQUAL(list->GetItemsCount(), 2);
  60. }
  61. Y_UNIT_TEST(TestCallableTypeBuilder) {
  62. TScopedAlloc alloc(__LOCATION__);
  63. TTypeEnvironment env(alloc);
  64. auto callableType = TCallableTypeBuilder(env, "func", TDataType::Create(NUdf::TDataType<ui32>::Id, env))
  65. .Add(env.GetVoidLazy()->GetType())
  66. .Add(env.GetTypeOfVoidLazy())
  67. .Build();
  68. UNIT_ASSERT_EQUAL(callableType->GetKind(), TType::EKind::Callable);
  69. UNIT_ASSERT_EQUAL(callableType->GetName(), "func");
  70. UNIT_ASSERT_EQUAL(callableType->GetArgumentsCount(), 2);
  71. }
  72. Y_UNIT_TEST(TestCallableTypeBuilderWithNamesAndFlags) {
  73. TScopedAlloc alloc(__LOCATION__);
  74. TTypeEnvironment env(alloc);
  75. auto callableType = TCallableTypeBuilder(env, "func", TDataType::Create(NUdf::TDataType<ui32>::Id, env))
  76. .Add(env.GetVoidLazy()->GetType())
  77. .Add(env.GetTypeOfVoidLazy())
  78. .SetArgumentName("Arg2")
  79. .SetArgumentFlags(NUdf::ICallablePayload::TArgumentFlags::AutoMap)
  80. .Add(env.GetListOfVoidLazy()->GetType())
  81. .SetArgumentName("Arg3")
  82. .Build();
  83. UNIT_ASSERT_EQUAL(callableType->GetKind(), TType::EKind::Callable);
  84. UNIT_ASSERT_EQUAL(callableType->GetName(), "func");
  85. UNIT_ASSERT_EQUAL(callableType->GetArgumentsCount(), 3);
  86. TTypeInfoHelper typeHelper;
  87. NUdf::TCallableTypeInspector callableInspector(typeHelper, callableType);
  88. UNIT_ASSERT(callableInspector);
  89. UNIT_ASSERT_EQUAL(callableInspector.GetArgsCount(), 3);
  90. UNIT_ASSERT_EQUAL(callableInspector.GetArgumentName(0), NUdf::TStringRef());
  91. UNIT_ASSERT_EQUAL(callableInspector.GetArgumentName(1), NUdf::TStringRef("Arg2"));
  92. UNIT_ASSERT_EQUAL(callableInspector.GetArgumentName(2), NUdf::TStringRef("Arg3"));
  93. UNIT_ASSERT_EQUAL(callableInspector.GetArgumentFlags(0), 0);
  94. UNIT_ASSERT_EQUAL(callableInspector.GetArgumentFlags(1), NUdf::ICallablePayload::TArgumentFlags::AutoMap);
  95. UNIT_ASSERT_EQUAL(callableInspector.GetArgumentFlags(2), 0);
  96. }
  97. Y_UNIT_TEST(TestCallableTypeBuilderBadOrderArgNames) {
  98. TScopedAlloc alloc(__LOCATION__);
  99. TTypeEnvironment env(alloc);
  100. UNIT_ASSERT_EXCEPTION(TCallableTypeBuilder(env, "func", TDataType::Create(NUdf::TDataType<ui32>::Id, env))
  101. .Add(env.GetVoidLazy()->GetType())
  102. .Add(env.GetTypeOfVoidLazy())
  103. .SetArgumentName("Arg2")
  104. .SetArgumentFlags(NUdf::ICallablePayload::TArgumentFlags::AutoMap)
  105. .Add(env.GetListOfVoidLazy()->GetType())
  106. .Build(), yexception);
  107. }
  108. Y_UNIT_TEST(TestCallableTypeBuilderDuplicateArgNames) {
  109. TScopedAlloc alloc(__LOCATION__);
  110. TTypeEnvironment env(alloc);
  111. UNIT_ASSERT_EXCEPTION(TCallableTypeBuilder(env, "func", TDataType::Create(NUdf::TDataType<ui32>::Id, env))
  112. .Add(env.GetVoidLazy()->GetType())
  113. .Add(env.GetTypeOfVoidLazy())
  114. .SetArgumentName("Arg2")
  115. .SetArgumentFlags(NUdf::ICallablePayload::TArgumentFlags::AutoMap)
  116. .Add(env.GetListOfVoidLazy()->GetType())
  117. .SetArgumentName("Arg2")
  118. .Build(), yexception);
  119. }
  120. Y_UNIT_TEST(TestDictLiteralBuilder) {
  121. TScopedAlloc alloc(__LOCATION__);
  122. TTypeEnvironment env(alloc);
  123. auto dict = TDictLiteralBuilder(env, TDataType::Create(NUdf::TDataType<ui32>::Id, env),
  124. TDataType::Create(NUdf::TDataType<char*>::Id, env)).Add(
  125. TRuntimeNode(BuildDataLiteral(NUdf::TUnboxedValuePod((ui32)132), NUdf::EDataSlot::Uint32, env), true),
  126. TRuntimeNode(BuildDataLiteral(NUdf::TUnboxedValuePod::Embedded("abc"), NUdf::TDataType<char*>::Id, env), true)
  127. ).Add(
  128. TRuntimeNode(BuildDataLiteral(NUdf::TUnboxedValuePod((ui32)234), NUdf::EDataSlot::Uint32, env), true),
  129. TRuntimeNode(BuildDataLiteral(NUdf::TUnboxedValuePod::Embedded("def"), NUdf::TDataType<char*>::Id, env), true)
  130. ).Build();
  131. UNIT_ASSERT_EQUAL(dict->GetType()->GetKind(), TType::EKind::Dict);
  132. UNIT_ASSERT_EQUAL(dict->GetItemsCount(), 2);
  133. }
  134. Y_UNIT_TEST(TestCallableBuilder) {
  135. TScopedAlloc alloc(__LOCATION__);
  136. TTypeEnvironment env(alloc);
  137. auto callable1 = TCallableBuilder(env, "func1", TDataType::Create(NUdf::TDataType<ui32>::Id, env))
  138. .Add(TRuntimeNode(env.GetEmptyStructLazy(), true))
  139. .Add(TRuntimeNode(env.GetVoidLazy(), true))
  140. .Build();
  141. UNIT_ASSERT_EQUAL(callable1->GetType()->GetKind(), TType::EKind::Callable);
  142. UNIT_ASSERT_EQUAL(callable1->GetInputsCount(), 2);
  143. auto callable2 = TCallableBuilder(env, "func2", TDataType::Create(NUdf::TDataType<ui32>::Id, env))
  144. .Add(TRuntimeNode(callable1, false))
  145. .Add(TRuntimeNode(env.GetVoidLazy(), true))
  146. .Build();
  147. UNIT_ASSERT_EQUAL(callable2->GetType()->GetKind(), TType::EKind::Callable);
  148. UNIT_ASSERT_EQUAL(callable2->GetType()->GetArgumentsCount(), 2);
  149. UNIT_ASSERT_EQUAL(callable2->GetType()->GetArgumentType(0)->GetKind(), TType::EKind::Data);
  150. UNIT_ASSERT_EQUAL(callable2->GetType()->GetArgumentType(1)->GetKind(), TType::EKind::Void);
  151. UNIT_ASSERT_EQUAL(callable2->GetInputsCount(), 2);
  152. }
  153. Y_UNIT_TEST(TestCallableBuilderWithNamesAndFlags) {
  154. TScopedAlloc alloc(__LOCATION__);
  155. TTypeEnvironment env(alloc);
  156. auto callable1 = TCallableBuilder(env, "func1", TDataType::Create(NUdf::TDataType<ui32>::Id, env))
  157. .Add(TRuntimeNode(env.GetEmptyStructLazy(), true))
  158. .Add(TRuntimeNode(env.GetVoidLazy(), true))
  159. .SetArgumentName("Arg2")
  160. .SetArgumentFlags(NUdf::ICallablePayload::TArgumentFlags::AutoMap)
  161. .Add(TRuntimeNode(env.GetEmptyTupleLazy(), true))
  162. .SetArgumentName("Arg3")
  163. .Build();
  164. UNIT_ASSERT_EQUAL(callable1->GetType()->GetKind(), TType::EKind::Callable);
  165. UNIT_ASSERT_EQUAL(callable1->GetInputsCount(), 3);
  166. TTypeInfoHelper typeHelper;
  167. NUdf::TCallableTypeInspector callableInspector(typeHelper, callable1->GetType());
  168. UNIT_ASSERT(callableInspector);
  169. UNIT_ASSERT_EQUAL(callableInspector.GetArgsCount(), 3);
  170. UNIT_ASSERT_EQUAL(callableInspector.GetArgumentName(0), NUdf::TStringRef());
  171. UNIT_ASSERT_EQUAL(callableInspector.GetArgumentName(1), NUdf::TStringRef("Arg2"));
  172. UNIT_ASSERT_EQUAL(callableInspector.GetArgumentName(2), NUdf::TStringRef("Arg3"));
  173. UNIT_ASSERT_EQUAL(callableInspector.GetArgumentFlags(0), 0);
  174. UNIT_ASSERT_EQUAL(callableInspector.GetArgumentFlags(1), NUdf::ICallablePayload::TArgumentFlags::AutoMap);
  175. UNIT_ASSERT_EQUAL(callableInspector.GetArgumentFlags(2), 0);
  176. }
  177. }
  178. }
  179. }