operation_ut.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. #include <yt/cpp/mapreduce/interface/common_ut.h>
  2. #include <yt/cpp/mapreduce/interface/job_statistics.h>
  3. #include <yt/cpp/mapreduce/interface/operation.h>
  4. #include <yt/cpp/mapreduce/interface/protobuf_table_schema_ut.pb.h>
  5. #include <yt/cpp/mapreduce/tests/yt_unittest_lib/yt_unittest_lib.h>
  6. #include <library/cpp/yson/node/node_io.h>
  7. #include <library/cpp/testing/unittest/registar.h>
  8. using namespace NYT;
  9. using namespace NYT::NUnitTesting;
  10. class TDummyInferenceContext
  11. : public IOperationPreparationContext
  12. {
  13. public:
  14. TDummyInferenceContext(int inputCount, int outputCount)
  15. : InputCount_(inputCount)
  16. , OutputCount_(outputCount)
  17. , InputSchemas_(inputCount)
  18. { }
  19. int GetInputCount() const override
  20. {
  21. return InputCount_;
  22. }
  23. int GetOutputCount() const override
  24. {
  25. return OutputCount_;
  26. }
  27. const TVector<TTableSchema>& GetInputSchemas() const override
  28. {
  29. return InputSchemas_;
  30. }
  31. const TTableSchema& GetInputSchema(int index) const override
  32. {
  33. return InputSchemas_[index];
  34. }
  35. TMaybe<TYPath> GetInputPath(int) const override
  36. {
  37. return Nothing();
  38. }
  39. TMaybe<TYPath> GetOutputPath(int) const override
  40. {
  41. return Nothing();
  42. }
  43. private:
  44. int InputCount_;
  45. int OutputCount_;
  46. TVector<TTableSchema> InputSchemas_;
  47. };
  48. Y_UNIT_TEST_SUITE(PrepareOperation)
  49. {
  50. Y_UNIT_TEST(BasicSchemas)
  51. {
  52. auto firstSchema = TTableSchema()
  53. .AddColumn(TColumnSchema().Name("some_column").Type(EValueType::VT_UINT64));
  54. auto otherSchema = TTableSchema()
  55. .AddColumn(TColumnSchema().Name("other_column").Type(EValueType::VT_BOOLEAN));
  56. auto thirdSchema = TTableSchema()
  57. .AddColumn(TColumnSchema().Name("third_column").Type(EValueType::VT_STRING));
  58. TDummyInferenceContext context(3,7);
  59. TJobOperationPreparer builder(context);
  60. builder
  61. .OutputSchema(1, firstSchema)
  62. .BeginOutputGroup(TVector<int>{2, 5})
  63. .Schema(otherSchema)
  64. .EndOutputGroup()
  65. .BeginOutputGroup(3, 5)
  66. .Schema(thirdSchema)
  67. .EndOutputGroup()
  68. .BeginOutputGroup(TVector<int>{0, 6})
  69. .Schema(thirdSchema)
  70. .EndOutputGroup();
  71. UNIT_ASSERT_EXCEPTION(builder.OutputSchema(1, otherSchema), TApiUsageError);
  72. UNIT_ASSERT_EXCEPTION(builder.BeginOutputGroup(3, 5).Schema(otherSchema), TApiUsageError);
  73. UNIT_ASSERT_EXCEPTION(builder.BeginOutputGroup(TVector<int>{3,6,7}).Schema(otherSchema), TApiUsageError);
  74. builder.Finish();
  75. auto result = builder.GetOutputSchemas();
  76. ASSERT_SERIALIZABLES_EQUAL(result[0], thirdSchema);
  77. ASSERT_SERIALIZABLES_EQUAL(result[1], firstSchema);
  78. ASSERT_SERIALIZABLES_EQUAL(result[2], otherSchema);
  79. ASSERT_SERIALIZABLES_EQUAL(result[3], thirdSchema);
  80. ASSERT_SERIALIZABLES_EQUAL(result[4], thirdSchema);
  81. ASSERT_SERIALIZABLES_EQUAL(result[5], otherSchema);
  82. ASSERT_SERIALIZABLES_EQUAL(result[6], thirdSchema);
  83. }
  84. Y_UNIT_TEST(NoSchema)
  85. {
  86. auto schema = TTableSchema()
  87. .AddColumn(TColumnSchema().Name("some_column").Type(EValueType::VT_UINT64));
  88. TDummyInferenceContext context(3,4);
  89. TJobOperationPreparer builder(context);
  90. builder
  91. .OutputSchema(1, schema)
  92. .NoOutputSchema(0)
  93. .BeginOutputGroup(2, 4)
  94. .Schema(schema)
  95. .EndOutputGroup();
  96. UNIT_ASSERT_EXCEPTION(builder.OutputSchema(0, schema), TApiUsageError);
  97. builder.Finish();
  98. auto result = builder.GetOutputSchemas();
  99. UNIT_ASSERT(result[0].Empty());
  100. ASSERT_SERIALIZABLES_EQUAL(result[1], schema);
  101. ASSERT_SERIALIZABLES_EQUAL(result[2], schema);
  102. ASSERT_SERIALIZABLES_EQUAL(result[3], schema);
  103. }
  104. Y_UNIT_TEST(Descriptions)
  105. {
  106. auto urlRowSchema = TTableSchema()
  107. .AddColumn(TColumnSchema().Name("Host").Type(NTi::Optional(NTi::String())))
  108. .AddColumn(TColumnSchema().Name("Path").Type(NTi::Optional(NTi::String())))
  109. .AddColumn(TColumnSchema().Name("HttpCode").Type(NTi::Optional(NTi::Int32())));
  110. auto urlRowStruct = NTi::Struct({
  111. {"Host", NTi::Optional(NTi::String())},
  112. {"Path", NTi::Optional(NTi::String())},
  113. {"HttpCode", NTi::Optional(NTi::Int32())},
  114. });
  115. auto rowFieldSerializationOptionSchema = TTableSchema()
  116. .AddColumn(TColumnSchema().Name("UrlRow_1").Type(NTi::Optional(urlRowStruct)))
  117. .AddColumn(TColumnSchema().Name("UrlRow_2").Type(NTi::Optional(NTi::String())));
  118. auto rowSerializedRepeatedFieldsSchema = TTableSchema()
  119. .AddColumn(TColumnSchema().Name("Ints").Type(NTi::List(NTi::Int64())))
  120. .AddColumn(TColumnSchema().Name("UrlRows").Type(NTi::List(urlRowStruct)));
  121. TDummyInferenceContext context(5,7);
  122. TJobOperationPreparer builder(context);
  123. builder
  124. .InputDescription<TUrlRow>(0)
  125. .BeginInputGroup(2, 3)
  126. .Description<TUrlRow>()
  127. .EndInputGroup()
  128. .BeginInputGroup(TVector<int>{1, 4})
  129. .Description<TRowSerializedRepeatedFields>()
  130. .EndInputGroup()
  131. .InputDescription<TUrlRow>(3);
  132. UNIT_ASSERT_EXCEPTION(builder.InputDescription<TUrlRow>(0), TApiUsageError);
  133. builder
  134. .OutputDescription<TUrlRow>(0, false)
  135. .OutputDescription<TRowFieldSerializationOption>(1)
  136. .BeginOutputGroup(2, 4)
  137. .Description<TUrlRow>()
  138. .EndOutputGroup()
  139. .BeginOutputGroup(TVector<int>{4,6})
  140. .Description<TRowSerializedRepeatedFields>()
  141. .EndOutputGroup()
  142. .OutputDescription<TUrlRow>(5, false);
  143. UNIT_ASSERT_EXCEPTION(builder.OutputDescription<TUrlRow>(0), TApiUsageError);
  144. UNIT_ASSERT_NO_EXCEPTION(builder.OutputSchema(0, urlRowSchema));
  145. UNIT_ASSERT_NO_EXCEPTION(builder.OutputSchema(5, urlRowSchema));
  146. UNIT_ASSERT_EXCEPTION(builder.OutputSchema(1, urlRowSchema), TApiUsageError);
  147. builder.Finish();
  148. auto result = builder.GetOutputSchemas();
  149. ASSERT_SERIALIZABLES_EQUAL(result[0], urlRowSchema);
  150. ASSERT_SERIALIZABLES_EQUAL(result[1], rowFieldSerializationOptionSchema);
  151. ASSERT_SERIALIZABLES_EQUAL(result[2], urlRowSchema);
  152. ASSERT_SERIALIZABLES_EQUAL(result[3], urlRowSchema);
  153. ASSERT_SERIALIZABLES_EQUAL(result[4], rowSerializedRepeatedFieldsSchema);
  154. ASSERT_SERIALIZABLES_EQUAL(result[5], urlRowSchema);
  155. ASSERT_SERIALIZABLES_EQUAL(result[6], rowSerializedRepeatedFieldsSchema);
  156. auto expectedInputDescriptions = TVector<TMaybe<TTableStructure>>{
  157. {TProtobufTableStructure{TUrlRow::descriptor()}},
  158. {TProtobufTableStructure{TRowSerializedRepeatedFields::descriptor()}},
  159. {TProtobufTableStructure{TUrlRow::descriptor()}},
  160. {TProtobufTableStructure{TUrlRow::descriptor()}},
  161. {TProtobufTableStructure{TRowSerializedRepeatedFields::descriptor()}},
  162. };
  163. UNIT_ASSERT_EQUAL(expectedInputDescriptions, builder.GetInputDescriptions());
  164. auto expectedOutputDescriptions = TVector<TMaybe<TTableStructure>>{
  165. {TProtobufTableStructure{TUrlRow::descriptor()}},
  166. {TProtobufTableStructure{TRowFieldSerializationOption::descriptor()}},
  167. {TProtobufTableStructure{TUrlRow::descriptor()}},
  168. {TProtobufTableStructure{TUrlRow::descriptor()}},
  169. {TProtobufTableStructure{TRowSerializedRepeatedFields::descriptor()}},
  170. {TProtobufTableStructure{TUrlRow::descriptor()}},
  171. {TProtobufTableStructure{TRowSerializedRepeatedFields::descriptor()}},
  172. };
  173. UNIT_ASSERT_EQUAL(expectedOutputDescriptions, builder.GetOutputDescriptions());
  174. }
  175. Y_UNIT_TEST(InputColumns)
  176. {
  177. TDummyInferenceContext context(5, 1);
  178. TJobOperationPreparer builder(context);
  179. builder
  180. .InputColumnFilter(2, {"a", "b"})
  181. .BeginInputGroup(0, 2)
  182. .ColumnFilter({"b", "c"})
  183. .ColumnRenaming({{"b", "B"}, {"c", "C"}})
  184. .EndInputGroup()
  185. .InputColumnRenaming(3, {{"a", "AAA"}})
  186. .NoOutputSchema(0);
  187. builder.Finish();
  188. auto expectedRenamings = TVector<THashMap<TString, TString>>{
  189. {{"b", "B"}, {"c", "C"}},
  190. {{"b", "B"}, {"c", "C"}},
  191. {},
  192. {{"a", "AAA"}},
  193. {},
  194. };
  195. UNIT_ASSERT_EQUAL(builder.GetInputColumnRenamings(), expectedRenamings);
  196. auto expectedFilters = TVector<TMaybe<TVector<TString>>>{
  197. {{"b", "c"}},
  198. {{"b", "c"}},
  199. {{"a", "b"}},
  200. {},
  201. {},
  202. };
  203. UNIT_ASSERT_EQUAL(builder.GetInputColumnFilters(), expectedFilters);
  204. }
  205. Y_UNIT_TEST(Bug_r7349102)
  206. {
  207. auto firstSchema = TTableSchema()
  208. .AddColumn(TColumnSchema().Name("some_column").Type(EValueType::VT_UINT64));
  209. auto otherSchema = TTableSchema()
  210. .AddColumn(TColumnSchema().Name("other_column").Type(EValueType::VT_BOOLEAN));
  211. auto thirdSchema = TTableSchema()
  212. .AddColumn(TColumnSchema().Name("third_column").Type(EValueType::VT_STRING));
  213. TDummyInferenceContext context(3,1);
  214. TJobOperationPreparer builder(context);
  215. builder
  216. .InputDescription<TUrlRow>(0)
  217. .InputDescription<TUrlRow>(1)
  218. .InputDescription<TUrlRow>(2)
  219. .OutputDescription<TUrlRow>(0);
  220. builder.Finish();
  221. }
  222. } // Y_UNIT_TEST_SUITE(SchemaInference)