yql_co.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #pragma once
  2. #include <yql/essentials/core/yql_type_annotation.h>
  3. #include <yql/essentials/core/expr_nodes/yql_expr_nodes.h>
  4. #include <string_view>
  5. namespace NYql {
  6. struct TOptimizeContext {
  7. TTypeAnnotationContext* Types = nullptr;
  8. TParentsMap* ParentsMap = nullptr;
  9. const TExprNode* GetParentIfSingle(const TExprNode& node) const {
  10. YQL_ENSURE(ParentsMap);
  11. const auto it = ParentsMap->find(&node);
  12. YQL_ENSURE(it != ParentsMap->cend());
  13. auto& parents = it->second;
  14. YQL_ENSURE(!parents.empty());
  15. if (parents.size() > 1) {
  16. return nullptr;
  17. }
  18. size_t usageCount = 0;
  19. for (const auto& child : (*parents.cbegin())->ChildrenList()) {
  20. if (child.Get() == &node && ++usageCount > 1) {
  21. return nullptr;
  22. }
  23. }
  24. YQL_ENSURE(usageCount == 1);
  25. return *parents.cbegin();
  26. }
  27. bool IsSingleUsage(const TExprNode& node) const {
  28. return bool(GetParentIfSingle(node));
  29. }
  30. bool IsSingleUsage(const NNodes::TExprBase& node) const {
  31. return IsSingleUsage(node.Ref());
  32. }
  33. bool HasParent(const TExprNode& node) const {
  34. YQL_ENSURE(ParentsMap);
  35. return ParentsMap->contains(&node);
  36. }
  37. bool IsPersistentNode(const TExprNode& node) const {
  38. if (Types) {
  39. for (auto& source: Types->DataSources) {
  40. if (source->IsPersistent(node)) {
  41. return true;
  42. }
  43. }
  44. for (auto& sink: Types->DataSinks) {
  45. if (sink->IsPersistent(node)) {
  46. return true;
  47. }
  48. }
  49. }
  50. return false;
  51. }
  52. bool IsPersistentNode(const NNodes::TExprBase& node) const {
  53. return IsPersistentNode(node.Ref());
  54. }
  55. };
  56. using TCallableOptimizerExt = std::function<TExprNode::TPtr (const TExprNode::TPtr&, TExprContext&, TOptimizeContext&)>;
  57. using TCallableOptimizerMap = std::unordered_map<std::string_view, TCallableOptimizerExt>;
  58. using TFinalizingOptimizerExt = std::function<bool (const TExprNode::TPtr&, TNodeOnNodeOwnedMap&, TExprContext&, TOptimizeContext&)>;
  59. using TFinalizingOptimizerMap = std::unordered_map<std::string_view, TFinalizingOptimizerExt>;
  60. struct TCoCallableRules {
  61. enum {
  62. SIMPLE_STEP_1,
  63. SIMPLE_STEP_2,
  64. SIMPLE_STEP_3,
  65. SIMPLE_STEPS
  66. };
  67. enum {
  68. FLOW_STEP_1,
  69. FLOW_STEP_2,
  70. FLOW_STEPS
  71. };
  72. // rules that don't make a flow fork - e.g. False || x -> x
  73. TCallableOptimizerMap SimpleCallables[SIMPLE_STEPS];
  74. // rules that make a flow fork - Join pushdown if Join has multiple usage
  75. TCallableOptimizerMap FlowCallables[FLOW_STEPS];
  76. TFinalizingOptimizerMap Finalizers;
  77. // rules to be applied before execution
  78. TCallableOptimizerMap FinalCallables;
  79. TCoCallableRules();
  80. static const TCoCallableRules& Instance();
  81. };
  82. void RegisterCoSimpleCallables1(TCallableOptimizerMap& map);
  83. void RegisterCoSimpleCallables2(TCallableOptimizerMap& map);
  84. void RegisterCoSimpleCallables3(TCallableOptimizerMap& map);
  85. void RegisterCoFlowCallables1(TCallableOptimizerMap& map);
  86. void RegisterCoFlowCallables2(TCallableOptimizerMap& map);
  87. void RegisterCoFinalizers(TFinalizingOptimizerMap& map);
  88. void RegisterCoFinalCallables(TCallableOptimizerMap& map);
  89. }