hqlcppds.cpp 192 KB

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