1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899 |
- /*##############################################################################
- HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems.
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- ############################################################################## */
- #include "platform.h"
- #include "jlib.hpp"
- #include "hqlexpr.hpp"
- #include "hqlattr.hpp"
- #include "hqlmeta.hpp"
- #include "hqlutil.hpp"
- #include "hqlcpputil.hpp"
- #include "hqlthql.hpp"
- #include "hqlcatom.hpp"
- #include "hqlfold.hpp"
- #include "hqlcerrors.hpp"
- #include "hqltrans.ipp"
- #include "hqlpmap.hpp"
- #include "hqltcppc.ipp"
- #include "hqlttcpp.ipp"
- #include "hqlresource.ipp"
- #include "../../thorlcr/thorutil/thbufdef.hpp"
- #define MINIMAL_CHANGES
- #define MAX_INLINE_COMMON_COUNT 5
- //#define TRACE_RESOURCING
- //#define VERIFY_RESOURCING
- //#define SPOT_UNCONDITIONAL_CONDITIONS
- #define DEFAULT_MAX_SOCKETS 2000 // configurable by setting max_sockets in .ini
- #define DEFAULT_TOTAL_MEMORY ((1024*1024*1800))
- #define FIXED_CLUSTER_SIZE 400
- #define MEM_Const_Minimal (1*1024*1024)
- #define DEFAULT_MAX_ACTIVITIES 100
- static IHqlExpression * backtrackPseudoExpr;
- MODULE_INIT(INIT_PRIORITY_STANDARD)
- {
- backtrackPseudoExpr = createAttribute(retryAtom);
- return true;
- }
- MODULE_EXIT()
- {
- ::Release(backtrackPseudoExpr);
- }
- //=== The following provides information about how each kind of activity is resourced ====
- static void setHashResources(IHqlExpression * expr, CResources & resources, const CResourceOptions & options)
- {
- unsigned memneeded = MEM_Const_Minimal+DISTRIBUTE_RESMEM(resources.clusterSize);
- resources.set(RESslavememory, memneeded).set(REShashdist, 1);
- }
- //MORE: Use a single function to map an hqlexpression to an activity kind.
- void getResources(IHqlExpression * expr, CResources & resources, const CResourceOptions & options)
- {
- //MORE: What effect should a child query have? Definitely the following, but what about other resourcing?
- if (options.isChildQuery || queryHint(expr, lightweightAtom))
- {
- resources.setLightweight();
- return;
- }
- bool isLocal = isLocalActivity(expr);
- bool isGrouped = isGroupedActivity(expr);
- switch (expr->getOperator())
- {
- case no_join:
- case no_selfjoin:
- case no_denormalize:
- case no_denormalizegroup:
- if (isKeyedJoin(expr) || expr->hasAttribute(_lightweight_Atom))
- resources.setLightweight();
- else if (expr->hasAttribute(lookupAtom))
- {
- if (expr->hasAttribute(fewAtom))
- {
- resources.setLightweight().set(RESslavememory, MEM_Const_Minimal+LOOKUPJOINL_SMART_BUFFER_SIZE);
- resources.setManyToMasterSockets(1);
- }
- else
- {
- resources.setHeavyweight().set(RESslavememory, MEM_Const_Minimal+LOOKUPJOINL_SMART_BUFFER_SIZE);
- resources.setManyToMasterSockets(1);
- }
- }
- else if (expr->hasAttribute(smartAtom))
- {
- resources.setHeavyweight();
- setHashResources(expr, resources, options);
- }
- else if (expr->hasAttribute(hashAtom))
- {
- resources.setHeavyweight();
- setHashResources(expr, resources, options);
- }
- else
- {
- resources.setHeavyweight().set(RESslavememory, MEM_Const_Minimal+SORT_BUFFER_TOTAL+JOIN_SMART_BUFFER_SIZE);
- if (!isLocal)
- {
- #ifndef SORT_USING_MP
- resources.setManyToManySockets(2);
- #endif
- }
- }
- break;
- case no_dedup:
- if (isGrouped || (!expr->hasAttribute(allAtom) && !expr->hasAttribute(hashAtom)))
- {
- resources.setLightweight();
- if (!isGrouped && !isLocal)
- {
- resources.set(RESslavememory, MEM_Const_Minimal+DEDUP_SMART_BUFFER_SIZE);
- resources.setManyToMasterSockets(1);
- }
- }
- else if (isLocal)
- {
- resources.setHeavyweight().set(RESslavememory, MEM_Const_Minimal+DEDUP_SMART_BUFFER_SIZE);
- //This can't be right....
- //resources.setManyToMasterSockets(1);
- }
- else
- {
- //hash dedup.
- resources.setHeavyweight();
- setHashResources(expr, resources, options);
- }
- break;
- case no_rollup:
- resources.setLightweight();
- if (!isGrouped && !isLocal)
- {
- resources.set(RESslavememory, MEM_Const_Minimal+DEDUP_SMART_BUFFER_SIZE);
- //MORE: Is this still correct?
- resources.setManyToMasterSockets(1);
- }
- break;
- case no_distribute:
- case no_keyeddistribute:
- resources.setLightweight();
- setHashResources(expr, resources, options);
- break;
- case no_subsort:
- if (expr->hasAttribute(manyAtom))
- resources.setHeavyweight();
- else
- resources.setLightweight();
- break;
- case no_sort:
- if (isGrouped)
- {
- if (expr->hasAttribute(manyAtom))
- resources.setHeavyweight();
- else
- resources.setLightweight();
- }
- else if (expr->hasAttribute(fewAtom) && isLocal)
- resources.setLightweight();
- else if (isLocal)
- resources.setHeavyweight();
- else
- {
- resources.setHeavyweight();
- #ifndef SORT_USING_MP
- resources.setManyToManySockets(2);
- #endif
- }
- break;
- case no_topn:
- resources.setLightweight();
- break;
- case no_pipe:
- //surely it should be something like this.
- resources.setLightweight().set(RESslavesocket, 1);
- break;
- case no_table:
- {
- IHqlExpression * mode = expr->queryChild(2);
- if (mode && mode->getOperator() == no_pipe)
- {
- resources.setLightweight().set(RESslavesocket, 1);
- }
- else
- {
- resources.setLightweight();
- if (expr->hasAttribute(_workflowPersist_Atom) && expr->hasAttribute(distributedAtom))
- setHashResources(expr, resources, options); // may require a hash distribute
- }
- break;
- }
- case no_output:
- {
- IHqlExpression * filename = expr->queryChild(1);
- if (expr->hasAttribute(_spill_Atom))
- {
- //resources.setLightweight(); // assume no resources(!)
- }
- else if (filename && filename->getOperator() == no_pipe)
- {
- resources.setLightweight().set(RESslavesocket, 1);
- }
- else if (filename && !filename->isAttribute())
- {
- resources.setLightweight();
- }
- else
- {
- resources.setLightweight().set(RESslavememory, WORKUNITWRITE_SMART_BUFFER_SIZE);
- }
- break;
- }
- case no_distribution:
- resources.setLightweight().set(RESmastersocket, 16).set(RESslavesocket, 1);
- break;
- case no_aggregate:
- case no_newaggregate:
- {
- IHqlExpression * grouping = queryRealChild(expr, 3);
- if (grouping)
- {
- //Is this really correct???
- resources.setLightweight();
- setHashResources(expr, resources, options);
- }
- else
- {
- resources.setLightweight();
- //if (!isGrouped)
- // resources.set(RESmastersocket, 16).set(RESslavesocket, 1);
- }
- }
- break;
- case no_hqlproject:
- resources.setLightweight();
- //Add a flag onto count project to indicate it is a different variety.
- if (expr->hasAttribute(_countProject_Atom) && !isLocal)
- resources.set(RESslavememory, COUNTPROJECT_SMART_BUFFER_SIZE);
- break;
- case no_enth:
- resources.setLightweight();
- if (!isLocal)
- resources.set(RESslavememory, CHOOSESETS_SMART_BUFFER_SIZE);
- break;
- case no_metaactivity:
- if (expr->hasAttribute(pullAtom))
- resources.setLightweight().set(RESslavememory, PULL_SMART_BUFFER_SIZE);
- break;
- case no_setresult:
- case no_extractresult:
- case no_outputscalar:
- resources.setLightweight();//.set(RESmastersocket, 1).set(RESslavesocket, 1);
- break;
- case no_choosesets:
- resources.setLightweight();
- if (!isLocal || expr->hasAttribute(enthAtom) || expr->hasAttribute(lastAtom))
- resources.set(RESslavememory, CHOOSESETS_SMART_BUFFER_SIZE);
- break;
- case no_iterate:
- resources.setLightweight();
- if (!isGrouped && !isLocal)
- resources.setManyToMasterSockets(1).set(RESslavememory, ITERATE_SMART_BUFFER_SIZE);
- break;
- case no_choosen:
- resources.setLightweight().set(RESslavememory, FIRSTN_SMART_BUFFER_SIZE);
- break;
- case no_spill:
- case no_spillgraphresult:
- //assumed to take no resources;
- break;
- case no_addfiles:
- case no_merge:
- {
- resources.setLightweight();
- unsigned bufSize = FUNNEL_PERINPUT_BUFF_SIZE*expr->numChildren();
- if (bufSize < FUNNEL_MIN_BUFF_SIZE) bufSize = FUNNEL_MIN_BUFF_SIZE;
- resources.set(RESslavememory, MEM_Const_Minimal+bufSize);
- break;
- }
- case no_compound:
- //MORE: Should really be the total resources for the lhs... Really needs more thought.
- break;
- case no_libraryselect:
- //Do not allocate any resources for this, we don't want it to cause a spill under any circumstances
- break;
- case no_split:
- //Should really be included in the cost....
- default:
- resources.setLightweight();
- break;
- }
- }
- CResources & CResources::setLightweight()
- {
- return set(RESslavememory, 0x10000).set(RESactivities, 1);
- }
- CResources & CResources::setHeavyweight()
- {
- return set(RESslavememory, 0x100000).set(RESheavy, 1).set(RESactivities, 1);
- }
- //-------------------------------------------------------------------------------------------
- const char * queryResourceName(ResourceType kind)
- {
- switch (kind)
- {
- case RESslavememory: return "Slave Memory";
- case RESslavesocket: return "Slave Sockets";
- case RESmastermemory: return "Master Memory";
- case RESmastersocket: return "Master Sockets";
- case REShashdist: return "Hash Distributes";
- case RESheavy: return "Heavyweight";
- case RESactivities: return "Activities";
- }
- return "Unknown";
- }
- inline ResourcerInfo * queryResourceInfo(IHqlExpression * expr) { return (ResourcerInfo *)expr->queryBody()->queryTransformExtra(); }
- inline bool isResourcedActivity(IHqlExpression * expr)
- {
- if (!expr)
- return false;
- ResourcerInfo * extra = queryResourceInfo(expr);
- return extra && extra->isResourcedActivity();
- }
- bool isWorthForcingHoist(IHqlExpression * expr)
- {
- loop
- {
- switch (expr->getOperator())
- {
- case no_selectnth:
- case no_filter:
- case no_newaggregate:
- if (isResourcedActivity(expr->queryChild(0)))
- return true;
- break;
- default:
- return false;
- }
- expr = expr->queryChild(0);
- }
- }
- void CResources::add(const CResources & other)
- {
- for (unsigned i = 0; i < RESmax; i++)
- resource[i] += other.resource[i];
- }
- bool CResources::addExceeds(const CResources & other, const CResources & limit) const
- {
- for (unsigned i = 0; i < RESmax; i++)
- {
- if (resource[i] + other.resource[i] > limit.resource[i])
- {
- //DBGLOG("Cannot merge because limit for %s exceeded (%d cf %d)", queryResourceName((ResourceType)i), resource[i] + other.resource[i], limit.resource[i]);
- return true;
- }
- }
- return false;
- }
- StringBuffer & CResources::getExceedsReason(StringBuffer & reasonText, const CResources & other, const CResources & limit) const
- {
- bool first = true;
- for (unsigned i = 0; i < RESmax; i++)
- {
- if (resource[i] + other.resource[i] > limit.resource[i])
- {
- if (!first) reasonText.append(", ");
- first = false;
- reasonText.appendf("%s (%d>%d)", queryResourceName((ResourceType)i), resource[i] + other.resource[i], limit.resource[i]);
- }
- }
- return reasonText;
- }
- bool CResources::exceeds(const CResources & limit) const
- {
- for (unsigned i = 0; i < RESmax; i++)
- {
- if (resource[i] > limit.resource[i])
- return true;
- }
- return false;
- }
- void CResources::maximize(const CResources & other)
- {
- for (unsigned i = 0; i < RESmax; i++)
- {
- if (resource[i] < other.resource[i])
- resource[i] = other.resource[i];
- }
- }
- CResources & CResources::setManyToMasterSockets(unsigned numSockets)
- {
- set(RESslavesocket, numSockets);
- set(RESmastersocket, numSockets * clusterSize);
- return *this;
- }
- CResources & CResources::setManyToManySockets(unsigned numSockets)
- {
- return set(RESslavesocket, numSockets * clusterSize);
- }
- void CResources::sub(const CResources & other)
- {
- for (unsigned i = 0; i < RESmax; i++)
- resource[i] -= other.resource[i];
- }
- //===========================================================================
- inline bool isAffectedByResourcing(IHqlExpression * expr)
- {
- switch (expr->getOperator())
- {
- case no_record:
- case no_constant:
- case no_attr:
- return false;
- }
- return true;
- }
- bool isSimpleAggregateResult(IHqlExpression * expr)
- {
- //MORE: no_extractresult is really what is meant
- if (expr->getOperator() != no_extractresult)
- return false;
- IHqlExpression * value = expr->queryChild(0);
- if (value->getOperator() != no_datasetfromrow)
- return false;
- //MORE: This currently doesn't hoist selects from nested records, but not sure there is a syntax to do that either.
- IHqlExpression * ds = value->queryChild(0);
- if (!isSelectFirstRow(ds))
- return false;
- ds = ds->queryChild(0);
- if (ds->getOperator() != no_newaggregate)
- return false;
- return true;
- }
- bool lightweightAndReducesDatasetSize(IHqlExpression * expr)
- {
- switch (expr->getOperator())
- {
- case no_hqlproject:
- case no_newusertable:
- return reducesRowSize(expr);
- case no_dedup:
- if (isGroupedActivity(expr) || (!expr->hasAttribute(allAtom) && !expr->hasAttribute(hashAtom)))
- return true;
- break;
- case no_aggregate:
- case no_newaggregate:
- {
- IHqlExpression * grouping = queryRealChild(expr, 3);
- if (grouping)
- return false;
- return true;
- }
- case no_rollupgroup:
- case no_rollup:
- case no_choosen:
- case no_choosesets:
- case no_enth:
- case no_sample:
- case no_filter:
- case no_limit:
- case no_filtergroup:
- return true;
- case no_group:
- //removing grouping will reduce size of the spill file.
- if (!isGrouped(expr))
- return true;
- break;
- }
- return false;
- }
- bool heavyweightAndReducesSizeOrSkew(IHqlExpression * expr)
- {
- switch (expr->getOperator())
- {
- case no_aggregate:
- case no_newaggregate:
- {
- //more; hash aggregate?
- break;
- }
- case no_distribute:
- return true;
- }
- return false;
- }
- //---------------------------------------------------------------------------
- IHqlExpression * CResourceOptions::createSpillName(bool isGraphResult)
- {
- if (isGraphResult)
- return getSizetConstant(nextResult++);
- StringBuffer s;
- s.append("~spill::");
- appendUniqueId(s, spillSequence.next());
- return createConstant(s.str());
- }
- //---------------------------------------------------------------------------
- IHqlExpression * appendUniqueAttr(IHqlExpression * expr)
- {
- return replaceOwnedAttribute(expr, createUniqueId());
- }
- bool queryAddUniqueToActivity(IHqlExpression * expr)
- {
- if (!isSourceActivity(expr))
- return false;
- switch (expr->getOperator())
- {
- case no_workunit_dataset:
- case no_getgraphresult:
- case no_getgraphloopresult:
- case no_xmlproject:
- case no_datasetfromrow:
- case no_datasetfromdictionary:
- case no_rows:
- case no_allnodes:
- case no_thisnode:
- case no_select: // it can get lost, and that then causes inconsistent trees.
- return false;
- }
- return true;
- }
- //---------------------------------------------------------------------------
- ResourceGraphLink::ResourceGraphLink(ResourceGraphInfo * _sourceGraph, IHqlExpression * _sourceNode, ResourceGraphInfo * _sinkGraph, IHqlExpression * _sinkNode, LinkKind _linkKind)
- {
- assertex(_sourceGraph);
- sourceGraph.set(_sourceGraph);
- sourceNode.set(_sourceNode);
- sinkGraph.set(_sinkGraph);
- sinkNode.set(_sinkNode);
- assertex(!sinkGraph || sinkNode);
- linkKind = _linkKind;
- trace("create");
- }
- ResourceGraphLink::~ResourceGraphLink()
- {
- trace("delete");
- }
- void ResourceGraphLink::changeSourceGraph(ResourceGraphInfo * newGraph)
- {
- sourceGraph->sinks.zap(*this);
- sourceGraph.set(newGraph);
- newGraph->sinks.append(*this);
- trace("change source");
- }
- void ResourceGraphLink::changeSinkGraph(ResourceGraphInfo * newGraph)
- {
- sinkGraph->sources.zap(*this);
- sinkGraph.set(newGraph);
- newGraph->sources.append(*this);
- trace("change sink");
- }
- bool ResourceGraphLink::isRedundantLink()
- {
- ResourcerInfo * info = queryResourceInfo(sourceNode);
- return info->expandRatherThanSpill(false);
- }
- void ResourceGraphLink::trace(const char * name)
- {
- #ifdef TRACE_RESOURCING
- PrintLog("%s: %lx source(%lx,%lx) sink(%lx,%lx) %s", name, this, sourceGraph.get(), sourceNode->queryBody(), sinkGraph.get(), sinkNode ? sinkNode->queryBody() : NULL,
- linkKind == SequenceLink ? "sequence" : "");
- #endif
- }
- ResourceGraphDependencyLink::ResourceGraphDependencyLink(ResourceGraphInfo * _sourceGraph, IHqlExpression * _sourceNode, ResourceGraphInfo * _sinkGraph, IHqlExpression * _sinkNode)
- : ResourceGraphLink(_sourceGraph, _sourceNode, _sinkGraph, _sinkNode, UnconditionalLink)
- {
- }
- void ResourceGraphDependencyLink::changeSourceGraph(ResourceGraphInfo * newGraph)
- {
- sourceGraph.set(newGraph);
- trace("change source");
- }
- void ResourceGraphDependencyLink::changeSinkGraph(ResourceGraphInfo * newGraph)
- {
- sinkGraph.set(newGraph);
- newGraph->dependsOn.append(*this);
- trace("change sink");
- }
- //---------------------------------------------------------------------------
- ResourceGraphInfo::ResourceGraphInfo(CResourceOptions * _options) : resources(_options->clusterSize)
- {
- options = _options;
- depth = 0;
- depthSequence = -1;
- beenResourced = false;
- isUnconditional = false;
- mergedConditionSource = false;
- hasConditionSource = false;
- hasSequentialSource = false;
- hasRootActivity = false;
- isDead = false;
- startedGeneratingResourced = false;
- inheritedExpandedDependencies = false;
- cachedDependent.other = NULL;
- cachedDependent.ignoreSources = false;
- cachedDependent.updateSequence = 0;
- cachedDependent.value = false;
- }
- ResourceGraphInfo::~ResourceGraphInfo()
- {
- }
- bool ResourceGraphInfo::addCondition(IHqlExpression * condition)
- {
- if (conditions.find(*condition) == NotFound)
- {
- conditions.append(*LINK(condition));
- #ifdef SPOT_UNCONDITIONAL_CONDITIONS
- IAtom * name = condition->queryName();
- IAtom * invName = NULL;
- if (name == trueAtom)
- invName = falseAtom;
- else if (name == falseAtom)
- invName = trueAtom;
-
- if (invName)
- {
- IHqlExpression * parent = condition->queryChild(1);
- OwnedHqlExpr invTag = createAttribute(invName, LINK(condition->queryChild(0)), LINK(parent));
- if (conditions.find(*invTag) != NotFound)
- {
- if (!parent)
- return true;
- return addCondition(parent);
- }
- }
- #endif
- }
- return false;
- }
- bool ResourceGraphInfo::isSharedInput(IHqlExpression * expr)
- {
- IHqlExpression * body = expr->queryBody();
- if (unbalancedExternalSources.contains(*body))
- return false;
- if (queryResourceInfo(expr)->expandRatherThanSplit())
- return false;
- unsigned numUses = 0;
- ForEachItemIn(i, balancedExternalSources)
- {
- if (body == &balancedExternalSources.item(i))
- numUses++;
- }
- //NumUses could be zero if an input should be expanded, and that input is shared by another graph which also expands
- //the input. E.g. project(meta(diskread).
- return numUses > 1;
- }
- void ResourceGraphInfo::addSharedInput(IHqlExpression * expr, IHqlExpression * mapped)
- {
- sharedInputs.append(*LINK(expr));
- sharedInputs.append(*LINK(mapped));
- }
- IHqlExpression * ResourceGraphInfo::queryMappedSharedInput(IHqlExpression * expr)
- {
- unsigned max = sharedInputs.ordinality();
- for (unsigned i=0; i < max; i+= 2)
- {
- if (expr == &sharedInputs.item(i))
- return &sharedInputs.item(i+1);
- }
- return NULL;
- }
- bool ResourceGraphInfo::allocateResources(const CResources & value, const CResources & limit)
- {
- if (resources.addExceeds(value, limit))
- return false;
- resources.add(value);
- return true;
- }
- bool ResourceGraphInfo::canMergeActionAsSibling(bool sequential) const
- {
- //sequential requires root actions in separate graphs
- if (sequential && hasRootActivity)
- return false;
- //Dependent actions need to be in a separate graph if this flag is set.
- if (options->actionLinkInNewGraph && !hasRootActivity)
- return false;
- return true;
- }
- bool ResourceGraphInfo::containsActiveSinks() const
- {
- ForEachItemIn(idx, sinks)
- {
- ResourcerInfo * info = queryResourceInfo(sinks.item(idx).sourceNode);
- if (!info->expandRatherThanSpill(false))
- return true;
- }
- return false;
- }
- bool ResourceGraphInfo::containsActionSink() const
- {
- ForEachItemIn(idx, sinks)
- {
- IHqlExpression * sink = sinks.item(idx).sourceNode;
- if (sink->isAction())
- return true;
- }
- return false;
- }
- void ResourceGraphInfo::display()
- {
- StringBuffer s;
- s.appendf("graph %p src(", this);
- ForEachItemIn(idxs, sources)
- s.appendf("%p ", sources.item(idxs).sourceGraph.get());
- s.append(") dep(");
- ForEachItemIn(idxd, dependsOn)
- s.appendf("%p ", dependsOn.item(idxd).sourceGraph.get());
- s.append(")");
- if (isUnconditional)
- s.append(" <unconditional>");
- DBGLOG("%s", s.str());
- }
- void ResourceGraphInfo::getMergeFailReason(StringBuffer & reasonText, ResourceGraphInfo * otherGraph, const CResources & limit)
- {
- resources.getExceedsReason(reasonText, otherGraph->resources, limit);
- }
- unsigned ResourceGraphInfo::getDepth()
- {
- //If no graphs have been merged since this was last called it is still valid.
- if (depthSequence == options->state.updateSequence)
- return depth;
- depthSequence = options->state.updateSequence;
- depth = 0;
- ForEachItemIn(idx, dependsOn)
- {
- ResourceGraphInfo * source = dependsOn.item(idx).sourceGraph;
- if (source->getDepth() >= depth)
- depth = source->getDepth() + 1;
- }
- ForEachItemIn(idx2, sources)
- {
- ResourceGraphInfo * source = sources.item(idx2).sourceGraph;
- if (source->getDepth() >= depth)
- depth = source->getDepth() + 1;
- }
- return depth;
- }
- bool ResourceGraphInfo::hasSameConditions(ResourceGraphInfo & other)
- {
- if (conditions.ordinality() != other.conditions.ordinality())
- return false;
- ForEachItemIn(i, conditions)
- if (other.conditions.find(conditions.item(i)) == NotFound)
- return false;
- return true;
- }
- bool ResourceGraphInfo::evalDependentOn(ResourceGraphInfo & other, bool ignoreSources)
- {
- ForEachItemIn(idx, dependsOn)
- {
- ResourceGraphInfo * cur = dependsOn.item(idx).sourceGraph;
- if (cur == &other)
- return true;
- if (cur->isDependentOn(other, false))
- return true;
- }
- ForEachItemIn(idx2, sources)
- {
- ResourceGraphInfo * cur = sources.item(idx2).sourceGraph;
- if ((cur == &other) && !ignoreSources)
- return true;
- if (cur->isDependentOn(other, false))
- return true;
- }
- return false;
- }
- bool ResourceGraphInfo::isDependentOn(ResourceGraphInfo & other, bool ignoreSources)
- {
- //Cache the last query so that traversal doesn't convert a graph into a tree walk
- if ((cachedDependent.other == &other) && (cachedDependent.ignoreSources == ignoreSources) &&
- (cachedDependent.updateSequence == options->state.updateSequence))
- return cachedDependent.value;
- if (getDepth() <= other.getDepth())
- return false;
- cachedDependent.other = &other;
- cachedDependent.ignoreSources = ignoreSources;
- cachedDependent.updateSequence = options->state.updateSequence;
- cachedDependent.value = evalDependentOn(other, ignoreSources);
- return cachedDependent.value;
- }
- bool ResourceGraphInfo::isVeryCheap()
- {
- if (sinks.ordinality() != 1)
- return false;
- IHqlExpression * sourceNode = sinks.item(0).sourceNode;
- if (isSimpleAggregateResult(sourceNode))
- return true;
- // Not sure about the following....
- // if (sourceNode->getOperator() == no_setresult)
- // return true;
- //Could be other examples...
- return false;
- }
- bool ResourceGraphInfo::mergeInSource(ResourceGraphInfo & other, const CResources & limit)
- {
- bool mergeConditions = false;
- if (!isUnconditional)
- {
- //if it is conditional and it is very cheap then it is still more efficient to merge
- //rather than read from a spill file.
- if (other.isUnconditional || !hasSameConditions(other))
- {
- if ((hasConditionSource || !isVeryCheap()) && (other.sinks.ordinality() != 1))
- return false;
- mergeConditions = true;
- }
- }
- if (isDependentOn(other, true))
- return false;
- if (!options->canSplit())
- {
- //Don't merge two graphs that will cause a splitter to be created
- //Either already used internally, or an output will be merged twice
- HqlExprArray mergeNodes;
- ForEachItemIn(idx, sources)
- {
- ResourceGraphLink & cur = sources.item(idx);
- if (cur.sourceGraph == &other)
- {
- IHqlExpression * sourceNode = cur.sourceNode->queryBody();
- ResourcerInfo * info = queryResourceInfo(sourceNode);
- if ((info->numInternalUses() != 0) || (mergeNodes.find(*sourceNode) != NotFound) || info->preventMerge())
- return false;
- mergeNodes.append(*LINK(sourceNode));
- }
- }
- }
- if (options->checkResources() && !allocateResources(other.resources, limit))
- return false;
- if (hasSequentialSource && other.hasSequentialSource)
- return false;
- mergeGraph(other, mergeConditions);
- return true;
- }
- void ResourceGraphInfo::mergeGraph(ResourceGraphInfo & other, bool mergeConditions)
- {
- #ifdef TRACE_RESOURCING
- DBGLOG("Merging%s source into%s sink", other.isUnconditional ? "" : " conditional", isUnconditional ? "" : " conditional");
- other.display();
- display();
- PrintLog("Merge %p into %p", &other, this);
- #endif
- if (other.hasConditionSource)
- hasConditionSource = true;
- if (other.hasSequentialSource)
- {
- assertex(!hasSequentialSource);
- hasSequentialSource = true;
- }
- //Recalculate the dependents, because sources of the source merged in may no longer be indirect
- //although they may be via another path.
- options->noteGraphsChanged();
- //If was very cheap and merged into an unconditional graph, make sure this is now tagged as
- //unconditional...
- if (other.isUnconditional)
- isUnconditional = true;
- //We need to stop spills being an arm of a conditional branch - otherwise they won't get executed.
- //so see if we have merged any conditional branches in
- if (other.mergedConditionSource)
- mergedConditionSource = true;
- if (mergeConditions)
- {
- //Replace conditions with those of parent. Only called when a very simple graph is
- //merged in, so replace conditions instead of merging
- conditions.kill();
- ForEachItemIn(i, other.conditions)
- conditions.append(OLINK(other.conditions.item(i)));
- }
-
- //sources and sinks are updated elsewhere...
- }
- bool ResourceGraphInfo::mergeInSibling(ResourceGraphInfo & other, const CResources & limit)
- {
- if ((!isUnconditional || !other.isUnconditional) && !hasSameConditions(other))
- return false;
- if (hasSequentialSource && other.hasSequentialSource)
- return false;
- if (isDependentOn(other, false) || other.isDependentOn(*this, false))
- return false;
- if (options->checkResources() && !allocateResources(other.resources, limit))
- return false;
- mergeGraph(other, false);
- return true;
- }
- void ResourceGraphInfo::removeResources(const CResources & value)
- {
- resources.sub(value);
- }
- //---------------------------------------------------------------------------
- unsigned ChildDependentArray::findOriginal(IHqlExpression * expr)
- {
- ForEachItem(i)
- {
- if (item(i).original == expr)
- return i;
- }
- return NotFound;
- }
- //---------------------------------------------------------------------------
- static void appendCloneAttribute(HqlExprArray & args, IHqlExpression * expr, IAtom * name)
- {
- IHqlExpression * prop = expr->queryAttribute(name);
- if (prop)
- args.append(*LINK(prop));
- }
- ResourcerInfo::ResourcerInfo(IHqlExpression * _original, CResourceOptions * _options)
- {
- original.set(_original);
- numUses = 0;
- numExternalUses = 0;
- gatheredDependencies = false;
- containsActivity = false;
- isActivity = false;
- isRootActivity = false;
- transformed = NULL;
- conditionSourceCount = 0;
- pathToExpr = PathUnknown;
- outputToUseForSpill = NULL;
- isAlreadyInScope = false;
- isSpillPoint = false;
- options = _options;
- balanced = true;
- currentSource = 0;
- linkedFromChild = false;
- forceHoist = false;
- neverSplit = false;
- isConditionalFilter = false;
- projectResult = true; // projected must also be non empty to actually project this dataset
- visited = false;
- lastPass = 0;
- balancedExternalUses = 0;
- #ifdef TRACE_BALANCED
- balanceId = 0;
- #endif
- }
- void ResourcerInfo::setConditionSource(IHqlExpression * condition, bool isFirst)
- {
- if (isFirst)
- {
- conditionSourceCount++;
- graph->hasConditionSource = true;
- }
- }
- bool ResourcerInfo::addCondition(IHqlExpression * condition)
- {
- conditions.append(*LINK(condition));
- return graph->addCondition(condition);
- }
- void ResourcerInfo::addProjected(IHqlExpression * next)
- {
- if (projectResult && !projected.contains(*next))
- projected.append(*LINK(next));
- }
- void ResourcerInfo::clearProjected()
- {
- projectResult = false;
- projected.kill();
- }
- void ResourcerInfo::addSpillFlags(HqlExprArray & args, bool isRead)
- {
- if (isGrouped(original))
- args.append(*createAttribute(groupedAtom));
- if (outputToUseForSpill)
- {
- assertex(isRead);
- appendCloneAttribute(args, outputToUseForSpill, __compressed__Atom);
- appendCloneAttribute(args, outputToUseForSpill, jobTempAtom);
- appendCloneAttribute(args, outputToUseForSpill, _spill_Atom);
- }
- else
- {
- args.append(*createAttribute(__compressed__Atom));
- args.append(*createAttribute(_spill_Atom));
- args.append(*createAttribute(_noReplicate_Atom));
- if (options->targetRoxie())
- args.append(*createAttribute(_noAccess_Atom));
- }
- if (isRead)
- {
- args.append(*createAttribute(_noVirtual_Atom)); // Don't interpret virtual fields as virtual...
- if (!original->isDataset() || hasSingleRow(original))
- args.append(*createAttribute(rowAtom)); // add a flag so the selectnth[1] can be removed later...
- }
- else
- {
- unsigned numUses = numExternalUses;
- if (false) // options->cloneConditionals)
- {
- //Remove all the links from conditional graphs...
- ForEachItemIn(i1, graph->sinks)
- {
- ResourceGraphLink & cur = graph->sinks.item(i1);
- if ((cur.sourceNode == original) && !cur.sinkGraph->isUnconditional)
- numUses--;
- }
- numUses += calcNumConditionalUses();
- }
- args.append(*createAttribute(_tempCount_Atom, getSizetConstant(numUses)));
- }
- }
- bool ResourcerInfo::alwaysExpand()
- {
- return (original->getOperator() == no_mapto);
- }
- static IHqlExpression * stripTrueFalse(IHqlExpression * expr)
- {
- if (expr->queryName() == instanceAtom)
- {
- IHqlExpression * parent = expr->queryChild(2);
- if (parent)
- parent = stripTrueFalse(parent);
- return createAttribute(instanceAtom, LINK(expr->queryChild(0)), LINK(expr->queryChild(1)), parent);
- }
- else
- {
- IHqlExpression * parent = expr->queryChild(1);
- if (parent && parent->isAttribute())
- parent = stripTrueFalse(parent);
- return createAttribute(tempAtom, LINK(expr->queryChild(0)), parent);
- }
- }
- unsigned ResourcerInfo::calcNumConditionalUses()
- {
- //for thor and hthor, where the conditional graph is cloned, the maximum number of times it can be read
- //is 1 for a shared conditional graph, or once for each combination of conditions it is used as the
- //input for. However if it is used more than once in a single branch of a condition it needs to be counted
- //several times.
- //It is always better to overestimate than underestimate. (But even better to get it right.)
- HqlExprArray uniqueConditions;
- UnsignedArray uniqueCounts;
- ForEachItemIn(idx, conditions)
- {
- IHqlExpression & cur = conditions.item(idx);
- OwnedHqlExpr unique = stripTrueFalse(&cur);
- unsigned numOccurences = 0;
- ForEachItemIn(j, conditions)
- if (&conditions.item(j) == &cur)
- numOccurences++;
- unsigned match = uniqueConditions.find(*unique);
- if (match == NotFound)
- {
- uniqueConditions.append(*unique.getClear());
- uniqueCounts.append(numOccurences);
- }
- else
- {
- if (uniqueCounts.item(match) < numOccurences)
- uniqueCounts.replace(numOccurences, match);
- }
- }
- unsigned condUses = 0;
- ForEachItemIn(k, uniqueCounts)
- condUses += uniqueCounts.item(k);
- return condUses;
- }
- // Each aggregate has form. Setresult(select(selectnth(aggregate...,1),field),name,seq)
- IHqlExpression * ResourcerInfo::createAggregation(IHqlExpression * expr)
- {
- LinkedHqlExpr transformed = expr;
- ForEachItemIn(idx, aggregates)
- {
- IHqlExpression & cur = aggregates.item(idx);
- IHqlExpression * row2ds = cur.queryChild(0);
- IHqlExpression * selectnth = row2ds->queryChild(0);
- IHqlExpression * aggregate = selectnth->queryChild(0);
- IHqlExpression * aggregateRecord = aggregate->queryChild(1);
- assertex(aggregate->getOperator() == no_newaggregate);
- HqlExprArray aggargs, setargs;
- //Through aggregate has form (dataset, record, transform, list of set results);
- aggargs.append(*LINK(transformed));
- aggargs.append(*LINK(aggregateRecord));
- IHqlExpression * mapped = replaceSelector(aggregate->queryChild(2), original, expr->queryNormalizedSelector());
- aggargs.append(*mapped);
- OwnedHqlExpr active = createDataset(no_anon, LINK(aggregateRecord), NULL);
- OwnedHqlExpr mappedSelect = replaceSelector(cur.queryChild(1), row2ds, active);
- setargs.append(*LINK(active));
- setargs.append(*LINK(mappedSelect));
- unwindChildren(setargs, &cur, 1);
- aggargs.append(*createValue(no_extractresult, makeVoidType(), setargs));
- transformed.setown(createDataset(no_throughaggregate, aggargs));
- }
- return transformed.getClear();
- }
- bool ResourcerInfo::useGraphResult()
- {
- if (options->useResultsForChildSpills && linkedFromChild)
- return true;
- if (options->alwaysUseGraphResults)
- return true;
- if (!options->useGraphResults)
- return false;
- if (linkedFromChild)
- return true;
- //Roxie converts spills into splitters, so best to retain them
- if (options->targetClusterType == RoxieCluster)
- return false;
- return true;
- }
- bool ResourcerInfo::useGlobalResult()
- {
- return (linkedFromChild && !useGraphResult());
- }
- IHqlExpression * ResourcerInfo::createSpillName()
- {
- if (outputToUseForSpill)
- return LINK(outputToUseForSpill->queryChild(1));
- if (!spillName)
- {
- if (useGraphResult())
- spillName.setown(options->createSpillName(true));
- else if (useGlobalResult())
- {
- StringBuffer valueText;
- appendUniqueId(valueText.append("spill"), options->spillSequence.next());
- spillName.setown(createConstant(valueText.str()));
- }
- else
- spillName.setown(options->createSpillName(false));
- }
- return LINK(spillName);
- }
- IHqlExpression * ResourcerInfo::createSpilledRead(IHqlExpression * spillReason)
- {
- OwnedHqlExpr dataset;
- HqlExprArray args;
- IHqlExpression * record = original->queryRecord();
- bool loseDistribution = true;
- if (useGraphResult())
- {
- if (spilledDataset)
- args.append(*LINK(spilledDataset));
- else
- args.append(*LINK(record));
- args.append(*LINK(options->graphIdExpr));
- args.append(*createSpillName());
- if (isGrouped(original))
- args.append(*createAttribute(groupedAtom));
- if (!original->isDataset())
- args.append(*createAttribute(rowAtom));
- args.append(*createAttribute(_spill_Atom));
- IHqlExpression * recordCountAttr = queryRecordCountInfo(original);
- if (recordCountAttr)
- args.append(*LINK(recordCountAttr));
- if (options->targetThor() && original->isDataset() && !options->isChildQuery)
- args.append(*createAttribute(_distributed_Atom));
- node_operator readOp = spilledDataset ? no_readspill : no_getgraphresult;
- if (original->isDatarow())
- dataset.setown(createRow(readOp, args));
- else if (original->isDictionary())
- dataset.setown(createDictionary(readOp, args));
- else
- dataset.setown(createDataset(readOp, args));
- loseDistribution = false;
- }
- else if (useGlobalResult())
- {
- args.append(*LINK(record));
- args.append(*createAttribute(nameAtom, createSpillName()));
- args.append(*createAttribute(sequenceAtom, getLocalSequenceNumber()));
- addSpillFlags(args, true);
- IHqlExpression * recordCountAttr = queryRecordCountInfo(original);
- if (recordCountAttr)
- args.append(*LINK(recordCountAttr));
- if (original->isDictionary())
- dataset.setown(createDictionary(no_workunit_dataset, args));
- else
- dataset.setown(createDataset(no_workunit_dataset, args));
- }
- else
- {
- if (spilledDataset)
- {
- args.append(*LINK(spilledDataset));
- args.append(*createSpillName());
- }
- else
- {
- args.append(*createSpillName());
- args.append(*LINK(record));
- }
- args.append(*createValue(no_thor));
- addSpillFlags(args, true);
- args.append(*createUniqueId());
- if (options->isChildQuery && options->targetRoxie())
- {
- args.append(*createAttribute(_colocal_Atom));
- args.append(*createLocalAttribute());
- }
- if (spillReason)
- args.append(*LINK(spillReason));
- if (spilledDataset)
- dataset.setown(createDataset(no_readspill, args));
- else
- {
- IHqlExpression * recordCountAttr = queryRecordCountInfo(original);
- if (recordCountAttr)
- args.append(*LINK(recordCountAttr));
- dataset.setown(createDataset(no_table, args));
- }
- loseDistribution = false;
- }
- dataset.setown(preserveTableInfo(dataset, original, loseDistribution, NULL));
- return wrapRowOwn(dataset.getClear());
- }
- IHqlExpression * ResourcerInfo::createSpilledWrite(IHqlExpression * transformed)
- {
- assertex(!outputToUseForSpill);
- HqlExprArray args;
- if (useGraphResult())
- {
- assertex(options->graphIdExpr);
- if (options->createSpillAsDataset && !linkedFromChild)
- {
- IHqlExpression * value = LINK(transformed);
- if (value->isDatarow())
- value = createDatasetFromRow(value);
- spilledDataset.setown(createDataset(no_commonspill, value));
- args.append(*LINK(spilledDataset));
- }
- else
- args.append(*LINK(transformed));
- args.append(*LINK(options->graphIdExpr));
- args.append(*createSpillName());
- args.append(*createAttribute(_spill_Atom));
- if (linkedFromChild)
- args.append(*createAttribute(_accessedFromChild_Atom));
- if (spilledDataset)
- return createValue(no_writespill, makeVoidType(), args);
- return createValue(no_setgraphresult, makeVoidType(), args);
- }
- else if (useGlobalResult())
- {
- args.append(*LINK(transformed));
- args.append(*createAttribute(sequenceAtom, getLocalSequenceNumber()));
- args.append(*createAttribute(namedAtom, createSpillName()));
- addSpillFlags(args, false);
- return createValue(no_output, makeVoidType(), args);
- }
- else
- {
- if (options->createSpillAsDataset)
- {
- IHqlExpression * value = LINK(transformed);
- if (value->isDatarow())
- value = createDatasetFromRow(value);
- spilledDataset.setown(createDataset(no_commonspill, value));
- args.append(*LINK(spilledDataset));
- }
- else
- args.append(*LINK(transformed));
- args.append(*createSpillName());
- addSpillFlags(args, false);
- if (options->createSpillAsDataset)
- return createValue(no_writespill, makeVoidType(), args);
- return createValue(no_output, makeVoidType(), args);
- }
- }
- bool ResourcerInfo::okToSpillThrough()
- {
- bool isGraphResult = useGraphResult();
- if (isGraphResult)
- return options->allowThroughResult;
- if (useGlobalResult())
- return false;
- return (options->allowThroughSpill && !options->createSpillAsDataset);
- }
- void ResourcerInfo::noteUsedFromChild(bool _forceHoist)
- {
- linkedFromChild = true;
- outputToUseForSpill = NULL;
- if (_forceHoist)
- forceHoist = true;
- }
- unsigned ResourcerInfo::numInternalUses()
- {
- return numUses - numExternalUses - aggregates.ordinality();
- }
- void ResourcerInfo::resetBalanced()
- {
- // don't reset balanced since it may be set for an external result
- balancedLinks.kill();
- curBalanceLink = 0;
- balancedVisiting = false;
- balancedExternalUses = 0;
- }
- bool ResourcerInfo::spillSharesSplitter()
- {
- if (outputToUseForSpill || useGraphResult() || useGlobalResult())
- return false;
- if (okToSpillThrough())
- return false;
- if (!isSplit() || !balanced)
- return false;
- return true;
- }
- void ResourcerInfo::setRootActivity()
- {
- isRootActivity = true;
- }
- IHqlExpression * ResourcerInfo::createSpiller(IHqlExpression * transformed, bool reuseSplitter)
- {
- if (outputToUseForSpill)
- return LINK(transformed);
- if (!okToSpillThrough())
- {
- OwnedHqlExpr split;
- if (reuseSplitter)
- {
- assertex(transformed->getOperator() == no_split);
- split.set(transformed);
- }
- else
- {
- if (transformed->isDataset())
- split.setown(createDataset(no_split, LINK(transformed), createComma(createAttribute(balancedAtom), createUniqueId())));
- else
- split.setown(createRow(no_split, LINK(transformed), createComma(createAttribute(balancedAtom), createUniqueId())));
- split.setown(cloneInheritedAnnotations(original, split));
- }
- splitterOutput.setown(createSpilledWrite(split));
- return split.getClear();
- }
- HqlExprArray args;
- node_operator op;
- args.append(*LINK(transformed));
- if (useGraphResult())
- {
- op = no_spillgraphresult;
- args.append(*LINK(options->graphIdExpr));
- args.append(*createSpillName());
- args.append(*createAttribute(_spill_Atom));
- }
- else
- {
- op = no_spill;
- args.append(*createSpillName());
- addSpillFlags(args, false);
- }
- OwnedHqlExpr spill;
- if (original->isDatarow())
- spill.setown(createRow(op, args));
- else
- spill.setown(createDataset(op, args));
- return cloneInheritedAnnotations(original, spill);
- }
- IHqlExpression * ResourcerInfo::createSplitter(IHqlExpression * transformed)
- {
- if (transformed->getOperator() == no_libraryscopeinstance)
- return LINK(transformed);
- IHqlExpression * attr = createUniqueId();
- if (balanced)
- attr = createComma(attr, createAttribute(balancedAtom));
- OwnedHqlExpr split;
- if (transformed->isDataset())
- split.setown(createDataset(no_split, LINK(transformed), attr));
- else
- split.setown(createRow(no_split, LINK(transformed), attr));
- return cloneInheritedAnnotations(original, split);
- }
- IHqlExpression * ResourcerInfo::createTransformedExpr(IHqlExpression * expr)
- {
- LinkedHqlExpr transformed = expr;
- if (aggregates.ordinality())
- transformed.setown(createAggregation(transformed));
- if (spillSharesSplitter())
- {
- transformed.setown(createSplitter(transformed));
- if (isExternalSpill())
- transformed.setown(createSpiller(transformed, true));
- }
- else
- {
- if (isExternalSpill())
- transformed.setown(createSpiller(transformed, false));
- if (isSplit())
- transformed.setown(createSplitter(transformed));
- else if (isSpilledWrite())
- transformed.setown(createSpilledWrite(transformed));
- }
- return transformed.getClear();
- }
- bool ResourcerInfo::expandRatherThanSpill(bool noteOtherSpills)
- {
- if (options->shareDontExpand)
- return expandRatherThanSplit();
- IHqlExpression * expr = original;
- if (!options->minimiseSpills || linkedFromChild)
- noteOtherSpills = false;
- if (noteOtherSpills)
- {
- ResourcerInfo * info = queryResourceInfo(expr);
- if (info && info->isSpilledWrite())
- return (info->transformed == NULL);
- }
- bool isFiltered = false;
- bool isProcessed = false;
- loop
- {
- ResourcerInfo * info = queryResourceInfo(expr);
- if (info && info->neverSplit)
- return true;
- if (info && info->forceHoist)
- return false;
- node_operator op = expr->getOperator();
- switch (op)
- {
- case no_table:
- {
- //This is only executed for hthor/thor. Roxie has used expandRatherThanSplit().
- //We need to balance the saving from reading reduced data in the other branches with the cost of
- //writing the spill file to disk.
- if (isFiltered && (numExternalUses >= options->filteredSpillThreshold))
- return false;
- IHqlExpression * mode = expr->queryChild(2);
- switch (mode->getOperator())
- {
- case no_thor: case no_flat:
- //MORE: The following is possibly better - but roxie should be able to read from non spill data files in child queries fine
- //if ((options->targetClusterType == RoxieCluster) && linkedFromChild)) return false;
- return true;
- default:
- return false;
- }
- }
- case no_stepped:
- return true;
- case no_inlinetable:
- {
- IHqlExpression * transforms = expr->queryChild(0);
- //The inline table code means this should generate smaller code, and more efficient
- if (!isFiltered && !isProcessed && transforms->isConstant())
- return true;
- if (transforms->numChildren() > MAX_INLINE_COMMON_COUNT)
- return false;
- return true;
- }
- case no_getresult:
- case no_temptable:
- case no_rows:
- case no_xmlproject:
- case no_workunit_dataset:
- return !isProcessed && !isFiltered;
- case no_getgraphresult:
- return !expr->hasAttribute(_streaming_Atom); // we must not duplicate streaming inputs!
- case no_keyindex:
- case no_newkeyindex:
- if (!isFiltered)
- return true;
- return options->cloneFilteredIndex;
- case no_datasetfromrow:
- if (getNumActivityArguments(expr) == 0)
- return true;
- return false;
- case no_fail:
- case no_null:
- return !expr->isAction();
- case no_assertsorted:
- case no_sorted:
- case no_grouped:
- case no_distributed:
- case no_preservemeta:
- case no_nofold:
- case no_nohoist:
- case no_section:
- case no_sectioninput:
- case no_dataset_alias:
- expr = expr->queryChild(0);
- break;
- case no_newusertable:
- case no_limit:
- case no_keyedlimit:
- expr = expr->queryChild(0);
- isProcessed = true;
- break;
- case no_hqlproject:
- if (expr->hasAttribute(_countProject_Atom) || expr->hasAttribute(prefetchAtom))
- return false;
- expr = expr->queryChild(0);
- isProcessed = true;
- break;
- //MORE: Not so sure about all the following, include them so behaviour doesn't change
- case no_compound_diskread:
- case no_compound_disknormalize:
- case no_compound_diskaggregate:
- case no_compound_diskcount:
- case no_compound_diskgroupaggregate:
- case no_compound_indexread:
- case no_compound_indexnormalize:
- case no_compound_indexaggregate:
- case no_compound_indexcount:
- case no_compound_indexgroupaggregate:
- case no_compound_childread:
- case no_compound_childnormalize:
- case no_compound_childaggregate:
- case no_compound_childcount:
- case no_compound_childgroupaggregate:
- case no_compound_selectnew:
- case no_compound_inline:
- case no_datasetfromdictionary:
- expr = expr->queryChild(0);
- break;
- case no_filter:
- isFiltered = true;
- expr = expr->queryChild(0);
- break;
- case no_select:
- {
- if (options->targetClusterType == RoxieCluster)
- return false;
- if (!isNewSelector(expr))
- return true;
- expr = expr->queryChild(0);
- break;
- }
- default:
- return false;
- }
- //The following reduces the number of spills by taking into account other spills.
- if (noteOtherSpills)
- {
- ResourcerInfo * info = queryResourceInfo(expr);
- if (info)
- {
- if (info->isSpilledWrite())
- return (info->transformed == NULL);
- if (info->numExternalUses)
- {
- if (isFiltered && (numExternalUses >= options->filteredSpillThreshold))
- return false;
- return true;
- }
- }
- }
- }
- }
- bool ResourcerInfo::expandRatherThanSplit()
- {
- //MORE: This doesn't really work - should do indexMatching first.
- //should only expand if one side that uses this is also filtered
- IHqlExpression * expr = original;
- loop
- {
- ResourcerInfo * info = queryResourceInfo(expr);
- if (info && info->neverSplit)
- return true;
- switch (expr->getOperator())
- {
- case no_keyindex:
- case no_newkeyindex:
- case no_rowset:
- case no_getgraphloopresultset:
- return true;
- case no_null:
- return !expr->isAction();
- case no_inlinetable:
- if (options->expandSingleConstRow && hasSingleRow(expr))
- {
- IHqlExpression * values = expr->queryChild(0);
- if (values->queryChild(0)->isConstant())
- return true;
- }
- return false;
- case no_stepped:
- case no_rowsetrange:
- case no_rowsetindex:
- return true;
- case no_sorted:
- case no_grouped:
- case no_distributed:
- case no_preservemeta:
- case no_compound_diskread:
- case no_compound_disknormalize:
- case no_compound_diskaggregate:
- case no_compound_diskcount:
- case no_compound_diskgroupaggregate:
- case no_compound_indexread:
- case no_compound_indexnormalize:
- case no_compound_indexaggregate:
- case no_compound_indexcount:
- case no_compound_indexgroupaggregate:
- case no_compound_childread:
- case no_compound_childnormalize:
- case no_compound_childaggregate:
- case no_compound_childcount:
- case no_compound_childgroupaggregate:
- case no_compound_selectnew:
- case no_compound_inline:
- case no_section:
- case no_sectioninput:
- case no_dataset_alias:
- break;
- case no_select:
- if (options->targetClusterType == RoxieCluster)
- return false;
- if (!isNewSelector(expr))
- {
- if (!hasLinkCountedModifier(expr))
- return false;
- return true;
- }
- break;
- case no_rows:
- //If executing in a child query then you'll have less thread contention if the iterator is duplicated
- //So should probably uncomment the following.
- //return true;
- return false;
- default:
- return false;
- }
- expr = expr->queryChild(0);
- }
- }
- bool ResourcerInfo::hasDependency() const
- {
- if (graph)
- {
- GraphLinkArray & graphLinks = graph->dependsOn;
- ForEachItemIn(i, graphLinks)
- {
- ResourceGraphLink & link = graphLinks.item(i);
- if (link.sinkNode == original)
- return true;
- }
- }
- else
- {
- //Really should save some information away... Err on side of caution
- return true;
- }
- return false;
- }
- bool neverCommonUp(IHqlExpression * expr)
- {
- loop
- {
- node_operator op = expr->getOperator();
- switch (op)
- {
- case no_keyindex:
- case no_newkeyindex:
- return true;
- case no_filter:
- expr = expr->queryChild(0);
- break;
- default:
- return false;
- }
- }
- }
- bool ResourcerInfo::neverCommonUp()
- {
- return ::neverCommonUp(original);
- }
- bool ResourcerInfo::isExternalSpill()
- {
- if (expandRatherThanSpill(true) || (numInternalUses() == 0))
- return false;
- return (numExternalUses != 0);
- }
- bool ResourcerInfo::isSplit()
- {
- return numSplitPaths() > 1;
- }
- unsigned ResourcerInfo::numSplitPaths()
- {
- unsigned internal = numInternalUses();
- if ((internal == 0) || !options->allowSplitBetweenSubGraphs)
- return internal;
- //MORE
- return internal;
- }
- bool ResourcerInfo::isSpilledWrite()
- {
- if (numInternalUses() == 0)
- return true;
- return false;
- }
- IHqlExpression * ResourcerInfo::wrapRowOwn(IHqlExpression * expr)
- {
- if (!original->isDataset() && !original->isDictionary() && !expr->isDatarow())
- expr = createRow(no_selectnth, expr, getSizetConstant(1));
- return expr;
- }
- //---------------------------------------------------------------------------
- EclResourcer::EclResourcer(IErrorReceiver & _errors, IConstWorkUnit * _wu, ClusterType _targetClusterType, unsigned _clusterSize, const HqlCppOptions & _translatorOptions, UniqueSequenceCounter & spillSequence)
- : options(spillSequence)
- {
- wu.set(_wu);
- errors = &_errors;
- lockTransformMutex();
- targetClusterType = _targetClusterType;
- clusterSize = _clusterSize ? _clusterSize : FIXED_CLUSTER_SIZE;
- insideNeverSplit = false;
- insideSteppedNeverSplit = false;
- sequential = false;
- options.minimizeSpillSize = _translatorOptions.minimizeSpillSize;
- thisPass = 1;
- unsigned totalMemory = _translatorOptions.resourceMaxMemory ? _translatorOptions.resourceMaxMemory : DEFAULT_TOTAL_MEMORY;
- unsigned maxSockets = _translatorOptions.resourceMaxSockets ? _translatorOptions.resourceMaxSockets : DEFAULT_MAX_SOCKETS;
- unsigned maxActivities = _translatorOptions.resourceMaxActivities ? _translatorOptions.resourceMaxActivities : DEFAULT_MAX_ACTIVITIES;
- unsigned maxHeavy = _translatorOptions.resourceMaxHeavy;
- unsigned maxDistribute = _translatorOptions.resourceMaxDistribute;
- resourceLimit = new CResources(0);
- resourceLimit->set(RESactivities, maxActivities);
- switch (targetClusterType)
- {
- case ThorLCRCluster:
- resourceLimit->set(RESheavy, maxHeavy).set(REShashdist, maxDistribute);
- resourceLimit->set(RESmastersocket, maxSockets).set(RESslavememory,totalMemory);
- resourceLimit->set(RESslavesocket, maxSockets).set(RESmastermemory, totalMemory);
- break;
- default:
- resourceLimit->set(RESheavy, 0xffffffff).set(REShashdist, 0xffffffff);
- resourceLimit->set(RESmastersocket, 0xffffffff).set(RESslavememory, 0xffffffff);
- resourceLimit->set(RESslavesocket, 0xffffffff).set(RESmastermemory, 0xffffffff);
- clusterSize = 1;
- break;
- }
- if (_translatorOptions.unlimitedResources)
- {
- resourceLimit->set(RESheavy, 0xffffffff).set(REShashdist, 0xffffffff);
- resourceLimit->set(RESmastersocket, 0xffffffff).set(RESslavememory,0xffffffff);
- resourceLimit->set(RESslavesocket, 0xffffffff).set(RESmastermemory,0xffffffff);
- }
- options.isChildQuery = false;
- options.targetClusterType = targetClusterType;
- options.filteredSpillThreshold = _translatorOptions.filteredReadSpillThreshold;
- options.allowThroughSpill = (targetClusterType != RoxieCluster) && (targetClusterType != ThorLCRCluster) && _translatorOptions.allowThroughSpill;
- options.allowThroughResult = (targetClusterType != RoxieCluster) && (targetClusterType != ThorLCRCluster);
- options.cloneFilteredIndex = (targetClusterType != RoxieCluster);
- options.spillSharedConditionals = (targetClusterType == RoxieCluster);
- options.shareDontExpand = (targetClusterType == RoxieCluster);
- options.graphIdExpr = NULL;
- //MORE The following doesn't always work - it gets sometimes confused about spill files - see latestheaderbuild for an example.
- //Try again once cloneConditionals is false for thor
- options.minimiseSpills = _translatorOptions.minimiseSpills;
- spillMultiCondition = _translatorOptions.spillMultiCondition;
- spotThroughAggregate = _translatorOptions.spotThroughAggregate && (targetClusterType != RoxieCluster) && (targetClusterType != ThorLCRCluster);
- options.noConditionalLinks = (targetClusterType == RoxieCluster) || ((targetClusterType != HThorCluster) && _translatorOptions.noConditionalLinks);
- options.hoistResourced = _translatorOptions.hoistResourced;
- options.alwaysUseGraphResults = _translatorOptions.alwaysUseGraphResults;
- options.useGraphResults = false; // modified by later call
- options.groupedChildIterators = _translatorOptions.groupedChildIterators;
- options.allowSplitBetweenSubGraphs = false;//(targetClusterType == RoxieCluster);
- options.clusterSize = clusterSize;
- options.preventKeyedSplit = _translatorOptions.preventKeyedSplit;
- options.preventSteppedSplit = _translatorOptions.preventSteppedSplit;
- options.minimizeSkewBeforeSpill = _translatorOptions.minimizeSkewBeforeSpill;
- options.expandSingleConstRow = true;
- options.createSpillAsDataset = _translatorOptions.optimizeSpillProject && (targetClusterType != HThorCluster);
- options.combineSiblings = _translatorOptions.combineSiblingGraphs && (targetClusterType != HThorCluster) && (targetClusterType != RoxieCluster);
- options.optimizeSharedInputs = _translatorOptions.optimizeSharedGraphInputs && options.combineSiblings;
- options.actionLinkInNewGraph = _translatorOptions.actionLinkInNewGraph || (targetClusterType == HThorCluster);
- options.convertCompoundToExecuteWhen = false;
- options.useResultsForChildSpills = _translatorOptions.useResultsForChildSpills;
- options.newBalancedSpotter = _translatorOptions.newBalancedSpotter;
- spilled = false;
- }
- EclResourcer::~EclResourcer()
- {
- delete resourceLimit;
- unlockTransformMutex();
- }
- void EclResourcer::setChildQuery(bool value)
- {
- options.isChildQuery = value;
- }
- void EclResourcer::setNewChildQuery(IHqlExpression * graphIdExpr, unsigned numResults)
- {
- options.graphIdExpr = graphIdExpr;
- options.nextResult = numResults;
- }
- void EclResourcer::changeGraph(IHqlExpression * expr, ResourceGraphInfo * newGraph)
- {
- ResourcerInfo * info = queryResourceInfo(expr);
- info->graph.set(newGraph);
- ForEachItemInRev(idx, links)
- {
- ResourceGraphLink & cur = links.item(idx);
- if (cur.sourceNode == expr)
- cur.changeSourceGraph(newGraph);
- else if (cur.sinkNode == expr)
- cur.changeSinkGraph(newGraph);
- assertex(cur.sinkGraph != cur.sourceGraph);
- }
- }
- ResourceGraphInfo * EclResourcer::createGraph()
- {
- ResourceGraphInfo * graph = new ResourceGraphInfo(&options);
- graphs.append(*LINK(graph));
- //PrintLog("Create graph %p", graph);
- return graph;
- }
- void EclResourcer::connectGraphs(ResourceGraphInfo * sourceGraph, IHqlExpression * sourceNode, ResourceGraphInfo * sinkGraph, IHqlExpression * sinkNode, LinkKind kind)
- {
- ResourceGraphLink * link = new ResourceGraphLink(sourceGraph, sourceNode, sinkGraph, sinkNode, kind);
- links.append(*link);
- if (sourceGraph)
- sourceGraph->sinks.append(*link);
- if (sinkGraph)
- sinkGraph->sources.append(*link);
- }
- ResourcerInfo * EclResourcer::queryCreateResourceInfo(IHqlExpression * expr)
- {
- IHqlExpression * body = expr->queryBody();
- ResourcerInfo * info = (ResourcerInfo *)body->queryTransformExtra();
- if (!info)
- {
- info = new ResourcerInfo(expr, &options);
- body->setTransformExtraOwned(info);
- }
- return info;
- }
- void EclResourcer::replaceGraphReferences(IHqlExpression * expr, ResourceGraphInfo * oldGraph, ResourceGraphInfo * newGraph)
- {
- ResourcerInfo * info = queryResourceInfo(expr);
- if (!info || !info->containsActivity)
- return;
- if (info->isActivity && info->graph != oldGraph)
- return;
- info->graph.set(newGraph);
- unsigned first = getFirstActivityArgument(expr);
- unsigned last = first + getNumActivityArguments(expr);
- for (unsigned idx=first; idx < last; idx++)
- replaceGraphReferences(expr->queryChild(idx), oldGraph, newGraph);
- }
- void EclResourcer::removeLink(ResourceGraphLink & link, bool keepExternalUses)
- {
- ResourcerInfo * info = (ResourcerInfo *)queryResourceInfo(link.sourceNode);
- assertex(info && info->numExternalUses > 0);
- if (!keepExternalUses)
- info->numExternalUses--;
- if (link.sinkGraph)
- link.sinkGraph->sources.zap(link);
- link.sourceGraph->sinks.zap(link);
- links.zap(link);
- }
- void EclResourcer::replaceGraphReferences(ResourceGraphInfo * oldGraph, ResourceGraphInfo * newGraph)
- {
- ForEachItemIn(idx1, oldGraph->sinks)
- {
- ResourceGraphLink & sink = oldGraph->sinks.item(idx1);
- replaceGraphReferences(sink.sourceNode, oldGraph, newGraph);
- }
- ForEachItemInRev(idx2, links)
- {
- ResourceGraphLink & cur = links.item(idx2);
- if (cur.sourceGraph == oldGraph)
- {
- if (cur.sinkGraph == newGraph)
- removeLink(cur, false);
- else
- cur.changeSourceGraph(newGraph);
- }
- else if (cur.sinkGraph == oldGraph)
- {
- if (cur.sourceGraph == newGraph)
- removeLink(cur, false);
- else
- cur.changeSinkGraph(newGraph);
- }
- }
- }
- //------------------------------------------------------------------------------------------
- // PASS1: Gather information about splitter locations..
- void EclResourcer::tagActiveCursors(HqlExprCopyArray & activeRows)
- {
- ForEachItemIn(i, activeRows)
- {
- IHqlExpression & cur = activeRows.item(i);
- activeSelectors.append(cur);
- queryCreateResourceInfo(&cur)->isAlreadyInScope = true;
- }
- }
- inline bool projectSelectorDatasetToField(IHqlExpression * row)
- {
- return ((row->getOperator() == no_selectnth) && getFieldCount(row->queryRecord()) > 1);
- }
- static IHqlExpression * skipScalarWrappers(IHqlExpression * value)
- {
- loop
- {
- node_operator op = value->getOperator();
- if ((op != no_globalscope) && (op != no_thisnode) && (op != no_evalonce))
- return value;
- value = value->queryChild(0);
- }
- }
- static HqlTransformerInfo eclHoistLocatorInfo("EclHoistLocator");
- class EclHoistLocator : public NewHqlTransformer
- {
- public:
- EclHoistLocator(ChildDependentArray & _matched) : NewHqlTransformer(eclHoistLocatorInfo), matched(_matched)
- {
- alwaysSingle = true;
- }
- void analyseChild(IHqlExpression * expr, bool _alwaysSingle)
- {
- alwaysSingle = _alwaysSingle;
- analyse(expr, 0);
- }
- void noteDataset(IHqlExpression * expr, IHqlExpression * hoisted, bool alwaysHoist)
- {
- unsigned match = matched.findOriginal(expr);
- if (match == NotFound)
- {
- if (!hoisted)
- hoisted = expr;
- CChildDependent & depend = * new CChildDependent(expr, hoisted, alwaysHoist, alwaysSingle, false);
- matched.append(depend);
- }
- else
- {
- CChildDependent & prev = matched.item(match);
- if (alwaysHoist && !prev.alwaysHoist)
- prev.alwaysHoist = true;
- if (alwaysSingle && !prev.isSingleNode)
- prev.isSingleNode = true;
- }
- }
- void noteScalar(IHqlExpression * expr, IHqlExpression * value)
- {
- unsigned match = matched.findOriginal(expr);
- if (match == NotFound)
- {
- value = skipScalarWrappers(value);
- OwnedHqlExpr hoisted;
- IHqlExpression * projected = NULL;
- if (value->getOperator() == no_select)
- {
- bool isNew;
- IHqlExpression * row = querySelectorDataset(value, isNew);
- if(isNew || row->isDatarow())
- {
- if (projectSelectorDatasetToField(row))
- projected = value;
- hoisted.set(row);
- }
- else
- {
- //very unusual - possibly a thisnode(myfield) hoisted from an allnodes
- //use a createrow of a single element in the default behavour below
- }
- }
- else if (value->getOperator() == no_createset)
- {
- IHqlExpression * ds = value->queryChild(0);
- IHqlExpression * selected = value->queryChild(1);
- OwnedHqlExpr field;
- //Project down to a single field.so implicit fields can still be optimized
- if (selected->getOperator() == no_select)
- field.set(selected->queryChild(1));
- else
- field.setown(createField(valueId, selected->getType(), NULL));
- OwnedHqlExpr record = createRecord(field);
- OwnedHqlExpr self = getSelf(record);
- OwnedHqlExpr assign = createAssign(createSelectExpr(LINK(self), LINK(field)), LINK(selected));
- OwnedHqlExpr transform = createValue(no_newtransform, makeTransformType(record->getType()), LINK(assign));
- hoisted.setown(createDataset(no_newusertable, LINK(ds), createComma(LINK(record), LINK(transform))));
- }
- if (!hoisted)
- {
- OwnedHqlExpr field = createField(valueId, value->getType(), NULL);
- OwnedHqlExpr record = createRecord(field);
- OwnedHqlExpr self = getSelf(record);
- OwnedHqlExpr assign = createAssign(createSelectExpr(LINK(self), LINK(field)), LINK(value));
- OwnedHqlExpr transform = createValue(no_transform, makeTransformType(record->getType()), LINK(assign));
- hoisted.setown(createRow(no_createrow, LINK(transform)));
- }
- CChildDependent & depend = * new CChildDependent(expr, hoisted, true, true, true);
- depend.projected = projected;
- matched.append(depend);
- }
- }
- protected:
- ChildDependentArray & matched;
- bool alwaysSingle;
- };
- class EclChildSplitPointLocator : public EclHoistLocator
- {
- public:
- EclChildSplitPointLocator(IHqlExpression * _original, HqlExprCopyArray & _selectors, ChildDependentArray & _matches, bool _groupedChildIterators)
- : EclHoistLocator(_matches), selectors(_selectors), groupedChildIterators(_groupedChildIterators)
- {
- original = _original;
- okToSelect = false;
- gathered = false;
- conditionalDepth = 0;
- executedOnce = false;
- switch (original->getOperator())
- {
- case no_call:
- case no_externalcall:
- case no_libraryscopeinstance:
- okToSelect = true;
- break;
- }
- }
- void findSplitPoints(IHqlExpression * expr, unsigned from, unsigned to, bool _alwaysSingle, bool _executedOnce)
- {
- alwaysSingle = _alwaysSingle;
- for (unsigned i=from; i < to; i++)
- {
- IHqlExpression * cur = expr->queryChild(i);
- executedOnce = _executedOnce || cur->isAttribute(); // assume attributes are only executed once.
- findSplitPoints(cur);
- }
- alwaysSingle = false;
- }
- protected:
- void findSplitPoints(IHqlExpression * expr)
- {
- //containsNonActiveDataset() would be nice - but that isn't percolated outside assigns etc.
- if (containsAnyDataset(expr) || containsMustHoist(expr) || !expr->isIndependentOfScope())
- {
- if (!gathered)
- {
- gatherAmbiguousSelectors(original);
- gathered = true;
- }
- analyse(expr, 0);
- }
- }
- bool queryHoistDataset(IHqlExpression * ds)
- {
- bool alwaysHoist = true;
- if (executedOnce)
- {
- if (conditionalDepth != 0)
- alwaysHoist = false;
- }
- return alwaysHoist;
- }
- bool queryNoteDataset(IHqlExpression * ds)
- {
- bool alwaysHoist = queryHoistDataset(ds);
- //MORE: It should be possible to remove this condition, but it causes problems with resourcing hsss.xhql amongst others -> disable for the moment
- if (alwaysHoist)
- noteDataset(ds, ds, alwaysHoist);
- return alwaysHoist;
- }
- virtual void analyseExpr(IHqlExpression * expr)
- {
- if (alreadyVisited(expr))
- return;
- node_operator op = expr->getOperator();
- switch (op)
- {
- case no_select:
- {
- bool isNew;
- IHqlExpression * ds = querySelectorDataset(expr, isNew);
- if (isNew)
- {
- if (isEvaluateable(ds))
- {
- //MORE: Following isn't a very nice test - stops implicit denormalize getting messed up
- if (expr->isDataset())
- break;
- //Debtable.....
- //Don't hoist counts on indexes or dataset - it may mean they are evaluated more frequently than need be.
- //If dependencies and root graphs are handled correctly this could be deleted.
- if (isCompoundAggregate(ds))
- break;
- if (!expr->isDatarow() && !expr->isDataset() && !expr->isDictionary())
- {
- if (queryHoistDataset(ds))
- {
- noteScalar(expr, expr);
- return;
- }
- }
- else
- {
- if (queryNoteDataset(ds))
- return;
- }
- }
- }
- break;
- }
- case no_createset:
- {
- IHqlExpression * ds = expr->queryChild(0);
- if (isEvaluateable(ds))
- {
- if (queryHoistDataset(ds))
- {
- noteScalar(expr, expr);
- //?? queryNoteDataset(ds);
- return;
- }
- }
- break;
- }
- case no_assign:
- {
- IHqlExpression * rhs = expr->queryChild(1);
- //if rhs is a new, evaluatable, dataset then we want to add it
- if ((rhs->isDataset() || rhs->isDictionary()) && isEvaluateable(rhs))
- {
- if (queryNoteDataset(rhs))
- return;
- }
- break;
- }
- case no_sizeof:
- case no_allnodes:
- case no_nohoist:
- case no_forcegraph:
- return;
- case no_globalscope:
- case no_evalonce:
- if (expr->hasAttribute(optAtom) && !expr->isIndependentOfScope())
- break;
- if (expr->isDataset() || expr->isDatarow() || expr->isDictionary())
- noteDataset(expr, expr->queryChild(0), true);
- else
- noteScalar(expr, expr->queryChild(0));
- return;
- case no_thisnode:
- throwUnexpected();
- case no_getgraphresult:
- if (expr->hasAttribute(_streaming_Atom) || expr->hasAttribute(_distributed_Atom))
- {
- noteDataset(expr, expr, true);
- return;
- }
- break;
- case no_getgraphloopresult:
- noteDataset(expr, expr, true);
- return;
- case no_createdictionary:
- if (isEvaluateable(expr) && !isConstantDictionary(expr))
- noteDataset(expr, expr, true);
- return;
- case no_selectnth:
- if (expr->queryChild(1)->isConstant())
- {
- IHqlExpression * ds = expr->queryChild(0);
- switch (ds->getOperator())
- {
- case no_getgraphresult:
- if (!expr->hasAttribute(_streaming_Atom) && !expr->hasAttribute(_distributed_Atom))
- break;
- //fallthrough
- case no_getgraphloopresult:
- noteDataset(expr, expr, true);
- return;
- }
- }
- break;
- }
- bool wasOkToSelect = okToSelect;
- if (expr->isDataset())
- {
- switch (expr->getOperator())
- {
- case no_compound_diskread:
- case no_compound_disknormalize:
- case no_compound_diskaggregate:
- case no_compound_diskcount:
- case no_compound_diskgroupaggregate:
- case no_compound_indexread:
- case no_compound_indexnormalize:
- case no_compound_indexaggregate:
- case no_compound_indexcount:
- case no_compound_indexgroupaggregate:
- case no_compound_childread:
- case no_compound_childnormalize:
- case no_compound_childaggregate:
- case no_compound_childcount:
- case no_compound_childgroupaggregate:
- case no_compound_selectnew:
- case no_compound_inline:
- case no_newkeyindex:
- case no_keyindex:
- case no_table:
- okToSelect = false;
- break;
- }
- if (okToSelect && isEvaluateable(expr))
- {
- if (queryNoteDataset(expr))
- return;
- }
- }
- else
- okToSelect = true;
- switch (op)
- {
- case no_if:
- case no_choose:
- case no_chooseds:
- {
- IHqlExpression * cond = expr->queryChild(0);
- analyseExpr(cond);
- if (expr->isDataset() || expr->isDatarow() || expr->isDictionary() || expr->isAction())
- conditionalDepth++;
- doAnalyseChildren(expr, 1);
- if (expr->isDataset() || expr->isDatarow() || expr->isDictionary() || expr->isAction())
- conditionalDepth--;
- break;
- }
- case no_mapto:
- {
- analyseExpr(expr->queryChild(0));
- if (expr->isDataset() || expr->isDatarow() || expr->isDictionary())
- conditionalDepth++;
- analyseExpr(expr->queryChild(1));
- if (expr->isDataset() || expr->isDatarow() || expr->isDictionary())
- conditionalDepth--;
- break;
- }
- case no_attr_expr:
- //Ignore internal tracking attributes e.g., _selectors_Atom
- if (!isInternalAttributeName(expr->queryName()))
- {
- //Default action for no_attr_expr is to not walk children, but we need to here.
- bool wasExecutedOnce = executedOnce;
- executedOnce = true;
- analyseChildren(expr);
- executedOnce = wasExecutedOnce;
- }
- break;
- default:
- NewHqlTransformer::analyseExpr(expr);
- break;
- }
- okToSelect = wasOkToSelect;
- }
- bool isCompoundAggregate(IHqlExpression * ds)
- {
- return false;
- //Generates worse code unless we take into account whether or not the newDisk operation flags are enabled.
- if (!isTrivialSelectN(ds))
- return false;
- IHqlExpression * agg = ds->queryChild(0);
- if (isSimpleCountAggregate(agg, true))
- return true;
- return false;
- }
- void gatherAmbiguousSelectors(IHqlExpression * expr)
- {
- //Horrible code to try and cope with ambiguous left selectors.
- //o Tree is ambiguous so same child expression can occur in different contexts - so can't depend on the context it is found in to work out if can hoist
- //o If any selector that is hidden within child expressions matches one in scope then can't hoist it.
- //If the current expression creates a selector, then can't hoist anything that depends on it [only add to hidden if in selectors to reduce searching]
- //o Want to hoist as much as possible.
- if (selectors.empty())
- return;
- unsigned first = getFirstActivityArgument(expr);
- unsigned last = first + getNumActivityArguments(expr);
- unsigned max = expr->numChildren();
- unsigned i;
- HqlExprCopyArray hiddenSelectors;
- for (i = 0; i < first; i++)
- expr->queryChild(i)->gatherTablesUsed(&hiddenSelectors, NULL);
- for (i = last; i < max; i++)
- expr->queryChild(i)->gatherTablesUsed(&hiddenSelectors, NULL);
- ForEachItemIn(iSel, selectors)
- {
- IHqlExpression & cur = selectors.item(iSel);
- if (hiddenSelectors.contains(cur))
- ambiguousSelectors.append(cur);
- }
- switch (getChildDatasetType(expr))
- {
- case childdataset_datasetleft:
- case childdataset_left:
- {
- IHqlExpression * ds = expr->queryChild(0);
- IHqlExpression * selSeq = querySelSeq(expr);
- OwnedHqlExpr left = createSelector(no_left, ds, selSeq);
- if (selectors.contains(*left))
- ambiguousSelectors.append(*left);
- break;
- }
- case childdataset_same_left_right:
- case childdataset_top_left_right:
- case childdataset_nway_left_right:
- {
- IHqlExpression * ds = expr->queryChild(0);
- IHqlExpression * selSeq = querySelSeq(expr);
- OwnedHqlExpr left = createSelector(no_left, ds, selSeq);
- OwnedHqlExpr right = createSelector(no_right, ds, selSeq);
- if (selectors.contains(*left))
- ambiguousSelectors.append(*left);
- if (selectors.contains(*right))
- ambiguousSelectors.append(*right);
- break;
- }
- case childdataset_leftright:
- {
- IHqlExpression * leftDs = expr->queryChild(0);
- IHqlExpression * rightDs = expr->queryChild(1);
- IHqlExpression * selSeq = querySelSeq(expr);
- OwnedHqlExpr left = createSelector(no_left, leftDs, selSeq);
- OwnedHqlExpr right = createSelector(no_right, rightDs, selSeq);
- if (selectors.contains(*left))
- ambiguousSelectors.append(*left);
- if (selectors.contains(*right))
- ambiguousSelectors.append(*right);
- break;
- }
- }
- }
- bool isEvaluateable(IHqlExpression * ds, bool ignoreInline = false)
- {
- //Don't hoist an alias - it could create unnecessary duplicate spills - hoist its input
- if (ds->getOperator() == no_dataset_alias)
- return false;
- //Not allowed to hoist
- if (isContextDependent(ds, (conditionalDepth == 0), true))
- return false;
- //MORE: Needs more work for child queries - need a GroupedChildIterator activity
- if (isGrouped(ds) && selectors.ordinality() && !groupedChildIterators)
- return false;
- //Check datasets are available
- HqlExprCopyArray scopeUsed;
- ds->gatherTablesUsed(NULL, &scopeUsed);
- ForEachItemIn(i, scopeUsed)
- {
- IHqlExpression & cur = scopeUsed.item(i);
- if (!selectors.contains(cur))
- return false;
- if (ambiguousSelectors.contains(cur))
- return false;
- }
- if (!isEfficientToHoistDataset(ds, ignoreInline))
- return false;
- return true;
- }
- bool isEfficientToHoistDataset(IHqlExpression * ds, bool ignoreInline) const
- {
- //MORE: This whole function could do with some significant improvements. Whether it is inefficient to hoist
- //depends on at least the following...
- //a) cost of serializing v cost of re-evaluating (which can depend on the engine).
- //b) How many times it will be evaluated in the child context
- if (ds->getOperator() == no_createdictionary)
- return true;
- if (isInlineTrivialDataset(ds))
- return false;
- #ifdef MINIMAL_CHANGES
- if (!ignoreInline)
- {
- //Generally this appears to be better to hoist since it involves calling a transform.
- if (ds->getOperator() == no_dataset_from_transform)
- return true;
- if (canProcessInline(NULL, ds))
- return false;
- }
- #endif
- return true;
- }
- protected:
- IHqlExpression * original;
- HqlExprCopyArray & selectors;
- HqlExprCopyArray ambiguousSelectors;
- unsigned conditionalDepth;
- bool okToSelect;
- bool gathered;
- bool groupedChildIterators;
- bool executedOnce;
- };
- void EclResourcer::gatherChildSplitPoints(IHqlExpression * expr, ResourcerInfo * info, unsigned first, unsigned last)
- {
- //NB: Don't call member functions to ensure correct nesting of transform mutexes.
- EclChildSplitPointLocator locator(expr, activeSelectors, info->childDependents, options.groupedChildIterators);
- unsigned max = expr->numChildren();
- //If child queries are supported then don't hoist the expressions if they might only be evaluated once
- //because they may be conditional
- bool alwaysOnce = false;
- switch (expr->getOperator())
- {
- case no_setresult:
- case no_selectnth:
- //set results, only done once=>don't hoist conditionals
- locator.findSplitPoints(expr, last, max, true, true);
- return;
- case no_createrow:
- case no_datasetfromrow:
- case no_projectrow:
- alwaysOnce = true;
- break;
- case no_loop:
- if ((options.targetClusterType == ThorLCRCluster) && !options.isChildQuery)
- {
- //This is ugly! The body is executed in parallel, so don't force that as a work unit result
- //It means some child query expressions within loops don't get forced into work unit writes
- //but that just means that the generated code will be not as good as it could be.
- const unsigned bodyArg = 4;
- locator.findSplitPoints(expr, 1, bodyArg, true, false);
- locator.findSplitPoints(expr, bodyArg, bodyArg+1, false, false);
- locator.findSplitPoints(expr, bodyArg+1, max, true, false);
- return;
- }
- break;
- }
- locator.findSplitPoints(expr, 0, first, true, true); // IF() conditions only evaluated once... => don't force
- locator.findSplitPoints(expr, last, max, true, alwaysOnce);
- }
- class EclThisNodeLocator : public EclHoistLocator
- {
- public:
- EclThisNodeLocator(ChildDependentArray & _matches)
- : EclHoistLocator(_matches)
- {
- allNodesDepth = 0;
- }
- protected:
- virtual void analyseExpr(IHqlExpression * expr)
- {
- //NB: This doesn't really work for no_thisnode occurring in multiple contexts. We should probably hoist it from everywhere if it is hoistable from anywhere,
- // although theoretically that gives us problems with ambiguous selectors.
- if (alreadyVisited(expr) || !containsThisNode(expr))
- return;
- node_operator op = expr->getOperator();
- switch (op)
- {
- case no_allnodes:
- allNodesDepth++;
- NewHqlTransformer::analyseExpr(expr);
- allNodesDepth--;
- return;
- case no_thisnode:
- if (allNodesDepth == 0)
- {
- if (expr->isDataset() || expr->isDatarow() || expr->isDictionary())
- noteDataset(expr, expr->queryChild(0), true);
- else
- noteScalar(expr, expr->queryChild(0));
- return;
- }
- allNodesDepth--;
- NewHqlTransformer::analyseExpr(expr);
- allNodesDepth++;
- return;
- }
- NewHqlTransformer::analyseExpr(expr);
- }
- protected:
- unsigned allNodesDepth;
- };
- static bool isPotentialCompoundSteppedIndexRead(IHqlExpression * expr)
- {
- loop
- {
- switch (expr->getOperator())
- {
- case no_compound_diskread:
- case no_compound_disknormalize:
- case no_compound_diskaggregate:
- case no_compound_diskcount:
- case no_compound_diskgroupaggregate:
- case no_compound_childread:
- case no_compound_childnormalize:
- case no_compound_childaggregate:
- case no_compound_childcount:
- case no_compound_childgroupaggregate:
- case no_compound_selectnew:
- case no_compound_inline:
- return false;
- case no_compound_indexread:
- case no_newkeyindex:
- return true;
- case no_getgraphloopresult:
- return true; // Could be an index read in another graph iteration, so don't combine
- case no_keyedlimit:
- case no_preload:
- case no_filter:
- case no_hqlproject:
- case no_newusertable:
- case no_limit:
- case no_sorted:
- case no_preservemeta:
- case no_distributed:
- case no_grouped:
- case no_stepped:
- case no_section:
- case no_sectioninput:
- case no_dataset_alias:
- break;
- case no_choosen:
- {
- IHqlExpression * arg2 = expr->queryChild(2);
- if (arg2 && !arg2->isPure())
- return false;
- break;
- }
- default:
- return false;
- }
- expr = expr->queryChild(0);
- }
- }
- bool EclResourcer::findSplitPoints(IHqlExpression * expr, bool isProjected)
- {
- ResourcerInfo * info = queryResourceInfo(expr);
- if (info && info->visited)
- {
- if (!isProjected)
- info->clearProjected();
- if (info->isAlreadyInScope || info->isActivity || !info->containsActivity)
- return info->containsActivity;
- }
- else
- {
- info = queryCreateResourceInfo(expr);
- info->visited = true;
- if (!isProjected)
- info->clearProjected();
- bool isActivity = true;
- switch (expr->getOperator())
- {
- case no_select:
- //either a select from a setresult or use of a child-dataset
- if (isNewSelector(expr))
- {
- info->containsActivity = findSplitPoints(expr->queryChild(0), false);
- assertex(queryResourceInfo(expr->queryChild(0))->isActivity);
- }
- if (expr->isDataset() || expr->isDatarow())
- {
- info->isActivity = true;
- info->containsActivity = true;
- }
- return info->containsActivity;
- case no_mapto:
- throwUnexpected();
- case no_activerow:
- info->isActivity = true;
- info->containsActivity = false;
- return false;
- case no_rowset: // don't resource this as an activity
- isActivity = false;
- break;
- case no_attr:
- case no_attr_expr:
- case no_attr_link:
- case no_getgraphloopresultset:
- info->isActivity = false;
- info->containsActivity = false;
- return false;
- case no_datasetlist:
- isActivity = false;
- break;
- case no_rowsetrange:
- {
- //Don't resource this as an activity if it is a function of the input graph rows,
- //however we do want to if it is coming from a dataset list.
- IHqlExpression * ds = expr->queryChild(0);
- //MORE: Should walk further down the tree to allow for nested rowsetranges etc.
- if (ds->getOperator() == no_rowset || ds->getOperator() == no_getgraphloopresultset)
- {
- info->isActivity = false;
- info->containsActivity = false;
- return false;
- }
- isActivity = false;
- break;
- }
- }
- ITypeInfo * type = expr->queryType();
- if (!type || type->isScalar())
- return false;
- info->isActivity = isActivity;
- info->containsActivity = true;
- }
- unsigned first = getFirstActivityArgument(expr);
- unsigned last = first + getNumActivityArguments(expr);
- if (options.hoistResourced)
- {
- switch (expr->getOperator())
- {
- case no_allnodes:
- {
- //MORE: This needs to recursively walk and lift any contained no_selfnode, but don't go past another nested no_allnodes;
- EclThisNodeLocator locator(info->childDependents);
- locator.analyseChild(expr->queryChild(0), true);
- break;
- }
- case no_childquery:
- throwUnexpected();
- default:
- {
- for (unsigned idx=first; idx < last; idx++)
- {
- IHqlExpression * cur = expr->queryChild(idx);
- findSplitPoints(cur, false);
- }
- gatherChildSplitPoints(expr, info, first, last);
- break;
- }
- }
- ForEachItemIn(i2, info->childDependents)
- {
- CChildDependent & cur = info->childDependents.item(i2);
- ResourcerInfo * hoistedInfo = queryCreateResourceInfo(cur.hoisted);
- if (cur.projected)
- hoistedInfo->addProjected(cur.projected);
- else
- hoistedInfo->clearProjected();
- if (cur.alwaysHoist)
- findSplitPoints(cur.hoisted, (cur.projected != NULL));
- allChildDependents.append(OLINK(cur));
- }
- }
- else
- {
- for (unsigned idx=first; idx < last; idx++)
- findSplitPoints(expr->queryChild(idx), false);
- }
- return info->containsActivity;
- }
- void EclResourcer::findSplitPoints(HqlExprArray & exprs)
- {
- //Finding items that should be hoisted from child queries, and evaluated at this level is tricky:
- //* After hoisting a child expression, that may in turn contain other child expressions.
- //* Some child expressions are only worth hoisting if they are a simple function of something
- // that will be evaluated here.
- //* If we're creating a single project for each x[1] then that needs to be done after all the
- // expressions to hoist have been found.
- //* The usage counts have to correct - including
- //* The select child dependents for a ds[n] need to be inherited by the project(ds)[n]
- //=>
- //First walk the expression tree gathering all the expressions that will be used.
- //Project the expressions that need projecting.
- //Finally walk the tree again, this time calculating the correct usage counts.
- //
- //On reflection the hoisting and projecting could be implemented as a completely separate transformation.
- //However, it shares a reasonable amount of the logic with the rest of the resourcing, so keep together
- //for the moment.
- ForEachItemIn(idx, exprs)
- findSplitPoints(&exprs.item(idx), false);
- extendSplitPoints();
- projectChildDependents();
- deriveUsageCounts(exprs);
- }
- void EclResourcer::extendSplitPoints()
- {
- //NB: findSplitPoints might call this array to be extended
- for (unsigned i1=0; i1 < allChildDependents.ordinality(); i1++)
- {
- CChildDependent & cur = allChildDependents.item(i1);
- if (!cur.alwaysHoist && isWorthForcingHoist(cur.hoisted))
- findSplitPoints(cur.hoisted, (cur.projected != NULL));
- }
- }
- IHqlExpression * EclResourcer::projectChildDependent(IHqlExpression * expr)
- {
- ResourcerInfo * info = queryResourceInfo(expr);
- if (!info || !info->projectResult || info->projected.empty())
- return expr;
- if (info->projectedExpr)
- return info->projectedExpr;
- assertex(expr->getOperator() == no_selectnth);
- IHqlExpression * row = expr;
- assertex(projectSelectorDatasetToField(row));
- unsigned totalFields = getFieldCount(row->queryRecord());
- if (totalFields == info->projected.ordinality())
- {
- info->clearProjected();
- return expr;
- }
- //Create a projection containing each of the fields that are used from the child queries.
- IHqlExpression * ds = row->queryChild(0);
- HqlExprArray fields;
- HqlExprArray values;
- ForEachItemIn(i, info->projected)
- {
- IHqlExpression * value = &info->projected.item(i);
- IHqlExpression * field = value->queryChild(1);
- LinkedHqlExpr projectedField = field;
- //Check for a very unusual situation where the same field is projected from two different sub records
- while (fields.contains(*projectedField))
- projectedField.setown(cloneFieldMangleName(field)); // Generates a new mangled name each time
- OwnedHqlExpr activeDs = createRow(no_activetable, LINK(ds->queryNormalizedSelector()));
- fields.append(*LINK(projectedField));
- values.append(*replaceSelector(value, row, activeDs));
- }
- OwnedHqlExpr projectedRecord = createRecord(fields);
- OwnedHqlExpr self = getSelf(projectedRecord);
- HqlExprArray assigns;
- ForEachItemIn(i2, fields)
- {
- IHqlExpression * field = &fields.item(i2);
- IHqlExpression * value = &values.item(i2);
- assigns.append(*createAssign(createSelectExpr(LINK(self), LINK(field)), LINK(value)));
- }
- OwnedHqlExpr transform = createValue(no_newtransform, makeTransformType(projectedRecord->getType()), assigns);
- OwnedHqlExpr projectedDs = createDataset(no_newusertable, LINK(ds), createComma(LINK(projectedRecord), LINK(transform)));
- info->projectedExpr.setown(replaceChild(row, 0, projectedDs));
- findSplitPoints(info->projectedExpr, false);
- //Ensure child dependents that are no longer used are not processed - otherwise the usage counts will be wrong.
- ForEachItemIn(ic, info->childDependents)
- {
- CChildDependent & cur = info->childDependents.item(ic);
- //Clearing these ensures that isResourcedActivity returns false;
- cur.hoisted.clear();
- cur.projectedHoisted.clear();
- }
- return info->projectedExpr;
- }
- void EclResourcer::projectChildDependents()
- {
- ForEachItemIn(i2, allChildDependents)
- {
- CChildDependent & cur = allChildDependents.item(i2);
- if (isResourcedActivity(cur.hoisted))
- cur.projectedHoisted.set(projectChildDependent(cur.hoisted));
- }
- }
- void EclResourcer::deriveUsageCounts(IHqlExpression * expr)
- {
- ResourcerInfo * info = queryResourceInfo(expr);
- if (!info || !(info->containsActivity || info->isActivity))
- return;
- bool savedInsideNeverSplit = insideNeverSplit;
- bool savedInsideSteppedNeverSplit = insideSteppedNeverSplit;
- if (insideSteppedNeverSplit && info)
- {
- if (!isPotentialCompoundSteppedIndexRead(expr) && (expr->getOperator() != no_datasetlist) && expr->getOperator() != no_inlinetable)
- insideSteppedNeverSplit = false;
- }
- if (info->numUses)
- {
- if (insideNeverSplit || insideSteppedNeverSplit)
- info->neverSplit = true;
- if (info->isAlreadyInScope || info->isActivity || !info->containsActivity)
- {
- info->numUses++;
- return;
- }
- }
- else
- {
- info->numUses++;
- if (insideNeverSplit || insideSteppedNeverSplit)
- info->neverSplit = true;
- switch (expr->getOperator())
- {
- case no_select:
- //either a select from a setresult or use of a child-dataset
- if (isNewSelector(expr))
- deriveUsageCounts(expr->queryChild(0));
- return;
- case no_activerow:
- case no_attr:
- case no_attr_expr:
- case no_attr_link:
- case no_rowset: // don't resource this as an activity
- case no_getgraphloopresultset:
- return;
- case no_keyedlimit:
- if (options.preventKeyedSplit)
- insideNeverSplit = true;
- break;
- case no_filter:
- if (options.preventKeyedSplit && filterIsKeyed(expr))
- insideNeverSplit = true;
- else
- {
- LinkedHqlExpr invariant;
- OwnedHqlExpr cond = extractFilterConditions(invariant, expr, expr->queryNormalizedSelector(), false, false);
- if (invariant)
- info->isConditionalFilter = true;
- }
- break;
- case no_hqlproject:
- case no_newusertable:
- case no_aggregate:
- case no_newaggregate:
- if (options.preventKeyedSplit && expr->hasAttribute(keyedAtom))
- insideNeverSplit = true;
- break;
- case no_stepped:
- case no_mergejoin:
- case no_nwayjoin:
- if (options.preventSteppedSplit)
- insideSteppedNeverSplit = true;
- break;
- case no_compound_diskread:
- case no_compound_disknormalize:
- case no_compound_diskaggregate:
- case no_compound_diskcount:
- case no_compound_diskgroupaggregate:
- case no_compound_indexread:
- case no_compound_indexnormalize:
- case no_compound_indexaggregate:
- case no_compound_indexcount:
- case no_compound_indexgroupaggregate:
- case no_compound_childread:
- case no_compound_childnormalize:
- case no_compound_childaggregate:
- case no_compound_childcount:
- case no_compound_childgroupaggregate:
- case no_compound_selectnew:
- case no_compound_inline:
- insideNeverSplit = true;
- break;
- }
- ITypeInfo * type = expr->queryType();
- if (!type || type->isScalar())
- {
- insideNeverSplit = savedInsideNeverSplit;
- insideSteppedNeverSplit = savedInsideSteppedNeverSplit;
- return;
- }
- }
- unsigned first = getFirstActivityArgument(expr);
- unsigned last = first + getNumActivityArguments(expr);
- for (unsigned idx=first; idx < last; idx++)
- deriveUsageCounts(expr->queryChild(idx));
- insideNeverSplit = savedInsideNeverSplit;
- insideSteppedNeverSplit = savedInsideSteppedNeverSplit;
- }
- void EclResourcer::deriveUsageCounts(const HqlExprArray & exprs)
- {
- ForEachItemIn(idx2, exprs)
- deriveUsageCounts(&exprs.item(idx2));
- ForEachItemIn(i2, allChildDependents)
- {
- CChildDependent & cur = allChildDependents.item(i2);
- if (isResourcedActivity(cur.projectedHoisted))
- deriveUsageCounts(cur.projectedHoisted);
- }
- }
- //------------------------------------------------------------------------------------------
- // PASS2: Actually create the subgraphs based on splitters.
- void EclResourcer::createInitialGraph(IHqlExpression * expr, IHqlExpression * owner, ResourceGraphInfo * ownerGraph, LinkKind linkKind, bool forceNewGraph)
- {
- ResourcerInfo * info = queryResourceInfo(expr);
- if (!info || !info->containsActivity)
- return;
- LinkKind childLinkKind = UnconditionalLink;
- Linked<ResourceGraphInfo> thisGraph = ownerGraph;
- bool forceNewChildGraph = false;
- if (info->isActivity)
- {
- //Need to ensure no_libraryselects are not separated from the no_libraryscopeinstance
- //so ensure they are placed in the same graph.
- node_operator op = expr->getOperator();
- if (op == no_libraryselect)
- {
- ResourcerInfo * moduleInfo = queryResourceInfo(expr->queryChild(1));
- if (!info->graph && moduleInfo->graph)
- info->graph.set(moduleInfo->graph);
- }
- if (info->graph)
- {
- connectGraphs(info->graph, expr, ownerGraph, owner, linkKind);
- info->numExternalUses++;
- return;
- }
- unsigned numUses = info->numUses;
- switch (op)
- {
- case no_libraryscopeinstance:
- numUses = 1;
- break;
- case no_libraryselect:
- forceNewGraph = true;
- break;
- }
- if (!ownerGraph || numUses > 1 || (linkKind != UnconditionalLink) || forceNewGraph)
- {
- thisGraph.setown(createGraph());
- connectGraphs(thisGraph, expr, ownerGraph, owner, linkKind);
- info->numExternalUses++;
- if (!ownerGraph && sequential)
- thisGraph->hasSequentialSource = true;
- }
- info->graph.set(thisGraph);
- if (info->isRootActivity)
- thisGraph->hasRootActivity = true;
- switch (expr->getOperator())
- {
- case no_compound:
- //NB: First argument is forced into a separate graph
- if (options.convertCompoundToExecuteWhen)
- {
- createInitialGraph(expr->queryChild(0), expr, thisGraph, UnconditionalLink, true);
- }
- else
- {
- HqlExprArray actions;
- expr->queryChild(0)->unwindList(actions, no_actionlist);
- ForEachItemIn(i, actions)
- createInitialGraph(&actions.item(i), expr, NULL, UnconditionalLink, true);
- }
- createInitialGraph(expr->queryChild(1), expr, thisGraph, UnconditionalLink, false);
- return;
- case no_executewhen:
- {
- bool newGraph = expr->isAction() && (options.targetClusterType == HThorCluster);
- createInitialGraph(expr->queryChild(0), expr, thisGraph, UnconditionalLink, newGraph);
- createInitialGraph(expr->queryChild(1), expr, thisGraph, UnconditionalLink, true);
- return;
- }
- case no_keyindex:
- case no_newkeyindex:
- return;
- case no_parallel:
- case no_actionlist:
- {
- ForEachChild(i, expr)
- createInitialGraph(expr->queryChild(i), expr, thisGraph, UnconditionalLink, options.actionLinkInNewGraph);
- return;
- }
- case no_if:
- case no_choose:
- case no_chooseds:
- //conditional nodes, the child branches are marked as conditional
- childLinkKind = UnconditionalLink;
- thisGraph->mergedConditionSource = true;
- if (!options.noConditionalLinks || (expr->isAction() && options.actionLinkInNewGraph))
- forceNewChildGraph = true;
- break;
- case no_filter:
- if (info->isConditionalFilter)
- {
- thisGraph->mergedConditionSource = true;
- if (!options.noConditionalLinks)
- forceNewChildGraph = true;
- }
- break;
- // case no_nonempty:
- case no_sequential:
- case no_orderedactionlist:
- {
- unsigned first = getFirstActivityArgument(expr);
- unsigned last = first + getNumActivityArguments(expr);
- createInitialGraph(expr->queryChild(first), expr, thisGraph, SequenceLink, options.actionLinkInNewGraph);
- for (unsigned idx = first+1; idx < last; idx++)
- createInitialGraph(expr->queryChild(idx), expr, thisGraph, SequenceLink, options.actionLinkInNewGraph);
- return;
- }
- case no_case:
- case no_map:
- {
- throwUnexpected();
- }
- case no_output:
- {
- //Tag the inputs to an output statement, so that if a spill was going to occur we read
- //from the output file instead of spilling.
- //Needs the grouping to be saved in the same way. Could cope with compressed matching, but not
- //much point - since fairly unlikely.
- IHqlExpression * filename = expr->queryChild(1);
- if (filename && (filename->getOperator() == no_constant) && !expr->hasAttribute(xmlAtom) && !expr->hasAttribute(jsonAtom) && !expr->hasAttribute(csvAtom))
- {
- IHqlExpression * dataset = expr->queryChild(0);
- if (expr->hasAttribute(groupedAtom) == isGrouped(dataset))
- {
- StringBuffer filenameText;
- filename->queryValue()->getStringValue(filenameText);
- ResourcerInfo * childInfo = queryResourceInfo(dataset);
- if (!childInfo->linkedFromChild && !isUpdatedConditionally(expr))
- childInfo->outputToUseForSpill = expr;
- }
- }
- if (isUpdatedConditionally(expr))
- thisGraph->mergedConditionSource = true;
- break;
- }
- case no_buildindex:
- if (isUpdatedConditionally(expr))
- thisGraph->mergedConditionSource = true;
- break;
- }
- }
- unsigned first = getFirstActivityArgument(expr);
- unsigned last = first + getNumActivityArguments(expr);
- for (unsigned idx = first; idx < last; idx++)
- createInitialGraph(expr->queryChild(idx), expr, thisGraph, childLinkKind, forceNewChildGraph);
- ForEachItemIn(i2, info->childDependents)
- {
- CChildDependent & cur = info->childDependents.item(i2);
- if (isResourcedActivity(cur.projectedHoisted))
- createInitialGraph(cur.projectedHoisted, expr, thisGraph, SequenceLink, true);
- }
- }
- void EclResourcer::createInitialGraphs(HqlExprArray & exprs)
- {
- ForEachItemIn(idx, exprs)
- {
- IHqlExpression * rootExpr = &exprs.item(idx);
- ResourcerInfo * info = queryResourceInfo(rootExpr);
- assertex(info->isActivity);
- info->setRootActivity();
- createInitialGraph(rootExpr, NULL, NULL, UnconditionalLink, false);
- }
- }
- void EclResourcer::createInitialRemoteGraph(IHqlExpression * expr, IHqlExpression * owner, ResourceGraphInfo * ownerGraph, bool forceNewGraph)
- {
- ResourcerInfo * info = queryResourceInfo(expr);
- if (!info || !info->containsActivity)
- return;
- Linked<ResourceGraphInfo> thisGraph = ownerGraph;
- if (info->isActivity)
- {
- if (info->graph)
- {
- connectGraphs(info->graph, expr, ownerGraph, owner, UnconditionalLink);
- info->numExternalUses++;
- return;
- }
- if (!ownerGraph || forceNewGraph)
- {
- thisGraph.setown(createGraph());
- connectGraphs(thisGraph, expr, ownerGraph, owner, UnconditionalLink);
- info->numExternalUses++;
- }
- info->graph.set(thisGraph);
- switch (expr->getOperator())
- {
- case no_compound:
- if (options.convertCompoundToExecuteWhen)
- createInitialRemoteGraph(expr->queryChild(0), expr, thisGraph, true);
- else
- createInitialRemoteGraph(expr->queryChild(0), expr, NULL, true);
- createInitialRemoteGraph(expr->queryChild(1), expr, thisGraph, false);
- return;
- case no_executewhen:
- createInitialRemoteGraph(expr->queryChild(0), expr, thisGraph, false);
- createInitialRemoteGraph(expr->queryChild(1), expr, thisGraph, true);
- return;
- }
- }
- unsigned first = getFirstActivityArgument(expr);
- unsigned last = first + getNumActivityArguments(expr);
- for (unsigned idx = first; idx < last; idx++)
- createInitialRemoteGraph(expr->queryChild(idx), expr, thisGraph, false);
- }
- void EclResourcer::createInitialRemoteGraphs(HqlExprArray & exprs)
- {
- ForEachItemIn(idx, exprs)
- createInitialRemoteGraph(&exprs.item(idx), NULL, NULL, false);
- }
- //------------------------------------------------------------------------------------------
- // PASS3: Tag graphs/links that are conditional or unconditional
- void EclResourcer::markChildDependentsAsUnconditional(ResourcerInfo * info, IHqlExpression * condition)
- {
- if (options.hoistResourced)
- {
- ForEachItemIn(i2, info->childDependents)
- {
- CChildDependent & cur = info->childDependents.item(i2);
- if (isResourcedActivity(cur.projectedHoisted))
- markAsUnconditional(cur.projectedHoisted, NULL, condition);
- }
- }
- }
- void EclResourcer::markAsUnconditional(IHqlExpression * expr, ResourceGraphInfo * ownerGraph, IHqlExpression * condition)
- {
- ResourcerInfo * info = queryResourceInfo(expr);
- if (!info || !info->containsActivity)
- return;
- if (!info->isActivity)
- {
- unsigned first = getFirstActivityArgument(expr);
- unsigned last = first + getNumActivityArguments(expr);
- for (unsigned idx=first; idx < last; idx++)
- markAsUnconditional(expr->queryChild(idx), ownerGraph, condition);
- return;
- }
- if (condition)
- if (info->addCondition(condition))
- condition = NULL;
- if (info->pathToExpr == ResourcerInfo::PathUnconditional)
- return;
- if ((info->pathToExpr == ResourcerInfo::PathConditional) && condition)
- {
- if (targetClusterType == RoxieCluster)
- {
- if (spillMultiCondition)
- {
- if (info->graph != ownerGraph)
- info->graph->isUnconditional = true;
- }
- return;
- }
- else
- {
- if (info->graph != ownerGraph)
- return;
- }
- }
- bool wasConditional = (info->pathToExpr == ResourcerInfo::PathConditional);
- if (!condition)
- {
- info->pathToExpr = ResourcerInfo::PathUnconditional;
- if (info->graph != ownerGraph)
- info->graph->isUnconditional = true;
- }
- else
- info->pathToExpr = ResourcerInfo::PathConditional;
- node_operator op = expr->getOperator();
- switch (op)
- {
- case no_if:
- case no_choose:
- case no_chooseds:
- if (options.noConditionalLinks)
- break;
- if (condition)
- markCondition(expr, condition, wasConditional);
- else
- {
- //This list is processed in a second phase.
- if (rootConditions.find(*expr) == NotFound)
- rootConditions.append(*LINK(expr));
- }
- markChildDependentsAsUnconditional(info, condition);
- return;
- case no_filter:
- if (!info->isConditionalFilter || options.noConditionalLinks)
- break;
- if (condition)
- markCondition(expr, condition, wasConditional);
- else
- {
- //This list is processed in a second phase.
- if (rootConditions.find(*expr) == NotFound)
- rootConditions.append(*LINK(expr));
- }
- markChildDependentsAsUnconditional(info, condition);
- return;
- case no_compound:
- if (!options.convertCompoundToExecuteWhen)
- {
- HqlExprArray actions;
- expr->queryChild(0)->unwindList(actions, no_actionlist);
- ForEachItemIn(i, actions)
- markAsUnconditional(&actions.item(i), NULL, NULL);
- markAsUnconditional(expr->queryChild(1), ownerGraph, condition);
- return;
- }
- break;
- case no_sequential:
- case no_orderedactionlist:
- // case no_nonempty:
- if (!options.isChildQuery)
- {
- unsigned first = getFirstActivityArgument(expr);
- unsigned last = first + getNumActivityArguments(expr);
- IHqlExpression * child0 = expr->queryChild(0);
- markAsUnconditional(child0, info->graph, condition);
- queryResourceInfo(child0)->graph->hasConditionSource = true; // force it to generate even if contains something very simple e.g., null action
- for (unsigned idx = first+1; idx < last; idx++)
- {
- OwnedHqlExpr tag = createAttribute(instanceAtom, LINK(expr), getSizetConstant(idx), LINK(condition));
- IHqlExpression * child = expr->queryChild(idx);
- markAsUnconditional(child, queryResourceInfo(child)->graph, tag);
- queryResourceInfo(child)->setConditionSource(tag, !wasConditional);
- }
- markChildDependentsAsUnconditional(info, condition);
- return;
- }
- break;
- case no_case:
- case no_map:
- UNIMPLEMENTED;
- }
- unsigned first = getFirstActivityArgument(expr);
- unsigned last = first + getNumActivityArguments(expr);
- for (unsigned idx=first; idx < last; idx++)
- markAsUnconditional(expr->queryChild(idx), info->graph, condition);
- markChildDependentsAsUnconditional(info, condition);
- }
- void EclResourcer::markConditionBranch(unsigned childIndex, IHqlExpression * expr, IHqlExpression * condition, bool wasConditional)
- {
- IHqlExpression * child = queryRealChild(expr, childIndex);
- if (child)
- {
- OwnedHqlExpr tag;
- if (expr->getOperator() == no_if)
- tag.setown(createAttribute(((childIndex==1) ? trueAtom : falseAtom), LINK(expr), LINK(condition)));
- else
- tag.setown(createAttribute(trueAtom, LINK(expr), LINK(condition), getSizetConstant(childIndex)));
- markAsUnconditional(child, queryResourceInfo(child)->graph, tag);
- queryResourceInfo(child)->setConditionSource(tag, !wasConditional);
- }
- }
- void EclResourcer::markCondition(IHqlExpression * expr, IHqlExpression * condition, bool wasConditional)
- {
- if (expr->getOperator() == no_filter)
- {
- markConditionBranch(0, expr, condition, wasConditional);
- }
- else
- {
- ForEachChildFrom(i, expr, 1)
- markConditionBranch(i, expr, condition, wasConditional);
- }
- }
- void EclResourcer::markConditions(HqlExprArray & exprs)
- {
- ForEachItemIn(idx, exprs)
- markAsUnconditional(&exprs.item(idx), NULL, NULL);
- ForEachItemIn(idx2, rootConditions)
- markCondition(&rootConditions.item(idx2), NULL, false);
- }
- //------------------------------------------------------------------------------------------
- // PASS4: Split subgraphs based on resource requirements for activities
- //This will need to be improved if we allow backtracking to get the best combination of activities to fit in the subgraph
- void EclResourcer::createResourceSplit(IHqlExpression * expr, IHqlExpression * owner, ResourceGraphInfo * ownerNewGraph, ResourceGraphInfo * originalGraph)
- {
- ResourcerInfo * info = queryResourceInfo(expr);
- info->graph.setown(createGraph());
- info->graph->isUnconditional = originalGraph->isUnconditional;
- changeGraph(expr, info->graph);
- connectGraphs(info->graph, expr, ownerNewGraph, owner, UnconditionalLink);
- info->numExternalUses++;
- }
- void EclResourcer::getResources(IHqlExpression * expr, CResources & exprResources)
- {
- ::getResources(expr, exprResources, options);
- }
- bool EclResourcer::calculateResourceSpillPoints(IHqlExpression * expr, ResourceGraphInfo * graph, CResources & resourcesSoFar, bool hasGoodSpillPoint, bool canSpill)
- {
- ResourcerInfo * info = queryResourceInfo(expr);
- if (!info || !info->containsActivity)
- return true;
- if (!info->isActivity)
- {
- unsigned first = getFirstActivityArgument(expr);
- unsigned last = first + getNumActivityArguments(expr);
- if (last - first == 1)
- return calculateResourceSpillPoints(expr->queryChild(first), graph, resourcesSoFar, hasGoodSpillPoint, canSpill);
- for (unsigned idx = first; idx < last; idx++)
- calculateResourceSpillPoints(expr->queryChild(idx), graph, resourcesSoFar, false, canSpill);
- return true;
- }
- if (info->graph != graph)
- return true;
- CResources exprResources(clusterSize);
- getResources(expr, exprResources);
- info->isSpillPoint = false;
- Owned<CResources> curResources = LINK(&resourcesSoFar);
- if (resourcesSoFar.addExceeds(exprResources, *resourceLimit))
- {
- if (hasGoodSpillPoint)
- return false;
- info->isSpillPoint = true;
- spilled = true;
- curResources.setown(new CResources(clusterSize));
- if (exprResources.exceeds(*resourceLimit))
- throwError2(HQLERR_CannotResourceActivity, getOpString(expr->getOperator()), clusterSize);
- }
- if (options.minimizeSkewBeforeSpill)
- {
- if (canSpill && heavyweightAndReducesSizeOrSkew(expr))
- {
- //if the input activity is going to cause us to run out of resources, then it is going to be better to split here than anywhere else
- //this code may conceivably cause extra spills far away because the hash distributes are moved.
- IHqlExpression * childExpr = expr->queryChild(0);
- ResourcerInfo * childInfo = queryResourceInfo(childExpr);
- if (childInfo->graph == graph)
- {
- CResources childResources(clusterSize);
- getResources(childExpr, childResources);
- childResources.add(exprResources);
- if (curResources->addExceeds(childResources, *resourceLimit))
- {
- info->isSpillPoint = true;
- spilled = true;
- calculateResourceSpillPoints(childExpr, graph, exprResources, false, true);
- return true;
- }
- }
- //otherwise continue as normal.
- }
- }
- curResources->add(exprResources);
- unsigned first = getFirstActivityArgument(expr);
- unsigned last = first + getNumActivityArguments(expr);
- if (hasGoodSpillPoint)
- {
- if (exprResources.resource[RESheavy] || exprResources.resource[REShashdist] || last-first != 1)
- hasGoodSpillPoint = false;
- }
- else if (!info->isSpillPoint && canSpill)
- {
- if (lightweightAndReducesDatasetSize(expr) || queryHint(expr, spillAtom))
- {
- CResources savedResources(*curResources);
- if (!calculateResourceSpillPoints(expr->queryChild(0), graph, *curResources, true, true))
- {
- curResources->set(savedResources);
- info->isSpillPoint = true;
- spilled = true;
- calculateResourceSpillPoints(expr->queryChild(0), graph, exprResources, false, true);
- }
- return true;
- }
- }
- node_operator op = expr->getOperator();
- if ((op == no_if) || (op == no_choose) || (op == no_chooseds))
- {
- //For conditions, spill on intersection of resources used, not union.
- CResources savedResources(*curResources);
- if (!calculateResourceSpillPoints(expr->queryChild(1), graph, *curResources, hasGoodSpillPoint, true))
- return false;
- ForEachChildFrom(i, expr, 2)
- {
- if (expr->queryChild(i)->isAttribute())
- continue;
- if (!calculateResourceSpillPoints(expr->queryChild(i), graph, savedResources, hasGoodSpillPoint, true))
- return false;
- curResources->maximize(savedResources);
- }
- }
- else
- {
- for (unsigned idx = first; idx < last; idx++)
- if (!calculateResourceSpillPoints(expr->queryChild(idx), graph, *curResources, hasGoodSpillPoint, true))
- return false;
- }
- return true;
- }
- void EclResourcer::insertResourceSpillPoints(IHqlExpression * expr, IHqlExpression * owner, ResourceGraphInfo * ownerOriginalGraph, ResourceGraphInfo * ownerNewGraph)
- {
- ResourcerInfo * info = queryResourceInfo(expr);
- if (!info || !info->containsActivity)
- return;
- if (!info->isActivity)
- {
- unsigned first = getFirstActivityArgument(expr);
- unsigned last = first + getNumActivityArguments(expr);
- for (unsigned idx = first; idx < last; idx++)
- insertResourceSpillPoints(expr->queryChild(idx), expr, ownerOriginalGraph, ownerNewGraph);
- return;
- }
- ResourceGraphInfo * originalGraph = info->graph; //NB: Graph will never cease to exist, so don't need to link.
- if (originalGraph != ownerOriginalGraph)
- return;
- if (info->isSpillPoint)
- createResourceSplit(expr, owner, ownerNewGraph, originalGraph);
- else if (info->graph != ownerNewGraph)
- changeGraph(expr, ownerNewGraph);
- CResources exprResources(clusterSize);
- getResources(expr, exprResources);
- bool ok = info->graph->allocateResources(exprResources, *resourceLimit);
- assertex(ok);
- node_operator op = expr->getOperator();
- if ((op == no_if) || (op == no_choose) || (op == no_chooseds))
- {
- CResources savedResources(info->graph->resources);
- insertResourceSpillPoints(expr->queryChild(1), expr, originalGraph, info->graph);
- ForEachChildFrom(i, expr, 2)
- {
- if (expr->queryChild(i)->isAttribute())
- continue;
- CResources branchResources(info->graph->resources);
- info->graph->resources.set(savedResources);
- insertResourceSpillPoints(expr->queryChild(i), expr, originalGraph, info->graph);
- info->graph->resources.maximize(branchResources);
- }
- }
- else
- {
- unsigned first = getFirstActivityArgument(expr);
- unsigned last = first + getNumActivityArguments(expr);
- for (unsigned idx = first; idx < last; idx++)
- insertResourceSpillPoints(expr->queryChild(idx), expr, originalGraph, info->graph);
- }
- }
- void EclResourcer::resourceSubGraph(ResourceGraphInfo * graph)
- {
- if (graph->beenResourced)
- return;
- graph->beenResourced = true;
- ForEachItemIn(idx, graph->sources)
- resourceSubGraph(graph->sources.item(idx).sourceGraph);
- IHqlExpression * sourceNode = graph->sinks.item(0).sourceNode->queryBody();
- #ifdef _DEBUG
- //Sanity check, ensure there is only a single sink for this graph.
- //However because libraryselects are tightly bound to their library instance there may be multiple library selects.
- //They won't affect the resourcing though, since they'll plug into the same library instance, and the selects use no resources.
- ForEachItemIn(idx2, graph->sinks)
- {
- IHqlExpression * thisSink = graph->sinks.item(idx2).sourceNode->queryBody();
- if (thisSink->getOperator() != no_libraryselect)
- assertex(thisSink == sourceNode);
- }
- #endif
- spilled = false;
- CResources resources(clusterSize);
- calculateResourceSpillPoints(sourceNode, graph, resources, false, false);
- if (spilled)
- insertResourceSpillPoints(sourceNode, NULL, graph, graph);
- else
- graph->resources.set(resources);
- }
- void EclResourcer::resourceSubGraphs(HqlExprArray & exprs)
- {
- ForEachItemIn(idx, graphs)
- resourceSubGraph(&graphs.item(idx));
- }
- //------------------------------------------------------------------------------------------
- // PASS5: Link subrgaphs with dependency information so they don't get merged by accident.
- void EclResourcer::addDependencySource(IHqlExpression * search, ResourceGraphInfo * curGraph, IHqlExpression * expr)
- {
- //MORE: Should we check this doesn't already exist?
- dependencySource.search.append(*LINK(search));
- dependencySource.graphs.append(*LINK(curGraph));
- dependencySource.exprs.append(*LINK(expr));
- }
- void EclResourcer::addDependencyUse(IHqlExpression * search, ResourceGraphInfo * curGraph, IHqlExpression * expr)
- {
- unsigned index = dependencySource.search.find(*search);
- if (index != NotFound)
- {
- if (&dependencySource.graphs.item(index) == curGraph)
- {
- //Don't give a warning if get/set is within the same activity (e.g., within a local())
- if (&dependencySource.exprs.item(index) != expr)
- //errors->reportWarning(HQLWRN_RecursiveDependendencies, HQLWRN_RecursiveDependendencies_Text, *codeGeneratorAtom, 0, 0, 0);
- errors->reportError(HQLWRN_RecursiveDependendencies, HQLWRN_RecursiveDependendencies_Text, codeGeneratorId->str(), 0, 0, 0);
- }
- else
- {
- ResourceGraphLink * link = new ResourceGraphDependencyLink(&dependencySource.graphs.item(index), &dependencySource.exprs.item(index), curGraph, expr);
- curGraph->dependsOn.append(*link);
- links.append(*link);
- }
- }
- }
- void EclResourcer::addRefExprDependency(IHqlExpression * expr, ResourceGraphInfo * curGraph, IHqlExpression * activityExpr)
- {
- IHqlExpression * filename = queryTableFilename(expr);
- if (filename)
- {
- OwnedHqlExpr value = createAttribute(fileAtom, getNormalizedFilename(filename));
- addDependencySource(value, curGraph, activityExpr);
- }
- }
- bool EclResourcer::addExprDependency(IHqlExpression * expr, ResourceGraphInfo * curGraph, IHqlExpression * activityExpr)
- {
- switch (expr->getOperator())
- {
- case no_buildindex:
- case no_output:
- {
- IHqlExpression * filename = queryRealChild(expr, 1);
- if (filename)
- {
- switch (filename->getOperator())
- {
- case no_pipe:
- // allWritten = true;
- break;
- default:
- OwnedHqlExpr value = createAttribute(fileAtom, getNormalizedFilename(filename));
- addDependencySource(value, curGraph, activityExpr);
- break;
- }
- }
- else
- {
- IHqlExpression * seq = querySequence(expr);
- assertex(seq && seq->queryValue());
- IHqlExpression * name = queryResultName(expr);
- OwnedHqlExpr value = createAttribute(resultAtom, LINK(seq), LINK(name));
- addDependencySource(value, curGraph, activityExpr);
- }
- return true;
- }
- case no_keydiff:
- {
- addRefExprDependency(expr->queryChild(0), curGraph, activityExpr);
- addRefExprDependency(expr->queryChild(1), curGraph, activityExpr);
- OwnedHqlExpr value = createAttribute(fileAtom, getNormalizedFilename(expr->queryChild(2)));
- addDependencySource(value, curGraph, activityExpr);
- return true;
- }
- case no_keypatch:
- {
- addRefExprDependency(expr->queryChild(0), curGraph, activityExpr);
- OwnedHqlExpr patchName = createAttribute(fileAtom, getNormalizedFilename(expr->queryChild(1)));
- addDependencyUse(patchName, curGraph, activityExpr);
- OwnedHqlExpr value = createAttribute(fileAtom, getNormalizedFilename(expr->queryChild(2)));
- addDependencySource(value, curGraph, activityExpr);
- return true;
- }
- case no_table:
- {
- IHqlExpression * filename = expr->queryChild(0);
- OwnedHqlExpr value = createAttribute(fileAtom, getNormalizedFilename(filename));
- addDependencyUse(value, curGraph, activityExpr);
- return !filename->isConstant();
- }
- case no_select:
- return isNewSelector(expr);
- case no_workunit_dataset:
- {
- IHqlExpression * sequence = queryAttributeChild(expr, sequenceAtom, 0);
- IHqlExpression * name = queryAttributeChild(expr, nameAtom, 0);
- OwnedHqlExpr value = createAttribute(resultAtom, LINK(sequence), LINK(name));
- addDependencyUse(value, curGraph, activityExpr);
- return false;
- }
- case no_getresult:
- {
- IHqlExpression * sequence = queryAttributeChild(expr, sequenceAtom, 0);
- IHqlExpression * name = queryAttributeChild(expr, namedAtom, 0);
- OwnedHqlExpr value = createAttribute(resultAtom, LINK(sequence), LINK(name));
- addDependencyUse(value, curGraph, activityExpr);
- return false;
- }
- case no_getgraphresult:
- {
- OwnedHqlExpr value = createAttribute(resultAtom, LINK(expr->queryChild(1)), LINK(expr->queryChild(2)));
- addDependencyUse(value, curGraph, activityExpr);
- return false;
- }
- case no_setgraphresult:
- {
- OwnedHqlExpr value = createAttribute(resultAtom, LINK(expr->queryChild(1)), LINK(expr->queryChild(2)));
- addDependencySource(value, curGraph, activityExpr);
- return true;
- }
- case no_ensureresult:
- case no_setresult:
- case no_extractresult:
- {
- IHqlExpression * sequence = queryAttributeChild(expr, sequenceAtom, 0);
- IHqlExpression * name = queryAttributeChild(expr, namedAtom, 0);
- OwnedHqlExpr value = createAttribute(resultAtom, LINK(sequence), LINK(name));
- addDependencySource(value, curGraph, activityExpr);
- return true;
- }
- case no_attr:
- case no_attr_link:
- case no_record:
- case no_field:
- return false; //no need to look any further
- default:
- return true;
- }
- }
- void EclResourcer::doAddChildDependencies(IHqlExpression * expr, ResourceGraphInfo * graph, IHqlExpression * activityExpr)
- {
- if (expr->queryTransformExtra())
- return;
- expr->setTransformExtraUnlinked(expr);
- if (addExprDependency(expr, graph, activityExpr))
- {
- ForEachChild(idx, expr)
- doAddChildDependencies(expr->queryChild(idx), graph, activityExpr);
- }
- }
- void EclResourcer::addChildDependencies(IHqlExpression * expr, ResourceGraphInfo * graph, IHqlExpression * activityExpr)
- {
- if (graph)
- {
- lockTransformMutex();
- doAddChildDependencies(expr, graph, activityExpr);
- unlockTransformMutex();
- }
- }
- void EclResourcer::addDependencies(IHqlExpression * expr, ResourceGraphInfo * graph, IHqlExpression * activityExpr)
- {
- ResourcerInfo * info = queryResourceInfo(expr);
- if (!info)
- return;
- if (info->containsActivity)
- {
- if (info->isActivity)
- {
- if (info->gatheredDependencies)
- return;
- info->gatheredDependencies = true;
- graph = info->graph;
- activityExpr = expr;
- }
- if (addExprDependency(expr, graph, activityExpr))
- {
- unsigned first = getFirstActivityArgument(expr);
- unsigned last = first + getNumActivityArguments(expr);
- ForEachChild(idx, expr)
- {
- if ((idx >= first) && (idx < last))
- addDependencies(expr->queryChild(idx), graph, activityExpr);
- else
- addChildDependencies(expr->queryChild(idx), graph, activityExpr);
- }
- }
- }
- else
- addChildDependencies(expr, graph, activityExpr);
- ForEachItemIn(i, info->childDependents)
- {
- CChildDependent & cur = info->childDependents.item(i);
- if (isResourcedActivity(cur.projectedHoisted))
- {
- addDependencies(cur.projectedHoisted, NULL, NULL);
- ResourcerInfo * sourceInfo = queryResourceInfo(cur.projectedHoisted);
- if (cur.isSingleNode)
- sourceInfo->noteUsedFromChild(cur.forceHoist);
- ResourceGraphLink * link = new ResourceGraphDependencyLink(sourceInfo->graph, cur.projectedHoisted, graph, expr);
- graph->dependsOn.append(*link);
- links.append(*link);
- }
- }
- }
- void EclResourcer::addDependencies(HqlExprArray & exprs)
- {
- ForEachItemIn(idx, exprs)
- addDependencies(&exprs.item(idx), NULL, NULL);
- }
- void EclResourcer::oldSpotUnbalancedSplitters(IHqlExpression * expr, unsigned whichSource, IHqlExpression * path, ResourceGraphInfo * graph)
- {
- ResourcerInfo * info = queryResourceInfo(expr);
- if (!info)
- return;
- if (graph && info->graph && info->graph != graph)
- {
- if ((info->currentSource == whichSource) && (info->pathToSplitter != path))
- graph->unbalancedExternalSources.append(*LINK(expr->queryBody()));
- info->currentSource = whichSource;
- info->pathToSplitter.set(path);
- return;
- }
- else
- {
- if (info->currentSource == whichSource)
- {
- if (info->pathToSplitter != path)
- info->balanced = false;
- return;
- }
- info->currentSource = whichSource;
- info->pathToSplitter.set(path);
- }
- if (info->containsActivity)
- {
- unsigned first = getFirstActivityArgument(expr);
- unsigned num = getNumActivityArguments(expr);
- bool modify = false;
- if (num > 1)
- {
- switch (expr->getOperator())
- {
- case no_addfiles:
- if (expr->hasAttribute(_ordered_Atom) || expr->hasAttribute(_orderedPull_Atom) || isGrouped(expr))
- modify = true;
- break;
- default:
- modify = true;
- break;
- }
- }
- unsigned last = first + num;
- for (unsigned idx = first; idx < last; idx++)
- {
- OwnedHqlExpr childPath = modify ? createAttribute(pathAtom, getSizetConstant(idx), LINK(path)) : LINK(path);
- oldSpotUnbalancedSplitters(expr->queryChild(idx), whichSource, childPath, graph);
- }
- }
- //Now check dependencies between graphs (for roxie)
- if (!graph)
- {
- if (info->graph)
- {
- GraphLinkArray & graphLinks = info->graph->dependsOn;
- ForEachItemIn(i, graphLinks)
- {
- ResourceGraphLink & link = graphLinks.item(i);
- if (link.sinkNode == expr)
- {
- OwnedHqlExpr childPath = createAttribute(dependencyAtom, LINK(link.sourceNode), LINK(path));
- oldSpotUnbalancedSplitters(link.sourceNode, whichSource, childPath, graph);
- }
- }
- }
- else
- {
- ForEachItemIn(i, links)
- {
- ResourceGraphLink & link = links.item(i);
- if (link.sinkNode == expr)
- {
- OwnedHqlExpr childPath = createAttribute(dependencyAtom, LINK(link.sourceNode), LINK(path));
- oldSpotUnbalancedSplitters(link.sourceNode, whichSource, childPath, graph);
- }
- }
- }
- }
- }
- void EclResourcer::oldSpotUnbalancedSplitters(HqlExprArray & exprs)
- {
- unsigned curSource = 1;
- switch (targetClusterType)
- {
- case HThorCluster:
- break;
- case ThorLCRCluster:
- {
- //Thor only handles one graph at a time, so only walk expressions within a single graph.
- ForEachItemIn(i1, graphs)
- {
- ResourceGraphInfo & curGraph = graphs.item(i1);
- ForEachItemIn(i2, curGraph.sinks)
- {
- ResourceGraphLink & cur = curGraph.sinks.item(i2);
- oldSpotUnbalancedSplitters(cur.sourceNode, curSource++, 0, &curGraph);
- }
- }
- }
- break;
- case RoxieCluster:
- {
- //Roxie pulls all at once, so need to analyse globally.
- ForEachItemIn(idx, exprs)
- oldSpotUnbalancedSplitters(&exprs.item(idx), curSource++, 0, NULL);
- break;
- }
- }
- }
- void EclResourcer::spotSharedInputs(IHqlExpression * expr, ResourceGraphInfo * graph)
- {
- ResourcerInfo * info = queryResourceInfo(expr);
- if (!info)
- return;
- if (info->graph && info->graph != graph)
- {
- IHqlExpression * body = expr->queryBody();
- if (!graph->unbalancedExternalSources.contains(*body))
- graph->balancedExternalSources.append(*LINK(body));
- return;
- }
- if (info->isSplit())
- {
- //overload currentSource to track if we have visited this splitter before. It cannot have value value NotFound up to now
- if (info->currentSource == NotFound)
- return;
- info->currentSource = NotFound;
- }
- if (info->containsActivity)
- {
- unsigned first = getFirstActivityArgument(expr);
- unsigned num = getNumActivityArguments(expr);
- unsigned last = first + num;
- for (unsigned idx = first; idx < last; idx++)
- {
- spotSharedInputs(expr->queryChild(idx), graph);
- }
- }
- }
- void EclResourcer::spotSharedInputs()
- {
- //Thor only handles one graph at a time, so only walk expressions within a single graph.
- ForEachItemIn(i1, graphs)
- {
- ResourceGraphInfo & curGraph = graphs.item(i1);
- HqlExprCopyArray visited;
- ForEachItemIn(i2, curGraph.sinks)
- {
- ResourceGraphLink & cur = curGraph.sinks.item(i2);
- IHqlExpression * curExpr = cur.sourceNode->queryBody();
- if (!visited.contains(*curExpr))
- {
- ResourcerInfo * info = queryResourceInfo(curExpr);
- if (!info->isExternalSpill() && !info->expandRatherThanSpill(true))
- {
- spotSharedInputs(curExpr, &curGraph);
- visited.append(*curExpr);
- }
- }
- }
- }
- }
- //--------------------------------------------------------------------------------------------------------------------
- /*
- * Splitters can either have a limited or unlimited read-ahead. A splitter with unlimited read-ahead is likely to use
- * more memory, and needs to be able to spill its read ahead buffer to disk. A splitter with limited read-ahead
- * ("balanced") is likely to be more efficient - but it can also potentially cause deadlock.
- *
- * A balanced splitter can deadlock because each of its output activities is effectively dependent on the others, which
- * can create dependency cycles in the graph.
- *
- * Say you have f(x,y) and g(x,y), if x and y are unbalanced splitters you have directed edges (f->x),(f->y),(g->x),(g->y)
- * which contains no cycles. If x is balanced then you also have (x->f),(x->g) - still without any cycles, but if y is
- * also balanced then you gain the edges (y->f,y->g) which creates a cycle. So either x or y need to become unbalanced.
- *
- * The previous implementation worked by tagging each splitter with the paths that were used to get there, and
- * marking a splitter as unbalanced if there was more than one path from the same output. However that fails to catch the
- * case where you have f(a,b) and g(a,b) which create more complicated cycles.
- *
- * This is essentially a graph traversal problem, where a balanced splitter makes each output dependent on each other
- * (they effectively become bi-directional dependencies). Converting balanced splitters to unbalanced is equivalent to
- * calculating the feedback arc set that will remove all the cycles. (Unfortunately calculating the minimal set is NP
- * hard, but because we are dealing with a specialised graph we can avoid that.)
- *
- * The approach is as follows
- * Loop through each output
- * If not already visited (from another output), then perform a depth-first traversal of the graph for that output
- *
- * When traversing a node we mark it as being visited, and then walk through the other links.
- * If node at the end of the current link is being visited then we have found a cycle - so we need to introduce
- * a unbalanced splitter to remove some of the paths. That splitter must be on the path between the two
- * visits to the nodes.
- * - It could be the node we have just reached
- * - It could be any other node on the route.
- *
- * We can tell which one it is because
- * - if is is the node we have just reached, the visiting link must be from an output, and previous link from node to output
- * - if on the path then exit path will be to an output, and entry path from an output.
- *
- * If not then there must be by definition another node on that path that satisfies that condition.
- *
- * Note: This may over-estimate the number of splitters that need to be marked as unbalanced, but in general it is fairly good.
- */
- CSplitterInfo::CSplitterInfo(EclResourcer & _resourcer, bool _preserveBalanced, bool _ignoreExternalDependencies)
- : resourcer(_resourcer), preserveBalanced(_preserveBalanced), ignoreExternalDependencies(_ignoreExternalDependencies)
- {
- nextBalanceId = 0;
- #ifdef TRACE_BALANCED
- printf("digraph {\n");
- #endif
- }
- CSplitterInfo::~CSplitterInfo()
- {
- if (preserveBalanced)
- restoreBalanced();
- #ifdef TRACE_BALANCED
- printf("}\n");
- #endif
- }
- void CSplitterInfo::addLink(IHqlExpression * source, IHqlExpression * sink, bool isExternal)
- {
- ResourcerInfo * sourceInfo = queryResourceInfo(source);
- #ifdef TRACE_BALANCED
- if (sourceInfo->balanceId == 0)
- {
- sourceInfo->balanceId = ++nextBalanceId;
- printf("\tn%u [label=\"%u\"]; // %s\n", sourceInfo->balanceId, sourceInfo->balanceId, getOpString(source->getOperator()));
- }
- #endif
- if (isExternal)
- {
- if (!externalSources.contains(*source))
- {
- externalSources.append(*source);
- if (preserveBalanced)
- wasBalanced.append(sourceInfo->balanced);
- }
- }
- sourceInfo->balanced = true;
- if (sink)
- {
- CSplitterLink * link = new CSplitterLink(source, sink);
- ResourcerInfo * sinkInfo = queryResourceInfo(sink);
- sourceInfo->balancedLinks.append(*link);
- sinkInfo->balancedLinks.append(*LINK(link));
- #ifdef TRACE_BALANCED
- printf("\tn%u -> n%u;\n", sourceInfo->balanceId, sinkInfo->balanceId);
- #endif
- }
- else
- {
- sinks.append(*source);
- sourceInfo->balancedExternalUses++;
- }
- }
- bool CSplitterInfo::allInputsPulledIndependently(IHqlExpression * expr) const
- {
- switch (expr->getOperator())
- {
- case no_addfiles:
- if (expr->hasAttribute(_ordered_Atom) || expr->hasAttribute(_orderedPull_Atom) || isGrouped(expr))
- return false;
- return true;
- case no_parallel:
- //MORE; This can probably return true - and generate fewer unbalanced splitters.
- break;
- }
- return false;
- }
- bool CSplitterInfo::isSplitOrBranch(IHqlExpression * expr) const
- {
- unsigned num = getNumActivityArguments(expr);
- if (num > 1)
- return true;
- //Is this potentially splitter? Better to have false positives...
- ResourcerInfo * info = queryResourceInfo(expr);
- assertex(info);
- if (info->numUses > 1)
- return true;
- if (info->hasDependency())
- return true;
- return false;
- }
- bool CSplitterInfo::isBalancedSplitter(IHqlExpression * expr) const
- {
- ResourcerInfo * info = queryResourceInfo(expr);
- if (!info->balanced)
- return false;
- unsigned numOutputs = info->balancedExternalUses;
- ForEachItemIn(i, info->balancedLinks)
- {
- CSplitterLink & cur = info->balancedLinks.item(i);
- if (cur.hasSource(expr))
- numOutputs++;
- }
- return (numOutputs > 1);
- }
- void CSplitterInfo::gatherPotentialSplitters(IHqlExpression * expr, IHqlExpression * sink, ResourceGraphInfo * graph, bool isDependency)
- {
- ResourcerInfo * info = queryResourceInfo(expr);
- if (!info)
- return;
- switch (expr->getOperator())
- {
- case no_null:
- case no_fail:
- //Any sources that never generate any rows are always fine as a splitter
- return;
- //MORE: A source that generates a single row, and subsequent rows are never read, is always fine.
- // but if read as a dataset it could deadlock unless there was a 1 row read-ahead.
- }
- bool alreadyVisited = resourcer.checkAlreadyVisited(info);
- if (!alreadyVisited)
- {
- info->resetBalanced();
- #ifdef TRACE_BALANCED
- info->balanceId = 0;
- #endif
- }
- if (graph && info->graph && info->graph != graph)
- {
- if (!ignoreExternalDependencies || !isDependency)
- addLink(expr, sink, true);
- return;
- }
- if (isSplitOrBranch(expr) || !sink)
- {
- addLink(expr, sink, false);
- if (alreadyVisited)
- return;
- if (allInputsPulledIndependently(expr))
- sink = NULL;
- else
- sink = expr;
- }
- if (info->containsActivity)
- {
- unsigned first = getFirstActivityArgument(expr);
- unsigned num = getNumActivityArguments(expr);
- unsigned last = first + num;
- for (unsigned idx = first; idx < last; idx++)
- gatherPotentialSplitters(expr->queryChild(idx), sink, graph, false);
- }
- //MORE: dependencies should have onStart as their sink, and there should be a link between onStart and allSinks.
- //Now check dependencies between graphs (for roxie) and possibly within a graph for thor
- if (info->graph)
- {
- GraphLinkArray & graphLinks = info->graph->dependsOn;
- ForEachItemIn(i, graphLinks)
- {
- ResourceGraphLink & link = graphLinks.item(i);
- if (link.sinkNode == expr)
- gatherPotentialDependencySplitters(link.sourceNode, sink, graph);
- }
- }
- else
- {
- ForEachItemIn(i, resourcer.links)
- {
- ResourceGraphLink & link = resourcer.links.item(i);
- if (link.isDependency() && (link.sinkNode == expr))
- {
- gatherPotentialDependencySplitters(link.sourceNode, sink, graph);
- }
- }
- }
- }
- void CSplitterInfo::gatherPotentialDependencySplitters(IHqlExpression * expr, IHqlExpression * sink, ResourceGraphInfo * graph)
- {
- //Strictly speaking dependencies are executed *before* the activity.
- //I experimented with making them dependent on a onStart pseudo expression, but that doesn't work because
- //there are multiple onStart items (as the dependencies nest).
- //There is still the outside possibility of deadlock between expressions and dependencies of items inside
- //non-ordered concats, but they would only occur in roxie, and this flag is currently ignored by roxie.
- gatherPotentialSplitters(expr, sink, graph, true);
- }
- void CSplitterInfo::restoreBalanced()
- {
- ForEachItemIn(i, externalSources)
- {
- IHqlExpression & cur = externalSources.item(i);
- ResourcerInfo * info = queryResourceInfo(&cur);
- info->balanced = wasBalanced.item(i);
- }
- }
- IHqlExpression * EclResourcer::walkPotentialSplitters(CSplitterInfo & connections, IHqlExpression * expr, const CSplitterLink & link)
- {
- ResourcerInfo * info = queryResourceInfo(expr);
- //If already visited all the links, then no need to do any more.
- if (info->finishedWalkingSplitters())
- return NULL;
- //Are we currently in the process of visiting this node?
- if (info->balancedVisiting)
- {
- #ifdef TRACE_BALANCED
- printf("//Follow %u->%u has problems....\n", queryResourceInfo(link.queryOther(expr))->balanceId, info->balanceId);
- #endif
- if (link.hasSource(expr))
- {
- //Walking up the tree, and the current node is being visited => it is a node that needs to become unbalanced - if it is a balanced splitter
- if (connections.isBalancedSplitter(expr))
- {
- const CSplitterLink * originalLink = info->queryCurrentLink();
- if (originalLink->hasSource(expr))
- return expr->queryBody();
- }
- //Must be a node with multiple inputs, walked from one input, and visited another.
- return backtrackPseudoExpr;
- }
- else
- {
- //found a loop-> need to do something about it.
- return backtrackPseudoExpr;
- }
- }
- #ifdef TRACE_BALANCED
- printf("//Follow %u->%u\n", queryResourceInfo(link.queryOther(expr))->balanceId, info->balanceId);
- #endif
- return walkPotentialSplitterLinks(connections, expr, &link);
- }
- IHqlExpression * EclResourcer::walkPotentialSplitterLinks(CSplitterInfo & connections, IHqlExpression * expr, const CSplitterLink * link)
- {
- ResourcerInfo * info = queryResourceInfo(expr);
- info->balancedVisiting = true;
- //This may iterate through all links again - but will return quickly if already visited
- for (unsigned i=0; i < info->balancedLinks.ordinality(); i++)
- {
- const CSplitterLink & cur = info->balancedLinks.item(i);
- if (&cur != link) // don't walk the link we reached here by
- {
- if (info->balanced || cur.hasSink(expr))
- {
- info->curBalanceLink = i;
- IHqlExpression * problem = walkPotentialSplitters(connections, cur.queryOther(expr), cur);
- if (problem)
- {
- bool forceUnbalanced = false;
- if (problem == backtrackPseudoExpr)
- {
- //both links are to outputs
- if (link && link->hasSource(expr) && cur.hasSource(expr))
- {
- assertex(connections.isBalancedSplitter(expr));
- forceUnbalanced = true;
- }
- }
- else
- forceUnbalanced = (expr->queryBody() == problem);
- if (!forceUnbalanced)
- {
- //No longer visiting - we'll come back here again later
- info->balancedVisiting = false;
- return problem;
- }
- #ifdef TRACE_BALANCED
- printf("\tn%u [color=red];\n", info->balanceId);
- printf("//%u marked as unbalanced\n", info->balanceId);
- #endif
- info->balanced = false;
- }
- }
- }
- }
- info->curBalanceLink = info->balancedLinks.ordinality();
- return NULL;
- }
- void EclResourcer::optimizeConditionalLinks(CSplitterInfo & connections)
- {
- //MORE: IF() can be special cased. If it creates two identical links then one of them can be removed
- //Implement by post processing the links and removing duplicates
- }
- void EclResourcer::walkPotentialSplitters(CSplitterInfo & connections)
- {
- ForEachItemIn(i, connections.sinks)
- {
- IHqlExpression & cur = connections.sinks.item(i);
- ResourcerInfo * info = queryResourceInfo(&cur);
- if (!info->finishedWalkingSplitters())
- {
- IHqlExpression * problem = walkPotentialSplitterLinks(connections, &cur, NULL);
- assertex(!problem);
- }
- }
- }
- void EclResourcer::extractSharedInputs(CSplitterInfo & connections, ResourceGraphInfo & graph)
- {
- ForEachItemIn(i, connections.externalSources)
- {
- IHqlExpression & cur = connections.externalSources.item(i);
- ResourcerInfo * info = queryResourceInfo(&cur);
- if (connections.isBalancedSplitter(&cur))
- {
- //Add two entries for compatibility with old code.
- graph.balancedExternalSources.append(*LINK(cur.queryBody()));
- graph.balancedExternalSources.append(*LINK(cur.queryBody()));
- }
- }
- }
- void EclResourcer::spotUnbalancedSplitters(const HqlExprArray & exprs)
- {
- switch (targetClusterType)
- {
- case HThorCluster:
- break;
- case ThorLCRCluster:
- {
- //Thor only handles one graph at a time, so only walk expressions within a single graph.
- ForEachItemIn(i1, graphs)
- {
- ResourceGraphInfo & curGraph = graphs.item(i1);
- CSplitterInfo info(*this, true, true);
- nextPass();
- ForEachItemIn(i2, curGraph.sinks)
- {
- ResourceGraphLink & cur = curGraph.sinks.item(i2);
- info.gatherPotentialSplitters(cur.sourceNode, NULL, &curGraph, false);
- }
- optimizeConditionalLinks(info);
- walkPotentialSplitters(info);
- extractSharedInputs(info, curGraph);
- }
- }
- break;
- case RoxieCluster:
- {
- //Roxie pulls all at once, so need to analyse globally.
- CSplitterInfo info(*this, false, false);
- nextPass();
- ForEachItemIn(i2, exprs)
- info.gatherPotentialSplitters(&exprs.item(i2), NULL, NULL, false);
- optimizeConditionalLinks(info);
- walkPotentialSplitters(info);
- //no splitters from reading at start of a subgraph
- break;
- }
- }
- }
- //------------------------------------------------------------------------------------------
- // PASS6: Merge sub graphs that can share resources and don't have dependencies
- // MORE: Once sources are merged, should try merging between trees.
- static bool conditionsMatch(const HqlExprArray & left, const HqlExprArray & right)
- {
- if (left.ordinality() != right.ordinality())
- return false;
- ForEachItemIn(i, left)
- {
- if (!left.contains(right.item(i)) || !right.contains(left.item(i)))
- return false;
- }
- return true;
- }
- bool EclResourcer::queryMergeGraphLink(ResourceGraphLink & link)
- {
- if (link.linkKind == UnconditionalLink)
- {
- //Don't combine any dependencies
- const GraphLinkArray & sinks = link.sourceGraph->sinks;
- ForEachItemIn(i1, sinks)
- {
- ResourceGraphLink & cur = sinks.item(i1);
- if (cur.sinkGraph && cur.sourceNode->isAction())
- return false;
- }
- //Roxie pulls all subgraphs at same time, so no problem with conditional links since handled at run time.
- if (options.noConditionalLinks)
- return true;
- //No conditionals in the sink graph=>will be executed just as frequently
- if (!link.sinkGraph->mergedConditionSource)
- return true;
- //Is this the only place this source graph is used? If so, always fine to merge
- if (sinks.ordinality() == 1)
- return true;
- //1) if context the source graph is being merged into is unconditional, then it is ok [ could have conditional and unconditional paths to same graph]
- //2) if context is conditional, then we don't really want to do it unless the conditions on all sinks are identical, and the only links occur between these two graphs.
- // (situation occurs with spill fed into two branches of a join).
- bool isConditionalInSinkGraph = false;
- bool accessedFromManyGraphs = false;
- ForEachItemIn(i, sinks)
- {
- ResourceGraphLink & cur = sinks.item(i);
- if (cur.sinkNode)
- {
- if (cur.sinkGraph != link.sinkGraph)
- accessedFromManyGraphs = true;
- else
- {
- if (!isConditionalInSinkGraph)
- {
- ResourcerInfo * sinkInfo = queryResourceInfo(cur.sinkNode);
- //If this is conditional, don't merge if there is a link to another graph
- if ((!cur.isDependency() && sinkInfo->isConditionExpr()) ||
- //if (sinkInfo->isConditionExpr() ||
- (!sinkInfo->isUnconditional() && sinkInfo->conditions.ordinality()))
- isConditionalInSinkGraph = true;
- }
- }
- }
- }
- if (isConditionalInSinkGraph && accessedFromManyGraphs)
- return false;
- return true;
- }
- return false;
- }
- bool EclResourcer::checkAlreadyVisited(ResourcerInfo * info)
- {
- if (info->lastPass == thisPass)
- return true;
- info->lastPass = thisPass;
- return false;
- }
- unsigned EclResourcer::getMaxDepth() const
- {
- unsigned maxDepth = 0;
- for (unsigned idx = 0; idx < graphs.ordinality(); idx++)
- {
- unsigned depth = graphs.item(idx).getDepth();
- if (depth > maxDepth)
- maxDepth = depth;
- }
- return maxDepth;
- }
- void EclResourcer::mergeSubGraphs(unsigned pass)
- {
- unsigned maxDepth = getMaxDepth();
- for (unsigned curDepth = maxDepth+1; curDepth-- != 0;)
- {
- mergeAgain:
- for (unsigned idx = 0; idx < graphs.ordinality(); idx++)
- {
- ResourceGraphInfo & cur = graphs.item(idx);
- if ((cur.getDepth() == curDepth) && !cur.isDead)
- {
- bool tryAgain;
- do
- {
- tryAgain = false;
- for (unsigned idxSource = 0; idxSource < cur.sources.ordinality(); /*incremented in loop*/)
- {
- ResourceGraphLink & curLink = cur.sources.item(idxSource);
- ResourceGraphInfo * source = curLink.sourceGraph;
- IHqlExpression * sourceNode = curLink.sourceNode;
- bool tryToMerge;
- bool expandSourceInPlace = queryResourceInfo(sourceNode)->expandRatherThanSpill(false);
- if (pass == 0)
- tryToMerge = !expandSourceInPlace;
- else
- tryToMerge = expandSourceInPlace;
- if (tryToMerge)
- {
- bool ok = true;
- ResourcerInfo * sourceResourceInfo = queryResourceInfo(sourceNode);
- if (sourceResourceInfo->outputToUseForSpill && (targetClusterType == HThorCluster))
- {
- if (curLink.sinkNode != sourceResourceInfo->outputToUseForSpill)
- ok = false;
- }
- if (sequential && source->containsActionSink())
- ok = false;
- unsigned curSourceDepth = source->getDepth();
- //MORE: Merging identical conditionals?
- if (ok && queryMergeGraphLink(curLink) &&
- !sourceResourceInfo->expandRatherThanSplit() &&
- cur.mergeInSource(*source, *resourceLimit))
- {
- //NB: Following cannot remove sources below the current index.
- replaceGraphReferences(source, &cur);
- source->isDead = true;
- #ifdef VERIFY_RESOURCING
- checkRecursion(&cur);
- #endif
- unsigned newDepth = cur.getDepth();
- //Unusual: The source we are merging with has just increased in depth, so any
- //dependents have also increased in depth. Need to try again at different depth
- //to see if one of those will merge in.
- if (newDepth > curSourceDepth)
- {
- curDepth += (newDepth - curSourceDepth);
- goto mergeAgain;
- }
- //depth of this element has changed, so don't check to see if it merges with any other
- //sources on this iteration.
- if (newDepth != curDepth)
- {
- tryAgain = false;
- break;
- }
- tryAgain = true;
- }
- else
- idxSource++;
- }
- else
- idxSource++;
- }
- } while (tryAgain);
- }
- }
- }
- }
- void EclResourcer::mergeSiblings()
- {
- unsigned maxDepth = getMaxDepth();
- for (unsigned curDepth = maxDepth+1; curDepth-- != 0;)
- {
- for (unsigned idx = 0; idx < graphs.ordinality(); idx++)
- {
- ResourceGraphInfo & cur = graphs.item(idx);
- if ((cur.getDepth() == curDepth) && !cur.isDead)
- {
- if (cur.containsActionSink())
- {
- if (!cur.canMergeActionAsSibling(sequential))
- continue;
- }
- ForEachItemIn(idxSource, cur.sources)
- {
- ResourceGraphLink & curLink = cur.sources.item(idxSource);
- ResourceGraphInfo * source = curLink.sourceGraph;
- IHqlExpression * sourceNode = curLink.sourceNode;
- ResourcerInfo * sourceInfo = queryResourceInfo(sourceNode);
- if (sourceInfo->neverSplit || sourceInfo->expandRatherThanSplit())
- continue;
- for (unsigned iSink = 0; iSink < source->sinks.ordinality(); )
- {
- ResourceGraphLink & secondLink = source->sinks.item(iSink);
- ResourceGraphInfo * sink = secondLink.sinkGraph;
- bool ok = false;
- if (sink && (sink != &cur) && !sink->isDead && sourceNode->queryBody() == secondLink.sourceNode->queryBody())
- {
- ok = true;
- if (sequential && !sink->canMergeActionAsSibling(sequential))
- ok = false;
- if (ok && cur.mergeInSibling(*sink, *resourceLimit))
- {
- //NB: Following cannot remove sources below the current index.
- replaceGraphReferences(sink, &cur);
- sink->isDead = true;
- }
- else
- iSink++;
- }
- else
- iSink++;
- }
- }
- }
- }
- }
- }
- void EclResourcer::mergeSubGraphs()
- {
- for (unsigned pass=0; pass < 2; pass++)
- mergeSubGraphs(pass);
- if (options.combineSiblings)
- mergeSiblings();
- ForEachItemInRev(idx2, graphs)
- {
- if (graphs.item(idx2).isDead)
- graphs.remove(idx2);
- }
- }
- //------------------------------------------------------------------------------------------
- // PASS7: Optimize aggregates off of splitters into through aggregates.
- bool EclResourcer::optimizeAggregate(IHqlExpression * expr)
- {
- if (!isSimpleAggregateResult(expr))
- return false;
- //expr is a no_extractresult
- if (queryResourceInfo(expr)->childDependents.ordinality())
- return false;
- IHqlExpression * row2ds = expr->queryChild(0); // no_datasetfromrow
- IHqlExpression * selectNth = row2ds->queryChild(0);
- ResourcerInfo * row2dsInfo = queryResourceInfo(row2ds);
- //If more than one set result for the same aggregate don't merge the aggregation because
- //it messes up the internal count. Should really fix it and do multiple stores in the
- //through aggregate.
- if (row2dsInfo->numInternalUses() > 1)
- return false;
- //Be careful not to lose any spills...
- if (row2dsInfo->numExternalUses)
- return false;
- ResourcerInfo * selectNthInfo = queryResourceInfo(selectNth);
- if (!selectNthInfo || selectNthInfo->numExternalUses)
- return false;
- IHqlExpression * aggregate = selectNth->queryChild(0); // no_newaggregate
- IHqlExpression * parent = aggregate->queryChild(0);
- ResourcerInfo * info = queryResourceInfo(parent);
- if (info->numInternalUses() <= 1)
- return false;
- //ok, we can go ahead and merge.
- info->aggregates.append(*LINK(expr));
- // info->numExternalUses--;
- // info->numUses--;
- return true;
- }
- void EclResourcer::optimizeAggregates()
- {
- for (unsigned idx = 0; idx < graphs.ordinality(); idx++)
- {
- ResourceGraphInfo & cur = graphs.item(idx);
- for (unsigned idxSink = 0; idxSink < cur.sinks.ordinality(); /*incremented in loop*/)
- {
- ResourceGraphLink & link = cur.sinks.item(idxSink);
- if ((link.sinkGraph == NULL) && optimizeAggregate(link.sourceNode))
- cur.sinks.remove(idxSink);
- else
- idxSink++;
- }
- }
- }
- //------------------------------------------------------------------------------------------
- // PASS8: Improve efficiency by merging the split points slightly
- IHqlExpression * EclResourcer::findPredecessor(IHqlExpression * expr, IHqlExpression * search, IHqlExpression * prev)
- {
- if (expr == search)
- return prev;
- ResourcerInfo * info = queryResourceInfo(expr);
- if (info && info->containsActivity)
- {
- unsigned first = getFirstActivityArgument(expr);
- unsigned last = first + getNumActivityArguments(expr);
- for (unsigned idx=first; idx < last; idx++)
- {
- IHqlExpression * match = findPredecessor(expr->queryChild(idx), search, expr);
- if (match)
- return match;
- }
- }
- return NULL;
- }
- IHqlExpression * EclResourcer::findPredecessor(ResourcerInfo * search)
- {
- ForEachItemIn(idx, links)
- {
- ResourceGraphLink & cur = links.item(idx);
- if (cur.sourceGraph == search->graph)
- {
- IHqlExpression * match = findPredecessor(cur.sourceNode, search->original, NULL);
- if (match)
- return match;
- }
- }
- return NULL;
- }
- void EclResourcer::moveExternalSpillPoints()
- {
- if (options.minimizeSpillSize == 0)
- return;
- //if we have a external spill point where all the external outputs reduce their data significantly
- //either via a project or a filter, then it might be worth including those activities in the main
- //graph and ,if all external children reduce data, then may be best to filter
- ForEachItemIn(idx, links)
- {
- ResourceGraphLink & cur = links.item(idx);
- if ((cur.linkKind == UnconditionalLink) && cur.sinkGraph)
- {
- while (lightweightAndReducesDatasetSize(cur.sinkNode))
- {
- ResourcerInfo * sourceInfo = queryResourceInfo(cur.sourceNode);
- if (!sourceInfo->isExternalSpill() || (sourceInfo->numExternalUses > options.minimizeSpillSize))
- break;
- ResourcerInfo * sinkInfo = queryResourceInfo(cur.sinkNode);
- if (sinkInfo->numInternalUses() != 1)
- break;
- IHqlExpression * sinkPred = findPredecessor(sinkInfo);
- sinkInfo->graph.set(cur.sourceGraph);
- sourceInfo->numExternalUses--;
- sinkInfo->numExternalUses++;
- cur.sourceNode.set(cur.sinkNode);
- cur.sinkNode.set(sinkPred);
- }
- }
- }
- }
- //------------------------------------------------------------------------------------------
- // PASS9: Create a new expression tree representing the information
- static HqlTransformerInfo childDependentReplacerInfo("ChildDependentReplacer");
- class ChildDependentReplacer : public MergingHqlTransformer
- {
- public:
- ChildDependentReplacer(const HqlExprCopyArray & _childDependents, const HqlExprArray & _replacements)
- : MergingHqlTransformer(childDependentReplacerInfo), childDependents(_childDependents), replacements(_replacements)
- {
- }
- protected:
- virtual IHqlExpression * createTransformed(IHqlExpression * expr)
- {
- unsigned match = childDependents.find(*expr);
- if (match != NotFound)
- return LINK(&replacements.item(match));
- return MergingHqlTransformer::createTransformed(expr);
- }
- protected:
- const HqlExprCopyArray & childDependents;
- const HqlExprArray & replacements;
- };
- static IHqlExpression * getScalarReplacement(CChildDependent & cur, ResourcerInfo * hoistedInfo, IHqlExpression * replacement)
- {
- //First skip any wrappers which are there to cause things to be hoisted.
- IHqlExpression * value = skipScalarWrappers(cur.original);
- //Now modify the spilled result depending on how the spilled result was created (see EclHoistLocator::noteScalar() above)
- if (value->getOperator() == no_select)
- {
- bool isNew;
- IHqlExpression * ds = querySelectorDataset(value, isNew);
- if(isNew || ds->isDatarow())
- {
- if (cur.hoisted != cur.projectedHoisted)
- {
- assertex(cur.projected);
- unsigned match = hoistedInfo->projected.find(*cur.projected);
- assertex(match != NotFound);
- IHqlExpression * projectedRecord = cur.projectedHoisted->queryRecord();
- IHqlExpression * projectedField = projectedRecord->queryChild(match);
- return createNewSelectExpr(LINK(replacement), LINK(projectedField));
- }
- return replaceSelectorDataset(value, replacement);
- }
- //Very unusual - can occur when a thisnode(somefield) is extracted from an allnodes.
- //It will have gone through the default case in the noteScalar() code
- }
- else if (value->getOperator() == no_createset)
- {
- IHqlExpression * record = replacement->queryRecord();
- IHqlExpression * field = record->queryChild(0);
- return createValue(no_createset, cur.original->getType(), LINK(replacement), createSelectExpr(LINK(replacement->queryNormalizedSelector()), LINK(field)));
- }
- IHqlExpression * record = replacement->queryRecord();
- return createNewSelectExpr(LINK(replacement), LINK(record->queryChild(0)));
- }
- IHqlExpression * EclResourcer::replaceResourcedReferences(ResourcerInfo * info, IHqlExpression * expr)
- {
- if (!isAffectedByResourcing(expr))
- return LINK(expr);
- LinkedHqlExpr mapped = expr;
- if (info && (info->childDependents.ordinality()))
- {
- HqlExprCopyArray originals;
- HqlExprArray replacements;
- ForEachItemIn(i, info->childDependents)
- {
- CChildDependent & cur = info->childDependents.item(i);
- if (!isResourcedActivity(cur.projectedHoisted))
- continue;
- OwnedHqlExpr replacement = createResourced(cur.projectedHoisted, NULL, false, false);
- IHqlExpression * original = cur.original;
- if (!original->isDataset() && !original->isDatarow() && !original->isDictionary())
- replacement.setown(getScalarReplacement(cur, queryResourceInfo(cur.hoisted), replacement));
- originals.append(*original);
- replacements.append(*replacement.getClear());
- }
- if (originals.ordinality())
- {
- ChildDependentReplacer replacer(originals, replacements);
- mapped.setown(replacer.transformRoot(mapped));
- }
- }
- return mapped.getClear();
- }
- IHqlExpression * EclResourcer::doCreateResourced(IHqlExpression * expr, ResourceGraphInfo * ownerGraph, bool expandInParent, bool defineSideEffect)
- {
- ResourcerInfo * info = queryResourceInfo(expr);
- node_operator op = expr->getOperator();
- HqlExprArray args;
- bool same = true;
- unsigned first = getFirstActivityArgument(expr);
- unsigned last = first + getNumActivityArguments(expr);
- OwnedHqlExpr transformed;
- switch (op)
- {
- case no_if:
- case no_choose:
- case no_chooseds:
- {
- ForEachChild(idx, expr)
- {
- IHqlExpression * child = expr->queryChild(idx);
- IHqlExpression * resourced;
- if ((idx < first) || (idx >= last))
- resourced = replaceResourcedReferences(info, child);
- else
- resourced = createResourced(child, ownerGraph, expandInParent, false);
- if (child != resourced)
- same = false;
- args.append(*resourced);
- }
- break;
- }
- case no_case:
- case no_map:
- UNIMPLEMENTED;
- case no_keyed:
- return LINK(expr);
- case no_compound:
- if (options.convertCompoundToExecuteWhen)
- {
- //NB: Arguments to no_executewhen are the reverse of no_compound.
- args.append(*createResourced(expr->queryChild(1), ownerGraph, expandInParent, false));
- args.append(*createResourced(expr->queryChild(0), ownerGraph, expandInParent, false));
- transformed.setown(createDataset(no_executewhen, args));
- }
- else
- transformed.setown(createResourced(expr->queryChild(1), ownerGraph, expandInParent, false));
- break;
- case no_executewhen:
- {
- args.append(*createResourced(expr->queryChild(0), ownerGraph, expandInParent, false));
- args.append(*createResourced(expr->queryChild(1), ownerGraph, expandInParent, false));
- assertex(args.item(1).getOperator() == no_callsideeffect);
- unwindChildren(args, expr, 2);
- same = false;
- break;
- }
- case no_select:
- {
- IHqlExpression * ds = expr->queryChild(0);
- OwnedHqlExpr newDs = createResourced(ds, ownerGraph, expandInParent, false);
- if (ds != newDs)
- {
- args.append(*LINK(newDs));
- unwindChildren(args, expr, 1);
- if (!expr->hasAttribute(newAtom) && isNewSelector(expr) && (newDs->getOperator() != no_select))
- args.append(*LINK(queryNewSelectAttrExpr()));
- same = false;
- }
- break;
- }
- case no_join:
- case no_denormalize:
- case no_denormalizegroup:
- if (false)//if (isKeyedJoin(expr))
- {
- args.append(*createResourced(expr->queryChild(0), ownerGraph, expandInParent, false));
- args.append(*LINK(expr->queryChild(1)));
- unsigned max = expr->numChildren();
- for (unsigned idx=2; idx < max; idx++)
- args.append(*replaceResourcedReferences(info, expr->queryChild(idx)));
- same = false;
- break;
- }
- //fall through...
- default:
- {
- IHqlExpression * activeTable = NULL;
- // Check to see if the activity has a dataset which is in scope for the rest of its arguments.
- // If so we'll need to remap references from the children.
- if (hasActiveTopDataset(expr) && (first != last))
- activeTable = expr->queryChild(0);
- ForEachChild(idx, expr)
- {
- IHqlExpression * child = expr->queryChild(idx);
- IHqlExpression * resourced;
- if ((idx < first) || (idx >= last))
- {
- LinkedHqlExpr mapped = child;
- if (activeTable && isAffectedByResourcing(child))
- {
- IHqlExpression * activeTableTransformed = &args.item(0);
- if (activeTable != activeTableTransformed)
- mapped.setown(scopedReplaceSelector(child, activeTable, activeTableTransformed));
- }
- resourced = replaceResourcedReferences(info, mapped);
- }
- else
- resourced = createResourced(child, ownerGraph, expandInParent, false);
- if (child != resourced)
- same = false;
- args.append(*resourced);
- }
- }
- break;
- }
- if (!transformed)
- transformed.setown(same ? LINK(expr) : expr->clone(args));
- if (!expandInParent)
- {
- if (!transformed->isAction())
- transformed.setown(info->createTransformedExpr(transformed));
- else if (defineSideEffect)
- transformed.setown(createValue(no_definesideeffect, LINK(transformed), createUniqueId()));
- }
- return transformed.getClear();
- }
- /*
- Need to be careful because result should not reuse the same expression tree unless that element is a splitter.
- createResourced()
- {
- if (!isActivity)
- if (!containsActivity)
- replace any refs to the activeTable with whatever it has been mapped to
- else
- recurse
- else if (!isDefinedInSameGraph)
- expand/create reader
- set active table
- else isSplitter and alreadyGeneratedForThisGraph
- return previous result
- else
- create transformed
- }
- */
- void EclResourcer::doCheckRecursion(ResourceGraphInfo * graph, PointerArray & visited)
- {
- visited.append(graph);
- ForEachItemIn(idxD, graph->dependsOn)
- checkRecursion(graph->dependsOn.item(idxD).sourceGraph, visited);
- ForEachItemIn(idxS, graph->sources)
- checkRecursion(graph->sources.item(idxS).sourceGraph, visited);
- visited.pop();
- }
- void EclResourcer::checkRecursion(ResourceGraphInfo * graph, PointerArray & visited)
- {
- if (visited.find(graph) != NotFound)
- throwUnexpected();
- doCheckRecursion(graph, visited);
- }
- void EclResourcer::checkRecursion(ResourceGraphInfo * graph)
- {
- PointerArray visited;
- doCheckRecursion(graph, visited);
- }
- IHqlExpression * EclResourcer::createResourced(IHqlExpression * expr, ResourceGraphInfo * ownerGraph, bool expandInParent, bool defineSideEffect)
- {
- ResourcerInfo * info = queryResourceInfo(expr);
- if (!info || !info->containsActivity)
- return replaceResourcedReferences(info, expr);
- if (!info->isActivity)
- {
- assertex(!defineSideEffect);
- HqlExprArray args;
- bool same = true;
- ForEachChild(idx, expr)
- {
- IHqlExpression * cur = expr->queryChild(idx);
- IHqlExpression * curResourced = createResourced(cur, ownerGraph, expandInParent, false);
- args.append(*curResourced);
- if (cur != curResourced)
- same = false;
- }
- if (same)
- return LINK(expr);
- return expr->clone(args);
- }
- if (info->graph != ownerGraph)
- {
- assertex(!defineSideEffect);
- bool isShared = options.optimizeSharedInputs && ownerGraph && ownerGraph->isSharedInput(expr);
- if (isShared)
- {
- IHqlExpression * mapped = ownerGraph->queryMappedSharedInput(expr->queryBody());
- if (mapped)
- return LINK(mapped);
- }
- IHqlExpression * source;
- if (info->expandRatherThanSpill(true))
- {
- bool expandChildInParent = options.minimiseSpills ? expandInParent : true;
- OwnedHqlExpr resourced = doCreateResourced(expr, ownerGraph, expandChildInParent, false);
- if (queryAddUniqueToActivity(resourced))
- source = appendUniqueAttr(resourced);
- else
- source = LINK(resourced);
- }
- else
- {
- if (!expr->isAction())
- {
- OwnedHqlExpr reason;
- if (ownerGraph && options.checkResources())
- {
- StringBuffer reasonText;
- ownerGraph->getMergeFailReason(reasonText, info->graph, *resourceLimit);
- if (reasonText.length())
- {
- reasonText.insert(0, "Resource limit spill: ");
- reason.setown(createAttribute(_spillReason_Atom, createConstant(reasonText.str())));
- }
- }
- source = info->createSpilledRead(reason);
- }
- else
- {
- IHqlExpression * transformed = info->transformed;
- if (transformed->getOperator() == no_definesideeffect)
- {
- IHqlExpression * uid = info->transformed->queryAttribute(_uid_Atom);
- assertex(uid);
- source = createValue(no_callsideeffect, makeVoidType(), LINK(uid));
- }
- else
- source = LINK(transformed);
- }
- }
- if (isShared)
- {
- if (!source->isAction())
- {
- source = createDatasetF(no_split, source, createAttribute(balancedAtom), createUniqueId(), NULL);
- ownerGraph->addSharedInput(expr->queryBody(), source);
- }
- }
- return source;
- }
- if (!expandInParent && info->transformed && info->isSplit())
- {
- return LINK(info->transformed);
- }
- OwnedHqlExpr resourced = doCreateResourced(expr, ownerGraph, expandInParent, defineSideEffect);
- if (queryAddUniqueToActivity(resourced))// && !resourced->hasAttribute(_internal_Atom))
- resourced.setown(appendUniqueAttr(resourced));
- if (!expandInParent)
- {
- info->transformed = resourced;
- }
- return resourced.getClear();
- }
- void EclResourcer::createResourced(ResourceGraphInfo * graph, HqlExprArray & transformed)
- {
- if (graph->createdGraph || graph->isDead)
- return;
- if (!graph->containsActiveSinks() && (!graph->hasConditionSource))
- return;
- #ifdef VERIFY_RESOURCING
- checkRecursion(graph);
- #endif
- // DBGLOG("Prepare to CreateResourced(%p)", graph);
- if (graph->startedGeneratingResourced)
- throwError(HQLWRN_RecursiveDependendencies);
- graph->startedGeneratingResourced = true;
- ForEachItemIn(idxD, graph->dependsOn)
- createResourced(graph->dependsOn.item(idxD).sourceGraph, transformed);
- ForEachItemIn(idxS, graph->sources)
- createResourced(graph->sources.item(idxS).sourceGraph, transformed);
- // DBGLOG("Create resourced %p", graph);
- //First generate the graphs for all the unconditional sinks
- HqlExprArray args;
- ForEachItemIn(idx, graph->sinks)
- {
- ResourceGraphLink & sink = graph->sinks.item(idx);
- IHqlExpression * sinkNode = sink.sourceNode;
- ResourcerInfo * info = queryResourceInfo(sinkNode);
- //If graph is unconditional, any condition sinks are forced to be generated (and spilt)
- if (!info->transformed)
- {
- // if it is a spiller, then it will be generated from another sink
- if (!info->isExternalSpill())
- {
- IHqlExpression * resourced = createResourced(sinkNode, graph, false, sinkNode->isAction() && sink.sinkGraph);
- assertex(info->transformed);
- args.append(*resourced);
- }
- }
- }
- ForEachItemIn(i2, graph->sinks)
- {
- ResourceGraphLink & sink = graph->sinks.item(i2);
- IHqlExpression * sinkNode = sink.sourceNode;
- ResourcerInfo * info = queryResourceInfo(sinkNode);
- IHqlExpression * splitter = info->splitterOutput;
- if (splitter && !args.contains(*splitter))
- args.append(*LINK(splitter));
- }
- if (args.ordinality() == 0)
- graph->isDead = true;
- else
- {
- if (options.useGraphResults || options.alwaysUseGraphResults)
- args.append(*createAttribute(childAtom));
- graph->createdGraph.setown(createValue(no_subgraph, makeVoidType(), args));
- transformed.append(*LINK(graph->createdGraph));
- }
- }
- void EclResourcer::inheritRedundantDependencies(ResourceGraphInfo * thisGraph)
- {
- if (thisGraph->inheritedExpandedDependencies)
- return;
- thisGraph->inheritedExpandedDependencies = true;
- ForEachItemIn(idx, thisGraph->sources)
- {
- ResourceGraphLink & cur = thisGraph->sources.item(idx);
- if (cur.isRedundantLink())
- {
- inheritRedundantDependencies(cur.sourceGraph);
- ForEachItemIn(i, cur.sourceGraph->dependsOn)
- {
- ResourceGraphLink & curDepend = cur.sourceGraph->dependsOn.item(i);
- ResourceGraphLink * link = new ResourceGraphDependencyLink(curDepend.sourceGraph, curDepend.sourceNode, thisGraph, cur.sinkNode);
- thisGraph->dependsOn.append(*link);
- links.append(*link);
- }
- }
- }
- }
- void EclResourcer::createResourced(HqlExprArray & transformed)
- {
- //Before removing null links (e.g., where the source graph is expanded inline), need to make sure
- //dependencies are cloned, otherwise graphs can be generated in the wrong order
- ForEachItemIn(idx1, graphs)
- inheritRedundantDependencies(&graphs.item(idx1));
- ForEachItemInRev(idx2, links)
- {
- ResourceGraphLink & cur = links.item(idx2);
- if (cur.isRedundantLink())
- removeLink(cur, true);
- }
- ForEachItemIn(idx3, graphs)
- createResourced(&graphs.item(idx3), transformed);
- }
- static int compareGraphDepth(CInterface * const * _l, CInterface * const * _r)
- {
- ResourceGraphInfo * l = (ResourceGraphInfo *)*_l;
- ResourceGraphInfo * r = (ResourceGraphInfo *)*_r;
- return l->getDepth() - r->getDepth();
- }
- static int compareLinkDepth(CInterface * const * _l, CInterface * const * _r)
- {
- ResourceGraphLink * l = (ResourceGraphLink *)*_l;
- ResourceGraphLink * r = (ResourceGraphLink *)*_r;
- int diff = l->sourceGraph->getDepth() - r->sourceGraph->getDepth();
- if (diff) return diff;
- if (l->sinkGraph)
- if (r->sinkGraph)
- return l->sinkGraph->getDepth() - r->sinkGraph->getDepth();
- else
- return -1;
- else
- if (r->sinkGraph)
- return +1;
- else
- return 0;
- }
- void EclResourcer::display(StringBuffer & out)
- {
- CIArrayOf<ResourceGraphInfo> sortedGraphs;
- ForEachItemIn(j1, graphs)
- sortedGraphs.append(OLINK(graphs.item(j1)));
- sortedGraphs.sort(compareGraphDepth);
- out.append("Graphs:\n");
- ForEachItemIn(i, sortedGraphs)
- {
- ResourceGraphInfo & cur = sortedGraphs.item(i);
- out.appendf("%d: depth(%d) uncond(%d) cond(%d) %s {%p}\n", i, cur.getDepth(), cur.isUnconditional, cur.hasConditionSource, cur.isDead ? "dead" : "", &cur);
- ForEachItemIn(j, cur.sources)
- {
- ResourceGraphLink & link = cur.sources.item(j);
- out.appendf(" Source: %p %s\n", link.sinkNode.get(), getOpString(link.sinkNode->getOperator()));
- }
- ForEachItemIn(k, cur.sinks)
- {
- ResourceGraphLink & link = cur.sinks.item(k);
- IHqlExpression * sourceNode = link.sourceNode;
- ResourcerInfo * sourceInfo = queryResourceInfo(sourceNode);
- out.appendf(" Sink: %p %s cond(%d,%d) int(%d) ext(%d)\n", sourceNode, getOpString(sourceNode->getOperator()), sourceInfo->conditions.ordinality(), sourceInfo->conditionSourceCount, sourceInfo->numInternalUses(), sourceInfo->numExternalUses);
- }
- ForEachItemIn(i3, dependencySource.graphs)
- {
- if (&dependencySource.graphs.item(i3) == &cur)
- {
- StringBuffer s;
- toECL(&dependencySource.search.item(i3), s);
- out.appendf(" Creates: %s\n", s.str());
- }
- }
- }
- out.append("Links:\n");
- CIArrayOf<ResourceGraphLink> sortedLinks;
- ForEachItemIn(j2, links)
- sortedLinks.append(OLINK(links.item(j2)));
- sortedLinks.sort(compareLinkDepth);
- ForEachItemIn(i2, sortedLinks)
- {
- ResourceGraphLink & link = sortedLinks.item(i2);
- unsigned len = out.length();
- out.appendf(" Source: %d %s", sortedGraphs.find(*link.sourceGraph), getOpString(link.sourceNode->getOperator()));
- if (link.sinkNode)
- {
- out.padTo(len+30);
- out.appendf(" Sink: %d %s", sortedGraphs.find(*link.sinkGraph), getOpString(link.sinkNode->getOperator()));
- }
- if (link.linkKind == SequenceLink)
- out.append(" <sequence>");
- out.newline();
- }
- }
- void EclResourcer::trace()
- {
- StringBuffer s;
- display(s);
- DBGLOG("%s", s.str());
- }
- //---------------------------------------------------------------------------
- static void expandLists(node_operator op, HqlExprArray & args, IHqlExpression * expr);
- static void expandChildren(node_operator op, HqlExprArray & args, IHqlExpression * expr)
- {
- ForEachChild(idx, expr)
- expandLists(op, args, expr->queryChild(idx));
- }
- static void expandLists(node_operator op, HqlExprArray & args, IHqlExpression * expr)
- {
- switch (expr->getOperator())
- {
- case no_sequential:
- case no_orderedactionlist:
- if (op != no_parallel)
- expandChildren(no_sequential, args, expr);
- else
- args.append(*LINK(expr));
- break;
- case no_comma:
- case no_compound:
- case no_actionlist:
- case no_parallel:
- if (op != no_sequential)
- expandChildren(no_parallel, args, expr);
- else
- args.append(*LINK(expr));
- break;
- case no_null:
- break;
- default:
- args.append(*LINK(expr));
- break;
- }
- }
- void EclResourcer::resourceGraph(IHqlExpression * expr, HqlExprArray & transformed)
- {
- HqlExprArray exprs;
- node_operator expandOp = options.isChildQuery ? no_any : no_parallel;
- expandLists(expandOp, exprs, expr);
- //NB: This only resources a single level of queries. SubQueries should be resourced in a separate
- //pass so that commonality between different activities/subgraphs isn't introduced/messed up.
- findSplitPoints(exprs);
- createInitialGraphs(exprs);
- markConditions(exprs);
- if (options.checkResources())
- resourceSubGraphs(exprs);
- addDependencies(exprs);
- #ifdef TRACE_RESOURCING
- trace();
- #endif
- mergeSubGraphs();
- #ifdef TRACE_RESOURCING
- trace();
- #endif
- if (!options.newBalancedSpotter)
- {
- oldSpotUnbalancedSplitters(exprs);
- if (options.optimizeSharedInputs)
- spotSharedInputs();
- }
- else
- spotUnbalancedSplitters(exprs);
- if (spotThroughAggregate)
- optimizeAggregates();
- moveExternalSpillPoints();
- createResourced(transformed);
- }
- void EclResourcer::resourceRemoteGraph(IHqlExpression * expr, HqlExprArray & transformed)
- {
- HqlExprArray exprs;
- expandLists(no_any, exprs, expr);
- //NB: This only resources a single level of queries. SubQueries should be resourced in a separate
- //pass so that commonality between different activities/subgraphs isn't introduced/messed up.
- findSplitPoints(exprs);
- createInitialRemoteGraphs(exprs);
- markConditions(exprs);
- addDependencies(exprs);
- #ifdef TRACE_RESOURCING
- trace();
- #endif
- mergeSubGraphs();
- #ifdef TRACE_RESOURCING
- trace();
- #endif
- createResourced(transformed);
- }
- //---------------------------------------------------------------------------
- IHqlExpression * resourceThorGraph(HqlCppTranslator & translator, IHqlExpression * expr, ClusterType targetClusterType, unsigned clusterSize, IHqlExpression * graphIdExpr)
- {
- HqlExprArray transformed;
- {
- EclResourcer resourcer(translator.queryErrorProcessor(), translator.wu(), targetClusterType, clusterSize, translator.queryOptions(), translator.querySpillSequence());
- if (graphIdExpr)
- resourcer.setNewChildQuery(graphIdExpr, 0);
- resourcer.resourceGraph(expr, transformed);
- }
- hoistNestedCompound(translator, transformed);
- return createActionList(transformed);
- }
- static IHqlExpression * doResourceGraph(HqlCppTranslator & translator, HqlExprCopyArray * activeRows, IHqlExpression * expr,
- ClusterType targetClusterType, unsigned clusterSize,
- IHqlExpression * graphIdExpr, unsigned numResults, bool isChild, bool useGraphResults)
- {
- HqlExprArray transformed;
- unsigned totalResults;
- {
- EclResourcer resourcer(translator.queryErrorProcessor(), translator.wu(), targetClusterType, clusterSize, translator.queryOptions(), translator.querySpillSequence());
- if (isChild)
- resourcer.setChildQuery(true);
- resourcer.setNewChildQuery(graphIdExpr, numResults);
- resourcer.setUseGraphResults(useGraphResults);
- if (isSequentialActionList(expr))
- resourcer.setSequential(true);
- if (activeRows)
- resourcer.tagActiveCursors(*activeRows);
- resourcer.resourceGraph(expr, transformed);
- totalResults = resourcer.numGraphResults();
- }
- hoistNestedCompound(translator, transformed);
- if (totalResults == 0)
- totalResults = 1;
- transformed.append(*createAttribute(numResultsAtom, getSizetConstant(totalResults)));
- transformed.append(*LINK(graphIdExpr));
- if (isSequentialActionList(expr))
- transformed.append(*createAttribute(sequentialAtom));
- return createValue(no_subgraph, makeVoidType(), transformed);
- }
- IHqlExpression * resourceLibraryGraph(HqlCppTranslator & translator, IHqlExpression * expr, ClusterType targetClusterType, unsigned clusterSize, IHqlExpression * graphIdExpr, unsigned numResults)
- {
- return doResourceGraph(translator, NULL, expr, targetClusterType, clusterSize, graphIdExpr, numResults, false, true); //?? what value for isChild (e.g., thor library call). Need to gen twice?
- }
- IHqlExpression * resourceNewChildGraph(HqlCppTranslator & translator, HqlExprCopyArray & activeRows, IHqlExpression * expr, ClusterType targetClusterType, IHqlExpression * graphIdExpr, unsigned numResults)
- {
- return doResourceGraph(translator, &activeRows, expr, targetClusterType, 0, graphIdExpr, numResults, true, true);
- }
- IHqlExpression * resourceLoopGraph(HqlCppTranslator & translator, HqlExprCopyArray & activeRows, IHqlExpression * expr, ClusterType targetClusterType, IHqlExpression * graphIdExpr, unsigned numResults, bool insideChildQuery)
- {
- return doResourceGraph(translator, &activeRows, expr, targetClusterType, 0, graphIdExpr, numResults, insideChildQuery, true);
- }
- IHqlExpression * resourceRemoteGraph(HqlCppTranslator & translator, IHqlExpression * expr, ClusterType targetClusterType, unsigned clusterSize)
- {
- HqlExprArray transformed;
- {
- EclResourcer resourcer(translator.queryErrorProcessor(), translator.wu(), targetClusterType, clusterSize, translator.queryOptions(), translator.querySpillSequence());
- resourcer.resourceRemoteGraph(expr, transformed);
- }
- hoistNestedCompound(translator, transformed);
- return createActionList(transformed);
- }
- /*
- Conditions:
- They are nasty. We process the tree in two passes. First we tag anything which must be evaluated, and
- save a list of condition statements to process later.
- Second pass we tag conditionals.
- a) all left and right branches of a condition are tagged. [conditionSourceCount]
- b) all conditional expressions are tagged with the conditions they are evaluated for.
- [if the condition lists match then it should be possible to merge the graphs]
- c) The spill count for an node should ignore the number of links from conditional graphs,
- but should add the number of conditions.
- d) if (a, b(f1) +b(f2), c) needs to link b twice though!
- */
- /*
- This transformer converts spill activities to no_dataset/no_output, and also converts splitters of splitters into
- a single splitter.
- */
- class SpillActivityTransformer : public NewHqlTransformer
- {
- public:
- SpillActivityTransformer(bool _createGraphResults);
- protected:
- virtual void analyseExpr(IHqlExpression * expr);
- virtual IHqlExpression * createTransformed(IHqlExpression * expr);
- bool isUnbalanced(IHqlExpression * body)
- {
- ANewTransformInfo * info = queryTransformExtra(body);
- return info->spareByte1 != 0;
- }
- void setUnbalanced(IHqlExpression * body)
- {
- ANewTransformInfo * info = queryTransformExtra(body);
- info->spareByte1 = true;
- }
- protected:
- bool createGraphResults;
- };
- static HqlTransformerInfo spillActivityTransformerInfo("SpillActivityTransformer");
- SpillActivityTransformer::SpillActivityTransformer(bool _createGraphResults)
- : NewHqlTransformer(spillActivityTransformerInfo), createGraphResults(_createGraphResults)
- {
- }
- void SpillActivityTransformer::analyseExpr(IHqlExpression * expr)
- {
- IHqlExpression * body = expr->queryBody();
- if (alreadyVisited(body))
- return;
- //If splitters are commoned up ensure unbalanced splitters stay unbalanced.
- if ((body->getOperator() == no_split) && !body->hasAttribute(balancedAtom))
- {
- IHqlExpression * splitter = NULL;
- IHqlExpression * cur = body->queryChild(0);
- loop
- {
- node_operator op = cur->getOperator();
- if (op == no_split)
- splitter = cur;
- else if (op != no_commonspill)
- break;
- cur = cur->queryChild(0);
- }
- if (splitter)
- setUnbalanced(splitter->queryBody());
- }
- NewHqlTransformer::analyseExpr(expr);
- }
- IHqlExpression * SpillActivityTransformer::createTransformed(IHqlExpression * expr)
- {
- IHqlExpression * annotation = queryTransformAnnotation(expr);
- if (annotation)
- return annotation;
- switch (expr->getOperator())
- {
- case no_split:
- {
- OwnedHqlExpr input = transform(expr->queryChild(0));
- if (input->getOperator() == no_split)
- return input.getClear();
- OwnedHqlExpr transformed = NewHqlTransformer::createTransformed(expr);
- if (transformed->hasAttribute(balancedAtom) && isUnbalanced(expr))
- return removeAttribute(transformed, balancedAtom);
- return transformed.getClear();
- }
- case no_writespill:
- {
- HqlExprArray args;
- transformChildren(expr, args);
- if (createGraphResults)
- return createValue(no_setgraphresult, makeVoidType(), args);
- return createValue(no_output, makeVoidType(), args);
- }
- case no_commonspill:
- return transform(expr->queryChild(0));
- case no_readspill:
- {
- OwnedHqlExpr ds = transform(expr->queryChild(0));
- node_operator readOp = createGraphResults ? no_getgraphresult : no_table;
- HqlExprArray args;
- if (!createGraphResults)
- args.append(*transform(expr->queryChild(1)));
- args.append(*LINK(ds->queryRecord()));
- if (createGraphResults)
- args.append(*transform(expr->queryChild(1)));
- ForEachChildFrom(i, expr, 2)
- {
- IHqlExpression * cur = expr->queryChild(i);
- args.append(*transform(cur));
- }
- IHqlExpression * recordCountAttr = queryRecordCountInfo(expr);
- if (recordCountAttr)
- args.append(*LINK(recordCountAttr));
- OwnedHqlExpr ret;
- if (expr->isDatarow())
- ret.setown(createRow(readOp, args));
- else if (expr->isDictionary())
- ret.setown(createDictionary(readOp, args));
- else
- ret.setown(createDataset(readOp, args));
- const bool loseDistribution = false;
- return preserveTableInfo(ret, ds, loseDistribution, NULL);
- }
- }
- return NewHqlTransformer::createTransformed(expr);
- }
- IHqlExpression * convertSpillsToActivities(IHqlExpression * expr, bool createGraphResults)
- {
- SpillActivityTransformer transformer(createGraphResults);
- transformer.analyse(expr, 0);
- return transformer.transformRoot(expr);
- }
|