cbo_optimizer_ut.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #include <library/cpp/testing/unittest/registar.h>
  2. #include <library/cpp/testing/hook/hook.h>
  3. #include <yql/essentials/parser/pg_wrapper/interface/optimizer.h>
  4. using namespace NYql;
  5. Y_UNIT_TEST_SUITE(CboOptimizer) {
  6. Y_UNIT_TEST(InputToString) {
  7. IOptimizer::TRel rel1 = {100000, 1000000, {{'a'}}};
  8. IOptimizer::TRel rel2 = {1000000, 9000009, {{'b'}}};
  9. IOptimizer::TRel rel3 = {10000, 9009, {{'c'}}};
  10. IOptimizer::TInput input = {{rel1, rel2, rel3}, {}, {}, {}};
  11. input.EqClasses.emplace_back(IOptimizer::TEq {
  12. {{1, 1}, {2, 1}, {3, 1}}
  13. });
  14. auto str = input.ToString();
  15. TString expected = R"__(Rels: [{rows: 100000,cost: 1000000,vars: [a]},
  16. {rows: 1000000,cost: 9000009,vars: [b]},
  17. {rows: 10000,cost: 9009,vars: [c]}]
  18. EqClasses: [[a,b,c]]
  19. )__";
  20. UNIT_ASSERT_STRINGS_EQUAL(expected, str);
  21. }
  22. Y_UNIT_TEST(OutputToString) {
  23. IOptimizer::TOutput output;
  24. auto str = output.ToString();
  25. TString expected = R"__(Rows: 0.00
  26. TotalCost: 0.00
  27. {
  28. }
  29. )__";
  30. UNIT_ASSERT_STRINGS_EQUAL(expected, str);
  31. }
  32. Y_UNIT_TEST(InputNormalize) {
  33. IOptimizer::TRel rel1 = {100000, 1000000, {{'a'}}};
  34. IOptimizer::TRel rel2 = {1000000, 9000009, {{'b'}}};
  35. IOptimizer::TRel rel3 = {10000, 9009, {{'c'}}};
  36. IOptimizer::TInput input = {{rel1, rel2, rel3}, {}, {}, {}};
  37. input.EqClasses.emplace_back(IOptimizer::TEq {
  38. {{1, 1}, {2, 1}}
  39. });
  40. input.EqClasses.emplace_back(IOptimizer::TEq {
  41. {{2, 1}, {3, 1}}
  42. });
  43. TString expected = R"__(Rels: [{rows: 100000,cost: 1000000,vars: [a]},
  44. {rows: 1000000,cost: 9000009,vars: [b]},
  45. {rows: 10000,cost: 9009,vars: [c]}]
  46. EqClasses: [[a,b],[b,c]]
  47. )__";
  48. UNIT_ASSERT_STRINGS_EQUAL(expected, input.ToString());
  49. input.Normalize();
  50. expected = R"__(Rels: [{rows: 100000,cost: 1000000,vars: [a]},
  51. {rows: 1000000,cost: 9000009,vars: [b]},
  52. {rows: 10000,cost: 9009,vars: [c]}]
  53. EqClasses: [[a,b,c]]
  54. )__";
  55. UNIT_ASSERT_STRINGS_EQUAL(expected, input.ToString());
  56. IOptimizer::TRel rel4 = {10001, 9009, {{'d'}}};
  57. IOptimizer::TInput input2 = {{rel1, rel2, rel3, rel4}, {}, {}, {}};
  58. input2.EqClasses.emplace_back(IOptimizer::TEq {
  59. {{1, 1}, {2, 1}}
  60. });
  61. input2.EqClasses.emplace_back(IOptimizer::TEq {
  62. {{4, 1}, {3, 1}}
  63. });
  64. expected = R"__(Rels: [{rows: 100000,cost: 1000000,vars: [a]},
  65. {rows: 1000000,cost: 9000009,vars: [b]},
  66. {rows: 10000,cost: 9009,vars: [c]},
  67. {rows: 10001,cost: 9009,vars: [d]}]
  68. EqClasses: [[a,b],[d,c]]
  69. )__";
  70. UNIT_ASSERT_STRINGS_EQUAL(expected, input2.ToString());
  71. input2.Normalize();
  72. expected = R"__(Rels: [{rows: 100000,cost: 1000000,vars: [a]},
  73. {rows: 1000000,cost: 9000009,vars: [b]},
  74. {rows: 10000,cost: 9009,vars: [c]},
  75. {rows: 10001,cost: 9009,vars: [d]}]
  76. EqClasses: [[a,b],[c,d]]
  77. )__";
  78. UNIT_ASSERT_STRINGS_EQUAL(expected, input2.ToString());
  79. }
  80. } // Y_UNIT_TEST_SUITE(CboOptimizer)