123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606 |
- #include "mkql_computation_node_holders.h"
- #include "mkql_computation_node_pack.h"
- #include "mkql_custom_list.h"
- #include "mkql_value_builder.h"
- #include "presort.h"
- #include <yql/essentials/minikql/mkql_node_builder.h>
- #include <yql/essentials/minikql/mkql_utils.h>
- #include <yql/essentials/minikql/mkql_alloc.h>
- #include <yql/essentials/minikql/mkql_node_cast.h>
- #include <yql/essentials/minikql/mkql_string_util.h>
- #include <yql/essentials/public/udf/udf_value.h>
- #include <library/cpp/containers/stack_vector/stack_vec.h>
- #include <util/generic/singleton.h>
- namespace NKikimr {
- namespace NMiniKQL {
- namespace {
- class TValueDataHolder: public TComputationValue<TValueDataHolder> {
- public:
- TValueDataHolder(TMemoryUsageInfo* memInfo, NUdf::TUnboxedValue&& value)
- : TComputationValue(memInfo)
- , Value(std::move(value))
- {}
- private:
- const NUdf::TUnboxedValue Value;
- };
- class TDirectListHolder: public TComputationValue<TDirectListHolder> {
- public:
- class TIterator: public TComputationValue<TIterator> {
- public:
- TIterator(const TDirectListHolder* parent)
- : TComputationValue(parent->GetMemInfo())
- , Parent(const_cast<TDirectListHolder*>(parent))
- , Iterator(parent->Items)
- , AtStart(true)
- {
- }
- private:
- bool Skip() override {
- if (AtStart) {
- AtStart = false;
- } else {
- if (Iterator.AtEnd()) {
- return false;
- }
- Iterator.Next();
- }
- return !Iterator.AtEnd();
- }
- bool Next(NUdf::TUnboxedValue& value) override {
- if (!Skip())
- return false;
- value = Iterator.Current();
- return true;
- }
- const NUdf::TRefCountedPtr<TDirectListHolder> Parent;
- TDefaultListRepresentation::TIterator Iterator;
- bool AtStart;
- };
- class TDictIterator: public TComputationValue<TDictIterator> {
- public:
- TDictIterator(TMemoryUsageInfo* memInfo, NUdf::TUnboxedValue&& iter)
- : TComputationValue(memInfo)
- , Iter(std::move(iter))
- , Index(Max<ui64>())
- {}
- private:
- bool Next(NUdf::TUnboxedValue& key) override {
- if (Iter.Skip()) {
- key = NUdf::TUnboxedValuePod(++Index);
- return true;
- }
- return false;
- }
- bool NextPair(NUdf::TUnboxedValue& key, NUdf::TUnboxedValue& payload) override {
- if (Iter.Next(payload)) {
- key = NUdf::TUnboxedValuePod(++Index);
- return true;
- }
- return false;
- }
- bool Skip() override {
- if (Iter.Skip()) {
- ++Index;
- return true;
- }
- return false;
- }
- const NUdf::TUnboxedValue Iter;
- ui64 Index;
- };
- TDirectListHolder(TMemoryUsageInfo* memInfo, TDefaultListRepresentation&& items)
- : TComputationValue(memInfo)
- , Items(std::move(items))
- {}
- private:
- bool Contains(const NUdf::TUnboxedValuePod& key) const override {
- const ui64 index = key.Get<ui64>();
- return (index < GetListLength());
- }
- NUdf::TUnboxedValue Lookup(const NUdf::TUnboxedValuePod& key) const override {
- const ui64 index = key.Get<ui64>();
- if (index >= GetListLength()) {
- return NUdf::TUnboxedValuePod();
- }
- return Items.GetItemByIndex(index).Release().MakeOptional();
- }
- NUdf::TUnboxedValue GetListIterator() const override {
- return NUdf::TUnboxedValuePod(new TIterator(this));
- }
- NUdf::TUnboxedValue GetKeysIterator() const override {
- return NUdf::TUnboxedValuePod(new TDictIterator(GetMemInfo(), GetListIterator()));
- }
- NUdf::TUnboxedValue GetDictIterator() const override {
- return NUdf::TUnboxedValuePod(new TDictIterator(GetMemInfo(), GetListIterator()));
- }
- NUdf::TUnboxedValue GetPayloadsIterator() const override {
- return NUdf::TUnboxedValuePod(new TIterator(this));
- }
- bool HasFastListLength() const override {
- return true;
- }
- ui64 GetListLength() const override {
- return Items.GetLength();
- }
- ui64 GetEstimatedListLength() const override {
- return Items.GetLength();
- }
- bool HasListItems() const override {
- return Items.GetLength() != 0;
- }
- const NUdf::TOpaqueListRepresentation* GetListRepresentation() const override {
- return reinterpret_cast<const NUdf::TOpaqueListRepresentation*>(&Items);
- }
- NUdf::IBoxedValuePtr ReverseListImpl(const NUdf::IValueBuilder& builder) const override {
- switch (Items.GetLength()) {
- case 0U: return builder.NewEmptyList().Release().AsBoxed();
- case 1U: return const_cast<TDirectListHolder*>(this);
- default: break;
- }
- TDefaultListRepresentation result;
- for (auto it = Items.GetReverseIterator(); !it.AtEnd(); it.Next()) {
- result = result.Append(NUdf::TUnboxedValue(it.Current()));
- }
- return new TDirectListHolder(GetMemInfo(), std::move(result));
- }
- NUdf::IBoxedValuePtr SkipListImpl(const NUdf::IValueBuilder& builder, ui64 count) const override {
- if (count == 0)
- return const_cast<TDirectListHolder*>(this);
- if (count >= Items.GetLength())
- return builder.NewEmptyList().Release().AsBoxed();
- auto result = Items.SkipFromBegin(static_cast<size_t>(count));
- return new TDirectListHolder(GetMemInfo(), std::move(result));
- }
- NUdf::IBoxedValuePtr TakeListImpl(const NUdf::IValueBuilder& builder, ui64 count) const override {
- if (count == 0)
- return builder.NewEmptyList().Release().AsBoxed();
- if (count >= Items.GetLength())
- return const_cast<TDirectListHolder*>(this);
- auto result = Items.SkipFromEnd(static_cast<size_t>(Items.GetLength() - count));
- return new TDirectListHolder(GetMemInfo(), std::move(result));
- }
- NUdf::IBoxedValuePtr ToIndexDictImpl(const NUdf::IValueBuilder& builder) const override {
- Y_UNUSED(builder);
- return const_cast<TDirectListHolder*>(this);
- }
- ui64 GetDictLength() const override {
- return GetListLength();
- }
- bool HasDictItems() const override {
- return Items.GetLength() != 0;
- }
- NUdf::TUnboxedValue GetElement(ui32 index) const final {
- return Items.GetItemByIndex(index);
- }
- const NUdf::TUnboxedValue* GetElements() const final {
- return Items.GetItems();
- }
- bool IsSortedDict() const override {
- return true;
- }
- TDefaultListRepresentation Items;
- };
- template <class TBaseVector>
- class TVectorHolderBase: public TComputationValue<TVectorHolderBase<TBaseVector>>, public TBaseVector {
- private:
- using TBaseValue = TComputationValue<TVectorHolderBase<TBaseVector>>;
- public:
- TVectorHolderBase(TMemoryUsageInfo* memInfo)
- : TBaseValue(memInfo)
- {
- }
- TVectorHolderBase(TMemoryUsageInfo* memInfo, TBaseVector&& vector)
- : TBaseValue(memInfo)
- , TBaseVector(std::move(vector)) {
- }
- ~TVectorHolderBase() {
- }
- private:
- class TValuesIterator: public TTemporaryComputationValue<TValuesIterator> {
- private:
- using TBase = TTemporaryComputationValue<TValuesIterator>;
- public:
- TValuesIterator(const TVectorHolderBase* parent)
- : TBase(parent->GetMemInfo())
- , Size(parent->size())
- , Parent(const_cast<TVectorHolderBase*>(parent)) {
- }
- private:
- bool Skip() final {
- return ++Current < Size;
- }
- bool Next(NUdf::TUnboxedValue& value) final {
- if (Size <= Current) {
- return false;
- }
- value = (*Parent)[Current];
- ++Current;
- return true;
- }
- const size_t Size;
- ui64 Current = 0;
- const NUdf::TRefCountedPtr<TVectorHolderBase> Parent;
- };
- class TDictIterator: public TTemporaryComputationValue<TDictIterator> {
- private:
- using TBase = TTemporaryComputationValue<TDictIterator>;
- public:
- TDictIterator(const TVectorHolderBase* parent)
- : TBase(parent->GetMemInfo())
- , Size(parent->size())
- , Parent(const_cast<TVectorHolderBase*>(parent)) {
- }
- private:
- bool Skip() final {
- return ++Current < Size;
- }
- bool Next(NUdf::TUnboxedValue& key) final {
- if (Current == Size) {
- return false;
- }
- key = NUdf::TUnboxedValuePod(Current);
- ++Current;
- return true;
- }
- bool NextPair(NUdf::TUnboxedValue& key, NUdf::TUnboxedValue& payload) final {
- if (Current == Size) {
- return false;
- }
- key = NUdf::TUnboxedValuePod(Current);
- payload = (*Parent)[Current];
- ++Current;
- return true;
- }
- const size_t Size;
- ui64 Current = 0;
- const NUdf::TRefCountedPtr<TVectorHolderBase> Parent;
- };
- bool HasListItems() const final {
- return TBaseVector::size();
- }
- bool HasDictItems() const final {
- return TBaseVector::size();
- }
- bool HasFastListLength() const final {
- return true;
- }
- ui64 GetListLength() const final {
- return TBaseVector::size();
- }
- ui64 GetDictLength() const final {
- return TBaseVector::size();
- }
- ui64 GetEstimatedListLength() const final {
- return TBaseVector::size();
- }
- NUdf::TUnboxedValue GetListIterator() const final {
- return NUdf::TUnboxedValuePod(new TValuesIterator(this));
- }
- NUdf::TUnboxedValue GetDictIterator() const final {
- return NUdf::TUnboxedValuePod(new TDictIterator(this));
- }
- NUdf::TUnboxedValue GetPayloadsIterator() const final {
- return NUdf::TUnboxedValuePod(new TValuesIterator(this));
- }
- NUdf::TUnboxedValue GetKeysIterator() const final {
- return NUdf::TUnboxedValuePod(new TDictIterator(this));
- }
- NUdf::IBoxedValuePtr ReverseListImpl(const NUdf::IValueBuilder&) const final {
- if (1U >= TBaseVector::size()) {
- return const_cast<TVectorHolderBase*>(this);
- }
- TBaseVector copy(TBaseVector::rbegin(), TBaseVector::rend());
- return new TVectorHolderBase(TBaseValue::GetMemInfo(), std::move(copy));
- }
- void Push(const NUdf::TUnboxedValuePod& value) final {
- TBaseVector::emplace_back(value);
- }
- NUdf::IBoxedValuePtr SkipListImpl(const NUdf::IValueBuilder& builder, ui64 count) const final {
- if (!count)
- return const_cast<TVectorHolderBase*>(this);
- if (count >= TBaseVector::size())
- return builder.NewEmptyList().Release().AsBoxed();
- TBaseVector copy(TBaseVector::begin() + count, TBaseVector::end());
- return new TVectorHolderBase(TBaseValue::GetMemInfo(), std::move(copy));
- }
- NUdf::IBoxedValuePtr TakeListImpl(const NUdf::IValueBuilder& builder, ui64 count) const final {
- if (!count)
- return builder.NewEmptyList().Release().AsBoxed();
- if (count >= TBaseVector::size())
- return const_cast<TVectorHolderBase*>(this);
- TBaseVector copy(TBaseVector::begin(), TBaseVector::begin() + count);
- return new TVectorHolderBase(TBaseValue::GetMemInfo(), std::move(copy));
- }
- NUdf::IBoxedValuePtr ToIndexDictImpl(const NUdf::IValueBuilder&) const final {
- return const_cast<TVectorHolderBase*>(this);
- }
- bool Contains(const NUdf::TUnboxedValuePod& key) const final {
- return key.Get<ui64>() < TBaseVector::size();
- }
- NUdf::TUnboxedValue Lookup(const NUdf::TUnboxedValuePod& key) const final {
- const auto index = key.Get<ui64>();
- return index < TBaseVector::size() ? TBaseVector::at(index).MakeOptional() : NUdf::TUnboxedValuePod();
- }
- const NUdf::TUnboxedValue* GetElements() const final {
- return TBaseVector::data();
- }
- bool IsSortedDict() const override {
- return true;
- }
- };
- class TVectorHolder: public TVectorHolderBase<TUnboxedValueVector> {
- private:
- using TBase = TVectorHolderBase<TUnboxedValueVector>;
- public:
- using TBase::TBase;
- };
- class TTemporaryVectorHolder: public TVectorHolderBase<TTemporaryUnboxedValueVector> {
- private:
- using TBase = TVectorHolderBase<TTemporaryUnboxedValueVector>;
- public:
- using TBase::TBase;
- };
- class TEmptyContainerHolder: public TComputationValue<TEmptyContainerHolder> {
- public:
- TEmptyContainerHolder(TMemoryUsageInfo* memInfo)
- : TComputationValue(memInfo), None()
- {}
- private:
- bool Contains(const NUdf::TUnboxedValuePod&) const override {
- return false;
- }
- NUdf::TUnboxedValue Lookup(const NUdf::TUnboxedValuePod&) const override {
- return None;
- }
- NUdf::EFetchStatus Fetch(NUdf::TUnboxedValue&) override {
- return NUdf::EFetchStatus::Finish;
- }
- NUdf::TUnboxedValue GetListIterator() const override {
- return NUdf::TUnboxedValuePod(const_cast<TEmptyContainerHolder*>(this));
- }
- NUdf::TUnboxedValue GetDictIterator() const override {
- return NUdf::TUnboxedValuePod(const_cast<TEmptyContainerHolder*>(this));
- }
- NUdf::TUnboxedValue GetKeysIterator() const override {
- return NUdf::TUnboxedValuePod(const_cast<TEmptyContainerHolder*>(this));
- }
- NUdf::TUnboxedValue GetPayloadsIterator() const override {
- return NUdf::TUnboxedValuePod(const_cast<TEmptyContainerHolder*>(this));
- }
- bool Skip() final {
- return false;
- }
- bool Next(NUdf::TUnboxedValue&) final {
- return false;
- }
- bool NextPair(NUdf::TUnboxedValue&, NUdf::TUnboxedValue&) final {
- return false;
- }
- const NUdf::TOpaqueListRepresentation* GetListRepresentation() const override {
- return reinterpret_cast<const NUdf::TOpaqueListRepresentation*>(&List);
- }
- bool HasFastListLength() const override {
- return true;
- }
- ui64 GetListLength() const override {
- return 0;
- }
- ui64 GetEstimatedListLength() const override {
- return 0;
- }
- bool HasListItems() const override {
- return false;
- }
- NUdf::IBoxedValuePtr ReverseListImpl(const NUdf::IValueBuilder& builder) const override {
- Y_UNUSED(builder);
- return const_cast<TEmptyContainerHolder*>(this);
- }
- NUdf::IBoxedValuePtr SkipListImpl(const NUdf::IValueBuilder& builder, ui64 count) const override {
- Y_UNUSED(builder);
- Y_UNUSED(count);
- return const_cast<TEmptyContainerHolder*>(this);
- }
- NUdf::IBoxedValuePtr TakeListImpl(const NUdf::IValueBuilder& builder, ui64 count) const override {
- Y_UNUSED(builder);
- Y_UNUSED(count);
- return const_cast<TEmptyContainerHolder*>(this);
- }
- NUdf::IBoxedValuePtr ToIndexDictImpl(const NUdf::IValueBuilder& builder) const override {
- Y_UNUSED(builder);
- return const_cast<TEmptyContainerHolder*>(this);
- }
- ui64 GetDictLength() const override {
- return 0;
- }
- bool HasDictItems() const override {
- return false;
- }
- bool IsSortedDict() const override {
- return true;
- }
- const NUdf::TUnboxedValue* GetElements() const override {
- return &None;
- }
- const NUdf::TUnboxedValue None;
- const TDefaultListRepresentation List;
- };
- class TSortedSetHolder: public TComputationValue<TSortedSetHolder> {
- public:
- typedef TUnboxedValueVector TItems;
- template <bool NoSwap>
- class TIterator: public TComputationValue<TIterator<NoSwap>> {
- public:
- TIterator(const TSortedSetHolder* parent)
- : TComputationValue<TIterator<NoSwap>>(parent->GetMemInfo())
- , Parent(const_cast<TSortedSetHolder*>(parent))
- , Iterator(Parent->Items.begin())
- , AtStart(true)
- {
- }
- private:
- bool Skip() override {
- if (AtStart) {
- AtStart = false;
- } else {
- if (Iterator == Parent->Items.end())
- return false;
- ++Iterator;
- }
- return Iterator != Parent->Items.end();
- }
- bool Next(NUdf::TUnboxedValue& key) override {
- if (!Skip())
- return false;
- if (NoSwap) {
- key = *Iterator;
- if (Parent->Packer) {
- key = Parent->Packer->Decode(key.AsStringRef(), false, Parent->HolderFactory);
- }
- } else {
- key = NUdf::TUnboxedValuePod::Void();
- }
- return true;
- }
- bool NextPair(NUdf::TUnboxedValue& key, NUdf::TUnboxedValue& payload) override {
- if (!Next(key))
- return false;
- if (NoSwap) {
- payload = NUdf::TUnboxedValuePod::Void();
- } else {
- payload = *Iterator;
- if (Parent->Packer) {
- payload = Parent->Packer->Decode(payload.AsStringRef(), false, Parent->HolderFactory);
- }
- }
- return true;
- }
- const NUdf::TRefCountedPtr<TSortedSetHolder> Parent;
- TItems::const_iterator Iterator;
- bool AtStart;
- };
- TSortedSetHolder(
- TMemoryUsageInfo* memInfo,
- TSortedSetFiller filler,
- const TKeyTypes& types,
- bool isTuple,
- EDictSortMode mode,
- bool eagerFill,
- TType* encodedType,
- const NUdf::ICompare* compare,
- const NUdf::IEquate* equate,
- const THolderFactory& holderFactory)
- : TComputationValue(memInfo)
- , Filler(filler)
- , Types(types)
- , IsTuple(isTuple)
- , Mode(mode)
- , Compare(compare)
- , Equate(equate)
- , IsBuilt(false)
- , HolderFactory(holderFactory)
- {
- if (encodedType) {
- Packer.emplace(encodedType);
- }
- if (eagerFill)
- LazyBuildDict();
- }
- ~TSortedSetHolder() {
- MKQL_MEM_RETURN(GetMemInfo(), &Items, Items.capacity() * sizeof(TItems::value_type));
- }
- private:
- bool Contains(const NUdf::TUnboxedValuePod& key) const override {
- LazyBuildDict();
- NUdf::TUnboxedValue encodedKey;
- if (Packer) {
- encodedKey = MakeString(Packer->Encode(key, false));
- }
- return BinarySearch(Items.begin(), Items.end(), NUdf::TUnboxedValuePod(Packer ? encodedKey : key),
- TValueLess(Types, IsTuple, Compare));
- }
- NUdf::TUnboxedValue Lookup(const NUdf::TUnboxedValuePod& key) const override {
- LazyBuildDict();
- NUdf::TUnboxedValue encodedKey;
- if (Packer) {
- encodedKey = MakeString(Packer->Encode(key, false));
- }
- const auto it = LowerBound(Items.begin(), Items.end(), NUdf::TUnboxedValuePod(Packer ? encodedKey : key), TValueLess(Types, IsTuple, Compare));
- if (it == Items.end() || !TValueEqual(Types, IsTuple, Equate)(*it, NUdf::TUnboxedValuePod(Packer ? encodedKey : key)))
- return NUdf::TUnboxedValuePod();
- return it->MakeOptional();
- }
- NUdf::TUnboxedValue GetKeysIterator() const override {
- LazyBuildDict();
- return NUdf::TUnboxedValuePod(new TIterator<true>(this));
- }
- NUdf::TUnboxedValue GetDictIterator() const override {
- LazyBuildDict();
- return NUdf::TUnboxedValuePod(new TIterator<true>(this));
- }
- NUdf::TUnboxedValue GetPayloadsIterator() const override {
- LazyBuildDict();
- return NUdf::TUnboxedValuePod(new TIterator<false>(this));
- }
- ui64 GetDictLength() const override {
- LazyBuildDict();
- return Items.size();
- }
- bool HasDictItems() const override {
- LazyBuildDict();
- return !Items.empty();
- }
- void LazyBuildDict() const {
- if (IsBuilt)
- return;
- Filler(Items);
- Filler = TSortedSetFiller();
- switch (Mode) {
- case EDictSortMode::RequiresSorting:
- StableSort(Items.begin(), Items.end(), TValueLess(Types, IsTuple, Compare));
- Items.erase(Unique(Items.begin(), Items.end(), TValueEqual(Types, IsTuple, Equate)), Items.end());
- break;
- case EDictSortMode::SortedUniqueAscending:
- break;
- case EDictSortMode::SortedUniqueDescening:
- Reverse(Items.begin(), Items.end());
- break;
- default:
- Y_ABORT();
- }
- Y_DEBUG_ABORT_UNLESS(IsSortedUnique());
- IsBuilt = true;
- if (!Items.empty()) {
- MKQL_MEM_TAKE(GetMemInfo(), &Items, Items.capacity() * sizeof(TItems::value_type));
- }
- }
- bool IsSortedUnique() const {
- TValueLess less(Types, IsTuple, Compare);
- for (size_t i = 1, e = Items.size(); i < e; ++i) {
- if (!less(Items[i - 1], Items[i]))
- return false;
- }
- return true;
- }
- bool IsSortedDict() const override {
- return true;
- }
- private:
- mutable TSortedSetFiller Filler;
- const TKeyTypes Types;
- const bool IsTuple;
- const EDictSortMode Mode;
- const NUdf::ICompare* Compare;
- const NUdf::IEquate* Equate;
- mutable bool IsBuilt;
- const THolderFactory& HolderFactory;
- mutable TItems Items;
- mutable std::optional<TGenericPresortEncoder> Packer;
- };
- class TSortedDictHolder: public TComputationValue<TSortedDictHolder> {
- public:
- typedef TKeyPayloadPairVector TItems;
- template <bool NoSwap>
- class TIterator: public TComputationValue<TIterator<NoSwap>> {
- public:
- TIterator(const TSortedDictHolder* parent)
- : TComputationValue<TIterator<NoSwap>>(parent->GetMemInfo())
- , Parent(const_cast<TSortedDictHolder*>(parent))
- , Iterator(Parent->Items.begin())
- , AtStart(true)
- {
- }
- private:
- bool Skip() override {
- if (AtStart) {
- AtStart = false;
- } else {
- if (Iterator == Parent->Items.end())
- return false;
- ++Iterator;
- }
- return Iterator != Parent->Items.end();
- }
- bool Next(NUdf::TUnboxedValue& key) override {
- if (!Skip())
- return false;
- if (NoSwap) {
- key = Iterator->first;
- if (Parent->Packer) {
- key = Parent->Packer->Decode(key.AsStringRef(), false, Parent->HolderFactory);
- }
- } else {
- key = Iterator->second;
- }
- return true;
- }
- bool NextPair(NUdf::TUnboxedValue& key, NUdf::TUnboxedValue& payload) override {
- if (!Next(key))
- return false;
- if (NoSwap) {
- payload = Iterator->second;
- } else {
- payload = Iterator->first;
- if (Parent->Packer) {
- payload = Parent->Packer->Decode(payload.AsStringRef(), false, Parent->HolderFactory);
- }
- }
- return true;
- }
- const NUdf::TRefCountedPtr<TSortedDictHolder> Parent;
- TItems::const_iterator Iterator;
- bool AtStart;
- };
- TSortedDictHolder(
- TMemoryUsageInfo* memInfo,
- TSortedDictFiller filler,
- const TKeyTypes& types,
- bool isTuple,
- EDictSortMode mode,
- bool eagerFill,
- TType* encodedType,
- const NUdf::ICompare* compare,
- const NUdf::IEquate* equate,
- const THolderFactory& holderFactory)
- : TComputationValue(memInfo)
- , Filler(filler)
- , Types(types)
- , IsTuple(isTuple)
- , Mode(mode)
- , Compare(compare)
- , Equate(equate)
- , IsBuilt(false)
- , HolderFactory(holderFactory)
- {
- if (encodedType) {
- Packer.emplace(encodedType);
- }
- if (eagerFill)
- LazyBuildDict();
- }
- ~TSortedDictHolder() {
- MKQL_MEM_RETURN(GetMemInfo(), &Items, Items.capacity() * sizeof(TItems::value_type));
- }
- private:
- bool Contains(const NUdf::TUnboxedValuePod& key) const override {
- LazyBuildDict();
- NUdf::TUnboxedValue encodedKey;
- if (Packer) {
- encodedKey = MakeString(Packer->Encode(key, false));
- }
- return BinarySearch(Items.begin(), Items.end(), TItems::value_type(NUdf::TUnboxedValuePod(Packer ? encodedKey : key), NUdf::TUnboxedValuePod()),
- TKeyPayloadPairLess(Types, IsTuple, Compare));
- }
- NUdf::TUnboxedValue Lookup(const NUdf::TUnboxedValuePod& key) const override {
- LazyBuildDict();
- NUdf::TUnboxedValue encodedKey;
- if (Packer) {
- encodedKey = MakeString(Packer->Encode(key, false));
- }
- const auto it = LowerBound(Items.begin(), Items.end(), TItems::value_type(NUdf::TUnboxedValuePod(Packer ? encodedKey : key), NUdf::TUnboxedValue()), TKeyPayloadPairLess(Types, IsTuple, Compare));
- if (it == Items.end() || !TKeyPayloadPairEqual(Types, IsTuple, Equate)({it->first, it->second}, TKeyPayloadPair(NUdf::TUnboxedValuePod(Packer ? encodedKey : key), {})))
- return NUdf::TUnboxedValuePod();
- return it->second.MakeOptional();
- }
- NUdf::TUnboxedValue GetKeysIterator() const override {
- LazyBuildDict();
- return NUdf::TUnboxedValuePod(new TIterator<true>(this));
- }
- NUdf::TUnboxedValue GetDictIterator() const override {
- LazyBuildDict();
- return NUdf::TUnboxedValuePod(new TIterator<true>(this));
- }
- NUdf::TUnboxedValue GetPayloadsIterator() const override {
- LazyBuildDict();
- return NUdf::TUnboxedValuePod(new TIterator<false>(this));
- }
- ui64 GetDictLength() const override {
- LazyBuildDict();
- return Items.size();
- }
- bool HasDictItems() const override {
- LazyBuildDict();
- return !Items.empty();
- }
- void LazyBuildDict() const {
- if (IsBuilt)
- return;
- Filler(Items);
- Filler = TSortedDictFiller();
- switch (Mode) {
- case EDictSortMode::RequiresSorting:
- StableSort(Items.begin(), Items.end(), TKeyPayloadPairLess(Types, IsTuple, Compare));
- Items.erase(Unique(Items.begin(), Items.end(), TKeyPayloadPairEqual(Types, IsTuple, Equate)), Items.end());
- break;
- case EDictSortMode::SortedUniqueAscending:
- break;
- case EDictSortMode::SortedUniqueDescening:
- Reverse(Items.begin(), Items.end());
- break;
- default:
- Y_ABORT();
- }
- Y_DEBUG_ABORT_UNLESS(IsSortedUnique());
- IsBuilt = true;
- if (!Items.empty()) {
- MKQL_MEM_TAKE(GetMemInfo(), &Items, Items.capacity() * sizeof(TItems::value_type));
- }
- }
- bool IsSortedUnique() const {
- TKeyPayloadPairLess less(Types, IsTuple, Compare);
- for (size_t i = 1, e = Items.size(); i < e; ++i) {
- if (!less(Items[i - 1], Items[i]))
- return false;
- }
- return true;
- }
- bool IsSortedDict() const override {
- return true;
- }
- private:
- mutable TSortedDictFiller Filler;
- const TKeyTypes Types;
- const bool IsTuple;
- const EDictSortMode Mode;
- const NUdf::ICompare* Compare;
- const NUdf::IEquate* Equate;
- mutable bool IsBuilt;
- const THolderFactory& HolderFactory;
- mutable TItems Items;
- mutable std::optional<TGenericPresortEncoder> Packer;
- };
- class THashedSetHolder : public TComputationValue<THashedSetHolder> {
- public:
- class TIterator : public TComputationValue<TIterator> {
- public:
- TIterator(const THashedSetHolder* parent)
- : TComputationValue(parent->GetMemInfo())
- , Parent(const_cast<THashedSetHolder*>(parent))
- , Iterator(Parent->Set.begin())
- , End(Parent->Set.end())
- , AtStart(true)
- {
- }
- private:
- bool Skip() override {
- if (AtStart) {
- AtStart = false;
- }
- else {
- if (Iterator == End) {
- return false;
- }
- ++Iterator;
- }
- return Iterator != End;
- }
- bool Next(NUdf::TUnboxedValue& key) override {
- if (!Skip())
- return false;
- key = *Iterator;
- if (Parent->Packer) {
- key = Parent->Packer->Unpack(key.AsStringRef(), Parent->HolderFactory);
- }
- return true;
- }
- bool NextPair(NUdf::TUnboxedValue& key, NUdf::TUnboxedValue& payload) override {
- if (!Next(key))
- return false;
- payload = NUdf::TUnboxedValuePod::Void();
- return true;
- }
- private:
- const NUdf::TRefCountedPtr<THashedSetHolder> Parent;
- TValuesDictHashSet::const_iterator Iterator;
- TValuesDictHashSet::const_iterator End;
- bool AtStart;
- };
- THashedSetHolder(TMemoryUsageInfo* memInfo, THashedSetFiller filler,
- const TKeyTypes& types, bool isTuple, bool eagerFill, TType* encodedType,
- const NUdf::IHash* hash, const NUdf::IEquate* equate, const THolderFactory& holderFactory)
- : TComputationValue(memInfo)
- , Filler(filler)
- , Types(types)
- , Set(0, TValueHasher(Types, isTuple, hash), TValueEqual(Types, isTuple, equate))
- , IsBuilt(false)
- , HolderFactory(holderFactory)
- {
- if (encodedType) {
- Packer.emplace(true, encodedType);
- }
- if (eagerFill)
- LazyBuildDict();
- }
- private:
- bool Contains(const NUdf::TUnboxedValuePod& key) const override {
- LazyBuildDict();
- NUdf::TUnboxedValue encodedKey;
- if (Packer) {
- encodedKey = MakeString(Packer->Pack(key));
- }
- return Set.find(NUdf::TUnboxedValuePod(Packer ? encodedKey : key)) != Set.cend();
- }
- NUdf::TUnboxedValue Lookup(const NUdf::TUnboxedValuePod& key) const override {
- LazyBuildDict();
- NUdf::TUnboxedValue encodedKey;
- if (Packer) {
- encodedKey = MakeString(Packer->Pack(key));
- }
- const auto it = Set.find(NUdf::TUnboxedValuePod(Packer ? encodedKey : key));
- if (it == Set.cend())
- return NUdf::TUnboxedValuePod();
- return NUdf::TUnboxedValuePod::Void();
- }
- NUdf::TUnboxedValue GetKeysIterator() const override {
- LazyBuildDict();
- return NUdf::TUnboxedValuePod(new TIterator(this));
- }
- NUdf::TUnboxedValue GetDictIterator() const override {
- LazyBuildDict();
- return NUdf::TUnboxedValuePod(new TIterator(this));
- }
- NUdf::TUnboxedValue GetPayloadsIterator() const override {
- LazyBuildDict();
- return NUdf::TUnboxedValuePod(new TIterator(this));
- }
- NUdf::TUnboxedValue GetListIterator() const override {
- LazyBuildDict();
- return NUdf::TUnboxedValuePod(new TIterator(this));
- }
- ui64 GetDictLength() const override {
- LazyBuildDict();
- return Set.size();
- }
- bool HasDictItems() const override {
- LazyBuildDict();
- return !Set.empty();
- }
- bool IsSortedDict() const override {
- return false;
- }
- private:
- void LazyBuildDict() const {
- if (IsBuilt)
- return;
- Filler(Set);
- Filler = THashedSetFiller();
- IsBuilt = true;
- }
- private:
- mutable THashedSetFiller Filler;
- const TKeyTypes Types;
- mutable TValuesDictHashSet Set;
- mutable bool IsBuilt;
- const THolderFactory& HolderFactory;
- mutable std::optional<TValuePacker> Packer;
- };
- template <typename T, bool OptionalKey>
- class THashedSingleFixedSetHolder : public TComputationValue<THashedSingleFixedSetHolder<T, OptionalKey>> {
- public:
- using TSetType = TValuesDictHashSingleFixedSet<T>;
- class TIterator : public TComputationValue<TIterator> {
- public:
- enum class EState {
- AtStart,
- AtNull,
- Iterator
- };
- TIterator(const THashedSingleFixedSetHolder* parent)
- : TComputationValue<TIterator>(parent->GetMemInfo())
- , Parent(const_cast<THashedSingleFixedSetHolder*>(parent))
- , Iterator(Parent->Set.begin())
- , End(Parent->Set.end())
- , State(EState::AtStart)
- {
- }
- private:
- bool Skip() final {
- switch (State) {
- case EState::AtStart:
- State = OptionalKey && Parent->HasNull ? EState::AtNull : EState::Iterator;
- break;
- case EState::AtNull:
- State = EState::Iterator;
- break;
- case EState::Iterator:
- if (Iterator == End)
- return false;
- ++Iterator;
- break;
- }
- return EState::AtNull == State || Iterator != End;
- }
- bool Next(NUdf::TUnboxedValue& key) final {
- if (!Skip())
- return false;
- key = EState::AtNull == State ? NUdf::TUnboxedValuePod() : NUdf::TUnboxedValuePod(*Iterator);
- return true;
- }
- bool NextPair(NUdf::TUnboxedValue& key, NUdf::TUnboxedValue& payload) final {
- if (!Next(key))
- return false;
- payload = NUdf::TUnboxedValuePod::Void();
- return true;
- }
- const NUdf::TRefCountedPtr<THashedSingleFixedSetHolder> Parent;
- typename TSetType::const_iterator Iterator;
- typename TSetType::const_iterator End;
- EState State;
- };
- THashedSingleFixedSetHolder(TMemoryUsageInfo* memInfo, TSetType&& set, bool hasNull)
- : TComputationValue<THashedSingleFixedSetHolder>(memInfo)
- , Set(std::move(set))
- , HasNull(hasNull)
- {
- MKQL_ENSURE(OptionalKey || !HasNull, "Null value is not allowed for non-optional key type");
- }
- private:
- bool Contains(const NUdf::TUnboxedValuePod& key) const final {
- if constexpr (OptionalKey) {
- if (!key) {
- return HasNull;
- }
- }
- return Set.find(key.Get<T>()) != Set.cend();
- }
- NUdf::TUnboxedValue Lookup(const NUdf::TUnboxedValuePod& key) const final {
- if (Contains(key))
- return NUdf::TUnboxedValuePod::Void();
- return NUdf::TUnboxedValuePod();
- }
- NUdf::TUnboxedValue GetKeysIterator() const final {
- return NUdf::TUnboxedValuePod(new TIterator(this));
- }
- NUdf::TUnboxedValue GetDictIterator() const final {
- return NUdf::TUnboxedValuePod(new TIterator(this));
- }
- NUdf::TUnboxedValue GetPayloadsIterator() const final {
- return NUdf::TUnboxedValuePod(new TIterator(this));
- }
- NUdf::TUnboxedValue GetListIterator() const final {
- return NUdf::TUnboxedValuePod(new TIterator(this));
- }
- ui64 GetDictLength() const final {
- return Set.size() + ui64(OptionalKey && HasNull);
- }
- bool HasDictItems() const final {
- return !Set.empty() || (OptionalKey && HasNull);
- }
- bool IsSortedDict() const final {
- return false;
- }
- const TSetType Set;
- const bool HasNull;
- };
- template <typename T, bool OptionalKey>
- class THashedSingleFixedCompactSetHolder : public TComputationValue<THashedSingleFixedCompactSetHolder<T, OptionalKey>> {
- public:
- using TSetType = TValuesDictHashSingleFixedCompactSet<T>;
- class TIterator : public TComputationValue<TIterator> {
- public:
- enum class EState {
- AtStart,
- AtNull,
- Iterator
- };
- TIterator(const THashedSingleFixedCompactSetHolder* parent)
- : TComputationValue<TIterator>(parent->GetMemInfo())
- , Parent(const_cast<THashedSingleFixedCompactSetHolder*>(parent))
- , Iterator(Parent->Set.Iterate())
- , State(EState::AtStart)
- {
- }
- private:
- bool Skip() final {
- switch (State) {
- case EState::AtStart:
- State = OptionalKey && Parent->HasNull ? EState::AtNull : EState::Iterator;
- break;
- case EState::AtNull:
- State = EState::Iterator;
- break;
- case EState::Iterator:
- if (!Iterator.Ok())
- return false;
- ++Iterator;
- break;
- }
- return EState::AtNull == State || Iterator.Ok();
- }
- bool Next(NUdf::TUnboxedValue& key) final {
- if (!Skip())
- return false;
- key = EState::AtNull == State ? NUdf::TUnboxedValuePod() : NUdf::TUnboxedValuePod(*Iterator);
- return true;
- }
- bool NextPair(NUdf::TUnboxedValue& key, NUdf::TUnboxedValue& payload) final {
- if (!Next(key))
- return false;
- payload = NUdf::TUnboxedValuePod::Void();
- return true;
- }
- const NUdf::TRefCountedPtr<THashedSingleFixedCompactSetHolder> Parent;
- typename TSetType::TIterator Iterator;
- EState State;
- };
- THashedSingleFixedCompactSetHolder(TMemoryUsageInfo* memInfo, TSetType&& set, bool hasNull)
- : TComputationValue<THashedSingleFixedCompactSetHolder>(memInfo)
- , Set(std::move(set))
- , HasNull(hasNull)
- {
- MKQL_ENSURE(OptionalKey || !HasNull, "Null value is not allowed for non-optional key type");
- }
- private:
- bool Contains(const NUdf::TUnboxedValuePod& key) const final {
- if constexpr (OptionalKey) {
- if (!key) {
- return HasNull;
- }
- }
- return Set.Has(key.Get<T>());
- }
- NUdf::TUnboxedValue Lookup(const NUdf::TUnboxedValuePod& key) const final {
- if (Contains(key))
- return NUdf::TUnboxedValuePod::Void();
- return NUdf::TUnboxedValuePod();
- }
- NUdf::TUnboxedValue GetKeysIterator() const final {
- return NUdf::TUnboxedValuePod(new TIterator(this));
- }
- NUdf::TUnboxedValue GetDictIterator() const final {
- return NUdf::TUnboxedValuePod(new TIterator(this));
- }
- NUdf::TUnboxedValue GetPayloadsIterator() const final {
- return NUdf::TUnboxedValuePod(new TIterator(this));
- }
- NUdf::TUnboxedValue GetListIterator() const final {
- return NUdf::TUnboxedValuePod(new TIterator(this));
- }
- ui64 GetDictLength() const final {
- return Set.Size() + ui64(OptionalKey && HasNull);
- }
- bool HasDictItems() const final {
- return !Set.Empty() || (OptionalKey && HasNull);
- }
- bool IsSortedDict() const final {
- return false;
- }
- const TSetType Set;
- const bool HasNull;
- };
- class THashedCompactSetHolder : public TComputationValue<THashedCompactSetHolder> {
- public:
- using TSetType = TValuesDictHashCompactSet;
- class TIterator : public TComputationValue<TIterator> {
- public:
- TIterator(const THashedCompactSetHolder* parent)
- : TComputationValue(parent->GetMemInfo())
- , Parent(const_cast<THashedCompactSetHolder*>(parent))
- , Iterator(Parent->Set.Iterate())
- , AtStart(true)
- {
- }
- private:
- bool Skip() override {
- if (AtStart) {
- AtStart = false;
- }
- else {
- if (!Iterator.Ok())
- return false;
- ++Iterator;
- }
- return Iterator.Ok();
- }
- bool Next(NUdf::TUnboxedValue& key) override {
- if (!Skip())
- return false;
- key = Parent->KeyPacker.Unpack(GetSmallValue(*Iterator), Parent->Ctx->HolderFactory);
- return true;
- }
- bool NextPair(NUdf::TUnboxedValue& key, NUdf::TUnboxedValue& payload) override {
- if (!Next(key))
- return false;
- payload = NUdf::TUnboxedValuePod::Void();
- return true;
- }
- const NUdf::TRefCountedPtr<THashedCompactSetHolder> Parent;
- typename TSetType::TIterator Iterator;
- bool AtStart;
- };
- THashedCompactSetHolder(TMemoryUsageInfo* memInfo, TSetType&& set, TPagedArena&& pool, TType* keyType, TComputationContext* ctx)
- : TComputationValue(memInfo)
- , Pool(std::move(pool))
- , Set(std::move(set))
- , KeyPacker(true, keyType)
- , Ctx(ctx)
- {
- }
- private:
- bool Contains(const NUdf::TUnboxedValuePod& key) const override {
- auto serializedKey = KeyPacker.Pack(NUdf::TUnboxedValuePod(key));
- ui64 smallValue = AsSmallValue(serializedKey);
- return Set.Has(smallValue);
- }
- NUdf::TUnboxedValue Lookup(const NUdf::TUnboxedValuePod& key) const override {
- auto serializedKey = KeyPacker.Pack(NUdf::TUnboxedValuePod(key));
- ui64 smallValue = AsSmallValue(serializedKey);
- if (Set.Has(smallValue))
- return NUdf::TUnboxedValuePod::Void();
- return NUdf::TUnboxedValuePod();
- }
- NUdf::TUnboxedValue GetKeysIterator() const override {
- return NUdf::TUnboxedValuePod(new TIterator(this));
- }
- NUdf::TUnboxedValue GetDictIterator() const override {
- return NUdf::TUnboxedValuePod(new TIterator(this));
- }
- NUdf::TUnboxedValue GetPayloadsIterator() const override {
- return NUdf::TUnboxedValuePod(new TIterator(this));
- }
- NUdf::TUnboxedValue GetListIterator() const override {
- return NUdf::TUnboxedValuePod(new TIterator(this));
- }
- ui64 GetDictLength() const override {
- return Set.Size();
- }
- bool HasDictItems() const override {
- return !Set.Empty();
- }
- bool IsSortedDict() const override {
- return false;
- }
- private:
- TPagedArena Pool;
- const TSetType Set;
- mutable TValuePacker KeyPacker;
- TComputationContext* Ctx;
- };
- class THashedCompactMapHolder : public TComputationValue<THashedCompactMapHolder> {
- public:
- using TMapType = TValuesDictHashCompactMap;
- template <bool NoSwap>
- class TIterator : public TComputationValue<TIterator<NoSwap>> {
- public:
- TIterator(const THashedCompactMapHolder* parent)
- : TComputationValue<TIterator<NoSwap>>(parent->GetMemInfo())
- , Parent(const_cast<THashedCompactMapHolder*>(parent))
- , Iterator(Parent->Map.Iterate())
- , AtStart(true)
- {
- }
- private:
- bool Skip() override {
- if (AtStart) {
- AtStart = false;
- }
- else {
- if (!Iterator.Ok())
- return false;
- ++Iterator;
- }
- return Iterator.Ok();
- }
- bool Next(NUdf::TUnboxedValue& key) override {
- if (!Skip())
- return false;
- key = NoSwap ?
- Parent->KeyPacker.Unpack(GetSmallValue(Iterator.Get().first), Parent->Ctx->HolderFactory):
- Parent->PayloadPacker.Unpack(GetSmallValue(Iterator.Get().second), Parent->Ctx->HolderFactory);
- return true;
- }
- bool NextPair(NUdf::TUnboxedValue& key, NUdf::TUnboxedValue& payload) override {
- if (!Next(key))
- return false;
- payload = NoSwap ?
- Parent->PayloadPacker.Unpack(GetSmallValue(Iterator.Get().second), Parent->Ctx->HolderFactory):
- Parent->KeyPacker.Unpack(GetSmallValue(Iterator.Get().first), Parent->Ctx->HolderFactory);
- return true;
- }
- const NUdf::TRefCountedPtr<THashedCompactMapHolder> Parent;
- typename TMapType::TIterator Iterator;
- bool AtStart;
- };
- THashedCompactMapHolder(TMemoryUsageInfo* memInfo, TMapType&& map, TPagedArena&& pool,
- TType* keyType, TType* payloadType, TComputationContext* ctx)
- : TComputationValue(memInfo)
- , Pool(std::move(pool))
- , Map(std::move(map))
- , KeyPacker(true, keyType)
- , PayloadPacker(false, payloadType)
- , Ctx(ctx)
- {
- }
- private:
- bool Contains(const NUdf::TUnboxedValuePod& key) const override {
- auto serializedKey = KeyPacker.Pack(NUdf::TUnboxedValuePod(key));
- ui64 smallValue = AsSmallValue(serializedKey);
- return Map.Has(smallValue);
- }
- NUdf::TUnboxedValue Lookup(const NUdf::TUnboxedValuePod& key) const override {
- auto serializedKey = KeyPacker.Pack(NUdf::TUnboxedValuePod(key));
- ui64 smallValue = AsSmallValue(serializedKey);
- auto it = Map.Find(smallValue);
- if (!it.Ok())
- return NUdf::TUnboxedValuePod();
- return PayloadPacker.Unpack(GetSmallValue(it.Get().second), Ctx->HolderFactory).Release().MakeOptional();
- }
- NUdf::TUnboxedValue GetKeysIterator() const override {
- return NUdf::TUnboxedValuePod(new TIterator<true>(this));
- }
- NUdf::TUnboxedValue GetDictIterator() const override {
- return NUdf::TUnboxedValuePod(new TIterator<true>(this));
- }
- NUdf::TUnboxedValue GetPayloadsIterator() const override {
- return NUdf::TUnboxedValuePod(new TIterator<false>(this));
- }
- ui64 GetDictLength() const override {
- return Map.Size();
- }
- bool HasDictItems() const override {
- return !Map.Empty();
- }
- bool IsSortedDict() const override {
- return false;
- }
- TPagedArena Pool;
- const TMapType Map;
- mutable TValuePacker KeyPacker;
- mutable TValuePacker PayloadPacker;
- TComputationContext* Ctx;
- };
- class THashedCompactMultiMapHolder : public TComputationValue<THashedCompactMultiMapHolder> {
- public:
- using TMapType = TValuesDictHashCompactMultiMap;
- using TMapIterator = typename TMapType::TIterator;
- class TPayloadList: public TCustomListValue {
- public:
- class TIterator : public TComputationValue<TIterator> {
- public:
- TIterator(const THashedCompactMultiMapHolder* parent, TMapIterator from)
- : TComputationValue(parent->GetMemInfo())
- , Parent(const_cast<THashedCompactMultiMapHolder*>(parent))
- , Iterator(from)
- {
- }
- private:
- bool Next(NUdf::TUnboxedValue& value) override {
- if (!Iterator.Ok()) {
- return false;
- }
- value = Parent->PayloadPacker.Unpack(GetSmallValue(Iterator.GetValue()), Parent->CompCtx.HolderFactory);
- ++Iterator;
- return true;
- }
- bool Skip() override {
- if (!Iterator.Ok()) {
- return false;
- }
- ++Iterator;
- return true;
- }
- const NUdf::TRefCountedPtr<THashedCompactMultiMapHolder> Parent;
- TMapIterator Iterator;
- };
- TPayloadList(TMemoryUsageInfo* memInfo, const THashedCompactMultiMapHolder* parent, TMapIterator from)
- : TCustomListValue(memInfo)
- , Parent(const_cast<THashedCompactMultiMapHolder*>(parent))
- , From(from)
- {
- Y_ASSERT(From.Ok());
- }
- private:
- bool HasFastListLength() const override {
- return true;
- }
- ui64 GetListLength() const override {
- if (!Length) {
- Length = Parent->Map.Count(From.GetKey());
- }
- return *Length;
- }
- bool HasListItems() const override {
- return true;
- }
- NUdf::TUnboxedValue GetListIterator() const override {
- return NUdf::TUnboxedValuePod(new TIterator(Parent.Get(), From));
- }
- const NUdf::TRefCountedPtr<THashedCompactMultiMapHolder> Parent;
- TMapIterator From;
- };
- template <bool NoSwap>
- class TIterator : public TComputationValue<TIterator<NoSwap>> {
- public:
- TIterator(const THashedCompactMultiMapHolder* parent)
- : TComputationValue<TIterator<NoSwap>>(parent->GetMemInfo())
- , Parent(const_cast<THashedCompactMultiMapHolder*>(parent))
- , Iterator(parent->Map.Iterate())
- {
- }
- private:
- bool NextPair(NUdf::TUnboxedValue& key, NUdf::TUnboxedValue& payload) override {
- if (!Iterator.Ok()) {
- return false;
- }
- if (NoSwap) {
- key = Parent->KeyPacker.Unpack(GetSmallValue(Iterator.GetKey()), Parent->CompCtx.HolderFactory);
- payload = Parent->CompCtx.HolderFactory.Create<TPayloadList>(Parent.Get(), Iterator.MakeCurrentKeyIter());
- } else {
- payload = Parent->KeyPacker.Unpack(GetSmallValue(Iterator.GetKey()), Parent->CompCtx.HolderFactory);
- key = Parent->CompCtx.HolderFactory.Create<TPayloadList>(Parent.Get(), Iterator.MakeCurrentKeyIter());
- }
- Iterator.NextKey();
- return true;
- }
- bool Next(NUdf::TUnboxedValue& key) override {
- if (!Iterator.Ok()) {
- return false;
- }
- key = NoSwap ?
- Parent->KeyPacker.Unpack(GetSmallValue(Iterator.GetKey()), Parent->CompCtx.HolderFactory):
- NUdf::TUnboxedValue(Parent->CompCtx.HolderFactory.Create<TPayloadList>(Parent.Get(), Iterator.MakeCurrentKeyIter()));
- Iterator.NextKey();
- return true;
- }
- bool Skip() override {
- if (!Iterator.Ok()) {
- return false;
- }
- Iterator.NextKey();
- return true;
- }
- const NUdf::TRefCountedPtr<THashedCompactMultiMapHolder> Parent;
- TMapIterator Iterator;
- };
- THashedCompactMultiMapHolder(TMemoryUsageInfo* memInfo, TMapType&& map, TPagedArena&& pool,
- TType* keyType, TType* payloadType, TComputationContext* ctx)
- : TComputationValue(memInfo)
- , Pool(std::move(pool))
- , Map(std::move(map))
- , KeyPacker(true, keyType)
- , PayloadPacker(false, payloadType)
- , CompCtx(*ctx)
- {
- }
- private:
- bool Contains(const NUdf::TUnboxedValuePod& key) const override {
- auto serializedKey = KeyPacker.Pack(NUdf::TUnboxedValuePod(key));
- ui64 smallValue = AsSmallValue(serializedKey);
- return Map.Has(smallValue);
- }
- NUdf::TUnboxedValue Lookup(const NUdf::TUnboxedValuePod& key) const override {
- auto serializedKey = KeyPacker.Pack(NUdf::TUnboxedValuePod(key));
- ui64 smallValue = AsSmallValue(serializedKey);
- auto it = Map.Find(smallValue);
- if (!it.Ok())
- return NUdf::TUnboxedValuePod();
- return CompCtx.HolderFactory.Create<TPayloadList>(this, it);
- }
- NUdf::TUnboxedValue GetKeysIterator() const override {
- return NUdf::TUnboxedValuePod(new TIterator<true>(this));
- }
- NUdf::TUnboxedValue GetDictIterator() const override {
- return NUdf::TUnboxedValuePod(new TIterator<true>(this));
- }
- NUdf::TUnboxedValue GetPayloadsIterator() const override {
- return NUdf::TUnboxedValuePod(new TIterator<false>(this));
- }
- ui64 GetDictLength() const override {
- return Map.UniqSize();
- }
- bool HasDictItems() const override {
- return !Map.Empty();
- }
- bool IsSortedDict() const override {
- return false;
- }
- TPagedArena Pool;
- const TMapType Map;
- mutable TValuePacker KeyPacker;
- mutable TValuePacker PayloadPacker;
- TComputationContext& CompCtx;
- };
- class THashedDictHolder: public TComputationValue<THashedDictHolder> {
- public:
- template <bool NoSwap>
- class TIterator: public TTemporaryComputationValue<TIterator<NoSwap>> {
- public:
- TIterator(const THashedDictHolder* parent)
- : TTemporaryComputationValue<TIterator<NoSwap>>(parent->GetMemInfo())
- , Parent(const_cast<THashedDictHolder*>(parent))
- , Iterator(Parent->Map.begin())
- , End(Parent->Map.end())
- , AtStart(true)
- {
- }
- private:
- bool Skip() override {
- if (AtStart) {
- AtStart = false;
- } else {
- if (Iterator == End)
- return false;
- ++Iterator;
- }
- return Iterator != End;
- }
- bool Next(NUdf::TUnboxedValue& key) override {
- if (!Skip())
- return false;
- if (NoSwap) {
- key = Iterator->first;
- if (Parent->Packer) {
- key = Parent->Packer->Unpack(key.AsStringRef(), Parent->HolderFactory);
- }
- } else {
- key = Iterator->second;
- }
- return true;
- }
- bool NextPair(NUdf::TUnboxedValue& key, NUdf::TUnboxedValue& payload) override {
- if (!Next(key))
- return false;
- if (NoSwap) {
- payload = Iterator->second;
- } else {
- payload = Iterator->first;
- if (Parent->Packer) {
- payload = Parent->Packer->Unpack(payload.AsStringRef(), Parent->HolderFactory);
- }
- }
- return true;
- }
- const NUdf::TRefCountedPtr<THashedDictHolder> Parent;
- TValuesDictHashMap::const_iterator Iterator;
- TValuesDictHashMap::const_iterator End;
- bool AtStart;
- };
- THashedDictHolder(TMemoryUsageInfo* memInfo, THashedDictFiller filler,
- const TKeyTypes& types, bool isTuple, bool eagerFill, TType* encodedType,
- const NUdf::IHash* hash, const NUdf::IEquate* equate, const THolderFactory& holderFactory)
- : TComputationValue(memInfo)
- , Filler(filler)
- , Types(types)
- , Map(0, TValueHasher(Types, isTuple, hash), TValueEqual(Types, isTuple, equate))
- , IsBuilt(false)
- , HolderFactory(holderFactory)
- {
- if (encodedType) {
- Packer.emplace(true, encodedType);
- }
- if (eagerFill)
- LazyBuildDict();
- }
- private:
- bool Contains(const NUdf::TUnboxedValuePod& key) const override {
- LazyBuildDict();
- NUdf::TUnboxedValue encodedKey;
- if (Packer) {
- encodedKey = MakeString(Packer->Pack(key));
- }
- return Map.find(NUdf::TUnboxedValuePod(Packer ? encodedKey : key)) != Map.cend();
- }
- NUdf::TUnboxedValue Lookup(const NUdf::TUnboxedValuePod& key) const override {
- LazyBuildDict();
- NUdf::TUnboxedValue encodedKey;
- if (Packer) {
- encodedKey = MakeString(Packer->Pack(key));
- }
- const auto it = Map.find(NUdf::TUnboxedValuePod(Packer ? encodedKey : key));
- if (it == Map.cend())
- return NUdf::TUnboxedValuePod();
- return it->second.MakeOptional();
- }
- NUdf::TUnboxedValue GetKeysIterator() const override {
- LazyBuildDict();
- return NUdf::TUnboxedValuePod(new TIterator<true>(this));
- }
- NUdf::TUnboxedValue GetDictIterator() const override {
- LazyBuildDict();
- return NUdf::TUnboxedValuePod(new TIterator<true>(this));
- }
- NUdf::TUnboxedValue GetPayloadsIterator() const override {
- LazyBuildDict();
- return NUdf::TUnboxedValuePod(new TIterator<false>(this));
- }
- ui64 GetDictLength() const override {
- LazyBuildDict();
- return Map.size();
- }
- bool HasDictItems() const override {
- LazyBuildDict();
- return !Map.empty();
- }
- bool IsSortedDict() const override {
- return false;
- }
- private:
- void LazyBuildDict() const {
- if (IsBuilt)
- return;
- Filler(Map);
- Filler = THashedDictFiller();
- IsBuilt = true;
- }
- private:
- mutable THashedDictFiller Filler;
- const TKeyTypes Types;
- mutable TValuesDictHashMap Map;
- mutable bool IsBuilt;
- const THolderFactory& HolderFactory;
- std::optional<TValuePacker> Packer;
- };
- template <typename T, bool OptionalKey>
- class THashedSingleFixedMapHolder : public TComputationValue<THashedSingleFixedMapHolder<T, OptionalKey>> {
- public:
- using TMapType = TValuesDictHashSingleFixedMap<T>;
- template <bool NoSwap>
- class TIterator : public TComputationValue<TIterator<NoSwap>> {
- public:
- enum class EState {
- AtStart,
- AtNull,
- Iterator
- };
- TIterator(const THashedSingleFixedMapHolder* parent)
- : TComputationValue<TIterator<NoSwap>>(parent->GetMemInfo())
- , Parent(const_cast<THashedSingleFixedMapHolder*>(parent))
- , Iterator(Parent->Map.begin())
- , End(Parent->Map.end())
- , State(EState::AtStart)
- {
- }
- private:
- bool Skip() final {
- switch (State) {
- case EState::AtStart:
- State = OptionalKey && Parent->NullPayload.has_value() ? EState::AtNull : EState::Iterator;
- break;
- case EState::AtNull:
- State = EState::Iterator;
- break;
- case EState::Iterator:
- if (Iterator == End) {
- return false;
- }
- ++Iterator;
- break;
- }
- return EState::AtNull == State || Iterator != End;
- }
- bool Next(NUdf::TUnboxedValue& key) final {
- if (!Skip())
- return false;
- key = NoSwap
- ? (EState::AtNull == State ? NUdf::TUnboxedValue() : NUdf::TUnboxedValue(NUdf::TUnboxedValuePod(Iterator->first)))
- : (EState::AtNull == State ? *Parent->NullPayload : Iterator->second);
- return true;
- }
- bool NextPair(NUdf::TUnboxedValue& key, NUdf::TUnboxedValue& payload) final {
- if (!Next(key))
- return false;
- payload = NoSwap
- ? (EState::AtNull == State ? *Parent->NullPayload : Iterator->second)
- : (EState::AtNull == State ? NUdf::TUnboxedValue() : NUdf::TUnboxedValue(NUdf::TUnboxedValuePod(Iterator->first)));
- return true;
- }
- const NUdf::TRefCountedPtr<THashedSingleFixedMapHolder> Parent;
- typename TMapType::const_iterator Iterator;
- typename TMapType::const_iterator End;
- EState State;
- };
- THashedSingleFixedMapHolder(TMemoryUsageInfo* memInfo, TValuesDictHashSingleFixedMap<T>&& map, std::optional<NUdf::TUnboxedValue>&& nullPayload)
- : TComputationValue<THashedSingleFixedMapHolder>(memInfo)
- , Map(std::move(map))
- , NullPayload(std::move(nullPayload))
- {
- }
- private:
- bool Contains(const NUdf::TUnboxedValuePod& key) const final {
- if constexpr (OptionalKey) {
- if (!key) {
- return NullPayload.has_value();
- }
- }
- return Map.find(key.Get<T>()) != Map.end();
- }
- NUdf::TUnboxedValue Lookup(const NUdf::TUnboxedValuePod& key) const final {
- if constexpr (OptionalKey) {
- if (!key) {
- return NullPayload.has_value() ? NullPayload->MakeOptional() : NUdf::TUnboxedValuePod();
- }
- }
- const auto it = Map.find(key.Get<T>());
- if (it == Map.end())
- return NUdf::TUnboxedValuePod();
- return it->second.MakeOptional();
- }
- NUdf::TUnboxedValue GetKeysIterator() const final {
- return NUdf::TUnboxedValuePod(new TIterator<true>(this));
- }
- NUdf::TUnboxedValue GetDictIterator() const final {
- return NUdf::TUnboxedValuePod(new TIterator<true>(this));
- }
- NUdf::TUnboxedValue GetPayloadsIterator() const final {
- return NUdf::TUnboxedValuePod(new TIterator<false>(this));
- }
- ui64 GetDictLength() const final {
- return Map.size() + ui64(OptionalKey && NullPayload.has_value());
- }
- bool HasDictItems() const final {
- return !Map.empty() || (OptionalKey && NullPayload.has_value());
- }
- bool IsSortedDict() const final {
- return false;
- }
- const TMapType Map;
- const std::optional<NUdf::TUnboxedValue> NullPayload;
- };
- template <typename T, bool OptionalKey>
- class THashedSingleFixedCompactMapHolder : public TComputationValue<THashedSingleFixedCompactMapHolder<T, OptionalKey>> {
- public:
- using TMapType = TValuesDictHashSingleFixedCompactMap<T>;
- template <bool NoSwap>
- class TIterator : public TComputationValue<TIterator<NoSwap>> {
- public:
- enum class EState {
- AtStart,
- AtNull,
- Iterator
- };
- TIterator(const THashedSingleFixedCompactMapHolder* parent)
- : TComputationValue<TIterator<NoSwap>>(parent->GetMemInfo())
- , Parent(const_cast<THashedSingleFixedCompactMapHolder*>(parent))
- , Iterator(Parent->Map.Iterate())
- , State(EState::AtStart)
- {
- }
- private:
- bool Skip() final {
- switch (State) {
- case EState::AtStart:
- State = OptionalKey && Parent->NullPayload.has_value() ? EState::AtNull : EState::Iterator;
- break;
- case EState::AtNull:
- State = EState::Iterator;
- break;
- case EState::Iterator:
- if (Iterator.Ok())
- ++Iterator;
- break;
- }
- return EState::AtNull == State || Iterator.Ok();
- }
- bool Next(NUdf::TUnboxedValue& key) final {
- if (!Skip())
- return false;
- key = NoSwap
- ? (EState::AtNull == State
- ? NUdf::TUnboxedValue()
- : NUdf::TUnboxedValue(NUdf::TUnboxedValuePod(Iterator.Get().first))
- )
- : (EState::AtNull == State
- ? Parent->PayloadPacker.Unpack(GetSmallValue(*Parent->NullPayload), Parent->Ctx->HolderFactory)
- : Parent->PayloadPacker.Unpack(GetSmallValue(Iterator.Get().second), Parent->Ctx->HolderFactory)
- );
- return true;
- }
- bool NextPair(NUdf::TUnboxedValue& key, NUdf::TUnboxedValue& payload) final {
- if (!Next(key))
- return false;
- payload = NoSwap
- ? (EState::AtNull == State
- ? Parent->PayloadPacker.Unpack(GetSmallValue(*Parent->NullPayload), Parent->Ctx->HolderFactory)
- : Parent->PayloadPacker.Unpack(GetSmallValue(Iterator.Get().second), Parent->Ctx->HolderFactory)
- )
- : (EState::AtNull == State
- ? NUdf::TUnboxedValue()
- : NUdf::TUnboxedValue(NUdf::TUnboxedValuePod(Iterator.Get().first))
- );
- return true;
- }
- const NUdf::TRefCountedPtr<THashedSingleFixedCompactMapHolder> Parent;
- typename TMapType::TIterator Iterator;
- EState State;
- };
- THashedSingleFixedCompactMapHolder(TMemoryUsageInfo* memInfo, TMapType&& map, std::optional<ui64>&& nullPayload, TPagedArena&& pool,
- TType* payloadType, TComputationContext* ctx)
- : TComputationValue<THashedSingleFixedCompactMapHolder>(memInfo)
- , Pool(std::move(pool))
- , Map(std::move(map))
- , NullPayload(std::move(nullPayload))
- , PayloadPacker(false, payloadType)
- , Ctx(ctx)
- {
- }
- private:
- bool Contains(const NUdf::TUnboxedValuePod& key) const final {
- if constexpr (OptionalKey) {
- if (!key) {
- return NullPayload.has_value();
- }
- }
- return Map.Has(key.Get<T>());
- }
- NUdf::TUnboxedValue Lookup(const NUdf::TUnboxedValuePod& key) const final {
- if constexpr (OptionalKey) {
- if (!key) {
- return NullPayload.has_value()
- ? PayloadPacker.Unpack(GetSmallValue(*NullPayload), Ctx->HolderFactory).Release().MakeOptional()
- : NUdf::TUnboxedValuePod();
- }
- }
- auto it = Map.Find(key.Get<T>());
- if (!it.Ok())
- return NUdf::TUnboxedValuePod();
- return PayloadPacker.Unpack(GetSmallValue(it.Get().second), Ctx->HolderFactory).Release().MakeOptional();
- }
- NUdf::TUnboxedValue GetKeysIterator() const final {
- return NUdf::TUnboxedValuePod(new TIterator<true>(this));
- }
- NUdf::TUnboxedValue GetDictIterator() const final {
- return NUdf::TUnboxedValuePod(new TIterator<true>(this));
- }
- NUdf::TUnboxedValue GetPayloadsIterator() const final {
- return NUdf::TUnboxedValuePod(new TIterator<false>(this));
- }
- ui64 GetDictLength() const final {
- return Map.Size() + ui64(OptionalKey && NullPayload.has_value());
- }
- bool HasDictItems() const final {
- return !Map.Empty() || (OptionalKey && NullPayload.has_value());
- }
- bool IsSortedDict() const final {
- return false;
- }
- private:
- TPagedArena Pool;
- const TMapType Map;
- const std::optional<ui64> NullPayload;
- mutable TValuePacker PayloadPacker;
- TComputationContext* Ctx;
- };
- template <typename T, bool OptionalKey>
- class THashedSingleFixedCompactMultiMapHolder : public TComputationValue<THashedSingleFixedCompactMultiMapHolder<T, OptionalKey>> {
- public:
- using TMapType = TValuesDictHashSingleFixedCompactMultiMap<T>;
- using TMapIterator = typename TMapType::TIterator;
- class TPayloadList: public TCustomListValue {
- public:
- class TIterator : public TComputationValue<TIterator> {
- public:
- TIterator(const THashedSingleFixedCompactMultiMapHolder* parent, TMapIterator from)
- : TComputationValue<TIterator>(parent->GetMemInfo())
- , Parent(const_cast<THashedSingleFixedCompactMultiMapHolder*>(parent))
- , Iterator(from)
- {
- }
- private:
- bool Next(NUdf::TUnboxedValue& value) final {
- if (!Iterator.Ok()) {
- return false;
- }
- value = Parent->PayloadPacker.Unpack(GetSmallValue(Iterator.GetValue()), Parent->Ctx->HolderFactory);
- ++Iterator;
- return true;
- }
- bool Skip() final {
- if (!Iterator.Ok()) {
- return false;
- }
- ++Iterator;
- return true;
- }
- const NUdf::TRefCountedPtr<THashedSingleFixedCompactMultiMapHolder> Parent;
- TMapIterator Iterator;
- };
- TPayloadList(TMemoryUsageInfo* memInfo, const THashedSingleFixedCompactMultiMapHolder* parent, TMapIterator from)
- : TCustomListValue(memInfo)
- , Parent(const_cast<THashedSingleFixedCompactMultiMapHolder*>(parent))
- , From(from)
- {
- Y_ASSERT(From.Ok());
- }
- bool HasFastListLength() const final {
- return true;
- }
- ui64 GetListLength() const final {
- if (!Length) {
- Length = Parent->Map.Count(From.GetKey());
- }
- return *Length;
- }
- bool HasListItems() const final {
- return true;
- }
- NUdf::TUnboxedValue GetListIterator() const final {
- return NUdf::TUnboxedValuePod(new TIterator(Parent.Get(), From));
- }
- const NUdf::TRefCountedPtr<THashedSingleFixedCompactMultiMapHolder> Parent;
- TMapIterator From;
- };
- class TNullPayloadList: public TCustomListValue {
- public:
- class TIterator : public TComputationValue<TIterator> {
- public:
- TIterator(const THashedSingleFixedCompactMultiMapHolder* parent)
- : TComputationValue<TIterator>(parent->GetMemInfo())
- , Parent(const_cast<THashedSingleFixedCompactMultiMapHolder*>(parent))
- , Iterator(Parent->NullPayloads.cbegin())
- {
- }
- private:
- bool Next(NUdf::TUnboxedValue& value) final {
- if (Iterator == Parent->NullPayloads.cend()) {
- return false;
- }
- value = Parent->PayloadPacker.Unpack(GetSmallValue(*Iterator), Parent->Ctx->HolderFactory);
- ++Iterator;
- return true;
- }
- bool Skip() final {
- if (Iterator == Parent->NullPayloads.cend()) {
- return false;
- }
- ++Iterator;
- return true;
- }
- const NUdf::TRefCountedPtr<THashedSingleFixedCompactMultiMapHolder> Parent;
- typename std::vector<ui64>::const_iterator Iterator;
- };
- TNullPayloadList(TMemoryUsageInfo* memInfo, const THashedSingleFixedCompactMultiMapHolder* parent)
- : TCustomListValue(memInfo)
- , Parent(const_cast<THashedSingleFixedCompactMultiMapHolder*>(parent))
- {
- }
- bool HasFastListLength() const final {
- return true;
- }
- ui64 GetListLength() const final {
- if (!Length) {
- Length = Parent->NullPayloads.size();
- }
- return *Length;
- }
- bool HasListItems() const final {
- return true;
- }
- NUdf::TUnboxedValue GetListIterator() const final {
- return NUdf::TUnboxedValuePod(new TIterator(Parent.Get()));
- }
- const NUdf::TRefCountedPtr<THashedSingleFixedCompactMultiMapHolder> Parent;
- };
- template <bool NoSwap>
- class TIterator : public TComputationValue<TIterator<NoSwap>> {
- public:
- TIterator(const THashedSingleFixedCompactMultiMapHolder* parent)
- : TComputationValue<TIterator<NoSwap>>(parent->GetMemInfo())
- , Parent(const_cast<THashedSingleFixedCompactMultiMapHolder*>(parent))
- , Iterator(parent->Map.Iterate())
- , AtNull(OptionalKey && !parent->NullPayloads.empty())
- {
- }
- private:
- bool Next(NUdf::TUnboxedValue& key) override {
- if (AtNull) {
- AtNull = false;
- key = NoSwap
- ? NUdf::TUnboxedValuePod()
- : Parent->Ctx->HolderFactory.template Create<TNullPayloadList>(Parent.Get());
- return true;
- }
- if (!Iterator.Ok()) {
- return false;
- }
- key = NoSwap ?
- NUdf::TUnboxedValuePod(Iterator.GetKey()):
- Parent->Ctx->HolderFactory.template Create<TPayloadList>(Parent.Get(), Iterator.MakeCurrentKeyIter());
- Iterator.NextKey();
- return true;
- }
- bool NextPair(NUdf::TUnboxedValue& key, NUdf::TUnboxedValue& payload) override {
- if (AtNull) {
- AtNull = false;
- if (NoSwap) {
- key = NUdf::TUnboxedValuePod();
- payload = Parent->Ctx->HolderFactory.template Create<TNullPayloadList>(Parent.Get());
- } else {
- payload = NUdf::TUnboxedValuePod();
- key = Parent->Ctx->HolderFactory.template Create<TNullPayloadList>(Parent.Get());
- }
- return true;
- }
- if (!Iterator.Ok()) {
- return false;
- }
- if (NoSwap) {
- key = NUdf::TUnboxedValuePod(Iterator.GetKey());
- payload = Parent->Ctx->HolderFactory.template Create<TPayloadList>(Parent.Get(), Iterator.MakeCurrentKeyIter());
- } else {
- payload = NUdf::TUnboxedValuePod(Iterator.GetKey());
- key = Parent->Ctx->HolderFactory.template Create<TPayloadList>(Parent.Get(), Iterator.MakeCurrentKeyIter());
- }
- Iterator.NextKey();
- return true;
- }
- bool Skip() override {
- if (AtNull) {
- AtNull = false;
- return true;
- }
- if (!Iterator.Ok()) {
- return false;
- }
- Iterator.NextKey();
- return true;
- }
- const NUdf::TRefCountedPtr<THashedSingleFixedCompactMultiMapHolder> Parent;
- TMapIterator Iterator;
- bool AtNull;
- };
- THashedSingleFixedCompactMultiMapHolder(TMemoryUsageInfo* memInfo, TMapType&& map, std::vector<ui64>&& nullPayloads, TPagedArena&& pool,
- TType* payloadType, TComputationContext* ctx)
- : TComputationValue<THashedSingleFixedCompactMultiMapHolder>(memInfo)
- , Pool(std::move(pool))
- , Map(std::move(map))
- , NullPayloads(std::move(nullPayloads))
- , PayloadPacker(false, payloadType)
- , Ctx(ctx)
- {
- }
- private:
- bool Contains(const NUdf::TUnboxedValuePod& key) const override {
- if constexpr (OptionalKey) {
- if (!key) {
- return !NullPayloads.empty();
- }
- }
- return Map.Has(key.Get<T>());
- }
- NUdf::TUnboxedValue Lookup(const NUdf::TUnboxedValuePod& key) const override {
- if constexpr (OptionalKey) {
- if (!key) {
- return NullPayloads.empty()
- ? NUdf::TUnboxedValuePod()
- : Ctx->HolderFactory.Create<TNullPayloadList>(this);
- }
- }
- const auto it = Map.Find(key.Get<T>());
- if (!it.Ok())
- return NUdf::TUnboxedValuePod();
- return Ctx->HolderFactory.Create<TPayloadList>(this, it);
- }
- NUdf::TUnboxedValue GetKeysIterator() const override {
- return NUdf::TUnboxedValuePod(new TIterator<true>(this));
- }
- NUdf::TUnboxedValue GetDictIterator() const override {
- return NUdf::TUnboxedValuePod(new TIterator<true>(this));
- }
- NUdf::TUnboxedValue GetPayloadsIterator() const override {
- return NUdf::TUnboxedValuePod(new TIterator<false>(this));
- }
- ui64 GetDictLength() const override {
- return Map.UniqSize() + ui64(OptionalKey && !NullPayloads.empty());
- }
- bool HasDictItems() const override {
- return !Map.Empty() || (OptionalKey && !NullPayloads.empty());
- }
- bool IsSortedDict() const override {
- return false;
- }
- private:
- TPagedArena Pool;
- const TMapType Map;
- const std::vector<ui64> NullPayloads;
- mutable TValuePacker PayloadPacker;
- TComputationContext* Ctx;
- };
- class TVariantHolder : public TComputationValue<TVariantHolder> {
- public:
- TVariantHolder(TMemoryUsageInfo* memInfo, NUdf::TUnboxedValue&& item, ui32 index)
- : TComputationValue(memInfo)
- , Item(std::move(item))
- , Index(index)
- {
- }
- private:
- NUdf::TUnboxedValue GetVariantItem() const override {
- return Item;
- }
- ui32 GetVariantIndex() const override {
- return Index;
- }
- const NUdf::TUnboxedValue Item;
- const ui32 Index;
- };
- class TListIteratorHolder : public TComputationValue<TListIteratorHolder> {
- public:
- TListIteratorHolder(TMemoryUsageInfo* memInfo, NUdf::TUnboxedValue&& list)
- : TComputationValue(memInfo)
- , List(std::move(list))
- , Iter(List.GetListIterator())
- {}
- private:
- NUdf::EFetchStatus Fetch(NUdf::TUnboxedValue& result) override {
- return Iter.Next(result) ? NUdf::EFetchStatus::Ok : NUdf::EFetchStatus::Finish;
- }
- const NUdf::TUnboxedValue List;
- const NUdf::TUnboxedValue Iter;
- };
- class TLimitedList: public TComputationValue<TLimitedList> {
- public:
- class TIterator: public TComputationValue<TIterator> {
- public:
- TIterator(TMemoryUsageInfo* memInfo, NUdf::TUnboxedValue&& iter, TMaybe<ui64> skip, TMaybe<ui64> take)
- : TComputationValue(memInfo)
- , Iter(std::move(iter))
- , Skip_(skip)
- , Take_(take)
- , Index(Max<ui64>())
- {
- }
- private:
- bool Next(NUdf::TUnboxedValue& value) override {
- if (!Iter) {
- return false;
- }
- if (Skip_) {
- while ((Index + 1) < Skip_.GetRef()) {
- if (!Iter.Skip()) {
- Iter = NUdf::TUnboxedValue();
- return false;
- }
- ++Index;
- }
- }
- if (Take_ && ((Index + 1) - Skip_.GetOrElse(0)) >= Take_.GetRef()) {
- Iter = NUdf::TUnboxedValue();
- return false;
- }
- if (!Iter.Next(value)) {
- Iter = NUdf::TUnboxedValue();
- return false;
- }
- ++Index;
- return true;
- }
- bool Skip() override {
- if (!Iter) {
- return false;
- }
- if (Skip_) {
- while ((Index + 1) < Skip_.GetRef()) {
- if (!Iter.Skip()) {
- Iter = NUdf::TUnboxedValue();
- return false;
- }
- ++Index;
- }
- }
- if (Take_ && ((Index + 1) - Skip_.GetOrElse(0)) >= Take_.GetRef()) {
- Iter = NUdf::TUnboxedValue();
- return false;
- }
- if (!Iter.Skip()) {
- Iter = NUdf::TUnboxedValue();
- return false;
- }
- ++Index;
- return true;
- }
- NUdf::TUnboxedValue Iter;
- const TMaybe<ui64> Skip_;
- const TMaybe<ui64> Take_;
- ui64 Index;
- };
- TLimitedList(TMemoryUsageInfo* memInfo, NUdf::TRefCountedPtr<NUdf::IBoxedValue> parent, TMaybe<ui64> skip, TMaybe<ui64> take)
- : TComputationValue(memInfo)
- , Parent(parent)
- , Skip(skip)
- , Take(take)
- {
- }
- private:
- bool HasFastListLength() const override {
- return Length.Defined();
- }
- ui64 GetListLength() const override {
- if (!Length) {
- ui64 length = NUdf::TBoxedValueAccessor::GetListLength(*Parent);
- if (Skip) {
- if (Skip.GetRef() >= length) {
- length = 0;
- } else {
- length -= Skip.GetRef();
- }
- }
- if (Take) {
- length = Min(length, Take.GetRef());
- }
- Length = length;
- }
- return Length.GetRef();
- }
- ui64 GetEstimatedListLength() const override {
- return GetListLength();
- }
- bool HasListItems() const override {
- if (HasItems) {
- return *HasItems;
- }
- if (Length) {
- HasItems = (*Length != 0);
- return *HasItems;
- }
- HasItems = GetListIterator().Skip();
- return *HasItems;
- }
- NUdf::TUnboxedValue GetListIterator() const override {
- return NUdf::TUnboxedValuePod(new TIterator(GetMemInfo(), NUdf::TBoxedValueAccessor::GetListIterator(*Parent), Skip, Take));
- }
- NUdf::IBoxedValuePtr SkipListImpl(const NUdf::IValueBuilder& builder, ui64 count) const override {
- if (!count) {
- return const_cast<TLimitedList*>(this);
- }
- if (Length) {
- if (count >= Length.GetRef()) {
- return builder.NewEmptyList().Release().AsBoxed();
- }
- }
- ui64 prevSkip = Skip.GetOrElse(0);
- if (count > Max<ui64>() - prevSkip) {
- return builder.NewEmptyList().Release().AsBoxed();
- }
- const ui64 newSkip = prevSkip + count;
- TMaybe<ui64> newTake = Take;
- if (newTake) {
- if (count >= newTake.GetRef()) {
- return builder.NewEmptyList().Release().AsBoxed();
- }
- newTake = newTake.GetRef() - count;
- }
- return new TLimitedList(GetMemInfo(), Parent, newSkip, newTake);
- }
- NUdf::IBoxedValuePtr TakeListImpl(const NUdf::IValueBuilder& builder, ui64 count) const override {
- if (!count) {
- return builder.NewEmptyList().Release().AsBoxed();
- }
- if (Length) {
- if (count >= Length.GetRef()) {
- return const_cast<TLimitedList*>(this);
- }
- }
- TMaybe<ui64> newTake = Take;
- if (newTake) {
- newTake = Min(count, newTake.GetRef());
- } else {
- newTake = count;
- }
- return new TLimitedList(GetMemInfo(), Parent, Skip, newTake);
- }
- NUdf::TRefCountedPtr<NUdf::IBoxedValue> Parent;
- TMaybe<ui64> Skip;
- TMaybe<ui64> Take;
- mutable TMaybe<ui64> Length;
- mutable TMaybe<bool> HasItems;
- };
- class TLazyListDecorator : public TComputationValue<TLazyListDecorator> {
- public:
- TLazyListDecorator(TMemoryUsageInfo* memInfo, NUdf::IBoxedValuePtr&& list)
- : TComputationValue(memInfo), List(std::move(list))
- {}
- private:
- bool HasListItems() const final {
- return NUdf::TBoxedValueAccessor::HasListItems(*List);
- }
- bool HasDictItems() const final {
- return NUdf::TBoxedValueAccessor::HasDictItems(*List);
- }
- bool HasFastListLength() const final {
- return NUdf::TBoxedValueAccessor::HasFastListLength(*List);
- }
- ui64 GetListLength() const final {
- return NUdf::TBoxedValueAccessor::GetListLength(*List);
- }
- ui64 GetDictLength() const final {
- return NUdf::TBoxedValueAccessor::GetDictLength(*List);
- }
- ui64 GetEstimatedListLength() const final {
- return NUdf::TBoxedValueAccessor::GetEstimatedListLength(*List);
- }
- NUdf::TUnboxedValue GetListIterator() const final {
- return NUdf::TBoxedValueAccessor::GetListIterator(*List);
- }
- NUdf::TUnboxedValue GetDictIterator() const final {
- return NUdf::TBoxedValueAccessor::GetDictIterator(*List);
- }
- NUdf::TUnboxedValue GetPayloadsIterator() const final {
- return NUdf::TBoxedValueAccessor::GetPayloadsIterator(*List);
- }
- NUdf::TUnboxedValue GetKeysIterator() const final {
- return NUdf::TBoxedValueAccessor::GetKeysIterator(*List);
- }
- NUdf::IBoxedValuePtr ReverseListImpl(const NUdf::IValueBuilder& builder) const final {
- return NUdf::TBoxedValueAccessor::ReverseListImpl(*List, builder);
- }
- NUdf::IBoxedValuePtr SkipListImpl(const NUdf::IValueBuilder& builder, ui64 count) const final {
- return NUdf::TBoxedValueAccessor::SkipListImpl(*List, builder, count);
- }
- NUdf::IBoxedValuePtr TakeListImpl(const NUdf::IValueBuilder& builder, ui64 count) const final {
- return NUdf::TBoxedValueAccessor::TakeListImpl(*List, builder, count);
- }
- NUdf::IBoxedValuePtr ToIndexDictImpl(const NUdf::IValueBuilder& builder) const final {
- return NUdf::TBoxedValueAccessor::ToIndexDictImpl(*List, builder);
- }
- bool Contains(const NUdf::TUnboxedValuePod& key) const final {
- return NUdf::TBoxedValueAccessor::Contains(*List, key);
- }
- NUdf::TUnboxedValue Lookup(const NUdf::TUnboxedValuePod& key) const final {
- return NUdf::TBoxedValueAccessor::Lookup(*List, key);
- }
- NUdf::TUnboxedValue GetElement(ui32 index) const final {
- return NUdf::TBoxedValueAccessor::GetElement(*List, index);
- }
- const NUdf::TUnboxedValue* GetElements() const final {
- return nullptr;
- }
- bool IsSortedDict() const final {
- return NUdf::TBoxedValueAccessor::IsSortedDict(*List);
- }
- const NUdf::IBoxedValuePtr List;
- };
- } // namespace
- ///////////////////////////////////////////////////////////////////////////////
- // TDictValueBuilder
- ///////////////////////////////////////////////////////////////////////////////
- class TDictValueBuilder: public NUdf::IDictValueBuilder
- {
- public:
- TDictValueBuilder(
- const THolderFactory& holderFactory,
- const TKeyTypes& types,
- bool isTuple,
- ui32 dictFlags,
- TType* encodeType,
- const NUdf::IHash* hash,
- const NUdf::IEquate* equate,
- const NUdf::ICompare* compare)
- : HolderFactory_(holderFactory)
- , Types_(types)
- , IsTuple_(isTuple)
- , DictFlags_(dictFlags)
- , EncodeType_(encodeType)
- , Hash_(hash)
- , Equate_(equate)
- , Compare_(compare)
- {
- Items_.reserve(10);
- }
- NUdf::IDictValueBuilder& Add(NUdf::TUnboxedValue&& key, NUdf::TUnboxedValue&& value) override
- {
- Items_.emplace_back(std::move(key), std::move(value));
- return *this;
- }
- NUdf::TUnboxedValue Build() override {
- if (Items_.empty())
- return HolderFactory_.GetEmptyContainerLazy();
- if (DictFlags_ & NUdf::TDictFlags::Hashed) {
- auto prepareFn = (DictFlags_ & NUdf::TDictFlags::Multi)
- ? &TDictValueBuilder::PrepareMultiHasedDict
- : &TDictValueBuilder::PrepareHasedDict;
- THashedDictFiller filler(std::bind(prepareFn, this, std::placeholders::_1));
- return HolderFactory_.CreateDirectHashedDictHolder(
- filler, Types_, IsTuple_, true, EncodeType_, Hash_, Equate_);
- }
- else {
- auto prepareFn = (DictFlags_ & NUdf::TDictFlags::Multi)
- ? &TDictValueBuilder::PrepareMultiSortedDict
- : &TDictValueBuilder::PrepareSortedDict;
- TSortedDictFiller filler(std::bind(prepareFn, this, std::placeholders::_1));
- EDictSortMode mode = (DictFlags_ & NUdf::TDictFlags::Multi)
- ? EDictSortMode::SortedUniqueAscending
- : EDictSortMode::RequiresSorting;
- return HolderFactory_.CreateDirectSortedDictHolder(filler, Types_, IsTuple_, mode, true,
- EncodeType_, Compare_, Equate_);
- }
- }
- private:
- void PrepareMultiHasedDict(TValuesDictHashMap& map) {
- TKeyPayloadPairVector localValues;
- localValues.swap(Items_);
- map.clear();
- std::optional<TValuePacker> packer;
- if (EncodeType_) {
- packer.emplace(true, EncodeType_);
- }
- for (auto& value : localValues) {
- auto key = value.first;
- if (packer) {
- key = MakeString(packer->Pack(key));
- }
- auto it = map.find(key);
- if (it == map.end()) {
- TDefaultListRepresentation emptyList;
- auto newList = HolderFactory_.CreateDirectListHolder(
- emptyList.Append(std::move(value.second)));
- map.emplace(std::move(key), std::move(newList));
- } else {
- auto prevList = GetDefaultListRepresentation(it->second);
- auto newList = HolderFactory_.CreateDirectListHolder(
- prevList->Append(std::move(value.second)));
- it->second = std::move(newList);
- }
- }
- }
- void PrepareHasedDict(TValuesDictHashMap& map) {
- TKeyPayloadPairVector localValues;
- localValues.swap(Items_);
- map.clear();
- std::optional<TValuePacker> packer;
- if (EncodeType_) {
- packer.emplace(true, EncodeType_);
- }
- for (auto& value : localValues) {
- auto key = value.first;
- if (packer) {
- key = MakeString(packer->Pack(key));
- }
- map.emplace(std::move(key), std::move(value.second));
- }
- }
- void PrepareMultiSortedDict(TKeyPayloadPairVector& values) {
- TKeyPayloadPairVector localValues;
- localValues.swap(Items_);
- std::optional<TGenericPresortEncoder> packer;
- if (EncodeType_) {
- packer.emplace(EncodeType_);
- for (auto& x : localValues) {
- x.first = MakeString(packer->Encode(x.first, false));
- }
- }
- StableSort(localValues.begin(), localValues.end(), TKeyPayloadPairLess(Types_, IsTuple_, Compare_));
- TKeyPayloadPairVector groups;
- groups.reserve(localValues.size());
- if (!localValues.empty()) {
- TDefaultListRepresentation currentList(std::move(localValues.begin()->second));
- auto lastKey = std::move(localValues.begin()->first);
- TValueEqual eqPredicate(Types_, IsTuple_, Equate_);
- for (auto it = localValues.begin() + 1; it != localValues.end(); ++it) {
- if (eqPredicate(lastKey, it->first)) {
- currentList = currentList.Append(std::move(it->second));
- } else {
- auto payload = HolderFactory_.CreateDirectListHolder(std::move(currentList));
- groups.emplace_back(std::move(lastKey), std::move(payload));
- currentList = TDefaultListRepresentation(std::move(it->second));
- lastKey = std::move(it->first);
- }
- }
- auto payload = HolderFactory_.CreateDirectListHolder(std::move(currentList));
- groups.emplace_back(std::move(lastKey), std::move(payload));
- }
- values.swap(groups);
- }
- void PrepareSortedDict(TKeyPayloadPairVector& values) {
- Items_.swap(values);
- std::optional<TGenericPresortEncoder> packer;
- if (EncodeType_) {
- packer.emplace(EncodeType_);
- for (auto& x : values) {
- x.first = MakeString(packer->Encode(x.first, false));
- }
- }
- }
- private:
- const THolderFactory& HolderFactory_;
- const TKeyTypes Types_;
- const bool IsTuple_;
- const ui32 DictFlags_;
- TType* const EncodeType_;
- const NUdf::IHash* Hash_;
- const NUdf::IEquate* Equate_;
- const NUdf::ICompare* Compare_;
- TKeyPayloadPairVector Items_;
- };
- ///////////////////////////////////////////////////////////////////////////////
- // TListValueBuilder
- ///////////////////////////////////////////////////////////////////////////////
- class TListValueBuilder: public NUdf::IListValueBuilder {
- public:
- explicit TListValueBuilder(const THolderFactory& HolderFactory)
- : HolderFactory_(HolderFactory)
- {}
- // Destroys (moves out from) the element
- IListValueBuilder& Add(NUdf::TUnboxedValue&& element) final {
- List_.emplace_back(element);
- return *this;
- }
- // Destroys (moves out from) the elements
- IListValueBuilder& AddMany(const NUdf::TUnboxedValue* elements, size_t count) final {
- std::copy_n(std::make_move_iterator(elements), count, std::back_inserter(List_));
- return *this;
- }
- NUdf::TUnboxedValue Build() final {
- if (List_.empty()) {
- return HolderFactory_.GetEmptyContainerLazy();
- }
- return HolderFactory_.VectorAsVectorHolder(std::move(List_));
- }
- private:
- const NMiniKQL::THolderFactory& HolderFactory_;
- TUnboxedValueVector List_;
- };
- //////////////////////////////////////////////////////////////////////////////
- // THolderFactory
- //////////////////////////////////////////////////////////////////////////////
- THolderFactory::THolderFactory(
- TAllocState& allocState,
- TMemoryUsageInfo& memInfo,
- const IFunctionRegistry* functionRegistry)
- : CurrentAllocState(&allocState)
- , MemInfo(memInfo)
- , FunctionRegistry(functionRegistry)
- {
- }
- THolderFactory::~THolderFactory() {
- if (EmptyContainer) {
- CurrentAllocState->UnlockObject(*EmptyContainer);
- }
- }
- NUdf::TUnboxedValuePod THolderFactory::GetEmptyContainerLazy() const {
- if (!EmptyContainer) {
- EmptyContainer.ConstructInPlace(
- NUdf::TUnboxedValuePod(AllocateOn<TEmptyContainerHolder>(CurrentAllocState, &MemInfo)));
- CurrentAllocState->LockObject(*EmptyContainer);
- }
- return *EmptyContainer;
- }
- NUdf::TUnboxedValuePod THolderFactory::CreateTypeHolder(TType* type) const {
- return NUdf::TUnboxedValuePod(AllocateOn<TTypeHolder>(CurrentAllocState, &MemInfo, type));
- }
- NUdf::TUnboxedValuePod THolderFactory::CreateDirectListHolder(TDefaultListRepresentation&& items) const{
- if (!items.GetLength())
- return GetEmptyContainerLazy();
- return NUdf::TUnboxedValuePod(AllocateOn<TDirectListHolder>(CurrentAllocState, &MemInfo, std::move(items)));
- }
- NUdf::TUnboxedValuePod THolderFactory::CreateDirectArrayHolder(ui64 size, NUdf::TUnboxedValue*& itemsPtr) const {
- if (!size) {
- itemsPtr = nullptr;
- return GetEmptyContainerLazy();
- }
- const auto buffer = MKQLAllocFastWithSize(
- sizeof(TDirectArrayHolderInplace) + size * sizeof(NUdf::TUnboxedValue), CurrentAllocState, EMemorySubPool::Default);
- const auto h = ::new(buffer) TDirectArrayHolderInplace(&MemInfo, size);
- auto res = NUdf::TUnboxedValuePod(h);
- itemsPtr = h->GetPtr();
- return res;
- }
- NUdf::TUnboxedValuePod THolderFactory::CreateArrowBlock(arrow::Datum&& datum) const {
- return Create<TArrowBlock>(std::move(datum));
- }
- NUdf::TUnboxedValuePod THolderFactory::VectorAsArray(TUnboxedValueVector& values) const {
- if (values.empty())
- return GetEmptyContainerLazy();
- NUdf::TUnboxedValue* itemsPtr = nullptr;
- auto tuple = CreateDirectArrayHolder(values.size(), itemsPtr);
- for (auto& value : values) {
- *itemsPtr++ = std::move(value);
- }
- return tuple;
- }
- NUdf::TUnboxedValuePod THolderFactory::NewVectorHolder() const {
- return NUdf::TUnboxedValuePod(new TVectorHolder(&MemInfo));
- }
- NUdf::TUnboxedValuePod THolderFactory::NewTemporaryVectorHolder() const {
- return NUdf::TUnboxedValuePod(new TTemporaryVectorHolder(&MemInfo));
- }
- const NUdf::IHash* THolderFactory::GetHash(const TType& type, bool useIHash) const {
- return useIHash ? HashRegistry.FindOrEmplace(type) : nullptr;
- }
- const NUdf::IEquate* THolderFactory::GetEquate(const TType& type, bool useIHash) const {
- return useIHash ? EquateRegistry.FindOrEmplace(type) : nullptr;
- }
- const NUdf::ICompare* THolderFactory::GetCompare(const TType& type, bool useIHash) const {
- return useIHash ? CompareRegistry.FindOrEmplace(type) : nullptr;
- }
- NUdf::TUnboxedValuePod THolderFactory::VectorAsVectorHolder(TUnboxedValueVector&& list) const {
- return NUdf::TUnboxedValuePod(new TVectorHolder(&MemInfo, std::move(list)));
- }
- NUdf::TUnboxedValuePod THolderFactory::CloneArray(const NUdf::TUnboxedValuePod list, NUdf::TUnboxedValue*& items) const {
- if (const auto size = list.GetListLength()) {
- const auto ptr = list.GetElements();
- if (ptr && list.UniqueBoxed()) {
- items = const_cast<NUdf::TUnboxedValue*>(ptr);
- return list;
- } else {
- const auto array = CreateDirectArrayHolder(size, items);
- if (ptr) {
- std::copy(ptr, ptr + size, items);
- } else if (const auto& it = list.GetListIterator()) {
- for (auto out = items; it.Next(*out++);)
- continue;
- }
- list.DeleteUnreferenced();
- return array;
- }
- } else {
- items = nullptr;
- return GetEmptyContainerLazy();
- }
- }
- NUdf::TUnboxedValuePod THolderFactory::Cloned(const NUdf::TUnboxedValuePod& it) const
- {
- TDefaultListRepresentation result;
- for (NUdf::TUnboxedValue item; it.Next(item);) {
- result = result.Append(std::move(item));
- }
- return CreateDirectListHolder(std::move(result));
- }
- NUdf::TUnboxedValuePod THolderFactory::Reversed(const NUdf::TUnboxedValuePod& it) const
- {
- TDefaultListRepresentation result;
- for (NUdf::TUnboxedValue item; it.Next(item);) {
- result = result.Prepend(std::move(item));
- }
- return CreateDirectListHolder(std::move(result));
- }
- NUdf::TUnboxedValuePod THolderFactory::CreateLimitedList(
- NUdf::IBoxedValuePtr&& parent,
- TMaybe<ui64> skip, TMaybe<ui64> take,
- TMaybe<ui64> knownLength) const
- {
- if (take && !take.GetRef()) {
- return GetEmptyContainerLazy();
- }
- if (skip && !skip.GetRef()) {
- skip = TMaybe<ui64>();
- }
- if (knownLength && skip) {
- if (skip.GetRef() >= knownLength.GetRef()) {
- return GetEmptyContainerLazy();
- }
- }
- if (knownLength && take) {
- if (take.GetRef() >= knownLength.GetRef() - skip.GetOrElse(0)) {
- take = TMaybe<ui64>();
- }
- }
- if (!skip && !take) {
- return NUdf::TUnboxedValuePod(std::move(parent));
- }
- return NUdf::TUnboxedValuePod(AllocateOn<TLimitedList>(CurrentAllocState, &MemInfo, std::move(parent), skip, take));
- }
- NUdf::TUnboxedValuePod THolderFactory::ReverseList(const NUdf::IValueBuilder* builder, const NUdf::TUnboxedValuePod list) const
- {
- auto boxed = list.AsBoxed();
- if (auto res = NUdf::TBoxedValueAccessor::ReverseListImpl(*boxed, *builder)) {
- return NUdf::TUnboxedValuePod(std::move(boxed = std::move(res)));
- }
- return Reversed(list.GetListIterator());
- }
- NUdf::TUnboxedValuePod THolderFactory::SkipList(const NUdf::IValueBuilder* builder, const NUdf::TUnboxedValuePod list, ui64 count) const
- {
- auto boxed = list.AsBoxed();
- if (auto res = NUdf::TBoxedValueAccessor::SkipListImpl(*boxed, *builder, count)) {
- return NUdf::TUnboxedValuePod(std::move(boxed = std::move(res)));
- }
- TMaybe<ui64> knownLength;
- if (list.HasFastListLength()) {
- knownLength = list.GetListLength();
- }
- return CreateLimitedList(std::move(boxed), count, TMaybe<ui64>(), knownLength);
- }
- NUdf::TUnboxedValuePod THolderFactory::TakeList(const NUdf::IValueBuilder* builder, const NUdf::TUnboxedValuePod list, ui64 count) const
- {
- auto boxed = list.AsBoxed();
- if (auto res = NUdf::TBoxedValueAccessor::TakeListImpl(*boxed, *builder, count)) {
- return NUdf::TUnboxedValuePod(std::move(boxed = std::move(res)));
- }
- TMaybe<ui64> knownLength;
- if (list.HasFastListLength()) {
- knownLength = list.GetListLength();
- }
- return CreateLimitedList(std::move(boxed), TMaybe<ui64>(), count, knownLength);
- }
- NUdf::TUnboxedValuePod THolderFactory::ToIndexDict(const NUdf::IValueBuilder* builder, const NUdf::TUnboxedValuePod list) const
- {
- auto boxed = list.AsBoxed();
- if (auto res = NUdf::TBoxedValueAccessor::ToIndexDictImpl(*boxed, *builder)) {
- return NUdf::TUnboxedValuePod(std::move(boxed = std::move(res)));
- }
- return Cloned(list.GetListIterator());
- }
- template<bool IsStream>
- NUdf::TUnboxedValuePod THolderFactory::Collect(NUdf::TUnboxedValuePod list) const {
- const auto boxed = list.AsBoxed(); // Only for release on exit.
- if (!IsStream && list.HasFastListLength()) {
- auto size = list.GetListLength();
- NUdf::TUnboxedValue* items = nullptr;
- const auto result = CreateDirectArrayHolder(size, items);
- TThresher<IsStream>::DoForEachItem(list,
- [&items] (NUdf::TUnboxedValue&& item) {
- *items++ = std::move(item);
- }
- );
- return result;
- } else {
- TDefaultListRepresentation res;
- TThresher<IsStream>::DoForEachItem(list,
- [&res] (NUdf::TUnboxedValue&& item) {
- res = res.Append(std::move(item));
- }
- );
- return CreateDirectListHolder(std::move(res));
- }
- }
- template NUdf::TUnboxedValuePod THolderFactory::Collect<true>(NUdf::TUnboxedValuePod list) const;
- template NUdf::TUnboxedValuePod THolderFactory::Collect<false>(NUdf::TUnboxedValuePod list) const;
- NUdf::TUnboxedValuePod THolderFactory::LazyList(NUdf::TUnboxedValuePod list) const {
- return NUdf::TUnboxedValuePod(AllocateOn<TLazyListDecorator>(CurrentAllocState, &MemInfo, list.AsBoxed()));;
- }
- NUdf::TUnboxedValuePod THolderFactory::Append(NUdf::TUnboxedValuePod list, NUdf::TUnboxedValuePod last) const {
- const auto boxed = list.AsBoxed();
- TDefaultListRepresentation resList;
- if (const auto leftRepr = reinterpret_cast<const TDefaultListRepresentation*>(NUdf::TBoxedValueAccessor::GetListRepresentation(*boxed))) {
- resList = std::move(*leftRepr);
- } else {
- TThresher<false>::DoForEachItem(list,
- [&resList] (NUdf::TUnboxedValue&& item) {
- resList = resList.Append(std::move(item));
- }
- );
- }
- resList = resList.Append(std::move(last));
- return CreateDirectListHolder(std::move(resList));
- }
- NUdf::TUnboxedValuePod THolderFactory::Prepend(NUdf::TUnboxedValuePod first, NUdf::TUnboxedValuePod list) const {
- const auto boxed = list.AsBoxed();
- TDefaultListRepresentation resList;
- if (const auto rightRepr = reinterpret_cast<const TDefaultListRepresentation*>(NUdf::TBoxedValueAccessor::GetListRepresentation(*boxed))) {
- resList = *rightRepr;
- } else {
- TThresher<false>::DoForEachItem(list,
- [&resList] (NUdf::TUnboxedValue&& item) {
- resList = resList.Append(std::move(item));
- }
- );
- }
- resList = resList.Prepend(std::move(first));
- return CreateDirectListHolder(std::move(resList));
- }
- NUdf::TUnboxedValuePod THolderFactory::ExtendStream(NUdf::TUnboxedValue* data, ui64 size) const {
- if (!data || !size) {
- return GetEmptyContainerLazy();
- }
- TUnboxedValueVector values(size);
- std::move(data, data + size, values.begin());
- return Create<TExtendStreamValue>(std::move(values));
- }
- template<>
- NUdf::TUnboxedValuePod THolderFactory::ExtendList<true>(NUdf::TUnboxedValue* data, ui64 size) const {
- if (!data || !size) {
- return GetEmptyContainerLazy();
- }
- TUnboxedValueVector values;
- values.reserve(size);
- std::transform(data, data + size, std::back_inserter(values), [this](NUdf::TUnboxedValue& stream){ return Create<TForwardListValue>(std::move(stream)); });
- return Create<TExtendListValue>(std::move(values));
- }
- template<>
- NUdf::TUnboxedValuePod THolderFactory::ExtendList<false>(NUdf::TUnboxedValue* data, ui64 size) const {
- if (!data || !size) {
- return GetEmptyContainerLazy();
- }
- using TElementsAndSize = std::tuple<const NUdf::TUnboxedValuePod*, ui64, ui64>;
- TSmallVec<TElementsAndSize, TMKQLAllocator<TElementsAndSize>> elements;
- elements.reserve(size);
- for (ui64 i = 0ULL; i < size; ++i) {
- if (const auto ptr = data[i].GetElements()) {
- if (const auto length = data[i].GetListLength()) {
- elements.emplace_back(ptr, length, i);
- }
- } else {
- TUnboxedValueVector values(size);
- std::move(data, data + size, values.begin());
- return Create<TExtendListValue>(std::move(values));
- }
- }
- const auto total = std::accumulate(elements.cbegin(), elements.cend(), 0ULL, [](ui64 s, TElementsAndSize i) { return s + std::get<1U>(i); });
- if (!total) {
- std::fill_n(data, size, NUdf::TUnboxedValue());
- return GetEmptyContainerLazy();
- }
- if (1U == elements.size()) {
- const auto result = data[std::get<2U>(elements.front())].Release();
- std::fill_n(data, size, NUdf::TUnboxedValue());
- return result;
- }
- auto it = elements.cbegin();
- if (const auto first = GetDefaultListRepresentation(data[std::get<2U>(*it++)])) {
- TDefaultListRepresentation list(*first);
- while (elements.cend() != it) {
- const auto& e = *it++;
- if (const auto repr = GetDefaultListRepresentation(data[std::get<2U>(e)])) {
- list = list.Extend(*repr);
- } else {
- std::for_each(std::get<0U>(e), std::get<0U>(e) + std::get<1U>(e),
- [&](NUdf::TUnboxedValue item) {
- list = list.Append(std::move(item));
- }
- );
- }
- }
- std::fill_n(data, size, NUdf::TUnboxedValue());
- return CreateDirectListHolder(std::move(list));
- } else {
- NUdf::TUnboxedValue *items = nullptr;
- const auto result = CreateDirectArrayHolder(total, items);
- for (const auto& i : elements) {
- std::copy_n(std::get<0U>(i), std::get<1U>(i), items);
- items += std::get<1U>(i);
- }
- std::fill_n(data, size, NUdf::TUnboxedValue());
- return result;
- }
- }
- NUdf::TUnboxedValuePod THolderFactory::CreateVariantHolder(NUdf::TUnboxedValuePod item, ui32 index) const {
- if (item.TryMakeVariant(index))
- return item;
- return CreateBoxedVariantHolder(std::move(item), index);
- }
- NUdf::TUnboxedValuePod THolderFactory::CreateBoxedVariantHolder(NUdf::TUnboxedValuePod item, ui32 index) const {
- return NUdf::TUnboxedValuePod(AllocateOn<TVariantHolder>(CurrentAllocState, &MemInfo, std::move(item), index));
- }
- NUdf::TUnboxedValuePod THolderFactory::CreateIteratorOverList(NUdf::TUnboxedValuePod list) const {
- return NUdf::TUnboxedValuePod(AllocateOn<TListIteratorHolder>(CurrentAllocState, &MemInfo, list));
- }
- NUdf::TUnboxedValuePod THolderFactory::CreateForwardList(NUdf::TUnboxedValuePod stream) const {
- return NUdf::TUnboxedValuePod(AllocateOn<TForwardListValue>(CurrentAllocState, &MemInfo, stream));
- }
- NUdf::TUnboxedValuePod THolderFactory::CreateDirectSortedSetHolder(
- TSortedSetFiller filler,
- const TKeyTypes& types,
- bool isTuple,
- EDictSortMode mode,
- bool eagerFill,
- TType* encodedType,
- const NUdf::ICompare* compare,
- const NUdf::IEquate* equate) const
- {
- return NUdf::TUnboxedValuePod(AllocateOn<TSortedSetHolder>(CurrentAllocState, &MemInfo,
- filler, types, isTuple, mode, eagerFill, encodedType, compare, equate, *this));
- }
- NUdf::TUnboxedValuePod THolderFactory::CreateDirectSortedDictHolder(
- TSortedDictFiller filler,
- const TKeyTypes& types,
- bool isTuple,
- EDictSortMode mode,
- bool eagerFill,
- TType* encodedType,
- const NUdf::ICompare* compare,
- const NUdf::IEquate* equate) const
- {
- return NUdf::TUnboxedValuePod(AllocateOn<TSortedDictHolder>(CurrentAllocState, &MemInfo,
- filler, types, isTuple, mode, eagerFill, encodedType, compare, equate, *this));
- }
- NUdf::TUnboxedValuePod THolderFactory::CreateDirectHashedDictHolder(
- THashedDictFiller filler,
- const TKeyTypes& types,
- bool isTuple,
- bool eagerFill,
- TType* encodedType,
- const NUdf::IHash* hash,
- const NUdf::IEquate* equate) const
- {
- return NUdf::TUnboxedValuePod(AllocateOn<THashedDictHolder>(CurrentAllocState, &MemInfo,
- filler, types, isTuple, eagerFill, encodedType, hash, equate, *this));
- }
- NUdf::TUnboxedValuePod THolderFactory::CreateDirectHashedSetHolder(
- THashedSetFiller filler,
- const TKeyTypes& types,
- bool isTuple,
- bool eagerFill,
- TType* encodedType,
- const NUdf::IHash* hash,
- const NUdf::IEquate* equate) const {
- return NUdf::TUnboxedValuePod(AllocateOn<THashedSetHolder>(CurrentAllocState, &MemInfo,
- filler, types, isTuple, eagerFill, encodedType, hash, equate, *this));
- }
- template <typename T, bool OptionalKey>
- NUdf::TUnboxedValuePod THolderFactory::CreateDirectHashedSingleFixedSetHolder(
- TValuesDictHashSingleFixedSet<T>&& set, bool hasNull) const {
- return NUdf::TUnboxedValuePod(AllocateOn<THashedSingleFixedSetHolder<T, OptionalKey>>(CurrentAllocState, &MemInfo, std::move(set), hasNull));
- }
- #define DEFINE_HASHED_SINGLE_FIXED_SET_OPT(xType) \
- template NUdf::TUnboxedValuePod THolderFactory::CreateDirectHashedSingleFixedSetHolder<xType, true> \
- (TValuesDictHashSingleFixedSet<xType>&& set, bool hasNull) const;
- KNOWN_PRIMITIVE_VALUE_TYPES(DEFINE_HASHED_SINGLE_FIXED_SET_OPT)
- #undef DEFINE_HASHED_SINGLE_FIXED_SET_OPT
- #define DEFINE_HASHED_SINGLE_FIXED_SET_NONOPT(xType) \
- template NUdf::TUnboxedValuePod THolderFactory::CreateDirectHashedSingleFixedSetHolder<xType, false> \
- (TValuesDictHashSingleFixedSet<xType>&& set, bool hasNull) const;
- KNOWN_PRIMITIVE_VALUE_TYPES(DEFINE_HASHED_SINGLE_FIXED_SET_NONOPT)
- #undef DEFINE_HASHED_SINGLE_FIXED_SET_NONOPT
- template <typename T, bool OptionalKey>
- NUdf::TUnboxedValuePod THolderFactory::CreateDirectHashedSingleFixedCompactSetHolder(
- TValuesDictHashSingleFixedCompactSet<T>&& set, bool hasNull) const {
- return NUdf::TUnboxedValuePod(AllocateOn<THashedSingleFixedCompactSetHolder<T, OptionalKey>>(CurrentAllocState, &MemInfo, std::move(set), hasNull));
- }
- #define DEFINE_HASHED_SINGLE_FIXED_COMPACT_SET_OPT(xType) \
- template NUdf::TUnboxedValuePod THolderFactory::CreateDirectHashedSingleFixedCompactSetHolder<xType, true> \
- (TValuesDictHashSingleFixedCompactSet<xType>&& set, bool hasNull) const;
- KNOWN_PRIMITIVE_VALUE_TYPES(DEFINE_HASHED_SINGLE_FIXED_COMPACT_SET_OPT)
- #undef DEFINE_HASHED_SINGLE_FIXED_COMPACT_SET_OPT
- #define DEFINE_HASHED_SINGLE_FIXED_COMPACT_SET_NONOPT(xType) \
- template NUdf::TUnboxedValuePod THolderFactory::CreateDirectHashedSingleFixedCompactSetHolder<xType, false> \
- (TValuesDictHashSingleFixedCompactSet<xType>&& set, bool hasNull) const;
- KNOWN_PRIMITIVE_VALUE_TYPES(DEFINE_HASHED_SINGLE_FIXED_COMPACT_SET_NONOPT)
- #undef DEFINE_HASHED_SINGLE_FIXED_COMPACT_SET_NONOPT
- template <typename T, bool OptionalKey>
- NUdf::TUnboxedValuePod THolderFactory::CreateDirectHashedSingleFixedMapHolder(
- TValuesDictHashSingleFixedMap<T>&& map, std::optional<NUdf::TUnboxedValue>&& nullPayload) const {
- return NUdf::TUnboxedValuePod(AllocateOn<THashedSingleFixedMapHolder<T, OptionalKey>>(CurrentAllocState, &MemInfo, std::move(map), std::move(nullPayload)));
- }
- NUdf::TUnboxedValuePod THolderFactory::CreateDirectHashedCompactSetHolder(
- TValuesDictHashCompactSet&& set, TPagedArena&& pool, TType* keyType, TComputationContext* ctx) const {
- return NUdf::TUnboxedValuePod(AllocateOn<THashedCompactSetHolder>(CurrentAllocState, &MemInfo, std::move(set), std::move(pool), keyType, ctx));
- }
- NUdf::TUnboxedValuePod THolderFactory::CreateDirectHashedCompactMapHolder(
- TValuesDictHashCompactMap&& map, TPagedArena&& pool, TType* keyType, TType* payloadType,
- TComputationContext* ctx) const {
- return NUdf::TUnboxedValuePod(AllocateOn<THashedCompactMapHolder>(CurrentAllocState, &MemInfo, std::move(map), std::move(pool), keyType, payloadType, ctx));
- }
- NUdf::TUnboxedValuePod THolderFactory::CreateDirectHashedCompactMultiMapHolder(
- TValuesDictHashCompactMultiMap&& map, TPagedArena&& pool, TType* keyType, TType* payloadType,
- TComputationContext* ctx) const {
- return NUdf::TUnboxedValuePod(AllocateOn<THashedCompactMultiMapHolder>(CurrentAllocState, &MemInfo, std::move(map), std::move(pool), keyType, payloadType, ctx));
- }
- template <typename T, bool OptionalKey>
- NUdf::TUnboxedValuePod THolderFactory::CreateDirectHashedSingleFixedCompactMapHolder(
- TValuesDictHashSingleFixedCompactMap<T>&& map, std::optional<ui64>&& nullPayload, TPagedArena&& pool, TType* payloadType,
- TComputationContext* ctx) const {
- return NUdf::TUnboxedValuePod(AllocateOn<THashedSingleFixedCompactMapHolder<T, OptionalKey>>(CurrentAllocState, &MemInfo,
- std::move(map), std::move(nullPayload), std::move(pool), payloadType, ctx));
- }
- template <typename T, bool OptionalKey>
- NUdf::TUnboxedValuePod THolderFactory::CreateDirectHashedSingleFixedCompactMultiMapHolder(
- TValuesDictHashSingleFixedCompactMultiMap<T>&& map, std::vector<ui64>&& nullPayloads, TPagedArena&& pool, TType* payloadType,
- TComputationContext* ctx) const {
- return NUdf::TUnboxedValuePod(AllocateOn<THashedSingleFixedCompactMultiMapHolder<T, OptionalKey>>(CurrentAllocState, &MemInfo,
- std::move(map), std::move(nullPayloads), std::move(pool), payloadType, ctx));
- }
- NUdf::IDictValueBuilder::TPtr THolderFactory::NewDict(
- const NUdf::TType* dictType,
- ui32 flags) const
- {
- TType* type = const_cast<TType*>(static_cast<const TType*>(dictType));
- TType* keyType = AS_TYPE(TDictType, type)->GetKeyType();
- TKeyTypes types;
- bool encoded;
- bool isTuple;
- bool useIHash;
- GetDictionaryKeyTypes(keyType, types, isTuple, encoded, useIHash);
- return new TDictValueBuilder(*this, types, isTuple, flags, encoded ? keyType : nullptr,
- GetHash(*keyType, useIHash), GetEquate(*keyType, useIHash),
- GetCompare(*keyType, useIHash));
- }
- NUdf::IListValueBuilder::TPtr THolderFactory::NewList() const {
- return new TListValueBuilder(*this);
- }
- #define DEFINE_HASHED_SINGLE_FIXED_MAP_OPT(xType) \
- template NUdf::TUnboxedValuePod THolderFactory::CreateDirectHashedSingleFixedMapHolder<xType, true> \
- (TValuesDictHashSingleFixedMap<xType>&& map, std::optional<NUdf::TUnboxedValue>&& nullPayload) const;
- KNOWN_PRIMITIVE_VALUE_TYPES(DEFINE_HASHED_SINGLE_FIXED_MAP_OPT)
- #undef DEFINE_HASHED_SINGLE_FIXED_MAP_OPT
- #define DEFINE_HASHED_SINGLE_FIXED_MAP_NONOPT(xType) \
- template NUdf::TUnboxedValuePod THolderFactory::CreateDirectHashedSingleFixedMapHolder<xType, false> \
- (TValuesDictHashSingleFixedMap<xType>&& map, std::optional<NUdf::TUnboxedValue>&& nullPayload) const;
- KNOWN_PRIMITIVE_VALUE_TYPES(DEFINE_HASHED_SINGLE_FIXED_MAP_NONOPT)
- #undef DEFINE_HASHED_SINGLE_FIXED_MAP_NONOPT
- #define DEFINE_HASHED_SINGLE_FIXED_COMPACT_MAP_OPT(xType) \
- template NUdf::TUnboxedValuePod THolderFactory::CreateDirectHashedSingleFixedCompactMapHolder<xType, true> \
- (TValuesDictHashSingleFixedCompactMap<xType>&& map, std::optional<ui64>&& nullPayload, TPagedArena&& pool, TType* payloadType, \
- TComputationContext* ctx) const;
- KNOWN_PRIMITIVE_VALUE_TYPES(DEFINE_HASHED_SINGLE_FIXED_COMPACT_MAP_OPT)
- #undef DEFINE_HASHED_SINGLE_FIXED_COMPACT_MAP_OPT
- #define DEFINE_HASHED_SINGLE_FIXED_COMPACT_MAP_NONOPT(xType) \
- template NUdf::TUnboxedValuePod THolderFactory::CreateDirectHashedSingleFixedCompactMapHolder<xType, false> \
- (TValuesDictHashSingleFixedCompactMap<xType>&& map, std::optional<ui64>&& nullPayload, TPagedArena&& pool, TType* payloadType, \
- TComputationContext* ctx) const;
- KNOWN_PRIMITIVE_VALUE_TYPES(DEFINE_HASHED_SINGLE_FIXED_COMPACT_MAP_NONOPT)
- #undef DEFINE_HASHED_SINGLE_FIXED_COMPACT_MAP_NONOPT
- #define DEFINE_HASHED_SINGLE_FIXED_COMPACT_MULTI_MAP_OPT(xType) \
- template NUdf::TUnboxedValuePod THolderFactory::CreateDirectHashedSingleFixedCompactMultiMapHolder<xType, true> \
- (TValuesDictHashSingleFixedCompactMultiMap<xType>&& map, std::vector<ui64>&& nullPayloads, TPagedArena&& pool, TType* payloadType, \
- TComputationContext* ctx) const;
- KNOWN_PRIMITIVE_VALUE_TYPES(DEFINE_HASHED_SINGLE_FIXED_COMPACT_MULTI_MAP_OPT)
- #undef DEFINE_HASHED_SINGLE_FIXED_COMPACT_MULTI_MAP_OPT
- #define DEFINE_HASHED_SINGLE_FIXED_COMPACT_MULTI_MAP_NONOPT(xType) \
- template NUdf::TUnboxedValuePod THolderFactory::CreateDirectHashedSingleFixedCompactMultiMapHolder<xType, false> \
- (TValuesDictHashSingleFixedCompactMultiMap<xType>&& map, std::vector<ui64>&& nullPayloads, TPagedArena&& pool, TType* payloadType, \
- TComputationContext* ctx) const;
- KNOWN_PRIMITIVE_VALUE_TYPES(DEFINE_HASHED_SINGLE_FIXED_COMPACT_MULTI_MAP_NONOPT)
- #undef DEFINE_HASHED_SINGLE_FIXED_COMPACT_MULTI_MAP_NONOPT
- void GetDictionaryKeyTypes(const TType* keyType, TKeyTypes& types, bool& isTuple, bool& encoded, bool& useIHash, bool expandTuple) {
- isTuple = false;
- encoded = false;
- useIHash = false;
- types.clear();
- if (!keyType->IsPresortSupported()) {
- useIHash = true;
- return;
- }
- const bool isOptional = keyType->IsOptional();
- if (isOptional) {
- keyType = AS_TYPE(TOptionalType, keyType)->GetItemType();
- }
- if (expandTuple && keyType->IsTuple()) {
- auto tuple = AS_TYPE(TTupleType, keyType);
- for (ui32 i = 0; i < tuple->GetElementsCount(); ++i) {
- bool isOptional;
- auto unpacked = UnpackOptional(tuple->GetElementType(i), isOptional);
- if (!unpacked->IsData()) {
- encoded = true;
- break;
- }
- types.emplace_back(*AS_TYPE(TDataType, unpacked)->GetDataSlot(), isOptional);
- }
- if (!encoded) {
- isTuple = true;
- }
- } else if (keyType->IsData()) {
- types.emplace_back(*AS_TYPE(TDataType, keyType)->GetDataSlot(), isOptional);
- } else {
- encoded = true;
- }
- if (encoded) {
- types.clear();
- types.emplace_back(NUdf::EDataSlot::String, false);
- return;
- }
- }
- TPlainContainerCache::TPlainContainerCache() {
- Clear();
- }
- void TPlainContainerCache::Clear() {
- Cached.fill(NUdf::TUnboxedValue());
- CachedItems.fill(nullptr);
- }
- NUdf::TUnboxedValuePod TPlainContainerCache::NewArray(const THolderFactory& factory, ui64 size, NUdf::TUnboxedValue*& items) {
- if (!CachedItems[CacheIndex] || !Cached[CacheIndex].UniqueBoxed()) {
- CacheIndex ^= 1U;
- if (!CachedItems[CacheIndex] || !Cached[CacheIndex].UniqueBoxed()) {
- Cached[CacheIndex] = factory.CreateDirectArrayHolder(size, CachedItems[CacheIndex]);
- items = CachedItems[CacheIndex];
- return static_cast<const NUdf::TUnboxedValuePod&>(Cached[CacheIndex]);
- }
- }
- items = CachedItems[CacheIndex];
- std::fill_n(items, size, NUdf::TUnboxedValue());
- return static_cast<const NUdf::TUnboxedValuePod&>(Cached[CacheIndex]);
- }
- } // namespace NMiniKQL
- } // namespace NKikimr
|