yql_expr_constraint.cpp 188 KB

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