executor.go 157 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098
  1. package executor
  2. import (
  3. "container/heap"
  4. "errors"
  5. "fmt"
  6. "math/rand"
  7. "sort"
  8. "strconv"
  9. "strings"
  10. "github.com/danfragoso/pizzasql-next/pkg/analyzer"
  11. "github.com/danfragoso/pizzasql-next/pkg/lexer"
  12. "github.com/danfragoso/pizzasql-next/pkg/parser"
  13. "github.com/danfragoso/pizzasql-next/pkg/storage"
  14. "github.com/danfragoso/pizzasql-next/pkg/version"
  15. )
  16. // Executor executes SQL statements.
  17. type Executor struct {
  18. schema *storage.SchemaManager
  19. table *storage.TableManager
  20. session *storage.Session
  21. analyzer *analyzer.Analyzer
  22. catalog *analyzer.Catalog
  23. // Last SchemaManager version reflected in catalog.
  24. catalogVersion uint64
  25. // Multi-database support
  26. attachedDatabases map[string]*DatabaseConnection // alias -> connection
  27. currentDatabase string // current database alias (default is "main")
  28. // Transaction state
  29. inTransaction bool
  30. savepoints []string // stack of savepoint names
  31. savepointPositions []int // session mutation-log positions for each savepoint
  32. // Subquery context for correlated subqueries
  33. outerRow storage.Row
  34. // Per-query cache for non-correlated IN (SELECT ...) subquery results.
  35. // Keyed by subquery AST pointer; valid for one top-level Execute call.
  36. subqueryCache map[*parser.SelectStmt]*Result
  37. // Per-query cache for decorrelated scalar aggregate subqueries.
  38. // Keyed by subquery AST pointer; valid for one top-level Execute call.
  39. correlatedAggCache map[*parser.SelectStmt]*correlatedAggCache
  40. // In-memory view registry: view name (lowercase) → SELECT AST.
  41. views map[string]*parser.SelectStmt
  42. // Materialized common table expressions for the current query, keyed by
  43. // lowercased CTE name. Recursive CTEs populate this during fixpoint
  44. // iteration so their own legs can read the working set.
  45. cteTables map[string]*cteTable
  46. // Session-local SQLite compatibility state, tracked per connection so
  47. // last_insert_rowid()/changes()/total_changes() reflect this session only.
  48. lastInsertRowID int64 // rowid of the most recent successful INSERT
  49. changes int64 // rows changed by the most recent INSERT/UPDATE/DELETE
  50. totalChanges int64 // rows changed since this connection opened (monotonic)
  51. }
  52. type correlatedAggCache struct {
  53. values map[string]interface{}
  54. defaultValue interface{}
  55. }
  56. type correlatedAggSpec struct {
  57. innerKey parser.Expr
  58. outerKey *parser.ColumnRef
  59. aggExpr parser.Expr
  60. }
  61. // DatabaseConnection represents an attached database.
  62. type DatabaseConnection struct {
  63. Alias string
  64. Path string // Database path or identifier
  65. Schema *storage.SchemaManager
  66. Table *storage.TableManager
  67. }
  68. // New creates a new executor.
  69. func New(schema *storage.SchemaManager, table *storage.TableManager) *Executor {
  70. catalog := analyzer.NewCatalog()
  71. executor := &Executor{
  72. schema: schema,
  73. table: table,
  74. session: storage.NewSession(schema, table),
  75. analyzer: analyzer.New(catalog),
  76. catalog: catalog,
  77. attachedDatabases: make(map[string]*DatabaseConnection),
  78. currentDatabase: "main",
  79. views: make(map[string]*parser.SelectStmt),
  80. }
  81. // Register the main database
  82. executor.attachedDatabases["main"] = &DatabaseConnection{
  83. Alias: "main",
  84. Path: schema.GetDatabaseName(),
  85. Schema: schema,
  86. Table: table,
  87. }
  88. return executor
  89. }
  90. // NewSessionExecutor returns a new executor sharing the same schema and table
  91. // managers but with fresh per-executor state (transaction, subquery caches,
  92. // views). It is used to isolate concurrent requests that must not share mutable
  93. // executor state.
  94. func (e *Executor) NewSessionExecutor() *Executor {
  95. exec := New(e.schema, e.table)
  96. exec.SyncCatalog()
  97. return exec
  98. }
  99. // SyncCatalog synchronizes the analyzer catalog with the storage schema.
  100. func (e *Executor) SyncCatalog() error {
  101. tables, err := e.schema.ListTables()
  102. if err != nil {
  103. return err
  104. }
  105. storageTables := make(map[string]struct{}, len(tables))
  106. for _, tableName := range tables {
  107. storageTables[strings.ToUpper(tableName)] = struct{}{}
  108. schema, err := e.schema.GetSchema(tableName)
  109. if err != nil {
  110. continue
  111. }
  112. // Drop table from catalog if it exists, then recreate with updated schema
  113. e.catalog.DropTable(tableName)
  114. e.catalog.CreateTable(schema.ToAnalyzerTableInfo())
  115. }
  116. for _, table := range e.catalog.GetTables() {
  117. if table.IsView {
  118. continue
  119. }
  120. if _, exists := storageTables[strings.ToUpper(table.Name)]; !exists {
  121. e.catalog.DropTable(table.Name)
  122. }
  123. }
  124. e.catalogVersion = e.schema.Version()
  125. return nil
  126. }
  127. // Execute executes a SQL statement.
  128. func (e *Executor) Execute(stmt parser.Statement) (*Result, error) {
  129. if _, beginning := stmt.(*parser.BeginStmt); !beginning && !e.inTransaction {
  130. e.schema.LockStatement()
  131. defer e.schema.UnlockStatement()
  132. }
  133. e.subqueryCache = make(map[*parser.SelectStmt]*Result)
  134. e.correlatedAggCache = make(map[*parser.SelectStmt]*correlatedAggCache)
  135. defer func() {
  136. e.subqueryCache = nil
  137. e.correlatedAggCache = nil
  138. }()
  139. // PRAGMA doesn't need analysis. SQLite catalog-introspection pragmas
  140. // (index_list, index_info, table_xinfo) are answered by the catalog agent
  141. // first; everything else falls through to the built-in handler.
  142. if pragma, ok := stmt.(*parser.PragmaStmt); ok {
  143. if res, handled, err := e.sqliteCatalogPragma(pragma); handled {
  144. return res, err
  145. }
  146. return e.executePragma(pragma)
  147. }
  148. // EXPLAIN doesn't need analysis
  149. if explain, ok := stmt.(*parser.ExplainStmt); ok {
  150. return e.executeExplain(explain)
  151. }
  152. // Transaction statements don't need analysis
  153. switch s := stmt.(type) {
  154. case *parser.BeginStmt:
  155. return e.executeBegin(s)
  156. case *parser.CommitStmt:
  157. return e.executeCommit(s)
  158. case *parser.RollbackStmt:
  159. return e.executeRollback(s)
  160. case *parser.SavepointStmt:
  161. return e.executeSavepoint(s)
  162. case *parser.ReleaseStmt:
  163. return e.executeRelease(s)
  164. case *parser.CreateIndexStmt:
  165. return e.executeCreateIndex(s)
  166. case *parser.DropIndexStmt:
  167. return e.executeDropIndex(s)
  168. case *parser.CreateViewStmt:
  169. return e.executeCreateView(s)
  170. case *parser.DropViewStmt:
  171. return e.executeDropView(s)
  172. case *parser.AttachStmt:
  173. return e.executeAttach(s)
  174. case *parser.DetachStmt:
  175. return e.executeDetach(s)
  176. }
  177. // SQLite catalog/metadata dispatch. SELECTs against sqlite_master /
  178. // sqlite_schema must be answered before the analyzer, which would otherwise
  179. // reject those virtual tables as unknown. Falls through when unhandled.
  180. if sel, ok := stmt.(*parser.SelectStmt); ok {
  181. if res, handled, err := e.sqliteCatalogSelect(sel); handled {
  182. return res, err
  183. }
  184. }
  185. // Analyze first. If the cached analyzer catalog is stale because schema was
  186. // changed through another executor/API path, resync from storage and retry
  187. // once before returning table/column-not-found errors.
  188. if err := e.analyzeWithCatalogRetry(stmt); err != nil {
  189. return nil, err
  190. }
  191. switch s := stmt.(type) {
  192. case *parser.SelectStmt:
  193. return e.executeSelect(s)
  194. case *parser.InsertStmt:
  195. return e.executeInsert(s)
  196. case *parser.UpdateStmt:
  197. return e.executeUpdate(s)
  198. case *parser.DeleteStmt:
  199. return e.executeDelete(s)
  200. case *parser.CreateTableStmt:
  201. return e.executeCreateTable(s)
  202. case *parser.DropTableStmt:
  203. return e.executeDropTable(s)
  204. case *parser.CreateIndexStmt:
  205. return e.executeCreateIndex(s)
  206. case *parser.DropIndexStmt:
  207. return e.executeDropIndex(s)
  208. case *parser.AlterTableStmt:
  209. return e.executeAlterTable(s)
  210. default:
  211. return nil, fmt.Errorf("unsupported statement type: %T", stmt)
  212. }
  213. }
  214. func (e *Executor) analyzeWithCatalogRetry(stmt parser.Statement) error {
  215. if e.catalogVersion != e.schema.Version() {
  216. if err := e.SyncCatalog(); err != nil {
  217. return err
  218. }
  219. }
  220. a := analyzer.New(e.catalog)
  221. err := a.Analyze(stmt)
  222. if err == nil {
  223. return nil
  224. }
  225. if !isCatalogMiss(err) {
  226. return err
  227. }
  228. if syncErr := e.SyncCatalog(); syncErr != nil {
  229. return err
  230. }
  231. a = analyzer.New(e.catalog)
  232. return a.Analyze(stmt)
  233. }
  234. func isCatalogMiss(err error) bool {
  235. var analysisErr *analyzer.AnalysisError
  236. if !errors.As(err, &analysisErr) {
  237. return false
  238. }
  239. return analysisErr.Type == analyzer.ErrTableNotFound ||
  240. analysisErr.Type == analyzer.ErrColumnNotFound
  241. }
  242. // isCountStarSingleTable reports whether the statement is the safe COUNT(*)
  243. // shape eligible for the metadata fast path: a single-table SELECT with exactly
  244. // one COUNT(*) column and no filters, grouping, DISTINCT, JOIN, subquery, or
  245. // LIMIT/OFFSET. Anything else returns false so unsupported shapes use the
  246. // normal scan path.
  247. func isCountStarSingleTable(stmt *parser.SelectStmt) bool {
  248. if stmt.Compound != nil || stmt.Distinct {
  249. return false
  250. }
  251. if stmt.Where != nil || stmt.Having != nil {
  252. return false
  253. }
  254. if len(stmt.GroupBy) > 0 || len(stmt.OrderBy) > 0 {
  255. return false
  256. }
  257. if stmt.Limit != nil || stmt.Offset != nil {
  258. return false
  259. }
  260. if len(stmt.From) != 1 {
  261. return false
  262. }
  263. ref := stmt.From[0]
  264. if ref.Subquery != nil || ref.Join != nil {
  265. return false
  266. }
  267. if len(stmt.Columns) != 1 || stmt.Columns[0].Star {
  268. return false
  269. }
  270. fn, ok := stmt.Columns[0].Expr.(*parser.FunctionCall)
  271. if !ok {
  272. return false
  273. }
  274. if !strings.EqualFold(fn.Name, "count") || !fn.Star || len(fn.Args) > 0 {
  275. return false
  276. }
  277. return true
  278. }
  279. // executeSelect executes a SELECT statement (or compound SELECT).
  280. func (e *Executor) executeSelect(stmt *parser.SelectStmt) (*Result, error) {
  281. if len(stmt.With) > 0 {
  282. return e.executeWith(stmt)
  283. }
  284. if stmt.Compound != nil {
  285. return e.executeCompound(stmt.Compound)
  286. }
  287. if len(stmt.From) == 0 {
  288. // SELECT without FROM (e.g., SELECT 1+1)
  289. return e.executeSelectExpr(stmt)
  290. }
  291. // Check if FROM clause is a subquery (derived table)
  292. if stmt.From[0].Subquery != nil {
  293. return e.executeSelectFromSubquery(stmt)
  294. }
  295. tableName := stmt.From[0].Name
  296. // A materialized CTE is served from memory, not from storage.
  297. if cte, ok := e.cteTableFor(tableName); ok {
  298. return e.executeSelectOnMaterialized(stmt, cte.columns, cteRowsToValues(cte))
  299. }
  300. // Transparently expand view references as derived-table subqueries.
  301. if viewDef, ok := e.views[strings.ToLower(tableName)]; ok {
  302. alias := stmt.From[0].Alias
  303. if alias == "" {
  304. alias = tableName
  305. }
  306. modifiedStmt := *stmt
  307. modifiedFrom := make([]parser.TableRef, len(stmt.From))
  308. copy(modifiedFrom, stmt.From)
  309. modifiedFrom[0] = parser.TableRef{Subquery: viewDef, Alias: alias}
  310. modifiedStmt.From = modifiedFrom
  311. return e.executeSelectFromSubquery(&modifiedStmt)
  312. }
  313. schema, err := e.schema.GetSchema(tableName)
  314. if err != nil {
  315. return nil, err
  316. }
  317. // COUNT(*) fast path: exact metadata-based count for the safe single-table
  318. // shape with no filters/grouping/distinct/join. Any unsupported shape falls
  319. // through to the normal scan path.
  320. if isCountStarSingleTable(stmt) {
  321. count, err := e.session.CountFast(tableName)
  322. if err != nil {
  323. return nil, err
  324. }
  325. result := NewResult("SELECT")
  326. if stmt.Columns[0].Alias != "" {
  327. result.AddColumn(stmt.Columns[0].Alias)
  328. } else {
  329. result.AddColumn("column1")
  330. }
  331. result.AddRow(int64(count))
  332. return result, nil
  333. }
  334. // Multi-table FROM (comma-separated implicit cross join): collect and cross join all tables,
  335. // then apply WHERE after. Don't push WHERE down here — conditions reference multiple tables.
  336. isMultiTable := len(stmt.From) > 1 && stmt.From[0].Join == nil
  337. // Optimize constant WHERE clauses
  338. var constantWhereResult *bool
  339. if stmt.Where != nil && !isMultiTable {
  340. // Check if WHERE clause is a constant expression (doesn't reference any columns)
  341. refs := collectColumnRefs(stmt.Where)
  342. if len(refs) == 0 {
  343. // Evaluate the constant expression
  344. val, err := e.evalExpr(stmt.Where, nil)
  345. if err == nil {
  346. result := toBool(val)
  347. constantWhereResult = &result
  348. }
  349. }
  350. }
  351. // If WHERE is constant false, check if we have aggregates first
  352. if constantWhereResult != nil && !*constantWhereResult {
  353. // If query has GROUP BY, return empty result (no groups match)
  354. // If query has aggregates but no GROUP BY, evaluate them on empty row set
  355. if e.hasAggregates(stmt.Columns) {
  356. if len(stmt.GroupBy) > 0 {
  357. // GROUP BY with no matching rows: return empty result (no groups)
  358. // Fall through to the non-aggregate case below
  359. } else {
  360. // Aggregate without GROUP BY: return single row with aggregate results on empty set
  361. return e.executeAggregateSelect(stmt, []storage.Row{}, schema)
  362. }
  363. }
  364. // Non-aggregate query with WHERE false: return empty result
  365. result := NewResult("SELECT")
  366. for i, col := range stmt.Columns {
  367. if col.Alias != "" {
  368. result.AddColumn(col.Alias)
  369. } else if ref, ok := col.Expr.(*parser.ColumnRef); ok {
  370. result.AddColumn(ref.Column)
  371. } else if col.Star {
  372. for _, c := range schema.Columns {
  373. result.AddColumn(c.Name)
  374. }
  375. } else {
  376. result.AddColumn(fmt.Sprintf("column%d", i+1))
  377. }
  378. }
  379. return result, nil
  380. }
  381. // Try to use index for WHERE clause (single-table only)
  382. var rows []storage.Row
  383. usedIndex := false
  384. // If WHERE is constant true, skip it during table scan
  385. effectiveWhere := stmt.Where
  386. if constantWhereResult != nil && *constantWhereResult {
  387. effectiveWhere = nil
  388. }
  389. // Pre-evaluate non-correlated subqueries in the WHERE before the scan takes
  390. // the session lock.
  391. e.primeSubqueries(effectiveWhere)
  392. // A WHERE that contains a subquery is evaluated after the scan. Evaluating a
  393. // correlated subquery inside the locked scan re-enters the session lock and
  394. // deadlocks, so only a subquery-free predicate is pushed into the scan.
  395. whereHasSubquery := false
  396. for _, ref := range collectColumnRefs(effectiveWhere) {
  397. if ref == "__subquery__" {
  398. whereHasSubquery = true
  399. break
  400. }
  401. }
  402. appliedInScan := effectiveWhere != nil && !whereHasSubquery && stmt.From[0].Alias == "" && !isMultiTable && stmt.From[0].Join == nil
  403. if effectiveWhere != nil && !isMultiTable {
  404. // Check if we can use an index
  405. colName, colValue, isEquality := e.extractIndexableCondition(stmt.Where)
  406. if isEquality {
  407. if strings.EqualFold(schema.PrimaryKey, colName) {
  408. row, getErr := e.session.GetByPK(tableName, fmt.Sprintf("%v", colValue))
  409. if getErr == nil {
  410. normalizeRowBySchema(row, schema)
  411. rows = []storage.Row{row}
  412. usedIndex = true
  413. } else if getErr == storage.ErrKeyNotFound {
  414. rows = []storage.Row{}
  415. usedIndex = true
  416. } else {
  417. return nil, getErr
  418. }
  419. } else {
  420. // Look for an index on this column
  421. indexes, _ := e.schema.ListTableIndexes(tableName)
  422. for _, idx := range indexes {
  423. if len(idx.Columns) == 1 && strings.EqualFold(idx.Columns[0].Name, colName) {
  424. rows, err = e.session.SelectByIndex(tableName, idx.Name, colValue)
  425. if err == nil {
  426. usedIndex = true
  427. for i := range rows {
  428. normalizeRowBySchema(rows[i], schema)
  429. }
  430. }
  431. break
  432. }
  433. }
  434. }
  435. }
  436. }
  437. // Fall back to full table scan if no index used
  438. if !usedIndex {
  439. var filterErr error
  440. var filter func(storage.Row) bool
  441. if appliedInScan {
  442. filter = func(row storage.Row) bool {
  443. val, ferr := e.evalExpr(effectiveWhere, row)
  444. if ferr != nil {
  445. filterErr = ferr
  446. return false
  447. }
  448. return toBool(val)
  449. }
  450. }
  451. rows, err = e.session.Select(tableName, filter)
  452. if filterErr != nil {
  453. return nil, filterErr
  454. }
  455. for _, row := range rows {
  456. normalizeRowBySchema(row, schema)
  457. }
  458. }
  459. if err != nil {
  460. return nil, err
  461. }
  462. // Add table alias to rows if there's an explicit alias
  463. if stmt.From[0].Alias != "" {
  464. for i := range rows {
  465. rows[i] = e.addTableAlias(rows[i], stmt.From[0].Alias)
  466. }
  467. } else if isMultiTable {
  468. // For multi-table cross joins without alias, prefix columns with table name
  469. // so WHERE can distinguish t3.a3 from t7.a7.
  470. for i := range rows {
  471. rows[i] = e.addTableAlias(rows[i], tableName)
  472. }
  473. }
  474. // Apply WHERE for a single-table scan that did not push the predicate into
  475. // the locked scan (aliased tables, or a WHERE containing a subquery). A
  476. // query with a JOIN defers WHERE until after the join, because the WHERE may
  477. // reference columns from the joined table.
  478. if effectiveWhere != nil && !isMultiTable && stmt.From[0].Join == nil && !appliedInScan {
  479. var filterErr error
  480. var filtered []storage.Row
  481. for _, row := range rows {
  482. val, ferr := e.evalExpr(effectiveWhere, row)
  483. if ferr != nil {
  484. filterErr = ferr
  485. break
  486. }
  487. if toBool(val) {
  488. filtered = append(filtered, row)
  489. }
  490. }
  491. if filterErr != nil {
  492. return nil, filterErr
  493. }
  494. rows = filtered
  495. }
  496. // Handle explicit JOINs from the first FROM entry (single-table+JOIN path)
  497. if !isMultiTable && len(stmt.From) > 0 && stmt.From[0].Join != nil {
  498. rows, err = e.executeJoins(stmt.From[0], rows)
  499. if err != nil {
  500. return nil, err
  501. }
  502. // Cross-join with any remaining comma-separated FROM entries (mixed JOIN+comma syntax)
  503. for _, tref := range stmt.From[1:] {
  504. rightRows, rerr := e.session.Select(tref.Name, nil)
  505. if rerr != nil {
  506. return nil, rerr
  507. }
  508. rightAlias := tref.Alias
  509. if rightAlias == "" {
  510. rightAlias = tref.Name
  511. }
  512. for i := range rightRows {
  513. rightRows[i] = e.addTableAlias(rightRows[i], rightAlias)
  514. }
  515. var joined []storage.Row
  516. for _, l := range rows {
  517. for _, r := range rightRows {
  518. m := make(storage.Row, len(l)+len(r))
  519. for k, v := range l {
  520. m[k] = v
  521. }
  522. for k, v := range r {
  523. m[k] = v
  524. }
  525. joined = append(joined, m)
  526. }
  527. }
  528. rows = joined
  529. // Handle JOINs within this tref too
  530. if tref.Join != nil {
  531. rows, err = e.executeJoins(tref, rows)
  532. if err != nil {
  533. return nil, err
  534. }
  535. }
  536. }
  537. // Apply WHERE after all joins. It is intentionally deferred past the base
  538. // scan for JOIN queries because the WHERE may reference joined-table columns.
  539. if effectiveWhere != nil {
  540. var filtered []storage.Row
  541. for _, row := range rows {
  542. val, _ := e.evalExpr(effectiveWhere, row)
  543. if toBool(val) {
  544. filtered = append(filtered, row)
  545. }
  546. }
  547. rows = filtered
  548. }
  549. }
  550. // Handle implicit cross joins (comma-separated FROM tables)
  551. if isMultiTable {
  552. // Build the column-set for each table so we can push WHERE conditions down.
  553. type tableInfo struct {
  554. alias string
  555. name string
  556. colsSet map[string]bool // lower-case column names for this table
  557. }
  558. allTableInfos := make([]tableInfo, len(stmt.From))
  559. for i, tref := range stmt.From {
  560. alias := tref.Alias
  561. if alias == "" {
  562. alias = tref.Name
  563. }
  564. sch, _ := e.schema.GetSchema(tref.Name)
  565. cols := map[string]bool{}
  566. if sch != nil {
  567. for _, c := range sch.Columns {
  568. cols[strings.ToLower(c.Name)] = true
  569. }
  570. }
  571. allTableInfos[i] = tableInfo{alias: alias, name: tref.Name, colsSet: cols}
  572. }
  573. // Split WHERE into AND-clauses and determine which tables each clause touches.
  574. var andClauses []parser.Expr
  575. if stmt.Where != nil {
  576. andClauses = splitANDClauses(stmt.Where)
  577. }
  578. // For each table, collect conditions that reference only its own columns.
  579. tableFilters := make([][]parser.Expr, len(stmt.From))
  580. var crossFilters []parser.Expr
  581. for _, clause := range andClauses {
  582. refs := collectColumnRefs(clause)
  583. ownerIdx := -1
  584. cross := false
  585. for _, ref := range refs {
  586. colLower := strings.ToLower(ref)
  587. found := -1
  588. for i, ti := range allTableInfos {
  589. if ti.colsSet[colLower] {
  590. if found == -1 {
  591. found = i
  592. } else if found != i {
  593. cross = true
  594. break
  595. }
  596. }
  597. }
  598. if cross {
  599. break
  600. }
  601. if found != -1 {
  602. if ownerIdx == -1 {
  603. ownerIdx = found
  604. } else if ownerIdx != found {
  605. cross = true
  606. break
  607. }
  608. }
  609. }
  610. if cross || ownerIdx == -1 {
  611. crossFilters = append(crossFilters, clause)
  612. } else {
  613. tableFilters[ownerIdx] = append(tableFilters[ownerIdx], clause)
  614. }
  615. }
  616. // Build cross-condition adjacency: for each cross filter, record which table indices it touches.
  617. type crossEdge struct{ a, b int }
  618. var crossEdges []crossEdge
  619. for _, clause := range crossFilters {
  620. refs := collectColumnRefs(clause)
  621. touched := map[int]bool{}
  622. for _, ref := range refs {
  623. cl := strings.ToLower(ref)
  624. for j, ti := range allTableInfos {
  625. if ti.colsSet[cl] {
  626. touched[j] = true
  627. }
  628. }
  629. }
  630. idxs := make([]int, 0, len(touched))
  631. for j := range touched {
  632. idxs = append(idxs, j)
  633. }
  634. if len(idxs) == 2 {
  635. crossEdges = append(crossEdges, crossEdge{idxs[0], idxs[1]})
  636. }
  637. }
  638. // Build alias-to-index map for applyWhenSeen.
  639. aliasToIdx := make(map[string]int, len(allTableInfos))
  640. for i, ti := range allTableInfos {
  641. aliasToIdx[strings.ToLower(ti.alias)] = i
  642. }
  643. // Helper: find cross-conditions applicable when seenSet is fully present.
  644. // A condition is applicable only when ALL tables it references are in seenSet.
  645. // For table-qualified refs (e.g. cor0.col2), we check the qualifying alias is seen.
  646. applyWhenSeen := func(seenSet map[int]bool, pending []parser.Expr) (applicable, still []parser.Expr) {
  647. for _, clause := range pending {
  648. tableRefs := collectTableColumnRefs(clause)
  649. ok := true
  650. for _, tr := range tableRefs {
  651. col := strings.ToLower(tr.col)
  652. tbl := strings.ToLower(tr.tbl)
  653. found := false
  654. if tbl != "" {
  655. // Explicit table qualifier — check that qualifying alias is seen.
  656. if idx, exists := aliasToIdx[tbl]; exists && seenSet[idx] {
  657. found = true
  658. }
  659. } else {
  660. // Unqualified — any seen table with this column satisfies it.
  661. for j, tti := range allTableInfos {
  662. if tti.colsSet[col] && seenSet[j] {
  663. found = true
  664. break
  665. }
  666. }
  667. }
  668. if !found {
  669. ok = false
  670. break
  671. }
  672. }
  673. if ok {
  674. applicable = append(applicable, clause)
  675. } else {
  676. still = append(still, clause)
  677. }
  678. }
  679. return
  680. }
  681. // Helper: inline cross-join two row-sets, applying a predicate.
  682. inlineJoin := func(left, right []storage.Row, pred parser.Expr) []storage.Row {
  683. out := make([]storage.Row, 0, len(left))
  684. for _, l := range left {
  685. for _, r := range right {
  686. m := make(storage.Row, len(l)+len(r))
  687. for k, v := range l {
  688. m[k] = v
  689. }
  690. for k, v := range r {
  691. m[k] = v
  692. }
  693. if pred != nil {
  694. val, _ := e.evalExpr(pred, m)
  695. if !toBool(val) {
  696. continue
  697. }
  698. }
  699. out = append(out, m)
  700. }
  701. }
  702. return out
  703. }
  704. // Pre-join connected components of "cross-only" tables (0 single-table filters,
  705. // connected via cross conditions to other cross-only tables).
  706. // This prevents n^k explosions when bare tables are joined last.
  707. crossOnlySet := map[int]bool{}
  708. for i := range stmt.From {
  709. if len(tableFilters[i]) > 0 {
  710. continue
  711. }
  712. for _, ce := range crossEdges {
  713. if ce.a == i || ce.b == i {
  714. crossOnlySet[i] = true
  715. break
  716. }
  717. }
  718. }
  719. // BFS: find connected components among cross-only tables.
  720. compOf := make([]int, len(stmt.From))
  721. for i := range compOf {
  722. compOf[i] = -1
  723. }
  724. nComps := 0
  725. for start := range stmt.From {
  726. if !crossOnlySet[start] || compOf[start] != -1 {
  727. continue
  728. }
  729. queue := []int{start}
  730. compOf[start] = nComps
  731. for len(queue) > 0 {
  732. cur := queue[0]
  733. queue = queue[1:]
  734. for _, ce := range crossEdges {
  735. var nb int = -1
  736. if ce.a == cur && crossOnlySet[ce.b] {
  737. nb = ce.b
  738. } else if ce.b == cur && crossOnlySet[ce.a] {
  739. nb = ce.a
  740. }
  741. if nb >= 0 && compOf[nb] == -1 {
  742. compOf[nb] = nComps
  743. queue = append(queue, nb)
  744. }
  745. }
  746. }
  747. nComps++
  748. }
  749. // Group cross-only tables by component.
  750. compTbls := make([][]int, nComps)
  751. for i, c := range compOf {
  752. if c >= 0 {
  753. compTbls[c] = append(compTbls[c], i)
  754. }
  755. }
  756. // Pre-join each component with ≥2 tables; collect results as virtual units.
  757. type virtualUnit struct {
  758. tableIdxs map[int]bool
  759. rows []storage.Row
  760. }
  761. var virtuals []virtualUnit
  762. preJoined := map[int]bool{} // original table indices consumed into virtuals
  763. remaining := make([]parser.Expr, len(crossFilters))
  764. copy(remaining, crossFilters)
  765. for _, comp := range compTbls {
  766. if len(comp) < 2 {
  767. continue
  768. }
  769. // Pick seed: table with most cross-edges within component.
  770. seed := comp[0]
  771. for _, idx := range comp[1:] {
  772. degIdx, degSeed := 0, 0
  773. for _, ce := range crossEdges {
  774. if ce.a == idx || ce.b == idx {
  775. degIdx++
  776. }
  777. if ce.a == seed || ce.b == seed {
  778. degSeed++
  779. }
  780. }
  781. if degIdx > degSeed {
  782. seed = idx
  783. }
  784. }
  785. // Load seed.
  786. seedRows, rerr := e.session.Select(stmt.From[seed].Name, nil)
  787. if rerr != nil {
  788. return nil, rerr
  789. }
  790. seedSchema, _ := e.schema.GetSchema(stmt.From[seed].Name)
  791. for j := range seedRows {
  792. normalizeRowBySchema(seedRows[j], seedSchema)
  793. seedRows[j] = e.addTableAlias(seedRows[j], allTableInfos[seed].alias)
  794. }
  795. vSeen := map[int]bool{seed: true}
  796. // Greedy within-component join.
  797. compSet := map[int]bool{}
  798. for _, idx := range comp {
  799. compSet[idx] = true
  800. }
  801. for len(vSeen) < len(comp) {
  802. // Pick next table in component with cross-edge to vSeen.
  803. nextC := -1
  804. for _, idx := range comp {
  805. if vSeen[idx] {
  806. continue
  807. }
  808. for _, ce := range crossEdges {
  809. if (ce.a == idx && vSeen[ce.b]) || (ce.b == idx && vSeen[ce.a]) {
  810. nextC = idx
  811. break
  812. }
  813. }
  814. if nextC >= 0 {
  815. break
  816. }
  817. }
  818. if nextC < 0 {
  819. for _, idx := range comp {
  820. if !vSeen[idx] {
  821. nextC = idx
  822. break
  823. }
  824. }
  825. }
  826. nextRows, rerr := e.session.Select(stmt.From[nextC].Name, nil)
  827. if rerr != nil {
  828. return nil, rerr
  829. }
  830. nextSchema, _ := e.schema.GetSchema(stmt.From[nextC].Name)
  831. for j := range nextRows {
  832. normalizeRowBySchema(nextRows[j], nextSchema)
  833. nextRows[j] = e.addTableAlias(nextRows[j], allTableInfos[nextC].alias)
  834. }
  835. vSeen[nextC] = true
  836. appl, still := applyWhenSeen(vSeen, remaining)
  837. remaining = still
  838. var pred parser.Expr
  839. if len(appl) > 0 {
  840. pred = combineAND(appl)
  841. }
  842. seedRows = inlineJoin(seedRows, nextRows, pred)
  843. }
  844. virtuals = append(virtuals, virtualUnit{tableIdxs: vSeen, rows: seedRows})
  845. for idx := range vSeen {
  846. preJoined[idx] = true
  847. }
  848. }
  849. // Build greedy order for non-pre-joined tables.
  850. // Score: single-table filter count + 1000 × cross-edges to already-joined.
  851. orderNonPJ := make([]int, 0, len(stmt.From)-len(preJoined))
  852. inOrderNPJ := make([]bool, len(stmt.From))
  853. best := -1
  854. for j := range stmt.From {
  855. if preJoined[j] {
  856. continue
  857. }
  858. if best < 0 || len(tableFilters[j]) > len(tableFilters[best]) {
  859. best = j
  860. }
  861. }
  862. if best >= 0 {
  863. orderNonPJ = append(orderNonPJ, best)
  864. inOrderNPJ[best] = true
  865. }
  866. for len(orderNonPJ)+len(preJoined) < len(stmt.From) {
  867. joined := map[int]bool{}
  868. for _, idx := range orderNonPJ {
  869. joined[idx] = true
  870. }
  871. nextIdx := -1
  872. nextScore := -1
  873. for j := range stmt.From {
  874. if inOrderNPJ[j] || preJoined[j] {
  875. continue
  876. }
  877. score := len(tableFilters[j])
  878. for _, ce := range crossEdges {
  879. if (ce.a == j && joined[ce.b]) || (ce.b == j && joined[ce.a]) {
  880. score += 1000
  881. }
  882. }
  883. if score > nextScore {
  884. nextScore = score
  885. nextIdx = j
  886. }
  887. }
  888. if nextIdx < 0 {
  889. for j := range stmt.From {
  890. if !inOrderNPJ[j] && !preJoined[j] {
  891. nextIdx = j
  892. break
  893. }
  894. }
  895. }
  896. if nextIdx >= 0 {
  897. orderNonPJ = append(orderNonPJ, nextIdx)
  898. inOrderNPJ[nextIdx] = true
  899. }
  900. }
  901. // Load the initial rows for the first non-pre-joined table (or use the already-loaded rows).
  902. seenTables := map[int]bool{}
  903. if len(orderNonPJ) > 0 {
  904. first := orderNonPJ[0]
  905. if first != 0 {
  906. rows, err = e.session.Select(stmt.From[first].Name, nil)
  907. if err != nil {
  908. return nil, err
  909. }
  910. firstSchema, _ := e.schema.GetSchema(stmt.From[first].Name)
  911. for i := range rows {
  912. normalizeRowBySchema(rows[i], firstSchema)
  913. rows[i] = e.addTableAlias(rows[i], allTableInfos[first].alias)
  914. }
  915. }
  916. if len(tableFilters[first]) > 0 {
  917. pred := combineAND(tableFilters[first])
  918. var filtered []storage.Row
  919. for _, row := range rows {
  920. val, _ := e.evalExpr(pred, row)
  921. if toBool(val) {
  922. filtered = append(filtered, row)
  923. }
  924. }
  925. rows = filtered
  926. }
  927. seenTables[first] = true
  928. // Join remaining non-pre-joined tables.
  929. for _, idx := range orderNonPJ[1:] {
  930. ti := allTableInfos[idx]
  931. rightRows, rerr := e.session.Select(stmt.From[idx].Name, nil)
  932. if rerr != nil {
  933. return nil, rerr
  934. }
  935. rightSchema, _ := e.schema.GetSchema(stmt.From[idx].Name)
  936. for j := range rightRows {
  937. normalizeRowBySchema(rightRows[j], rightSchema)
  938. rightRows[j] = e.addTableAlias(rightRows[j], ti.alias)
  939. }
  940. if len(tableFilters[idx]) > 0 {
  941. pred := combineAND(tableFilters[idx])
  942. var filtered []storage.Row
  943. for _, row := range rightRows {
  944. val, _ := e.evalExpr(pred, row)
  945. if toBool(val) {
  946. filtered = append(filtered, row)
  947. }
  948. }
  949. rightRows = filtered
  950. }
  951. seenTables[idx] = true
  952. appl, still := applyWhenSeen(seenTables, remaining)
  953. remaining = still
  954. var pred parser.Expr
  955. if len(appl) > 0 {
  956. pred = combineAND(appl)
  957. }
  958. rows = inlineJoin(rows, rightRows, pred)
  959. }
  960. } else {
  961. // All tables were pre-joined; start with empty placeholder.
  962. rows = []storage.Row{{}}
  963. }
  964. // Integrate virtual (pre-joined) units into the result.
  965. for _, vu := range virtuals {
  966. for idx := range vu.tableIdxs {
  967. seenTables[idx] = true
  968. }
  969. appl, still := applyWhenSeen(seenTables, remaining)
  970. remaining = still
  971. var pred parser.Expr
  972. if len(appl) > 0 {
  973. pred = combineAND(appl)
  974. }
  975. rows = inlineJoin(rows, vu.rows, pred)
  976. }
  977. // Apply any remaining conditions (shouldn't normally happen).
  978. if len(remaining) > 0 {
  979. pred := combineAND(remaining)
  980. var filtered []storage.Row
  981. for _, row := range rows {
  982. val, _ := e.evalExpr(pred, row)
  983. if toBool(val) {
  984. filtered = append(filtered, row)
  985. }
  986. }
  987. rows = filtered
  988. }
  989. // Also cross-join with any JOIN chains within FROM entries (mixed comma+JOIN syntax).
  990. // We cannot use executeJoins here because the left rows already have qualified keys
  991. // from the isMultiTable cross-join; re-aliasing the left side would corrupt them.
  992. for _, tref := range stmt.From {
  993. join := tref.Join
  994. for join != nil && join.Table != nil {
  995. rightRef := join.Table
  996. rightRows, rerr := e.session.Select(rightRef.Name, nil)
  997. if rerr != nil {
  998. return nil, rerr
  999. }
  1000. rightAlias := rightRef.Alias
  1001. if rightAlias == "" {
  1002. rightAlias = rightRef.Name
  1003. }
  1004. rightSchema, _ := e.schema.GetSchema(rightRef.Name)
  1005. for j := range rightRows {
  1006. normalizeRowBySchema(rightRows[j], rightSchema)
  1007. rightRows[j] = e.addTableAlias(rightRows[j], rightAlias)
  1008. }
  1009. var joined []storage.Row
  1010. for _, l := range rows {
  1011. for _, r := range rightRows {
  1012. m := make(storage.Row, len(l)+len(r))
  1013. for k, v := range l {
  1014. m[k] = v
  1015. }
  1016. for k, v := range r {
  1017. if _, exists := m[k]; !exists {
  1018. m[k] = v
  1019. } else if strings.Contains(k, ".") {
  1020. m[k] = v // qualified keys from right always win
  1021. }
  1022. }
  1023. if join.Condition != nil {
  1024. val, _ := e.evalExpr(join.Condition, m)
  1025. if !toBool(val) {
  1026. continue
  1027. }
  1028. }
  1029. joined = append(joined, m)
  1030. }
  1031. }
  1032. rows = joined
  1033. join = rightRef.Join
  1034. }
  1035. }
  1036. }
  1037. // Handle GROUP BY
  1038. if len(stmt.GroupBy) > 0 {
  1039. return e.executeGroupBy(stmt, rows, schema)
  1040. }
  1041. // Check for aggregate functions without GROUP BY
  1042. hasAggregate := e.hasAggregates(stmt.Columns)
  1043. if hasAggregate {
  1044. return e.executeAggregateSelect(stmt, rows, schema)
  1045. }
  1046. // Apply ORDER BY, LIMIT, and OFFSET.
  1047. rows = e.orderAndLimitRows(rows, stmt.OrderBy, stmt.Limit, stmt.Offset, stmt.Columns)
  1048. // Build result
  1049. result := NewResult("SELECT")
  1050. // For multi-table or JOIN queries, collect all table refs for SELECT * expansion.
  1051. hasJoin := len(stmt.From) > 0 && stmt.From[0].Join != nil
  1052. allTableRefs := collectAllTableRefs(stmt.From)
  1053. // Resolve qualified wildcard (table.*) projections once, keyed by qualifier.
  1054. type starProjection struct {
  1055. cols []storage.Column
  1056. prefix string
  1057. }
  1058. tableStars := make(map[string]starProjection)
  1059. for _, col := range stmt.Columns {
  1060. if col.TableStar == "" {
  1061. continue
  1062. }
  1063. key := strings.ToUpper(col.TableStar)
  1064. if _, ok := tableStars[key]; ok {
  1065. continue
  1066. }
  1067. cols, prefix, err := e.resolveTableStar(stmt.From, col.TableStar)
  1068. if err != nil {
  1069. return nil, err
  1070. }
  1071. tableStars[key] = starProjection{cols: cols, prefix: prefix}
  1072. }
  1073. // Determine columns
  1074. for i, col := range stmt.Columns {
  1075. if col.Alias != "" {
  1076. result.AddColumn(col.Alias)
  1077. } else if ref, ok := col.Expr.(*parser.ColumnRef); ok {
  1078. result.AddColumn(ref.Column)
  1079. } else if col.Star {
  1080. if isMultiTable || hasJoin {
  1081. // Add columns from ALL joined tables in order
  1082. for _, tref := range allTableRefs {
  1083. sch, _ := e.schema.GetSchema(tref.Name)
  1084. if sch != nil {
  1085. for _, c := range sch.Columns {
  1086. result.AddColumn(c.Name)
  1087. }
  1088. }
  1089. }
  1090. } else {
  1091. // Handle SELECT * - add all columns from schema
  1092. for _, c := range schema.Columns {
  1093. result.AddColumn(c.Name)
  1094. }
  1095. }
  1096. } else if col.TableStar != "" {
  1097. proj := tableStars[strings.ToUpper(col.TableStar)]
  1098. for _, c := range proj.cols {
  1099. result.AddColumn(c.Name)
  1100. }
  1101. } else {
  1102. result.AddColumn(fmt.Sprintf("column%d", i+1))
  1103. }
  1104. }
  1105. // Populate column types from schema metadata for single-table projections so
  1106. // protocol consumers can decode typed values (e.g. timestamps) even when the
  1107. // result set is empty. Join and multi-table projections defer type metadata.
  1108. if !isMultiTable && !hasJoin {
  1109. result.ColumnTypes = selectColumnTypes(stmt, schema)
  1110. }
  1111. // Window functions in the projection are evaluated over the scanned rows
  1112. // before their per-row values are read.
  1113. windowValues, werr := e.computeWindowValues(stmt, rows)
  1114. if werr != nil {
  1115. return nil, werr
  1116. }
  1117. // Add rows - evaluate each select expression
  1118. for rowIdx, row := range rows {
  1119. values := make([]interface{}, 0)
  1120. for _, col := range stmt.Columns {
  1121. if col.Star {
  1122. if isMultiTable || hasJoin {
  1123. // For multi-table SELECT *, extract columns using qualified names
  1124. for _, tref := range allTableRefs {
  1125. sch, _ := e.schema.GetSchema(tref.Name)
  1126. if sch != nil {
  1127. for _, c := range sch.Columns {
  1128. qualKey := tref.Alias + "." + c.Name
  1129. val, ok := row[qualKey]
  1130. if !ok {
  1131. val = row[c.Name]
  1132. }
  1133. values = append(values, val)
  1134. }
  1135. }
  1136. }
  1137. } else {
  1138. // For SELECT *, add all columns in order. A real column named
  1139. // oid/rowid/_rowid_ is an ordinary column here, not the hidden
  1140. // rowid alias, so use its own value key.
  1141. for _, c := range schema.Columns {
  1142. values = append(values, row[c.Name])
  1143. }
  1144. }
  1145. } else if col.TableStar != "" {
  1146. // Qualified wildcard (table.*): emit only that table's columns,
  1147. // resolving values via the effective alias, falling back to the
  1148. // unqualified key for single-table queries without an alias.
  1149. proj := tableStars[strings.ToUpper(col.TableStar)]
  1150. for _, c := range proj.cols {
  1151. val, ok := row[proj.prefix+"."+c.Name]
  1152. if !ok {
  1153. val = row[c.Name]
  1154. }
  1155. values = append(values, val)
  1156. }
  1157. } else if we, ok := col.Expr.(*parser.WindowExpr); ok {
  1158. values = append(values, windowValues[we][rowIdx])
  1159. } else {
  1160. // Evaluate the expression
  1161. val, err := e.evalExpr(col.Expr, row)
  1162. if err != nil {
  1163. return nil, err
  1164. }
  1165. values = append(values, val)
  1166. }
  1167. }
  1168. result.AddRow(values...)
  1169. }
  1170. // Apply DISTINCT if specified
  1171. if stmt.Distinct {
  1172. result.Rows = e.applyDistinct(result.Rows)
  1173. result.RowCount = len(result.Rows)
  1174. }
  1175. return result, nil
  1176. }
  1177. // executeCompound executes a compound SELECT (UNION / UNION ALL / INTERSECT / EXCEPT).
  1178. func (e *Executor) executeCompound(c *parser.CompoundSelect) (*Result, error) {
  1179. left, err := e.executeSelect(c.Left)
  1180. if err != nil {
  1181. return nil, err
  1182. }
  1183. right, err := e.executeSelect(c.Right)
  1184. if err != nil {
  1185. return nil, err
  1186. }
  1187. rowKey := func(row []interface{}) string {
  1188. parts := make([]string, len(row))
  1189. for i, v := range row {
  1190. if v == nil {
  1191. parts[i] = "\x00NULL"
  1192. } else {
  1193. parts[i] = fmt.Sprintf("%v", v)
  1194. }
  1195. }
  1196. return strings.Join(parts, "\x01")
  1197. }
  1198. result := NewResult("SELECT")
  1199. for _, col := range left.Columns {
  1200. result.AddColumn(col)
  1201. }
  1202. switch c.Op {
  1203. case parser.SetOpUnion:
  1204. seen := map[string]bool{}
  1205. for _, row := range left.Rows {
  1206. k := rowKey(row)
  1207. if !seen[k] {
  1208. seen[k] = true
  1209. result.AddRow(row...)
  1210. }
  1211. }
  1212. for _, row := range right.Rows {
  1213. k := rowKey(row)
  1214. if !seen[k] {
  1215. seen[k] = true
  1216. result.AddRow(row...)
  1217. }
  1218. }
  1219. case parser.SetOpUnionAll:
  1220. for _, row := range left.Rows {
  1221. result.AddRow(row...)
  1222. }
  1223. for _, row := range right.Rows {
  1224. result.AddRow(row...)
  1225. }
  1226. case parser.SetOpIntersect:
  1227. rightSet := map[string]bool{}
  1228. for _, row := range right.Rows {
  1229. rightSet[rowKey(row)] = true
  1230. }
  1231. seen := map[string]bool{}
  1232. for _, row := range left.Rows {
  1233. k := rowKey(row)
  1234. if rightSet[k] && !seen[k] {
  1235. seen[k] = true
  1236. result.AddRow(row...)
  1237. }
  1238. }
  1239. case parser.SetOpExcept:
  1240. rightSet := map[string]bool{}
  1241. for _, row := range right.Rows {
  1242. rightSet[rowKey(row)] = true
  1243. }
  1244. seen := map[string]bool{}
  1245. for _, row := range left.Rows {
  1246. k := rowKey(row)
  1247. if !rightSet[k] && !seen[k] {
  1248. seen[k] = true
  1249. result.AddRow(row...)
  1250. }
  1251. }
  1252. }
  1253. // Apply compound-level ORDER BY / LIMIT / OFFSET if present.
  1254. if len(c.OrderBy) > 0 {
  1255. e.sortResultRows(result, c.OrderBy, nil, nil)
  1256. }
  1257. if c.Limit != nil {
  1258. limitVal, err := e.evalExpr(c.Limit, nil)
  1259. if err == nil {
  1260. limit := int(toFloat(limitVal))
  1261. if limit < len(result.Rows) {
  1262. result.Rows = result.Rows[:limit]
  1263. }
  1264. }
  1265. }
  1266. if c.Offset != nil {
  1267. offsetVal, err := e.evalExpr(c.Offset, nil)
  1268. if err == nil {
  1269. offset := int(toFloat(offsetVal))
  1270. if offset >= len(result.Rows) {
  1271. result.Rows = nil
  1272. } else if offset > 0 {
  1273. result.Rows = result.Rows[offset:]
  1274. }
  1275. }
  1276. }
  1277. return result, nil
  1278. }
  1279. // executeSelectExpr executes a SELECT without FROM.
  1280. func (e *Executor) executeSelectExpr(stmt *parser.SelectStmt) (*Result, error) {
  1281. // If any column contains an aggregate, treat as single-group aggregate over one implicit row.
  1282. if e.hasAggregates(stmt.Columns) {
  1283. return e.executeAggregateSelect(stmt, []storage.Row{{}}, nil)
  1284. }
  1285. result := NewResult("SELECT")
  1286. // Determine columns
  1287. for i, col := range stmt.Columns {
  1288. if col.Alias != "" {
  1289. result.AddColumn(col.Alias)
  1290. } else {
  1291. result.AddColumn(fmt.Sprintf("column%d", i+1))
  1292. }
  1293. }
  1294. // Evaluate expressions
  1295. values := make([]interface{}, len(stmt.Columns))
  1296. for i, col := range stmt.Columns {
  1297. val, err := e.evalExpr(col.Expr, nil)
  1298. if err != nil {
  1299. return nil, err
  1300. }
  1301. values[i] = val
  1302. }
  1303. result.AddRow(values...)
  1304. return result, nil
  1305. }
  1306. // executeSelectFromSubquery executes a SELECT with a subquery in FROM clause.
  1307. func (e *Executor) executeSelectFromSubquery(stmt *parser.SelectStmt) (*Result, error) {
  1308. // Execute the subquery to get the derived table
  1309. subqueryResult, err := e.executeSelect(stmt.From[0].Subquery)
  1310. if err != nil {
  1311. return nil, fmt.Errorf("subquery error: %w", err)
  1312. }
  1313. return e.executeSelectOnMaterialized(stmt, subqueryResult.Columns, subqueryResult.Rows)
  1314. }
  1315. // executeSelectOnMaterialized runs a SELECT whose FROM[0] is already
  1316. // materialized as (columns, rowValues). It is shared by derived tables and
  1317. // materialized CTEs.
  1318. func (e *Executor) executeSelectOnMaterialized(stmt *parser.SelectStmt, columns []string, rowValues [][]interface{}) (*Result, error) {
  1319. derivedRows := make([]storage.Row, 0, len(rowValues))
  1320. for _, values := range rowValues {
  1321. row := make(storage.Row, len(columns))
  1322. for i, col := range columns {
  1323. if i < len(values) {
  1324. row[col] = values[i]
  1325. }
  1326. }
  1327. derivedRows = append(derivedRows, row)
  1328. }
  1329. // Handle JOINs if present
  1330. if stmt.From[0].Join != nil {
  1331. var err error
  1332. derivedRows, err = e.executeJoin(stmt.From[0], derivedRows)
  1333. if err != nil {
  1334. return nil, err
  1335. }
  1336. }
  1337. // Apply WHERE clause on derived table
  1338. if stmt.Where != nil {
  1339. filteredRows := make([]storage.Row, 0)
  1340. for _, row := range derivedRows {
  1341. val, err := e.evalExpr(stmt.Where, row)
  1342. if err != nil {
  1343. continue
  1344. }
  1345. if toBool(val) {
  1346. filteredRows = append(filteredRows, row)
  1347. }
  1348. }
  1349. derivedRows = filteredRows
  1350. }
  1351. // Handle GROUP BY
  1352. if len(stmt.GroupBy) > 0 {
  1353. return e.executeGroupBy(stmt, derivedRows, schemaFromColumns(columns))
  1354. }
  1355. // Check for aggregate functions without GROUP BY
  1356. if e.hasAggregates(stmt.Columns) {
  1357. return e.executeAggregateSelect(stmt, derivedRows, schemaFromColumns(columns))
  1358. }
  1359. // Apply ORDER BY, LIMIT, and OFFSET.
  1360. derivedRows = e.orderAndLimitRows(derivedRows, stmt.OrderBy, stmt.Limit, stmt.Offset, stmt.Columns)
  1361. // Build result. A projection may mix * and expressions, so each column is
  1362. // expanded independently.
  1363. result := NewResult("SELECT")
  1364. for _, col := range stmt.Columns {
  1365. switch {
  1366. case col.Star:
  1367. for _, c := range columns {
  1368. result.AddColumn(c)
  1369. }
  1370. case col.Alias != "":
  1371. result.AddColumn(col.Alias)
  1372. case col.Expr != nil:
  1373. if colRef, ok := col.Expr.(*parser.ColumnRef); ok {
  1374. result.AddColumn(colRef.Column)
  1375. } else {
  1376. result.AddColumn("column")
  1377. }
  1378. default:
  1379. result.AddColumn("column")
  1380. }
  1381. }
  1382. // Window functions in the projection are evaluated over the materialized
  1383. // rows before the values are read.
  1384. windowValues, err := e.computeWindowValues(stmt, derivedRows)
  1385. if err != nil {
  1386. return nil, err
  1387. }
  1388. for rowIdx, row := range derivedRows {
  1389. values := make([]interface{}, 0, len(stmt.Columns))
  1390. for _, col := range stmt.Columns {
  1391. switch {
  1392. case col.Star:
  1393. for _, c := range columns {
  1394. values = append(values, row[c])
  1395. }
  1396. case col.Expr != nil:
  1397. if we, ok := col.Expr.(*parser.WindowExpr); ok {
  1398. values = append(values, windowValues[we][rowIdx])
  1399. continue
  1400. }
  1401. val, err := e.evalExpr(col.Expr, row)
  1402. if err != nil {
  1403. return nil, err
  1404. }
  1405. values = append(values, val)
  1406. }
  1407. }
  1408. result.AddRow(values...)
  1409. }
  1410. return result, nil
  1411. }
  1412. // schemaFromColumns builds a schema with ANY-typed columns for materialized
  1413. // derived tables and CTEs.
  1414. func schemaFromColumns(columns []string) *storage.Schema {
  1415. schema := &storage.Schema{Name: "derived", Columns: make([]storage.Column, len(columns))}
  1416. for i, col := range columns {
  1417. schema.Columns[i] = storage.Column{Name: col, Type: "ANY"}
  1418. }
  1419. return schema
  1420. }
  1421. // executeAggregateSelect executes a SELECT with aggregate functions.
  1422. func (e *Executor) executeAggregateSelect(stmt *parser.SelectStmt, rows []storage.Row, schema *storage.Schema) (*Result, error) {
  1423. result := NewResult("SELECT")
  1424. // Determine columns and evaluate aggregates
  1425. for i, col := range stmt.Columns {
  1426. if col.Alias != "" {
  1427. result.AddColumn(col.Alias)
  1428. } else if col.Star {
  1429. result.AddColumn("*")
  1430. } else {
  1431. result.AddColumn(fmt.Sprintf("column%d", i+1))
  1432. }
  1433. }
  1434. // Calculate values
  1435. values := make([]interface{}, len(stmt.Columns))
  1436. for i, col := range stmt.Columns {
  1437. val, err := e.evalAggregateExpr(col.Expr, rows)
  1438. if err != nil {
  1439. return nil, err
  1440. }
  1441. values[i] = val
  1442. }
  1443. result.AddRow(values...)
  1444. return result, nil
  1445. }
  1446. // executeGroupBy executes a GROUP BY query.
  1447. func (e *Executor) executeGroupBy(stmt *parser.SelectStmt, rows []storage.Row, schema *storage.Schema) (*Result, error) {
  1448. result := NewResult("SELECT")
  1449. // Expand SELECT * if present
  1450. expandedColumns := make([]parser.SelectColumn, 0, len(stmt.Columns))
  1451. for _, col := range stmt.Columns {
  1452. if col.Star {
  1453. for _, c := range schema.Columns {
  1454. expandedColumns = append(expandedColumns, parser.SelectColumn{
  1455. Expr: &parser.ColumnRef{Column: c.Name},
  1456. })
  1457. }
  1458. } else if col.TableStar != "" {
  1459. // Qualified wildcard: expand only the named table's columns.
  1460. cols, prefix, err := e.resolveTableStar(stmt.From, col.TableStar)
  1461. if err != nil {
  1462. return nil, err
  1463. }
  1464. for _, c := range cols {
  1465. expandedColumns = append(expandedColumns, parser.SelectColumn{
  1466. Expr: &parser.ColumnRef{Table: prefix, Column: c.Name},
  1467. })
  1468. }
  1469. } else {
  1470. expandedColumns = append(expandedColumns, col)
  1471. }
  1472. }
  1473. // Determine column names
  1474. columnNames := make([]string, len(expandedColumns))
  1475. for i, col := range expandedColumns {
  1476. if col.Alias != "" {
  1477. columnNames[i] = col.Alias
  1478. result.AddColumn(col.Alias)
  1479. } else if ref, ok := col.Expr.(*parser.ColumnRef); ok {
  1480. columnNames[i] = ref.Column
  1481. result.AddColumn(ref.Column)
  1482. } else {
  1483. columnNames[i] = fmt.Sprintf("column%d", i+1)
  1484. result.AddColumn(columnNames[i])
  1485. }
  1486. }
  1487. // Fast path: use running accumulators instead of collecting rows per group.
  1488. // Applicable when there is no HAVING clause and all aggregate SELECT columns
  1489. // are direct FunctionCalls (COUNT/SUM/AVG/MIN/MAX).
  1490. if e.canUseGroupAccum(stmt, expandedColumns) {
  1491. return e.executeGroupByAccum(stmt, rows, result, expandedColumns, columnNames)
  1492. }
  1493. // Slow path: collect full rows per group then evaluate aggregates over them.
  1494. groups := make(map[string][]storage.Row)
  1495. for _, row := range rows {
  1496. key := e.buildGroupKey(stmt.GroupBy, row)
  1497. groups[key] = append(groups[key], row)
  1498. }
  1499. for _, groupRows := range groups {
  1500. if stmt.Having != nil {
  1501. val, err := e.evalAggregateExpr(stmt.Having, groupRows)
  1502. if err != nil || val == nil || !toBool(val) {
  1503. continue
  1504. }
  1505. }
  1506. values := make([]interface{}, len(expandedColumns))
  1507. for i, col := range expandedColumns {
  1508. if e.isAggregate(col.Expr) {
  1509. val, err := e.evalAggregateExpr(col.Expr, groupRows)
  1510. if err != nil {
  1511. return nil, err
  1512. }
  1513. values[i] = val
  1514. } else {
  1515. val, err := e.evalExpr(col.Expr, groupRows[0])
  1516. if err != nil {
  1517. return nil, err
  1518. }
  1519. values[i] = val
  1520. }
  1521. }
  1522. result.AddRow(values...)
  1523. }
  1524. return e.finalizeGroupResult(stmt, result, expandedColumns, columnNames)
  1525. }
  1526. // canUseGroupAccum returns true when the fast accumulator path can handle the query.
  1527. func (e *Executor) canUseGroupAccum(stmt *parser.SelectStmt, expandedColumns []parser.SelectColumn) bool {
  1528. if stmt.Having != nil {
  1529. return false
  1530. }
  1531. for _, col := range expandedColumns {
  1532. if !e.isAggregate(col.Expr) {
  1533. continue
  1534. }
  1535. fn, ok := col.Expr.(*parser.FunctionCall)
  1536. if !ok {
  1537. return false
  1538. }
  1539. switch strings.ToUpper(fn.Name) {
  1540. case "COUNT", "SUM", "AVG", "MIN", "MAX":
  1541. default:
  1542. return false
  1543. }
  1544. }
  1545. return true
  1546. }
  1547. // aggColInfo pairs a SELECT column index with its aggregate FunctionCall.
  1548. type aggColInfo struct {
  1549. colIdx int
  1550. fn *parser.FunctionCall
  1551. }
  1552. // aggAccum holds running state for a single aggregate function.
  1553. type aggAccum struct {
  1554. count int64
  1555. sumI int64
  1556. sumF float64
  1557. allInt bool
  1558. hasVal bool
  1559. extreme interface{}
  1560. seen map[interface{}]struct{} // for DISTINCT
  1561. }
  1562. // groupAccumState holds per-group state for the fast accumulator path.
  1563. type groupAccumState struct {
  1564. firstRow storage.Row
  1565. accums []*aggAccum
  1566. }
  1567. // executeGroupByAccum is the fast GROUP BY path: increments per-group counters as rows
  1568. // arrive rather than materialising row slices, keeping O(1) state per group.
  1569. func (e *Executor) executeGroupByAccum(stmt *parser.SelectStmt, rows []storage.Row, result *Result, expandedColumns []parser.SelectColumn, columnNames []string) (*Result, error) {
  1570. var aggCols []aggColInfo
  1571. for i, col := range expandedColumns {
  1572. if e.isAggregate(col.Expr) {
  1573. aggCols = append(aggCols, aggColInfo{i, col.Expr.(*parser.FunctionCall)})
  1574. }
  1575. }
  1576. states := make(map[string]*groupAccumState, 64)
  1577. var keyOrder []string
  1578. for _, row := range rows {
  1579. key := e.buildGroupKey(stmt.GroupBy, row)
  1580. state, exists := states[key]
  1581. if !exists {
  1582. accums := make([]*aggAccum, len(aggCols))
  1583. for j, ac := range aggCols {
  1584. a := &aggAccum{allInt: true}
  1585. if ac.fn.Distinct {
  1586. a.seen = make(map[interface{}]struct{})
  1587. }
  1588. accums[j] = a
  1589. }
  1590. state = &groupAccumState{firstRow: row, accums: accums}
  1591. states[key] = state
  1592. keyOrder = append(keyOrder, key)
  1593. }
  1594. for j, ac := range aggCols {
  1595. e.feedAggAccum(state.accums[j], ac.fn, row)
  1596. }
  1597. }
  1598. for _, key := range keyOrder {
  1599. state := states[key]
  1600. values := make([]interface{}, len(expandedColumns))
  1601. for i, col := range expandedColumns {
  1602. if e.isAggregate(col.Expr) {
  1603. for j, ac := range aggCols {
  1604. if ac.colIdx == i {
  1605. values[i] = finalizeAggAccum(state.accums[j], ac.fn)
  1606. break
  1607. }
  1608. }
  1609. } else {
  1610. val, _ := e.evalExpr(col.Expr, state.firstRow)
  1611. values[i] = val
  1612. }
  1613. }
  1614. result.AddRow(values...)
  1615. }
  1616. return e.finalizeGroupResult(stmt, result, expandedColumns, columnNames)
  1617. }
  1618. // feedAggAccum updates a running accumulator with one row.
  1619. func (e *Executor) feedAggAccum(a *aggAccum, fn *parser.FunctionCall, row storage.Row) {
  1620. switch strings.ToUpper(fn.Name) {
  1621. case "COUNT":
  1622. if fn.Star {
  1623. a.count++
  1624. return
  1625. }
  1626. if len(fn.Args) == 0 {
  1627. return
  1628. }
  1629. val, _ := e.evalExpr(fn.Args[0], row)
  1630. if val == nil {
  1631. return
  1632. }
  1633. if fn.Distinct {
  1634. k := fmt.Sprintf("%v", val)
  1635. if _, exists := a.seen[k]; exists {
  1636. return
  1637. }
  1638. a.seen[k] = struct{}{}
  1639. }
  1640. a.count++
  1641. case "SUM":
  1642. if len(fn.Args) == 0 {
  1643. return
  1644. }
  1645. val, _ := e.evalExpr(fn.Args[0], row)
  1646. if val == nil {
  1647. return
  1648. }
  1649. if fn.Distinct {
  1650. k := fmt.Sprintf("%v", val)
  1651. if _, exists := a.seen[k]; exists {
  1652. return
  1653. }
  1654. a.seen[k] = struct{}{}
  1655. }
  1656. if isIntVal(val) {
  1657. a.sumI += toInt64(val)
  1658. } else {
  1659. a.allInt = false
  1660. a.sumF += toFloat(val)
  1661. }
  1662. a.hasVal = true
  1663. case "AVG":
  1664. if len(fn.Args) == 0 {
  1665. return
  1666. }
  1667. val, _ := e.evalExpr(fn.Args[0], row)
  1668. if val == nil {
  1669. return
  1670. }
  1671. a.sumF += toFloat(val)
  1672. a.count++
  1673. a.hasVal = true
  1674. case "MIN":
  1675. if len(fn.Args) == 0 {
  1676. return
  1677. }
  1678. val, _ := e.evalExpr(fn.Args[0], row)
  1679. if val != nil && (a.extreme == nil || compare(val, a.extreme) < 0) {
  1680. a.extreme = val
  1681. }
  1682. case "MAX":
  1683. if len(fn.Args) == 0 {
  1684. return
  1685. }
  1686. val, _ := e.evalExpr(fn.Args[0], row)
  1687. if val != nil && (a.extreme == nil || compare(val, a.extreme) > 0) {
  1688. a.extreme = val
  1689. }
  1690. }
  1691. }
  1692. // finalizeAggAccum computes the final aggregate value from a running accumulator.
  1693. func finalizeAggAccum(a *aggAccum, fn *parser.FunctionCall) interface{} {
  1694. switch strings.ToUpper(fn.Name) {
  1695. case "COUNT":
  1696. return a.count
  1697. case "SUM":
  1698. if !a.hasVal {
  1699. return nil
  1700. }
  1701. if a.allInt {
  1702. return a.sumI
  1703. }
  1704. return a.sumF + float64(a.sumI)
  1705. case "AVG":
  1706. if !a.hasVal || a.count == 0 {
  1707. return nil
  1708. }
  1709. return a.sumF / float64(a.count)
  1710. case "MIN", "MAX":
  1711. return a.extreme
  1712. }
  1713. return nil
  1714. }
  1715. // finalizeGroupResult applies DISTINCT, ORDER BY, and LIMIT/OFFSET to a GROUP BY result.
  1716. func (e *Executor) finalizeGroupResult(stmt *parser.SelectStmt, result *Result, expandedColumns []parser.SelectColumn, columnNames []string) (*Result, error) {
  1717. if stmt.Distinct {
  1718. result.Rows = e.applyDistinct(result.Rows)
  1719. }
  1720. e.orderAndLimitResultRows(result, stmt.OrderBy, stmt.Limit, stmt.Offset, expandedColumns, columnNames)
  1721. return result, nil
  1722. }
  1723. // executeJoins recursively processes all JOIN clauses in a table reference.
  1724. func (e *Executor) executeJoins(tableRef parser.TableRef, leftRows []storage.Row) ([]storage.Row, error) {
  1725. return e.executeJoinsWithMode(tableRef, leftRows, true)
  1726. }
  1727. func (e *Executor) executeJoinsWithMode(tableRef parser.TableRef, leftRows []storage.Row, qualifyLeft bool) ([]storage.Row, error) {
  1728. if tableRef.Join == nil || tableRef.Join.Table == nil {
  1729. return leftRows, nil
  1730. }
  1731. // Get the right table name. Equality joins against its primary key can probe
  1732. // only the referenced rows instead of scanning the whole table.
  1733. rightTableRef := tableRef.Join.Table
  1734. rightTable := rightTableRef.Name
  1735. // Perform the join between left and right
  1736. var result []storage.Row
  1737. leftTableName := tableRef.Name
  1738. leftAlias := tableRef.Alias
  1739. rightAlias := rightTableRef.Alias
  1740. // If leftAlias is empty, use the table name
  1741. if leftAlias == "" {
  1742. leftAlias = leftTableName
  1743. }
  1744. if rightAlias == "" {
  1745. rightAlias = rightTable
  1746. }
  1747. leftAliasForMerge := leftAlias
  1748. if !qualifyLeft {
  1749. leftAliasForMerge = ""
  1750. }
  1751. // Build a synthetic TableRef so we can reuse extractEqualityJoinKeys.
  1752. syntheticLeft := parser.TableRef{Name: leftTableName, Alias: leftAlias}
  1753. syntheticJoin := &parser.JoinClause{
  1754. Type: tableRef.Join.Type,
  1755. Table: &parser.TableRef{Name: rightTable, Alias: rightAlias},
  1756. Condition: tableRef.Join.Condition,
  1757. }
  1758. leftKey, rightKey, canHash := extractEqualityJoinKeys(tableRef.Join.Condition, syntheticLeft, syntheticJoin)
  1759. var rightRows []storage.Row
  1760. var rightSchema *storage.Schema
  1761. var err error
  1762. switch {
  1763. case rightTableRef.Subquery != nil:
  1764. rightRows, rightSchema, err = e.materializeJoinSubquery(rightTableRef)
  1765. if err != nil {
  1766. return nil, err
  1767. }
  1768. case e.cteTableExists(rightTable):
  1769. cte, _ := e.cteTableFor(rightTable)
  1770. rightRows = cloneRows(cte.rows)
  1771. rightSchema = schemaFromColumns(cte.columns)
  1772. default:
  1773. rightSchema, err = e.schema.GetSchema(rightTable)
  1774. if err != nil {
  1775. return nil, err
  1776. }
  1777. if canHash && strings.EqualFold(rightSchema.PrimaryKey, rightKey) && len(leftRows) <= 256 {
  1778. seen := make(map[string]bool, len(leftRows))
  1779. for _, left := range leftRows {
  1780. key := joinKeyString(left, leftKey)
  1781. if key == "\x00" || seen[key] {
  1782. continue
  1783. }
  1784. seen[key] = true
  1785. row, getErr := e.session.GetByPK(rightTable, key)
  1786. if getErr == storage.ErrKeyNotFound {
  1787. continue
  1788. }
  1789. if getErr != nil {
  1790. return nil, getErr
  1791. }
  1792. normalizeRowBySchema(row, rightSchema)
  1793. rightRows = append(rightRows, row)
  1794. }
  1795. } else {
  1796. rightRows, err = e.session.Select(rightTable, nil)
  1797. if err != nil {
  1798. return nil, err
  1799. }
  1800. for _, row := range rightRows {
  1801. normalizeRowBySchema(row, rightSchema)
  1802. }
  1803. }
  1804. }
  1805. switch tableRef.Join.Type {
  1806. case parser.JoinInner:
  1807. if canHash {
  1808. hashTable := make(map[string][]storage.Row, len(rightRows))
  1809. for _, right := range rightRows {
  1810. k := joinKeyString(right, rightKey)
  1811. hashTable[k] = append(hashTable[k], right)
  1812. }
  1813. for _, left := range leftRows {
  1814. k := joinKeyString(left, leftKey)
  1815. for _, right := range hashTable[k] {
  1816. result = append(result, e.mergeRows(left, right, leftAliasForMerge, rightAlias))
  1817. }
  1818. }
  1819. } else {
  1820. for _, left := range leftRows {
  1821. for _, right := range rightRows {
  1822. merged := e.mergeRows(left, right, leftAliasForMerge, rightAlias)
  1823. if tableRef.Join.Condition != nil {
  1824. match, _ := e.evalExpr(tableRef.Join.Condition, merged)
  1825. if toBool(match) {
  1826. result = append(result, merged)
  1827. }
  1828. } else {
  1829. result = append(result, merged)
  1830. }
  1831. }
  1832. }
  1833. }
  1834. case parser.JoinLeft:
  1835. if canHash {
  1836. hashTable := make(map[string][]storage.Row, len(rightRows))
  1837. for _, right := range rightRows {
  1838. k := joinKeyString(right, rightKey)
  1839. hashTable[k] = append(hashTable[k], right)
  1840. }
  1841. nullRight := makeNullRow(rightRows, rightTable, e)
  1842. for _, left := range leftRows {
  1843. k := joinKeyString(left, leftKey)
  1844. matches := hashTable[k]
  1845. if len(matches) == 0 {
  1846. result = append(result, e.mergeRows(left, nullRight, leftAliasForMerge, rightAlias))
  1847. } else {
  1848. for _, right := range matches {
  1849. result = append(result, e.mergeRows(left, right, leftAliasForMerge, rightAlias))
  1850. }
  1851. }
  1852. }
  1853. } else {
  1854. for _, left := range leftRows {
  1855. matched := false
  1856. for _, right := range rightRows {
  1857. merged := e.mergeRows(left, right, leftAliasForMerge, rightAlias)
  1858. if tableRef.Join.Condition != nil {
  1859. match, _ := e.evalExpr(tableRef.Join.Condition, merged)
  1860. if toBool(match) {
  1861. result = append(result, merged)
  1862. matched = true
  1863. }
  1864. }
  1865. }
  1866. if !matched {
  1867. nullRight := makeNullRow(rightRows, rightTable, e)
  1868. result = append(result, e.mergeRows(left, nullRight, leftAliasForMerge, rightAlias))
  1869. }
  1870. }
  1871. }
  1872. case parser.JoinCross:
  1873. for _, left := range leftRows {
  1874. for _, right := range rightRows {
  1875. result = append(result, e.mergeRows(left, right, leftAliasForMerge, rightAlias))
  1876. }
  1877. }
  1878. }
  1879. // Recursively process any additional joins
  1880. if rightTableRef.Join != nil {
  1881. return e.executeJoinsWithMode(*rightTableRef, result, false)
  1882. }
  1883. return result, nil
  1884. }
  1885. // executeJoin executes a JOIN operation.
  1886. func (e *Executor) executeJoin(tableRef parser.TableRef, leftRows []storage.Row) ([]storage.Row, error) {
  1887. join := tableRef.Join
  1888. if join == nil || join.Table == nil {
  1889. return leftRows, nil
  1890. }
  1891. rightTable := join.Table.Name
  1892. var rightRows []storage.Row
  1893. var err error
  1894. switch {
  1895. case join.Table.Subquery != nil:
  1896. rightRows, _, err = e.materializeJoinSubquery(join.Table)
  1897. case e.cteTableExists(rightTable):
  1898. cte, _ := e.cteTableFor(rightTable)
  1899. rightRows = cloneRows(cte.rows)
  1900. default:
  1901. rightRows, err = e.session.Select(rightTable, nil)
  1902. }
  1903. if err != nil {
  1904. return nil, err
  1905. }
  1906. var result []storage.Row
  1907. switch join.Type {
  1908. case parser.JoinInner:
  1909. leftKey, rightKey, canHash := extractEqualityJoinKeys(join.Condition, tableRef, join)
  1910. if canHash {
  1911. // Hash join: build phase on right, probe phase on left — O(N+M) vs O(N*M)
  1912. hashTable := make(map[string][]storage.Row, len(rightRows))
  1913. for _, right := range rightRows {
  1914. k := joinKeyString(right, rightKey)
  1915. hashTable[k] = append(hashTable[k], right)
  1916. }
  1917. for _, left := range leftRows {
  1918. k := joinKeyString(left, leftKey)
  1919. for _, right := range hashTable[k] {
  1920. result = append(result, e.mergeRows(left, right, tableRef.Alias, join.Table.Alias))
  1921. }
  1922. }
  1923. } else {
  1924. for _, left := range leftRows {
  1925. for _, right := range rightRows {
  1926. merged := e.mergeRows(left, right, tableRef.Alias, join.Table.Alias)
  1927. if join.Condition != nil {
  1928. match, _ := e.evalExpr(join.Condition, merged)
  1929. if toBool(match) {
  1930. result = append(result, merged)
  1931. }
  1932. } else {
  1933. result = append(result, merged)
  1934. }
  1935. }
  1936. }
  1937. }
  1938. case parser.JoinLeft:
  1939. leftKey, rightKey, canHash := extractEqualityJoinKeys(join.Condition, tableRef, join)
  1940. if canHash {
  1941. hashTable := make(map[string][]storage.Row, len(rightRows))
  1942. for _, right := range rightRows {
  1943. k := joinKeyString(right, rightKey)
  1944. hashTable[k] = append(hashTable[k], right)
  1945. }
  1946. nullRight := makeNullRow(rightRows, rightTable, e)
  1947. for _, left := range leftRows {
  1948. k := joinKeyString(left, leftKey)
  1949. matches := hashTable[k]
  1950. if len(matches) == 0 {
  1951. result = append(result, e.mergeRows(left, nullRight, tableRef.Alias, join.Table.Alias))
  1952. } else {
  1953. for _, right := range matches {
  1954. result = append(result, e.mergeRows(left, right, tableRef.Alias, join.Table.Alias))
  1955. }
  1956. }
  1957. }
  1958. } else {
  1959. for _, left := range leftRows {
  1960. matched := false
  1961. for _, right := range rightRows {
  1962. merged := e.mergeRows(left, right, tableRef.Alias, join.Table.Alias)
  1963. if join.Condition != nil {
  1964. match, _ := e.evalExpr(join.Condition, merged)
  1965. if toBool(match) {
  1966. result = append(result, merged)
  1967. matched = true
  1968. }
  1969. }
  1970. }
  1971. if !matched {
  1972. nullRight := makeNullRow(rightRows, rightTable, e)
  1973. result = append(result, e.mergeRows(left, nullRight, tableRef.Alias, join.Table.Alias))
  1974. }
  1975. }
  1976. }
  1977. case parser.JoinCross:
  1978. for _, left := range leftRows {
  1979. for _, right := range rightRows {
  1980. result = append(result, e.mergeRows(left, right, tableRef.Alias, join.Table.Alias))
  1981. }
  1982. }
  1983. }
  1984. return result, nil
  1985. }
  1986. // extractEqualityJoinKeys checks if a JOIN condition is a simple col = col equality
  1987. // and returns the key names to probe in left rows and build from right rows.
  1988. func extractEqualityJoinKeys(condition parser.Expr, leftRef parser.TableRef, join *parser.JoinClause) (leftKey, rightKey string, ok bool) {
  1989. if condition == nil {
  1990. return "", "", false
  1991. }
  1992. bin, isBin := condition.(*parser.BinaryExpr)
  1993. if !isBin || bin.Op != lexer.TokenEq {
  1994. return "", "", false
  1995. }
  1996. lRef, leftIsCol := bin.Left.(*parser.ColumnRef)
  1997. rRef, rightIsCol := bin.Right.(*parser.ColumnRef)
  1998. if !leftIsCol || !rightIsCol {
  1999. return "", "", false
  2000. }
  2001. leftAlias := leftRef.Alias
  2002. leftName := leftRef.Name
  2003. rightAlias := join.Table.Alias
  2004. rightName := join.Table.Name
  2005. leftJoinKey := func(r *parser.ColumnRef) (string, bool) {
  2006. if r.Table == "" || r.Table == leftAlias || r.Table == leftName {
  2007. return r.Column, true
  2008. }
  2009. // In a chained explicit JOIN, the left row already contains every table
  2010. // joined so far. Preserve qualified references such as "o.id" so joins
  2011. // against earlier tables can still use the hash path.
  2012. if r.Table != rightAlias && r.Table != rightName {
  2013. return r.Table + "." + r.Column, true
  2014. }
  2015. return "", false
  2016. }
  2017. rightJoinKey := func(r *parser.ColumnRef) (string, bool) {
  2018. if r.Table == "" || r.Table == rightAlias || r.Table == rightName {
  2019. return r.Column, true
  2020. }
  2021. return "", false
  2022. }
  2023. if lk, leftOK := leftJoinKey(lRef); leftOK {
  2024. if rk, rightOK := rightJoinKey(rRef); rightOK {
  2025. return lk, rk, true
  2026. }
  2027. }
  2028. if lk, leftOK := leftJoinKey(rRef); leftOK {
  2029. if rk, rightOK := rightJoinKey(lRef); rightOK {
  2030. return lk, rk, true
  2031. }
  2032. }
  2033. return "", "", false
  2034. }
  2035. // joinKeyString returns a string representation of a row's join key for hashing.
  2036. func joinKeyString(row storage.Row, col string) string {
  2037. if v, ok := row[col]; ok {
  2038. return fmt.Sprintf("%v", v)
  2039. }
  2040. return "\x00"
  2041. }
  2042. // makeNullRow builds a null-valued row based on the right table's rows or schema.
  2043. func makeNullRow(rightRows []storage.Row, rightTable string, e *Executor) storage.Row {
  2044. nullRight := make(storage.Row)
  2045. if len(rightRows) > 0 {
  2046. for k := range rightRows[0] {
  2047. nullRight[k] = nil
  2048. }
  2049. } else {
  2050. rightSchema, err := e.schema.GetSchema(rightTable)
  2051. if err == nil {
  2052. for _, col := range rightSchema.Columns {
  2053. nullRight[col.Name] = nil
  2054. }
  2055. }
  2056. }
  2057. return nullRight
  2058. }
  2059. // mergeRows merges two rows with optional table aliases.
  2060. func (e *Executor) mergeRows(left, right storage.Row, leftAlias, rightAlias string) storage.Row {
  2061. result := make(storage.Row)
  2062. for k, v := range left {
  2063. // Copy the key as-is (it might already be qualified)
  2064. result[k] = v
  2065. // Only add qualified name if the key is NOT already qualified and we have an alias
  2066. if leftAlias != "" && !strings.Contains(k, ".") {
  2067. result[leftAlias+"."+k] = v
  2068. }
  2069. }
  2070. for k, v := range right {
  2071. // For unqualified names, only add if they don't already exist
  2072. // This prevents right table columns from overwriting left table columns
  2073. if !strings.Contains(k, ".") {
  2074. if _, exists := result[k]; !exists {
  2075. result[k] = v
  2076. }
  2077. // Add qualified name for right table
  2078. if rightAlias != "" {
  2079. result[rightAlias+"."+k] = v
  2080. }
  2081. } else {
  2082. // Already qualified, just copy it
  2083. result[k] = v
  2084. }
  2085. }
  2086. return result
  2087. }
  2088. // collectAllTableRefs returns a flat list of (alias, tableName) pairs for all tables
  2089. // referenced in a FROM clause, following both implicit (comma) and explicit JOIN chains.
  2090. func collectAllTableRefs(from []parser.TableRef) []parser.TableRef {
  2091. var refs []parser.TableRef
  2092. for _, tref := range from {
  2093. cur := tref
  2094. for {
  2095. // Shallow copy to hold only this table (no join chain)
  2096. flat := parser.TableRef{Name: cur.Name, Alias: cur.Alias}
  2097. if flat.Alias == "" {
  2098. flat.Alias = flat.Name
  2099. }
  2100. refs = append(refs, flat)
  2101. if cur.Join == nil || cur.Join.Table == nil {
  2102. break
  2103. }
  2104. cur = *cur.Join.Table
  2105. }
  2106. }
  2107. return refs
  2108. }
  2109. // resolveTableStar resolves a qualified wildcard (table.*) qualifier to the
  2110. // matching table ref in a FROM clause. It returns the table's schema columns
  2111. // and the key prefix used to look values up in a joined/aliased row (the
  2112. // effective table alias). An unknown qualifier is an error, never a silent
  2113. // fallback to plain column expansion.
  2114. func (e *Executor) resolveTableStar(from []parser.TableRef, qualifier string) ([]storage.Column, string, error) {
  2115. refs := collectAllTableRefs(from)
  2116. for _, ref := range refs {
  2117. if strings.EqualFold(ref.Alias, qualifier) {
  2118. sch, err := e.schema.GetSchema(ref.Name)
  2119. if err != nil {
  2120. return nil, "", err
  2121. }
  2122. return sch.Columns, ref.Alias, nil
  2123. }
  2124. }
  2125. for _, ref := range refs {
  2126. if strings.EqualFold(ref.Name, qualifier) {
  2127. sch, err := e.schema.GetSchema(ref.Name)
  2128. if err != nil {
  2129. return nil, "", err
  2130. }
  2131. return sch.Columns, ref.Alias, nil
  2132. }
  2133. }
  2134. return nil, "", fmt.Errorf("no such table or alias: %s", qualifier)
  2135. }
  2136. // addTableAlias adds table-qualified names to a row.
  2137. // normalizeRowBySchema converts float64 values in integer-affinity columns to int64.
  2138. // This is needed because JSON deserialization always produces float64 for numbers.
  2139. func normalizeRowBySchema(row storage.Row, schema *storage.Schema) {
  2140. if schema == nil {
  2141. return
  2142. }
  2143. for _, col := range schema.Columns {
  2144. upper := strings.ToUpper(col.Type)
  2145. isInt := strings.Contains(upper, "INT") || upper == "BOOLEAN" || upper == "BOOL"
  2146. if !isInt {
  2147. continue
  2148. }
  2149. if f, ok := row[col.Name].(float64); ok {
  2150. row[col.Name] = int64(f)
  2151. }
  2152. }
  2153. }
  2154. func (e *Executor) addTableAlias(row storage.Row, alias string) storage.Row {
  2155. result := make(storage.Row)
  2156. for k, v := range row {
  2157. result[k] = v
  2158. // Don't add alias to already-qualified names
  2159. if !strings.Contains(k, ".") {
  2160. result[alias+"."+k] = v
  2161. }
  2162. }
  2163. return result
  2164. }
  2165. // isConflictError reports whether an insert failed because of a primary-key or
  2166. // unique-index conflict.
  2167. func isConflictError(err error) bool {
  2168. if err == nil {
  2169. return false
  2170. }
  2171. msg := err.Error()
  2172. return strings.Contains(msg, "duplicate") || strings.Contains(msg, "UNIQUE constraint failed")
  2173. }
  2174. // findConflictRow returns the first durable row whose target columns match the
  2175. // inserted row.
  2176. func (e *Executor) findConflictRow(table string, row storage.Row, columns []string) (storage.Row, bool) {
  2177. rows, err := e.session.Select(table, func(existing storage.Row) bool {
  2178. for _, col := range columns {
  2179. if compare(existing[col], row[col]) != 0 {
  2180. return false
  2181. }
  2182. }
  2183. return true
  2184. })
  2185. if err != nil || len(rows) == 0 {
  2186. return nil, false
  2187. }
  2188. return rows[0], true
  2189. }
  2190. // applyUpsert implements ON CONFLICT (target) DO UPDATE SET ... for primary-key
  2191. // and unique-index conflicts, resolving excluded.<col> to the new row.
  2192. func (e *Executor) applyUpsert(table string, schema *storage.Schema, existing, row storage.Row, assignments []parser.Assignment) error {
  2193. context := make(storage.Row)
  2194. for k, v := range existing {
  2195. context[k] = v
  2196. context[table+"."+k] = v
  2197. }
  2198. for k, v := range row {
  2199. context["excluded."+k] = v
  2200. }
  2201. pk := fmt.Sprintf("%v", existing[schema.PrimaryKey])
  2202. _, updated, err := e.session.UpdateByPK(table, pk, func(storage.Row) (storage.Row, error) {
  2203. updates := make(storage.Row)
  2204. for _, assignment := range assignments {
  2205. value, evalErr := e.evalExpr(assignment.Value, context)
  2206. if evalErr != nil {
  2207. return nil, evalErr
  2208. }
  2209. updates[assignment.Column] = value
  2210. }
  2211. return updates, nil
  2212. })
  2213. if err != nil {
  2214. return err
  2215. }
  2216. if !updated {
  2217. return fmt.Errorf("ON CONFLICT row disappeared during update")
  2218. }
  2219. return nil
  2220. }
  2221. // executeInsert executes an INSERT statement.
  2222. func (e *Executor) executeInsert(stmt *parser.InsertStmt) (*Result, error) {
  2223. tableName := stmt.Table.Name
  2224. schema, err := e.schema.GetSchema(tableName)
  2225. if err != nil {
  2226. return nil, err
  2227. }
  2228. // INSERT ... SELECT: materialise the SELECT result and bulk-insert.
  2229. if stmt.Select != nil {
  2230. sel, err := e.executeSelect(stmt.Select)
  2231. if err != nil {
  2232. return nil, err
  2233. }
  2234. rows := make([]storage.Row, 0, len(sel.Rows))
  2235. for _, selRow := range sel.Rows {
  2236. row := make(storage.Row)
  2237. if len(stmt.Columns) > 0 {
  2238. for i, col := range stmt.Columns {
  2239. if i < len(selRow) {
  2240. row[col] = selRow[i]
  2241. }
  2242. }
  2243. } else {
  2244. for i, col := range schema.Columns {
  2245. if i < len(selRow) {
  2246. row[col.Name] = selRow[i]
  2247. }
  2248. }
  2249. }
  2250. rows = append(rows, row)
  2251. }
  2252. var count int
  2253. var lastRowID int64
  2254. if e.inTransaction {
  2255. for _, row := range rows {
  2256. rid, err := e.session.InsertWithRowID(tableName, row)
  2257. if err != nil {
  2258. return nil, err
  2259. }
  2260. lastRowID = rid
  2261. count++
  2262. }
  2263. } else {
  2264. count, lastRowID, err = e.session.InsertBulkWithLastRowID(tableName, rows)
  2265. }
  2266. if err != nil {
  2267. return nil, err
  2268. }
  2269. result := NewResult("INSERT")
  2270. result.SetRowCount(count)
  2271. result.SetLastInsertID(lastRowID)
  2272. if count > 0 {
  2273. e.lastInsertRowID = lastRowID
  2274. }
  2275. e.recordChanges(int64(count))
  2276. return result, nil
  2277. }
  2278. count := 0
  2279. var lastRowID int64
  2280. err = e.runDMLAtomic(tableName, func() error {
  2281. for _, values := range stmt.Values {
  2282. row := make(storage.Row)
  2283. if len(stmt.Columns) > 0 {
  2284. // Named columns
  2285. for i, col := range stmt.Columns {
  2286. if i < len(values) {
  2287. val, err := e.evalExpr(values[i], nil)
  2288. if err != nil {
  2289. return err
  2290. }
  2291. row[col] = val
  2292. }
  2293. }
  2294. } else {
  2295. // All columns in order
  2296. for i, col := range schema.Columns {
  2297. if i < len(values) {
  2298. val, err := e.evalExpr(values[i], nil)
  2299. if err != nil {
  2300. return err
  2301. }
  2302. row[col.Name] = val
  2303. }
  2304. }
  2305. }
  2306. // ON CONFLICT is resolved before inserting. A unique-index conflict
  2307. // may only surface at transaction commit, so the insert error is not
  2308. // a reliable signal.
  2309. if stmt.ConflictDoNothing || len(stmt.ConflictUpdate) > 0 {
  2310. columns := stmt.ConflictTarget
  2311. if len(columns) == 0 {
  2312. columns = []string{schema.PrimaryKey}
  2313. }
  2314. if existing, found := e.findConflictRow(tableName, row, columns); found {
  2315. if stmt.ConflictDoNothing {
  2316. continue
  2317. }
  2318. if err := e.applyUpsert(tableName, schema, existing, row, stmt.ConflictUpdate); err != nil {
  2319. return err
  2320. }
  2321. count++
  2322. continue
  2323. }
  2324. }
  2325. rowID, err := e.session.InsertWithRowID(tableName, row)
  2326. if err != nil {
  2327. // Handle conflict based on OnConflict action
  2328. if isConflictError(err) {
  2329. switch stmt.OnConflict {
  2330. case parser.ConflictIgnore:
  2331. // Silently ignore the duplicate
  2332. continue
  2333. case parser.ConflictReplace:
  2334. // Delete existing row and insert new one
  2335. pkValue := row[schema.PrimaryKey]
  2336. if pkValue != nil {
  2337. e.session.Delete(tableName, func(r storage.Row) bool {
  2338. return fmt.Sprintf("%v", r[schema.PrimaryKey]) == fmt.Sprintf("%v", pkValue)
  2339. })
  2340. // Try insert again
  2341. replacedID, replaceErr := e.session.InsertWithRowID(tableName, row)
  2342. if replaceErr != nil {
  2343. return replaceErr
  2344. }
  2345. lastRowID = replacedID
  2346. }
  2347. case parser.ConflictAbort, parser.ConflictFail:
  2348. return err
  2349. case parser.ConflictRollback:
  2350. // In a real implementation, this would rollback the transaction
  2351. return err
  2352. default:
  2353. return err
  2354. }
  2355. } else {
  2356. return err
  2357. }
  2358. } else {
  2359. lastRowID = rowID
  2360. }
  2361. count++
  2362. }
  2363. return nil
  2364. })
  2365. if err != nil {
  2366. return nil, err
  2367. }
  2368. result := NewResult("INSERT")
  2369. result.SetRowCount(count)
  2370. result.SetLastInsertID(lastRowID)
  2371. if count > 0 {
  2372. e.lastInsertRowID = lastRowID
  2373. }
  2374. e.recordChanges(int64(count))
  2375. return result, nil
  2376. }
  2377. // recordChanges updates the session-local changes()/total_changes() state after
  2378. // a successful INSERT/UPDATE/DELETE. total_changes() is a monotonic counter of
  2379. // every completed DML statement (SQLite semantics): it is incremented even when
  2380. // the change is later undone by ROLLBACK or ROLLBACK TO, and is never decremented.
  2381. func (e *Executor) recordChanges(affected int64) {
  2382. e.changes = affected
  2383. e.totalChanges += affected
  2384. }
  2385. // runDMLAtomic runs apply, staging the DML in an implicit transaction when the
  2386. // target table carries a UNIQUE index and no explicit transaction is open, so a
  2387. // statement that fails partway (e.g. a multi-row UPDATE/INSERT that hits a unique
  2388. // violation after earlier rows staged) leaves no durable effects — matching
  2389. // SQLite's statement-level atomicity. It commits on success and rolls back on any
  2390. // error. When already in an explicit transaction, apply runs directly and the
  2391. // surrounding COMMIT/ROLLBACK governs durability.
  2392. func (e *Executor) runDMLAtomic(tableName string, apply func() error) error {
  2393. if e.inTransaction {
  2394. return apply()
  2395. }
  2396. uniq, err := e.table.HasUniqueIndex(tableName)
  2397. if err != nil {
  2398. return err
  2399. }
  2400. if !uniq {
  2401. return apply()
  2402. }
  2403. if err := e.session.Begin(); err != nil {
  2404. return err
  2405. }
  2406. if err := apply(); err != nil {
  2407. _ = e.session.Rollback()
  2408. return err
  2409. }
  2410. return e.session.Commit()
  2411. }
  2412. func containsFold(values []string, target string) bool {
  2413. for _, value := range values {
  2414. if strings.EqualFold(value, target) {
  2415. return true
  2416. }
  2417. }
  2418. return false
  2419. }
  2420. // primeSubqueries pre-executes non-correlated subqueries referenced by a
  2421. // statement expression and stores their results in the per-query subquery cache.
  2422. // It runs before the storage session acquires its lock for a scan-based
  2423. // UPDATE/DELETE. Without this, evaluating a predicate such as
  2424. // "WHERE id IN (SELECT ...)" invokes the session recursively while the session
  2425. // lock is held, which deadlocks. The cache is only consulted when there is no
  2426. // outer row (see evalInExpr), so priming is limited to that same non-correlated
  2427. // case.
  2428. func (e *Executor) primeSubqueries(expr parser.Expr) {
  2429. if expr == nil || e.subqueryCache == nil || e.outerRow != nil {
  2430. return
  2431. }
  2432. prime := func(sub *parser.SelectStmt) {
  2433. if sub == nil {
  2434. return
  2435. }
  2436. if _, ok := e.subqueryCache[sub]; ok {
  2437. return
  2438. }
  2439. if result, err := e.executeSelect(sub); err == nil {
  2440. e.subqueryCache[sub] = result
  2441. }
  2442. }
  2443. var walk func(parser.Expr)
  2444. walk = func(x parser.Expr) {
  2445. if x == nil {
  2446. return
  2447. }
  2448. switch n := x.(type) {
  2449. case *parser.InExpr:
  2450. prime(n.Subquery)
  2451. walk(n.Left)
  2452. for _, v := range n.Values {
  2453. walk(v)
  2454. }
  2455. case *parser.SubqueryExpr:
  2456. prime(n.Query)
  2457. case *parser.ExistsExpr:
  2458. prime(n.Subquery)
  2459. case *parser.BinaryExpr:
  2460. walk(n.Left)
  2461. walk(n.Right)
  2462. case *parser.UnaryExpr:
  2463. walk(n.Operand)
  2464. case *parser.BetweenExpr:
  2465. walk(n.Left)
  2466. walk(n.Low)
  2467. walk(n.High)
  2468. case *parser.LikeExpr:
  2469. walk(n.Left)
  2470. walk(n.Pattern)
  2471. walk(n.Escape)
  2472. case *parser.IsNullExpr:
  2473. walk(n.Left)
  2474. case *parser.CaseExpr:
  2475. walk(n.Operand)
  2476. for _, w := range n.Whens {
  2477. walk(w.Condition)
  2478. walk(w.Result)
  2479. }
  2480. walk(n.Else)
  2481. case *parser.FunctionCall:
  2482. for _, a := range n.Args {
  2483. walk(a)
  2484. }
  2485. case *parser.ParenExpr:
  2486. walk(n.Expr)
  2487. case *parser.CastExpr:
  2488. walk(n.Expr)
  2489. }
  2490. }
  2491. walk(expr)
  2492. }
  2493. // executeUpdate executes an UPDATE statement.
  2494. func (e *Executor) executeUpdate(stmt *parser.UpdateStmt) (*Result, error) {
  2495. tableName := stmt.Table.Name
  2496. schema, err := e.schema.GetSchema(tableName)
  2497. if err != nil {
  2498. return nil, err
  2499. }
  2500. // Build filter
  2501. var filter func(storage.Row) bool
  2502. if stmt.Where != nil {
  2503. filter = func(row storage.Row) bool {
  2504. val, err := e.evalExpr(stmt.Where, row)
  2505. if err != nil {
  2506. return false
  2507. }
  2508. return toBool(val)
  2509. }
  2510. }
  2511. // Use UpdateFunc to evaluate expressions per-row (supports self-referencing like balance = balance + 100)
  2512. updateFn := func(row storage.Row) (storage.Row, error) {
  2513. updates := make(storage.Row)
  2514. for _, assign := range stmt.Set {
  2515. val, err := e.evalExpr(assign.Value, row)
  2516. if err != nil {
  2517. return nil, err
  2518. }
  2519. updates[assign.Column] = val
  2520. }
  2521. return updates, nil
  2522. }
  2523. if stmt.Where != nil {
  2524. column, value, equality := e.extractIndexableCondition(stmt.Where)
  2525. updatesPrimaryKey := false
  2526. for _, assignment := range stmt.Set {
  2527. if strings.EqualFold(assignment.Column, schema.PrimaryKey) {
  2528. updatesPrimaryKey = true
  2529. break
  2530. }
  2531. }
  2532. if equality && strings.EqualFold(column, schema.PrimaryKey) && !updatesPrimaryKey {
  2533. _, updated, err := e.session.UpdateByPK(tableName, fmt.Sprintf("%v", value), updateFn)
  2534. if err != nil {
  2535. return nil, err
  2536. }
  2537. count := 0
  2538. if updated {
  2539. count = 1
  2540. }
  2541. result := NewResult("UPDATE")
  2542. result.SetRowCount(count)
  2543. e.recordChanges(int64(count))
  2544. return result, nil
  2545. }
  2546. }
  2547. // Pre-evaluate subqueries in the predicate and assignments before the
  2548. // scan-based update takes the session lock.
  2549. e.primeSubqueries(stmt.Where)
  2550. for _, assign := range stmt.Set {
  2551. e.primeSubqueries(assign.Value)
  2552. }
  2553. count := 0
  2554. err = e.runDMLAtomic(tableName, func() error {
  2555. n, err := e.session.UpdateFunc(tableName, updateFn, filter)
  2556. if err != nil {
  2557. return err
  2558. }
  2559. count = n
  2560. return nil
  2561. })
  2562. if err != nil {
  2563. return nil, err
  2564. }
  2565. result := NewResult("UPDATE")
  2566. result.SetRowCount(count)
  2567. e.recordChanges(int64(count))
  2568. return result, nil
  2569. }
  2570. // executeDelete executes a DELETE statement.
  2571. func (e *Executor) executeDelete(stmt *parser.DeleteStmt) (*Result, error) {
  2572. tableName := stmt.Table.Name
  2573. schema, err := e.schema.GetSchema(tableName)
  2574. if err != nil {
  2575. return nil, err
  2576. }
  2577. // Build filter
  2578. var filter func(storage.Row) bool
  2579. if stmt.Where != nil {
  2580. filter = func(row storage.Row) bool {
  2581. val, err := e.evalExpr(stmt.Where, row)
  2582. if err != nil {
  2583. return false
  2584. }
  2585. return toBool(val)
  2586. }
  2587. }
  2588. if stmt.Where != nil {
  2589. column, value, equality := e.extractIndexableCondition(stmt.Where)
  2590. if equality && strings.EqualFold(column, schema.PrimaryKey) {
  2591. _, deleted, err := e.session.DeleteByPK(tableName, fmt.Sprintf("%v", value))
  2592. if err != nil {
  2593. return nil, err
  2594. }
  2595. count := 0
  2596. if deleted {
  2597. count = 1
  2598. }
  2599. result := NewResult("DELETE")
  2600. result.SetRowCount(count)
  2601. e.recordChanges(int64(count))
  2602. return result, nil
  2603. }
  2604. }
  2605. // Pre-evaluate subqueries in the predicate before the scan-based delete
  2606. // takes the session lock.
  2607. e.primeSubqueries(stmt.Where)
  2608. count, err := e.session.Delete(tableName, filter)
  2609. if err != nil {
  2610. return nil, err
  2611. }
  2612. result := NewResult("DELETE")
  2613. result.SetRowCount(count)
  2614. e.recordChanges(int64(count))
  2615. return result, nil
  2616. }
  2617. // executeCreateTable executes a CREATE TABLE statement.
  2618. func (e *Executor) executeCreateTable(stmt *parser.CreateTableStmt) (*Result, error) {
  2619. // Check if exists
  2620. if e.schema.TableExists(stmt.Table.Name) {
  2621. if stmt.IfNotExists {
  2622. result := NewResult("CREATE TABLE")
  2623. return result, nil
  2624. }
  2625. return nil, fmt.Errorf("table already exists: %s", stmt.Table.Name)
  2626. }
  2627. // Build schema
  2628. schema := &storage.Schema{
  2629. Name: stmt.Table.Name,
  2630. }
  2631. for _, colDef := range stmt.Columns {
  2632. col := storage.Column{
  2633. Name: colDef.Name,
  2634. Type: colDef.Type.Name,
  2635. Nullable: true,
  2636. }
  2637. for _, constraint := range colDef.Constraints {
  2638. switch constraint.Type {
  2639. case parser.ConstraintPrimaryKey:
  2640. col.PrimaryKey = true
  2641. col.Nullable = false
  2642. schema.PrimaryKey = col.Name
  2643. case parser.ConstraintNotNull:
  2644. col.Nullable = false
  2645. case parser.ConstraintDefault:
  2646. if constraint.Default != nil {
  2647. val, _ := e.evalExpr(constraint.Default, nil)
  2648. col.Default = val
  2649. }
  2650. case parser.ConstraintAutoIncrement:
  2651. schema.AutoIncrement = true
  2652. }
  2653. }
  2654. schema.Columns = append(schema.Columns, col)
  2655. }
  2656. // Handle table-level constraints
  2657. for _, constraint := range stmt.Constraints {
  2658. if constraint.Type == parser.ConstraintPrimaryKey && len(constraint.Columns) > 0 {
  2659. schema.PrimaryKey = constraint.Columns[0]
  2660. for i := range schema.Columns {
  2661. if strings.EqualFold(schema.Columns[i].Name, schema.PrimaryKey) {
  2662. schema.Columns[i].PrimaryKey = true
  2663. schema.Columns[i].Nullable = false
  2664. }
  2665. }
  2666. }
  2667. }
  2668. // Translate inline/table UNIQUE constraints (Gogs/GORM style) into the same
  2669. // scan-validated unique Index metadata that explicit CREATE UNIQUE INDEX
  2670. // produces, so both enforcement and the catalog observe them.
  2671. uniqIndexes := e.uniqueConstraintIndexes(stmt)
  2672. if err := e.schema.CreateTable(schema); err != nil {
  2673. if stmt.IfNotExists && strings.Contains(err.Error(), "table already exists") {
  2674. return NewResult("CREATE TABLE"), nil
  2675. }
  2676. return nil, err
  2677. }
  2678. // Register each materialized unique index. On failure, best-effort cleanup of
  2679. // the table and any indexes already created for it (DDL is not transactional).
  2680. var created []*storage.Index
  2681. for _, idx := range uniqIndexes {
  2682. if err := e.schema.CreateIndex(idx); err != nil {
  2683. for _, c := range created {
  2684. e.schema.DropIndex(c.Name)
  2685. }
  2686. e.schema.DropTable(stmt.Table.Name)
  2687. return nil, err
  2688. }
  2689. created = append(created, idx)
  2690. }
  2691. if err := e.SyncCatalog(); err != nil {
  2692. return nil, err
  2693. }
  2694. result := NewResult("CREATE TABLE")
  2695. return result, nil
  2696. }
  2697. // uniqueConstraintIndexes translates a CREATE TABLE's inline/table UNIQUE
  2698. // constraints into unique Index definitions. Named constraints keep their name;
  2699. // unnamed constraints (column-level UNIQUE, or bare UNIQUE(col,...)) receive a
  2700. // deterministic internal name derived from the table and columns.
  2701. func (e *Executor) uniqueConstraintIndexes(stmt *parser.CreateTableStmt) []*storage.Index {
  2702. var indexes []*storage.Index
  2703. used := make(map[string]bool)
  2704. uniqueName := func(base string, columns []string) string {
  2705. if base == "" {
  2706. base = "uniq_" + strings.ToLower(stmt.Table.Name) + "_" + strings.ToLower(strings.Join(columns, "_"))
  2707. }
  2708. name := base
  2709. for n := 2; used[strings.ToLower(name)]; n++ {
  2710. name = fmt.Sprintf("%s_%d", base, n)
  2711. }
  2712. used[strings.ToLower(name)] = true
  2713. return name
  2714. }
  2715. add := func(name string, columns []string) {
  2716. idx := &storage.Index{Name: uniqueName(name, columns), Table: stmt.Table.Name, Unique: true}
  2717. for _, c := range columns {
  2718. idx.Columns = append(idx.Columns, storage.IndexColumn{Name: c})
  2719. }
  2720. indexes = append(indexes, idx)
  2721. }
  2722. for _, constraint := range stmt.Constraints {
  2723. if constraint.Type == parser.ConstraintUnique && len(constraint.Columns) > 0 {
  2724. add(constraint.Name, constraint.Columns)
  2725. }
  2726. }
  2727. for _, colDef := range stmt.Columns {
  2728. for _, constraint := range colDef.Constraints {
  2729. if constraint.Type == parser.ConstraintUnique {
  2730. add("", []string{colDef.Name})
  2731. }
  2732. }
  2733. }
  2734. return indexes
  2735. }
  2736. // executeDropTable executes a DROP TABLE statement.
  2737. func (e *Executor) executeDropTable(stmt *parser.DropTableStmt) (*Result, error) {
  2738. for _, tableRef := range stmt.Tables {
  2739. if !e.schema.TableExists(tableRef.Name) {
  2740. if stmt.IfExists {
  2741. continue
  2742. }
  2743. return nil, fmt.Errorf("table not found: %s", tableRef.Name)
  2744. }
  2745. // First, drop all indexes associated with this table
  2746. indexes, _ := e.schema.ListTableIndexes(tableRef.Name)
  2747. for _, idx := range indexes {
  2748. // Clear index entries
  2749. columns := make([]string, len(idx.Columns))
  2750. for i, col := range idx.Columns {
  2751. columns[i] = col.Name
  2752. }
  2753. e.session.ClearIndex(idx.Name, tableRef.Name, columns)
  2754. // Drop the index schema
  2755. e.schema.DropIndex(idx.Name)
  2756. }
  2757. // DropTable removes durable rows and schema state together.
  2758. if err := e.schema.DropTable(tableRef.Name); err != nil {
  2759. return nil, err
  2760. }
  2761. e.session.InvalidateCache(tableRef.Name)
  2762. }
  2763. if err := e.SyncCatalog(); err != nil {
  2764. return nil, err
  2765. }
  2766. result := NewResult("DROP TABLE")
  2767. return result, nil
  2768. }
  2769. // executeCreateIndex creates a new index.
  2770. func (e *Executor) executeCreateIndex(stmt *parser.CreateIndexStmt) (*Result, error) {
  2771. // Check if index already exists
  2772. if e.schema.IndexExists(stmt.Name) {
  2773. if stmt.IfNotExists {
  2774. result := NewResult("CREATE INDEX")
  2775. return result, nil
  2776. }
  2777. return nil, fmt.Errorf("index already exists: %s", stmt.Name)
  2778. }
  2779. // Verify table exists
  2780. if !e.schema.TableExists(stmt.Table) {
  2781. return nil, fmt.Errorf("table not found: %s", stmt.Table)
  2782. }
  2783. // Verify columns exist
  2784. schema, err := e.schema.GetSchema(stmt.Table)
  2785. if err != nil {
  2786. return nil, err
  2787. }
  2788. for _, col := range stmt.Columns {
  2789. if _, found := schema.GetColumn(col.Name); !found {
  2790. return nil, fmt.Errorf("column not found: %s", col.Name)
  2791. }
  2792. }
  2793. // Create storage index
  2794. index := &storage.Index{
  2795. Name: stmt.Name,
  2796. Table: stmt.Table,
  2797. Unique: stmt.Unique,
  2798. }
  2799. for _, col := range stmt.Columns {
  2800. index.Columns = append(index.Columns, storage.IndexColumn{
  2801. Name: col.Name,
  2802. Desc: col.Desc,
  2803. })
  2804. }
  2805. if stmt.Unique {
  2806. // Validate existing rows and register the index under the table's
  2807. // exclusive gate so no concurrent writer can insert a conflicting value
  2808. // between validation and registration.
  2809. if err := e.table.CreateUniqueIndex(index); err != nil {
  2810. if stmt.IfNotExists && strings.Contains(err.Error(), "index already exists") {
  2811. return NewResult("CREATE INDEX"), nil
  2812. }
  2813. return nil, err
  2814. }
  2815. } else {
  2816. if err := e.schema.CreateIndex(index); err != nil {
  2817. if stmt.IfNotExists && strings.Contains(err.Error(), "index already exists") {
  2818. return NewResult("CREATE INDEX"), nil
  2819. }
  2820. return nil, err
  2821. }
  2822. }
  2823. // Build index entries for existing rows
  2824. columns := make([]string, len(stmt.Columns))
  2825. for i, col := range stmt.Columns {
  2826. columns[i] = col.Name
  2827. }
  2828. if err := e.session.BuildIndex(stmt.Name, stmt.Table, columns); err != nil {
  2829. // Rollback index creation on failure
  2830. e.schema.DropIndex(stmt.Name)
  2831. return nil, fmt.Errorf("failed to build index: %w", err)
  2832. }
  2833. result := NewResult("CREATE INDEX")
  2834. return result, nil
  2835. }
  2836. // executeDropIndex drops an index.
  2837. func (e *Executor) executeDropIndex(stmt *parser.DropIndexStmt) (*Result, error) {
  2838. if !e.schema.IndexExists(stmt.Name) {
  2839. if stmt.IfExists {
  2840. result := NewResult("DROP INDEX")
  2841. return result, nil
  2842. }
  2843. return nil, fmt.Errorf("index not found: %s", stmt.Name)
  2844. }
  2845. // Get index info to clear entries
  2846. index, err := e.schema.GetIndex(stmt.Name)
  2847. if err == nil && index != nil {
  2848. columns := make([]string, len(index.Columns))
  2849. for i, col := range index.Columns {
  2850. columns[i] = col.Name
  2851. }
  2852. e.session.ClearIndex(stmt.Name, index.Table, columns)
  2853. }
  2854. if err := e.schema.DropIndex(stmt.Name); err != nil {
  2855. return nil, err
  2856. }
  2857. result := NewResult("DROP INDEX")
  2858. return result, nil
  2859. }
  2860. func (e *Executor) executeCreateView(stmt *parser.CreateViewStmt) (*Result, error) {
  2861. name := strings.ToLower(stmt.View.Name)
  2862. if _, exists := e.views[name]; exists {
  2863. if stmt.IfNotExists {
  2864. return NewResult("CREATE VIEW"), nil
  2865. }
  2866. return nil, fmt.Errorf("view already exists: %s", stmt.View.Name)
  2867. }
  2868. e.views[name] = stmt.Select
  2869. // Derive view columns from SELECT list for catalog registration.
  2870. var viewCols []analyzer.ColumnInfo
  2871. hasStar := false
  2872. for _, col := range stmt.Select.Columns {
  2873. if col.Star {
  2874. hasStar = true
  2875. break
  2876. }
  2877. colName := col.Alias
  2878. if colName == "" {
  2879. if ref, ok := col.Expr.(*parser.ColumnRef); ok {
  2880. colName = ref.Column
  2881. } else {
  2882. colName = fmt.Sprintf("col_%d", len(viewCols))
  2883. }
  2884. }
  2885. viewCols = append(viewCols, analyzer.ColumnInfo{
  2886. Name: colName,
  2887. TableName: stmt.View.Name,
  2888. Type: analyzer.TypeAny,
  2889. Nullable: true,
  2890. })
  2891. }
  2892. // For SELECT *, pull columns from the underlying table(s).
  2893. if hasStar && len(stmt.Select.From) > 0 {
  2894. baseName := stmt.Select.From[0].Name
  2895. if schema, err := e.schema.GetSchema(baseName); err == nil {
  2896. for _, c := range schema.Columns {
  2897. viewCols = append(viewCols, analyzer.ColumnInfo{
  2898. Name: c.Name,
  2899. TableName: stmt.View.Name,
  2900. Type: analyzer.TypeAny,
  2901. Nullable: true,
  2902. })
  2903. }
  2904. }
  2905. }
  2906. // Register in catalog so the analyzer accepts SELECT FROM this view.
  2907. e.catalog.CreateTable(&analyzer.TableInfo{ //nolint:errcheck
  2908. Name: stmt.View.Name,
  2909. Columns: viewCols,
  2910. IsView: true,
  2911. })
  2912. return NewResult("CREATE VIEW"), nil
  2913. }
  2914. func (e *Executor) executeDropView(stmt *parser.DropViewStmt) (*Result, error) {
  2915. for _, ref := range stmt.Views {
  2916. name := strings.ToLower(ref.Name)
  2917. if _, exists := e.views[name]; !exists {
  2918. if stmt.IfExists {
  2919. continue
  2920. }
  2921. return nil, fmt.Errorf("view not found: %s", ref.Name)
  2922. }
  2923. delete(e.views, name)
  2924. e.catalog.DropTable(ref.Name) //nolint:errcheck
  2925. }
  2926. return NewResult("DROP VIEW"), nil
  2927. }
  2928. // executeAlterTable executes an ALTER TABLE statement.
  2929. func (e *Executor) executeAlterTable(stmt *parser.AlterTableStmt) (*Result, error) {
  2930. switch action := stmt.Action.(type) {
  2931. case *parser.AddColumnAction:
  2932. return e.executeAlterTableAddColumn(stmt.Table, action)
  2933. case *parser.DropColumnAction:
  2934. return e.executeAlterTableDropColumn(stmt.Table, action)
  2935. case *parser.RenameTableAction:
  2936. return e.executeAlterTableRename(stmt.Table, action)
  2937. case *parser.RenameColumnAction:
  2938. return e.executeAlterTableRenameColumn(stmt.Table, action)
  2939. default:
  2940. return nil, fmt.Errorf("unsupported ALTER TABLE action: %T", action)
  2941. }
  2942. }
  2943. // executeAlterTableAddColumn adds a column to a table.
  2944. func (e *Executor) executeAlterTableAddColumn(table string, action *parser.AddColumnAction) (*Result, error) {
  2945. if action.IfNotExists {
  2946. schema, err := e.schema.GetSchema(table)
  2947. if err != nil {
  2948. return nil, err
  2949. }
  2950. if _, exists := schema.GetColumn(action.Column.Name); exists {
  2951. return NewResult("ALTER TABLE"), nil
  2952. }
  2953. }
  2954. col := storage.Column{
  2955. Name: action.Column.Name,
  2956. Type: action.Column.Type.Name,
  2957. Nullable: true,
  2958. }
  2959. // Process column constraints
  2960. for _, constraint := range action.Column.Constraints {
  2961. switch constraint.Type {
  2962. case parser.ConstraintPrimaryKey:
  2963. col.PrimaryKey = true
  2964. col.Nullable = false
  2965. case parser.ConstraintNotNull:
  2966. col.Nullable = false
  2967. case parser.ConstraintDefault:
  2968. if constraint.Default != nil {
  2969. val, _ := e.evalExpr(constraint.Default, nil)
  2970. col.Default = val
  2971. }
  2972. }
  2973. }
  2974. if err := e.schema.AddColumn(table, col); err != nil {
  2975. if action.IfNotExists && strings.Contains(err.Error(), "column already exists") {
  2976. return NewResult("ALTER TABLE"), nil
  2977. }
  2978. return nil, err
  2979. }
  2980. // Update catalog
  2981. e.SyncCatalog()
  2982. result := NewResult("ALTER TABLE")
  2983. return result, nil
  2984. }
  2985. // executeAlterTableDropColumn drops a column from a table.
  2986. func (e *Executor) executeAlterTableDropColumn(table string, action *parser.DropColumnAction) (*Result, error) {
  2987. if err := e.schema.DropColumn(table, action.Column); err != nil {
  2988. return nil, err
  2989. }
  2990. // Update catalog
  2991. e.SyncCatalog()
  2992. result := NewResult("ALTER TABLE")
  2993. return result, nil
  2994. }
  2995. // executeAlterTableRename renames a table.
  2996. func (e *Executor) executeAlterTableRename(table string, action *parser.RenameTableAction) (*Result, error) {
  2997. if err := e.schema.RenameTable(table, action.NewName); err != nil {
  2998. return nil, err
  2999. }
  3000. e.session.InvalidateCache(table)
  3001. e.session.InvalidateCache(action.NewName)
  3002. // Update catalog
  3003. e.SyncCatalog()
  3004. result := NewResult("ALTER TABLE")
  3005. return result, nil
  3006. }
  3007. // executeAlterTableRenameColumn renames a column.
  3008. func (e *Executor) executeAlterTableRenameColumn(table string, action *parser.RenameColumnAction) (*Result, error) {
  3009. if err := e.schema.RenameColumn(table, action.OldName, action.NewName); err != nil {
  3010. return nil, err
  3011. }
  3012. // Update catalog
  3013. e.SyncCatalog()
  3014. result := NewResult("ALTER TABLE")
  3015. return result, nil
  3016. }
  3017. // Transaction execution methods
  3018. // executeBegin starts a new transaction.
  3019. func (e *Executor) executeBegin(stmt *parser.BeginStmt) (*Result, error) {
  3020. if e.inTransaction {
  3021. return nil, fmt.Errorf("cannot start a transaction within a transaction")
  3022. }
  3023. if err := e.session.Begin(); err != nil {
  3024. return nil, err
  3025. }
  3026. e.inTransaction = true
  3027. e.savepoints = nil
  3028. e.savepointPositions = nil
  3029. result := NewResult("BEGIN")
  3030. return result, nil
  3031. }
  3032. // executeCommit commits the current transaction.
  3033. func (e *Executor) executeCommit(stmt *parser.CommitStmt) (*Result, error) {
  3034. if !e.inTransaction {
  3035. return nil, fmt.Errorf("cannot commit: no transaction in progress")
  3036. }
  3037. if err := e.session.Commit(); err != nil {
  3038. e.inTransaction = false
  3039. e.savepoints = nil
  3040. e.savepointPositions = nil
  3041. return nil, err
  3042. }
  3043. e.inTransaction = false
  3044. e.savepoints = nil
  3045. e.savepointPositions = nil
  3046. result := NewResult("COMMIT")
  3047. return result, nil
  3048. }
  3049. // executeRollback rolls back the current transaction or to a savepoint.
  3050. func (e *Executor) executeRollback(stmt *parser.RollbackStmt) (*Result, error) {
  3051. if !e.inTransaction {
  3052. return nil, fmt.Errorf("cannot rollback: no transaction in progress")
  3053. }
  3054. if stmt.Savepoint != "" {
  3055. // Rollback to savepoint
  3056. return e.rollbackToSavepoint(stmt.Savepoint)
  3057. }
  3058. if err := e.session.Rollback(); err != nil {
  3059. return nil, err
  3060. }
  3061. e.inTransaction = false
  3062. e.savepoints = nil
  3063. e.savepointPositions = nil
  3064. // total_changes() is monotonic (SQLite semantics): it is NOT decremented on
  3065. // rollback. last_insert_rowid() and changes() also intentionally hold.
  3066. result := NewResult("ROLLBACK")
  3067. return result, nil
  3068. }
  3069. // executeSavepoint creates a savepoint.
  3070. func (e *Executor) executeSavepoint(stmt *parser.SavepointStmt) (*Result, error) {
  3071. if !e.inTransaction {
  3072. // SQLite allows SAVEPOINT outside transaction (starts implicit transaction)
  3073. if err := e.session.Begin(); err != nil {
  3074. return nil, err
  3075. }
  3076. e.inTransaction = true
  3077. e.savepoints = nil
  3078. e.savepointPositions = nil
  3079. }
  3080. // Add savepoint marker
  3081. e.savepoints = append(e.savepoints, stmt.Name)
  3082. e.savepointPositions = append(e.savepointPositions, e.session.Snapshot())
  3083. result := NewResult("SAVEPOINT")
  3084. return result, nil
  3085. }
  3086. // executeRelease releases a savepoint.
  3087. func (e *Executor) executeRelease(stmt *parser.ReleaseStmt) (*Result, error) {
  3088. if !e.inTransaction {
  3089. return nil, fmt.Errorf("cannot release savepoint: no transaction in progress")
  3090. }
  3091. // Find and remove the savepoint
  3092. found := false
  3093. for i := len(e.savepoints) - 1; i >= 0; i-- {
  3094. if e.savepoints[i] == stmt.Name {
  3095. e.savepoints = e.savepoints[:i]
  3096. e.savepointPositions = e.savepointPositions[:i]
  3097. found = true
  3098. break
  3099. }
  3100. }
  3101. if !found {
  3102. return nil, fmt.Errorf("no such savepoint: %s", stmt.Name)
  3103. }
  3104. result := NewResult("RELEASE")
  3105. return result, nil
  3106. }
  3107. // executeAttach attaches a database.
  3108. func (e *Executor) executeAttach(stmt *parser.AttachStmt) (*Result, error) {
  3109. // Check if alias already exists
  3110. if _, exists := e.attachedDatabases[stmt.Alias]; exists {
  3111. return nil, fmt.Errorf("database alias already exists: %s", stmt.Alias)
  3112. }
  3113. // Reserved alias check
  3114. if strings.EqualFold(stmt.Alias, "temp") || strings.EqualFold(stmt.Alias, "temporary") {
  3115. return nil, fmt.Errorf("reserved database alias: %s", stmt.Alias)
  3116. }
  3117. // Get the pool from the main schema manager
  3118. pool := e.schema.GetPool()
  3119. // Create new schema and table managers for the attached database
  3120. // In PizzaKV, each database is just a different namespace/prefix
  3121. schema := storage.NewSchemaManager(pool, stmt.FilePath)
  3122. table := storage.NewTableManager(pool, schema, stmt.FilePath)
  3123. // Register the database connection
  3124. e.attachedDatabases[stmt.Alias] = &DatabaseConnection{
  3125. Alias: stmt.Alias,
  3126. Path: stmt.FilePath,
  3127. Schema: schema,
  3128. Table: table,
  3129. }
  3130. // Sync the catalog with the attached database's tables
  3131. tables, _ := schema.ListTables()
  3132. for _, tableName := range tables {
  3133. tSchema, err := schema.GetSchema(tableName)
  3134. if err != nil {
  3135. continue
  3136. }
  3137. // Add with database prefix
  3138. tableInfo := tSchema.ToAnalyzerTableInfo()
  3139. tableInfo.Name = stmt.Alias + "." + tableInfo.Name
  3140. e.catalog.CreateTable(tableInfo)
  3141. }
  3142. result := NewResult("ATTACH")
  3143. return result, nil
  3144. }
  3145. // executeDetach detaches a database.
  3146. func (e *Executor) executeDetach(stmt *parser.DetachStmt) (*Result, error) {
  3147. // Cannot detach main database
  3148. if strings.EqualFold(stmt.Alias, "main") {
  3149. return nil, fmt.Errorf("cannot detach main database")
  3150. }
  3151. // Check if database exists
  3152. if _, exists := e.attachedDatabases[stmt.Alias]; !exists {
  3153. return nil, fmt.Errorf("no such database: %s", stmt.Alias)
  3154. }
  3155. // Remove from attached databases
  3156. delete(e.attachedDatabases, stmt.Alias)
  3157. // Note: We don't remove from catalog as that would be more complex
  3158. // In a production system, we'd need to track which tables belong to which database
  3159. result := NewResult("DETACH")
  3160. return result, nil
  3161. }
  3162. // rollbackToSavepoint rolls back to a specific savepoint.
  3163. func (e *Executor) rollbackToSavepoint(name string) (*Result, error) {
  3164. // Find savepoint index
  3165. savepointIdx := -1
  3166. for i := len(e.savepoints) - 1; i >= 0; i-- {
  3167. if e.savepoints[i] == name {
  3168. savepointIdx = i
  3169. break
  3170. }
  3171. }
  3172. if savepointIdx == -1 {
  3173. return nil, fmt.Errorf("no such savepoint: %s", name)
  3174. }
  3175. logPosition := e.savepointPositions[savepointIdx]
  3176. e.session.RollbackTo(logPosition)
  3177. // Remove savepoints after the target
  3178. e.savepoints = e.savepoints[:savepointIdx+1]
  3179. e.savepointPositions = e.savepointPositions[:savepointIdx+1]
  3180. result := NewResult("ROLLBACK")
  3181. return result, nil
  3182. }
  3183. // RollbackActive rolls back an open transaction, such as when its client
  3184. // disconnects before sending COMMIT or ROLLBACK.
  3185. func (e *Executor) RollbackActive() error {
  3186. if !e.inTransaction {
  3187. return nil
  3188. }
  3189. _, err := e.executeRollback(&parser.RollbackStmt{})
  3190. return err
  3191. }
  3192. // extractIndexableCondition extracts column name and value from a simple equality condition.
  3193. // Returns (column, value, true) if the expression is column = literal.
  3194. func (e *Executor) extractIndexableCondition(expr parser.Expr) (string, interface{}, bool) {
  3195. binExpr, ok := expr.(*parser.BinaryExpr)
  3196. if !ok {
  3197. return "", nil, false
  3198. }
  3199. // Only handle equality for now
  3200. if binExpr.Op != lexer.TokenEq {
  3201. return "", nil, false
  3202. }
  3203. // Check for column = literal pattern
  3204. colRef, leftIsCol := binExpr.Left.(*parser.ColumnRef)
  3205. litExpr, rightIsLit := binExpr.Right.(*parser.LiteralExpr)
  3206. if leftIsCol && rightIsLit {
  3207. val, _ := e.evalLiteral(litExpr)
  3208. return colRef.Column, val, true
  3209. }
  3210. // Check for literal = column pattern
  3211. litExpr, leftIsLit := binExpr.Left.(*parser.LiteralExpr)
  3212. colRef, rightIsCol := binExpr.Right.(*parser.ColumnRef)
  3213. if leftIsLit && rightIsCol {
  3214. val, _ := e.evalLiteral(litExpr)
  3215. return colRef.Column, val, true
  3216. }
  3217. return "", nil, false
  3218. }
  3219. // executePragma executes a PRAGMA statement.
  3220. func (e *Executor) executePragma(stmt *parser.PragmaStmt) (*Result, error) {
  3221. switch stmt.Name {
  3222. case "table_info":
  3223. return e.pragmaTableInfo(stmt.Arg)
  3224. case "table_list":
  3225. return e.pragmaTableList()
  3226. case "database_list":
  3227. return e.pragmaDatabaseList()
  3228. case "version":
  3229. return e.pragmaVersion()
  3230. default:
  3231. return nil, fmt.Errorf("unknown pragma: %s", stmt.Name)
  3232. }
  3233. }
  3234. // pragmaTableInfo returns column information for a table.
  3235. func (e *Executor) pragmaTableInfo(tableName string) (*Result, error) {
  3236. if tableName == "" {
  3237. return nil, fmt.Errorf("table_info requires a table name")
  3238. }
  3239. schema, err := e.schema.GetSchema(tableName)
  3240. if err != nil {
  3241. return nil, err
  3242. }
  3243. result := NewResult("PRAGMA")
  3244. result.AddColumn("cid")
  3245. result.AddColumn("name")
  3246. result.AddColumn("type")
  3247. result.AddColumn("notnull")
  3248. result.AddColumn("dflt_value")
  3249. result.AddColumn("pk")
  3250. for i, col := range schema.Columns {
  3251. notnull := 0
  3252. if !col.Nullable {
  3253. notnull = 1
  3254. }
  3255. pk := 0
  3256. if col.PrimaryKey {
  3257. pk = 1
  3258. }
  3259. result.AddRow(int64(i), col.Name, col.Type, int64(notnull), col.Default, int64(pk))
  3260. }
  3261. return result, nil
  3262. }
  3263. // pragmaTableList returns a list of all tables.
  3264. func (e *Executor) pragmaTableList() (*Result, error) {
  3265. tables, err := e.schema.ListTables()
  3266. if err != nil {
  3267. return nil, err
  3268. }
  3269. result := NewResult("PRAGMA")
  3270. result.AddColumn("schema")
  3271. result.AddColumn("name")
  3272. result.AddColumn("type")
  3273. for _, t := range tables {
  3274. result.AddRow("main", t, "table")
  3275. }
  3276. return result, nil
  3277. }
  3278. // pragmaDatabaseList returns a list of databases.
  3279. func (e *Executor) pragmaDatabaseList() (*Result, error) {
  3280. result := NewResult("PRAGMA")
  3281. result.AddColumn("seq")
  3282. result.AddColumn("name")
  3283. result.AddColumn("file")
  3284. // We only have one database
  3285. result.AddRow(int64(0), "main", "")
  3286. return result, nil
  3287. }
  3288. // pragmaVersion returns the PizzaSQL version.
  3289. func (e *Executor) pragmaVersion() (*Result, error) {
  3290. result := NewResult("PRAGMA")
  3291. result.AddColumn("version")
  3292. result.AddRow(version.String())
  3293. return result, nil
  3294. }
  3295. // executeExplain executes an EXPLAIN statement.
  3296. func (e *Executor) executeExplain(stmt *parser.ExplainStmt) (*Result, error) {
  3297. result := NewResult("EXPLAIN")
  3298. if stmt.QueryPlan {
  3299. // EXPLAIN QUERY PLAN format
  3300. result.AddColumn("id")
  3301. result.AddColumn("parent")
  3302. result.AddColumn("notused")
  3303. result.AddColumn("detail")
  3304. plan := e.generateQueryPlan(stmt.Statement)
  3305. for i, step := range plan {
  3306. result.AddRow(int64(i), int64(0), int64(0), step)
  3307. }
  3308. } else {
  3309. // Simple EXPLAIN format
  3310. result.AddColumn("addr")
  3311. result.AddColumn("opcode")
  3312. result.AddColumn("p1")
  3313. result.AddColumn("p2")
  3314. result.AddColumn("p3")
  3315. result.AddColumn("p4")
  3316. result.AddColumn("p5")
  3317. result.AddColumn("comment")
  3318. ops := e.generateOpcodes(stmt.Statement)
  3319. for i, op := range ops {
  3320. result.AddRow(int64(i), op, int64(0), int64(0), int64(0), "", int64(0), "")
  3321. }
  3322. }
  3323. return result, nil
  3324. }
  3325. // generateQueryPlan generates a simple query plan description.
  3326. func (e *Executor) generateQueryPlan(stmt parser.Statement) []string {
  3327. var plan []string
  3328. switch s := stmt.(type) {
  3329. case *parser.SelectStmt:
  3330. if len(s.From) > 0 {
  3331. plan = append(plan, fmt.Sprintf("SCAN TABLE %s", s.From[0].Name))
  3332. if s.Where != nil {
  3333. plan = append(plan, "FILTER")
  3334. }
  3335. if len(s.OrderBy) > 0 {
  3336. plan = append(plan, "SORT")
  3337. }
  3338. if s.Limit != nil {
  3339. plan = append(plan, "LIMIT")
  3340. }
  3341. } else {
  3342. plan = append(plan, "SCALAR EXPRESSION")
  3343. }
  3344. case *parser.InsertStmt:
  3345. plan = append(plan, fmt.Sprintf("INSERT INTO %s", s.Table.Name))
  3346. case *parser.UpdateStmt:
  3347. plan = append(plan, fmt.Sprintf("SCAN TABLE %s", s.Table.Name))
  3348. plan = append(plan, "UPDATE")
  3349. case *parser.DeleteStmt:
  3350. plan = append(plan, fmt.Sprintf("SCAN TABLE %s", s.Table.Name))
  3351. plan = append(plan, "DELETE")
  3352. default:
  3353. plan = append(plan, "EXECUTE")
  3354. }
  3355. return plan
  3356. }
  3357. // generateOpcodes generates simplified opcodes for EXPLAIN.
  3358. func (e *Executor) generateOpcodes(stmt parser.Statement) []string {
  3359. var ops []string
  3360. switch s := stmt.(type) {
  3361. case *parser.SelectStmt:
  3362. ops = append(ops, "Init")
  3363. if len(s.From) > 0 {
  3364. ops = append(ops, "OpenRead")
  3365. ops = append(ops, "Rewind")
  3366. ops = append(ops, "Column")
  3367. ops = append(ops, "ResultRow")
  3368. ops = append(ops, "Next")
  3369. ops = append(ops, "Close")
  3370. } else {
  3371. ops = append(ops, "Integer")
  3372. ops = append(ops, "ResultRow")
  3373. }
  3374. ops = append(ops, "Halt")
  3375. case *parser.InsertStmt:
  3376. ops = append(ops, "Init")
  3377. ops = append(ops, "OpenWrite")
  3378. ops = append(ops, "NewRowid")
  3379. ops = append(ops, "Insert")
  3380. ops = append(ops, "Close")
  3381. ops = append(ops, "Halt")
  3382. case *parser.UpdateStmt:
  3383. ops = append(ops, "Init")
  3384. ops = append(ops, "OpenWrite")
  3385. ops = append(ops, "Rewind")
  3386. ops = append(ops, "Column")
  3387. ops = append(ops, "Update")
  3388. ops = append(ops, "Next")
  3389. ops = append(ops, "Close")
  3390. ops = append(ops, "Halt")
  3391. case *parser.DeleteStmt:
  3392. ops = append(ops, "Init")
  3393. ops = append(ops, "OpenWrite")
  3394. ops = append(ops, "Rewind")
  3395. ops = append(ops, "Delete")
  3396. ops = append(ops, "Next")
  3397. ops = append(ops, "Close")
  3398. ops = append(ops, "Halt")
  3399. default:
  3400. ops = append(ops, "Init")
  3401. ops = append(ops, "Halt")
  3402. }
  3403. return ops
  3404. }
  3405. // evalExpr evaluates an expression.
  3406. func (e *Executor) evalExpr(expr parser.Expr, row storage.Row) (interface{}, error) {
  3407. if expr == nil {
  3408. return nil, nil
  3409. }
  3410. switch ex := expr.(type) {
  3411. case *parser.LiteralExpr:
  3412. return e.evalLiteral(ex)
  3413. case *parser.ColumnRef:
  3414. return e.evalColumnRef(ex, row)
  3415. case *parser.BinaryExpr:
  3416. return e.evalBinaryExpr(ex, row)
  3417. case *parser.UnaryExpr:
  3418. return e.evalUnaryExpr(ex, row)
  3419. case *parser.FunctionCall:
  3420. return e.evalFunctionCall(ex, row)
  3421. case *parser.ParenExpr:
  3422. return e.evalExpr(ex.Expr, row)
  3423. case *parser.CaseExpr:
  3424. return e.evalCaseExpr(ex, row)
  3425. case *parser.InExpr:
  3426. return e.evalInExpr(ex, row)
  3427. case *parser.BetweenExpr:
  3428. return e.evalBetweenExpr(ex, row)
  3429. case *parser.LikeExpr:
  3430. return e.evalLikeExpr(ex, row)
  3431. case *parser.IsNullExpr:
  3432. return e.evalIsNullExpr(ex, row)
  3433. case *parser.CastExpr:
  3434. return e.evalCastExpr(ex, row)
  3435. case *parser.SubqueryExpr:
  3436. return e.evalSubqueryExpr(ex, row)
  3437. case *parser.ExistsExpr:
  3438. return e.evalExistsExpr(ex, row)
  3439. default:
  3440. return nil, fmt.Errorf("unsupported expression type: %T", expr)
  3441. }
  3442. }
  3443. func (e *Executor) evalLiteral(lit *parser.LiteralExpr) (interface{}, error) {
  3444. switch lit.Type {
  3445. case lexer.TokenNumber:
  3446. // Check for scientific notation (e.g., 1e+06) or decimal point
  3447. if strings.Contains(lit.Value, ".") || strings.ContainsAny(lit.Value, "eE") {
  3448. f, err := strconv.ParseFloat(lit.Value, 64)
  3449. if err != nil {
  3450. return nil, err
  3451. }
  3452. // If it's a whole number (no fractional part), return as int64
  3453. if f == float64(int64(f)) {
  3454. return int64(f), nil
  3455. }
  3456. return f, nil
  3457. }
  3458. return strconv.ParseInt(lit.Value, 10, 64)
  3459. case lexer.TokenString:
  3460. return lit.Value, nil
  3461. case lexer.TokenNULL:
  3462. return nil, nil
  3463. case lexer.TokenTRUE:
  3464. return true, nil
  3465. case lexer.TokenFALSE:
  3466. return false, nil
  3467. default:
  3468. return lit.Value, nil
  3469. }
  3470. }
  3471. func (e *Executor) evalColumnRef(ref *parser.ColumnRef, row storage.Row) (interface{}, error) {
  3472. if row == nil {
  3473. return nil, fmt.Errorf("no row context for column: %s", ref.Column)
  3474. }
  3475. // For qualified column references (table.column):
  3476. //
  3477. // Resolution order:
  3478. // 1. Exact qualified key in outer row ("t1.b" → outer)
  3479. // 2. Case-insensitive qualified in outer row
  3480. // 3. Exact qualified key in current row ("x.b" → inner alias)
  3481. // 4. Case-insensitive qualified in current row
  3482. // 5. Unqualified in outer row — only reached when qualified lookup in current
  3483. // row failed, meaning the qualifier refers to an outer table not the inner
  3484. // alias (e.g. "t1.b" in a subquery "FROM t1 AS x" resolves here).
  3485. // 6. Unqualified in current row (last resort)
  3486. if ref.Table != "" {
  3487. if e.outerRow != nil {
  3488. // Step 1-2: qualified lookup in outer row
  3489. if val, ok := e.outerRow[ref.Table+"."+ref.Column]; ok {
  3490. return val, nil
  3491. }
  3492. for k, v := range e.outerRow {
  3493. if strings.EqualFold(k, ref.Table+"."+ref.Column) {
  3494. return v, nil
  3495. }
  3496. }
  3497. }
  3498. // Step 3-4: qualified lookup in current row
  3499. if val, ok := row[ref.Table+"."+ref.Column]; ok {
  3500. return val, nil
  3501. }
  3502. for k, v := range row {
  3503. if strings.EqualFold(k, ref.Table+"."+ref.Column) {
  3504. return v, nil
  3505. }
  3506. }
  3507. // Step 5: qualified lookup failed in current row — try outer row unqualified.
  3508. // This handles correlated subqueries where the qualifier names an outer table
  3509. // (e.g. "t1.b" when the inner FROM is "t1 AS x", so current row has "x.b"
  3510. // but no "t1.b").
  3511. if e.outerRow != nil {
  3512. if val, ok := e.outerRow[ref.Column]; ok {
  3513. return val, nil
  3514. }
  3515. for k, v := range e.outerRow {
  3516. if strings.EqualFold(k, ref.Column) {
  3517. return v, nil
  3518. }
  3519. }
  3520. }
  3521. }
  3522. // Step 6: unqualified fallback in current row (handles unqualified refs and
  3523. // single-table queries like "SELECT t1.a FROM t1" where rows have plain keys).
  3524. if val, ok := row[ref.Column]; ok {
  3525. return val, nil
  3526. }
  3527. for k, v := range row {
  3528. if strings.EqualFold(k, ref.Column) {
  3529. return v, nil
  3530. }
  3531. }
  3532. // For unqualified refs with an outer row context (ref.Table == "").
  3533. if e.outerRow != nil && ref.Table == "" {
  3534. if val, ok := e.outerRow[ref.Column]; ok {
  3535. return val, nil
  3536. }
  3537. for k, v := range e.outerRow {
  3538. if strings.EqualFold(k, ref.Column) {
  3539. return v, nil
  3540. }
  3541. }
  3542. }
  3543. // Hidden rowid alias (rowid/oid/_rowid_): only used when the row carries no
  3544. // real column of that name. SQLite semantics give an explicit column named
  3545. // oid/rowid/_rowid_ precedence over the hidden rowid alias, so this fallback
  3546. // runs last.
  3547. if storage.IsRowIDColumn(ref.Column) {
  3548. if val, ok := row["_rowid_"]; ok {
  3549. return val, nil
  3550. }
  3551. }
  3552. return nil, nil // Column not found, return NULL
  3553. }
  3554. func (e *Executor) evalBinaryExpr(expr *parser.BinaryExpr, row storage.Row) (interface{}, error) {
  3555. left, err := e.evalExpr(expr.Left, row)
  3556. if err != nil {
  3557. return nil, err
  3558. }
  3559. right, err := e.evalExpr(expr.Right, row)
  3560. if err != nil {
  3561. return nil, err
  3562. }
  3563. switch expr.Op {
  3564. case lexer.TokenPlus:
  3565. if left == nil || right == nil {
  3566. return nil, nil
  3567. }
  3568. if isIntVal(left) && isIntVal(right) {
  3569. return toInt64(left) + toInt64(right), nil
  3570. }
  3571. return toFloat(left) + toFloat(right), nil
  3572. case lexer.TokenMinus:
  3573. if left == nil || right == nil {
  3574. return nil, nil
  3575. }
  3576. if isIntVal(left) && isIntVal(right) {
  3577. return toInt64(left) - toInt64(right), nil
  3578. }
  3579. return toFloat(left) - toFloat(right), nil
  3580. case lexer.TokenStar:
  3581. if left == nil || right == nil {
  3582. return nil, nil
  3583. }
  3584. if isIntVal(left) && isIntVal(right) {
  3585. return toInt64(left) * toInt64(right), nil
  3586. }
  3587. return toFloat(left) * toFloat(right), nil
  3588. case lexer.TokenSlash:
  3589. if left == nil || right == nil {
  3590. return nil, nil
  3591. }
  3592. // Integer division when both operands are integers (truncates toward zero, matching SQLite)
  3593. if isIntVal(left) && isIntVal(right) {
  3594. ri := toInt64(right)
  3595. if ri == 0 {
  3596. return nil, nil
  3597. }
  3598. return toInt64(left) / ri, nil
  3599. }
  3600. r := toFloat(right)
  3601. if r == 0 {
  3602. return nil, nil // Division by zero returns NULL
  3603. }
  3604. return toFloat(left) / r, nil
  3605. case lexer.TokenPercent:
  3606. if left == nil || right == nil {
  3607. return nil, nil
  3608. }
  3609. if isIntVal(left) && isIntVal(right) {
  3610. ri := toInt64(right)
  3611. if ri == 0 {
  3612. return nil, nil
  3613. }
  3614. return toInt64(left) % ri, nil
  3615. }
  3616. return int64(toFloat(left)) % int64(toFloat(right)), nil
  3617. case lexer.TokenEq:
  3618. if left == nil || right == nil {
  3619. return nil, nil
  3620. }
  3621. return compare(left, right) == 0, nil
  3622. case lexer.TokenNeq:
  3623. if left == nil || right == nil {
  3624. return nil, nil
  3625. }
  3626. return compare(left, right) != 0, nil
  3627. case lexer.TokenLt:
  3628. if left == nil || right == nil {
  3629. return nil, nil
  3630. }
  3631. return compare(left, right) < 0, nil
  3632. case lexer.TokenLte:
  3633. if left == nil || right == nil {
  3634. return nil, nil
  3635. }
  3636. return compare(left, right) <= 0, nil
  3637. case lexer.TokenGt:
  3638. if left == nil || right == nil {
  3639. return nil, nil
  3640. }
  3641. return compare(left, right) > 0, nil
  3642. case lexer.TokenGte:
  3643. if left == nil || right == nil {
  3644. return nil, nil
  3645. }
  3646. return compare(left, right) >= 0, nil
  3647. case lexer.TokenAND:
  3648. // Three-value logic: FALSE AND x = FALSE; NULL AND TRUE = NULL; TRUE AND TRUE = TRUE
  3649. if left != nil && !toBool(left) {
  3650. return false, nil
  3651. }
  3652. if right != nil && !toBool(right) {
  3653. return false, nil
  3654. }
  3655. if left == nil || right == nil {
  3656. return nil, nil
  3657. }
  3658. return true, nil
  3659. case lexer.TokenOR:
  3660. // Three-value logic: TRUE OR x = TRUE; NULL OR FALSE = NULL; FALSE OR FALSE = FALSE
  3661. if left != nil && toBool(left) {
  3662. return true, nil
  3663. }
  3664. if right != nil && toBool(right) {
  3665. return true, nil
  3666. }
  3667. if left == nil || right == nil {
  3668. return nil, nil
  3669. }
  3670. return false, nil
  3671. case lexer.TokenConcat:
  3672. return toString(left) + toString(right), nil
  3673. default:
  3674. return nil, fmt.Errorf("unsupported operator: %v", expr.Op)
  3675. }
  3676. }
  3677. // applyBinaryOp applies a binary operator to two already-evaluated values.
  3678. func (e *Executor) applyBinaryOp(op lexer.TokenType, left, right interface{}) (interface{}, error) {
  3679. dummy := &parser.BinaryExpr{Op: op}
  3680. _ = dummy
  3681. switch op {
  3682. case lexer.TokenPlus:
  3683. if left == nil || right == nil {
  3684. return nil, nil
  3685. }
  3686. if isIntVal(left) && isIntVal(right) {
  3687. return toInt64(left) + toInt64(right), nil
  3688. }
  3689. return toFloat(left) + toFloat(right), nil
  3690. case lexer.TokenMinus:
  3691. if left == nil || right == nil {
  3692. return nil, nil
  3693. }
  3694. if isIntVal(left) && isIntVal(right) {
  3695. return toInt64(left) - toInt64(right), nil
  3696. }
  3697. return toFloat(left) - toFloat(right), nil
  3698. case lexer.TokenStar:
  3699. if left == nil || right == nil {
  3700. return nil, nil
  3701. }
  3702. if isIntVal(left) && isIntVal(right) {
  3703. return toInt64(left) * toInt64(right), nil
  3704. }
  3705. return toFloat(left) * toFloat(right), nil
  3706. case lexer.TokenSlash:
  3707. if left == nil || right == nil {
  3708. return nil, nil
  3709. }
  3710. if isIntVal(left) && isIntVal(right) {
  3711. ri := toInt64(right)
  3712. if ri == 0 {
  3713. return nil, nil
  3714. }
  3715. return toInt64(left) / ri, nil
  3716. }
  3717. r := toFloat(right)
  3718. if r == 0 {
  3719. return nil, nil
  3720. }
  3721. return toFloat(left) / r, nil
  3722. case lexer.TokenPercent:
  3723. if left == nil || right == nil {
  3724. return nil, nil
  3725. }
  3726. if isIntVal(left) && isIntVal(right) {
  3727. ri := toInt64(right)
  3728. if ri == 0 {
  3729. return nil, nil
  3730. }
  3731. return toInt64(left) % ri, nil
  3732. }
  3733. return int64(toFloat(left)) % int64(toFloat(right)), nil
  3734. case lexer.TokenEq:
  3735. if left == nil || right == nil {
  3736. return nil, nil
  3737. }
  3738. return compare(left, right) == 0, nil
  3739. case lexer.TokenNeq:
  3740. if left == nil || right == nil {
  3741. return nil, nil
  3742. }
  3743. return compare(left, right) != 0, nil
  3744. case lexer.TokenLt:
  3745. if left == nil || right == nil {
  3746. return nil, nil
  3747. }
  3748. return compare(left, right) < 0, nil
  3749. case lexer.TokenLte:
  3750. if left == nil || right == nil {
  3751. return nil, nil
  3752. }
  3753. return compare(left, right) <= 0, nil
  3754. case lexer.TokenGt:
  3755. if left == nil || right == nil {
  3756. return nil, nil
  3757. }
  3758. return compare(left, right) > 0, nil
  3759. case lexer.TokenGte:
  3760. if left == nil || right == nil {
  3761. return nil, nil
  3762. }
  3763. return compare(left, right) >= 0, nil
  3764. case lexer.TokenAND:
  3765. if left != nil && !toBool(left) {
  3766. return false, nil
  3767. }
  3768. if right != nil && !toBool(right) {
  3769. return false, nil
  3770. }
  3771. if left == nil || right == nil {
  3772. return nil, nil
  3773. }
  3774. return true, nil
  3775. case lexer.TokenOR:
  3776. if left != nil && toBool(left) {
  3777. return true, nil
  3778. }
  3779. if right != nil && toBool(right) {
  3780. return true, nil
  3781. }
  3782. if left == nil || right == nil {
  3783. return nil, nil
  3784. }
  3785. return false, nil
  3786. case lexer.TokenConcat:
  3787. return toString(left) + toString(right), nil
  3788. default:
  3789. return nil, fmt.Errorf("unsupported operator: %v", op)
  3790. }
  3791. }
  3792. // evalBuiltinFunction applies a named scalar function to pre-evaluated args.
  3793. func (e *Executor) evalBuiltinFunction(name string, args []interface{}) (interface{}, error) {
  3794. switch name {
  3795. case "NULLIF":
  3796. if len(args) >= 2 && compare(args[0], args[1]) == 0 {
  3797. return nil, nil
  3798. }
  3799. if len(args) > 0 {
  3800. return args[0], nil
  3801. }
  3802. case "IFNULL", "NVL":
  3803. if len(args) >= 2 {
  3804. if args[0] == nil {
  3805. return args[1], nil
  3806. }
  3807. return args[0], nil
  3808. }
  3809. case "COALESCE":
  3810. for _, a := range args {
  3811. if a != nil {
  3812. return a, nil
  3813. }
  3814. }
  3815. return nil, nil
  3816. case "ABS":
  3817. if len(args) > 0 && args[0] != nil {
  3818. if isIntVal(args[0]) {
  3819. v := toInt64(args[0])
  3820. if v < 0 {
  3821. return -v, nil
  3822. }
  3823. return v, nil
  3824. }
  3825. v := toFloat(args[0])
  3826. if v < 0 {
  3827. return -v, nil
  3828. }
  3829. return v, nil
  3830. }
  3831. case "LENGTH":
  3832. if len(args) > 0 && args[0] != nil {
  3833. return int64(len(fmt.Sprintf("%v", args[0]))), nil
  3834. }
  3835. }
  3836. // Fall back: store pre-evaluated values in row and build column refs.
  3837. row := make(storage.Row, len(args))
  3838. fn := &parser.FunctionCall{Name: name}
  3839. for i, a := range args {
  3840. key := fmt.Sprintf("__arg%d__", i)
  3841. row[key] = a
  3842. fn.Args = append(fn.Args, &parser.ColumnRef{Column: key})
  3843. }
  3844. return e.evalFunctionCall(fn, row)
  3845. }
  3846. func (e *Executor) evalUnaryExpr(expr *parser.UnaryExpr, row storage.Row) (interface{}, error) {
  3847. val, err := e.evalExpr(expr.Operand, row)
  3848. if err != nil {
  3849. return nil, err
  3850. }
  3851. switch expr.Op {
  3852. case lexer.TokenMinus:
  3853. if val == nil {
  3854. return nil, nil
  3855. }
  3856. if isIntVal(val) {
  3857. return -toInt64(val), nil
  3858. }
  3859. return -toFloat(val), nil
  3860. case lexer.TokenPlus:
  3861. if val == nil {
  3862. return nil, nil
  3863. }
  3864. if isIntVal(val) {
  3865. return toInt64(val), nil
  3866. }
  3867. return toFloat(val), nil
  3868. case lexer.TokenNOT:
  3869. if val == nil {
  3870. return nil, nil // NOT NULL = NULL
  3871. }
  3872. return !toBool(val), nil
  3873. default:
  3874. return val, nil
  3875. }
  3876. }
  3877. func (e *Executor) evalFunctionCall(fn *parser.FunctionCall, row storage.Row) (interface{}, error) {
  3878. name := strings.ToUpper(fn.Name)
  3879. // Evaluate arguments
  3880. args := make([]interface{}, len(fn.Args))
  3881. for i, arg := range fn.Args {
  3882. val, err := e.evalExpr(arg, row)
  3883. if err != nil {
  3884. return nil, err
  3885. }
  3886. args[i] = val
  3887. }
  3888. switch name {
  3889. case "UPPER":
  3890. if len(args) > 0 {
  3891. if args[0] == nil {
  3892. return nil, nil // NULL propagation
  3893. }
  3894. return strings.ToUpper(toString(args[0])), nil
  3895. }
  3896. case "LOWER":
  3897. if len(args) > 0 {
  3898. if args[0] == nil {
  3899. return nil, nil // NULL propagation
  3900. }
  3901. return strings.ToLower(toString(args[0])), nil
  3902. }
  3903. case "LENGTH":
  3904. if len(args) > 0 {
  3905. if args[0] == nil {
  3906. return nil, nil // NULL propagation
  3907. }
  3908. return int64(len(toString(args[0]))), nil
  3909. }
  3910. case "ABS":
  3911. if len(args) > 0 {
  3912. if args[0] == nil {
  3913. return nil, nil
  3914. }
  3915. v := toFloat(args[0])
  3916. if v < 0 {
  3917. return -v, nil
  3918. }
  3919. return v, nil
  3920. }
  3921. case "COALESCE":
  3922. for _, arg := range args {
  3923. if arg != nil {
  3924. return arg, nil
  3925. }
  3926. }
  3927. return nil, nil
  3928. case "NULLIF":
  3929. if len(args) >= 2 && compare(args[0], args[1]) == 0 {
  3930. return nil, nil
  3931. }
  3932. if len(args) > 0 {
  3933. return args[0], nil
  3934. }
  3935. case "IFNULL":
  3936. if len(args) >= 2 {
  3937. if args[0] == nil {
  3938. return args[1], nil
  3939. }
  3940. return args[0], nil
  3941. }
  3942. case "TYPEOF":
  3943. if len(args) > 0 {
  3944. switch args[0].(type) {
  3945. case nil:
  3946. return "null", nil
  3947. case int64, int:
  3948. return "integer", nil
  3949. case float64:
  3950. return "real", nil
  3951. case string:
  3952. return "text", nil
  3953. case []byte:
  3954. return "blob", nil
  3955. default:
  3956. return "text", nil
  3957. }
  3958. }
  3959. case "SUBSTR", "SUBSTRING":
  3960. if len(args) >= 2 {
  3961. s := toString(args[0])
  3962. start := int(toFloat(args[1])) - 1 // SQL is 1-indexed
  3963. if start < 0 {
  3964. start = 0
  3965. }
  3966. if start >= len(s) {
  3967. return "", nil
  3968. }
  3969. if len(args) >= 3 {
  3970. length := int(toFloat(args[2]))
  3971. if start+length > len(s) {
  3972. length = len(s) - start
  3973. }
  3974. return s[start : start+length], nil
  3975. }
  3976. return s[start:], nil
  3977. }
  3978. case "TRIM":
  3979. if len(args) > 0 {
  3980. return strings.TrimSpace(toString(args[0])), nil
  3981. }
  3982. case "REPLACE":
  3983. if len(args) >= 3 {
  3984. return strings.ReplaceAll(toString(args[0]), toString(args[1]), toString(args[2])), nil
  3985. }
  3986. // Additional SQLite functions
  3987. case "PRINTF":
  3988. if len(args) > 0 {
  3989. format := toString(args[0])
  3990. fmtArgs := make([]interface{}, len(args)-1)
  3991. for i := 1; i < len(args); i++ {
  3992. fmtArgs[i-1] = args[i]
  3993. }
  3994. return fmt.Sprintf(format, fmtArgs...), nil
  3995. }
  3996. case "HEX":
  3997. if len(args) > 0 {
  3998. s := toString(args[0])
  3999. return strings.ToUpper(fmt.Sprintf("%x", []byte(s))), nil
  4000. }
  4001. case "UNHEX":
  4002. if len(args) > 0 {
  4003. s := toString(args[0])
  4004. var result []byte
  4005. for i := 0; i < len(s)-1; i += 2 {
  4006. var b byte
  4007. fmt.Sscanf(s[i:i+2], "%x", &b)
  4008. result = append(result, b)
  4009. }
  4010. return string(result), nil
  4011. }
  4012. case "RANDOM":
  4013. return rand.Int63(), nil
  4014. case "RANDOMBLOB":
  4015. if len(args) > 0 {
  4016. n := int(toFloat(args[0]))
  4017. if n <= 0 {
  4018. n = 1
  4019. }
  4020. if n > 1000000 {
  4021. n = 1000000
  4022. }
  4023. blob := make([]byte, n)
  4024. rand.Read(blob)
  4025. return string(blob), nil
  4026. }
  4027. case "ZEROBLOB":
  4028. if len(args) > 0 {
  4029. n := int(toFloat(args[0]))
  4030. if n <= 0 {
  4031. n = 1
  4032. }
  4033. if n > 1000000 {
  4034. n = 1000000
  4035. }
  4036. return string(make([]byte, n)), nil
  4037. }
  4038. case "INSTR":
  4039. if len(args) >= 2 {
  4040. s := toString(args[0])
  4041. substr := toString(args[1])
  4042. idx := strings.Index(s, substr)
  4043. if idx < 0 {
  4044. return int64(0), nil
  4045. }
  4046. return int64(idx + 1), nil // SQL is 1-indexed
  4047. }
  4048. case "GLOB":
  4049. if len(args) >= 2 {
  4050. pattern := toString(args[0])
  4051. s := toString(args[1])
  4052. return matchGlob(pattern, s), nil
  4053. }
  4054. case "ROUND":
  4055. if len(args) > 0 {
  4056. v := toFloat(args[0])
  4057. decimals := 0
  4058. if len(args) >= 2 {
  4059. decimals = int(toFloat(args[1]))
  4060. }
  4061. mult := 1.0
  4062. for i := 0; i < decimals; i++ {
  4063. mult *= 10
  4064. }
  4065. return float64(int64(v*mult+0.5)) / mult, nil
  4066. }
  4067. case "MAX":
  4068. if len(args) > 0 {
  4069. max := args[0]
  4070. for _, arg := range args[1:] {
  4071. if compare(arg, max) > 0 {
  4072. max = arg
  4073. }
  4074. }
  4075. return max, nil
  4076. }
  4077. case "MIN":
  4078. if len(args) > 0 {
  4079. min := args[0]
  4080. for _, arg := range args[1:] {
  4081. if compare(arg, min) < 0 {
  4082. min = arg
  4083. }
  4084. }
  4085. return min, nil
  4086. }
  4087. case "CONCAT":
  4088. var result strings.Builder
  4089. for _, arg := range args {
  4090. result.WriteString(toString(arg))
  4091. }
  4092. return result.String(), nil
  4093. // Date/Time functions
  4094. case "DATE":
  4095. return evalDateFunc(args)
  4096. case "TIME":
  4097. return evalTimeFunc(args)
  4098. case "DATETIME":
  4099. return evalDatetimeFunc(args)
  4100. case "JULIANDAY":
  4101. return evalJuliandayFunc(args)
  4102. case "UNIXEPOCH":
  4103. return evalUnixepochFunc(args)
  4104. case "STRFTIME":
  4105. return evalStrftimeFunc(args)
  4106. case "TIMEDIFF":
  4107. return evalTimediffFunc(args)
  4108. case "PIZZASQL_VERSION", "SQLITE_VERSION":
  4109. return version.String(), nil
  4110. case "LAST_INSERT_ROWID":
  4111. return e.lastInsertRowID, nil
  4112. case "CHANGES":
  4113. return e.changes, nil
  4114. case "TOTAL_CHANGES":
  4115. return e.totalChanges, nil
  4116. }
  4117. return nil, nil
  4118. }
  4119. func (e *Executor) evalCaseExpr(expr *parser.CaseExpr, row storage.Row) (interface{}, error) {
  4120. var operand interface{}
  4121. if expr.Operand != nil {
  4122. var err error
  4123. operand, err = e.evalExpr(expr.Operand, row)
  4124. if err != nil {
  4125. return nil, err
  4126. }
  4127. }
  4128. for _, when := range expr.Whens {
  4129. cond, err := e.evalExpr(when.Condition, row)
  4130. if err != nil {
  4131. return nil, err
  4132. }
  4133. var match bool
  4134. if expr.Operand != nil {
  4135. // Simple CASE: CASE operand WHEN val THEN ... — NULL operand matches nothing
  4136. if operand == nil {
  4137. continue
  4138. }
  4139. match = compare(operand, cond) == 0
  4140. } else {
  4141. // Searched CASE: CASE WHEN cond THEN ... — NULL condition is falsy
  4142. match = toBool(cond)
  4143. }
  4144. if match {
  4145. return e.evalExpr(when.Result, row)
  4146. }
  4147. }
  4148. if expr.Else != nil {
  4149. return e.evalExpr(expr.Else, row)
  4150. }
  4151. return nil, nil
  4152. }
  4153. func (e *Executor) evalInExpr(expr *parser.InExpr, row storage.Row) (interface{}, error) {
  4154. left, err := e.evalExpr(expr.Left, row)
  4155. if err != nil {
  4156. return nil, err
  4157. }
  4158. // Handle subquery: IN (SELECT ...)
  4159. if expr.Subquery != nil {
  4160. var result *Result
  4161. var err error
  4162. // Cache non-correlated subquery results for the duration of this query.
  4163. // Safe when outerRow is nil (no outer context that the subquery could reference).
  4164. if e.subqueryCache != nil && e.outerRow == nil {
  4165. if cached, ok := e.subqueryCache[expr.Subquery]; ok {
  4166. result = cached
  4167. } else {
  4168. result, err = e.executeSelect(expr.Subquery)
  4169. if err == nil {
  4170. e.subqueryCache[expr.Subquery] = result
  4171. }
  4172. }
  4173. } else {
  4174. result, err = e.executeSelect(expr.Subquery)
  4175. }
  4176. if err != nil {
  4177. return nil, fmt.Errorf("IN subquery error: %w", err)
  4178. }
  4179. if len(result.Columns) != 1 {
  4180. return nil, fmt.Errorf("subquery in IN must return exactly one column")
  4181. }
  4182. // SQL three-valued logic: if left is NULL → NULL; if any match → true; if any NULL → NULL; else false.
  4183. if left == nil {
  4184. return nil, nil
  4185. }
  4186. sawNull := false
  4187. for _, resultRow := range result.Rows {
  4188. if len(resultRow) == 0 {
  4189. continue
  4190. }
  4191. v := resultRow[0]
  4192. if v == nil {
  4193. sawNull = true
  4194. continue
  4195. }
  4196. if compare(left, v) == 0 {
  4197. if expr.Not {
  4198. return false, nil
  4199. }
  4200. return true, nil
  4201. }
  4202. }
  4203. if sawNull {
  4204. return nil, nil
  4205. }
  4206. if expr.Not {
  4207. return true, nil
  4208. }
  4209. return false, nil
  4210. }
  4211. // Handle value list: IN (1, 2, 3).
  4212. // Empty list: always FALSE (IN) / TRUE (NOT IN), even for NULL.
  4213. if len(expr.Values) == 0 {
  4214. return expr.Not, nil
  4215. }
  4216. // SQL three-valued logic: if left is NULL → NULL; if any match → true/false;
  4217. // if list contains NULL and no match → NULL.
  4218. if left == nil {
  4219. return nil, nil
  4220. }
  4221. sawNull := false
  4222. for _, val := range expr.Values {
  4223. v, err := e.evalExpr(val, row)
  4224. if err != nil {
  4225. return nil, err
  4226. }
  4227. if v == nil {
  4228. sawNull = true
  4229. continue
  4230. }
  4231. if compare(left, v) == 0 {
  4232. if expr.Not {
  4233. return false, nil
  4234. }
  4235. return true, nil
  4236. }
  4237. }
  4238. if sawNull {
  4239. return nil, nil
  4240. }
  4241. if expr.Not {
  4242. return true, nil
  4243. }
  4244. return false, nil
  4245. }
  4246. func (e *Executor) evalBetweenExpr(expr *parser.BetweenExpr, row storage.Row) (interface{}, error) {
  4247. val, err := e.evalExpr(expr.Left, row)
  4248. if err != nil {
  4249. return nil, err
  4250. }
  4251. low, err := e.evalExpr(expr.Low, row)
  4252. if err != nil {
  4253. return nil, err
  4254. }
  4255. high, err := e.evalExpr(expr.High, row)
  4256. if err != nil {
  4257. return nil, err
  4258. }
  4259. if expr.Not {
  4260. // NOT BETWEEN is equivalent to: val < low OR val > high
  4261. // We need to handle NULL using three-valued OR logic:
  4262. // NULL OR TRUE = TRUE
  4263. // NULL OR FALSE = NULL
  4264. // NULL OR NULL = NULL
  4265. var lessThan, greaterThan interface{}
  4266. if val == nil || low == nil {
  4267. lessThan = nil // NULL
  4268. } else {
  4269. lessThan = compare(val, low) < 0
  4270. }
  4271. if val == nil || high == nil {
  4272. greaterThan = nil // NULL
  4273. } else {
  4274. greaterThan = compare(val, high) > 0
  4275. }
  4276. // Implement three-valued OR
  4277. if toBool(lessThan) || toBool(greaterThan) {
  4278. return true, nil
  4279. }
  4280. if lessThan == nil || greaterThan == nil {
  4281. return nil, nil // NULL
  4282. }
  4283. return false, nil
  4284. } else {
  4285. // BETWEEN is equivalent to: val >= low AND val <= high
  4286. // Three-value logic: if val < low → FALSE (regardless of high); if val >= low and high is NULL → NULL
  4287. if val == nil {
  4288. return nil, nil
  4289. }
  4290. // x BETWEEN a AND b = (x >= a) AND (x <= b)
  4291. // NULL AND FALSE = FALSE; NULL AND TRUE = NULL
  4292. if low == nil {
  4293. // x >= NULL = NULL; check upper bound for early FALSE
  4294. if high != nil && compare(val, high) > 0 {
  4295. return false, nil // NULL AND FALSE = FALSE
  4296. }
  4297. return nil, nil // NULL AND TRUE/NULL = NULL
  4298. }
  4299. if compare(val, low) < 0 {
  4300. return false, nil // val < low → FALSE AND anything = FALSE
  4301. }
  4302. if high == nil {
  4303. return nil, nil // TRUE AND NULL = NULL
  4304. }
  4305. return compare(val, high) <= 0, nil
  4306. }
  4307. }
  4308. func (e *Executor) evalLikeExpr(expr *parser.LikeExpr, row storage.Row) (interface{}, error) {
  4309. val, err := e.evalExpr(expr.Left, row)
  4310. if err != nil {
  4311. return nil, err
  4312. }
  4313. pattern, err := e.evalExpr(expr.Pattern, row)
  4314. if err != nil {
  4315. return nil, err
  4316. }
  4317. s := toString(val)
  4318. p := toString(pattern)
  4319. // Convert SQL LIKE pattern to simple matching
  4320. // % matches any sequence, _ matches single character
  4321. matched := matchLike(s, p)
  4322. if expr.Not {
  4323. return !matched, nil
  4324. }
  4325. return matched, nil
  4326. }
  4327. func (e *Executor) evalIsNullExpr(expr *parser.IsNullExpr, row storage.Row) (interface{}, error) {
  4328. val, err := e.evalExpr(expr.Left, row)
  4329. if err != nil {
  4330. return nil, err
  4331. }
  4332. isNull := val == nil
  4333. if expr.Not {
  4334. return !isNull, nil
  4335. }
  4336. return isNull, nil
  4337. }
  4338. func (e *Executor) evalCastExpr(expr *parser.CastExpr, row storage.Row) (interface{}, error) {
  4339. val, err := e.evalExpr(expr.Expr, row)
  4340. if err != nil {
  4341. return nil, err
  4342. }
  4343. if val == nil {
  4344. return nil, nil // CAST(NULL AS any) = NULL
  4345. }
  4346. typeName := strings.ToUpper(expr.Type.Name)
  4347. switch {
  4348. case strings.Contains(typeName, "INT"):
  4349. return int64(toFloat(val)), nil
  4350. case strings.Contains(typeName, "REAL"), strings.Contains(typeName, "FLOAT"), strings.Contains(typeName, "DOUBLE"):
  4351. return toFloat(val), nil
  4352. case strings.Contains(typeName, "TEXT"), strings.Contains(typeName, "CHAR"):
  4353. return toString(val), nil
  4354. default:
  4355. return val, nil
  4356. }
  4357. }
  4358. // evalSubqueryExpr executes a scalar subquery and returns its value.
  4359. // A scalar subquery must return exactly one column. It returns:
  4360. // - The single value if the subquery returns one row
  4361. // - NULL if the subquery returns no rows
  4362. // - Error if the subquery returns more than one row (for strict SQL compliance)
  4363. func (e *Executor) evalSubqueryExpr(expr *parser.SubqueryExpr, row storage.Row) (interface{}, error) {
  4364. if row != nil && e.correlatedAggCache != nil {
  4365. if val, ok, err := e.evalDecorrelatedAggSubquery(expr.Query, row); ok || err != nil {
  4366. return val, err
  4367. }
  4368. }
  4369. // Save and set outer row context for correlated subqueries
  4370. savedOuter := e.outerRow
  4371. e.outerRow = row
  4372. defer func() { e.outerRow = savedOuter }()
  4373. // Execute the subquery
  4374. result, err := e.executeSelect(expr.Query)
  4375. if err != nil {
  4376. return nil, fmt.Errorf("subquery error: %w", err)
  4377. }
  4378. // Check for empty result
  4379. if result.RowCount == 0 {
  4380. return nil, nil // Return NULL for empty subquery
  4381. }
  4382. // Check column count
  4383. if len(result.Columns) == 0 {
  4384. return nil, fmt.Errorf("subquery must return at least one column")
  4385. }
  4386. // For scalar subquery, return first column of first row
  4387. // Note: Strict SQL would error if more than one row is returned
  4388. // but we follow SQLite behavior which just returns the first value
  4389. if len(result.Rows) > 0 && len(result.Rows[0]) > 0 {
  4390. return result.Rows[0][0], nil
  4391. }
  4392. return nil, nil
  4393. }
  4394. func (e *Executor) evalDecorrelatedAggSubquery(query *parser.SelectStmt, outerRow storage.Row) (interface{}, bool, error) {
  4395. spec, ok := e.correlatedAggSpec(query)
  4396. if !ok {
  4397. return nil, false, nil
  4398. }
  4399. outerVal, err := e.evalExpr(spec.outerKey, outerRow)
  4400. if err != nil {
  4401. return nil, true, err
  4402. }
  4403. cache, exists := e.correlatedAggCache[query]
  4404. if !exists {
  4405. cache, err = e.buildCorrelatedAggCache(query, spec)
  4406. if err != nil {
  4407. return nil, true, err
  4408. }
  4409. e.correlatedAggCache[query] = cache
  4410. }
  4411. if outerVal == nil {
  4412. return cache.defaultValue, true, nil
  4413. }
  4414. if val, exists := cache.values[fmt.Sprintf("%v", outerVal)]; exists {
  4415. return val, true, nil
  4416. }
  4417. return cache.defaultValue, true, nil
  4418. }
  4419. func (e *Executor) correlatedAggSpec(query *parser.SelectStmt) (correlatedAggSpec, bool) {
  4420. if query == nil ||
  4421. query.Compound != nil ||
  4422. len(query.Columns) != 1 ||
  4423. len(query.From) == 0 ||
  4424. query.Where == nil ||
  4425. len(query.GroupBy) > 0 ||
  4426. query.Having != nil ||
  4427. query.Limit != nil ||
  4428. query.Offset != nil {
  4429. return correlatedAggSpec{}, false
  4430. }
  4431. if query.Columns[0].Star {
  4432. return correlatedAggSpec{}, false
  4433. }
  4434. agg, ok := query.Columns[0].Expr.(*parser.FunctionCall)
  4435. if !ok {
  4436. return correlatedAggSpec{}, false
  4437. }
  4438. switch strings.ToUpper(agg.Name) {
  4439. case "COUNT", "SUM", "AVG", "MIN", "MAX":
  4440. default:
  4441. return correlatedAggSpec{}, false
  4442. }
  4443. innerAliases := collectFromAliases(query.From)
  4444. bin, ok := query.Where.(*parser.BinaryExpr)
  4445. if !ok || bin.Op != lexer.TokenEq {
  4446. return correlatedAggSpec{}, false
  4447. }
  4448. leftRef, leftIsRef := bin.Left.(*parser.ColumnRef)
  4449. rightRef, rightIsRef := bin.Right.(*parser.ColumnRef)
  4450. if !leftIsRef || !rightIsRef {
  4451. return correlatedAggSpec{}, false
  4452. }
  4453. leftInner := refBelongsToAliases(leftRef, innerAliases)
  4454. rightInner := refBelongsToAliases(rightRef, innerAliases)
  4455. if leftInner == rightInner {
  4456. return correlatedAggSpec{}, false
  4457. }
  4458. if leftInner {
  4459. return correlatedAggSpec{innerKey: leftRef, outerKey: rightRef, aggExpr: agg}, true
  4460. }
  4461. return correlatedAggSpec{innerKey: rightRef, outerKey: leftRef, aggExpr: agg}, true
  4462. }
  4463. func (e *Executor) buildCorrelatedAggCache(query *parser.SelectStmt, spec correlatedAggSpec) (*correlatedAggCache, error) {
  4464. grouped := *query
  4465. grouped.Where = nil
  4466. grouped.GroupBy = []parser.Expr{spec.innerKey}
  4467. grouped.Having = nil
  4468. grouped.OrderBy = nil
  4469. grouped.Limit = nil
  4470. grouped.Offset = nil
  4471. grouped.Columns = []parser.SelectColumn{
  4472. {Expr: spec.innerKey, Alias: "__corr_key"},
  4473. {Expr: spec.aggExpr, Alias: "__corr_value"},
  4474. }
  4475. savedOuter := e.outerRow
  4476. e.outerRow = nil
  4477. result, err := e.executeSelect(&grouped)
  4478. e.outerRow = savedOuter
  4479. if err != nil {
  4480. return nil, fmt.Errorf("decorrelated aggregate subquery error: %w", err)
  4481. }
  4482. cache := &correlatedAggCache{
  4483. values: make(map[string]interface{}, len(result.Rows)),
  4484. defaultValue: correlatedAggDefault(spec.aggExpr),
  4485. }
  4486. for _, row := range result.Rows {
  4487. if len(row) < 2 || row[0] == nil {
  4488. continue
  4489. }
  4490. cache.values[fmt.Sprintf("%v", row[0])] = row[1]
  4491. }
  4492. return cache, nil
  4493. }
  4494. func correlatedAggDefault(expr parser.Expr) interface{} {
  4495. if fn, ok := expr.(*parser.FunctionCall); ok && strings.EqualFold(fn.Name, "COUNT") {
  4496. return int64(0)
  4497. }
  4498. return nil
  4499. }
  4500. func collectFromAliases(from []parser.TableRef) map[string]struct{} {
  4501. aliases := make(map[string]struct{})
  4502. var addRef func(parser.TableRef)
  4503. addRef = func(ref parser.TableRef) {
  4504. if ref.Name != "" {
  4505. aliases[strings.ToLower(ref.Name)] = struct{}{}
  4506. }
  4507. if ref.Alias != "" {
  4508. aliases[strings.ToLower(ref.Alias)] = struct{}{}
  4509. }
  4510. if ref.Join != nil && ref.Join.Table != nil {
  4511. addRef(*ref.Join.Table)
  4512. }
  4513. }
  4514. for _, ref := range from {
  4515. addRef(ref)
  4516. }
  4517. return aliases
  4518. }
  4519. func refBelongsToAliases(ref *parser.ColumnRef, aliases map[string]struct{}) bool {
  4520. if ref == nil || ref.Table == "" {
  4521. return false
  4522. }
  4523. _, ok := aliases[strings.ToLower(ref.Table)]
  4524. return ok
  4525. }
  4526. // evalExistsExpr evaluates an EXISTS expression.
  4527. // Returns true if the subquery returns at least one row, false otherwise.
  4528. func (e *Executor) evalExistsExpr(expr *parser.ExistsExpr, row storage.Row) (interface{}, error) {
  4529. // Save and set outer row context for correlated subqueries
  4530. savedOuter := e.outerRow
  4531. e.outerRow = row
  4532. defer func() { e.outerRow = savedOuter }()
  4533. // Execute the subquery
  4534. result, err := e.executeSelect(expr.Subquery)
  4535. if err != nil {
  4536. return nil, fmt.Errorf("EXISTS subquery error: %w", err)
  4537. }
  4538. // EXISTS returns true if any rows are returned
  4539. return len(result.Rows) > 0, nil
  4540. }
  4541. // evalAggregateExpr evaluates an aggregate expression over multiple rows.
  4542. func (e *Executor) evalAggregateExpr(expr parser.Expr, rows []storage.Row) (interface{}, error) {
  4543. fn, ok := expr.(*parser.FunctionCall)
  4544. if !ok {
  4545. // Not a function call - could be a binary expression with aggregates inside
  4546. // Evaluate it with the aggregate evaluation context
  4547. return e.evalExprWithAggregates(expr, rows)
  4548. }
  4549. name := strings.ToUpper(fn.Name)
  4550. switch name {
  4551. case "COUNT":
  4552. if fn.Star {
  4553. return int64(len(rows)), nil
  4554. }
  4555. if fn.Distinct {
  4556. seen := make(map[interface{}]struct{})
  4557. for _, row := range rows {
  4558. if len(fn.Args) > 0 {
  4559. val, _ := e.evalExpr(fn.Args[0], row)
  4560. if val != nil {
  4561. seen[val] = struct{}{}
  4562. }
  4563. }
  4564. }
  4565. return int64(len(seen)), nil
  4566. }
  4567. count := int64(0)
  4568. for _, row := range rows {
  4569. if len(fn.Args) > 0 {
  4570. val, _ := e.evalExpr(fn.Args[0], row)
  4571. if val != nil {
  4572. count++
  4573. }
  4574. }
  4575. }
  4576. return count, nil
  4577. case "SUM":
  4578. var sumInt int64
  4579. var sumFloat float64
  4580. allInt := true
  4581. hasValues := false
  4582. var seen map[interface{}]struct{}
  4583. if fn.Distinct {
  4584. seen = make(map[interface{}]struct{})
  4585. }
  4586. for _, row := range rows {
  4587. if len(fn.Args) > 0 {
  4588. val, _ := e.evalExpr(fn.Args[0], row)
  4589. if val != nil {
  4590. if fn.Distinct {
  4591. key := fmt.Sprintf("%v", val)
  4592. if _, exists := seen[key]; exists {
  4593. continue
  4594. }
  4595. seen[key] = struct{}{}
  4596. }
  4597. if isIntVal(val) {
  4598. sumInt += toInt64(val)
  4599. } else {
  4600. allInt = false
  4601. sumFloat += toFloat(val)
  4602. }
  4603. hasValues = true
  4604. }
  4605. }
  4606. }
  4607. if !hasValues {
  4608. return nil, nil
  4609. }
  4610. if allInt {
  4611. return sumInt, nil
  4612. }
  4613. return sumFloat + float64(sumInt), nil
  4614. case "AVG":
  4615. var sum float64
  4616. count := 0
  4617. for _, row := range rows {
  4618. if len(fn.Args) > 0 {
  4619. val, _ := e.evalExpr(fn.Args[0], row)
  4620. if val != nil {
  4621. sum += toFloat(val)
  4622. count++
  4623. }
  4624. }
  4625. }
  4626. if count == 0 {
  4627. return nil, nil
  4628. }
  4629. return sum / float64(count), nil
  4630. case "MIN":
  4631. var min interface{}
  4632. for _, row := range rows {
  4633. if len(fn.Args) > 0 {
  4634. val, _ := e.evalExpr(fn.Args[0], row)
  4635. if val != nil && (min == nil || compare(val, min) < 0) {
  4636. min = val
  4637. }
  4638. }
  4639. }
  4640. return min, nil
  4641. case "MAX":
  4642. var max interface{}
  4643. for _, row := range rows {
  4644. if len(fn.Args) > 0 {
  4645. val, _ := e.evalExpr(fn.Args[0], row)
  4646. if val != nil && (max == nil || compare(val, max) > 0) {
  4647. max = val
  4648. }
  4649. }
  4650. }
  4651. return max, nil
  4652. default:
  4653. // Non-aggregate scalar function: evaluate args through aggregate context
  4654. // (so COUNT/MIN/etc. inside NULLIF/COALESCE work correctly).
  4655. return e.evalExprWithAggregates(expr, rows)
  4656. }
  4657. }
  4658. // evalExprWithAggregates evaluates an expression that may contain aggregate functions
  4659. func (e *Executor) evalExprWithAggregates(expr parser.Expr, rows []storage.Row) (interface{}, error) {
  4660. switch ex := expr.(type) {
  4661. case *parser.BinaryExpr:
  4662. left, err := e.evalExprWithAggregates(ex.Left, rows)
  4663. if err != nil {
  4664. return nil, err
  4665. }
  4666. right, err := e.evalExprWithAggregates(ex.Right, rows)
  4667. if err != nil {
  4668. return nil, err
  4669. }
  4670. // Use the same logic as evalBinaryExpr to preserve integer semantics.
  4671. combined := &parser.BinaryExpr{Op: ex.Op}
  4672. return e.applyBinaryOp(combined.Op, left, right)
  4673. case *parser.FunctionCall:
  4674. name := strings.ToUpper(ex.Name)
  4675. switch name {
  4676. case "COUNT", "SUM", "AVG", "MIN", "MAX", "TOTAL", "GROUP_CONCAT":
  4677. return e.evalAggregateExpr(expr, rows)
  4678. default:
  4679. // Non-aggregate: evaluate each arg with aggregate context, then apply scalar.
  4680. args := make([]interface{}, len(ex.Args))
  4681. for i, arg := range ex.Args {
  4682. v, err := e.evalExprWithAggregates(arg, rows)
  4683. if err != nil {
  4684. return nil, err
  4685. }
  4686. args[i] = v
  4687. }
  4688. return e.evalBuiltinFunction(name, args)
  4689. }
  4690. case *parser.ParenExpr:
  4691. return e.evalExprWithAggregates(ex.Expr, rows)
  4692. case *parser.UnaryExpr:
  4693. operand, err := e.evalExprWithAggregates(ex.Operand, rows)
  4694. if err != nil {
  4695. return nil, err
  4696. }
  4697. switch ex.Op {
  4698. case lexer.TokenPlus:
  4699. return operand, nil
  4700. case lexer.TokenMinus:
  4701. if operand == nil {
  4702. return nil, nil
  4703. }
  4704. if isIntVal(operand) {
  4705. return -toInt64(operand), nil
  4706. }
  4707. return -toFloat(operand), nil
  4708. case lexer.TokenNOT:
  4709. if operand == nil {
  4710. return nil, nil // NOT NULL = NULL
  4711. }
  4712. return !toBool(operand), nil
  4713. default:
  4714. return nil, fmt.Errorf("unsupported unary operator: %v", ex.Op)
  4715. }
  4716. case *parser.CastExpr:
  4717. // Evaluate inner expression with aggregate context, then apply cast.
  4718. val, err := e.evalExprWithAggregates(ex.Expr, rows)
  4719. if err != nil {
  4720. return nil, err
  4721. }
  4722. if val == nil {
  4723. return nil, nil
  4724. }
  4725. switch strings.ToUpper(ex.Type.Name) {
  4726. case "INTEGER", "INT", "BIGINT", "SMALLINT", "TINYINT", "SIGNED":
  4727. if isIntVal(val) {
  4728. return toInt64(val), nil
  4729. }
  4730. return int64(toFloat(val)), nil
  4731. case "REAL", "FLOAT", "DOUBLE", "NUMERIC", "DECIMAL":
  4732. return toFloat(val), nil
  4733. case "TEXT", "VARCHAR", "CHAR", "STRING":
  4734. return fmt.Sprintf("%v", val), nil
  4735. }
  4736. return val, nil
  4737. case *parser.CaseExpr:
  4738. var operand interface{}
  4739. if ex.Operand != nil {
  4740. operand, _ = e.evalExprWithAggregates(ex.Operand, rows)
  4741. }
  4742. for _, when := range ex.Whens {
  4743. condVal, _ := e.evalExprWithAggregates(when.Condition, rows)
  4744. var matched bool
  4745. if ex.Operand != nil {
  4746. matched = operand != nil && condVal != nil && compare(operand, condVal) == 0
  4747. } else {
  4748. matched = toBool(condVal)
  4749. }
  4750. if matched {
  4751. return e.evalExprWithAggregates(when.Result, rows)
  4752. }
  4753. }
  4754. if ex.Else != nil {
  4755. return e.evalExprWithAggregates(ex.Else, rows)
  4756. }
  4757. return nil, nil
  4758. case *parser.IsNullExpr:
  4759. val, err := e.evalExprWithAggregates(ex.Left, rows)
  4760. if err != nil {
  4761. return nil, err
  4762. }
  4763. isNull := val == nil
  4764. if ex.Not {
  4765. return !isNull, nil
  4766. }
  4767. return isNull, nil
  4768. case *parser.BetweenExpr:
  4769. val, err := e.evalExprWithAggregates(ex.Left, rows)
  4770. if err != nil {
  4771. return nil, err
  4772. }
  4773. low, err := e.evalExprWithAggregates(ex.Low, rows)
  4774. if err != nil {
  4775. return nil, err
  4776. }
  4777. high, err := e.evalExprWithAggregates(ex.High, rows)
  4778. if err != nil {
  4779. return nil, err
  4780. }
  4781. if ex.Not {
  4782. // NOT BETWEEN: val < low OR val > high
  4783. var lessThan, greaterThan interface{}
  4784. if val == nil || low == nil {
  4785. lessThan = nil
  4786. } else {
  4787. lessThan = compare(val, low) < 0
  4788. }
  4789. if val == nil || high == nil {
  4790. greaterThan = nil
  4791. } else {
  4792. greaterThan = compare(val, high) > 0
  4793. }
  4794. // Three-valued OR
  4795. if toBool(lessThan) || toBool(greaterThan) {
  4796. return true, nil
  4797. }
  4798. if lessThan == nil || greaterThan == nil {
  4799. return nil, nil
  4800. }
  4801. return false, nil
  4802. } else {
  4803. // BETWEEN: val >= low AND val <= high
  4804. if val == nil {
  4805. return nil, nil
  4806. }
  4807. if low == nil {
  4808. if high != nil && compare(val, high) > 0 {
  4809. return false, nil
  4810. }
  4811. return nil, nil
  4812. }
  4813. if compare(val, low) < 0 {
  4814. return false, nil
  4815. }
  4816. if high == nil {
  4817. return nil, nil
  4818. }
  4819. return compare(val, high) <= 0, nil
  4820. }
  4821. case *parser.InExpr:
  4822. left, err := e.evalExprWithAggregates(ex.Left, rows)
  4823. if err != nil {
  4824. return nil, err
  4825. }
  4826. // Handle subquery
  4827. if ex.Subquery != nil {
  4828. var result *Result
  4829. var err error
  4830. if e.subqueryCache != nil && e.outerRow == nil {
  4831. if cached, ok := e.subqueryCache[ex.Subquery]; ok {
  4832. result = cached
  4833. } else {
  4834. result, err = e.executeSelect(ex.Subquery)
  4835. if err == nil {
  4836. e.subqueryCache[ex.Subquery] = result
  4837. }
  4838. }
  4839. } else {
  4840. result, err = e.executeSelect(ex.Subquery)
  4841. }
  4842. if err != nil {
  4843. return nil, fmt.Errorf("IN subquery error: %w", err)
  4844. }
  4845. if len(result.Columns) != 1 {
  4846. return nil, fmt.Errorf("subquery in IN must return exactly one column")
  4847. }
  4848. if left == nil {
  4849. return nil, nil
  4850. }
  4851. sawNull := false
  4852. for _, resultRow := range result.Rows {
  4853. if len(resultRow) == 0 {
  4854. continue
  4855. }
  4856. v := resultRow[0]
  4857. if v == nil {
  4858. sawNull = true
  4859. continue
  4860. }
  4861. if compare(left, v) == 0 {
  4862. if ex.Not {
  4863. return false, nil
  4864. }
  4865. return true, nil
  4866. }
  4867. }
  4868. if sawNull {
  4869. return nil, nil
  4870. }
  4871. if ex.Not {
  4872. return true, nil
  4873. }
  4874. return false, nil
  4875. }
  4876. // Handle value list
  4877. if len(ex.Values) == 0 {
  4878. return ex.Not, nil
  4879. }
  4880. if left == nil {
  4881. return nil, nil
  4882. }
  4883. sawNull := false
  4884. for _, val := range ex.Values {
  4885. v, err := e.evalExprWithAggregates(val, rows)
  4886. if err != nil {
  4887. return nil, err
  4888. }
  4889. if v == nil {
  4890. sawNull = true
  4891. continue
  4892. }
  4893. if compare(left, v) == 0 {
  4894. if ex.Not {
  4895. return false, nil
  4896. }
  4897. return true, nil
  4898. }
  4899. }
  4900. if sawNull {
  4901. return nil, nil
  4902. }
  4903. if ex.Not {
  4904. return true, nil
  4905. }
  4906. return false, nil
  4907. default:
  4908. // Literals and non-aggregate expressions.
  4909. if len(rows) > 0 {
  4910. return e.evalExpr(expr, rows[0])
  4911. }
  4912. return e.evalExpr(expr, storage.Row{})
  4913. }
  4914. }
  4915. // Helper functions
  4916. func (e *Executor) getSelectColumns(stmt *parser.SelectStmt, schema *storage.Schema) []string {
  4917. var columns []string
  4918. for _, col := range stmt.Columns {
  4919. if col.Star {
  4920. for _, c := range schema.Columns {
  4921. columns = append(columns, c.Name)
  4922. }
  4923. } else if col.Alias != "" {
  4924. columns = append(columns, col.Alias)
  4925. } else if ref, ok := col.Expr.(*parser.ColumnRef); ok {
  4926. columns = append(columns, ref.Column)
  4927. } else {
  4928. columns = append(columns, fmt.Sprintf("column%d", len(columns)+1))
  4929. }
  4930. }
  4931. return columns
  4932. }
  4933. func (e *Executor) hasAggregates(columns []parser.SelectColumn) bool {
  4934. for _, col := range columns {
  4935. if e.isAggregate(col.Expr) {
  4936. return true
  4937. }
  4938. }
  4939. return false
  4940. }
  4941. func (e *Executor) isAggregate(expr parser.Expr) bool {
  4942. if _, ok := expr.(*parser.WindowExpr); ok {
  4943. // Window functions are computed after grouping; they are not aggregates.
  4944. return false
  4945. }
  4946. if fn, ok := expr.(*parser.FunctionCall); ok {
  4947. name := strings.ToUpper(fn.Name)
  4948. switch name {
  4949. case "COUNT", "SUM", "AVG", "MIN", "MAX", "TOTAL", "GROUP_CONCAT":
  4950. return true
  4951. }
  4952. // Non-aggregate function: check if any arg contains an aggregate.
  4953. for _, arg := range fn.Args {
  4954. if e.isAggregate(arg) {
  4955. return true
  4956. }
  4957. }
  4958. return false
  4959. }
  4960. switch ex := expr.(type) {
  4961. case *parser.UnaryExpr:
  4962. return e.isAggregate(ex.Operand)
  4963. case *parser.BinaryExpr:
  4964. return e.isAggregate(ex.Left) || e.isAggregate(ex.Right)
  4965. case *parser.ParenExpr:
  4966. return e.isAggregate(ex.Expr)
  4967. case *parser.CaseExpr:
  4968. if ex.Operand != nil && e.isAggregate(ex.Operand) {
  4969. return true
  4970. }
  4971. for _, w := range ex.Whens {
  4972. if e.isAggregate(w.Condition) || e.isAggregate(w.Result) {
  4973. return true
  4974. }
  4975. }
  4976. if ex.Else != nil {
  4977. return e.isAggregate(ex.Else)
  4978. }
  4979. case *parser.CastExpr:
  4980. return e.isAggregate(ex.Expr)
  4981. case *parser.IsNullExpr:
  4982. return e.isAggregate(ex.Left)
  4983. case *parser.BetweenExpr:
  4984. return e.isAggregate(ex.Left) || e.isAggregate(ex.Low) || e.isAggregate(ex.High)
  4985. case *parser.InExpr:
  4986. if e.isAggregate(ex.Left) {
  4987. return true
  4988. }
  4989. for _, val := range ex.Values {
  4990. if e.isAggregate(val) {
  4991. return true
  4992. }
  4993. }
  4994. return false
  4995. }
  4996. return false
  4997. }
  4998. func (e *Executor) buildGroupKey(groupBy []parser.Expr, row storage.Row) string {
  4999. var parts []string
  5000. for _, expr := range groupBy {
  5001. val, _ := e.evalExpr(expr, row)
  5002. parts = append(parts, fmt.Sprintf("%v", val))
  5003. }
  5004. return strings.Join(parts, "|")
  5005. }
  5006. // resolveOrderByPositions replaces positional ORDER BY expressions (e.g. ORDER BY 1)
  5007. // with the corresponding SELECT column expressions per SQL-92 semantics.
  5008. func resolveOrderByPositions(orderBy []parser.OrderByItem, selectCols []parser.SelectColumn) []parser.OrderByItem {
  5009. result := make([]parser.OrderByItem, len(orderBy))
  5010. for i, item := range orderBy {
  5011. if lit, ok := item.Expr.(*parser.LiteralExpr); ok {
  5012. if pos, err := strconv.Atoi(lit.Value); err == nil && pos >= 1 && pos <= len(selectCols) {
  5013. col := selectCols[pos-1]
  5014. if col.Expr != nil {
  5015. result[i] = parser.OrderByItem{Expr: col.Expr, Desc: item.Desc, NullsOrder: item.NullsOrder}
  5016. continue
  5017. }
  5018. }
  5019. }
  5020. result[i] = item
  5021. }
  5022. return result
  5023. }
  5024. // orderByLess reports whether key slice a sorts before b under orderBy.
  5025. // Keys are precomputed per-row ORDER BY expression values, one per item.
  5026. func orderByLess(a, b []interface{}, orderBy []parser.OrderByItem) bool {
  5027. for i, item := range orderBy {
  5028. av, bv := a[i], b[i]
  5029. if av == nil || bv == nil {
  5030. if av == nil && bv == nil {
  5031. continue
  5032. }
  5033. // SQLite treats NULL as smaller than any value, so the default puts
  5034. // NULLs first for ASC and last for DESC. NULLS FIRST/LAST overrides it.
  5035. nullsFirst := !item.Desc
  5036. switch item.NullsOrder {
  5037. case parser.NullsFirst:
  5038. nullsFirst = true
  5039. case parser.NullsLast:
  5040. nullsFirst = false
  5041. }
  5042. if av == nil {
  5043. return nullsFirst
  5044. }
  5045. return !nullsFirst
  5046. }
  5047. cmp := compare(av, bv)
  5048. if cmp != 0 {
  5049. if item.Desc {
  5050. return cmp > 0
  5051. }
  5052. return cmp < 0
  5053. }
  5054. }
  5055. return false
  5056. }
  5057. // sortKeyOrder returns a permutation of [0..len(keys)) that sorts the keys
  5058. // ascending per orderBy.
  5059. func sortKeyOrder(keys [][]interface{}, orderBy []parser.OrderByItem) []int {
  5060. order := make([]int, len(keys))
  5061. for i := range order {
  5062. order[i] = i
  5063. }
  5064. sort.Slice(order, func(a, b int) bool {
  5065. return orderByLess(keys[order[a]], keys[order[b]], orderBy)
  5066. })
  5067. return order
  5068. }
  5069. // topNHeap is a bounded max-heap that keeps the k smallest elements (per
  5070. // orderByLess) seen so far.
  5071. type topNHeap struct {
  5072. keys [][]interface{}
  5073. idx []int
  5074. orderBy []parser.OrderByItem
  5075. }
  5076. func (h *topNHeap) Len() int { return len(h.idx) }
  5077. func (h *topNHeap) Less(i, j int) bool {
  5078. return orderByLess(h.keys[h.idx[j]], h.keys[h.idx[i]], h.orderBy)
  5079. }
  5080. func (h *topNHeap) Swap(i, j int) { h.idx[i], h.idx[j] = h.idx[j], h.idx[i] }
  5081. func (h *topNHeap) Push(x interface{}) { h.idx = append(h.idx, x.(int)) }
  5082. func (h *topNHeap) Pop() interface{} {
  5083. n := len(h.idx)
  5084. x := h.idx[n-1]
  5085. h.idx = h.idx[:n-1]
  5086. return x
  5087. }
  5088. // topNKeyOrder returns the indices of the k smallest keys (per orderByLess) in
  5089. // ascending order, without fully sorting all n elements. If k >= n it falls
  5090. // back to a full sort.
  5091. func topNKeyOrder(keys [][]interface{}, orderBy []parser.OrderByItem, k int) []int {
  5092. if k <= 0 {
  5093. return nil
  5094. }
  5095. if k >= len(keys) {
  5096. return sortKeyOrder(keys, orderBy)
  5097. }
  5098. h := &topNHeap{keys: keys, orderBy: orderBy, idx: make([]int, 0, k)}
  5099. for i := range keys {
  5100. if h.Len() < k {
  5101. heap.Push(h, i)
  5102. } else if orderByLess(keys[i], keys[h.idx[0]], orderBy) {
  5103. h.idx[0] = i
  5104. heap.Fix(h, 0)
  5105. }
  5106. }
  5107. selected := append([]int(nil), h.idx...)
  5108. sort.Slice(selected, func(a, b int) bool {
  5109. return orderByLess(keys[selected[a]], keys[selected[b]], orderBy)
  5110. })
  5111. return selected
  5112. }
  5113. func reorderRows(rows []storage.Row, order []int) {
  5114. tmp := make([]storage.Row, len(rows))
  5115. for i, idx := range order {
  5116. tmp[i] = rows[idx]
  5117. }
  5118. copy(rows, tmp)
  5119. }
  5120. // sortRowKeys precomputes the ORDER BY expression value for each row so each
  5121. // expression is evaluated once per row instead of O(n log n) times.
  5122. func (e *Executor) sortRowKeys(rows []storage.Row, orderBy []parser.OrderByItem) [][]interface{} {
  5123. keys := make([][]interface{}, len(rows))
  5124. for i, row := range rows {
  5125. ks := make([]interface{}, len(orderBy))
  5126. for j, item := range orderBy {
  5127. ks[j], _ = e.evalExpr(item.Expr, row)
  5128. }
  5129. keys[i] = ks
  5130. }
  5131. return keys
  5132. }
  5133. func (e *Executor) sortRows(rows []storage.Row, orderBy []parser.OrderByItem) {
  5134. order := sortKeyOrder(e.sortRowKeys(rows, orderBy), orderBy)
  5135. reorderRows(rows, order)
  5136. }
  5137. // topNRows sorts only enough to keep the k smallest rows (per orderBy).
  5138. func (e *Executor) topNRows(rows []storage.Row, orderBy []parser.OrderByItem, k int) []storage.Row {
  5139. order := topNKeyOrder(e.sortRowKeys(rows, orderBy), orderBy, k)
  5140. out := make([]storage.Row, len(order))
  5141. for i, idx := range order {
  5142. out[i] = rows[idx]
  5143. }
  5144. return out
  5145. }
  5146. // orderAndLimitRows applies ORDER BY (with a bounded top-N selection when a
  5147. // LIMIT is present), then OFFSET and LIMIT, preserving SQL semantics.
  5148. func (e *Executor) orderAndLimitRows(rows []storage.Row, orderBy []parser.OrderByItem, limitExpr, offsetExpr parser.Expr, selectCols []parser.SelectColumn) []storage.Row {
  5149. orderBy = resolveOrderByPositions(orderBy, selectCols)
  5150. var offset, limit int
  5151. hasOffset := offsetExpr != nil
  5152. hasLimit := limitExpr != nil
  5153. if hasOffset {
  5154. offset = e.evalIntExpr(offsetExpr)
  5155. }
  5156. if hasLimit {
  5157. limit = e.evalIntExpr(limitExpr)
  5158. }
  5159. if len(orderBy) > 0 {
  5160. if hasLimit && limit >= 0 {
  5161. k := offset + limit
  5162. if k >= 0 && k < len(rows) {
  5163. rows = e.topNRows(rows, orderBy, k)
  5164. } else {
  5165. e.sortRows(rows, orderBy)
  5166. }
  5167. } else {
  5168. e.sortRows(rows, orderBy)
  5169. }
  5170. }
  5171. if hasOffset {
  5172. if offset < len(rows) {
  5173. rows = rows[offset:]
  5174. } else {
  5175. rows = nil
  5176. }
  5177. }
  5178. if hasLimit {
  5179. if limit < len(rows) {
  5180. rows = rows[:limit]
  5181. }
  5182. }
  5183. return rows
  5184. }
  5185. // resultRowKey evaluates a single ORDER BY item against a result row, honoring
  5186. // select-column aliases exactly like the previous sortResultRows implementation.
  5187. func (e *Executor) resultRowKey(result *Result, rowIdx int, item parser.OrderByItem, columnNames []string) interface{} {
  5188. if ref, ok := item.Expr.(*parser.ColumnRef); ok && ref.Table == "" {
  5189. for idx, name := range columnNames {
  5190. if strings.EqualFold(name, ref.Column) {
  5191. if idx < len(result.Rows[rowIdx]) {
  5192. return result.Rows[rowIdx][idx]
  5193. }
  5194. }
  5195. }
  5196. }
  5197. row := e.resultRowToStorageRow(result, rowIdx)
  5198. v, _ := e.evalExpr(item.Expr, row)
  5199. return v
  5200. }
  5201. // resultRowKeys precomputes the ORDER BY expression value for each result row.
  5202. func (e *Executor) resultRowKeys(result *Result, orderBy []parser.OrderByItem, columnNames []string) [][]interface{} {
  5203. keys := make([][]interface{}, len(result.Rows))
  5204. for i := range result.Rows {
  5205. ks := make([]interface{}, len(orderBy))
  5206. for j, item := range orderBy {
  5207. ks[j] = e.resultRowKey(result, i, item, columnNames)
  5208. }
  5209. keys[i] = ks
  5210. }
  5211. return keys
  5212. }
  5213. // sortResultRows sorts Result.Rows based on ORDER BY clauses.
  5214. // It handles column aliases by matching them against the select columns.
  5215. func (e *Executor) sortResultRows(result *Result, orderBy []parser.OrderByItem, selectColumns []parser.SelectColumn, columnNames []string) {
  5216. orderBy = resolveOrderByPositions(orderBy, selectColumns)
  5217. order := sortKeyOrder(e.resultRowKeys(result, orderBy, columnNames), orderBy)
  5218. rows := make([][]interface{}, len(order))
  5219. for i, idx := range order {
  5220. rows[i] = result.Rows[idx]
  5221. }
  5222. result.Rows = rows
  5223. }
  5224. // orderAndLimitResultRows applies ORDER BY (with bounded top-N selection when a
  5225. // LIMIT is present), then OFFSET and LIMIT, to a Result's rows.
  5226. func (e *Executor) orderAndLimitResultRows(result *Result, orderBy []parser.OrderByItem, limitExpr, offsetExpr parser.Expr, selectCols []parser.SelectColumn, columnNames []string) {
  5227. orderBy = resolveOrderByPositions(orderBy, selectCols)
  5228. var offset, limit int
  5229. hasOffset := offsetExpr != nil
  5230. hasLimit := limitExpr != nil
  5231. if hasOffset {
  5232. offset = e.evalIntExpr(offsetExpr)
  5233. }
  5234. if hasLimit {
  5235. limit = e.evalIntExpr(limitExpr)
  5236. }
  5237. if len(orderBy) > 0 {
  5238. if hasLimit && limit >= 0 {
  5239. k := offset + limit
  5240. if k >= 0 && k < len(result.Rows) {
  5241. order := topNKeyOrder(e.resultRowKeys(result, orderBy, columnNames), orderBy, k)
  5242. rows := make([][]interface{}, len(order))
  5243. for i, idx := range order {
  5244. rows[i] = result.Rows[idx]
  5245. }
  5246. result.Rows = rows
  5247. } else {
  5248. e.sortResultRows(result, orderBy, nil, columnNames)
  5249. }
  5250. } else {
  5251. e.sortResultRows(result, orderBy, nil, columnNames)
  5252. }
  5253. }
  5254. if hasOffset {
  5255. if offset < len(result.Rows) {
  5256. result.Rows = result.Rows[offset:]
  5257. } else {
  5258. result.Rows = nil
  5259. }
  5260. result.RowCount = len(result.Rows)
  5261. }
  5262. if hasLimit {
  5263. if limit < len(result.Rows) {
  5264. result.Rows = result.Rows[:limit]
  5265. }
  5266. result.RowCount = len(result.Rows)
  5267. }
  5268. }
  5269. // resultRowToStorageRow converts a Result row back to storage.Row for expression evaluation.
  5270. func (e *Executor) resultRowToStorageRow(result *Result, rowIdx int) storage.Row {
  5271. row := make(storage.Row)
  5272. for colIdx, colName := range result.Columns {
  5273. if colIdx < len(result.Rows[rowIdx]) {
  5274. row[colName] = result.Rows[rowIdx][colIdx]
  5275. }
  5276. }
  5277. return row
  5278. }
  5279. func (e *Executor) evalIntExpr(expr parser.Expr) int {
  5280. val, _ := e.evalExpr(expr, nil)
  5281. return int(toFloat(val))
  5282. }
  5283. // Type conversion helpers
  5284. func isIntVal(v interface{}) bool {
  5285. switch v.(type) {
  5286. case int64, int, bool:
  5287. return true
  5288. default:
  5289. return false
  5290. }
  5291. }
  5292. func toInt64(v interface{}) int64 {
  5293. switch val := v.(type) {
  5294. case int64:
  5295. return val
  5296. case int:
  5297. return int64(val)
  5298. case float64:
  5299. return int64(val)
  5300. case bool:
  5301. if val {
  5302. return 1
  5303. }
  5304. return 0
  5305. default:
  5306. return 0
  5307. }
  5308. }
  5309. func toFloat(v interface{}) float64 {
  5310. switch val := v.(type) {
  5311. case nil:
  5312. return 0
  5313. case int64:
  5314. return float64(val)
  5315. case int:
  5316. return float64(val)
  5317. case float64:
  5318. return val
  5319. case bool:
  5320. if val {
  5321. return 1
  5322. }
  5323. return 0
  5324. case string:
  5325. f, _ := strconv.ParseFloat(val, 64)
  5326. return f
  5327. default:
  5328. return 0
  5329. }
  5330. }
  5331. func toBool(v interface{}) bool {
  5332. switch val := v.(type) {
  5333. case nil:
  5334. return false
  5335. case bool:
  5336. return val
  5337. case int64:
  5338. return val != 0
  5339. case int:
  5340. return val != 0
  5341. case float64:
  5342. return val != 0
  5343. case string:
  5344. return val != "" && val != "0" && strings.ToLower(val) != "false"
  5345. default:
  5346. return false
  5347. }
  5348. }
  5349. func toString(v interface{}) string {
  5350. if v == nil {
  5351. return ""
  5352. }
  5353. return fmt.Sprintf("%v", v)
  5354. }
  5355. func compare(a, b interface{}) int {
  5356. if a == nil && b == nil {
  5357. return 0
  5358. }
  5359. if a == nil {
  5360. return -1
  5361. }
  5362. if b == nil {
  5363. return 1
  5364. }
  5365. // Try numeric comparison
  5366. fa, oka := toNumeric(a)
  5367. fb, okb := toNumeric(b)
  5368. if oka && okb {
  5369. if fa < fb {
  5370. return -1
  5371. }
  5372. if fa > fb {
  5373. return 1
  5374. }
  5375. return 0
  5376. }
  5377. // String comparison
  5378. sa := toString(a)
  5379. sb := toString(b)
  5380. return strings.Compare(sa, sb)
  5381. }
  5382. func toNumeric(v interface{}) (float64, bool) {
  5383. switch val := v.(type) {
  5384. case int64:
  5385. return float64(val), true
  5386. case int:
  5387. return float64(val), true
  5388. case float64:
  5389. return val, true
  5390. case string:
  5391. f, err := strconv.ParseFloat(val, 64)
  5392. return f, err == nil
  5393. default:
  5394. return 0, false
  5395. }
  5396. }
  5397. // splitANDClauses flattens a tree of AND binary expressions into a slice of leaf conditions.
  5398. func splitANDClauses(expr parser.Expr) []parser.Expr {
  5399. if bin, ok := expr.(*parser.BinaryExpr); ok && bin.Op == lexer.TokenAND {
  5400. left := splitANDClauses(bin.Left)
  5401. right := splitANDClauses(bin.Right)
  5402. return append(left, right...)
  5403. }
  5404. return []parser.Expr{expr}
  5405. }
  5406. // collectColumnRefs returns all unqualified column names referenced in an expression.
  5407. func collectColumnRefs(expr parser.Expr) []string {
  5408. var refs []string
  5409. var hasSubquery bool
  5410. var walk func(parser.Expr)
  5411. walk = func(e parser.Expr) {
  5412. if e == nil {
  5413. return
  5414. }
  5415. switch n := e.(type) {
  5416. case *parser.ColumnRef:
  5417. refs = append(refs, n.Column)
  5418. case *parser.BinaryExpr:
  5419. walk(n.Left)
  5420. walk(n.Right)
  5421. case *parser.UnaryExpr:
  5422. walk(n.Operand)
  5423. case *parser.InExpr:
  5424. walk(n.Left)
  5425. // Check for subquery
  5426. if n.Subquery != nil {
  5427. hasSubquery = true
  5428. }
  5429. for _, v := range n.Values {
  5430. walk(v)
  5431. }
  5432. case *parser.BetweenExpr:
  5433. walk(n.Left)
  5434. walk(n.Low)
  5435. walk(n.High)
  5436. case *parser.LikeExpr:
  5437. walk(n.Left)
  5438. walk(n.Pattern)
  5439. case *parser.IsNullExpr:
  5440. walk(n.Left)
  5441. case *parser.CaseExpr:
  5442. walk(n.Operand)
  5443. for _, w := range n.Whens {
  5444. walk(w.Condition)
  5445. walk(w.Result)
  5446. }
  5447. walk(n.Else)
  5448. case *parser.FunctionCall:
  5449. for _, a := range n.Args {
  5450. walk(a)
  5451. }
  5452. case *parser.ParenExpr:
  5453. walk(n.Expr)
  5454. case *parser.CastExpr:
  5455. walk(n.Expr)
  5456. case *parser.SubqueryExpr:
  5457. // Subqueries may reference outer columns
  5458. hasSubquery = true
  5459. case *parser.ExistsExpr:
  5460. // EXISTS subqueries may reference outer columns
  5461. hasSubquery = true
  5462. case *parser.LiteralExpr:
  5463. // Literals have no column refs
  5464. }
  5465. }
  5466. walk(expr)
  5467. // If we have subqueries, add a sentinel value to indicate non-constant
  5468. if hasSubquery {
  5469. refs = append(refs, "__subquery__")
  5470. }
  5471. return refs
  5472. }
  5473. type tableColRef struct{ tbl, col string }
  5474. // collectTableColumnRefs returns all column references with their table qualifier (may be "").
  5475. func collectTableColumnRefs(expr parser.Expr) []tableColRef {
  5476. var refs []tableColRef
  5477. var walk func(parser.Expr)
  5478. walk = func(e parser.Expr) {
  5479. if e == nil {
  5480. return
  5481. }
  5482. switch n := e.(type) {
  5483. case *parser.ColumnRef:
  5484. refs = append(refs, tableColRef{tbl: n.Table, col: n.Column})
  5485. case *parser.BinaryExpr:
  5486. walk(n.Left)
  5487. walk(n.Right)
  5488. case *parser.UnaryExpr:
  5489. walk(n.Operand)
  5490. case *parser.InExpr:
  5491. walk(n.Left)
  5492. for _, v := range n.Values {
  5493. walk(v)
  5494. }
  5495. case *parser.BetweenExpr:
  5496. walk(n.Left)
  5497. walk(n.Low)
  5498. walk(n.High)
  5499. case *parser.LikeExpr:
  5500. walk(n.Left)
  5501. walk(n.Pattern)
  5502. case *parser.IsNullExpr:
  5503. walk(n.Left)
  5504. case *parser.CaseExpr:
  5505. walk(n.Operand)
  5506. for _, w := range n.Whens {
  5507. walk(w.Condition)
  5508. walk(w.Result)
  5509. }
  5510. walk(n.Else)
  5511. case *parser.FunctionCall:
  5512. for _, a := range n.Args {
  5513. walk(a)
  5514. }
  5515. }
  5516. }
  5517. walk(expr)
  5518. return refs
  5519. }
  5520. // combineAND combines a list of expressions with AND.
  5521. func combineAND(clauses []parser.Expr) parser.Expr {
  5522. if len(clauses) == 0 {
  5523. return nil
  5524. }
  5525. result := clauses[0]
  5526. for _, c := range clauses[1:] {
  5527. result = &parser.BinaryExpr{Left: result, Op: lexer.TokenAND, Right: c}
  5528. }
  5529. return result
  5530. }
  5531. // matchLike matches a string against a SQL LIKE pattern.
  5532. func matchLike(s, pattern string) bool {
  5533. // Simple implementation - convert to lowercase for case-insensitive matching
  5534. s = strings.ToLower(s)
  5535. pattern = strings.ToLower(pattern)
  5536. return matchLikeHelper(s, pattern)
  5537. }
  5538. func matchLikeHelper(s, p string) bool {
  5539. if p == "" {
  5540. return s == ""
  5541. }
  5542. if p[0] == '%' {
  5543. // % matches any sequence
  5544. for i := 0; i <= len(s); i++ {
  5545. if matchLikeHelper(s[i:], p[1:]) {
  5546. return true
  5547. }
  5548. }
  5549. return false
  5550. }
  5551. if s == "" {
  5552. return false
  5553. }
  5554. if p[0] == '_' || p[0] == s[0] {
  5555. return matchLikeHelper(s[1:], p[1:])
  5556. }
  5557. return false
  5558. }
  5559. // matchGlob matches a string against a GLOB pattern.
  5560. // GLOB uses * for any sequence and ? for single character (case-sensitive).
  5561. func matchGlob(pattern, s string) bool {
  5562. return matchGlobHelper(pattern, s)
  5563. }
  5564. func matchGlobHelper(p, s string) bool {
  5565. if p == "" {
  5566. return s == ""
  5567. }
  5568. if p[0] == '*' {
  5569. // * matches any sequence
  5570. for i := 0; i <= len(s); i++ {
  5571. if matchGlobHelper(p[1:], s[i:]) {
  5572. return true
  5573. }
  5574. }
  5575. return false
  5576. }
  5577. if s == "" {
  5578. return false
  5579. }
  5580. if p[0] == '?' || p[0] == s[0] {
  5581. return matchGlobHelper(p[1:], s[1:])
  5582. }
  5583. // Handle character classes [...]
  5584. if p[0] == '[' {
  5585. end := strings.Index(p, "]")
  5586. if end > 0 {
  5587. class := p[1:end]
  5588. match := false
  5589. negate := false
  5590. if len(class) > 0 && class[0] == '^' {
  5591. negate = true
  5592. class = class[1:]
  5593. }
  5594. for _, c := range class {
  5595. if byte(c) == s[0] {
  5596. match = true
  5597. break
  5598. }
  5599. }
  5600. if negate {
  5601. match = !match
  5602. }
  5603. if match {
  5604. return matchGlobHelper(p[end+1:], s[1:])
  5605. }
  5606. }
  5607. }
  5608. return false
  5609. }
  5610. // applyDistinct removes duplicate rows from the result
  5611. func (e *Executor) applyDistinct(rows [][]interface{}) [][]interface{} {
  5612. if len(rows) == 0 {
  5613. return rows
  5614. }
  5615. seen := make(map[string]bool)
  5616. uniqueRows := make([][]interface{}, 0)
  5617. for _, row := range rows {
  5618. // Create a key from all column values
  5619. key := ""
  5620. for i, val := range row {
  5621. if i > 0 {
  5622. key += "\x00" // Use null byte as separator
  5623. }
  5624. key += fmt.Sprintf("%v", val)
  5625. }
  5626. if !seen[key] {
  5627. seen[key] = true
  5628. uniqueRows = append(uniqueRows, row)
  5629. }
  5630. }
  5631. return uniqueRows
  5632. }