yql_expr_constraint.cpp 187 KB

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