hqlcppds.cpp 168 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #include "jliball.hpp"
  14. #include "hql.hpp"
  15. #include "platform.h"
  16. #include "jlib.hpp"
  17. #include "jmisc.hpp"
  18. #include "jstream.ipp"
  19. #include "jdebug.hpp"
  20. #include "hql.hpp"
  21. #include "hqlthql.hpp"
  22. #include "hqlhtcpp.ipp"
  23. #include "hqlttcpp.ipp"
  24. #include "hqlutil.hpp"
  25. #include "hqlthql.hpp"
  26. #include "hqlpmap.hpp"
  27. #include "hqlattr.hpp"
  28. #include "hqlwcpp.hpp"
  29. #include "hqlcpputil.hpp"
  30. #include "hqltcppc.ipp"
  31. #include "hqlopt.hpp"
  32. #include "hqlfold.hpp"
  33. #include "hqlcerrors.hpp"
  34. #include "hqlcatom.hpp"
  35. #include "hqlresource.hpp"
  36. #include "hqlregex.ipp"
  37. #include "hqlsource.ipp"
  38. #include "hqlcse.ipp"
  39. #include "hqlgraph.ipp"
  40. #include "hqlccommon.hpp"
  41. #include "hqliter.ipp"
  42. #include "hqlinline.hpp"
  43. #include "hqlusage.hpp"
  44. #include "hqlcppds.hpp"
  45. #define MAX_FIXED_SIZE_RAW 1024
  46. #define INLINE_TABLE_EXPAND_LIMIT 4
  47. void addGraphIdAttribute(ActivityInstance * instance, BuildCtx & ctx, IHqlExpression * graphId)
  48. {
  49. SubGraphInfo * match = matchActiveGraph(ctx, graphId);
  50. assertex(match);
  51. instance->addAttributeInt("_graphId", match->graphId);
  52. }
  53. //===========================================================================
  54. void HqlCppTranslator::doBuildRowIfBranch(BuildCtx & initctx, BuildCtx & ctx, BoundRow * targetRow, IHqlExpression * branchExpr)
  55. {
  56. IHqlExpression * targetRowExpr = targetRow->queryBound();
  57. Owned<IReferenceSelector> rowSelector = buildNewRow(ctx, branchExpr);
  58. Owned<BoundRow> boundRow = rowSelector->getRow(ctx);
  59. OwnedHqlExpr rowExpr = getPointer(boundRow->queryBound());
  60. OwnedHqlExpr castRow = createValue(no_implicitcast, targetRowExpr->getType(), LINK(rowExpr));
  61. ctx.addAssign(targetRowExpr, castRow);
  62. if (rowSelector->isConditional())
  63. targetRow->setConditional(true);
  64. }
  65. IReferenceSelector * HqlCppTranslator::doBuildRowIf(BuildCtx & ctx, IHqlExpression * expr)
  66. {
  67. OwnedHqlExpr foldedCond = foldHqlExpression(expr->queryChild(0));
  68. if (foldedCond->queryValue())
  69. {
  70. unsigned branch = (foldedCond->queryValue()->getBoolValue()) ? 1 : 2;
  71. return buildNewRow(ctx, expr->queryChild(branch));
  72. }
  73. IHqlExpression * trueBranch = expr->queryChild(1);
  74. IHqlExpression * falseBranch = expr->queryChild(2);
  75. //Ideally should have a constant modifier on the following row...
  76. Owned<ITypeInfo> rowType = makeReferenceModifier(expr->getType());
  77. OwnedHqlExpr rowExpr = ctx.getTempDeclare(rowType, NULL);
  78. Owned<BoundRow> row = createBoundRow(expr->queryBody(), rowExpr);
  79. //MORE: Need casts because cursor may be (probably are) constant, but temporary isn't
  80. //should find out by looking at the a const modifier on the cursor.
  81. BuildCtx condctx(ctx);
  82. IHqlStmt * cond = buildFilterViaExpr(condctx, foldedCond);
  83. //Mark the context as conditional after the filter test, so any temporaries from the filter aren't affected.
  84. condctx.associateExpr(queryConditionalRowMarker(), rowExpr);
  85. doBuildRowIfBranch(ctx, condctx, row, trueBranch);
  86. condctx.selectElse(cond);
  87. condctx.associateExpr(queryConditionalRowMarker(), rowExpr);
  88. doBuildRowIfBranch(ctx, condctx, row, falseBranch);
  89. ctx.associate(*row);
  90. return createReferenceSelector(row);
  91. }
  92. IReferenceSelector * HqlCppTranslator::doBuildRowDeserializeRow(BuildCtx & ctx, IHqlExpression * expr)
  93. {
  94. IHqlExpression * srcRow = expr->queryChild(0);
  95. IHqlExpression * record = expr->queryRecord();
  96. Owned<BoundRow> tempRow = declareLinkedRow(ctx, expr, false);
  97. CHqlBoundTarget target;
  98. target.expr.set(tempRow->queryBound());
  99. HqlExprArray args;
  100. args.append(*createRowAllocator(ctx, record));
  101. args.append(*createRowSerializer(ctx, record, deserializerAtom));
  102. args.append(*LINK(srcRow));
  103. Owned<ITypeInfo> resultType = makeReferenceModifier(makeAttributeModifier(makeRowType(record->getType()), getLinkCountedAttr()));
  104. OwnedHqlExpr call = bindFunctionCall(rtlDeserializeRowAtom, args, resultType);
  105. buildExprAssign(ctx, target, call);
  106. ctx.associate(*tempRow);
  107. return createReferenceSelector(tempRow);
  108. }
  109. void HqlCppTranslator::buildConstRow(IHqlExpression * record, IHqlExpression * rowData, CHqlBoundExpr & bound)
  110. {
  111. OwnedHqlExpr marker = createAttribute(rowAtom, LINK(record), LINK(rowData));
  112. BuildCtx declareCtx(*code, literalAtom);
  113. if (declareCtx.getMatchExpr(marker, bound))
  114. return;
  115. //MORE: This probably needs to go in the header as well...
  116. Owned<ITypeInfo> rowType = makeConstantModifier(makeRowType(record->getType()));
  117. StringBuffer rowName;
  118. getUniqueId(rowName.append("r"));
  119. //Generate two variables to cope with the different ways the data is interpreted.
  120. //Would prefer it to be cleaner... row value would need an associated size
  121. unsigned dataSize = rowData->queryType()->getSize();
  122. OwnedITypeInfo declareType;
  123. OwnedHqlExpr initializer;
  124. if (options.staticRowsUseStringInitializer)
  125. {
  126. //Generates smaller code (and probably more efficient representation in the c++ compiler
  127. //const byte[5+1] = "Hello"; need an extra byte for the implicit \0
  128. declareType.setown(makeDataType(dataSize+1));
  129. initializer.set(rowData);
  130. }
  131. else
  132. {
  133. //Following is strictly correct, but much larger.
  134. //const byte[5] = { 'H','e','l','l','o' };
  135. declareType.set(rowData->queryType());
  136. initializer.setown(createValue(no_create_initializer, rowData->getType(), LINK(rowData)));
  137. }
  138. //MORE: Currently these are marked as const rows, but not generated as such
  139. OwnedHqlExpr boundDeclare = createVariable(rowName, makeConstantModifier(LINK(declareType)));
  140. OwnedHqlExpr boundRow = createVariable(rowName, LINK(rowType));
  141. declareCtx.addDeclare(boundDeclare, initializer);
  142. if (options.spanMultipleCpp)
  143. {
  144. BuildCtx protoctx(*code, mainprototypesAtom);
  145. protoctx.addDeclareExternal(boundDeclare);
  146. }
  147. bound.length.setown(getSizetConstant(dataSize));
  148. bound.expr.set(boundRow);
  149. declareCtx.associateExpr(marker, bound);
  150. }
  151. bool HqlCppTranslator::doBuildRowConstantTransform(IHqlExpression * transform, CHqlBoundExpr & bound)
  152. {
  153. if (!transform->isConstant() || !options.generateStaticInlineTables)
  154. return false;
  155. OwnedHqlExpr constRow = createConstantRowExpr(transform);
  156. if (!constRow || !canGenerateStringInline(constRow->queryType()->getSize()))
  157. return false;
  158. buildConstRow(transform->queryRecord(), constRow, bound);
  159. return true;
  160. }
  161. IReferenceSelector * HqlCppTranslator::doBuildRowCreateRow(BuildCtx & ctx, IHqlExpression * expr)
  162. {
  163. CHqlBoundExpr bound;
  164. if (!doBuildRowConstantTransform(expr->queryChild(0), bound))
  165. return doBuildRowViaTemp(ctx, expr);
  166. BoundRow * row = bindConstantRow(ctx, expr, bound);
  167. return createReferenceSelector(row);
  168. }
  169. BoundRow * HqlCppTranslator::bindConstantRow(BuildCtx & ctx, IHqlExpression * expr, CHqlBoundExpr & bound)
  170. {
  171. BoundRow * row = bindRow(ctx, expr, bound.expr);
  172. //MORE: This should be done more cleanly
  173. OwnedHqlExpr sizeOfRow = createSizeof(row->querySelector());
  174. ctx.associateExpr(sizeOfRow, bound.length);
  175. return row;
  176. }
  177. bool HqlCppTranslator::doBuildRowConstantNull(IHqlExpression * expr, CHqlBoundExpr & bound)
  178. {
  179. if (!options.generateStaticInlineTables)
  180. return false;
  181. IHqlExpression * record = expr->queryRecord();
  182. OwnedHqlExpr constRow = createConstantNullRowExpr(record);
  183. if (!constRow)
  184. return false;
  185. buildConstRow(record, constRow, bound);
  186. return true;
  187. }
  188. IReferenceSelector * HqlCppTranslator::doBuildRowNull(BuildCtx & ctx, IHqlExpression * expr)
  189. {
  190. CHqlBoundExpr bound;
  191. if (!doBuildRowConstantNull(expr, bound))
  192. return doBuildRowViaTemp(ctx, expr);
  193. BoundRow * row = bindRow(ctx, expr, bound.expr);
  194. return createReferenceSelector(row);
  195. }
  196. IReferenceSelector * HqlCppTranslator::doBuildRowViaTemp(BuildCtx & ctx, IHqlExpression * expr)
  197. {
  198. HqlExprAssociation * match;
  199. if (expr->isDataset())
  200. match = ctx.queryAssociation(expr->queryNormalizedSelector(), AssocCursor, NULL);
  201. else
  202. match = ctx.queryAssociation(expr, AssocRow, NULL);
  203. if (match)
  204. {
  205. BoundRow * row = (BoundRow *)match;
  206. return createReferenceSelector(row, expr);
  207. }
  208. Owned<BoundRow> tempRow = declareTempRow(ctx, ctx, expr);
  209. Owned<BoundRow> rowBuilder = createRowBuilder(ctx, tempRow);
  210. Owned<IReferenceSelector> createdRef = createReferenceSelector(rowBuilder);
  211. buildRowAssign(ctx, createdRef, expr);
  212. finalizeTempRow(ctx, tempRow, rowBuilder);
  213. ctx.associate(*tempRow);
  214. return createReferenceSelector(tempRow);
  215. }
  216. void HqlCppTranslator::buildDefaultRow(BuildCtx & ctx, IHqlExpression * expr, CHqlBoundExpr & bound)
  217. {
  218. OwnedHqlExpr clearExpr = createRow(no_null, LINK(expr->queryRecord()), createAttribute(clearAtom));
  219. BoundRow * matchedRow = (BoundRow *)ctx.queryAssociation(clearExpr, AssocRow, NULL);
  220. if (!matchedRow)
  221. {
  222. if (doBuildRowConstantNull(expr, bound))
  223. {
  224. bindRow(ctx, clearExpr, bound.expr);
  225. }
  226. else
  227. {
  228. BuildCtx * declarectx = &ctx;
  229. BuildCtx * callCtx = &ctx;
  230. getInvariantMemberContext(ctx, &declarectx, &callCtx, true, false);
  231. Owned<BoundRow> tempRow = declareTempRow(*declarectx, *callCtx, clearExpr);
  232. Owned<BoundRow> rowBuilder = createRowBuilder(*callCtx, tempRow);
  233. OwnedHqlExpr size = createVariable(LINK(sizetType));
  234. OwnedHqlExpr clearCall = createClearRowCall(*callCtx, rowBuilder);
  235. callCtx->addDeclare(size, clearCall);
  236. OwnedHqlExpr sizeOfRow = createSizeof(rowBuilder->querySelector());
  237. callCtx->associateExpr(sizeOfRow, size);
  238. finalizeTempRow(*callCtx, tempRow, rowBuilder);
  239. declarectx->associate(*tempRow);
  240. bound.expr.set(tempRow->queryBound());
  241. }
  242. }
  243. else
  244. bound.expr.set(matchedRow->queryBound());
  245. //yuk yuk, hack. If called from a const context then need to make the reference unconst.
  246. //The real fix is to implement real const tracking throughout the code generator, but that is far from trivial.
  247. //rkc39.hql is an example...
  248. if (ctx.queryMatchExpr(constantMemberMarkerExpr))
  249. bound.expr.setown(createValue(no_cast, makeReferenceModifier(bound.expr->getType()), getPointer(bound.expr)));
  250. }
  251. void HqlCppTranslator::buildNullRow(BuildCtx & ctx, IHqlExpression * expr, CHqlBoundExpr & bound)
  252. {
  253. bound.expr.setown(createValue(no_nullptr, makeRowReferenceType(expr)));
  254. }
  255. IReferenceSelector * HqlCppTranslator::doBuildRowFromXML(BuildCtx & ctx, IHqlExpression * expr)
  256. {
  257. // assertex(supportsLinkCountedRows);
  258. Owned<ITypeInfo> overrideType = setLinkCountedAttr(expr->queryType(), true);
  259. Owned<ITypeInfo> utf8Type = makeUtf8Type(UNKNOWN_LENGTH, NULL);
  260. IHqlExpression * record = expr->queryRecord();
  261. OwnedHqlExpr ds = createDataset(no_anon, LINK(record));
  262. StringBuffer xmlInstanceName, xmlFactoryName, s;
  263. bool usesContents = false;
  264. getUniqueId(xmlInstanceName.append("xml"));
  265. buildXmlReadTransform(ds, xmlFactoryName, usesContents);
  266. OwnedHqlExpr curActivityId = getCurrentActivityId(ctx);
  267. //MORE: This should be generalised so that any invariant class creation can be handled by the same code.
  268. BuildCtx * declareCtx = &ctx;
  269. BuildCtx * initCtx = &ctx;
  270. if (!insideOnCreate(ctx) && getInvariantMemberContext(ctx, &declareCtx, &initCtx, false, false))
  271. {
  272. declareCtx->addQuoted(s.clear().append("Owned<IXmlToRowTransformer> ").append(xmlInstanceName).append(";"));
  273. s.clear().append(xmlInstanceName).append(".setown(").append(xmlFactoryName).append("(ctx,");
  274. generateExprCpp(s, curActivityId).append("));");
  275. initCtx->addQuoted(s);
  276. }
  277. else
  278. {
  279. s.append("Owned<IXmlToRowTransformer> ").append(xmlInstanceName).append(" = ").append(xmlFactoryName).append("(ctx,");
  280. generateExprCpp(s, curActivityId).append(");");
  281. ctx.addQuoted(s);
  282. }
  283. HqlExprArray args;
  284. args.append(*createRowAllocator(ctx, record));
  285. args.append(*ensureExprType(expr->queryChild(1), utf8Type));
  286. args.append(*createQuoted(xmlInstanceName, makeBoolType()));
  287. args.append(*createConstant(expr->hasProperty(trimAtom)));
  288. OwnedHqlExpr function = bindFunctionCall(createRowFromXmlAtom, args, overrideType);
  289. CHqlBoundExpr bound;
  290. buildExpr(ctx, function, bound);
  291. Owned<ITypeInfo> rowType = makeReferenceModifier(LINK(overrideType));
  292. OwnedHqlExpr rowExpr = ctx.getTempDeclare(rowType, NULL);
  293. Owned<BoundRow> row = createBoundRow(expr, rowExpr);
  294. ctx.associate(*row); // associate here because it is compared inside the loop
  295. OwnedHqlExpr defaultRowPtr = getPointer(bound.expr);
  296. ctx.addAssign(rowExpr, defaultRowPtr);
  297. return createReferenceSelector(row);
  298. }
  299. //NB: If this is a dataset operation, this function assumes that any parent datasets are already in scope
  300. // i.e. processed in buildDatasetAssign()
  301. // the one exception is aggregate because it needs to treat its input differently.
  302. IReferenceSelector * HqlCppTranslator::buildNewOrActiveRow(BuildCtx & ctx, IHqlExpression * expr, bool isNew)
  303. {
  304. if (isNew)
  305. return buildNewRow(ctx, expr);
  306. else
  307. return buildActiveRow(ctx, expr);
  308. }
  309. IReferenceSelector * HqlCppTranslator::buildNewRow(BuildCtx & ctx, IHqlExpression * expr)
  310. {
  311. assertex(!expr->isDataset());
  312. BoundRow * match = static_cast<BoundRow *>(ctx.queryAssociation(expr, AssocRow, NULL));
  313. if (match)
  314. return createReferenceSelector(match, expr);
  315. BoundRow * row = NULL;
  316. node_operator op = expr->getOperator();
  317. switch (op)
  318. {
  319. case no_activerow:
  320. return buildActiveRow(ctx, expr->queryChild(0));
  321. case no_if:
  322. return doBuildRowIf(ctx, expr);
  323. case no_id2blob:
  324. return doBuildRowIdToBlob(ctx, expr, true);
  325. case no_index:
  326. case no_selectnth:
  327. return buildDatasetIndex(ctx, expr);
  328. case no_selectmap:
  329. return buildDatasetSelectMap(ctx, expr);
  330. case no_left:
  331. case no_right:
  332. case no_self:
  333. case no_top:
  334. case no_activetable:
  335. return buildActiveRow(ctx, expr);
  336. case no_fromxml:
  337. return doBuildRowFromXML(ctx, expr);
  338. case no_serialize:
  339. if (isDummySerializeDeserialize(expr))
  340. return buildNewRow(ctx, expr->queryChild(0)->queryChild(0));
  341. return doBuildRowViaTemp(ctx, expr);
  342. case no_deserialize:
  343. if (isDummySerializeDeserialize(expr))
  344. return buildNewRow(ctx, expr->queryChild(0)->queryChild(0));
  345. return doBuildRowDeserializeRow(ctx, expr);
  346. case no_deref:
  347. {
  348. //Untested
  349. CHqlBoundExpr bound;
  350. buildExpr(ctx, expr, bound);
  351. row = bindRow(ctx, expr, bound.expr);
  352. break;
  353. }
  354. case no_createrow:
  355. return doBuildRowCreateRow(ctx, expr);
  356. case no_newusertable:
  357. case no_hqlproject:
  358. case no_temprow:
  359. case no_projectrow:
  360. return doBuildRowViaTemp(ctx, expr);
  361. case no_null:
  362. return doBuildRowNull(ctx, expr);
  363. case no_typetransfer:
  364. {
  365. CHqlBoundExpr bound;
  366. IHqlExpression * value = expr->queryChild(1);
  367. if (value->isDatarow())
  368. buildAddress(ctx, value, bound);
  369. else
  370. buildExpr(ctx, value, bound);
  371. OwnedHqlExpr cursorExpr = createValue(no_implicitcast, makeReferenceModifier(expr->getType()), LINK(bound.expr));
  372. row = bindRow(ctx, expr, cursorExpr);
  373. break;
  374. }
  375. case no_getresult:
  376. {
  377. IHqlExpression * seqAttr = expr->queryProperty(sequenceAtom);
  378. IHqlExpression * nameAttr = expr->queryProperty(namedAtom);
  379. IHqlExpression * record = expr->queryRecord();
  380. OwnedHqlExpr serializedRecord = getSerializedForm(record);
  381. OwnedHqlExpr temp = createDatasetF(no_getresult, LINK(serializedRecord), LINK(seqAttr), LINK(nameAttr), NULL);
  382. OwnedHqlExpr row = createRow(no_selectnth, LINK(temp), createComma(getSizetConstant(1), createAttribute(noBoundCheckAtom)));
  383. row.setown(ensureDeserialized(row, expr->queryType()));
  384. return buildNewRow(ctx, row);
  385. }
  386. case no_matchattr:
  387. return doBuildRowMatchAttr(ctx, expr);
  388. case no_matchrow:
  389. return doBuildRowMatchRow(ctx, expr, true);
  390. case no_getgraphresult:
  391. case no_call:
  392. case no_externalcall:
  393. case no_alias:
  394. case no_translated:
  395. case no_libraryinput:
  396. {
  397. CHqlBoundExpr bound;
  398. buildExpr(ctx, expr, bound);
  399. Owned<ITypeInfo> rawType = removeModifier(expr->queryType(), typemod_ref);
  400. OwnedHqlExpr cursorExpr = createValue(no_implicitcast, makeReferenceModifier(LINK(rawType)), LINK(bound.expr));
  401. row = bindRow(ctx, expr, cursorExpr);
  402. if (bound.length)
  403. {
  404. OwnedHqlExpr sizeOfRow = createSizeof(row->querySelector());
  405. ctx.associateExpr(sizeOfRow, bound.length);
  406. }
  407. //We could associate the original expression to allow better cse for child datasets in transforms, but it doesn't actually improve any examples
  408. //IHqlExpression * original = queryPropertyChild(expr, _original_Atom, 0);
  409. //if (original)
  410. // bindRow(ctx, original, cursorExpr)->setResultAlias();
  411. break;//return createReferenceSelector(cursor);
  412. }
  413. case no_comma:
  414. case no_compound:
  415. buildStmt(ctx, expr->queryChild(0));
  416. return buildNewRow(ctx, expr->queryChild(1));
  417. case no_select:
  418. {
  419. #ifdef _DEBUG
  420. IHqlExpression * field = expr->queryChild(1);
  421. #endif
  422. Owned<IReferenceSelector> selector;
  423. if (isNewSelector(expr))
  424. selector.setown(buildNewRow(ctx, expr->queryChild(0)));
  425. else
  426. selector.setown(buildActiveRow(ctx, expr->queryChild(0)));
  427. return selector->select(ctx, expr);
  428. }
  429. //If called because known to be a single row.
  430. case no_datasetfromrow:
  431. case no_nofold:
  432. case no_nohoist:
  433. case no_section:
  434. case no_sectioninput:
  435. return buildNewRow(ctx, expr->queryChild(0));
  436. case no_skip:
  437. {
  438. buildStmt(ctx, expr);
  439. OwnedHqlExpr null = createNullExpr(expr);
  440. return buildNewRow(ctx, null);
  441. }
  442. case no_alias_scope:
  443. {
  444. expandAliasScope(ctx, expr);
  445. return buildNewRow(ctx, expr->queryChild(0));
  446. }
  447. case no_split:
  448. throwUnexpected();
  449. //not at all sure about this.....
  450. return buildNewRow(ctx, expr->queryChild(0));
  451. default:
  452. {
  453. HqlExprAssociation * match;
  454. if (expr->isDataset())
  455. match = ctx.queryAssociation(expr->queryNormalizedSelector(), AssocCursor, NULL);
  456. else
  457. match = ctx.queryAssociation(expr, AssocRow, NULL);
  458. if (match)
  459. {
  460. BoundRow * row = (BoundRow *)match;
  461. IReferenceSelector * alias = row->queryAlias();
  462. if (alias)
  463. return LINK(alias);
  464. return createReferenceSelector(row, expr);
  465. }
  466. UNIMPLEMENTED_XY("row", getOpString(expr->getOperator()));
  467. }
  468. }
  469. assertex(row);
  470. return createReferenceSelector(row);
  471. }
  472. IReferenceSelector * HqlCppTranslator::buildActiveRow(BuildCtx & ctx, IHqlExpression * expr)
  473. {
  474. node_operator op = expr->getOperator();
  475. switch (op)
  476. {
  477. case no_left:
  478. case no_right:
  479. case no_self:
  480. case no_top:
  481. case no_activetable:
  482. case no_selfref: // shouldn't ever occur...
  483. //All selectors should be listed here...
  484. break;
  485. case no_activerow:
  486. return buildActiveRow(ctx, expr->queryChild(0));
  487. default:
  488. if (!expr->isDataset() && !expr->isDictionary())
  489. return buildNewRow(ctx, expr);
  490. break;
  491. }
  492. HqlExprAssociation * match = ctx.queryAssociation(expr->queryNormalizedSelector(), AssocCursor, NULL);
  493. if (match)
  494. {
  495. BoundRow * row = (BoundRow *)match;
  496. IReferenceSelector * alias = row->queryAlias();
  497. if (alias)
  498. return LINK(alias);
  499. return createReferenceSelector(row, expr);
  500. }
  501. switch (op)
  502. {
  503. case no_select:
  504. {
  505. #ifdef _DEBUG
  506. IHqlExpression * field = expr->queryChild(1);
  507. #endif
  508. Owned<IReferenceSelector> selector = buildNewOrActiveRow(ctx, expr->queryChild(0), isNewSelector(expr));
  509. return selector->select(ctx, expr);
  510. }
  511. case no_id2blob:
  512. return doBuildRowIdToBlob(ctx, expr, false);
  513. }
  514. StringBuffer tablename;
  515. getExprIdentifier(tablename, expr);
  516. traceExpression("Dataset not found", expr);
  517. RowAssociationIterator iter(ctx);
  518. ForEach(iter)
  519. {
  520. BoundRow & cur = iter.get();
  521. traceExpression("BoundCursor:", cur.querySelector());
  522. }
  523. throwError1(HQLERR_DatasetNotActive, tablename.str());
  524. return NULL; //remove warning about control paths
  525. }
  526. //---------------------------------------------------------------------------
  527. void HqlCppTranslator::doBuildExprAggregate(BuildCtx & ctx, IHqlExpression * expr, CHqlBoundExpr & tgt)
  528. {
  529. OwnedHqlExpr normalized = normalizeAnyDatasetAliases(expr);
  530. if (expr != normalized)
  531. {
  532. buildExpr(ctx, normalized, tgt);
  533. return;
  534. }
  535. node_operator op = expr->getOperator();
  536. ITypeInfo * type = expr->queryType();
  537. ITypeInfo * tempType = op == no_count ? unsignedType : type;
  538. LoopInvariantHelper helper;
  539. BuildCtx aggctx(ctx);
  540. if (options.optimizeLoopInvariant)
  541. helper.getBestContext(aggctx, expr);
  542. CHqlBoundTarget result;
  543. createTempFor(aggctx, tempType, result, typemod_none, FormatNatural);
  544. doBuildAssignAggregate(aggctx, result, expr);
  545. tgt.setFromTarget(result);
  546. if (!isSameBasicType(type, tempType))
  547. tgt.expr.setown(createValue(no_implicitcast, LINK(type), tgt.expr.getClear()));
  548. if (expr->isPure())
  549. aggctx.associateExpr(expr, tgt);
  550. }
  551. void HqlCppTranslator::doBuildAssignAggregateLoop(BuildCtx & ctx, const CHqlBoundTarget & target, IHqlExpression * expr, IHqlExpression * dataset, IHqlExpression * doneFirstVar)
  552. {
  553. node_operator op = expr->getOperator();
  554. switch (op)
  555. {
  556. case no_exists:
  557. {
  558. OwnedHqlExpr optimized = queryOptimizedExists(ctx, expr, dataset);
  559. if (optimized)
  560. {
  561. assignBound(ctx, target, optimized);
  562. return;
  563. }
  564. break;
  565. }
  566. case no_count:
  567. {
  568. CHqlBoundExpr temp;
  569. if (canBuildOptimizedCount(ctx, dataset, temp))
  570. {
  571. OwnedHqlExpr thisCount = temp.getTranslatedExpr();
  572. buildIncrementAssign(ctx, target, thisCount);
  573. return;
  574. }
  575. break;
  576. }
  577. }
  578. switch (dataset->getOperator())
  579. {
  580. case no_if:
  581. {
  582. BuildCtx subctx(ctx);
  583. IHqlStmt * stmt = buildFilterViaExpr(subctx, dataset->queryChild(0));
  584. doBuildAssignAggregateLoop(subctx, target, expr, dataset->queryChild(1), doneFirstVar);
  585. subctx.selectElse(stmt);
  586. doBuildAssignAggregateLoop(subctx, target, expr, dataset->queryChild(2), doneFirstVar);
  587. return;
  588. }
  589. case no_addfiles:
  590. {
  591. doBuildAssignAggregateLoop(ctx, target, expr, dataset->queryChild(0), doneFirstVar);
  592. doBuildAssignAggregateLoop(ctx, target, expr, dataset->queryChild(1), doneFirstVar);
  593. return;
  594. }
  595. case no_chooseds:
  596. {
  597. CHqlBoundExpr cond;
  598. buildExpr(ctx, dataset->queryChild(0), cond);
  599. IHqlExpression * last = queryLastNonAttribute(dataset);
  600. BuildCtx subctx(ctx);
  601. IHqlStmt * switchstmt = subctx.addSwitch(cond.expr);
  602. ForEachChildFrom(i, dataset, 1)
  603. {
  604. IHqlExpression * cur = dataset->queryChild(i);
  605. if (cur != last)
  606. {
  607. OwnedHqlExpr label = getSizetConstant(i);
  608. subctx.addCase(switchstmt, label);
  609. }
  610. else
  611. subctx.addDefault(switchstmt);
  612. doBuildAssignAggregateLoop(subctx, target, expr, cur, doneFirstVar);
  613. }
  614. return;
  615. }
  616. case no_null:
  617. return;
  618. }
  619. LinkedHqlExpr arg = expr->queryChild(1);
  620. IHqlExpression * oldDataset = expr->queryChild(0);
  621. //If no_if or no_addfiles has been optimized above then the selector for the argument will have changed => map it.
  622. if (arg && (dataset != oldDataset))
  623. arg.setown(replaceSelector(arg, oldDataset, dataset));
  624. bool needToBreak = (op == no_exists);
  625. if (needToBreak)
  626. {
  627. //if it can have at most one row (fairly strange code!) then don't add a break
  628. //unless it was deliberately a choosen to restrict the number of iterations.
  629. if (hasNoMoreRowsThan(dataset, 1) && (dataset->getOperator() != no_choosen))
  630. needToBreak = false;
  631. }
  632. BuildCtx loopctx(ctx);
  633. buildDatasetIterate(loopctx, dataset, needToBreak);
  634. switch (op)
  635. {
  636. case no_exists:
  637. buildExprAssign(loopctx, target, queryBoolExpr(true));
  638. if (needToBreak)
  639. loopctx.addBreak();
  640. break;
  641. case no_count:
  642. {
  643. OwnedHqlExpr inc = createValue(no_postinc, makeVoidType(), LINK(target.expr));
  644. loopctx.addExpr(inc);
  645. break;
  646. }
  647. case no_sum:
  648. {
  649. OwnedHqlExpr cseArg = options.spotCSE ? spotScalarCSE(arg) : LINK(arg);
  650. buildIncrementAssign(loopctx, target, cseArg);
  651. break;
  652. }
  653. case no_min:
  654. case no_max:
  655. {
  656. BuildCtx maxctx(loopctx);
  657. OwnedHqlExpr resultExpr = target.getTranslatedExpr();
  658. OwnedHqlExpr cseArg = options.spotCSE ? spotScalarCSE(arg) : LINK(arg);
  659. OwnedHqlExpr simpleArg = buildSimplifyExpr(loopctx, cseArg);
  660. OwnedHqlExpr test = createBoolExpr((op == no_min) ? no_lt : no_gt, LINK(simpleArg), LINK(resultExpr));
  661. if (doneFirstVar)
  662. {
  663. IHqlExpression * firstTest = createValue(no_not, makeBoolType(), LINK(doneFirstVar));
  664. test.setown(createBoolExpr(no_or, firstTest, test.getClear()));
  665. }
  666. buildFilter(maxctx, test);
  667. buildExprAssign(maxctx, target, simpleArg);
  668. if (doneFirstVar)
  669. buildAssignToTemp(maxctx, doneFirstVar, queryBoolExpr(true));
  670. break;
  671. }
  672. default:
  673. assertex(!"unknown aggregate on child datasets");
  674. break;
  675. }
  676. }
  677. bool assignAggregateDirect(const CHqlBoundTarget & target, IHqlExpression * expr)
  678. {
  679. node_operator op = expr->getOperator();
  680. ITypeInfo * type = expr->queryType();
  681. ITypeInfo * tempType = op == no_count ? unsignedType : type;
  682. if (!isSameUnqualifiedType(target.queryType(), tempType))
  683. return false;
  684. //For exists/count/sum use a temporary variable, and then assign rather than accumulating directly in the target
  685. switch (op)
  686. {
  687. case no_sum:
  688. if (type->getTypeCode() != type_int)
  689. break;
  690. //fall through
  691. case no_exists:
  692. case no_count:
  693. if (target.expr->getOperator() != no_variable)
  694. return false;
  695. break;
  696. }
  697. return true;
  698. }
  699. static bool isNullValueMinimumValue(ITypeInfo * type)
  700. {
  701. switch (type->getTypeCode())
  702. {
  703. case type_int:
  704. case type_swapint:
  705. case type_decimal:
  706. return !type->isSigned();
  707. case type_data:
  708. case type_qstring:
  709. return true;
  710. }
  711. return false;
  712. }
  713. void HqlCppTranslator::doBuildAssignAggregate(BuildCtx & ctx, const CHqlBoundTarget & target, IHqlExpression * _expr)
  714. {
  715. OwnedHqlExpr expr = normalizeAnyDatasetAliases(_expr);
  716. if (assignAggregateDirect(target, expr))
  717. {
  718. IHqlExpression * dataset = expr->queryChild(0);
  719. OwnedHqlExpr resultExpr = target.getTranslatedExpr();
  720. node_operator op = expr->getOperator();
  721. switch (op)
  722. {
  723. case no_exists:
  724. buildExprAssign(ctx, target, queryBoolExpr(false));
  725. break;
  726. default:
  727. {
  728. OwnedHqlExpr null = createNullExpr(target.queryType());
  729. buildExprAssign(ctx, target, null);
  730. break;
  731. }
  732. }
  733. OwnedHqlExpr doneFirstVar;
  734. if ((op == no_min) || ((op == no_max) && !isNullValueMinimumValue(target.queryType())))
  735. {
  736. doneFirstVar.setown(ctx.getTempDeclare(queryBoolType(), queryBoolExpr(false)));
  737. }
  738. doBuildAssignAggregateLoop(ctx, target, expr, dataset, doneFirstVar);
  739. }
  740. else
  741. {
  742. doBuildExprAssign(ctx, target, expr);
  743. }
  744. }
  745. //---------------------------------------------------------------------------
  746. bool HqlCppTranslator::canBuildOptimizedCount(BuildCtx & ctx, IHqlExpression * dataset, CHqlBoundExpr & tgt)
  747. {
  748. switch (dataset->getOperator())
  749. {
  750. case no_select:
  751. {
  752. if (isMultiLevelDatasetSelector(dataset, false))
  753. return false;
  754. Owned<IReferenceSelector> selector = buildReference(ctx, dataset);
  755. CHqlBoundExpr temp;
  756. selector->get(ctx, temp);
  757. tgt.expr.setown(getBoundCount(temp));
  758. return true;
  759. }
  760. break;
  761. default:
  762. if (!options.tempDatasetsUseLinkedRows)
  763. break;
  764. if (!alwaysEvaluatesToBound(dataset))
  765. break;
  766. //fall through
  767. case no_rows:
  768. case no_null:
  769. {
  770. CHqlBoundExpr temp;
  771. buildDataset(ctx, dataset, temp, FormatNatural);
  772. tgt.expr.setown(getBoundCount(temp));
  773. return true;
  774. }
  775. }
  776. #if 0
  777. //This is improves a few obscure cases (normally in the global context). I'm not convinced it is worth the extra cycles.
  778. //Could also remove the bound.count test.
  779. CHqlBoundExpr bound;
  780. if (ctx.getMatchExpr(dataset, bound) && bound.count)
  781. {
  782. tgt.expr.setown(getBoundCount(bound));
  783. return true;
  784. }
  785. #endif
  786. return false;
  787. }
  788. void HqlCppTranslator::doBuildExprCount(BuildCtx & ctx, IHqlExpression * expr, CHqlBoundExpr & tgt)
  789. {
  790. if (expr->hasProperty(keyedAtom))
  791. throwError(HQLERR_KeyedCountNonKeyable);
  792. IHqlExpression * dataset = expr->queryChild(0);
  793. CHqlBoundExpr temp;
  794. if (canBuildOptimizedCount(ctx, dataset, temp))
  795. {
  796. OwnedHqlExpr translated = temp.getTranslatedExpr();
  797. OwnedHqlExpr cast = ensureExprType(translated, expr->queryType());
  798. buildExpr(ctx, cast, tgt);
  799. }
  800. else
  801. doBuildExprAggregate(ctx, expr, tgt);
  802. }
  803. //---------------------------------------------------------------------------
  804. IHqlExpression * HqlCppTranslator::queryOptimizedExists(BuildCtx & ctx, IHqlExpression * expr, IHqlExpression * dataset)
  805. {
  806. node_operator dsOp = dataset->getOperator();
  807. //really this is isSimple()
  808. CHqlBoundExpr optimized;
  809. bool canOptimizeCount = canBuildOptimizedCount(ctx, dataset, optimized);
  810. node_operator op = (expr->getOperator() == no_exists) ? no_ne : no_eq;
  811. bool specialCase = false;
  812. switch (dsOp)
  813. {
  814. case no_select:
  815. specialCase = canOptimizeCount;
  816. break;
  817. default:
  818. specialCase = !canOptimizeCount && alwaysEvaluatesToBound(dataset);
  819. break;
  820. }
  821. if (specialCase)
  822. {
  823. CHqlBoundExpr temp;
  824. buildDataset(ctx, dataset, temp, FormatNatural);
  825. IHqlExpression * test;
  826. if (temp.count)
  827. test = LINK(temp.count);
  828. else
  829. test = getBoundSize(temp);
  830. return createBoolExpr(op, test, createConstant(test->queryType()->castFrom(false, 0)));
  831. }
  832. else if (canOptimizeCount)
  833. {
  834. IHqlExpression * count = optimized.expr;
  835. return createBoolExpr(op, LINK(count), createConstant(count->queryType()->castFrom(false, 0)));
  836. }
  837. return NULL;
  838. }
  839. void HqlCppTranslator::doBuildExprExists(BuildCtx & ctx, IHqlExpression * expr, CHqlBoundExpr & tgt)
  840. {
  841. OwnedHqlExpr optimized = queryOptimizedExists(ctx, expr, expr->queryChild(0));
  842. if (optimized)
  843. tgt.expr.setown(optimized.getClear());
  844. else
  845. doBuildExprAggregate(ctx, expr, tgt);
  846. }
  847. //---------------------------------------------------------------------------
  848. static IHqlExpression * createMinMax(node_operator compareOp, ITypeInfo * type, IHqlExpression * left, IHqlExpression * right)
  849. {
  850. return createValue(no_if, LINK(type),
  851. createBoolExpr(compareOp, LINK(left), LINK(right)),
  852. LINK(left), LINK(right));
  853. }
  854. bool HqlCppTranslator::doBuildAggregateMinMaxList(BuildCtx & ctx, const CHqlBoundTarget * target, IHqlExpression * expr, IHqlExpression * list, CHqlBoundExpr * tgt, node_operator compareOp)
  855. {
  856. OwnedHqlExpr value;
  857. unsigned max = list->numChildren();
  858. switch(max)
  859. {
  860. case 0:
  861. value.setown(createNullExpr(expr));
  862. break;
  863. case 1:
  864. value.set(list->queryChild(0));
  865. break;
  866. case 2:
  867. case 3:
  868. {
  869. OwnedHqlExpr simple[3];
  870. for (unsigned i=0; i < max; i++)
  871. {
  872. CHqlBoundExpr bound;
  873. buildSimpleExpr(ctx, list->queryChild(i), bound);
  874. simple[i].setown(bound.getTranslatedExpr());
  875. }
  876. ITypeInfo * type = expr->queryType();
  877. if (max == 2)
  878. value.setown(createMinMax(compareOp, type, simple[0], simple[1]));
  879. else
  880. {
  881. OwnedHqlExpr cmp02 = createMinMax(compareOp, type, simple[0], simple[2]);
  882. OwnedHqlExpr cmp12 = createMinMax(compareOp, type, simple[1], simple[2]);
  883. value.setown(createValue(no_if, expr->getType(),
  884. createBoolExpr(compareOp, LINK(simple[0]), LINK(simple[1])),
  885. LINK(cmp02), LINK(cmp12)));
  886. }
  887. }
  888. }
  889. if (value)
  890. {
  891. buildExprOrAssign(ctx, target, value, tgt);
  892. return true;
  893. }
  894. {
  895. CHqlBoundTarget temp;
  896. createTempFor(ctx, expr, temp);
  897. buildExprAssign(ctx, temp, list->queryChild(0));
  898. OwnedHqlExpr best = temp.getTranslatedExpr();
  899. for (unsigned i=1; i < list->numChildren(); i++)
  900. {
  901. CHqlBoundExpr bound;
  902. buildSimpleExpr(ctx, list->queryChild(i), bound);
  903. OwnedHqlExpr simple = bound.getTranslatedExpr();
  904. BuildCtx subctx(ctx);
  905. OwnedHqlExpr cond = createBoolExpr(compareOp, LINK(simple), LINK(best));
  906. buildFilter(subctx, cond);
  907. buildExprAssign(subctx, temp, simple);
  908. }
  909. buildExprOrAssign(ctx, target, best, tgt);
  910. return true;
  911. }
  912. }
  913. void HqlCppTranslator::doBuildAggregateList(BuildCtx & ctx, const CHqlBoundTarget * target, IHqlExpression * expr, CHqlBoundExpr * tgt)
  914. {
  915. OwnedHqlExpr list = normalizeListCasts(expr->queryChild(0));
  916. if (list->getOperator() == no_alias_scope)
  917. {
  918. expandAliasScope(ctx, list);
  919. list.set(list->queryChild(0));
  920. }
  921. node_operator aggOp;
  922. switch (expr->getOperator())
  923. {
  924. case no_existslist:
  925. {
  926. //Fixed length lists should have been optimized away
  927. CHqlBoundExpr bound;
  928. buildExpr(ctx, list, bound);
  929. OwnedHqlExpr test;
  930. if (bound.count)
  931. test.set(bound.count);
  932. else
  933. test.setown(getBoundLength(bound));
  934. OwnedHqlExpr value = createValue(no_ne, makeBoolType(), LINK(test), ensureExprType(queryZero(), test->queryType()));
  935. OwnedHqlExpr translated = createTranslated(value);
  936. buildExprOrAssign(ctx, target, translated, tgt);
  937. return;
  938. }
  939. case no_countlist:
  940. {
  941. //Fixed length lists should have been optimized away
  942. CHqlBoundExpr bound;
  943. buildExpr(ctx, list, bound);
  944. OwnedHqlExpr test = getBoundCount(bound);
  945. OwnedHqlExpr value = ensureExprType(test, expr->queryType());
  946. OwnedHqlExpr translated = createTranslated(value);
  947. buildExprOrAssign(ctx, target, translated, tgt);
  948. return;
  949. }
  950. case no_sumlist:
  951. aggOp = no_sumgroup;
  952. if (list->getOperator() == no_list)
  953. {
  954. HqlExprArray args;
  955. ITypeInfo * exprType = expr->queryType();
  956. ForEachChild(i, list)
  957. args.append(*ensureExprType(list->queryChild(i), exprType));
  958. OwnedHqlExpr balanced = createBalanced(no_add, exprType, args);
  959. if (!balanced)
  960. balanced.setown(ensureExprType(queryZero(), exprType));
  961. buildExprOrAssign(ctx, target, balanced, tgt);
  962. return;
  963. }
  964. //special case fixed length lists
  965. break;
  966. case no_minlist:
  967. aggOp = no_mingroup;
  968. if (list->getOperator() == no_list)
  969. {
  970. if (doBuildAggregateMinMaxList(ctx, target, expr, list, tgt, no_lt))
  971. return;
  972. }
  973. break;
  974. case no_maxlist:
  975. aggOp = no_maxgroup;
  976. if (list->getOperator() == no_list)
  977. {
  978. if (doBuildAggregateMinMaxList(ctx, target, expr, list, tgt, no_gt))
  979. return;
  980. }
  981. break;
  982. default:
  983. throwUnexpectedOp(expr->getOperator());
  984. }
  985. ITypeInfo * elemType = list->queryType()->queryChildType();
  986. if (!elemType) elemType = defaultIntegralType;
  987. //Default implementation in terms of a dataset
  988. OwnedHqlExpr field = createField(valueAtom, LINK(elemType), NULL);
  989. OwnedHqlExpr record = createRecord(field);
  990. OwnedHqlExpr ds = createDataset(no_temptable, LINK(list), LINK(record));
  991. OwnedHqlExpr aggField = createField(valueAtom, expr->getType(), NULL);
  992. OwnedHqlExpr aggRecord = createRecord(aggField);
  993. OwnedHqlExpr self = createSelector(no_self, aggRecord, NULL);
  994. OwnedHqlExpr aggExpr = createValue(aggOp, expr->getType(), createSelectExpr(LINK(ds), LINK(field)));
  995. OwnedHqlExpr aggAssign = createAssign(createSelectExpr(LINK(self), LINK(aggField)), LINK(aggExpr));
  996. OwnedHqlExpr aggTransform = createValue(no_newtransform, makeTransformType(aggRecord->getType()), LINK(aggAssign));
  997. OwnedHqlExpr agg = createDataset(no_newaggregate, LINK(ds), createComma(LINK(aggRecord), LINK(aggTransform)));
  998. OwnedHqlExpr result = createNewSelectExpr(createRow(no_selectnth, LINK(agg), createConstantOne()), LINK(aggField));
  999. buildExprOrAssign(ctx, target, result, tgt);
  1000. }
  1001. //---------------------------------------------------------------------------
  1002. static HqlTransformerInfo graphIndependanceCheckerInfo("GraphIndependanceChecker");
  1003. class GraphIndependanceChecker : public NewHqlTransformer
  1004. {
  1005. public:
  1006. GraphIndependanceChecker(IHqlExpression * _graph) : NewHqlTransformer(graphIndependanceCheckerInfo), graph(_graph) { independent = true; }
  1007. void analyseExpr(IHqlExpression * expr)
  1008. {
  1009. if (!independent || alreadyVisited(expr))
  1010. return;
  1011. switch (expr->getOperator())
  1012. {
  1013. case no_getgraphresult:
  1014. case no_getgraphloopresultset:
  1015. case no_getgraphloopresult:
  1016. if (expr->queryChild(1) == graph)
  1017. {
  1018. independent = false;
  1019. return;
  1020. }
  1021. break;
  1022. }
  1023. NewHqlTransformer::analyseExpr(expr);
  1024. }
  1025. inline bool isIndependent() const { return independent; }
  1026. protected:
  1027. LinkedHqlExpr graph;
  1028. bool independent;
  1029. };
  1030. bool isGraphIndependent(IHqlExpression * expr, IHqlExpression * graph)
  1031. {
  1032. switch (expr->getOperator())
  1033. {
  1034. case no_constant:
  1035. return true;
  1036. }
  1037. GraphIndependanceChecker checker(graph);
  1038. checker.analyse(expr, 0);
  1039. return checker.isIndependent();
  1040. }
  1041. ///--------------------------------------------------------------------------------------------------------------------
  1042. IHqlExpression * createCounterAsGraphResult(IHqlExpression * counter, IHqlExpression * represents, unsigned seq)
  1043. {
  1044. OwnedHqlExpr value = createScalarFromGraphResult(counter->queryType(), unsignedType, represents, seq);
  1045. OwnedHqlExpr internalAttr = createAttribute(internalAtom);
  1046. return createAlias(value, internalAttr);
  1047. }
  1048. ChildGraphExprBuilder::ChildGraphExprBuilder(unsigned _numInputs)
  1049. : numInputs(_numInputs)
  1050. {
  1051. numOutputs=0;
  1052. represents.setown(createAttribute(graphAtom, createUniqueId()));
  1053. resultsExpr.setown(createAttribute(resultsAtom, LINK(represents)));
  1054. }
  1055. IHqlExpression * ChildGraphExprBuilder::addDataset(IHqlExpression * expr)
  1056. {
  1057. OwnedHqlExpr resultNumExpr;
  1058. ForEachItemIn(i, results)
  1059. {
  1060. IHqlExpression & curSetResult = results.item(i);
  1061. if (expr->queryBody() == curSetResult.queryChild(0)->queryBody())
  1062. {
  1063. resultNumExpr.set(curSetResult.queryChild(2));
  1064. break;
  1065. }
  1066. }
  1067. if (!resultNumExpr)
  1068. {
  1069. resultNumExpr.setown(getSizetConstant(numResults()));
  1070. results.append(*createValue(no_setgraphresult, makeVoidType(), LINK(expr), LINK(represents), LINK(resultNumExpr)));
  1071. numOutputs++;
  1072. }
  1073. HqlExprArray args;
  1074. args.append(*LINK(expr->queryRecord()));
  1075. args.append(*LINK(represents));
  1076. args.append(*LINK(resultNumExpr));
  1077. if (isGrouped(expr))
  1078. args.append(*createAttribute(groupedAtom));
  1079. if (!expr->isDataset())
  1080. args.append(*createAttribute(rowAtom));
  1081. args.append(*createAttribute(externalAtom, LINK(resultsExpr)));
  1082. args.append(*createAttribute(_original_Atom, LINK(expr)));
  1083. IHqlExpression * recordCountAttr = queryRecordCountInfo(expr);
  1084. if (recordCountAttr)
  1085. args.append(*LINK(recordCountAttr));
  1086. OwnedHqlExpr ret = createDataset(no_getgraphresult, args);
  1087. if (expr->isDatarow())
  1088. ret.setown(createRow(no_selectnth, LINK(ret), createComma(getSizetConstant(1), createAttribute(noBoundCheckAtom))));
  1089. return ret.getClear();
  1090. }
  1091. void ChildGraphExprBuilder::addAction(IHqlExpression * expr)
  1092. {
  1093. results.append(*LINK(expr));
  1094. }
  1095. unsigned ChildGraphExprBuilder::addInput()
  1096. {
  1097. unsigned id = numResults();
  1098. numInputs++;
  1099. return id;
  1100. }
  1101. IHqlExpression * ChildGraphExprBuilder::getGraph()
  1102. {
  1103. HqlExprArray args;
  1104. args.append(*LINK(represents));
  1105. args.append(*getSizetConstant(numResults()));
  1106. args.append(*createActionList(results));
  1107. return createValue(no_childquery, makeVoidType(), args);
  1108. }
  1109. //---------------------------------------------------------------------------
  1110. // Child dataset processing
  1111. ChildGraphBuilder::ChildGraphBuilder(HqlCppTranslator & _translator, IHqlExpression * subgraph)
  1112. : translator(_translator)
  1113. {
  1114. represents.set(subgraph->queryChild(0));
  1115. id = translator.nextActivityId();
  1116. appendUniqueId(instanceName.append("child"), id);
  1117. instanceExpr.setown(createQuoted(instanceName, makeBoolType()));
  1118. resultsExpr.setown(createAttribute(resultsAtom, LINK(represents)));
  1119. StringBuffer s;
  1120. resultInstanceExpr.setown(createQuoted(appendUniqueId(s.append("res"), id), makeBoolType()));
  1121. numResults = (unsigned)getIntValue(subgraph->queryChild(1));
  1122. IHqlExpression * actions = subgraph->queryChild(2);
  1123. actions->unwindList(results, no_actionlist);
  1124. }
  1125. void ChildGraphBuilder::generateGraph(BuildCtx & ctx)
  1126. {
  1127. BuildCtx graphctx(ctx);
  1128. //Make sure at least one results - because currently that's how we determine if new resourcing is being used
  1129. //Remove this line once all engines use the new child queries exclusively
  1130. if (numResults == 0) numResults++;
  1131. OwnedHqlExpr query = createActionList(results);
  1132. OwnedHqlExpr resourced = translator.getResourcedChildGraph(graphctx, query, represents, numResults, no_none);
  1133. Owned<ParentExtract> extractBuilder = translator.createExtractBuilder(graphctx, PETchild, represents, resourced, true);
  1134. if (!translator.queryOptions().serializeRowsetInExtract)
  1135. extractBuilder->setAllowDestructor();
  1136. translator.beginExtract(graphctx, extractBuilder);
  1137. translator.doBuildThorSubGraph(graphctx, resourced, SubGraphChild, id, represents);
  1138. EvalContext * instance = translator.queryEvalContext(graphctx);
  1139. OwnedHqlExpr retInstanceExpr;
  1140. if (instance && !translator.insideOnCreate(graphctx))
  1141. retInstanceExpr.setown(instance->createGraphLookup(id, true));
  1142. else
  1143. retInstanceExpr.setown(translator.doCreateGraphLookup(graphctx, graphctx, id, "this", true));
  1144. assertex(retInstanceExpr == instanceExpr);
  1145. CHqlBoundExpr boundExtract;
  1146. extractBuilder->endCreateExtract(boundExtract);
  1147. HqlExprArray args;
  1148. args.append(*LINK(instanceExpr));
  1149. args.append(*createTranslated(boundExtract.length));
  1150. args.append(*boundExtract.getTranslatedExpr());
  1151. OwnedHqlExpr call = translator.bindFunctionCall(evaluateChildQueryInstanceAtom, args);
  1152. CHqlBoundExpr bound;
  1153. translator.buildExpr(graphctx, call, bound);
  1154. StringBuffer s;
  1155. s.append("Owned<IEclGraphResults> ");
  1156. translator.generateExprCpp(s, resultInstanceExpr);
  1157. s.append(" = ");
  1158. translator.generateExprCpp(s, bound.expr);
  1159. s.append(";");
  1160. graphctx.addQuoted(s);
  1161. translator.endExtract(graphctx, extractBuilder);
  1162. ctx.associateExpr(resultsExpr, resultInstanceExpr);
  1163. }
  1164. void ChildGraphBuilder::generatePrefetchGraph(BuildCtx & _ctx, OwnedHqlExpr * retGraphExpr)
  1165. {
  1166. BuildCtx ctx(_ctx);
  1167. ctx.addGroup();
  1168. BuildCtx aliasctx(ctx);
  1169. aliasctx.addGroup();
  1170. OwnedHqlExpr query = createActionList(results);
  1171. OwnedHqlExpr resourced = translator.getResourcedChildGraph(ctx, query, represents, numResults, no_none);
  1172. Owned<ParentExtract> extractBuilder = translator.createExtractBuilder(ctx, PETchild, represents, resourced, false);
  1173. createBuilderAlias(aliasctx, extractBuilder);
  1174. translator.beginExtract(ctx, extractBuilder);
  1175. translator.doBuildThorSubGraph(ctx, resourced, SubGraphChild, id, represents);
  1176. EvalContext * instance = translator.queryEvalContext(ctx);
  1177. OwnedHqlExpr retInstanceExpr;
  1178. assertex(instance && !translator.insideOnCreate(ctx));
  1179. retInstanceExpr.setown(instance->createGraphLookup(id, true));
  1180. assertex(retInstanceExpr == instanceExpr);
  1181. retGraphExpr->setown(retInstanceExpr.getClear());
  1182. }
  1183. void ChildGraphBuilder::createBuilderAlias(BuildCtx & ctx, ParentExtract * extractBuilder)
  1184. {
  1185. StringBuffer s;
  1186. s.append("rtlRowBuilder & ");
  1187. translator.generateExprCpp(s, extractBuilder->queryExtractName());
  1188. s.append(" = builder;");
  1189. ctx.addQuoted(s);
  1190. }
  1191. unique_id_t ChildGraphBuilder::buildLoopBody(BuildCtx & ctx, bool multiInstance)
  1192. {
  1193. BuildCtx subctx(ctx);
  1194. subctx.addGroup();
  1195. OwnedHqlExpr query = createActionList(results);
  1196. OwnedHqlExpr resourced = translator.getResourcedChildGraph(ctx, query, represents, numResults, no_loop);
  1197. //Add a flag to indicate multi instance
  1198. if (multiInstance)
  1199. resourced.setown(appendOwnedOperand(resourced, createAttribute(multiInstanceAtom)));
  1200. bool isGlobalThorLoop = translator.targetThor() && !translator.insideChildQuery(ctx);
  1201. Owned<ParentExtract> extractBuilder = isGlobalThorLoop ? translator.createExtractBuilder(ctx, PETloop, represents, GraphRemote, false)
  1202. : translator.createExtractBuilder(ctx, PETloop, represents, resourced, false);
  1203. createBuilderAlias(subctx, extractBuilder);
  1204. translator.beginExtract(ctx, extractBuilder);
  1205. translator.doBuildThorSubGraph(ctx, resourced, SubGraphLoop, id, represents);
  1206. translator.endExtract(ctx, extractBuilder);
  1207. return id;
  1208. }
  1209. static HqlTransformerInfo graphLoopReplacerInfo("GraphLoopReplacer");
  1210. class GraphLoopReplacer : public NewHqlTransformer
  1211. {
  1212. public:
  1213. GraphLoopReplacer(IHqlExpression * _rowsid, IHqlExpression * _represents, IHqlExpression * _counter, bool _isParallel) :
  1214. NewHqlTransformer(graphLoopReplacerInfo), rowsid(_rowsid), represents(_represents), counter(_counter), isParallel(_isParallel)
  1215. {
  1216. }
  1217. virtual IHqlExpression * createTransformed(IHqlExpression * expr)
  1218. {
  1219. switch (expr->getOperator())
  1220. {
  1221. case no_counter:
  1222. if (expr->queryBody() == counter)
  1223. {
  1224. if (isParallel)
  1225. {
  1226. HqlExprArray args;
  1227. args.append(*LINK(represents));
  1228. // unwindChildren(args, expr);
  1229. OwnedHqlExpr ret = createValue(no_loopcounter, expr->getType(), args);
  1230. //Yuk: Wrap this in an alias to ensure it is evaluated at the correct place.
  1231. //there has to be a better way..... We could...
  1232. //a) strictly defined when it can be evaluated - e.g., ctx->defines(graph) && (!parentctx || !parentctx->definesGraph)
  1233. //b) set a flag in the expression to indicate forced evaluation (even worse than the alias)
  1234. //c) add the code to evaluate no_loopcounter inside evaluateInContext
  1235. return createAlias(ret, internalAttrExpr);
  1236. }
  1237. else
  1238. {
  1239. counterResult.setown(createCounterAsGraphResult(counter, represents, 0));
  1240. return LINK(counterResult);
  1241. }
  1242. }
  1243. break;
  1244. case no_rowsetindex:
  1245. {
  1246. IHqlExpression * rowset = expr->queryChild(0);
  1247. if (rowset->getOperator() != no_rowset)
  1248. break;
  1249. IHqlExpression * rows = rowset->queryChild(0);
  1250. if (rows->queryChild(1) != rowsid)
  1251. break;
  1252. HqlExprArray args;
  1253. args.append(*LINK(rows->queryChild(0)->queryRecord()));
  1254. args.append(*LINK(represents));
  1255. args.append(*transform(expr->queryChild(1)));
  1256. return createDataset(no_getgraphloopresult, args);
  1257. }
  1258. case no_rowset:
  1259. {
  1260. IHqlExpression * rows = expr->queryChild(0);
  1261. if (rows->queryChild(1) != rowsid)
  1262. break;
  1263. HqlExprArray args;
  1264. args.append(*LINK(rows->queryChild(0)->queryRecord()));
  1265. args.append(*LINK(represents));
  1266. return createValue(no_getgraphloopresultset, expr->getType(), args);
  1267. }
  1268. }
  1269. return NewHqlTransformer::createTransformed(expr);
  1270. }
  1271. inline IHqlExpression * queryCounterResult() { return counterResult; }
  1272. protected:
  1273. IHqlExpression * rowsid;
  1274. IHqlExpression * represents;
  1275. IHqlExpression * counter;
  1276. OwnedHqlExpr counterResult;
  1277. bool isParallel;
  1278. };
  1279. unique_id_t ChildGraphBuilder::buildGraphLoopBody(BuildCtx & ctx, bool isParallel)
  1280. {
  1281. BuildCtx subctx(ctx);
  1282. subctx.addGroup();
  1283. OwnedHqlExpr query = createActionList(results);
  1284. translator.traceExpression("Before Loop resource", query);
  1285. OwnedHqlExpr resourced = translator.getResourcedChildGraph(ctx, query, represents, numResults, no_loop);
  1286. translator.traceExpression("After Loop resource", resourced);
  1287. //Add a flag to indicate multi instance
  1288. if (isParallel)
  1289. {
  1290. HqlExprArray args;
  1291. unwindChildren(args, resourced);
  1292. args.append(*createAttribute(multiInstanceAtom));
  1293. args.append(*createAttribute(delayedAtom));
  1294. resourced.setown(resourced->clone(args));
  1295. }
  1296. bool isGlobalThorLoop = translator.targetThor() && !translator.insideChildQuery(ctx);
  1297. Owned<ParentExtract> extractBuilder = isGlobalThorLoop ? translator.createExtractBuilder(ctx, PETloop, represents, GraphRemote, false)
  1298. : translator.createExtractBuilder(ctx, PETloop, represents, resourced, false);
  1299. createBuilderAlias(subctx, extractBuilder);
  1300. translator.beginExtract(ctx, extractBuilder);
  1301. translator.doBuildThorSubGraph(ctx, resourced, SubGraphLoop, id, represents);
  1302. translator.endExtract(ctx, extractBuilder);
  1303. return id;
  1304. }
  1305. unique_id_t ChildGraphBuilder::buildRemoteGraph(BuildCtx & ctx)
  1306. {
  1307. BuildCtx subctx(ctx);
  1308. subctx.addGroup();
  1309. OwnedHqlExpr query = createActionList(results);
  1310. OwnedHqlExpr resourced = translator.getResourcedChildGraph(ctx, query, represents, numResults, no_allnodes);
  1311. Owned<ParentExtract> extractBuilder = translator.createExtractBuilder(ctx, PETremote, represents, GraphRemote, false);
  1312. createBuilderAlias(subctx, extractBuilder);
  1313. translator.beginExtract(ctx, extractBuilder);
  1314. translator.doBuildThorSubGraph(ctx, resourced, SubGraphChild, id, represents);
  1315. translator.endExtract(ctx, extractBuilder);
  1316. return id;
  1317. }
  1318. void HqlCppTranslator::buildChildGraph(BuildCtx & ctx, IHqlExpression * expr)
  1319. {
  1320. IHqlExpression * represents= expr->queryChild(0);
  1321. OwnedHqlExpr resultsExpr = createAttribute(resultsAtom, LINK(represents));
  1322. //Shouldn't really happen, but if this graph has already benn called just use the results
  1323. if (ctx.queryMatchExpr(resultsExpr))
  1324. return;
  1325. ChildGraphBuilder graphBuilder(*this, expr);
  1326. graphBuilder.generateGraph(ctx);
  1327. }
  1328. void HqlCppTranslator::beginExtract(BuildCtx & ctx, ParentExtract * extractBuilder)
  1329. {
  1330. ctx.associate(*extractBuilder);
  1331. }
  1332. void HqlCppTranslator::endExtract(BuildCtx & ctx, ParentExtract * extractBuilder)
  1333. {
  1334. extractBuilder->endUseExtract(ctx);
  1335. ctx.removeAssociation(extractBuilder);
  1336. }
  1337. void HqlCppTranslator::buildAssignChildDataset(BuildCtx & ctx, const CHqlBoundTarget & target, IHqlExpression * expr)
  1338. {
  1339. switch (expr->getOperator())
  1340. {
  1341. case no_call:
  1342. case no_externalcall:
  1343. case no_libraryinput:
  1344. buildDatasetAssign(ctx, target, expr);
  1345. return;
  1346. }
  1347. OwnedHqlExpr call;
  1348. {
  1349. ChildGraphExprBuilder builder(0);
  1350. call.setown(builder.addDataset(expr));
  1351. OwnedHqlExpr subquery = builder.getGraph();
  1352. buildStmt(ctx, subquery);
  1353. }
  1354. buildExprAssign(ctx, target, call);
  1355. }
  1356. IHqlExpression * HqlCppTranslator::getResourcedChildGraph(BuildCtx & ctx, IHqlExpression * expr, IHqlExpression * graphIdExpr, unsigned numResults, node_operator graphKind)
  1357. {
  1358. if (options.paranoidCheckNormalized || options.paranoidCheckDependencies)
  1359. DBGLOG("Before resourcing a child graph");
  1360. LinkedHqlExpr resourced = expr;
  1361. checkNormalized(ctx, resourced);
  1362. unsigned csfFlags = CSFindex|options.optimizeDiskFlag;
  1363. switch (targetClusterType)
  1364. {
  1365. case HThorCluster:
  1366. csfFlags |= CSFcompoundSpill;
  1367. break;
  1368. case ThorCluster:
  1369. case ThorLCRCluster:
  1370. //Don't compound spills inside a child query because it can cause non remote projects to become remote
  1371. //And we'll also probably be using the roxie code to implement
  1372. break;
  1373. case RoxieCluster:
  1374. break;
  1375. }
  1376. {
  1377. unsigned time = msTick();
  1378. CompoundSourceTransformer transformer(*this, CSFpreload|csfFlags);
  1379. resourced.setown(transformer.process(resourced));
  1380. checkNormalized(ctx, resourced);
  1381. DEBUG_TIMER("EclServer: tree transform: optimize disk read", msTick()-time);
  1382. }
  1383. if (options.optimizeChildGraph)
  1384. {
  1385. unsigned time = msTick();
  1386. traceExpression("BeforeOptimizeSub", resourced);
  1387. resourced.setown(optimizeHqlExpression(resourced, getOptimizeFlags()|HOOcompoundproject));
  1388. traceExpression("AfterOptimizeSub", resourced);
  1389. DEBUG_TIMER("EclServer: optimize graph", msTick()-time);
  1390. }
  1391. traceExpression("BeforeResourcing Child", resourced);
  1392. cycle_t time = msTick();
  1393. HqlExprCopyArray activeRows;
  1394. gatherActiveCursors(ctx, activeRows);
  1395. if (graphKind == no_loop)
  1396. {
  1397. bool insideChild = insideChildQuery(ctx);
  1398. resourced.setown(resourceLoopGraph(*this, activeRows, resourced, targetClusterType, graphIdExpr, &numResults, insideChild));
  1399. }
  1400. else
  1401. resourced.setown(resourceNewChildGraph(*this, activeRows, resourced, targetClusterType, graphIdExpr, &numResults));
  1402. DEBUG_TIMER("EclServer: resource graph", msTick()-time);
  1403. checkNormalized(ctx, resourced);
  1404. traceExpression("AfterResourcing Child", resourced);
  1405. //Convert queries on preloaded into compound activities - before resourcing so keyed gets done correctly
  1406. // Second attempt to spot compound disk reads - this time of spill files. Since resourcing has removed
  1407. // any sharing we don't need to bother about sharing.
  1408. {
  1409. unsigned time = msTick();
  1410. CompoundSourceTransformer transformer(*this, csfFlags);
  1411. resourced.setown(transformer.process(resourced));
  1412. DEBUG_TIMER("EclServer: tree transform: optimize disk read", msTick()-time);
  1413. }
  1414. //Now call the optimizer again - the main purpose is to move projects over limits and into compound index/disk reads
  1415. if (options.optimizeChildGraph)
  1416. {
  1417. unsigned time = msTick();
  1418. traceExpression("BeforeOptimize2", resourced);
  1419. resourced.setown(optimizeHqlExpression(resourced, getOptimizeFlags()|HOOcompoundproject));
  1420. traceExpression("AfterOptimize2", resourced);
  1421. DEBUG_TIMER("EclServer: optimize graph", msTick()-time);
  1422. }
  1423. if (numResults == 0) numResults++;
  1424. HqlExprArray children;
  1425. resourced->unwindList(children, no_actionlist);
  1426. children.append(*createAttribute(numResultsAtom, getSizetConstant(numResults)));
  1427. children.append(*LINK(graphIdExpr));
  1428. resourced.setown(createValue(no_subgraph, makeVoidType(), children));
  1429. if (options.paranoidCheckNormalized || options.paranoidCheckDependencies)
  1430. DBGLOG("After resourcing a child graph");
  1431. return resourced.getClear();
  1432. }
  1433. void HqlCppTranslator::buildChildDataset(BuildCtx & ctx, IHqlExpression * expr, CHqlBoundExpr & tgt)
  1434. {
  1435. if (expr->isPure() && ctx.getMatchExpr(expr, tgt))
  1436. return;
  1437. LoopInvariantHelper helper;
  1438. BuildCtx bestctx(ctx);
  1439. if (options.optimizeLoopInvariant)
  1440. helper.getBestContext(bestctx, expr);
  1441. CHqlBoundTarget temp;
  1442. //MORE: Should have similar code to buildTempExpr()
  1443. createTempFor(bestctx, expr, temp);
  1444. buildAssignChildDataset(bestctx, temp, expr);
  1445. tgt.setFromTarget(temp);
  1446. if (expr->isPure())
  1447. bestctx.associateExpr(expr, tgt);
  1448. }
  1449. unique_id_t HqlCppTranslator::buildGraphLoopSubgraph(BuildCtx & ctx, IHqlExpression * dataset, IHqlExpression * selSeq, IHqlExpression * rowsid, IHqlExpression * body, IHqlExpression * counter, bool multiInstance)
  1450. {
  1451. ChildGraphExprBuilder graphBuilder(0);
  1452. OwnedHqlExpr transformedBody;
  1453. OwnedHqlExpr counterResult;
  1454. IHqlExpression * graphid = graphBuilder.queryRepresents();
  1455. {
  1456. const bool isParallel = multiInstance;
  1457. GraphLoopReplacer replacer(rowsid, graphid, counter, isParallel);
  1458. transformedBody.setown(replacer.transformRoot(body));
  1459. counterResult.set(replacer.queryCounterResult());
  1460. }
  1461. if (counterResult)
  1462. graphBuilder.addInput();
  1463. OwnedHqlExpr result = createValue(no_setgraphloopresult, makeVoidType(), LINK(transformedBody), LINK(graphid));
  1464. graphBuilder.addAction(result);
  1465. OwnedHqlExpr subquery = graphBuilder.getGraph();
  1466. ChildGraphBuilder builder(*this, subquery);
  1467. return builder.buildGraphLoopBody(ctx, multiInstance);
  1468. }
  1469. unique_id_t HqlCppTranslator::buildRemoteSubgraph(BuildCtx & ctx, IHqlExpression * dataset)
  1470. {
  1471. ChildGraphExprBuilder graphBuilder(0);
  1472. if (dataset->isAction())
  1473. {
  1474. graphBuilder.addAction(dataset);
  1475. }
  1476. else
  1477. {
  1478. OwnedHqlExpr ignoredResult = graphBuilder.addDataset(dataset);
  1479. }
  1480. OwnedHqlExpr subquery = graphBuilder.getGraph();
  1481. ChildGraphBuilder builder(*this, subquery);
  1482. return builder.buildRemoteGraph(ctx);
  1483. }
  1484. //---------------------------------------------------------------------------
  1485. // Functions to check whether a dataset can be evaluated inline or not.
  1486. //MORE: These should probably be split out into an hqlinline.cpp
  1487. bool HqlCppTranslator::canIterateInline(BuildCtx * ctx, IHqlExpression * expr)
  1488. {
  1489. return (isInlineOk() && ::canIterateInline(ctx, expr));
  1490. }
  1491. bool HqlCppTranslator::canAssignInline(BuildCtx * ctx, IHqlExpression * expr)
  1492. {
  1493. if (!isInlineOk())
  1494. return false;
  1495. return options.allowInlineSpill ? ::canProcessInline(ctx, expr) : ::canAssignInline(ctx, expr);
  1496. }
  1497. bool HqlCppTranslator::canEvaluateInline(BuildCtx * ctx, IHqlExpression * expr)
  1498. {
  1499. if (!isInlineOk())
  1500. return false;
  1501. return options.allowInlineSpill ? ::canProcessInline(ctx, expr) : ::canEvaluateInline(ctx, expr);
  1502. }
  1503. bool HqlCppTranslator::canProcessInline(BuildCtx * ctx, IHqlExpression * expr)
  1504. {
  1505. if (!isInlineOk())
  1506. return false;
  1507. return ::canProcessInline(ctx, expr);
  1508. }
  1509. bool HqlCppTranslator::isInlineOk()
  1510. {
  1511. if (!activeGraphCtx)
  1512. return true;
  1513. return true;
  1514. }
  1515. IHqlExpression * HqlCppTranslator::buildSpillChildDataset(BuildCtx & ctx, IHqlExpression * expr)
  1516. {
  1517. CHqlBoundExpr bound;
  1518. buildChildDataset(ctx, expr, bound);
  1519. return bound.getTranslatedExpr();
  1520. }
  1521. IHqlExpression * HqlCppTranslator::forceInlineAssignDataset(BuildCtx & ctx, IHqlExpression * expr)
  1522. {
  1523. loop
  1524. {
  1525. CHqlBoundExpr bound;
  1526. if (expr->isPure() && ctx.getMatchExpr(expr, bound))
  1527. return bound.getTranslatedExpr();
  1528. if (canProcessInline(&ctx, expr) || (expr->getOperator() == no_translated))
  1529. return LINK(expr);
  1530. switch (expr->getOperator())
  1531. {
  1532. case no_compound:
  1533. buildStmt(ctx, expr->queryChild(0));
  1534. expr = expr->queryChild(1);
  1535. break;
  1536. default:
  1537. return buildSpillChildDataset(ctx, expr);
  1538. }
  1539. }
  1540. }
  1541. //---------------------------------------------------------------------------
  1542. // Dataset temp creation
  1543. IHqlExpression * createGetResultFromWorkunitDataset(IHqlExpression * expr)
  1544. {
  1545. IHqlExpression * name = queryPropertyChild(expr, nameAtom, 0);
  1546. if (name)
  1547. name = createExprAttribute(namedAtom, LINK(name));
  1548. assertex(expr->isDataset());
  1549. return createDataset(no_getresult, LINK(expr->queryRecord()), createComma(LINK(expr->queryProperty(sequenceAtom)), name));
  1550. }
  1551. void HqlCppTranslator::buildAssignSerializedDataset(BuildCtx & ctx, const CHqlBoundTarget & target, IHqlExpression * expr)
  1552. {
  1553. OwnedITypeInfo serializedType = getSerializedForm(expr->queryType());
  1554. assertex(recordTypesMatch(target.queryType(), serializedType));
  1555. HqlExprArray args;
  1556. args.append(*createRowSerializer(ctx, expr->queryRecord(), serializerAtom));
  1557. args.append(*LINK(expr));
  1558. OwnedHqlExpr call = bindFunctionCall(rowset2DatasetXAtom, args);
  1559. buildExprAssign(ctx, target, call);
  1560. }
  1561. void HqlCppTranslator::buildSerializedDataset(BuildCtx & ctx, IHqlExpression * expr, CHqlBoundExpr & tgt)
  1562. {
  1563. CHqlBoundTarget target;
  1564. OwnedITypeInfo serializedType = getSerializedForm(expr->queryType());
  1565. createTempFor(ctx, serializedType, target, typemod_none, FormatBlockedDataset);
  1566. buildAssignSerializedDataset(ctx, target, expr);
  1567. tgt.setFromTarget(target);
  1568. }
  1569. void HqlCppTranslator::buildAssignLinkedDataset(BuildCtx & ctx, const CHqlBoundTarget & target, IHqlExpression * expr)
  1570. {
  1571. OwnedITypeInfo serializedType = getSerializedForm(target.queryType());
  1572. assertex(recordTypesMatch(serializedType, expr->queryType()));
  1573. IHqlExpression * record = ::queryRecord(target.queryType());
  1574. HqlExprArray args;
  1575. args.append(*createRowSerializer(ctx, record, deserializerAtom));
  1576. args.append(*LINK(expr));
  1577. OwnedHqlExpr call = bindFunctionCall(isGrouped(expr) ? groupedDataset2RowsetXAtom : dataset2RowsetXAtom, args, target.queryType());
  1578. buildExprAssign(ctx, target, call);
  1579. }
  1580. void HqlCppTranslator::buildLinkedDataset(BuildCtx & ctx, ITypeInfo * type, IHqlExpression * expr, CHqlBoundExpr & tgt)
  1581. {
  1582. CHqlBoundTarget target;
  1583. createTempFor(ctx, type, target, typemod_none, FormatLinkedDataset);
  1584. if (hasLinkedRow(expr->queryType()))
  1585. buildDatasetAssign(ctx, target, expr);
  1586. else
  1587. buildAssignLinkedDataset(ctx, target, expr);
  1588. tgt.setFromTarget(target);
  1589. }
  1590. void HqlCppTranslator::ensureDatasetFormat(BuildCtx & ctx, ITypeInfo * type, CHqlBoundExpr & tgt, ExpressionFormat format)
  1591. {
  1592. switch (format)
  1593. {
  1594. case FormatBlockedDataset:
  1595. if (isArrayRowset(tgt.queryType()))
  1596. {
  1597. OwnedHqlExpr deserializedExpr = tgt.getTranslatedExpr();
  1598. LinkedHqlExpr savedCount = tgt.count;
  1599. buildSerializedDataset(ctx, deserializedExpr, tgt);
  1600. if (savedCount && !isFixedWidthDataset(deserializedExpr))
  1601. tgt.count.set(savedCount);
  1602. return;
  1603. }
  1604. break;
  1605. case FormatLinkedDataset:
  1606. if (!hasLinkCountedModifier(tgt.queryType()))
  1607. {
  1608. OwnedHqlExpr serializedExpr = tgt.getTranslatedExpr();
  1609. buildLinkedDataset(ctx, type, serializedExpr, tgt);
  1610. return;
  1611. }
  1612. break;
  1613. case FormatArrayDataset:
  1614. if (!isArrayRowset(tgt.queryType()))
  1615. {
  1616. OwnedHqlExpr serializedExpr = tgt.getTranslatedExpr();
  1617. buildLinkedDataset(ctx, type, serializedExpr, tgt);
  1618. return;
  1619. }
  1620. break;
  1621. }
  1622. }
  1623. void HqlCppTranslator::buildDataset(BuildCtx & ctx, IHqlExpression * expr, CHqlBoundExpr & tgt, ExpressionFormat format)
  1624. {
  1625. doBuildDataset(ctx, expr, tgt, format);
  1626. ensureDatasetFormat(ctx, expr->queryType(), tgt, format);
  1627. }
  1628. void HqlCppTranslator::doBuildDataset(BuildCtx & ctx, IHqlExpression * expr, CHqlBoundExpr & tgt, ExpressionFormat format)
  1629. {
  1630. if (expr->isPure() && ctx.getMatchExpr(expr, tgt))
  1631. return;
  1632. /*
  1633. OwnedHqlExpr transformed = normalizeAnyDatasetAliases(expr);
  1634. if (transformed && (transformed != expr))
  1635. {
  1636. doBuildDataset(ctx, transformed, tgt, format);
  1637. ctx.associateExpr(expr, tgt);
  1638. return;
  1639. }
  1640. */
  1641. node_operator op = expr->getOperator();
  1642. switch (op)
  1643. {
  1644. case no_dataset_alias:
  1645. if (!expr->hasProperty(_normalized_Atom))
  1646. {
  1647. OwnedHqlExpr uniqueChild = normalizeDatasetAlias(expr);
  1648. doBuildDataset(ctx, uniqueChild, tgt, format);
  1649. }
  1650. else
  1651. doBuildDataset(ctx, expr->queryChild(0), tgt, format);
  1652. return;
  1653. case no_alias:
  1654. doBuildExprAlias(ctx, expr, &tgt);
  1655. return;
  1656. case no_owned_ds:
  1657. buildTempExpr(ctx, expr, tgt);
  1658. return;
  1659. case no_null:
  1660. {
  1661. tgt.count.setown(getSizetConstant(0));
  1662. tgt.length.setown(getSizetConstant(0));
  1663. IHqlExpression * record = expr->queryRecord();
  1664. ITypeInfo * type = makeTableType(makeRowType(record->getType()), NULL, NULL, NULL);
  1665. if ((format == FormatLinkedDataset) || (format == FormatArrayDataset))
  1666. type = makeAttributeModifier(type, getLinkCountedAttr());
  1667. tgt.expr.setown(createValue(no_nullptr, makeReferenceModifier(type)));
  1668. return;
  1669. }
  1670. case no_translated:
  1671. expandTranslated(expr, tgt);
  1672. return;
  1673. case no_select:
  1674. {
  1675. if (isMultiLevelDatasetSelector(expr, false))
  1676. break;
  1677. Owned<IReferenceSelector> selected = buildReference(ctx, expr);
  1678. selected->get(ctx, tgt);
  1679. return;
  1680. }
  1681. case no_libraryinput:
  1682. if (!buildExprInCorrectContext(ctx, expr, tgt, false))
  1683. throwUnexpected();
  1684. return;
  1685. case no_call:
  1686. case no_externalcall:
  1687. if (!hasStreamedModifier(expr->queryType()))
  1688. {
  1689. buildTempExpr(ctx, expr, tgt);
  1690. return;
  1691. }
  1692. break;
  1693. case no_newaggregate:
  1694. if (canAssignInline(&ctx, expr))
  1695. {
  1696. Owned<BoundRow> tempRow = declareTempAnonRow(ctx, ctx, expr);
  1697. Owned<BoundRow> rowBuilder = createRowBuilder(ctx, tempRow);
  1698. Owned<IReferenceSelector> createdRef = createReferenceSelector(rowBuilder);
  1699. BuildCtx subctx(ctx);
  1700. subctx.addGroup();
  1701. doBuildRowAssignAggregate(subctx, createdRef, expr);
  1702. finalizeTempRow(ctx, tempRow, rowBuilder);
  1703. convertBoundRowToDataset(ctx, tgt, tempRow, format);
  1704. return;
  1705. }
  1706. break;
  1707. case no_id2blob:
  1708. doBuildExprIdToBlob(ctx, expr, tgt);
  1709. return;
  1710. case no_rows:
  1711. {
  1712. if (!buildExprInCorrectContext(ctx, expr, tgt, false))
  1713. throwError(HQLERR_RowsUsedOutsideContext);
  1714. return;
  1715. }
  1716. case no_limit:
  1717. if (expr->hasProperty(skipAtom) || expr->hasProperty(onFailAtom))
  1718. break;
  1719. doBuildDatasetLimit(ctx, expr, tgt, format);
  1720. return;
  1721. case no_compound_childread:
  1722. case no_compound_childnormalize:
  1723. case no_compound_childaggregate:
  1724. case no_compound_selectnew:
  1725. case no_compound_inline:
  1726. case no_distributed:
  1727. case no_preservemeta:
  1728. case no_sorted:
  1729. case no_nofold:
  1730. case no_nohoist:
  1731. case no_section:
  1732. case no_sectioninput:
  1733. buildDataset(ctx, expr->queryChild(0), tgt, format);
  1734. return;
  1735. case no_forcegraph:
  1736. #ifdef _DEBUG
  1737. throwUnexpected();
  1738. #endif
  1739. buildDataset(ctx, expr->queryChild(0), tgt, format);
  1740. return;
  1741. case no_getgraphresult:
  1742. doBuildExprGetGraphResult(ctx, expr, tgt, format);
  1743. return;
  1744. case no_getresult:
  1745. case no_workunit_dataset:
  1746. if (!isGrouped(expr))
  1747. {
  1748. doBuildExprGetResult(ctx, expr, tgt);
  1749. return;
  1750. }
  1751. break;
  1752. case no_skip:
  1753. {
  1754. buildStmt(ctx, expr);
  1755. OwnedHqlExpr null = createNullExpr(expr);
  1756. buildDataset(ctx, null, tgt, format);
  1757. return;
  1758. }
  1759. case no_serialize:
  1760. if (isDummySerializeDeserialize(expr))
  1761. doBuildDataset(ctx, expr->queryChild(0)->queryChild(0), tgt, format);
  1762. else
  1763. buildSerializedDataset(ctx, expr->queryChild(0), tgt);
  1764. return;
  1765. case no_deserialize:
  1766. if (isDummySerializeDeserialize(expr))
  1767. doBuildDataset(ctx, expr->queryChild(0)->queryChild(0), tgt, format);
  1768. else
  1769. buildLinkedDataset(ctx, expr->queryType(), expr->queryChild(0), tgt);
  1770. return;
  1771. case no_datasetfromrow:
  1772. {
  1773. IHqlExpression * row = expr->queryChild(0);
  1774. if (isAlwaysActiveRow(row) && (format == FormatNatural))
  1775. {
  1776. Owned<IReferenceSelector> selector = buildActiveRow(ctx, row);
  1777. BuildCtx groupctx(ctx);
  1778. groupctx.addGroup();
  1779. BoundRow * bound = bindSelectorAsRootRow(groupctx, selector, row);
  1780. convertBoundRowToDataset(groupctx, tgt, bound, format);
  1781. tgt.count.setown(getSizetConstant(1));
  1782. ctx.associateExpr(expr, tgt);
  1783. return;
  1784. }
  1785. break;
  1786. }
  1787. case no_inlinetable:
  1788. if (doBuildDatasetInlineTable(ctx, expr, tgt, format))
  1789. return;
  1790. break;
  1791. case no_compound:
  1792. {
  1793. buildStmt(ctx, expr->queryChild(0));
  1794. buildDataset(ctx, expr->queryChild(1), tgt, format);
  1795. return;
  1796. }
  1797. }
  1798. bool singleRow = hasSingleRow(expr);
  1799. bool useTempRow = singleRow && canAssignInline(&ctx, expr) && (format != FormatLinkedDataset) && (format != FormatArrayDataset);
  1800. //Conditional row assignment if variable length causes offset to be recalculated outside of the if()
  1801. //if (useTempRow && (op == no_if) && isVariableSizeRecord(expr->queryRecord()))
  1802. // useTempRow = false;
  1803. if (useTempRow)
  1804. {
  1805. Owned<BoundRow> tempRow = declareTempAnonRow(ctx, ctx, expr);
  1806. Owned<BoundRow> rowBuilder = createRowBuilder(ctx, tempRow);
  1807. Owned<IHqlCppDatasetBuilder> builder = createSingleRowTempDatasetBuilder(expr->queryRecord(), rowBuilder);
  1808. builder->buildDeclare(ctx);
  1809. buildDatasetAssign(ctx, builder, expr);
  1810. //builder->buildFinish(ctx, tempTarget);
  1811. finalizeTempRow(ctx, tempRow, rowBuilder);
  1812. convertBoundRowToDataset(ctx, tgt, tempRow, format);
  1813. }
  1814. else
  1815. {
  1816. if (!canAssignInline(&ctx, expr))
  1817. {
  1818. CHqlBoundTarget tempTarget;
  1819. createTempFor(ctx, expr->queryType(), tempTarget, typemod_none, format);
  1820. buildDatasetAssign(ctx, tempTarget, expr);
  1821. tgt.setFromTarget(tempTarget);
  1822. //buildTempExpr(ctx, expr, tgt); // can't use this because it causes recursion on no_selectnth
  1823. }
  1824. else
  1825. {
  1826. Owned<IHqlCppDatasetBuilder> builder;
  1827. IHqlExpression * record = expr->queryRecord();
  1828. OwnedHqlExpr serializedRecord = getSerializedForm(record);
  1829. if (format == FormatNatural)
  1830. {
  1831. if (record != serializedRecord)
  1832. ensureContextAvailable(ctx);
  1833. if (!ctx.queryMatchExpr(codeContextMarkerExpr))
  1834. {
  1835. if (record != serializedRecord)
  1836. throwError(HQLERR_LinkedDatasetNoContext);
  1837. format = FormatBlockedDataset;
  1838. }
  1839. else if ((record == serializedRecord) && !options.tempDatasetsUseLinkedRows)
  1840. {
  1841. format = FormatBlockedDataset;
  1842. }
  1843. else
  1844. {
  1845. format = FormatLinkedDataset;
  1846. }
  1847. }
  1848. else if (record != serializedRecord)
  1849. format = FormatLinkedDataset; // Have to serialize it later - otherwise it won't be compatible
  1850. if (format == FormatLinkedDataset || format == FormatArrayDataset)
  1851. {
  1852. if (expr->isDictionary())
  1853. {
  1854. builder.setown(createLinkedDictionaryBuilder(record));
  1855. }
  1856. else
  1857. {
  1858. IHqlExpression * choosenLimit = NULL;
  1859. if ((op == no_choosen) && !isChooseNAllLimit(expr->queryChild(1)) && !queryRealChild(expr, 2))
  1860. {
  1861. choosenLimit = expr->queryChild(1);
  1862. expr = expr->queryChild(0);
  1863. }
  1864. //MORE: Extract limit and choosen and pass as parameters
  1865. builder.setown(createLinkedDatasetBuilder(record, choosenLimit));
  1866. }
  1867. }
  1868. else if ((op == no_choosen) && !isChooseNAllLimit(expr->queryChild(1)) && !queryRealChild(expr, 2))
  1869. {
  1870. //Build a limited builder - it is likely to be just as efficient, and often much more e.g., choosen(a+b, n)
  1871. builder.setown(createChoosenDatasetBuilder(serializedRecord, expr->queryChild(1)));
  1872. expr = expr->queryChild(0);
  1873. }
  1874. else
  1875. builder.setown(createBlockedDatasetBuilder(serializedRecord));
  1876. builder->buildDeclare(ctx);
  1877. buildDatasetAssign(ctx, builder, expr);
  1878. builder->buildFinish(ctx, tgt);
  1879. }
  1880. }
  1881. if (singleRow)
  1882. tgt.count.setown(getSizetConstant(1));
  1883. else if (op == no_inlinetable)
  1884. {
  1885. IHqlExpression * transforms = expr->queryChild(0);
  1886. if (!transformListContainsSkip(transforms))
  1887. tgt.count.setown(getSizetConstant(transforms->numChildren()));
  1888. }
  1889. ctx.associateExpr(expr, tgt);
  1890. }
  1891. //---------------------------------------------------------------------------
  1892. // Dataset assignment - to temp
  1893. void HqlCppTranslator::buildDatasetAssign(BuildCtx & ctx, const CHqlBoundTarget & target, IHqlExpression * expr)
  1894. {
  1895. node_operator op = expr->getOperator();
  1896. switch (op)
  1897. {
  1898. case no_call:
  1899. case no_externalcall:
  1900. if (!hasStreamedModifier(expr->queryType()))
  1901. {
  1902. doBuildCall(ctx, &target, expr, NULL);
  1903. return;
  1904. }
  1905. break;
  1906. case no_getgraphresult:
  1907. doBuildAssignGetGraphResult(ctx, target, expr);
  1908. return;
  1909. case no_workunit_dataset:
  1910. case no_getresult:
  1911. buildExprAssign(ctx, target, expr);
  1912. return;
  1913. case no_null:
  1914. {
  1915. CHqlBoundExpr bound;
  1916. buildDataset(ctx, expr, bound, isArrayRowset(expr->queryType()) ? FormatLinkedDataset : FormatBlockedDataset);
  1917. OwnedHqlExpr translated = bound.getTranslatedExpr();
  1918. if (target.count) ctx.addAssign(target.count, bound.count);
  1919. if (target.length) ctx.addAssign(target.length, bound.length);
  1920. ctx.addAssign(target.expr, bound.expr);
  1921. return;
  1922. }
  1923. case no_inlinedictionary:
  1924. case no_inlinetable:
  1925. {
  1926. //This will typically generate a loop. If few items then it is more efficient to expand the assigns/clones out.
  1927. if (expr->queryChild(0)->numChildren() > INLINE_TABLE_EXPAND_LIMIT)
  1928. {
  1929. CHqlBoundExpr bound;
  1930. if (doBuildDatasetInlineTable(ctx, expr, bound, FormatNatural))
  1931. {
  1932. OwnedHqlExpr translated = bound.getTranslatedExpr();
  1933. buildDatasetAssign(ctx, target, translated);
  1934. return;
  1935. }
  1936. }
  1937. break;
  1938. }
  1939. case no_alias:
  1940. {
  1941. CHqlBoundExpr bound;
  1942. buildDataset(ctx, expr, bound, FormatNatural);
  1943. OwnedHqlExpr translated = bound.getTranslatedExpr();
  1944. buildDatasetAssign(ctx, target, translated);
  1945. return;
  1946. }
  1947. case no_owned_ds:
  1948. {
  1949. ITypeInfo * targetType = target.queryType();
  1950. if (hasLinkCountedModifier(targetType) && hasWrapperModifier(targetType))
  1951. {
  1952. CHqlBoundExpr bound;
  1953. buildDataset(ctx, expr->queryChild(0), bound, FormatLinkedDataset);
  1954. OwnedHqlExpr compound = createValue(no_complex, bound.expr->getType(), LINK(bound.count), LINK(bound.expr));
  1955. ctx.addAssign(target.expr, compound);
  1956. return;
  1957. }
  1958. break;
  1959. }
  1960. case no_compound:
  1961. {
  1962. buildStmt(ctx, expr->queryChild(0));
  1963. buildDatasetAssign(ctx, target, expr->queryChild(1));
  1964. return;
  1965. }
  1966. case no_compound_childread:
  1967. case no_compound_childnormalize:
  1968. case no_compound_childaggregate:
  1969. case no_compound_selectnew:
  1970. case no_compound_inline:
  1971. case no_distributed:
  1972. case no_preservemeta:
  1973. case no_sorted:
  1974. case no_nofold:
  1975. case no_nohoist:
  1976. case no_section:
  1977. case no_sectioninput:
  1978. buildDatasetAssign(ctx, target, expr->queryChild(0));
  1979. return;
  1980. case no_serialize:
  1981. if (isDummySerializeDeserialize(expr))
  1982. buildDatasetAssign(ctx, target, expr->queryChild(0)->queryChild(0));
  1983. else
  1984. buildAssignSerializedDataset(ctx, target, expr->queryChild(0));
  1985. return;
  1986. case no_deserialize:
  1987. if (isDummySerializeDeserialize(expr))
  1988. buildDatasetAssign(ctx, target, expr->queryChild(0)->queryChild(0));
  1989. else
  1990. buildAssignLinkedDataset(ctx, target, expr->queryChild(0));
  1991. return;
  1992. case no_select:
  1993. {
  1994. bool isNew;
  1995. IHqlExpression * ds = querySelectorDataset(expr, isNew);
  1996. if (!isNew || ds->isDatarow())
  1997. {
  1998. Owned<IReferenceSelector> selected = buildReference(ctx, expr);
  1999. selected->assignTo(ctx, target);
  2000. return;
  2001. }
  2002. break;
  2003. }
  2004. case no_typetransfer:
  2005. {
  2006. IHqlExpression * child = expr->queryChild(0);
  2007. if (expr->isDataset() && child->isDataset())
  2008. {
  2009. //Special case no-op type transfers and assignment from a call returning unknown-type dataset
  2010. if (!recordTypesMatch(expr, child) &&
  2011. !(child->getOperator() == no_externalcall && recordTypesMatch(child, queryNullRecord())))
  2012. {
  2013. CHqlBoundExpr bound;
  2014. buildDataset(ctx, child, bound, FormatNatural);
  2015. ITypeInfo * newType = cloneModifiers(bound.expr->queryType(), expr->queryType());
  2016. bound.expr.setown(createValue(no_typetransfer, newType, LINK(bound.expr)));
  2017. OwnedHqlExpr translated = bound.getTranslatedExpr();
  2018. buildDatasetAssign(ctx, target, translated);
  2019. }
  2020. else
  2021. buildDatasetAssign(ctx, target, child);
  2022. return;
  2023. }
  2024. break;
  2025. }
  2026. }
  2027. if (!canAssignInline(&ctx, expr) && (op != no_translated))
  2028. {
  2029. buildAssignChildDataset(ctx, target, expr);
  2030. return;
  2031. }
  2032. ITypeInfo * to = target.queryType();
  2033. ITypeInfo * exprType = expr->queryType();
  2034. bool targetOutOfLine = isArrayRowset(to);
  2035. switch (op)
  2036. {
  2037. case no_limit:
  2038. assertex(!expr->hasProperty(skipAtom) && !expr->hasProperty(onFailAtom));
  2039. //Do the limit check as a post test.
  2040. //It means we may read more records than we need to, but the code is inline, and the code is generally much better.
  2041. if (target.count)
  2042. {
  2043. buildDatasetAssign(ctx, target, expr->queryChild(0));
  2044. CHqlBoundExpr bound;
  2045. bound.setFromTarget(target);
  2046. doBuildCheckDatasetLimit(ctx, expr, bound);
  2047. return;
  2048. }
  2049. break;
  2050. case no_translated:
  2051. {
  2052. bool sourceOutOfLine = isArrayRowset(exprType);
  2053. if (sourceOutOfLine != targetOutOfLine)
  2054. {
  2055. OwnedITypeInfo serializedSourceType = getSerializedForm(exprType);
  2056. OwnedITypeInfo serializedTargetType = getSerializedForm(to);
  2057. if (queryUnqualifiedType(serializedSourceType) == queryUnqualifiedType(serializedTargetType))
  2058. {
  2059. if (targetOutOfLine)
  2060. {
  2061. buildAssignLinkedDataset(ctx, target, expr);
  2062. }
  2063. else
  2064. {
  2065. buildAssignSerializedDataset(ctx, target, expr);
  2066. }
  2067. return;
  2068. }
  2069. }
  2070. break;
  2071. }
  2072. }
  2073. if (recordTypesMatch(to, exprType))
  2074. {
  2075. switch (op)
  2076. {
  2077. case no_rows:
  2078. {
  2079. CHqlBoundExpr bound;
  2080. buildDataset(ctx, expr, bound, FormatLinkedDataset);
  2081. OwnedHqlExpr translated = bound.getTranslatedExpr();
  2082. buildDatasetAssign(ctx, target, translated);
  2083. return;
  2084. }
  2085. case no_select:
  2086. {
  2087. bool isNew;
  2088. IHqlExpression * ds = querySelectorDataset(expr, isNew);
  2089. if (isNew && !ds->isDatarow())
  2090. break;
  2091. }
  2092. //fall through
  2093. case no_translated:
  2094. case no_null:
  2095. case no_id2blob:
  2096. {
  2097. _ATOM func = NULL;
  2098. if (!isArrayRowset(to))
  2099. {
  2100. if (!isArrayRowset(exprType))
  2101. func = dataset2DatasetXAtom;
  2102. }
  2103. else if (hasLinkCountedModifier(to))
  2104. {
  2105. if (hasLinkCountedModifier(exprType))
  2106. {
  2107. CHqlBoundExpr bound;
  2108. buildDataset(ctx, expr, bound, FormatLinkedDataset);
  2109. assertex(bound.count && bound.expr);
  2110. if (hasWrapperModifier(to))
  2111. {
  2112. //assigns to count and rows members
  2113. StringBuffer s;
  2114. generateExprCpp(s, target.expr);
  2115. s.append(".set(");
  2116. generateExprCpp(s, bound.count);
  2117. s.append(",");
  2118. generateExprCpp(s, bound.expr);
  2119. s.append(");");
  2120. ctx.addQuoted(s);
  2121. }
  2122. else
  2123. {
  2124. ctx.addAssign(target.count, bound.count);
  2125. HqlExprArray args;
  2126. args.append(*LINK(bound.expr));
  2127. OwnedHqlExpr call = bindTranslatedFunctionCall(linkRowsetAtom, args);
  2128. ctx.addAssign(target.expr, call);
  2129. }
  2130. return;
  2131. }
  2132. }
  2133. if (func)
  2134. {
  2135. HqlExprArray args;
  2136. args.append(*LINK(expr));
  2137. OwnedHqlExpr call = bindFunctionCall(func, args);
  2138. buildExprAssign(ctx, target, call);
  2139. return;
  2140. }
  2141. }
  2142. }
  2143. }
  2144. IHqlExpression * record = ::queryRecord(to);
  2145. Owned<IHqlCppDatasetBuilder> builder;
  2146. if (targetOutOfLine)
  2147. {
  2148. if (target.queryType()->getTypeCode() == type_dictionary)
  2149. {
  2150. builder.setown(createLinkedDictionaryBuilder(record));
  2151. }
  2152. else
  2153. {
  2154. IHqlExpression * choosenLimit = NULL;
  2155. if ((op == no_choosen) && !isChooseNAllLimit(expr->queryChild(1)) && !queryRealChild(expr, 2))
  2156. {
  2157. choosenLimit = expr->queryChild(1);
  2158. expr = expr->queryChild(0);
  2159. }
  2160. builder.setown(createLinkedDatasetBuilder(record, choosenLimit));
  2161. }
  2162. }
  2163. else
  2164. builder.setown(createBlockedDatasetBuilder(record));
  2165. builder->buildDeclare(ctx);
  2166. buildDatasetAssign(ctx, builder, expr);
  2167. builder->buildFinish(ctx, target);
  2168. }
  2169. //---------------------------------------------------------------------------
  2170. void HqlCppTranslator::doBuildCheckDatasetLimit(BuildCtx & ctx, IHqlExpression * expr, const CHqlBoundExpr & bound)
  2171. {
  2172. IHqlExpression * record = expr->queryRecord();
  2173. IHqlExpression * limit = expr->queryChild(1);
  2174. OwnedHqlExpr test;
  2175. if (!bound.count && bound.length && isFixedRecordSize(record))
  2176. {
  2177. OwnedHqlExpr size = bound.length->queryValue() ? LINK(bound.length) : createTranslated(bound.length);
  2178. OwnedHqlExpr maxSize = createValue(no_mul, LINK(sizetType), ensureExprType(limit, sizetType), getSizetConstant(getFixedRecordSize(record)));
  2179. test.setown(createBoolExpr(no_gt, ensureExprType(size, sizetType), LINK(maxSize)));
  2180. }
  2181. else
  2182. {
  2183. OwnedHqlExpr count = getBoundCount(bound);
  2184. OwnedHqlExpr translatedCount = count->queryValue() ? LINK(count) : createTranslated(count);
  2185. test.setown(createBoolExpr(no_gt, ensureExprType(translatedCount, sizetType), ensureExprType(limit, sizetType)));
  2186. }
  2187. OwnedHqlExpr folded = foldHqlExpression(test);
  2188. LinkedHqlExpr fail = queryRealChild(expr, 2);
  2189. if (folded->queryValue())
  2190. {
  2191. if (!folded->queryValue()->getBoolValue())
  2192. return;
  2193. StringBuffer failMessageText;
  2194. if (fail)
  2195. {
  2196. OwnedHqlExpr failMessage = getFailMessage(fail, true);
  2197. if (failMessage && failMessage->queryValue())
  2198. failMessage->queryValue()->getStringValue(failMessageText);
  2199. }
  2200. if (failMessageText.length())
  2201. WARNING1(HQLWRN_LimitAlwaysExceededX, failMessageText.str());
  2202. else
  2203. WARNING(HQLWRN_LimitAlwaysExceeded);
  2204. }
  2205. if (!fail)
  2206. fail.setown(createFailAction("Limit exceeded", limit, NULL, queryCurrentActivityId(ctx)));
  2207. BuildCtx subctx(ctx);
  2208. buildFilter(subctx, folded);
  2209. buildStmt(subctx, fail);
  2210. }
  2211. void HqlCppTranslator::doBuildDatasetLimit(BuildCtx & ctx, IHqlExpression * expr, CHqlBoundExpr & tgt, ExpressionFormat format)
  2212. {
  2213. buildDataset(ctx, expr->queryChild(0), tgt, format);
  2214. doBuildCheckDatasetLimit(ctx, expr, tgt);
  2215. }
  2216. bool HqlCppTranslator::doBuildDatasetInlineTable(BuildCtx & ctx, IHqlExpression * expr, CHqlBoundExpr & tgt, ExpressionFormat format)
  2217. {
  2218. if (!options.generateStaticInlineTables)
  2219. return false;
  2220. IHqlExpression * transforms = expr->queryChild(0);
  2221. IHqlExpression * record = expr->queryRecord();
  2222. if (transforms->numChildren() == 0)
  2223. {
  2224. OwnedHqlExpr null = createDataset(no_null, LINK(record));
  2225. buildDataset(ctx, null, tgt, format);
  2226. return true;
  2227. }
  2228. BuildCtx declareCtx(*code, literalAtom);
  2229. //Remove unique id when checking for constant datasets already generated
  2230. OwnedHqlExpr exprNoUnique = removeProperty(expr, _uid_Atom);
  2231. if (declareCtx.getMatchExpr(exprNoUnique, tgt))
  2232. return true;
  2233. HqlExprArray rows;
  2234. unsigned maxRows = transforms->numChildren();
  2235. unsigned row;
  2236. for (row = 0; row < maxRows; row++)
  2237. {
  2238. OwnedHqlExpr constRow = createConstantRowExpr(transforms->queryChild(row));
  2239. if (!constRow || !canGenerateStringInline(constRow->queryType()->getSize()))
  2240. return false;
  2241. rows.append(*constRow.getClear());
  2242. }
  2243. HqlExprArray boundRows;
  2244. ForEachItemIn(i, rows)
  2245. {
  2246. CHqlBoundExpr bound;
  2247. buildConstRow(record, &rows.item(i), bound);
  2248. boundRows.append(*bound.expr.getClear());
  2249. }
  2250. Owned<ITypeInfo> rowType = makeConstantModifier(makeReferenceModifier(makeRowType(LINK(queryRecordType(expr->queryType())))));
  2251. OwnedHqlExpr values = createValue(no_list, makeSetType(LINK(rowType)), boundRows);
  2252. Owned<ITypeInfo> tableType = makeConstantModifier(makeArrayType(LINK(rowType), maxRows));
  2253. OwnedITypeInfo rowsType = makeOutOfLineModifier(makeTableType(LINK(rowType), NULL, NULL, NULL));
  2254. OwnedHqlExpr table = declareCtx.getTempDeclare(tableType, values);
  2255. if (options.spanMultipleCpp)
  2256. {
  2257. BuildCtx protoctx(*code, mainprototypesAtom);
  2258. protoctx.addDeclareExternal(table);
  2259. }
  2260. tgt.count.setown(getSizetConstant(maxRows));
  2261. tgt.expr.setown(createValue(no_typetransfer, LINK(rowsType), LINK(table)));
  2262. declareCtx.associateExpr(exprNoUnique, tgt);
  2263. return true;
  2264. }
  2265. //---------------------------------------------------------------------------
  2266. // Dataset creation via builder
  2267. void HqlCppTranslator::buildDatasetAssignTempTable(BuildCtx & ctx, IHqlCppDatasetBuilder * target, IHqlExpression * expr)
  2268. {
  2269. OwnedHqlExpr values = normalizeListCasts(expr->queryChild(0));
  2270. if (values->getOperator() == no_null)
  2271. return;
  2272. IHqlExpression * record = expr->queryChild(1);
  2273. OwnedHqlExpr rowsExpr;
  2274. if (values->queryType()->getTypeCode() == type_set)
  2275. {
  2276. if ((values->getOperator() == no_list) && !values->isConstant())
  2277. {
  2278. ForEachChild(i, values)
  2279. {
  2280. BuildCtx loopctx(ctx);
  2281. BoundRow * targetRow = target->buildCreateRow(loopctx);
  2282. OwnedHqlExpr targetField = createSelectExpr(LINK(targetRow->querySelector()), LINK(record->queryChild(0)));
  2283. buildAssign(loopctx, targetField, values->queryChild(i));
  2284. target->finishRow(loopctx, targetRow);
  2285. }
  2286. }
  2287. else
  2288. {
  2289. Owned<IHqlCppSetCursor> cursor = createSetSelector(ctx, values);
  2290. BuildCtx loopctx(ctx);
  2291. CHqlBoundExpr boundCurElement;
  2292. cursor->buildIterateLoop(loopctx, boundCurElement, false);
  2293. BoundRow * targetRow = target->buildCreateRow(loopctx);
  2294. OwnedHqlExpr targetField = createSelectExpr(LINK(targetRow->querySelector()), LINK(record->queryChild(0)));
  2295. OwnedHqlExpr value = boundCurElement.getTranslatedExpr();
  2296. buildAssign(loopctx, targetField, value);
  2297. target->finishRow(loopctx, targetRow);
  2298. }
  2299. }
  2300. else
  2301. {
  2302. BuildCtx subctx(ctx);
  2303. BoundRow * targetRow = target->buildCreateRow(subctx);
  2304. Owned<IReferenceSelector> targetRef = buildActiveRow(subctx, targetRow->querySelector());
  2305. buildRowAssign(subctx, targetRef, values);
  2306. target->finishRow(subctx, targetRow);
  2307. }
  2308. }
  2309. void HqlCppTranslator::buildDatasetAssignInlineTable(BuildCtx & ctx, IHqlCppDatasetBuilder * target, IHqlExpression * expr)
  2310. {
  2311. IHqlExpression * transforms = expr->queryChild(0);
  2312. if (transforms->numChildren() == 0)
  2313. return;
  2314. unsigned maxRows = transforms->numChildren();
  2315. unsigned row;
  2316. const bool copyConstantRows = true;//getFieldCount(expr->queryRecord()) > 2;
  2317. for (row = 0; row < maxRows; row++)
  2318. {
  2319. IHqlExpression * transform = transforms->queryChild(row);
  2320. OwnedHqlExpr rowValue = createRow(no_createrow, LINK(transform));
  2321. BuildCtx subctx(ctx);
  2322. CHqlBoundExpr bound;
  2323. //Work in progress. Check if there are several fields - otherwise not worth it.s
  2324. if (doBuildRowConstantTransform(transform, bound))
  2325. {
  2326. BoundRow * row = bindConstantRow(subctx, rowValue, bound);
  2327. if (target->buildLinkRow(subctx, row))
  2328. continue;
  2329. }
  2330. BoundRow * targetRow = target->buildCreateRow(subctx);
  2331. Owned<IReferenceSelector> targetRef = buildActiveRow(subctx, targetRow->querySelector());
  2332. buildRowAssign(subctx, targetRef, rowValue);
  2333. target->finishRow(subctx, targetRow);
  2334. }
  2335. }
  2336. void HqlCppTranslator::buildDatasetAssignDatasetFromTransform(BuildCtx & ctx, IHqlCppDatasetBuilder * target, IHqlExpression * expr)
  2337. {
  2338. assertex(expr->getOperator() == no_dataset_from_transform);
  2339. IHqlExpression * count = expr->queryChild(0);
  2340. if (isZero(count) || isNegative(count))
  2341. return;
  2342. IHqlExpression * transform = expr->queryChild(1);
  2343. IHqlExpression * counter = queryPropertyChild(expr, _countProject_Atom, 0);
  2344. // If it is at all possible that it could be negative, we must test before producing rows
  2345. CHqlBoundExpr boundCount;
  2346. buildSimpleExpr(ctx, count, boundCount);
  2347. BuildCtx subctx(ctx);
  2348. if (couldBeNegative(count))
  2349. {
  2350. OwnedHqlExpr zero = createConstant(0, count->getType());
  2351. OwnedHqlExpr ifTest = createValue(no_gt, makeBoolType(), boundCount.getTranslatedExpr(), LINK(zero));
  2352. buildFilter(subctx, ifTest);
  2353. }
  2354. // loopVar = 1;
  2355. OwnedHqlExpr loopVar = subctx.getTempDeclare(counterType, NULL);
  2356. OwnedHqlExpr one = getSizetConstant(1);
  2357. buildAssignToTemp(subctx, loopVar, one);
  2358. // for(; loopVar <= maxRows; loopVar++)
  2359. OwnedHqlExpr loopTest = createValue(no_le, makeBoolType(), LINK(loopVar), LINK(boundCount.expr));
  2360. OwnedHqlExpr inc = createValue(no_postinc, loopVar->getType(), LINK(loopVar));
  2361. subctx.addLoop(loopTest, inc, false);
  2362. if (counter)
  2363. subctx.associateExpr(counter, loopVar);
  2364. OwnedHqlExpr rowValue = createRow(no_createrow, LINK(transform));
  2365. BoundRow * targetRow = target->buildCreateRow(subctx);
  2366. Owned<IReferenceSelector> targetRef = buildActiveRow(subctx, targetRow->querySelector());
  2367. buildRowAssign(subctx, targetRef, rowValue);
  2368. target->finishRow(subctx, targetRow);
  2369. }
  2370. class InlineDatasetSkipCallback : public CInterface, implements IHqlCodeCallback
  2371. {
  2372. public:
  2373. IMPLEMENT_IINTERFACE
  2374. virtual void buildCode(HqlCppTranslator & translator, BuildCtx & ctx)
  2375. {
  2376. ctx.addContinue();
  2377. }
  2378. };
  2379. void HqlCppTranslator::buildDatasetAssignProject(BuildCtx & ctx, IHqlCppDatasetBuilder * target, IHqlExpression * expr)
  2380. {
  2381. BuildCtx iterctx(ctx);
  2382. IHqlExpression * ds = expr->queryChild(0);
  2383. IHqlExpression * counter = queryPropertyChild(expr, _countProject_Atom, 0);
  2384. OwnedHqlExpr counterVar;
  2385. if (counter)
  2386. {
  2387. counterVar.setown(iterctx.getTempDeclare(unsignedType, queryZero()));
  2388. }
  2389. bool containsSkip = transformContainsSkip(expr->queryChild(1));
  2390. BoundRow * sourceCursor = buildDatasetIterate(iterctx, ds, containsSkip);
  2391. if (counter)
  2392. {
  2393. iterctx.associateExpr(counter, counterVar);
  2394. OwnedHqlExpr inc = createValue(no_postinc, LINK(unsignedType), LINK(counterVar));
  2395. iterctx.addExpr(inc);
  2396. }
  2397. if (sourceCursor)
  2398. {
  2399. if (isNullProject(expr, false))
  2400. {
  2401. if (target->buildLinkRow(iterctx, sourceCursor))
  2402. return;
  2403. }
  2404. BoundRow * targetRow = target->buildCreateRow(iterctx);
  2405. HqlExprAssociation * skipAssociation = NULL;
  2406. if (containsSkip)
  2407. {
  2408. OwnedHqlExpr callback = createUnknown(no_unknown, makeVoidType(), NULL, new InlineDatasetSkipCallback);
  2409. skipAssociation = ctx.associateExpr(skipActionMarker, callback);
  2410. }
  2411. Owned<IReferenceSelector> targetRef = buildActiveRow(iterctx, targetRow->querySelector());
  2412. switch (expr->getOperator())
  2413. {
  2414. case no_hqlproject:
  2415. doBuildRowAssignProject(iterctx, targetRef, expr);
  2416. break;
  2417. case no_newuserdictionary:
  2418. case no_newusertable:
  2419. doBuildRowAssignUserTable(iterctx, targetRef, expr);
  2420. break;
  2421. }
  2422. ctx.removeAssociation(skipAssociation);
  2423. target->finishRow(iterctx, targetRow);
  2424. }
  2425. }
  2426. void HqlCppTranslator::buildDatasetAssignJoin(BuildCtx & ctx, IHqlCppDatasetBuilder * target, IHqlExpression * expr)
  2427. {
  2428. IHqlExpression * left = expr->queryChild(0);
  2429. IHqlExpression * right = expr->queryChild(1);
  2430. IHqlExpression * cond = expr->queryChild(2);
  2431. IHqlExpression * selSeq = querySelSeq(expr);
  2432. bool leftOuter = expr->hasProperty(leftonlyAtom) || expr->hasProperty(leftouterAtom);
  2433. CHqlBoundExpr nullRhs;
  2434. if (leftOuter)
  2435. buildDefaultRow(ctx, right, nullRhs);
  2436. BuildCtx leftIterCtx(ctx);
  2437. BoundRow * leftCursor = buildDatasetIterate(leftIterCtx, left, false);
  2438. bindTableCursor(leftIterCtx, left, leftCursor->queryBound(), no_left, selSeq);
  2439. OwnedHqlExpr matchedAnyVar;
  2440. if (leftOuter)
  2441. matchedAnyVar.setown(leftIterCtx.getTempDeclare(queryBoolType(), queryBoolExpr(false)));
  2442. BuildCtx rightIterCtx(leftIterCtx);
  2443. BoundRow * rightCursor = buildDatasetIterate(rightIterCtx, right, false);
  2444. bindTableCursor(rightIterCtx, right, rightCursor->queryBound(), no_right, selSeq);
  2445. OwnedHqlExpr cseCond = options.spotCSE ? spotScalarCSE(cond) : LINK(cond);
  2446. buildFilter(rightIterCtx, cseCond);
  2447. if (!expr->hasProperty(leftonlyAtom))
  2448. {
  2449. BoundRow * targetRow = target->buildCreateRow(rightIterCtx);
  2450. Owned<IReferenceSelector> targetRef = buildActiveRow(rightIterCtx, targetRow->querySelector());
  2451. OwnedHqlExpr rowValue = createRow(no_createrow, LINK(expr->queryChild(3)));
  2452. buildRowAssign(rightIterCtx, targetRef, rowValue);
  2453. target->finishRow(rightIterCtx, targetRow);
  2454. }
  2455. if (matchedAnyVar)
  2456. {
  2457. buildAssignToTemp(rightIterCtx, matchedAnyVar, queryBoolExpr(true));
  2458. OwnedHqlExpr test = getInverse(matchedAnyVar);
  2459. leftIterCtx.addFilter(test);
  2460. OwnedHqlExpr defaultRowPtr = getPointer(nullRhs.expr);
  2461. bindTableCursor(leftIterCtx, right, defaultRowPtr, no_right, selSeq);
  2462. BoundRow * targetRow = target->buildCreateRow(leftIterCtx);
  2463. Owned<IReferenceSelector> targetRef = buildActiveRow(leftIterCtx, targetRow->querySelector());
  2464. OwnedHqlExpr rowValue = createRow(no_createrow, LINK(expr->queryChild(3)));
  2465. buildRowAssign(leftIterCtx, targetRef, rowValue);
  2466. target->finishRow(leftIterCtx, targetRow);
  2467. }
  2468. }
  2469. void HqlCppTranslator::buildDatasetAssignAggregate(BuildCtx & ctx, IHqlCppDatasetBuilder * target, IHqlExpression * expr)
  2470. {
  2471. BuildCtx subctx(ctx);
  2472. subctx.addGroup();
  2473. BoundRow * targetRow = target->buildCreateRow(subctx);
  2474. Owned<IReferenceSelector> targetRef = buildActiveRow(subctx, targetRow->querySelector());
  2475. doBuildRowAssignAggregate(subctx, targetRef, expr);
  2476. target->finishRow(subctx, targetRow);
  2477. }
  2478. void HqlCppTranslator::buildDatasetAssignChoose(BuildCtx & ctx, IHqlCppDatasetBuilder * target, IHqlExpression * expr)
  2479. {
  2480. CHqlBoundExpr cond;
  2481. buildExpr(ctx, expr->queryChild(0), cond);
  2482. IHqlExpression * last = queryLastNonAttribute(expr);
  2483. BuildCtx subctx(ctx);
  2484. IHqlStmt * switchstmt = subctx.addSwitch(cond.expr);
  2485. ForEachChildFrom(i, expr, 1)
  2486. {
  2487. IHqlExpression * cur = expr->queryChild(i);
  2488. if (cur != last)
  2489. {
  2490. OwnedHqlExpr label = getSizetConstant(i);
  2491. subctx.addCase(switchstmt, label);
  2492. }
  2493. else
  2494. subctx.addDefault(switchstmt);
  2495. buildDatasetAssign(subctx, target, cur);
  2496. }
  2497. }
  2498. void HqlCppTranslator::buildDatasetAssign(BuildCtx & ctx, IHqlCppDatasetBuilder * target, IHqlExpression * _expr)
  2499. {
  2500. OwnedHqlExpr expr = forceInlineAssignDataset(ctx, _expr);
  2501. bool isRowAssign = false;
  2502. BuildCtx subctx(ctx);
  2503. switch (expr->getOperator())
  2504. {
  2505. case no_addfiles:
  2506. buildDatasetAssign(subctx, target, expr->queryChild(0));
  2507. buildDatasetAssign(subctx, target, expr->queryChild(1));
  2508. return;
  2509. case no_temptable:
  2510. buildDatasetAssignTempTable(subctx, target, expr);
  2511. //MORE: Create rows and assign each one in turn. Could possibly be done with a different dataset selector
  2512. return;
  2513. case no_inlinedictionary:
  2514. case no_inlinetable:
  2515. buildDatasetAssignInlineTable(subctx, target, expr);
  2516. return;
  2517. case no_dataset_from_transform:
  2518. buildDatasetAssignDatasetFromTransform(subctx, target, expr);
  2519. return;
  2520. case no_xmlproject:
  2521. buildDatasetAssignXmlProject(subctx, target, expr);
  2522. return;
  2523. case no_datasetfromrow:
  2524. {
  2525. isRowAssign = true;
  2526. expr.set(expr->queryChild(0));
  2527. break;
  2528. }
  2529. case no_if:
  2530. {
  2531. CHqlBoundExpr bound;
  2532. buildExpr(subctx, expr->queryChild(0), bound);
  2533. IHqlStmt * filter = subctx.addFilter(bound.expr);
  2534. buildDatasetAssign(subctx, target, expr->queryChild(1));
  2535. IHqlExpression * elseExpr = expr->queryChild(2);
  2536. if (elseExpr && elseExpr->getOperator() != no_null)
  2537. {
  2538. subctx.selectElse(filter);
  2539. buildDatasetAssign(subctx, target, elseExpr);
  2540. }
  2541. }
  2542. return;
  2543. case no_chooseds:
  2544. buildDatasetAssignChoose(subctx, target, expr);
  2545. return;
  2546. case no_null:
  2547. return;
  2548. case no_activetable:
  2549. case no_temprow:
  2550. case no_createrow:
  2551. case no_projectrow:
  2552. case no_typetransfer:
  2553. isRowAssign = true;
  2554. break;
  2555. case no_newaggregate:
  2556. buildDatasetAssignAggregate(subctx, target, expr);
  2557. return;
  2558. case no_hqlproject:
  2559. case no_newusertable:
  2560. case no_newuserdictionary:
  2561. buildDatasetAssignProject(subctx, target, expr);
  2562. return;
  2563. case no_compound_childread:
  2564. case no_compound_childnormalize:
  2565. case no_compound_childaggregate:
  2566. case no_compound_selectnew:
  2567. case no_compound_inline:
  2568. case no_distributed:
  2569. case no_preservemeta:
  2570. case no_sorted:
  2571. case no_nofold:
  2572. case no_nohoist:
  2573. case no_section:
  2574. case no_sectioninput:
  2575. buildDatasetAssign(subctx, target, expr->queryChild(0));
  2576. return;
  2577. case no_alias_scope:
  2578. // expandAliasScope(subctx, expr);
  2579. buildDatasetAssign(subctx, target, expr->queryChild(0));
  2580. return;
  2581. case no_filter:
  2582. {
  2583. //We want to evaluate invariant conditions outside of the loop, rather than inside the dataset assignment
  2584. //Currently the test is whether the expression is independent of any tables. Better would be to
  2585. //see if the test was dependent on any of the datasets introduced by expr->queryChild(0).
  2586. HqlExprArray conds;
  2587. unwindFilterConditions(conds, expr);
  2588. IHqlExpression * ds = expr->queryChild(0);
  2589. #if 0
  2590. HqlExprCopyArray selectors;
  2591. loop
  2592. {
  2593. selectors.append(*ds->queryNormalizedSelector());
  2594. IHqlExpression * root = queryRoot(expr);
  2595. if (!root || root->getOperator() != no_select)
  2596. break;
  2597. bool isNew;
  2598. ds = querySelectorDataset(root, isNew);
  2599. if (!isNew)
  2600. break;
  2601. }
  2602. #endif
  2603. unsigned max = conds.ordinality();
  2604. unsigned i = 0;
  2605. bool optimized = false;
  2606. while (i < max)
  2607. {
  2608. IHqlExpression & cur = conds.item(i);
  2609. #if 0
  2610. bool overlap = false;
  2611. ForEachItemIn(j, selectors)
  2612. {
  2613. if (containsSelector(&cur, &selectors.item(j)))
  2614. {
  2615. overlap = true;
  2616. break;
  2617. }
  2618. }
  2619. #else
  2620. bool overlap = containsSelector(&cur, ds->queryNormalizedSelector());
  2621. #endif
  2622. if (!overlap)
  2623. {
  2624. buildFilter(subctx, &cur);
  2625. conds.remove(i);
  2626. optimized = true;
  2627. max--;
  2628. }
  2629. else
  2630. i++;
  2631. }
  2632. if (max == 0)
  2633. {
  2634. buildDatasetAssign(subctx, target, ds);
  2635. return;
  2636. }
  2637. if (optimized)
  2638. {
  2639. conds.add(*LINK(ds), 0);
  2640. expr.setown(expr->clone(conds));
  2641. }
  2642. break;
  2643. }
  2644. case no_join:
  2645. buildDatasetAssignJoin(subctx, target, expr);
  2646. return;
  2647. }
  2648. ITypeInfo * type = expr->queryType();
  2649. if (type)
  2650. {
  2651. switch (type->getTypeCode())
  2652. {
  2653. case type_record: // e.g. dataset.recordfield
  2654. case type_row:
  2655. isRowAssign = true;
  2656. break;
  2657. }
  2658. }
  2659. if (isRowAssign)
  2660. {
  2661. bool done = false;
  2662. subctx.addGroup();
  2663. //Some code primarily here to improve the generated code for productions inside parse statements for text parsing.
  2664. //see if we can replace a memcpy of the child record with a link...
  2665. if (!target->isRestricted())
  2666. {
  2667. switch (expr->getOperator())
  2668. {
  2669. case no_matchattr:
  2670. case no_left:
  2671. case no_right:
  2672. {
  2673. //Only try for really simple cases...
  2674. Owned<IReferenceSelector> sourceRef = buildNewRow(subctx, expr);
  2675. Owned<BoundRow> sourceRow = sourceRef->getRow(subctx);
  2676. if (!target->buildLinkRow(subctx, sourceRow))
  2677. {
  2678. BoundRow * targetRow = target->buildCreateRow(subctx);
  2679. Owned<IReferenceSelector> targetRef = buildActiveRow(subctx, targetRow->querySelector());
  2680. buildRowAssign(subctx, targetRef, sourceRef);
  2681. target->finishRow(subctx, targetRow);
  2682. }
  2683. done = true;
  2684. break;
  2685. }
  2686. }
  2687. }
  2688. if (!done)
  2689. {
  2690. BoundRow * match = static_cast<BoundRow *>(ctx.queryAssociation(expr, AssocRow, NULL));
  2691. if (match && target->buildLinkRow(subctx, match))
  2692. done = true;
  2693. }
  2694. if (!done)
  2695. {
  2696. BoundRow * targetRow = target->buildCreateRow(subctx);
  2697. Owned<IReferenceSelector> targetRef = buildActiveRow(subctx, targetRow->querySelector());
  2698. buildRowAssign(subctx, targetRef, expr);
  2699. target->finishRow(subctx, targetRow);
  2700. }
  2701. }
  2702. else
  2703. {
  2704. if (!target->buildAppendRows(subctx, expr))
  2705. {
  2706. BoundRow * sourceRow = buildDatasetIterate(subctx, expr, false);
  2707. if (sourceRow)
  2708. {
  2709. if (!target->buildLinkRow(subctx, sourceRow))
  2710. {
  2711. BoundRow * targetRow = target->buildCreateRow(subctx);
  2712. Owned<IReferenceSelector> targetRef = buildActiveRow(subctx, targetRow->querySelector());
  2713. Owned<IReferenceSelector> sourceRef = buildActiveRow(subctx, sourceRow->querySelector());
  2714. buildRowAssign(subctx, targetRef, sourceRef);
  2715. target->finishRow(subctx, targetRow);
  2716. }
  2717. }
  2718. }
  2719. }
  2720. }
  2721. //---------------------------------------------------------------------------
  2722. // Dataset iteration
  2723. BoundRow * HqlCppTranslator::buildDatasetIterateSelectN(BuildCtx & ctx, IHqlExpression * expr, bool needToBreak)
  2724. {
  2725. OwnedHqlExpr counter = ctx.getTempDeclare(sizetType, NULL);
  2726. buildAssignToTemp(ctx, counter, expr->queryChild(1));
  2727. BoundRow * cursor = buildDatasetIterate(ctx, expr->queryChild(0), needToBreak);
  2728. if (cursor)
  2729. {
  2730. OwnedHqlExpr dec = createValue(no_predec, counter->getType(), LINK(counter));
  2731. OwnedHqlExpr test = createBoolExpr(no_eq, LINK(dec), getSizetConstant(0));
  2732. ctx.addFilter(test);
  2733. }
  2734. return cursor;
  2735. }
  2736. BoundRow * HqlCppTranslator::buildDatasetIterateChoosen(BuildCtx & ctx, IHqlExpression * expr, bool needToBreak)
  2737. {
  2738. OwnedHqlExpr counter = ctx.getTempDeclare(sizetType, queryZero());
  2739. CHqlBoundExpr boundLow, boundHigh;
  2740. OwnedHqlExpr foldedHigh = foldHqlExpression(expr->queryChild(1));
  2741. if (!isChooseNAllLimit(foldedHigh))
  2742. buildSimpleExpr(ctx, foldedHigh, boundHigh);
  2743. if (queryRealChild(expr, 2))
  2744. {
  2745. OwnedHqlExpr foldedLow = foldHqlExpression(expr->queryChild(2));
  2746. OwnedHqlExpr low = adjustValue(foldedLow, -1);
  2747. buildSimpleExpr(ctx, low, boundLow);
  2748. if (boundHigh.expr)
  2749. boundHigh.expr.setown(createValue(no_add, LINK(boundHigh.queryType()), LINK(boundHigh.expr), LINK(boundLow.expr)));
  2750. }
  2751. BoundRow * cursor = buildDatasetIterate(ctx, expr->queryChild(0), needToBreak);
  2752. if (cursor)
  2753. {
  2754. OwnedHqlExpr inc = createValue(no_postinc, counter->getType(), LINK(counter));
  2755. ctx.addExpr(inc);
  2756. OwnedHqlExpr cond;
  2757. if (boundLow.expr)
  2758. extendConditionOwn(cond, no_and, createBoolExpr(no_gt, LINK(counter), LINK(boundLow.expr)));
  2759. if (boundHigh.expr)
  2760. extendConditionOwn(cond, no_and, createBoolExpr(no_le, LINK(counter), LINK(boundHigh.expr)));
  2761. if (cond)
  2762. ctx.addFilter(cond);
  2763. }
  2764. return cursor;
  2765. }
  2766. BoundRow * HqlCppTranslator::buildDatasetIterateLimit(BuildCtx & ctx, IHqlExpression * expr, bool needToBreak)
  2767. {
  2768. OwnedHqlExpr counter = ctx.getTempDeclare(sizetType, queryZero());
  2769. CHqlBoundExpr boundHigh;
  2770. OwnedHqlExpr foldedHigh = foldHqlExpression(expr->queryChild(1));
  2771. buildSimpleExpr(ctx, foldedHigh, boundHigh);
  2772. BoundRow * cursor = buildDatasetIterate(ctx, expr->queryChild(0), needToBreak);
  2773. if (cursor)
  2774. {
  2775. OwnedHqlExpr inc = createValue(no_preinc, counter->getType(), LINK(counter));
  2776. OwnedHqlExpr cond = createBoolExpr(no_gt, LINK(inc), LINK(boundHigh.expr));
  2777. BuildCtx subctx(ctx);
  2778. subctx.addFilter(cond);
  2779. LinkedHqlExpr fail = expr->queryChild(2);
  2780. if (!fail || fail->isAttribute())
  2781. fail.setown(createFailAction("Limit exceeded", foldedHigh, NULL, queryCurrentActivityId(ctx)));
  2782. buildStmt(subctx, fail);
  2783. }
  2784. return cursor;
  2785. }
  2786. BoundRow * HqlCppTranslator::buildDatasetIterateProject(BuildCtx & ctx, IHqlExpression * expr, bool needToBreak)
  2787. {
  2788. IHqlExpression * dataset = expr->queryChild(0);
  2789. OwnedHqlExpr counterVar;
  2790. IHqlExpression * counter = queryPropertyChild(expr, _countProject_Atom, 0);
  2791. if (counter)
  2792. {
  2793. counterVar.setown(ctx.getTempDeclare(unsignedType, queryZero()));
  2794. }
  2795. bool containsSkip = transformContainsSkip(expr->queryChild(1));
  2796. if (containsSkip)
  2797. needToBreak = true;
  2798. buildDatasetIterate(ctx, dataset, needToBreak);
  2799. Owned<BoundRow> tempRow = declareTempAnonRow(ctx, ctx, expr);
  2800. if (counter)
  2801. {
  2802. ctx.associateExpr(counter, counterVar);
  2803. OwnedHqlExpr inc = createValue(no_postinc, LINK(unsignedType), LINK(counterVar));
  2804. ctx.addExpr(inc);
  2805. }
  2806. Owned<BoundRow> rowBuilder = createRowBuilder(ctx, tempRow);
  2807. OwnedHqlExpr leftSelect = createSelector(no_left, dataset, querySelSeq(expr));
  2808. OwnedHqlExpr transform = replaceSelector(expr->queryChild(1), leftSelect, dataset->queryNormalizedSelector());
  2809. HqlExprAssociation * skipAssociation = NULL;
  2810. if (containsSkip)
  2811. {
  2812. OwnedHqlExpr callback = createUnknown(no_unknown, makeVoidType(), NULL, new InlineDatasetSkipCallback);
  2813. skipAssociation = ctx.associateExpr(skipActionMarker, callback);
  2814. }
  2815. doTransform(ctx, transform, rowBuilder);
  2816. ctx.removeAssociation(skipAssociation); //remove it in case keeping hold of it causes issues.
  2817. finalizeTempRow(ctx, tempRow, rowBuilder);
  2818. return bindTableCursor(ctx, expr, tempRow->queryBound());
  2819. }
  2820. BoundRow * HqlCppTranslator::buildDatasetIterateUserTable(BuildCtx & ctx, IHqlExpression * expr, bool needToBreak)
  2821. {
  2822. IHqlExpression * dataset = expr->queryChild(0);
  2823. buildDatasetIterate(ctx, dataset, needToBreak);
  2824. Owned<BoundRow> tempRow = declareTempAnonRow(ctx, ctx, expr);
  2825. Owned<BoundRow> rowBuilder = createRowBuilder(ctx, tempRow);
  2826. doTransform(ctx, expr->queryChild(2), rowBuilder);
  2827. finalizeTempRow(ctx, tempRow, rowBuilder);
  2828. return bindTableCursor(ctx, expr, tempRow->queryBound());
  2829. }
  2830. BoundRow * HqlCppTranslator::buildDatasetIterateSpecialTempTable(BuildCtx & ctx, IHqlExpression * expr, bool needToBreak)
  2831. {
  2832. IHqlExpression * values = expr->queryChild(0);
  2833. bool requiresTempRow = false;
  2834. ITypeInfo * type = values->queryType()->queryChildType();
  2835. switch (type->getTypeCode())
  2836. {
  2837. case type_alien:
  2838. case type_bitfield:
  2839. requiresTempRow = true;
  2840. break;
  2841. default:
  2842. if (type->getSize() == UNKNOWN_LENGTH)
  2843. requiresTempRow = true;
  2844. break;
  2845. }
  2846. Owned<IHqlCppSetCursor> cursor = createSetSelector(ctx, values);
  2847. CHqlBoundExpr boundCurElement;
  2848. cursor->buildIterateLoop(ctx, boundCurElement, false);
  2849. OwnedHqlExpr address = getPointer(boundCurElement.expr);
  2850. if (requiresTempRow)
  2851. {
  2852. Owned<BoundRow> tempRow = declareTempAnonRow(ctx, ctx, expr);
  2853. Owned<BoundRow> rowBuilder = createRowBuilder(ctx, tempRow);
  2854. IHqlExpression * record = expr->queryRecord();
  2855. OwnedHqlExpr target = createSelectExpr(LINK(rowBuilder->querySelector()), LINK(record->queryChild(0)));
  2856. OwnedHqlExpr curValue = boundCurElement.getTranslatedExpr();
  2857. buildAssign(ctx, target, curValue);
  2858. finalizeTempRow(ctx, tempRow, rowBuilder);
  2859. return bindTableCursor(ctx, expr, tempRow->queryBound());
  2860. }
  2861. else
  2862. {
  2863. assertex(!boundCurElement.length && !boundCurElement.count);
  2864. address.setown(createValue(no_implicitcast, makeRowReferenceType(expr), LINK(address)));
  2865. return bindTableCursor(ctx, expr, address);
  2866. }
  2867. }
  2868. BoundRow * HqlCppTranslator::buildDatasetIterateStreamedCall(BuildCtx & ctx, IHqlExpression * expr, bool needToBreak)
  2869. {
  2870. CHqlBoundExpr bound;
  2871. doBuildExprCall(ctx, expr, bound);
  2872. ITypeInfo * exprType = expr->queryType();
  2873. Owned<ITypeInfo> wrappedType = makeWrapperModifier(LINK(exprType));
  2874. OwnedHqlExpr temp = ctx.getTempDeclare(wrappedType, bound.expr);
  2875. ctx.addLoop(NULL, NULL, false);
  2876. Owned<ITypeInfo> wrappedRowType = makeWrapperModifier(LINK(queryRowType(exprType)));
  2877. OwnedHqlExpr tempRow = ctx.getTempDeclare(wrappedRowType, NULL);
  2878. StringBuffer s;
  2879. generateExprCpp(s, tempRow).append(".setown(");
  2880. generateExprCpp(s, temp).append("->nextRow());");
  2881. ctx.addQuoted(s);
  2882. s.clear().append("if (!");generateExprCpp(s, tempRow).append(".getbytes()) break;");
  2883. ctx.addQuoted(s);
  2884. return bindTableCursor(ctx, expr, tempRow);
  2885. }
  2886. BoundRow * HqlCppTranslator::buildDatasetIterate(BuildCtx & ctx, IHqlExpression * expr, bool needToBreak)
  2887. {
  2888. if (!canProcessInline(&ctx, expr))
  2889. {
  2890. Owned<IHqlCppDatasetCursor> cursor = createDatasetSelector(ctx, expr);
  2891. return cursor->buildIterateLoop(ctx, needToBreak);
  2892. }
  2893. switch (expr->getOperator())
  2894. {
  2895. case no_dataset_alias:
  2896. if (!expr->hasProperty(_normalized_Atom))
  2897. {
  2898. OwnedHqlExpr uniqueChild = normalizeDatasetAlias(expr);
  2899. BoundRow * childCursor = buildDatasetIterate(ctx, uniqueChild, needToBreak);
  2900. return rebindTableCursor(ctx, expr, childCursor, no_none, NULL);
  2901. }
  2902. else
  2903. {
  2904. throwUnexpected();
  2905. //The following would only be triggered for a splitter (not yet generated), and that would require
  2906. //disambiguation when that was built.
  2907. BoundRow * childCursor = buildDatasetIterate(ctx, expr->queryChild(0), needToBreak);
  2908. return rebindTableCursor(ctx, expr, childCursor, no_none, NULL);
  2909. }
  2910. case no_null:
  2911. buildFilter(ctx, queryBoolExpr(false));
  2912. return NULL;
  2913. case no_filter:
  2914. {
  2915. IHqlExpression * dataset = expr->queryChild(0);
  2916. #ifdef _OPTIMZE_INLINE_FILTERS_
  2917. //Good code, but messes up accidental cse in some createSegmentMonitor calls.
  2918. HqlExprAttr invariant;
  2919. OwnedHqlExpr cond = extractFilterConditions(invariant, expr, dataset);
  2920. if (invariant)
  2921. buildFilter(ctx, invariant);
  2922. //MORE: if (canAssignInline(ctx, ds) && !canIterateInline(ctx, ds)) break;
  2923. BoundRow * cursor = buildDatasetIterate(ctx, dataset, needToBreak);
  2924. if (cond)
  2925. buildFilter(ctx, cond);
  2926. #else
  2927. //MORE: if (canAssignInline(ctx, ds) && !canIterateInline(ctx, ds)) break;
  2928. BoundRow * cursor = buildDatasetIterate(ctx, dataset, needToBreak);
  2929. unsigned max = expr->numChildren();
  2930. for (unsigned i=1; i < max; i++)
  2931. buildFilter(ctx, expr->queryChild(i));
  2932. #endif
  2933. return cursor;
  2934. }
  2935. case no_id2blob:
  2936. case no_select:
  2937. case no_translated:
  2938. {
  2939. Owned<IHqlCppDatasetCursor> cursor = createDatasetSelector(ctx, expr);
  2940. return cursor->buildIterateLoop(ctx, needToBreak);
  2941. }
  2942. case no_choosen:
  2943. return buildDatasetIterateChoosen(ctx, expr, needToBreak);
  2944. case no_limit:
  2945. return buildDatasetIterateLimit(ctx, expr, needToBreak);
  2946. case no_index:
  2947. case no_selectnth:
  2948. return buildDatasetIterateSelectN(ctx, expr, needToBreak);
  2949. case no_hqlproject:
  2950. return buildDatasetIterateProject(ctx, expr, needToBreak);
  2951. case no_newusertable:
  2952. return buildDatasetIterateUserTable(ctx, expr, needToBreak);
  2953. case no_compound_childread:
  2954. case no_compound_childnormalize:
  2955. case no_compound_childaggregate:
  2956. case no_compound_selectnew:
  2957. case no_compound_inline:
  2958. case no_distributed:
  2959. case no_preservemeta:
  2960. case no_sorted:
  2961. case no_nofold:
  2962. case no_nohoist:
  2963. return buildDatasetIterate(ctx, expr->queryChild(0), needToBreak);
  2964. case no_sectioninput:
  2965. case no_section:
  2966. {
  2967. BoundRow * row = buildDatasetIterate(ctx, expr->queryChild(0), needToBreak);
  2968. #ifdef _DEBUG
  2969. StringBuffer s;
  2970. if (expr->getOperator() == no_section)
  2971. s.append("//---- section ");
  2972. else
  2973. s.append("//---- end section ");
  2974. getStringValue(s, expr->queryChild(1), "<?>").append(" ");
  2975. getStringValue(s, expr->queryChild(2)).append("----");
  2976. ctx.addQuoted(s);
  2977. #endif
  2978. return row;
  2979. }
  2980. case no_alias_scope:
  2981. // expandAliasScope(ctx, expr);
  2982. return buildDatasetIterate(ctx, expr->queryChild(0), needToBreak);
  2983. case no_temptable:
  2984. {
  2985. IHqlExpression * values = expr->queryChild(0);
  2986. if (values->queryType()->getTypeCode() == type_set)
  2987. {
  2988. if (values->getOperator() == no_alias)
  2989. values = values->queryChild(0);
  2990. bool special = false;
  2991. switch (values->getOperator())
  2992. {
  2993. case no_getresult:
  2994. case no_null:
  2995. special = true;
  2996. break;
  2997. }
  2998. if (special)
  2999. return buildDatasetIterateSpecialTempTable(ctx, expr, needToBreak);
  3000. }
  3001. break;
  3002. }
  3003. case no_call:
  3004. case no_externalcall:
  3005. if (hasStreamedModifier(expr->queryType()))
  3006. return buildDatasetIterateStreamedCall(ctx, expr, needToBreak);
  3007. break;
  3008. #if 0
  3009. //Following should improve the code, but I'm not sure how to correctly convert a referenceSelector to a boundExpr (since it may be with an existing row)
  3010. case no_datasetfromrow:
  3011. if (!needToBreak)
  3012. {
  3013. BoundRow * row = buildNewRow(ctx, expr->queryChild(0));
  3014. bindTableCursor(ctx, expr, tempRow->queryBound(), no_none, NULL);
  3015. return row;
  3016. }
  3017. break;
  3018. #endif
  3019. }
  3020. Owned<IHqlCppDatasetCursor> cursor = createDatasetSelector(ctx, expr);
  3021. return cursor->buildIterateLoop(ctx, needToBreak);
  3022. }
  3023. //---------------------------------------------------------------------------
  3024. // Row Assignment
  3025. void HqlCppTranslator::buildCompoundAssign(BuildCtx & ctx, IHqlExpression * left, IReferenceSelector * leftSelector, IHqlExpression * rightScope, IHqlExpression * rightSelector)
  3026. {
  3027. switch (left->getOperator())
  3028. {
  3029. case no_ifblock:
  3030. {
  3031. BuildCtx subctx(ctx);
  3032. OwnedHqlExpr test = replaceSelector(left->queryChild(0), querySelfReference(), leftSelector->queryExpr());
  3033. buildFilter(subctx, test);
  3034. buildCompoundAssign(subctx, left->queryChild(1), leftSelector, rightScope, rightSelector);
  3035. //This calculates the size of the previous block. It means that subsequent uses of the
  3036. //offsets are cached - even if they are inside another ifblock().
  3037. CHqlBoundExpr bound;
  3038. IHqlExpression * mapexpr = createSelectExpr(LINK(leftSelector->queryExpr()), LINK(left));
  3039. OwnedHqlExpr size = createValue(no_sizeof, makeIntType(4,false), mapexpr);
  3040. buildCachedExpr(ctx, size, bound);
  3041. }
  3042. break;
  3043. case no_field:
  3044. {
  3045. Owned<IReferenceSelector> selectedLeft = leftSelector->select(ctx, left);
  3046. OwnedHqlExpr selectedRight;
  3047. IHqlExpression * leftRecord = ::queryRecord(leftSelector->queryType());
  3048. if (!rightScope || (leftRecord==rightScope))
  3049. {
  3050. selectedRight.setown(createSelectExpr(LINK(rightSelector), LINK(left)));
  3051. }
  3052. else
  3053. {
  3054. IHqlSimpleScope * scope = rightScope->querySimpleScope();
  3055. IHqlExpression * resolved = scope->lookupSymbol(left->queryName());
  3056. assertex(resolved);
  3057. selectedRight.setown(createSelectExpr(LINK(rightSelector), resolved));
  3058. }
  3059. if (left->queryType()->getTypeCode() == type_row)
  3060. buildCompoundAssign(ctx, left->queryRecord(), selectedLeft, selectedRight->queryRecord(), selectedRight);
  3061. else
  3062. selectedLeft->set(ctx, selectedRight);
  3063. break;
  3064. }
  3065. break;
  3066. case no_record:
  3067. {
  3068. ForEachChild(i, left)
  3069. buildCompoundAssign(ctx, left->queryChild(i), leftSelector, rightScope, rightSelector);
  3070. break;
  3071. }
  3072. case no_attr:
  3073. case no_attr_expr:
  3074. case no_attr_link:
  3075. break;
  3076. default:
  3077. UNIMPLEMENTED;
  3078. }
  3079. }
  3080. void HqlCppTranslator::buildCompoundAssign(BuildCtx & ctx, IHqlExpression * left, IHqlExpression * right)
  3081. {
  3082. //MORE: May need to resolve lhs and rhs in scope to find out the record type.
  3083. // buildCompoundAssign(ctx, column->queryRecord(), selector->queryExpr(), value->queryRecord(), value);
  3084. UNIMPLEMENTED;
  3085. }
  3086. void HqlCppTranslator::doBuildRowAssignAggregateClear(BuildCtx & ctx, IReferenceSelector * target, IHqlExpression * expr)
  3087. {
  3088. IHqlExpression * transform = expr->queryChild(2);
  3089. unsigned numAggregates = transform->numChildren();
  3090. unsigned idx;
  3091. OwnedHqlExpr self = getSelf(expr);
  3092. for (idx = 0; idx < numAggregates; idx++)
  3093. {
  3094. IHqlExpression * cur = transform->queryChild(idx);
  3095. Owned<IReferenceSelector> curTarget = createSelfSelect(ctx, target, cur->queryChild(0), self);
  3096. IHqlExpression * src = cur->queryChild(1);
  3097. switch (src->getOperator())
  3098. {
  3099. case no_countgroup:
  3100. case no_maxgroup:
  3101. case no_mingroup:
  3102. case no_sumgroup:
  3103. case no_existsgroup:
  3104. curTarget->buildClear(ctx, 0);
  3105. break;
  3106. default:
  3107. if (src->isConstant())
  3108. curTarget->set(ctx, src);
  3109. else
  3110. curTarget->buildClear(ctx, 0);
  3111. break;
  3112. }
  3113. }
  3114. }
  3115. void HqlCppTranslator::doBuildRowAssignAggregateNext(BuildCtx & ctx, IReferenceSelector * target, IHqlExpression * expr, bool isSingleExists, IHqlExpression * alreadyDoneExpr)
  3116. {
  3117. IHqlExpression * transform = expr->queryChild(2);
  3118. unsigned numAggregates = transform->numChildren();
  3119. OwnedHqlExpr self = getSelf(expr);
  3120. OwnedHqlExpr notAlreadyDone = alreadyDoneExpr ? getInverse(alreadyDoneExpr) : NULL;
  3121. bool isVariableOffset = false;
  3122. for (unsigned idx = 0; idx < numAggregates; idx++)
  3123. {
  3124. IHqlExpression * cur = transform->queryChild(idx);
  3125. if (cur->isAttribute())
  3126. continue;
  3127. IHqlExpression * targetSelect = cur->queryChild(0);
  3128. Owned<IReferenceSelector> curTarget = createSelfSelect(ctx, target, targetSelect, self);
  3129. IHqlExpression * src = cur->queryChild(1);
  3130. IHqlExpression * arg = src->queryChild(0);
  3131. IHqlExpression * cond = src->queryChild(1);
  3132. BuildCtx condctx(ctx);
  3133. node_operator srcOp = src->getOperator();
  3134. switch (srcOp)
  3135. {
  3136. case no_countgroup:
  3137. {
  3138. assertex(!(arg && isVariableOffset));
  3139. if (arg)
  3140. buildFilter(condctx, arg);
  3141. OwnedHqlExpr one = getSizetConstant(1);
  3142. if (isVariableOffset)
  3143. {
  3144. IHqlStmt * ifStmt = condctx.addFilter(notAlreadyDone);
  3145. curTarget->set(condctx, one);
  3146. condctx.selectElse(ifStmt);
  3147. }
  3148. buildIncrementAssign(condctx, curTarget, one);
  3149. }
  3150. break;
  3151. case no_sumgroup:
  3152. {
  3153. assertex(!(cond && isVariableOffset));
  3154. if (cond)
  3155. buildFilter(condctx, cond);
  3156. if (isVariableOffset)
  3157. {
  3158. IHqlStmt * ifStmt = condctx.addFilter(notAlreadyDone);
  3159. curTarget->set(condctx, arg);
  3160. condctx.selectElse(ifStmt);
  3161. }
  3162. buildIncrementAssign(condctx, curTarget, arg);
  3163. }
  3164. break;
  3165. case no_maxgroup:
  3166. case no_mingroup:
  3167. {
  3168. node_operator op = (srcOp == no_maxgroup) ? no_gt : no_lt;
  3169. assertex(!cond);
  3170. OwnedHqlExpr castArg = ensureExprType(arg, targetSelect->queryType()); // cast to correct type, assume it can fit in the target type.
  3171. OwnedHqlExpr temp = buildSimplifyExpr(condctx, castArg);
  3172. OwnedHqlExpr compare = createBoolExpr (op, LINK(temp), LINK(curTarget->queryExpr()));
  3173. if (notAlreadyDone)
  3174. compare.setown(createBoolExpr(no_or, LINK(notAlreadyDone), compare.getClear()));
  3175. buildFilter(condctx, compare);
  3176. curTarget->set(condctx, temp);
  3177. }
  3178. break;
  3179. case no_existsgroup:
  3180. assertex(!(arg && isVariableOffset));
  3181. if (arg)
  3182. buildFilter(condctx, arg);
  3183. curTarget->set(condctx, queryBoolExpr(true));
  3184. if (isSingleExists)
  3185. condctx.addBreak();
  3186. break;
  3187. default:
  3188. if (!src->isConstant() || isVariableOffset)
  3189. {
  3190. condctx.addFilter(notAlreadyDone);
  3191. curTarget->set(condctx, src);
  3192. }
  3193. break;
  3194. }
  3195. if (targetSelect->queryType()->getSize() == UNKNOWN_LENGTH)
  3196. isVariableOffset = true;
  3197. }
  3198. if (alreadyDoneExpr)
  3199. buildAssignToTemp(ctx, alreadyDoneExpr, queryBoolExpr(true));
  3200. }
  3201. void HqlCppTranslator::doBuildRowAssignAggregate(BuildCtx & ctx, IReferenceSelector * target, IHqlExpression * expr)
  3202. {
  3203. IHqlExpression * dataset = expr->queryChild(0);
  3204. IHqlExpression * transform = expr->queryChild(2);
  3205. unsigned numAggregates = transform->numChildren();
  3206. if (isKeyedCountAggregate(expr))
  3207. throwError(HQLERR_KeyedCountNonKeyable);
  3208. bool needGuard = false;
  3209. bool isSingleExists = true;
  3210. unsigned idx;
  3211. for (idx = 0; idx < numAggregates; idx++)
  3212. {
  3213. IHqlExpression * cur = transform->queryChild(idx);
  3214. if (cur->isAttribute())
  3215. continue;
  3216. IHqlExpression * tgt = cur->queryChild(0);
  3217. IHqlExpression * src = cur->queryChild(1);
  3218. switch (src->getOperator())
  3219. {
  3220. case no_countgroup:
  3221. case no_sumgroup:
  3222. isSingleExists = false;
  3223. break;
  3224. case no_existsgroup:
  3225. break;
  3226. case no_mingroup:
  3227. isSingleExists = false;
  3228. needGuard = true;
  3229. break;
  3230. case no_maxgroup:
  3231. isSingleExists = false;
  3232. if (!isNullValueMinimumValue(src->queryType()))
  3233. needGuard = true;
  3234. break;
  3235. default:
  3236. isSingleExists = false;
  3237. if (!src->isConstant())
  3238. needGuard = true;
  3239. break;
  3240. }
  3241. if ((tgt->queryType()->getSize() == UNKNOWN_LENGTH) && (idx+1 != numAggregates))
  3242. needGuard = true;
  3243. }
  3244. OwnedHqlExpr guard;
  3245. if (needGuard)
  3246. {
  3247. Owned<ITypeInfo> boolType = makeBoolType();
  3248. guard.setown(ctx.getTempDeclare(boolType, queryBoolExpr(false)));
  3249. }
  3250. doBuildRowAssignAggregateClear(ctx, target, expr);
  3251. BuildCtx condctx(ctx);
  3252. BoundRow * cursor = buildDatasetIterate(condctx, dataset, isSingleExists);
  3253. doBuildRowAssignAggregateNext(condctx, target, expr, isSingleExists, guard);
  3254. }
  3255. void HqlCppTranslator::doBuildRowAssignProject(BuildCtx & ctx, IReferenceSelector * target, IHqlExpression * expr)
  3256. {
  3257. IHqlExpression * dataset = expr->queryChild(0);
  3258. IHqlExpression * counter = queryPropertyChild(expr, _countProject_Atom, 0);
  3259. if (counter && !ctx.queryMatchExpr(counter))
  3260. throwError(HQLERR_CounterNotFound);
  3261. BuildCtx subctx(ctx);
  3262. assertex(target->isRoot());
  3263. IHqlExpression * selSeq = querySelSeq(expr);
  3264. OwnedHqlExpr leftSelect = createSelector(no_left, dataset, selSeq);
  3265. OwnedHqlExpr activeDataset = ensureActiveRow(dataset->queryNormalizedSelector());
  3266. OwnedHqlExpr transform = queryNewReplaceSelector(expr->queryChild(1), leftSelect, activeDataset);
  3267. Owned<BoundRow> selfCursor;
  3268. if (!transform)
  3269. {
  3270. subctx.addGroup();
  3271. selfCursor.set(bindSelectorAsSelf(subctx, target, expr));
  3272. //Mapping may potentially be ambiguous, so do things correctly (see hqlsource for details)
  3273. BoundRow * prevCursor = resolveSelectorDataset(subctx, dataset->queryNormalizedSelector());
  3274. transform.set(expr->queryChild(1));
  3275. bindTableCursor(subctx, dataset, prevCursor->queryBound(), no_left, selSeq);
  3276. }
  3277. else
  3278. {
  3279. //Not introducing any new left rows => no problem assigning to target selector
  3280. selfCursor.setown(target->getRow(subctx));
  3281. }
  3282. doTransform(subctx, transform, selfCursor);
  3283. }
  3284. void HqlCppTranslator::doBuildRowAssignCreateRow(BuildCtx & ctx, IReferenceSelector * target, IHqlExpression * expr)
  3285. {
  3286. IHqlExpression * transform = expr->queryChild(0);
  3287. if (transform->isConstant())
  3288. {
  3289. #ifdef USE_CONSTANT_ROW_FOR_ASSIGN
  3290. //Generally not worthwhile - unless maybe it has a large number of fields....
  3291. CHqlBoundExpr bound;
  3292. if (doBuildRowConstantTransform(transform, bound))
  3293. {
  3294. OwnedHqlExpr raw = bound.getTranslatedExpr();
  3295. buildRowAssign(ctx, target, raw);
  3296. return;
  3297. }
  3298. #endif
  3299. }
  3300. Owned<BoundRow> selfCursor = target->getRow(ctx);
  3301. doTransform(ctx, transform, selfCursor);
  3302. }
  3303. void HqlCppTranslator::doBuildRowAssignNullRow(BuildCtx & ctx, IReferenceSelector * target, IHqlExpression * expr)
  3304. {
  3305. target->buildClear(ctx, 0);
  3306. }
  3307. void HqlCppTranslator::doBuildRowAssignProjectRow(BuildCtx & ctx, IReferenceSelector * target, IHqlExpression * expr)
  3308. {
  3309. IHqlExpression * srcRow = expr->queryChild(0);
  3310. IHqlExpression * transform = expr->queryChild(1);
  3311. Owned<IReferenceSelector> source = buildNewRow(ctx, srcRow);
  3312. BuildCtx subctx(ctx);
  3313. OwnedHqlExpr leftSelect = createSelector(no_left, srcRow, querySelSeq(expr));
  3314. OwnedHqlExpr newTransform = replaceSelector(transform, leftSelect, srcRow);
  3315. Owned<BoundRow> selfCursor = target->getRow(subctx);
  3316. doTransform(subctx, newTransform, selfCursor);
  3317. }
  3318. void HqlCppTranslator::doBuildRowAssignSerializeRow(BuildCtx & ctx, IReferenceSelector * target, IHqlExpression * expr)
  3319. {
  3320. IHqlExpression * srcRow = expr->queryChild(0);
  3321. Owned<IReferenceSelector> source = buildNewRow(ctx, srcRow);
  3322. BuildCtx subctx(ctx);
  3323. Owned<BoundRow> leftCursor = source->getRow(subctx);
  3324. BoundRow * selfCursor = bindSelectorAsSelf(subctx, target, expr);
  3325. IHqlExpression * unserializedRecord = srcRow->queryRecord();
  3326. //If builder isn't provided then the target must be a fixed size record that doesn't require serialization
  3327. //Therefore this should never be called.
  3328. assertex(selfCursor->queryBuilder());
  3329. {
  3330. HqlExprArray args;
  3331. args.append(*createRowSerializer(ctx, unserializedRecord, serializerAtom));
  3332. args.append(*LINK(srcRow));
  3333. Owned<ITypeInfo> type = makeTransformType(expr->queryRecord()->getType());
  3334. OwnedHqlExpr call = bindFunctionCall(rtlSerializeToBuilderAtom, args, type);
  3335. doTransform(subctx, call, selfCursor);
  3336. //MORE: This doesn't associated the returned size with the target if assigned to a child field.
  3337. //very unusual code, so not too concerned.
  3338. }
  3339. subctx.removeAssociation(selfCursor);
  3340. }
  3341. void HqlCppTranslator::doBuildRowAssignUserTable(BuildCtx & ctx, IReferenceSelector * target, IHqlExpression * expr)
  3342. {
  3343. Owned<BoundRow> selfCursor = target->getRow(ctx);
  3344. doTransform(ctx, expr->queryChild(2), selfCursor);
  3345. }
  3346. void HqlCppTranslator::buildRowAssign(BuildCtx & ctx, IReferenceSelector * target, IHqlExpression * expr)
  3347. {
  3348. switch (expr->getOperator())
  3349. {
  3350. case no_temprow:
  3351. throwUnexpected();
  3352. case no_projectrow:
  3353. doBuildRowAssignProjectRow(ctx, target, expr);
  3354. return;
  3355. case no_createrow:
  3356. doBuildRowAssignCreateRow(ctx, target, expr);
  3357. return;
  3358. case no_null:
  3359. doBuildRowAssignNullRow(ctx, target, expr);
  3360. return;
  3361. case no_serialize:
  3362. if (isDummySerializeDeserialize(expr))
  3363. buildRowAssign(ctx, target, expr->queryChild(0)->queryChild(0));
  3364. else
  3365. doBuildRowAssignSerializeRow(ctx, target, expr);
  3366. return;
  3367. case no_if:
  3368. {
  3369. //Assigning a variable size record can mean that references to self need recalculating outside of the condition,
  3370. //producing poor code.
  3371. if (!isVariableSizeRecord(expr->queryRecord()))
  3372. {
  3373. OwnedHqlExpr foldedCond = foldHqlExpression(expr->queryChild(0));
  3374. BuildCtx condctx(ctx);
  3375. IHqlStmt * cond = buildFilterViaExpr(condctx, foldedCond);
  3376. buildRowAssign(condctx, target, expr->queryChild(1));
  3377. condctx.selectElse(cond);
  3378. buildRowAssign(condctx, target, expr->queryChild(2));
  3379. return;
  3380. }
  3381. }
  3382. break;
  3383. /*
  3384. case no_externalcall:
  3385. //MORE: Should assign directly to the target, but may not be very easy....
  3386. if (target->isBinary() && queryRecord(source->queryType()) == expr->queryRecord())
  3387. {
  3388. CHqlBoundExpr address;
  3389. target->buildAddress(ctx, address);
  3390. row.setown(createValue(no_typetransfer, makeReferenceModifier(LINK(selector->queryType())), LINK(address.expr)));
  3391. doBuildCall(ctx, &target, expr, NULL);
  3392. }
  3393. break;
  3394. */
  3395. case no_comma:
  3396. case no_compound:
  3397. buildStmt(ctx, expr->queryChild(0));
  3398. buildRowAssign(ctx, target, expr->queryChild(1));
  3399. return;
  3400. }
  3401. Owned<IReferenceSelector> src = buildNewRow(ctx, expr);
  3402. buildRowAssign(ctx, target, src);
  3403. }
  3404. void HqlCppTranslator::buildRowAssign(BuildCtx & ctx, IReferenceSelector * target, IReferenceSelector * source)
  3405. {
  3406. BuildCtx subctx(ctx);
  3407. IHqlExpression * sourceRecord = ::queryRecord(source->queryType());
  3408. IHqlExpression * targetRecord = ::queryRecord(target->queryType());
  3409. //if record structures are identical, then we must just be able to block copy the information across.
  3410. bool useMemcpy = (sourceRecord == targetRecord) && source->isBinary() && !source->isConditional() &&
  3411. !recordRequiresSerialization(sourceRecord);
  3412. if (useMemcpy)
  3413. {
  3414. if (source->queryRootRow()->isConditional())
  3415. {
  3416. IHqlStmt * ifStmt = subctx.addFilter(source->queryRootRow()->queryBound());
  3417. target->setRow(subctx, source);
  3418. subctx.selectElse(ifStmt);
  3419. target->buildClear(subctx, 0);
  3420. }
  3421. else
  3422. target->setRow(subctx, source);
  3423. }
  3424. else
  3425. buildCompoundAssign(subctx, targetRecord, target, sourceRecord, source->queryExpr());
  3426. }
  3427. //---------------------------------------------------------------------------
  3428. // Dataset selection
  3429. IReferenceSelector * HqlCppTranslator::doBuildRowSelectTop(BuildCtx & ctx, IHqlExpression * expr)
  3430. {
  3431. //create a temporary
  3432. Owned<ITypeInfo> rowType = makeReferenceModifier(expr->getType());
  3433. OwnedHqlExpr rowExpr = ctx.getTempDeclare(rowType, NULL);
  3434. Owned<BoundRow> row = createBoundRow(expr, rowExpr);
  3435. ctx.associate(*row); // associate here because it is compared inside the loop
  3436. CHqlBoundExpr boundCleared;
  3437. #ifdef CREATE_DEAULT_ROW_IF_NULL
  3438. buildDefaultRow(ctx, expr, boundCleared);
  3439. #else
  3440. buildNullRow(ctx, expr, boundCleared);
  3441. #endif
  3442. OwnedHqlExpr defaultRowPtr = getPointer(boundCleared.expr);
  3443. //Declare row for final level, iterate the appropriate number of times, and then assign and break.
  3444. BuildCtx initctx(ctx);
  3445. initctx.addGroup(); // add a group to allow a temporary to be declared later.
  3446. initctx.addAssign(rowExpr, defaultRowPtr);
  3447. HqlExprAssociation * savedMarker = ctx.associateExpr(queryConditionalRowMarker(), rowExpr);
  3448. BuildCtx iterctx(ctx);
  3449. IHqlExpression * sort = expr->queryChild(0);
  3450. IHqlExpression * dataset = sort->queryChild(0);
  3451. IHqlExpression * sortList = sort->queryChild(1);
  3452. BoundRow * chooseCursor = buildDatasetIterate(iterctx, dataset, false);
  3453. //if (!best) { best=row; } else { if (next < best) best = row; } Must short-circuit the test of best
  3454. OwnedHqlExpr testBest = createBoolExpr(no_eq, LINK(rowExpr), LINK(defaultRowPtr));
  3455. IHqlStmt * ifStmt = iterctx.addFilter(testBest);
  3456. {
  3457. OwnedHqlExpr source = ensureIteratedRowIsLive(initctx, ctx, iterctx, chooseCursor, dataset, expr);
  3458. OwnedHqlExpr castLeftRow = createValue(no_implicitcast, LINK(rowType), LINK(source));//chooseCursor->queryBound()));
  3459. iterctx.addAssign(rowExpr, castLeftRow);
  3460. }
  3461. iterctx.selectElse(ifStmt);
  3462. CHqlBoundExpr bound;
  3463. buildOrderedCompare(iterctx, dataset, sortList, bound, dataset, expr);
  3464. OwnedHqlExpr compare = createBoolExpr(no_lt, LINK(bound.expr), getZero());
  3465. iterctx.addFilter(compare);
  3466. {
  3467. OwnedHqlExpr source = ensureIteratedRowIsLive(initctx, ctx, iterctx, chooseCursor, dataset, expr);
  3468. OwnedHqlExpr castLeftRow = createValue(no_implicitcast, LINK(rowType), LINK(source));//chooseCursor->queryBound()));
  3469. iterctx.addAssign(rowExpr, castLeftRow);
  3470. }
  3471. ctx.removeAssociation(savedMarker);
  3472. //Set conditional later on, so test in main loop is explicit
  3473. #ifndef CREATE_DEAULT_ROW_IF_NULL
  3474. row->setConditional(true);
  3475. #endif
  3476. return createReferenceSelector(row);
  3477. }
  3478. BoundRow * HqlCppTranslator::buildOptimizeSelectFirstRow(BuildCtx & ctx, IHqlExpression * expr)
  3479. {
  3480. BoundRow * parentRow = NULL;
  3481. node_operator op = expr->getOperator();
  3482. switch (op)
  3483. {
  3484. case no_compound_childaggregate:
  3485. return buildOptimizeSelectFirstRow(ctx, expr->queryChild(0));
  3486. case no_hqlproject:
  3487. case no_newusertable:
  3488. {
  3489. parentRow = buildOptimizeSelectFirstRow(ctx, expr->queryChild(0));
  3490. if (!parentRow)
  3491. return NULL;
  3492. }
  3493. //fall through
  3494. case no_newaggregate:
  3495. {
  3496. Owned<BoundRow> tempRow = declareTempAnonRow(ctx, ctx, expr);
  3497. Owned<BoundRow> rowBuilder = createRowBuilder(ctx, tempRow);
  3498. Owned<IReferenceSelector> createdRef = createReferenceSelector(rowBuilder);
  3499. BuildCtx subctx(ctx);
  3500. subctx.addGroup();
  3501. if (parentRow)
  3502. {
  3503. if (op == no_hqlproject)
  3504. bindTableCursor(ctx, expr->queryChild(0), tempRow->queryBound(), no_left, querySelSeq(expr));
  3505. else
  3506. bindTableCursor(ctx, expr->queryChild(0), tempRow->queryBound(), no_none, NULL);
  3507. }
  3508. doBuildRowAssignAggregate(subctx, createdRef, expr);
  3509. finalizeTempRow(ctx, tempRow, rowBuilder);
  3510. return tempRow;
  3511. }
  3512. //
  3513. default:
  3514. return NULL;
  3515. }
  3516. }
  3517. void HqlCppTranslator::convertBoundDatasetToFirstRow(IHqlExpression * expr, CHqlBoundExpr & bound)
  3518. {
  3519. Owned<ITypeInfo> type = makeReferenceModifier(expr->getType());
  3520. ITypeInfo * boundType = bound.queryType();
  3521. if (isArrayRowset(boundType))
  3522. {
  3523. Linked<ITypeInfo> rowType = queryUnqualifiedType(queryRowType(boundType));
  3524. rowType.setown(makeReferenceModifier(LINK(rowType)));
  3525. if (hasLinkedRow(boundType))
  3526. type.setown(setLinkCountedAttr(type, true));
  3527. bound.expr.setown(createValue(no_deref, LINK(rowType), LINK(bound.expr)));
  3528. }
  3529. else if (bound.queryType()->isReference())
  3530. bound.expr.setown(createValue(no_typetransfer, LINK(type), bound.expr.getClear()));
  3531. else
  3532. bound.expr.setown(createValue(no_implicitcast, LINK(type), bound.expr.getClear()));
  3533. }
  3534. void HqlCppTranslator::convertBoundRowToDataset(BuildCtx & ctx, CHqlBoundExpr & bound, const BoundRow * row, ExpressionFormat preferredFormat)
  3535. {
  3536. IHqlExpression * boundRow = row->queryBound();
  3537. IHqlExpression * record = row->queryDataset()->queryRecord();
  3538. Owned<ITypeInfo> type = makeTableType(makeRowType(LINK(record->queryType())), NULL, NULL, NULL);
  3539. Owned<ITypeInfo> refType = makeReferenceModifier(LINK(type));
  3540. if (hasLinkCountedModifier(boundRow->queryType()) && (preferredFormat != FormatBlockedDataset))
  3541. {
  3542. OwnedHqlExpr curActivityId = getCurrentActivityId(ctx);
  3543. StringBuffer allocatorName;
  3544. ensureRowAllocator(allocatorName, ctx, record, curActivityId);
  3545. OwnedHqlExpr src = getPointer(boundRow);
  3546. //We can't just take the address of the link counted row, have to create a temporary dataset
  3547. //could be fixed once the link counting is tracked on rows and datasets
  3548. CHqlBoundTarget target;
  3549. createTempFor(ctx, type, target, typemod_none, FormatLinkedDataset);
  3550. StringBuffer s;
  3551. generateExprCpp(s, target.expr).append(".setRow(").append(allocatorName).append(",");
  3552. generateExprCpp(s, src).append(");");
  3553. ctx.addQuoted(s);
  3554. bound.setFromTarget(target);
  3555. }
  3556. else if (boundRow->queryType()->isReference())
  3557. bound.expr.setown(createValue(no_typetransfer, LINK(refType), LINK(boundRow)));
  3558. else
  3559. bound.expr.setown(createValue(no_implicitcast, LINK(refType), LINK(boundRow)));
  3560. bound.count.setown(getSizetConstant(1));
  3561. }
  3562. IHqlExpression * HqlCppTranslator::ensureIteratedRowIsLive(BuildCtx & initctx, BuildCtx & searchctx, BuildCtx & iterctx, BoundRow * row, IHqlExpression * dataset, IHqlExpression * rowExpr)
  3563. {
  3564. //The problem is that we are iterating through the rows in a dataset, and we need to access the "best" row outside of the loop
  3565. //However subsequent iterations of the loop might invalidate the current row, so we need to ensure the best row is retained.
  3566. //There should really be a better way, but it isn't too bad with link counted rows.
  3567. bool needToPreserve = false;
  3568. IHqlExpression * ds = dataset;
  3569. while (ds && !needToPreserve)
  3570. {
  3571. if (ds->isDatarow())
  3572. {
  3573. if (initctx.queryAssociation(ds, AssocRow, NULL))
  3574. break;
  3575. }
  3576. else
  3577. {
  3578. if (initctx.queryMatchExpr(ds))
  3579. break;
  3580. }
  3581. switch (ds->getOperator())
  3582. {
  3583. case no_filter:
  3584. case no_grouped:
  3585. case no_stepped:
  3586. case no_sorted:
  3587. case no_distributed:
  3588. case no_preservemeta:
  3589. case no_choosen:
  3590. case no_selectnth: // can occur as the lhs of no_select
  3591. case no_compound_childnormalize:
  3592. case no_compound_selectnew:
  3593. case no_compound_childread:
  3594. case no_dataset_alias:
  3595. ds = ds->queryChild(0);
  3596. break;
  3597. case no_select:
  3598. if (ds->isDataset())
  3599. {
  3600. if (ds->hasProperty(newAtom))
  3601. {
  3602. ds = ds->queryChild(0);
  3603. //don't walk complexds[1].childDataset<new> since the [1] will be generated as a temporary
  3604. // if (!ds->isDataset() && (ds->getOperator() != no_select))
  3605. // ds = NULL;
  3606. }
  3607. else
  3608. ds = NULL;
  3609. }
  3610. else
  3611. {
  3612. //ds.x.y, always walk to to ds.x
  3613. ds = ds->queryChild(0);
  3614. }
  3615. break;
  3616. case no_rows:
  3617. case no_call:
  3618. case no_externalcall:
  3619. case no_getresult:
  3620. case no_getgraphresult:
  3621. case no_alias:
  3622. ds = NULL;
  3623. break;
  3624. default:
  3625. needToPreserve = true;
  3626. break;
  3627. }
  3628. }
  3629. OwnedHqlExpr source = getPointer(row->queryBound());
  3630. if (!needToPreserve || initctx.hasAssociation(*row, false))
  3631. return source.getClear();
  3632. //If link counted, then declare a member that is a link counted row to ensure this row remains linked.
  3633. ITypeInfo * rowType = rowExpr->queryType();
  3634. if (hasLinkCountedModifier(row->queryBound()))
  3635. {
  3636. CHqlBoundTarget saveTarget;
  3637. createTempFor(initctx, rowType, saveTarget, typemod_wrapper, FormatLinkedDataset);
  3638. StringBuffer s;
  3639. generateExprCpp(s, saveTarget.expr);
  3640. s.append(".set(");
  3641. generateExprCpp(s, source).append(");");
  3642. iterctx.addQuoted(s);
  3643. return getPointer(saveTarget.expr);
  3644. }
  3645. BuildCtx childctx(iterctx);
  3646. childctx.addGroup();
  3647. Owned<BoundRow> tempRow = declareTempRow(childctx, childctx, rowExpr);
  3648. Owned<BoundRow> rowBuilder = createRowBuilder(childctx, tempRow);
  3649. OwnedHqlExpr sourceSelector = ensureActiveRow(row->querySelector());
  3650. buildAssign(childctx, rowBuilder->querySelector(), sourceSelector);
  3651. finalizeTempRow(childctx, tempRow, rowBuilder);
  3652. return getPointer(tempRow->queryBound());
  3653. }
  3654. IReferenceSelector * HqlCppTranslator::buildDatasetIndexViaIterator(BuildCtx & ctx, IHqlExpression * expr)
  3655. {
  3656. OwnedHqlExpr dataset = normalizeAnyDatasetAliases(querySkipDatasetMeta(expr->queryChild(0)));
  3657. IHqlExpression * index = expr->queryChild(1);
  3658. IHqlExpression * childDataset = dataset;
  3659. switch (dataset->getOperator())
  3660. {
  3661. case no_hqlproject:
  3662. //optimize selectnth(project(rows, t), n) to projectrow(selectnth(rows, n), t)
  3663. IHqlExpression * transform = dataset->queryChild(1);
  3664. if (!containsSkip(transform) && !expr->hasProperty(_countProject_Atom))
  3665. childDataset = dataset->queryChild(0);
  3666. break;
  3667. }
  3668. //create a temporary
  3669. //Following works because rows are created as temporaries in the class, so still in scope outside the iterate loop.
  3670. //Not a strictly correct assumption - e.g., if ever done in the main process() code.
  3671. Owned<ITypeInfo> rowType = makeReferenceModifier(expr->getType());
  3672. OwnedHqlExpr rowExpr = ctx.getTempDeclare(rowType, NULL);
  3673. Owned<BoundRow> row = createBoundRow(expr, rowExpr);
  3674. CHqlBoundExpr boundCleared;
  3675. #ifdef CREATE_DEAULT_ROW_IF_NULL
  3676. buildDefaultRow(ctx, expr, boundCleared);
  3677. #else
  3678. buildNullRow(ctx, expr, boundCleared);
  3679. row->setConditional(true);
  3680. #endif
  3681. OwnedHqlExpr defaultRowPtr = getPointer(boundCleared.expr);
  3682. //Declare row for final level, iterate the appropriate number of times, and then assign and break.
  3683. BuildCtx initctx(ctx);
  3684. initctx.addGroup(); // add a group to allow a temporary to be declared later.
  3685. initctx.addAssign(rowExpr, defaultRowPtr);
  3686. HqlExprAssociation * savedMarker = ctx.associateExpr(queryConditionalRowMarker(), rowExpr);
  3687. BuildCtx iterctx(ctx);
  3688. bool done = false;
  3689. if (childDataset->getOperator() == no_rows) //hasOutOfLineModifier(dataset->queryType()))
  3690. {
  3691. CHqlBoundExpr boundDs;
  3692. buildDataset(ctx, childDataset, boundDs, FormatNatural);
  3693. if (boundDs.count && isArrayRowset(boundDs.expr->queryType()))
  3694. {
  3695. OwnedHqlExpr castIndex = ensureExprType(index, unsignedType);
  3696. OwnedHqlExpr adjustedIndex = adjustValue(castIndex, -1);
  3697. CHqlBoundExpr boundIndex;
  3698. buildExpr(ctx, adjustedIndex, boundIndex);
  3699. OwnedHqlExpr count = getBoundCount(boundDs); // could be serialized, so can't assume bound.count is set
  3700. OwnedHqlExpr test = createValue(no_gt, makeBoolType(), LINK(count), LINK(boundIndex.expr));
  3701. iterctx.addFilter(test);
  3702. if (dataset != childDataset)
  3703. {
  3704. Owned<ITypeInfo> datasetRowType = makeRowReferenceType(boundDs);
  3705. OwnedHqlExpr selectedRow = createValue(no_index, LINK(datasetRowType), LINK(boundDs.expr), LINK(boundIndex.expr));
  3706. OwnedHqlExpr projected = createRow(no_projectrow, createTranslated(selectedRow), createComma(LINK(dataset->queryChild(1)), LINK(querySelSeq(dataset))));
  3707. Owned<IReferenceSelector> newRow = buildNewRow(iterctx, projected);
  3708. OwnedHqlExpr newPtr = getPointer(newRow->queryRootRow()->queryBound());
  3709. iterctx.addAssign(rowExpr, newPtr);
  3710. }
  3711. else
  3712. {
  3713. OwnedHqlExpr selectedRow = createValue(no_index, LINK(rowType), LINK(boundDs.expr), LINK(boundIndex.expr));
  3714. iterctx.addAssign(rowExpr, selectedRow);
  3715. }
  3716. done = true;
  3717. }
  3718. }
  3719. if (!done)
  3720. {
  3721. BoundRow * chooseCursor;
  3722. //If choosing the first element, then no need to maintain a counter...
  3723. IValue * indexValue = index->queryValue();
  3724. if (indexValue && (indexValue->getIntValue() == 1))
  3725. chooseCursor = buildDatasetIterate(iterctx, dataset, true);
  3726. else
  3727. chooseCursor = buildDatasetIterate(iterctx, expr, true);
  3728. if (chooseCursor)
  3729. {
  3730. OwnedHqlExpr source = getPointer(chooseCursor->queryBound());
  3731. //MORE: Need casts because cursor may be (probably are) constant, but temporary isn't
  3732. //should somehow fnd out by looking at the cursors.
  3733. OwnedHqlExpr castLeftRow = createValue(no_implicitcast, LINK(rowType), LINK(source));
  3734. iterctx.addAssign(rowExpr, castLeftRow);
  3735. iterctx.addBreak();
  3736. }
  3737. }
  3738. ctx.removeAssociation(savedMarker);
  3739. ctx.associate(*row);
  3740. return createReferenceSelector(row);
  3741. }
  3742. IReferenceSelector * HqlCppTranslator::buildDatasetIndex(BuildCtx & ctx, IHqlExpression * expr)
  3743. {
  3744. HqlExprAssociation * match = ctx.queryAssociation(expr, AssocRow, NULL);
  3745. if (match)
  3746. return createReferenceSelector(static_cast<BoundRow *>(match));
  3747. #if 0
  3748. //Causes some queries (ncf10) to run out of memory, so disable for the moment.
  3749. OwnedHqlExpr optimized = optimizeHqlExpression(expr, getOptimizeFlags()|HOOcompoundproject);
  3750. if (optimized != expr)
  3751. return buildNewRow(ctx, optimized);
  3752. #endif
  3753. OwnedHqlExpr dataset = normalizeAnyDatasetAliases(expr->queryChild(0));
  3754. //Special cases:
  3755. //i) selecting row [1] from something that only has a single row
  3756. //ii) selecting row [n] from something that can be iterated.
  3757. //iii) row[1] from something sorted that can be iterated.
  3758. BoundRow * row = NULL;
  3759. if (isTrivialSelectN(expr))
  3760. {
  3761. BoundRow * row = NULL;
  3762. #if 0
  3763. //This could be a good idea - but again it can mess up cse since dataset never gets bound.
  3764. //Could enable if I implement cse on datasets within transforms.
  3765. // if (canIterateInline(&ctx, dataset))
  3766. // row = buildOptimizeSelectFirstRow(ctx, dataset);
  3767. #endif
  3768. if (!row)
  3769. {
  3770. CHqlBoundExpr bound;
  3771. buildDataset(ctx, dataset, bound, FormatNatural);
  3772. convertBoundDatasetToFirstRow(expr, bound);
  3773. row = bindRow(ctx, expr, bound.expr);
  3774. }
  3775. return createReferenceSelector(row);
  3776. }
  3777. else if (canIterateInline(&ctx, dataset))
  3778. {
  3779. //MORE? Following doesn't work for implicit normalize which iterates multiple levels
  3780. bool specialCase = false;
  3781. dataset.set(querySkipDatasetMeta(dataset));
  3782. switch (dataset->getOperator())
  3783. {
  3784. case no_select:
  3785. specialCase = !isMultiLevelDatasetSelector(expr, false);
  3786. break;
  3787. case no_if:
  3788. case no_inlinedictionary:
  3789. case no_inlinetable:
  3790. case no_join:
  3791. //Always creates a temporary, so don't use an iterator
  3792. specialCase = true;
  3793. break;
  3794. default:
  3795. specialCase = alwaysEvaluatesToBound(dataset);
  3796. break;
  3797. }
  3798. if (!specialCase)
  3799. return buildDatasetIndexViaIterator(ctx, expr);
  3800. }
  3801. else if (isSelectSortedTop(expr) && canIterateInline(&ctx, dataset->queryChild(0)))
  3802. {
  3803. return doBuildRowSelectTop(ctx, expr);
  3804. }
  3805. //MORE: Is this a good idea???
  3806. else if (!canProcessInline(&ctx, expr))
  3807. {
  3808. CHqlBoundExpr bound;
  3809. OwnedHqlExpr dsExpr = expr->isDatarow() ? createDatasetFromRow(LINK(expr)) : LINK(expr);
  3810. buildDataset(ctx, dsExpr, bound, FormatNatural);
  3811. convertBoundDatasetToFirstRow(expr, bound);
  3812. row = bindRow(ctx, expr, bound.expr);
  3813. }
  3814. if (!row)
  3815. {
  3816. Owned<IHqlCppDatasetCursor> cursor = createDatasetSelector(ctx, dataset);
  3817. row = cursor->buildSelectNth(ctx, expr);
  3818. if (!row)
  3819. {
  3820. CHqlBoundExpr boundCleared;
  3821. buildDefaultRow(ctx, dataset, boundCleared);
  3822. OwnedHqlExpr defaultRowPtr = getPointer(boundCleared.expr);
  3823. row = bindRow(ctx, expr, defaultRowPtr);
  3824. }
  3825. }
  3826. return createReferenceSelector(row);
  3827. }
  3828. IReferenceSelector * HqlCppTranslator::buildDatasetSelectMap(BuildCtx & ctx, IHqlExpression * expr)
  3829. {
  3830. HqlExprAssociation * match = ctx.queryAssociation(expr, AssocRow, NULL);
  3831. if (match)
  3832. return createReferenceSelector(static_cast<BoundRow *>(match));
  3833. OwnedHqlExpr dataset = normalizeAnyDatasetAliases(expr->queryChild(0));
  3834. BoundRow * row = NULL;
  3835. assertex(canProcessInline(&ctx, expr));
  3836. if (!row)
  3837. {
  3838. Owned<IHqlCppDatasetCursor> cursor = createDatasetSelector(ctx, dataset);
  3839. row = cursor->buildSelectMap(ctx, expr);
  3840. if (!row)
  3841. {
  3842. CHqlBoundExpr boundCleared;
  3843. buildDefaultRow(ctx, dataset, boundCleared);
  3844. OwnedHqlExpr defaultRowPtr = getPointer(boundCleared.expr);
  3845. row = bindRow(ctx, expr, defaultRowPtr);
  3846. }
  3847. }
  3848. return createReferenceSelector(row);
  3849. }
  3850. //---------------------------------------------------------------------------
  3851. IHqlExpression * HqlCppTranslator::buildGetLocalResult(BuildCtx & ctx, IHqlExpression * expr, bool preferLinkedRows)
  3852. {
  3853. IHqlExpression * graphId = expr->queryChild(1);
  3854. IHqlExpression * resultNum = expr->queryChild(2);
  3855. Linked<ITypeInfo> exprType = queryUnqualifiedType(expr->queryType());
  3856. if (preferLinkedRows && !hasLinkCountedModifier(exprType))
  3857. exprType.setown(makeAttributeModifier(LINK(exprType), getLinkCountedAttr()));
  3858. if (expr->hasProperty(externalAtom))
  3859. {
  3860. IHqlExpression * resultInstance = queryPropertyChild(expr, externalAtom, 0);
  3861. HqlExprAssociation * matchedResults = ctx.queryMatchExpr(resultInstance);
  3862. if (!matchedResults)
  3863. {
  3864. //Very unusual - a result is required from a child query, but that child query is actually in
  3865. //the parent/grandparent. We need to evaluate in the parent instead.
  3866. CHqlBoundExpr match;
  3867. if (!buildExprInCorrectContext(ctx, expr, match, false))
  3868. throwUnexpected();
  3869. return match.getTranslatedExpr();
  3870. }
  3871. HqlExprArray args;
  3872. args.append(*LINK(matchedResults->queryExpr()));
  3873. args.append(*LINK(resultNum));
  3874. if (preferLinkedRows)
  3875. return bindFunctionCall(getChildQueryLinkedResultAtom, args, exprType);
  3876. return bindFunctionCall(getChildQueryResultAtom, args, exprType);
  3877. }
  3878. assertex(activeActivities.ordinality());
  3879. queryAddResultDependancy(activeActivities.tos(), graphId, resultNum);
  3880. SubGraphInfo * activeSubgraph = queryActiveSubGraph(ctx);
  3881. assertex(activeSubgraph && graphId == activeSubgraph->graphTag);
  3882. unique_id_t id = activeSubgraph->graphId;
  3883. EvalContext * instance = queryEvalContext(ctx);
  3884. OwnedHqlExpr retInstanceExpr;
  3885. if (instance && !insideOnCreate(ctx))
  3886. retInstanceExpr.setown(instance->createGraphLookup(id, false));
  3887. else
  3888. retInstanceExpr.setown(doCreateGraphLookup(ctx, ctx, id, "this", true));
  3889. HqlExprArray args;
  3890. args.append(*LINK(retInstanceExpr));
  3891. args.append(*LINK(resultNum));
  3892. if (preferLinkedRows)
  3893. return bindFunctionCall(getLocalLinkedResultAtom, args, exprType);
  3894. return bindFunctionCall(getLocalResultAtom, args, exprType);
  3895. }
  3896. void HqlCppTranslator::doBuildAssignGetGraphResult(BuildCtx & ctx, const CHqlBoundTarget & target, IHqlExpression * expr)
  3897. {
  3898. if (expr->hasProperty(_streaming_Atom))
  3899. throwError(HQLERR_LoopTooComplexForParallel);
  3900. bool isTargetLinkCounted = hasLinkCountedModifier(target.queryType());
  3901. if (expr->hasProperty(externalAtom))
  3902. {
  3903. OwnedHqlExpr call = buildGetLocalResult(ctx, expr, isTargetLinkCounted);
  3904. buildExprAssign(ctx, target, call);
  3905. return;
  3906. }
  3907. if (!isCurrentActiveGraph(ctx, expr->queryChild(1)))
  3908. {
  3909. CHqlBoundExpr match;
  3910. if (!buildExprInCorrectContext(ctx, expr, match, false))
  3911. throwError(HQLERR_GraphContextNotFound);
  3912. assign(ctx, target, match);
  3913. return;
  3914. }
  3915. OwnedHqlExpr call = buildGetLocalResult(ctx, expr, isTargetLinkCounted);
  3916. buildExprAssign(ctx, target, call);
  3917. }
  3918. void HqlCppTranslator::doBuildExprGetGraphResult(BuildCtx & ctx, IHqlExpression * expr, CHqlBoundExpr & tgt, ExpressionFormat format)
  3919. {
  3920. if (!expr->hasProperty(externalAtom))
  3921. {
  3922. doBuildAliasValue(ctx, expr, tgt);
  3923. return;
  3924. if (!isCurrentActiveGraph(ctx, expr->queryChild(1)))
  3925. {
  3926. if (!buildExprInCorrectContext(ctx, expr, tgt, false))
  3927. throwError(HQLERR_GraphContextNotFound);
  3928. return;
  3929. }
  3930. }
  3931. bool useLinkCounted = recordRequiresSerialization(expr->queryRecord()) || options.tempDatasetsUseLinkedRows;
  3932. OwnedHqlExpr call = buildGetLocalResult(ctx, expr, useLinkCounted);
  3933. switch (expr->queryType()->getTypeCode())
  3934. {
  3935. case type_row:
  3936. throwUnexpected();
  3937. case type_dictionary:
  3938. case type_table:
  3939. case type_groupedtable:
  3940. buildTempExpr(ctx, call, tgt);
  3941. break;
  3942. default:
  3943. buildExpr(ctx, call, tgt);
  3944. break;
  3945. }
  3946. }
  3947. ABoundActivity * HqlCppTranslator::doBuildActivityGetGraphResult(BuildCtx & ctx, IHqlExpression * expr)
  3948. {
  3949. IHqlExpression * graphId = expr->queryChild(1);
  3950. IHqlExpression * resultNum = expr->queryChild(2);
  3951. ThorActivityKind activityKind = (expr->hasProperty(_streaming_Atom) ? TAKlocalstreamread : TAKlocalresultread);
  3952. bool useImplementationClass = options.minimizeActivityClasses && (resultNum->getOperator() == no_constant);
  3953. Owned<ActivityInstance> instance = new ActivityInstance(*this, ctx, activityKind, expr, "LocalResultRead");
  3954. if (useImplementationClass)
  3955. instance->setImplementationClass(newLocalResultReadArgAtom);
  3956. if (expr->hasProperty(_loop_Atom))
  3957. {
  3958. if (isCurrentActiveGraph(ctx, graphId))
  3959. instance->graphLabel.set("Begin Loop");
  3960. else
  3961. instance->graphLabel.set("Outer Loop Input");
  3962. }
  3963. buildActivityFramework(instance);
  3964. buildInstancePrefix(instance);
  3965. if (!useImplementationClass)
  3966. doBuildUnsignedFunction(instance->classctx, "querySequence", resultNum);
  3967. else
  3968. instance->addConstructorParameter(resultNum);
  3969. addGraphIdAttribute(instance, ctx, graphId);
  3970. buildInstanceSuffix(instance);
  3971. queryAddResultDependancy(*instance->queryBoundActivity(), graphId, resultNum);
  3972. return instance->getBoundActivity();
  3973. }
  3974. ABoundActivity * HqlCppTranslator::doBuildActivitySetGraphResult(BuildCtx & ctx, IHqlExpression * expr, bool isRoot)
  3975. {
  3976. IHqlExpression * dataset = expr->queryChild(0);
  3977. IHqlExpression * graphId = expr->queryChild(1);
  3978. IHqlExpression * resultNum = expr->queryChild(2);
  3979. bool isSpill = expr->hasProperty(_spill_Atom);
  3980. ABoundActivity * parentActivity = activeActivities.ordinality() ? &activeActivities.tos() : NULL;
  3981. Owned<ABoundActivity> boundDataset = buildCachedActivity(ctx, dataset);
  3982. bool useImplementationClass = options.minimizeActivityClasses;
  3983. Owned<ActivityInstance> instance;
  3984. if (expr->getOperator() == no_spillgraphresult)
  3985. {
  3986. instance.setown(new ActivityInstance(*this, ctx, TAKlocalresultspill, expr, "LocalResultSpill"));
  3987. }
  3988. else
  3989. {
  3990. instance.setown(new ActivityInstance(*this, ctx, TAKlocalresultwrite, expr, "LocalResultWrite"));
  3991. }
  3992. if (useImplementationClass)
  3993. instance->setImplementationClass(newLocalResultSpillArgAtom);
  3994. if (expr->hasProperty(_loop_Atom))
  3995. instance->graphLabel.set("End Loop");
  3996. buildActivityFramework(instance, isRoot && !isSpill);
  3997. buildInstancePrefix(instance);
  3998. if (!useImplementationClass)
  3999. {
  4000. doBuildUnsignedFunction(instance->classctx, "querySequence", resultNum);
  4001. doBuildBoolFunction(instance->classctx, "usedOutsideGraph", !isSpill);
  4002. }
  4003. else
  4004. {
  4005. instance->addConstructorParameter(resultNum);
  4006. instance->addConstructorParameter(queryBoolExpr(!isSpill));
  4007. }
  4008. if (parentActivity && !insideRemoteGraph(ctx) && !isSpill)
  4009. {
  4010. const char * relationship;
  4011. if (expr->hasProperty(_loop_Atom))
  4012. relationship = "Body";
  4013. else if (insideRemoteGraph(ctx))
  4014. relationship = "Remote";
  4015. else
  4016. relationship = "Child";
  4017. addDependency(ctx, instance->queryBoundActivity(), parentActivity, childAtom, relationship);
  4018. }
  4019. instance->addAttributeBool("_isSpill", isSpill);
  4020. if (targetRoxie())
  4021. addGraphIdAttribute(instance, ctx, graphId);
  4022. buildInstanceSuffix(instance);
  4023. buildConnectInputOutput(ctx, instance, boundDataset, 0, 0);
  4024. associateRemoteResult(*instance, graphId, resultNum);
  4025. return instance->getBoundActivity();
  4026. }
  4027. ABoundActivity * HqlCppTranslator::doBuildActivityReturnResult(BuildCtx & ctx, IHqlExpression * expr, bool isRoot)
  4028. {
  4029. IHqlExpression * dataset = expr->queryChild(0);
  4030. ABoundActivity * parentActivity = activeActivities.ordinality() ? &activeActivities.tos() : NULL;
  4031. Owned<ABoundActivity> boundDataset = buildCachedActivity(ctx, dataset);
  4032. ThorActivityKind kind;
  4033. const char * helper;
  4034. if (dataset->isDataset())
  4035. {
  4036. kind = TAKdatasetresult;
  4037. helper = "DatasetResult";
  4038. }
  4039. else
  4040. {
  4041. kind = TAKrowresult;
  4042. helper = "RowResult";
  4043. }
  4044. Owned<ActivityInstance> instance = new ActivityInstance(*this, ctx, kind, expr, helper);
  4045. buildActivityFramework(instance, isRoot);
  4046. buildInstancePrefix(instance);
  4047. if (parentActivity && !insideRemoteGraph(ctx))
  4048. addDependency(ctx, instance->queryBoundActivity(), parentActivity, childAtom, "Child");
  4049. buildInstanceSuffix(instance);
  4050. buildConnectInputOutput(ctx, instance, boundDataset, 0, 0);
  4051. return instance->getBoundActivity();
  4052. }
  4053. void HqlCppTranslator::doBuildAssignLoopCounter(BuildCtx & ctx, const CHqlBoundTarget & target, IHqlExpression * expr)
  4054. {
  4055. if (!isCurrentActiveGraph(ctx, expr->queryChild(0)))
  4056. {
  4057. CHqlBoundExpr match;
  4058. if (!buildExprInCorrectContext(ctx, expr, match, false))
  4059. throwError(HQLERR_GraphContextNotFound);
  4060. assign(ctx, target, match);
  4061. return;
  4062. }
  4063. HqlExprArray args;
  4064. OwnedHqlExpr call = bindFunctionCall(getGraphLoopCounterAtom, args);
  4065. buildExprAssign(ctx, target, call);
  4066. }
  4067. //---------------------------------------------------------------------------
  4068. ABoundActivity * HqlCppTranslator::doBuildActivityGetGraphLoopResult(BuildCtx & ctx, IHqlExpression * expr)
  4069. {
  4070. IHqlExpression * graphId = expr->queryChild(1);
  4071. IHqlExpression * resultNum = expr->queryChild(2);
  4072. Owned<ActivityInstance> instance = new ActivityInstance(*this, ctx, TAKgraphloopresultread, expr, "GraphLoopResultRead");
  4073. buildActivityFramework(instance);
  4074. buildInstancePrefix(instance);
  4075. doBuildUnsignedFunction(instance->startctx, "querySequence", resultNum);
  4076. addGraphIdAttribute(instance, ctx, graphId);
  4077. buildInstanceSuffix(instance);
  4078. return instance->getBoundActivity();
  4079. }
  4080. ABoundActivity * HqlCppTranslator::doBuildActivitySetGraphLoopResult(BuildCtx & ctx, IHqlExpression * expr)
  4081. {
  4082. IHqlExpression * dataset = expr->queryChild(0);
  4083. IHqlExpression * graphId = expr->queryChild(1);
  4084. bool isSpill = expr->hasProperty(_spill_Atom);
  4085. ABoundActivity * parentActivity = activeActivities.ordinality() ? &activeActivities.tos() : NULL;
  4086. Owned<ABoundActivity> boundDataset = buildCachedActivity(ctx, dataset);
  4087. bool useImplementationClass = options.minimizeActivityClasses;
  4088. Owned<ActivityInstance> instance = new ActivityInstance(*this, ctx, TAKgraphloopresultwrite, expr, "GraphLoopResultWrite");
  4089. if (useImplementationClass)
  4090. instance->setImplementationClass(newGraphLoopResultWriteArgAtom);
  4091. buildActivityFramework(instance, true);
  4092. buildInstancePrefix(instance);
  4093. if (parentActivity && !insideRemoteGraph(ctx) && !isSpill)
  4094. addDependency(ctx, instance->queryBoundActivity(), parentActivity, childAtom, "Body");
  4095. if (targetRoxie())
  4096. addGraphIdAttribute(instance, ctx, graphId);
  4097. buildInstanceSuffix(instance);
  4098. buildConnectInputOutput(ctx, instance, boundDataset, 0, 0);
  4099. return instance->getBoundActivity();
  4100. }
  4101. //---------------------------------------------------------------------------
  4102. static IHqlExpression * queryResultExpr(IHqlExpression * expr)
  4103. {
  4104. loop
  4105. {
  4106. switch (expr->getOperator())
  4107. {
  4108. case no_compound:
  4109. expr = expr->queryChild(1);
  4110. break;
  4111. case no_subgraph:
  4112. expr = expr->queryChild(0);
  4113. break;
  4114. case no_returnresult:
  4115. return expr;
  4116. default:
  4117. throwUnexpectedOp(expr->getOperator());
  4118. }
  4119. }
  4120. }
  4121. ABoundActivity * HqlCppTranslator::doBuildActivityForceLocal(BuildCtx & ctx, IHqlExpression * expr)
  4122. {
  4123. IHqlExpression * child = expr->queryChild(0);
  4124. if (targetHThor() || (targetThor() && !insideChildQuery(ctx)))
  4125. {
  4126. WARNING(HQLWRN_LocalHasNoEffect);
  4127. return buildCachedActivity(ctx, child);
  4128. }
  4129. OwnedHqlExpr result = createValue(no_returnresult, makeVoidType(), LINK(child));
  4130. OwnedHqlExpr remote = resourceThorGraph(*this, result, RoxieCluster, 1, NULL);
  4131. unique_id_t localId = doBuildThorSubGraph(ctx, remote, SubGraphRemote);
  4132. Owned<ActivityInstance> instance = new ActivityInstance(*this, ctx, TAKlocalgraph, expr, "Null");
  4133. buildActivityFramework(instance);
  4134. buildInstancePrefix(instance);
  4135. instance->addAttributeInt("_subgraph", localId);
  4136. ActivityAssociation * match = static_cast<ActivityAssociation *>(ctx.queryAssociation(queryResultExpr(remote), AssocActivity, NULL));
  4137. assertex(match);
  4138. addDependency(ctx, match->activity, instance->queryBoundActivity(), childAtom);
  4139. buildInstanceSuffix(instance);
  4140. return instance->getBoundActivity();
  4141. }
  4142. void HqlCppTranslator::doBuildStmtApply(BuildCtx & ctx, IHqlExpression * expr)
  4143. {
  4144. IHqlExpression * dataset = expr->queryChild(0);
  4145. IHqlExpression * start = expr->queryProperty(beforeAtom);
  4146. IHqlExpression * end = expr->queryProperty(afterAtom);
  4147. if (start)
  4148. buildStmt(ctx, start->queryChild(0));
  4149. BuildCtx condctx(ctx);
  4150. BoundRow * cursor = buildDatasetIterate(condctx, dataset, false);
  4151. unsigned max = expr->numChildren();
  4152. for (unsigned i=1; i < max; i++)
  4153. {
  4154. IHqlExpression * cur = expr->queryChild(i);
  4155. if (!cur->isAttribute())
  4156. buildStmt(condctx, cur);
  4157. }
  4158. if (end)
  4159. buildStmt(ctx, end->queryChild(0));
  4160. }