yql_optimize.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #pragma once
  2. #include <yql/essentials/core/yql_graph_transformer.h>
  3. #include <yql/essentials/core/yql_expr_optimize.h>
  4. #include <yql/essentials/core/expr_nodes/yql_expr_nodes.h>
  5. #include <yql/essentials/ast/yql_expr.h>
  6. #include <yql/essentials/utils/log/log_component.h>
  7. #include <util/generic/vector.h>
  8. #include <util/generic/strbuf.h>
  9. #include <util/generic/ptr.h>
  10. #include <util/generic/set.h>
  11. #include <util/generic/string.h>
  12. #include <functional>
  13. #include <initializer_list>
  14. namespace NYql {
  15. class TOptimizeTransformerBase: public TSyncTransformerBase {
  16. public:
  17. using TGetParents = std::function<const TParentsMap*()>;
  18. using THandler = std::function<NNodes::TMaybeNode<NNodes::TExprBase>(NNodes::TExprBase, TExprContext&, IOptimizationContext&, const TGetParents&)>;
  19. using TFilter = std::function<bool(const TExprNode*)>;
  20. TOptimizeTransformerBase(TTypeAnnotationContext* types, NLog::EComponent logComponent, const TSet<TString>& disabledOpts);
  21. TStatus DoTransform(TExprNode::TPtr input, TExprNode::TPtr& output, TExprContext& ctx) override;
  22. void Rewind() override;
  23. protected:
  24. class TIgnoreOptimizationContext;
  25. class TRemapOptimizationContext;
  26. static TFilter Any();
  27. static TFilter Names(std::initializer_list<TStringBuf> names);
  28. static TFilter Or(std::initializer_list<TFilter> filters);
  29. void AddHandler(size_t step, TFilter filter, TStringBuf optName, THandler handler);
  30. void SetGlobal(size_t step);
  31. template <class TDerived>
  32. THandler Hndl(NNodes::TMaybeNode<NNodes::TExprBase>(TDerived::* handler)(NNodes::TExprBase, TExprContext&, const TGetParents&)) {
  33. return [this, handler] (NNodes::TExprBase node, TExprContext& ctx, IOptimizationContext& /*optCtx*/, const TGetParents& parents) {
  34. return (static_cast<TDerived*>(this)->*handler)(node, ctx, parents);
  35. };
  36. }
  37. template <class TDerived>
  38. THandler Hndl(NNodes::TMaybeNode<NNodes::TExprBase>(TDerived::* handler)(NNodes::TExprBase, TExprContext&, const TGetParents&) const) const {
  39. return [this, handler] (NNodes::TExprBase node, TExprContext& ctx, IOptimizationContext& /*optCtx*/, const TGetParents& parents) {
  40. return (static_cast<const TDerived*>(this)->*handler)(node, ctx, parents);
  41. };
  42. }
  43. template <class TDerived>
  44. THandler Hndl(NNodes::TMaybeNode<NNodes::TExprBase>(TDerived::* handler)(NNodes::TExprBase, TExprContext&, IOptimizationContext&, const TGetParents&)) {
  45. return [this, handler] (NNodes::TExprBase node, TExprContext& ctx, IOptimizationContext& optCtx, const TGetParents& parents) {
  46. return (static_cast<TDerived*>(this)->*handler)(node, ctx, optCtx, parents);
  47. };
  48. }
  49. template <class TDerived>
  50. THandler Hndl(NNodes::TMaybeNode<NNodes::TExprBase>(TDerived::* handler)(NNodes::TExprBase, TExprContext&, IOptimizationContext&, const TGetParents&) const) const {
  51. return [this, handler] (NNodes::TExprBase node, TExprContext& ctx, IOptimizationContext& optCtx, const TGetParents& parents) {
  52. return (static_cast<const TDerived*>(this)->*handler)(node, ctx, optCtx, parents);
  53. };
  54. }
  55. template <class TDerived>
  56. THandler Hndl(NNodes::TMaybeNode<NNodes::TExprBase>(TDerived::* handler)(NNodes::TExprBase, TExprContext&, IOptimizationContext&)) {
  57. return [this, handler] (NNodes::TExprBase node, TExprContext& ctx, IOptimizationContext& optCtx, const TGetParents& /*parents*/) {
  58. return (static_cast<TDerived*>(this)->*handler)(node, ctx, optCtx);
  59. };
  60. }
  61. template <class TDerived>
  62. THandler Hndl(NNodes::TMaybeNode<NNodes::TExprBase>(TDerived::* handler)(NNodes::TExprBase, TExprContext&, IOptimizationContext&) const) const {
  63. return [this, handler] (NNodes::TExprBase node, TExprContext& ctx, IOptimizationContext& optCtx, const TGetParents& /*parents*/) {
  64. return (static_cast<const TDerived*>(this)->*handler)(node, ctx, optCtx);
  65. };
  66. }
  67. template <class TDerived>
  68. THandler Hndl(NNodes::TMaybeNode<NNodes::TExprBase>(TDerived::* handler)(NNodes::TExprBase, TExprContext&)) {
  69. return [this, handler] (NNodes::TExprBase node, TExprContext& ctx, IOptimizationContext& /*optCtx*/, const TGetParents& /*parents*/) {
  70. return (static_cast<TDerived*>(this)->*handler)(node, ctx);
  71. };
  72. }
  73. template <class TDerived>
  74. THandler Hndl(NNodes::TMaybeNode<NNodes::TExprBase>(TDerived::* handler)(NNodes::TExprBase, TExprContext&) const) const {
  75. return [this, handler] (NNodes::TExprBase node, TExprContext& ctx, IOptimizationContext& /*optCtx*/, const TGetParents& /*parents*/) {
  76. return (static_cast<const TDerived*>(this)->*handler)(node, ctx);
  77. };
  78. }
  79. protected:
  80. struct TOptInfo {
  81. TString OptName;
  82. TFilter Filter;
  83. THandler Handler;
  84. };
  85. struct TStep {
  86. TProcessedNodesSet ProcessedNodes;
  87. TVector<TOptInfo> Optimizers;
  88. bool Global = false;
  89. };
  90. TTypeAnnotationContext* Types;
  91. const NLog::EComponent LogComponent;
  92. TSet<TString> DisabledOpts;
  93. TVector<TStep> Steps;
  94. };
  95. } // NYql