optimizer.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #pragma once
  2. #include <yql/essentials/core/cbo/cbo_optimizer_new.h>
  3. #include <functional>
  4. namespace NYql {
  5. struct TExprContext;
  6. struct IOptimizer {
  7. using TVarId = std::tuple<int, int>;
  8. struct TVar {
  9. char Name = 0; // debug name: 'a', 'b', 'c', ...
  10. };
  11. struct TRel {
  12. double Rows = 0;
  13. double TotalCost = 0;
  14. std::vector<TVar> TargetVars;
  15. };
  16. struct TEq {
  17. std::vector<TVarId> Vars;
  18. };
  19. struct TInput {
  20. std::vector<TRel> Rels;
  21. std::vector<TEq> EqClasses;
  22. std::vector<TEq> Left;
  23. std::vector<TEq> Right;
  24. TString ToString() const;
  25. void Normalize();
  26. };
  27. enum class EJoinType {
  28. Unknown,
  29. Inner,
  30. Left,
  31. Right,
  32. };
  33. enum class EJoinStrategy {
  34. Unknown,
  35. Hash,
  36. Loop
  37. };
  38. struct TJoinNode {
  39. EJoinType Mode = EJoinType::Unknown;
  40. EJoinStrategy Strategy = EJoinStrategy::Unknown;
  41. // only a = b && c = d ... supported yet
  42. std::vector<TVarId> LeftVars = {};
  43. std::vector<TVarId> RightVars = {};
  44. std::vector<int> Rels = {};
  45. int Outer = -1; // index in Nodes
  46. int Inner = -1; // index in Nodes
  47. };
  48. struct TOutput {
  49. std::vector<TJoinNode> Nodes;
  50. TInput* Input = nullptr;
  51. double Rows = 0;
  52. double TotalCost = 0;
  53. TString ToString(bool printCost = true) const;
  54. };
  55. virtual ~IOptimizer() = default;
  56. virtual TOutput JoinSearch() = 0;
  57. };
  58. IOptimizer* MakePgOptimizerInternal(const IOptimizer::TInput& input, const std::function<void(const TString&)>& log = {});
  59. IOptimizerNew* MakePgOptimizerNew(IProviderContext& pctx, TExprContext& ctx, const std::function<void(const TString&)>& log = {});
  60. } // namespace NYql