yql_expr.h 89 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931
  1. #pragma once
  2. #include "yql_ast.h"
  3. #include "yql_expr_types.h"
  4. #include "yql_type_string.h"
  5. #include "yql_expr_builder.h"
  6. #include "yql_gc_nodes.h"
  7. #include "yql_constraint.h"
  8. #include "yql_pos_handle.h"
  9. #include <yql/essentials/core/url_lister/interface/url_lister_manager.h>
  10. #include <yql/essentials/utils/yql_panic.h>
  11. #include <yql/essentials/public/issue/yql_issue_manager.h>
  12. #include <yql/essentials/public/udf/udf_data_type.h>
  13. #include <library/cpp/yson/node/node.h>
  14. #include <library/cpp/yson/writer.h>
  15. #include <library/cpp/string_utils/levenshtein_diff/levenshtein_diff.h>
  16. #include <library/cpp/enumbitset/enumbitset.h>
  17. #include <library/cpp/containers/stack_vector/stack_vec.h>
  18. #include <library/cpp/deprecated/enum_codegen/enum_codegen.h>
  19. #include <util/string/ascii.h>
  20. #include <util/string/builder.h>
  21. #include <util/generic/array_ref.h>
  22. #include <util/generic/deque.h>
  23. #include <util/generic/cast.h>
  24. #include <util/generic/hash.h>
  25. #include <util/generic/maybe.h>
  26. #include <util/generic/set.h>
  27. #include <util/generic/bt_exception.h>
  28. #include <util/generic/algorithm.h>
  29. #include <util/digest/murmur.h>
  30. #include <algorithm>
  31. #include <unordered_set>
  32. #include <unordered_map>
  33. #include <span>
  34. #include <stack>
  35. //#define YQL_CHECK_NODES_CONSISTENCY
  36. #ifdef YQL_CHECK_NODES_CONSISTENCY
  37. #define ENSURE_NOT_DELETED \
  38. YQL_ENSURE(!Dead(), "Access to dead node # " << UniqueId_ << ": " << Type_ << " '" << ContentUnchecked() << "'");
  39. #define ENSURE_NOT_FROZEN \
  40. YQL_ENSURE(!Frozen(), "Change in frozen node # " << UniqueId_ << ": " << Type_ << " '" << ContentUnchecked() << "'");
  41. #define ENSURE_NOT_FROZEN_CTX \
  42. YQL_ENSURE(!Frozen, "Change in frozen expr context.");
  43. #else
  44. #define ENSURE_NOT_DELETED Y_DEBUG_ABORT_UNLESS(!Dead(), "Access to dead node # %lu: %d '%s'", UniqueId_, (int)Type_, TString(ContentUnchecked()).data());
  45. #define ENSURE_NOT_FROZEN Y_DEBUG_ABORT_UNLESS(!Frozen());
  46. #define ENSURE_NOT_FROZEN_CTX Y_DEBUG_ABORT_UNLESS(!Frozen);
  47. #endif
  48. namespace NYql {
  49. using NUdf::EDataSlot;
  50. class TUnitExprType;
  51. class TMultiExprType;
  52. class TTupleExprType;
  53. class TStructExprType;
  54. class TItemExprType;
  55. class TListExprType;
  56. class TStreamExprType;
  57. class TDataExprType;
  58. class TPgExprType;
  59. class TWorldExprType;
  60. class TOptionalExprType;
  61. class TCallableExprType;
  62. class TResourceExprType;
  63. class TTypeExprType;
  64. class TDictExprType;
  65. class TVoidExprType;
  66. class TNullExprType;
  67. class TGenericExprType;
  68. class TTaggedExprType;
  69. class TErrorExprType;
  70. class TVariantExprType;
  71. class TStreamExprType;
  72. class TFlowExprType;
  73. class TEmptyListExprType;
  74. class TEmptyDictExprType;
  75. class TBlockExprType;
  76. class TScalarExprType;
  77. const size_t DefaultMistypeDistance = 3;
  78. const TString YqlVirtualPrefix = "_yql_virtual_";
  79. extern const TStringBuf ZeroString;
  80. struct TTypeAnnotationVisitor {
  81. virtual ~TTypeAnnotationVisitor() = default;
  82. virtual void Visit(const TUnitExprType& type) = 0;
  83. virtual void Visit(const TMultiExprType& type) = 0;
  84. virtual void Visit(const TTupleExprType& type) = 0;
  85. virtual void Visit(const TStructExprType& type) = 0;
  86. virtual void Visit(const TItemExprType& type) = 0;
  87. virtual void Visit(const TListExprType& type) = 0;
  88. virtual void Visit(const TStreamExprType& type) = 0;
  89. virtual void Visit(const TFlowExprType& type) = 0;
  90. virtual void Visit(const TDataExprType& type) = 0;
  91. virtual void Visit(const TPgExprType& type) = 0;
  92. virtual void Visit(const TWorldExprType& type) = 0;
  93. virtual void Visit(const TOptionalExprType& type) = 0;
  94. virtual void Visit(const TCallableExprType& type) = 0;
  95. virtual void Visit(const TResourceExprType& type) = 0;
  96. virtual void Visit(const TTypeExprType& type) = 0;
  97. virtual void Visit(const TDictExprType& type) = 0;
  98. virtual void Visit(const TVoidExprType& type) = 0;
  99. virtual void Visit(const TNullExprType& type) = 0;
  100. virtual void Visit(const TGenericExprType& type) = 0;
  101. virtual void Visit(const TTaggedExprType& type) = 0;
  102. virtual void Visit(const TErrorExprType& type) = 0;
  103. virtual void Visit(const TVariantExprType& type) = 0;
  104. virtual void Visit(const TEmptyListExprType& type) = 0;
  105. virtual void Visit(const TEmptyDictExprType& type) = 0;
  106. virtual void Visit(const TBlockExprType& type) = 0;
  107. virtual void Visit(const TScalarExprType& type) = 0;
  108. };
  109. struct TDefaultTypeAnnotationVisitor : public TTypeAnnotationVisitor {
  110. void Visit(const TUnitExprType& type) override;
  111. void Visit(const TMultiExprType& type) override;
  112. void Visit(const TTupleExprType& type) override;
  113. void Visit(const TStructExprType& type) override;
  114. void Visit(const TItemExprType& type) override;
  115. void Visit(const TListExprType& type) override;
  116. void Visit(const TStreamExprType& type) override;
  117. void Visit(const TFlowExprType& type) override;
  118. void Visit(const TDataExprType& type) override;
  119. void Visit(const TPgExprType& type) override;
  120. void Visit(const TWorldExprType& type) override;
  121. void Visit(const TOptionalExprType& type) override;
  122. void Visit(const TCallableExprType& type) override;
  123. void Visit(const TResourceExprType& type) override;
  124. void Visit(const TTypeExprType& type) override;
  125. void Visit(const TDictExprType& type) override;
  126. void Visit(const TVoidExprType& type) override;
  127. void Visit(const TNullExprType& type) override;
  128. void Visit(const TGenericExprType& type) override;
  129. void Visit(const TTaggedExprType& type) override;
  130. void Visit(const TErrorExprType& type) override;
  131. void Visit(const TVariantExprType& type) override;
  132. void Visit(const TEmptyListExprType& type) override;
  133. void Visit(const TEmptyDictExprType& type) override;
  134. void Visit(const TBlockExprType& type) override;
  135. void Visit(const TScalarExprType& type) override;
  136. };
  137. class TErrorTypeVisitor : public TDefaultTypeAnnotationVisitor
  138. {
  139. public:
  140. TErrorTypeVisitor(TExprContext& ctx);
  141. void Visit(const TErrorExprType& type) override;
  142. bool HasErrors() const;
  143. private:
  144. TExprContext& Ctx_;
  145. bool HasErrors_ = false;
  146. };
  147. enum ETypeAnnotationFlags : ui32 {
  148. TypeNonComposable = 0x01,
  149. TypeNonPersistable = 0x02,
  150. TypeNonComputable = 0x04,
  151. TypeNonInspectable = 0x08,
  152. TypeNonHashable = 0x10,
  153. TypeNonEquatable = 0x20,
  154. TypeNonComparable = 0x40,
  155. TypeHasNull = 0x80,
  156. TypeHasOptional = 0x100,
  157. TypeHasManyValues = 0x200,
  158. TypeHasBareYson = 0x400,
  159. TypeHasNestedOptional = 0x800,
  160. TypeNonPresortable = 0x1000,
  161. TypeHasDynamicSize = 0x2000,
  162. TypeNonComparableInternal = 0x4000,
  163. TypeHasError = 0x8000,
  164. };
  165. const ui64 TypeHashMagic = 0x10000;
  166. inline ui64 StreamHash(const void* buffer, size_t size, ui64 seed) {
  167. return MurmurHash(buffer, size, seed);
  168. }
  169. inline ui64 StreamHash(ui64 value, ui64 seed) {
  170. return MurmurHash(&value, sizeof(value), seed);
  171. }
  172. void ReportError(TExprContext& ctx, const TIssue& issue);
  173. class TTypeAnnotationNode {
  174. protected:
  175. TTypeAnnotationNode(ETypeAnnotationKind kind, ui32 flags, ui64 hash, ui64 usedPgExtensions)
  176. : Kind(kind)
  177. , Flags(flags)
  178. , Hash(hash)
  179. , UsedPgExtensions(usedPgExtensions)
  180. {
  181. }
  182. public:
  183. virtual ~TTypeAnnotationNode() = default;
  184. template <typename T>
  185. const T* Cast() const {
  186. static_assert(std::is_base_of<TTypeAnnotationNode, T>::value,
  187. "Should be derived from TTypeAnnotationNode");
  188. const auto ret = dynamic_cast<const T*>(this);
  189. YQL_ENSURE(ret, "Cannot cast type " << *this << " to " << ETypeAnnotationKind(T::KindValue));
  190. return ret;
  191. }
  192. template <typename T>
  193. const T* UserCast(TPosition pos, TExprContext& ctx) const {
  194. static_assert(std::is_base_of<TTypeAnnotationNode, T>::value,
  195. "Should be derived from TTypeAnnotationNode");
  196. const auto ret = dynamic_cast<const T*>(this);
  197. if (!ret) {
  198. ReportError(ctx, TIssue(pos, TStringBuilder() << "Cannot cast type " << *this << " to " << ETypeAnnotationKind(T::KindValue)));
  199. }
  200. return ret;
  201. }
  202. ETypeAnnotationKind GetKind() const {
  203. return Kind;
  204. }
  205. bool IsComposable() const {
  206. return (GetFlags() & TypeNonComposable) == 0;
  207. }
  208. bool IsPersistable() const {
  209. return (GetFlags() & TypeNonPersistable) == 0;
  210. }
  211. bool IsComputable() const {
  212. return (GetFlags() & TypeNonComputable) == 0;
  213. }
  214. bool IsInspectable() const {
  215. return (GetFlags() & TypeNonInspectable) == 0;
  216. }
  217. bool IsHashable() const {
  218. return IsPersistable() && (GetFlags() & TypeNonHashable) == 0;
  219. }
  220. bool IsEquatable() const {
  221. return IsPersistable() && (GetFlags() & TypeNonEquatable) == 0;
  222. }
  223. bool IsComparable() const {
  224. return IsPersistable() && (GetFlags() & TypeNonComparable) == 0;
  225. }
  226. bool IsComparableInternal() const {
  227. return IsPersistable() && (GetFlags() & TypeNonComparableInternal) == 0;
  228. }
  229. bool HasNull() const {
  230. return (GetFlags() & TypeHasNull) != 0;
  231. }
  232. bool HasOptional() const {
  233. return (GetFlags() & TypeHasOptional) != 0;
  234. }
  235. bool HasNestedOptional() const {
  236. return (GetFlags() & TypeHasNestedOptional) != 0;
  237. }
  238. bool HasOptionalOrNull() const {
  239. return (GetFlags() & (TypeHasOptional | TypeHasNull)) != 0;
  240. }
  241. bool IsOptionalOrNull() const {
  242. auto kind = GetKind();
  243. return kind == ETypeAnnotationKind::Optional || kind == ETypeAnnotationKind::Null || kind == ETypeAnnotationKind::Pg;
  244. }
  245. bool IsBlockOrScalar() const {
  246. return IsBlock() || IsScalar();
  247. }
  248. bool IsBlock() const {
  249. return GetKind() == ETypeAnnotationKind::Block;
  250. }
  251. bool IsScalar() const {
  252. return GetKind() == ETypeAnnotationKind::Scalar;
  253. }
  254. bool HasFixedSizeRepr() const {
  255. return (GetFlags() & (TypeHasDynamicSize | TypeNonPersistable | TypeNonComputable)) == 0;
  256. }
  257. bool IsSingleton() const {
  258. return (GetFlags() & TypeHasManyValues) == 0;
  259. }
  260. bool HasBareYson() const {
  261. return (GetFlags() & TypeHasBareYson) != 0;
  262. }
  263. bool IsPresortSupported() const {
  264. return (GetFlags() & TypeNonPresortable) == 0;
  265. }
  266. bool HasErrors() const {
  267. return (GetFlags() & TypeHasError) != 0;
  268. }
  269. ui32 GetFlags() const {
  270. return Flags;
  271. }
  272. ui64 GetHash() const {
  273. return Hash;
  274. }
  275. ui64 GetUsedPgExtensions() const {
  276. return UsedPgExtensions;
  277. }
  278. bool Equals(const TTypeAnnotationNode& node) const;
  279. void Accept(TTypeAnnotationVisitor& visitor) const;
  280. void Out(IOutputStream& out) const {
  281. out << FormatType(this);
  282. }
  283. struct THash {
  284. size_t operator()(const TTypeAnnotationNode* node) const {
  285. return node->GetHash();
  286. }
  287. };
  288. struct TEqual {
  289. bool operator()(const TTypeAnnotationNode* one, const TTypeAnnotationNode* two) const {
  290. return one->Equals(*two);
  291. }
  292. };
  293. typedef std::vector<const TTypeAnnotationNode*> TListType;
  294. typedef std::span<const TTypeAnnotationNode*> TSpanType;
  295. protected:
  296. template <typename T>
  297. static ui32 CombineFlags(const T& items) {
  298. ui32 flags = 0;
  299. for (auto& item : items) {
  300. flags |= item->GetFlags();
  301. }
  302. return flags;
  303. }
  304. template <typename T>
  305. static ui64 CombinePgExtensions(const T& items) {
  306. ui64 mask = 0;
  307. for (auto& item : items) {
  308. mask |= item->GetUsedPgExtensions();
  309. }
  310. return mask;
  311. }
  312. private:
  313. const ETypeAnnotationKind Kind;
  314. const ui32 Flags;
  315. const ui64 Hash;
  316. const ui64 UsedPgExtensions;
  317. };
  318. class TUnitExprType : public TTypeAnnotationNode {
  319. public:
  320. static constexpr ETypeAnnotationKind KindValue = ETypeAnnotationKind::Unit;
  321. TUnitExprType(ui64 hash)
  322. : TTypeAnnotationNode(KindValue,
  323. TypeNonComputable | TypeNonPersistable, hash, 0)
  324. {
  325. }
  326. static ui64 MakeHash() {
  327. return TypeHashMagic | (ui64)ETypeAnnotationKind::Unit;
  328. }
  329. bool operator==(const TUnitExprType& other) const {
  330. Y_UNUSED(other);
  331. return true;
  332. }
  333. };
  334. class TTupleExprType : public TTypeAnnotationNode {
  335. public:
  336. static constexpr ETypeAnnotationKind KindValue = ETypeAnnotationKind::Tuple;
  337. TTupleExprType(ui64 hash, const TTypeAnnotationNode::TListType& items)
  338. : TTypeAnnotationNode(KindValue, CombineFlags(items), hash, CombinePgExtensions(items))
  339. , Items(items)
  340. {
  341. }
  342. static ui64 MakeHash(const TTypeAnnotationNode::TListType& items) {
  343. ui64 hash = TypeHashMagic | (ui64)ETypeAnnotationKind::Tuple;
  344. hash = StreamHash(items.size(), hash);
  345. for (const auto& item : items) {
  346. hash = StreamHash(item->GetHash(), hash);
  347. }
  348. return hash;
  349. }
  350. size_t GetSize() const {
  351. return Items.size();
  352. }
  353. const TTypeAnnotationNode::TListType& GetItems() const {
  354. return Items;
  355. }
  356. bool operator==(const TTupleExprType& other) const {
  357. if (GetSize() != other.GetSize()) {
  358. return false;
  359. }
  360. for (ui32 i = 0, e = GetSize(); i < e; ++i) {
  361. if (GetItems()[i] != other.GetItems()[i]) {
  362. return false;
  363. }
  364. }
  365. return true;
  366. }
  367. bool Validate(TPosition position, TExprContext& ctx) const;
  368. bool Validate(TPositionHandle position, TExprContext& ctx) const;
  369. private:
  370. TTypeAnnotationNode::TListType Items;
  371. };
  372. class TMultiExprType : public TTypeAnnotationNode {
  373. public:
  374. static constexpr ETypeAnnotationKind KindValue = ETypeAnnotationKind::Multi;
  375. TMultiExprType(ui64 hash, const TTypeAnnotationNode::TListType& items)
  376. : TTypeAnnotationNode(KindValue, CombineFlags(items), hash, CombinePgExtensions(items))
  377. , Items(items)
  378. {
  379. }
  380. static ui64 MakeHash(const TTypeAnnotationNode::TListType& items) {
  381. ui64 hash = TypeHashMagic | (ui64)ETypeAnnotationKind::Multi;
  382. hash = StreamHash(items.size(), hash);
  383. for (const auto& item : items) {
  384. hash = StreamHash(item->GetHash(), hash);
  385. }
  386. return hash;
  387. }
  388. size_t GetSize() const {
  389. return Items.size();
  390. }
  391. const TTypeAnnotationNode::TListType& GetItems() const {
  392. return Items;
  393. }
  394. bool operator==(const TMultiExprType& other) const {
  395. if (GetSize() != other.GetSize()) {
  396. return false;
  397. }
  398. for (ui32 i = 0, e = GetSize(); i < e; ++i) {
  399. if (GetItems()[i] != other.GetItems()[i]) {
  400. return false;
  401. }
  402. }
  403. return true;
  404. }
  405. bool Validate(TPosition position, TExprContext& ctx) const;
  406. bool Validate(TPositionHandle position, TExprContext& ctx) const;
  407. private:
  408. TTypeAnnotationNode::TListType Items;
  409. };
  410. struct TExprContext;
  411. bool ValidateName(TPosition position, TStringBuf name, TStringBuf descr, TExprContext& ctx);
  412. bool ValidateName(TPositionHandle position, TStringBuf name, TStringBuf descr, TExprContext& ctx);
  413. class TItemExprType : public TTypeAnnotationNode {
  414. public:
  415. static constexpr ETypeAnnotationKind KindValue = ETypeAnnotationKind::Item;
  416. TItemExprType(ui64 hash, const TStringBuf& name, const TTypeAnnotationNode* itemType)
  417. : TTypeAnnotationNode(KindValue, itemType->GetFlags(), hash, itemType->GetUsedPgExtensions())
  418. , Name(name)
  419. , ItemType(itemType)
  420. {
  421. }
  422. static ui64 MakeHash(const TStringBuf& name, const TTypeAnnotationNode* itemType) {
  423. ui64 hash = TypeHashMagic | (ui64)ETypeAnnotationKind::Item;
  424. hash = StreamHash(name.size(), hash);
  425. hash = StreamHash(name.data(), name.size(), hash);
  426. return StreamHash(itemType->GetHash(), hash);
  427. }
  428. bool Validate(TPosition position, TExprContext& ctx) const;
  429. bool Validate(TPositionHandle position, TExprContext& ctx) const;
  430. const TStringBuf& GetName() const {
  431. return Name;
  432. }
  433. TStringBuf GetCleanName(bool isVirtual) const;
  434. const TTypeAnnotationNode* GetItemType() const {
  435. return ItemType;
  436. }
  437. bool operator==(const TItemExprType& other) const {
  438. return GetName() == other.GetName() && GetItemType() == other.GetItemType();
  439. }
  440. const TItemExprType* GetCleanItem(bool isVirtual, TExprContext& ctx) const;
  441. private:
  442. const TStringBuf Name;
  443. const TTypeAnnotationNode* ItemType;
  444. };
  445. class TStructExprType : public TTypeAnnotationNode {
  446. public:
  447. static constexpr ETypeAnnotationKind KindValue = ETypeAnnotationKind::Struct;
  448. struct TItemLess {
  449. bool operator()(const TItemExprType* x, const TItemExprType* y) const {
  450. return x->GetName() < y->GetName();
  451. };
  452. bool operator()(const TItemExprType* x, const TStringBuf& y) const {
  453. return x->GetName() < y;
  454. };
  455. bool operator()(const TStringBuf& x, const TItemExprType* y) const {
  456. return x < y->GetName();
  457. };
  458. };
  459. TStructExprType(ui64 hash, const TVector<const TItemExprType*>& items)
  460. : TTypeAnnotationNode(KindValue, TypeNonComparable | CombineFlags(items), hash, CombinePgExtensions(items))
  461. , Items(items)
  462. {
  463. }
  464. static ui64 MakeHash(const TVector<const TItemExprType*>& items) {
  465. Y_DEBUG_ABORT_UNLESS(IsSorted(items.begin(), items.end(), TItemLess()));
  466. ui64 hash = TypeHashMagic | (ui64)ETypeAnnotationKind::Struct;
  467. hash = StreamHash(items.size(), hash);
  468. for (const auto& item : items) {
  469. hash = StreamHash(item->GetHash(), hash);
  470. }
  471. return hash;
  472. }
  473. bool Validate(TPosition position, TExprContext& ctx) const;
  474. bool Validate(TPositionHandle position, TExprContext& ctx) const;
  475. size_t GetSize() const {
  476. return Items.size();
  477. }
  478. const TVector<const TItemExprType*>& GetItems() const {
  479. return Items;
  480. }
  481. TMaybe<ui32> FindItem(const TStringBuf& name) const {
  482. auto it = LowerBound(Items.begin(), Items.end(), name, TItemLess());
  483. if (it == Items.end() || (*it)->GetName() != name) {
  484. return TMaybe<ui32>();
  485. }
  486. return it - Items.begin();
  487. }
  488. TMaybe<ui32> FindItemI(const TStringBuf& name, bool* isVirtual) const {
  489. for (ui32 v = 0; v < 2; ++v) {
  490. if (isVirtual) {
  491. *isVirtual = v > 0;
  492. }
  493. auto nameToSearch = (v ? YqlVirtualPrefix : "") + name;
  494. auto strict = FindItem(nameToSearch);
  495. if (strict) {
  496. return strict;
  497. }
  498. TMaybe<ui32> ret;
  499. for (ui32 i = 0; i < Items.size(); ++i) {
  500. if (AsciiEqualsIgnoreCase(nameToSearch, Items[i]->GetName())) {
  501. if (ret) {
  502. return Nothing();
  503. }
  504. ret = i;
  505. }
  506. }
  507. if (ret) {
  508. return ret;
  509. }
  510. }
  511. return Nothing();
  512. }
  513. const TTypeAnnotationNode* FindItemType(const TStringBuf& name) const {
  514. const auto it = LowerBound(Items.begin(), Items.end(), name, TItemLess());
  515. if (it == Items.end() || (*it)->GetName() != name) {
  516. return nullptr;
  517. }
  518. return (*it)->GetItemType();
  519. }
  520. TMaybe<TStringBuf> FindMistype(const TStringBuf& name) const {
  521. for (const auto& item: Items) {
  522. if (NLevenshtein::Distance(name, item->GetName()) < DefaultMistypeDistance) {
  523. return item->GetName();
  524. }
  525. }
  526. return TMaybe<TStringBuf>();
  527. }
  528. bool operator==(const TStructExprType& other) const {
  529. if (GetSize() != other.GetSize()) {
  530. return false;
  531. }
  532. for (ui32 i = 0, e = GetSize(); i < e; ++i) {
  533. if (GetItems()[i] != other.GetItems()[i]) {
  534. return false;
  535. }
  536. }
  537. return true;
  538. }
  539. TString ToString() const {
  540. TStringBuilder sb;
  541. for (std::size_t i = 0; i < Items.size(); i++) {
  542. sb << i << ": " << Items[i]->GetName() << "(" << FormatType(Items[i]->GetItemType()) << ")";
  543. if (i != Items.size() - 1) {
  544. sb << ", ";
  545. }
  546. }
  547. return sb;
  548. }
  549. private:
  550. TVector<const TItemExprType*> Items;
  551. };
  552. class TListExprType : public TTypeAnnotationNode {
  553. public:
  554. static constexpr ETypeAnnotationKind KindValue = ETypeAnnotationKind::List;
  555. TListExprType(ui64 hash, const TTypeAnnotationNode* itemType)
  556. : TTypeAnnotationNode(KindValue, itemType->GetFlags() | TypeHasDynamicSize, hash, itemType->GetUsedPgExtensions())
  557. , ItemType(itemType)
  558. {
  559. }
  560. static ui64 MakeHash(const TTypeAnnotationNode* itemType) {
  561. ui64 hash = TypeHashMagic | (ui64)ETypeAnnotationKind::List;
  562. return StreamHash(itemType->GetHash(), hash);
  563. }
  564. const TTypeAnnotationNode* GetItemType() const {
  565. return ItemType;
  566. }
  567. bool operator==(const TListExprType& other) const {
  568. return GetItemType() == other.GetItemType();
  569. }
  570. private:
  571. const TTypeAnnotationNode* ItemType;
  572. };
  573. class TStreamExprType : public TTypeAnnotationNode {
  574. public:
  575. static constexpr ETypeAnnotationKind KindValue = ETypeAnnotationKind::Stream;
  576. TStreamExprType(ui64 hash, const TTypeAnnotationNode* itemType)
  577. : TTypeAnnotationNode(KindValue, itemType->GetFlags() | TypeNonPersistable, hash, itemType->GetUsedPgExtensions())
  578. , ItemType(itemType)
  579. {
  580. }
  581. static ui64 MakeHash(const TTypeAnnotationNode* itemType) {
  582. ui64 hash = TypeHashMagic | (ui64)ETypeAnnotationKind::Stream;
  583. return StreamHash(itemType->GetHash(), hash);
  584. }
  585. const TTypeAnnotationNode* GetItemType() const {
  586. return ItemType;
  587. }
  588. bool operator==(const TStreamExprType& other) const {
  589. return GetItemType() == other.GetItemType();
  590. }
  591. private:
  592. const TTypeAnnotationNode* ItemType;
  593. };
  594. class TFlowExprType : public TTypeAnnotationNode {
  595. public:
  596. static constexpr ETypeAnnotationKind KindValue = ETypeAnnotationKind::Flow;
  597. TFlowExprType(ui64 hash, const TTypeAnnotationNode* itemType)
  598. : TTypeAnnotationNode(KindValue, itemType->GetFlags() | TypeNonPersistable, hash, itemType->GetUsedPgExtensions())
  599. , ItemType(itemType)
  600. {
  601. }
  602. static ui64 MakeHash(const TTypeAnnotationNode* itemType) {
  603. ui64 hash = TypeHashMagic | (ui64)ETypeAnnotationKind::Flow;
  604. return StreamHash(itemType->GetHash(), hash);
  605. }
  606. const TTypeAnnotationNode* GetItemType() const {
  607. return ItemType;
  608. }
  609. bool operator==(const TFlowExprType& other) const {
  610. return GetItemType() == other.GetItemType();
  611. }
  612. private:
  613. const TTypeAnnotationNode* ItemType;
  614. };
  615. class TBlockExprType : public TTypeAnnotationNode {
  616. public:
  617. static constexpr ETypeAnnotationKind KindValue = ETypeAnnotationKind::Block;
  618. TBlockExprType(ui64 hash, const TTypeAnnotationNode* itemType)
  619. : TTypeAnnotationNode(KindValue, itemType->GetFlags() | TypeNonPersistable, hash, itemType->GetUsedPgExtensions())
  620. , ItemType(itemType)
  621. {
  622. }
  623. static ui64 MakeHash(const TTypeAnnotationNode* itemType) {
  624. ui64 hash = TypeHashMagic | (ui64)ETypeAnnotationKind::Block;
  625. return StreamHash(itemType->GetHash(), hash);
  626. }
  627. const TTypeAnnotationNode* GetItemType() const {
  628. return ItemType;
  629. }
  630. bool operator==(const TBlockExprType& other) const {
  631. return GetItemType() == other.GetItemType();
  632. }
  633. private:
  634. const TTypeAnnotationNode* ItemType;
  635. };
  636. class TScalarExprType : public TTypeAnnotationNode {
  637. public:
  638. static constexpr ETypeAnnotationKind KindValue = ETypeAnnotationKind::Scalar;
  639. TScalarExprType(ui64 hash, const TTypeAnnotationNode* itemType)
  640. : TTypeAnnotationNode(KindValue, itemType->GetFlags() | TypeNonPersistable, hash, itemType->GetUsedPgExtensions())
  641. , ItemType(itemType)
  642. {
  643. }
  644. static ui64 MakeHash(const TTypeAnnotationNode* itemType) {
  645. ui64 hash = TypeHashMagic | (ui64)ETypeAnnotationKind::Scalar;
  646. return StreamHash(itemType->GetHash(), hash);
  647. }
  648. const TTypeAnnotationNode* GetItemType() const {
  649. return ItemType;
  650. }
  651. bool operator==(const TScalarExprType& other) const {
  652. return GetItemType() == other.GetItemType();
  653. }
  654. private:
  655. const TTypeAnnotationNode* ItemType;
  656. };
  657. class TDataExprType : public TTypeAnnotationNode {
  658. public:
  659. static constexpr ETypeAnnotationKind KindValue = ETypeAnnotationKind::Data;
  660. TDataExprType(ui64 hash, EDataSlot slot)
  661. : TTypeAnnotationNode(KindValue, GetFlags(slot), hash, 0)
  662. , Slot(slot)
  663. {
  664. }
  665. static ui32 GetFlags(EDataSlot slot) {
  666. ui32 ret = TypeHasManyValues;
  667. auto props = NUdf::GetDataTypeInfo(slot).Features;
  668. if (!(props & NUdf::CanHash)) {
  669. ret |= TypeNonHashable;
  670. }
  671. if (!(props & NUdf::CanEquate)) {
  672. ret |= TypeNonEquatable;
  673. }
  674. if (!(props & NUdf::CanCompare)) {
  675. ret |= TypeNonComparable;
  676. ret |= TypeNonComparableInternal;
  677. }
  678. if (slot == NUdf::EDataSlot::Yson) {
  679. ret |= TypeHasBareYson;
  680. }
  681. if (props & NUdf::StringType) {
  682. ret |= TypeHasDynamicSize;
  683. }
  684. return ret;
  685. }
  686. static ui64 MakeHash(EDataSlot slot) {
  687. ui64 hash = TypeHashMagic | (ui64)ETypeAnnotationKind::Data;
  688. auto dataType = NUdf::GetDataTypeInfo(slot).Name;
  689. hash = StreamHash(dataType.size(), hash);
  690. return StreamHash(dataType.data(), dataType.size(), hash);
  691. }
  692. EDataSlot GetSlot() const {
  693. return Slot;
  694. }
  695. TStringBuf GetName() const {
  696. return NUdf::GetDataTypeInfo(Slot).Name;
  697. }
  698. bool operator==(const TDataExprType& other) const {
  699. return Slot == other.Slot;
  700. }
  701. private:
  702. EDataSlot Slot;
  703. };
  704. class TDataExprParamsType : public TDataExprType {
  705. public:
  706. TDataExprParamsType(ui64 hash, EDataSlot slot, const TStringBuf& one, const TStringBuf& two)
  707. : TDataExprType(hash, slot), One(one), Two(two)
  708. {}
  709. static ui64 MakeHash(EDataSlot slot, const TStringBuf& one, const TStringBuf& two) {
  710. auto hash = TDataExprType::MakeHash(slot);
  711. hash = StreamHash(one.size(), hash);
  712. hash = StreamHash(one.data(), one.size(), hash);
  713. hash = StreamHash(two.size(), hash);
  714. hash = StreamHash(two.data(), two.size(), hash);
  715. return hash;
  716. }
  717. const TStringBuf& GetParamOne() const {
  718. return One;
  719. }
  720. const TStringBuf& GetParamTwo() const {
  721. return Two;
  722. }
  723. bool operator==(const TDataExprParamsType& other) const {
  724. return GetSlot() == other.GetSlot() && GetParamOne() == other.GetParamOne() && GetParamTwo() == other.GetParamTwo();
  725. }
  726. bool Validate(TPosition position, TExprContext& ctx) const;
  727. bool Validate(TPositionHandle position, TExprContext& ctx) const;
  728. private:
  729. const TStringBuf One, Two;
  730. };
  731. class TPgExprType : public TTypeAnnotationNode {
  732. public:
  733. static constexpr ETypeAnnotationKind KindValue = ETypeAnnotationKind::Pg;
  734. // TODO: TypeHasDynamicSize for Pg types
  735. TPgExprType(ui64 hash, ui32 typeId)
  736. : TTypeAnnotationNode(KindValue, GetFlags(typeId), hash, GetPgExtensionsMask(typeId))
  737. , TypeId(typeId)
  738. {
  739. }
  740. static ui64 MakeHash(ui32 typeId) {
  741. ui64 hash = TypeHashMagic | (ui64)ETypeAnnotationKind::Pg;
  742. return StreamHash(typeId, hash);
  743. }
  744. const TString& GetName() const;
  745. ui32 GetId() const {
  746. return TypeId;
  747. }
  748. bool operator==(const TPgExprType& other) const {
  749. return TypeId == other.TypeId;
  750. }
  751. private:
  752. ui32 GetFlags(ui32 typeId);
  753. ui64 GetPgExtensionsMask(ui32 typeId);
  754. private:
  755. ui32 TypeId;
  756. };
  757. ui64 MakePgExtensionMask(ui32 extensionIndex);
  758. class TWorldExprType : public TTypeAnnotationNode {
  759. public:
  760. static constexpr ETypeAnnotationKind KindValue = ETypeAnnotationKind::World;
  761. TWorldExprType(ui64 hash)
  762. : TTypeAnnotationNode(KindValue,
  763. TypeNonComposable | TypeNonComputable | TypeNonPersistable | TypeNonInspectable, hash, 0)
  764. {
  765. }
  766. static ui64 MakeHash() {
  767. return TypeHashMagic | (ui64)ETypeAnnotationKind::World;
  768. }
  769. bool operator==(const TWorldExprType& other) const {
  770. Y_UNUSED(other);
  771. return true;
  772. }
  773. };
  774. class TOptionalExprType : public TTypeAnnotationNode {
  775. public:
  776. static constexpr ETypeAnnotationKind KindValue = ETypeAnnotationKind::Optional;
  777. TOptionalExprType(ui64 hash, const TTypeAnnotationNode* itemType)
  778. : TTypeAnnotationNode(KindValue, GetFlags(itemType), hash, itemType->GetUsedPgExtensions())
  779. , ItemType(itemType)
  780. {
  781. }
  782. static ui32 GetFlags(const TTypeAnnotationNode* itemType) {
  783. auto ret = TypeHasOptional | itemType->GetFlags();
  784. if (itemType->GetKind() == ETypeAnnotationKind::Data &&
  785. itemType->Cast<TDataExprType>()->GetSlot() == NUdf::EDataSlot::Yson) {
  786. ret = ret & ~TypeHasBareYson;
  787. }
  788. if (itemType->IsOptionalOrNull()) {
  789. ret |= TypeHasNestedOptional;
  790. }
  791. return ret;
  792. }
  793. static ui64 MakeHash(const TTypeAnnotationNode* itemType) {
  794. ui64 hash = TypeHashMagic | (ui64)ETypeAnnotationKind::Optional;
  795. return StreamHash(itemType->GetHash(), hash);
  796. }
  797. const TTypeAnnotationNode* GetItemType() const {
  798. return ItemType;
  799. }
  800. bool operator==(const TOptionalExprType& other) const {
  801. return GetItemType() == other.GetItemType();
  802. }
  803. private:
  804. const TTypeAnnotationNode* ItemType;
  805. };
  806. class TVariantExprType : public TTypeAnnotationNode {
  807. public:
  808. static constexpr ETypeAnnotationKind KindValue = ETypeAnnotationKind::Variant;
  809. TVariantExprType(ui64 hash, const TTypeAnnotationNode* underlyingType)
  810. : TTypeAnnotationNode(KindValue, MakeFlags(underlyingType), hash, underlyingType->GetUsedPgExtensions())
  811. , UnderlyingType(underlyingType)
  812. {
  813. }
  814. static ui64 MakeHash(const TTypeAnnotationNode* underlyingType) {
  815. ui64 hash = TypeHashMagic | (ui64)ETypeAnnotationKind::Variant;
  816. return StreamHash(underlyingType->GetHash(), hash);
  817. }
  818. const TTypeAnnotationNode* GetUnderlyingType() const {
  819. return UnderlyingType;
  820. }
  821. bool operator==(const TVariantExprType& other) const {
  822. return GetUnderlyingType() == other.GetUnderlyingType();
  823. }
  824. bool Validate(TPosition position, TExprContext& ctx) const;
  825. bool Validate(TPositionHandle position, TExprContext& ctx) const;
  826. static ui32 MakeFlags(const TTypeAnnotationNode* underlyingType);
  827. private:
  828. const TTypeAnnotationNode* UnderlyingType;
  829. };
  830. class TTypeExprType : public TTypeAnnotationNode {
  831. public:
  832. static constexpr ETypeAnnotationKind KindValue = ETypeAnnotationKind::Type;
  833. TTypeExprType(ui64 hash, const TTypeAnnotationNode* type)
  834. : TTypeAnnotationNode(KindValue, TypeNonPersistable | TypeNonComputable | (type->GetFlags() & TypeHasError), hash, 0)
  835. , Type(type)
  836. {
  837. }
  838. static ui64 MakeHash(const TTypeAnnotationNode* type) {
  839. ui64 hash = TypeHashMagic | (ui64)ETypeAnnotationKind::Type;
  840. return StreamHash(type->GetHash(), hash);
  841. }
  842. const TTypeAnnotationNode* GetType() const {
  843. return Type;
  844. }
  845. bool operator==(const TTypeExprType& other) const {
  846. return GetType() == other.GetType();
  847. }
  848. private:
  849. const TTypeAnnotationNode* Type;
  850. };
  851. class TDictExprType : public TTypeAnnotationNode {
  852. public:
  853. static constexpr ETypeAnnotationKind KindValue = ETypeAnnotationKind::Dict;
  854. TDictExprType(ui64 hash, const TTypeAnnotationNode* keyType, const TTypeAnnotationNode* payloadType)
  855. : TTypeAnnotationNode(KindValue, TypeNonComparable | TypeHasDynamicSize |
  856. keyType->GetFlags() | payloadType->GetFlags(), hash,
  857. keyType->GetUsedPgExtensions() | payloadType->GetUsedPgExtensions())
  858. , KeyType(keyType)
  859. , PayloadType(payloadType)
  860. {
  861. }
  862. static ui64 MakeHash(const TTypeAnnotationNode* keyType, const TTypeAnnotationNode* payloadType) {
  863. ui64 hash = TypeHashMagic | (ui64)ETypeAnnotationKind::Dict;
  864. return StreamHash(StreamHash(keyType->GetHash(), hash), payloadType->GetHash());
  865. }
  866. bool Validate(TPosition position, TExprContext& ctx) const;
  867. bool Validate(TPositionHandle position, TExprContext& ctx) const;
  868. const TTypeAnnotationNode* GetKeyType() const {
  869. return KeyType;
  870. }
  871. const TTypeAnnotationNode* GetPayloadType() const {
  872. return PayloadType;
  873. }
  874. bool operator==(const TDictExprType& other) const {
  875. return GetKeyType() == other.GetKeyType() &&
  876. GetPayloadType() == other.GetPayloadType();
  877. }
  878. private:
  879. const TTypeAnnotationNode* KeyType;
  880. const TTypeAnnotationNode* PayloadType;
  881. };
  882. class TVoidExprType : public TTypeAnnotationNode {
  883. public:
  884. static constexpr ETypeAnnotationKind KindValue = ETypeAnnotationKind::Void;
  885. TVoidExprType(ui64 hash)
  886. : TTypeAnnotationNode(KindValue, 0, hash, 0)
  887. {
  888. }
  889. static ui64 MakeHash() {
  890. return TypeHashMagic | (ui64)ETypeAnnotationKind::Void;
  891. }
  892. bool operator==(const TVoidExprType& other) const {
  893. Y_UNUSED(other);
  894. return true;
  895. }
  896. };
  897. class TNullExprType : public TTypeAnnotationNode {
  898. public:
  899. static constexpr ETypeAnnotationKind KindValue = ETypeAnnotationKind::Null;
  900. TNullExprType(ui64 hash)
  901. : TTypeAnnotationNode(KindValue, TypeHasNull, hash, 0)
  902. {
  903. }
  904. static ui64 MakeHash() {
  905. return TypeHashMagic | (ui64)ETypeAnnotationKind::Null;
  906. }
  907. bool operator==(const TNullExprType& other) const {
  908. Y_UNUSED(other);
  909. return true;
  910. }
  911. };
  912. class TCallableExprType : public TTypeAnnotationNode {
  913. public:
  914. static constexpr ETypeAnnotationKind KindValue = ETypeAnnotationKind::Callable;
  915. struct TArgumentInfo {
  916. const TTypeAnnotationNode* Type = nullptr;
  917. TStringBuf Name;
  918. ui64 Flags = 0;
  919. bool operator==(const TArgumentInfo& other) const {
  920. return Type == other.Type && Name == other.Name && Flags == other.Flags;
  921. }
  922. bool operator!=(const TArgumentInfo& other) const {
  923. return !(*this == other);
  924. }
  925. };
  926. TCallableExprType(ui64 hash, const TTypeAnnotationNode* returnType, const TVector<TArgumentInfo>& arguments
  927. , size_t optionalArgumentsCount, const TStringBuf& payload)
  928. : TTypeAnnotationNode(KindValue, MakeFlags(arguments, returnType), hash, returnType->GetUsedPgExtensions())
  929. , ReturnType(returnType)
  930. , Arguments(arguments)
  931. , OptionalArgumentsCount(optionalArgumentsCount)
  932. , Payload(payload)
  933. {
  934. for (ui32 i = 0; i < Arguments.size(); ++i) {
  935. const auto& arg = Arguments[i];
  936. if (!arg.Name.empty()) {
  937. IndexByName.insert({ arg.Name, i });
  938. }
  939. }
  940. }
  941. static ui64 MakeHash(const TTypeAnnotationNode* returnType, const TVector<TArgumentInfo>& arguments
  942. , size_t optionalArgumentsCount, const TStringBuf& payload) {
  943. ui64 hash = TypeHashMagic | (ui64)ETypeAnnotationKind::Callable;
  944. hash = StreamHash(returnType->GetHash(), hash);
  945. hash = StreamHash(arguments.size(), hash);
  946. for (const auto& arg : arguments) {
  947. hash = StreamHash(arg.Name.size(), hash);
  948. hash = StreamHash(arg.Name.data(), arg.Name.size(), hash);
  949. hash = StreamHash(arg.Flags, hash);
  950. hash = StreamHash(arg.Type->GetHash(), hash);
  951. }
  952. hash = StreamHash(optionalArgumentsCount, hash);
  953. hash = StreamHash(payload.size(), hash);
  954. hash = StreamHash(payload.data(), payload.size(), hash);
  955. return hash;
  956. }
  957. const TTypeAnnotationNode* GetReturnType() const {
  958. return ReturnType;
  959. }
  960. size_t GetOptionalArgumentsCount() const {
  961. return OptionalArgumentsCount;
  962. }
  963. const TStringBuf& GetPayload() const {
  964. return Payload;
  965. }
  966. size_t GetArgumentsSize() const {
  967. return Arguments.size();
  968. }
  969. const TVector<TArgumentInfo>& GetArguments() const {
  970. return Arguments;
  971. }
  972. bool operator==(const TCallableExprType& other) const {
  973. if (GetArgumentsSize() != other.GetArgumentsSize()) {
  974. return false;
  975. }
  976. if (GetOptionalArgumentsCount() != other.GetOptionalArgumentsCount()) {
  977. return false;
  978. }
  979. if (GetReturnType() != other.GetReturnType()) {
  980. return false;
  981. }
  982. for (ui32 i = 0, e = GetArgumentsSize(); i < e; ++i) {
  983. if (GetArguments()[i] != other.GetArguments()[i]) {
  984. return false;
  985. }
  986. }
  987. return true;
  988. }
  989. bool Validate(TPosition position, TExprContext& ctx) const;
  990. bool Validate(TPositionHandle position, TExprContext& ctx) const;
  991. TMaybe<ui32> ArgumentIndexByName(const TStringBuf& name) const {
  992. auto it = IndexByName.find(name);
  993. if (it == IndexByName.end()) {
  994. return {};
  995. }
  996. return it->second;
  997. }
  998. private:
  999. static ui32 MakeFlags(const TVector<TArgumentInfo>& arguments, const TTypeAnnotationNode* returnType) {
  1000. ui32 flags = TypeNonPersistable;
  1001. flags |= returnType->GetFlags();
  1002. for (const auto& arg : arguments) {
  1003. flags |= (arg.Type->GetFlags() & TypeHasError);
  1004. }
  1005. return flags;
  1006. }
  1007. private:
  1008. const TTypeAnnotationNode* ReturnType;
  1009. TVector<TArgumentInfo> Arguments;
  1010. const size_t OptionalArgumentsCount;
  1011. const TStringBuf Payload;
  1012. THashMap<TStringBuf, ui32> IndexByName;
  1013. };
  1014. class TGenericExprType : public TTypeAnnotationNode {
  1015. public:
  1016. static constexpr ETypeAnnotationKind KindValue = ETypeAnnotationKind::Generic;
  1017. TGenericExprType(ui64 hash)
  1018. : TTypeAnnotationNode(KindValue, TypeNonComputable, hash, 0)
  1019. {
  1020. }
  1021. static ui64 MakeHash() {
  1022. return TypeHashMagic | (ui64)ETypeAnnotationKind::Generic;
  1023. }
  1024. bool operator==(const TGenericExprType& other) const {
  1025. Y_UNUSED(other);
  1026. return true;
  1027. }
  1028. };
  1029. class TResourceExprType : public TTypeAnnotationNode {
  1030. public:
  1031. static constexpr ETypeAnnotationKind KindValue = ETypeAnnotationKind::Resource;
  1032. TResourceExprType(ui64 hash, const TStringBuf& tag)
  1033. : TTypeAnnotationNode(KindValue, TypeNonPersistable | TypeHasManyValues, hash, 0)
  1034. , Tag(tag)
  1035. {}
  1036. static ui64 MakeHash(const TStringBuf& tag) {
  1037. ui64 hash = TypeHashMagic | (ui64)ETypeAnnotationKind::Resource;
  1038. hash = StreamHash(tag.size(), hash);
  1039. return StreamHash(tag.data(), tag.size(), hash);
  1040. }
  1041. const TStringBuf& GetTag() const {
  1042. return Tag;
  1043. }
  1044. bool operator==(const TResourceExprType& other) const {
  1045. return Tag == other.Tag;
  1046. }
  1047. private:
  1048. const TStringBuf Tag;
  1049. };
  1050. class TTaggedExprType : public TTypeAnnotationNode {
  1051. public:
  1052. static constexpr ETypeAnnotationKind KindValue = ETypeAnnotationKind::Tagged;
  1053. TTaggedExprType(ui64 hash, const TTypeAnnotationNode* baseType, const TStringBuf& tag)
  1054. : TTypeAnnotationNode(KindValue, baseType->GetFlags(), hash, baseType->GetUsedPgExtensions())
  1055. , BaseType(baseType)
  1056. , Tag(tag)
  1057. {}
  1058. static ui64 MakeHash(const TTypeAnnotationNode* baseType, const TStringBuf& tag) {
  1059. ui64 hash = TypeHashMagic | (ui64)ETypeAnnotationKind::Tagged;
  1060. hash = StreamHash(baseType->GetHash(), hash);
  1061. hash = StreamHash(tag.size(), hash);
  1062. return StreamHash(tag.data(), tag.size(), hash);
  1063. }
  1064. const TStringBuf& GetTag() const {
  1065. return Tag;
  1066. }
  1067. const TTypeAnnotationNode* GetBaseType() const {
  1068. return BaseType;
  1069. }
  1070. bool operator==(const TTaggedExprType& other) const {
  1071. return Tag == other.Tag && GetBaseType() == other.GetBaseType();
  1072. }
  1073. bool Validate(TPosition position, TExprContext& ctx) const;
  1074. bool Validate(TPositionHandle position, TExprContext& ctx) const;
  1075. private:
  1076. const TTypeAnnotationNode* BaseType;
  1077. const TStringBuf Tag;
  1078. };
  1079. class TErrorExprType : public TTypeAnnotationNode {
  1080. public:
  1081. static constexpr ETypeAnnotationKind KindValue = ETypeAnnotationKind::Error;
  1082. TErrorExprType(ui64 hash, const TIssue& error)
  1083. : TTypeAnnotationNode(KindValue, TypeHasError, hash, 0)
  1084. , Error(error)
  1085. {}
  1086. static ui64 MakeHash(const TIssue& error) {
  1087. return error.Hash();
  1088. }
  1089. const TIssue& GetError() const {
  1090. return Error;
  1091. }
  1092. bool operator==(const TErrorExprType& other) const {
  1093. return Error == other.Error;
  1094. }
  1095. private:
  1096. const TIssue Error;
  1097. };
  1098. class TEmptyListExprType : public TTypeAnnotationNode {
  1099. public:
  1100. static constexpr ETypeAnnotationKind KindValue = ETypeAnnotationKind::EmptyList;
  1101. TEmptyListExprType(ui64 hash)
  1102. : TTypeAnnotationNode(KindValue, 0, hash, 0)
  1103. {
  1104. }
  1105. static ui64 MakeHash() {
  1106. return TypeHashMagic | (ui64)ETypeAnnotationKind::EmptyList;
  1107. }
  1108. bool operator==(const TEmptyListExprType& other) const {
  1109. Y_UNUSED(other);
  1110. return true;
  1111. }
  1112. };
  1113. class TEmptyDictExprType : public TTypeAnnotationNode {
  1114. public:
  1115. static constexpr ETypeAnnotationKind KindValue = ETypeAnnotationKind::EmptyDict;
  1116. TEmptyDictExprType(ui64 hash)
  1117. : TTypeAnnotationNode(KindValue, 0, hash, 0)
  1118. {
  1119. }
  1120. static ui64 MakeHash() {
  1121. return TypeHashMagic | (ui64)ETypeAnnotationKind::EmptyDict;
  1122. }
  1123. bool operator==(const TEmptyDictExprType& other) const {
  1124. Y_UNUSED(other);
  1125. return true;
  1126. }
  1127. };
  1128. inline bool TTypeAnnotationNode::Equals(const TTypeAnnotationNode& node) const {
  1129. if (this == &node) {
  1130. return true;
  1131. }
  1132. if (Hash != node.GetHash()) {
  1133. return false;
  1134. }
  1135. if (Kind != node.GetKind()) {
  1136. return false;
  1137. }
  1138. switch (Kind) {
  1139. case ETypeAnnotationKind::Unit:
  1140. return static_cast<const TUnitExprType&>(*this) == static_cast<const TUnitExprType&>(node);
  1141. case ETypeAnnotationKind::Tuple:
  1142. return static_cast<const TTupleExprType&>(*this) == static_cast<const TTupleExprType&>(node);
  1143. case ETypeAnnotationKind::Struct:
  1144. return static_cast<const TStructExprType&>(*this) == static_cast<const TStructExprType&>(node);
  1145. case ETypeAnnotationKind::Item:
  1146. return static_cast<const TItemExprType&>(*this) == static_cast<const TItemExprType&>(node);
  1147. case ETypeAnnotationKind::List:
  1148. return static_cast<const TListExprType&>(*this) == static_cast<const TListExprType&>(node);
  1149. case ETypeAnnotationKind::Data:
  1150. return static_cast<const TDataExprType&>(*this) == static_cast<const TDataExprType&>(node);
  1151. case ETypeAnnotationKind::Pg:
  1152. return static_cast<const TPgExprType&>(*this) == static_cast<const TPgExprType&>(node);
  1153. case ETypeAnnotationKind::World:
  1154. return static_cast<const TWorldExprType&>(*this) == static_cast<const TWorldExprType&>(node);
  1155. case ETypeAnnotationKind::Optional:
  1156. return static_cast<const TOptionalExprType&>(*this) == static_cast<const TOptionalExprType&>(node);
  1157. case ETypeAnnotationKind::Type:
  1158. return static_cast<const TTypeExprType&>(*this) == static_cast<const TTypeExprType&>(node);
  1159. case ETypeAnnotationKind::Dict:
  1160. return static_cast<const TDictExprType&>(*this) == static_cast<const TDictExprType&>(node);
  1161. case ETypeAnnotationKind::Void:
  1162. return static_cast<const TVoidExprType&>(*this) == static_cast<const TVoidExprType&>(node);
  1163. case ETypeAnnotationKind::Null:
  1164. return static_cast<const TNullExprType&>(*this) == static_cast<const TNullExprType&>(node);
  1165. case ETypeAnnotationKind::Callable:
  1166. return static_cast<const TCallableExprType&>(*this) == static_cast<const TCallableExprType&>(node);
  1167. case ETypeAnnotationKind::Generic:
  1168. return static_cast<const TGenericExprType&>(*this) == static_cast<const TGenericExprType&>(node);
  1169. case ETypeAnnotationKind::Resource:
  1170. return static_cast<const TResourceExprType&>(*this) == static_cast<const TResourceExprType&>(node);
  1171. case ETypeAnnotationKind::Tagged:
  1172. return static_cast<const TTaggedExprType&>(*this) == static_cast<const TTaggedExprType&>(node);
  1173. case ETypeAnnotationKind::Error:
  1174. return static_cast<const TErrorExprType&>(*this) == static_cast<const TErrorExprType&>(node);
  1175. case ETypeAnnotationKind::Variant:
  1176. return static_cast<const TVariantExprType&>(*this) == static_cast<const TVariantExprType&>(node);
  1177. case ETypeAnnotationKind::Stream:
  1178. return static_cast<const TStreamExprType&>(*this) == static_cast<const TStreamExprType&>(node);
  1179. case ETypeAnnotationKind::Flow:
  1180. return static_cast<const TFlowExprType&>(*this) == static_cast<const TFlowExprType&>(node);
  1181. case ETypeAnnotationKind::EmptyList:
  1182. return static_cast<const TEmptyListExprType&>(*this) == static_cast<const TEmptyListExprType&>(node);
  1183. case ETypeAnnotationKind::EmptyDict:
  1184. return static_cast<const TEmptyDictExprType&>(*this) == static_cast<const TEmptyDictExprType&>(node);
  1185. case ETypeAnnotationKind::Multi:
  1186. return static_cast<const TMultiExprType&>(*this) == static_cast<const TMultiExprType&>(node);
  1187. case ETypeAnnotationKind::Block:
  1188. return static_cast<const TBlockExprType&>(*this) == static_cast<const TBlockExprType&>(node);
  1189. case ETypeAnnotationKind::Scalar:
  1190. return static_cast<const TScalarExprType&>(*this) == static_cast<const TScalarExprType&>(node);
  1191. case ETypeAnnotationKind::LastType:
  1192. YQL_ENSURE(false, "Incorrect type");
  1193. }
  1194. return false;
  1195. }
  1196. inline void TTypeAnnotationNode::Accept(TTypeAnnotationVisitor& visitor) const {
  1197. switch (Kind) {
  1198. case ETypeAnnotationKind::Unit:
  1199. return visitor.Visit(static_cast<const TUnitExprType&>(*this));
  1200. case ETypeAnnotationKind::Tuple:
  1201. return visitor.Visit(static_cast<const TTupleExprType&>(*this));
  1202. case ETypeAnnotationKind::Struct:
  1203. return visitor.Visit(static_cast<const TStructExprType&>(*this));
  1204. case ETypeAnnotationKind::Item:
  1205. return visitor.Visit(static_cast<const TItemExprType&>(*this));
  1206. case ETypeAnnotationKind::List:
  1207. return visitor.Visit(static_cast<const TListExprType&>(*this));
  1208. case ETypeAnnotationKind::Data:
  1209. return visitor.Visit(static_cast<const TDataExprType&>(*this));
  1210. case ETypeAnnotationKind::Pg:
  1211. return visitor.Visit(static_cast<const TPgExprType&>(*this));
  1212. case ETypeAnnotationKind::World:
  1213. return visitor.Visit(static_cast<const TWorldExprType&>(*this));
  1214. case ETypeAnnotationKind::Optional:
  1215. return visitor.Visit(static_cast<const TOptionalExprType&>(*this));
  1216. case ETypeAnnotationKind::Type:
  1217. return visitor.Visit(static_cast<const TTypeExprType&>(*this));
  1218. case ETypeAnnotationKind::Dict:
  1219. return visitor.Visit(static_cast<const TDictExprType&>(*this));
  1220. case ETypeAnnotationKind::Void:
  1221. return visitor.Visit(static_cast<const TVoidExprType&>(*this));
  1222. case ETypeAnnotationKind::Null:
  1223. return visitor.Visit(static_cast<const TNullExprType&>(*this));
  1224. case ETypeAnnotationKind::Callable:
  1225. return visitor.Visit(static_cast<const TCallableExprType&>(*this));
  1226. case ETypeAnnotationKind::Generic:
  1227. return visitor.Visit(static_cast<const TGenericExprType&>(*this));
  1228. case ETypeAnnotationKind::Resource:
  1229. return visitor.Visit(static_cast<const TResourceExprType&>(*this));
  1230. case ETypeAnnotationKind::Tagged:
  1231. return visitor.Visit(static_cast<const TTaggedExprType&>(*this));
  1232. case ETypeAnnotationKind::Error:
  1233. return visitor.Visit(static_cast<const TErrorExprType&>(*this));
  1234. case ETypeAnnotationKind::Variant:
  1235. return visitor.Visit(static_cast<const TVariantExprType&>(*this));
  1236. case ETypeAnnotationKind::Stream:
  1237. return visitor.Visit(static_cast<const TStreamExprType&>(*this));
  1238. case ETypeAnnotationKind::Flow:
  1239. return visitor.Visit(static_cast<const TFlowExprType&>(*this));
  1240. case ETypeAnnotationKind::EmptyList:
  1241. return visitor.Visit(static_cast<const TEmptyListExprType&>(*this));
  1242. case ETypeAnnotationKind::EmptyDict:
  1243. return visitor.Visit(static_cast<const TEmptyDictExprType&>(*this));
  1244. case ETypeAnnotationKind::Multi:
  1245. return visitor.Visit(static_cast<const TMultiExprType&>(*this));
  1246. case ETypeAnnotationKind::Block:
  1247. return visitor.Visit(static_cast<const TBlockExprType&>(*this));
  1248. case ETypeAnnotationKind::Scalar:
  1249. return visitor.Visit(static_cast<const TScalarExprType&>(*this));
  1250. case ETypeAnnotationKind::LastType:
  1251. YQL_ENSURE(false, "Incorrect type");
  1252. }
  1253. }
  1254. class TExprNode {
  1255. friend class TExprNodeBuilder;
  1256. friend class TExprNodeReplaceBuilder;
  1257. friend struct TExprContext;
  1258. private:
  1259. struct TExprFlags {
  1260. enum : ui16 {
  1261. Default = 0,
  1262. Dead = 0x01,
  1263. Frozen = 0x02,
  1264. };
  1265. static constexpr ui32 FlagsMask = 0x03; // all flags should fit here
  1266. };
  1267. public:
  1268. typedef TIntrusivePtr<TExprNode> TPtr;
  1269. typedef std::vector<TPtr> TListType;
  1270. typedef TArrayRef<const TPtr> TChildrenType;
  1271. struct TPtrHash : private std::hash<const TExprNode*> {
  1272. size_t operator()(const TPtr& p) const {
  1273. return std::hash<const TExprNode*>::operator()(p.Get());
  1274. }
  1275. };
  1276. #define YQL_EXPR_NODE_TYPE_MAP(xx) \
  1277. xx(List, 0) \
  1278. xx(Atom, 1) \
  1279. xx(Callable, 2) \
  1280. xx(Lambda, 3) \
  1281. xx(Argument, 4) \
  1282. xx(Arguments, 5) \
  1283. xx(World, 7)
  1284. enum EType : ui8 {
  1285. YQL_EXPR_NODE_TYPE_MAP(ENUM_VALUE_GEN)
  1286. };
  1287. static constexpr ui32 TypeMask = 0x07; // all types should fit here
  1288. #define YQL_EXPR_NODE_STATE_MAP(xx) \
  1289. xx(Initial, 0) \
  1290. xx(TypeInProgress, 1) \
  1291. xx(TypePending, 2) \
  1292. xx(TypeComplete, 3) \
  1293. xx(ConstrInProgress, 4) \
  1294. xx(ConstrPending, 5) \
  1295. xx(ConstrComplete, 6) \
  1296. xx(ExecutionRequired, 7) \
  1297. xx(ExecutionInProgress, 8) \
  1298. xx(ExecutionPending, 9) \
  1299. xx(ExecutionComplete, 10) \
  1300. xx(Error, 11) \
  1301. xx(Last, 12)
  1302. enum class EState : ui8 {
  1303. YQL_EXPR_NODE_STATE_MAP(ENUM_VALUE_GEN)
  1304. };
  1305. static TPtr GetResult(const TPtr& node) {
  1306. return node->Type() == Callable ? node->Result : node;
  1307. }
  1308. const TExprNode& GetResult() const {
  1309. ENSURE_NOT_DELETED
  1310. if (Type() != Callable) {
  1311. return *this;
  1312. }
  1313. YQL_ENSURE(Result);
  1314. return *Result;
  1315. }
  1316. bool HasResult() const {
  1317. ENSURE_NOT_DELETED
  1318. return Type() != Callable || bool(Result);
  1319. }
  1320. void SetResult(TPtr&& result) {
  1321. ENSURE_NOT_DELETED
  1322. ENSURE_NOT_FROZEN
  1323. Result = std::move(result);
  1324. }
  1325. bool IsCallable(const std::string_view& name) const {
  1326. ENSURE_NOT_DELETED
  1327. return Type() == TExprNode::Callable && Content() == name;
  1328. }
  1329. bool IsCallable(const std::initializer_list<std::string_view>& names) const {
  1330. ENSURE_NOT_DELETED
  1331. return Type() == TExprNode::Callable && names.end() != std::find(names.begin(), names.end(), Content());
  1332. }
  1333. template <class TKey>
  1334. bool IsCallable(const THashSet<TKey>& names) const {
  1335. ENSURE_NOT_DELETED
  1336. return Type() == TExprNode::Callable && names.contains(Content());
  1337. }
  1338. bool IsCallable() const {
  1339. ENSURE_NOT_DELETED
  1340. return Type() == TExprNode::Callable;
  1341. }
  1342. bool IsAtom() const {
  1343. ENSURE_NOT_DELETED
  1344. return Type() == TExprNode::Atom;
  1345. }
  1346. bool IsWorld() const {
  1347. ENSURE_NOT_DELETED
  1348. return Type() == TExprNode::World;
  1349. }
  1350. bool IsAtom(const std::string_view& content) const {
  1351. ENSURE_NOT_DELETED
  1352. return Type() == TExprNode::Atom && Content() == content;
  1353. }
  1354. bool IsAtom(const std::initializer_list<std::string_view>& names) const {
  1355. ENSURE_NOT_DELETED
  1356. return Type() == TExprNode::Atom && names.end() != std::find(names.begin(), names.end(), Content());
  1357. }
  1358. bool IsList() const {
  1359. ENSURE_NOT_DELETED
  1360. return Type() == TExprNode::List;
  1361. }
  1362. bool IsLambda() const {
  1363. ENSURE_NOT_DELETED
  1364. return Type() == TExprNode::Lambda;
  1365. }
  1366. bool IsArgument() const {
  1367. ENSURE_NOT_DELETED
  1368. return Type() == TExprNode::Argument;
  1369. }
  1370. bool IsArguments() const {
  1371. ENSURE_NOT_DELETED
  1372. return Type() == TExprNode::Arguments;
  1373. }
  1374. bool IsComposable() const {
  1375. ENSURE_NOT_DELETED
  1376. return !IsLambda() && TypeAnnotation_->IsComposable();
  1377. }
  1378. bool IsPersistable() const {
  1379. ENSURE_NOT_DELETED
  1380. return !IsLambda() && TypeAnnotation_->IsPersistable();
  1381. }
  1382. bool IsComputable() const {
  1383. ENSURE_NOT_DELETED
  1384. return !IsLambda() && TypeAnnotation_->IsComputable();
  1385. }
  1386. bool IsInspectable() const {
  1387. ENSURE_NOT_DELETED
  1388. return !IsLambda() && TypeAnnotation_->IsInspectable();
  1389. }
  1390. bool ForDisclosing() const {
  1391. ENSURE_NOT_DELETED
  1392. return Type() == TExprNode::List && ShallBeDisclosed;
  1393. }
  1394. void SetDisclosing() {
  1395. ENSURE_NOT_DELETED
  1396. Y_ENSURE(Type() == TExprNode::List, "Must be list.");
  1397. ShallBeDisclosed = true;
  1398. }
  1399. ui32 GetFlagsToCompare() const {
  1400. ENSURE_NOT_DELETED
  1401. ui32 ret = Flags();
  1402. if ((ret & TNodeFlags::BinaryContent) == 0) {
  1403. ret |= TNodeFlags::ArbitraryContent | TNodeFlags::MultilineContent;
  1404. }
  1405. return ret;
  1406. }
  1407. TString Dump() const;
  1408. bool StartsExecution() const {
  1409. ENSURE_NOT_DELETED
  1410. return State == EState::ExecutionComplete
  1411. || State == EState::ExecutionInProgress
  1412. || State == EState::ExecutionRequired
  1413. || State == EState::ExecutionPending;
  1414. }
  1415. bool IsComplete() const {
  1416. YQL_ENSURE(HasLambdaScope);
  1417. return !OuterLambda;
  1418. }
  1419. bool IsLiteralList() const {
  1420. YQL_ENSURE(IsList());
  1421. return LiteralList;
  1422. }
  1423. void SetLiteralList(bool literal) {
  1424. YQL_ENSURE(IsList());
  1425. LiteralList = literal;
  1426. }
  1427. void Ref() {
  1428. ENSURE_NOT_DELETED
  1429. ENSURE_NOT_FROZEN
  1430. Y_ENSURE(RefCount_ < Max<ui32>());
  1431. ++RefCount_;
  1432. }
  1433. void UnRef() {
  1434. ENSURE_NOT_DELETED
  1435. ENSURE_NOT_FROZEN
  1436. if (!--RefCount_) {
  1437. Result.Reset();
  1438. Children_.clear();
  1439. Constraints_.Clear();
  1440. MarkDead();
  1441. }
  1442. }
  1443. ui32 UseCount() const { return RefCount_; }
  1444. bool Unique() const { return 1U == UseCount(); }
  1445. bool Dead() const {
  1446. return ExprFlags_ & TExprFlags::Dead;
  1447. }
  1448. TPositionHandle Pos() const {
  1449. ENSURE_NOT_DELETED
  1450. return Position_;
  1451. }
  1452. TPosition Pos(const TExprContext& ctx) const;
  1453. EType Type() const {
  1454. ENSURE_NOT_DELETED
  1455. return (EType)Type_;
  1456. }
  1457. TListType::size_type ChildrenSize() const {
  1458. ENSURE_NOT_DELETED
  1459. return Children_.size();
  1460. }
  1461. TExprNode* Child(ui32 index) const {
  1462. ENSURE_NOT_DELETED
  1463. Y_ENSURE(index < Children_.size(), "index out of range");
  1464. return Children_[index].Get();
  1465. }
  1466. TPtr ChildPtr(ui32 index) const {
  1467. ENSURE_NOT_DELETED
  1468. Y_ENSURE(index < Children_.size(), "index out of range");
  1469. return Children_[index];
  1470. }
  1471. TPtr& ChildRef(ui32 index) {
  1472. ENSURE_NOT_DELETED
  1473. ENSURE_NOT_FROZEN
  1474. Y_ENSURE(index < Children_.size(), "index out of range");
  1475. return Children_[index];
  1476. }
  1477. const TExprNode& Head() const {
  1478. ENSURE_NOT_DELETED
  1479. Y_ENSURE(!Children_.empty(), "no children");
  1480. return *Children_.front();
  1481. }
  1482. TExprNode& Head() {
  1483. ENSURE_NOT_DELETED
  1484. Y_ENSURE(!Children_.empty(), "no children");
  1485. return *Children_.front();
  1486. }
  1487. TPtr HeadPtr() const {
  1488. ENSURE_NOT_DELETED
  1489. Y_ENSURE(!Children_.empty(), "no children");
  1490. return Children_.front();
  1491. }
  1492. TPtr& HeadRef() {
  1493. ENSURE_NOT_DELETED
  1494. ENSURE_NOT_FROZEN
  1495. Y_ENSURE(!Children_.empty(), "no children");
  1496. return Children_.front();
  1497. }
  1498. const TExprNode& Tail() const {
  1499. ENSURE_NOT_DELETED
  1500. Y_ENSURE(!Children_.empty(), "no children");
  1501. return *Children_.back();
  1502. }
  1503. TExprNode& Tail() {
  1504. ENSURE_NOT_DELETED
  1505. Y_ENSURE(!Children_.empty(), "no children");
  1506. return *Children_.back();
  1507. }
  1508. TPtr TailPtr() const {
  1509. ENSURE_NOT_DELETED
  1510. Y_ENSURE(!Children_.empty(), "no children");
  1511. return Children_.back();
  1512. }
  1513. TPtr& TailRef() {
  1514. ENSURE_NOT_DELETED
  1515. ENSURE_NOT_FROZEN
  1516. Y_ENSURE(!Children_.empty(), "no children");
  1517. return Children_.back();
  1518. }
  1519. TChildrenType Children() const {
  1520. ENSURE_NOT_DELETED
  1521. return TChildrenType(Children_.data(), Children_.size());
  1522. }
  1523. TListType ChildrenList() const {
  1524. ENSURE_NOT_DELETED
  1525. return Children_;
  1526. }
  1527. void ChangeChildrenInplace(TListType&& newChildren) {
  1528. ENSURE_NOT_DELETED
  1529. Children_ = std::move(newChildren);
  1530. }
  1531. template<class F>
  1532. void ForEachChild(const F& visitor) const {
  1533. for (const auto& child : Children_)
  1534. visitor(*child);
  1535. }
  1536. TStringBuf Content() const {
  1537. ENSURE_NOT_DELETED
  1538. return ContentUnchecked();
  1539. }
  1540. ui32 Flags() const {
  1541. ENSURE_NOT_DELETED
  1542. return Flags_;
  1543. }
  1544. void NormalizeAtomFlags(const TExprNode& otherAtom) {
  1545. ENSURE_NOT_DELETED
  1546. ENSURE_NOT_FROZEN
  1547. Y_ENSURE(Type_ == Atom && otherAtom.Type_ == Atom, "Expected atoms");
  1548. Y_ENSURE((Flags_ & TNodeFlags::BinaryContent) ==
  1549. (otherAtom.Flags_ & TNodeFlags::BinaryContent), "Mismatch binary atom flags");
  1550. if (!(Flags_ & TNodeFlags::BinaryContent)) {
  1551. Flags_ = Min(Flags_, otherAtom.Flags_);
  1552. }
  1553. }
  1554. ui64 UniqueId() const {
  1555. ENSURE_NOT_DELETED
  1556. return UniqueId_;
  1557. }
  1558. const TConstraintNode* GetConstraint(TStringBuf name) const {
  1559. ENSURE_NOT_DELETED
  1560. Y_ENSURE(static_cast<EState>(State) >= EState::ConstrComplete);
  1561. return Constraints_.GetConstraint(name);
  1562. }
  1563. template <class TConstraintType>
  1564. const TConstraintType* GetConstraint() const {
  1565. ENSURE_NOT_DELETED
  1566. Y_ENSURE(static_cast<EState>(State) >= EState::ConstrComplete);
  1567. return Constraints_.GetConstraint<TConstraintType>();
  1568. }
  1569. const TConstraintNode::TListType& GetAllConstraints() const {
  1570. ENSURE_NOT_DELETED
  1571. Y_ENSURE(static_cast<EState>(State) >= EState::ConstrComplete);
  1572. return Constraints_.GetAllConstraints();
  1573. }
  1574. const TConstraintSet& GetConstraintSet() const {
  1575. ENSURE_NOT_DELETED
  1576. Y_ENSURE(static_cast<EState>(State) >= EState::ConstrComplete);
  1577. return Constraints_;
  1578. }
  1579. void AddConstraint(const TConstraintNode* node) {
  1580. ENSURE_NOT_DELETED
  1581. ENSURE_NOT_FROZEN
  1582. Y_ENSURE(static_cast<EState>(State) >= EState::TypeComplete);
  1583. Y_ENSURE(!StartsExecution());
  1584. Constraints_.AddConstraint(node);
  1585. State = EState::ConstrComplete;
  1586. }
  1587. void CopyConstraints(const TExprNode& node) {
  1588. ENSURE_NOT_DELETED
  1589. ENSURE_NOT_FROZEN
  1590. Y_ENSURE(static_cast<EState>(State) >= EState::TypeComplete);
  1591. Constraints_ = node.Constraints_;
  1592. State = EState::ConstrComplete;
  1593. }
  1594. void SetConstraints(const TConstraintSet& constraints) {
  1595. ENSURE_NOT_DELETED
  1596. ENSURE_NOT_FROZEN
  1597. Y_ENSURE(static_cast<EState>(State) >= EState::TypeComplete);
  1598. Constraints_ = constraints;
  1599. State = EState::ConstrComplete;
  1600. }
  1601. static TPtr NewAtom(ui64 uniqueId, TPositionHandle pos, const TStringBuf& content, ui32 flags) {
  1602. return Make(pos, Atom, {}, content, flags, uniqueId);
  1603. }
  1604. static TPtr NewArgument(ui64 uniqueId, TPositionHandle pos, const TStringBuf& name) {
  1605. return Make(pos, Argument, {}, name, 0, uniqueId);
  1606. }
  1607. static TPtr NewArguments(ui64 uniqueId, TPositionHandle pos, TListType&& argNodes) {
  1608. return Make(pos, Arguments, std::move(argNodes), ZeroString, 0, uniqueId);
  1609. }
  1610. static TPtr NewLambda(ui64 uniqueId, TPositionHandle pos, TListType&& lambda) {
  1611. return Make(pos, Lambda, std::move(lambda), ZeroString, 0, uniqueId);
  1612. }
  1613. static TPtr NewLambda(ui64 uniqueId, TPositionHandle pos, TPtr&& args, TListType&& body) {
  1614. TListType lambda(body.size() + 1U);
  1615. lambda.front() = std::move(args);
  1616. std::move(body.rbegin(), body.rend(), lambda.rbegin());
  1617. return NewLambda(uniqueId, pos, std::move(lambda));
  1618. }
  1619. static TPtr NewLambda(ui64 uniqueId, TPositionHandle pos, TPtr&& args, TPtr&& body) {
  1620. TListType children(body ? 2 : 1);
  1621. children.front() = std::move(args);
  1622. if (body) {
  1623. children.back() = std::move(body);
  1624. }
  1625. return NewLambda(uniqueId, pos, std::move(children));
  1626. }
  1627. static TPtr NewWorld(ui64 uniqueId, TPositionHandle pos) {
  1628. return Make(pos, World, {}, {}, 0, uniqueId);
  1629. }
  1630. static TPtr NewList(ui64 uniqueId, TPositionHandle pos, TListType&& children) {
  1631. return Make(pos, List, std::move(children), ZeroString, 0, uniqueId);
  1632. }
  1633. static TPtr NewCallable(ui64 uniqueId, TPositionHandle pos, const TStringBuf& name, TListType&& children) {
  1634. return Make(pos, Callable, std::move(children), name, 0, uniqueId);
  1635. }
  1636. TPtr Clone(ui64 newUniqueId) const {
  1637. ENSURE_NOT_DELETED
  1638. return Make(Position_, (EType)Type_, TListType(Children_), Content(), Flags_, newUniqueId);
  1639. }
  1640. TPtr CloneWithPosition(ui64 newUniqueId, TPositionHandle pos) const {
  1641. ENSURE_NOT_DELETED
  1642. return Make(pos, (EType)Type_, TListType(Children_), Content(), Flags_, newUniqueId);
  1643. }
  1644. static TPtr NewNode(TPositionHandle position, EType type, TListType&& children, const TStringBuf& content, ui32 flags, ui64 uniqueId) {
  1645. return Make(position, type, std::move(children), content, flags, uniqueId);
  1646. }
  1647. TPtr ChangeContent(ui64 newUniqueId, const TStringBuf& content) const {
  1648. ENSURE_NOT_DELETED
  1649. return Make(Position_, (EType)Type_, TListType(Children_), content, Flags_, newUniqueId);
  1650. }
  1651. TPtr ChangeChildren(ui64 newUniqueId, TListType&& children) const {
  1652. ENSURE_NOT_DELETED
  1653. return Make(Position_, (EType)Type_, std::move(children), Content(), Flags_, newUniqueId);
  1654. }
  1655. TPtr ChangeChild(ui64 newUniqueId, ui32 index, TPtr&& child) const {
  1656. ENSURE_NOT_DELETED
  1657. Y_ENSURE(index < Children_.size(), "index out of range");
  1658. TListType newChildren(Children_);
  1659. newChildren[index] = std::move(child);
  1660. return Make(Position_, (EType)Type_, std::move(newChildren), Content(), Flags_, newUniqueId);
  1661. }
  1662. void SetTypeAnn(const TTypeAnnotationNode* typeAnn) {
  1663. TypeAnnotation_ = typeAnn;
  1664. State = TypeAnnotation_ ? EState::TypeComplete : EState::Initial;
  1665. }
  1666. const TTypeAnnotationNode* GetTypeAnn() const {
  1667. return TypeAnnotation_;
  1668. }
  1669. EState GetState() const {
  1670. return State;
  1671. }
  1672. void SetState(EState state) {
  1673. State = state;
  1674. }
  1675. ui32 GetArgIndex() const {
  1676. YQL_ENSURE(Type() == EType::Argument);
  1677. return ArgIndex;
  1678. }
  1679. void SetArgIndex(ui32 argIndex) {
  1680. YQL_ENSURE(Type() == EType::Argument);
  1681. YQL_ENSURE(argIndex <= Max<ui16>());
  1682. ArgIndex = (ui16)argIndex;
  1683. }
  1684. ui64 GetHash() const {
  1685. Y_DEBUG_ABORT_UNLESS(HashAbove == HashBelow);
  1686. return HashAbove;
  1687. }
  1688. void SetHash(ui64 hash) {
  1689. HashAbove = HashBelow = hash;
  1690. }
  1691. ui64 GetHashAbove() const {
  1692. return HashAbove;
  1693. }
  1694. void SetHashAbove(ui64 hash) {
  1695. HashAbove = hash;
  1696. }
  1697. ui64 GetHashBelow() const {
  1698. return HashBelow;
  1699. }
  1700. void SetHashBelow(ui64 hash) {
  1701. HashBelow = hash;
  1702. }
  1703. ui64 GetBloom() const {
  1704. return Bloom;
  1705. }
  1706. void SetBloom(ui64 bloom) {
  1707. Bloom = bloom;
  1708. }
  1709. // return pair of outer and inner lambda.
  1710. std::optional<std::pair<const TExprNode*, const TExprNode*>> GetDependencyScope() const {
  1711. if (HasLambdaScope) {
  1712. return std::make_pair(OuterLambda, InnerLambda);
  1713. }
  1714. return std::nullopt;
  1715. }
  1716. void SetDependencyScope(const TExprNode* outerLambda, const TExprNode* innerLambda) {
  1717. Y_DEBUG_ABORT_UNLESS(outerLambda == innerLambda || outerLambda->GetLambdaLevel() < innerLambda->GetLambdaLevel(), "Wrong scope of closures.");
  1718. HasLambdaScope = 1;
  1719. OuterLambda = outerLambda;
  1720. InnerLambda = innerLambda;
  1721. }
  1722. ui16 GetLambdaLevel() const { return LambdaLevel; }
  1723. void SetLambdaLevel(ui16 lambdaLevel) { LambdaLevel = lambdaLevel; }
  1724. bool IsUsedInDependsOn() const {
  1725. YQL_ENSURE(Type() == EType::Argument);
  1726. return UsedInDependsOn;
  1727. }
  1728. void SetUsedInDependsOn() {
  1729. YQL_ENSURE(Type() == EType::Argument);
  1730. UsedInDependsOn = 1;
  1731. }
  1732. void SetUnorderedChildren() {
  1733. YQL_ENSURE(Type() == EType::List || Type() == EType::Callable);
  1734. UnordChildren = 1;
  1735. }
  1736. bool UnorderedChildren() const {
  1737. YQL_ENSURE(Type() == EType::List || Type() == EType::Callable);
  1738. return bool(UnordChildren);
  1739. }
  1740. ~TExprNode() {
  1741. Y_ABORT_UNLESS(Dead(), "Node (id: %lu, type: %s, content: '%s') not dead on destruction.",
  1742. UniqueId_, ToString(Type_).data(), TString(ContentUnchecked()).data());
  1743. Y_ABORT_UNLESS(!UseCount(), "Node (id: %lu, type: %s, content: '%s') has non-zero use count on destruction.",
  1744. UniqueId_, ToString(Type_).data(), TString(ContentUnchecked()).data());
  1745. }
  1746. private:
  1747. static TPtr Make(TPositionHandle position, EType type, TListType&& children, const TStringBuf& content, ui32 flags, ui64 uniqueId) {
  1748. Y_ENSURE(flags <= TNodeFlags::FlagsMask);
  1749. Y_ENSURE(children.size() <= Max<ui32>());
  1750. Y_ENSURE(content.size() <= Max<ui32>());
  1751. for (size_t i = 0; i < children.size(); ++i) {
  1752. Y_ENSURE(children[i], "Unable to create node " << content << ": " << i << "th child is null");
  1753. }
  1754. return TPtr(new TExprNode(position, type, std::move(children), content.data(), ui32(content.size()), flags, uniqueId));
  1755. }
  1756. TExprNode(TPositionHandle position, EType type, TListType&& children,
  1757. const char* content, ui32 contentSize, ui32 flags, ui64 uniqueId)
  1758. : Children_(std::move(children))
  1759. , Content_(content)
  1760. , UniqueId_(uniqueId)
  1761. , Position_(position)
  1762. , ContentSize(contentSize)
  1763. , Type_(type)
  1764. , Flags_(flags)
  1765. , ExprFlags_(TExprFlags::Default)
  1766. , State(EState::Initial)
  1767. , HasLambdaScope(0)
  1768. , UsedInDependsOn(0)
  1769. , UnordChildren(0)
  1770. , ShallBeDisclosed(0)
  1771. , LiteralList(0)
  1772. {}
  1773. TExprNode(const TExprNode&) = delete;
  1774. TExprNode(TExprNode&&) = delete;
  1775. TExprNode& operator=(const TExprNode&) = delete;
  1776. TExprNode& operator=(TExprNode&&) = delete;
  1777. bool Frozen() const {
  1778. return ExprFlags_ & TExprFlags::Frozen;
  1779. }
  1780. void MarkFrozen(bool frozen = true) {
  1781. if (frozen) {
  1782. ExprFlags_ |= TExprFlags::Frozen;
  1783. } else {
  1784. ExprFlags_ &= ~TExprFlags::Frozen;
  1785. }
  1786. }
  1787. void MarkDead() {
  1788. ExprFlags_ |= TExprFlags::Dead;
  1789. }
  1790. TStringBuf ContentUnchecked() const {
  1791. return TStringBuf(Content_, ContentSize);
  1792. }
  1793. TListType Children_;
  1794. TConstraintSet Constraints_;
  1795. const char* Content_ = nullptr;
  1796. const TExprNode* OuterLambda = nullptr;
  1797. const TExprNode* InnerLambda = nullptr;
  1798. TPtr Result;
  1799. ui64 HashAbove = 0ULL;
  1800. ui64 HashBelow = 0ULL;
  1801. ui64 Bloom = 0ULL;
  1802. const ui64 UniqueId_;
  1803. const TTypeAnnotationNode* TypeAnnotation_ = nullptr;
  1804. const TPositionHandle Position_;
  1805. ui32 RefCount_ = 0U;
  1806. const ui32 ContentSize;
  1807. ui16 ArgIndex = ui16(-1);
  1808. ui16 LambdaLevel = 0; // filled together with OuterLambda
  1809. ui16 IntermediateHashesCount = 0;
  1810. static_assert(TypeMask <= 7, "EType wont fit in 3 bits, increase Type_ bitfield size");
  1811. static_assert(TNodeFlags::FlagsMask <= 7, "TNodeFlags wont fit in 3 bits, increase Flags_ bitfield size");
  1812. static_assert(TExprFlags::FlagsMask <= 3, "TExprFlags wont fit in 2 bits, increase ExprFlags_ bitfield size");
  1813. static_assert(int(EState::Last) <= 16, "EState wont fit in 4 bits, increase State bitfield size");
  1814. struct {
  1815. ui8 Type_ : 3;
  1816. ui8 Flags_ : 3;
  1817. ui8 ExprFlags_ : 2;
  1818. EState State : 4;
  1819. ui8 HasLambdaScope : 1;
  1820. ui8 UsedInDependsOn : 1;
  1821. ui8 UnordChildren : 1;
  1822. ui8 ShallBeDisclosed: 1;
  1823. ui8 LiteralList : 1;
  1824. };
  1825. };
  1826. class TExportTable {
  1827. public:
  1828. using TSymbols = THashMap<TString, TExprNode::TPtr>;
  1829. TExportTable() = default;
  1830. TExportTable(TExprContext& ctx, TSymbols&& symbols)
  1831. : Symbols_(std::move(symbols))
  1832. , Ctx_(&ctx)
  1833. {}
  1834. const TSymbols& Symbols() const {
  1835. return Symbols_;
  1836. }
  1837. TSymbols& Symbols(TExprContext& ctx) {
  1838. if (Ctx_) {
  1839. YQL_ENSURE(Ctx_ == &ctx);
  1840. } else {
  1841. Ctx_ = &ctx;
  1842. }
  1843. return Symbols_;
  1844. }
  1845. TExprContext& ExprCtx() const {
  1846. YQL_ENSURE(Ctx_);
  1847. return *Ctx_;
  1848. }
  1849. private:
  1850. TSymbols Symbols_;
  1851. TExprContext* Ctx_ = nullptr;
  1852. };
  1853. using TModulesTable = THashMap<TString, TExportTable>;
  1854. class IModuleResolver {
  1855. public:
  1856. typedef std::shared_ptr<IModuleResolver> TPtr;
  1857. virtual bool AddFromFile(const std::string_view& file, TExprContext& ctx, ui16 syntaxVersion, ui32 packageVersion, TPosition pos = {}) = 0;
  1858. virtual bool AddFromUrl(const std::string_view& file, const std::string_view& url, const std::string_view& tokenName, TExprContext& ctx, ui16 syntaxVersion, ui32 packageVersion, TPosition pos = {}) = 0;
  1859. virtual bool AddFromMemory(const std::string_view& file, const TString& body, TExprContext& ctx, ui16 syntaxVersion, ui32 packageVersion, TPosition pos = {}) = 0;
  1860. virtual bool AddFromMemory(const std::string_view& file, const TString& body, TExprContext& ctx, ui16 syntaxVersion, ui32 packageVersion, TPosition pos, TString& moduleName, std::vector<TString>* exports = nullptr, std::vector<TString>* imports = nullptr) = 0;
  1861. virtual bool Link(TExprContext& ctx) = 0;
  1862. virtual void UpdateNextUniqueId(TExprContext& ctx) const = 0;
  1863. virtual ui64 GetNextUniqueId() const = 0;
  1864. virtual void RegisterPackage(const TString& package) = 0;
  1865. virtual bool SetPackageDefaultVersion(const TString& package, ui32 version) = 0;
  1866. virtual const TExportTable* GetModule(const TString& module) const = 0;
  1867. virtual void WriteStatistics(NYson::TYsonWriter& writer) = 0;
  1868. /*
  1869. Create new resolver which will use already collected modules in readonly manner.
  1870. Parent resolver should be alive while using child due to raw data sharing.
  1871. */
  1872. virtual IModuleResolver::TPtr CreateMutableChild() const = 0;
  1873. virtual void SetFileAliasPrefix(TString&& prefix) = 0;
  1874. virtual TString GetFileAliasPrefix() const = 0;
  1875. virtual ~IModuleResolver() = default;
  1876. };
  1877. struct TExprStep {
  1878. enum ELevel {
  1879. Params,
  1880. ExpandApplyForLambdas,
  1881. ValidateProviders,
  1882. Configure,
  1883. ExprEval,
  1884. DiscoveryIO,
  1885. Epochs,
  1886. Intents,
  1887. LoadTablesMetadata,
  1888. RewriteIO,
  1889. Recapture,
  1890. LastLevel
  1891. };
  1892. TExprStep()
  1893. {
  1894. }
  1895. void Done(ELevel level) {
  1896. Steps_.Set(level);
  1897. }
  1898. void Reset() {
  1899. Steps_.Reset();
  1900. }
  1901. TExprStep& Repeat(ELevel level) {
  1902. Steps_.Reset(level);
  1903. return *this;
  1904. }
  1905. bool IsDone(ELevel level) {
  1906. return Steps_.Test(level);
  1907. }
  1908. private:
  1909. TEnumBitSet<ELevel, Params, LastLevel> Steps_;
  1910. };
  1911. template <typename T>
  1912. struct TMakeTypeImpl;
  1913. template <class T>
  1914. using TNodeMap = std::unordered_map<const TExprNode*, T>;
  1915. using TNodeSet = std::unordered_set<const TExprNode*>;
  1916. using TNodeOnNodeOwnedMap = TNodeMap<TExprNode::TPtr>;
  1917. using TParentsMap = TNodeMap<TNodeSet>;
  1918. using TNodeMultiSet = std::unordered_multiset<const TExprNode*>;
  1919. using TParentsMultiMap = TNodeMap<TNodeMultiSet>;
  1920. template <>
  1921. struct TMakeTypeImpl<TVoidExprType> {
  1922. static const TVoidExprType* Make(TExprContext& ctx);
  1923. };
  1924. template <>
  1925. struct TMakeTypeImpl<TNullExprType> {
  1926. static const TNullExprType* Make(TExprContext& ctx);
  1927. };
  1928. template <>
  1929. struct TMakeTypeImpl<TEmptyListExprType> {
  1930. static const TEmptyListExprType* Make(TExprContext& ctx);
  1931. };
  1932. template <>
  1933. struct TMakeTypeImpl<TEmptyDictExprType> {
  1934. static const TEmptyDictExprType* Make(TExprContext& ctx);
  1935. };
  1936. template <>
  1937. struct TMakeTypeImpl<TUnitExprType> {
  1938. static const TUnitExprType* Make(TExprContext& ctx);
  1939. };
  1940. template <>
  1941. struct TMakeTypeImpl<TWorldExprType> {
  1942. static const TWorldExprType* Make(TExprContext& ctx);
  1943. };
  1944. template <>
  1945. struct TMakeTypeImpl<TGenericExprType> {
  1946. static const TGenericExprType* Make(TExprContext& ctx);
  1947. };
  1948. template <>
  1949. struct TMakeTypeImpl<TItemExprType> {
  1950. static const TItemExprType* Make(TExprContext& ctx, const TStringBuf& name, const TTypeAnnotationNode* itemType);
  1951. };
  1952. template <>
  1953. struct TMakeTypeImpl<TListExprType> {
  1954. static const TListExprType* Make(TExprContext& ctx, const TTypeAnnotationNode* itemType);
  1955. };
  1956. template <>
  1957. struct TMakeTypeImpl<TOptionalExprType> {
  1958. static const TOptionalExprType* Make(TExprContext& ctx, const TTypeAnnotationNode* itemType);
  1959. };
  1960. template <>
  1961. struct TMakeTypeImpl<TVariantExprType> {
  1962. static const TVariantExprType* Make(TExprContext& ctx, const TTypeAnnotationNode* underlyingType);
  1963. };
  1964. template <>
  1965. struct TMakeTypeImpl<TErrorExprType> {
  1966. static const TErrorExprType* Make(TExprContext& ctx, const TIssue& error);
  1967. };
  1968. template <>
  1969. struct TMakeTypeImpl<TDictExprType> {
  1970. static const TDictExprType* Make(TExprContext& ctx, const TTypeAnnotationNode* keyType,
  1971. const TTypeAnnotationNode* payloadType);
  1972. };
  1973. template <>
  1974. struct TMakeTypeImpl<TTypeExprType> {
  1975. static const TTypeExprType* Make(TExprContext& ctx, const TTypeAnnotationNode* baseType);
  1976. };
  1977. template <>
  1978. struct TMakeTypeImpl<TDataExprType> {
  1979. static const TDataExprType* Make(TExprContext& ctx, EDataSlot slot);
  1980. };
  1981. template <>
  1982. struct TMakeTypeImpl<TPgExprType> {
  1983. static const TPgExprType* Make(TExprContext& ctx, ui32 typeId);
  1984. };
  1985. template <>
  1986. struct TMakeTypeImpl<TDataExprParamsType> {
  1987. static const TDataExprParamsType* Make(TExprContext& ctx, EDataSlot slot, const TStringBuf& one, const TStringBuf& two);
  1988. };
  1989. template <>
  1990. struct TMakeTypeImpl<TCallableExprType> {
  1991. static const TCallableExprType* Make(
  1992. TExprContext& ctx, const TTypeAnnotationNode* returnType, const TVector<TCallableExprType::TArgumentInfo>& arguments,
  1993. size_t optionalArgumentsCount, const TStringBuf& payload);
  1994. };
  1995. template <>
  1996. struct TMakeTypeImpl<TResourceExprType> {
  1997. static const TResourceExprType* Make(TExprContext& ctx, const TStringBuf& tag);
  1998. };
  1999. template <>
  2000. struct TMakeTypeImpl<TTaggedExprType> {
  2001. static const TTaggedExprType* Make(TExprContext& ctx, const TTypeAnnotationNode* baseType, const TStringBuf& tag);
  2002. };
  2003. template <>
  2004. struct TMakeTypeImpl<TStructExprType> {
  2005. static const TStructExprType* Make(TExprContext& ctx, const TVector<const TItemExprType*>& items);
  2006. };
  2007. template <>
  2008. struct TMakeTypeImpl<TTupleExprType> {
  2009. static const TTupleExprType* Make(TExprContext& ctx, const TTypeAnnotationNode::TListType& items);
  2010. };
  2011. template <>
  2012. struct TMakeTypeImpl<TMultiExprType> {
  2013. static const TMultiExprType* Make(TExprContext& ctx, const TTypeAnnotationNode::TListType& items);
  2014. };
  2015. template <>
  2016. struct TMakeTypeImpl<TStreamExprType> {
  2017. static const TStreamExprType* Make(TExprContext& ctx, const TTypeAnnotationNode* itemType);
  2018. };
  2019. template <>
  2020. struct TMakeTypeImpl<TFlowExprType> {
  2021. static const TFlowExprType* Make(TExprContext& ctx, const TTypeAnnotationNode* itemType);
  2022. };
  2023. template <>
  2024. struct TMakeTypeImpl<TBlockExprType> {
  2025. static const TBlockExprType* Make(TExprContext& ctx, const TTypeAnnotationNode* itemType);
  2026. };
  2027. template <>
  2028. struct TMakeTypeImpl<TScalarExprType> {
  2029. static const TScalarExprType* Make(TExprContext& ctx, const TTypeAnnotationNode* itemType);
  2030. };
  2031. using TSingletonTypeCache = std::tuple<
  2032. const TVoidExprType*,
  2033. const TNullExprType*,
  2034. const TUnitExprType*,
  2035. const TEmptyListExprType*,
  2036. const TEmptyDictExprType*,
  2037. const TWorldExprType*,
  2038. const TGenericExprType*,
  2039. const TTupleExprType*,
  2040. const TStructExprType*,
  2041. const TMultiExprType*
  2042. >;
  2043. struct TExprContext : private TNonCopyable {
  2044. class TFreezeGuard {
  2045. public:
  2046. TFreezeGuard(const TFreezeGuard&) = delete;
  2047. TFreezeGuard& operator=(const TFreezeGuard&) = delete;
  2048. TFreezeGuard(TExprContext& ctx)
  2049. : Ctx(ctx)
  2050. {
  2051. Ctx.Freeze();
  2052. }
  2053. ~TFreezeGuard() {
  2054. Ctx.UnFreeze();
  2055. }
  2056. private:
  2057. TExprContext& Ctx;
  2058. };
  2059. TIssueManager IssueManager;
  2060. TNodeMap<TIssues> AssociativeIssues;
  2061. TMemoryPool StringPool;
  2062. std::unordered_set<std::string_view> Strings;
  2063. std::unordered_map<ui32, std::string_view> Indexes;
  2064. std::stack<std::unique_ptr<const TTypeAnnotationNode>> TypeNodes;
  2065. std::stack<std::unique_ptr<const TConstraintNode>> ConstraintNodes;
  2066. std::deque<std::unique_ptr<TExprNode>> ExprNodes;
  2067. TSingletonTypeCache SingletonTypeCache;
  2068. std::unordered_set<const TTypeAnnotationNode*, TTypeAnnotationNode::THash, TTypeAnnotationNode::TEqual> TypeSet;
  2069. std::unordered_set<const TConstraintNode*, TConstraintNode::THash, TConstraintNode::TEqual> ConstraintSet;
  2070. std::unordered_map<const TTypeAnnotationNode*, TExprNode::TPtr> TypeAsNodeCache;
  2071. std::unordered_set<TStringBuf, THash<TStringBuf>> DisabledConstraints;
  2072. ui64 NextUniqueId = 0;
  2073. ui64 NodeAllocationCounter = 0;
  2074. ui64 NodesAllocationLimit = 3000000;
  2075. ui64 StringsAllocationLimit = 100000000;
  2076. ui64 RepeatTransformLimit = 1000000;
  2077. ui64 RepeatTransformCounter = 0;
  2078. ui64 TypeAnnNodeRepeatLimit = 1000;
  2079. TGcNodeConfig GcConfig;
  2080. std::unordered_multimap<ui64, TExprNode*> UniqueNodes;
  2081. TExprStep Step;
  2082. bool Frozen;
  2083. explicit TExprContext(ui64 nextUniqueId = 0ULL);
  2084. ~TExprContext();
  2085. ui64 AllocateNextUniqueId() {
  2086. ENSURE_NOT_FROZEN_CTX
  2087. const auto ret = ++NextUniqueId;
  2088. return ret;
  2089. }
  2090. TStringBuf AppendString(const TStringBuf& buf) {
  2091. ENSURE_NOT_FROZEN_CTX
  2092. if (buf.size() == 0) {
  2093. return ZeroString;
  2094. }
  2095. auto it = Strings.find(buf);
  2096. if (it != Strings.end()) {
  2097. return *it;
  2098. }
  2099. auto newBuf = StringPool.AppendString(buf);
  2100. Strings.insert(it, newBuf);
  2101. return newBuf;
  2102. }
  2103. TPositionHandle AppendPosition(const TPosition& pos);
  2104. TPosition GetPosition(TPositionHandle handle) const;
  2105. TExprNodeBuilder Builder(TPositionHandle pos) {
  2106. return TExprNodeBuilder(pos, *this);
  2107. }
  2108. [[nodiscard]]
  2109. TExprNode::TPtr RenameNode(const TExprNode& node, const TStringBuf& name);
  2110. [[nodiscard]]
  2111. TExprNode::TPtr ShallowCopy(const TExprNode& node);
  2112. [[nodiscard]]
  2113. TExprNode::TPtr ShallowCopyWithPosition(const TExprNode& node, TPositionHandle pos);
  2114. [[nodiscard]]
  2115. TExprNode::TPtr ChangeChildren(const TExprNode& node, TExprNode::TListType&& children);
  2116. [[nodiscard]]
  2117. TExprNode::TPtr ChangeChild(const TExprNode& node, ui32 index, TExprNode::TPtr&& child);
  2118. [[nodiscard]]
  2119. TExprNode::TPtr ExactChangeChildren(const TExprNode& node, TExprNode::TListType&& children);
  2120. [[nodiscard]]
  2121. TExprNode::TPtr ExactShallowCopy(const TExprNode& node);
  2122. [[nodiscard]]
  2123. TExprNode::TPtr DeepCopyLambda(const TExprNode& node, TExprNode::TListType&& body);
  2124. [[nodiscard]]
  2125. TExprNode::TPtr DeepCopyLambda(const TExprNode& node, TExprNode::TPtr&& body = TExprNode::TPtr());
  2126. [[nodiscard]]
  2127. TExprNode::TPtr FuseLambdas(const TExprNode& outer, const TExprNode& inner);
  2128. using TCustomDeepCopier = std::function<bool(const TExprNode& node, TExprNode::TListType& newChildren)>;
  2129. [[nodiscard]]
  2130. TExprNode::TPtr DeepCopy(const TExprNode& node, TExprContext& nodeContext, TNodeOnNodeOwnedMap& deepClones,
  2131. bool internStrings, bool copyTypes, bool copyResult = false, TCustomDeepCopier customCopier = {});
  2132. [[nodiscard]]
  2133. TExprNode::TPtr SwapWithHead(const TExprNode& node);
  2134. TExprNode::TPtr ReplaceNode(TExprNode::TPtr&& start, const TExprNode& src, TExprNode::TPtr dst);
  2135. TExprNode::TPtr ReplaceNodes(TExprNode::TPtr&& start, const TNodeOnNodeOwnedMap& replaces);
  2136. template<bool KeepTypeAnns = false>
  2137. TExprNode::TListType ReplaceNodes(TExprNode::TListType&& start, const TNodeOnNodeOwnedMap& replaces);
  2138. TExprNode::TPtr NewAtom(TPositionHandle pos, const TStringBuf& content, ui32 flags = TNodeFlags::ArbitraryContent) {
  2139. ++NodeAllocationCounter;
  2140. const auto node = TExprNode::NewAtom(AllocateNextUniqueId(), pos, AppendString(content), flags);
  2141. ExprNodes.emplace_back(node.Get());
  2142. return node;
  2143. }
  2144. TExprNode::TPtr NewAtom(TPositionHandle pos, ui32 index) {
  2145. ++NodeAllocationCounter;
  2146. const auto node = TExprNode::NewAtom(AllocateNextUniqueId(), pos, GetIndexAsString(index), TNodeFlags::Default);
  2147. ExprNodes.emplace_back(node.Get());
  2148. return node;
  2149. }
  2150. TExprNode::TPtr NewArgument(TPositionHandle pos, const TStringBuf& name) {
  2151. ++NodeAllocationCounter;
  2152. const auto node = TExprNode::NewArgument(AllocateNextUniqueId(), pos, AppendString(name));
  2153. ExprNodes.emplace_back(node.Get());
  2154. return node;
  2155. }
  2156. TExprNode::TPtr NewArguments(TPositionHandle pos, TExprNode::TListType&& argNodes) {
  2157. ++NodeAllocationCounter;
  2158. const auto node = TExprNode::NewArguments(AllocateNextUniqueId(), pos, std::move(argNodes));
  2159. ExprNodes.emplace_back(node.Get());
  2160. return node;
  2161. }
  2162. TExprNode::TPtr NewLambda(TPositionHandle pos, TExprNode::TListType&& lambda) {
  2163. ++NodeAllocationCounter;
  2164. const auto node = TExprNode::NewLambda(AllocateNextUniqueId(), pos, std::move(lambda));
  2165. ExprNodes.emplace_back(node.Get());
  2166. return node;
  2167. }
  2168. TExprNode::TPtr NewLambda(TPositionHandle pos, TExprNode::TPtr&& args, TExprNode::TListType&& body) {
  2169. ++NodeAllocationCounter;
  2170. const auto node = TExprNode::NewLambda(AllocateNextUniqueId(), pos, std::move(args), std::move(body));
  2171. ExprNodes.emplace_back(node.Get());
  2172. return node;
  2173. }
  2174. TExprNode::TPtr NewLambda(TPositionHandle pos, TExprNode::TPtr&& args, TExprNode::TPtr&& body) {
  2175. ++NodeAllocationCounter;
  2176. const auto node = TExprNode::NewLambda(AllocateNextUniqueId(), pos, std::move(args), std::move(body));
  2177. ExprNodes.emplace_back(node.Get());
  2178. return node;
  2179. }
  2180. TExprNode::TPtr NewWorld(TPositionHandle pos) {
  2181. ++NodeAllocationCounter;
  2182. const auto node = TExprNode::NewWorld(AllocateNextUniqueId(), pos);
  2183. ExprNodes.emplace_back(node.Get());
  2184. return node;
  2185. }
  2186. TExprNode::TPtr NewList(TPositionHandle pos, TExprNode::TListType&& children) {
  2187. ++NodeAllocationCounter;
  2188. const auto node = TExprNode::NewList(AllocateNextUniqueId(), pos, std::move(children));
  2189. ExprNodes.emplace_back(node.Get());
  2190. return node;
  2191. }
  2192. TExprNode::TPtr NewCallable(TPositionHandle pos, const TStringBuf& name, TExprNode::TListType&& children) {
  2193. ++NodeAllocationCounter;
  2194. const auto node = TExprNode::NewCallable(AllocateNextUniqueId(), pos, AppendString(name), std::move(children));
  2195. ExprNodes.emplace_back(node.Get());
  2196. return node;
  2197. }
  2198. TExprNode::TPtr NewAtom(TPosition pos, const TStringBuf& content, ui32 flags = TNodeFlags::ArbitraryContent) {
  2199. return NewAtom(AppendPosition(pos), content, flags);
  2200. }
  2201. TExprNode::TPtr NewAtom(TPosition pos, ui32 index) {
  2202. return NewAtom(AppendPosition(pos), index);
  2203. }
  2204. TExprNode::TPtr NewArgument(TPosition pos, const TStringBuf& name) {
  2205. return NewArgument(AppendPosition(pos), name);
  2206. }
  2207. TExprNode::TPtr NewArguments(TPosition pos, TExprNode::TListType&& argNodes) {
  2208. return NewArguments(AppendPosition(pos), std::move(argNodes));
  2209. }
  2210. TExprNode::TPtr NewLambda(TPosition pos, TExprNode::TListType&& lambda) {
  2211. return NewLambda(AppendPosition(pos), std::move(lambda));
  2212. }
  2213. TExprNode::TPtr NewLambda(TPosition pos, TExprNode::TPtr&& args, TExprNode::TListType&& body) {
  2214. return NewLambda(AppendPosition(pos), std::move(args), std::move(body));
  2215. }
  2216. TExprNode::TPtr NewLambda(TPosition pos, TExprNode::TPtr&& args, TExprNode::TPtr&& body) {
  2217. return NewLambda(AppendPosition(pos), std::move(args), std::move(body));
  2218. }
  2219. TExprNode::TPtr NewWorld(TPosition pos) {
  2220. return NewWorld(AppendPosition(pos));
  2221. }
  2222. TExprNode::TPtr NewList(TPosition pos, TExprNode::TListType&& children) {
  2223. return NewList(AppendPosition(pos), std::move(children));
  2224. }
  2225. TExprNode::TPtr NewCallable(TPosition pos, const TStringBuf& name, TExprNode::TListType&& children) {
  2226. return NewCallable(AppendPosition(pos), name, std::move(children));
  2227. }
  2228. TExprNode::TPtr WrapByCallableIf(bool condition, const TStringBuf& callable, TExprNode::TPtr&& node);
  2229. template <typename T, typename... Args>
  2230. const T* MakeType(Args&&... args);
  2231. template <typename T, typename... Args>
  2232. const T* MakeConstraint(Args&&... args);
  2233. TConstraintSet MakeConstraintSet(const NYT::TNode& serializedConstraints);
  2234. void AddError(const TIssue& error) {
  2235. ENSURE_NOT_FROZEN_CTX
  2236. IssueManager.RaiseIssue(error);
  2237. }
  2238. bool AddWarning(const TIssue& warning) {
  2239. ENSURE_NOT_FROZEN_CTX
  2240. return IssueManager.RaiseWarning(warning);
  2241. }
  2242. void Freeze();
  2243. void UnFreeze();
  2244. void Reset();
  2245. template <class TConstraint>
  2246. bool IsConstraintEnabled() const {
  2247. return DisabledConstraints.find(TConstraint::Name()) == DisabledConstraints.end();
  2248. }
  2249. std::string_view GetIndexAsString(ui32 index);
  2250. private:
  2251. using TPositionHandleEqualPred = std::function<bool(TPositionHandle, TPositionHandle)>;
  2252. using TPositionHandleHasher = std::function<size_t(TPositionHandle)>;
  2253. bool IsEqual(TPositionHandle a, TPositionHandle b) const;
  2254. size_t GetHash(TPositionHandle p) const;
  2255. std::unordered_set<TPositionHandle, TPositionHandleHasher, TPositionHandleEqualPred> PositionSet;
  2256. std::deque<TPosition> Positions;
  2257. };
  2258. template <typename T, typename... Args>
  2259. inline const T* TExprContext::MakeConstraint(Args&&... args) {
  2260. ENSURE_NOT_FROZEN_CTX
  2261. if (!IsConstraintEnabled<T>()) {
  2262. return nullptr;
  2263. }
  2264. T sample(*this, std::forward<Args>(args)...);
  2265. const auto it = ConstraintSet.find(&sample);
  2266. if (ConstraintSet.cend() != it) {
  2267. return static_cast<const T*>(*it);
  2268. }
  2269. ConstraintNodes.emplace(new T(std::move(sample)));
  2270. const auto ins = ConstraintSet.emplace(ConstraintNodes.top().get());
  2271. return static_cast<const T*>(*ins.first);
  2272. }
  2273. #undef ENSURE_NOT_DELETED
  2274. #undef ENSURE_NOT_FROZEN
  2275. #undef ENSURE_NOT_FROZEN_CTX
  2276. inline bool IsSameAnnotation(const TTypeAnnotationNode& left, const TTypeAnnotationNode& right) {
  2277. return &left == &right;
  2278. }
  2279. template <typename T, typename... Args>
  2280. const T* TExprContext::MakeType(Args&&... args) {
  2281. return TMakeTypeImpl<T>::Make(*this, std::forward<Args>(args)...);
  2282. }
  2283. struct TExprAnnotationFlags {
  2284. enum {
  2285. None = 0x00,
  2286. Position = 0x01,
  2287. Types = 0x02
  2288. };
  2289. };
  2290. ///////////////////////////////////////////////////////////////////////////////
  2291. // TNodeException
  2292. ///////////////////////////////////////////////////////////////////////////////
  2293. class TNodeException: public yexception {
  2294. public:
  2295. TNodeException();
  2296. explicit TNodeException(const TExprNode& node);
  2297. explicit TNodeException(const TExprNode* node);
  2298. explicit TNodeException(const TPositionHandle& pos);
  2299. inline const TPositionHandle& Pos() const {
  2300. return Pos_;
  2301. }
  2302. private:
  2303. const TPositionHandle Pos_;
  2304. };
  2305. bool CompileExpr(TAstNode& astRoot, TExprNode::TPtr& exprRoot, TExprContext& ctx,
  2306. IModuleResolver* resolver, IUrlListerManager* urlListerManager,
  2307. bool hasAnnotations = false, ui32 typeAnnotationIndex = Max<ui32>(), ui16 syntaxVersion = 0);
  2308. bool CompileExpr(TAstNode& astRoot, TExprNode::TPtr& exprRoot, TExprContext& ctx,
  2309. IModuleResolver* resolver, IUrlListerManager* urlListerManager,
  2310. ui32 annotationFlags, ui16 syntaxVersion = 0);
  2311. struct TLibraryCohesion {
  2312. TExportTable Exports;
  2313. TNodeMap<std::pair<TString, TString>> Imports;
  2314. };
  2315. bool CompileExpr(TAstNode& astRoot, TLibraryCohesion& cohesion, TExprContext& ctx, ui16 syntaxVersion = 0);
  2316. const TTypeAnnotationNode* CompileTypeAnnotation(const TAstNode& node, TExprContext& ctx);
  2317. // validate consistency of arguments and lambdas
  2318. void CheckArguments(const TExprNode& root);
  2319. void CheckCounts(const TExprNode& root);
  2320. // Compare expression trees and return first diffrent nodes.
  2321. bool CompareExprTrees(const TExprNode*& one, const TExprNode*& two);
  2322. bool CompareExprTreeParts(const TExprNode& one, const TExprNode& two, const TNodeMap<ui32>& argsMap);
  2323. TString MakeCacheKey(const TExprNode& root);
  2324. void GatherParents(const TExprNode& node, TParentsMap& parentsMap);
  2325. struct TConvertToAstSettings {
  2326. ui32 AnnotationFlags = 0;
  2327. bool RefAtoms = false;
  2328. std::function<bool(const TExprNode&)> NoInlineFunc;
  2329. bool PrintArguments = false;
  2330. bool AllowFreeArgs = false;
  2331. bool NormalizeAtomFlags = false;
  2332. IAllocator* Allocator = TDefaultAllocator::Instance();
  2333. };
  2334. TAstParseResult ConvertToAst(const TExprNode& root, TExprContext& ctx, const TConvertToAstSettings& settings);
  2335. // refAtoms allows omit copying of atom bodies - they will be referenced from expr graph
  2336. TAstParseResult ConvertToAst(const TExprNode& root, TExprContext& ctx, ui32 annotationFlags, bool refAtoms);
  2337. TExprNode::TListType GetLambdaBody(const TExprNode& lambda);
  2338. TString SubstParameters(const TString& str, const TMaybe<NYT::TNode>& params, TSet<TString>* usedNames);
  2339. const TTypeAnnotationNode* GetSeqItemType(const TTypeAnnotationNode* seq);
  2340. const TTypeAnnotationNode& GetSeqItemType(const TTypeAnnotationNode& seq);
  2341. const TTypeAnnotationNode& RemoveOptionality(const TTypeAnnotationNode& type);
  2342. TMaybe<TIssue> NormalizeName(TPosition position, TString& name);
  2343. TString NormalizeName(const TStringBuf& name);
  2344. } // namespace NYql
  2345. template<>
  2346. inline void Out<NYql::TTypeAnnotationNode>(
  2347. IOutputStream &out, const NYql::TTypeAnnotationNode& type)
  2348. {
  2349. type.Out(out);
  2350. }
  2351. #include "yql_expr_builder.inl"