yql_expr_constraint.cpp 188 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654
  1. #include "yql_expr_constraint.h"
  2. #include "yql_callable_transform.h"
  3. #include "yql_opt_utils.h"
  4. #include <yql/essentials/core/expr_nodes/yql_expr_nodes.h>
  5. #include <yql/essentials/core/yql_type_helpers.h>
  6. #include <yql/essentials/core/yql_join.h>
  7. #include <yql/essentials/utils/log/profile.h>
  8. #include <library/cpp/yson/node/node_io.h>
  9. #include <util/generic/scope.h>
  10. #include <util/generic/utility.h>
  11. #include <util/generic/algorithm.h>
  12. #include <util/string/builder.h>
  13. #include <util/string/type.h>
  14. namespace NYql {
  15. using namespace NNodes;
  16. namespace {
  17. template <size_t FromChild, class... Other>
  18. struct TApplyConstraintFromInput;
  19. template <size_t FromChild>
  20. struct TApplyConstraintFromInput<FromChild> {
  21. static void Do(const TExprNode::TPtr&) {
  22. }
  23. };
  24. template <size_t FromChild, class TConstraint, class... Other>
  25. struct TApplyConstraintFromInput<FromChild, TConstraint, Other...> {
  26. static void Do(const TExprNode::TPtr& input) {
  27. if (auto c = input->Child(FromChild)->GetConstraint<TConstraint>()) {
  28. input->AddConstraint(c);
  29. }
  30. TApplyConstraintFromInput<FromChild, Other...>::Do(input);
  31. }
  32. };
  33. template <class TConstraint>
  34. const TConstraint* MakeCommonConstraint(const TExprNode::TPtr& input, size_t from, TExprContext& ctx) {
  35. TVector<const TConstraintSet*> constraints;
  36. for (size_t i = from; i < input->ChildrenSize(); ++i) {
  37. constraints.push_back(&input->Child(i)->GetConstraintSet());
  38. }
  39. return TConstraint::MakeCommon(constraints, ctx);
  40. }
  41. template <class... Other>
  42. struct TApplyCommonConstraint;
  43. template <class TConstraint>
  44. struct TApplyCommonConstraint<TConstraint> {
  45. static void Do(const TExprNode::TPtr& input, const std::vector<const TConstraintSet*>& constraints, TExprContext& ctx) {
  46. if (auto c = TConstraint::MakeCommon(constraints, ctx)) {
  47. input->AddConstraint(c);
  48. }
  49. }
  50. };
  51. template <class TConstraint, class... Other>
  52. struct TApplyCommonConstraint<TConstraint, Other...> {
  53. static void Do(const TExprNode::TPtr& input, const std::vector<const TConstraintSet*>& constraints, TExprContext& ctx) {
  54. if (auto c = TConstraint::MakeCommon(constraints, ctx)) {
  55. input->AddConstraint(c);
  56. }
  57. TApplyCommonConstraint<Other...>::Do(input, constraints, ctx);
  58. }
  59. };
  60. class TCallableConstraintTransformer : public TCallableTransformerBase<TCallableConstraintTransformer> {
  61. using THandler = TStatus(TCallableConstraintTransformer::*)(const TExprNode::TPtr&, TExprNode::TPtr&, TExprContext&) const;
  62. public:
  63. TCallableConstraintTransformer(TTypeAnnotationContext& types, bool instantOnly, bool subGraph)
  64. : TCallableTransformerBase<TCallableConstraintTransformer>(types, instantOnly)
  65. , SubGraph(subGraph)
  66. {
  67. Functions["FailMe"] = &TCallableConstraintTransformer::FailMe;
  68. Functions["Unordered"] = &TCallableConstraintTransformer::FromFirst<TEmptyConstraintNode, TUniqueConstraintNode, TDistinctConstraintNode, TVarIndexConstraintNode, TMultiConstraintNode>;
  69. Functions["UnorderedSubquery"] = &TCallableConstraintTransformer::FromFirst<TEmptyConstraintNode, TUniqueConstraintNode, TDistinctConstraintNode, TVarIndexConstraintNode, TMultiConstraintNode>;
  70. Functions["Sort"] = &TCallableConstraintTransformer::SortWrap;
  71. Functions["AssumeSorted"] = &TCallableConstraintTransformer::SortWrap;
  72. Functions["AssumeUnique"] = &TCallableConstraintTransformer::AssumeUniqueWrap<false, true>;
  73. Functions["AssumeDistinct"] = &TCallableConstraintTransformer::AssumeUniqueWrap<true, true>;
  74. Functions["AssumeUniqueHint"] = &TCallableConstraintTransformer::AssumeUniqueWrap<false, false>;
  75. Functions["AssumeDistinctHint"] = &TCallableConstraintTransformer::AssumeUniqueWrap<true, false>;
  76. Functions["AssumeConstraints"] = &TCallableConstraintTransformer::AssumeConstraintsWrap;
  77. Functions["AssumeChopped"] = &TCallableConstraintTransformer::AssumeChoppedWrap;
  78. Functions["AssumeColumnOrder"] = &TCallableConstraintTransformer::CopyAllFrom<0>;
  79. Functions["AssumeAllMembersNullableAtOnce"] = &TCallableConstraintTransformer::CopyAllFrom<0>;
  80. Functions["Top"] = &TCallableConstraintTransformer::TopWrap<false>;
  81. Functions["TopSort"] = &TCallableConstraintTransformer::TopWrap<true>;
  82. Functions["TakeWhile"] = &TCallableConstraintTransformer::FilterWrap<true>;
  83. Functions["SkipWhile"] = &TCallableConstraintTransformer::FilterWrap<true>;
  84. Functions["TakeWhileInclusive"] = &TCallableConstraintTransformer::FilterWrap<true>;
  85. Functions["SkipWhileInclusive"] = &TCallableConstraintTransformer::FilterWrap<true>;
  86. Functions["WideTakeWhile"] = &TCallableConstraintTransformer::FilterWrap<true>;
  87. Functions["WideSkipWhile"] = &TCallableConstraintTransformer::FilterWrap<true>;
  88. Functions["WideTakeWhileInclusive"] = &TCallableConstraintTransformer::FilterWrap<true>;
  89. Functions["WideSkipWhileInclusive"] = &TCallableConstraintTransformer::FilterWrap<true>;
  90. Functions["Iterator"] = &TCallableConstraintTransformer::CopyAllFrom<0>;
  91. Functions["ForwardList"] = &TCallableConstraintTransformer::CopyAllFrom<0>;
  92. Functions["LazyList"] = &TCallableConstraintTransformer::CopyAllFrom<0>;
  93. Functions["TableSource"] = &TCallableConstraintTransformer::CopyAllFrom<0>;
  94. Functions["WideTableSource"] = &TCallableConstraintTransformer::CopyAllFrom<0>;
  95. Functions["ToFlow"] = &TCallableConstraintTransformer::CopyAllFrom<0>;
  96. Functions["FromFlow"] = &TCallableConstraintTransformer::CopyAllFrom<0>;
  97. Functions["ToStream"] = &TCallableConstraintTransformer::CopyAllFrom<0>;
  98. Functions["ToSequence"] = &TCallableConstraintTransformer::CopyAllFrom<0>;
  99. Functions["Collect"] = &TCallableConstraintTransformer::CopyAllFrom<0>;
  100. Functions["FilterNullMembers"] = &TCallableConstraintTransformer::FromFirst<TSortedConstraintNode, TPartOfSortedConstraintNode, TChoppedConstraintNode, TPartOfChoppedConstraintNode, TEmptyConstraintNode, TUniqueConstraintNode, TPartOfUniqueConstraintNode, TDistinctConstraintNode, TPartOfDistinctConstraintNode, TVarIndexConstraintNode>;
  101. Functions["SkipNullMembers"] = &TCallableConstraintTransformer::FromFirst<TSortedConstraintNode, TPartOfSortedConstraintNode, TChoppedConstraintNode, TPartOfChoppedConstraintNode, TEmptyConstraintNode, TUniqueConstraintNode, TPartOfUniqueConstraintNode, TDistinctConstraintNode, TPartOfDistinctConstraintNode, TVarIndexConstraintNode>;
  102. Functions["FilterNullElements"] = &TCallableConstraintTransformer::FromFirst<TSortedConstraintNode, TPartOfSortedConstraintNode, TChoppedConstraintNode, TPartOfChoppedConstraintNode, TEmptyConstraintNode, TUniqueConstraintNode, TPartOfUniqueConstraintNode, TDistinctConstraintNode, TPartOfDistinctConstraintNode, TVarIndexConstraintNode>;
  103. Functions["SkipNullElements"] = &TCallableConstraintTransformer::FromFirst<TSortedConstraintNode, TPartOfSortedConstraintNode, TChoppedConstraintNode, TPartOfChoppedConstraintNode, TEmptyConstraintNode, TUniqueConstraintNode, TPartOfUniqueConstraintNode, TDistinctConstraintNode, TPartOfDistinctConstraintNode, TVarIndexConstraintNode>;
  104. Functions["Right!"] = &TCallableConstraintTransformer::CopyAllFrom<0>;
  105. Functions["Cons!"] = &TCallableConstraintTransformer::CopyAllFrom<1>;
  106. Functions["ExtractMembers"] = &TCallableConstraintTransformer::ExtractMembersWrap;
  107. Functions["RemoveSystemMembers"] = &TCallableConstraintTransformer::RemovePrefixMembersWrap;
  108. Functions["RemovePrefixMembers"] = &TCallableConstraintTransformer::RemovePrefixMembersWrap;
  109. Functions["FlattenMembers"] = &TCallableConstraintTransformer::FlattenMembersWrap;
  110. Functions["SelectMembers"] = &TCallableConstraintTransformer::SelectMembersWrap;
  111. Functions["FilterMembers"] = &TCallableConstraintTransformer::SelectMembersWrap;
  112. Functions["CastStruct"] = &TCallableConstraintTransformer::SelectMembersWrap;
  113. Functions["SafeCast"] = &TCallableConstraintTransformer::CastWrap<false>;
  114. Functions["StrictCast"] = &TCallableConstraintTransformer::CastWrap<true>;
  115. Functions["ToString"] = &TCallableConstraintTransformer::CastWrap<true>;
  116. Functions["ToBytes"] = &TCallableConstraintTransformer::CastWrap<true>;
  117. Functions["DivePrefixMembers"] = &TCallableConstraintTransformer::DivePrefixMembersWrap;
  118. Functions["OrderedFilter"] = &TCallableConstraintTransformer::FilterWrap<true>;
  119. Functions["Filter"] = &TCallableConstraintTransformer::FilterWrap<false>;
  120. Functions["WideFilter"] = &TCallableConstraintTransformer::FilterWrap<true>;
  121. Functions["OrderedMap"] = &TCallableConstraintTransformer::MapWrap<true, false>;
  122. Functions["Map"] = &TCallableConstraintTransformer::MapWrap<false, false>;
  123. Functions["MapNext"] = &TCallableConstraintTransformer::MapWrap<true, false>;
  124. Functions["OrderedFlatMap"] = &TCallableConstraintTransformer::MapWrap<true, true>;
  125. Functions["FlatMap"] = &TCallableConstraintTransformer::MapWrap<false, true>;
  126. Functions["OrderedMultiMap"] = &TCallableConstraintTransformer::MapWrap<true, false>;
  127. Functions["MultiMap"] = &TCallableConstraintTransformer::MapWrap<false, false>;
  128. Functions["ExpandMap"] = &TCallableConstraintTransformer::MapWrap<true, false, false, true>;
  129. Functions["WideMap"] = &TCallableConstraintTransformer::MapWrap<true, false, true, true>;
  130. Functions["NarrowMap"] = &TCallableConstraintTransformer::MapWrap<true, false, true, false>;
  131. Functions["NarrowFlatMap"] = &TCallableConstraintTransformer::MapWrap<true, true, true, false>;
  132. Functions["NarrowMultiMap"] = &TCallableConstraintTransformer::MapWrap<true, false, true, false>;
  133. Functions["OrderedFlatMapToEquiJoin"] = &TCallableConstraintTransformer::MapWrap<true, true>;
  134. Functions["FlatMapToEquiJoin"] = &TCallableConstraintTransformer::MapWrap<false, true>;
  135. Functions["OrderedLMap"] = &TCallableConstraintTransformer::LMapWrap<true>;
  136. Functions["LMap"] = &TCallableConstraintTransformer::LMapWrap<false>;
  137. Functions["Extract"] = &TCallableConstraintTransformer::FromFirst<TEmptyConstraintNode>;
  138. Functions["OrderedExtract"] = &TCallableConstraintTransformer::FromFirst<TEmptyConstraintNode>;
  139. Functions["OrderedExtend"] = &TCallableConstraintTransformer::ExtendWrap<true>;
  140. Functions["Extend"] = &TCallableConstraintTransformer::ExtendWrap<false>;
  141. Functions["UnionAll"] = &TCallableConstraintTransformer::ExtendWrap<false>;
  142. Functions["Merge"] = &TCallableConstraintTransformer::MergeWrap<false>;
  143. Functions["UnionMerge"] = &TCallableConstraintTransformer::MergeWrap<true>;
  144. Functions["Skip"] = &TCallableConstraintTransformer::CopyAllFrom<0>;
  145. Functions["Take"] = &TCallableConstraintTransformer::TakeWrap;
  146. Functions["Limit"] = &TCallableConstraintTransformer::TakeWrap;
  147. Functions["Member"] = &TCallableConstraintTransformer::MemberWrap;
  148. Functions["AsStruct"] = &TCallableConstraintTransformer::AsStructWrap;
  149. Functions["BlockAsStruct"] = &TCallableConstraintTransformer::AsStructWrap;
  150. Functions["Just"] = &TCallableConstraintTransformer::FromFirst<TUniqueConstraintNode, TPartOfUniqueConstraintNode, TDistinctConstraintNode, TPartOfDistinctConstraintNode, TPartOfSortedConstraintNode, TPartOfChoppedConstraintNode, TVarIndexConstraintNode, TMultiConstraintNode>;
  151. Functions["Unwrap"] = &TCallableConstraintTransformer::FromFirst<TUniqueConstraintNode, TPartOfUniqueConstraintNode, TDistinctConstraintNode, TPartOfDistinctConstraintNode, TPartOfSortedConstraintNode, TPartOfChoppedConstraintNode, TVarIndexConstraintNode, TMultiConstraintNode>;
  152. Functions["Ensure"] = &TCallableConstraintTransformer::CopyAllFrom<0>;
  153. Functions["ToList"] = &TCallableConstraintTransformer::FromFirst<TEmptyConstraintNode, TUniqueConstraintNode, TPartOfUniqueConstraintNode, TDistinctConstraintNode, TPartOfDistinctConstraintNode, TPartOfSortedConstraintNode, TPartOfChoppedConstraintNode, TVarIndexConstraintNode, TMultiConstraintNode>;
  154. Functions["ToOptional"] = &TCallableConstraintTransformer::FromFirst<TEmptyConstraintNode, TVarIndexConstraintNode, TMultiConstraintNode>;
  155. Functions["Head"] = &TCallableConstraintTransformer::FromFirst<TEmptyConstraintNode, TVarIndexConstraintNode, TMultiConstraintNode>;
  156. Functions["Last"] = &TCallableConstraintTransformer::FromFirst<TEmptyConstraintNode, TVarIndexConstraintNode, TMultiConstraintNode>;
  157. Functions["Reverse"] = &TCallableConstraintTransformer::ReverseWrap;
  158. Functions["Replicate"] = &TCallableConstraintTransformer::FromFirst<TVarIndexConstraintNode, TMultiConstraintNode>;
  159. Functions["AddMember"] = &TCallableConstraintTransformer::AddMemberWrap;
  160. Functions["RemoveMember"] = &TCallableConstraintTransformer::RemoveMemberWrap;
  161. Functions["ForceRemoveMember"] = &TCallableConstraintTransformer::RemoveMemberWrap;
  162. Functions["ReplaceMember"] = &TCallableConstraintTransformer::ReplaceMemberWrap;
  163. Functions["AsList"] = &TCallableConstraintTransformer::AsListWrap;
  164. Functions["OptionalIf"] = &TCallableConstraintTransformer::PassOrEmptyWrap<false, false>;
  165. Functions["FlatOptionalIf"] = &TCallableConstraintTransformer::PassOrEmptyWrap<false, true>;
  166. Functions["ListIf"] = &TCallableConstraintTransformer::PassOrEmptyWrap<true, false>;
  167. Functions["FlatListIf"] = &TCallableConstraintTransformer::PassOrEmptyWrap<true, true>;
  168. Functions["EmptyIterator"] = &TCallableConstraintTransformer::FromEmpty;
  169. Functions["EmptyFrom"] = &TCallableConstraintTransformer::EmptyFromWrap;
  170. Functions["List"] = &TCallableConstraintTransformer::ListWrap;
  171. Functions["Dict"] = &TCallableConstraintTransformer::DictWrap;
  172. Functions["EmptyList"] = &TCallableConstraintTransformer::FromEmpty;
  173. Functions["EmptyDict"] = &TCallableConstraintTransformer::FromEmpty;
  174. Functions["DictFromKeys"] = &TCallableConstraintTransformer::DictFromKeysWrap;
  175. Functions["If"] = &TCallableConstraintTransformer::IfWrap;
  176. Functions["Nothing"] = &TCallableConstraintTransformer::FromEmpty;
  177. Functions["IfPresent"] = &TCallableConstraintTransformer::IfPresentWrap;
  178. Functions["Coalesce"] = &TCallableConstraintTransformer::CommonFromChildren<0, TSortedConstraintNode, TPartOfSortedConstraintNode, TChoppedConstraintNode, TPartOfChoppedConstraintNode, TEmptyConstraintNode, TUniqueConstraintNode, TPartOfUniqueConstraintNode, TDistinctConstraintNode, TPartOfDistinctConstraintNode, TVarIndexConstraintNode, TMultiConstraintNode>;
  179. Functions["CombineByKey"] = &TCallableConstraintTransformer::FromFinalLambda<TCoCombineByKey::idx_FinishHandlerLambda>;
  180. Functions["FinalizeByKey"] = &TCallableConstraintTransformer::FromFinalLambda<TCoFinalizeByKey::idx_FinishHandlerLambda>;
  181. Functions["CombineCore"] = &TCallableConstraintTransformer::FromFinalLambda<TCoCombineCore::idx_FinishHandler>;
  182. Functions["PartitionByKey"] = &TCallableConstraintTransformer::ShuffleByKeysWrap<true>;
  183. Functions["PartitionsByKeys"] = &TCallableConstraintTransformer::ShuffleByKeysWrap<true>;
  184. Functions["ShuffleByKeys"] = &TCallableConstraintTransformer::ShuffleByKeysWrap<false>;
  185. Functions["Switch"] = &TCallableConstraintTransformer::SwitchWrap;
  186. Functions["Visit"] = &TCallableConstraintTransformer::VisitWrap;
  187. Functions["VariantItem"] = &TCallableConstraintTransformer::VariantItemWrap;
  188. Functions["Variant"] = &TCallableConstraintTransformer::VariantWrap;
  189. Functions["Guess"] = &TCallableConstraintTransformer::GuessWrap;
  190. Functions["Mux"] = &TCallableConstraintTransformer::MuxWrap;
  191. Functions["Nth"] = &TCallableConstraintTransformer::NthWrap;
  192. Functions["EquiJoin"] = &TCallableConstraintTransformer::EquiJoinWrap;
  193. Functions["JoinDict"] = &TCallableConstraintTransformer::JoinDictWrap;
  194. Functions["MapJoinCore"] = &TCallableConstraintTransformer::MapJoinCoreWrap;
  195. Functions["GraceJoinCore"] = &TCallableConstraintTransformer::GraceJoinCoreWrap;
  196. Functions["GraceSelfJoinCore"] = &TCallableConstraintTransformer::GraceSelfJoinCoreWrap;
  197. Functions["CommonJoinCore"] = &TCallableConstraintTransformer::FromFirst<TEmptyConstraintNode>;
  198. Functions["ToDict"] = &TCallableConstraintTransformer::ToDictWrap;
  199. Functions["DictItems"] = &TCallableConstraintTransformer::DictItemsWrap;
  200. Functions["DictKeys"] = &TCallableConstraintTransformer::DictHalfWrap<true>;
  201. Functions["DictPayloads"] = &TCallableConstraintTransformer::DictHalfWrap<false>;
  202. Functions["Chain1Map"] = &TCallableConstraintTransformer::Chain1MapWrap<false>;
  203. Functions["WideChain1Map"] = &TCallableConstraintTransformer::Chain1MapWrap<true>;
  204. Functions["IsKeySwitch"] = &TCallableConstraintTransformer::IsKeySwitchWrap;
  205. Functions["Condense"] = &TCallableConstraintTransformer::CondenseWrap;
  206. Functions["Condense1"] = &TCallableConstraintTransformer::Condense1Wrap<false>;
  207. Functions["GroupingCore"] = &TCallableConstraintTransformer::InheriteEmptyFromInput;
  208. Functions["Chopper"] = &TCallableConstraintTransformer::InheriteEmptyFromInput;
  209. Functions["WideChopper"] = &TCallableConstraintTransformer::InheriteEmptyFromInput;
  210. Functions["WideCombiner"] = &TCallableConstraintTransformer::InheriteEmptyFromInput;
  211. Functions["WideCondense1"] = &TCallableConstraintTransformer::Condense1Wrap<true>;
  212. Functions["Aggregate"] = &TCallableConstraintTransformer::AggregateWrap<true>;
  213. Functions["AggregateMergeState"] = &TCallableConstraintTransformer::AggregateWrap<true>;
  214. Functions["AggregateMergeFinalize"] = &TCallableConstraintTransformer::AggregateWrap<true>;
  215. Functions["AggregateMergeManyFinalize"] = &TCallableConstraintTransformer::AggregateWrap<true>;
  216. Functions["AggregateFinalize"] = &TCallableConstraintTransformer::AggregateWrap<true>;
  217. Functions["AggregateCombine"] = &TCallableConstraintTransformer::AggregateWrap<false>;
  218. Functions["AggregateCombineState"] = &TCallableConstraintTransformer::AggregateWrap<false>;
  219. Functions["Fold"] = &TCallableConstraintTransformer::FoldWrap;
  220. Functions["Fold1"] = &TCallableConstraintTransformer::FoldWrap;
  221. Functions["WithContext"] = &TCallableConstraintTransformer::CopyAllFrom<0>;
  222. Functions["WithWorld"] = &TCallableConstraintTransformer::CopyAllFrom<0>;
  223. Functions["WideTop"] = &TCallableConstraintTransformer::WideTopWrap<false>;
  224. Functions["WideTopSort"] = &TCallableConstraintTransformer::WideTopWrap<true>;
  225. Functions["WideSort"] = &TCallableConstraintTransformer::WideTopWrap<true>;
  226. Functions["WideTopBlocks"] = &TCallableConstraintTransformer::WideTopWrap<false>;
  227. Functions["WideTopSortBlocks"] = &TCallableConstraintTransformer::WideTopWrap<true>;
  228. Functions["WideSortBlocks"] = &TCallableConstraintTransformer::WideTopWrap<true>;
  229. Functions["WideToBlocks"] = &TCallableConstraintTransformer::CopyAllFrom<0>;
  230. Functions["WideFromBlocks"] = &TCallableConstraintTransformer::CopyAllFrom<0>;
  231. Functions["ReplicateScalars"] = &TCallableConstraintTransformer::CopyAllFrom<0>;
  232. Functions["BlockMergeFinalizeHashed"] = &TCallableConstraintTransformer::AggregateWrap<true>;
  233. Functions["BlockMergeManyFinalizeHashed"] = &TCallableConstraintTransformer::AggregateWrap<true>;
  234. Functions["MultiHoppingCore"] = &TCallableConstraintTransformer::MultiHoppingCoreWrap;
  235. Functions["StablePickle"] = &TCallableConstraintTransformer::FromFirst<TUniqueConstraintNode, TPartOfUniqueConstraintNode, TDistinctConstraintNode, TPartOfDistinctConstraintNode, TPartOfChoppedConstraintNode, TVarIndexConstraintNode>;
  236. Functions["Unpickle"] = &TCallableConstraintTransformer::FromSecond<TUniqueConstraintNode, TPartOfUniqueConstraintNode, TDistinctConstraintNode, TPartOfDistinctConstraintNode, TPartOfChoppedConstraintNode, TVarIndexConstraintNode>;
  237. }
  238. std::optional<IGraphTransformer::TStatus> ProcessCore(const TExprNode::TPtr& input, TExprNode::TPtr& output, TExprContext& ctx) {
  239. if (const auto func = Functions.find(input->Content()); Functions.cend() != func) {
  240. return (this->*func->second)(input, output, ctx);
  241. }
  242. return std::nullopt;
  243. }
  244. std::optional<IGraphTransformer::TStatus> ProcessList(const TExprNode::TPtr& input, TExprNode::TPtr& output, TExprContext& ctx) {
  245. if (!input->ChildrenSize() || ETypeAnnotationKind::Tuple != input->GetTypeAnn()->GetKind())
  246. return TStatus::Ok;
  247. return AsTupleWrap(input, output, ctx);
  248. }
  249. TStatus ProcessUnknown(const TExprNode::TPtr& input, TExprContext&) {
  250. return UpdateAllChildLambdasConstraints(*input);
  251. }
  252. TStatus ValidateProviderCommitResult(const TExprNode::TPtr&, TExprContext&) {
  253. return TStatus::Ok;
  254. }
  255. TStatus ValidateProviderReadResult(const TExprNode::TPtr&, TExprContext&) {
  256. return TStatus::Ok;
  257. }
  258. TStatus ValidateProviderWriteResult(const TExprNode::TPtr&, TExprContext&) {
  259. return TStatus::Ok;
  260. }
  261. TStatus ValidateProviderConfigureResult(const TExprNode::TPtr&, TExprContext&) {
  262. return TStatus::Ok;
  263. }
  264. IGraphTransformer& GetTransformer(IDataProvider& provider) const {
  265. return provider.GetConstraintTransformer(InstantOnly, SubGraph);
  266. }
  267. private:
  268. template <size_t Ndx>
  269. TStatus CopyAllFrom(const TExprNode::TPtr& input, TExprNode::TPtr& output, TExprContext& ctx) const {
  270. Y_UNUSED(output);
  271. Y_UNUSED(ctx);
  272. input->CopyConstraints(*input->Child(Ndx));
  273. return TStatus::Ok;
  274. }
  275. template <class... TConstraints>
  276. TStatus FromFirst(const TExprNode::TPtr& input, TExprNode::TPtr& output, TExprContext& ctx) const {
  277. Y_UNUSED(output);
  278. Y_UNUSED(ctx);
  279. TApplyConstraintFromInput<0, TConstraints...>::Do(input);
  280. return TStatus::Ok;
  281. }
  282. template <class... TConstraints>
  283. TStatus FromSecond(const TExprNode::TPtr& input, TExprNode::TPtr& output, TExprContext& ctx) const {
  284. Y_UNUSED(output);
  285. Y_UNUSED(ctx);
  286. TApplyConstraintFromInput<1, TConstraints...>::Do(input);
  287. return TStatus::Ok;
  288. }
  289. template <size_t StartFromChild, class... TConstraints>
  290. TStatus CommonFromChildren(const TExprNode::TPtr& input, TExprNode::TPtr& output, TExprContext& ctx) const {
  291. Y_UNUSED(output);
  292. TVector<const TConstraintSet*> constraints;
  293. for (size_t i = StartFromChild; i < input->ChildrenSize(); ++i) {
  294. constraints.push_back(&input->Child(i)->GetConstraintSet());
  295. }
  296. TApplyCommonConstraint<TConstraints...>::Do(input, constraints, ctx);
  297. return TStatus::Ok;
  298. }
  299. TStatus FromEmpty(const TExprNode::TPtr& input, TExprNode::TPtr& /*output*/, TExprContext& ctx) const {
  300. input->AddConstraint(ctx.MakeConstraint<TEmptyConstraintNode>());
  301. return TStatus::Ok;
  302. }
  303. TStatus FailMe(const TExprNode::TPtr& input, TExprNode::TPtr& /*output*/, TExprContext& ctx) const {
  304. if (input->Child(0)->Content() == "constraint") {
  305. input->AddConstraint(ctx.MakeConstraint<TEmptyConstraintNode>());
  306. }
  307. return TStatus::Ok;
  308. }
  309. TStatus EmptyFromWrap(const TExprNode::TPtr& input, TExprNode::TPtr& output, TExprContext& ctx) const {
  310. auto set = input->Head().GetConstraintSet();
  311. set.RemoveConstraint(TEmptyConstraintNode::Name());
  312. if (!set) {
  313. const auto type = input->GetTypeAnn();
  314. output = ctx.NewCallable(input->Pos(), GetEmptyCollectionName(type), {ExpandType(input->Pos(), *type, ctx)});
  315. return TStatus::Repeat;
  316. }
  317. set.AddConstraint(ctx.MakeConstraint<TEmptyConstraintNode>());
  318. input->SetConstraints(set);
  319. return TStatus::Ok;
  320. }
  321. template <size_t LambdaIdx>
  322. TStatus FromFinalLambda(const TExprNode::TPtr& input, TExprNode::TPtr& output, TExprContext& ctx) const {
  323. TStatus status = UpdateAllChildLambdasConstraints(*input);
  324. if (status != TStatus::Ok) {
  325. return status;
  326. }
  327. TApplyConstraintFromInput<LambdaIdx, TMultiConstraintNode, TEmptyConstraintNode>::Do(input);
  328. return FromFirst<TEmptyConstraintNode>(input, output, ctx);
  329. }
  330. TStatus InheriteEmptyFromInput(const TExprNode::TPtr& input, TExprNode::TPtr& output, TExprContext& ctx) const {
  331. auto status = UpdateAllChildLambdasConstraints(*input);
  332. if (status != TStatus::Ok) {
  333. return status;
  334. }
  335. return FromFirst<TEmptyConstraintNode>(input, output, ctx);
  336. }
  337. template<bool Sort>
  338. TStatus WideTopWrap(const TExprNode::TPtr& input, TExprNode::TPtr& output, TExprContext& ctx) const {
  339. if constexpr (Sort) {
  340. TSortedConstraintNode::TContainerType sorted;
  341. sorted.reserve(input->Tail().ChildrenSize());
  342. for (const auto& item : input->Tail().Children()) {
  343. if (item->Tail().IsCallable("Bool"))
  344. sorted.emplace_back(std::make_pair(TPartOfConstraintBase::TSetType{TPartOfConstraintBase::TPathType(1U, item->Head().Content())}, FromString<bool>(item->Tail().Tail().Content())));
  345. else
  346. break;
  347. }
  348. if (!sorted.empty()) {
  349. input->AddConstraint(ctx.MakeConstraint<TSortedConstraintNode>(std::move(sorted)));
  350. }
  351. }
  352. return FromFirst<TEmptyConstraintNode, TUniqueConstraintNode, TDistinctConstraintNode, TVarIndexConstraintNode>(input, output, ctx);
  353. }
  354. TStatus SortWrap(const TExprNode::TPtr& input, TExprNode::TPtr& output, TExprContext& ctx) const {
  355. if (const auto status = UpdateLambdaConstraints(input->Tail()); status != TStatus::Ok) {
  356. return status;
  357. }
  358. if (const auto sorted = DeduceSortConstraint(*input->Child(1), *input->Child(2), ctx)) {
  359. input->AddConstraint(sorted->GetSimplifiedForType(*input->GetTypeAnn(), ctx));
  360. }
  361. return FromFirst<TEmptyConstraintNode, TUniqueConstraintNode, TDistinctConstraintNode, TVarIndexConstraintNode>(input, output, ctx);
  362. }
  363. TStatus AssumeConstraintsWrap(const TExprNode::TPtr& input, TExprNode::TPtr& /*output*/, TExprContext& ctx) const {
  364. TConstraintSet set;
  365. try {
  366. set = ctx.MakeConstraintSet(NYT::NodeFromYsonString(input->Tail().Content()));
  367. } catch (...) {
  368. ctx.AddError(TIssue(ctx.GetPosition(input->Pos()), TStringBuilder() <<
  369. "Bad constraints yson-value: " << CurrentExceptionMessage()));
  370. return IGraphTransformer::TStatus::Error;
  371. }
  372. if (!set) {
  373. ctx.AddError(TIssue(ctx.GetPosition(input->Pos()), "AssumeConstraints with empty constraints set"));
  374. return IGraphTransformer::TStatus::Error;
  375. }
  376. for (auto constraint: set.GetAllConstraints()) {
  377. if (!constraint->IsApplicableToType(*input->GetTypeAnn())) {
  378. ctx.AddError(TIssue(ctx.GetPosition(input->Pos()), TStringBuilder() << *constraint
  379. << " is not applicable to " << *input->GetTypeAnn()));
  380. return IGraphTransformer::TStatus::Error;
  381. }
  382. }
  383. for (auto constr: input->Head().GetAllConstraints()) {
  384. if (!constr->GetName().starts_with("PartOf") && !set.GetConstraint(constr->GetName())) {
  385. set.AddConstraint(constr);
  386. }
  387. }
  388. input->SetConstraints(set);
  389. return IGraphTransformer::TStatus::Ok;
  390. }
  391. template<bool Distinct, bool Strict>
  392. TStatus AssumeUniqueWrap(const TExprNode::TPtr& input, TExprNode::TPtr& output, TExprContext& ctx) const {
  393. typename TUniqueConstraintNodeBase<Distinct>::TContentType content;
  394. for (auto i = 1U; i < input->ChildrenSize(); ++i) {
  395. TPartOfConstraintBase::TSetOfSetsType sets;
  396. sets.reserve(input->Child(i)->ChildrenSize());
  397. for (const auto& list : input->Child(i)->Children()) {
  398. if (list->IsAtom())
  399. sets.insert_unique(TPartOfConstraintBase::TSetType{TPartOfConstraintBase::TPathType(1U, list->Content())});
  400. else if (list->IsList()) {
  401. TPartOfConstraintBase::TSetType columns;
  402. columns.reserve(list->ChildrenSize());
  403. for (const auto& column: list->Children()) {
  404. if (column->IsAtom())
  405. columns.insert_unique(TPartOfConstraintBase::TPathType(1U, column->Content()));
  406. else if (column->IsList()) {
  407. TPartOfConstraintBase::TPathType path(column->ChildrenSize());
  408. std::transform(column->Children().cbegin(), column->Children().cend(), path.begin(), [](const TExprNode::TPtr& atom) { return atom->Content(); } );
  409. columns.insert_unique(std::move(path));
  410. }
  411. }
  412. sets.insert_unique(std::move(columns));
  413. }
  414. }
  415. content.insert_unique(std::move(sets));
  416. }
  417. if (content.empty())
  418. content.insert_unique(TPartOfConstraintBase::TSetOfSetsType{TPartOfConstraintBase::TSetType{TPartOfConstraintBase::TPathType()}});
  419. auto constraint = ctx.MakeConstraint<TUniqueConstraintNodeBase<Distinct>>(std::move(content));
  420. if (!constraint->IsApplicableToType(*input->GetTypeAnn())) {
  421. if constexpr (Strict) {
  422. ctx.AddError(TIssue(ctx.GetPosition(input->Pos()), TStringBuilder() << *constraint
  423. << " is not applicable to " << *input->GetTypeAnn()));
  424. } else {
  425. auto issue = TIssue(ctx.GetPosition(input->Pos()),
  426. TStringBuilder() << (Distinct ? "Distinct" : "Unique") << " sql hint contains invalid column: " << Endl
  427. << *constraint << " is not applicable to " << *input->GetTypeAnn());
  428. SetIssueCode(EYqlIssueCode::TIssuesIds_EIssueCode_YQL_HINT_INVALID_PARAMETERS, issue);
  429. if (ctx.AddWarning(issue)) {
  430. output = input->HeadPtr();
  431. return IGraphTransformer::TStatus::Repeat;
  432. }
  433. }
  434. return IGraphTransformer::TStatus::Error;
  435. }
  436. if constexpr (!Strict) {
  437. output = ctx.RenameNode(*input, Distinct ? "AssumeDistinct" : "AssumeUnique");
  438. return IGraphTransformer::TStatus::Repeat;
  439. }
  440. if (const auto old = input->Head().GetConstraint<TUniqueConstraintNodeBase<Distinct>>()) {
  441. if (old->Includes(*constraint)) {
  442. output = input->HeadPtr();
  443. return TStatus::Repeat;
  444. } else
  445. constraint = TUniqueConstraintNodeBase<Distinct>::Merge(old, constraint, ctx);
  446. }
  447. input->AddConstraint(constraint);
  448. return FromFirst<TSortedConstraintNode, TChoppedConstraintNode, TUniqueConstraintNodeBase<!Distinct>, TEmptyConstraintNode, TVarIndexConstraintNode>(input, output, ctx);
  449. }
  450. TStatus AssumeChoppedWrap(const TExprNode::TPtr& input, TExprNode::TPtr& output, TExprContext& ctx) const {
  451. TPartOfConstraintBase::TSetOfSetsType sets;
  452. for (auto i = 1U; i < input->ChildrenSize(); ++i) {
  453. TPartOfConstraintBase::TSetType columns;
  454. columns.reserve(input->Child(i)->ChildrenSize());
  455. for (const auto& column: input->Child(i)->Children()) {
  456. if (column->IsAtom())
  457. columns.insert_unique(TPartOfConstraintBase::TPathType(1U, column->Content()));
  458. else if (column->IsList()) {
  459. TPartOfConstraintBase::TPathType path(column->ChildrenSize());
  460. std::transform(column->Children().cbegin(), column->Children().cend(), path.begin(), [](const TExprNode::TPtr& atom) { return atom->Content(); } );
  461. columns.insert_unique(std::move(path));
  462. }
  463. }
  464. sets.insert_unique(std::move(columns));
  465. }
  466. const auto constraint = ctx.MakeConstraint<TChoppedConstraintNode>(std::move(sets));
  467. if (!constraint->IsApplicableToType(*input->GetTypeAnn())) {
  468. ctx.AddError(TIssue(ctx.GetPosition(input->Pos()), TStringBuilder() << *constraint
  469. << " is not applicable to " << *input->GetTypeAnn()));
  470. return IGraphTransformer::TStatus::Error;
  471. }
  472. if (const auto old = input->Head().GetConstraint<TChoppedConstraintNode>()) {
  473. if (old->Equals(*constraint)) {
  474. output = input->HeadPtr();
  475. return TStatus::Repeat;
  476. }
  477. }
  478. input->AddConstraint(constraint);
  479. return FromFirst<TSortedConstraintNode, TDistinctConstraintNode, TUniqueConstraintNode, TEmptyConstraintNode, TVarIndexConstraintNode>(input, output, ctx);
  480. }
  481. template <bool UseSort>
  482. TStatus TopWrap(const TExprNode::TPtr& input, TExprNode::TPtr& output, TExprContext& ctx) const {
  483. if (const auto status = UpdateLambdaConstraints(input->Tail()); status != TStatus::Ok) {
  484. return status;
  485. }
  486. if constexpr (UseSort) {
  487. if (const auto sorted = DeduceSortConstraint(*input->Child(2), *input->Child(3), ctx)) {
  488. input->AddConstraint(sorted->GetSimplifiedForType(*input->GetTypeAnn(), ctx));
  489. }
  490. }
  491. return FromFirst<TEmptyConstraintNode, TUniqueConstraintNode, TDistinctConstraintNode, TVarIndexConstraintNode>(input, output, ctx);
  492. }
  493. template<class TConstraint>
  494. static void FilterFromHead(const TExprNode& input, TConstraintSet& constraints, const TPartOfConstraintBase::TPathFilter& filter, TExprContext& ctx) {
  495. if (const auto source = input.Head().GetConstraint<TConstraint>()) {
  496. if (const auto filtered = source->FilterFields(ctx, filter)) {
  497. constraints.AddConstraint(filtered);
  498. }
  499. }
  500. }
  501. template<class TConstraint, bool Simplify = false>
  502. static void ReduceFromHead(const TExprNode::TPtr& input, const TPartOfConstraintBase::TPathReduce& reduce, TExprContext& ctx) {
  503. if (const auto source = input->Head().GetConstraint<TConstraint>()) {
  504. if (const auto filtered = source->RenameFields(ctx, reduce)) {
  505. if constexpr (Simplify)
  506. input->AddConstraint(filtered->GetSimplifiedForType(*input->GetTypeAnn(), ctx));
  507. else
  508. input->AddConstraint(filtered);
  509. }
  510. }
  511. }
  512. template<class TConstraint, bool Simplify = false>
  513. static void FilterFromHead(const TExprNode::TPtr& input, const TPartOfConstraintBase::TPathFilter& filter, TExprContext& ctx) {
  514. if (const auto source = input->Head().GetConstraint<TConstraint>()) {
  515. if (const auto filtered = source->FilterFields(ctx, filter)) {
  516. if constexpr (Simplify)
  517. input->AddConstraint(filtered->GetSimplifiedForType(*input->GetTypeAnn(), ctx));
  518. else
  519. input->AddConstraint(filtered);
  520. }
  521. }
  522. }
  523. template<class TConstraint>
  524. static void FilterFromHeadIfMissed(const TExprNode::TPtr& input, const TPartOfConstraintBase::TPathFilter& filter, TExprContext& ctx) {
  525. if (!input->GetConstraint<TConstraint>())
  526. FilterFromHead<TConstraint>(input, filter, ctx);
  527. }
  528. TStatus SelectMembersWrap(const TExprNode::TPtr& input, TExprNode::TPtr& /*output*/, TExprContext& ctx) const {
  529. auto outItemType = input->GetTypeAnn();
  530. while (outItemType->GetKind() == ETypeAnnotationKind::Optional) {
  531. outItemType = outItemType->Cast<TOptionalExprType>()->GetItemType();
  532. }
  533. if (outItemType->GetKind() == ETypeAnnotationKind::Variant) {
  534. if (outItemType->Cast<TVariantExprType>()->GetUnderlyingType()->GetKind() == ETypeAnnotationKind::Tuple) {
  535. const auto outSize = outItemType->Cast<TVariantExprType>()->GetUnderlyingType()->Cast<TTupleExprType>()->GetSize();
  536. auto multi = input->Head().GetConstraint<TMultiConstraintNode>();
  537. if (multi && multi->GetItems().back().first >= outSize) {
  538. TMultiConstraintNode::TMapType filteredItems;
  539. for (auto& item: multi->GetItems()) {
  540. if (item.first < outSize) {
  541. filteredItems.push_back(item);
  542. }
  543. }
  544. multi = filteredItems.empty() ? nullptr : ctx.MakeConstraint<TMultiConstraintNode>(std::move(filteredItems));
  545. }
  546. if (multi) {
  547. input->AddConstraint(multi);
  548. }
  549. auto varIndex = input->Head().GetConstraint<TVarIndexConstraintNode>();
  550. if (varIndex && varIndex->GetIndexMapping().back().first >= outSize) {
  551. TVarIndexConstraintNode::TMapType filteredItems;
  552. for (auto& item: varIndex->GetIndexMapping()) {
  553. if (item.first < outSize) {
  554. filteredItems.push_back(item);
  555. }
  556. }
  557. varIndex = filteredItems.empty() ? nullptr : ctx.MakeConstraint<TVarIndexConstraintNode>(std::move(filteredItems));
  558. }
  559. if (varIndex) {
  560. input->AddConstraint(varIndex);
  561. }
  562. }
  563. }
  564. else if (outItemType->GetKind() == ETypeAnnotationKind::Struct) {
  565. const auto filter = [outItemType](const TPartOfConstraintBase::TPathType& path) {
  566. return !path.empty() && TPartOfConstraintBase::GetSubTypeByPath(path, *outItemType);
  567. };
  568. FilterFromHead<TPartOfSortedConstraintNode>(input, filter, ctx);
  569. FilterFromHead<TPartOfChoppedConstraintNode>(input, filter, ctx);
  570. FilterFromHead<TPartOfUniqueConstraintNode>(input, filter, ctx);
  571. FilterFromHead<TPartOfDistinctConstraintNode>(input, filter, ctx);
  572. }
  573. return TStatus::Ok;
  574. }
  575. template <bool Strict>
  576. TStatus CastWrap(const TExprNode::TPtr& input, TExprNode::TPtr& /*output*/, TExprContext& ctx) const {
  577. const auto outItemType = input->GetTypeAnn();
  578. const auto inItemType = input->Head().GetTypeAnn();
  579. const auto filter = [inItemType, outItemType, toString = input->IsCallable({"ToString", "ToBytes"})](const TPartOfConstraintBase::TPathType& path) {
  580. if (const auto outType = TPartOfConstraintBase::GetSubTypeByPath(path, *outItemType)) {
  581. const auto inType = TPartOfConstraintBase::GetSubTypeByPath(path, *inItemType);
  582. return (toString && inType->GetKind() == ETypeAnnotationKind::Data && inType->Cast<TDataExprType>()->GetSlot() == EDataSlot::Utf8) || IsSameAnnotation(*outType, *inType);
  583. }
  584. return false;
  585. };
  586. const auto filterForUnique = [inItemType, outItemType](const TPartOfConstraintBase::TPathType& path) {
  587. const auto castResult = CastResult<Strict>(TPartOfConstraintBase::GetSubTypeByPath(path, *inItemType), TPartOfConstraintBase::GetSubTypeByPath(path, *outItemType));
  588. return NUdf::ECastOptions::Complete == castResult || NUdf::ECastOptions::MayFail == castResult;
  589. };
  590. const auto filterForDistinct = [inItemType, outItemType](const TPartOfConstraintBase::TPathType& path) {
  591. return NUdf::ECastOptions::Complete == CastResult<Strict>(TPartOfConstraintBase::GetSubTypeByPath(path, *inItemType), TPartOfConstraintBase::GetSubTypeByPath(path, *outItemType));
  592. };
  593. FilterFromHead<TSortedConstraintNode>(input, filter, ctx);
  594. FilterFromHead<TChoppedConstraintNode>(input, filter, ctx);
  595. FilterFromHead<TUniqueConstraintNode>(input, filterForUnique, ctx);
  596. FilterFromHead<TDistinctConstraintNode>(input, filterForDistinct, ctx);
  597. FilterFromHead<TPartOfSortedConstraintNode>(input, filter, ctx);
  598. FilterFromHead<TPartOfChoppedConstraintNode>(input, filter, ctx);
  599. FilterFromHead<TPartOfUniqueConstraintNode>(input, filterForUnique, ctx);
  600. FilterFromHead<TPartOfDistinctConstraintNode>(input, filterForDistinct, ctx);
  601. return TStatus::Ok;
  602. }
  603. TStatus DivePrefixMembersWrap(const TExprNode::TPtr& input, TExprNode::TPtr& output, TExprContext& ctx) const {
  604. const auto prefixes = input->Tail().Children();
  605. const auto rename = [&prefixes](const TPartOfConstraintBase::TPathType& path) -> std::vector<TPartOfConstraintBase::TPathType> {
  606. if (path.empty())
  607. return {};
  608. for (const auto& p : prefixes) {
  609. if (const auto& prefix = p->Content(); path.front().starts_with(prefix)) {
  610. auto out = path;
  611. out.front() = out.front().substr(prefix.length());
  612. return {std::move(out)};
  613. }
  614. }
  615. return {};
  616. };
  617. ReduceFromHead<TPartOfSortedConstraintNode>(input, rename, ctx);
  618. ReduceFromHead<TPartOfChoppedConstraintNode>(input, rename, ctx);
  619. ReduceFromHead<TPartOfUniqueConstraintNode>(input, rename, ctx);
  620. ReduceFromHead<TPartOfDistinctConstraintNode>(input, rename, ctx);
  621. return FromFirst<TVarIndexConstraintNode>(input, output, ctx);
  622. }
  623. TStatus ExtractMembersWrap(const TExprNode::TPtr& input, TExprNode::TPtr& output, TExprContext& ctx) const {
  624. const auto outItemType = GetSeqItemType(*input->GetTypeAnn()).Cast<TStructExprType>();
  625. const auto filter = [outItemType](const TPartOfConstraintBase::TPathType& path) { return !path.empty() && outItemType->FindItem(path.front()); };
  626. FilterFromHead<TSortedConstraintNode, true>(input, filter, ctx);
  627. FilterFromHead<TChoppedConstraintNode, true>(input, filter, ctx);
  628. FilterFromHead<TUniqueConstraintNode, true>(input, filter, ctx);
  629. FilterFromHead<TDistinctConstraintNode, true>(input, filter, ctx);
  630. FilterFromHead<TPartOfSortedConstraintNode>(input, filter, ctx);
  631. FilterFromHead<TPartOfChoppedConstraintNode>(input, filter, ctx);
  632. FilterFromHead<TPartOfUniqueConstraintNode>(input, filter, ctx);
  633. FilterFromHead<TPartOfDistinctConstraintNode>(input, filter, ctx);
  634. return FromFirst<TEmptyConstraintNode, TVarIndexConstraintNode>(input, output, ctx);
  635. }
  636. TStatus RemovePrefixMembersWrap(const TExprNode::TPtr& input, TExprNode::TPtr& output, TExprContext& ctx) const {
  637. const TTypeAnnotationNode* outItemType = GetSeqItemType(input->GetTypeAnn());
  638. if (!outItemType) {
  639. outItemType = input->GetTypeAnn();
  640. }
  641. if (outItemType->GetKind() == ETypeAnnotationKind::Struct) {
  642. const auto outStructType = outItemType->Cast<TStructExprType>();
  643. const auto filter = [outStructType](const TPartOfConstraintBase::TPathType& path) { return !path.empty() && outStructType->FindItem(path.front()); };
  644. FilterFromHead<TPartOfSortedConstraintNode>(input, filter, ctx);
  645. FilterFromHead<TPartOfChoppedConstraintNode>(input, filter, ctx);
  646. FilterFromHead<TPartOfUniqueConstraintNode>(input, filter, ctx);
  647. FilterFromHead<TPartOfDistinctConstraintNode>(input, filter, ctx);
  648. }
  649. else if (outItemType->GetKind() == ETypeAnnotationKind::Variant) {
  650. if (auto multi = input->Head().GetConstraint<TMultiConstraintNode>()) {
  651. TMultiConstraintNode::TMapType multiItems;
  652. auto tupleUnderType = outItemType->Cast<TVariantExprType>()->GetUnderlyingType()->Cast<TTupleExprType>();
  653. for (auto& item: multi->GetItems()) {
  654. YQL_ENSURE(item.first < tupleUnderType->GetSize());
  655. auto& constr = multiItems[item.first];
  656. const auto outStructType = tupleUnderType->GetItems()[item.first]->Cast<TStructExprType>();
  657. const auto filter = [outStructType](const TPartOfConstraintBase::TPathType& path) { return !path.empty() && outStructType->FindItem(path.front()); };
  658. FilterFromHead<TPartOfSortedConstraintNode>(*input, constr, filter, ctx);
  659. FilterFromHead<TPartOfChoppedConstraintNode>(*input, constr, filter, ctx);
  660. FilterFromHead<TPartOfUniqueConstraintNode>(*input, constr, filter, ctx);
  661. FilterFromHead<TPartOfDistinctConstraintNode>(*input, constr, filter, ctx);
  662. }
  663. input->AddConstraint(ctx.MakeConstraint<TMultiConstraintNode>(std::move(multiItems)));
  664. }
  665. }
  666. return FromFirst<TEmptyConstraintNode, TVarIndexConstraintNode>(input, output, ctx);
  667. }
  668. // TODO: Empty for false condition
  669. template <bool Ordered>
  670. TStatus FilterWrap(const TExprNode::TPtr& input, TExprNode::TPtr& output, TExprContext& ctx) const {
  671. if (const auto status = UpdateLambdaConstraints(*input->Child(1)); status != TStatus::Ok) {
  672. return status;
  673. }
  674. if constexpr (Ordered) {
  675. FromFirst<TSortedConstraintNode, TPartOfSortedConstraintNode, TChoppedConstraintNode, TPartOfChoppedConstraintNode>(input, output, ctx);
  676. }
  677. return FromFirst<TEmptyConstraintNode, TUniqueConstraintNode, TPartOfUniqueConstraintNode, TDistinctConstraintNode, TPartOfDistinctConstraintNode, TVarIndexConstraintNode, TMultiConstraintNode>(input, output, ctx);
  678. }
  679. template<class TConstraint>
  680. static const TConstraint* GetConstraintFromWideResultLambda(const TExprNode& lambda, TExprContext& ctx);
  681. template<class TConstraintType>
  682. static const TConstraintType* GetLambdaConstraint(const TExprNode& lambda, TExprContext& ctx) {
  683. if (2U == lambda.ChildrenSize())
  684. return lambda.Tail().GetConstraint<TConstraintType>();
  685. TVector<const TConstraintSet*> constraints;
  686. constraints.reserve(lambda.ChildrenSize() - 1U);
  687. for (size_t i = 1U; i < lambda.ChildrenSize(); ++i) {
  688. constraints.emplace_back(&lambda.Child(i)->GetConstraintSet());
  689. }
  690. return TConstraintType::MakeCommon(constraints, ctx);
  691. }
  692. template<class TConstraintType, bool WideLambda>
  693. static const TConstraintType* GetConstraintFromLambda(const TExprNode& lambda, TExprContext& ctx) {
  694. if constexpr (WideLambda)
  695. return GetConstraintFromWideResultLambda<TConstraintType>(lambda, ctx);
  696. else
  697. return GetLambdaConstraint<TConstraintType>(lambda, ctx);
  698. }
  699. static std::optional<bool> GetDirection(const TExprNode& dir) {
  700. if (dir.IsCallable("Bool"))
  701. return IsTrue(dir.Tail().Content());
  702. if (dir.IsCallable("Not"))
  703. if (const auto d = GetDirection(dir.Head()))
  704. return !*d;
  705. return std::nullopt;
  706. }
  707. static std::vector<std::pair<TPartOfConstraintBase::TPathType, bool>>
  708. ExtractSimpleSortTraits(const TExprNode& sortDirections, const TExprNode& keySelectorLambda) {
  709. const auto& keySelectorBody = keySelectorLambda.Tail();
  710. const auto& keySelectorArg = keySelectorLambda.Head().Head();
  711. std::vector<std::pair<TPartOfConstraintBase::TPathType, bool>> columns;
  712. if (const auto dir = GetDirection(sortDirections))
  713. columns.emplace_back(TPartOfConstraintBase::TPathType(), *dir);
  714. else if (sortDirections.IsList())
  715. if (const auto size = keySelectorBody.ChildrenSize()) {
  716. columns.reserve(size);
  717. for (auto i = 0U; i < size; ++i)
  718. if (const auto dir = GetDirection(*sortDirections.Child(i)))
  719. columns.emplace_back(TPartOfConstraintBase::TPathType(), *dir);
  720. else
  721. return {};
  722. } else
  723. return {};
  724. else
  725. return {};
  726. if (keySelectorBody.IsList())
  727. if (const auto size = keySelectorBody.ChildrenSize()) {
  728. TPartOfConstraintBase::TSetType set;
  729. set.reserve(size);
  730. columns.resize(size, std::make_pair(TPartOfConstraintBase::TPathType(), columns.back().second));
  731. auto it = columns.begin();
  732. for (auto i = 0U; i < size; ++i) {
  733. if (auto path = GetPathToKey<true>(*keySelectorBody.Child(i), keySelectorArg)) {
  734. if (set.insert(*path).second)
  735. it++->first = std::move(*path);
  736. else if (columns.cend() != it)
  737. it = columns.erase(it);
  738. } else {
  739. return {};
  740. }
  741. }
  742. } else
  743. return {};
  744. else if (auto path = GetPathToKey<true>(keySelectorBody, keySelectorArg))
  745. if (columns.size() == 1U)
  746. columns.front().first = std::move(*path);
  747. else
  748. return {};
  749. else
  750. return {};
  751. return columns;
  752. }
  753. template<class TConstraint, bool OrderedMap, class TInput>
  754. static void GetFromMapLambda(const TInput& input, const TConstraintSet& handler, TConstraintSet& output, bool isSingleItem, TExprContext& ctx) {
  755. constexpr bool isOrderConstraint = std::is_same<typename TConstraint::TMainConstraint, TSortedConstraintNode>() || std::is_same<typename TConstraint::TMainConstraint, TChoppedConstraintNode>();
  756. if (const auto lambda = handler.GetConstraint<TConstraint>()) {
  757. const auto original = input.template GetConstraint<typename TConstraint::TMainConstraint>();
  758. if constexpr (OrderedMap || !isOrderConstraint) {
  759. if (original) {
  760. if (const auto complete = TConstraint::MakeComplete(ctx, lambda->GetColumnMapping(), original)) {
  761. output.AddConstraint(complete);
  762. }
  763. }
  764. }
  765. if (const auto part = input.template GetConstraint<TConstraint>()) {
  766. auto mapping = lambda->GetColumnMapping();
  767. for (auto it = mapping.cbegin(); mapping.cend() != it;) {
  768. if (part->GetColumnMapping().contains(it->first))
  769. ++it;
  770. else
  771. it = mapping.erase(it);
  772. }
  773. if (!mapping.empty()) {
  774. output.AddConstraint(ctx.MakeConstraint<TConstraint>(std::move(mapping)));
  775. }
  776. } else if (isOrderConstraint || isSingleItem) {
  777. if (const auto filtered = lambda->RemoveOriginal(ctx, original))
  778. output.AddConstraint(filtered);
  779. }
  780. }
  781. }
  782. template<class TConstraint, bool OrderedMap, bool WideOutput>
  783. static void GetFromMapLambda(const TExprNode::TPtr& input, bool isSingleItem, TExprContext& ctx) {
  784. constexpr bool isOrderConstraint = std::is_same<typename TConstraint::TMainConstraint, TSortedConstraintNode>() || std::is_same<typename TConstraint::TMainConstraint, TChoppedConstraintNode>();
  785. if (const auto lambda = GetConstraintFromLambda<TConstraint, WideOutput>(input->Tail(), ctx)) {
  786. const auto original = GetDetailed(input->Head().GetConstraint<typename TConstraint::TMainConstraint>(), *input->Head().GetTypeAnn(), ctx);
  787. if constexpr (OrderedMap || !isOrderConstraint) {
  788. if (original) {
  789. if (const auto complete = TConstraint::MakeComplete(ctx, lambda->GetColumnMapping(), original)) {
  790. input->AddConstraint(complete->GetSimplifiedForType(*input->GetTypeAnn(), ctx));
  791. }
  792. }
  793. }
  794. if (const auto part = input->Head().GetConstraint<TConstraint>()) {
  795. auto mapping = lambda->GetColumnMapping();
  796. for (auto it = mapping.cbegin(); mapping.cend() != it;) {
  797. if (part->GetColumnMapping().contains(it->first))
  798. ++it;
  799. else
  800. it = mapping.erase(it);
  801. }
  802. if (!mapping.empty()) {
  803. input->AddConstraint(ctx.MakeConstraint<TConstraint>(std::move(mapping)));
  804. }
  805. } else if (isOrderConstraint || isSingleItem) {
  806. if (const auto filtered = lambda->RemoveOriginal(ctx, original))
  807. input->AddConstraint(filtered);
  808. }
  809. }
  810. }
  811. template <bool Ordered, bool Flat, bool WideInput = false, bool WideOutput = false>
  812. TStatus MapWrap(const TExprNode::TPtr& input, TExprNode::TPtr& output, TExprContext& ctx) const {
  813. auto argConstraints = GetConstraintsForInputArgument<Ordered, WideInput>(*input, ctx);
  814. if constexpr (Ordered && !(Flat || WideInput || WideOutput)) {
  815. // TODO: is temporary crutch for MapNext.
  816. if (argConstraints.size() < input->Tail().Head().ChildrenSize())
  817. argConstraints.resize(input->Tail().Head().ChildrenSize(), argConstraints.front());
  818. }
  819. if (const auto status = UpdateLambdaConstraints(input->TailRef(), ctx, argConstraints); status != TStatus::Ok) {
  820. return status;
  821. }
  822. const bool singleItem = ETypeAnnotationKind::Optional == input->GetTypeAnn()->GetKind();
  823. GetFromMapLambda<TPartOfUniqueConstraintNode, Ordered, WideOutput>(input, singleItem, ctx);
  824. GetFromMapLambda<TPartOfDistinctConstraintNode, Ordered, WideOutput>(input, singleItem, ctx);
  825. GetFromMapLambda<TPartOfSortedConstraintNode, Ordered, WideOutput>(input, singleItem, ctx);
  826. GetFromMapLambda<TPartOfChoppedConstraintNode, Ordered, WideOutput>(input, singleItem, ctx);
  827. const auto lambdaVarIndex = GetConstraintFromLambda<TVarIndexConstraintNode, WideOutput>(input->Tail(), ctx);
  828. const auto lambdaMulti = GetConstraintFromLambda<TMultiConstraintNode, WideOutput>(input->Tail(), ctx);
  829. const auto inItemType = GetSeqItemType(input->Head().GetTypeAnn());
  830. const bool multiInput = ETypeAnnotationKind::Variant == inItemType->GetKind();
  831. if (const auto varIndex = input->Head().GetConstraint<TVarIndexConstraintNode>()) {
  832. if (multiInput) {
  833. if (lambdaVarIndex) {
  834. if (const auto outVarIndex = GetVarIndexOverVarIndexConstraint(*varIndex, *lambdaVarIndex, ctx)) {
  835. input->AddConstraint(outVarIndex);
  836. }
  837. }
  838. } else {
  839. if (lambdaMulti) {
  840. TVarIndexConstraintNode::TMapType remapItems;
  841. for (auto& multiItem: lambdaMulti->GetItems()) {
  842. for (auto& varItem: varIndex->GetIndexMapping()) {
  843. remapItems.push_back(std::make_pair(multiItem.first, varItem.second));
  844. }
  845. }
  846. if (!remapItems.empty()) {
  847. ::SortUnique(remapItems);
  848. input->AddConstraint(ctx.MakeConstraint<TVarIndexConstraintNode>(std::move(remapItems)));
  849. }
  850. } else {
  851. input->AddConstraint(varIndex);
  852. }
  853. }
  854. }
  855. const auto inputMulti = input->Head().GetConstraint<TMultiConstraintNode>();
  856. if (lambdaMulti && !input->Head().GetConstraint<TEmptyConstraintNode>()) {
  857. TMultiConstraintNode::TMapType remappedItems;
  858. for (auto& item: lambdaMulti->GetItems()) {
  859. remappedItems.push_back(std::make_pair(item.first, TConstraintSet{}));
  860. if (!multiInput) { // remapping one to many
  861. GetFromMapLambda<TPartOfUniqueConstraintNode, Ordered>(input->Head(), item.second, remappedItems.back().second, singleItem, ctx);
  862. GetFromMapLambda<TPartOfDistinctConstraintNode, Ordered>(input->Head(), item.second, remappedItems.back().second, singleItem, ctx);
  863. GetFromMapLambda<TPartOfSortedConstraintNode, Ordered>(input->Head(), item.second, remappedItems.back().second, singleItem, ctx);
  864. GetFromMapLambda<TPartOfChoppedConstraintNode, Ordered>(input->Head(), item.second, remappedItems.back().second, singleItem, ctx);
  865. if (const auto empty = item.second.template GetConstraint<TEmptyConstraintNode>()) {
  866. remappedItems.pop_back();
  867. }
  868. }
  869. else if (lambdaVarIndex && inputMulti) {
  870. const auto range = lambdaVarIndex->GetIndexMapping().equal_range(item.first);
  871. switch (std::distance(range.first, range.second)) {
  872. case 0: // new index
  873. break;
  874. case 1: // remapping 1 to 1
  875. if (const auto origConstr = inputMulti->GetItem(range.first->second)) {
  876. GetFromMapLambda<TPartOfUniqueConstraintNode, Ordered>(*origConstr, item.second, remappedItems.back().second, singleItem, ctx);
  877. GetFromMapLambda<TPartOfDistinctConstraintNode, Ordered>(*origConstr, item.second, remappedItems.back().second, singleItem, ctx);
  878. GetFromMapLambda<TPartOfSortedConstraintNode, Ordered>(*origConstr, item.second, remappedItems.back().second, singleItem, ctx);
  879. GetFromMapLambda<TPartOfChoppedConstraintNode, Ordered>(*origConstr, item.second, remappedItems.back().second, singleItem, ctx);
  880. if (const auto empty = item.second.template GetConstraint<TEmptyConstraintNode>()) {
  881. remappedItems.pop_back();
  882. }
  883. } else {
  884. remappedItems.pop_back();
  885. }
  886. break;
  887. default: // remapping many to one
  888. {
  889. std::vector<const TConstraintSet*> nonEmpty;
  890. for (auto i = range.first; i != range.second; ++i) {
  891. if (auto origConstr = inputMulti->GetItem(i->second)) {
  892. nonEmpty.push_back(origConstr);
  893. }
  894. }
  895. EraseIf(nonEmpty, [] (const TConstraintSet* c) { return !!c->GetConstraint<TEmptyConstraintNode>(); });
  896. if (nonEmpty.empty()) {
  897. remappedItems.back().second.AddConstraint(ctx.MakeConstraint<TEmptyConstraintNode>());
  898. } else if (nonEmpty.size() == 1) {
  899. remappedItems.back().second = std::move(*nonEmpty.front());
  900. }
  901. }
  902. }
  903. } else {
  904. remappedItems.back().second = item.second;
  905. }
  906. }
  907. if (remappedItems) {
  908. input->AddConstraint(ctx.MakeConstraint<TMultiConstraintNode>(std::move(remappedItems)));
  909. }
  910. }
  911. else if (inputMulti && lambdaVarIndex) { // Many to one
  912. const auto range = lambdaVarIndex->GetIndexMapping().equal_range(0);
  913. std::vector<const TConstraintSet*> nonEmpty;
  914. for (auto i = range.first; i != range.second; ++i) {
  915. if (auto origConstr = inputMulti->GetItem(i->second)) {
  916. nonEmpty.push_back(origConstr);
  917. }
  918. }
  919. EraseIf(nonEmpty, [] (const TConstraintSet* c) { return !!c->GetConstraint<TEmptyConstraintNode>(); });
  920. if (nonEmpty.empty()) {
  921. input->AddConstraint(ctx.MakeConstraint<TEmptyConstraintNode>());
  922. } else if (nonEmpty.size() == 1) {
  923. input->SetConstraints(*nonEmpty.front());
  924. }
  925. }
  926. if constexpr (Flat) {
  927. if (const auto lambdaEmpty = GetConstraintFromLambda<TEmptyConstraintNode, WideOutput>(input->Tail(), ctx)) {
  928. input->AddConstraint(lambdaEmpty);
  929. const auto& filter = std::bind(&TPartOfConstraintBase::GetSubTypeByPath, std::placeholders::_1, std::cref(GetSeqItemType(*input->GetTypeAnn())));
  930. FilterFromHeadIfMissed<TUniqueConstraintNode>(input, filter, ctx);
  931. FilterFromHeadIfMissed<TDistinctConstraintNode>(input, filter, ctx);
  932. if constexpr (Ordered) {
  933. FilterFromHeadIfMissed<TSortedConstraintNode>(input, filter, ctx);
  934. FilterFromHeadIfMissed<TChoppedConstraintNode>(input, filter, ctx);
  935. }
  936. }
  937. }
  938. return FromFirst<TEmptyConstraintNode>(input, output, ctx);
  939. }
  940. template <bool Ordered>
  941. TStatus LMapWrap(const TExprNode::TPtr& input, TExprNode::TPtr& output, TExprContext& ctx) const {
  942. TConstraintNode::TListType argConstraints;
  943. for (const auto c: input->Head().GetAllConstraints()) {
  944. if (Ordered || (c->GetName() != TSortedConstraintNode::Name() && c->GetName() != TChoppedConstraintNode::Name())) {
  945. argConstraints.push_back(c);
  946. }
  947. }
  948. if (const auto status = UpdateLambdaConstraints(input->TailRef(), ctx, {argConstraints}); status != TStatus::Ok) {
  949. return status;
  950. }
  951. TSet<TStringBuf> except;
  952. if constexpr (!Ordered) {
  953. except.insert(TSortedConstraintNode::Name());
  954. except.insert(TChoppedConstraintNode::Name());
  955. }
  956. if (input->Tail().GetTypeAnn()->GetKind() == ETypeAnnotationKind::Optional) {
  957. except.insert(TEmptyConstraintNode::Name());
  958. }
  959. CopyExcept(*input, input->Tail(), except);
  960. return FromFirst<TEmptyConstraintNode>(input, output, ctx);
  961. }
  962. TStatus AsListWrap(const TExprNode::TPtr& input, TExprNode::TPtr& output, TExprContext& ctx) const {
  963. if (input->ChildrenSize() == 1) {
  964. if (const auto unique = input->Head().GetConstraint<TUniqueConstraintNode>()) {
  965. input->AddConstraint(unique);
  966. }
  967. if (const auto unique = input->Head().GetConstraint<TDistinctConstraintNode>()) {
  968. input->AddConstraint(unique);
  969. }
  970. if (const auto part = input->Head().GetConstraint<TPartOfUniqueConstraintNode>()) {
  971. input->AddConstraint(part);
  972. }
  973. if (const auto part = input->Head().GetConstraint<TPartOfDistinctConstraintNode>()) {
  974. input->AddConstraint(part);
  975. }
  976. }
  977. return CommonFromChildren<0, TPartOfSortedConstraintNode, TPartOfChoppedConstraintNode, TVarIndexConstraintNode, TMultiConstraintNode>(input, output, ctx);
  978. }
  979. template<bool Ordered>
  980. TStatus ExtendWrap(const TExprNode::TPtr& input, TExprNode::TPtr& output, TExprContext& ctx) const {
  981. if (input->ChildrenSize() == 1) {
  982. if (const auto unique = input->Head().GetConstraint<TUniqueConstraintNode>()) {
  983. input->AddConstraint(unique);
  984. }
  985. if (const auto part = input->Head().GetConstraint<TPartOfUniqueConstraintNode>()) {
  986. input->AddConstraint(part);
  987. }
  988. if (const auto unique = input->Head().GetConstraint<TDistinctConstraintNode>()) {
  989. input->AddConstraint(unique);
  990. }
  991. if (const auto part = input->Head().GetConstraint<TPartOfDistinctConstraintNode>()) {
  992. input->AddConstraint(part);
  993. }
  994. if constexpr (Ordered) {
  995. if (const auto sorted = input->Head().GetConstraint<TSortedConstraintNode>()) {
  996. input->AddConstraint(sorted);
  997. }
  998. if (const auto part = input->Head().GetConstraint<TPartOfSortedConstraintNode>()) {
  999. input->AddConstraint(part);
  1000. }
  1001. if (const auto sorted = input->Head().GetConstraint<TChoppedConstraintNode>()) {
  1002. input->AddConstraint(sorted);
  1003. }
  1004. if (const auto part = input->Head().GetConstraint<TPartOfChoppedConstraintNode>()) {
  1005. input->AddConstraint(part);
  1006. }
  1007. }
  1008. }
  1009. return CommonFromChildren<0, TEmptyConstraintNode, TVarIndexConstraintNode, TMultiConstraintNode>(input, output, ctx);
  1010. }
  1011. template <bool Union>
  1012. TStatus MergeWrap(const TExprNode::TPtr& input, TExprNode::TPtr& output, TExprContext& ctx) const {
  1013. if (auto sort = MakeCommonConstraint<TSortedConstraintNode>(input, 0, ctx)) {
  1014. if (Union && input->ChildrenSize() > 1) {
  1015. // Check and exclude modified keys from final constraint
  1016. const auto resultItemType = input->GetTypeAnn()->Cast<TListExprType>()->GetItemType();
  1017. std::vector<const TTypeAnnotationNode*> inputs;
  1018. for (const auto& child: input->Children()) {
  1019. inputs.emplace_back(child->GetTypeAnn()->Cast<TListExprType>()->GetItemType());
  1020. }
  1021. auto content = sort->GetContent();
  1022. for (auto i = 0U; i < content.size(); ++i) {
  1023. for (auto it = content[i].first.cbegin(); content[i].first.cend() != it;) {
  1024. const auto resultItemSubType = TPartOfConstraintBase::GetSubTypeByPath(*it, *resultItemType);
  1025. YQL_ENSURE(resultItemSubType, "Missing " << *it << " in result type");
  1026. auto childNdx = 0U;
  1027. while (childNdx < input->ChildrenSize()) {
  1028. if (const auto inputItemSubType = TPartOfConstraintBase::GetSubTypeByPath(*it, *inputs[childNdx])) {
  1029. if (IsSameAnnotation(*inputItemSubType, *resultItemSubType)) {
  1030. ++childNdx;
  1031. continue;
  1032. }
  1033. } else {
  1034. YQL_ENSURE(input->Child(childNdx)->GetConstraint<TEmptyConstraintNode>(), "Missing column " << *it << " in non empty input type");
  1035. }
  1036. break;
  1037. }
  1038. if (childNdx < input->ChildrenSize())
  1039. it = content[i].first.erase(it);
  1040. else
  1041. ++it;
  1042. }
  1043. if (content[i].first.empty()) {
  1044. content.resize(i);
  1045. break;
  1046. }
  1047. }
  1048. sort = content.empty() ? nullptr : ctx.MakeConstraint<TSortedConstraintNode>(std::move(content));
  1049. }
  1050. if (sort) {
  1051. input->AddConstraint(sort);
  1052. }
  1053. }
  1054. return ExtendWrap<false>(input, output, ctx);
  1055. }
  1056. TStatus TakeWrap(const TExprNode::TPtr& input, TExprNode::TPtr& output, TExprContext& ctx) const {
  1057. if (input->Tail().IsCallable("Uint64") && !FromString<ui64>(input->Tail().Head().Content())) {
  1058. input->AddConstraint(ctx.MakeConstraint<TEmptyConstraintNode>());
  1059. }
  1060. return CopyAllFrom<0>(input, output, ctx);
  1061. }
  1062. TStatus MemberWrap(const TExprNode::TPtr& input, TExprNode::TPtr& /*output*/, TExprContext& ctx) const {
  1063. const auto& memberName = input->Tail().Content();
  1064. const auto& structNode = input->Head();
  1065. if (const auto emptyConstraint = structNode.GetConstraint<TEmptyConstraintNode>()) {
  1066. input->AddConstraint(emptyConstraint);
  1067. } else {
  1068. if (const auto part = structNode.GetConstraint<TPartOfSortedConstraintNode>()) {
  1069. if (const auto extracted = part->ExtractField(ctx, memberName)) {
  1070. input->AddConstraint(extracted);
  1071. }
  1072. }
  1073. if (const auto part = structNode.GetConstraint<TPartOfChoppedConstraintNode>()) {
  1074. if (const auto extracted = part->ExtractField(ctx, memberName)) {
  1075. input->AddConstraint(extracted);
  1076. }
  1077. }
  1078. if (const auto part = structNode.GetConstraint<TPartOfUniqueConstraintNode>()) {
  1079. if (const auto extracted = part->ExtractField(ctx, memberName)) {
  1080. input->AddConstraint(extracted);
  1081. }
  1082. }
  1083. if (const auto part = structNode.GetConstraint<TPartOfDistinctConstraintNode>()) {
  1084. if (const auto extracted = part->ExtractField(ctx, memberName)) {
  1085. input->AddConstraint(extracted);
  1086. }
  1087. }
  1088. }
  1089. if (structNode.IsCallable("AsStruct")) {
  1090. for (const auto& child: structNode.Children()) {
  1091. if (child->Head().IsAtom(memberName)) {
  1092. TApplyConstraintFromInput<1, TVarIndexConstraintNode>::Do(child);
  1093. break;
  1094. }
  1095. }
  1096. } else {
  1097. TApplyConstraintFromInput<0, TVarIndexConstraintNode>::Do(input);
  1098. }
  1099. return TStatus::Ok;
  1100. }
  1101. TStatus AsTupleWrap(const TExprNode::TPtr& input, TExprNode::TPtr& /*output*/, TExprContext& ctx) const {
  1102. TPartOfSortedConstraintNode::TMapType sorted;
  1103. TPartOfChoppedConstraintNode::TMapType chopped;
  1104. TPartOfUniqueConstraintNode::TMapType uniques;
  1105. TPartOfDistinctConstraintNode::TMapType distincts;
  1106. std::vector<const TConstraintSet*> structConstraints;
  1107. for (auto i = 0U; i < input->ChildrenSize(); ++i) {
  1108. const auto child = input->Child(i);
  1109. const auto& name = ctx.GetIndexAsString(i);
  1110. if (const auto part = child->GetConstraint<TPartOfSortedConstraintNode>()) {
  1111. TPartOfSortedConstraintNode::UniqueMerge(sorted, part->GetColumnMapping(name));
  1112. }
  1113. if (const auto part = child->GetConstraint<TPartOfChoppedConstraintNode>()) {
  1114. TPartOfChoppedConstraintNode::UniqueMerge(chopped, part->GetColumnMapping(name));
  1115. }
  1116. if (const auto part = child->GetConstraint<TPartOfUniqueConstraintNode>()) {
  1117. TPartOfUniqueConstraintNode::UniqueMerge(uniques, part->GetColumnMapping(name));
  1118. }
  1119. if (const auto part = child->GetConstraint<TPartOfDistinctConstraintNode>()) {
  1120. TPartOfDistinctConstraintNode::UniqueMerge(distincts, part->GetColumnMapping(name));
  1121. }
  1122. if (const auto& valueNode = SkipModifiers(child); TCoMember::Match(valueNode) || TCoNth::Match(valueNode)) {
  1123. structConstraints.push_back(&valueNode->Head().GetConstraintSet());
  1124. } else if (valueNode->IsArgument() && ETypeAnnotationKind::Struct != valueNode->GetTypeAnn()->GetKind()) {
  1125. structConstraints.push_back(&valueNode->GetConstraintSet());
  1126. }
  1127. }
  1128. if (!sorted.empty()) {
  1129. input->AddConstraint(ctx.MakeConstraint<TPartOfSortedConstraintNode>(std::move(sorted)));
  1130. }
  1131. if (!chopped.empty()) {
  1132. input->AddConstraint(ctx.MakeConstraint<TPartOfChoppedConstraintNode>(std::move(chopped)));
  1133. }
  1134. if (!uniques.empty()) {
  1135. input->AddConstraint(ctx.MakeConstraint<TPartOfUniqueConstraintNode>(std::move(uniques)));
  1136. }
  1137. if (!distincts.empty()) {
  1138. input->AddConstraint(ctx.MakeConstraint<TPartOfDistinctConstraintNode>(std::move(distincts)));
  1139. }
  1140. if (const auto varIndex = TVarIndexConstraintNode::MakeCommon(structConstraints, ctx)) {
  1141. input->AddConstraint(varIndex);
  1142. }
  1143. return TStatus::Ok;
  1144. }
  1145. TStatus AsStructWrap(const TExprNode::TPtr& input, TExprNode::TPtr& /*output*/, TExprContext& ctx) const {
  1146. TPartOfSortedConstraintNode::TMapType sorted;
  1147. TPartOfChoppedConstraintNode::TMapType chopped;
  1148. TPartOfUniqueConstraintNode::TMapType uniques;
  1149. TPartOfDistinctConstraintNode::TMapType distincts;
  1150. std::vector<const TConstraintSet*> structConstraints;
  1151. for (const auto& child : input->Children()) {
  1152. const auto& name = child->Head().Content();
  1153. if (const auto part = child->Tail().GetConstraint<TPartOfSortedConstraintNode>()) {
  1154. TPartOfSortedConstraintNode::UniqueMerge(sorted, part->GetColumnMapping(name));
  1155. }
  1156. if (const auto part = child->Tail().GetConstraint<TPartOfChoppedConstraintNode>()) {
  1157. TPartOfChoppedConstraintNode::UniqueMerge(chopped, part->GetColumnMapping(name));
  1158. }
  1159. if (const auto part = child->Tail().GetConstraint<TPartOfUniqueConstraintNode>()) {
  1160. TPartOfUniqueConstraintNode::UniqueMerge(uniques, part->GetColumnMapping(name));
  1161. }
  1162. if (const auto part = child->Tail().GetConstraint<TPartOfDistinctConstraintNode>()) {
  1163. TPartOfDistinctConstraintNode::UniqueMerge(distincts, part->GetColumnMapping(name));
  1164. }
  1165. if (const auto valueNode = SkipModifiers(&child->Tail()); TCoMember::Match(valueNode) || TCoNth::Match(valueNode)) {
  1166. structConstraints.push_back(&valueNode->Head().GetConstraintSet());
  1167. } else if (valueNode->Type() == TExprNode::Argument) {
  1168. structConstraints.push_back(&valueNode->GetConstraintSet());
  1169. }
  1170. }
  1171. if (!sorted.empty()) {
  1172. input->AddConstraint(ctx.MakeConstraint<TPartOfSortedConstraintNode>(std::move(sorted)));
  1173. }
  1174. if (!chopped.empty()) {
  1175. input->AddConstraint(ctx.MakeConstraint<TPartOfChoppedConstraintNode>(std::move(chopped)));
  1176. }
  1177. if (!uniques.empty()) {
  1178. input->AddConstraint(ctx.MakeConstraint<TPartOfUniqueConstraintNode>(std::move(uniques)));
  1179. }
  1180. if (!distincts.empty()) {
  1181. input->AddConstraint(ctx.MakeConstraint<TPartOfDistinctConstraintNode>(std::move(distincts)));
  1182. }
  1183. if (const auto varIndex = TVarIndexConstraintNode::MakeCommon(structConstraints, ctx)) {
  1184. input->AddConstraint(varIndex);
  1185. }
  1186. return TStatus::Ok;
  1187. }
  1188. TStatus FlattenMembersWrap(const TExprNode::TPtr& input, TExprNode::TPtr& /*output*/, TExprContext& ctx) const {
  1189. TPartOfSortedConstraintNode::TMapType sorted;
  1190. TPartOfChoppedConstraintNode::TMapType chopped;
  1191. TPartOfUniqueConstraintNode::TMapType uniques;
  1192. TPartOfDistinctConstraintNode::TMapType distincts;
  1193. for (const auto& child : input->Children()) {
  1194. const auto& prefix = child->Head().Content();
  1195. if (const auto part = child->Tail().GetConstraint<TPartOfSortedConstraintNode>()) {
  1196. TPartOfSortedConstraintNode::UniqueMerge(sorted, part->GetColumnMapping(ctx, prefix));
  1197. }
  1198. if (const auto part = child->Tail().GetConstraint<TPartOfChoppedConstraintNode>()) {
  1199. TPartOfChoppedConstraintNode::UniqueMerge(chopped, part->GetColumnMapping(ctx, prefix));
  1200. }
  1201. if (const auto part = child->Tail().GetConstraint<TPartOfUniqueConstraintNode>()) {
  1202. TPartOfUniqueConstraintNode::UniqueMerge(uniques, part->GetColumnMapping(ctx, prefix));
  1203. }
  1204. if (const auto part = child->Tail().GetConstraint<TPartOfDistinctConstraintNode>()) {
  1205. TPartOfDistinctConstraintNode::UniqueMerge(distincts, part->GetColumnMapping(ctx, prefix));
  1206. }
  1207. }
  1208. if (!sorted.empty()) {
  1209. input->AddConstraint(ctx.MakeConstraint<TPartOfSortedConstraintNode>(std::move(sorted)));
  1210. }
  1211. if (!chopped.empty()) {
  1212. input->AddConstraint(ctx.MakeConstraint<TPartOfChoppedConstraintNode>(std::move(chopped)));
  1213. }
  1214. if (!uniques.empty()) {
  1215. input->AddConstraint(ctx.MakeConstraint<TPartOfUniqueConstraintNode>(std::move(uniques)));
  1216. }
  1217. if (!distincts.empty()) {
  1218. input->AddConstraint(ctx.MakeConstraint<TPartOfDistinctConstraintNode>(std::move(distincts)));
  1219. }
  1220. return TStatus::Ok;
  1221. }
  1222. template<class TPartOfConstraint>
  1223. static void AddPartOf(const TExprNode::TPtr& input, TExprContext& ctx) {
  1224. typename TPartOfConstraint::TMapType map;
  1225. if (const auto part = input->Head().GetConstraint<TPartOfConstraint>()) {
  1226. map = part->GetColumnMapping();
  1227. }
  1228. if (const auto part = input->Tail().GetConstraint<TPartOfConstraint>()) {
  1229. TPartOfConstraint::UniqueMerge(map, part->GetColumnMapping(input->Child(1)->Content()));
  1230. }
  1231. if (!map.empty()) {
  1232. input->AddConstraint(ctx.MakeConstraint<TPartOfConstraint>(std::move(map)));
  1233. }
  1234. }
  1235. template<class TPartOfConstraint>
  1236. static void ReplacePartOf(const TExprNode::TPtr& input, TExprContext& ctx) {
  1237. typename TPartOfConstraint::TMapType sorted;
  1238. const auto& name = input->Child(1)->Content();
  1239. if (const auto part = input->Head().GetConstraint<TPartOfConstraint>()) {
  1240. if (const auto filtered = part->FilterFields(ctx, [&name](const TPartOfConstraintBase::TPathType& path) { return !path.empty() && path.front() != name; })) {
  1241. sorted = filtered->GetColumnMapping();
  1242. }
  1243. }
  1244. if (const auto part = input->Tail().GetConstraint<TPartOfConstraint>()) {
  1245. TPartOfConstraint::UniqueMerge(sorted, part->GetColumnMapping(name));
  1246. }
  1247. if (!sorted.empty()) {
  1248. input->AddConstraint(ctx.MakeConstraint<TPartOfConstraint>(std::move(sorted)));
  1249. }
  1250. }
  1251. TStatus AddMemberWrap(const TExprNode::TPtr& input, TExprNode::TPtr& /*output*/, TExprContext& ctx) const {
  1252. const auto& addStructNode = input->Head();
  1253. const auto& extraFieldNode = input->Tail();
  1254. if (const auto emptyConstraint = addStructNode.GetConstraint<TEmptyConstraintNode>()) {
  1255. input->AddConstraint(emptyConstraint);
  1256. }
  1257. AddPartOf<TPartOfSortedConstraintNode>(input, ctx);
  1258. AddPartOf<TPartOfChoppedConstraintNode>(input, ctx);
  1259. AddPartOf<TPartOfUniqueConstraintNode>(input, ctx);
  1260. AddPartOf<TPartOfDistinctConstraintNode>(input, ctx);
  1261. TVector<const TConstraintSet*> structConstraints;
  1262. structConstraints.push_back(&addStructNode.GetConstraintSet());
  1263. if (const auto& valueNode = SkipModifiers(&extraFieldNode); TCoMember::Match(valueNode) || TCoNth::Match(valueNode)) {
  1264. structConstraints.push_back(&valueNode->Head().GetConstraintSet());
  1265. } else if (valueNode->Type() == TExprNode::Argument) {
  1266. structConstraints.push_back(&valueNode->GetConstraintSet());
  1267. }
  1268. if (const auto varIndex = TVarIndexConstraintNode::MakeCommon(structConstraints, ctx)) {
  1269. input->AddConstraint(varIndex);
  1270. }
  1271. return TStatus::Ok;
  1272. }
  1273. TStatus RemoveMemberWrap(const TExprNode::TPtr& input, TExprNode::TPtr& output, TExprContext& ctx) const {
  1274. const auto& name = input->Tail().Content();
  1275. const auto filter = [&name](const TPartOfConstraintBase::TPathType& path) { return !path.empty() && path.front() != name; };
  1276. FilterFromHead<TPartOfSortedConstraintNode>(input, filter, ctx);
  1277. FilterFromHead<TPartOfChoppedConstraintNode>(input, filter, ctx);
  1278. FilterFromHead<TPartOfUniqueConstraintNode>(input, filter, ctx);
  1279. FilterFromHead<TPartOfDistinctConstraintNode>(input, filter, ctx);
  1280. return FromFirst<TVarIndexConstraintNode>(input, output, ctx);
  1281. }
  1282. TStatus ReplaceMemberWrap(const TExprNode::TPtr& input, TExprNode::TPtr& /*output*/, TExprContext& ctx) const {
  1283. TVector<const TConstraintSet*> structConstraints;
  1284. structConstraints.push_back(&input->Head().GetConstraintSet());
  1285. ReplacePartOf<TPartOfSortedConstraintNode>(input, ctx);
  1286. ReplacePartOf<TPartOfChoppedConstraintNode>(input, ctx);
  1287. ReplacePartOf<TPartOfUniqueConstraintNode>(input, ctx);
  1288. ReplacePartOf<TPartOfDistinctConstraintNode>(input, ctx);
  1289. if (const auto varIndex = TVarIndexConstraintNode::MakeCommon(structConstraints, ctx)) {
  1290. input->AddConstraint(varIndex);
  1291. }
  1292. return TStatus::Ok;
  1293. }
  1294. TStatus ListWrap(const TExprNode::TPtr& input, TExprNode::TPtr& output, TExprContext& ctx) const {
  1295. switch (input->ChildrenSize()) {
  1296. case 1:
  1297. return FromEmpty(input, output, ctx);
  1298. case 2:
  1299. return FromSecond<TUniqueConstraintNode, TPartOfUniqueConstraintNode, TDistinctConstraintNode, TPartOfDistinctConstraintNode, TVarIndexConstraintNode, TMultiConstraintNode>(input, output, ctx);
  1300. default:
  1301. break;
  1302. }
  1303. return CommonFromChildren<1, TVarIndexConstraintNode, TMultiConstraintNode>(input, output, ctx);
  1304. }
  1305. template<bool Ordered, bool WideInput, bool WithUnique = true>
  1306. static TSmallVec<TConstraintNode::TListType> GetConstraintsForInputArgument(const TExprNode& node, TExprContext& ctx) {
  1307. TSmallVec<TConstraintNode::TListType> argsConstraints(WideInput ? node.Child(1U)->Head().ChildrenSize() : 1U);
  1308. if constexpr (WideInput) {
  1309. if constexpr (Ordered) {
  1310. if (const auto& mapping = TPartOfSortedConstraintNode::GetCommonMapping(node.Head().GetConstraint<TSortedConstraintNode>(), node.Head().GetConstraint<TPartOfSortedConstraintNode>()); !mapping.empty()) {
  1311. for (ui32 i = 0U; i < argsConstraints.size(); ++i) {
  1312. if (auto extracted = TPartOfSortedConstraintNode::ExtractField(mapping, ctx.GetIndexAsString(i)); !extracted.empty()) {
  1313. argsConstraints[i].emplace_back(ctx.MakeConstraint<TPartOfSortedConstraintNode>(std::move(extracted)));
  1314. }
  1315. }
  1316. }
  1317. if (const auto& mapping = TPartOfChoppedConstraintNode::GetCommonMapping(node.Head().GetConstraint<TChoppedConstraintNode>(), node.Head().GetConstraint<TPartOfChoppedConstraintNode>()); !mapping.empty()) {
  1318. for (ui32 i = 0U; i < argsConstraints.size(); ++i) {
  1319. if (auto extracted = TPartOfChoppedConstraintNode::ExtractField(mapping, ctx.GetIndexAsString(i)); !extracted.empty()) {
  1320. argsConstraints[i].emplace_back(ctx.MakeConstraint<TPartOfChoppedConstraintNode>(std::move(extracted)));
  1321. }
  1322. }
  1323. }
  1324. }
  1325. if constexpr (WithUnique) {
  1326. if (const auto& mapping = TPartOfUniqueConstraintNode::GetCommonMapping(node.Head().GetConstraint<TUniqueConstraintNode>(), node.Head().GetConstraint<TPartOfUniqueConstraintNode>()); !mapping.empty()) {
  1327. for (ui32 i = 0U; i < argsConstraints.size(); ++i) {
  1328. if (auto extracted = TPartOfUniqueConstraintNode::ExtractField(mapping, ctx.GetIndexAsString(i)); !extracted.empty()) {
  1329. argsConstraints[i].emplace_back(ctx.MakeConstraint<TPartOfUniqueConstraintNode>(std::move(extracted)));
  1330. }
  1331. }
  1332. }
  1333. if (const auto& mapping = TPartOfDistinctConstraintNode::GetCommonMapping(node.Head().GetConstraint<TDistinctConstraintNode>(), node.Head().GetConstraint<TPartOfDistinctConstraintNode>()); !mapping.empty()) {
  1334. for (ui32 i = 0U; i < argsConstraints.size(); ++i) {
  1335. if (auto extracted = TPartOfDistinctConstraintNode::ExtractField(mapping, ctx.GetIndexAsString(i)); !extracted.empty()) {
  1336. argsConstraints[i].emplace_back(ctx.MakeConstraint<TPartOfDistinctConstraintNode>(std::move(extracted)));
  1337. }
  1338. }
  1339. }
  1340. }
  1341. } else {
  1342. if (const auto inItemType = GetSeqItemType(node.Head().GetTypeAnn())) {
  1343. if (inItemType->GetKind() == ETypeAnnotationKind::Variant) {
  1344. if (inItemType->Cast<TVariantExprType>()->GetUnderlyingType()->GetKind() == ETypeAnnotationKind::Tuple) {
  1345. const auto tupleType = inItemType->Cast<TVariantExprType>()->GetUnderlyingType()->Cast<TTupleExprType>();
  1346. argsConstraints.front().push_back(ctx.MakeConstraint<TVarIndexConstraintNode>(*inItemType->Cast<TVariantExprType>()));
  1347. TMultiConstraintNode::TMapType multiItems;
  1348. multiItems.reserve(tupleType->GetSize());
  1349. for (size_t i = 0; i < tupleType->GetSize(); ++i) {
  1350. multiItems.emplace_back(i, TConstraintSet{});
  1351. if constexpr (WithUnique) {
  1352. const auto inputMulti = node.Head().GetConstraint<TMultiConstraintNode>();
  1353. if (const auto inputConstr = inputMulti ? inputMulti->GetItem(i) : nullptr) {
  1354. if (auto mapping = TPartOfUniqueConstraintNode::GetCommonMapping(inputConstr->GetConstraint<TUniqueConstraintNode>(), inputConstr->GetConstraint<TPartOfUniqueConstraintNode>()); !mapping.empty()) {
  1355. multiItems.back().second.AddConstraint(ctx.MakeConstraint<TPartOfUniqueConstraintNode>(std::move(mapping)));
  1356. }
  1357. if (auto mapping = TPartOfDistinctConstraintNode::GetCommonMapping(inputConstr->GetConstraint<TDistinctConstraintNode>(), inputConstr->GetConstraint<TPartOfDistinctConstraintNode>()); !mapping.empty()) {
  1358. multiItems.back().second.AddConstraint(ctx.MakeConstraint<TPartOfDistinctConstraintNode>(std::move(mapping)));
  1359. }
  1360. }
  1361. }
  1362. }
  1363. if (!multiItems.empty()) {
  1364. argsConstraints.front().emplace_back(ctx.MakeConstraint<TMultiConstraintNode>(std::move(multiItems)));
  1365. }
  1366. }
  1367. } else {
  1368. if constexpr (Ordered) {
  1369. if (auto mapping = TPartOfSortedConstraintNode::GetCommonMapping(GetDetailed(node.Head().GetConstraint<TSortedConstraintNode>(), *node.Head().GetTypeAnn(), ctx), node.Head().GetConstraint<TPartOfSortedConstraintNode>()); !mapping.empty()) {
  1370. argsConstraints.front().emplace_back(ctx.MakeConstraint<TPartOfSortedConstraintNode>(std::move(mapping)));
  1371. }
  1372. if (auto mapping = TPartOfChoppedConstraintNode::GetCommonMapping(GetDetailed(node.Head().GetConstraint<TChoppedConstraintNode>(), *node.Head().GetTypeAnn(), ctx), node.Head().GetConstraint<TPartOfChoppedConstraintNode>()); !mapping.empty()) {
  1373. argsConstraints.front().emplace_back(ctx.MakeConstraint<TPartOfChoppedConstraintNode>(std::move(mapping)));
  1374. }
  1375. }
  1376. if constexpr (WithUnique) {
  1377. if (auto mapping = TPartOfUniqueConstraintNode::GetCommonMapping(GetDetailed(node.Head().GetConstraint<TUniqueConstraintNode>(), *node.Head().GetTypeAnn(), ctx), node.Head().GetConstraint<TPartOfUniqueConstraintNode>()); !mapping.empty()) {
  1378. argsConstraints.front().emplace_back(ctx.MakeConstraint<TPartOfUniqueConstraintNode>(std::move(mapping)));
  1379. }
  1380. if (auto mapping = TPartOfDistinctConstraintNode::GetCommonMapping(GetDetailed(node.Head().GetConstraint<TDistinctConstraintNode>(), *node.Head().GetTypeAnn(), ctx), node.Head().GetConstraint<TPartOfDistinctConstraintNode>()); !mapping.empty()) {
  1381. argsConstraints.front().emplace_back(ctx.MakeConstraint<TPartOfDistinctConstraintNode>(std::move(mapping)));
  1382. }
  1383. }
  1384. }
  1385. }
  1386. }
  1387. return argsConstraints;
  1388. }
  1389. TStatus DictWrap(const TExprNode::TPtr& input, TExprNode::TPtr& output, TExprContext& ctx) const {
  1390. const std::vector<std::string_view> k(1U, ctx.GetIndexAsString(0U));
  1391. input->AddConstraint(ctx.MakeConstraint<TUniqueConstraintNode>(k));
  1392. input->AddConstraint(ctx.MakeConstraint<TDistinctConstraintNode>(k));
  1393. if (input->ChildrenSize() == 1) {
  1394. return FromEmpty(input, output, ctx);
  1395. }
  1396. return TStatus::Ok;
  1397. }
  1398. TStatus DictFromKeysWrap(const TExprNode::TPtr& input, TExprNode::TPtr& /*output*/, TExprContext& ctx) const {
  1399. if (input->Child(1)->ChildrenSize() == 0) {
  1400. input->AddConstraint(ctx.MakeConstraint<TEmptyConstraintNode>());
  1401. }
  1402. const std::vector<std::string_view> k(1U, ctx.GetIndexAsString(0U));
  1403. input->AddConstraint(ctx.MakeConstraint<TUniqueConstraintNode>(k));
  1404. input->AddConstraint(ctx.MakeConstraint<TDistinctConstraintNode>(k));
  1405. return TStatus::Ok;
  1406. }
  1407. template<bool IsList, bool IsFlat>
  1408. TStatus PassOrEmptyWrap(const TExprNode::TPtr& input, TExprNode::TPtr& output, TExprContext& ctx) const {
  1409. if (const auto part = input->Tail().GetConstraint<TPartOfSortedConstraintNode>())
  1410. if (const auto filtered = part->CompleteOnly(ctx))
  1411. input->AddConstraint(filtered);
  1412. if (const auto part = input->Tail().GetConstraint<TPartOfChoppedConstraintNode>())
  1413. if (const auto filtered = part->CompleteOnly(ctx))
  1414. input->AddConstraint(filtered);
  1415. if (const auto part = input->Tail().GetConstraint<TPartOfDistinctConstraintNode>())
  1416. if (const auto filtered = part->CompleteOnly(ctx))
  1417. input->AddConstraint(filtered);
  1418. if (const auto part = input->Tail().GetConstraint<TPartOfUniqueConstraintNode>())
  1419. if constexpr (IsList) {
  1420. if (const auto filtered = part->CompleteOnly(ctx))
  1421. input->AddConstraint(filtered);
  1422. } else
  1423. input->AddConstraint(part);
  1424. if constexpr (IsFlat) {
  1425. if (const auto empty = input->Tail().GetConstraint<TEmptyConstraintNode>())
  1426. input->AddConstraint(empty);
  1427. }
  1428. return FromSecond<TUniqueConstraintNode, TDistinctConstraintNode, TSortedConstraintNode, TChoppedConstraintNode, TVarIndexConstraintNode, TMultiConstraintNode>(input, output, ctx);
  1429. }
  1430. TStatus IfWrap(const TExprNode::TPtr& input, TExprNode::TPtr&, TExprContext& ctx) const {
  1431. std::vector<const TConstraintSet*> constraints;
  1432. constraints.reserve((input->ChildrenSize() << 1U) + 1U);
  1433. constraints.emplace_back(&input->Tail().GetConstraintSet());
  1434. for (auto i = 0U; i < input->ChildrenSize() - 1U; ++i) {
  1435. constraints.emplace_back(&input->Child(++i)->GetConstraintSet());
  1436. }
  1437. if (constraints.empty())
  1438. input->AddConstraint(ctx.MakeConstraint<TEmptyConstraintNode>());
  1439. else if (1U == constraints.size())
  1440. input->SetConstraints(**constraints.cbegin());
  1441. else
  1442. TApplyCommonConstraint<TSortedConstraintNode
  1443. , TPartOfSortedConstraintNode
  1444. , TChoppedConstraintNode
  1445. , TPartOfChoppedConstraintNode
  1446. , TUniqueConstraintNode
  1447. , TPartOfUniqueConstraintNode
  1448. , TDistinctConstraintNode
  1449. , TPartOfDistinctConstraintNode
  1450. , TEmptyConstraintNode
  1451. , TVarIndexConstraintNode
  1452. , TMultiConstraintNode
  1453. >::Do(input, constraints, ctx);
  1454. return TStatus::Ok;
  1455. }
  1456. TStatus IfPresentWrap(const TExprNode::TPtr& input, TExprNode::TPtr&, TExprContext& ctx) const {
  1457. auto optionals = input->ChildrenList();
  1458. const auto lambdaIndex = optionals.size() - 2U;
  1459. auto lambda = std::move(optionals[lambdaIndex]);
  1460. optionals.resize(lambdaIndex);
  1461. std::vector<const TConstraintNode::TListType> constraints;
  1462. constraints.reserve(optionals.size());
  1463. std::transform(optionals.cbegin(), optionals.cend(), std::back_inserter(constraints), [](const TExprNode::TPtr& node){ return node->GetAllConstraints(); });
  1464. if (const auto status = UpdateLambdaConstraints(input->ChildRef(lambdaIndex), ctx, constraints); status != TStatus::Ok) {
  1465. return status;
  1466. }
  1467. if (std::any_of(optionals.cbegin(), optionals.cend(), [] (const TExprNode::TPtr& node) { return bool(node->GetConstraint<TEmptyConstraintNode>()); })) {
  1468. input->CopyConstraints(input->Tail());
  1469. return TStatus::Ok;
  1470. }
  1471. const std::vector<const TConstraintSet*> both = { &lambda->GetConstraintSet(), &input->Tail().GetConstraintSet() };
  1472. TApplyCommonConstraint<TPartOfSortedConstraintNode
  1473. , TPartOfChoppedConstraintNode
  1474. , TPartOfUniqueConstraintNode
  1475. , TPartOfDistinctConstraintNode
  1476. , TEmptyConstraintNode
  1477. , TVarIndexConstraintNode
  1478. , TMultiConstraintNode
  1479. >::Do(input, both, ctx);
  1480. return TStatus::Ok;
  1481. }
  1482. TStatus ReverseWrap(const TExprNode::TPtr& input, TExprNode::TPtr& output, TExprContext& ctx) const {
  1483. if (const auto sorted = input->Head().GetConstraint<TSortedConstraintNode>()) {
  1484. auto content = sorted->GetContent();
  1485. std::for_each(content.begin(), content.end(), [](std::pair<TPartOfConstraintBase::TSetType, bool>& pair) { pair.second = !pair.second; });
  1486. input->AddConstraint(ctx.MakeConstraint<TSortedConstraintNode>(std::move(content)));
  1487. }
  1488. return FromFirst<TEmptyConstraintNode, TChoppedConstraintNode, TPartOfChoppedConstraintNode, TUniqueConstraintNode, TPartOfUniqueConstraintNode, TDistinctConstraintNode, TPartOfDistinctConstraintNode, TVarIndexConstraintNode, TMultiConstraintNode>(input, output, ctx);
  1489. }
  1490. TStatus SwitchWrap(const TExprNode::TPtr& input, TExprNode::TPtr& output, TExprContext& ctx) const {
  1491. TStatus status = TStatus::Ok;
  1492. TDynBitMap outFromChildren; // children, from which take a multi constraint for output
  1493. if (const auto multi = input->Head().GetConstraint<TMultiConstraintNode>()) {
  1494. for (size_t i = 2; i < input->ChildrenSize(); ++i) {
  1495. TMultiConstraintNode::TMapType items;
  1496. ui32 lambdaInputIndex = 0;
  1497. for (auto& child : input->Child(i)->Children()) {
  1498. const ui32 index = FromString<ui32>(child->Content());
  1499. if (auto c = multi->GetItem(index)) {
  1500. items[lambdaInputIndex] = *c;
  1501. outFromChildren.Set(i + 1);
  1502. }
  1503. ++lambdaInputIndex;
  1504. }
  1505. TConstraintNode::TListType argConstraints;
  1506. if (!items.empty()) {
  1507. if (input->Child(i)->ChildrenSize() > 1) {
  1508. argConstraints.push_back(ctx.MakeConstraint<TMultiConstraintNode>(std::move(items)));
  1509. argConstraints.push_back(ctx.MakeConstraint<TVarIndexConstraintNode>(input->Child(i)->ChildrenSize()));
  1510. } else {
  1511. argConstraints = items.front().second.GetAllConstraints();
  1512. }
  1513. }
  1514. status = status.Combine(UpdateLambdaConstraints(input->ChildRef(++i), ctx, {argConstraints}));
  1515. }
  1516. } else {
  1517. const bool inVar = GetSeqItemType(*input->Head().GetTypeAnn()).GetKind() == ETypeAnnotationKind::Variant;
  1518. const TSmallVec<TConstraintNode::TListType> argConstraints(1U, inVar ? TConstraintNode::TListType() : input->Head().GetAllConstraints());
  1519. for (size_t i = 3; i < input->ChildrenSize(); i += 2) {
  1520. status = status.Combine(UpdateLambdaConstraints(input->ChildRef(i), ctx, argConstraints));
  1521. }
  1522. outFromChildren.Set(0, input->ChildrenSize());
  1523. }
  1524. if (status != TStatus::Ok) {
  1525. return status;
  1526. }
  1527. const auto inputVarIndex = input->Head().GetConstraint<TVarIndexConstraintNode>();
  1528. const bool emptyInput = input->Head().GetConstraint<TEmptyConstraintNode>();
  1529. if (GetSeqItemType(*input->GetTypeAnn()).GetKind() == ETypeAnnotationKind::Variant) {
  1530. ui32 outIndexOffset = 0;
  1531. TMultiConstraintNode::TMapType multiItems;
  1532. TVarIndexConstraintNode::TMapType remapItems;
  1533. bool emptyOut = true;
  1534. for (size_t i = 2; i < input->ChildrenSize(); i += 2) {
  1535. const auto lambda = input->Child(i + 1);
  1536. const auto& lambdaItemType = GetSeqItemType(*lambda->GetTypeAnn());
  1537. if (inputVarIndex) {
  1538. if (auto varIndex = lambda->GetConstraint<TVarIndexConstraintNode>()) {
  1539. for (auto& item: varIndex->GetIndexMapping()) {
  1540. YQL_ENSURE(item.second < input->Child(i)->ChildrenSize());
  1541. const auto srcIndex = FromString<size_t>(input->Child(i)->Child(item.second)->Content());
  1542. remapItems.push_back(std::make_pair(outIndexOffset + item.first, srcIndex));
  1543. }
  1544. } else if (lambdaItemType.GetKind() == ETypeAnnotationKind::Variant && input->Child(i)->ChildrenSize() == 1) {
  1545. const auto srcIndex = FromString<size_t>(input->Child(i)->Head().Content());
  1546. for (size_t j = 0; j < lambdaItemType.Cast<TVariantExprType>()->GetUnderlyingType()->Cast<TTupleExprType>()->GetSize(); ++j) {
  1547. remapItems.push_back(std::make_pair(outIndexOffset + j, srcIndex));
  1548. }
  1549. } else if (lambdaItemType.GetKind() != ETypeAnnotationKind::Variant && input->Child(i)->ChildrenSize() > 1) {
  1550. for (auto& child : input->Child(i)->Children()) {
  1551. const auto srcIndex = FromString<size_t>(child->Content());
  1552. remapItems.push_back(std::make_pair(outIndexOffset, srcIndex));
  1553. }
  1554. }
  1555. }
  1556. const bool lambdaEmpty = lambda->GetConstraint<TEmptyConstraintNode>();
  1557. if (!lambdaEmpty) {
  1558. emptyOut = false;
  1559. }
  1560. if (lambdaItemType.GetKind() == ETypeAnnotationKind::Variant) {
  1561. if (!emptyInput && outFromChildren.Test(i + 1)) {
  1562. if (auto multi = lambda->GetConstraint<TMultiConstraintNode>()) {
  1563. for (auto& item: multi->GetItems()) {
  1564. multiItems.insert_unique(std::make_pair(outIndexOffset + item.first, item.second));
  1565. }
  1566. }
  1567. }
  1568. outIndexOffset += lambdaItemType.Cast<TVariantExprType>()->GetUnderlyingType()->Cast<TTupleExprType>()->GetSize();
  1569. } else {
  1570. if (!emptyInput && outFromChildren.Test(i + 1) && !lambdaEmpty) {
  1571. multiItems[outIndexOffset] = lambda->GetConstraintSet();
  1572. }
  1573. ++outIndexOffset;
  1574. }
  1575. }
  1576. if (inputVarIndex && !remapItems.empty()) {
  1577. TVarIndexConstraintNode::TMapType result;
  1578. for (auto& item: remapItems) {
  1579. auto range = inputVarIndex->GetIndexMapping().equal_range(item.second);
  1580. for (auto it = range.first; it != range.second; ++it) {
  1581. result.push_back(std::make_pair(item.first, it->second));
  1582. }
  1583. }
  1584. if (!result.empty()) {
  1585. ::Sort(result);
  1586. input->AddConstraint(ctx.MakeConstraint<TVarIndexConstraintNode>(std::move(result)));
  1587. }
  1588. }
  1589. if (!multiItems.empty()) {
  1590. input->AddConstraint(ctx.MakeConstraint<TMultiConstraintNode>(std::move(multiItems)));
  1591. }
  1592. if (emptyOut) {
  1593. input->AddConstraint(ctx.MakeConstraint<TEmptyConstraintNode>());
  1594. }
  1595. } else {
  1596. YQL_ENSURE(input->ChildrenSize() == 4);
  1597. input->CopyConstraints(*input->Child(3));
  1598. }
  1599. return FromFirst<TEmptyConstraintNode>(input, output, ctx);
  1600. }
  1601. TStatus VisitWrap(const TExprNode::TPtr& input, TExprNode::TPtr&, TExprContext& ctx) const {
  1602. TStatus status = TStatus::Ok;
  1603. TDynBitMap outFromChildren; // children, from which take a multi constraint for output
  1604. TDynBitMap usedAlts;
  1605. const auto inMulti = input->Head().GetConstraint<TMultiConstraintNode>();
  1606. for (ui32 i = 1; i < input->ChildrenSize(); ++i) {
  1607. if (const auto child = input->Child(i); child->IsAtom()) {
  1608. TSmallVec<TConstraintNode::TListType> argConstraints(1U);
  1609. if (inMulti) {
  1610. const auto index = FromString<ui32>(child->Content());
  1611. usedAlts.Set(index);
  1612. if (const auto c = inMulti->GetItem(index)) {
  1613. argConstraints.front() = c->GetAllConstraints();
  1614. outFromChildren.Set(i + 1U);
  1615. }
  1616. }
  1617. status = status.Combine(UpdateLambdaConstraints(input->ChildRef(++i), ctx, argConstraints));
  1618. } else if (inMulti) { // Check that we can fall to default branch
  1619. for (auto& item: inMulti->GetItems()) {
  1620. if (!usedAlts.Test(item.first)) {
  1621. outFromChildren.Set(i);
  1622. break;
  1623. }
  1624. }
  1625. }
  1626. }
  1627. if (status != TStatus::Ok) {
  1628. return status;
  1629. }
  1630. if (!inMulti) {
  1631. outFromChildren.Set(0, input->ChildrenSize());
  1632. }
  1633. auto outType = input->GetTypeAnn();
  1634. if (auto t = GetSeqItemType(outType)) {
  1635. outType = t;
  1636. }
  1637. if (outType->GetKind() == ETypeAnnotationKind::Variant && outType->Cast<TVariantExprType>()->GetUnderlyingType()->GetKind() == ETypeAnnotationKind::Tuple) {
  1638. TVector<const TConstraintSet*> outConstraints;
  1639. TVarIndexConstraintNode::TMapType remapItems;
  1640. for (ui32 i = 1; i < input->ChildrenSize(); ++i) {
  1641. if (input->Child(i)->IsAtom()) {
  1642. ui32 index = FromString<ui32>(input->Child(i)->Content());
  1643. ++i;
  1644. if (outFromChildren.Test(i)) {
  1645. outConstraints.push_back(&input->Child(i)->GetConstraintSet());
  1646. if (const auto outMulti = input->Child(i)->GetConstraint<TMultiConstraintNode>()) {
  1647. for (auto& item: outMulti->GetItems()) {
  1648. remapItems.push_back(std::make_pair(item.first, index));
  1649. }
  1650. }
  1651. }
  1652. } else {
  1653. if (outFromChildren.Test(i)) {
  1654. outConstraints.push_back(&input->Child(i)->GetConstraintSet());
  1655. const auto outMulti = input->Child(i)->GetConstraint<TMultiConstraintNode>();
  1656. if (outMulti && inMulti) {
  1657. for (auto& outItem: outMulti->GetItems()) {
  1658. for (auto& inItem: inMulti->GetItems()) {
  1659. if (!usedAlts.Test(inItem.first)) {
  1660. remapItems.push_back(std::make_pair(outItem.first, inItem.first));
  1661. }
  1662. }
  1663. }
  1664. }
  1665. }
  1666. }
  1667. }
  1668. if (auto multi = TMultiConstraintNode::MakeCommon(outConstraints, ctx)) {
  1669. input->AddConstraint(multi);
  1670. }
  1671. if (auto empty = TEmptyConstraintNode::MakeCommon(outConstraints, ctx)) {
  1672. input->AddConstraint(empty);
  1673. }
  1674. if (auto varIndex = input->Head().GetConstraint<TVarIndexConstraintNode>()) {
  1675. TVarIndexConstraintNode::TMapType varIndexItems;
  1676. for (auto& item: remapItems) {
  1677. const auto range = varIndex->GetIndexMapping().equal_range(item.second);
  1678. for (auto i = range.first; i != range.second; ++i) {
  1679. varIndexItems.push_back(std::make_pair(item.first, i->second));
  1680. }
  1681. }
  1682. if (!varIndexItems.empty()) {
  1683. ::Sort(varIndexItems);
  1684. input->AddConstraint(ctx.MakeConstraint<TVarIndexConstraintNode>(std::move(varIndexItems)));
  1685. }
  1686. }
  1687. }
  1688. else {
  1689. std::vector<const TConstraintSet*> nonEmpty;
  1690. for (ui32 i = 1; i < input->ChildrenSize(); ++i) {
  1691. if (input->Child(i)->IsAtom()) {
  1692. ++i;
  1693. }
  1694. if (outFromChildren.Test(i)) {
  1695. nonEmpty.push_back(&input->Child(i)->GetConstraintSet());
  1696. }
  1697. }
  1698. EraseIf(nonEmpty, [] (const TConstraintSet* c) { return !!c->GetConstraint<TEmptyConstraintNode>(); });
  1699. if (nonEmpty.empty()) {
  1700. input->AddConstraint(ctx.MakeConstraint<TEmptyConstraintNode>());
  1701. } else if (nonEmpty.size() == 1) {
  1702. input->SetConstraints(*nonEmpty.front());
  1703. }
  1704. if (auto varIndex = input->Head().GetConstraint<TVarIndexConstraintNode>()) {
  1705. TVarIndexConstraintNode::TMapType varIndexItems;
  1706. for (ui32 i = 1; i < input->ChildrenSize(); ++i) {
  1707. if (input->Child(i)->IsAtom()) {
  1708. const auto index = FromString<ui32>(input->Child(i++)->Content());
  1709. if (outFromChildren.Test(i) && IsDepended(input->Child(i)->Tail(), input->Child(i)->Head().Head())) { // Somehow depends on arg
  1710. const auto range = varIndex->GetIndexMapping().equal_range(index);
  1711. for (auto i = range.first; i != range.second; ++i) {
  1712. varIndexItems.push_back(std::make_pair(0, i->second));
  1713. }
  1714. }
  1715. }
  1716. else if (outFromChildren.Test(i)) {
  1717. if (inMulti) {
  1718. for (auto& inItem: inMulti->GetItems()) {
  1719. if (!usedAlts.Test(inItem.first)) {
  1720. auto range = varIndex->GetIndexMapping().equal_range(inItem.first);
  1721. for (auto i = range.first; i != range.second; ++i) {
  1722. varIndexItems.push_back(std::make_pair(0, i->second));
  1723. }
  1724. }
  1725. }
  1726. }
  1727. }
  1728. }
  1729. if (!varIndexItems.empty()) {
  1730. ::SortUnique(varIndexItems);
  1731. input->AddConstraint(ctx.MakeConstraint<TVarIndexConstraintNode>(std::move(varIndexItems)));
  1732. }
  1733. }
  1734. }
  1735. return TStatus::Ok;
  1736. }
  1737. TStatus VariantItemWrap(const TExprNode::TPtr& input, TExprNode::TPtr& /*output*/, TExprContext& ctx) const {
  1738. auto inputType = input->Head().GetTypeAnn();
  1739. if (inputType->GetKind() == ETypeAnnotationKind::Optional) {
  1740. inputType = inputType->Cast<TOptionalExprType>()->GetItemType();
  1741. }
  1742. const auto underlyingType = inputType->Cast<TVariantExprType>()->GetUnderlyingType();
  1743. if (underlyingType->GetKind() == ETypeAnnotationKind::Tuple) {
  1744. if (auto multi = input->Head().GetConstraint<TMultiConstraintNode>()) {
  1745. std::vector<TMultiConstraintNode::TMapType::value_type> nonEmpty;
  1746. std::copy_if(multi->GetItems().begin(), multi->GetItems().end(), std::back_inserter(nonEmpty),
  1747. [] (const TMultiConstraintNode::TMapType::value_type& v) {
  1748. return !v.second.GetConstraint<TEmptyConstraintNode>();
  1749. }
  1750. );
  1751. if (nonEmpty.empty()) {
  1752. input->AddConstraint(ctx.MakeConstraint<TEmptyConstraintNode>());
  1753. } else if (nonEmpty.size() == 1) {
  1754. input->SetConstraints(nonEmpty.front().second);
  1755. }
  1756. }
  1757. if (auto varIndex = input->Head().GetConstraint<TVarIndexConstraintNode>()) {
  1758. TVarIndexConstraintNode::TMapType varIndexItems;
  1759. for (auto& item: varIndex->GetIndexMapping()) {
  1760. varIndexItems.push_back(std::make_pair(0, item.second));
  1761. }
  1762. if (!varIndexItems.empty()) {
  1763. ::SortUnique(varIndexItems);
  1764. input->AddConstraint(ctx.MakeConstraint<TVarIndexConstraintNode>(std::move(varIndexItems)));
  1765. }
  1766. }
  1767. }
  1768. return TStatus::Ok;
  1769. }
  1770. TStatus VariantWrap(const TExprNode::TPtr& input, TExprNode::TPtr& /*output*/, TExprContext& ctx) const {
  1771. if (input->GetTypeAnn()->Cast<TVariantExprType>()->GetUnderlyingType()->GetKind() == ETypeAnnotationKind::Tuple) {
  1772. const auto index = FromString<ui32>(input->Child(1)->Content());
  1773. TConstraintSet target;
  1774. CopyExcept(target, input->Head().GetConstraintSet(), TVarIndexConstraintNode::Name());
  1775. input->AddConstraint(ctx.MakeConstraint<TMultiConstraintNode>(index, target));
  1776. if (auto varIndex = input->Head().GetConstraint<TVarIndexConstraintNode>()) {
  1777. TVarIndexConstraintNode::TMapType filteredItems;
  1778. for (auto& item: varIndex->GetIndexMapping()) {
  1779. filteredItems.push_back(std::make_pair(index, item.second));
  1780. }
  1781. input->AddConstraint(ctx.MakeConstraint<TVarIndexConstraintNode>(std::move(filteredItems)));
  1782. }
  1783. }
  1784. return TStatus::Ok;
  1785. }
  1786. TStatus GuessWrap(const TExprNode::TPtr& input, TExprNode::TPtr& /*output*/, TExprContext& ctx) const {
  1787. auto inputType = input->Head().GetTypeAnn();
  1788. if (inputType->GetKind() == ETypeAnnotationKind::Optional) {
  1789. inputType = inputType->Cast<TOptionalExprType>()->GetItemType();
  1790. }
  1791. const auto underlyingType = inputType->Cast<TVariantExprType>()->GetUnderlyingType();
  1792. if (underlyingType->GetKind() == ETypeAnnotationKind::Tuple) {
  1793. const auto guessIndex = FromString<ui32>(input->Child(1)->Content());
  1794. if (auto multi = input->Head().GetConstraint<TMultiConstraintNode>()) {
  1795. if (auto c = multi->GetItem(guessIndex)) {
  1796. input->SetConstraints(*c);
  1797. } else {
  1798. input->AddConstraint(ctx.MakeConstraint<TEmptyConstraintNode>());
  1799. }
  1800. }
  1801. if (auto varIndex = input->Head().GetConstraint<TVarIndexConstraintNode>()) {
  1802. TVarIndexConstraintNode::TMapType filteredItems;
  1803. for (auto& item: varIndex->GetIndexMapping()) {
  1804. if (item.first == guessIndex) {
  1805. filteredItems.push_back(std::make_pair(0, item.second));
  1806. }
  1807. }
  1808. if (!filteredItems.empty()) {
  1809. input->AddConstraint(ctx.MakeConstraint<TVarIndexConstraintNode>(std::move(filteredItems)));
  1810. }
  1811. }
  1812. }
  1813. return TStatus::Ok;
  1814. }
  1815. TStatus MuxWrap(const TExprNode::TPtr& input, TExprNode::TPtr& /*output*/, TExprContext& ctx) const {
  1816. const auto listItemType = GetSeqItemType(input->GetTypeAnn());
  1817. if (!listItemType) {
  1818. return TStatus::Ok;
  1819. }
  1820. if (listItemType->Cast<TVariantExprType>()->GetUnderlyingType()->GetKind() == ETypeAnnotationKind::Tuple) {
  1821. if (input->Head().IsList()) {
  1822. TMultiConstraintNode::TMapType items;
  1823. ui32 index = 0;
  1824. for (auto& child: input->Head().Children()) {
  1825. items.push_back(std::make_pair(index, child->GetConstraintSet()));
  1826. ++index;
  1827. }
  1828. if (!items.empty()) {
  1829. input->AddConstraint(ctx.MakeConstraint<TMultiConstraintNode>(std::move(items)));
  1830. }
  1831. }
  1832. }
  1833. return TStatus::Ok;
  1834. }
  1835. TStatus NthWrap(const TExprNode::TPtr& input, TExprNode::TPtr& /*output*/, TExprContext& ctx) const {
  1836. const auto& memberName = input->Tail().Content();
  1837. const auto& structNode = input->Head();
  1838. if (const auto emptyConstraint = structNode.GetConstraint<TEmptyConstraintNode>()) {
  1839. input->AddConstraint(emptyConstraint);
  1840. } else {
  1841. if (const auto part = structNode.GetConstraint<TPartOfSortedConstraintNode>()) {
  1842. if (const auto extracted = part->ExtractField(ctx, memberName)) {
  1843. input->AddConstraint(extracted);
  1844. }
  1845. }
  1846. if (const auto part = structNode.GetConstraint<TPartOfChoppedConstraintNode>()) {
  1847. if (const auto extracted = part->ExtractField(ctx, memberName)) {
  1848. input->AddConstraint(extracted);
  1849. }
  1850. }
  1851. if (const auto part = structNode.GetConstraint<TPartOfUniqueConstraintNode>()) {
  1852. if (const auto extracted = part->ExtractField(ctx, memberName)) {
  1853. input->AddConstraint(extracted);
  1854. }
  1855. }
  1856. if (const auto part = structNode.GetConstraint<TPartOfDistinctConstraintNode>()) {
  1857. if (const auto extracted = part->ExtractField(ctx, memberName)) {
  1858. input->AddConstraint(extracted);
  1859. }
  1860. }
  1861. }
  1862. if (input->Head().IsList()) {
  1863. input->CopyConstraints(*input->Head().Child(FromString<ui32>(input->Child(1)->Content())));
  1864. }
  1865. else if (input->Head().IsCallable("Demux")) {
  1866. if (auto multi = input->Head().Head().GetConstraint<TMultiConstraintNode>()) {
  1867. if (auto c = multi->GetItem(FromString<ui32>(input->Child(1)->Content()))) {
  1868. input->SetConstraints(*c);
  1869. }
  1870. }
  1871. }
  1872. return TStatus::Ok;
  1873. }
  1874. TStatus EquiJoinWrap(const TExprNode::TPtr& input, TExprNode::TPtr& /*output*/, TExprContext& ctx) const {
  1875. const auto numLists = input->ChildrenSize() - 2U;
  1876. std::vector<size_t> emptyInputs;
  1877. TJoinLabels labels;
  1878. for (auto i = 0U; i < numLists; ++i) {
  1879. const auto& list = input->Child(i)->Head();
  1880. if (list.GetConstraint<TEmptyConstraintNode>()) {
  1881. emptyInputs.push_back(i);
  1882. }
  1883. if (const auto err = labels.Add(ctx, input->Child(i)->Tail(),
  1884. GetSeqItemType(*list.GetTypeAnn()).Cast<TStructExprType>(),
  1885. GetDetailed(list.GetConstraint<TUniqueConstraintNode>(), *list.GetTypeAnn(), ctx),
  1886. GetDetailed(list.GetConstraint<TDistinctConstraintNode>(), *list.GetTypeAnn(), ctx))) {
  1887. ctx.AddError(*err);
  1888. return TStatus::Error;
  1889. }
  1890. }
  1891. const auto joinTree = input->Child(numLists);
  1892. for (auto i: emptyInputs) {
  1893. if (IsRequiredSide(joinTree, labels, i).first) {
  1894. input->AddConstraint(ctx.MakeConstraint<TEmptyConstraintNode>());
  1895. break;
  1896. }
  1897. }
  1898. TJoinOptions options;
  1899. if (const auto status = ValidateEquiJoinOptions(input->Pos(), input->Tail(), options, ctx); status != IGraphTransformer::TStatus::Ok || options.Flatten) {
  1900. return status;
  1901. }
  1902. const TUniqueConstraintNode* unique = nullptr;
  1903. const TDistinctConstraintNode* distinct = nullptr;
  1904. if (const auto status = EquiJoinConstraints(input->Pos(), unique, distinct, labels, *joinTree, ctx); status != IGraphTransformer::TStatus::Ok) {
  1905. return status;
  1906. }
  1907. if (const auto renames = LoadJoinRenameMap(input->Tail()); !renames.empty() && (unique || distinct)) {
  1908. const auto rename = [&renames](const TPartOfConstraintBase::TPathType& path) -> std::vector<TPartOfConstraintBase::TPathType> {
  1909. if (path.empty())
  1910. return {};
  1911. const auto it = renames.find(path.front());
  1912. if (renames.cend() == it)
  1913. return {path};
  1914. if (it->second.empty())
  1915. return {};
  1916. std::vector<TPartOfConstraintBase::TPathType> res(it->second.size());
  1917. std::transform(it->second.cbegin(), it->second.cend(), res.begin(), [&path](const std::string_view& newName) {
  1918. auto newPath = path;
  1919. newPath.front() = newName;
  1920. return newPath;
  1921. });
  1922. return res;
  1923. };
  1924. if (unique)
  1925. unique = unique->RenameFields(ctx, rename);
  1926. if (distinct)
  1927. distinct = distinct->RenameFields(ctx, rename);
  1928. }
  1929. if (unique)
  1930. input->AddConstraint(unique->GetSimplifiedForType(*input->GetTypeAnn(), ctx));
  1931. if (distinct)
  1932. input->AddConstraint(distinct->GetSimplifiedForType(*input->GetTypeAnn(), ctx));
  1933. return TStatus::Ok;
  1934. }
  1935. static std::vector<std::string_view> GetKeys(const TExprNode& keys) {
  1936. std::vector<std::string_view> result;
  1937. result.reserve(keys.ChildrenSize());
  1938. keys.ForEachChild([&result](const TExprNode& key) { result.emplace_back(key.Content()); });
  1939. return result;
  1940. }
  1941. template<bool ForDict = false>
  1942. static TPartOfConstraintBase::TPathReduce GetRenames(const TExprNode& renames) {
  1943. std::unordered_map<std::string_view, std::string_view> map(renames.ChildrenSize() >> 1U);
  1944. for (auto i = 0U; i < renames.ChildrenSize(); ++++i)
  1945. map.emplace(renames.Child(i)->Content(), renames.Child(i + 1U)->Content());
  1946. return [map](const TPartOfConstraintBase::TPathType& path) -> std::vector<TPartOfConstraintBase::TPathType> {
  1947. if constexpr (ForDict) {
  1948. if (path.size() > 1U && path.front() == "1"sv) {
  1949. auto out = path;
  1950. out.pop_front();
  1951. if (const auto it = map.find(out.front()); map.cend() != it) {
  1952. out.front() = it->second;
  1953. return {std::move(out)};
  1954. }
  1955. }
  1956. } else {
  1957. if (!path.empty()) {
  1958. if (const auto it = map.find(path.front()); map.cend() != it) {
  1959. auto out = path;
  1960. out.front() = it->second;
  1961. return {std::move(out)};
  1962. }
  1963. }
  1964. }
  1965. return {};
  1966. };
  1967. }
  1968. TStatus MapJoinCoreWrap(const TExprNode::TPtr& input, TExprNode::TPtr& /*output*/, TExprContext& ctx) const {
  1969. const TCoMapJoinCore core(input);
  1970. const auto& joinType = core.JoinKind().Ref();
  1971. if (const auto empty = core.LeftInput().Ref().GetConstraint<TEmptyConstraintNode>()) {
  1972. input->AddConstraint(empty);
  1973. } else if (const auto empty = core.RightDict().Ref().GetConstraint<TEmptyConstraintNode>()) {
  1974. if (joinType.IsAtom({"Inner", "LeftSemi"})) {
  1975. input->AddConstraint(empty);
  1976. }
  1977. }
  1978. if (joinType.IsAtom({"LeftSemi", "LeftOnly"})) {
  1979. const auto rename = GetRenames(core.LeftRenames().Ref());
  1980. if (const auto unique = core.LeftInput().Ref().GetConstraint<TUniqueConstraintNode>())
  1981. if (const auto renamed = unique->RenameFields(ctx, rename))
  1982. input->AddConstraint(renamed);
  1983. if (const auto distinct = core.LeftInput().Ref().GetConstraint<TDistinctConstraintNode>())
  1984. if (const auto renamed = distinct->RenameFields(ctx, rename))
  1985. input->AddConstraint(renamed);
  1986. } else {
  1987. if (const auto unique = core.LeftInput().Ref().GetConstraint<TUniqueConstraintNode>()) {
  1988. if (unique->ContainsCompleteSet(GetKeys(core.LeftKeysColumns().Ref())) && core.RightDict().Ref().GetTypeAnn()->Cast<TDictExprType>()->GetPayloadType()->GetKind() != ETypeAnnotationKind::List) {
  1989. const auto rename = GetRenames(core.LeftRenames().Ref());
  1990. const auto rightRename = GetRenames<true>(core.RightRenames().Ref());
  1991. auto commonUnique = unique->RenameFields(ctx, rename);
  1992. if (const auto rUnique = core.RightDict().Ref().GetConstraint<TUniqueConstraintNode>()) {
  1993. commonUnique = TUniqueConstraintNode::Merge(commonUnique, rUnique->RenameFields(ctx, rightRename), ctx);
  1994. }
  1995. const auto distinct = core.LeftInput().Ref().GetConstraint<TDistinctConstraintNode>();
  1996. auto commonDistinct = distinct ? distinct->RenameFields(ctx, rename) : nullptr;
  1997. if (joinType.IsAtom("Inner")) {
  1998. if (const auto rDistinct = core.RightDict().Ref().GetConstraint<TDistinctConstraintNode>()) {
  1999. commonDistinct = TDistinctConstraintNode::Merge(commonDistinct, rDistinct->RenameFields(ctx, rightRename), ctx);
  2000. }
  2001. }
  2002. if (commonUnique)
  2003. input->AddConstraint(commonUnique);
  2004. if (commonDistinct)
  2005. input->AddConstraint(commonDistinct);
  2006. }
  2007. }
  2008. }
  2009. if (const auto sorted = core.LeftInput().Ref().GetConstraint<TSortedConstraintNode>())
  2010. if (const auto renamed = sorted->RenameFields(ctx, GetRenames(core.LeftRenames().Ref())))
  2011. input->AddConstraint(renamed);
  2012. if (const auto chopped = core.LeftInput().Ref().GetConstraint<TChoppedConstraintNode>())
  2013. if (const auto renamed = chopped->RenameFields(ctx, GetRenames(core.LeftRenames().Ref())))
  2014. input->AddConstraint(renamed);
  2015. return TStatus::Ok;
  2016. }
  2017. TExprNode::TPtr GraceJoinRightInput(const TCoGraceJoinCore& core) const {
  2018. return core.RightInput().Ptr();
  2019. }
  2020. TExprNode::TPtr GraceJoinRightInput(const TCoGraceSelfJoinCore& core) const {
  2021. return core.Input().Ptr();
  2022. }
  2023. TExprNode::TPtr GraceJoinLeftInput(const TCoGraceJoinCore& core) const {
  2024. return core.LeftInput().Ptr();
  2025. }
  2026. TExprNode::TPtr GraceJoinLeftInput(const TCoGraceSelfJoinCore& core) const {
  2027. return core.Input().Ptr();
  2028. }
  2029. template<typename GraceJoinCoreType>
  2030. TStatus GraceJoinCoreWrapImpl(const TExprNode::TPtr& input, TExprNode::TPtr& output, TExprContext& ctx) const {
  2031. Y_UNUSED(output);
  2032. const GraceJoinCoreType core(input);
  2033. const auto& joinType = core.JoinKind().Ref();
  2034. TExprNode::TPtr leftInput = GraceJoinLeftInput(core);
  2035. TExprNode::TPtr rightInput = GraceJoinRightInput(core);
  2036. if (const auto lEmpty = leftInput->GetConstraint<TEmptyConstraintNode>(), rEmpty = rightInput->GetConstraint<TEmptyConstraintNode>(); lEmpty && rEmpty) {
  2037. input->AddConstraint(ctx.MakeConstraint<TEmptyConstraintNode>());
  2038. } else if (lEmpty && joinType.Content().starts_with("Left")) {
  2039. input->AddConstraint(lEmpty);
  2040. } else if (rEmpty && joinType.Content().starts_with("Right")) {
  2041. input->AddConstraint(rEmpty);
  2042. } else if ((lEmpty || rEmpty) && (joinType.IsAtom("Inner") || joinType.Content().ends_with("Semi"))) {
  2043. input->AddConstraint(ctx.MakeConstraint<TEmptyConstraintNode>());
  2044. }
  2045. bool leftAny = false, rigthAny = false;
  2046. core.Flags().Ref().ForEachChild([&](const TExprNode& flag) {
  2047. if (flag.IsAtom("LeftAny"))
  2048. leftAny = true;
  2049. else if (flag.IsAtom("RightAny"))
  2050. rigthAny = true;
  2051. });
  2052. const TUniqueConstraintNode* lUnique = leftInput->GetConstraint<TUniqueConstraintNode>();
  2053. const TUniqueConstraintNode* rUnique = rightInput->GetConstraint<TUniqueConstraintNode>();
  2054. const bool lOneRow = lUnique && (leftAny || lUnique->ContainsCompleteSet(GetKeys(core.LeftKeysColumns().Ref())));
  2055. const bool rOneRow = rUnique && (rigthAny || rUnique->ContainsCompleteSet(GetKeys(core.RightKeysColumns().Ref())));
  2056. const bool singleSide = joinType.Content().ends_with("Semi") || joinType.Content().ends_with("Only");
  2057. if (singleSide || lOneRow || rOneRow) {
  2058. const TUniqueConstraintNode* unique = nullptr;
  2059. const TDistinctConstraintNode* distinct = nullptr;
  2060. const bool leftSide = joinType.Content().starts_with("Left");
  2061. const bool rightSide = joinType.Content().starts_with("Right");
  2062. const auto leftRename = GetRenames(core.LeftRenames().Ref());
  2063. const auto rightRename = GetRenames(core.RightRenames().Ref());
  2064. if (singleSide) {
  2065. if (leftSide && lUnique)
  2066. unique = lUnique->RenameFields(ctx, leftRename);
  2067. else if (rightSide && rUnique)
  2068. unique = rUnique->RenameFields(ctx, rightRename);
  2069. } else {
  2070. const bool exclusion = joinType.IsAtom("Exclusion");
  2071. const bool useLeft = lUnique && (rOneRow || exclusion);
  2072. const bool useRight = rUnique && (lOneRow || exclusion);
  2073. if (useLeft && !useRight)
  2074. unique = lUnique->RenameFields(ctx, leftRename);
  2075. else if (useRight && !useLeft)
  2076. unique = rUnique->RenameFields(ctx, rightRename);
  2077. else if (useLeft && useRight)
  2078. unique = TUniqueConstraintNode::Merge(lUnique->RenameFields(ctx, leftRename), rUnique->RenameFields(ctx, rightRename), ctx);
  2079. }
  2080. const auto lDistinct = leftInput->GetConstraint<TDistinctConstraintNode>();
  2081. const auto rDistinct = rightInput->GetConstraint<TDistinctConstraintNode>();
  2082. if (singleSide) {
  2083. if (leftSide && lDistinct)
  2084. distinct = lDistinct->RenameFields(ctx, leftRename);
  2085. else if (rightSide && rDistinct)
  2086. distinct = rDistinct->RenameFields(ctx, rightRename);
  2087. } else {
  2088. const bool inner = joinType.IsAtom("Inner");
  2089. const bool useLeft = lDistinct && rOneRow && (inner || leftSide);
  2090. const bool useRight = rDistinct && lOneRow && (inner || rightSide);
  2091. if (useLeft && !useRight)
  2092. distinct = lDistinct->RenameFields(ctx, leftRename);
  2093. else if (useRight && !useLeft)
  2094. distinct = rDistinct->RenameFields(ctx, rightRename);
  2095. else if (useLeft && useRight)
  2096. distinct = TDistinctConstraintNode::Merge(lDistinct->RenameFields(ctx, leftRename), rDistinct->RenameFields(ctx, rightRename), ctx);
  2097. }
  2098. if (unique)
  2099. input->AddConstraint(unique);
  2100. if (distinct)
  2101. input->AddConstraint(distinct);
  2102. }
  2103. return TStatus::Ok;
  2104. }
  2105. TStatus GraceJoinCoreWrap(const TExprNode::TPtr& input, TExprNode::TPtr& output, TExprContext& ctx) const {
  2106. return GraceJoinCoreWrapImpl<TCoGraceJoinCore>(input, output, ctx);
  2107. }
  2108. TStatus GraceSelfJoinCoreWrap(const TExprNode::TPtr& input, TExprNode::TPtr& output, TExprContext& ctx) const {
  2109. return GraceJoinCoreWrapImpl<TCoGraceSelfJoinCore>(input, output, ctx);
  2110. }
  2111. template<bool Distinct>
  2112. static const TUniqueConstraintNodeBase<Distinct>* GetForPayload(const TExprNode& input, TExprContext& ctx) {
  2113. if (const auto constraint = input.GetConstraint<TUniqueConstraintNodeBase<Distinct>>()) {
  2114. return constraint->RenameFields(ctx, [&ctx](const TPartOfConstraintBase::TPathType& path) -> std::vector<TPartOfConstraintBase::TPathType> {
  2115. if (path.empty() || path.front() != ctx.GetIndexAsString(1U))
  2116. return {};
  2117. auto copy = path;
  2118. copy.pop_front();
  2119. return {copy};
  2120. });
  2121. }
  2122. return nullptr;
  2123. }
  2124. TStatus JoinDictWrap(const TExprNode::TPtr& input, TExprNode::TPtr& /*output*/, TExprContext& ctx) const {
  2125. const TCoJoinDict join(input);
  2126. const auto& joinType = join.JoinKind().Ref();
  2127. if (const auto lEmpty = join.LeftInput().Ref().GetConstraint<TEmptyConstraintNode>(), rEmpty = join.RightInput().Ref().GetConstraint<TEmptyConstraintNode>(); lEmpty && rEmpty) {
  2128. input->AddConstraint(ctx.MakeConstraint<TEmptyConstraintNode>());
  2129. } else if (lEmpty && joinType.Content().starts_with("Left")) {
  2130. input->AddConstraint(lEmpty);
  2131. } else if (rEmpty && joinType.Content().starts_with("Right")) {
  2132. input->AddConstraint(rEmpty);
  2133. } else if ((lEmpty || rEmpty) && (joinType.IsAtom("Inner") || joinType.Content().ends_with("Semi"))) {
  2134. input->AddConstraint(ctx.MakeConstraint<TEmptyConstraintNode>());
  2135. }
  2136. bool lOneRow = false, rOneRow = false;
  2137. if (const auto& flags = join.Flags()) {
  2138. flags.Cast().Ref().ForEachChild([&](const TExprNode& flag) {
  2139. lOneRow = lOneRow || flag.IsAtom("LeftUnique");
  2140. rOneRow = rOneRow || flag.IsAtom("RightUnique");
  2141. });
  2142. }
  2143. const auto lUnique = GetForPayload<false>(join.LeftInput().Ref(), ctx);
  2144. const auto rUnique = GetForPayload<false>(join.RightInput().Ref(), ctx);
  2145. const auto lDistinct = GetForPayload<true>(join.LeftInput().Ref(), ctx);
  2146. const auto rDistinct = GetForPayload<true>(join.RightInput().Ref(), ctx);
  2147. const bool leftSide = joinType.Content().starts_with("Left");
  2148. const bool rightSide = joinType.Content().starts_with("Right");
  2149. if (joinType.Content().ends_with("Semi") || joinType.Content().ends_with("Only")) {
  2150. if (leftSide) {
  2151. if (lUnique)
  2152. input->AddConstraint(lUnique);
  2153. if (lDistinct)
  2154. input->AddConstraint(lDistinct);
  2155. } else if (rightSide) {
  2156. if (rUnique)
  2157. input->AddConstraint(rUnique);
  2158. if (rDistinct)
  2159. input->AddConstraint(rDistinct);
  2160. }
  2161. } else if (lOneRow || rOneRow) {
  2162. const auto rename = [](const std::string_view& prefix, TPartOfConstraintBase::TPathType path) {
  2163. path.emplace_front(prefix);
  2164. return std::vector<TPartOfConstraintBase::TPathType>(1U, std::move(path));
  2165. };
  2166. const auto leftRename = std::bind(rename, ctx.GetIndexAsString(0U), std::placeholders::_1);
  2167. const auto rightRename = std::bind(rename, ctx.GetIndexAsString(1U), std::placeholders::_1);
  2168. if (lUnique || rUnique) {
  2169. const TUniqueConstraintNode* unique = nullptr;
  2170. const bool exclusion = joinType.IsAtom("Exclusion");
  2171. const bool useLeft = lUnique && (rOneRow || exclusion);
  2172. const bool useRight = rUnique && (lOneRow || exclusion);
  2173. if (useLeft && !useRight)
  2174. unique = lUnique->RenameFields(ctx, leftRename);
  2175. else if (useRight && !useLeft)
  2176. unique = rUnique->RenameFields(ctx, rightRename);
  2177. else if (useLeft && useRight)
  2178. unique = TUniqueConstraintNode::Merge(lUnique->RenameFields(ctx, leftRename), rUnique->RenameFields(ctx, rightRename), ctx);
  2179. if (unique)
  2180. input->AddConstraint(unique);
  2181. }
  2182. if (lDistinct || rDistinct) {
  2183. const TDistinctConstraintNode* distinct = nullptr;
  2184. const bool inner = joinType.IsAtom("Inner");
  2185. const bool useLeft = lDistinct && rOneRow && (inner || leftSide);
  2186. const bool useRight = rDistinct && lOneRow && (inner || rightSide);
  2187. if (useLeft && !useRight)
  2188. distinct = lDistinct->RenameFields(ctx, leftRename);
  2189. else if (useRight && !useLeft)
  2190. distinct = rDistinct->RenameFields(ctx, rightRename);
  2191. else if (useLeft && useRight)
  2192. distinct = TDistinctConstraintNode::Merge(lDistinct->RenameFields(ctx, leftRename), rDistinct->RenameFields(ctx, rightRename), ctx);
  2193. if (distinct)
  2194. input->AddConstraint(distinct);
  2195. }
  2196. }
  2197. return TStatus::Ok;
  2198. }
  2199. TStatus IsKeySwitchWrap(const TExprNode::TPtr& input, TExprNode::TPtr& /*output*/, TExprContext& ctx) const {
  2200. const TCoIsKeySwitch keySwitch(input);
  2201. TSmallVec<TConstraintNode::TListType> itemConstraints, stateConstraints;
  2202. itemConstraints.emplace_back(keySwitch.Item().Ref().GetAllConstraints());
  2203. stateConstraints.emplace_back(keySwitch.State().Ref().GetAllConstraints());
  2204. return UpdateLambdaConstraints(input->ChildRef(TCoIsKeySwitch::idx_ItemKeyExtractor), ctx, itemConstraints)
  2205. .Combine(UpdateLambdaConstraints(input->ChildRef(TCoIsKeySwitch::idx_StateKeyExtractor), ctx, stateConstraints));
  2206. }
  2207. template<bool Wide>
  2208. static TPartOfConstraintBase::TSetType GetSimpleKeys(const TExprNode& node, const TExprNode::TChildrenType& args, TExprContext& ctx) {
  2209. TPartOfConstraintBase::TSetType keys;
  2210. if (node.IsCallable("AggrNotEquals")) {
  2211. const TExprNode& body = node.Head().IsCallable("StablePickle") ? node.Head() : node;
  2212. if (body.Head().IsList() && body.Tail().IsList() && body.Head().ChildrenSize() == body.Tail().ChildrenSize()) {
  2213. keys.reserve(body.Tail().ChildrenSize());
  2214. for (auto i = 0U; i < body.Head().ChildrenSize(); ++i){
  2215. if (auto l = GetPathToKey(*body.Head().Child(i), args), r = GetPathToKey(*body.Tail().Child(i), args); l && r && *l == *r) {
  2216. if constexpr (Wide) {
  2217. auto path = r->first;
  2218. path.emplace_front(ctx.GetIndexAsString(r->second));
  2219. } else {
  2220. YQL_ENSURE(l->second == 0U, "Unexpected arg index: " << l->second);
  2221. keys.insert_unique(l->first);
  2222. }
  2223. }
  2224. }
  2225. } else if (auto l = GetPathToKey(body.Head(), args), r = GetPathToKey(body.Tail(), args); l && r && *l == *r) {
  2226. if constexpr (Wide) {
  2227. auto path = l->first;
  2228. path.emplace_front(ctx.GetIndexAsString(l->second));
  2229. } else {
  2230. YQL_ENSURE(r->second == 0U, "Unexpected arg index: " << r->second);
  2231. keys.insert_unique(r->first);
  2232. }
  2233. }
  2234. } else if (node.IsCallable("Or")) {
  2235. keys.reserve(node.ChildrenSize());
  2236. for (auto i = 0U; i < node.ChildrenSize(); ++i) {
  2237. const auto& part = GetSimpleKeys<Wide>(*node.Child(i), args, ctx);
  2238. keys.insert_unique(part.cbegin(), part.cend());
  2239. }
  2240. }
  2241. return keys;
  2242. }
  2243. template<bool Wide>
  2244. static TPartOfConstraintBase::TSetType GetSimpleKeys(const TExprNode& selector, TExprContext& ctx) {
  2245. YQL_ENSURE(selector.IsLambda() && 2U == selector.ChildrenSize());
  2246. const auto& body = selector.Tail();
  2247. if constexpr (!Wide) {
  2248. if (TCoIsKeySwitch::Match(&body)) {
  2249. const TCoIsKeySwitch keySwitch(&body);
  2250. const auto& i = GetPathsToKeys(*ctx.ReplaceNode(keySwitch.ItemKeyExtractor().Body().Ptr(), keySwitch.ItemKeyExtractor().Args().Arg(0).Ref(), keySwitch.Item().Ptr()), keySwitch.Item().Ref());
  2251. const auto& s = GetPathsToKeys(*ctx.ReplaceNode(keySwitch.StateKeyExtractor().Body().Ptr(), keySwitch.StateKeyExtractor().Args().Arg(0).Ref(), keySwitch.State().Ptr()), keySwitch.Item().Ref());
  2252. return i == s ? i : TPartOfConstraintBase::TSetType();
  2253. }
  2254. }
  2255. return GetSimpleKeys<Wide>(selector.Tail(), selector.Head().Children(), ctx);
  2256. }
  2257. static TExprNode::TPtr FuseInitLambda(const TExprNode& inner, const TExprNode& outer, TExprContext& ctx) {
  2258. YQL_ENSURE(outer.IsLambda() && inner.IsLambda());
  2259. const auto& outerArgs = outer.Head();
  2260. const auto& innerArgs = inner.Head();
  2261. YQL_ENSURE(outerArgs.ChildrenSize() + 1U == innerArgs.ChildrenSize() + inner.ChildrenSize());
  2262. TNodeOnNodeOwnedMap outerReplaces(outerArgs.ChildrenSize());
  2263. auto i = 0U;
  2264. for (auto& item : innerArgs.ChildrenList())
  2265. YQL_ENSURE(outerReplaces.emplace(outerArgs.Child(i++), std::move(item)).second);
  2266. for (auto& item : GetLambdaBody(inner))
  2267. YQL_ENSURE(outerReplaces.emplace(outerArgs.Child(i++), std::move(item)).second);
  2268. return ctx.NewLambda(outer.Pos(), inner.HeadPtr(), ctx.ReplaceNodes(GetLambdaBody(outer), outerReplaces));
  2269. }
  2270. TStatus CondenseWrap(const TExprNode::TPtr& input, TExprNode::TPtr& output, TExprContext& ctx) const {
  2271. auto argsConstraints = GetConstraintsForInputArgument<true, false, false>(*input, ctx);
  2272. const auto initState = input->Child(1);
  2273. argsConstraints.emplace_back(initState->GetAllConstraints());
  2274. if (const auto status = UpdateLambdaConstraints(input->ChildRef(2), ctx, argsConstraints)
  2275. .Combine(UpdateLambdaConstraints(input->TailRef(), ctx, argsConstraints)); status != TStatus::Ok) {
  2276. return status;
  2277. }
  2278. return FromFirst<TEmptyConstraintNode>(input, output, ctx);
  2279. }
  2280. template<class TConstraint, bool Wide>
  2281. static void GetCommonFromBothLambdas(const TExprNode::TPtr& input, const typename TConstraint::TMainConstraint* original, TExprContext& ctx) {
  2282. if (original)
  2283. if (const auto initPart = GetConstraintFromLambda<TConstraint, Wide>(*input->Child(1), ctx))
  2284. if (const auto init = TConstraint::MakeComplete(ctx, initPart->GetColumnMapping(), original))
  2285. if (const auto updatePart = GetConstraintFromLambda<TConstraint, Wide>(input->Tail(), ctx))
  2286. if (const auto update = TConstraint::MakeComplete(ctx, updatePart->GetColumnMapping(), original))
  2287. if (const auto common = init->MakeCommon(update, ctx))
  2288. input->AddConstraint(common->GetSimplifiedForType(*input->GetTypeAnn(), ctx));
  2289. }
  2290. template<bool Wide>
  2291. TStatus Condense1Wrap(const TExprNode::TPtr& input, TExprNode::TPtr& output, TExprContext& ctx) const {
  2292. auto argsConstraints = GetConstraintsForInputArgument<true, Wide, false>(*input, ctx);
  2293. const auto initLambda = input->Child(1);
  2294. const auto switchLambda = input->Child(2);
  2295. const TUniqueConstraintNode* unique = nullptr;
  2296. const TDistinctConstraintNode* distinct = nullptr;
  2297. const auto sorted = input->Head().GetConstraint<TSortedConstraintNode>();
  2298. const auto chopped = input->Head().GetConstraint<TChoppedConstraintNode>();
  2299. if (sorted || chopped) {
  2300. if (const auto& keys = GetSimpleKeys<Wide>(*FuseInitLambda(*initLambda, *switchLambda, ctx), ctx); !keys.empty()) {
  2301. if (sorted && (sorted->StartsWith(keys) || sorted->GetSimplifiedForType(*input->Head().GetTypeAnn(), ctx)->StartsWith(keys)) ||
  2302. chopped && (chopped->Equals(keys) || chopped->GetSimplifiedForType(*input->Head().GetTypeAnn(), ctx)->Equals(keys))) {
  2303. TPartOfConstraintBase::TSetOfSetsType sets;
  2304. sets.reserve(keys.size());
  2305. for (const auto& key : keys)
  2306. sets.insert_unique(TPartOfConstraintBase::TSetType{key});
  2307. unique = ctx.MakeConstraint<TUniqueConstraintNode>(TUniqueConstraintNode::TContentType{sets})->GetComplicatedForType(*input->Head().GetTypeAnn(), ctx);
  2308. distinct = ctx.MakeConstraint<TDistinctConstraintNode>(TDistinctConstraintNode::TContentType{sets})->GetComplicatedForType(*input->Head().GetTypeAnn(), ctx);
  2309. if constexpr (Wide) {
  2310. if (const auto& mapping = TPartOfUniqueConstraintNode::GetCommonMapping(unique); !mapping.empty()) {
  2311. for (ui32 i = 0U; i < argsConstraints.size(); ++i) {
  2312. if (auto extracted = TPartOfUniqueConstraintNode::ExtractField(mapping, ctx.GetIndexAsString(i)); !extracted.empty()) {
  2313. argsConstraints[i].emplace_back(ctx.MakeConstraint<TPartOfUniqueConstraintNode>(std::move(extracted)));
  2314. }
  2315. }
  2316. }
  2317. if (const auto& mapping = TPartOfDistinctConstraintNode::GetCommonMapping(distinct); !mapping.empty()) {
  2318. for (ui32 i = 0U; i < argsConstraints.size(); ++i) {
  2319. if (auto extracted = TPartOfDistinctConstraintNode::ExtractField(mapping, ctx.GetIndexAsString(i)); !extracted.empty()) {
  2320. argsConstraints[i].emplace_back(ctx.MakeConstraint<TPartOfDistinctConstraintNode>(std::move(extracted)));
  2321. }
  2322. }
  2323. }
  2324. } else {
  2325. argsConstraints.front().emplace_back(ctx.MakeConstraint<TPartOfUniqueConstraintNode>(TPartOfUniqueConstraintNode::GetCommonMapping(unique)));
  2326. argsConstraints.front().emplace_back(ctx.MakeConstraint<TPartOfDistinctConstraintNode>(TPartOfDistinctConstraintNode::GetCommonMapping(distinct)));
  2327. }
  2328. }
  2329. }
  2330. }
  2331. if (const auto status = UpdateLambdaConstraints(input->ChildRef(1), ctx, argsConstraints); status != TStatus::Ok) {
  2332. return status;
  2333. }
  2334. argsConstraints.reserve(argsConstraints.size() + initLambda->ChildrenSize() - 1U);
  2335. for (ui32 i = 1U; i < initLambda->ChildrenSize(); ++i) {
  2336. argsConstraints.emplace_back(initLambda->Child(i)->GetAllConstraints());
  2337. }
  2338. if (const auto status = UpdateLambdaConstraints(input->ChildRef(2), ctx, argsConstraints)
  2339. .Combine(UpdateLambdaConstraints(input->TailRef(), ctx, argsConstraints)); status != TStatus::Ok) {
  2340. return status;
  2341. }
  2342. GetCommonFromBothLambdas<TPartOfUniqueConstraintNode, Wide>(input, unique, ctx);
  2343. GetCommonFromBothLambdas<TPartOfDistinctConstraintNode, Wide>(input, distinct, ctx);
  2344. return FromFirst<TEmptyConstraintNode>(input, output, ctx);
  2345. }
  2346. template<class TConstraint, bool Wide>
  2347. static void GetCommonFromBothLambdas(const TExprNode::TPtr& input, TExprContext& ctx) {
  2348. if (const auto original = input->Head().GetConstraint<typename TConstraint::TMainConstraint>())
  2349. if (const auto initPart = GetConstraintFromLambda<TConstraint, Wide>(*input->Child(1), ctx))
  2350. if (const auto init = TConstraint::MakeComplete(ctx, initPart->GetColumnMapping(), original))
  2351. if (const auto updatePart = GetConstraintFromLambda<TConstraint, Wide>(input->Tail(), ctx))
  2352. if (const auto update = TConstraint::MakeComplete(ctx, updatePart->GetColumnMapping(), original))
  2353. if (const auto common = init->MakeCommon(update, ctx))
  2354. input->AddConstraint(common->GetSimplifiedForType(*input->GetTypeAnn(), ctx));
  2355. }
  2356. template<bool Wide>
  2357. TStatus Chain1MapWrap(const TExprNode::TPtr& input, TExprNode::TPtr& output, TExprContext& ctx) const {
  2358. auto argsConstraints = GetConstraintsForInputArgument<true, Wide>(*input, ctx);
  2359. if (const auto status = UpdateLambdaConstraints(input->ChildRef(1), ctx, argsConstraints); status != TStatus::Ok) {
  2360. return status;
  2361. }
  2362. const auto initLambda = input->Child(1);
  2363. argsConstraints.reserve(argsConstraints.size() + initLambda->ChildrenSize() - 1U);
  2364. for (ui32 i = 1U; i < initLambda->ChildrenSize(); ++i) {
  2365. argsConstraints.emplace_back(initLambda->Child(i)->GetAllConstraints());
  2366. }
  2367. if (const auto status = UpdateLambdaConstraints(input->ChildRef(2), ctx, argsConstraints); status != TStatus::Ok) {
  2368. return status;
  2369. }
  2370. GetCommonFromBothLambdas<TPartOfSortedConstraintNode, Wide>(input, ctx);
  2371. GetCommonFromBothLambdas<TPartOfChoppedConstraintNode, Wide>(input, ctx);
  2372. GetCommonFromBothLambdas<TPartOfUniqueConstraintNode, Wide>(input, ctx);
  2373. GetCommonFromBothLambdas<TPartOfDistinctConstraintNode, Wide>(input, ctx);
  2374. return FromFirst<TEmptyConstraintNode>(input, output, ctx);
  2375. }
  2376. template<bool Distinct>
  2377. static void GetUniquesForPayloads(const TExprNode::TPtr& input, TExprContext& ctx) {
  2378. typename TUniqueConstraintNodeBase<Distinct>::TContentType content{TPartOfConstraintBase::TSetOfSetsType{TPartOfConstraintBase::TSetType{TPartOfConstraintBase::TPathType{ctx.GetIndexAsString(0U)}}}};
  2379. if (const auto lambda = GetConstraintFromLambda<TPartOfConstraintNode<TUniqueConstraintNodeBase<Distinct>>, false>(*input->Child(2), ctx)) {
  2380. if (const auto original = GetDetailed(input->Head().GetConstraint<TUniqueConstraintNodeBase<Distinct>>(), *input->Head().GetTypeAnn(), ctx)) {
  2381. if (const auto complete = TPartOfConstraintNode<TUniqueConstraintNodeBase<Distinct>>::MakeComplete(ctx, lambda->GetColumnMapping(), original, ctx.GetIndexAsString(1U))) {
  2382. content.insert_unique(complete->GetContent().cbegin(), complete->GetContent().cend());
  2383. }
  2384. }
  2385. }
  2386. input->AddConstraint(ctx.MakeConstraint<TUniqueConstraintNodeBase<Distinct>>(std::move(content)));
  2387. }
  2388. TStatus ToDictWrap(const TExprNode::TPtr& input, TExprNode::TPtr& output, TExprContext& ctx) const {
  2389. const auto argsConstraints = GetConstraintsForInputArgument<false, false>(*input, ctx);
  2390. if (const auto status = UpdateLambdaConstraints(input->ChildRef(1), ctx, argsConstraints); status != TStatus::Ok) {
  2391. return status;
  2392. }
  2393. if (const auto status = UpdateLambdaConstraints(input->ChildRef(2), ctx, argsConstraints); status != TStatus::Ok) {
  2394. return status;
  2395. }
  2396. GetUniquesForPayloads<true>(input, ctx);
  2397. GetUniquesForPayloads<false>(input, ctx);
  2398. return FromFirst<TEmptyConstraintNode>(input, output, ctx);
  2399. }
  2400. TStatus DictItemsWrap(const TExprNode::TPtr& input, TExprNode::TPtr& output, TExprContext& ctx) const {
  2401. if (const auto unique = input->Head().GetConstraint<TUniqueConstraintNode>())
  2402. input->AddConstraint(unique->GetSimplifiedForType(*input->GetTypeAnn(), ctx));
  2403. if (const auto distinct = input->Head().GetConstraint<TDistinctConstraintNode>())
  2404. input->AddConstraint(distinct->GetSimplifiedForType(*input->GetTypeAnn(), ctx));
  2405. return FromFirst<TPartOfUniqueConstraintNode, TPartOfDistinctConstraintNode, TEmptyConstraintNode>(input, output, ctx);
  2406. }
  2407. template<bool Keys>
  2408. TStatus DictHalfWrap(const TExprNode::TPtr& input, TExprNode::TPtr& output, TExprContext& ctx) const {
  2409. const auto& side = ctx.GetIndexAsString(Keys ? 0U : 1U);
  2410. const auto reduce = [&side](const TPartOfConstraintBase::TPathType& path) -> std::vector<TPartOfConstraintBase::TPathType> {
  2411. if (path.empty() || path.front() != side)
  2412. return {};
  2413. auto copy = path;
  2414. copy.pop_front();
  2415. return {copy};
  2416. };
  2417. ReduceFromHead<TUniqueConstraintNode, true>(input, reduce, ctx);
  2418. ReduceFromHead<TDistinctConstraintNode, true>(input, reduce, ctx);
  2419. ReduceFromHead<TPartOfUniqueConstraintNode>(input, reduce, ctx);
  2420. ReduceFromHead<TPartOfDistinctConstraintNode>(input, reduce, ctx);
  2421. return FromFirst<TEmptyConstraintNode>(input, output, ctx);
  2422. }
  2423. template<bool Partitions>
  2424. TStatus ShuffleByKeysWrap(const TExprNode::TPtr& input, TExprNode::TPtr& output, TExprContext& ctx) const {
  2425. using TCoBase = std::conditional_t<Partitions, TCoPartitionByKeyBase, TCoShuffleByKeys>;
  2426. if (const auto status = UpdateLambdaConstraints(*input->Child(TCoBase::idx_KeySelectorLambda)); status != TStatus::Ok) {
  2427. return status;
  2428. }
  2429. TPartOfConstraintBase::TSetType keys;
  2430. if constexpr (Partitions) {
  2431. keys = GetPathsToKeys(input->Child(TCoBase::idx_KeySelectorLambda)->Tail(), input->Child(TCoBase::idx_KeySelectorLambda)->Head().Head());
  2432. if (const auto sortKeySelector = input->Child(TCoBase::idx_SortKeySelectorLambda); sortKeySelector->IsLambda()) {
  2433. if (const auto status = UpdateLambdaConstraints(*sortKeySelector); status != TStatus::Ok) {
  2434. return status;
  2435. }
  2436. auto sortKeys = GetPathsToKeys(sortKeySelector->Tail(), sortKeySelector->Head().Head());
  2437. std::move(sortKeys.begin(), sortKeys.end(), std::back_inserter(keys));
  2438. std::sort(keys.begin(), keys.end());
  2439. }
  2440. }
  2441. const auto filter = [](const std::string_view& name) {
  2442. return name == TEmptyConstraintNode::Name() || name == TUniqueConstraintNode::Name() || name == TDistinctConstraintNode::Name();
  2443. };
  2444. TConstraintNode::TListType argConstraints;
  2445. const auto source = input->Child(TCoBase::idx_Input);
  2446. std::copy_if(source->GetAllConstraints().cbegin(), source->GetAllConstraints().cend(), std::back_inserter(argConstraints), std::bind(filter, std::bind(&TConstraintNode::GetName, std::placeholders::_1)));
  2447. if (const auto multi = source->template GetConstraint<TMultiConstraintNode>())
  2448. if (const auto filtered = multi->FilterConstraints(ctx, filter))
  2449. argConstraints.emplace_back(filtered);
  2450. if constexpr (Partitions) {
  2451. if (!keys.empty())
  2452. argConstraints.emplace_back(ctx.MakeConstraint<TChoppedConstraintNode>(keys)->GetComplicatedForType(*input->Head().GetTypeAnn(), ctx));
  2453. }
  2454. if (const auto status = UpdateLambdaConstraints(input->ChildRef(TCoBase::idx_ListHandlerLambda), ctx, {argConstraints}); status != TStatus::Ok) {
  2455. return status;
  2456. }
  2457. const auto handlerLambda = input->Child(TCoBase::idx_ListHandlerLambda);
  2458. if (const auto unique = handlerLambda->template GetConstraint<TUniqueConstraintNode>())
  2459. input->AddConstraint(unique);
  2460. if (const auto distinct = handlerLambda->template GetConstraint<TDistinctConstraintNode>())
  2461. input->AddConstraint(distinct);
  2462. const bool multiInput = ETypeAnnotationKind::Variant == GetSeqItemType(*input->Head().GetTypeAnn()).GetKind();
  2463. const auto lambdaVarIndex = handlerLambda->template GetConstraint<TVarIndexConstraintNode>();
  2464. const auto multi = input->Head().template GetConstraint<TMultiConstraintNode>();
  2465. const auto lambdaMulti = handlerLambda->template GetConstraint<TMultiConstraintNode>();
  2466. if (const auto varIndex = input->Head().template GetConstraint<TVarIndexConstraintNode>()) {
  2467. if (multiInput) {
  2468. if (lambdaVarIndex) {
  2469. if (auto outVarIndex = GetVarIndexOverVarIndexConstraint(*varIndex, *lambdaVarIndex, ctx)) {
  2470. input->AddConstraint(outVarIndex);
  2471. }
  2472. }
  2473. } else {
  2474. if (lambdaMulti) {
  2475. TVarIndexConstraintNode::TMapType remapItems;
  2476. for (auto& multiItem: lambdaMulti->GetItems()) {
  2477. for (auto& varItem: varIndex->GetIndexMapping()) {
  2478. remapItems.push_back(std::make_pair(multiItem.first, varItem.second));
  2479. }
  2480. }
  2481. if (!remapItems.empty()) {
  2482. ::SortUnique(remapItems);
  2483. input->AddConstraint(ctx.MakeConstraint<TVarIndexConstraintNode>(std::move(remapItems)));
  2484. }
  2485. } else {
  2486. input->AddConstraint(varIndex);
  2487. }
  2488. }
  2489. }
  2490. if (lambdaMulti && !input->Head().GetConstraint<TEmptyConstraintNode>()) {
  2491. TMultiConstraintNode::TMapType remappedItems;
  2492. for (const auto& item: lambdaMulti->GetItems()) {
  2493. remappedItems.push_back(std::make_pair(item.first, TConstraintSet{}));
  2494. if (!multiInput) { // remapping one to many
  2495. if (const auto empty = item.second.template GetConstraint<TEmptyConstraintNode>())
  2496. remappedItems.pop_back();
  2497. else {
  2498. if (const auto unique = item.second.template GetConstraint<TUniqueConstraintNode>())
  2499. remappedItems.back().second.AddConstraint(unique);
  2500. if (const auto distinct = item.second.template GetConstraint<TDistinctConstraintNode>())
  2501. remappedItems.back().second.AddConstraint(distinct);
  2502. }
  2503. }
  2504. else if (lambdaVarIndex && multi) {
  2505. const auto range = lambdaVarIndex->GetIndexMapping().equal_range(item.first);
  2506. switch (std::distance(range.first, range.second)) {
  2507. case 0: // new index
  2508. break;
  2509. case 1: // remapping 1 to 1
  2510. if (auto origConstr = multi->GetItem(range.first->second)) {
  2511. if (const auto empty = item.second.template GetConstraint<TEmptyConstraintNode>())
  2512. remappedItems.pop_back();
  2513. else {
  2514. if (const auto unique = item.second.template GetConstraint<TUniqueConstraintNode>())
  2515. remappedItems.back().second.AddConstraint(unique);
  2516. if (const auto distinct = item.second.template GetConstraint<TDistinctConstraintNode>())
  2517. remappedItems.back().second.AddConstraint(distinct);
  2518. }
  2519. } else {
  2520. remappedItems.pop_back();
  2521. }
  2522. break;
  2523. default: // remapping many to one
  2524. {
  2525. std::vector<const TConstraintSet*> nonEmpty;
  2526. for (auto i = range.first; i != range.second; ++i) {
  2527. if (auto origConstr = multi->GetItem(i->second)) {
  2528. nonEmpty.push_back(origConstr);
  2529. }
  2530. }
  2531. EraseIf(nonEmpty, [] (const TConstraintSet* c) { return !!c->GetConstraint<TEmptyConstraintNode>(); });
  2532. if (nonEmpty.empty()) {
  2533. remappedItems.back().second.AddConstraint(ctx.MakeConstraint<TEmptyConstraintNode>());
  2534. } else if (nonEmpty.size() == 1) {
  2535. remappedItems.back().second = std::move(*nonEmpty.front());
  2536. }
  2537. }
  2538. }
  2539. } else {
  2540. remappedItems.back().second = item.second;
  2541. }
  2542. }
  2543. if (remappedItems) {
  2544. input->AddConstraint(ctx.MakeConstraint<TMultiConstraintNode>(std::move(remappedItems)));
  2545. }
  2546. }
  2547. else if (multi && lambdaVarIndex) { // Many to one
  2548. const auto range = lambdaVarIndex->GetIndexMapping().equal_range(0);
  2549. std::vector<const TConstraintSet*> nonEmpty;
  2550. for (auto i = range.first; i != range.second; ++i) {
  2551. if (auto origConstr = multi->GetItem(i->second)) {
  2552. nonEmpty.push_back(origConstr);
  2553. }
  2554. }
  2555. EraseIf(nonEmpty, [] (const TConstraintSet* c) { return !!c->GetConstraint<TEmptyConstraintNode>(); });
  2556. if (nonEmpty.empty()) {
  2557. input->AddConstraint(ctx.MakeConstraint<TEmptyConstraintNode>());
  2558. } else if (nonEmpty.size() == 1) {
  2559. input->SetConstraints(*nonEmpty.front());
  2560. }
  2561. }
  2562. TApplyConstraintFromInput<TCoBase::idx_ListHandlerLambda, TEmptyConstraintNode>::Do(input);
  2563. return FromFirst<TEmptyConstraintNode>(input, output, ctx);
  2564. }
  2565. template <bool Final>
  2566. TStatus AggregateWrap(const TExprNode::TPtr& input, TExprNode::TPtr& output, TExprContext& ctx) const {
  2567. if (HasSetting(input->Tail(), "session")) {
  2568. // TODO: support sessions
  2569. return TStatus::Ok;
  2570. }
  2571. if (const auto size = input->Child(1)->ChildrenSize()) {
  2572. if constexpr (Final) {
  2573. bool allKeysInOutput = true;
  2574. if (auto outputColumnsSetting = GetSetting(input->Tail(), "output_columns")) {
  2575. THashSet<TStringBuf> outputColumns;
  2576. for (auto& col : outputColumnsSetting->Child(1)->Children()) {
  2577. YQL_ENSURE(col->IsAtom());
  2578. outputColumns.insert(col->Content());
  2579. }
  2580. allKeysInOutput = AllOf(input->Child(1)->Children(), [&](const auto& key) { return outputColumns.contains(key->Content()); });
  2581. }
  2582. if (allKeysInOutput) {
  2583. std::vector<std::string_view> columns;
  2584. columns.reserve(size);
  2585. for (const auto& child: input->Child(1)->Children()) {
  2586. columns.emplace_back(child->Content());
  2587. }
  2588. input->AddConstraint(ctx.MakeConstraint<TUniqueConstraintNode>(columns));
  2589. input->AddConstraint(ctx.MakeConstraint<TDistinctConstraintNode>(columns));
  2590. }
  2591. }
  2592. return FromFirst<TEmptyConstraintNode>(input, output, ctx);
  2593. }
  2594. return TStatus::Ok;
  2595. }
  2596. TStatus FoldWrap(const TExprNode::TPtr& input, TExprNode::TPtr&, TExprContext& ctx) const {
  2597. const TStructExprType* inItemType = GetNonEmptyStructItemType(*input->Head().GetTypeAnn());
  2598. const TStructExprType* outItemType = GetNonEmptyStructItemType(*input->GetTypeAnn());
  2599. if (!inItemType || !outItemType) {
  2600. return UpdateAllChildLambdasConstraints(*input);
  2601. }
  2602. if (input->Child(1)->IsLambda()) {
  2603. TConstraintNode::TListType argConstraints;
  2604. if (const auto status = UpdateLambdaConstraints(input->ChildRef(1), ctx, {argConstraints}); status != TStatus::Ok) {
  2605. return status;
  2606. }
  2607. }
  2608. const auto initState = input->Child(1);
  2609. auto stateConstraints = initState->GetAllConstraints();
  2610. stateConstraints.erase(
  2611. std::remove_if(
  2612. stateConstraints.begin(),
  2613. stateConstraints.end(),
  2614. [](const TConstraintNode* c) { return c->GetName() == TEmptyConstraintNode::Name(); }
  2615. ),
  2616. stateConstraints.end()
  2617. );
  2618. TConstraintNode::TListType argConstraints;
  2619. if (const auto status = UpdateLambdaConstraints(input->TailRef(), ctx, {argConstraints, stateConstraints}); status != TStatus::Ok) {
  2620. return status;
  2621. }
  2622. return TStatus::Ok;
  2623. }
  2624. TStatus MultiHoppingCoreWrap(const TExprNode::TPtr& input, TExprNode::TPtr&, TExprContext& ctx) const {
  2625. if (const auto status = UpdateAllChildLambdasConstraints(*input); status != TStatus::Ok) {
  2626. return status;
  2627. }
  2628. TExprNode::TPtr keySelectorLambda = input->Child(TCoMultiHoppingCore::idx_KeyExtractor);
  2629. const auto keys = GetPathsToKeys(keySelectorLambda->Tail(), keySelectorLambda->Head().Head());
  2630. std::vector<std::string_view> columns(keys.size());
  2631. std::transform(keys.begin(), keys.end(), columns.begin(), [](const TPartOfConstraintBase::TPathType& path) -> std::string_view {
  2632. return path.front();
  2633. });
  2634. if (!columns.empty()) {
  2635. input->AddConstraint(ctx.MakeConstraint<TUniqueConstraintNode>(columns));
  2636. input->AddConstraint(ctx.MakeConstraint<TDistinctConstraintNode>(columns));
  2637. }
  2638. return TStatus::Ok;
  2639. }
  2640. private:
  2641. template <class TConstraintContainer>
  2642. static void CopyExcept(TConstraintContainer& dst, const TConstraintContainer& from, const TSet<TStringBuf>& except) {
  2643. for (auto c: from.GetAllConstraints()) {
  2644. if (!except.contains(c->GetName())) {
  2645. dst.AddConstraint(c);
  2646. }
  2647. }
  2648. }
  2649. template <class TConstraintContainer>
  2650. static void CopyExcept(TConstraintContainer& dst, const TConstraintContainer& from, TStringBuf except) {
  2651. for (auto c: from.GetAllConstraints()) {
  2652. if (c->GetName() != except) {
  2653. dst.AddConstraint(c);
  2654. }
  2655. }
  2656. }
  2657. static void ExtractKeys(const TExprNode& keySelectorLambda, TVector<TStringBuf>& columns) {
  2658. const auto arg = keySelectorLambda.Head().Child(0);
  2659. auto body = keySelectorLambda.Child(1);
  2660. if (body->IsCallable("StablePickle")) {
  2661. body = body->Child(0);
  2662. }
  2663. ExtractSimpleKeys(body, arg, columns);
  2664. }
  2665. template<class TConstraintWithFields>
  2666. static const TConstraintWithFields* GetDetailed(const TConstraintWithFields* constraint, const TTypeAnnotationNode& type, TExprContext& ctx) {
  2667. return constraint ? constraint->GetComplicatedForType(type, ctx) : nullptr;
  2668. }
  2669. static const TStructExprType* GetNonEmptyStructItemType(const TTypeAnnotationNode& type) {
  2670. const auto itemType = GetSeqItemType(&type);
  2671. if (!itemType || itemType->GetKind() != ETypeAnnotationKind::Struct) {
  2672. return nullptr;
  2673. }
  2674. const TStructExprType* structType = itemType->Cast<TStructExprType>();
  2675. return structType->GetSize() ? structType : nullptr;
  2676. }
  2677. static const TSortedConstraintNode* DeduceSortConstraint(const TExprNode& directions, const TExprNode& keyExtractor, TExprContext& ctx) {
  2678. if (const auto& columns = ExtractSimpleSortTraits(directions, keyExtractor); !columns.empty()) {
  2679. TSortedConstraintNode::TContainerType content(columns.size());
  2680. std::transform(columns.cbegin(), columns.cend(), content.begin(), [](const std::pair<TPartOfConstraintBase::TPathType, bool>& item) {
  2681. return std::make_pair(TSortedConstraintNode::TSetType{item.first}, item.second);
  2682. });
  2683. return ctx.MakeConstraint<TSortedConstraintNode>(std::move(content));
  2684. }
  2685. return nullptr;
  2686. }
  2687. static const TVarIndexConstraintNode* GetVarIndexOverVarIndexConstraint(const TVarIndexConstraintNode& inputVarIndex,
  2688. const TVarIndexConstraintNode& varIndex, TExprContext& ctx)
  2689. {
  2690. TVarIndexConstraintNode::TMapType result;
  2691. for (auto& item: varIndex.GetIndexMapping()) {
  2692. auto range = inputVarIndex.GetIndexMapping().equal_range(item.second);
  2693. for (auto it = range.first; it != range.second; ++it) {
  2694. result.push_back(std::make_pair(item.first, it->second));
  2695. }
  2696. }
  2697. if (!result.empty()) {
  2698. return ctx.MakeConstraint<TVarIndexConstraintNode>(std::move(result));
  2699. }
  2700. return nullptr;
  2701. }
  2702. static const TExprNode* SkipModifiers(const TExprNode* valueNode) {
  2703. if (TCoJust::Match(valueNode)) {
  2704. return SkipModifiers(valueNode->Child(0));
  2705. }
  2706. if (TCoUnwrap::Match(valueNode)) {
  2707. return SkipModifiers(valueNode->Child(0));
  2708. }
  2709. return valueNode;
  2710. }
  2711. private:
  2712. const bool SubGraph;
  2713. std::unordered_map<std::string_view, THandler> Functions;
  2714. };
  2715. template<> const TPartOfSortedConstraintNode*
  2716. TCallableConstraintTransformer::GetConstraintFromWideResultLambda<TPartOfSortedConstraintNode>(const TExprNode& lambda, TExprContext& ctx) {
  2717. TPartOfSortedConstraintNode::TMapType sorted;
  2718. for (auto i = 1U; i < lambda.ChildrenSize(); ++i) {
  2719. if (const auto part = lambda.Child(i)->GetConstraint<TPartOfSortedConstraintNode>())
  2720. TPartOfSortedConstraintNode::UniqueMerge(sorted, part->GetColumnMapping(ctx.GetIndexAsString(i - 1U)));
  2721. }
  2722. return sorted.empty() ? nullptr : ctx.MakeConstraint<TPartOfSortedConstraintNode>(std::move(sorted));
  2723. }
  2724. template<> const TPartOfChoppedConstraintNode*
  2725. TCallableConstraintTransformer::GetConstraintFromWideResultLambda<TPartOfChoppedConstraintNode>(const TExprNode& lambda, TExprContext& ctx) {
  2726. TPartOfChoppedConstraintNode::TMapType chopped;
  2727. for (auto i = 1U; i < lambda.ChildrenSize(); ++i) {
  2728. if (const auto part = lambda.Child(i)->GetConstraint<TPartOfChoppedConstraintNode>())
  2729. TPartOfChoppedConstraintNode::UniqueMerge(chopped, part->GetColumnMapping(ctx.GetIndexAsString(i - 1U)));
  2730. }
  2731. return chopped.empty() ? nullptr : ctx.MakeConstraint<TPartOfChoppedConstraintNode>(std::move(chopped));
  2732. }
  2733. template<> const TPartOfUniqueConstraintNode*
  2734. TCallableConstraintTransformer::GetConstraintFromWideResultLambda<TPartOfUniqueConstraintNode>(const TExprNode& lambda, TExprContext& ctx) {
  2735. TPartOfUniqueConstraintNode::TMapType uniques;
  2736. for (auto i = 1U; i < lambda.ChildrenSize(); ++i) {
  2737. if (const auto part = lambda.Child(i)->GetConstraint<TPartOfUniqueConstraintNode>())
  2738. TPartOfUniqueConstraintNode::UniqueMerge(uniques, part->GetColumnMapping(ctx.GetIndexAsString(i - 1U)));
  2739. }
  2740. return uniques.empty() ? nullptr : ctx.MakeConstraint<TPartOfUniqueConstraintNode>(std::move(uniques));
  2741. }
  2742. template<> const TPartOfDistinctConstraintNode*
  2743. TCallableConstraintTransformer::GetConstraintFromWideResultLambda<TPartOfDistinctConstraintNode>(const TExprNode& lambda, TExprContext& ctx) {
  2744. TPartOfDistinctConstraintNode::TMapType uniques;
  2745. for (auto i = 1U; i < lambda.ChildrenSize(); ++i) {
  2746. if (const auto part = lambda.Child(i)->GetConstraint<TPartOfDistinctConstraintNode>())
  2747. TPartOfDistinctConstraintNode::UniqueMerge(uniques, part->GetColumnMapping(ctx.GetIndexAsString(i - 1U)));
  2748. }
  2749. return uniques.empty() ? nullptr : ctx.MakeConstraint<TPartOfDistinctConstraintNode>(std::move(uniques));
  2750. }
  2751. template<> const TVarIndexConstraintNode*
  2752. TCallableConstraintTransformer::TCallableConstraintTransformer::GetConstraintFromWideResultLambda<TVarIndexConstraintNode>(const TExprNode& lambda, TExprContext& ctx) {
  2753. TVector<const TConstraintSet*> structConstraints;
  2754. structConstraints.reserve(lambda.ChildrenSize() - 1U);
  2755. for (auto i = 1U; i < lambda.ChildrenSize(); ++i) {
  2756. auto valueNode = lambda.Child(i);
  2757. if (TCoCoalesce::Match(valueNode)) {
  2758. if (valueNode->Head().GetTypeAnn()->GetKind() != ETypeAnnotationKind::Optional || valueNode->ChildrenSize() == 1) {
  2759. valueNode = valueNode->Child(0);
  2760. }
  2761. }
  2762. if (TCoJust::Match(valueNode)) {
  2763. valueNode = valueNode->Child(0);
  2764. }
  2765. if (TCoMember::Match(valueNode) || TCoNth::Match(valueNode)) {
  2766. structConstraints.push_back(&valueNode->Head().GetConstraintSet());
  2767. } else if (valueNode->Type() == TExprNode::Argument) {
  2768. structConstraints.push_back(&valueNode->GetConstraintSet());
  2769. }
  2770. }
  2771. return TVarIndexConstraintNode::MakeCommon(structConstraints, ctx);
  2772. }
  2773. template<class TConstraint> const TConstraint*
  2774. TCallableConstraintTransformer::GetConstraintFromWideResultLambda(const TExprNode&, TExprContext&) { return nullptr; }
  2775. class TDefaultCallableConstraintTransformer : public TSyncTransformerBase {
  2776. public:
  2777. TStatus DoTransform(TExprNode::TPtr input, TExprNode::TPtr& output, TExprContext& ctx) override {
  2778. Y_UNUSED(output);
  2779. Y_UNUSED(ctx);
  2780. return UpdateAllChildLambdasConstraints(*input);
  2781. }
  2782. void Rewind() final {
  2783. }
  2784. };
  2785. template<bool DisableCheck>
  2786. class TConstraintTransformer : public TGraphTransformerBase {
  2787. public:
  2788. TConstraintTransformer(TAutoPtr<IGraphTransformer> callableTransformer, TTypeAnnotationContext& types)
  2789. : CallableTransformer(callableTransformer)
  2790. , Types(types)
  2791. {
  2792. }
  2793. ~TConstraintTransformer() = default;
  2794. TStatus DoTransform(TExprNode::TPtr input, TExprNode::TPtr& output, TExprContext& ctx) final {
  2795. YQL_PROFILE_SCOPE(DEBUG, "ConstraintTransformer::DoTransform");
  2796. output = input;
  2797. auto status = TransformNode(input, output, ctx);
  2798. UpdateStatusIfChanged(status, input, output);
  2799. if (status.Level != TStatus::Error && HasRenames) {
  2800. output = ctx.ReplaceNodes(std::move(output), Processed);
  2801. }
  2802. Processed.clear();
  2803. if (status == TStatus::Ok) {
  2804. Types.ExpectedConstraints.clear();
  2805. }
  2806. HasRenames = false;
  2807. return status;
  2808. }
  2809. NThreading::TFuture<void> DoGetAsyncFuture(const TExprNode& input) final {
  2810. YQL_PROFILE_SCOPE(DEBUG, "ConstraintTransformer::DoGetAsyncFuture");
  2811. Y_UNUSED(input);
  2812. TVector<NThreading::TFuture<void>> futures;
  2813. for (const auto& callable : CallableInputs) {
  2814. futures.push_back(CallableTransformer->GetAsyncFuture(*callable));
  2815. }
  2816. return WaitExceptionOrAll(futures);
  2817. }
  2818. TStatus DoApplyAsyncChanges(TExprNode::TPtr input, TExprNode::TPtr& output, TExprContext& ctx) final {
  2819. YQL_PROFILE_SCOPE(DEBUG, "ConstraintTransformer::DoApplyAsyncChanges");
  2820. output = input;
  2821. TStatus combinedStatus = TStatus::Ok;
  2822. for (const auto& callable : CallableInputs) {
  2823. callable->SetState(TExprNode::EState::ConstrPending);
  2824. TExprNode::TPtr callableOutput;
  2825. auto status = CallableTransformer->ApplyAsyncChanges(callable, callableOutput, ctx);
  2826. Y_ABORT_UNLESS(callableOutput);
  2827. YQL_ENSURE(status != TStatus::Async);
  2828. YQL_ENSURE(callableOutput == callable);
  2829. combinedStatus = combinedStatus.Combine(status);
  2830. if (status.Level == TStatus::Error) {
  2831. callable->SetState(TExprNode::EState::Error);
  2832. }
  2833. }
  2834. CallableInputs.clear();
  2835. if (combinedStatus.Level == TStatus::Ok) {
  2836. Processed.clear();
  2837. }
  2838. return combinedStatus;
  2839. }
  2840. void Rewind() final {
  2841. CallableTransformer->Rewind();
  2842. CallableInputs.clear();
  2843. Processed.clear();
  2844. HasRenames = false;
  2845. CurrentFunctions = {};
  2846. }
  2847. private:
  2848. TStatus TransformNode(const TExprNode::TPtr& start, TExprNode::TPtr& output, TExprContext& ctx) {
  2849. output = start;
  2850. auto processedPair = Processed.emplace(start.Get(), nullptr); // by default node is not changed
  2851. if (!processedPair.second) {
  2852. if (processedPair.first->second) {
  2853. output = processedPair.first->second;
  2854. return TStatus::Repeat;
  2855. }
  2856. switch (start->GetState()) {
  2857. case TExprNode::EState::Initial:
  2858. case TExprNode::EState::TypeInProgress:
  2859. case TExprNode::EState::TypePending:
  2860. return TStatus(TStatus::Repeat, true);
  2861. case TExprNode::EState::TypeComplete:
  2862. break;
  2863. case TExprNode::EState::ConstrInProgress:
  2864. return IGraphTransformer::TStatus::Async;
  2865. case TExprNode::EState::ConstrPending:
  2866. if (start->Type() == TExprNode::Lambda) {
  2867. if (start->Head().GetState() != TExprNode::EState::ConstrComplete) {
  2868. return TStatus::Ok;
  2869. } else if (start->Head().ChildrenSize() == 0) {
  2870. break;
  2871. }
  2872. }
  2873. if (start->Type() == TExprNode::Arguments || start->Type() == TExprNode::Argument) {
  2874. break;
  2875. }
  2876. return TStatus(TStatus::Repeat, true);
  2877. case TExprNode::EState::ConstrComplete:
  2878. case TExprNode::EState::ExecutionInProgress:
  2879. case TExprNode::EState::ExecutionRequired:
  2880. case TExprNode::EState::ExecutionPending:
  2881. case TExprNode::EState::ExecutionComplete:
  2882. return TStatus::Ok;
  2883. case TExprNode::EState::Error:
  2884. return TStatus::Error;
  2885. default:
  2886. YQL_ENSURE(false, "Unknown state");
  2887. }
  2888. }
  2889. auto input = start;
  2890. for (;;) {
  2891. TIssueScopeGuard issueScope(ctx.IssueManager, [this, input, &ctx]() -> TIssuePtr {
  2892. TStringBuilder str;
  2893. str << "At ";
  2894. switch (input->Type()) {
  2895. case TExprNode::Callable:
  2896. if (!CurrentFunctions.empty() && CurrentFunctions.top().second) {
  2897. return nullptr;
  2898. }
  2899. if (!CurrentFunctions.empty()) {
  2900. CurrentFunctions.top().second = true;
  2901. }
  2902. str << "function: " << input->Content();
  2903. break;
  2904. case TExprNode::List:
  2905. if (CurrentFunctions.empty()) {
  2906. str << "tuple";
  2907. } else if (!CurrentFunctions.top().second) {
  2908. CurrentFunctions.top().second = true;
  2909. str << "function: " << CurrentFunctions.top().first;
  2910. } else {
  2911. return nullptr;
  2912. }
  2913. break;
  2914. case TExprNode::Lambda:
  2915. if (CurrentFunctions.empty()) {
  2916. str << "lambda";
  2917. } else if (!CurrentFunctions.top().second) {
  2918. CurrentFunctions.top().second = true;
  2919. str << "function: " << CurrentFunctions.top().first;
  2920. } else {
  2921. return nullptr;
  2922. }
  2923. break;
  2924. default:
  2925. str << "unknown";
  2926. }
  2927. return MakeIntrusive<TIssue>(ctx.GetPosition(input->Pos()), str);
  2928. });
  2929. if (input->IsCallable()) {
  2930. CurrentFunctions.emplace(input->Content(), false);
  2931. }
  2932. Y_SCOPE_EXIT(this, input) {
  2933. if (input->IsCallable()) {
  2934. CurrentFunctions.pop();
  2935. if (!CurrentFunctions.empty() && CurrentFunctions.top().first.ends_with('!')) {
  2936. CurrentFunctions.top().second = true;
  2937. }
  2938. }
  2939. };
  2940. TStatus retStatus = TStatus::Error;
  2941. switch (input->GetState()) {
  2942. case TExprNode::EState::Initial:
  2943. case TExprNode::EState::TypeInProgress:
  2944. case TExprNode::EState::TypePending:
  2945. return TStatus(TStatus::Repeat, true);
  2946. case TExprNode::EState::TypeComplete:
  2947. case TExprNode::EState::ConstrPending:
  2948. break;
  2949. case TExprNode::EState::ConstrInProgress:
  2950. return IGraphTransformer::TStatus::Async;
  2951. case TExprNode::EState::ConstrComplete:
  2952. case TExprNode::EState::ExecutionInProgress:
  2953. case TExprNode::EState::ExecutionRequired:
  2954. case TExprNode::EState::ExecutionPending:
  2955. case TExprNode::EState::ExecutionComplete:
  2956. return TStatus::Ok;
  2957. case TExprNode::EState::Error:
  2958. return TStatus::Error;
  2959. default:
  2960. YQL_ENSURE(false, "Unknown state");
  2961. }
  2962. input->SetState(TExprNode::EState::ConstrPending);
  2963. switch (input->Type()) {
  2964. case TExprNode::Atom:
  2965. case TExprNode::World:
  2966. input->SetState(TExprNode::EState::ConstrComplete);
  2967. CheckExpected(*input);
  2968. return TStatus::Ok;
  2969. case TExprNode::List:
  2970. {
  2971. retStatus = TransformChildren(input, output, ctx);
  2972. if (retStatus == TStatus::Ok) {
  2973. retStatus = CallableTransformer->Transform(input, output, ctx);
  2974. if (retStatus == TStatus::Ok) {
  2975. input->SetState(TExprNode::EState::ConstrComplete);
  2976. CheckExpected(*input);
  2977. break;
  2978. }
  2979. }
  2980. if (retStatus != TStatus::Error && input != output) {
  2981. processedPair.first->second = output;
  2982. }
  2983. break;
  2984. }
  2985. case TExprNode::Lambda:
  2986. {
  2987. YQL_ENSURE(input->ChildrenSize() > 0U);
  2988. TExprNode::TPtr out;
  2989. auto argStatus = TransformNode(input->HeadPtr(), out, ctx);
  2990. UpdateStatusIfChanged(argStatus, input->HeadPtr(), out);
  2991. if (argStatus.Level == TStatus::Error) {
  2992. input->SetState(TExprNode::EState::Error);
  2993. return argStatus;
  2994. }
  2995. if (argStatus.Level == TStatus::Repeat)
  2996. return TStatus::Ok;
  2997. TStatus bodyStatus = TStatus::Ok;
  2998. TExprNode::TListType newBody;
  2999. newBody.reserve(input->ChildrenSize() - 1U);
  3000. bool updatedChildren = false;
  3001. for (ui32 i = 1U; i < input->ChildrenSize(); ++i) {
  3002. const auto child = input->ChildPtr(i);
  3003. TExprNode::TPtr newChild;
  3004. auto childStatus = TransformNode(child, newChild, ctx);
  3005. UpdateStatusIfChanged(childStatus, child, newChild);
  3006. updatedChildren = updatedChildren || (newChild != child);
  3007. bodyStatus = bodyStatus.Combine(childStatus);
  3008. newBody.emplace_back(std::move(newChild));
  3009. }
  3010. retStatus = argStatus.Combine(bodyStatus);
  3011. if (retStatus != TStatus::Ok) {
  3012. if (retStatus.Level == TStatus::Error) {
  3013. input->SetState(TExprNode::EState::Error);
  3014. }
  3015. else if (updatedChildren) {
  3016. output = ctx.DeepCopyLambda(*input, std::move(newBody));
  3017. processedPair.first->second = output;
  3018. HasRenames = true;
  3019. }
  3020. } else {
  3021. if (input->ChildrenSize() != 2U)
  3022. input->SetState(TExprNode::EState::ConstrComplete);
  3023. else
  3024. input->CopyConstraints(input->Tail());
  3025. CheckExpected(*input);
  3026. }
  3027. break;
  3028. }
  3029. case TExprNode::Argument:
  3030. if (input->GetState() != TExprNode::EState::ConstrComplete) {
  3031. return TStatus::Repeat;
  3032. }
  3033. return TStatus::Ok;
  3034. case TExprNode::Arguments:
  3035. {
  3036. if (input->Children().empty()) {
  3037. if (TExprNode::EState::ConstrComplete == input->GetState()) {
  3038. return TStatus::Ok;
  3039. }
  3040. return TStatus::Repeat;
  3041. }
  3042. retStatus = TStatus::Ok;
  3043. for (auto& child : input->Children()) {
  3044. TExprNode::TPtr tmp;
  3045. auto childStatus = TransformNode(child, tmp, ctx);
  3046. UpdateStatusIfChanged(childStatus, child, tmp);
  3047. YQL_ENSURE(tmp == child);
  3048. retStatus = retStatus.Combine(childStatus);
  3049. }
  3050. if (retStatus != TStatus::Ok) {
  3051. if (retStatus.Level == TStatus::Error) {
  3052. input->SetState(TExprNode::EState::Error);
  3053. }
  3054. } else {
  3055. input->SetState(TExprNode::EState::ConstrComplete);
  3056. }
  3057. return retStatus;
  3058. }
  3059. case TExprNode::Callable:
  3060. {
  3061. retStatus = TransformChildren(input, output, ctx);
  3062. if (retStatus != TStatus::Ok) {
  3063. if (retStatus != TStatus::Error && input != output) {
  3064. processedPair.first->second = output;
  3065. }
  3066. break;
  3067. }
  3068. CurrentFunctions.top().second = true;
  3069. retStatus = CallableTransformer->Transform(input, output, ctx);
  3070. if (retStatus == TStatus::Error) {
  3071. input->SetState(TExprNode::EState::Error);
  3072. } else if (retStatus == TStatus::Ok) {
  3073. // Sanity check
  3074. for (size_t i = 0; i < input->ChildrenSize(); ++i) {
  3075. YQL_ENSURE(input->Child(i)->GetState() >= TExprNode::EState::ConstrComplete,
  3076. "Child with index " << i << " of callable " << TString{input->Content()}.Quote() << " has bad state after constraint transform");
  3077. }
  3078. input->SetState(TExprNode::EState::ConstrComplete);
  3079. CheckExpected(*input);
  3080. } else if (retStatus == TStatus::Async) {
  3081. CallableInputs.push_back(input);
  3082. input->SetState(TExprNode::EState::ConstrInProgress);
  3083. } else {
  3084. if (output != input.Get()) {
  3085. processedPair.first->second = output;
  3086. HasRenames = true;
  3087. }
  3088. }
  3089. break;
  3090. }
  3091. default:
  3092. YQL_ENSURE(false, "Unknown type");
  3093. }
  3094. if (retStatus.Level != TStatus::Repeat || retStatus.HasRestart) {
  3095. return retStatus;
  3096. }
  3097. input = output;
  3098. }
  3099. }
  3100. TStatus TransformChildren(const TExprNode::TPtr& input, TExprNode::TPtr& output, TExprContext& ctx) {
  3101. TStatus combinedStatus = TStatus::Ok;
  3102. TExprNode::TListType newChildren;
  3103. bool updatedChildren = false;
  3104. for (ui32 i = 0; i < input->ChildrenSize(); ++i) {
  3105. const auto child = input->ChildPtr(i);
  3106. TExprNode::TPtr newChild;
  3107. auto childStatus = TransformNode(child, newChild, ctx);
  3108. UpdateStatusIfChanged(childStatus, child, newChild);
  3109. updatedChildren = updatedChildren || (newChild != child);
  3110. combinedStatus = combinedStatus.Combine(childStatus);
  3111. newChildren.emplace_back(std::move(newChild));
  3112. }
  3113. if (combinedStatus != TStatus::Ok) {
  3114. if (combinedStatus.Level == TStatus::Error) {
  3115. input->SetState(TExprNode::EState::Error);
  3116. }
  3117. else if (updatedChildren) {
  3118. output = ctx.ChangeChildren(*input, std::move(newChildren));
  3119. HasRenames = true;
  3120. }
  3121. }
  3122. return combinedStatus;
  3123. }
  3124. void UpdateStatusIfChanged(TStatus& status, const TExprNode::TPtr& input, const TExprNode::TPtr& output) {
  3125. if (status.Level == TStatus::Ok && input != output) {
  3126. status = TStatus(TStatus::Repeat, status.HasRestart);
  3127. }
  3128. }
  3129. void CheckExpected(const TExprNode& input) {
  3130. if constexpr (DisableCheck)
  3131. return;
  3132. if (const auto it = Types.ExpectedConstraints.find(input.UniqueId()); it != Types.ExpectedConstraints.cend()) {
  3133. for (const auto expectedConstr: it->second) {
  3134. if (!Types.DisableConstraintCheck.contains(expectedConstr->GetName())) {
  3135. if (auto newConstr = input.GetConstraint(expectedConstr->GetName())) {
  3136. if (expectedConstr->GetName() == TMultiConstraintNode::Name()) {
  3137. YQL_ENSURE(static_cast<const TMultiConstraintNode*>(newConstr)->FilteredIncludes(*expectedConstr, Types.DisableConstraintCheck), "Rewrite error, unequal " << *newConstr
  3138. << " constraint in node " << input.Content() << ", previous was " << *expectedConstr);
  3139. } else {
  3140. YQL_ENSURE(newConstr->Includes(*expectedConstr), "Rewrite error, unequal " << *newConstr
  3141. << " constraint in node " << input.Content() << ", previous was " << *expectedConstr);
  3142. }
  3143. } else {
  3144. if (expectedConstr->GetName() == TMultiConstraintNode::Name()) {
  3145. // Constraint Multi(0:{Empty},1:{Empty}, ..., N:{Empty}) can be reduced to Empty
  3146. newConstr = input.GetConstraint<TEmptyConstraintNode>();
  3147. }
  3148. YQL_ENSURE(newConstr, "Rewrite error, missing " << *expectedConstr << " constraint in node " << input.Content());
  3149. }
  3150. }
  3151. }
  3152. }
  3153. }
  3154. private:
  3155. TAutoPtr<IGraphTransformer> CallableTransformer;
  3156. std::deque<TExprNode::TPtr> CallableInputs;
  3157. TNodeOnNodeOwnedMap Processed;
  3158. bool HasRenames = false;
  3159. std::stack<std::pair<std::string_view, bool>> CurrentFunctions;
  3160. TTypeAnnotationContext& Types;
  3161. };
  3162. } // namespace
  3163. TAutoPtr<IGraphTransformer> CreateConstraintTransformer(TTypeAnnotationContext& types, bool instantOnly, bool subGraph, bool disableCheck) {
  3164. TAutoPtr<IGraphTransformer> callableTransformer(new TCallableConstraintTransformer(types, instantOnly, subGraph));
  3165. return disableCheck ?
  3166. static_cast<IGraphTransformer*>(new TConstraintTransformer<true>(callableTransformer, types)):
  3167. static_cast<IGraphTransformer*>(new TConstraintTransformer<false>(callableTransformer, types));
  3168. }
  3169. TAutoPtr<IGraphTransformer> CreateDefCallableConstraintTransformer() {
  3170. return new TDefaultCallableConstraintTransformer();
  3171. }
  3172. IGraphTransformer::TStatus UpdateLambdaConstraints(const TExprNode& lambda) {
  3173. const auto args = lambda.Child(0);
  3174. for (const auto& arg: args->Children()) {
  3175. if (arg->GetState() == TExprNode::EState::TypeComplete || arg->GetState() == TExprNode::EState::ConstrPending) {
  3176. arg->SetState(TExprNode::EState::ConstrComplete);
  3177. }
  3178. YQL_ENSURE(arg->GetAllConstraints().empty());
  3179. }
  3180. if (args->GetState() == TExprNode::EState::TypeComplete || args->GetState() == TExprNode::EState::ConstrPending) {
  3181. args->SetState(TExprNode::EState::ConstrComplete);
  3182. }
  3183. if (lambda.GetState() != TExprNode::EState::ConstrComplete) {
  3184. return IGraphTransformer::TStatus::Repeat;
  3185. }
  3186. return IGraphTransformer::TStatus::Ok;
  3187. }
  3188. IGraphTransformer::TStatus UpdateLambdaConstraints(TExprNode::TPtr& lambda, TExprContext& ctx, const TArrayRef<const TConstraintNode::TListType>& constraints) {
  3189. bool updateArgs = false;
  3190. const auto args = lambda->Child(0);
  3191. YQL_ENSURE(args->ChildrenSize() == constraints.size());
  3192. size_t i = 0;
  3193. for (const auto& constrList: constraints) {
  3194. const auto arg = args->Child(i++);
  3195. if (arg->GetState() == TExprNode::EState::TypeComplete || arg->GetState() == TExprNode::EState::ConstrPending) {
  3196. for (const auto c: constrList) {
  3197. arg->AddConstraint(c);
  3198. }
  3199. arg->SetState(TExprNode::EState::ConstrComplete);
  3200. } else {
  3201. if (constrList.size() != arg->GetAllConstraints().size() || !AllOf(constrList, [arg] (const TConstraintNode* c) { return arg->GetConstraint(c->GetName()) == c; })) {
  3202. updateArgs = true;
  3203. }
  3204. }
  3205. }
  3206. if (updateArgs) {
  3207. TNodeOnNodeOwnedMap replaces(constraints.size());
  3208. TExprNode::TListType argsChildren;
  3209. argsChildren.reserve(constraints.size());
  3210. i = 0;
  3211. for (const auto& constrList: constraints) {
  3212. const auto arg = args->Child(i++);
  3213. const auto newArg = ctx.ShallowCopy(*arg);
  3214. newArg->SetTypeAnn(arg->GetTypeAnn());
  3215. for (const auto c: constrList) {
  3216. newArg->AddConstraint(c);
  3217. }
  3218. newArg->SetState(TExprNode::EState::ConstrComplete);
  3219. YQL_ENSURE(replaces.emplace(arg, newArg).second);
  3220. argsChildren.emplace_back(std::move(newArg));
  3221. }
  3222. auto newArgs = ctx.NewArguments(args->Pos(), std::move(argsChildren));
  3223. newArgs->SetTypeAnn(ctx.MakeType<TUnitExprType>());
  3224. newArgs->SetState(TExprNode::EState::ConstrComplete);
  3225. const auto type = lambda->GetTypeAnn();
  3226. lambda = ctx.NewLambda(lambda->Pos(), std::move(newArgs), ctx.ReplaceNodes<true>(GetLambdaBody(*lambda), replaces));
  3227. lambda->SetTypeAnn(type);
  3228. lambda->Head().ForEachChild(std::bind(&TExprNode::SetDependencyScope, std::placeholders::_1, lambda.Get(), lambda.Get()));
  3229. return IGraphTransformer::TStatus::Repeat;
  3230. }
  3231. if (args->GetState() == TExprNode::EState::TypeComplete || args->GetState() == TExprNode::EState::ConstrPending) {
  3232. args->SetState(TExprNode::EState::ConstrComplete);
  3233. }
  3234. if (lambda->GetState() != TExprNode::EState::ConstrComplete) {
  3235. return IGraphTransformer::TStatus::Repeat;
  3236. }
  3237. return IGraphTransformer::TStatus::Ok;
  3238. }
  3239. IGraphTransformer::TStatus UpdateAllChildLambdasConstraints(const TExprNode& node) {
  3240. IGraphTransformer::TStatus status = IGraphTransformer::TStatus::Ok;
  3241. for (ui32 i = 0; i < node.ChildrenSize(); ++i) {
  3242. const auto child = node.Child(i);
  3243. if (child->Type() == TExprNode::EType::Lambda) {
  3244. status = status.Combine(UpdateLambdaConstraints(*child));
  3245. }
  3246. }
  3247. return status;
  3248. }
  3249. } // namespace NYql