list_builtin.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. #pragma once
  2. #include "node.h"
  3. #include "context.h"
  4. #include <yql/essentials/ast/yql_type_string.h>
  5. #include <library/cpp/charset/ci_string.h>
  6. #include <util/string/builder.h>
  7. #include <util/string/cast.h>
  8. #include <util/string/util.h>
  9. using namespace NYql;
  10. namespace NSQLTranslationV0 {
  11. class TListBuiltin: public TCallNode {
  12. public:
  13. TListBuiltin(TPosition pos,
  14. const TString& opName,
  15. const TVector<TNodePtr>& args)
  16. : TCallNode(pos, opName, args.size(), args.size(), args)
  17. , OpName(opName)
  18. , Args(args)
  19. {}
  20. bool DoInit(TContext& ctx, ISource* src) override = 0;
  21. TAstNode* Translate(TContext& ctx) const override;
  22. protected:
  23. const TString OpName;
  24. TVector<TNodePtr> Args;
  25. TNodePtr Node;
  26. inline TNodePtr GetIdentityLambda();
  27. inline TNodePtr SkipEmpty(TNodePtr arg);
  28. void DoUpdateState() const override {
  29. State.Set(ENodeState::Aggregated, Args[0]->IsAggregated());
  30. }
  31. };
  32. class TListSortBuiltin final: public TListBuiltin {
  33. public:
  34. TListSortBuiltin(TPosition pos, const TVector<TNodePtr>& args, bool asc)
  35. : TListBuiltin(pos, "Sort", args)
  36. , Asc(asc)
  37. {}
  38. bool DoInit(TContext& ctx, ISource* src) override;
  39. TNodePtr DoClone() const final {
  40. return new TListSortBuiltin(Pos, CloneContainer(Args), Asc);
  41. }
  42. private:
  43. bool Asc;
  44. };
  45. class TListExtractBuiltin final: public TListBuiltin {
  46. public:
  47. TListExtractBuiltin(TPosition pos, const TVector<TNodePtr>& args)
  48. : TListBuiltin(pos, "OrderedExtract", args)
  49. {}
  50. bool DoInit(TContext& ctx, ISource* src) override;
  51. TNodePtr DoClone() const final {
  52. return new TListExtractBuiltin(Pos, CloneContainer(Args));
  53. }
  54. };
  55. class TListProcessBuiltin: public TListBuiltin {
  56. protected:
  57. TListProcessBuiltin(TPosition pos,
  58. const TString& opName,
  59. const TVector<TNodePtr>& args)
  60. : TListBuiltin(pos, opName, args)
  61. , OpLiteral(nullptr)
  62. {}
  63. bool CheckArgs(TContext& ctx, ISource* src);
  64. TNodePtr PrepareResult();
  65. const TString* OpLiteral;
  66. };
  67. class TListMapBuiltin final: public TListProcessBuiltin {
  68. public:
  69. TListMapBuiltin(TPosition pos,
  70. const TVector<TNodePtr>& args,
  71. bool flat)
  72. : TListProcessBuiltin(pos, flat ? "OrderedFlatMap" : "OrderedMap", args)
  73. , Flat(flat)
  74. {}
  75. bool DoInit(TContext& ctx, ISource* src) override;
  76. TNodePtr DoClone() const final {
  77. return new TListMapBuiltin(Pos, CloneContainer(Args), Flat);
  78. }
  79. protected:
  80. virtual TNodePtr GetMapLambda();
  81. private:
  82. bool Flat;
  83. };
  84. class TListFilterBuiltin final: public TListProcessBuiltin {
  85. public:
  86. TListFilterBuiltin(TPosition pos,
  87. const TVector<TNodePtr>& args)
  88. : TListProcessBuiltin(pos, "OrderedFilter", args)
  89. {}
  90. bool DoInit(TContext& ctx, ISource* src) override;
  91. TNodePtr DoClone() const final {
  92. return new TListFilterBuiltin(Pos, CloneContainer(Args));
  93. }
  94. protected:
  95. virtual TNodePtr GetFilterLambda();
  96. };
  97. class TListFoldBuiltin: public TListBuiltin {
  98. public:
  99. TListFoldBuiltin(TPosition pos,
  100. const TString& opName,
  101. const TString& stateType,
  102. const TString& stateValue,
  103. const ui32 argCount,
  104. const TVector<TNodePtr>& args)
  105. : TListBuiltin(pos, opName, args)
  106. , StateType(stateType)
  107. , StateValue(stateValue)
  108. , ArgCount(argCount)
  109. {
  110. }
  111. bool DoInit(TContext& ctx, ISource* src) override;
  112. protected:
  113. const TString StateType;
  114. const TString StateValue;
  115. const ui32 ArgCount;
  116. virtual TNodePtr GetInitialState();
  117. virtual TNodePtr GetUpdateLambda();
  118. };
  119. class TListFoldBuiltinImpl final: public TListFoldBuiltin {
  120. public:
  121. TListFoldBuiltinImpl(TPosition pos, const TString& opName, const TString& stateType, const TString& stateValue,
  122. const ui32 argCount, const TVector<TNodePtr>& args)
  123. : TListFoldBuiltin(pos, opName, stateType, stateValue, argCount, args)
  124. {}
  125. TNodePtr DoClone() const final {
  126. return new TListFoldBuiltinImpl(Pos, OpName, StateType, StateValue, ArgCount, CloneContainer(Args));
  127. }
  128. };
  129. class TListCountBuiltin final: public TListFoldBuiltin {
  130. public:
  131. TListCountBuiltin(TPosition pos, const TVector<TNodePtr>& args)
  132. : TListFoldBuiltin(pos, "Inc", "Uint64", "0", 1, args)
  133. {}
  134. TNodePtr DoClone() const final {
  135. return new TListCountBuiltin(Pos, CloneContainer(Args));
  136. }
  137. private:
  138. virtual TNodePtr GetUpdateLambda();
  139. };
  140. class TListAvgBuiltin final: public TListFoldBuiltin {
  141. public:
  142. TListAvgBuiltin(TPosition pos, const TVector<TNodePtr>& args)
  143. : TListFoldBuiltin(pos, "Avg", "", "", 1, args)
  144. {
  145. }
  146. TNodePtr DoClone() const final {
  147. return new TListAvgBuiltin(Pos, CloneContainer(Args));
  148. }
  149. private:
  150. bool DoInit(TContext& ctx, ISource* src) override;
  151. TNodePtr GetInitialState() override;
  152. TNodePtr GetUpdateLambda() override;
  153. };
  154. class TListHasBuiltin final: public TListFoldBuiltin {
  155. public:
  156. TListHasBuiltin(TPosition pos, const TVector<TNodePtr>& args)
  157. : TListFoldBuiltin(pos, "==", "Bool", "false", 2, args)
  158. {
  159. }
  160. TNodePtr DoClone() const final {
  161. return new TListHasBuiltin(Pos, CloneContainer(Args));
  162. }
  163. private:
  164. TNodePtr GetUpdateLambda() override;
  165. void DoUpdateState() const override {
  166. bool isAggregated = true;
  167. for (const auto& arg: Args) {
  168. if (!arg->IsAggregated()) {
  169. isAggregated = false;
  170. break;
  171. }
  172. }
  173. State.Set(ENodeState::Aggregated, isAggregated);
  174. }
  175. };
  176. class TListFold1Builtin final: public TListBuiltin {
  177. public:
  178. TListFold1Builtin(TPosition pos,
  179. const TString& opName,
  180. const TVector<TNodePtr>& args)
  181. : TListBuiltin(pos, opName, args)
  182. {
  183. }
  184. bool DoInit(TContext& ctx, ISource* src) override;
  185. TNodePtr DoClone() const final {
  186. return new TListFold1Builtin(Pos, OpName, CloneContainer(Args));
  187. }
  188. protected:
  189. virtual TNodePtr GetInitLambda();
  190. virtual TNodePtr GetUpdateLambda();
  191. };
  192. class TListUniqBuiltin final: public TListBuiltin {
  193. public:
  194. TListUniqBuiltin(TPosition pos,
  195. const TVector<TNodePtr>& args)
  196. : TListBuiltin(pos, "ListUniq", args)
  197. {}
  198. bool DoInit(TContext& ctx, ISource* src) override;
  199. TNodePtr DoClone() const final {
  200. return new TListUniqBuiltin(Pos, CloneContainer(Args));
  201. }
  202. };
  203. class TListCreateBuiltin final: public TListBuiltin {
  204. public:
  205. TListCreateBuiltin(TPosition pos,
  206. const TVector<TNodePtr>& args)
  207. : TListBuiltin(pos, "ListCreate", args)
  208. {}
  209. bool DoInit(TContext& ctx, ISource* src) override;
  210. TNodePtr DoClone() const final {
  211. return new TListCreateBuiltin(Pos, CloneContainer(Args));
  212. }
  213. };
  214. class TDictCreateBuiltin final: public TListBuiltin {
  215. public:
  216. TDictCreateBuiltin(TPosition pos,
  217. const TVector<TNodePtr>& args)
  218. : TListBuiltin(pos, "DictCreate", args)
  219. {}
  220. bool DoInit(TContext& ctx, ISource* src) override;
  221. TNodePtr DoClone() const final {
  222. return new TDictCreateBuiltin(Pos, CloneContainer(Args));
  223. }
  224. };
  225. } // namespace NSQLTranslationV0