yql_expr.h 87 KB

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