You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

sqlite.patch001 491 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131
  1. diff -Npur sqlite-version-3.32.2/src/expr.c sqlite-version-3.32.2-patched/src/expr.c
  2. --- sqlite-version-3.32.2/src/expr.c 2020-06-04 20:58:43.000000000 +0800
  3. +++ sqlite-version-3.32.2-patched/src/expr.c 2020-07-08 10:00:47.367088648 +0800
  4. @@ -3813,6 +3813,7 @@ expr_code_doover:
  5. AggInfo *pAggInfo = pExpr->pAggInfo;
  6. struct AggInfo_col *pCol;
  7. assert( pAggInfo!=0 );
  8. + assert( AggInfoValid(pAggInfo) );
  9. assert( pExpr->iAgg>=0 && pExpr->iAgg<pAggInfo->nColumn );
  10. pCol = &pAggInfo->aCol[pExpr->iAgg];
  11. if( !pAggInfo->directMode ){
  12. @@ -4121,6 +4122,7 @@ expr_code_doover:
  13. assert( !ExprHasProperty(pExpr, EP_IntValue) );
  14. sqlite3ErrorMsg(pParse, "misuse of aggregate: %s()", pExpr->u.zToken);
  15. }else{
  16. + assert( AggInfoValid(pInfo) );
  17. return pInfo->aFunc[pExpr->iAgg].iMem;
  18. }
  19. break;
  20. @@ -5658,13 +5660,7 @@ struct SrcCount {
  21. ** Count the number of references to columns.
  22. */
  23. static int exprSrcCount(Walker *pWalker, Expr *pExpr){
  24. - /* There was once a NEVER() on the second term on the grounds that
  25. - ** sqlite3FunctionUsesThisSrc() was always called before
  26. - ** sqlite3ExprAnalyzeAggregates() and so the TK_COLUMNs have not yet
  27. - ** been converted into TK_AGG_COLUMN. But this is no longer true due
  28. - ** to window functions - sqlite3WindowRewrite() may now indirectly call
  29. - ** FunctionUsesThisSrc() when creating a new sub-select. */
  30. - if( pExpr->op==TK_COLUMN || pExpr->op==TK_AGG_COLUMN ){
  31. + if( pExpr->op==TK_COLUMN || NEVER(pExpr->op==TK_AGG_COLUMN) ){
  32. int i;
  33. struct SrcCount *p = pWalker->u.pSrcCount;
  34. SrcList *pSrc = p->pSrc;
  35. diff -Npur sqlite-version-3.32.2/src/global.c sqlite-version-3.32.2-patched/src/global.c
  36. --- sqlite-version-3.32.2/src/global.c 2020-06-04 20:58:43.000000000 +0800
  37. +++ sqlite-version-3.32.2-patched/src/global.c 2020-07-08 10:00:47.367088648 +0800
  38. @@ -300,6 +300,11 @@ sqlite3_uint64 sqlite3NProfileCnt = 0;
  39. int sqlite3PendingByte = 0x40000000;
  40. #endif
  41. +/*
  42. +** Flags for select tracing and the ".selecttrace" macro of the CLI
  43. +*/
  44. +/**/ u32 sqlite3SelectTrace = 0;
  45. +
  46. #include "opcodes.h"
  47. /*
  48. ** Properties of opcodes. The OPFLG_INITIALIZER macro is
  49. diff -Npur sqlite-version-3.32.2/src/resolve.c sqlite-version-3.32.2-patched/src/resolve.c
  50. --- sqlite-version-3.32.2/src/resolve.c 2020-06-04 20:58:43.000000000 +0800
  51. +++ sqlite-version-3.32.2-patched/src/resolve.c 2020-07-08 10:00:47.367088648 +0800
  52. @@ -1715,6 +1715,14 @@ static int resolveSelectStep(Walker *pWa
  53. return WRC_Abort;
  54. }
  55. }
  56. + }else if( p->pWin && ALWAYS( (p->selFlags & SF_WinRewrite)==0 ) ){
  57. + sqlite3WindowRewrite(pParse, p);
  58. +#if SELECTTRACE_ENABLED
  59. + if( (sqlite3SelectTrace & 0x108)!=0 ){
  60. + SELECTTRACE(0x104,pParse,p, ("after window rewrite:\n"));
  61. + sqlite3TreeViewSelect(0, p, 0);
  62. + }
  63. +#endif
  64. }
  65. #endif
  66. diff -Npur sqlite-version-3.32.2/src/select.c sqlite-version-3.32.2-patched/src/select.c
  67. --- sqlite-version-3.32.2/src/select.c 2020-06-04 20:58:43.000000000 +0800
  68. +++ sqlite-version-3.32.2-patched/src/select.c 2020-07-08 10:00:50.899152517 +0800
  69. @@ -15,20 +15,6 @@
  70. #include "sqliteInt.h"
  71. /*
  72. -** Trace output macros
  73. -*/
  74. -#if SELECTTRACE_ENABLED
  75. -/***/ int sqlite3SelectTrace = 0;
  76. -# define SELECTTRACE(K,P,S,X) \
  77. - if(sqlite3SelectTrace&(K)) \
  78. - sqlite3DebugPrintf("%u/%d/%p: ",(S)->selId,(P)->addrExplain,(S)),\
  79. - sqlite3DebugPrintf X
  80. -#else
  81. -# define SELECTTRACE(K,P,S,X)
  82. -#endif
  83. -
  84. -
  85. -/*
  86. ** An instance of the following object is used to record information about
  87. ** how to process the DISTINCT keyword, to simplify passing that information
  88. ** into the selectInnerLoop() routine.
  89. @@ -2717,9 +2703,7 @@ static int multiSelect(
  90. selectOpName(p->op)));
  91. rc = sqlite3Select(pParse, p, &uniondest);
  92. testcase( rc!=SQLITE_OK );
  93. - /* Query flattening in sqlite3Select() might refill p->pOrderBy.
  94. - ** Be sure to delete p->pOrderBy, therefore, to avoid a memory leak. */
  95. - sqlite3ExprListDelete(db, p->pOrderBy);
  96. + assert( p->pOrderBy==0 );
  97. pDelete = p->pPrior;
  98. p->pPrior = pPrior;
  99. p->pOrderBy = 0;
  100. @@ -4105,7 +4089,7 @@ static int flattenSubquery(
  101. ** We look at every expression in the outer query and every place we see
  102. ** "a" we substitute "x*3" and every place we see "b" we substitute "y+10".
  103. */
  104. - if( pSub->pOrderBy ){
  105. + if( pSub->pOrderBy && (pParent->selFlags & SF_NoopOrderBy)==0 ){
  106. /* At this point, any non-zero iOrderByCol values indicate that the
  107. ** ORDER BY column expression is identical to the iOrderByCol'th
  108. ** expression returned by SELECT statement pSub. Since these values
  109. @@ -4426,11 +4410,14 @@ static int pushDownWhereTerms(
  110. ){
  111. Expr *pNew;
  112. int nChng = 0;
  113. + Select *pSel;
  114. if( pWhere==0 ) return 0;
  115. if( pSubq->selFlags & SF_Recursive ) return 0; /* restriction (2) */
  116. #ifndef SQLITE_OMIT_WINDOWFUNC
  117. - if( pSubq->pWin ) return 0; /* restriction (6) */
  118. + for(pSel=pSubq; pSel; pSel=pSel->pPrior){
  119. + if( pSel->pWin ) return 0; /* restriction (6) */
  120. + }
  121. #endif
  122. #ifdef SQLITE_DEBUG
  123. @@ -5766,6 +5753,9 @@ int sqlite3Select(
  124. }
  125. if( sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0) ) return 1;
  126. memset(&sAggInfo, 0, sizeof(sAggInfo));
  127. +#ifdef SQLITE_DEBUG
  128. + sAggInfo.iAggMagic = SQLITE_AGGMAGIC_VALID;
  129. +#endif
  130. #if SELECTTRACE_ENABLED
  131. SELECTTRACE(1,pParse,p, ("begin processing:\n", pParse->addrExplain));
  132. if( sqlite3SelectTrace & 0x100 ){
  133. @@ -5787,6 +5777,7 @@ int sqlite3Select(
  134. sqlite3ExprListDelete(db, p->pOrderBy);
  135. p->pOrderBy = 0;
  136. p->selFlags &= ~SF_Distinct;
  137. + p->selFlags |= SF_NoopOrderBy;
  138. }
  139. sqlite3SelectPrep(pParse, p, 0);
  140. if( pParse->nErr || db->mallocFailed ){
  141. @@ -5804,19 +5795,6 @@ int sqlite3Select(
  142. generateColumnNames(pParse, p);
  143. }
  144. -#ifndef SQLITE_OMIT_WINDOWFUNC
  145. - rc = sqlite3WindowRewrite(pParse, p);
  146. - if( rc ){
  147. - assert( db->mallocFailed || pParse->nErr>0 );
  148. - goto select_end;
  149. - }
  150. -#if SELECTTRACE_ENABLED
  151. - if( p->pWin && (sqlite3SelectTrace & 0x108)!=0 ){
  152. - SELECTTRACE(0x104,pParse,p, ("after window rewrite:\n"));
  153. - sqlite3TreeViewSelect(0, p, 0);
  154. - }
  155. -#endif
  156. -#endif /* SQLITE_OMIT_WINDOWFUNC */
  157. pTabList = p->pSrc;
  158. isAgg = (p->selFlags & SF_Aggregate)!=0;
  159. memset(&sSort, 0, sizeof(sSort));
  160. @@ -6144,7 +6122,7 @@ int sqlite3Select(
  161. if( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct
  162. && sqlite3ExprListCompare(sSort.pOrderBy, pEList, -1)==0
  163. #ifndef SQLITE_OMIT_WINDOWFUNC
  164. - && p->pWin==0
  165. + && ALWAYS(p->pWin==0)
  166. #endif
  167. ){
  168. p->selFlags &= ~SF_Distinct;
  169. @@ -6791,6 +6769,14 @@ int sqlite3Select(
  170. select_end:
  171. sqlite3ExprListDelete(db, pMinMaxOrderBy);
  172. sqlite3DbFree(db, sAggInfo.aCol);
  173. +#ifdef SQLITE_DEBUG
  174. + for(i=0; i<sAggInfo.nFunc; i++){
  175. + assert( sAggInfo.aFunc[i].pExpr!=0 );
  176. + assert( sAggInfo.aFunc[i].pExpr->pAggInfo==&sAggInfo );
  177. + sAggInfo.aFunc[i].pExpr->pAggInfo = 0;
  178. + }
  179. + sAggInfo.iAggMagic = 0;
  180. +#endif
  181. sqlite3DbFree(db, sAggInfo.aFunc);
  182. #if SELECTTRACE_ENABLED
  183. SELECTTRACE(0x1,pParse,p,("end processing\n"));
  184. diff -Npur sqlite-version-3.32.2/src/select.c.orig sqlite-version-3.32.2-patched/src/select.c.orig
  185. --- sqlite-version-3.32.2/src/select.c.orig 1970-01-01 08:00:00.000000000 +0800
  186. +++ sqlite-version-3.32.2-patched/src/select.c.orig 2020-07-08 10:00:47.367088648 +0800
  187. @@ -0,0 +1,6790 @@
  188. +/*
  189. +** 2001 September 15
  190. +**
  191. +** The author disclaims copyright to this source code. In place of
  192. +** a legal notice, here is a blessing:
  193. +**
  194. +** May you do good and not evil.
  195. +** May you find forgiveness for yourself and forgive others.
  196. +** May you share freely, never taking more than you give.
  197. +**
  198. +*************************************************************************
  199. +** This file contains C code routines that are called by the parser
  200. +** to handle SELECT statements in SQLite.
  201. +*/
  202. +#include "sqliteInt.h"
  203. +
  204. +/*
  205. +** An instance of the following object is used to record information about
  206. +** how to process the DISTINCT keyword, to simplify passing that information
  207. +** into the selectInnerLoop() routine.
  208. +*/
  209. +typedef struct DistinctCtx DistinctCtx;
  210. +struct DistinctCtx {
  211. + u8 isTnct; /* True if the DISTINCT keyword is present */
  212. + u8 eTnctType; /* One of the WHERE_DISTINCT_* operators */
  213. + int tabTnct; /* Ephemeral table used for DISTINCT processing */
  214. + int addrTnct; /* Address of OP_OpenEphemeral opcode for tabTnct */
  215. +};
  216. +
  217. +/*
  218. +** An instance of the following object is used to record information about
  219. +** the ORDER BY (or GROUP BY) clause of query is being coded.
  220. +**
  221. +** The aDefer[] array is used by the sorter-references optimization. For
  222. +** example, assuming there is no index that can be used for the ORDER BY,
  223. +** for the query:
  224. +**
  225. +** SELECT a, bigblob FROM t1 ORDER BY a LIMIT 10;
  226. +**
  227. +** it may be more efficient to add just the "a" values to the sorter, and
  228. +** retrieve the associated "bigblob" values directly from table t1 as the
  229. +** 10 smallest "a" values are extracted from the sorter.
  230. +**
  231. +** When the sorter-reference optimization is used, there is one entry in the
  232. +** aDefer[] array for each database table that may be read as values are
  233. +** extracted from the sorter.
  234. +*/
  235. +typedef struct SortCtx SortCtx;
  236. +struct SortCtx {
  237. + ExprList *pOrderBy; /* The ORDER BY (or GROUP BY clause) */
  238. + int nOBSat; /* Number of ORDER BY terms satisfied by indices */
  239. + int iECursor; /* Cursor number for the sorter */
  240. + int regReturn; /* Register holding block-output return address */
  241. + int labelBkOut; /* Start label for the block-output subroutine */
  242. + int addrSortIndex; /* Address of the OP_SorterOpen or OP_OpenEphemeral */
  243. + int labelDone; /* Jump here when done, ex: LIMIT reached */
  244. + int labelOBLopt; /* Jump here when sorter is full */
  245. + u8 sortFlags; /* Zero or more SORTFLAG_* bits */
  246. +#ifdef SQLITE_ENABLE_SORTER_REFERENCES
  247. + u8 nDefer; /* Number of valid entries in aDefer[] */
  248. + struct DeferredCsr {
  249. + Table *pTab; /* Table definition */
  250. + int iCsr; /* Cursor number for table */
  251. + int nKey; /* Number of PK columns for table pTab (>=1) */
  252. + } aDefer[4];
  253. +#endif
  254. + struct RowLoadInfo *pDeferredRowLoad; /* Deferred row loading info or NULL */
  255. +};
  256. +#define SORTFLAG_UseSorter 0x01 /* Use SorterOpen instead of OpenEphemeral */
  257. +
  258. +/*
  259. +** Delete all the content of a Select structure. Deallocate the structure
  260. +** itself depending on the value of bFree
  261. +**
  262. +** If bFree==1, call sqlite3DbFree() on the p object.
  263. +** If bFree==0, Leave the first Select object unfreed
  264. +*/
  265. +static void clearSelect(sqlite3 *db, Select *p, int bFree){
  266. + while( p ){
  267. + Select *pPrior = p->pPrior;
  268. + sqlite3ExprListDelete(db, p->pEList);
  269. + sqlite3SrcListDelete(db, p->pSrc);
  270. + sqlite3ExprDelete(db, p->pWhere);
  271. + sqlite3ExprListDelete(db, p->pGroupBy);
  272. + sqlite3ExprDelete(db, p->pHaving);
  273. + sqlite3ExprListDelete(db, p->pOrderBy);
  274. + sqlite3ExprDelete(db, p->pLimit);
  275. +#ifndef SQLITE_OMIT_WINDOWFUNC
  276. + if( OK_IF_ALWAYS_TRUE(p->pWinDefn) ){
  277. + sqlite3WindowListDelete(db, p->pWinDefn);
  278. + }
  279. +#endif
  280. + if( OK_IF_ALWAYS_TRUE(p->pWith) ) sqlite3WithDelete(db, p->pWith);
  281. + if( bFree ) sqlite3DbFreeNN(db, p);
  282. + p = pPrior;
  283. + bFree = 1;
  284. + }
  285. +}
  286. +
  287. +/*
  288. +** Initialize a SelectDest structure.
  289. +*/
  290. +void sqlite3SelectDestInit(SelectDest *pDest, int eDest, int iParm){
  291. + pDest->eDest = (u8)eDest;
  292. + pDest->iSDParm = iParm;
  293. + pDest->zAffSdst = 0;
  294. + pDest->iSdst = 0;
  295. + pDest->nSdst = 0;
  296. +}
  297. +
  298. +
  299. +/*
  300. +** Allocate a new Select structure and return a pointer to that
  301. +** structure.
  302. +*/
  303. +Select *sqlite3SelectNew(
  304. + Parse *pParse, /* Parsing context */
  305. + ExprList *pEList, /* which columns to include in the result */
  306. + SrcList *pSrc, /* the FROM clause -- which tables to scan */
  307. + Expr *pWhere, /* the WHERE clause */
  308. + ExprList *pGroupBy, /* the GROUP BY clause */
  309. + Expr *pHaving, /* the HAVING clause */
  310. + ExprList *pOrderBy, /* the ORDER BY clause */
  311. + u32 selFlags, /* Flag parameters, such as SF_Distinct */
  312. + Expr *pLimit /* LIMIT value. NULL means not used */
  313. +){
  314. + Select *pNew;
  315. + Select standin;
  316. + pNew = sqlite3DbMallocRawNN(pParse->db, sizeof(*pNew) );
  317. + if( pNew==0 ){
  318. + assert( pParse->db->mallocFailed );
  319. + pNew = &standin;
  320. + }
  321. + if( pEList==0 ){
  322. + pEList = sqlite3ExprListAppend(pParse, 0,
  323. + sqlite3Expr(pParse->db,TK_ASTERISK,0));
  324. + }
  325. + pNew->pEList = pEList;
  326. + pNew->op = TK_SELECT;
  327. + pNew->selFlags = selFlags;
  328. + pNew->iLimit = 0;
  329. + pNew->iOffset = 0;
  330. + pNew->selId = ++pParse->nSelect;
  331. + pNew->addrOpenEphm[0] = -1;
  332. + pNew->addrOpenEphm[1] = -1;
  333. + pNew->nSelectRow = 0;
  334. + if( pSrc==0 ) pSrc = sqlite3DbMallocZero(pParse->db, sizeof(*pSrc));
  335. + pNew->pSrc = pSrc;
  336. + pNew->pWhere = pWhere;
  337. + pNew->pGroupBy = pGroupBy;
  338. + pNew->pHaving = pHaving;
  339. + pNew->pOrderBy = pOrderBy;
  340. + pNew->pPrior = 0;
  341. + pNew->pNext = 0;
  342. + pNew->pLimit = pLimit;
  343. + pNew->pWith = 0;
  344. +#ifndef SQLITE_OMIT_WINDOWFUNC
  345. + pNew->pWin = 0;
  346. + pNew->pWinDefn = 0;
  347. +#endif
  348. + if( pParse->db->mallocFailed ) {
  349. + clearSelect(pParse->db, pNew, pNew!=&standin);
  350. + pNew = 0;
  351. + }else{
  352. + assert( pNew->pSrc!=0 || pParse->nErr>0 );
  353. + }
  354. + assert( pNew!=&standin );
  355. + return pNew;
  356. +}
  357. +
  358. +
  359. +/*
  360. +** Delete the given Select structure and all of its substructures.
  361. +*/
  362. +void sqlite3SelectDelete(sqlite3 *db, Select *p){
  363. + if( OK_IF_ALWAYS_TRUE(p) ) clearSelect(db, p, 1);
  364. +}
  365. +
  366. +/*
  367. +** Delete all the substructure for p, but keep p allocated. Redefine
  368. +** p to be a single SELECT where every column of the result set has a
  369. +** value of NULL.
  370. +*/
  371. +void sqlite3SelectReset(Parse *pParse, Select *p){
  372. + if( ALWAYS(p) ){
  373. + clearSelect(pParse->db, p, 0);
  374. + memset(&p->iLimit, 0, sizeof(Select) - offsetof(Select,iLimit));
  375. + p->pEList = sqlite3ExprListAppend(pParse, 0,
  376. + sqlite3ExprAlloc(pParse->db,TK_NULL,0,0));
  377. + p->pSrc = sqlite3DbMallocZero(pParse->db, sizeof(SrcList));
  378. + }
  379. +}
  380. +
  381. +/*
  382. +** Return a pointer to the right-most SELECT statement in a compound.
  383. +*/
  384. +static Select *findRightmost(Select *p){
  385. + while( p->pNext ) p = p->pNext;
  386. + return p;
  387. +}
  388. +
  389. +/*
  390. +** Given 1 to 3 identifiers preceding the JOIN keyword, determine the
  391. +** type of join. Return an integer constant that expresses that type
  392. +** in terms of the following bit values:
  393. +**
  394. +** JT_INNER
  395. +** JT_CROSS
  396. +** JT_OUTER
  397. +** JT_NATURAL
  398. +** JT_LEFT
  399. +** JT_RIGHT
  400. +**
  401. +** A full outer join is the combination of JT_LEFT and JT_RIGHT.
  402. +**
  403. +** If an illegal or unsupported join type is seen, then still return
  404. +** a join type, but put an error in the pParse structure.
  405. +*/
  406. +int sqlite3JoinType(Parse *pParse, Token *pA, Token *pB, Token *pC){
  407. + int jointype = 0;
  408. + Token *apAll[3];
  409. + Token *p;
  410. + /* 0123456789 123456789 123456789 123 */
  411. + static const char zKeyText[] = "naturaleftouterightfullinnercross";
  412. + static const struct {
  413. + u8 i; /* Beginning of keyword text in zKeyText[] */
  414. + u8 nChar; /* Length of the keyword in characters */
  415. + u8 code; /* Join type mask */
  416. + } aKeyword[] = {
  417. + /* natural */ { 0, 7, JT_NATURAL },
  418. + /* left */ { 6, 4, JT_LEFT|JT_OUTER },
  419. + /* outer */ { 10, 5, JT_OUTER },
  420. + /* right */ { 14, 5, JT_RIGHT|JT_OUTER },
  421. + /* full */ { 19, 4, JT_LEFT|JT_RIGHT|JT_OUTER },
  422. + /* inner */ { 23, 5, JT_INNER },
  423. + /* cross */ { 28, 5, JT_INNER|JT_CROSS },
  424. + };
  425. + int i, j;
  426. + apAll[0] = pA;
  427. + apAll[1] = pB;
  428. + apAll[2] = pC;
  429. + for(i=0; i<3 && apAll[i]; i++){
  430. + p = apAll[i];
  431. + for(j=0; j<ArraySize(aKeyword); j++){
  432. + if( p->n==aKeyword[j].nChar
  433. + && sqlite3StrNICmp((char*)p->z, &zKeyText[aKeyword[j].i], p->n)==0 ){
  434. + jointype |= aKeyword[j].code;
  435. + break;
  436. + }
  437. + }
  438. + testcase( j==0 || j==1 || j==2 || j==3 || j==4 || j==5 || j==6 );
  439. + if( j>=ArraySize(aKeyword) ){
  440. + jointype |= JT_ERROR;
  441. + break;
  442. + }
  443. + }
  444. + if(
  445. + (jointype & (JT_INNER|JT_OUTER))==(JT_INNER|JT_OUTER) ||
  446. + (jointype & JT_ERROR)!=0
  447. + ){
  448. + const char *zSp = " ";
  449. + assert( pB!=0 );
  450. + if( pC==0 ){ zSp++; }
  451. + sqlite3ErrorMsg(pParse, "unknown or unsupported join type: "
  452. + "%T %T%s%T", pA, pB, zSp, pC);
  453. + jointype = JT_INNER;
  454. + }else if( (jointype & JT_OUTER)!=0
  455. + && (jointype & (JT_LEFT|JT_RIGHT))!=JT_LEFT ){
  456. + sqlite3ErrorMsg(pParse,
  457. + "RIGHT and FULL OUTER JOINs are not currently supported");
  458. + jointype = JT_INNER;
  459. + }
  460. + return jointype;
  461. +}
  462. +
  463. +/*
  464. +** Return the index of a column in a table. Return -1 if the column
  465. +** is not contained in the table.
  466. +*/
  467. +static int columnIndex(Table *pTab, const char *zCol){
  468. + int i;
  469. + for(i=0; i<pTab->nCol; i++){
  470. + if( sqlite3StrICmp(pTab->aCol[i].zName, zCol)==0 ) return i;
  471. + }
  472. + return -1;
  473. +}
  474. +
  475. +/*
  476. +** Search the first N tables in pSrc, from left to right, looking for a
  477. +** table that has a column named zCol.
  478. +**
  479. +** When found, set *piTab and *piCol to the table index and column index
  480. +** of the matching column and return TRUE.
  481. +**
  482. +** If not found, return FALSE.
  483. +*/
  484. +static int tableAndColumnIndex(
  485. + SrcList *pSrc, /* Array of tables to search */
  486. + int N, /* Number of tables in pSrc->a[] to search */
  487. + const char *zCol, /* Name of the column we are looking for */
  488. + int *piTab, /* Write index of pSrc->a[] here */
  489. + int *piCol, /* Write index of pSrc->a[*piTab].pTab->aCol[] here */
  490. + int bIgnoreHidden /* True to ignore hidden columns */
  491. +){
  492. + int i; /* For looping over tables in pSrc */
  493. + int iCol; /* Index of column matching zCol */
  494. +
  495. + assert( (piTab==0)==(piCol==0) ); /* Both or neither are NULL */
  496. + for(i=0; i<N; i++){
  497. + iCol = columnIndex(pSrc->a[i].pTab, zCol);
  498. + if( iCol>=0
  499. + && (bIgnoreHidden==0 || IsHiddenColumn(&pSrc->a[i].pTab->aCol[iCol])==0)
  500. + ){
  501. + if( piTab ){
  502. + *piTab = i;
  503. + *piCol = iCol;
  504. + }
  505. + return 1;
  506. + }
  507. + }
  508. + return 0;
  509. +}
  510. +
  511. +/*
  512. +** This function is used to add terms implied by JOIN syntax to the
  513. +** WHERE clause expression of a SELECT statement. The new term, which
  514. +** is ANDed with the existing WHERE clause, is of the form:
  515. +**
  516. +** (tab1.col1 = tab2.col2)
  517. +**
  518. +** where tab1 is the iSrc'th table in SrcList pSrc and tab2 is the
  519. +** (iSrc+1)'th. Column col1 is column iColLeft of tab1, and col2 is
  520. +** column iColRight of tab2.
  521. +*/
  522. +static void addWhereTerm(
  523. + Parse *pParse, /* Parsing context */
  524. + SrcList *pSrc, /* List of tables in FROM clause */
  525. + int iLeft, /* Index of first table to join in pSrc */
  526. + int iColLeft, /* Index of column in first table */
  527. + int iRight, /* Index of second table in pSrc */
  528. + int iColRight, /* Index of column in second table */
  529. + int isOuterJoin, /* True if this is an OUTER join */
  530. + Expr **ppWhere /* IN/OUT: The WHERE clause to add to */
  531. +){
  532. + sqlite3 *db = pParse->db;
  533. + Expr *pE1;
  534. + Expr *pE2;
  535. + Expr *pEq;
  536. +
  537. + assert( iLeft<iRight );
  538. + assert( pSrc->nSrc>iRight );
  539. + assert( pSrc->a[iLeft].pTab );
  540. + assert( pSrc->a[iRight].pTab );
  541. +
  542. + pE1 = sqlite3CreateColumnExpr(db, pSrc, iLeft, iColLeft);
  543. + pE2 = sqlite3CreateColumnExpr(db, pSrc, iRight, iColRight);
  544. +
  545. + pEq = sqlite3PExpr(pParse, TK_EQ, pE1, pE2);
  546. + if( pEq && isOuterJoin ){
  547. + ExprSetProperty(pEq, EP_FromJoin);
  548. + assert( !ExprHasProperty(pEq, EP_TokenOnly|EP_Reduced) );
  549. + ExprSetVVAProperty(pEq, EP_NoReduce);
  550. + pEq->iRightJoinTable = (i16)pE2->iTable;
  551. + }
  552. + *ppWhere = sqlite3ExprAnd(pParse, *ppWhere, pEq);
  553. +}
  554. +
  555. +/*
  556. +** Set the EP_FromJoin property on all terms of the given expression.
  557. +** And set the Expr.iRightJoinTable to iTable for every term in the
  558. +** expression.
  559. +**
  560. +** The EP_FromJoin property is used on terms of an expression to tell
  561. +** the LEFT OUTER JOIN processing logic that this term is part of the
  562. +** join restriction specified in the ON or USING clause and not a part
  563. +** of the more general WHERE clause. These terms are moved over to the
  564. +** WHERE clause during join processing but we need to remember that they
  565. +** originated in the ON or USING clause.
  566. +**
  567. +** The Expr.iRightJoinTable tells the WHERE clause processing that the
  568. +** expression depends on table iRightJoinTable even if that table is not
  569. +** explicitly mentioned in the expression. That information is needed
  570. +** for cases like this:
  571. +**
  572. +** SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.b AND t1.x=5
  573. +**
  574. +** The where clause needs to defer the handling of the t1.x=5
  575. +** term until after the t2 loop of the join. In that way, a
  576. +** NULL t2 row will be inserted whenever t1.x!=5. If we do not
  577. +** defer the handling of t1.x=5, it will be processed immediately
  578. +** after the t1 loop and rows with t1.x!=5 will never appear in
  579. +** the output, which is incorrect.
  580. +*/
  581. +void sqlite3SetJoinExpr(Expr *p, int iTable){
  582. + while( p ){
  583. + ExprSetProperty(p, EP_FromJoin);
  584. + assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) );
  585. + ExprSetVVAProperty(p, EP_NoReduce);
  586. + p->iRightJoinTable = (i16)iTable;
  587. + if( p->op==TK_FUNCTION && p->x.pList ){
  588. + int i;
  589. + for(i=0; i<p->x.pList->nExpr; i++){
  590. + sqlite3SetJoinExpr(p->x.pList->a[i].pExpr, iTable);
  591. + }
  592. + }
  593. + sqlite3SetJoinExpr(p->pLeft, iTable);
  594. + p = p->pRight;
  595. + }
  596. +}
  597. +
  598. +/* Undo the work of sqlite3SetJoinExpr(). In the expression p, convert every
  599. +** term that is marked with EP_FromJoin and iRightJoinTable==iTable into
  600. +** an ordinary term that omits the EP_FromJoin mark.
  601. +**
  602. +** This happens when a LEFT JOIN is simplified into an ordinary JOIN.
  603. +*/
  604. +static void unsetJoinExpr(Expr *p, int iTable){
  605. + while( p ){
  606. + if( ExprHasProperty(p, EP_FromJoin)
  607. + && (iTable<0 || p->iRightJoinTable==iTable) ){
  608. + ExprClearProperty(p, EP_FromJoin);
  609. + }
  610. + if( p->op==TK_FUNCTION && p->x.pList ){
  611. + int i;
  612. + for(i=0; i<p->x.pList->nExpr; i++){
  613. + unsetJoinExpr(p->x.pList->a[i].pExpr, iTable);
  614. + }
  615. + }
  616. + unsetJoinExpr(p->pLeft, iTable);
  617. + p = p->pRight;
  618. + }
  619. +}
  620. +
  621. +/*
  622. +** This routine processes the join information for a SELECT statement.
  623. +** ON and USING clauses are converted into extra terms of the WHERE clause.
  624. +** NATURAL joins also create extra WHERE clause terms.
  625. +**
  626. +** The terms of a FROM clause are contained in the Select.pSrc structure.
  627. +** The left most table is the first entry in Select.pSrc. The right-most
  628. +** table is the last entry. The join operator is held in the entry to
  629. +** the left. Thus entry 0 contains the join operator for the join between
  630. +** entries 0 and 1. Any ON or USING clauses associated with the join are
  631. +** also attached to the left entry.
  632. +**
  633. +** This routine returns the number of errors encountered.
  634. +*/
  635. +static int sqliteProcessJoin(Parse *pParse, Select *p){
  636. + SrcList *pSrc; /* All tables in the FROM clause */
  637. + int i, j; /* Loop counters */
  638. + struct SrcList_item *pLeft; /* Left table being joined */
  639. + struct SrcList_item *pRight; /* Right table being joined */
  640. +
  641. + pSrc = p->pSrc;
  642. + pLeft = &pSrc->a[0];
  643. + pRight = &pLeft[1];
  644. + for(i=0; i<pSrc->nSrc-1; i++, pRight++, pLeft++){
  645. + Table *pRightTab = pRight->pTab;
  646. + int isOuter;
  647. +
  648. + if( NEVER(pLeft->pTab==0 || pRightTab==0) ) continue;
  649. + isOuter = (pRight->fg.jointype & JT_OUTER)!=0;
  650. +
  651. + /* When the NATURAL keyword is present, add WHERE clause terms for
  652. + ** every column that the two tables have in common.
  653. + */
  654. + if( pRight->fg.jointype & JT_NATURAL ){
  655. + if( pRight->pOn || pRight->pUsing ){
  656. + sqlite3ErrorMsg(pParse, "a NATURAL join may not have "
  657. + "an ON or USING clause", 0);
  658. + return 1;
  659. + }
  660. + for(j=0; j<pRightTab->nCol; j++){
  661. + char *zName; /* Name of column in the right table */
  662. + int iLeft; /* Matching left table */
  663. + int iLeftCol; /* Matching column in the left table */
  664. +
  665. + if( IsHiddenColumn(&pRightTab->aCol[j]) ) continue;
  666. + zName = pRightTab->aCol[j].zName;
  667. + if( tableAndColumnIndex(pSrc, i+1, zName, &iLeft, &iLeftCol, 1) ){
  668. + addWhereTerm(pParse, pSrc, iLeft, iLeftCol, i+1, j,
  669. + isOuter, &p->pWhere);
  670. + }
  671. + }
  672. + }
  673. +
  674. + /* Disallow both ON and USING clauses in the same join
  675. + */
  676. + if( pRight->pOn && pRight->pUsing ){
  677. + sqlite3ErrorMsg(pParse, "cannot have both ON and USING "
  678. + "clauses in the same join");
  679. + return 1;
  680. + }
  681. +
  682. + /* Add the ON clause to the end of the WHERE clause, connected by
  683. + ** an AND operator.
  684. + */
  685. + if( pRight->pOn ){
  686. + if( isOuter ) sqlite3SetJoinExpr(pRight->pOn, pRight->iCursor);
  687. + p->pWhere = sqlite3ExprAnd(pParse, p->pWhere, pRight->pOn);
  688. + pRight->pOn = 0;
  689. + }
  690. +
  691. + /* Create extra terms on the WHERE clause for each column named
  692. + ** in the USING clause. Example: If the two tables to be joined are
  693. + ** A and B and the USING clause names X, Y, and Z, then add this
  694. + ** to the WHERE clause: A.X=B.X AND A.Y=B.Y AND A.Z=B.Z
  695. + ** Report an error if any column mentioned in the USING clause is
  696. + ** not contained in both tables to be joined.
  697. + */
  698. + if( pRight->pUsing ){
  699. + IdList *pList = pRight->pUsing;
  700. + for(j=0; j<pList->nId; j++){
  701. + char *zName; /* Name of the term in the USING clause */
  702. + int iLeft; /* Table on the left with matching column name */
  703. + int iLeftCol; /* Column number of matching column on the left */
  704. + int iRightCol; /* Column number of matching column on the right */
  705. +
  706. + zName = pList->a[j].zName;
  707. + iRightCol = columnIndex(pRightTab, zName);
  708. + if( iRightCol<0
  709. + || !tableAndColumnIndex(pSrc, i+1, zName, &iLeft, &iLeftCol, 0)
  710. + ){
  711. + sqlite3ErrorMsg(pParse, "cannot join using column %s - column "
  712. + "not present in both tables", zName);
  713. + return 1;
  714. + }
  715. + addWhereTerm(pParse, pSrc, iLeft, iLeftCol, i+1, iRightCol,
  716. + isOuter, &p->pWhere);
  717. + }
  718. + }
  719. + }
  720. + return 0;
  721. +}
  722. +
  723. +/*
  724. +** An instance of this object holds information (beyond pParse and pSelect)
  725. +** needed to load the next result row that is to be added to the sorter.
  726. +*/
  727. +typedef struct RowLoadInfo RowLoadInfo;
  728. +struct RowLoadInfo {
  729. + int regResult; /* Store results in array of registers here */
  730. + u8 ecelFlags; /* Flag argument to ExprCodeExprList() */
  731. +#ifdef SQLITE_ENABLE_SORTER_REFERENCES
  732. + ExprList *pExtra; /* Extra columns needed by sorter refs */
  733. + int regExtraResult; /* Where to load the extra columns */
  734. +#endif
  735. +};
  736. +
  737. +/*
  738. +** This routine does the work of loading query data into an array of
  739. +** registers so that it can be added to the sorter.
  740. +*/
  741. +static void innerLoopLoadRow(
  742. + Parse *pParse, /* Statement under construction */
  743. + Select *pSelect, /* The query being coded */
  744. + RowLoadInfo *pInfo /* Info needed to complete the row load */
  745. +){
  746. + sqlite3ExprCodeExprList(pParse, pSelect->pEList, pInfo->regResult,
  747. + 0, pInfo->ecelFlags);
  748. +#ifdef SQLITE_ENABLE_SORTER_REFERENCES
  749. + if( pInfo->pExtra ){
  750. + sqlite3ExprCodeExprList(pParse, pInfo->pExtra, pInfo->regExtraResult, 0, 0);
  751. + sqlite3ExprListDelete(pParse->db, pInfo->pExtra);
  752. + }
  753. +#endif
  754. +}
  755. +
  756. +/*
  757. +** Code the OP_MakeRecord instruction that generates the entry to be
  758. +** added into the sorter.
  759. +**
  760. +** Return the register in which the result is stored.
  761. +*/
  762. +static int makeSorterRecord(
  763. + Parse *pParse,
  764. + SortCtx *pSort,
  765. + Select *pSelect,
  766. + int regBase,
  767. + int nBase
  768. +){
  769. + int nOBSat = pSort->nOBSat;
  770. + Vdbe *v = pParse->pVdbe;
  771. + int regOut = ++pParse->nMem;
  772. + if( pSort->pDeferredRowLoad ){
  773. + innerLoopLoadRow(pParse, pSelect, pSort->pDeferredRowLoad);
  774. + }
  775. + sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase+nOBSat, nBase-nOBSat, regOut);
  776. + return regOut;
  777. +}
  778. +
  779. +/*
  780. +** Generate code that will push the record in registers regData
  781. +** through regData+nData-1 onto the sorter.
  782. +*/
  783. +static void pushOntoSorter(
  784. + Parse *pParse, /* Parser context */
  785. + SortCtx *pSort, /* Information about the ORDER BY clause */
  786. + Select *pSelect, /* The whole SELECT statement */
  787. + int regData, /* First register holding data to be sorted */
  788. + int regOrigData, /* First register holding data before packing */
  789. + int nData, /* Number of elements in the regData data array */
  790. + int nPrefixReg /* No. of reg prior to regData available for use */
  791. +){
  792. + Vdbe *v = pParse->pVdbe; /* Stmt under construction */
  793. + int bSeq = ((pSort->sortFlags & SORTFLAG_UseSorter)==0);
  794. + int nExpr = pSort->pOrderBy->nExpr; /* No. of ORDER BY terms */
  795. + int nBase = nExpr + bSeq + nData; /* Fields in sorter record */
  796. + int regBase; /* Regs for sorter record */
  797. + int regRecord = 0; /* Assembled sorter record */
  798. + int nOBSat = pSort->nOBSat; /* ORDER BY terms to skip */
  799. + int op; /* Opcode to add sorter record to sorter */
  800. + int iLimit; /* LIMIT counter */
  801. + int iSkip = 0; /* End of the sorter insert loop */
  802. +
  803. + assert( bSeq==0 || bSeq==1 );
  804. +
  805. + /* Three cases:
  806. + ** (1) The data to be sorted has already been packed into a Record
  807. + ** by a prior OP_MakeRecord. In this case nData==1 and regData
  808. + ** will be completely unrelated to regOrigData.
  809. + ** (2) All output columns are included in the sort record. In that
  810. + ** case regData==regOrigData.
  811. + ** (3) Some output columns are omitted from the sort record due to
  812. + ** the SQLITE_ENABLE_SORTER_REFERENCE optimization, or due to the
  813. + ** SQLITE_ECEL_OMITREF optimization, or due to the
  814. + ** SortCtx.pDeferredRowLoad optimiation. In any of these cases
  815. + ** regOrigData is 0 to prevent this routine from trying to copy
  816. + ** values that might not yet exist.
  817. + */
  818. + assert( nData==1 || regData==regOrigData || regOrigData==0 );
  819. +
  820. + if( nPrefixReg ){
  821. + assert( nPrefixReg==nExpr+bSeq );
  822. + regBase = regData - nPrefixReg;
  823. + }else{
  824. + regBase = pParse->nMem + 1;
  825. + pParse->nMem += nBase;
  826. + }
  827. + assert( pSelect->iOffset==0 || pSelect->iLimit!=0 );
  828. + iLimit = pSelect->iOffset ? pSelect->iOffset+1 : pSelect->iLimit;
  829. + pSort->labelDone = sqlite3VdbeMakeLabel(pParse);
  830. + sqlite3ExprCodeExprList(pParse, pSort->pOrderBy, regBase, regOrigData,
  831. + SQLITE_ECEL_DUP | (regOrigData? SQLITE_ECEL_REF : 0));
  832. + if( bSeq ){
  833. + sqlite3VdbeAddOp2(v, OP_Sequence, pSort->iECursor, regBase+nExpr);
  834. + }
  835. + if( nPrefixReg==0 && nData>0 ){
  836. + sqlite3ExprCodeMove(pParse, regData, regBase+nExpr+bSeq, nData);
  837. + }
  838. + if( nOBSat>0 ){
  839. + int regPrevKey; /* The first nOBSat columns of the previous row */
  840. + int addrFirst; /* Address of the OP_IfNot opcode */
  841. + int addrJmp; /* Address of the OP_Jump opcode */
  842. + VdbeOp *pOp; /* Opcode that opens the sorter */
  843. + int nKey; /* Number of sorting key columns, including OP_Sequence */
  844. + KeyInfo *pKI; /* Original KeyInfo on the sorter table */
  845. +
  846. + regRecord = makeSorterRecord(pParse, pSort, pSelect, regBase, nBase);
  847. + regPrevKey = pParse->nMem+1;
  848. + pParse->nMem += pSort->nOBSat;
  849. + nKey = nExpr - pSort->nOBSat + bSeq;
  850. + if( bSeq ){
  851. + addrFirst = sqlite3VdbeAddOp1(v, OP_IfNot, regBase+nExpr);
  852. + }else{
  853. + addrFirst = sqlite3VdbeAddOp1(v, OP_SequenceTest, pSort->iECursor);
  854. + }
  855. + VdbeCoverage(v);
  856. + sqlite3VdbeAddOp3(v, OP_Compare, regPrevKey, regBase, pSort->nOBSat);
  857. + pOp = sqlite3VdbeGetOp(v, pSort->addrSortIndex);
  858. + if( pParse->db->mallocFailed ) return;
  859. + pOp->p2 = nKey + nData;
  860. + pKI = pOp->p4.pKeyInfo;
  861. + memset(pKI->aSortFlags, 0, pKI->nKeyField); /* Makes OP_Jump testable */
  862. + sqlite3VdbeChangeP4(v, -1, (char*)pKI, P4_KEYINFO);
  863. + testcase( pKI->nAllField > pKI->nKeyField+2 );
  864. + pOp->p4.pKeyInfo = sqlite3KeyInfoFromExprList(pParse,pSort->pOrderBy,nOBSat,
  865. + pKI->nAllField-pKI->nKeyField-1);
  866. + pOp = 0; /* Ensure pOp not used after sqltie3VdbeAddOp3() */
  867. + addrJmp = sqlite3VdbeCurrentAddr(v);
  868. + sqlite3VdbeAddOp3(v, OP_Jump, addrJmp+1, 0, addrJmp+1); VdbeCoverage(v);
  869. + pSort->labelBkOut = sqlite3VdbeMakeLabel(pParse);
  870. + pSort->regReturn = ++pParse->nMem;
  871. + sqlite3VdbeAddOp2(v, OP_Gosub, pSort->regReturn, pSort->labelBkOut);
  872. + sqlite3VdbeAddOp1(v, OP_ResetSorter, pSort->iECursor);
  873. + if( iLimit ){
  874. + sqlite3VdbeAddOp2(v, OP_IfNot, iLimit, pSort->labelDone);
  875. + VdbeCoverage(v);
  876. + }
  877. + sqlite3VdbeJumpHere(v, addrFirst);
  878. + sqlite3ExprCodeMove(pParse, regBase, regPrevKey, pSort->nOBSat);
  879. + sqlite3VdbeJumpHere(v, addrJmp);
  880. + }
  881. + if( iLimit ){
  882. + /* At this point the values for the new sorter entry are stored
  883. + ** in an array of registers. They need to be composed into a record
  884. + ** and inserted into the sorter if either (a) there are currently
  885. + ** less than LIMIT+OFFSET items or (b) the new record is smaller than
  886. + ** the largest record currently in the sorter. If (b) is true and there
  887. + ** are already LIMIT+OFFSET items in the sorter, delete the largest
  888. + ** entry before inserting the new one. This way there are never more
  889. + ** than LIMIT+OFFSET items in the sorter.
  890. + **
  891. + ** If the new record does not need to be inserted into the sorter,
  892. + ** jump to the next iteration of the loop. If the pSort->labelOBLopt
  893. + ** value is not zero, then it is a label of where to jump. Otherwise,
  894. + ** just bypass the row insert logic. See the header comment on the
  895. + ** sqlite3WhereOrderByLimitOptLabel() function for additional info.
  896. + */
  897. + int iCsr = pSort->iECursor;
  898. + sqlite3VdbeAddOp2(v, OP_IfNotZero, iLimit, sqlite3VdbeCurrentAddr(v)+4);
  899. + VdbeCoverage(v);
  900. + sqlite3VdbeAddOp2(v, OP_Last, iCsr, 0);
  901. + iSkip = sqlite3VdbeAddOp4Int(v, OP_IdxLE,
  902. + iCsr, 0, regBase+nOBSat, nExpr-nOBSat);
  903. + VdbeCoverage(v);
  904. + sqlite3VdbeAddOp1(v, OP_Delete, iCsr);
  905. + }
  906. + if( regRecord==0 ){
  907. + regRecord = makeSorterRecord(pParse, pSort, pSelect, regBase, nBase);
  908. + }
  909. + if( pSort->sortFlags & SORTFLAG_UseSorter ){
  910. + op = OP_SorterInsert;
  911. + }else{
  912. + op = OP_IdxInsert;
  913. + }
  914. + sqlite3VdbeAddOp4Int(v, op, pSort->iECursor, regRecord,
  915. + regBase+nOBSat, nBase-nOBSat);
  916. + if( iSkip ){
  917. + sqlite3VdbeChangeP2(v, iSkip,
  918. + pSort->labelOBLopt ? pSort->labelOBLopt : sqlite3VdbeCurrentAddr(v));
  919. + }
  920. +}
  921. +
  922. +/*
  923. +** Add code to implement the OFFSET
  924. +*/
  925. +static void codeOffset(
  926. + Vdbe *v, /* Generate code into this VM */
  927. + int iOffset, /* Register holding the offset counter */
  928. + int iContinue /* Jump here to skip the current record */
  929. +){
  930. + if( iOffset>0 ){
  931. + sqlite3VdbeAddOp3(v, OP_IfPos, iOffset, iContinue, 1); VdbeCoverage(v);
  932. + VdbeComment((v, "OFFSET"));
  933. + }
  934. +}
  935. +
  936. +/*
  937. +** Add code that will check to make sure the N registers starting at iMem
  938. +** form a distinct entry. iTab is a sorting index that holds previously
  939. +** seen combinations of the N values. A new entry is made in iTab
  940. +** if the current N values are new.
  941. +**
  942. +** A jump to addrRepeat is made and the N+1 values are popped from the
  943. +** stack if the top N elements are not distinct.
  944. +*/
  945. +static void codeDistinct(
  946. + Parse *pParse, /* Parsing and code generating context */
  947. + int iTab, /* A sorting index used to test for distinctness */
  948. + int addrRepeat, /* Jump to here if not distinct */
  949. + int N, /* Number of elements */
  950. + int iMem /* First element */
  951. +){
  952. + Vdbe *v;
  953. + int r1;
  954. +
  955. + v = pParse->pVdbe;
  956. + r1 = sqlite3GetTempReg(pParse);
  957. + sqlite3VdbeAddOp4Int(v, OP_Found, iTab, addrRepeat, iMem, N); VdbeCoverage(v);
  958. + sqlite3VdbeAddOp3(v, OP_MakeRecord, iMem, N, r1);
  959. + sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iTab, r1, iMem, N);
  960. + sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
  961. + sqlite3ReleaseTempReg(pParse, r1);
  962. +}
  963. +
  964. +#ifdef SQLITE_ENABLE_SORTER_REFERENCES
  965. +/*
  966. +** This function is called as part of inner-loop generation for a SELECT
  967. +** statement with an ORDER BY that is not optimized by an index. It
  968. +** determines the expressions, if any, that the sorter-reference
  969. +** optimization should be used for. The sorter-reference optimization
  970. +** is used for SELECT queries like:
  971. +**
  972. +** SELECT a, bigblob FROM t1 ORDER BY a LIMIT 10
  973. +**
  974. +** If the optimization is used for expression "bigblob", then instead of
  975. +** storing values read from that column in the sorter records, the PK of
  976. +** the row from table t1 is stored instead. Then, as records are extracted from
  977. +** the sorter to return to the user, the required value of bigblob is
  978. +** retrieved directly from table t1. If the values are very large, this
  979. +** can be more efficient than storing them directly in the sorter records.
  980. +**
  981. +** The ExprList_item.bSorterRef flag is set for each expression in pEList
  982. +** for which the sorter-reference optimization should be enabled.
  983. +** Additionally, the pSort->aDefer[] array is populated with entries
  984. +** for all cursors required to evaluate all selected expressions. Finally.
  985. +** output variable (*ppExtra) is set to an expression list containing
  986. +** expressions for all extra PK values that should be stored in the
  987. +** sorter records.
  988. +*/
  989. +static void selectExprDefer(
  990. + Parse *pParse, /* Leave any error here */
  991. + SortCtx *pSort, /* Sorter context */
  992. + ExprList *pEList, /* Expressions destined for sorter */
  993. + ExprList **ppExtra /* Expressions to append to sorter record */
  994. +){
  995. + int i;
  996. + int nDefer = 0;
  997. + ExprList *pExtra = 0;
  998. + for(i=0; i<pEList->nExpr; i++){
  999. + struct ExprList_item *pItem = &pEList->a[i];
  1000. + if( pItem->u.x.iOrderByCol==0 ){
  1001. + Expr *pExpr = pItem->pExpr;
  1002. + Table *pTab = pExpr->y.pTab;
  1003. + if( pExpr->op==TK_COLUMN && pExpr->iColumn>=0 && pTab && !IsVirtual(pTab)
  1004. + && (pTab->aCol[pExpr->iColumn].colFlags & COLFLAG_SORTERREF)
  1005. + ){
  1006. + int j;
  1007. + for(j=0; j<nDefer; j++){
  1008. + if( pSort->aDefer[j].iCsr==pExpr->iTable ) break;
  1009. + }
  1010. + if( j==nDefer ){
  1011. + if( nDefer==ArraySize(pSort->aDefer) ){
  1012. + continue;
  1013. + }else{
  1014. + int nKey = 1;
  1015. + int k;
  1016. + Index *pPk = 0;
  1017. + if( !HasRowid(pTab) ){
  1018. + pPk = sqlite3PrimaryKeyIndex(pTab);
  1019. + nKey = pPk->nKeyCol;
  1020. + }
  1021. + for(k=0; k<nKey; k++){
  1022. + Expr *pNew = sqlite3PExpr(pParse, TK_COLUMN, 0, 0);
  1023. + if( pNew ){
  1024. + pNew->iTable = pExpr->iTable;
  1025. + pNew->y.pTab = pExpr->y.pTab;
  1026. + pNew->iColumn = pPk ? pPk->aiColumn[k] : -1;
  1027. + pExtra = sqlite3ExprListAppend(pParse, pExtra, pNew);
  1028. + }
  1029. + }
  1030. + pSort->aDefer[nDefer].pTab = pExpr->y.pTab;
  1031. + pSort->aDefer[nDefer].iCsr = pExpr->iTable;
  1032. + pSort->aDefer[nDefer].nKey = nKey;
  1033. + nDefer++;
  1034. + }
  1035. + }
  1036. + pItem->bSorterRef = 1;
  1037. + }
  1038. + }
  1039. + }
  1040. + pSort->nDefer = (u8)nDefer;
  1041. + *ppExtra = pExtra;
  1042. +}
  1043. +#endif
  1044. +
  1045. +/*
  1046. +** This routine generates the code for the inside of the inner loop
  1047. +** of a SELECT.
  1048. +**
  1049. +** If srcTab is negative, then the p->pEList expressions
  1050. +** are evaluated in order to get the data for this row. If srcTab is
  1051. +** zero or more, then data is pulled from srcTab and p->pEList is used only
  1052. +** to get the number of columns and the collation sequence for each column.
  1053. +*/
  1054. +static void selectInnerLoop(
  1055. + Parse *pParse, /* The parser context */
  1056. + Select *p, /* The complete select statement being coded */
  1057. + int srcTab, /* Pull data from this table if non-negative */
  1058. + SortCtx *pSort, /* If not NULL, info on how to process ORDER BY */
  1059. + DistinctCtx *pDistinct, /* If not NULL, info on how to process DISTINCT */
  1060. + SelectDest *pDest, /* How to dispose of the results */
  1061. + int iContinue, /* Jump here to continue with next row */
  1062. + int iBreak /* Jump here to break out of the inner loop */
  1063. +){
  1064. + Vdbe *v = pParse->pVdbe;
  1065. + int i;
  1066. + int hasDistinct; /* True if the DISTINCT keyword is present */
  1067. + int eDest = pDest->eDest; /* How to dispose of results */
  1068. + int iParm = pDest->iSDParm; /* First argument to disposal method */
  1069. + int nResultCol; /* Number of result columns */
  1070. + int nPrefixReg = 0; /* Number of extra registers before regResult */
  1071. + RowLoadInfo sRowLoadInfo; /* Info for deferred row loading */
  1072. +
  1073. + /* Usually, regResult is the first cell in an array of memory cells
  1074. + ** containing the current result row. In this case regOrig is set to the
  1075. + ** same value. However, if the results are being sent to the sorter, the
  1076. + ** values for any expressions that are also part of the sort-key are omitted
  1077. + ** from this array. In this case regOrig is set to zero. */
  1078. + int regResult; /* Start of memory holding current results */
  1079. + int regOrig; /* Start of memory holding full result (or 0) */
  1080. +
  1081. + assert( v );
  1082. + assert( p->pEList!=0 );
  1083. + hasDistinct = pDistinct ? pDistinct->eTnctType : WHERE_DISTINCT_NOOP;
  1084. + if( pSort && pSort->pOrderBy==0 ) pSort = 0;
  1085. + if( pSort==0 && !hasDistinct ){
  1086. + assert( iContinue!=0 );
  1087. + codeOffset(v, p->iOffset, iContinue);
  1088. + }
  1089. +
  1090. + /* Pull the requested columns.
  1091. + */
  1092. + nResultCol = p->pEList->nExpr;
  1093. +
  1094. + if( pDest->iSdst==0 ){
  1095. + if( pSort ){
  1096. + nPrefixReg = pSort->pOrderBy->nExpr;
  1097. + if( !(pSort->sortFlags & SORTFLAG_UseSorter) ) nPrefixReg++;
  1098. + pParse->nMem += nPrefixReg;
  1099. + }
  1100. + pDest->iSdst = pParse->nMem+1;
  1101. + pParse->nMem += nResultCol;
  1102. + }else if( pDest->iSdst+nResultCol > pParse->nMem ){
  1103. + /* This is an error condition that can result, for example, when a SELECT
  1104. + ** on the right-hand side of an INSERT contains more result columns than
  1105. + ** there are columns in the table on the left. The error will be caught
  1106. + ** and reported later. But we need to make sure enough memory is allocated
  1107. + ** to avoid other spurious errors in the meantime. */
  1108. + pParse->nMem += nResultCol;
  1109. + }
  1110. + pDest->nSdst = nResultCol;
  1111. + regOrig = regResult = pDest->iSdst;
  1112. + if( srcTab>=0 ){
  1113. + for(i=0; i<nResultCol; i++){
  1114. + sqlite3VdbeAddOp3(v, OP_Column, srcTab, i, regResult+i);
  1115. + VdbeComment((v, "%s", p->pEList->a[i].zEName));
  1116. + }
  1117. + }else if( eDest!=SRT_Exists ){
  1118. +#ifdef SQLITE_ENABLE_SORTER_REFERENCES
  1119. + ExprList *pExtra = 0;
  1120. +#endif
  1121. + /* If the destination is an EXISTS(...) expression, the actual
  1122. + ** values returned by the SELECT are not required.
  1123. + */
  1124. + u8 ecelFlags; /* "ecel" is an abbreviation of "ExprCodeExprList" */
  1125. + ExprList *pEList;
  1126. + if( eDest==SRT_Mem || eDest==SRT_Output || eDest==SRT_Coroutine ){
  1127. + ecelFlags = SQLITE_ECEL_DUP;
  1128. + }else{
  1129. + ecelFlags = 0;
  1130. + }
  1131. + if( pSort && hasDistinct==0 && eDest!=SRT_EphemTab && eDest!=SRT_Table ){
  1132. + /* For each expression in p->pEList that is a copy of an expression in
  1133. + ** the ORDER BY clause (pSort->pOrderBy), set the associated
  1134. + ** iOrderByCol value to one more than the index of the ORDER BY
  1135. + ** expression within the sort-key that pushOntoSorter() will generate.
  1136. + ** This allows the p->pEList field to be omitted from the sorted record,
  1137. + ** saving space and CPU cycles. */
  1138. + ecelFlags |= (SQLITE_ECEL_OMITREF|SQLITE_ECEL_REF);
  1139. +
  1140. + for(i=pSort->nOBSat; i<pSort->pOrderBy->nExpr; i++){
  1141. + int j;
  1142. + if( (j = pSort->pOrderBy->a[i].u.x.iOrderByCol)>0 ){
  1143. + p->pEList->a[j-1].u.x.iOrderByCol = i+1-pSort->nOBSat;
  1144. + }
  1145. + }
  1146. +#ifdef SQLITE_ENABLE_SORTER_REFERENCES
  1147. + selectExprDefer(pParse, pSort, p->pEList, &pExtra);
  1148. + if( pExtra && pParse->db->mallocFailed==0 ){
  1149. + /* If there are any extra PK columns to add to the sorter records,
  1150. + ** allocate extra memory cells and adjust the OpenEphemeral
  1151. + ** instruction to account for the larger records. This is only
  1152. + ** required if there are one or more WITHOUT ROWID tables with
  1153. + ** composite primary keys in the SortCtx.aDefer[] array. */
  1154. + VdbeOp *pOp = sqlite3VdbeGetOp(v, pSort->addrSortIndex);
  1155. + pOp->p2 += (pExtra->nExpr - pSort->nDefer);
  1156. + pOp->p4.pKeyInfo->nAllField += (pExtra->nExpr - pSort->nDefer);
  1157. + pParse->nMem += pExtra->nExpr;
  1158. + }
  1159. +#endif
  1160. +
  1161. + /* Adjust nResultCol to account for columns that are omitted
  1162. + ** from the sorter by the optimizations in this branch */
  1163. + pEList = p->pEList;
  1164. + for(i=0; i<pEList->nExpr; i++){
  1165. + if( pEList->a[i].u.x.iOrderByCol>0
  1166. +#ifdef SQLITE_ENABLE_SORTER_REFERENCES
  1167. + || pEList->a[i].bSorterRef
  1168. +#endif
  1169. + ){
  1170. + nResultCol--;
  1171. + regOrig = 0;
  1172. + }
  1173. + }
  1174. +
  1175. + testcase( regOrig );
  1176. + testcase( eDest==SRT_Set );
  1177. + testcase( eDest==SRT_Mem );
  1178. + testcase( eDest==SRT_Coroutine );
  1179. + testcase( eDest==SRT_Output );
  1180. + assert( eDest==SRT_Set || eDest==SRT_Mem
  1181. + || eDest==SRT_Coroutine || eDest==SRT_Output );
  1182. + }
  1183. + sRowLoadInfo.regResult = regResult;
  1184. + sRowLoadInfo.ecelFlags = ecelFlags;
  1185. +#ifdef SQLITE_ENABLE_SORTER_REFERENCES
  1186. + sRowLoadInfo.pExtra = pExtra;
  1187. + sRowLoadInfo.regExtraResult = regResult + nResultCol;
  1188. + if( pExtra ) nResultCol += pExtra->nExpr;
  1189. +#endif
  1190. + if( p->iLimit
  1191. + && (ecelFlags & SQLITE_ECEL_OMITREF)!=0
  1192. + && nPrefixReg>0
  1193. + ){
  1194. + assert( pSort!=0 );
  1195. + assert( hasDistinct==0 );
  1196. + pSort->pDeferredRowLoad = &sRowLoadInfo;
  1197. + regOrig = 0;
  1198. + }else{
  1199. + innerLoopLoadRow(pParse, p, &sRowLoadInfo);
  1200. + }
  1201. + }
  1202. +
  1203. + /* If the DISTINCT keyword was present on the SELECT statement
  1204. + ** and this row has been seen before, then do not make this row
  1205. + ** part of the result.
  1206. + */
  1207. + if( hasDistinct ){
  1208. + switch( pDistinct->eTnctType ){
  1209. + case WHERE_DISTINCT_ORDERED: {
  1210. + VdbeOp *pOp; /* No longer required OpenEphemeral instr. */
  1211. + int iJump; /* Jump destination */
  1212. + int regPrev; /* Previous row content */
  1213. +
  1214. + /* Allocate space for the previous row */
  1215. + regPrev = pParse->nMem+1;
  1216. + pParse->nMem += nResultCol;
  1217. +
  1218. + /* Change the OP_OpenEphemeral coded earlier to an OP_Null
  1219. + ** sets the MEM_Cleared bit on the first register of the
  1220. + ** previous value. This will cause the OP_Ne below to always
  1221. + ** fail on the first iteration of the loop even if the first
  1222. + ** row is all NULLs.
  1223. + */
  1224. + sqlite3VdbeChangeToNoop(v, pDistinct->addrTnct);
  1225. + pOp = sqlite3VdbeGetOp(v, pDistinct->addrTnct);
  1226. + pOp->opcode = OP_Null;
  1227. + pOp->p1 = 1;
  1228. + pOp->p2 = regPrev;
  1229. + pOp = 0; /* Ensure pOp is not used after sqlite3VdbeAddOp() */
  1230. +
  1231. + iJump = sqlite3VdbeCurrentAddr(v) + nResultCol;
  1232. + for(i=0; i<nResultCol; i++){
  1233. + CollSeq *pColl = sqlite3ExprCollSeq(pParse, p->pEList->a[i].pExpr);
  1234. + if( i<nResultCol-1 ){
  1235. + sqlite3VdbeAddOp3(v, OP_Ne, regResult+i, iJump, regPrev+i);
  1236. + VdbeCoverage(v);
  1237. + }else{
  1238. + sqlite3VdbeAddOp3(v, OP_Eq, regResult+i, iContinue, regPrev+i);
  1239. + VdbeCoverage(v);
  1240. + }
  1241. + sqlite3VdbeChangeP4(v, -1, (const char *)pColl, P4_COLLSEQ);
  1242. + sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
  1243. + }
  1244. + assert( sqlite3VdbeCurrentAddr(v)==iJump || pParse->db->mallocFailed );
  1245. + sqlite3VdbeAddOp3(v, OP_Copy, regResult, regPrev, nResultCol-1);
  1246. + break;
  1247. + }
  1248. +
  1249. + case WHERE_DISTINCT_UNIQUE: {
  1250. + sqlite3VdbeChangeToNoop(v, pDistinct->addrTnct);
  1251. + break;
  1252. + }
  1253. +
  1254. + default: {
  1255. + assert( pDistinct->eTnctType==WHERE_DISTINCT_UNORDERED );
  1256. + codeDistinct(pParse, pDistinct->tabTnct, iContinue, nResultCol,
  1257. + regResult);
  1258. + break;
  1259. + }
  1260. + }
  1261. + if( pSort==0 ){
  1262. + codeOffset(v, p->iOffset, iContinue);
  1263. + }
  1264. + }
  1265. +
  1266. + switch( eDest ){
  1267. + /* In this mode, write each query result to the key of the temporary
  1268. + ** table iParm.
  1269. + */
  1270. +#ifndef SQLITE_OMIT_COMPOUND_SELECT
  1271. + case SRT_Union: {
  1272. + int r1;
  1273. + r1 = sqlite3GetTempReg(pParse);
  1274. + sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nResultCol, r1);
  1275. + sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm, r1, regResult, nResultCol);
  1276. + sqlite3ReleaseTempReg(pParse, r1);
  1277. + break;
  1278. + }
  1279. +
  1280. + /* Construct a record from the query result, but instead of
  1281. + ** saving that record, use it as a key to delete elements from
  1282. + ** the temporary table iParm.
  1283. + */
  1284. + case SRT_Except: {
  1285. + sqlite3VdbeAddOp3(v, OP_IdxDelete, iParm, regResult, nResultCol);
  1286. + break;
  1287. + }
  1288. +#endif /* SQLITE_OMIT_COMPOUND_SELECT */
  1289. +
  1290. + /* Store the result as data using a unique key.
  1291. + */
  1292. + case SRT_Fifo:
  1293. + case SRT_DistFifo:
  1294. + case SRT_Table:
  1295. + case SRT_EphemTab: {
  1296. + int r1 = sqlite3GetTempRange(pParse, nPrefixReg+1);
  1297. + testcase( eDest==SRT_Table );
  1298. + testcase( eDest==SRT_EphemTab );
  1299. + testcase( eDest==SRT_Fifo );
  1300. + testcase( eDest==SRT_DistFifo );
  1301. + sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nResultCol, r1+nPrefixReg);
  1302. +#ifndef SQLITE_OMIT_CTE
  1303. + if( eDest==SRT_DistFifo ){
  1304. + /* If the destination is DistFifo, then cursor (iParm+1) is open
  1305. + ** on an ephemeral index. If the current row is already present
  1306. + ** in the index, do not write it to the output. If not, add the
  1307. + ** current row to the index and proceed with writing it to the
  1308. + ** output table as well. */
  1309. + int addr = sqlite3VdbeCurrentAddr(v) + 4;
  1310. + sqlite3VdbeAddOp4Int(v, OP_Found, iParm+1, addr, r1, 0);
  1311. + VdbeCoverage(v);
  1312. + sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm+1, r1,regResult,nResultCol);
  1313. + assert( pSort==0 );
  1314. + }
  1315. +#endif
  1316. + if( pSort ){
  1317. + assert( regResult==regOrig );
  1318. + pushOntoSorter(pParse, pSort, p, r1+nPrefixReg, regOrig, 1, nPrefixReg);
  1319. + }else{
  1320. + int r2 = sqlite3GetTempReg(pParse);
  1321. + sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, r2);
  1322. + sqlite3VdbeAddOp3(v, OP_Insert, iParm, r1, r2);
  1323. + sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
  1324. + sqlite3ReleaseTempReg(pParse, r2);
  1325. + }
  1326. + sqlite3ReleaseTempRange(pParse, r1, nPrefixReg+1);
  1327. + break;
  1328. + }
  1329. +
  1330. +#ifndef SQLITE_OMIT_SUBQUERY
  1331. + /* If we are creating a set for an "expr IN (SELECT ...)" construct,
  1332. + ** then there should be a single item on the stack. Write this
  1333. + ** item into the set table with bogus data.
  1334. + */
  1335. + case SRT_Set: {
  1336. + if( pSort ){
  1337. + /* At first glance you would think we could optimize out the
  1338. + ** ORDER BY in this case since the order of entries in the set
  1339. + ** does not matter. But there might be a LIMIT clause, in which
  1340. + ** case the order does matter */
  1341. + pushOntoSorter(
  1342. + pParse, pSort, p, regResult, regOrig, nResultCol, nPrefixReg);
  1343. + }else{
  1344. + int r1 = sqlite3GetTempReg(pParse);
  1345. + assert( sqlite3Strlen30(pDest->zAffSdst)==nResultCol );
  1346. + sqlite3VdbeAddOp4(v, OP_MakeRecord, regResult, nResultCol,
  1347. + r1, pDest->zAffSdst, nResultCol);
  1348. + sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm, r1, regResult, nResultCol);
  1349. + sqlite3ReleaseTempReg(pParse, r1);
  1350. + }
  1351. + break;
  1352. + }
  1353. +
  1354. + /* If any row exist in the result set, record that fact and abort.
  1355. + */
  1356. + case SRT_Exists: {
  1357. + sqlite3VdbeAddOp2(v, OP_Integer, 1, iParm);
  1358. + /* The LIMIT clause will terminate the loop for us */
  1359. + break;
  1360. + }
  1361. +
  1362. + /* If this is a scalar select that is part of an expression, then
  1363. + ** store the results in the appropriate memory cell or array of
  1364. + ** memory cells and break out of the scan loop.
  1365. + */
  1366. + case SRT_Mem: {
  1367. + if( pSort ){
  1368. + assert( nResultCol<=pDest->nSdst );
  1369. + pushOntoSorter(
  1370. + pParse, pSort, p, regResult, regOrig, nResultCol, nPrefixReg);
  1371. + }else{
  1372. + assert( nResultCol==pDest->nSdst );
  1373. + assert( regResult==iParm );
  1374. + /* The LIMIT clause will jump out of the loop for us */
  1375. + }
  1376. + break;
  1377. + }
  1378. +#endif /* #ifndef SQLITE_OMIT_SUBQUERY */
  1379. +
  1380. + case SRT_Coroutine: /* Send data to a co-routine */
  1381. + case SRT_Output: { /* Return the results */
  1382. + testcase( eDest==SRT_Coroutine );
  1383. + testcase( eDest==SRT_Output );
  1384. + if( pSort ){
  1385. + pushOntoSorter(pParse, pSort, p, regResult, regOrig, nResultCol,
  1386. + nPrefixReg);
  1387. + }else if( eDest==SRT_Coroutine ){
  1388. + sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
  1389. + }else{
  1390. + sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, nResultCol);
  1391. + }
  1392. + break;
  1393. + }
  1394. +
  1395. +#ifndef SQLITE_OMIT_CTE
  1396. + /* Write the results into a priority queue that is order according to
  1397. + ** pDest->pOrderBy (in pSO). pDest->iSDParm (in iParm) is the cursor for an
  1398. + ** index with pSO->nExpr+2 columns. Build a key using pSO for the first
  1399. + ** pSO->nExpr columns, then make sure all keys are unique by adding a
  1400. + ** final OP_Sequence column. The last column is the record as a blob.
  1401. + */
  1402. + case SRT_DistQueue:
  1403. + case SRT_Queue: {
  1404. + int nKey;
  1405. + int r1, r2, r3;
  1406. + int addrTest = 0;
  1407. + ExprList *pSO;
  1408. + pSO = pDest->pOrderBy;
  1409. + assert( pSO );
  1410. + nKey = pSO->nExpr;
  1411. + r1 = sqlite3GetTempReg(pParse);
  1412. + r2 = sqlite3GetTempRange(pParse, nKey+2);
  1413. + r3 = r2+nKey+1;
  1414. + if( eDest==SRT_DistQueue ){
  1415. + /* If the destination is DistQueue, then cursor (iParm+1) is open
  1416. + ** on a second ephemeral index that holds all values every previously
  1417. + ** added to the queue. */
  1418. + addrTest = sqlite3VdbeAddOp4Int(v, OP_Found, iParm+1, 0,
  1419. + regResult, nResultCol);
  1420. + VdbeCoverage(v);
  1421. + }
  1422. + sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nResultCol, r3);
  1423. + if( eDest==SRT_DistQueue ){
  1424. + sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm+1, r3);
  1425. + sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
  1426. + }
  1427. + for(i=0; i<nKey; i++){
  1428. + sqlite3VdbeAddOp2(v, OP_SCopy,
  1429. + regResult + pSO->a[i].u.x.iOrderByCol - 1,
  1430. + r2+i);
  1431. + }
  1432. + sqlite3VdbeAddOp2(v, OP_Sequence, iParm, r2+nKey);
  1433. + sqlite3VdbeAddOp3(v, OP_MakeRecord, r2, nKey+2, r1);
  1434. + sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm, r1, r2, nKey+2);
  1435. + if( addrTest ) sqlite3VdbeJumpHere(v, addrTest);
  1436. + sqlite3ReleaseTempReg(pParse, r1);
  1437. + sqlite3ReleaseTempRange(pParse, r2, nKey+2);
  1438. + break;
  1439. + }
  1440. +#endif /* SQLITE_OMIT_CTE */
  1441. +
  1442. +
  1443. +
  1444. +#if !defined(SQLITE_OMIT_TRIGGER)
  1445. + /* Discard the results. This is used for SELECT statements inside
  1446. + ** the body of a TRIGGER. The purpose of such selects is to call
  1447. + ** user-defined functions that have side effects. We do not care
  1448. + ** about the actual results of the select.
  1449. + */
  1450. + default: {
  1451. + assert( eDest==SRT_Discard );
  1452. + break;
  1453. + }
  1454. +#endif
  1455. + }
  1456. +
  1457. + /* Jump to the end of the loop if the LIMIT is reached. Except, if
  1458. + ** there is a sorter, in which case the sorter has already limited
  1459. + ** the output for us.
  1460. + */
  1461. + if( pSort==0 && p->iLimit ){
  1462. + sqlite3VdbeAddOp2(v, OP_DecrJumpZero, p->iLimit, iBreak); VdbeCoverage(v);
  1463. + }
  1464. +}
  1465. +
  1466. +/*
  1467. +** Allocate a KeyInfo object sufficient for an index of N key columns and
  1468. +** X extra columns.
  1469. +*/
  1470. +KeyInfo *sqlite3KeyInfoAlloc(sqlite3 *db, int N, int X){
  1471. + int nExtra = (N+X)*(sizeof(CollSeq*)+1) - sizeof(CollSeq*);
  1472. + KeyInfo *p = sqlite3DbMallocRawNN(db, sizeof(KeyInfo) + nExtra);
  1473. + if( p ){
  1474. + p->aSortFlags = (u8*)&p->aColl[N+X];
  1475. + p->nKeyField = (u16)N;
  1476. + p->nAllField = (u16)(N+X);
  1477. + p->enc = ENC(db);
  1478. + p->db = db;
  1479. + p->nRef = 1;
  1480. + memset(&p[1], 0, nExtra);
  1481. + }else{
  1482. + sqlite3OomFault(db);
  1483. + }
  1484. + return p;
  1485. +}
  1486. +
  1487. +/*
  1488. +** Deallocate a KeyInfo object
  1489. +*/
  1490. +void sqlite3KeyInfoUnref(KeyInfo *p){
  1491. + if( p ){
  1492. + assert( p->nRef>0 );
  1493. + p->nRef--;
  1494. + if( p->nRef==0 ) sqlite3DbFreeNN(p->db, p);
  1495. + }
  1496. +}
  1497. +
  1498. +/*
  1499. +** Make a new pointer to a KeyInfo object
  1500. +*/
  1501. +KeyInfo *sqlite3KeyInfoRef(KeyInfo *p){
  1502. + if( p ){
  1503. + assert( p->nRef>0 );
  1504. + p->nRef++;
  1505. + }
  1506. + return p;
  1507. +}
  1508. +
  1509. +#ifdef SQLITE_DEBUG
  1510. +/*
  1511. +** Return TRUE if a KeyInfo object can be change. The KeyInfo object
  1512. +** can only be changed if this is just a single reference to the object.
  1513. +**
  1514. +** This routine is used only inside of assert() statements.
  1515. +*/
  1516. +int sqlite3KeyInfoIsWriteable(KeyInfo *p){ return p->nRef==1; }
  1517. +#endif /* SQLITE_DEBUG */
  1518. +
  1519. +/*
  1520. +** Given an expression list, generate a KeyInfo structure that records
  1521. +** the collating sequence for each expression in that expression list.
  1522. +**
  1523. +** If the ExprList is an ORDER BY or GROUP BY clause then the resulting
  1524. +** KeyInfo structure is appropriate for initializing a virtual index to
  1525. +** implement that clause. If the ExprList is the result set of a SELECT
  1526. +** then the KeyInfo structure is appropriate for initializing a virtual
  1527. +** index to implement a DISTINCT test.
  1528. +**
  1529. +** Space to hold the KeyInfo structure is obtained from malloc. The calling
  1530. +** function is responsible for seeing that this structure is eventually
  1531. +** freed.
  1532. +*/
  1533. +KeyInfo *sqlite3KeyInfoFromExprList(
  1534. + Parse *pParse, /* Parsing context */
  1535. + ExprList *pList, /* Form the KeyInfo object from this ExprList */
  1536. + int iStart, /* Begin with this column of pList */
  1537. + int nExtra /* Add this many extra columns to the end */
  1538. +){
  1539. + int nExpr;
  1540. + KeyInfo *pInfo;
  1541. + struct ExprList_item *pItem;
  1542. + sqlite3 *db = pParse->db;
  1543. + int i;
  1544. +
  1545. + nExpr = pList->nExpr;
  1546. + pInfo = sqlite3KeyInfoAlloc(db, nExpr-iStart, nExtra+1);
  1547. + if( pInfo ){
  1548. + assert( sqlite3KeyInfoIsWriteable(pInfo) );
  1549. + for(i=iStart, pItem=pList->a+iStart; i<nExpr; i++, pItem++){
  1550. + pInfo->aColl[i-iStart] = sqlite3ExprNNCollSeq(pParse, pItem->pExpr);
  1551. + pInfo->aSortFlags[i-iStart] = pItem->sortFlags;
  1552. + }
  1553. + }
  1554. + return pInfo;
  1555. +}
  1556. +
  1557. +/*
  1558. +** Name of the connection operator, used for error messages.
  1559. +*/
  1560. +static const char *selectOpName(int id){
  1561. + char *z;
  1562. + switch( id ){
  1563. + case TK_ALL: z = "UNION ALL"; break;
  1564. + case TK_INTERSECT: z = "INTERSECT"; break;
  1565. + case TK_EXCEPT: z = "EXCEPT"; break;
  1566. + default: z = "UNION"; break;
  1567. + }
  1568. + return z;
  1569. +}
  1570. +
  1571. +#ifndef SQLITE_OMIT_EXPLAIN
  1572. +/*
  1573. +** Unless an "EXPLAIN QUERY PLAN" command is being processed, this function
  1574. +** is a no-op. Otherwise, it adds a single row of output to the EQP result,
  1575. +** where the caption is of the form:
  1576. +**
  1577. +** "USE TEMP B-TREE FOR xxx"
  1578. +**
  1579. +** where xxx is one of "DISTINCT", "ORDER BY" or "GROUP BY". Exactly which
  1580. +** is determined by the zUsage argument.
  1581. +*/
  1582. +static void explainTempTable(Parse *pParse, const char *zUsage){
  1583. + ExplainQueryPlan((pParse, 0, "USE TEMP B-TREE FOR %s", zUsage));
  1584. +}
  1585. +
  1586. +/*
  1587. +** Assign expression b to lvalue a. A second, no-op, version of this macro
  1588. +** is provided when SQLITE_OMIT_EXPLAIN is defined. This allows the code
  1589. +** in sqlite3Select() to assign values to structure member variables that
  1590. +** only exist if SQLITE_OMIT_EXPLAIN is not defined without polluting the
  1591. +** code with #ifndef directives.
  1592. +*/
  1593. +# define explainSetInteger(a, b) a = b
  1594. +
  1595. +#else
  1596. +/* No-op versions of the explainXXX() functions and macros. */
  1597. +# define explainTempTable(y,z)
  1598. +# define explainSetInteger(y,z)
  1599. +#endif
  1600. +
  1601. +
  1602. +/*
  1603. +** If the inner loop was generated using a non-null pOrderBy argument,
  1604. +** then the results were placed in a sorter. After the loop is terminated
  1605. +** we need to run the sorter and output the results. The following
  1606. +** routine generates the code needed to do that.
  1607. +*/
  1608. +static void generateSortTail(
  1609. + Parse *pParse, /* Parsing context */
  1610. + Select *p, /* The SELECT statement */
  1611. + SortCtx *pSort, /* Information on the ORDER BY clause */
  1612. + int nColumn, /* Number of columns of data */
  1613. + SelectDest *pDest /* Write the sorted results here */
  1614. +){
  1615. + Vdbe *v = pParse->pVdbe; /* The prepared statement */
  1616. + int addrBreak = pSort->labelDone; /* Jump here to exit loop */
  1617. + int addrContinue = sqlite3VdbeMakeLabel(pParse);/* Jump here for next cycle */
  1618. + int addr; /* Top of output loop. Jump for Next. */
  1619. + int addrOnce = 0;
  1620. + int iTab;
  1621. + ExprList *pOrderBy = pSort->pOrderBy;
  1622. + int eDest = pDest->eDest;
  1623. + int iParm = pDest->iSDParm;
  1624. + int regRow;
  1625. + int regRowid;
  1626. + int iCol;
  1627. + int nKey; /* Number of key columns in sorter record */
  1628. + int iSortTab; /* Sorter cursor to read from */
  1629. + int i;
  1630. + int bSeq; /* True if sorter record includes seq. no. */
  1631. + int nRefKey = 0;
  1632. + struct ExprList_item *aOutEx = p->pEList->a;
  1633. +
  1634. + assert( addrBreak<0 );
  1635. + if( pSort->labelBkOut ){
  1636. + sqlite3VdbeAddOp2(v, OP_Gosub, pSort->regReturn, pSort->labelBkOut);
  1637. + sqlite3VdbeGoto(v, addrBreak);
  1638. + sqlite3VdbeResolveLabel(v, pSort->labelBkOut);
  1639. + }
  1640. +
  1641. +#ifdef SQLITE_ENABLE_SORTER_REFERENCES
  1642. + /* Open any cursors needed for sorter-reference expressions */
  1643. + for(i=0; i<pSort->nDefer; i++){
  1644. + Table *pTab = pSort->aDefer[i].pTab;
  1645. + int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
  1646. + sqlite3OpenTable(pParse, pSort->aDefer[i].iCsr, iDb, pTab, OP_OpenRead);
  1647. + nRefKey = MAX(nRefKey, pSort->aDefer[i].nKey);
  1648. + }
  1649. +#endif
  1650. +
  1651. + iTab = pSort->iECursor;
  1652. + if( eDest==SRT_Output || eDest==SRT_Coroutine || eDest==SRT_Mem ){
  1653. + regRowid = 0;
  1654. + regRow = pDest->iSdst;
  1655. + }else{
  1656. + regRowid = sqlite3GetTempReg(pParse);
  1657. + if( eDest==SRT_EphemTab || eDest==SRT_Table ){
  1658. + regRow = sqlite3GetTempReg(pParse);
  1659. + nColumn = 0;
  1660. + }else{
  1661. + regRow = sqlite3GetTempRange(pParse, nColumn);
  1662. + }
  1663. + }
  1664. + nKey = pOrderBy->nExpr - pSort->nOBSat;
  1665. + if( pSort->sortFlags & SORTFLAG_UseSorter ){
  1666. + int regSortOut = ++pParse->nMem;
  1667. + iSortTab = pParse->nTab++;
  1668. + if( pSort->labelBkOut ){
  1669. + addrOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
  1670. + }
  1671. + sqlite3VdbeAddOp3(v, OP_OpenPseudo, iSortTab, regSortOut,
  1672. + nKey+1+nColumn+nRefKey);
  1673. + if( addrOnce ) sqlite3VdbeJumpHere(v, addrOnce);
  1674. + addr = 1 + sqlite3VdbeAddOp2(v, OP_SorterSort, iTab, addrBreak);
  1675. + VdbeCoverage(v);
  1676. + codeOffset(v, p->iOffset, addrContinue);
  1677. + sqlite3VdbeAddOp3(v, OP_SorterData, iTab, regSortOut, iSortTab);
  1678. + bSeq = 0;
  1679. + }else{
  1680. + addr = 1 + sqlite3VdbeAddOp2(v, OP_Sort, iTab, addrBreak); VdbeCoverage(v);
  1681. + codeOffset(v, p->iOffset, addrContinue);
  1682. + iSortTab = iTab;
  1683. + bSeq = 1;
  1684. + }
  1685. + for(i=0, iCol=nKey+bSeq-1; i<nColumn; i++){
  1686. +#ifdef SQLITE_ENABLE_SORTER_REFERENCES
  1687. + if( aOutEx[i].bSorterRef ) continue;
  1688. +#endif
  1689. + if( aOutEx[i].u.x.iOrderByCol==0 ) iCol++;
  1690. + }
  1691. +#ifdef SQLITE_ENABLE_SORTER_REFERENCES
  1692. + if( pSort->nDefer ){
  1693. + int iKey = iCol+1;
  1694. + int regKey = sqlite3GetTempRange(pParse, nRefKey);
  1695. +
  1696. + for(i=0; i<pSort->nDefer; i++){
  1697. + int iCsr = pSort->aDefer[i].iCsr;
  1698. + Table *pTab = pSort->aDefer[i].pTab;
  1699. + int nKey = pSort->aDefer[i].nKey;
  1700. +
  1701. + sqlite3VdbeAddOp1(v, OP_NullRow, iCsr);
  1702. + if( HasRowid(pTab) ){
  1703. + sqlite3VdbeAddOp3(v, OP_Column, iSortTab, iKey++, regKey);
  1704. + sqlite3VdbeAddOp3(v, OP_SeekRowid, iCsr,
  1705. + sqlite3VdbeCurrentAddr(v)+1, regKey);
  1706. + }else{
  1707. + int k;
  1708. + int iJmp;
  1709. + assert( sqlite3PrimaryKeyIndex(pTab)->nKeyCol==nKey );
  1710. + for(k=0; k<nKey; k++){
  1711. + sqlite3VdbeAddOp3(v, OP_Column, iSortTab, iKey++, regKey+k);
  1712. + }
  1713. + iJmp = sqlite3VdbeCurrentAddr(v);
  1714. + sqlite3VdbeAddOp4Int(v, OP_SeekGE, iCsr, iJmp+2, regKey, nKey);
  1715. + sqlite3VdbeAddOp4Int(v, OP_IdxLE, iCsr, iJmp+3, regKey, nKey);
  1716. + sqlite3VdbeAddOp1(v, OP_NullRow, iCsr);
  1717. + }
  1718. + }
  1719. + sqlite3ReleaseTempRange(pParse, regKey, nRefKey);
  1720. + }
  1721. +#endif
  1722. + for(i=nColumn-1; i>=0; i--){
  1723. +#ifdef SQLITE_ENABLE_SORTER_REFERENCES
  1724. + if( aOutEx[i].bSorterRef ){
  1725. + sqlite3ExprCode(pParse, aOutEx[i].pExpr, regRow+i);
  1726. + }else
  1727. +#endif
  1728. + {
  1729. + int iRead;
  1730. + if( aOutEx[i].u.x.iOrderByCol ){
  1731. + iRead = aOutEx[i].u.x.iOrderByCol-1;
  1732. + }else{
  1733. + iRead = iCol--;
  1734. + }
  1735. + sqlite3VdbeAddOp3(v, OP_Column, iSortTab, iRead, regRow+i);
  1736. + VdbeComment((v, "%s", aOutEx[i].zEName));
  1737. + }
  1738. + }
  1739. + switch( eDest ){
  1740. + case SRT_Table:
  1741. + case SRT_EphemTab: {
  1742. + sqlite3VdbeAddOp3(v, OP_Column, iSortTab, nKey+bSeq, regRow);
  1743. + sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, regRowid);
  1744. + sqlite3VdbeAddOp3(v, OP_Insert, iParm, regRow, regRowid);
  1745. + sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
  1746. + break;
  1747. + }
  1748. +#ifndef SQLITE_OMIT_SUBQUERY
  1749. + case SRT_Set: {
  1750. + assert( nColumn==sqlite3Strlen30(pDest->zAffSdst) );
  1751. + sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, nColumn, regRowid,
  1752. + pDest->zAffSdst, nColumn);
  1753. + sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm, regRowid, regRow, nColumn);
  1754. + break;
  1755. + }
  1756. + case SRT_Mem: {
  1757. + /* The LIMIT clause will terminate the loop for us */
  1758. + break;
  1759. + }
  1760. +#endif
  1761. + default: {
  1762. + assert( eDest==SRT_Output || eDest==SRT_Coroutine );
  1763. + testcase( eDest==SRT_Output );
  1764. + testcase( eDest==SRT_Coroutine );
  1765. + if( eDest==SRT_Output ){
  1766. + sqlite3VdbeAddOp2(v, OP_ResultRow, pDest->iSdst, nColumn);
  1767. + }else{
  1768. + sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
  1769. + }
  1770. + break;
  1771. + }
  1772. + }
  1773. + if( regRowid ){
  1774. + if( eDest==SRT_Set ){
  1775. + sqlite3ReleaseTempRange(pParse, regRow, nColumn);
  1776. + }else{
  1777. + sqlite3ReleaseTempReg(pParse, regRow);
  1778. + }
  1779. + sqlite3ReleaseTempReg(pParse, regRowid);
  1780. + }
  1781. + /* The bottom of the loop
  1782. + */
  1783. + sqlite3VdbeResolveLabel(v, addrContinue);
  1784. + if( pSort->sortFlags & SORTFLAG_UseSorter ){
  1785. + sqlite3VdbeAddOp2(v, OP_SorterNext, iTab, addr); VdbeCoverage(v);
  1786. + }else{
  1787. + sqlite3VdbeAddOp2(v, OP_Next, iTab, addr); VdbeCoverage(v);
  1788. + }
  1789. + if( pSort->regReturn ) sqlite3VdbeAddOp1(v, OP_Return, pSort->regReturn);
  1790. + sqlite3VdbeResolveLabel(v, addrBreak);
  1791. +}
  1792. +
  1793. +/*
  1794. +** Return a pointer to a string containing the 'declaration type' of the
  1795. +** expression pExpr. The string may be treated as static by the caller.
  1796. +**
  1797. +** Also try to estimate the size of the returned value and return that
  1798. +** result in *pEstWidth.
  1799. +**
  1800. +** The declaration type is the exact datatype definition extracted from the
  1801. +** original CREATE TABLE statement if the expression is a column. The
  1802. +** declaration type for a ROWID field is INTEGER. Exactly when an expression
  1803. +** is considered a column can be complex in the presence of subqueries. The
  1804. +** result-set expression in all of the following SELECT statements is
  1805. +** considered a column by this function.
  1806. +**
  1807. +** SELECT col FROM tbl;
  1808. +** SELECT (SELECT col FROM tbl;
  1809. +** SELECT (SELECT col FROM tbl);
  1810. +** SELECT abc FROM (SELECT col AS abc FROM tbl);
  1811. +**
  1812. +** The declaration type for any expression other than a column is NULL.
  1813. +**
  1814. +** This routine has either 3 or 6 parameters depending on whether or not
  1815. +** the SQLITE_ENABLE_COLUMN_METADATA compile-time option is used.
  1816. +*/
  1817. +#ifdef SQLITE_ENABLE_COLUMN_METADATA
  1818. +# define columnType(A,B,C,D,E) columnTypeImpl(A,B,C,D,E)
  1819. +#else /* if !defined(SQLITE_ENABLE_COLUMN_METADATA) */
  1820. +# define columnType(A,B,C,D,E) columnTypeImpl(A,B)
  1821. +#endif
  1822. +static const char *columnTypeImpl(
  1823. + NameContext *pNC,
  1824. +#ifndef SQLITE_ENABLE_COLUMN_METADATA
  1825. + Expr *pExpr
  1826. +#else
  1827. + Expr *pExpr,
  1828. + const char **pzOrigDb,
  1829. + const char **pzOrigTab,
  1830. + const char **pzOrigCol
  1831. +#endif
  1832. +){
  1833. + char const *zType = 0;
  1834. + int j;
  1835. +#ifdef SQLITE_ENABLE_COLUMN_METADATA
  1836. + char const *zOrigDb = 0;
  1837. + char const *zOrigTab = 0;
  1838. + char const *zOrigCol = 0;
  1839. +#endif
  1840. +
  1841. + assert( pExpr!=0 );
  1842. + assert( pNC->pSrcList!=0 );
  1843. + switch( pExpr->op ){
  1844. + case TK_COLUMN: {
  1845. + /* The expression is a column. Locate the table the column is being
  1846. + ** extracted from in NameContext.pSrcList. This table may be real
  1847. + ** database table or a subquery.
  1848. + */
  1849. + Table *pTab = 0; /* Table structure column is extracted from */
  1850. + Select *pS = 0; /* Select the column is extracted from */
  1851. + int iCol = pExpr->iColumn; /* Index of column in pTab */
  1852. + while( pNC && !pTab ){
  1853. + SrcList *pTabList = pNC->pSrcList;
  1854. + for(j=0;j<pTabList->nSrc && pTabList->a[j].iCursor!=pExpr->iTable;j++);
  1855. + if( j<pTabList->nSrc ){
  1856. + pTab = pTabList->a[j].pTab;
  1857. + pS = pTabList->a[j].pSelect;
  1858. + }else{
  1859. + pNC = pNC->pNext;
  1860. + }
  1861. + }
  1862. +
  1863. + if( pTab==0 ){
  1864. + /* At one time, code such as "SELECT new.x" within a trigger would
  1865. + ** cause this condition to run. Since then, we have restructured how
  1866. + ** trigger code is generated and so this condition is no longer
  1867. + ** possible. However, it can still be true for statements like
  1868. + ** the following:
  1869. + **
  1870. + ** CREATE TABLE t1(col INTEGER);
  1871. + ** SELECT (SELECT t1.col) FROM FROM t1;
  1872. + **
  1873. + ** when columnType() is called on the expression "t1.col" in the
  1874. + ** sub-select. In this case, set the column type to NULL, even
  1875. + ** though it should really be "INTEGER".
  1876. + **
  1877. + ** This is not a problem, as the column type of "t1.col" is never
  1878. + ** used. When columnType() is called on the expression
  1879. + ** "(SELECT t1.col)", the correct type is returned (see the TK_SELECT
  1880. + ** branch below. */
  1881. + break;
  1882. + }
  1883. +
  1884. + assert( pTab && pExpr->y.pTab==pTab );
  1885. + if( pS ){
  1886. + /* The "table" is actually a sub-select or a view in the FROM clause
  1887. + ** of the SELECT statement. Return the declaration type and origin
  1888. + ** data for the result-set column of the sub-select.
  1889. + */
  1890. + if( iCol>=0 && iCol<pS->pEList->nExpr ){
  1891. + /* If iCol is less than zero, then the expression requests the
  1892. + ** rowid of the sub-select or view. This expression is legal (see
  1893. + ** test case misc2.2.2) - it always evaluates to NULL.
  1894. + */
  1895. + NameContext sNC;
  1896. + Expr *p = pS->pEList->a[iCol].pExpr;
  1897. + sNC.pSrcList = pS->pSrc;
  1898. + sNC.pNext = pNC;
  1899. + sNC.pParse = pNC->pParse;
  1900. + zType = columnType(&sNC, p,&zOrigDb,&zOrigTab,&zOrigCol);
  1901. + }
  1902. + }else{
  1903. + /* A real table or a CTE table */
  1904. + assert( !pS );
  1905. +#ifdef SQLITE_ENABLE_COLUMN_METADATA
  1906. + if( iCol<0 ) iCol = pTab->iPKey;
  1907. + assert( iCol==XN_ROWID || (iCol>=0 && iCol<pTab->nCol) );
  1908. + if( iCol<0 ){
  1909. + zType = "INTEGER";
  1910. + zOrigCol = "rowid";
  1911. + }else{
  1912. + zOrigCol = pTab->aCol[iCol].zName;
  1913. + zType = sqlite3ColumnType(&pTab->aCol[iCol],0);
  1914. + }
  1915. + zOrigTab = pTab->zName;
  1916. + if( pNC->pParse && pTab->pSchema ){
  1917. + int iDb = sqlite3SchemaToIndex(pNC->pParse->db, pTab->pSchema);
  1918. + zOrigDb = pNC->pParse->db->aDb[iDb].zDbSName;
  1919. + }
  1920. +#else
  1921. + assert( iCol==XN_ROWID || (iCol>=0 && iCol<pTab->nCol) );
  1922. + if( iCol<0 ){
  1923. + zType = "INTEGER";
  1924. + }else{
  1925. + zType = sqlite3ColumnType(&pTab->aCol[iCol],0);
  1926. + }
  1927. +#endif
  1928. + }
  1929. + break;
  1930. + }
  1931. +#ifndef SQLITE_OMIT_SUBQUERY
  1932. + case TK_SELECT: {
  1933. + /* The expression is a sub-select. Return the declaration type and
  1934. + ** origin info for the single column in the result set of the SELECT
  1935. + ** statement.
  1936. + */
  1937. + NameContext sNC;
  1938. + Select *pS = pExpr->x.pSelect;
  1939. + Expr *p = pS->pEList->a[0].pExpr;
  1940. + assert( ExprHasProperty(pExpr, EP_xIsSelect) );
  1941. + sNC.pSrcList = pS->pSrc;
  1942. + sNC.pNext = pNC;
  1943. + sNC.pParse = pNC->pParse;
  1944. + zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol);
  1945. + break;
  1946. + }
  1947. +#endif
  1948. + }
  1949. +
  1950. +#ifdef SQLITE_ENABLE_COLUMN_METADATA
  1951. + if( pzOrigDb ){
  1952. + assert( pzOrigTab && pzOrigCol );
  1953. + *pzOrigDb = zOrigDb;
  1954. + *pzOrigTab = zOrigTab;
  1955. + *pzOrigCol = zOrigCol;
  1956. + }
  1957. +#endif
  1958. + return zType;
  1959. +}
  1960. +
  1961. +/*
  1962. +** Generate code that will tell the VDBE the declaration types of columns
  1963. +** in the result set.
  1964. +*/
  1965. +static void generateColumnTypes(
  1966. + Parse *pParse, /* Parser context */
  1967. + SrcList *pTabList, /* List of tables */
  1968. + ExprList *pEList /* Expressions defining the result set */
  1969. +){
  1970. +#ifndef SQLITE_OMIT_DECLTYPE
  1971. + Vdbe *v = pParse->pVdbe;
  1972. + int i;
  1973. + NameContext sNC;
  1974. + sNC.pSrcList = pTabList;
  1975. + sNC.pParse = pParse;
  1976. + sNC.pNext = 0;
  1977. + for(i=0; i<pEList->nExpr; i++){
  1978. + Expr *p = pEList->a[i].pExpr;
  1979. + const char *zType;
  1980. +#ifdef SQLITE_ENABLE_COLUMN_METADATA
  1981. + const char *zOrigDb = 0;
  1982. + const char *zOrigTab = 0;
  1983. + const char *zOrigCol = 0;
  1984. + zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol);
  1985. +
  1986. + /* The vdbe must make its own copy of the column-type and other
  1987. + ** column specific strings, in case the schema is reset before this
  1988. + ** virtual machine is deleted.
  1989. + */
  1990. + sqlite3VdbeSetColName(v, i, COLNAME_DATABASE, zOrigDb, SQLITE_TRANSIENT);
  1991. + sqlite3VdbeSetColName(v, i, COLNAME_TABLE, zOrigTab, SQLITE_TRANSIENT);
  1992. + sqlite3VdbeSetColName(v, i, COLNAME_COLUMN, zOrigCol, SQLITE_TRANSIENT);
  1993. +#else
  1994. + zType = columnType(&sNC, p, 0, 0, 0);
  1995. +#endif
  1996. + sqlite3VdbeSetColName(v, i, COLNAME_DECLTYPE, zType, SQLITE_TRANSIENT);
  1997. + }
  1998. +#endif /* !defined(SQLITE_OMIT_DECLTYPE) */
  1999. +}
  2000. +
  2001. +
  2002. +/*
  2003. +** Compute the column names for a SELECT statement.
  2004. +**
  2005. +** The only guarantee that SQLite makes about column names is that if the
  2006. +** column has an AS clause assigning it a name, that will be the name used.
  2007. +** That is the only documented guarantee. However, countless applications
  2008. +** developed over the years have made baseless assumptions about column names
  2009. +** and will break if those assumptions changes. Hence, use extreme caution
  2010. +** when modifying this routine to avoid breaking legacy.
  2011. +**
  2012. +** See Also: sqlite3ColumnsFromExprList()
  2013. +**
  2014. +** The PRAGMA short_column_names and PRAGMA full_column_names settings are
  2015. +** deprecated. The default setting is short=ON, full=OFF. 99.9% of all
  2016. +** applications should operate this way. Nevertheless, we need to support the
  2017. +** other modes for legacy:
  2018. +**
  2019. +** short=OFF, full=OFF: Column name is the text of the expression has it
  2020. +** originally appears in the SELECT statement. In
  2021. +** other words, the zSpan of the result expression.
  2022. +**
  2023. +** short=ON, full=OFF: (This is the default setting). If the result
  2024. +** refers directly to a table column, then the
  2025. +** result column name is just the table column
  2026. +** name: COLUMN. Otherwise use zSpan.
  2027. +**
  2028. +** full=ON, short=ANY: If the result refers directly to a table column,
  2029. +** then the result column name with the table name
  2030. +** prefix, ex: TABLE.COLUMN. Otherwise use zSpan.
  2031. +*/
  2032. +static void generateColumnNames(
  2033. + Parse *pParse, /* Parser context */
  2034. + Select *pSelect /* Generate column names for this SELECT statement */
  2035. +){
  2036. + Vdbe *v = pParse->pVdbe;
  2037. + int i;
  2038. + Table *pTab;
  2039. + SrcList *pTabList;
  2040. + ExprList *pEList;
  2041. + sqlite3 *db = pParse->db;
  2042. + int fullName; /* TABLE.COLUMN if no AS clause and is a direct table ref */
  2043. + int srcName; /* COLUMN or TABLE.COLUMN if no AS clause and is direct */
  2044. +
  2045. +#ifndef SQLITE_OMIT_EXPLAIN
  2046. + /* If this is an EXPLAIN, skip this step */
  2047. + if( pParse->explain ){
  2048. + return;
  2049. + }
  2050. +#endif
  2051. +
  2052. + if( pParse->colNamesSet ) return;
  2053. + /* Column names are determined by the left-most term of a compound select */
  2054. + while( pSelect->pPrior ) pSelect = pSelect->pPrior;
  2055. + SELECTTRACE(1,pParse,pSelect,("generating column names\n"));
  2056. + pTabList = pSelect->pSrc;
  2057. + pEList = pSelect->pEList;
  2058. + assert( v!=0 );
  2059. + assert( pTabList!=0 );
  2060. + pParse->colNamesSet = 1;
  2061. + fullName = (db->flags & SQLITE_FullColNames)!=0;
  2062. + srcName = (db->flags & SQLITE_ShortColNames)!=0 || fullName;
  2063. + sqlite3VdbeSetNumCols(v, pEList->nExpr);
  2064. + for(i=0; i<pEList->nExpr; i++){
  2065. + Expr *p = pEList->a[i].pExpr;
  2066. +
  2067. + assert( p!=0 );
  2068. + assert( p->op!=TK_AGG_COLUMN ); /* Agg processing has not run yet */
  2069. + assert( p->op!=TK_COLUMN || p->y.pTab!=0 ); /* Covering idx not yet coded */
  2070. + if( pEList->a[i].zEName && pEList->a[i].eEName==ENAME_NAME ){
  2071. + /* An AS clause always takes first priority */
  2072. + char *zName = pEList->a[i].zEName;
  2073. + sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, SQLITE_TRANSIENT);
  2074. + }else if( srcName && p->op==TK_COLUMN ){
  2075. + char *zCol;
  2076. + int iCol = p->iColumn;
  2077. + pTab = p->y.pTab;
  2078. + assert( pTab!=0 );
  2079. + if( iCol<0 ) iCol = pTab->iPKey;
  2080. + assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
  2081. + if( iCol<0 ){
  2082. + zCol = "rowid";
  2083. + }else{
  2084. + zCol = pTab->aCol[iCol].zName;
  2085. + }
  2086. + if( fullName ){
  2087. + char *zName = 0;
  2088. + zName = sqlite3MPrintf(db, "%s.%s", pTab->zName, zCol);
  2089. + sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, SQLITE_DYNAMIC);
  2090. + }else{
  2091. + sqlite3VdbeSetColName(v, i, COLNAME_NAME, zCol, SQLITE_TRANSIENT);
  2092. + }
  2093. + }else{
  2094. + const char *z = pEList->a[i].zEName;
  2095. + z = z==0 ? sqlite3MPrintf(db, "column%d", i+1) : sqlite3DbStrDup(db, z);
  2096. + sqlite3VdbeSetColName(v, i, COLNAME_NAME, z, SQLITE_DYNAMIC);
  2097. + }
  2098. + }
  2099. + generateColumnTypes(pParse, pTabList, pEList);
  2100. +}
  2101. +
  2102. +/*
  2103. +** Given an expression list (which is really the list of expressions
  2104. +** that form the result set of a SELECT statement) compute appropriate
  2105. +** column names for a table that would hold the expression list.
  2106. +**
  2107. +** All column names will be unique.
  2108. +**
  2109. +** Only the column names are computed. Column.zType, Column.zColl,
  2110. +** and other fields of Column are zeroed.
  2111. +**
  2112. +** Return SQLITE_OK on success. If a memory allocation error occurs,
  2113. +** store NULL in *paCol and 0 in *pnCol and return SQLITE_NOMEM.
  2114. +**
  2115. +** The only guarantee that SQLite makes about column names is that if the
  2116. +** column has an AS clause assigning it a name, that will be the name used.
  2117. +** That is the only documented guarantee. However, countless applications
  2118. +** developed over the years have made baseless assumptions about column names
  2119. +** and will break if those assumptions changes. Hence, use extreme caution
  2120. +** when modifying this routine to avoid breaking legacy.
  2121. +**
  2122. +** See Also: generateColumnNames()
  2123. +*/
  2124. +int sqlite3ColumnsFromExprList(
  2125. + Parse *pParse, /* Parsing context */
  2126. + ExprList *pEList, /* Expr list from which to derive column names */
  2127. + i16 *pnCol, /* Write the number of columns here */
  2128. + Column **paCol /* Write the new column list here */
  2129. +){
  2130. + sqlite3 *db = pParse->db; /* Database connection */
  2131. + int i, j; /* Loop counters */
  2132. + u32 cnt; /* Index added to make the name unique */
  2133. + Column *aCol, *pCol; /* For looping over result columns */
  2134. + int nCol; /* Number of columns in the result set */
  2135. + char *zName; /* Column name */
  2136. + int nName; /* Size of name in zName[] */
  2137. + Hash ht; /* Hash table of column names */
  2138. +
  2139. + sqlite3HashInit(&ht);
  2140. + if( pEList ){
  2141. + nCol = pEList->nExpr;
  2142. + aCol = sqlite3DbMallocZero(db, sizeof(aCol[0])*nCol);
  2143. + testcase( aCol==0 );
  2144. + if( nCol>32767 ) nCol = 32767;
  2145. + }else{
  2146. + nCol = 0;
  2147. + aCol = 0;
  2148. + }
  2149. + assert( nCol==(i16)nCol );
  2150. + *pnCol = nCol;
  2151. + *paCol = aCol;
  2152. +
  2153. + for(i=0, pCol=aCol; i<nCol && !db->mallocFailed; i++, pCol++){
  2154. + /* Get an appropriate name for the column
  2155. + */
  2156. + if( (zName = pEList->a[i].zEName)!=0 && pEList->a[i].eEName==ENAME_NAME ){
  2157. + /* If the column contains an "AS <name>" phrase, use <name> as the name */
  2158. + }else{
  2159. + Expr *pColExpr = sqlite3ExprSkipCollateAndLikely(pEList->a[i].pExpr);
  2160. + while( pColExpr->op==TK_DOT ){
  2161. + pColExpr = pColExpr->pRight;
  2162. + assert( pColExpr!=0 );
  2163. + }
  2164. + if( pColExpr->op==TK_COLUMN ){
  2165. + /* For columns use the column name name */
  2166. + int iCol = pColExpr->iColumn;
  2167. + Table *pTab = pColExpr->y.pTab;
  2168. + assert( pTab!=0 );
  2169. + if( iCol<0 ) iCol = pTab->iPKey;
  2170. + zName = iCol>=0 ? pTab->aCol[iCol].zName : "rowid";
  2171. + }else if( pColExpr->op==TK_ID ){
  2172. + assert( !ExprHasProperty(pColExpr, EP_IntValue) );
  2173. + zName = pColExpr->u.zToken;
  2174. + }else{
  2175. + /* Use the original text of the column expression as its name */
  2176. + zName = pEList->a[i].zEName;
  2177. + }
  2178. + }
  2179. + if( zName && !sqlite3IsTrueOrFalse(zName) ){
  2180. + zName = sqlite3DbStrDup(db, zName);
  2181. + }else{
  2182. + zName = sqlite3MPrintf(db,"column%d",i+1);
  2183. + }
  2184. +
  2185. + /* Make sure the column name is unique. If the name is not unique,
  2186. + ** append an integer to the name so that it becomes unique.
  2187. + */
  2188. + cnt = 0;
  2189. + while( zName && sqlite3HashFind(&ht, zName)!=0 ){
  2190. + nName = sqlite3Strlen30(zName);
  2191. + if( nName>0 ){
  2192. + for(j=nName-1; j>0 && sqlite3Isdigit(zName[j]); j--){}
  2193. + if( zName[j]==':' ) nName = j;
  2194. + }
  2195. + zName = sqlite3MPrintf(db, "%.*z:%u", nName, zName, ++cnt);
  2196. + if( cnt>3 ) sqlite3_randomness(sizeof(cnt), &cnt);
  2197. + }
  2198. + pCol->zName = zName;
  2199. + pCol->hName = sqlite3StrIHash(zName);
  2200. + sqlite3ColumnPropertiesFromName(0, pCol);
  2201. + if( zName && sqlite3HashInsert(&ht, zName, pCol)==pCol ){
  2202. + sqlite3OomFault(db);
  2203. + }
  2204. + }
  2205. + sqlite3HashClear(&ht);
  2206. + if( db->mallocFailed ){
  2207. + for(j=0; j<i; j++){
  2208. + sqlite3DbFree(db, aCol[j].zName);
  2209. + }
  2210. + sqlite3DbFree(db, aCol);
  2211. + *paCol = 0;
  2212. + *pnCol = 0;
  2213. + return SQLITE_NOMEM_BKPT;
  2214. + }
  2215. + return SQLITE_OK;
  2216. +}
  2217. +
  2218. +/*
  2219. +** Add type and collation information to a column list based on
  2220. +** a SELECT statement.
  2221. +**
  2222. +** The column list presumably came from selectColumnNamesFromExprList().
  2223. +** The column list has only names, not types or collations. This
  2224. +** routine goes through and adds the types and collations.
  2225. +**
  2226. +** This routine requires that all identifiers in the SELECT
  2227. +** statement be resolved.
  2228. +*/
  2229. +void sqlite3SelectAddColumnTypeAndCollation(
  2230. + Parse *pParse, /* Parsing contexts */
  2231. + Table *pTab, /* Add column type information to this table */
  2232. + Select *pSelect, /* SELECT used to determine types and collations */
  2233. + char aff /* Default affinity for columns */
  2234. +){
  2235. + sqlite3 *db = pParse->db;
  2236. + NameContext sNC;
  2237. + Column *pCol;
  2238. + CollSeq *pColl;
  2239. + int i;
  2240. + Expr *p;
  2241. + struct ExprList_item *a;
  2242. +
  2243. + assert( pSelect!=0 );
  2244. + assert( (pSelect->selFlags & SF_Resolved)!=0 );
  2245. + assert( pTab->nCol==pSelect->pEList->nExpr || db->mallocFailed );
  2246. + if( db->mallocFailed ) return;
  2247. + memset(&sNC, 0, sizeof(sNC));
  2248. + sNC.pSrcList = pSelect->pSrc;
  2249. + a = pSelect->pEList->a;
  2250. + for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
  2251. + const char *zType;
  2252. + int n, m;
  2253. + p = a[i].pExpr;
  2254. + zType = columnType(&sNC, p, 0, 0, 0);
  2255. + /* pCol->szEst = ... // Column size est for SELECT tables never used */
  2256. + pCol->affinity = sqlite3ExprAffinity(p);
  2257. + if( zType ){
  2258. + m = sqlite3Strlen30(zType);
  2259. + n = sqlite3Strlen30(pCol->zName);
  2260. + pCol->zName = sqlite3DbReallocOrFree(db, pCol->zName, n+m+2);
  2261. + if( pCol->zName ){
  2262. + memcpy(&pCol->zName[n+1], zType, m+1);
  2263. + pCol->colFlags |= COLFLAG_HASTYPE;
  2264. + }
  2265. + }
  2266. + if( pCol->affinity<=SQLITE_AFF_NONE ) pCol->affinity = aff;
  2267. + pColl = sqlite3ExprCollSeq(pParse, p);
  2268. + if( pColl && pCol->zColl==0 ){
  2269. + pCol->zColl = sqlite3DbStrDup(db, pColl->zName);
  2270. + }
  2271. + }
  2272. + pTab->szTabRow = 1; /* Any non-zero value works */
  2273. +}
  2274. +
  2275. +/*
  2276. +** Given a SELECT statement, generate a Table structure that describes
  2277. +** the result set of that SELECT.
  2278. +*/
  2279. +Table *sqlite3ResultSetOfSelect(Parse *pParse, Select *pSelect, char aff){
  2280. + Table *pTab;
  2281. + sqlite3 *db = pParse->db;
  2282. + u64 savedFlags;
  2283. +
  2284. + savedFlags = db->flags;
  2285. + db->flags &= ~(u64)SQLITE_FullColNames;
  2286. + db->flags |= SQLITE_ShortColNames;
  2287. + sqlite3SelectPrep(pParse, pSelect, 0);
  2288. + db->flags = savedFlags;
  2289. + if( pParse->nErr ) return 0;
  2290. + while( pSelect->pPrior ) pSelect = pSelect->pPrior;
  2291. + pTab = sqlite3DbMallocZero(db, sizeof(Table) );
  2292. + if( pTab==0 ){
  2293. + return 0;
  2294. + }
  2295. + pTab->nTabRef = 1;
  2296. + pTab->zName = 0;
  2297. + pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
  2298. + sqlite3ColumnsFromExprList(pParse, pSelect->pEList, &pTab->nCol, &pTab->aCol);
  2299. + sqlite3SelectAddColumnTypeAndCollation(pParse, pTab, pSelect, aff);
  2300. + pTab->iPKey = -1;
  2301. + if( db->mallocFailed ){
  2302. + sqlite3DeleteTable(db, pTab);
  2303. + return 0;
  2304. + }
  2305. + return pTab;
  2306. +}
  2307. +
  2308. +/*
  2309. +** Get a VDBE for the given parser context. Create a new one if necessary.
  2310. +** If an error occurs, return NULL and leave a message in pParse.
  2311. +*/
  2312. +Vdbe *sqlite3GetVdbe(Parse *pParse){
  2313. + if( pParse->pVdbe ){
  2314. + return pParse->pVdbe;
  2315. + }
  2316. + if( pParse->pToplevel==0
  2317. + && OptimizationEnabled(pParse->db,SQLITE_FactorOutConst)
  2318. + ){
  2319. + pParse->okConstFactor = 1;
  2320. + }
  2321. + return sqlite3VdbeCreate(pParse);
  2322. +}
  2323. +
  2324. +
  2325. +/*
  2326. +** Compute the iLimit and iOffset fields of the SELECT based on the
  2327. +** pLimit expressions. pLimit->pLeft and pLimit->pRight hold the expressions
  2328. +** that appear in the original SQL statement after the LIMIT and OFFSET
  2329. +** keywords. Or NULL if those keywords are omitted. iLimit and iOffset
  2330. +** are the integer memory register numbers for counters used to compute
  2331. +** the limit and offset. If there is no limit and/or offset, then
  2332. +** iLimit and iOffset are negative.
  2333. +**
  2334. +** This routine changes the values of iLimit and iOffset only if
  2335. +** a limit or offset is defined by pLimit->pLeft and pLimit->pRight. iLimit
  2336. +** and iOffset should have been preset to appropriate default values (zero)
  2337. +** prior to calling this routine.
  2338. +**
  2339. +** The iOffset register (if it exists) is initialized to the value
  2340. +** of the OFFSET. The iLimit register is initialized to LIMIT. Register
  2341. +** iOffset+1 is initialized to LIMIT+OFFSET.
  2342. +**
  2343. +** Only if pLimit->pLeft!=0 do the limit registers get
  2344. +** redefined. The UNION ALL operator uses this property to force
  2345. +** the reuse of the same limit and offset registers across multiple
  2346. +** SELECT statements.
  2347. +*/
  2348. +static void computeLimitRegisters(Parse *pParse, Select *p, int iBreak){
  2349. + Vdbe *v = 0;
  2350. + int iLimit = 0;
  2351. + int iOffset;
  2352. + int n;
  2353. + Expr *pLimit = p->pLimit;
  2354. +
  2355. + if( p->iLimit ) return;
  2356. +
  2357. + /*
  2358. + ** "LIMIT -1" always shows all rows. There is some
  2359. + ** controversy about what the correct behavior should be.
  2360. + ** The current implementation interprets "LIMIT 0" to mean
  2361. + ** no rows.
  2362. + */
  2363. + if( pLimit ){
  2364. + assert( pLimit->op==TK_LIMIT );
  2365. + assert( pLimit->pLeft!=0 );
  2366. + p->iLimit = iLimit = ++pParse->nMem;
  2367. + v = sqlite3GetVdbe(pParse);
  2368. + assert( v!=0 );
  2369. + if( sqlite3ExprIsInteger(pLimit->pLeft, &n) ){
  2370. + sqlite3VdbeAddOp2(v, OP_Integer, n, iLimit);
  2371. + VdbeComment((v, "LIMIT counter"));
  2372. + if( n==0 ){
  2373. + sqlite3VdbeGoto(v, iBreak);
  2374. + }else if( n>=0 && p->nSelectRow>sqlite3LogEst((u64)n) ){
  2375. + p->nSelectRow = sqlite3LogEst((u64)n);
  2376. + p->selFlags |= SF_FixedLimit;
  2377. + }
  2378. + }else{
  2379. + sqlite3ExprCode(pParse, pLimit->pLeft, iLimit);
  2380. + sqlite3VdbeAddOp1(v, OP_MustBeInt, iLimit); VdbeCoverage(v);
  2381. + VdbeComment((v, "LIMIT counter"));
  2382. + sqlite3VdbeAddOp2(v, OP_IfNot, iLimit, iBreak); VdbeCoverage(v);
  2383. + }
  2384. + if( pLimit->pRight ){
  2385. + p->iOffset = iOffset = ++pParse->nMem;
  2386. + pParse->nMem++; /* Allocate an extra register for limit+offset */
  2387. + sqlite3ExprCode(pParse, pLimit->pRight, iOffset);
  2388. + sqlite3VdbeAddOp1(v, OP_MustBeInt, iOffset); VdbeCoverage(v);
  2389. + VdbeComment((v, "OFFSET counter"));
  2390. + sqlite3VdbeAddOp3(v, OP_OffsetLimit, iLimit, iOffset+1, iOffset);
  2391. + VdbeComment((v, "LIMIT+OFFSET"));
  2392. + }
  2393. + }
  2394. +}
  2395. +
  2396. +#ifndef SQLITE_OMIT_COMPOUND_SELECT
  2397. +/*
  2398. +** Return the appropriate collating sequence for the iCol-th column of
  2399. +** the result set for the compound-select statement "p". Return NULL if
  2400. +** the column has no default collating sequence.
  2401. +**
  2402. +** The collating sequence for the compound select is taken from the
  2403. +** left-most term of the select that has a collating sequence.
  2404. +*/
  2405. +static CollSeq *multiSelectCollSeq(Parse *pParse, Select *p, int iCol){
  2406. + CollSeq *pRet;
  2407. + if( p->pPrior ){
  2408. + pRet = multiSelectCollSeq(pParse, p->pPrior, iCol);
  2409. + }else{
  2410. + pRet = 0;
  2411. + }
  2412. + assert( iCol>=0 );
  2413. + /* iCol must be less than p->pEList->nExpr. Otherwise an error would
  2414. + ** have been thrown during name resolution and we would not have gotten
  2415. + ** this far */
  2416. + if( pRet==0 && ALWAYS(iCol<p->pEList->nExpr) ){
  2417. + pRet = sqlite3ExprCollSeq(pParse, p->pEList->a[iCol].pExpr);
  2418. + }
  2419. + return pRet;
  2420. +}
  2421. +
  2422. +/*
  2423. +** The select statement passed as the second parameter is a compound SELECT
  2424. +** with an ORDER BY clause. This function allocates and returns a KeyInfo
  2425. +** structure suitable for implementing the ORDER BY.
  2426. +**
  2427. +** Space to hold the KeyInfo structure is obtained from malloc. The calling
  2428. +** function is responsible for ensuring that this structure is eventually
  2429. +** freed.
  2430. +*/
  2431. +static KeyInfo *multiSelectOrderByKeyInfo(Parse *pParse, Select *p, int nExtra){
  2432. + ExprList *pOrderBy = p->pOrderBy;
  2433. + int nOrderBy = p->pOrderBy->nExpr;
  2434. + sqlite3 *db = pParse->db;
  2435. + KeyInfo *pRet = sqlite3KeyInfoAlloc(db, nOrderBy+nExtra, 1);
  2436. + if( pRet ){
  2437. + int i;
  2438. + for(i=0; i<nOrderBy; i++){
  2439. + struct ExprList_item *pItem = &pOrderBy->a[i];
  2440. + Expr *pTerm = pItem->pExpr;
  2441. + CollSeq *pColl;
  2442. +
  2443. + if( pTerm->flags & EP_Collate ){
  2444. + pColl = sqlite3ExprCollSeq(pParse, pTerm);
  2445. + }else{
  2446. + pColl = multiSelectCollSeq(pParse, p, pItem->u.x.iOrderByCol-1);
  2447. + if( pColl==0 ) pColl = db->pDfltColl;
  2448. + pOrderBy->a[i].pExpr =
  2449. + sqlite3ExprAddCollateString(pParse, pTerm, pColl->zName);
  2450. + }
  2451. + assert( sqlite3KeyInfoIsWriteable(pRet) );
  2452. + pRet->aColl[i] = pColl;
  2453. + pRet->aSortFlags[i] = pOrderBy->a[i].sortFlags;
  2454. + }
  2455. + }
  2456. +
  2457. + return pRet;
  2458. +}
  2459. +
  2460. +#ifndef SQLITE_OMIT_CTE
  2461. +/*
  2462. +** This routine generates VDBE code to compute the content of a WITH RECURSIVE
  2463. +** query of the form:
  2464. +**
  2465. +** <recursive-table> AS (<setup-query> UNION [ALL] <recursive-query>)
  2466. +** \___________/ \_______________/
  2467. +** p->pPrior p
  2468. +**
  2469. +**
  2470. +** There is exactly one reference to the recursive-table in the FROM clause
  2471. +** of recursive-query, marked with the SrcList->a[].fg.isRecursive flag.
  2472. +**
  2473. +** The setup-query runs once to generate an initial set of rows that go
  2474. +** into a Queue table. Rows are extracted from the Queue table one by
  2475. +** one. Each row extracted from Queue is output to pDest. Then the single
  2476. +** extracted row (now in the iCurrent table) becomes the content of the
  2477. +** recursive-table for a recursive-query run. The output of the recursive-query
  2478. +** is added back into the Queue table. Then another row is extracted from Queue
  2479. +** and the iteration continues until the Queue table is empty.
  2480. +**
  2481. +** If the compound query operator is UNION then no duplicate rows are ever
  2482. +** inserted into the Queue table. The iDistinct table keeps a copy of all rows
  2483. +** that have ever been inserted into Queue and causes duplicates to be
  2484. +** discarded. If the operator is UNION ALL, then duplicates are allowed.
  2485. +**
  2486. +** If the query has an ORDER BY, then entries in the Queue table are kept in
  2487. +** ORDER BY order and the first entry is extracted for each cycle. Without
  2488. +** an ORDER BY, the Queue table is just a FIFO.
  2489. +**
  2490. +** If a LIMIT clause is provided, then the iteration stops after LIMIT rows
  2491. +** have been output to pDest. A LIMIT of zero means to output no rows and a
  2492. +** negative LIMIT means to output all rows. If there is also an OFFSET clause
  2493. +** with a positive value, then the first OFFSET outputs are discarded rather
  2494. +** than being sent to pDest. The LIMIT count does not begin until after OFFSET
  2495. +** rows have been skipped.
  2496. +*/
  2497. +static void generateWithRecursiveQuery(
  2498. + Parse *pParse, /* Parsing context */
  2499. + Select *p, /* The recursive SELECT to be coded */
  2500. + SelectDest *pDest /* What to do with query results */
  2501. +){
  2502. + SrcList *pSrc = p->pSrc; /* The FROM clause of the recursive query */
  2503. + int nCol = p->pEList->nExpr; /* Number of columns in the recursive table */
  2504. + Vdbe *v = pParse->pVdbe; /* The prepared statement under construction */
  2505. + Select *pSetup = p->pPrior; /* The setup query */
  2506. + int addrTop; /* Top of the loop */
  2507. + int addrCont, addrBreak; /* CONTINUE and BREAK addresses */
  2508. + int iCurrent = 0; /* The Current table */
  2509. + int regCurrent; /* Register holding Current table */
  2510. + int iQueue; /* The Queue table */
  2511. + int iDistinct = 0; /* To ensure unique results if UNION */
  2512. + int eDest = SRT_Fifo; /* How to write to Queue */
  2513. + SelectDest destQueue; /* SelectDest targetting the Queue table */
  2514. + int i; /* Loop counter */
  2515. + int rc; /* Result code */
  2516. + ExprList *pOrderBy; /* The ORDER BY clause */
  2517. + Expr *pLimit; /* Saved LIMIT and OFFSET */
  2518. + int regLimit, regOffset; /* Registers used by LIMIT and OFFSET */
  2519. +
  2520. +#ifndef SQLITE_OMIT_WINDOWFUNC
  2521. + if( p->pWin ){
  2522. + sqlite3ErrorMsg(pParse, "cannot use window functions in recursive queries");
  2523. + return;
  2524. + }
  2525. +#endif
  2526. +
  2527. + /* Obtain authorization to do a recursive query */
  2528. + if( sqlite3AuthCheck(pParse, SQLITE_RECURSIVE, 0, 0, 0) ) return;
  2529. +
  2530. + /* Process the LIMIT and OFFSET clauses, if they exist */
  2531. + addrBreak = sqlite3VdbeMakeLabel(pParse);
  2532. + p->nSelectRow = 320; /* 4 billion rows */
  2533. + computeLimitRegisters(pParse, p, addrBreak);
  2534. + pLimit = p->pLimit;
  2535. + regLimit = p->iLimit;
  2536. + regOffset = p->iOffset;
  2537. + p->pLimit = 0;
  2538. + p->iLimit = p->iOffset = 0;
  2539. + pOrderBy = p->pOrderBy;
  2540. +
  2541. + /* Locate the cursor number of the Current table */
  2542. + for(i=0; ALWAYS(i<pSrc->nSrc); i++){
  2543. + if( pSrc->a[i].fg.isRecursive ){
  2544. + iCurrent = pSrc->a[i].iCursor;
  2545. + break;
  2546. + }
  2547. + }
  2548. +
  2549. + /* Allocate cursors numbers for Queue and Distinct. The cursor number for
  2550. + ** the Distinct table must be exactly one greater than Queue in order
  2551. + ** for the SRT_DistFifo and SRT_DistQueue destinations to work. */
  2552. + iQueue = pParse->nTab++;
  2553. + if( p->op==TK_UNION ){
  2554. + eDest = pOrderBy ? SRT_DistQueue : SRT_DistFifo;
  2555. + iDistinct = pParse->nTab++;
  2556. + }else{
  2557. + eDest = pOrderBy ? SRT_Queue : SRT_Fifo;
  2558. + }
  2559. + sqlite3SelectDestInit(&destQueue, eDest, iQueue);
  2560. +
  2561. + /* Allocate cursors for Current, Queue, and Distinct. */
  2562. + regCurrent = ++pParse->nMem;
  2563. + sqlite3VdbeAddOp3(v, OP_OpenPseudo, iCurrent, regCurrent, nCol);
  2564. + if( pOrderBy ){
  2565. + KeyInfo *pKeyInfo = multiSelectOrderByKeyInfo(pParse, p, 1);
  2566. + sqlite3VdbeAddOp4(v, OP_OpenEphemeral, iQueue, pOrderBy->nExpr+2, 0,
  2567. + (char*)pKeyInfo, P4_KEYINFO);
  2568. + destQueue.pOrderBy = pOrderBy;
  2569. + }else{
  2570. + sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iQueue, nCol);
  2571. + }
  2572. + VdbeComment((v, "Queue table"));
  2573. + if( iDistinct ){
  2574. + p->addrOpenEphm[0] = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iDistinct, 0);
  2575. + p->selFlags |= SF_UsesEphemeral;
  2576. + }
  2577. +
  2578. + /* Detach the ORDER BY clause from the compound SELECT */
  2579. + p->pOrderBy = 0;
  2580. +
  2581. + /* Store the results of the setup-query in Queue. */
  2582. + pSetup->pNext = 0;
  2583. + ExplainQueryPlan((pParse, 1, "SETUP"));
  2584. + rc = sqlite3Select(pParse, pSetup, &destQueue);
  2585. + pSetup->pNext = p;
  2586. + if( rc ) goto end_of_recursive_query;
  2587. +
  2588. + /* Find the next row in the Queue and output that row */
  2589. + addrTop = sqlite3VdbeAddOp2(v, OP_Rewind, iQueue, addrBreak); VdbeCoverage(v);
  2590. +
  2591. + /* Transfer the next row in Queue over to Current */
  2592. + sqlite3VdbeAddOp1(v, OP_NullRow, iCurrent); /* To reset column cache */
  2593. + if( pOrderBy ){
  2594. + sqlite3VdbeAddOp3(v, OP_Column, iQueue, pOrderBy->nExpr+1, regCurrent);
  2595. + }else{
  2596. + sqlite3VdbeAddOp2(v, OP_RowData, iQueue, regCurrent);
  2597. + }
  2598. + sqlite3VdbeAddOp1(v, OP_Delete, iQueue);
  2599. +
  2600. + /* Output the single row in Current */
  2601. + addrCont = sqlite3VdbeMakeLabel(pParse);
  2602. + codeOffset(v, regOffset, addrCont);
  2603. + selectInnerLoop(pParse, p, iCurrent,
  2604. + 0, 0, pDest, addrCont, addrBreak);
  2605. + if( regLimit ){
  2606. + sqlite3VdbeAddOp2(v, OP_DecrJumpZero, regLimit, addrBreak);
  2607. + VdbeCoverage(v);
  2608. + }
  2609. + sqlite3VdbeResolveLabel(v, addrCont);
  2610. +
  2611. + /* Execute the recursive SELECT taking the single row in Current as
  2612. + ** the value for the recursive-table. Store the results in the Queue.
  2613. + */
  2614. + if( p->selFlags & SF_Aggregate ){
  2615. + sqlite3ErrorMsg(pParse, "recursive aggregate queries not supported");
  2616. + }else{
  2617. + p->pPrior = 0;
  2618. + ExplainQueryPlan((pParse, 1, "RECURSIVE STEP"));
  2619. + sqlite3Select(pParse, p, &destQueue);
  2620. + assert( p->pPrior==0 );
  2621. + p->pPrior = pSetup;
  2622. + }
  2623. +
  2624. + /* Keep running the loop until the Queue is empty */
  2625. + sqlite3VdbeGoto(v, addrTop);
  2626. + sqlite3VdbeResolveLabel(v, addrBreak);
  2627. +
  2628. +end_of_recursive_query:
  2629. + sqlite3ExprListDelete(pParse->db, p->pOrderBy);
  2630. + p->pOrderBy = pOrderBy;
  2631. + p->pLimit = pLimit;
  2632. + return;
  2633. +}
  2634. +#endif /* SQLITE_OMIT_CTE */
  2635. +
  2636. +/* Forward references */
  2637. +static int multiSelectOrderBy(
  2638. + Parse *pParse, /* Parsing context */
  2639. + Select *p, /* The right-most of SELECTs to be coded */
  2640. + SelectDest *pDest /* What to do with query results */
  2641. +);
  2642. +
  2643. +/*
  2644. +** Handle the special case of a compound-select that originates from a
  2645. +** VALUES clause. By handling this as a special case, we avoid deep
  2646. +** recursion, and thus do not need to enforce the SQLITE_LIMIT_COMPOUND_SELECT
  2647. +** on a VALUES clause.
  2648. +**
  2649. +** Because the Select object originates from a VALUES clause:
  2650. +** (1) There is no LIMIT or OFFSET or else there is a LIMIT of exactly 1
  2651. +** (2) All terms are UNION ALL
  2652. +** (3) There is no ORDER BY clause
  2653. +**
  2654. +** The "LIMIT of exactly 1" case of condition (1) comes about when a VALUES
  2655. +** clause occurs within scalar expression (ex: "SELECT (VALUES(1),(2),(3))").
  2656. +** The sqlite3CodeSubselect will have added the LIMIT 1 clause in tht case.
  2657. +** Since the limit is exactly 1, we only need to evalutes the left-most VALUES.
  2658. +*/
  2659. +static int multiSelectValues(
  2660. + Parse *pParse, /* Parsing context */
  2661. + Select *p, /* The right-most of SELECTs to be coded */
  2662. + SelectDest *pDest /* What to do with query results */
  2663. +){
  2664. + int nRow = 1;
  2665. + int rc = 0;
  2666. + int bShowAll = p->pLimit==0;
  2667. + assert( p->selFlags & SF_MultiValue );
  2668. + do{
  2669. + assert( p->selFlags & SF_Values );
  2670. + assert( p->op==TK_ALL || (p->op==TK_SELECT && p->pPrior==0) );
  2671. + assert( p->pNext==0 || p->pEList->nExpr==p->pNext->pEList->nExpr );
  2672. +#ifndef SQLITE_OMIT_WINDOWFUNC
  2673. + if( p->pWin ) return -1;
  2674. +#endif
  2675. + if( p->pPrior==0 ) break;
  2676. + assert( p->pPrior->pNext==p );
  2677. + p = p->pPrior;
  2678. + nRow += bShowAll;
  2679. + }while(1);
  2680. + ExplainQueryPlan((pParse, 0, "SCAN %d CONSTANT ROW%s", nRow,
  2681. + nRow==1 ? "" : "S"));
  2682. + while( p ){
  2683. + selectInnerLoop(pParse, p, -1, 0, 0, pDest, 1, 1);
  2684. + if( !bShowAll ) break;
  2685. + p->nSelectRow = nRow;
  2686. + p = p->pNext;
  2687. + }
  2688. + return rc;
  2689. +}
  2690. +
  2691. +/*
  2692. +** This routine is called to process a compound query form from
  2693. +** two or more separate queries using UNION, UNION ALL, EXCEPT, or
  2694. +** INTERSECT
  2695. +**
  2696. +** "p" points to the right-most of the two queries. the query on the
  2697. +** left is p->pPrior. The left query could also be a compound query
  2698. +** in which case this routine will be called recursively.
  2699. +**
  2700. +** The results of the total query are to be written into a destination
  2701. +** of type eDest with parameter iParm.
  2702. +**
  2703. +** Example 1: Consider a three-way compound SQL statement.
  2704. +**
  2705. +** SELECT a FROM t1 UNION SELECT b FROM t2 UNION SELECT c FROM t3
  2706. +**
  2707. +** This statement is parsed up as follows:
  2708. +**
  2709. +** SELECT c FROM t3
  2710. +** |
  2711. +** `-----> SELECT b FROM t2
  2712. +** |
  2713. +** `------> SELECT a FROM t1
  2714. +**
  2715. +** The arrows in the diagram above represent the Select.pPrior pointer.
  2716. +** So if this routine is called with p equal to the t3 query, then
  2717. +** pPrior will be the t2 query. p->op will be TK_UNION in this case.
  2718. +**
  2719. +** Notice that because of the way SQLite parses compound SELECTs, the
  2720. +** individual selects always group from left to right.
  2721. +*/
  2722. +static int multiSelect(
  2723. + Parse *pParse, /* Parsing context */
  2724. + Select *p, /* The right-most of SELECTs to be coded */
  2725. + SelectDest *pDest /* What to do with query results */
  2726. +){
  2727. + int rc = SQLITE_OK; /* Success code from a subroutine */
  2728. + Select *pPrior; /* Another SELECT immediately to our left */
  2729. + Vdbe *v; /* Generate code to this VDBE */
  2730. + SelectDest dest; /* Alternative data destination */
  2731. + Select *pDelete = 0; /* Chain of simple selects to delete */
  2732. + sqlite3 *db; /* Database connection */
  2733. +
  2734. + /* Make sure there is no ORDER BY or LIMIT clause on prior SELECTs. Only
  2735. + ** the last (right-most) SELECT in the series may have an ORDER BY or LIMIT.
  2736. + */
  2737. + assert( p && p->pPrior ); /* Calling function guarantees this much */
  2738. + assert( (p->selFlags & SF_Recursive)==0 || p->op==TK_ALL || p->op==TK_UNION );
  2739. + assert( p->selFlags & SF_Compound );
  2740. + db = pParse->db;
  2741. + pPrior = p->pPrior;
  2742. + dest = *pDest;
  2743. + if( pPrior->pOrderBy || pPrior->pLimit ){
  2744. + sqlite3ErrorMsg(pParse,"%s clause should come after %s not before",
  2745. + pPrior->pOrderBy!=0 ? "ORDER BY" : "LIMIT", selectOpName(p->op));
  2746. + rc = 1;
  2747. + goto multi_select_end;
  2748. + }
  2749. +
  2750. + v = sqlite3GetVdbe(pParse);
  2751. + assert( v!=0 ); /* The VDBE already created by calling function */
  2752. +
  2753. + /* Create the destination temporary table if necessary
  2754. + */
  2755. + if( dest.eDest==SRT_EphemTab ){
  2756. + assert( p->pEList );
  2757. + sqlite3VdbeAddOp2(v, OP_OpenEphemeral, dest.iSDParm, p->pEList->nExpr);
  2758. + dest.eDest = SRT_Table;
  2759. + }
  2760. +
  2761. + /* Special handling for a compound-select that originates as a VALUES clause.
  2762. + */
  2763. + if( p->selFlags & SF_MultiValue ){
  2764. + rc = multiSelectValues(pParse, p, &dest);
  2765. + if( rc>=0 ) goto multi_select_end;
  2766. + rc = SQLITE_OK;
  2767. + }
  2768. +
  2769. + /* Make sure all SELECTs in the statement have the same number of elements
  2770. + ** in their result sets.
  2771. + */
  2772. + assert( p->pEList && pPrior->pEList );
  2773. + assert( p->pEList->nExpr==pPrior->pEList->nExpr );
  2774. +
  2775. +#ifndef SQLITE_OMIT_CTE
  2776. + if( p->selFlags & SF_Recursive ){
  2777. + generateWithRecursiveQuery(pParse, p, &dest);
  2778. + }else
  2779. +#endif
  2780. +
  2781. + /* Compound SELECTs that have an ORDER BY clause are handled separately.
  2782. + */
  2783. + if( p->pOrderBy ){
  2784. + return multiSelectOrderBy(pParse, p, pDest);
  2785. + }else{
  2786. +
  2787. +#ifndef SQLITE_OMIT_EXPLAIN
  2788. + if( pPrior->pPrior==0 ){
  2789. + ExplainQueryPlan((pParse, 1, "COMPOUND QUERY"));
  2790. + ExplainQueryPlan((pParse, 1, "LEFT-MOST SUBQUERY"));
  2791. + }
  2792. +#endif
  2793. +
  2794. + /* Generate code for the left and right SELECT statements.
  2795. + */
  2796. + switch( p->op ){
  2797. + case TK_ALL: {
  2798. + int addr = 0;
  2799. + int nLimit;
  2800. + assert( !pPrior->pLimit );
  2801. + pPrior->iLimit = p->iLimit;
  2802. + pPrior->iOffset = p->iOffset;
  2803. + pPrior->pLimit = p->pLimit;
  2804. + rc = sqlite3Select(pParse, pPrior, &dest);
  2805. + p->pLimit = 0;
  2806. + if( rc ){
  2807. + goto multi_select_end;
  2808. + }
  2809. + p->pPrior = 0;
  2810. + p->iLimit = pPrior->iLimit;
  2811. + p->iOffset = pPrior->iOffset;
  2812. + if( p->iLimit ){
  2813. + addr = sqlite3VdbeAddOp1(v, OP_IfNot, p->iLimit); VdbeCoverage(v);
  2814. + VdbeComment((v, "Jump ahead if LIMIT reached"));
  2815. + if( p->iOffset ){
  2816. + sqlite3VdbeAddOp3(v, OP_OffsetLimit,
  2817. + p->iLimit, p->iOffset+1, p->iOffset);
  2818. + }
  2819. + }
  2820. + ExplainQueryPlan((pParse, 1, "UNION ALL"));
  2821. + rc = sqlite3Select(pParse, p, &dest);
  2822. + testcase( rc!=SQLITE_OK );
  2823. + pDelete = p->pPrior;
  2824. + p->pPrior = pPrior;
  2825. + p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
  2826. + if( pPrior->pLimit
  2827. + && sqlite3ExprIsInteger(pPrior->pLimit->pLeft, &nLimit)
  2828. + && nLimit>0 && p->nSelectRow > sqlite3LogEst((u64)nLimit)
  2829. + ){
  2830. + p->nSelectRow = sqlite3LogEst((u64)nLimit);
  2831. + }
  2832. + if( addr ){
  2833. + sqlite3VdbeJumpHere(v, addr);
  2834. + }
  2835. + break;
  2836. + }
  2837. + case TK_EXCEPT:
  2838. + case TK_UNION: {
  2839. + int unionTab; /* Cursor number of the temp table holding result */
  2840. + u8 op = 0; /* One of the SRT_ operations to apply to self */
  2841. + int priorOp; /* The SRT_ operation to apply to prior selects */
  2842. + Expr *pLimit; /* Saved values of p->nLimit */
  2843. + int addr;
  2844. + SelectDest uniondest;
  2845. +
  2846. + testcase( p->op==TK_EXCEPT );
  2847. + testcase( p->op==TK_UNION );
  2848. + priorOp = SRT_Union;
  2849. + if( dest.eDest==priorOp ){
  2850. + /* We can reuse a temporary table generated by a SELECT to our
  2851. + ** right.
  2852. + */
  2853. + assert( p->pLimit==0 ); /* Not allowed on leftward elements */
  2854. + unionTab = dest.iSDParm;
  2855. + }else{
  2856. + /* We will need to create our own temporary table to hold the
  2857. + ** intermediate results.
  2858. + */
  2859. + unionTab = pParse->nTab++;
  2860. + assert( p->pOrderBy==0 );
  2861. + addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, unionTab, 0);
  2862. + assert( p->addrOpenEphm[0] == -1 );
  2863. + p->addrOpenEphm[0] = addr;
  2864. + findRightmost(p)->selFlags |= SF_UsesEphemeral;
  2865. + assert( p->pEList );
  2866. + }
  2867. +
  2868. + /* Code the SELECT statements to our left
  2869. + */
  2870. + assert( !pPrior->pOrderBy );
  2871. + sqlite3SelectDestInit(&uniondest, priorOp, unionTab);
  2872. + rc = sqlite3Select(pParse, pPrior, &uniondest);
  2873. + if( rc ){
  2874. + goto multi_select_end;
  2875. + }
  2876. +
  2877. + /* Code the current SELECT statement
  2878. + */
  2879. + if( p->op==TK_EXCEPT ){
  2880. + op = SRT_Except;
  2881. + }else{
  2882. + assert( p->op==TK_UNION );
  2883. + op = SRT_Union;
  2884. + }
  2885. + p->pPrior = 0;
  2886. + pLimit = p->pLimit;
  2887. + p->pLimit = 0;
  2888. + uniondest.eDest = op;
  2889. + ExplainQueryPlan((pParse, 1, "%s USING TEMP B-TREE",
  2890. + selectOpName(p->op)));
  2891. + rc = sqlite3Select(pParse, p, &uniondest);
  2892. + testcase( rc!=SQLITE_OK );
  2893. + /* Query flattening in sqlite3Select() might refill p->pOrderBy.
  2894. + ** Be sure to delete p->pOrderBy, therefore, to avoid a memory leak. */
  2895. + sqlite3ExprListDelete(db, p->pOrderBy);
  2896. + pDelete = p->pPrior;
  2897. + p->pPrior = pPrior;
  2898. + p->pOrderBy = 0;
  2899. + if( p->op==TK_UNION ){
  2900. + p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
  2901. + }
  2902. + sqlite3ExprDelete(db, p->pLimit);
  2903. + p->pLimit = pLimit;
  2904. + p->iLimit = 0;
  2905. + p->iOffset = 0;
  2906. +
  2907. + /* Convert the data in the temporary table into whatever form
  2908. + ** it is that we currently need.
  2909. + */
  2910. + assert( unionTab==dest.iSDParm || dest.eDest!=priorOp );
  2911. + assert( p->pEList || db->mallocFailed );
  2912. + if( dest.eDest!=priorOp && db->mallocFailed==0 ){
  2913. + int iCont, iBreak, iStart;
  2914. + iBreak = sqlite3VdbeMakeLabel(pParse);
  2915. + iCont = sqlite3VdbeMakeLabel(pParse);
  2916. + computeLimitRegisters(pParse, p, iBreak);
  2917. + sqlite3VdbeAddOp2(v, OP_Rewind, unionTab, iBreak); VdbeCoverage(v);
  2918. + iStart = sqlite3VdbeCurrentAddr(v);
  2919. + selectInnerLoop(pParse, p, unionTab,
  2920. + 0, 0, &dest, iCont, iBreak);
  2921. + sqlite3VdbeResolveLabel(v, iCont);
  2922. + sqlite3VdbeAddOp2(v, OP_Next, unionTab, iStart); VdbeCoverage(v);
  2923. + sqlite3VdbeResolveLabel(v, iBreak);
  2924. + sqlite3VdbeAddOp2(v, OP_Close, unionTab, 0);
  2925. + }
  2926. + break;
  2927. + }
  2928. + default: assert( p->op==TK_INTERSECT ); {
  2929. + int tab1, tab2;
  2930. + int iCont, iBreak, iStart;
  2931. + Expr *pLimit;
  2932. + int addr;
  2933. + SelectDest intersectdest;
  2934. + int r1;
  2935. +
  2936. + /* INTERSECT is different from the others since it requires
  2937. + ** two temporary tables. Hence it has its own case. Begin
  2938. + ** by allocating the tables we will need.
  2939. + */
  2940. + tab1 = pParse->nTab++;
  2941. + tab2 = pParse->nTab++;
  2942. + assert( p->pOrderBy==0 );
  2943. +
  2944. + addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab1, 0);
  2945. + assert( p->addrOpenEphm[0] == -1 );
  2946. + p->addrOpenEphm[0] = addr;
  2947. + findRightmost(p)->selFlags |= SF_UsesEphemeral;
  2948. + assert( p->pEList );
  2949. +
  2950. + /* Code the SELECTs to our left into temporary table "tab1".
  2951. + */
  2952. + sqlite3SelectDestInit(&intersectdest, SRT_Union, tab1);
  2953. + rc = sqlite3Select(pParse, pPrior, &intersectdest);
  2954. + if( rc ){
  2955. + goto multi_select_end;
  2956. + }
  2957. +
  2958. + /* Code the current SELECT into temporary table "tab2"
  2959. + */
  2960. + addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab2, 0);
  2961. + assert( p->addrOpenEphm[1] == -1 );
  2962. + p->addrOpenEphm[1] = addr;
  2963. + p->pPrior = 0;
  2964. + pLimit = p->pLimit;
  2965. + p->pLimit = 0;
  2966. + intersectdest.iSDParm = tab2;
  2967. + ExplainQueryPlan((pParse, 1, "%s USING TEMP B-TREE",
  2968. + selectOpName(p->op)));
  2969. + rc = sqlite3Select(pParse, p, &intersectdest);
  2970. + testcase( rc!=SQLITE_OK );
  2971. + pDelete = p->pPrior;
  2972. + p->pPrior = pPrior;
  2973. + if( p->nSelectRow>pPrior->nSelectRow ){
  2974. + p->nSelectRow = pPrior->nSelectRow;
  2975. + }
  2976. + sqlite3ExprDelete(db, p->pLimit);
  2977. + p->pLimit = pLimit;
  2978. +
  2979. + /* Generate code to take the intersection of the two temporary
  2980. + ** tables.
  2981. + */
  2982. + if( rc ) break;
  2983. + assert( p->pEList );
  2984. + iBreak = sqlite3VdbeMakeLabel(pParse);
  2985. + iCont = sqlite3VdbeMakeLabel(pParse);
  2986. + computeLimitRegisters(pParse, p, iBreak);
  2987. + sqlite3VdbeAddOp2(v, OP_Rewind, tab1, iBreak); VdbeCoverage(v);
  2988. + r1 = sqlite3GetTempReg(pParse);
  2989. + iStart = sqlite3VdbeAddOp2(v, OP_RowData, tab1, r1);
  2990. + sqlite3VdbeAddOp4Int(v, OP_NotFound, tab2, iCont, r1, 0);
  2991. + VdbeCoverage(v);
  2992. + sqlite3ReleaseTempReg(pParse, r1);
  2993. + selectInnerLoop(pParse, p, tab1,
  2994. + 0, 0, &dest, iCont, iBreak);
  2995. + sqlite3VdbeResolveLabel(v, iCont);
  2996. + sqlite3VdbeAddOp2(v, OP_Next, tab1, iStart); VdbeCoverage(v);
  2997. + sqlite3VdbeResolveLabel(v, iBreak);
  2998. + sqlite3VdbeAddOp2(v, OP_Close, tab2, 0);
  2999. + sqlite3VdbeAddOp2(v, OP_Close, tab1, 0);
  3000. + break;
  3001. + }
  3002. + }
  3003. +
  3004. + #ifndef SQLITE_OMIT_EXPLAIN
  3005. + if( p->pNext==0 ){
  3006. + ExplainQueryPlanPop(pParse);
  3007. + }
  3008. + #endif
  3009. + }
  3010. + if( pParse->nErr ) goto multi_select_end;
  3011. +
  3012. + /* Compute collating sequences used by
  3013. + ** temporary tables needed to implement the compound select.
  3014. + ** Attach the KeyInfo structure to all temporary tables.
  3015. + **
  3016. + ** This section is run by the right-most SELECT statement only.
  3017. + ** SELECT statements to the left always skip this part. The right-most
  3018. + ** SELECT might also skip this part if it has no ORDER BY clause and
  3019. + ** no temp tables are required.
  3020. + */
  3021. + if( p->selFlags & SF_UsesEphemeral ){
  3022. + int i; /* Loop counter */
  3023. + KeyInfo *pKeyInfo; /* Collating sequence for the result set */
  3024. + Select *pLoop; /* For looping through SELECT statements */
  3025. + CollSeq **apColl; /* For looping through pKeyInfo->aColl[] */
  3026. + int nCol; /* Number of columns in result set */
  3027. +
  3028. + assert( p->pNext==0 );
  3029. + nCol = p->pEList->nExpr;
  3030. + pKeyInfo = sqlite3KeyInfoAlloc(db, nCol, 1);
  3031. + if( !pKeyInfo ){
  3032. + rc = SQLITE_NOMEM_BKPT;
  3033. + goto multi_select_end;
  3034. + }
  3035. + for(i=0, apColl=pKeyInfo->aColl; i<nCol; i++, apColl++){
  3036. + *apColl = multiSelectCollSeq(pParse, p, i);
  3037. + if( 0==*apColl ){
  3038. + *apColl = db->pDfltColl;
  3039. + }
  3040. + }
  3041. +
  3042. + for(pLoop=p; pLoop; pLoop=pLoop->pPrior){
  3043. + for(i=0; i<2; i++){
  3044. + int addr = pLoop->addrOpenEphm[i];
  3045. + if( addr<0 ){
  3046. + /* If [0] is unused then [1] is also unused. So we can
  3047. + ** always safely abort as soon as the first unused slot is found */
  3048. + assert( pLoop->addrOpenEphm[1]<0 );
  3049. + break;
  3050. + }
  3051. + sqlite3VdbeChangeP2(v, addr, nCol);
  3052. + sqlite3VdbeChangeP4(v, addr, (char*)sqlite3KeyInfoRef(pKeyInfo),
  3053. + P4_KEYINFO);
  3054. + pLoop->addrOpenEphm[i] = -1;
  3055. + }
  3056. + }
  3057. + sqlite3KeyInfoUnref(pKeyInfo);
  3058. + }
  3059. +
  3060. +multi_select_end:
  3061. + pDest->iSdst = dest.iSdst;
  3062. + pDest->nSdst = dest.nSdst;
  3063. + sqlite3SelectDelete(db, pDelete);
  3064. + return rc;
  3065. +}
  3066. +#endif /* SQLITE_OMIT_COMPOUND_SELECT */
  3067. +
  3068. +/*
  3069. +** Error message for when two or more terms of a compound select have different
  3070. +** size result sets.
  3071. +*/
  3072. +void sqlite3SelectWrongNumTermsError(Parse *pParse, Select *p){
  3073. + if( p->selFlags & SF_Values ){
  3074. + sqlite3ErrorMsg(pParse, "all VALUES must have the same number of terms");
  3075. + }else{
  3076. + sqlite3ErrorMsg(pParse, "SELECTs to the left and right of %s"
  3077. + " do not have the same number of result columns", selectOpName(p->op));
  3078. + }
  3079. +}
  3080. +
  3081. +/*
  3082. +** Code an output subroutine for a coroutine implementation of a
  3083. +** SELECT statment.
  3084. +**
  3085. +** The data to be output is contained in pIn->iSdst. There are
  3086. +** pIn->nSdst columns to be output. pDest is where the output should
  3087. +** be sent.
  3088. +**
  3089. +** regReturn is the number of the register holding the subroutine
  3090. +** return address.
  3091. +**
  3092. +** If regPrev>0 then it is the first register in a vector that
  3093. +** records the previous output. mem[regPrev] is a flag that is false
  3094. +** if there has been no previous output. If regPrev>0 then code is
  3095. +** generated to suppress duplicates. pKeyInfo is used for comparing
  3096. +** keys.
  3097. +**
  3098. +** If the LIMIT found in p->iLimit is reached, jump immediately to
  3099. +** iBreak.
  3100. +*/
  3101. +static int generateOutputSubroutine(
  3102. + Parse *pParse, /* Parsing context */
  3103. + Select *p, /* The SELECT statement */
  3104. + SelectDest *pIn, /* Coroutine supplying data */
  3105. + SelectDest *pDest, /* Where to send the data */
  3106. + int regReturn, /* The return address register */
  3107. + int regPrev, /* Previous result register. No uniqueness if 0 */
  3108. + KeyInfo *pKeyInfo, /* For comparing with previous entry */
  3109. + int iBreak /* Jump here if we hit the LIMIT */
  3110. +){
  3111. + Vdbe *v = pParse->pVdbe;
  3112. + int iContinue;
  3113. + int addr;
  3114. +
  3115. + addr = sqlite3VdbeCurrentAddr(v);
  3116. + iContinue = sqlite3VdbeMakeLabel(pParse);
  3117. +
  3118. + /* Suppress duplicates for UNION, EXCEPT, and INTERSECT
  3119. + */
  3120. + if( regPrev ){
  3121. + int addr1, addr2;
  3122. + addr1 = sqlite3VdbeAddOp1(v, OP_IfNot, regPrev); VdbeCoverage(v);
  3123. + addr2 = sqlite3VdbeAddOp4(v, OP_Compare, pIn->iSdst, regPrev+1, pIn->nSdst,
  3124. + (char*)sqlite3KeyInfoRef(pKeyInfo), P4_KEYINFO);
  3125. + sqlite3VdbeAddOp3(v, OP_Jump, addr2+2, iContinue, addr2+2); VdbeCoverage(v);
  3126. + sqlite3VdbeJumpHere(v, addr1);
  3127. + sqlite3VdbeAddOp3(v, OP_Copy, pIn->iSdst, regPrev+1, pIn->nSdst-1);
  3128. + sqlite3VdbeAddOp2(v, OP_Integer, 1, regPrev);
  3129. + }
  3130. + if( pParse->db->mallocFailed ) return 0;
  3131. +
  3132. + /* Suppress the first OFFSET entries if there is an OFFSET clause
  3133. + */
  3134. + codeOffset(v, p->iOffset, iContinue);
  3135. +
  3136. + assert( pDest->eDest!=SRT_Exists );
  3137. + assert( pDest->eDest!=SRT_Table );
  3138. + switch( pDest->eDest ){
  3139. + /* Store the result as data using a unique key.
  3140. + */
  3141. + case SRT_EphemTab: {
  3142. + int r1 = sqlite3GetTempReg(pParse);
  3143. + int r2 = sqlite3GetTempReg(pParse);
  3144. + sqlite3VdbeAddOp3(v, OP_MakeRecord, pIn->iSdst, pIn->nSdst, r1);
  3145. + sqlite3VdbeAddOp2(v, OP_NewRowid, pDest->iSDParm, r2);
  3146. + sqlite3VdbeAddOp3(v, OP_Insert, pDest->iSDParm, r1, r2);
  3147. + sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
  3148. + sqlite3ReleaseTempReg(pParse, r2);
  3149. + sqlite3ReleaseTempReg(pParse, r1);
  3150. + break;
  3151. + }
  3152. +
  3153. +#ifndef SQLITE_OMIT_SUBQUERY
  3154. + /* If we are creating a set for an "expr IN (SELECT ...)".
  3155. + */
  3156. + case SRT_Set: {
  3157. + int r1;
  3158. + testcase( pIn->nSdst>1 );
  3159. + r1 = sqlite3GetTempReg(pParse);
  3160. + sqlite3VdbeAddOp4(v, OP_MakeRecord, pIn->iSdst, pIn->nSdst,
  3161. + r1, pDest->zAffSdst, pIn->nSdst);
  3162. + sqlite3VdbeAddOp4Int(v, OP_IdxInsert, pDest->iSDParm, r1,
  3163. + pIn->iSdst, pIn->nSdst);
  3164. + sqlite3ReleaseTempReg(pParse, r1);
  3165. + break;
  3166. + }
  3167. +
  3168. + /* If this is a scalar select that is part of an expression, then
  3169. + ** store the results in the appropriate memory cell and break out
  3170. + ** of the scan loop. Note that the select might return multiple columns
  3171. + ** if it is the RHS of a row-value IN operator.
  3172. + */
  3173. + case SRT_Mem: {
  3174. + if( pParse->nErr==0 ){
  3175. + testcase( pIn->nSdst>1 );
  3176. + sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSDParm, pIn->nSdst);
  3177. + }
  3178. + /* The LIMIT clause will jump out of the loop for us */
  3179. + break;
  3180. + }
  3181. +#endif /* #ifndef SQLITE_OMIT_SUBQUERY */
  3182. +
  3183. + /* The results are stored in a sequence of registers
  3184. + ** starting at pDest->iSdst. Then the co-routine yields.
  3185. + */
  3186. + case SRT_Coroutine: {
  3187. + if( pDest->iSdst==0 ){
  3188. + pDest->iSdst = sqlite3GetTempRange(pParse, pIn->nSdst);
  3189. + pDest->nSdst = pIn->nSdst;
  3190. + }
  3191. + sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSdst, pIn->nSdst);
  3192. + sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
  3193. + break;
  3194. + }
  3195. +
  3196. + /* If none of the above, then the result destination must be
  3197. + ** SRT_Output. This routine is never called with any other
  3198. + ** destination other than the ones handled above or SRT_Output.
  3199. + **
  3200. + ** For SRT_Output, results are stored in a sequence of registers.
  3201. + ** Then the OP_ResultRow opcode is used to cause sqlite3_step() to
  3202. + ** return the next row of result.
  3203. + */
  3204. + default: {
  3205. + assert( pDest->eDest==SRT_Output );
  3206. + sqlite3VdbeAddOp2(v, OP_ResultRow, pIn->iSdst, pIn->nSdst);
  3207. + break;
  3208. + }
  3209. + }
  3210. +
  3211. + /* Jump to the end of the loop if the LIMIT is reached.
  3212. + */
  3213. + if( p->iLimit ){
  3214. + sqlite3VdbeAddOp2(v, OP_DecrJumpZero, p->iLimit, iBreak); VdbeCoverage(v);
  3215. + }
  3216. +
  3217. + /* Generate the subroutine return
  3218. + */
  3219. + sqlite3VdbeResolveLabel(v, iContinue);
  3220. + sqlite3VdbeAddOp1(v, OP_Return, regReturn);
  3221. +
  3222. + return addr;
  3223. +}
  3224. +
  3225. +/*
  3226. +** Alternative compound select code generator for cases when there
  3227. +** is an ORDER BY clause.
  3228. +**
  3229. +** We assume a query of the following form:
  3230. +**
  3231. +** <selectA> <operator> <selectB> ORDER BY <orderbylist>
  3232. +**
  3233. +** <operator> is one of UNION ALL, UNION, EXCEPT, or INTERSECT. The idea
  3234. +** is to code both <selectA> and <selectB> with the ORDER BY clause as
  3235. +** co-routines. Then run the co-routines in parallel and merge the results
  3236. +** into the output. In addition to the two coroutines (called selectA and
  3237. +** selectB) there are 7 subroutines:
  3238. +**
  3239. +** outA: Move the output of the selectA coroutine into the output
  3240. +** of the compound query.
  3241. +**
  3242. +** outB: Move the output of the selectB coroutine into the output
  3243. +** of the compound query. (Only generated for UNION and
  3244. +** UNION ALL. EXCEPT and INSERTSECT never output a row that
  3245. +** appears only in B.)
  3246. +**
  3247. +** AltB: Called when there is data from both coroutines and A<B.
  3248. +**
  3249. +** AeqB: Called when there is data from both coroutines and A==B.
  3250. +**
  3251. +** AgtB: Called when there is data from both coroutines and A>B.
  3252. +**
  3253. +** EofA: Called when data is exhausted from selectA.
  3254. +**
  3255. +** EofB: Called when data is exhausted from selectB.
  3256. +**
  3257. +** The implementation of the latter five subroutines depend on which
  3258. +** <operator> is used:
  3259. +**
  3260. +**
  3261. +** UNION ALL UNION EXCEPT INTERSECT
  3262. +** ------------- ----------------- -------------- -----------------
  3263. +** AltB: outA, nextA outA, nextA outA, nextA nextA
  3264. +**
  3265. +** AeqB: outA, nextA nextA nextA outA, nextA
  3266. +**
  3267. +** AgtB: outB, nextB outB, nextB nextB nextB
  3268. +**
  3269. +** EofA: outB, nextB outB, nextB halt halt
  3270. +**
  3271. +** EofB: outA, nextA outA, nextA outA, nextA halt
  3272. +**
  3273. +** In the AltB, AeqB, and AgtB subroutines, an EOF on A following nextA
  3274. +** causes an immediate jump to EofA and an EOF on B following nextB causes
  3275. +** an immediate jump to EofB. Within EofA and EofB, and EOF on entry or
  3276. +** following nextX causes a jump to the end of the select processing.
  3277. +**
  3278. +** Duplicate removal in the UNION, EXCEPT, and INTERSECT cases is handled
  3279. +** within the output subroutine. The regPrev register set holds the previously
  3280. +** output value. A comparison is made against this value and the output
  3281. +** is skipped if the next results would be the same as the previous.
  3282. +**
  3283. +** The implementation plan is to implement the two coroutines and seven
  3284. +** subroutines first, then put the control logic at the bottom. Like this:
  3285. +**
  3286. +** goto Init
  3287. +** coA: coroutine for left query (A)
  3288. +** coB: coroutine for right query (B)
  3289. +** outA: output one row of A
  3290. +** outB: output one row of B (UNION and UNION ALL only)
  3291. +** EofA: ...
  3292. +** EofB: ...
  3293. +** AltB: ...
  3294. +** AeqB: ...
  3295. +** AgtB: ...
  3296. +** Init: initialize coroutine registers
  3297. +** yield coA
  3298. +** if eof(A) goto EofA
  3299. +** yield coB
  3300. +** if eof(B) goto EofB
  3301. +** Cmpr: Compare A, B
  3302. +** Jump AltB, AeqB, AgtB
  3303. +** End: ...
  3304. +**
  3305. +** We call AltB, AeqB, AgtB, EofA, and EofB "subroutines" but they are not
  3306. +** actually called using Gosub and they do not Return. EofA and EofB loop
  3307. +** until all data is exhausted then jump to the "end" labe. AltB, AeqB,
  3308. +** and AgtB jump to either L2 or to one of EofA or EofB.
  3309. +*/
  3310. +#ifndef SQLITE_OMIT_COMPOUND_SELECT
  3311. +static int multiSelectOrderBy(
  3312. + Parse *pParse, /* Parsing context */
  3313. + Select *p, /* The right-most of SELECTs to be coded */
  3314. + SelectDest *pDest /* What to do with query results */
  3315. +){
  3316. + int i, j; /* Loop counters */
  3317. + Select *pPrior; /* Another SELECT immediately to our left */
  3318. + Vdbe *v; /* Generate code to this VDBE */
  3319. + SelectDest destA; /* Destination for coroutine A */
  3320. + SelectDest destB; /* Destination for coroutine B */
  3321. + int regAddrA; /* Address register for select-A coroutine */
  3322. + int regAddrB; /* Address register for select-B coroutine */
  3323. + int addrSelectA; /* Address of the select-A coroutine */
  3324. + int addrSelectB; /* Address of the select-B coroutine */
  3325. + int regOutA; /* Address register for the output-A subroutine */
  3326. + int regOutB; /* Address register for the output-B subroutine */
  3327. + int addrOutA; /* Address of the output-A subroutine */
  3328. + int addrOutB = 0; /* Address of the output-B subroutine */
  3329. + int addrEofA; /* Address of the select-A-exhausted subroutine */
  3330. + int addrEofA_noB; /* Alternate addrEofA if B is uninitialized */
  3331. + int addrEofB; /* Address of the select-B-exhausted subroutine */
  3332. + int addrAltB; /* Address of the A<B subroutine */
  3333. + int addrAeqB; /* Address of the A==B subroutine */
  3334. + int addrAgtB; /* Address of the A>B subroutine */
  3335. + int regLimitA; /* Limit register for select-A */
  3336. + int regLimitB; /* Limit register for select-A */
  3337. + int regPrev; /* A range of registers to hold previous output */
  3338. + int savedLimit; /* Saved value of p->iLimit */
  3339. + int savedOffset; /* Saved value of p->iOffset */
  3340. + int labelCmpr; /* Label for the start of the merge algorithm */
  3341. + int labelEnd; /* Label for the end of the overall SELECT stmt */
  3342. + int addr1; /* Jump instructions that get retargetted */
  3343. + int op; /* One of TK_ALL, TK_UNION, TK_EXCEPT, TK_INTERSECT */
  3344. + KeyInfo *pKeyDup = 0; /* Comparison information for duplicate removal */
  3345. + KeyInfo *pKeyMerge; /* Comparison information for merging rows */
  3346. + sqlite3 *db; /* Database connection */
  3347. + ExprList *pOrderBy; /* The ORDER BY clause */
  3348. + int nOrderBy; /* Number of terms in the ORDER BY clause */
  3349. + int *aPermute; /* Mapping from ORDER BY terms to result set columns */
  3350. +
  3351. + assert( p->pOrderBy!=0 );
  3352. + assert( pKeyDup==0 ); /* "Managed" code needs this. Ticket #3382. */
  3353. + db = pParse->db;
  3354. + v = pParse->pVdbe;
  3355. + assert( v!=0 ); /* Already thrown the error if VDBE alloc failed */
  3356. + labelEnd = sqlite3VdbeMakeLabel(pParse);
  3357. + labelCmpr = sqlite3VdbeMakeLabel(pParse);
  3358. +
  3359. +
  3360. + /* Patch up the ORDER BY clause
  3361. + */
  3362. + op = p->op;
  3363. + pPrior = p->pPrior;
  3364. + assert( pPrior->pOrderBy==0 );
  3365. + pOrderBy = p->pOrderBy;
  3366. + assert( pOrderBy );
  3367. + nOrderBy = pOrderBy->nExpr;
  3368. +
  3369. + /* For operators other than UNION ALL we have to make sure that
  3370. + ** the ORDER BY clause covers every term of the result set. Add
  3371. + ** terms to the ORDER BY clause as necessary.
  3372. + */
  3373. + if( op!=TK_ALL ){
  3374. + for(i=1; db->mallocFailed==0 && i<=p->pEList->nExpr; i++){
  3375. + struct ExprList_item *pItem;
  3376. + for(j=0, pItem=pOrderBy->a; j<nOrderBy; j++, pItem++){
  3377. + assert( pItem->u.x.iOrderByCol>0 );
  3378. + if( pItem->u.x.iOrderByCol==i ) break;
  3379. + }
  3380. + if( j==nOrderBy ){
  3381. + Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0);
  3382. + if( pNew==0 ) return SQLITE_NOMEM_BKPT;
  3383. + pNew->flags |= EP_IntValue;
  3384. + pNew->u.iValue = i;
  3385. + p->pOrderBy = pOrderBy = sqlite3ExprListAppend(pParse, pOrderBy, pNew);
  3386. + if( pOrderBy ) pOrderBy->a[nOrderBy++].u.x.iOrderByCol = (u16)i;
  3387. + }
  3388. + }
  3389. + }
  3390. +
  3391. + /* Compute the comparison permutation and keyinfo that is used with
  3392. + ** the permutation used to determine if the next
  3393. + ** row of results comes from selectA or selectB. Also add explicit
  3394. + ** collations to the ORDER BY clause terms so that when the subqueries
  3395. + ** to the right and the left are evaluated, they use the correct
  3396. + ** collation.
  3397. + */
  3398. + aPermute = sqlite3DbMallocRawNN(db, sizeof(int)*(nOrderBy + 1));
  3399. + if( aPermute ){
  3400. + struct ExprList_item *pItem;
  3401. + aPermute[0] = nOrderBy;
  3402. + for(i=1, pItem=pOrderBy->a; i<=nOrderBy; i++, pItem++){
  3403. + assert( pItem->u.x.iOrderByCol>0 );
  3404. + assert( pItem->u.x.iOrderByCol<=p->pEList->nExpr );
  3405. + aPermute[i] = pItem->u.x.iOrderByCol - 1;
  3406. + }
  3407. + pKeyMerge = multiSelectOrderByKeyInfo(pParse, p, 1);
  3408. + }else{
  3409. + pKeyMerge = 0;
  3410. + }
  3411. +
  3412. + /* Reattach the ORDER BY clause to the query.
  3413. + */
  3414. + p->pOrderBy = pOrderBy;
  3415. + pPrior->pOrderBy = sqlite3ExprListDup(pParse->db, pOrderBy, 0);
  3416. +
  3417. + /* Allocate a range of temporary registers and the KeyInfo needed
  3418. + ** for the logic that removes duplicate result rows when the
  3419. + ** operator is UNION, EXCEPT, or INTERSECT (but not UNION ALL).
  3420. + */
  3421. + if( op==TK_ALL ){
  3422. + regPrev = 0;
  3423. + }else{
  3424. + int nExpr = p->pEList->nExpr;
  3425. + assert( nOrderBy>=nExpr || db->mallocFailed );
  3426. + regPrev = pParse->nMem+1;
  3427. + pParse->nMem += nExpr+1;
  3428. + sqlite3VdbeAddOp2(v, OP_Integer, 0, regPrev);
  3429. + pKeyDup = sqlite3KeyInfoAlloc(db, nExpr, 1);
  3430. + if( pKeyDup ){
  3431. + assert( sqlite3KeyInfoIsWriteable(pKeyDup) );
  3432. + for(i=0; i<nExpr; i++){
  3433. + pKeyDup->aColl[i] = multiSelectCollSeq(pParse, p, i);
  3434. + pKeyDup->aSortFlags[i] = 0;
  3435. + }
  3436. + }
  3437. + }
  3438. +
  3439. + /* Separate the left and the right query from one another
  3440. + */
  3441. + p->pPrior = 0;
  3442. + pPrior->pNext = 0;
  3443. + sqlite3ResolveOrderGroupBy(pParse, p, p->pOrderBy, "ORDER");
  3444. + if( pPrior->pPrior==0 ){
  3445. + sqlite3ResolveOrderGroupBy(pParse, pPrior, pPrior->pOrderBy, "ORDER");
  3446. + }
  3447. +
  3448. + /* Compute the limit registers */
  3449. + computeLimitRegisters(pParse, p, labelEnd);
  3450. + if( p->iLimit && op==TK_ALL ){
  3451. + regLimitA = ++pParse->nMem;
  3452. + regLimitB = ++pParse->nMem;
  3453. + sqlite3VdbeAddOp2(v, OP_Copy, p->iOffset ? p->iOffset+1 : p->iLimit,
  3454. + regLimitA);
  3455. + sqlite3VdbeAddOp2(v, OP_Copy, regLimitA, regLimitB);
  3456. + }else{
  3457. + regLimitA = regLimitB = 0;
  3458. + }
  3459. + sqlite3ExprDelete(db, p->pLimit);
  3460. + p->pLimit = 0;
  3461. +
  3462. + regAddrA = ++pParse->nMem;
  3463. + regAddrB = ++pParse->nMem;
  3464. + regOutA = ++pParse->nMem;
  3465. + regOutB = ++pParse->nMem;
  3466. + sqlite3SelectDestInit(&destA, SRT_Coroutine, regAddrA);
  3467. + sqlite3SelectDestInit(&destB, SRT_Coroutine, regAddrB);
  3468. +
  3469. + ExplainQueryPlan((pParse, 1, "MERGE (%s)", selectOpName(p->op)));
  3470. +
  3471. + /* Generate a coroutine to evaluate the SELECT statement to the
  3472. + ** left of the compound operator - the "A" select.
  3473. + */
  3474. + addrSelectA = sqlite3VdbeCurrentAddr(v) + 1;
  3475. + addr1 = sqlite3VdbeAddOp3(v, OP_InitCoroutine, regAddrA, 0, addrSelectA);
  3476. + VdbeComment((v, "left SELECT"));
  3477. + pPrior->iLimit = regLimitA;
  3478. + ExplainQueryPlan((pParse, 1, "LEFT"));
  3479. + sqlite3Select(pParse, pPrior, &destA);
  3480. + sqlite3VdbeEndCoroutine(v, regAddrA);
  3481. + sqlite3VdbeJumpHere(v, addr1);
  3482. +
  3483. + /* Generate a coroutine to evaluate the SELECT statement on
  3484. + ** the right - the "B" select
  3485. + */
  3486. + addrSelectB = sqlite3VdbeCurrentAddr(v) + 1;
  3487. + addr1 = sqlite3VdbeAddOp3(v, OP_InitCoroutine, regAddrB, 0, addrSelectB);
  3488. + VdbeComment((v, "right SELECT"));
  3489. + savedLimit = p->iLimit;
  3490. + savedOffset = p->iOffset;
  3491. + p->iLimit = regLimitB;
  3492. + p->iOffset = 0;
  3493. + ExplainQueryPlan((pParse, 1, "RIGHT"));
  3494. + sqlite3Select(pParse, p, &destB);
  3495. + p->iLimit = savedLimit;
  3496. + p->iOffset = savedOffset;
  3497. + sqlite3VdbeEndCoroutine(v, regAddrB);
  3498. +
  3499. + /* Generate a subroutine that outputs the current row of the A
  3500. + ** select as the next output row of the compound select.
  3501. + */
  3502. + VdbeNoopComment((v, "Output routine for A"));
  3503. + addrOutA = generateOutputSubroutine(pParse,
  3504. + p, &destA, pDest, regOutA,
  3505. + regPrev, pKeyDup, labelEnd);
  3506. +
  3507. + /* Generate a subroutine that outputs the current row of the B
  3508. + ** select as the next output row of the compound select.
  3509. + */
  3510. + if( op==TK_ALL || op==TK_UNION ){
  3511. + VdbeNoopComment((v, "Output routine for B"));
  3512. + addrOutB = generateOutputSubroutine(pParse,
  3513. + p, &destB, pDest, regOutB,
  3514. + regPrev, pKeyDup, labelEnd);
  3515. + }
  3516. + sqlite3KeyInfoUnref(pKeyDup);
  3517. +
  3518. + /* Generate a subroutine to run when the results from select A
  3519. + ** are exhausted and only data in select B remains.
  3520. + */
  3521. + if( op==TK_EXCEPT || op==TK_INTERSECT ){
  3522. + addrEofA_noB = addrEofA = labelEnd;
  3523. + }else{
  3524. + VdbeNoopComment((v, "eof-A subroutine"));
  3525. + addrEofA = sqlite3VdbeAddOp2(v, OP_Gosub, regOutB, addrOutB);
  3526. + addrEofA_noB = sqlite3VdbeAddOp2(v, OP_Yield, regAddrB, labelEnd);
  3527. + VdbeCoverage(v);
  3528. + sqlite3VdbeGoto(v, addrEofA);
  3529. + p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
  3530. + }
  3531. +
  3532. + /* Generate a subroutine to run when the results from select B
  3533. + ** are exhausted and only data in select A remains.
  3534. + */
  3535. + if( op==TK_INTERSECT ){
  3536. + addrEofB = addrEofA;
  3537. + if( p->nSelectRow > pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow;
  3538. + }else{
  3539. + VdbeNoopComment((v, "eof-B subroutine"));
  3540. + addrEofB = sqlite3VdbeAddOp2(v, OP_Gosub, regOutA, addrOutA);
  3541. + sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, labelEnd); VdbeCoverage(v);
  3542. + sqlite3VdbeGoto(v, addrEofB);
  3543. + }
  3544. +
  3545. + /* Generate code to handle the case of A<B
  3546. + */
  3547. + VdbeNoopComment((v, "A-lt-B subroutine"));
  3548. + addrAltB = sqlite3VdbeAddOp2(v, OP_Gosub, regOutA, addrOutA);
  3549. + sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, addrEofA); VdbeCoverage(v);
  3550. + sqlite3VdbeGoto(v, labelCmpr);
  3551. +
  3552. + /* Generate code to handle the case of A==B
  3553. + */
  3554. + if( op==TK_ALL ){
  3555. + addrAeqB = addrAltB;
  3556. + }else if( op==TK_INTERSECT ){
  3557. + addrAeqB = addrAltB;
  3558. + addrAltB++;
  3559. + }else{
  3560. + VdbeNoopComment((v, "A-eq-B subroutine"));
  3561. + addrAeqB =
  3562. + sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, addrEofA); VdbeCoverage(v);
  3563. + sqlite3VdbeGoto(v, labelCmpr);
  3564. + }
  3565. +
  3566. + /* Generate code to handle the case of A>B
  3567. + */
  3568. + VdbeNoopComment((v, "A-gt-B subroutine"));
  3569. + addrAgtB = sqlite3VdbeCurrentAddr(v);
  3570. + if( op==TK_ALL || op==TK_UNION ){
  3571. + sqlite3VdbeAddOp2(v, OP_Gosub, regOutB, addrOutB);
  3572. + }
  3573. + sqlite3VdbeAddOp2(v, OP_Yield, regAddrB, addrEofB); VdbeCoverage(v);
  3574. + sqlite3VdbeGoto(v, labelCmpr);
  3575. +
  3576. + /* This code runs once to initialize everything.
  3577. + */
  3578. + sqlite3VdbeJumpHere(v, addr1);
  3579. + sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, addrEofA_noB); VdbeCoverage(v);
  3580. + sqlite3VdbeAddOp2(v, OP_Yield, regAddrB, addrEofB); VdbeCoverage(v);
  3581. +
  3582. + /* Implement the main merge loop
  3583. + */
  3584. + sqlite3VdbeResolveLabel(v, labelCmpr);
  3585. + sqlite3VdbeAddOp4(v, OP_Permutation, 0, 0, 0, (char*)aPermute, P4_INTARRAY);
  3586. + sqlite3VdbeAddOp4(v, OP_Compare, destA.iSdst, destB.iSdst, nOrderBy,
  3587. + (char*)pKeyMerge, P4_KEYINFO);
  3588. + sqlite3VdbeChangeP5(v, OPFLAG_PERMUTE);
  3589. + sqlite3VdbeAddOp3(v, OP_Jump, addrAltB, addrAeqB, addrAgtB); VdbeCoverage(v);
  3590. +
  3591. + /* Jump to the this point in order to terminate the query.
  3592. + */
  3593. + sqlite3VdbeResolveLabel(v, labelEnd);
  3594. +
  3595. + /* Reassembly the compound query so that it will be freed correctly
  3596. + ** by the calling function */
  3597. + if( p->pPrior ){
  3598. + sqlite3SelectDelete(db, p->pPrior);
  3599. + }
  3600. + p->pPrior = pPrior;
  3601. + pPrior->pNext = p;
  3602. +
  3603. + /*** TBD: Insert subroutine calls to close cursors on incomplete
  3604. + **** subqueries ****/
  3605. + ExplainQueryPlanPop(pParse);
  3606. + return pParse->nErr!=0;
  3607. +}
  3608. +#endif
  3609. +
  3610. +#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
  3611. +
  3612. +/* An instance of the SubstContext object describes an substitution edit
  3613. +** to be performed on a parse tree.
  3614. +**
  3615. +** All references to columns in table iTable are to be replaced by corresponding
  3616. +** expressions in pEList.
  3617. +*/
  3618. +typedef struct SubstContext {
  3619. + Parse *pParse; /* The parsing context */
  3620. + int iTable; /* Replace references to this table */
  3621. + int iNewTable; /* New table number */
  3622. + int isLeftJoin; /* Add TK_IF_NULL_ROW opcodes on each replacement */
  3623. + ExprList *pEList; /* Replacement expressions */
  3624. +} SubstContext;
  3625. +
  3626. +/* Forward Declarations */
  3627. +static void substExprList(SubstContext*, ExprList*);
  3628. +static void substSelect(SubstContext*, Select*, int);
  3629. +
  3630. +/*
  3631. +** Scan through the expression pExpr. Replace every reference to
  3632. +** a column in table number iTable with a copy of the iColumn-th
  3633. +** entry in pEList. (But leave references to the ROWID column
  3634. +** unchanged.)
  3635. +**
  3636. +** This routine is part of the flattening procedure. A subquery
  3637. +** whose result set is defined by pEList appears as entry in the
  3638. +** FROM clause of a SELECT such that the VDBE cursor assigned to that
  3639. +** FORM clause entry is iTable. This routine makes the necessary
  3640. +** changes to pExpr so that it refers directly to the source table
  3641. +** of the subquery rather the result set of the subquery.
  3642. +*/
  3643. +static Expr *substExpr(
  3644. + SubstContext *pSubst, /* Description of the substitution */
  3645. + Expr *pExpr /* Expr in which substitution occurs */
  3646. +){
  3647. + if( pExpr==0 ) return 0;
  3648. + if( ExprHasProperty(pExpr, EP_FromJoin)
  3649. + && pExpr->iRightJoinTable==pSubst->iTable
  3650. + ){
  3651. + pExpr->iRightJoinTable = pSubst->iNewTable;
  3652. + }
  3653. + if( pExpr->op==TK_COLUMN
  3654. + && pExpr->iTable==pSubst->iTable
  3655. + && !ExprHasProperty(pExpr, EP_FixedCol)
  3656. + ){
  3657. + if( pExpr->iColumn<0 ){
  3658. + pExpr->op = TK_NULL;
  3659. + }else{
  3660. + Expr *pNew;
  3661. + Expr *pCopy = pSubst->pEList->a[pExpr->iColumn].pExpr;
  3662. + Expr ifNullRow;
  3663. + assert( pSubst->pEList!=0 && pExpr->iColumn<pSubst->pEList->nExpr );
  3664. + assert( pExpr->pRight==0 );
  3665. + if( sqlite3ExprIsVector(pCopy) ){
  3666. + sqlite3VectorErrorMsg(pSubst->pParse, pCopy);
  3667. + }else{
  3668. + sqlite3 *db = pSubst->pParse->db;
  3669. + if( pSubst->isLeftJoin && pCopy->op!=TK_COLUMN ){
  3670. + memset(&ifNullRow, 0, sizeof(ifNullRow));
  3671. + ifNullRow.op = TK_IF_NULL_ROW;
  3672. + ifNullRow.pLeft = pCopy;
  3673. + ifNullRow.iTable = pSubst->iNewTable;
  3674. + ifNullRow.flags = EP_Skip;
  3675. + pCopy = &ifNullRow;
  3676. + }
  3677. + testcase( ExprHasProperty(pCopy, EP_Subquery) );
  3678. + pNew = sqlite3ExprDup(db, pCopy, 0);
  3679. + if( pNew && pSubst->isLeftJoin ){
  3680. + ExprSetProperty(pNew, EP_CanBeNull);
  3681. + }
  3682. + if( pNew && ExprHasProperty(pExpr,EP_FromJoin) ){
  3683. + pNew->iRightJoinTable = pExpr->iRightJoinTable;
  3684. + ExprSetProperty(pNew, EP_FromJoin);
  3685. + }
  3686. + sqlite3ExprDelete(db, pExpr);
  3687. + pExpr = pNew;
  3688. +
  3689. + /* Ensure that the expression now has an implicit collation sequence,
  3690. + ** just as it did when it was a column of a view or sub-query. */
  3691. + if( pExpr ){
  3692. + if( pExpr->op!=TK_COLUMN && pExpr->op!=TK_COLLATE ){
  3693. + CollSeq *pColl = sqlite3ExprCollSeq(pSubst->pParse, pExpr);
  3694. + pExpr = sqlite3ExprAddCollateString(pSubst->pParse, pExpr,
  3695. + (pColl ? pColl->zName : "BINARY")
  3696. + );
  3697. + }
  3698. + ExprClearProperty(pExpr, EP_Collate);
  3699. + }
  3700. + }
  3701. + }
  3702. + }else{
  3703. + if( pExpr->op==TK_IF_NULL_ROW && pExpr->iTable==pSubst->iTable ){
  3704. + pExpr->iTable = pSubst->iNewTable;
  3705. + }
  3706. + pExpr->pLeft = substExpr(pSubst, pExpr->pLeft);
  3707. + pExpr->pRight = substExpr(pSubst, pExpr->pRight);
  3708. + if( ExprHasProperty(pExpr, EP_xIsSelect) ){
  3709. + substSelect(pSubst, pExpr->x.pSelect, 1);
  3710. + }else{
  3711. + substExprList(pSubst, pExpr->x.pList);
  3712. + }
  3713. +#ifndef SQLITE_OMIT_WINDOWFUNC
  3714. + if( ExprHasProperty(pExpr, EP_WinFunc) ){
  3715. + Window *pWin = pExpr->y.pWin;
  3716. + pWin->pFilter = substExpr(pSubst, pWin->pFilter);
  3717. + substExprList(pSubst, pWin->pPartition);
  3718. + substExprList(pSubst, pWin->pOrderBy);
  3719. + }
  3720. +#endif
  3721. + }
  3722. + return pExpr;
  3723. +}
  3724. +static void substExprList(
  3725. + SubstContext *pSubst, /* Description of the substitution */
  3726. + ExprList *pList /* List to scan and in which to make substitutes */
  3727. +){
  3728. + int i;
  3729. + if( pList==0 ) return;
  3730. + for(i=0; i<pList->nExpr; i++){
  3731. + pList->a[i].pExpr = substExpr(pSubst, pList->a[i].pExpr);
  3732. + }
  3733. +}
  3734. +static void substSelect(
  3735. + SubstContext *pSubst, /* Description of the substitution */
  3736. + Select *p, /* SELECT statement in which to make substitutions */
  3737. + int doPrior /* Do substitutes on p->pPrior too */
  3738. +){
  3739. + SrcList *pSrc;
  3740. + struct SrcList_item *pItem;
  3741. + int i;
  3742. + if( !p ) return;
  3743. + do{
  3744. + substExprList(pSubst, p->pEList);
  3745. + substExprList(pSubst, p->pGroupBy);
  3746. + substExprList(pSubst, p->pOrderBy);
  3747. + p->pHaving = substExpr(pSubst, p->pHaving);
  3748. + p->pWhere = substExpr(pSubst, p->pWhere);
  3749. + pSrc = p->pSrc;
  3750. + assert( pSrc!=0 );
  3751. + for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
  3752. + substSelect(pSubst, pItem->pSelect, 1);
  3753. + if( pItem->fg.isTabFunc ){
  3754. + substExprList(pSubst, pItem->u1.pFuncArg);
  3755. + }
  3756. + }
  3757. + }while( doPrior && (p = p->pPrior)!=0 );
  3758. +}
  3759. +#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
  3760. +
  3761. +#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
  3762. +/*
  3763. +** pSelect is a SELECT statement and pSrcItem is one item in the FROM
  3764. +** clause of that SELECT.
  3765. +**
  3766. +** This routine scans the entire SELECT statement and recomputes the
  3767. +** pSrcItem->colUsed mask.
  3768. +*/
  3769. +static int recomputeColumnsUsedExpr(Walker *pWalker, Expr *pExpr){
  3770. + struct SrcList_item *pItem;
  3771. + if( pExpr->op!=TK_COLUMN ) return WRC_Continue;
  3772. + pItem = pWalker->u.pSrcItem;
  3773. + if( pItem->iCursor!=pExpr->iTable ) return WRC_Continue;
  3774. + if( pExpr->iColumn<0 ) return WRC_Continue;
  3775. + pItem->colUsed |= sqlite3ExprColUsed(pExpr);
  3776. + return WRC_Continue;
  3777. +}
  3778. +static void recomputeColumnsUsed(
  3779. + Select *pSelect, /* The complete SELECT statement */
  3780. + struct SrcList_item *pSrcItem /* Which FROM clause item to recompute */
  3781. +){
  3782. + Walker w;
  3783. + if( NEVER(pSrcItem->pTab==0) ) return;
  3784. + memset(&w, 0, sizeof(w));
  3785. + w.xExprCallback = recomputeColumnsUsedExpr;
  3786. + w.xSelectCallback = sqlite3SelectWalkNoop;
  3787. + w.u.pSrcItem = pSrcItem;
  3788. + pSrcItem->colUsed = 0;
  3789. + sqlite3WalkSelect(&w, pSelect);
  3790. +}
  3791. +#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
  3792. +
  3793. +#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
  3794. +/*
  3795. +** This routine attempts to flatten subqueries as a performance optimization.
  3796. +** This routine returns 1 if it makes changes and 0 if no flattening occurs.
  3797. +**
  3798. +** To understand the concept of flattening, consider the following
  3799. +** query:
  3800. +**
  3801. +** SELECT a FROM (SELECT x+y AS a FROM t1 WHERE z<100) WHERE a>5
  3802. +**
  3803. +** The default way of implementing this query is to execute the
  3804. +** subquery first and store the results in a temporary table, then
  3805. +** run the outer query on that temporary table. This requires two
  3806. +** passes over the data. Furthermore, because the temporary table
  3807. +** has no indices, the WHERE clause on the outer query cannot be
  3808. +** optimized.
  3809. +**
  3810. +** This routine attempts to rewrite queries such as the above into
  3811. +** a single flat select, like this:
  3812. +**
  3813. +** SELECT x+y AS a FROM t1 WHERE z<100 AND a>5
  3814. +**
  3815. +** The code generated for this simplification gives the same result
  3816. +** but only has to scan the data once. And because indices might
  3817. +** exist on the table t1, a complete scan of the data might be
  3818. +** avoided.
  3819. +**
  3820. +** Flattening is subject to the following constraints:
  3821. +**
  3822. +** (**) We no longer attempt to flatten aggregate subqueries. Was:
  3823. +** The subquery and the outer query cannot both be aggregates.
  3824. +**
  3825. +** (**) We no longer attempt to flatten aggregate subqueries. Was:
  3826. +** (2) If the subquery is an aggregate then
  3827. +** (2a) the outer query must not be a join and
  3828. +** (2b) the outer query must not use subqueries
  3829. +** other than the one FROM-clause subquery that is a candidate
  3830. +** for flattening. (This is due to ticket [2f7170d73bf9abf80]
  3831. +** from 2015-02-09.)
  3832. +**
  3833. +** (3) If the subquery is the right operand of a LEFT JOIN then
  3834. +** (3a) the subquery may not be a join and
  3835. +** (3b) the FROM clause of the subquery may not contain a virtual
  3836. +** table and
  3837. +** (3c) the outer query may not be an aggregate.
  3838. +** (3d) the outer query may not be DISTINCT.
  3839. +**
  3840. +** (4) The subquery can not be DISTINCT.
  3841. +**
  3842. +** (**) At one point restrictions (4) and (5) defined a subset of DISTINCT
  3843. +** sub-queries that were excluded from this optimization. Restriction
  3844. +** (4) has since been expanded to exclude all DISTINCT subqueries.
  3845. +**
  3846. +** (**) We no longer attempt to flatten aggregate subqueries. Was:
  3847. +** If the subquery is aggregate, the outer query may not be DISTINCT.
  3848. +**
  3849. +** (7) The subquery must have a FROM clause. TODO: For subqueries without
  3850. +** A FROM clause, consider adding a FROM clause with the special
  3851. +** table sqlite_once that consists of a single row containing a
  3852. +** single NULL.
  3853. +**
  3854. +** (8) If the subquery uses LIMIT then the outer query may not be a join.
  3855. +**
  3856. +** (9) If the subquery uses LIMIT then the outer query may not be aggregate.
  3857. +**
  3858. +** (**) Restriction (10) was removed from the code on 2005-02-05 but we
  3859. +** accidently carried the comment forward until 2014-09-15. Original
  3860. +** constraint: "If the subquery is aggregate then the outer query
  3861. +** may not use LIMIT."
  3862. +**
  3863. +** (11) The subquery and the outer query may not both have ORDER BY clauses.
  3864. +**
  3865. +** (**) Not implemented. Subsumed into restriction (3). Was previously
  3866. +** a separate restriction deriving from ticket #350.
  3867. +**
  3868. +** (13) The subquery and outer query may not both use LIMIT.
  3869. +**
  3870. +** (14) The subquery may not use OFFSET.
  3871. +**
  3872. +** (15) If the outer query is part of a compound select, then the
  3873. +** subquery may not use LIMIT.
  3874. +** (See ticket #2339 and ticket [02a8e81d44]).
  3875. +**
  3876. +** (16) If the outer query is aggregate, then the subquery may not
  3877. +** use ORDER BY. (Ticket #2942) This used to not matter
  3878. +** until we introduced the group_concat() function.
  3879. +**
  3880. +** (17) If the subquery is a compound select, then
  3881. +** (17a) all compound operators must be a UNION ALL, and
  3882. +** (17b) no terms within the subquery compound may be aggregate
  3883. +** or DISTINCT, and
  3884. +** (17c) every term within the subquery compound must have a FROM clause
  3885. +** (17d) the outer query may not be
  3886. +** (17d1) aggregate, or
  3887. +** (17d2) DISTINCT, or
  3888. +** (17d3) a join.
  3889. +** (17e) the subquery may not contain window functions
  3890. +**
  3891. +** The parent and sub-query may contain WHERE clauses. Subject to
  3892. +** rules (11), (13) and (14), they may also contain ORDER BY,
  3893. +** LIMIT and OFFSET clauses. The subquery cannot use any compound
  3894. +** operator other than UNION ALL because all the other compound
  3895. +** operators have an implied DISTINCT which is disallowed by
  3896. +** restriction (4).
  3897. +**
  3898. +** Also, each component of the sub-query must return the same number
  3899. +** of result columns. This is actually a requirement for any compound
  3900. +** SELECT statement, but all the code here does is make sure that no
  3901. +** such (illegal) sub-query is flattened. The caller will detect the
  3902. +** syntax error and return a detailed message.
  3903. +**
  3904. +** (18) If the sub-query is a compound select, then all terms of the
  3905. +** ORDER BY clause of the parent must be simple references to
  3906. +** columns of the sub-query.
  3907. +**
  3908. +** (19) If the subquery uses LIMIT then the outer query may not
  3909. +** have a WHERE clause.
  3910. +**
  3911. +** (20) If the sub-query is a compound select, then it must not use
  3912. +** an ORDER BY clause. Ticket #3773. We could relax this constraint
  3913. +** somewhat by saying that the terms of the ORDER BY clause must
  3914. +** appear as unmodified result columns in the outer query. But we
  3915. +** have other optimizations in mind to deal with that case.
  3916. +**
  3917. +** (21) If the subquery uses LIMIT then the outer query may not be
  3918. +** DISTINCT. (See ticket [752e1646fc]).
  3919. +**
  3920. +** (22) The subquery may not be a recursive CTE.
  3921. +**
  3922. +** (**) Subsumed into restriction (17d3). Was: If the outer query is
  3923. +** a recursive CTE, then the sub-query may not be a compound query.
  3924. +** This restriction is because transforming the
  3925. +** parent to a compound query confuses the code that handles
  3926. +** recursive queries in multiSelect().
  3927. +**
  3928. +** (**) We no longer attempt to flatten aggregate subqueries. Was:
  3929. +** The subquery may not be an aggregate that uses the built-in min() or
  3930. +** or max() functions. (Without this restriction, a query like:
  3931. +** "SELECT x FROM (SELECT max(y), x FROM t1)" would not necessarily
  3932. +** return the value X for which Y was maximal.)
  3933. +**
  3934. +** (25) If either the subquery or the parent query contains a window
  3935. +** function in the select list or ORDER BY clause, flattening
  3936. +** is not attempted.
  3937. +**
  3938. +**
  3939. +** In this routine, the "p" parameter is a pointer to the outer query.
  3940. +** The subquery is p->pSrc->a[iFrom]. isAgg is true if the outer query
  3941. +** uses aggregates.
  3942. +**
  3943. +** If flattening is not attempted, this routine is a no-op and returns 0.
  3944. +** If flattening is attempted this routine returns 1.
  3945. +**
  3946. +** All of the expression analysis must occur on both the outer query and
  3947. +** the subquery before this routine runs.
  3948. +*/
  3949. +static int flattenSubquery(
  3950. + Parse *pParse, /* Parsing context */
  3951. + Select *p, /* The parent or outer SELECT statement */
  3952. + int iFrom, /* Index in p->pSrc->a[] of the inner subquery */
  3953. + int isAgg /* True if outer SELECT uses aggregate functions */
  3954. +){
  3955. + const char *zSavedAuthContext = pParse->zAuthContext;
  3956. + Select *pParent; /* Current UNION ALL term of the other query */
  3957. + Select *pSub; /* The inner query or "subquery" */
  3958. + Select *pSub1; /* Pointer to the rightmost select in sub-query */
  3959. + SrcList *pSrc; /* The FROM clause of the outer query */
  3960. + SrcList *pSubSrc; /* The FROM clause of the subquery */
  3961. + int iParent; /* VDBE cursor number of the pSub result set temp table */
  3962. + int iNewParent = -1;/* Replacement table for iParent */
  3963. + int isLeftJoin = 0; /* True if pSub is the right side of a LEFT JOIN */
  3964. + int i; /* Loop counter */
  3965. + Expr *pWhere; /* The WHERE clause */
  3966. + struct SrcList_item *pSubitem; /* The subquery */
  3967. + sqlite3 *db = pParse->db;
  3968. +
  3969. + /* Check to see if flattening is permitted. Return 0 if not.
  3970. + */
  3971. + assert( p!=0 );
  3972. + assert( p->pPrior==0 );
  3973. + if( OptimizationDisabled(db, SQLITE_QueryFlattener) ) return 0;
  3974. + pSrc = p->pSrc;
  3975. + assert( pSrc && iFrom>=0 && iFrom<pSrc->nSrc );
  3976. + pSubitem = &pSrc->a[iFrom];
  3977. + iParent = pSubitem->iCursor;
  3978. + pSub = pSubitem->pSelect;
  3979. + assert( pSub!=0 );
  3980. +
  3981. +#ifndef SQLITE_OMIT_WINDOWFUNC
  3982. + if( p->pWin || pSub->pWin ) return 0; /* Restriction (25) */
  3983. +#endif
  3984. +
  3985. + pSubSrc = pSub->pSrc;
  3986. + assert( pSubSrc );
  3987. + /* Prior to version 3.1.2, when LIMIT and OFFSET had to be simple constants,
  3988. + ** not arbitrary expressions, we allowed some combining of LIMIT and OFFSET
  3989. + ** because they could be computed at compile-time. But when LIMIT and OFFSET
  3990. + ** became arbitrary expressions, we were forced to add restrictions (13)
  3991. + ** and (14). */
  3992. + if( pSub->pLimit && p->pLimit ) return 0; /* Restriction (13) */
  3993. + if( pSub->pLimit && pSub->pLimit->pRight ) return 0; /* Restriction (14) */
  3994. + if( (p->selFlags & SF_Compound)!=0 && pSub->pLimit ){
  3995. + return 0; /* Restriction (15) */
  3996. + }
  3997. + if( pSubSrc->nSrc==0 ) return 0; /* Restriction (7) */
  3998. + if( pSub->selFlags & SF_Distinct ) return 0; /* Restriction (4) */
  3999. + if( pSub->pLimit && (pSrc->nSrc>1 || isAgg) ){
  4000. + return 0; /* Restrictions (8)(9) */
  4001. + }
  4002. + if( p->pOrderBy && pSub->pOrderBy ){
  4003. + return 0; /* Restriction (11) */
  4004. + }
  4005. + if( isAgg && pSub->pOrderBy ) return 0; /* Restriction (16) */
  4006. + if( pSub->pLimit && p->pWhere ) return 0; /* Restriction (19) */
  4007. + if( pSub->pLimit && (p->selFlags & SF_Distinct)!=0 ){
  4008. + return 0; /* Restriction (21) */
  4009. + }
  4010. + if( pSub->selFlags & (SF_Recursive) ){
  4011. + return 0; /* Restrictions (22) */
  4012. + }
  4013. +
  4014. + /*
  4015. + ** If the subquery is the right operand of a LEFT JOIN, then the
  4016. + ** subquery may not be a join itself (3a). Example of why this is not
  4017. + ** allowed:
  4018. + **
  4019. + ** t1 LEFT OUTER JOIN (t2 JOIN t3)
  4020. + **
  4021. + ** If we flatten the above, we would get
  4022. + **
  4023. + ** (t1 LEFT OUTER JOIN t2) JOIN t3
  4024. + **
  4025. + ** which is not at all the same thing.
  4026. + **
  4027. + ** If the subquery is the right operand of a LEFT JOIN, then the outer
  4028. + ** query cannot be an aggregate. (3c) This is an artifact of the way
  4029. + ** aggregates are processed - there is no mechanism to determine if
  4030. + ** the LEFT JOIN table should be all-NULL.
  4031. + **
  4032. + ** See also tickets #306, #350, and #3300.
  4033. + */
  4034. + if( (pSubitem->fg.jointype & JT_OUTER)!=0 ){
  4035. + isLeftJoin = 1;
  4036. + if( pSubSrc->nSrc>1 /* (3a) */
  4037. + || isAgg /* (3b) */
  4038. + || IsVirtual(pSubSrc->a[0].pTab) /* (3c) */
  4039. + || (p->selFlags & SF_Distinct)!=0 /* (3d) */
  4040. + ){
  4041. + return 0;
  4042. + }
  4043. + }
  4044. +#ifdef SQLITE_EXTRA_IFNULLROW
  4045. + else if( iFrom>0 && !isAgg ){
  4046. + /* Setting isLeftJoin to -1 causes OP_IfNullRow opcodes to be generated for
  4047. + ** every reference to any result column from subquery in a join, even
  4048. + ** though they are not necessary. This will stress-test the OP_IfNullRow
  4049. + ** opcode. */
  4050. + isLeftJoin = -1;
  4051. + }
  4052. +#endif
  4053. +
  4054. + /* Restriction (17): If the sub-query is a compound SELECT, then it must
  4055. + ** use only the UNION ALL operator. And none of the simple select queries
  4056. + ** that make up the compound SELECT are allowed to be aggregate or distinct
  4057. + ** queries.
  4058. + */
  4059. + if( pSub->pPrior ){
  4060. + if( pSub->pOrderBy ){
  4061. + return 0; /* Restriction (20) */
  4062. + }
  4063. + if( isAgg || (p->selFlags & SF_Distinct)!=0 || pSrc->nSrc!=1 ){
  4064. + return 0; /* (17d1), (17d2), or (17d3) */
  4065. + }
  4066. + for(pSub1=pSub; pSub1; pSub1=pSub1->pPrior){
  4067. + testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
  4068. + testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
  4069. + assert( pSub->pSrc!=0 );
  4070. + assert( pSub->pEList->nExpr==pSub1->pEList->nExpr );
  4071. + if( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))!=0 /* (17b) */
  4072. + || (pSub1->pPrior && pSub1->op!=TK_ALL) /* (17a) */
  4073. + || pSub1->pSrc->nSrc<1 /* (17c) */
  4074. +#ifndef SQLITE_OMIT_WINDOWFUNC
  4075. + || pSub1->pWin /* (17e) */
  4076. +#endif
  4077. + ){
  4078. + return 0;
  4079. + }
  4080. + testcase( pSub1->pSrc->nSrc>1 );
  4081. + }
  4082. +
  4083. + /* Restriction (18). */
  4084. + if( p->pOrderBy ){
  4085. + int ii;
  4086. + for(ii=0; ii<p->pOrderBy->nExpr; ii++){
  4087. + if( p->pOrderBy->a[ii].u.x.iOrderByCol==0 ) return 0;
  4088. + }
  4089. + }
  4090. + }
  4091. +
  4092. + /* Ex-restriction (23):
  4093. + ** The only way that the recursive part of a CTE can contain a compound
  4094. + ** subquery is for the subquery to be one term of a join. But if the
  4095. + ** subquery is a join, then the flattening has already been stopped by
  4096. + ** restriction (17d3)
  4097. + */
  4098. + assert( (p->selFlags & SF_Recursive)==0 || pSub->pPrior==0 );
  4099. +
  4100. + /***** If we reach this point, flattening is permitted. *****/
  4101. + SELECTTRACE(1,pParse,p,("flatten %u.%p from term %d\n",
  4102. + pSub->selId, pSub, iFrom));
  4103. +
  4104. + /* Authorize the subquery */
  4105. + pParse->zAuthContext = pSubitem->zName;
  4106. + TESTONLY(i =) sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0);
  4107. + testcase( i==SQLITE_DENY );
  4108. + pParse->zAuthContext = zSavedAuthContext;
  4109. +
  4110. + /* If the sub-query is a compound SELECT statement, then (by restrictions
  4111. + ** 17 and 18 above) it must be a UNION ALL and the parent query must
  4112. + ** be of the form:
  4113. + **
  4114. + ** SELECT <expr-list> FROM (<sub-query>) <where-clause>
  4115. + **
  4116. + ** followed by any ORDER BY, LIMIT and/or OFFSET clauses. This block
  4117. + ** creates N-1 copies of the parent query without any ORDER BY, LIMIT or
  4118. + ** OFFSET clauses and joins them to the left-hand-side of the original
  4119. + ** using UNION ALL operators. In this case N is the number of simple
  4120. + ** select statements in the compound sub-query.
  4121. + **
  4122. + ** Example:
  4123. + **
  4124. + ** SELECT a+1 FROM (
  4125. + ** SELECT x FROM tab
  4126. + ** UNION ALL
  4127. + ** SELECT y FROM tab
  4128. + ** UNION ALL
  4129. + ** SELECT abs(z*2) FROM tab2
  4130. + ** ) WHERE a!=5 ORDER BY 1
  4131. + **
  4132. + ** Transformed into:
  4133. + **
  4134. + ** SELECT x+1 FROM tab WHERE x+1!=5
  4135. + ** UNION ALL
  4136. + ** SELECT y+1 FROM tab WHERE y+1!=5
  4137. + ** UNION ALL
  4138. + ** SELECT abs(z*2)+1 FROM tab2 WHERE abs(z*2)+1!=5
  4139. + ** ORDER BY 1
  4140. + **
  4141. + ** We call this the "compound-subquery flattening".
  4142. + */
  4143. + for(pSub=pSub->pPrior; pSub; pSub=pSub->pPrior){
  4144. + Select *pNew;
  4145. + ExprList *pOrderBy = p->pOrderBy;
  4146. + Expr *pLimit = p->pLimit;
  4147. + Select *pPrior = p->pPrior;
  4148. + p->pOrderBy = 0;
  4149. + p->pSrc = 0;
  4150. + p->pPrior = 0;
  4151. + p->pLimit = 0;
  4152. + pNew = sqlite3SelectDup(db, p, 0);
  4153. + p->pLimit = pLimit;
  4154. + p->pOrderBy = pOrderBy;
  4155. + p->pSrc = pSrc;
  4156. + p->op = TK_ALL;
  4157. + if( pNew==0 ){
  4158. + p->pPrior = pPrior;
  4159. + }else{
  4160. + pNew->pPrior = pPrior;
  4161. + if( pPrior ) pPrior->pNext = pNew;
  4162. + pNew->pNext = p;
  4163. + p->pPrior = pNew;
  4164. + SELECTTRACE(2,pParse,p,("compound-subquery flattener"
  4165. + " creates %u as peer\n",pNew->selId));
  4166. + }
  4167. + if( db->mallocFailed ) return 1;
  4168. + }
  4169. +
  4170. + /* Begin flattening the iFrom-th entry of the FROM clause
  4171. + ** in the outer query.
  4172. + */
  4173. + pSub = pSub1 = pSubitem->pSelect;
  4174. +
  4175. + /* Delete the transient table structure associated with the
  4176. + ** subquery
  4177. + */
  4178. + sqlite3DbFree(db, pSubitem->zDatabase);
  4179. + sqlite3DbFree(db, pSubitem->zName);
  4180. + sqlite3DbFree(db, pSubitem->zAlias);
  4181. + pSubitem->zDatabase = 0;
  4182. + pSubitem->zName = 0;
  4183. + pSubitem->zAlias = 0;
  4184. + pSubitem->pSelect = 0;
  4185. +
  4186. + /* Defer deleting the Table object associated with the
  4187. + ** subquery until code generation is
  4188. + ** complete, since there may still exist Expr.pTab entries that
  4189. + ** refer to the subquery even after flattening. Ticket #3346.
  4190. + **
  4191. + ** pSubitem->pTab is always non-NULL by test restrictions and tests above.
  4192. + */
  4193. + if( ALWAYS(pSubitem->pTab!=0) ){
  4194. + Table *pTabToDel = pSubitem->pTab;
  4195. + if( pTabToDel->nTabRef==1 ){
  4196. + Parse *pToplevel = sqlite3ParseToplevel(pParse);
  4197. + pTabToDel->pNextZombie = pToplevel->pZombieTab;
  4198. + pToplevel->pZombieTab = pTabToDel;
  4199. + }else{
  4200. + pTabToDel->nTabRef--;
  4201. + }
  4202. + pSubitem->pTab = 0;
  4203. + }
  4204. +
  4205. + /* The following loop runs once for each term in a compound-subquery
  4206. + ** flattening (as described above). If we are doing a different kind
  4207. + ** of flattening - a flattening other than a compound-subquery flattening -
  4208. + ** then this loop only runs once.
  4209. + **
  4210. + ** This loop moves all of the FROM elements of the subquery into the
  4211. + ** the FROM clause of the outer query. Before doing this, remember
  4212. + ** the cursor number for the original outer query FROM element in
  4213. + ** iParent. The iParent cursor will never be used. Subsequent code
  4214. + ** will scan expressions looking for iParent references and replace
  4215. + ** those references with expressions that resolve to the subquery FROM
  4216. + ** elements we are now copying in.
  4217. + */
  4218. + for(pParent=p; pParent; pParent=pParent->pPrior, pSub=pSub->pPrior){
  4219. + int nSubSrc;
  4220. + u8 jointype = 0;
  4221. + assert( pSub!=0 );
  4222. + pSubSrc = pSub->pSrc; /* FROM clause of subquery */
  4223. + nSubSrc = pSubSrc->nSrc; /* Number of terms in subquery FROM clause */
  4224. + pSrc = pParent->pSrc; /* FROM clause of the outer query */
  4225. +
  4226. + if( pSrc ){
  4227. + assert( pParent==p ); /* First time through the loop */
  4228. + jointype = pSubitem->fg.jointype;
  4229. + }else{
  4230. + assert( pParent!=p ); /* 2nd and subsequent times through the loop */
  4231. + pSrc = sqlite3SrcListAppend(pParse, 0, 0, 0);
  4232. + if( pSrc==0 ) break;
  4233. + pParent->pSrc = pSrc;
  4234. + }
  4235. +
  4236. + /* The subquery uses a single slot of the FROM clause of the outer
  4237. + ** query. If the subquery has more than one element in its FROM clause,
  4238. + ** then expand the outer query to make space for it to hold all elements
  4239. + ** of the subquery.
  4240. + **
  4241. + ** Example:
  4242. + **
  4243. + ** SELECT * FROM tabA, (SELECT * FROM sub1, sub2), tabB;
  4244. + **
  4245. + ** The outer query has 3 slots in its FROM clause. One slot of the
  4246. + ** outer query (the middle slot) is used by the subquery. The next
  4247. + ** block of code will expand the outer query FROM clause to 4 slots.
  4248. + ** The middle slot is expanded to two slots in order to make space
  4249. + ** for the two elements in the FROM clause of the subquery.
  4250. + */
  4251. + if( nSubSrc>1 ){
  4252. + pSrc = sqlite3SrcListEnlarge(pParse, pSrc, nSubSrc-1,iFrom+1);
  4253. + if( pSrc==0 ) break;
  4254. + pParent->pSrc = pSrc;
  4255. + }
  4256. +
  4257. + /* Transfer the FROM clause terms from the subquery into the
  4258. + ** outer query.
  4259. + */
  4260. + for(i=0; i<nSubSrc; i++){
  4261. + sqlite3IdListDelete(db, pSrc->a[i+iFrom].pUsing);
  4262. + assert( pSrc->a[i+iFrom].fg.isTabFunc==0 );
  4263. + pSrc->a[i+iFrom] = pSubSrc->a[i];
  4264. + iNewParent = pSubSrc->a[i].iCursor;
  4265. + memset(&pSubSrc->a[i], 0, sizeof(pSubSrc->a[i]));
  4266. + }
  4267. + pSrc->a[iFrom].fg.jointype = jointype;
  4268. +
  4269. + /* Now begin substituting subquery result set expressions for
  4270. + ** references to the iParent in the outer query.
  4271. + **
  4272. + ** Example:
  4273. + **
  4274. + ** SELECT a+5, b*10 FROM (SELECT x*3 AS a, y+10 AS b FROM t1) WHERE a>b;
  4275. + ** \ \_____________ subquery __________/ /
  4276. + ** \_____________________ outer query ______________________________/
  4277. + **
  4278. + ** We look at every expression in the outer query and every place we see
  4279. + ** "a" we substitute "x*3" and every place we see "b" we substitute "y+10".
  4280. + */
  4281. + if( pSub->pOrderBy ){
  4282. + /* At this point, any non-zero iOrderByCol values indicate that the
  4283. + ** ORDER BY column expression is identical to the iOrderByCol'th
  4284. + ** expression returned by SELECT statement pSub. Since these values
  4285. + ** do not necessarily correspond to columns in SELECT statement pParent,
  4286. + ** zero them before transfering the ORDER BY clause.
  4287. + **
  4288. + ** Not doing this may cause an error if a subsequent call to this
  4289. + ** function attempts to flatten a compound sub-query into pParent
  4290. + ** (the only way this can happen is if the compound sub-query is
  4291. + ** currently part of pSub->pSrc). See ticket [d11a6e908f]. */
  4292. + ExprList *pOrderBy = pSub->pOrderBy;
  4293. + for(i=0; i<pOrderBy->nExpr; i++){
  4294. + pOrderBy->a[i].u.x.iOrderByCol = 0;
  4295. + }
  4296. + assert( pParent->pOrderBy==0 );
  4297. + pParent->pOrderBy = pOrderBy;
  4298. + pSub->pOrderBy = 0;
  4299. + }
  4300. + pWhere = pSub->pWhere;
  4301. + pSub->pWhere = 0;
  4302. + if( isLeftJoin>0 ){
  4303. + sqlite3SetJoinExpr(pWhere, iNewParent);
  4304. + }
  4305. + pParent->pWhere = sqlite3ExprAnd(pParse, pWhere, pParent->pWhere);
  4306. + if( db->mallocFailed==0 ){
  4307. + SubstContext x;
  4308. + x.pParse = pParse;
  4309. + x.iTable = iParent;
  4310. + x.iNewTable = iNewParent;
  4311. + x.isLeftJoin = isLeftJoin;
  4312. + x.pEList = pSub->pEList;
  4313. + substSelect(&x, pParent, 0);
  4314. + }
  4315. +
  4316. + /* The flattened query is a compound if either the inner or the
  4317. + ** outer query is a compound. */
  4318. + pParent->selFlags |= pSub->selFlags & SF_Compound;
  4319. + assert( (pSub->selFlags & SF_Distinct)==0 ); /* restriction (17b) */
  4320. +
  4321. + /*
  4322. + ** SELECT ... FROM (SELECT ... LIMIT a OFFSET b) LIMIT x OFFSET y;
  4323. + **
  4324. + ** One is tempted to try to add a and b to combine the limits. But this
  4325. + ** does not work if either limit is negative.
  4326. + */
  4327. + if( pSub->pLimit ){
  4328. + pParent->pLimit = pSub->pLimit;
  4329. + pSub->pLimit = 0;
  4330. + }
  4331. +
  4332. + /* Recompute the SrcList_item.colUsed masks for the flattened
  4333. + ** tables. */
  4334. + for(i=0; i<nSubSrc; i++){
  4335. + recomputeColumnsUsed(pParent, &pSrc->a[i+iFrom]);
  4336. + }
  4337. + }
  4338. +
  4339. + /* Finially, delete what is left of the subquery and return
  4340. + ** success.
  4341. + */
  4342. + sqlite3SelectDelete(db, pSub1);
  4343. +
  4344. +#if SELECTTRACE_ENABLED
  4345. + if( sqlite3SelectTrace & 0x100 ){
  4346. + SELECTTRACE(0x100,pParse,p,("After flattening:\n"));
  4347. + sqlite3TreeViewSelect(0, p, 0);
  4348. + }
  4349. +#endif
  4350. +
  4351. + return 1;
  4352. +}
  4353. +#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
  4354. +
  4355. +/*
  4356. +** A structure to keep track of all of the column values that are fixed to
  4357. +** a known value due to WHERE clause constraints of the form COLUMN=VALUE.
  4358. +*/
  4359. +typedef struct WhereConst WhereConst;
  4360. +struct WhereConst {
  4361. + Parse *pParse; /* Parsing context */
  4362. + int nConst; /* Number for COLUMN=CONSTANT terms */
  4363. + int nChng; /* Number of times a constant is propagated */
  4364. + Expr **apExpr; /* [i*2] is COLUMN and [i*2+1] is VALUE */
  4365. +};
  4366. +
  4367. +/*
  4368. +** Add a new entry to the pConst object. Except, do not add duplicate
  4369. +** pColumn entires. Also, do not add if doing so would not be appropriate.
  4370. +**
  4371. +** The caller guarantees the pColumn is a column and pValue is a constant.
  4372. +** This routine has to do some additional checks before completing the
  4373. +** insert.
  4374. +*/
  4375. +static void constInsert(
  4376. + WhereConst *pConst, /* The WhereConst into which we are inserting */
  4377. + Expr *pColumn, /* The COLUMN part of the constraint */
  4378. + Expr *pValue, /* The VALUE part of the constraint */
  4379. + Expr *pExpr /* Overall expression: COLUMN=VALUE or VALUE=COLUMN */
  4380. +){
  4381. + int i;
  4382. + assert( pColumn->op==TK_COLUMN );
  4383. + assert( sqlite3ExprIsConstant(pValue) );
  4384. +
  4385. + if( ExprHasProperty(pColumn, EP_FixedCol) ) return;
  4386. + if( sqlite3ExprAffinity(pValue)!=0 ) return;
  4387. + if( !sqlite3IsBinary(sqlite3ExprCompareCollSeq(pConst->pParse,pExpr)) ){
  4388. + return;
  4389. + }
  4390. +
  4391. + /* 2018-10-25 ticket [cf5ed20f]
  4392. + ** Make sure the same pColumn is not inserted more than once */
  4393. + for(i=0; i<pConst->nConst; i++){
  4394. + const Expr *pE2 = pConst->apExpr[i*2];
  4395. + assert( pE2->op==TK_COLUMN );
  4396. + if( pE2->iTable==pColumn->iTable
  4397. + && pE2->iColumn==pColumn->iColumn
  4398. + ){
  4399. + return; /* Already present. Return without doing anything. */
  4400. + }
  4401. + }
  4402. +
  4403. + pConst->nConst++;
  4404. + pConst->apExpr = sqlite3DbReallocOrFree(pConst->pParse->db, pConst->apExpr,
  4405. + pConst->nConst*2*sizeof(Expr*));
  4406. + if( pConst->apExpr==0 ){
  4407. + pConst->nConst = 0;
  4408. + }else{
  4409. + pConst->apExpr[pConst->nConst*2-2] = pColumn;
  4410. + pConst->apExpr[pConst->nConst*2-1] = pValue;
  4411. + }
  4412. +}
  4413. +
  4414. +/*
  4415. +** Find all terms of COLUMN=VALUE or VALUE=COLUMN in pExpr where VALUE
  4416. +** is a constant expression and where the term must be true because it
  4417. +** is part of the AND-connected terms of the expression. For each term
  4418. +** found, add it to the pConst structure.
  4419. +*/
  4420. +static void findConstInWhere(WhereConst *pConst, Expr *pExpr){
  4421. + Expr *pRight, *pLeft;
  4422. + if( pExpr==0 ) return;
  4423. + if( ExprHasProperty(pExpr, EP_FromJoin) ) return;
  4424. + if( pExpr->op==TK_AND ){
  4425. + findConstInWhere(pConst, pExpr->pRight);
  4426. + findConstInWhere(pConst, pExpr->pLeft);
  4427. + return;
  4428. + }
  4429. + if( pExpr->op!=TK_EQ ) return;
  4430. + pRight = pExpr->pRight;
  4431. + pLeft = pExpr->pLeft;
  4432. + assert( pRight!=0 );
  4433. + assert( pLeft!=0 );
  4434. + if( pRight->op==TK_COLUMN && sqlite3ExprIsConstant(pLeft) ){
  4435. + constInsert(pConst,pRight,pLeft,pExpr);
  4436. + }
  4437. + if( pLeft->op==TK_COLUMN && sqlite3ExprIsConstant(pRight) ){
  4438. + constInsert(pConst,pLeft,pRight,pExpr);
  4439. + }
  4440. +}
  4441. +
  4442. +/*
  4443. +** This is a Walker expression callback. pExpr is a candidate expression
  4444. +** to be replaced by a value. If pExpr is equivalent to one of the
  4445. +** columns named in pWalker->u.pConst, then overwrite it with its
  4446. +** corresponding value.
  4447. +*/
  4448. +static int propagateConstantExprRewrite(Walker *pWalker, Expr *pExpr){
  4449. + int i;
  4450. + WhereConst *pConst;
  4451. + if( pExpr->op!=TK_COLUMN ) return WRC_Continue;
  4452. + if( ExprHasProperty(pExpr, EP_FixedCol|EP_FromJoin) ){
  4453. + testcase( ExprHasProperty(pExpr, EP_FixedCol) );
  4454. + testcase( ExprHasProperty(pExpr, EP_FromJoin) );
  4455. + return WRC_Continue;
  4456. + }
  4457. + pConst = pWalker->u.pConst;
  4458. + for(i=0; i<pConst->nConst; i++){
  4459. + Expr *pColumn = pConst->apExpr[i*2];
  4460. + if( pColumn==pExpr ) continue;
  4461. + if( pColumn->iTable!=pExpr->iTable ) continue;
  4462. + if( pColumn->iColumn!=pExpr->iColumn ) continue;
  4463. + /* A match is found. Add the EP_FixedCol property */
  4464. + pConst->nChng++;
  4465. + ExprClearProperty(pExpr, EP_Leaf);
  4466. + ExprSetProperty(pExpr, EP_FixedCol);
  4467. + assert( pExpr->pLeft==0 );
  4468. + pExpr->pLeft = sqlite3ExprDup(pConst->pParse->db, pConst->apExpr[i*2+1], 0);
  4469. + break;
  4470. + }
  4471. + return WRC_Prune;
  4472. +}
  4473. +
  4474. +/*
  4475. +** The WHERE-clause constant propagation optimization.
  4476. +**
  4477. +** If the WHERE clause contains terms of the form COLUMN=CONSTANT or
  4478. +** CONSTANT=COLUMN that are top-level AND-connected terms that are not
  4479. +** part of a ON clause from a LEFT JOIN, then throughout the query
  4480. +** replace all other occurrences of COLUMN with CONSTANT.
  4481. +**
  4482. +** For example, the query:
  4483. +**
  4484. +** SELECT * FROM t1, t2, t3 WHERE t1.a=39 AND t2.b=t1.a AND t3.c=t2.b
  4485. +**
  4486. +** Is transformed into
  4487. +**
  4488. +** SELECT * FROM t1, t2, t3 WHERE t1.a=39 AND t2.b=39 AND t3.c=39
  4489. +**
  4490. +** Return true if any transformations where made and false if not.
  4491. +**
  4492. +** Implementation note: Constant propagation is tricky due to affinity
  4493. +** and collating sequence interactions. Consider this example:
  4494. +**
  4495. +** CREATE TABLE t1(a INT,b TEXT);
  4496. +** INSERT INTO t1 VALUES(123,'0123');
  4497. +** SELECT * FROM t1 WHERE a=123 AND b=a;
  4498. +** SELECT * FROM t1 WHERE a=123 AND b=123;
  4499. +**
  4500. +** The two SELECT statements above should return different answers. b=a
  4501. +** is alway true because the comparison uses numeric affinity, but b=123
  4502. +** is false because it uses text affinity and '0123' is not the same as '123'.
  4503. +** To work around this, the expression tree is not actually changed from
  4504. +** "b=a" to "b=123" but rather the "a" in "b=a" is tagged with EP_FixedCol
  4505. +** and the "123" value is hung off of the pLeft pointer. Code generator
  4506. +** routines know to generate the constant "123" instead of looking up the
  4507. +** column value. Also, to avoid collation problems, this optimization is
  4508. +** only attempted if the "a=123" term uses the default BINARY collation.
  4509. +*/
  4510. +static int propagateConstants(
  4511. + Parse *pParse, /* The parsing context */
  4512. + Select *p /* The query in which to propagate constants */
  4513. +){
  4514. + WhereConst x;
  4515. + Walker w;
  4516. + int nChng = 0;
  4517. + x.pParse = pParse;
  4518. + do{
  4519. + x.nConst = 0;
  4520. + x.nChng = 0;
  4521. + x.apExpr = 0;
  4522. + findConstInWhere(&x, p->pWhere);
  4523. + if( x.nConst ){
  4524. + memset(&w, 0, sizeof(w));
  4525. + w.pParse = pParse;
  4526. + w.xExprCallback = propagateConstantExprRewrite;
  4527. + w.xSelectCallback = sqlite3SelectWalkNoop;
  4528. + w.xSelectCallback2 = 0;
  4529. + w.walkerDepth = 0;
  4530. + w.u.pConst = &x;
  4531. + sqlite3WalkExpr(&w, p->pWhere);
  4532. + sqlite3DbFree(x.pParse->db, x.apExpr);
  4533. + nChng += x.nChng;
  4534. + }
  4535. + }while( x.nChng );
  4536. + return nChng;
  4537. +}
  4538. +
  4539. +#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
  4540. +/*
  4541. +** Make copies of relevant WHERE clause terms of the outer query into
  4542. +** the WHERE clause of subquery. Example:
  4543. +**
  4544. +** SELECT * FROM (SELECT a AS x, c-d AS y FROM t1) WHERE x=5 AND y=10;
  4545. +**
  4546. +** Transformed into:
  4547. +**
  4548. +** SELECT * FROM (SELECT a AS x, c-d AS y FROM t1 WHERE a=5 AND c-d=10)
  4549. +** WHERE x=5 AND y=10;
  4550. +**
  4551. +** The hope is that the terms added to the inner query will make it more
  4552. +** efficient.
  4553. +**
  4554. +** Do not attempt this optimization if:
  4555. +**
  4556. +** (1) (** This restriction was removed on 2017-09-29. We used to
  4557. +** disallow this optimization for aggregate subqueries, but now
  4558. +** it is allowed by putting the extra terms on the HAVING clause.
  4559. +** The added HAVING clause is pointless if the subquery lacks
  4560. +** a GROUP BY clause. But such a HAVING clause is also harmless
  4561. +** so there does not appear to be any reason to add extra logic
  4562. +** to suppress it. **)
  4563. +**
  4564. +** (2) The inner query is the recursive part of a common table expression.
  4565. +**
  4566. +** (3) The inner query has a LIMIT clause (since the changes to the WHERE
  4567. +** clause would change the meaning of the LIMIT).
  4568. +**
  4569. +** (4) The inner query is the right operand of a LEFT JOIN and the
  4570. +** expression to be pushed down does not come from the ON clause
  4571. +** on that LEFT JOIN.
  4572. +**
  4573. +** (5) The WHERE clause expression originates in the ON or USING clause
  4574. +** of a LEFT JOIN where iCursor is not the right-hand table of that
  4575. +** left join. An example:
  4576. +**
  4577. +** SELECT *
  4578. +** FROM (SELECT 1 AS a1 UNION ALL SELECT 2) AS aa
  4579. +** JOIN (SELECT 1 AS b2 UNION ALL SELECT 2) AS bb ON (a1=b2)
  4580. +** LEFT JOIN (SELECT 8 AS c3 UNION ALL SELECT 9) AS cc ON (b2=2);
  4581. +**
  4582. +** The correct answer is three rows: (1,1,NULL),(2,2,8),(2,2,9).
  4583. +** But if the (b2=2) term were to be pushed down into the bb subquery,
  4584. +** then the (1,1,NULL) row would be suppressed.
  4585. +**
  4586. +** (6) The inner query features one or more window-functions (since
  4587. +** changes to the WHERE clause of the inner query could change the
  4588. +** window over which window functions are calculated).
  4589. +**
  4590. +** Return 0 if no changes are made and non-zero if one or more WHERE clause
  4591. +** terms are duplicated into the subquery.
  4592. +*/
  4593. +static int pushDownWhereTerms(
  4594. + Parse *pParse, /* Parse context (for malloc() and error reporting) */
  4595. + Select *pSubq, /* The subquery whose WHERE clause is to be augmented */
  4596. + Expr *pWhere, /* The WHERE clause of the outer query */
  4597. + int iCursor, /* Cursor number of the subquery */
  4598. + int isLeftJoin /* True if pSubq is the right term of a LEFT JOIN */
  4599. +){
  4600. + Expr *pNew;
  4601. + int nChng = 0;
  4602. + Select *pSel;
  4603. + if( pWhere==0 ) return 0;
  4604. + if( pSubq->selFlags & SF_Recursive ) return 0; /* restriction (2) */
  4605. +
  4606. +#ifndef SQLITE_OMIT_WINDOWFUNC
  4607. + for(pSel=pSubq; pSel; pSel=pSel->pPrior){
  4608. + if( pSel->pWin ) return 0; /* restriction (6) */
  4609. + }
  4610. +#endif
  4611. +
  4612. +#ifdef SQLITE_DEBUG
  4613. + /* Only the first term of a compound can have a WITH clause. But make
  4614. + ** sure no other terms are marked SF_Recursive in case something changes
  4615. + ** in the future.
  4616. + */
  4617. + {
  4618. + Select *pX;
  4619. + for(pX=pSubq; pX; pX=pX->pPrior){
  4620. + assert( (pX->selFlags & (SF_Recursive))==0 );
  4621. + }
  4622. + }
  4623. +#endif
  4624. +
  4625. + if( pSubq->pLimit!=0 ){
  4626. + return 0; /* restriction (3) */
  4627. + }
  4628. + while( pWhere->op==TK_AND ){
  4629. + nChng += pushDownWhereTerms(pParse, pSubq, pWhere->pRight,
  4630. + iCursor, isLeftJoin);
  4631. + pWhere = pWhere->pLeft;
  4632. + }
  4633. + if( isLeftJoin
  4634. + && (ExprHasProperty(pWhere,EP_FromJoin)==0
  4635. + || pWhere->iRightJoinTable!=iCursor)
  4636. + ){
  4637. + return 0; /* restriction (4) */
  4638. + }
  4639. + if( ExprHasProperty(pWhere,EP_FromJoin) && pWhere->iRightJoinTable!=iCursor ){
  4640. + return 0; /* restriction (5) */
  4641. + }
  4642. + if( sqlite3ExprIsTableConstant(pWhere, iCursor) ){
  4643. + nChng++;
  4644. + while( pSubq ){
  4645. + SubstContext x;
  4646. + pNew = sqlite3ExprDup(pParse->db, pWhere, 0);
  4647. + unsetJoinExpr(pNew, -1);
  4648. + x.pParse = pParse;
  4649. + x.iTable = iCursor;
  4650. + x.iNewTable = iCursor;
  4651. + x.isLeftJoin = 0;
  4652. + x.pEList = pSubq->pEList;
  4653. + pNew = substExpr(&x, pNew);
  4654. + if( pSubq->selFlags & SF_Aggregate ){
  4655. + pSubq->pHaving = sqlite3ExprAnd(pParse, pSubq->pHaving, pNew);
  4656. + }else{
  4657. + pSubq->pWhere = sqlite3ExprAnd(pParse, pSubq->pWhere, pNew);
  4658. + }
  4659. + pSubq = pSubq->pPrior;
  4660. + }
  4661. + }
  4662. + return nChng;
  4663. +}
  4664. +#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
  4665. +
  4666. +/*
  4667. +** The pFunc is the only aggregate function in the query. Check to see
  4668. +** if the query is a candidate for the min/max optimization.
  4669. +**
  4670. +** If the query is a candidate for the min/max optimization, then set
  4671. +** *ppMinMax to be an ORDER BY clause to be used for the optimization
  4672. +** and return either WHERE_ORDERBY_MIN or WHERE_ORDERBY_MAX depending on
  4673. +** whether pFunc is a min() or max() function.
  4674. +**
  4675. +** If the query is not a candidate for the min/max optimization, return
  4676. +** WHERE_ORDERBY_NORMAL (which must be zero).
  4677. +**
  4678. +** This routine must be called after aggregate functions have been
  4679. +** located but before their arguments have been subjected to aggregate
  4680. +** analysis.
  4681. +*/
  4682. +static u8 minMaxQuery(sqlite3 *db, Expr *pFunc, ExprList **ppMinMax){
  4683. + int eRet = WHERE_ORDERBY_NORMAL; /* Return value */
  4684. + ExprList *pEList = pFunc->x.pList; /* Arguments to agg function */
  4685. + const char *zFunc; /* Name of aggregate function pFunc */
  4686. + ExprList *pOrderBy;
  4687. + u8 sortFlags = 0;
  4688. +
  4689. + assert( *ppMinMax==0 );
  4690. + assert( pFunc->op==TK_AGG_FUNCTION );
  4691. + assert( !IsWindowFunc(pFunc) );
  4692. + if( pEList==0 || pEList->nExpr!=1 || ExprHasProperty(pFunc, EP_WinFunc) ){
  4693. + return eRet;
  4694. + }
  4695. + zFunc = pFunc->u.zToken;
  4696. + if( sqlite3StrICmp(zFunc, "min")==0 ){
  4697. + eRet = WHERE_ORDERBY_MIN;
  4698. + if( sqlite3ExprCanBeNull(pEList->a[0].pExpr) ){
  4699. + sortFlags = KEYINFO_ORDER_BIGNULL;
  4700. + }
  4701. + }else if( sqlite3StrICmp(zFunc, "max")==0 ){
  4702. + eRet = WHERE_ORDERBY_MAX;
  4703. + sortFlags = KEYINFO_ORDER_DESC;
  4704. + }else{
  4705. + return eRet;
  4706. + }
  4707. + *ppMinMax = pOrderBy = sqlite3ExprListDup(db, pEList, 0);
  4708. + assert( pOrderBy!=0 || db->mallocFailed );
  4709. + if( pOrderBy ) pOrderBy->a[0].sortFlags = sortFlags;
  4710. + return eRet;
  4711. +}
  4712. +
  4713. +/*
  4714. +** The select statement passed as the first argument is an aggregate query.
  4715. +** The second argument is the associated aggregate-info object. This
  4716. +** function tests if the SELECT is of the form:
  4717. +**
  4718. +** SELECT count(*) FROM <tbl>
  4719. +**
  4720. +** where table is a database table, not a sub-select or view. If the query
  4721. +** does match this pattern, then a pointer to the Table object representing
  4722. +** <tbl> is returned. Otherwise, 0 is returned.
  4723. +*/
  4724. +static Table *isSimpleCount(Select *p, AggInfo *pAggInfo){
  4725. + Table *pTab;
  4726. + Expr *pExpr;
  4727. +
  4728. + assert( !p->pGroupBy );
  4729. +
  4730. + if( p->pWhere || p->pEList->nExpr!=1
  4731. + || p->pSrc->nSrc!=1 || p->pSrc->a[0].pSelect
  4732. + ){
  4733. + return 0;
  4734. + }
  4735. + pTab = p->pSrc->a[0].pTab;
  4736. + pExpr = p->pEList->a[0].pExpr;
  4737. + assert( pTab && !pTab->pSelect && pExpr );
  4738. +
  4739. + if( IsVirtual(pTab) ) return 0;
  4740. + if( pExpr->op!=TK_AGG_FUNCTION ) return 0;
  4741. + if( NEVER(pAggInfo->nFunc==0) ) return 0;
  4742. + if( (pAggInfo->aFunc[0].pFunc->funcFlags&SQLITE_FUNC_COUNT)==0 ) return 0;
  4743. + if( ExprHasProperty(pExpr, EP_Distinct|EP_WinFunc) ) return 0;
  4744. +
  4745. + return pTab;
  4746. +}
  4747. +
  4748. +/*
  4749. +** If the source-list item passed as an argument was augmented with an
  4750. +** INDEXED BY clause, then try to locate the specified index. If there
  4751. +** was such a clause and the named index cannot be found, return
  4752. +** SQLITE_ERROR and leave an error in pParse. Otherwise, populate
  4753. +** pFrom->pIndex and return SQLITE_OK.
  4754. +*/
  4755. +int sqlite3IndexedByLookup(Parse *pParse, struct SrcList_item *pFrom){
  4756. + if( pFrom->pTab && pFrom->fg.isIndexedBy ){
  4757. + Table *pTab = pFrom->pTab;
  4758. + char *zIndexedBy = pFrom->u1.zIndexedBy;
  4759. + Index *pIdx;
  4760. + for(pIdx=pTab->pIndex;
  4761. + pIdx && sqlite3StrICmp(pIdx->zName, zIndexedBy);
  4762. + pIdx=pIdx->pNext
  4763. + );
  4764. + if( !pIdx ){
  4765. + sqlite3ErrorMsg(pParse, "no such index: %s", zIndexedBy, 0);
  4766. + pParse->checkSchema = 1;
  4767. + return SQLITE_ERROR;
  4768. + }
  4769. + pFrom->pIBIndex = pIdx;
  4770. + }
  4771. + return SQLITE_OK;
  4772. +}
  4773. +/*
  4774. +** Detect compound SELECT statements that use an ORDER BY clause with
  4775. +** an alternative collating sequence.
  4776. +**
  4777. +** SELECT ... FROM t1 EXCEPT SELECT ... FROM t2 ORDER BY .. COLLATE ...
  4778. +**
  4779. +** These are rewritten as a subquery:
  4780. +**
  4781. +** SELECT * FROM (SELECT ... FROM t1 EXCEPT SELECT ... FROM t2)
  4782. +** ORDER BY ... COLLATE ...
  4783. +**
  4784. +** This transformation is necessary because the multiSelectOrderBy() routine
  4785. +** above that generates the code for a compound SELECT with an ORDER BY clause
  4786. +** uses a merge algorithm that requires the same collating sequence on the
  4787. +** result columns as on the ORDER BY clause. See ticket
  4788. +** http://www.sqlite.org/src/info/6709574d2a
  4789. +**
  4790. +** This transformation is only needed for EXCEPT, INTERSECT, and UNION.
  4791. +** The UNION ALL operator works fine with multiSelectOrderBy() even when
  4792. +** there are COLLATE terms in the ORDER BY.
  4793. +*/
  4794. +static int convertCompoundSelectToSubquery(Walker *pWalker, Select *p){
  4795. + int i;
  4796. + Select *pNew;
  4797. + Select *pX;
  4798. + sqlite3 *db;
  4799. + struct ExprList_item *a;
  4800. + SrcList *pNewSrc;
  4801. + Parse *pParse;
  4802. + Token dummy;
  4803. +
  4804. + if( p->pPrior==0 ) return WRC_Continue;
  4805. + if( p->pOrderBy==0 ) return WRC_Continue;
  4806. + for(pX=p; pX && (pX->op==TK_ALL || pX->op==TK_SELECT); pX=pX->pPrior){}
  4807. + if( pX==0 ) return WRC_Continue;
  4808. + a = p->pOrderBy->a;
  4809. + for(i=p->pOrderBy->nExpr-1; i>=0; i--){
  4810. + if( a[i].pExpr->flags & EP_Collate ) break;
  4811. + }
  4812. + if( i<0 ) return WRC_Continue;
  4813. +
  4814. + /* If we reach this point, that means the transformation is required. */
  4815. +
  4816. + pParse = pWalker->pParse;
  4817. + db = pParse->db;
  4818. + pNew = sqlite3DbMallocZero(db, sizeof(*pNew) );
  4819. + if( pNew==0 ) return WRC_Abort;
  4820. + memset(&dummy, 0, sizeof(dummy));
  4821. + pNewSrc = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&dummy,pNew,0,0);
  4822. + if( pNewSrc==0 ) return WRC_Abort;
  4823. + *pNew = *p;
  4824. + p->pSrc = pNewSrc;
  4825. + p->pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db, TK_ASTERISK, 0));
  4826. + p->op = TK_SELECT;
  4827. + p->pWhere = 0;
  4828. + pNew->pGroupBy = 0;
  4829. + pNew->pHaving = 0;
  4830. + pNew->pOrderBy = 0;
  4831. + p->pPrior = 0;
  4832. + p->pNext = 0;
  4833. + p->pWith = 0;
  4834. +#ifndef SQLITE_OMIT_WINDOWFUNC
  4835. + p->pWinDefn = 0;
  4836. +#endif
  4837. + p->selFlags &= ~SF_Compound;
  4838. + assert( (p->selFlags & SF_Converted)==0 );
  4839. + p->selFlags |= SF_Converted;
  4840. + assert( pNew->pPrior!=0 );
  4841. + pNew->pPrior->pNext = pNew;
  4842. + pNew->pLimit = 0;
  4843. + return WRC_Continue;
  4844. +}
  4845. +
  4846. +/*
  4847. +** Check to see if the FROM clause term pFrom has table-valued function
  4848. +** arguments. If it does, leave an error message in pParse and return
  4849. +** non-zero, since pFrom is not allowed to be a table-valued function.
  4850. +*/
  4851. +static int cannotBeFunction(Parse *pParse, struct SrcList_item *pFrom){
  4852. + if( pFrom->fg.isTabFunc ){
  4853. + sqlite3ErrorMsg(pParse, "'%s' is not a function", pFrom->zName);
  4854. + return 1;
  4855. + }
  4856. + return 0;
  4857. +}
  4858. +
  4859. +#ifndef SQLITE_OMIT_CTE
  4860. +/*
  4861. +** Argument pWith (which may be NULL) points to a linked list of nested
  4862. +** WITH contexts, from inner to outermost. If the table identified by
  4863. +** FROM clause element pItem is really a common-table-expression (CTE)
  4864. +** then return a pointer to the CTE definition for that table. Otherwise
  4865. +** return NULL.
  4866. +**
  4867. +** If a non-NULL value is returned, set *ppContext to point to the With
  4868. +** object that the returned CTE belongs to.
  4869. +*/
  4870. +static struct Cte *searchWith(
  4871. + With *pWith, /* Current innermost WITH clause */
  4872. + struct SrcList_item *pItem, /* FROM clause element to resolve */
  4873. + With **ppContext /* OUT: WITH clause return value belongs to */
  4874. +){
  4875. + const char *zName;
  4876. + if( pItem->zDatabase==0 && (zName = pItem->zName)!=0 ){
  4877. + With *p;
  4878. + for(p=pWith; p; p=p->pOuter){
  4879. + int i;
  4880. + for(i=0; i<p->nCte; i++){
  4881. + if( sqlite3StrICmp(zName, p->a[i].zName)==0 ){
  4882. + *ppContext = p;
  4883. + return &p->a[i];
  4884. + }
  4885. + }
  4886. + }
  4887. + }
  4888. + return 0;
  4889. +}
  4890. +
  4891. +/* The code generator maintains a stack of active WITH clauses
  4892. +** with the inner-most WITH clause being at the top of the stack.
  4893. +**
  4894. +** This routine pushes the WITH clause passed as the second argument
  4895. +** onto the top of the stack. If argument bFree is true, then this
  4896. +** WITH clause will never be popped from the stack. In this case it
  4897. +** should be freed along with the Parse object. In other cases, when
  4898. +** bFree==0, the With object will be freed along with the SELECT
  4899. +** statement with which it is associated.
  4900. +*/
  4901. +void sqlite3WithPush(Parse *pParse, With *pWith, u8 bFree){
  4902. + assert( bFree==0 || (pParse->pWith==0 && pParse->pWithToFree==0) );
  4903. + if( pWith ){
  4904. + assert( pParse->pWith!=pWith );
  4905. + pWith->pOuter = pParse->pWith;
  4906. + pParse->pWith = pWith;
  4907. + if( bFree ) pParse->pWithToFree = pWith;
  4908. + }
  4909. +}
  4910. +
  4911. +/*
  4912. +** This function checks if argument pFrom refers to a CTE declared by
  4913. +** a WITH clause on the stack currently maintained by the parser. And,
  4914. +** if currently processing a CTE expression, if it is a recursive
  4915. +** reference to the current CTE.
  4916. +**
  4917. +** If pFrom falls into either of the two categories above, pFrom->pTab
  4918. +** and other fields are populated accordingly. The caller should check
  4919. +** (pFrom->pTab!=0) to determine whether or not a successful match
  4920. +** was found.
  4921. +**
  4922. +** Whether or not a match is found, SQLITE_OK is returned if no error
  4923. +** occurs. If an error does occur, an error message is stored in the
  4924. +** parser and some error code other than SQLITE_OK returned.
  4925. +*/
  4926. +static int withExpand(
  4927. + Walker *pWalker,
  4928. + struct SrcList_item *pFrom
  4929. +){
  4930. + Parse *pParse = pWalker->pParse;
  4931. + sqlite3 *db = pParse->db;
  4932. + struct Cte *pCte; /* Matched CTE (or NULL if no match) */
  4933. + With *pWith; /* WITH clause that pCte belongs to */
  4934. +
  4935. + assert( pFrom->pTab==0 );
  4936. + if( pParse->nErr ){
  4937. + return SQLITE_ERROR;
  4938. + }
  4939. +
  4940. + pCte = searchWith(pParse->pWith, pFrom, &pWith);
  4941. + if( pCte ){
  4942. + Table *pTab;
  4943. + ExprList *pEList;
  4944. + Select *pSel;
  4945. + Select *pLeft; /* Left-most SELECT statement */
  4946. + int bMayRecursive; /* True if compound joined by UNION [ALL] */
  4947. + With *pSavedWith; /* Initial value of pParse->pWith */
  4948. +
  4949. + /* If pCte->zCteErr is non-NULL at this point, then this is an illegal
  4950. + ** recursive reference to CTE pCte. Leave an error in pParse and return
  4951. + ** early. If pCte->zCteErr is NULL, then this is not a recursive reference.
  4952. + ** In this case, proceed. */
  4953. + if( pCte->zCteErr ){
  4954. + sqlite3ErrorMsg(pParse, pCte->zCteErr, pCte->zName);
  4955. + return SQLITE_ERROR;
  4956. + }
  4957. + if( cannotBeFunction(pParse, pFrom) ) return SQLITE_ERROR;
  4958. +
  4959. + assert( pFrom->pTab==0 );
  4960. + pFrom->pTab = pTab = sqlite3DbMallocZero(db, sizeof(Table));
  4961. + if( pTab==0 ) return WRC_Abort;
  4962. + pTab->nTabRef = 1;
  4963. + pTab->zName = sqlite3DbStrDup(db, pCte->zName);
  4964. + pTab->iPKey = -1;
  4965. + pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
  4966. + pTab->tabFlags |= TF_Ephemeral | TF_NoVisibleRowid;
  4967. + pFrom->pSelect = sqlite3SelectDup(db, pCte->pSelect, 0);
  4968. + if( db->mallocFailed ) return SQLITE_NOMEM_BKPT;
  4969. + assert( pFrom->pSelect );
  4970. +
  4971. + /* Check if this is a recursive CTE. */
  4972. + pSel = pFrom->pSelect;
  4973. + bMayRecursive = ( pSel->op==TK_ALL || pSel->op==TK_UNION );
  4974. + if( bMayRecursive ){
  4975. + int i;
  4976. + SrcList *pSrc = pFrom->pSelect->pSrc;
  4977. + for(i=0; i<pSrc->nSrc; i++){
  4978. + struct SrcList_item *pItem = &pSrc->a[i];
  4979. + if( pItem->zDatabase==0
  4980. + && pItem->zName!=0
  4981. + && 0==sqlite3StrICmp(pItem->zName, pCte->zName)
  4982. + ){
  4983. + pItem->pTab = pTab;
  4984. + pItem->fg.isRecursive = 1;
  4985. + pTab->nTabRef++;
  4986. + pSel->selFlags |= SF_Recursive;
  4987. + }
  4988. + }
  4989. + }
  4990. +
  4991. + /* Only one recursive reference is permitted. */
  4992. + if( pTab->nTabRef>2 ){
  4993. + sqlite3ErrorMsg(
  4994. + pParse, "multiple references to recursive table: %s", pCte->zName
  4995. + );
  4996. + return SQLITE_ERROR;
  4997. + }
  4998. + assert( pTab->nTabRef==1 ||
  4999. + ((pSel->selFlags&SF_Recursive) && pTab->nTabRef==2 ));
  5000. +
  5001. + pCte->zCteErr = "circular reference: %s";
  5002. + pSavedWith = pParse->pWith;
  5003. + pParse->pWith = pWith;
  5004. + if( bMayRecursive ){
  5005. + Select *pPrior = pSel->pPrior;
  5006. + assert( pPrior->pWith==0 );
  5007. + pPrior->pWith = pSel->pWith;
  5008. + sqlite3WalkSelect(pWalker, pPrior);
  5009. + pPrior->pWith = 0;
  5010. + }else{
  5011. + sqlite3WalkSelect(pWalker, pSel);
  5012. + }
  5013. + pParse->pWith = pWith;
  5014. +
  5015. + for(pLeft=pSel; pLeft->pPrior; pLeft=pLeft->pPrior);
  5016. + pEList = pLeft->pEList;
  5017. + if( pCte->pCols ){
  5018. + if( pEList && pEList->nExpr!=pCte->pCols->nExpr ){
  5019. + sqlite3ErrorMsg(pParse, "table %s has %d values for %d columns",
  5020. + pCte->zName, pEList->nExpr, pCte->pCols->nExpr
  5021. + );
  5022. + pParse->pWith = pSavedWith;
  5023. + return SQLITE_ERROR;
  5024. + }
  5025. + pEList = pCte->pCols;
  5026. + }
  5027. +
  5028. + sqlite3ColumnsFromExprList(pParse, pEList, &pTab->nCol, &pTab->aCol);
  5029. + if( bMayRecursive ){
  5030. + if( pSel->selFlags & SF_Recursive ){
  5031. + pCte->zCteErr = "multiple recursive references: %s";
  5032. + }else{
  5033. + pCte->zCteErr = "recursive reference in a subquery: %s";
  5034. + }
  5035. + sqlite3WalkSelect(pWalker, pSel);
  5036. + }
  5037. + pCte->zCteErr = 0;
  5038. + pParse->pWith = pSavedWith;
  5039. + }
  5040. +
  5041. + return SQLITE_OK;
  5042. +}
  5043. +#endif
  5044. +
  5045. +#ifndef SQLITE_OMIT_CTE
  5046. +/*
  5047. +** If the SELECT passed as the second argument has an associated WITH
  5048. +** clause, pop it from the stack stored as part of the Parse object.
  5049. +**
  5050. +** This function is used as the xSelectCallback2() callback by
  5051. +** sqlite3SelectExpand() when walking a SELECT tree to resolve table
  5052. +** names and other FROM clause elements.
  5053. +*/
  5054. +static void selectPopWith(Walker *pWalker, Select *p){
  5055. + Parse *pParse = pWalker->pParse;
  5056. + if( OK_IF_ALWAYS_TRUE(pParse->pWith) && p->pPrior==0 ){
  5057. + With *pWith = findRightmost(p)->pWith;
  5058. + if( pWith!=0 ){
  5059. + assert( pParse->pWith==pWith || pParse->nErr );
  5060. + pParse->pWith = pWith->pOuter;
  5061. + }
  5062. + }
  5063. +}
  5064. +#else
  5065. +#define selectPopWith 0
  5066. +#endif
  5067. +
  5068. +/*
  5069. +** The SrcList_item structure passed as the second argument represents a
  5070. +** sub-query in the FROM clause of a SELECT statement. This function
  5071. +** allocates and populates the SrcList_item.pTab object. If successful,
  5072. +** SQLITE_OK is returned. Otherwise, if an OOM error is encountered,
  5073. +** SQLITE_NOMEM.
  5074. +*/
  5075. +int sqlite3ExpandSubquery(Parse *pParse, struct SrcList_item *pFrom){
  5076. + Select *pSel = pFrom->pSelect;
  5077. + Table *pTab;
  5078. +
  5079. + assert( pSel );
  5080. + pFrom->pTab = pTab = sqlite3DbMallocZero(pParse->db, sizeof(Table));
  5081. + if( pTab==0 ) return SQLITE_NOMEM;
  5082. + pTab->nTabRef = 1;
  5083. + if( pFrom->zAlias ){
  5084. + pTab->zName = sqlite3DbStrDup(pParse->db, pFrom->zAlias);
  5085. + }else{
  5086. + pTab->zName = sqlite3MPrintf(pParse->db, "subquery_%u", pSel->selId);
  5087. + }
  5088. + while( pSel->pPrior ){ pSel = pSel->pPrior; }
  5089. + sqlite3ColumnsFromExprList(pParse, pSel->pEList,&pTab->nCol,&pTab->aCol);
  5090. + pTab->iPKey = -1;
  5091. + pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
  5092. + pTab->tabFlags |= TF_Ephemeral;
  5093. +
  5094. + return pParse->nErr ? SQLITE_ERROR : SQLITE_OK;
  5095. +}
  5096. +
  5097. +/*
  5098. +** This routine is a Walker callback for "expanding" a SELECT statement.
  5099. +** "Expanding" means to do the following:
  5100. +**
  5101. +** (1) Make sure VDBE cursor numbers have been assigned to every
  5102. +** element of the FROM clause.
  5103. +**
  5104. +** (2) Fill in the pTabList->a[].pTab fields in the SrcList that
  5105. +** defines FROM clause. When views appear in the FROM clause,
  5106. +** fill pTabList->a[].pSelect with a copy of the SELECT statement
  5107. +** that implements the view. A copy is made of the view's SELECT
  5108. +** statement so that we can freely modify or delete that statement
  5109. +** without worrying about messing up the persistent representation
  5110. +** of the view.
  5111. +**
  5112. +** (3) Add terms to the WHERE clause to accommodate the NATURAL keyword
  5113. +** on joins and the ON and USING clause of joins.
  5114. +**
  5115. +** (4) Scan the list of columns in the result set (pEList) looking
  5116. +** for instances of the "*" operator or the TABLE.* operator.
  5117. +** If found, expand each "*" to be every column in every table
  5118. +** and TABLE.* to be every column in TABLE.
  5119. +**
  5120. +*/
  5121. +static int selectExpander(Walker *pWalker, Select *p){
  5122. + Parse *pParse = pWalker->pParse;
  5123. + int i, j, k;
  5124. + SrcList *pTabList;
  5125. + ExprList *pEList;
  5126. + struct SrcList_item *pFrom;
  5127. + sqlite3 *db = pParse->db;
  5128. + Expr *pE, *pRight, *pExpr;
  5129. + u16 selFlags = p->selFlags;
  5130. + u32 elistFlags = 0;
  5131. +
  5132. + p->selFlags |= SF_Expanded;
  5133. + if( db->mallocFailed ){
  5134. + return WRC_Abort;
  5135. + }
  5136. + assert( p->pSrc!=0 );
  5137. + if( (selFlags & SF_Expanded)!=0 ){
  5138. + return WRC_Prune;
  5139. + }
  5140. + if( pWalker->eCode ){
  5141. + /* Renumber selId because it has been copied from a view */
  5142. + p->selId = ++pParse->nSelect;
  5143. + }
  5144. + pTabList = p->pSrc;
  5145. + pEList = p->pEList;
  5146. + sqlite3WithPush(pParse, p->pWith, 0);
  5147. +
  5148. + /* Make sure cursor numbers have been assigned to all entries in
  5149. + ** the FROM clause of the SELECT statement.
  5150. + */
  5151. + sqlite3SrcListAssignCursors(pParse, pTabList);
  5152. +
  5153. + /* Look up every table named in the FROM clause of the select. If
  5154. + ** an entry of the FROM clause is a subquery instead of a table or view,
  5155. + ** then create a transient table structure to describe the subquery.
  5156. + */
  5157. + for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
  5158. + Table *pTab;
  5159. + assert( pFrom->fg.isRecursive==0 || pFrom->pTab!=0 );
  5160. + if( pFrom->fg.isRecursive ) continue;
  5161. + assert( pFrom->pTab==0 );
  5162. +#ifndef SQLITE_OMIT_CTE
  5163. + if( withExpand(pWalker, pFrom) ) return WRC_Abort;
  5164. + if( pFrom->pTab ) {} else
  5165. +#endif
  5166. + if( pFrom->zName==0 ){
  5167. +#ifndef SQLITE_OMIT_SUBQUERY
  5168. + Select *pSel = pFrom->pSelect;
  5169. + /* A sub-query in the FROM clause of a SELECT */
  5170. + assert( pSel!=0 );
  5171. + assert( pFrom->pTab==0 );
  5172. + if( sqlite3WalkSelect(pWalker, pSel) ) return WRC_Abort;
  5173. + if( sqlite3ExpandSubquery(pParse, pFrom) ) return WRC_Abort;
  5174. +#endif
  5175. + }else{
  5176. + /* An ordinary table or view name in the FROM clause */
  5177. + assert( pFrom->pTab==0 );
  5178. + pFrom->pTab = pTab = sqlite3LocateTableItem(pParse, 0, pFrom);
  5179. + if( pTab==0 ) return WRC_Abort;
  5180. + if( pTab->nTabRef>=0xffff ){
  5181. + sqlite3ErrorMsg(pParse, "too many references to \"%s\": max 65535",
  5182. + pTab->zName);
  5183. + pFrom->pTab = 0;
  5184. + return WRC_Abort;
  5185. + }
  5186. + pTab->nTabRef++;
  5187. + if( !IsVirtual(pTab) && cannotBeFunction(pParse, pFrom) ){
  5188. + return WRC_Abort;
  5189. + }
  5190. +#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
  5191. + if( IsVirtual(pTab) || pTab->pSelect ){
  5192. + i16 nCol;
  5193. + u8 eCodeOrig = pWalker->eCode;
  5194. + if( sqlite3ViewGetColumnNames(pParse, pTab) ) return WRC_Abort;
  5195. + assert( pFrom->pSelect==0 );
  5196. + if( pTab->pSelect && (db->flags & SQLITE_EnableView)==0 ){
  5197. + sqlite3ErrorMsg(pParse, "access to view \"%s\" prohibited",
  5198. + pTab->zName);
  5199. + }
  5200. +#ifndef SQLITE_OMIT_VIRTUALTABLE
  5201. + if( IsVirtual(pTab)
  5202. + && pFrom->fg.fromDDL
  5203. + && ALWAYS(pTab->pVTable!=0)
  5204. + && pTab->pVTable->eVtabRisk > ((db->flags & SQLITE_TrustedSchema)!=0)
  5205. + ){
  5206. + sqlite3ErrorMsg(pParse, "unsafe use of virtual table \"%s\"",
  5207. + pTab->zName);
  5208. + }
  5209. +#endif
  5210. + pFrom->pSelect = sqlite3SelectDup(db, pTab->pSelect, 0);
  5211. + nCol = pTab->nCol;
  5212. + pTab->nCol = -1;
  5213. + pWalker->eCode = 1; /* Turn on Select.selId renumbering */
  5214. + sqlite3WalkSelect(pWalker, pFrom->pSelect);
  5215. + pWalker->eCode = eCodeOrig;
  5216. + pTab->nCol = nCol;
  5217. + }
  5218. +#endif
  5219. + }
  5220. +
  5221. + /* Locate the index named by the INDEXED BY clause, if any. */
  5222. + if( sqlite3IndexedByLookup(pParse, pFrom) ){
  5223. + return WRC_Abort;
  5224. + }
  5225. + }
  5226. +
  5227. + /* Process NATURAL keywords, and ON and USING clauses of joins.
  5228. + */
  5229. + if( pParse->nErr || db->mallocFailed || sqliteProcessJoin(pParse, p) ){
  5230. + return WRC_Abort;
  5231. + }
  5232. +
  5233. + /* For every "*" that occurs in the column list, insert the names of
  5234. + ** all columns in all tables. And for every TABLE.* insert the names
  5235. + ** of all columns in TABLE. The parser inserted a special expression
  5236. + ** with the TK_ASTERISK operator for each "*" that it found in the column
  5237. + ** list. The following code just has to locate the TK_ASTERISK
  5238. + ** expressions and expand each one to the list of all columns in
  5239. + ** all tables.
  5240. + **
  5241. + ** The first loop just checks to see if there are any "*" operators
  5242. + ** that need expanding.
  5243. + */
  5244. + for(k=0; k<pEList->nExpr; k++){
  5245. + pE = pEList->a[k].pExpr;
  5246. + if( pE->op==TK_ASTERISK ) break;
  5247. + assert( pE->op!=TK_DOT || pE->pRight!=0 );
  5248. + assert( pE->op!=TK_DOT || (pE->pLeft!=0 && pE->pLeft->op==TK_ID) );
  5249. + if( pE->op==TK_DOT && pE->pRight->op==TK_ASTERISK ) break;
  5250. + elistFlags |= pE->flags;
  5251. + }
  5252. + if( k<pEList->nExpr ){
  5253. + /*
  5254. + ** If we get here it means the result set contains one or more "*"
  5255. + ** operators that need to be expanded. Loop through each expression
  5256. + ** in the result set and expand them one by one.
  5257. + */
  5258. + struct ExprList_item *a = pEList->a;
  5259. + ExprList *pNew = 0;
  5260. + int flags = pParse->db->flags;
  5261. + int longNames = (flags & SQLITE_FullColNames)!=0
  5262. + && (flags & SQLITE_ShortColNames)==0;
  5263. +
  5264. + for(k=0; k<pEList->nExpr; k++){
  5265. + pE = a[k].pExpr;
  5266. + elistFlags |= pE->flags;
  5267. + pRight = pE->pRight;
  5268. + assert( pE->op!=TK_DOT || pRight!=0 );
  5269. + if( pE->op!=TK_ASTERISK
  5270. + && (pE->op!=TK_DOT || pRight->op!=TK_ASTERISK)
  5271. + ){
  5272. + /* This particular expression does not need to be expanded.
  5273. + */
  5274. + pNew = sqlite3ExprListAppend(pParse, pNew, a[k].pExpr);
  5275. + if( pNew ){
  5276. + pNew->a[pNew->nExpr-1].zEName = a[k].zEName;
  5277. + pNew->a[pNew->nExpr-1].eEName = a[k].eEName;
  5278. + a[k].zEName = 0;
  5279. + }
  5280. + a[k].pExpr = 0;
  5281. + }else{
  5282. + /* This expression is a "*" or a "TABLE.*" and needs to be
  5283. + ** expanded. */
  5284. + int tableSeen = 0; /* Set to 1 when TABLE matches */
  5285. + char *zTName = 0; /* text of name of TABLE */
  5286. + if( pE->op==TK_DOT ){
  5287. + assert( pE->pLeft!=0 );
  5288. + assert( !ExprHasProperty(pE->pLeft, EP_IntValue) );
  5289. + zTName = pE->pLeft->u.zToken;
  5290. + }
  5291. + for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
  5292. + Table *pTab = pFrom->pTab;
  5293. + Select *pSub = pFrom->pSelect;
  5294. + char *zTabName = pFrom->zAlias;
  5295. + const char *zSchemaName = 0;
  5296. + int iDb;
  5297. + if( zTabName==0 ){
  5298. + zTabName = pTab->zName;
  5299. + }
  5300. + if( db->mallocFailed ) break;
  5301. + if( pSub==0 || (pSub->selFlags & SF_NestedFrom)==0 ){
  5302. + pSub = 0;
  5303. + if( zTName && sqlite3StrICmp(zTName, zTabName)!=0 ){
  5304. + continue;
  5305. + }
  5306. + iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
  5307. + zSchemaName = iDb>=0 ? db->aDb[iDb].zDbSName : "*";
  5308. + }
  5309. + for(j=0; j<pTab->nCol; j++){
  5310. + char *zName = pTab->aCol[j].zName;
  5311. + char *zColname; /* The computed column name */
  5312. + char *zToFree; /* Malloced string that needs to be freed */
  5313. + Token sColname; /* Computed column name as a token */
  5314. +
  5315. + assert( zName );
  5316. + if( zTName && pSub
  5317. + && sqlite3MatchEName(&pSub->pEList->a[j], 0, zTName, 0)==0
  5318. + ){
  5319. + continue;
  5320. + }
  5321. +
  5322. + /* If a column is marked as 'hidden', omit it from the expanded
  5323. + ** result-set list unless the SELECT has the SF_IncludeHidden
  5324. + ** bit set.
  5325. + */
  5326. + if( (p->selFlags & SF_IncludeHidden)==0
  5327. + && IsHiddenColumn(&pTab->aCol[j])
  5328. + ){
  5329. + continue;
  5330. + }
  5331. + tableSeen = 1;
  5332. +
  5333. + if( i>0 && zTName==0 ){
  5334. + if( (pFrom->fg.jointype & JT_NATURAL)!=0
  5335. + && tableAndColumnIndex(pTabList, i, zName, 0, 0, 1)
  5336. + ){
  5337. + /* In a NATURAL join, omit the join columns from the
  5338. + ** table to the right of the join */
  5339. + continue;
  5340. + }
  5341. + if( sqlite3IdListIndex(pFrom->pUsing, zName)>=0 ){
  5342. + /* In a join with a USING clause, omit columns in the
  5343. + ** using clause from the table on the right. */
  5344. + continue;
  5345. + }
  5346. + }
  5347. + pRight = sqlite3Expr(db, TK_ID, zName);
  5348. + zColname = zName;
  5349. + zToFree = 0;
  5350. + if( longNames || pTabList->nSrc>1 ){
  5351. + Expr *pLeft;
  5352. + pLeft = sqlite3Expr(db, TK_ID, zTabName);
  5353. + pExpr = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight);
  5354. + if( zSchemaName ){
  5355. + pLeft = sqlite3Expr(db, TK_ID, zSchemaName);
  5356. + pExpr = sqlite3PExpr(pParse, TK_DOT, pLeft, pExpr);
  5357. + }
  5358. + if( longNames ){
  5359. + zColname = sqlite3MPrintf(db, "%s.%s", zTabName, zName);
  5360. + zToFree = zColname;
  5361. + }
  5362. + }else{
  5363. + pExpr = pRight;
  5364. + }
  5365. + pNew = sqlite3ExprListAppend(pParse, pNew, pExpr);
  5366. + sqlite3TokenInit(&sColname, zColname);
  5367. + sqlite3ExprListSetName(pParse, pNew, &sColname, 0);
  5368. + if( pNew && (p->selFlags & SF_NestedFrom)!=0 && !IN_RENAME_OBJECT ){
  5369. + struct ExprList_item *pX = &pNew->a[pNew->nExpr-1];
  5370. + sqlite3DbFree(db, pX->zEName);
  5371. + if( pSub ){
  5372. + pX->zEName = sqlite3DbStrDup(db, pSub->pEList->a[j].zEName);
  5373. + testcase( pX->zEName==0 );
  5374. + }else{
  5375. + pX->zEName = sqlite3MPrintf(db, "%s.%s.%s",
  5376. + zSchemaName, zTabName, zColname);
  5377. + testcase( pX->zEName==0 );
  5378. + }
  5379. + pX->eEName = ENAME_TAB;
  5380. + }
  5381. + sqlite3DbFree(db, zToFree);
  5382. + }
  5383. + }
  5384. + if( !tableSeen ){
  5385. + if( zTName ){
  5386. + sqlite3ErrorMsg(pParse, "no such table: %s", zTName);
  5387. + }else{
  5388. + sqlite3ErrorMsg(pParse, "no tables specified");
  5389. + }
  5390. + }
  5391. + }
  5392. + }
  5393. + sqlite3ExprListDelete(db, pEList);
  5394. + p->pEList = pNew;
  5395. + }
  5396. + if( p->pEList ){
  5397. + if( p->pEList->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
  5398. + sqlite3ErrorMsg(pParse, "too many columns in result set");
  5399. + return WRC_Abort;
  5400. + }
  5401. + if( (elistFlags & (EP_HasFunc|EP_Subquery))!=0 ){
  5402. + p->selFlags |= SF_ComplexResult;
  5403. + }
  5404. + }
  5405. + return WRC_Continue;
  5406. +}
  5407. +
  5408. +#if SQLITE_DEBUG
  5409. +/*
  5410. +** Always assert. This xSelectCallback2 implementation proves that the
  5411. +** xSelectCallback2 is never invoked.
  5412. +*/
  5413. +void sqlite3SelectWalkAssert2(Walker *NotUsed, Select *NotUsed2){
  5414. + UNUSED_PARAMETER2(NotUsed, NotUsed2);
  5415. + assert( 0 );
  5416. +}
  5417. +#endif
  5418. +/*
  5419. +** This routine "expands" a SELECT statement and all of its subqueries.
  5420. +** For additional information on what it means to "expand" a SELECT
  5421. +** statement, see the comment on the selectExpand worker callback above.
  5422. +**
  5423. +** Expanding a SELECT statement is the first step in processing a
  5424. +** SELECT statement. The SELECT statement must be expanded before
  5425. +** name resolution is performed.
  5426. +**
  5427. +** If anything goes wrong, an error message is written into pParse.
  5428. +** The calling function can detect the problem by looking at pParse->nErr
  5429. +** and/or pParse->db->mallocFailed.
  5430. +*/
  5431. +static void sqlite3SelectExpand(Parse *pParse, Select *pSelect){
  5432. + Walker w;
  5433. + w.xExprCallback = sqlite3ExprWalkNoop;
  5434. + w.pParse = pParse;
  5435. + if( OK_IF_ALWAYS_TRUE(pParse->hasCompound) ){
  5436. + w.xSelectCallback = convertCompoundSelectToSubquery;
  5437. + w.xSelectCallback2 = 0;
  5438. + sqlite3WalkSelect(&w, pSelect);
  5439. + }
  5440. + w.xSelectCallback = selectExpander;
  5441. + w.xSelectCallback2 = selectPopWith;
  5442. + w.eCode = 0;
  5443. + sqlite3WalkSelect(&w, pSelect);
  5444. +}
  5445. +
  5446. +
  5447. +#ifndef SQLITE_OMIT_SUBQUERY
  5448. +/*
  5449. +** This is a Walker.xSelectCallback callback for the sqlite3SelectTypeInfo()
  5450. +** interface.
  5451. +**
  5452. +** For each FROM-clause subquery, add Column.zType and Column.zColl
  5453. +** information to the Table structure that represents the result set
  5454. +** of that subquery.
  5455. +**
  5456. +** The Table structure that represents the result set was constructed
  5457. +** by selectExpander() but the type and collation information was omitted
  5458. +** at that point because identifiers had not yet been resolved. This
  5459. +** routine is called after identifier resolution.
  5460. +*/
  5461. +static void selectAddSubqueryTypeInfo(Walker *pWalker, Select *p){
  5462. + Parse *pParse;
  5463. + int i;
  5464. + SrcList *pTabList;
  5465. + struct SrcList_item *pFrom;
  5466. +
  5467. + assert( p->selFlags & SF_Resolved );
  5468. + if( p->selFlags & SF_HasTypeInfo ) return;
  5469. + p->selFlags |= SF_HasTypeInfo;
  5470. + pParse = pWalker->pParse;
  5471. + pTabList = p->pSrc;
  5472. + for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
  5473. + Table *pTab = pFrom->pTab;
  5474. + assert( pTab!=0 );
  5475. + if( (pTab->tabFlags & TF_Ephemeral)!=0 ){
  5476. + /* A sub-query in the FROM clause of a SELECT */
  5477. + Select *pSel = pFrom->pSelect;
  5478. + if( pSel ){
  5479. + while( pSel->pPrior ) pSel = pSel->pPrior;
  5480. + sqlite3SelectAddColumnTypeAndCollation(pParse, pTab, pSel,
  5481. + SQLITE_AFF_NONE);
  5482. + }
  5483. + }
  5484. + }
  5485. +}
  5486. +#endif
  5487. +
  5488. +
  5489. +/*
  5490. +** This routine adds datatype and collating sequence information to
  5491. +** the Table structures of all FROM-clause subqueries in a
  5492. +** SELECT statement.
  5493. +**
  5494. +** Use this routine after name resolution.
  5495. +*/
  5496. +static void sqlite3SelectAddTypeInfo(Parse *pParse, Select *pSelect){
  5497. +#ifndef SQLITE_OMIT_SUBQUERY
  5498. + Walker w;
  5499. + w.xSelectCallback = sqlite3SelectWalkNoop;
  5500. + w.xSelectCallback2 = selectAddSubqueryTypeInfo;
  5501. + w.xExprCallback = sqlite3ExprWalkNoop;
  5502. + w.pParse = pParse;
  5503. + sqlite3WalkSelect(&w, pSelect);
  5504. +#endif
  5505. +}
  5506. +
  5507. +
  5508. +/*
  5509. +** This routine sets up a SELECT statement for processing. The
  5510. +** following is accomplished:
  5511. +**
  5512. +** * VDBE Cursor numbers are assigned to all FROM-clause terms.
  5513. +** * Ephemeral Table objects are created for all FROM-clause subqueries.
  5514. +** * ON and USING clauses are shifted into WHERE statements
  5515. +** * Wildcards "*" and "TABLE.*" in result sets are expanded.
  5516. +** * Identifiers in expression are matched to tables.
  5517. +**
  5518. +** This routine acts recursively on all subqueries within the SELECT.
  5519. +*/
  5520. +void sqlite3SelectPrep(
  5521. + Parse *pParse, /* The parser context */
  5522. + Select *p, /* The SELECT statement being coded. */
  5523. + NameContext *pOuterNC /* Name context for container */
  5524. +){
  5525. + assert( p!=0 || pParse->db->mallocFailed );
  5526. + if( pParse->db->mallocFailed ) return;
  5527. + if( p->selFlags & SF_HasTypeInfo ) return;
  5528. + sqlite3SelectExpand(pParse, p);
  5529. + if( pParse->nErr || pParse->db->mallocFailed ) return;
  5530. + sqlite3ResolveSelectNames(pParse, p, pOuterNC);
  5531. + if( pParse->nErr || pParse->db->mallocFailed ) return;
  5532. + sqlite3SelectAddTypeInfo(pParse, p);
  5533. +}
  5534. +
  5535. +/*
  5536. +** Reset the aggregate accumulator.
  5537. +**
  5538. +** The aggregate accumulator is a set of memory cells that hold
  5539. +** intermediate results while calculating an aggregate. This
  5540. +** routine generates code that stores NULLs in all of those memory
  5541. +** cells.
  5542. +*/
  5543. +static void resetAccumulator(Parse *pParse, AggInfo *pAggInfo){
  5544. + Vdbe *v = pParse->pVdbe;
  5545. + int i;
  5546. + struct AggInfo_func *pFunc;
  5547. + int nReg = pAggInfo->nFunc + pAggInfo->nColumn;
  5548. + if( nReg==0 ) return;
  5549. + if( pParse->nErr ) return;
  5550. +#ifdef SQLITE_DEBUG
  5551. + /* Verify that all AggInfo registers are within the range specified by
  5552. + ** AggInfo.mnReg..AggInfo.mxReg */
  5553. + assert( nReg==pAggInfo->mxReg-pAggInfo->mnReg+1 );
  5554. + for(i=0; i<pAggInfo->nColumn; i++){
  5555. + assert( pAggInfo->aCol[i].iMem>=pAggInfo->mnReg
  5556. + && pAggInfo->aCol[i].iMem<=pAggInfo->mxReg );
  5557. + }
  5558. + for(i=0; i<pAggInfo->nFunc; i++){
  5559. + assert( pAggInfo->aFunc[i].iMem>=pAggInfo->mnReg
  5560. + && pAggInfo->aFunc[i].iMem<=pAggInfo->mxReg );
  5561. + }
  5562. +#endif
  5563. + sqlite3VdbeAddOp3(v, OP_Null, 0, pAggInfo->mnReg, pAggInfo->mxReg);
  5564. + for(pFunc=pAggInfo->aFunc, i=0; i<pAggInfo->nFunc; i++, pFunc++){
  5565. + if( pFunc->iDistinct>=0 ){
  5566. + Expr *pE = pFunc->pExpr;
  5567. + assert( !ExprHasProperty(pE, EP_xIsSelect) );
  5568. + if( pE->x.pList==0 || pE->x.pList->nExpr!=1 ){
  5569. + sqlite3ErrorMsg(pParse, "DISTINCT aggregates must have exactly one "
  5570. + "argument");
  5571. + pFunc->iDistinct = -1;
  5572. + }else{
  5573. + KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pE->x.pList,0,0);
  5574. + sqlite3VdbeAddOp4(v, OP_OpenEphemeral, pFunc->iDistinct, 0, 0,
  5575. + (char*)pKeyInfo, P4_KEYINFO);
  5576. + }
  5577. + }
  5578. + }
  5579. +}
  5580. +
  5581. +/*
  5582. +** Invoke the OP_AggFinalize opcode for every aggregate function
  5583. +** in the AggInfo structure.
  5584. +*/
  5585. +static void finalizeAggFunctions(Parse *pParse, AggInfo *pAggInfo){
  5586. + Vdbe *v = pParse->pVdbe;
  5587. + int i;
  5588. + struct AggInfo_func *pF;
  5589. + for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
  5590. + ExprList *pList = pF->pExpr->x.pList;
  5591. + assert( !ExprHasProperty(pF->pExpr, EP_xIsSelect) );
  5592. + sqlite3VdbeAddOp2(v, OP_AggFinal, pF->iMem, pList ? pList->nExpr : 0);
  5593. + sqlite3VdbeAppendP4(v, pF->pFunc, P4_FUNCDEF);
  5594. + }
  5595. +}
  5596. +
  5597. +
  5598. +/*
  5599. +** Update the accumulator memory cells for an aggregate based on
  5600. +** the current cursor position.
  5601. +**
  5602. +** If regAcc is non-zero and there are no min() or max() aggregates
  5603. +** in pAggInfo, then only populate the pAggInfo->nAccumulator accumulator
  5604. +** registers if register regAcc contains 0. The caller will take care
  5605. +** of setting and clearing regAcc.
  5606. +*/
  5607. +static void updateAccumulator(Parse *pParse, int regAcc, AggInfo *pAggInfo){
  5608. + Vdbe *v = pParse->pVdbe;
  5609. + int i;
  5610. + int regHit = 0;
  5611. + int addrHitTest = 0;
  5612. + struct AggInfo_func *pF;
  5613. + struct AggInfo_col *pC;
  5614. +
  5615. + pAggInfo->directMode = 1;
  5616. + for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
  5617. + int nArg;
  5618. + int addrNext = 0;
  5619. + int regAgg;
  5620. + ExprList *pList = pF->pExpr->x.pList;
  5621. + assert( !ExprHasProperty(pF->pExpr, EP_xIsSelect) );
  5622. + assert( !IsWindowFunc(pF->pExpr) );
  5623. + if( ExprHasProperty(pF->pExpr, EP_WinFunc) ){
  5624. + Expr *pFilter = pF->pExpr->y.pWin->pFilter;
  5625. + if( pAggInfo->nAccumulator
  5626. + && (pF->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL)
  5627. + ){
  5628. + if( regHit==0 ) regHit = ++pParse->nMem;
  5629. + /* If this is the first row of the group (regAcc==0), clear the
  5630. + ** "magnet" register regHit so that the accumulator registers
  5631. + ** are populated if the FILTER clause jumps over the the
  5632. + ** invocation of min() or max() altogether. Or, if this is not
  5633. + ** the first row (regAcc==1), set the magnet register so that the
  5634. + ** accumulators are not populated unless the min()/max() is invoked and
  5635. + ** indicates that they should be. */
  5636. + sqlite3VdbeAddOp2(v, OP_Copy, regAcc, regHit);
  5637. + }
  5638. + addrNext = sqlite3VdbeMakeLabel(pParse);
  5639. + sqlite3ExprIfFalse(pParse, pFilter, addrNext, SQLITE_JUMPIFNULL);
  5640. + }
  5641. + if( pList ){
  5642. + nArg = pList->nExpr;
  5643. + regAgg = sqlite3GetTempRange(pParse, nArg);
  5644. + sqlite3ExprCodeExprList(pParse, pList, regAgg, 0, SQLITE_ECEL_DUP);
  5645. + }else{
  5646. + nArg = 0;
  5647. + regAgg = 0;
  5648. + }
  5649. + if( pF->iDistinct>=0 ){
  5650. + if( addrNext==0 ){
  5651. + addrNext = sqlite3VdbeMakeLabel(pParse);
  5652. + }
  5653. + testcase( nArg==0 ); /* Error condition */
  5654. + testcase( nArg>1 ); /* Also an error */
  5655. + codeDistinct(pParse, pF->iDistinct, addrNext, 1, regAgg);
  5656. + }
  5657. + if( pF->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
  5658. + CollSeq *pColl = 0;
  5659. + struct ExprList_item *pItem;
  5660. + int j;
  5661. + assert( pList!=0 ); /* pList!=0 if pF->pFunc has NEEDCOLL */
  5662. + for(j=0, pItem=pList->a; !pColl && j<nArg; j++, pItem++){
  5663. + pColl = sqlite3ExprCollSeq(pParse, pItem->pExpr);
  5664. + }
  5665. + if( !pColl ){
  5666. + pColl = pParse->db->pDfltColl;
  5667. + }
  5668. + if( regHit==0 && pAggInfo->nAccumulator ) regHit = ++pParse->nMem;
  5669. + sqlite3VdbeAddOp4(v, OP_CollSeq, regHit, 0, 0, (char *)pColl, P4_COLLSEQ);
  5670. + }
  5671. + sqlite3VdbeAddOp3(v, OP_AggStep, 0, regAgg, pF->iMem);
  5672. + sqlite3VdbeAppendP4(v, pF->pFunc, P4_FUNCDEF);
  5673. + sqlite3VdbeChangeP5(v, (u8)nArg);
  5674. + sqlite3ReleaseTempRange(pParse, regAgg, nArg);
  5675. + if( addrNext ){
  5676. + sqlite3VdbeResolveLabel(v, addrNext);
  5677. + }
  5678. + }
  5679. + if( regHit==0 && pAggInfo->nAccumulator ){
  5680. + regHit = regAcc;
  5681. + }
  5682. + if( regHit ){
  5683. + addrHitTest = sqlite3VdbeAddOp1(v, OP_If, regHit); VdbeCoverage(v);
  5684. + }
  5685. + for(i=0, pC=pAggInfo->aCol; i<pAggInfo->nAccumulator; i++, pC++){
  5686. + sqlite3ExprCode(pParse, pC->pExpr, pC->iMem);
  5687. + }
  5688. +
  5689. + pAggInfo->directMode = 0;
  5690. + if( addrHitTest ){
  5691. + sqlite3VdbeJumpHereOrPopInst(v, addrHitTest);
  5692. + }
  5693. +}
  5694. +
  5695. +/*
  5696. +** Add a single OP_Explain instruction to the VDBE to explain a simple
  5697. +** count(*) query ("SELECT count(*) FROM pTab").
  5698. +*/
  5699. +#ifndef SQLITE_OMIT_EXPLAIN
  5700. +static void explainSimpleCount(
  5701. + Parse *pParse, /* Parse context */
  5702. + Table *pTab, /* Table being queried */
  5703. + Index *pIdx /* Index used to optimize scan, or NULL */
  5704. +){
  5705. + if( pParse->explain==2 ){
  5706. + int bCover = (pIdx!=0 && (HasRowid(pTab) || !IsPrimaryKeyIndex(pIdx)));
  5707. + sqlite3VdbeExplain(pParse, 0, "SCAN TABLE %s%s%s",
  5708. + pTab->zName,
  5709. + bCover ? " USING COVERING INDEX " : "",
  5710. + bCover ? pIdx->zName : ""
  5711. + );
  5712. + }
  5713. +}
  5714. +#else
  5715. +# define explainSimpleCount(a,b,c)
  5716. +#endif
  5717. +
  5718. +/*
  5719. +** sqlite3WalkExpr() callback used by havingToWhere().
  5720. +**
  5721. +** If the node passed to the callback is a TK_AND node, return
  5722. +** WRC_Continue to tell sqlite3WalkExpr() to iterate through child nodes.
  5723. +**
  5724. +** Otherwise, return WRC_Prune. In this case, also check if the
  5725. +** sub-expression matches the criteria for being moved to the WHERE
  5726. +** clause. If so, add it to the WHERE clause and replace the sub-expression
  5727. +** within the HAVING expression with a constant "1".
  5728. +*/
  5729. +static int havingToWhereExprCb(Walker *pWalker, Expr *pExpr){
  5730. + if( pExpr->op!=TK_AND ){
  5731. + Select *pS = pWalker->u.pSelect;
  5732. + if( sqlite3ExprIsConstantOrGroupBy(pWalker->pParse, pExpr, pS->pGroupBy) ){
  5733. + sqlite3 *db = pWalker->pParse->db;
  5734. + Expr *pNew = sqlite3Expr(db, TK_INTEGER, "1");
  5735. + if( pNew ){
  5736. + Expr *pWhere = pS->pWhere;
  5737. + SWAP(Expr, *pNew, *pExpr);
  5738. + pNew = sqlite3ExprAnd(pWalker->pParse, pWhere, pNew);
  5739. + pS->pWhere = pNew;
  5740. + pWalker->eCode = 1;
  5741. + }
  5742. + }
  5743. + return WRC_Prune;
  5744. + }
  5745. + return WRC_Continue;
  5746. +}
  5747. +
  5748. +/*
  5749. +** Transfer eligible terms from the HAVING clause of a query, which is
  5750. +** processed after grouping, to the WHERE clause, which is processed before
  5751. +** grouping. For example, the query:
  5752. +**
  5753. +** SELECT * FROM <tables> WHERE a=? GROUP BY b HAVING b=? AND c=?
  5754. +**
  5755. +** can be rewritten as:
  5756. +**
  5757. +** SELECT * FROM <tables> WHERE a=? AND b=? GROUP BY b HAVING c=?
  5758. +**
  5759. +** A term of the HAVING expression is eligible for transfer if it consists
  5760. +** entirely of constants and expressions that are also GROUP BY terms that
  5761. +** use the "BINARY" collation sequence.
  5762. +*/
  5763. +static void havingToWhere(Parse *pParse, Select *p){
  5764. + Walker sWalker;
  5765. + memset(&sWalker, 0, sizeof(sWalker));
  5766. + sWalker.pParse = pParse;
  5767. + sWalker.xExprCallback = havingToWhereExprCb;
  5768. + sWalker.u.pSelect = p;
  5769. + sqlite3WalkExpr(&sWalker, p->pHaving);
  5770. +#if SELECTTRACE_ENABLED
  5771. + if( sWalker.eCode && (sqlite3SelectTrace & 0x100)!=0 ){
  5772. + SELECTTRACE(0x100,pParse,p,("Move HAVING terms into WHERE:\n"));
  5773. + sqlite3TreeViewSelect(0, p, 0);
  5774. + }
  5775. +#endif
  5776. +}
  5777. +
  5778. +/*
  5779. +** Check to see if the pThis entry of pTabList is a self-join of a prior view.
  5780. +** If it is, then return the SrcList_item for the prior view. If it is not,
  5781. +** then return 0.
  5782. +*/
  5783. +static struct SrcList_item *isSelfJoinView(
  5784. + SrcList *pTabList, /* Search for self-joins in this FROM clause */
  5785. + struct SrcList_item *pThis /* Search for prior reference to this subquery */
  5786. +){
  5787. + struct SrcList_item *pItem;
  5788. + for(pItem = pTabList->a; pItem<pThis; pItem++){
  5789. + Select *pS1;
  5790. + if( pItem->pSelect==0 ) continue;
  5791. + if( pItem->fg.viaCoroutine ) continue;
  5792. + if( pItem->zName==0 ) continue;
  5793. + assert( pItem->pTab!=0 );
  5794. + assert( pThis->pTab!=0 );
  5795. + if( pItem->pTab->pSchema!=pThis->pTab->pSchema ) continue;
  5796. + if( sqlite3_stricmp(pItem->zName, pThis->zName)!=0 ) continue;
  5797. + pS1 = pItem->pSelect;
  5798. + if( pItem->pTab->pSchema==0 && pThis->pSelect->selId!=pS1->selId ){
  5799. + /* The query flattener left two different CTE tables with identical
  5800. + ** names in the same FROM clause. */
  5801. + continue;
  5802. + }
  5803. + if( sqlite3ExprCompare(0, pThis->pSelect->pWhere, pS1->pWhere, -1)
  5804. + || sqlite3ExprCompare(0, pThis->pSelect->pHaving, pS1->pHaving, -1)
  5805. + ){
  5806. + /* The view was modified by some other optimization such as
  5807. + ** pushDownWhereTerms() */
  5808. + continue;
  5809. + }
  5810. + return pItem;
  5811. + }
  5812. + return 0;
  5813. +}
  5814. +
  5815. +#ifdef SQLITE_COUNTOFVIEW_OPTIMIZATION
  5816. +/*
  5817. +** Attempt to transform a query of the form
  5818. +**
  5819. +** SELECT count(*) FROM (SELECT x FROM t1 UNION ALL SELECT y FROM t2)
  5820. +**
  5821. +** Into this:
  5822. +**
  5823. +** SELECT (SELECT count(*) FROM t1)+(SELECT count(*) FROM t2)
  5824. +**
  5825. +** The transformation only works if all of the following are true:
  5826. +**
  5827. +** * The subquery is a UNION ALL of two or more terms
  5828. +** * The subquery does not have a LIMIT clause
  5829. +** * There is no WHERE or GROUP BY or HAVING clauses on the subqueries
  5830. +** * The outer query is a simple count(*) with no WHERE clause or other
  5831. +** extraneous syntax.
  5832. +**
  5833. +** Return TRUE if the optimization is undertaken.
  5834. +*/
  5835. +static int countOfViewOptimization(Parse *pParse, Select *p){
  5836. + Select *pSub, *pPrior;
  5837. + Expr *pExpr;
  5838. + Expr *pCount;
  5839. + sqlite3 *db;
  5840. + if( (p->selFlags & SF_Aggregate)==0 ) return 0; /* This is an aggregate */
  5841. + if( p->pEList->nExpr!=1 ) return 0; /* Single result column */
  5842. + if( p->pWhere ) return 0;
  5843. + if( p->pGroupBy ) return 0;
  5844. + pExpr = p->pEList->a[0].pExpr;
  5845. + if( pExpr->op!=TK_AGG_FUNCTION ) return 0; /* Result is an aggregate */
  5846. + if( sqlite3_stricmp(pExpr->u.zToken,"count") ) return 0; /* Is count() */
  5847. + if( pExpr->x.pList!=0 ) return 0; /* Must be count(*) */
  5848. + if( p->pSrc->nSrc!=1 ) return 0; /* One table in FROM */
  5849. + pSub = p->pSrc->a[0].pSelect;
  5850. + if( pSub==0 ) return 0; /* The FROM is a subquery */
  5851. + if( pSub->pPrior==0 ) return 0; /* Must be a compound ry */
  5852. + do{
  5853. + if( pSub->op!=TK_ALL && pSub->pPrior ) return 0; /* Must be UNION ALL */
  5854. + if( pSub->pWhere ) return 0; /* No WHERE clause */
  5855. + if( pSub->pLimit ) return 0; /* No LIMIT clause */
  5856. + if( pSub->selFlags & SF_Aggregate ) return 0; /* Not an aggregate */
  5857. + pSub = pSub->pPrior; /* Repeat over compound */
  5858. + }while( pSub );
  5859. +
  5860. + /* If we reach this point then it is OK to perform the transformation */
  5861. +
  5862. + db = pParse->db;
  5863. + pCount = pExpr;
  5864. + pExpr = 0;
  5865. + pSub = p->pSrc->a[0].pSelect;
  5866. + p->pSrc->a[0].pSelect = 0;
  5867. + sqlite3SrcListDelete(db, p->pSrc);
  5868. + p->pSrc = sqlite3DbMallocZero(pParse->db, sizeof(*p->pSrc));
  5869. + while( pSub ){
  5870. + Expr *pTerm;
  5871. + pPrior = pSub->pPrior;
  5872. + pSub->pPrior = 0;
  5873. + pSub->pNext = 0;
  5874. + pSub->selFlags |= SF_Aggregate;
  5875. + pSub->selFlags &= ~SF_Compound;
  5876. + pSub->nSelectRow = 0;
  5877. + sqlite3ExprListDelete(db, pSub->pEList);
  5878. + pTerm = pPrior ? sqlite3ExprDup(db, pCount, 0) : pCount;
  5879. + pSub->pEList = sqlite3ExprListAppend(pParse, 0, pTerm);
  5880. + pTerm = sqlite3PExpr(pParse, TK_SELECT, 0, 0);
  5881. + sqlite3PExprAddSelect(pParse, pTerm, pSub);
  5882. + if( pExpr==0 ){
  5883. + pExpr = pTerm;
  5884. + }else{
  5885. + pExpr = sqlite3PExpr(pParse, TK_PLUS, pTerm, pExpr);
  5886. + }
  5887. + pSub = pPrior;
  5888. + }
  5889. + p->pEList->a[0].pExpr = pExpr;
  5890. + p->selFlags &= ~SF_Aggregate;
  5891. +
  5892. +#if SELECTTRACE_ENABLED
  5893. + if( sqlite3SelectTrace & 0x400 ){
  5894. + SELECTTRACE(0x400,pParse,p,("After count-of-view optimization:\n"));
  5895. + sqlite3TreeViewSelect(0, p, 0);
  5896. + }
  5897. +#endif
  5898. + return 1;
  5899. +}
  5900. +#endif /* SQLITE_COUNTOFVIEW_OPTIMIZATION */
  5901. +
  5902. +/*
  5903. +** Generate code for the SELECT statement given in the p argument.
  5904. +**
  5905. +** The results are returned according to the SelectDest structure.
  5906. +** See comments in sqliteInt.h for further information.
  5907. +**
  5908. +** This routine returns the number of errors. If any errors are
  5909. +** encountered, then an appropriate error message is left in
  5910. +** pParse->zErrMsg.
  5911. +**
  5912. +** This routine does NOT free the Select structure passed in. The
  5913. +** calling function needs to do that.
  5914. +*/
  5915. +int sqlite3Select(
  5916. + Parse *pParse, /* The parser context */
  5917. + Select *p, /* The SELECT statement being coded. */
  5918. + SelectDest *pDest /* What to do with the query results */
  5919. +){
  5920. + int i, j; /* Loop counters */
  5921. + WhereInfo *pWInfo; /* Return from sqlite3WhereBegin() */
  5922. + Vdbe *v; /* The virtual machine under construction */
  5923. + int isAgg; /* True for select lists like "count(*)" */
  5924. + ExprList *pEList = 0; /* List of columns to extract. */
  5925. + SrcList *pTabList; /* List of tables to select from */
  5926. + Expr *pWhere; /* The WHERE clause. May be NULL */
  5927. + ExprList *pGroupBy; /* The GROUP BY clause. May be NULL */
  5928. + Expr *pHaving; /* The HAVING clause. May be NULL */
  5929. + int rc = 1; /* Value to return from this function */
  5930. + DistinctCtx sDistinct; /* Info on how to code the DISTINCT keyword */
  5931. + SortCtx sSort; /* Info on how to code the ORDER BY clause */
  5932. + AggInfo sAggInfo; /* Information used by aggregate queries */
  5933. + int iEnd; /* Address of the end of the query */
  5934. + sqlite3 *db; /* The database connection */
  5935. + ExprList *pMinMaxOrderBy = 0; /* Added ORDER BY for min/max queries */
  5936. + u8 minMaxFlag; /* Flag for min/max queries */
  5937. +
  5938. + db = pParse->db;
  5939. + v = sqlite3GetVdbe(pParse);
  5940. + if( p==0 || db->mallocFailed || pParse->nErr ){
  5941. + return 1;
  5942. + }
  5943. + if( sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0) ) return 1;
  5944. + memset(&sAggInfo, 0, sizeof(sAggInfo));
  5945. +#ifdef SQLITE_DEBUG
  5946. + sAggInfo.iAggMagic = SQLITE_AGGMAGIC_VALID;
  5947. +#endif
  5948. +#if SELECTTRACE_ENABLED
  5949. + SELECTTRACE(1,pParse,p, ("begin processing:\n", pParse->addrExplain));
  5950. + if( sqlite3SelectTrace & 0x100 ){
  5951. + sqlite3TreeViewSelect(0, p, 0);
  5952. + }
  5953. +#endif
  5954. +
  5955. + assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistFifo );
  5956. + assert( p->pOrderBy==0 || pDest->eDest!=SRT_Fifo );
  5957. + assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistQueue );
  5958. + assert( p->pOrderBy==0 || pDest->eDest!=SRT_Queue );
  5959. + if( IgnorableOrderby(pDest) ){
  5960. + assert(pDest->eDest==SRT_Exists || pDest->eDest==SRT_Union ||
  5961. + pDest->eDest==SRT_Except || pDest->eDest==SRT_Discard ||
  5962. + pDest->eDest==SRT_Queue || pDest->eDest==SRT_DistFifo ||
  5963. + pDest->eDest==SRT_DistQueue || pDest->eDest==SRT_Fifo);
  5964. + /* If ORDER BY makes no difference in the output then neither does
  5965. + ** DISTINCT so it can be removed too. */
  5966. + sqlite3ExprListDelete(db, p->pOrderBy);
  5967. + p->pOrderBy = 0;
  5968. + p->selFlags &= ~SF_Distinct;
  5969. + }
  5970. + sqlite3SelectPrep(pParse, p, 0);
  5971. + if( pParse->nErr || db->mallocFailed ){
  5972. + goto select_end;
  5973. + }
  5974. + assert( p->pEList!=0 );
  5975. +#if SELECTTRACE_ENABLED
  5976. + if( sqlite3SelectTrace & 0x104 ){
  5977. + SELECTTRACE(0x104,pParse,p, ("after name resolution:\n"));
  5978. + sqlite3TreeViewSelect(0, p, 0);
  5979. + }
  5980. +#endif
  5981. +
  5982. + if( pDest->eDest==SRT_Output ){
  5983. + generateColumnNames(pParse, p);
  5984. + }
  5985. +
  5986. + pTabList = p->pSrc;
  5987. + isAgg = (p->selFlags & SF_Aggregate)!=0;
  5988. + memset(&sSort, 0, sizeof(sSort));
  5989. + sSort.pOrderBy = p->pOrderBy;
  5990. +
  5991. + /* Try to various optimizations (flattening subqueries, and strength
  5992. + ** reduction of join operators) in the FROM clause up into the main query
  5993. + */
  5994. +#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
  5995. + for(i=0; !p->pPrior && i<pTabList->nSrc; i++){
  5996. + struct SrcList_item *pItem = &pTabList->a[i];
  5997. + Select *pSub = pItem->pSelect;
  5998. + Table *pTab = pItem->pTab;
  5999. +
  6000. + /* Convert LEFT JOIN into JOIN if there are terms of the right table
  6001. + ** of the LEFT JOIN used in the WHERE clause.
  6002. + */
  6003. + if( (pItem->fg.jointype & JT_LEFT)!=0
  6004. + && sqlite3ExprImpliesNonNullRow(p->pWhere, pItem->iCursor)
  6005. + && OptimizationEnabled(db, SQLITE_SimplifyJoin)
  6006. + ){
  6007. + SELECTTRACE(0x100,pParse,p,
  6008. + ("LEFT-JOIN simplifies to JOIN on term %d\n",i));
  6009. + pItem->fg.jointype &= ~(JT_LEFT|JT_OUTER);
  6010. + unsetJoinExpr(p->pWhere, pItem->iCursor);
  6011. + }
  6012. +
  6013. + /* No futher action if this term of the FROM clause is no a subquery */
  6014. + if( pSub==0 ) continue;
  6015. +
  6016. + /* Catch mismatch in the declared columns of a view and the number of
  6017. + ** columns in the SELECT on the RHS */
  6018. + if( pTab->nCol!=pSub->pEList->nExpr ){
  6019. + sqlite3ErrorMsg(pParse, "expected %d columns for '%s' but got %d",
  6020. + pTab->nCol, pTab->zName, pSub->pEList->nExpr);
  6021. + goto select_end;
  6022. + }
  6023. +
  6024. + /* Do not try to flatten an aggregate subquery.
  6025. + **
  6026. + ** Flattening an aggregate subquery is only possible if the outer query
  6027. + ** is not a join. But if the outer query is not a join, then the subquery
  6028. + ** will be implemented as a co-routine and there is no advantage to
  6029. + ** flattening in that case.
  6030. + */
  6031. + if( (pSub->selFlags & SF_Aggregate)!=0 ) continue;
  6032. + assert( pSub->pGroupBy==0 );
  6033. +
  6034. + /* If the outer query contains a "complex" result set (that is,
  6035. + ** if the result set of the outer query uses functions or subqueries)
  6036. + ** and if the subquery contains an ORDER BY clause and if
  6037. + ** it will be implemented as a co-routine, then do not flatten. This
  6038. + ** restriction allows SQL constructs like this:
  6039. + **
  6040. + ** SELECT expensive_function(x)
  6041. + ** FROM (SELECT x FROM tab ORDER BY y LIMIT 10);
  6042. + **
  6043. + ** The expensive_function() is only computed on the 10 rows that
  6044. + ** are output, rather than every row of the table.
  6045. + **
  6046. + ** The requirement that the outer query have a complex result set
  6047. + ** means that flattening does occur on simpler SQL constraints without
  6048. + ** the expensive_function() like:
  6049. + **
  6050. + ** SELECT x FROM (SELECT x FROM tab ORDER BY y LIMIT 10);
  6051. + */
  6052. + if( pSub->pOrderBy!=0
  6053. + && i==0
  6054. + && (p->selFlags & SF_ComplexResult)!=0
  6055. + && (pTabList->nSrc==1
  6056. + || (pTabList->a[1].fg.jointype&(JT_LEFT|JT_CROSS))!=0)
  6057. + ){
  6058. + continue;
  6059. + }
  6060. +
  6061. + if( flattenSubquery(pParse, p, i, isAgg) ){
  6062. + if( pParse->nErr ) goto select_end;
  6063. + /* This subquery can be absorbed into its parent. */
  6064. + i = -1;
  6065. + }
  6066. + pTabList = p->pSrc;
  6067. + if( db->mallocFailed ) goto select_end;
  6068. + if( !IgnorableOrderby(pDest) ){
  6069. + sSort.pOrderBy = p->pOrderBy;
  6070. + }
  6071. + }
  6072. +#endif
  6073. +
  6074. +#ifndef SQLITE_OMIT_COMPOUND_SELECT
  6075. + /* Handle compound SELECT statements using the separate multiSelect()
  6076. + ** procedure.
  6077. + */
  6078. + if( p->pPrior ){
  6079. + rc = multiSelect(pParse, p, pDest);
  6080. +#if SELECTTRACE_ENABLED
  6081. + SELECTTRACE(0x1,pParse,p,("end compound-select processing\n"));
  6082. + if( (sqlite3SelectTrace & 0x2000)!=0 && ExplainQueryPlanParent(pParse)==0 ){
  6083. + sqlite3TreeViewSelect(0, p, 0);
  6084. + }
  6085. +#endif
  6086. + if( p->pNext==0 ) ExplainQueryPlanPop(pParse);
  6087. + return rc;
  6088. + }
  6089. +#endif
  6090. +
  6091. + /* Do the WHERE-clause constant propagation optimization if this is
  6092. + ** a join. No need to speed time on this operation for non-join queries
  6093. + ** as the equivalent optimization will be handled by query planner in
  6094. + ** sqlite3WhereBegin().
  6095. + */
  6096. + if( pTabList->nSrc>1
  6097. + && OptimizationEnabled(db, SQLITE_PropagateConst)
  6098. + && propagateConstants(pParse, p)
  6099. + ){
  6100. +#if SELECTTRACE_ENABLED
  6101. + if( sqlite3SelectTrace & 0x100 ){
  6102. + SELECTTRACE(0x100,pParse,p,("After constant propagation:\n"));
  6103. + sqlite3TreeViewSelect(0, p, 0);
  6104. + }
  6105. +#endif
  6106. + }else{
  6107. + SELECTTRACE(0x100,pParse,p,("Constant propagation not helpful\n"));
  6108. + }
  6109. +
  6110. +#ifdef SQLITE_COUNTOFVIEW_OPTIMIZATION
  6111. + if( OptimizationEnabled(db, SQLITE_QueryFlattener|SQLITE_CountOfView)
  6112. + && countOfViewOptimization(pParse, p)
  6113. + ){
  6114. + if( db->mallocFailed ) goto select_end;
  6115. + pEList = p->pEList;
  6116. + pTabList = p->pSrc;
  6117. + }
  6118. +#endif
  6119. +
  6120. + /* For each term in the FROM clause, do two things:
  6121. + ** (1) Authorized unreferenced tables
  6122. + ** (2) Generate code for all sub-queries
  6123. + */
  6124. + for(i=0; i<pTabList->nSrc; i++){
  6125. + struct SrcList_item *pItem = &pTabList->a[i];
  6126. + SelectDest dest;
  6127. + Select *pSub;
  6128. +#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
  6129. + const char *zSavedAuthContext;
  6130. +#endif
  6131. +
  6132. + /* Issue SQLITE_READ authorizations with a fake column name for any
  6133. + ** tables that are referenced but from which no values are extracted.
  6134. + ** Examples of where these kinds of null SQLITE_READ authorizations
  6135. + ** would occur:
  6136. + **
  6137. + ** SELECT count(*) FROM t1; -- SQLITE_READ t1.""
  6138. + ** SELECT t1.* FROM t1, t2; -- SQLITE_READ t2.""
  6139. + **
  6140. + ** The fake column name is an empty string. It is possible for a table to
  6141. + ** have a column named by the empty string, in which case there is no way to
  6142. + ** distinguish between an unreferenced table and an actual reference to the
  6143. + ** "" column. The original design was for the fake column name to be a NULL,
  6144. + ** which would be unambiguous. But legacy authorization callbacks might
  6145. + ** assume the column name is non-NULL and segfault. The use of an empty
  6146. + ** string for the fake column name seems safer.
  6147. + */
  6148. + if( pItem->colUsed==0 && pItem->zName!=0 ){
  6149. + sqlite3AuthCheck(pParse, SQLITE_READ, pItem->zName, "", pItem->zDatabase);
  6150. + }
  6151. +
  6152. +#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
  6153. + /* Generate code for all sub-queries in the FROM clause
  6154. + */
  6155. + pSub = pItem->pSelect;
  6156. + if( pSub==0 ) continue;
  6157. +
  6158. + /* The code for a subquery should only be generated once, though it is
  6159. + ** technically harmless for it to be generated multiple times. The
  6160. + ** following assert() will detect if something changes to cause
  6161. + ** the same subquery to be coded multiple times, as a signal to the
  6162. + ** developers to try to optimize the situation.
  6163. + **
  6164. + ** Update 2019-07-24:
  6165. + ** See ticket https://sqlite.org/src/tktview/c52b09c7f38903b1311cec40.
  6166. + ** The dbsqlfuzz fuzzer found a case where the same subquery gets
  6167. + ** coded twice. So this assert() now becomes a testcase(). It should
  6168. + ** be very rare, though.
  6169. + */
  6170. + testcase( pItem->addrFillSub!=0 );
  6171. +
  6172. + /* Increment Parse.nHeight by the height of the largest expression
  6173. + ** tree referred to by this, the parent select. The child select
  6174. + ** may contain expression trees of at most
  6175. + ** (SQLITE_MAX_EXPR_DEPTH-Parse.nHeight) height. This is a bit
  6176. + ** more conservative than necessary, but much easier than enforcing
  6177. + ** an exact limit.
  6178. + */
  6179. + pParse->nHeight += sqlite3SelectExprHeight(p);
  6180. +
  6181. + /* Make copies of constant WHERE-clause terms in the outer query down
  6182. + ** inside the subquery. This can help the subquery to run more efficiently.
  6183. + */
  6184. + if( OptimizationEnabled(db, SQLITE_PushDown)
  6185. + && pushDownWhereTerms(pParse, pSub, p->pWhere, pItem->iCursor,
  6186. + (pItem->fg.jointype & JT_OUTER)!=0)
  6187. + ){
  6188. +#if SELECTTRACE_ENABLED
  6189. + if( sqlite3SelectTrace & 0x100 ){
  6190. + SELECTTRACE(0x100,pParse,p,
  6191. + ("After WHERE-clause push-down into subquery %d:\n", pSub->selId));
  6192. + sqlite3TreeViewSelect(0, p, 0);
  6193. + }
  6194. +#endif
  6195. + }else{
  6196. + SELECTTRACE(0x100,pParse,p,("Push-down not possible\n"));
  6197. + }
  6198. +
  6199. + zSavedAuthContext = pParse->zAuthContext;
  6200. + pParse->zAuthContext = pItem->zName;
  6201. +
  6202. + /* Generate code to implement the subquery
  6203. + **
  6204. + ** The subquery is implemented as a co-routine if the subquery is
  6205. + ** guaranteed to be the outer loop (so that it does not need to be
  6206. + ** computed more than once)
  6207. + **
  6208. + ** TODO: Are there other reasons beside (1) to use a co-routine
  6209. + ** implementation?
  6210. + */
  6211. + if( i==0
  6212. + && (pTabList->nSrc==1
  6213. + || (pTabList->a[1].fg.jointype&(JT_LEFT|JT_CROSS))!=0) /* (1) */
  6214. + ){
  6215. + /* Implement a co-routine that will return a single row of the result
  6216. + ** set on each invocation.
  6217. + */
  6218. + int addrTop = sqlite3VdbeCurrentAddr(v)+1;
  6219. +
  6220. + pItem->regReturn = ++pParse->nMem;
  6221. + sqlite3VdbeAddOp3(v, OP_InitCoroutine, pItem->regReturn, 0, addrTop);
  6222. + VdbeComment((v, "%s", pItem->pTab->zName));
  6223. + pItem->addrFillSub = addrTop;
  6224. + sqlite3SelectDestInit(&dest, SRT_Coroutine, pItem->regReturn);
  6225. + ExplainQueryPlan((pParse, 1, "CO-ROUTINE %u", pSub->selId));
  6226. + sqlite3Select(pParse, pSub, &dest);
  6227. + pItem->pTab->nRowLogEst = pSub->nSelectRow;
  6228. + pItem->fg.viaCoroutine = 1;
  6229. + pItem->regResult = dest.iSdst;
  6230. + sqlite3VdbeEndCoroutine(v, pItem->regReturn);
  6231. + sqlite3VdbeJumpHere(v, addrTop-1);
  6232. + sqlite3ClearTempRegCache(pParse);
  6233. + }else{
  6234. + /* Generate a subroutine that will fill an ephemeral table with
  6235. + ** the content of this subquery. pItem->addrFillSub will point
  6236. + ** to the address of the generated subroutine. pItem->regReturn
  6237. + ** is a register allocated to hold the subroutine return address
  6238. + */
  6239. + int topAddr;
  6240. + int onceAddr = 0;
  6241. + int retAddr;
  6242. + struct SrcList_item *pPrior;
  6243. +
  6244. + testcase( pItem->addrFillSub==0 ); /* Ticket c52b09c7f38903b1311 */
  6245. + pItem->regReturn = ++pParse->nMem;
  6246. + topAddr = sqlite3VdbeAddOp2(v, OP_Integer, 0, pItem->regReturn);
  6247. + pItem->addrFillSub = topAddr+1;
  6248. + if( pItem->fg.isCorrelated==0 ){
  6249. + /* If the subquery is not correlated and if we are not inside of
  6250. + ** a trigger, then we only need to compute the value of the subquery
  6251. + ** once. */
  6252. + onceAddr = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
  6253. + VdbeComment((v, "materialize \"%s\"", pItem->pTab->zName));
  6254. + }else{
  6255. + VdbeNoopComment((v, "materialize \"%s\"", pItem->pTab->zName));
  6256. + }
  6257. + pPrior = isSelfJoinView(pTabList, pItem);
  6258. + if( pPrior ){
  6259. + sqlite3VdbeAddOp2(v, OP_OpenDup, pItem->iCursor, pPrior->iCursor);
  6260. + assert( pPrior->pSelect!=0 );
  6261. + pSub->nSelectRow = pPrior->pSelect->nSelectRow;
  6262. + }else{
  6263. + sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor);
  6264. + ExplainQueryPlan((pParse, 1, "MATERIALIZE %u", pSub->selId));
  6265. + sqlite3Select(pParse, pSub, &dest);
  6266. + }
  6267. + pItem->pTab->nRowLogEst = pSub->nSelectRow;
  6268. + if( onceAddr ) sqlite3VdbeJumpHere(v, onceAddr);
  6269. + retAddr = sqlite3VdbeAddOp1(v, OP_Return, pItem->regReturn);
  6270. + VdbeComment((v, "end %s", pItem->pTab->zName));
  6271. + sqlite3VdbeChangeP1(v, topAddr, retAddr);
  6272. + sqlite3ClearTempRegCache(pParse);
  6273. + }
  6274. + if( db->mallocFailed ) goto select_end;
  6275. + pParse->nHeight -= sqlite3SelectExprHeight(p);
  6276. + pParse->zAuthContext = zSavedAuthContext;
  6277. +#endif
  6278. + }
  6279. +
  6280. + /* Various elements of the SELECT copied into local variables for
  6281. + ** convenience */
  6282. + pEList = p->pEList;
  6283. + pWhere = p->pWhere;
  6284. + pGroupBy = p->pGroupBy;
  6285. + pHaving = p->pHaving;
  6286. + sDistinct.isTnct = (p->selFlags & SF_Distinct)!=0;
  6287. +
  6288. +#if SELECTTRACE_ENABLED
  6289. + if( sqlite3SelectTrace & 0x400 ){
  6290. + SELECTTRACE(0x400,pParse,p,("After all FROM-clause analysis:\n"));
  6291. + sqlite3TreeViewSelect(0, p, 0);
  6292. + }
  6293. +#endif
  6294. +
  6295. + /* If the query is DISTINCT with an ORDER BY but is not an aggregate, and
  6296. + ** if the select-list is the same as the ORDER BY list, then this query
  6297. + ** can be rewritten as a GROUP BY. In other words, this:
  6298. + **
  6299. + ** SELECT DISTINCT xyz FROM ... ORDER BY xyz
  6300. + **
  6301. + ** is transformed to:
  6302. + **
  6303. + ** SELECT xyz FROM ... GROUP BY xyz ORDER BY xyz
  6304. + **
  6305. + ** The second form is preferred as a single index (or temp-table) may be
  6306. + ** used for both the ORDER BY and DISTINCT processing. As originally
  6307. + ** written the query must use a temp-table for at least one of the ORDER
  6308. + ** BY and DISTINCT, and an index or separate temp-table for the other.
  6309. + */
  6310. + if( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct
  6311. + && sqlite3ExprListCompare(sSort.pOrderBy, pEList, -1)==0
  6312. +#ifndef SQLITE_OMIT_WINDOWFUNC
  6313. + && ALWAYS(p->pWin==0)
  6314. +#endif
  6315. + ){
  6316. + p->selFlags &= ~SF_Distinct;
  6317. + pGroupBy = p->pGroupBy = sqlite3ExprListDup(db, pEList, 0);
  6318. + p->selFlags |= SF_Aggregate;
  6319. + /* Notice that even thought SF_Distinct has been cleared from p->selFlags,
  6320. + ** the sDistinct.isTnct is still set. Hence, isTnct represents the
  6321. + ** original setting of the SF_Distinct flag, not the current setting */
  6322. + assert( sDistinct.isTnct );
  6323. +
  6324. +#if SELECTTRACE_ENABLED
  6325. + if( sqlite3SelectTrace & 0x400 ){
  6326. + SELECTTRACE(0x400,pParse,p,("Transform DISTINCT into GROUP BY:\n"));
  6327. + sqlite3TreeViewSelect(0, p, 0);
  6328. + }
  6329. +#endif
  6330. + }
  6331. +
  6332. + /* If there is an ORDER BY clause, then create an ephemeral index to
  6333. + ** do the sorting. But this sorting ephemeral index might end up
  6334. + ** being unused if the data can be extracted in pre-sorted order.
  6335. + ** If that is the case, then the OP_OpenEphemeral instruction will be
  6336. + ** changed to an OP_Noop once we figure out that the sorting index is
  6337. + ** not needed. The sSort.addrSortIndex variable is used to facilitate
  6338. + ** that change.
  6339. + */
  6340. + if( sSort.pOrderBy ){
  6341. + KeyInfo *pKeyInfo;
  6342. + pKeyInfo = sqlite3KeyInfoFromExprList(
  6343. + pParse, sSort.pOrderBy, 0, pEList->nExpr);
  6344. + sSort.iECursor = pParse->nTab++;
  6345. + sSort.addrSortIndex =
  6346. + sqlite3VdbeAddOp4(v, OP_OpenEphemeral,
  6347. + sSort.iECursor, sSort.pOrderBy->nExpr+1+pEList->nExpr, 0,
  6348. + (char*)pKeyInfo, P4_KEYINFO
  6349. + );
  6350. + }else{
  6351. + sSort.addrSortIndex = -1;
  6352. + }
  6353. +
  6354. + /* If the output is destined for a temporary table, open that table.
  6355. + */
  6356. + if( pDest->eDest==SRT_EphemTab ){
  6357. + sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pDest->iSDParm, pEList->nExpr);
  6358. + }
  6359. +
  6360. + /* Set the limiter.
  6361. + */
  6362. + iEnd = sqlite3VdbeMakeLabel(pParse);
  6363. + if( (p->selFlags & SF_FixedLimit)==0 ){
  6364. + p->nSelectRow = 320; /* 4 billion rows */
  6365. + }
  6366. + computeLimitRegisters(pParse, p, iEnd);
  6367. + if( p->iLimit==0 && sSort.addrSortIndex>=0 ){
  6368. + sqlite3VdbeChangeOpcode(v, sSort.addrSortIndex, OP_SorterOpen);
  6369. + sSort.sortFlags |= SORTFLAG_UseSorter;
  6370. + }
  6371. +
  6372. + /* Open an ephemeral index to use for the distinct set.
  6373. + */
  6374. + if( p->selFlags & SF_Distinct ){
  6375. + sDistinct.tabTnct = pParse->nTab++;
  6376. + sDistinct.addrTnct = sqlite3VdbeAddOp4(v, OP_OpenEphemeral,
  6377. + sDistinct.tabTnct, 0, 0,
  6378. + (char*)sqlite3KeyInfoFromExprList(pParse, p->pEList,0,0),
  6379. + P4_KEYINFO);
  6380. + sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
  6381. + sDistinct.eTnctType = WHERE_DISTINCT_UNORDERED;
  6382. + }else{
  6383. + sDistinct.eTnctType = WHERE_DISTINCT_NOOP;
  6384. + }
  6385. +
  6386. + if( !isAgg && pGroupBy==0 ){
  6387. + /* No aggregate functions and no GROUP BY clause */
  6388. + u16 wctrlFlags = (sDistinct.isTnct ? WHERE_WANT_DISTINCT : 0)
  6389. + | (p->selFlags & SF_FixedLimit);
  6390. +#ifndef SQLITE_OMIT_WINDOWFUNC
  6391. + Window *pWin = p->pWin; /* Master window object (or NULL) */
  6392. + if( pWin ){
  6393. + sqlite3WindowCodeInit(pParse, p);
  6394. + }
  6395. +#endif
  6396. + assert( WHERE_USE_LIMIT==SF_FixedLimit );
  6397. +
  6398. +
  6399. + /* Begin the database scan. */
  6400. + SELECTTRACE(1,pParse,p,("WhereBegin\n"));
  6401. + pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, sSort.pOrderBy,
  6402. + p->pEList, wctrlFlags, p->nSelectRow);
  6403. + if( pWInfo==0 ) goto select_end;
  6404. + if( sqlite3WhereOutputRowCount(pWInfo) < p->nSelectRow ){
  6405. + p->nSelectRow = sqlite3WhereOutputRowCount(pWInfo);
  6406. + }
  6407. + if( sDistinct.isTnct && sqlite3WhereIsDistinct(pWInfo) ){
  6408. + sDistinct.eTnctType = sqlite3WhereIsDistinct(pWInfo);
  6409. + }
  6410. + if( sSort.pOrderBy ){
  6411. + sSort.nOBSat = sqlite3WhereIsOrdered(pWInfo);
  6412. + sSort.labelOBLopt = sqlite3WhereOrderByLimitOptLabel(pWInfo);
  6413. + if( sSort.nOBSat==sSort.pOrderBy->nExpr ){
  6414. + sSort.pOrderBy = 0;
  6415. + }
  6416. + }
  6417. +
  6418. + /* If sorting index that was created by a prior OP_OpenEphemeral
  6419. + ** instruction ended up not being needed, then change the OP_OpenEphemeral
  6420. + ** into an OP_Noop.
  6421. + */
  6422. + if( sSort.addrSortIndex>=0 && sSort.pOrderBy==0 ){
  6423. + sqlite3VdbeChangeToNoop(v, sSort.addrSortIndex);
  6424. + }
  6425. +
  6426. + assert( p->pEList==pEList );
  6427. +#ifndef SQLITE_OMIT_WINDOWFUNC
  6428. + if( pWin ){
  6429. + int addrGosub = sqlite3VdbeMakeLabel(pParse);
  6430. + int iCont = sqlite3VdbeMakeLabel(pParse);
  6431. + int iBreak = sqlite3VdbeMakeLabel(pParse);
  6432. + int regGosub = ++pParse->nMem;
  6433. +
  6434. + sqlite3WindowCodeStep(pParse, p, pWInfo, regGosub, addrGosub);
  6435. +
  6436. + sqlite3VdbeAddOp2(v, OP_Goto, 0, iBreak);
  6437. + sqlite3VdbeResolveLabel(v, addrGosub);
  6438. + VdbeNoopComment((v, "inner-loop subroutine"));
  6439. + sSort.labelOBLopt = 0;
  6440. + selectInnerLoop(pParse, p, -1, &sSort, &sDistinct, pDest, iCont, iBreak);
  6441. + sqlite3VdbeResolveLabel(v, iCont);
  6442. + sqlite3VdbeAddOp1(v, OP_Return, regGosub);
  6443. + VdbeComment((v, "end inner-loop subroutine"));
  6444. + sqlite3VdbeResolveLabel(v, iBreak);
  6445. + }else
  6446. +#endif /* SQLITE_OMIT_WINDOWFUNC */
  6447. + {
  6448. + /* Use the standard inner loop. */
  6449. + selectInnerLoop(pParse, p, -1, &sSort, &sDistinct, pDest,
  6450. + sqlite3WhereContinueLabel(pWInfo),
  6451. + sqlite3WhereBreakLabel(pWInfo));
  6452. +
  6453. + /* End the database scan loop.
  6454. + */
  6455. + sqlite3WhereEnd(pWInfo);
  6456. + }
  6457. + }else{
  6458. + /* This case when there exist aggregate functions or a GROUP BY clause
  6459. + ** or both */
  6460. + NameContext sNC; /* Name context for processing aggregate information */
  6461. + int iAMem; /* First Mem address for storing current GROUP BY */
  6462. + int iBMem; /* First Mem address for previous GROUP BY */
  6463. + int iUseFlag; /* Mem address holding flag indicating that at least
  6464. + ** one row of the input to the aggregator has been
  6465. + ** processed */
  6466. + int iAbortFlag; /* Mem address which causes query abort if positive */
  6467. + int groupBySort; /* Rows come from source in GROUP BY order */
  6468. + int addrEnd; /* End of processing for this SELECT */
  6469. + int sortPTab = 0; /* Pseudotable used to decode sorting results */
  6470. + int sortOut = 0; /* Output register from the sorter */
  6471. + int orderByGrp = 0; /* True if the GROUP BY and ORDER BY are the same */
  6472. +
  6473. + /* Remove any and all aliases between the result set and the
  6474. + ** GROUP BY clause.
  6475. + */
  6476. + if( pGroupBy ){
  6477. + int k; /* Loop counter */
  6478. + struct ExprList_item *pItem; /* For looping over expression in a list */
  6479. +
  6480. + for(k=p->pEList->nExpr, pItem=p->pEList->a; k>0; k--, pItem++){
  6481. + pItem->u.x.iAlias = 0;
  6482. + }
  6483. + for(k=pGroupBy->nExpr, pItem=pGroupBy->a; k>0; k--, pItem++){
  6484. + pItem->u.x.iAlias = 0;
  6485. + }
  6486. + assert( 66==sqlite3LogEst(100) );
  6487. + if( p->nSelectRow>66 ) p->nSelectRow = 66;
  6488. +
  6489. + /* If there is both a GROUP BY and an ORDER BY clause and they are
  6490. + ** identical, then it may be possible to disable the ORDER BY clause
  6491. + ** on the grounds that the GROUP BY will cause elements to come out
  6492. + ** in the correct order. It also may not - the GROUP BY might use a
  6493. + ** database index that causes rows to be grouped together as required
  6494. + ** but not actually sorted. Either way, record the fact that the
  6495. + ** ORDER BY and GROUP BY clauses are the same by setting the orderByGrp
  6496. + ** variable. */
  6497. + if( sSort.pOrderBy && pGroupBy->nExpr==sSort.pOrderBy->nExpr ){
  6498. + int ii;
  6499. + /* The GROUP BY processing doesn't care whether rows are delivered in
  6500. + ** ASC or DESC order - only that each group is returned contiguously.
  6501. + ** So set the ASC/DESC flags in the GROUP BY to match those in the
  6502. + ** ORDER BY to maximize the chances of rows being delivered in an
  6503. + ** order that makes the ORDER BY redundant. */
  6504. + for(ii=0; ii<pGroupBy->nExpr; ii++){
  6505. + u8 sortFlags = sSort.pOrderBy->a[ii].sortFlags & KEYINFO_ORDER_DESC;
  6506. + pGroupBy->a[ii].sortFlags = sortFlags;
  6507. + }
  6508. + if( sqlite3ExprListCompare(pGroupBy, sSort.pOrderBy, -1)==0 ){
  6509. + orderByGrp = 1;
  6510. + }
  6511. + }
  6512. + }else{
  6513. + assert( 0==sqlite3LogEst(1) );
  6514. + p->nSelectRow = 0;
  6515. + }
  6516. +
  6517. + /* Create a label to jump to when we want to abort the query */
  6518. + addrEnd = sqlite3VdbeMakeLabel(pParse);
  6519. +
  6520. + /* Convert TK_COLUMN nodes into TK_AGG_COLUMN and make entries in
  6521. + ** sAggInfo for all TK_AGG_FUNCTION nodes in expressions of the
  6522. + ** SELECT statement.
  6523. + */
  6524. + memset(&sNC, 0, sizeof(sNC));
  6525. + sNC.pParse = pParse;
  6526. + sNC.pSrcList = pTabList;
  6527. + sNC.uNC.pAggInfo = &sAggInfo;
  6528. + VVA_ONLY( sNC.ncFlags = NC_UAggInfo; )
  6529. + sAggInfo.mnReg = pParse->nMem+1;
  6530. + sAggInfo.nSortingColumn = pGroupBy ? pGroupBy->nExpr : 0;
  6531. + sAggInfo.pGroupBy = pGroupBy;
  6532. + sqlite3ExprAnalyzeAggList(&sNC, pEList);
  6533. + sqlite3ExprAnalyzeAggList(&sNC, sSort.pOrderBy);
  6534. + if( pHaving ){
  6535. + if( pGroupBy ){
  6536. + assert( pWhere==p->pWhere );
  6537. + assert( pHaving==p->pHaving );
  6538. + assert( pGroupBy==p->pGroupBy );
  6539. + havingToWhere(pParse, p);
  6540. + pWhere = p->pWhere;
  6541. + }
  6542. + sqlite3ExprAnalyzeAggregates(&sNC, pHaving);
  6543. + }
  6544. + sAggInfo.nAccumulator = sAggInfo.nColumn;
  6545. + if( p->pGroupBy==0 && p->pHaving==0 && sAggInfo.nFunc==1 ){
  6546. + minMaxFlag = minMaxQuery(db, sAggInfo.aFunc[0].pExpr, &pMinMaxOrderBy);
  6547. + }else{
  6548. + minMaxFlag = WHERE_ORDERBY_NORMAL;
  6549. + }
  6550. + for(i=0; i<sAggInfo.nFunc; i++){
  6551. + Expr *pExpr = sAggInfo.aFunc[i].pExpr;
  6552. + assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
  6553. + sNC.ncFlags |= NC_InAggFunc;
  6554. + sqlite3ExprAnalyzeAggList(&sNC, pExpr->x.pList);
  6555. +#ifndef SQLITE_OMIT_WINDOWFUNC
  6556. + assert( !IsWindowFunc(pExpr) );
  6557. + if( ExprHasProperty(pExpr, EP_WinFunc) ){
  6558. + sqlite3ExprAnalyzeAggregates(&sNC, pExpr->y.pWin->pFilter);
  6559. + }
  6560. +#endif
  6561. + sNC.ncFlags &= ~NC_InAggFunc;
  6562. + }
  6563. + sAggInfo.mxReg = pParse->nMem;
  6564. + if( db->mallocFailed ) goto select_end;
  6565. +#if SELECTTRACE_ENABLED
  6566. + if( sqlite3SelectTrace & 0x400 ){
  6567. + int ii;
  6568. + SELECTTRACE(0x400,pParse,p,("After aggregate analysis %p:\n", &sAggInfo));
  6569. + sqlite3TreeViewSelect(0, p, 0);
  6570. + for(ii=0; ii<sAggInfo.nColumn; ii++){
  6571. + sqlite3DebugPrintf("agg-column[%d] iMem=%d\n",
  6572. + ii, sAggInfo.aCol[ii].iMem);
  6573. + sqlite3TreeViewExpr(0, sAggInfo.aCol[ii].pExpr, 0);
  6574. + }
  6575. + for(ii=0; ii<sAggInfo.nFunc; ii++){
  6576. + sqlite3DebugPrintf("agg-func[%d]: iMem=%d\n",
  6577. + ii, sAggInfo.aFunc[ii].iMem);
  6578. + sqlite3TreeViewExpr(0, sAggInfo.aFunc[ii].pExpr, 0);
  6579. + }
  6580. + }
  6581. +#endif
  6582. +
  6583. +
  6584. + /* Processing for aggregates with GROUP BY is very different and
  6585. + ** much more complex than aggregates without a GROUP BY.
  6586. + */
  6587. + if( pGroupBy ){
  6588. + KeyInfo *pKeyInfo; /* Keying information for the group by clause */
  6589. + int addr1; /* A-vs-B comparision jump */
  6590. + int addrOutputRow; /* Start of subroutine that outputs a result row */
  6591. + int regOutputRow; /* Return address register for output subroutine */
  6592. + int addrSetAbort; /* Set the abort flag and return */
  6593. + int addrTopOfLoop; /* Top of the input loop */
  6594. + int addrSortingIdx; /* The OP_OpenEphemeral for the sorting index */
  6595. + int addrReset; /* Subroutine for resetting the accumulator */
  6596. + int regReset; /* Return address register for reset subroutine */
  6597. +
  6598. + /* If there is a GROUP BY clause we might need a sorting index to
  6599. + ** implement it. Allocate that sorting index now. If it turns out
  6600. + ** that we do not need it after all, the OP_SorterOpen instruction
  6601. + ** will be converted into a Noop.
  6602. + */
  6603. + sAggInfo.sortingIdx = pParse->nTab++;
  6604. + pKeyInfo = sqlite3KeyInfoFromExprList(pParse,pGroupBy,0,sAggInfo.nColumn);
  6605. + addrSortingIdx = sqlite3VdbeAddOp4(v, OP_SorterOpen,
  6606. + sAggInfo.sortingIdx, sAggInfo.nSortingColumn,
  6607. + 0, (char*)pKeyInfo, P4_KEYINFO);
  6608. +
  6609. + /* Initialize memory locations used by GROUP BY aggregate processing
  6610. + */
  6611. + iUseFlag = ++pParse->nMem;
  6612. + iAbortFlag = ++pParse->nMem;
  6613. + regOutputRow = ++pParse->nMem;
  6614. + addrOutputRow = sqlite3VdbeMakeLabel(pParse);
  6615. + regReset = ++pParse->nMem;
  6616. + addrReset = sqlite3VdbeMakeLabel(pParse);
  6617. + iAMem = pParse->nMem + 1;
  6618. + pParse->nMem += pGroupBy->nExpr;
  6619. + iBMem = pParse->nMem + 1;
  6620. + pParse->nMem += pGroupBy->nExpr;
  6621. + sqlite3VdbeAddOp2(v, OP_Integer, 0, iAbortFlag);
  6622. + VdbeComment((v, "clear abort flag"));
  6623. + sqlite3VdbeAddOp3(v, OP_Null, 0, iAMem, iAMem+pGroupBy->nExpr-1);
  6624. +
  6625. + /* Begin a loop that will extract all source rows in GROUP BY order.
  6626. + ** This might involve two separate loops with an OP_Sort in between, or
  6627. + ** it might be a single loop that uses an index to extract information
  6628. + ** in the right order to begin with.
  6629. + */
  6630. + sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset);
  6631. + SELECTTRACE(1,pParse,p,("WhereBegin\n"));
  6632. + pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pGroupBy, 0,
  6633. + WHERE_GROUPBY | (orderByGrp ? WHERE_SORTBYGROUP : 0), 0
  6634. + );
  6635. + if( pWInfo==0 ) goto select_end;
  6636. + if( sqlite3WhereIsOrdered(pWInfo)==pGroupBy->nExpr ){
  6637. + /* The optimizer is able to deliver rows in group by order so
  6638. + ** we do not have to sort. The OP_OpenEphemeral table will be
  6639. + ** cancelled later because we still need to use the pKeyInfo
  6640. + */
  6641. + groupBySort = 0;
  6642. + }else{
  6643. + /* Rows are coming out in undetermined order. We have to push
  6644. + ** each row into a sorting index, terminate the first loop,
  6645. + ** then loop over the sorting index in order to get the output
  6646. + ** in sorted order
  6647. + */
  6648. + int regBase;
  6649. + int regRecord;
  6650. + int nCol;
  6651. + int nGroupBy;
  6652. +
  6653. + explainTempTable(pParse,
  6654. + (sDistinct.isTnct && (p->selFlags&SF_Distinct)==0) ?
  6655. + "DISTINCT" : "GROUP BY");
  6656. +
  6657. + groupBySort = 1;
  6658. + nGroupBy = pGroupBy->nExpr;
  6659. + nCol = nGroupBy;
  6660. + j = nGroupBy;
  6661. + for(i=0; i<sAggInfo.nColumn; i++){
  6662. + if( sAggInfo.aCol[i].iSorterColumn>=j ){
  6663. + nCol++;
  6664. + j++;
  6665. + }
  6666. + }
  6667. + regBase = sqlite3GetTempRange(pParse, nCol);
  6668. + sqlite3ExprCodeExprList(pParse, pGroupBy, regBase, 0, 0);
  6669. + j = nGroupBy;
  6670. + for(i=0; i<sAggInfo.nColumn; i++){
  6671. + struct AggInfo_col *pCol = &sAggInfo.aCol[i];
  6672. + if( pCol->iSorterColumn>=j ){
  6673. + int r1 = j + regBase;
  6674. + sqlite3ExprCodeGetColumnOfTable(v,
  6675. + pCol->pTab, pCol->iTable, pCol->iColumn, r1);
  6676. + j++;
  6677. + }
  6678. + }
  6679. + regRecord = sqlite3GetTempReg(pParse);
  6680. + sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol, regRecord);
  6681. + sqlite3VdbeAddOp2(v, OP_SorterInsert, sAggInfo.sortingIdx, regRecord);
  6682. + sqlite3ReleaseTempReg(pParse, regRecord);
  6683. + sqlite3ReleaseTempRange(pParse, regBase, nCol);
  6684. + sqlite3WhereEnd(pWInfo);
  6685. + sAggInfo.sortingIdxPTab = sortPTab = pParse->nTab++;
  6686. + sortOut = sqlite3GetTempReg(pParse);
  6687. + sqlite3VdbeAddOp3(v, OP_OpenPseudo, sortPTab, sortOut, nCol);
  6688. + sqlite3VdbeAddOp2(v, OP_SorterSort, sAggInfo.sortingIdx, addrEnd);
  6689. + VdbeComment((v, "GROUP BY sort")); VdbeCoverage(v);
  6690. + sAggInfo.useSortingIdx = 1;
  6691. + }
  6692. +
  6693. + /* If the index or temporary table used by the GROUP BY sort
  6694. + ** will naturally deliver rows in the order required by the ORDER BY
  6695. + ** clause, cancel the ephemeral table open coded earlier.
  6696. + **
  6697. + ** This is an optimization - the correct answer should result regardless.
  6698. + ** Use the SQLITE_GroupByOrder flag with SQLITE_TESTCTRL_OPTIMIZER to
  6699. + ** disable this optimization for testing purposes. */
  6700. + if( orderByGrp && OptimizationEnabled(db, SQLITE_GroupByOrder)
  6701. + && (groupBySort || sqlite3WhereIsSorted(pWInfo))
  6702. + ){
  6703. + sSort.pOrderBy = 0;
  6704. + sqlite3VdbeChangeToNoop(v, sSort.addrSortIndex);
  6705. + }
  6706. +
  6707. + /* Evaluate the current GROUP BY terms and store in b0, b1, b2...
  6708. + ** (b0 is memory location iBMem+0, b1 is iBMem+1, and so forth)
  6709. + ** Then compare the current GROUP BY terms against the GROUP BY terms
  6710. + ** from the previous row currently stored in a0, a1, a2...
  6711. + */
  6712. + addrTopOfLoop = sqlite3VdbeCurrentAddr(v);
  6713. + if( groupBySort ){
  6714. + sqlite3VdbeAddOp3(v, OP_SorterData, sAggInfo.sortingIdx,
  6715. + sortOut, sortPTab);
  6716. + }
  6717. + for(j=0; j<pGroupBy->nExpr; j++){
  6718. + if( groupBySort ){
  6719. + sqlite3VdbeAddOp3(v, OP_Column, sortPTab, j, iBMem+j);
  6720. + }else{
  6721. + sAggInfo.directMode = 1;
  6722. + sqlite3ExprCode(pParse, pGroupBy->a[j].pExpr, iBMem+j);
  6723. + }
  6724. + }
  6725. + sqlite3VdbeAddOp4(v, OP_Compare, iAMem, iBMem, pGroupBy->nExpr,
  6726. + (char*)sqlite3KeyInfoRef(pKeyInfo), P4_KEYINFO);
  6727. + addr1 = sqlite3VdbeCurrentAddr(v);
  6728. + sqlite3VdbeAddOp3(v, OP_Jump, addr1+1, 0, addr1+1); VdbeCoverage(v);
  6729. +
  6730. + /* Generate code that runs whenever the GROUP BY changes.
  6731. + ** Changes in the GROUP BY are detected by the previous code
  6732. + ** block. If there were no changes, this block is skipped.
  6733. + **
  6734. + ** This code copies current group by terms in b0,b1,b2,...
  6735. + ** over to a0,a1,a2. It then calls the output subroutine
  6736. + ** and resets the aggregate accumulator registers in preparation
  6737. + ** for the next GROUP BY batch.
  6738. + */
  6739. + sqlite3ExprCodeMove(pParse, iBMem, iAMem, pGroupBy->nExpr);
  6740. + sqlite3VdbeAddOp2(v, OP_Gosub, regOutputRow, addrOutputRow);
  6741. + VdbeComment((v, "output one row"));
  6742. + sqlite3VdbeAddOp2(v, OP_IfPos, iAbortFlag, addrEnd); VdbeCoverage(v);
  6743. + VdbeComment((v, "check abort flag"));
  6744. + sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset);
  6745. + VdbeComment((v, "reset accumulator"));
  6746. +
  6747. + /* Update the aggregate accumulators based on the content of
  6748. + ** the current row
  6749. + */
  6750. + sqlite3VdbeJumpHere(v, addr1);
  6751. + updateAccumulator(pParse, iUseFlag, &sAggInfo);
  6752. + sqlite3VdbeAddOp2(v, OP_Integer, 1, iUseFlag);
  6753. + VdbeComment((v, "indicate data in accumulator"));
  6754. +
  6755. + /* End of the loop
  6756. + */
  6757. + if( groupBySort ){
  6758. + sqlite3VdbeAddOp2(v, OP_SorterNext, sAggInfo.sortingIdx, addrTopOfLoop);
  6759. + VdbeCoverage(v);
  6760. + }else{
  6761. + sqlite3WhereEnd(pWInfo);
  6762. + sqlite3VdbeChangeToNoop(v, addrSortingIdx);
  6763. + }
  6764. +
  6765. + /* Output the final row of result
  6766. + */
  6767. + sqlite3VdbeAddOp2(v, OP_Gosub, regOutputRow, addrOutputRow);
  6768. + VdbeComment((v, "output final row"));
  6769. +
  6770. + /* Jump over the subroutines
  6771. + */
  6772. + sqlite3VdbeGoto(v, addrEnd);
  6773. +
  6774. + /* Generate a subroutine that outputs a single row of the result
  6775. + ** set. This subroutine first looks at the iUseFlag. If iUseFlag
  6776. + ** is less than or equal to zero, the subroutine is a no-op. If
  6777. + ** the processing calls for the query to abort, this subroutine
  6778. + ** increments the iAbortFlag memory location before returning in
  6779. + ** order to signal the caller to abort.
  6780. + */
  6781. + addrSetAbort = sqlite3VdbeCurrentAddr(v);
  6782. + sqlite3VdbeAddOp2(v, OP_Integer, 1, iAbortFlag);
  6783. + VdbeComment((v, "set abort flag"));
  6784. + sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
  6785. + sqlite3VdbeResolveLabel(v, addrOutputRow);
  6786. + addrOutputRow = sqlite3VdbeCurrentAddr(v);
  6787. + sqlite3VdbeAddOp2(v, OP_IfPos, iUseFlag, addrOutputRow+2);
  6788. + VdbeCoverage(v);
  6789. + VdbeComment((v, "Groupby result generator entry point"));
  6790. + sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
  6791. + finalizeAggFunctions(pParse, &sAggInfo);
  6792. + sqlite3ExprIfFalse(pParse, pHaving, addrOutputRow+1, SQLITE_JUMPIFNULL);
  6793. + selectInnerLoop(pParse, p, -1, &sSort,
  6794. + &sDistinct, pDest,
  6795. + addrOutputRow+1, addrSetAbort);
  6796. + sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
  6797. + VdbeComment((v, "end groupby result generator"));
  6798. +
  6799. + /* Generate a subroutine that will reset the group-by accumulator
  6800. + */
  6801. + sqlite3VdbeResolveLabel(v, addrReset);
  6802. + resetAccumulator(pParse, &sAggInfo);
  6803. + sqlite3VdbeAddOp2(v, OP_Integer, 0, iUseFlag);
  6804. + VdbeComment((v, "indicate accumulator empty"));
  6805. + sqlite3VdbeAddOp1(v, OP_Return, regReset);
  6806. +
  6807. + } /* endif pGroupBy. Begin aggregate queries without GROUP BY: */
  6808. + else {
  6809. + Table *pTab;
  6810. + if( (pTab = isSimpleCount(p, &sAggInfo))!=0 ){
  6811. + /* If isSimpleCount() returns a pointer to a Table structure, then
  6812. + ** the SQL statement is of the form:
  6813. + **
  6814. + ** SELECT count(*) FROM <tbl>
  6815. + **
  6816. + ** where the Table structure returned represents table <tbl>.
  6817. + **
  6818. + ** This statement is so common that it is optimized specially. The
  6819. + ** OP_Count instruction is executed either on the intkey table that
  6820. + ** contains the data for table <tbl> or on one of its indexes. It
  6821. + ** is better to execute the op on an index, as indexes are almost
  6822. + ** always spread across less pages than their corresponding tables.
  6823. + */
  6824. + const int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
  6825. + const int iCsr = pParse->nTab++; /* Cursor to scan b-tree */
  6826. + Index *pIdx; /* Iterator variable */
  6827. + KeyInfo *pKeyInfo = 0; /* Keyinfo for scanned index */
  6828. + Index *pBest = 0; /* Best index found so far */
  6829. + int iRoot = pTab->tnum; /* Root page of scanned b-tree */
  6830. +
  6831. + sqlite3CodeVerifySchema(pParse, iDb);
  6832. + sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
  6833. +
  6834. + /* Search for the index that has the lowest scan cost.
  6835. + **
  6836. + ** (2011-04-15) Do not do a full scan of an unordered index.
  6837. + **
  6838. + ** (2013-10-03) Do not count the entries in a partial index.
  6839. + **
  6840. + ** In practice the KeyInfo structure will not be used. It is only
  6841. + ** passed to keep OP_OpenRead happy.
  6842. + */
  6843. + if( !HasRowid(pTab) ) pBest = sqlite3PrimaryKeyIndex(pTab);
  6844. + if( !p->pSrc->a[0].fg.notIndexed ){
  6845. + for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
  6846. + if( pIdx->bUnordered==0
  6847. + && pIdx->szIdxRow<pTab->szTabRow
  6848. + && pIdx->pPartIdxWhere==0
  6849. + && (!pBest || pIdx->szIdxRow<pBest->szIdxRow)
  6850. + ){
  6851. + pBest = pIdx;
  6852. + }
  6853. + }
  6854. + }
  6855. + if( pBest ){
  6856. + iRoot = pBest->tnum;
  6857. + pKeyInfo = sqlite3KeyInfoOfIndex(pParse, pBest);
  6858. + }
  6859. +
  6860. + /* Open a read-only cursor, execute the OP_Count, close the cursor. */
  6861. + sqlite3VdbeAddOp4Int(v, OP_OpenRead, iCsr, iRoot, iDb, 1);
  6862. + if( pKeyInfo ){
  6863. + sqlite3VdbeChangeP4(v, -1, (char *)pKeyInfo, P4_KEYINFO);
  6864. + }
  6865. + sqlite3VdbeAddOp2(v, OP_Count, iCsr, sAggInfo.aFunc[0].iMem);
  6866. + sqlite3VdbeAddOp1(v, OP_Close, iCsr);
  6867. + explainSimpleCount(pParse, pTab, pBest);
  6868. + }else{
  6869. + int regAcc = 0; /* "populate accumulators" flag */
  6870. +
  6871. + /* If there are accumulator registers but no min() or max() functions
  6872. + ** without FILTER clauses, allocate register regAcc. Register regAcc
  6873. + ** will contain 0 the first time the inner loop runs, and 1 thereafter.
  6874. + ** The code generated by updateAccumulator() uses this to ensure
  6875. + ** that the accumulator registers are (a) updated only once if
  6876. + ** there are no min() or max functions or (b) always updated for the
  6877. + ** first row visited by the aggregate, so that they are updated at
  6878. + ** least once even if the FILTER clause means the min() or max()
  6879. + ** function visits zero rows. */
  6880. + if( sAggInfo.nAccumulator ){
  6881. + for(i=0; i<sAggInfo.nFunc; i++){
  6882. + if( ExprHasProperty(sAggInfo.aFunc[i].pExpr, EP_WinFunc) ) continue;
  6883. + if( sAggInfo.aFunc[i].pFunc->funcFlags&SQLITE_FUNC_NEEDCOLL ) break;
  6884. + }
  6885. + if( i==sAggInfo.nFunc ){
  6886. + regAcc = ++pParse->nMem;
  6887. + sqlite3VdbeAddOp2(v, OP_Integer, 0, regAcc);
  6888. + }
  6889. + }
  6890. +
  6891. + /* This case runs if the aggregate has no GROUP BY clause. The
  6892. + ** processing is much simpler since there is only a single row
  6893. + ** of output.
  6894. + */
  6895. + assert( p->pGroupBy==0 );
  6896. + resetAccumulator(pParse, &sAggInfo);
  6897. +
  6898. + /* If this query is a candidate for the min/max optimization, then
  6899. + ** minMaxFlag will have been previously set to either
  6900. + ** WHERE_ORDERBY_MIN or WHERE_ORDERBY_MAX and pMinMaxOrderBy will
  6901. + ** be an appropriate ORDER BY expression for the optimization.
  6902. + */
  6903. + assert( minMaxFlag==WHERE_ORDERBY_NORMAL || pMinMaxOrderBy!=0 );
  6904. + assert( pMinMaxOrderBy==0 || pMinMaxOrderBy->nExpr==1 );
  6905. +
  6906. + SELECTTRACE(1,pParse,p,("WhereBegin\n"));
  6907. + pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pMinMaxOrderBy,
  6908. + 0, minMaxFlag, 0);
  6909. + if( pWInfo==0 ){
  6910. + goto select_end;
  6911. + }
  6912. + updateAccumulator(pParse, regAcc, &sAggInfo);
  6913. + if( regAcc ) sqlite3VdbeAddOp2(v, OP_Integer, 1, regAcc);
  6914. + if( sqlite3WhereIsOrdered(pWInfo)>0 ){
  6915. + sqlite3VdbeGoto(v, sqlite3WhereBreakLabel(pWInfo));
  6916. + VdbeComment((v, "%s() by index",
  6917. + (minMaxFlag==WHERE_ORDERBY_MIN?"min":"max")));
  6918. + }
  6919. + sqlite3WhereEnd(pWInfo);
  6920. + finalizeAggFunctions(pParse, &sAggInfo);
  6921. + }
  6922. +
  6923. + sSort.pOrderBy = 0;
  6924. + sqlite3ExprIfFalse(pParse, pHaving, addrEnd, SQLITE_JUMPIFNULL);
  6925. + selectInnerLoop(pParse, p, -1, 0, 0,
  6926. + pDest, addrEnd, addrEnd);
  6927. + }
  6928. + sqlite3VdbeResolveLabel(v, addrEnd);
  6929. +
  6930. + } /* endif aggregate query */
  6931. +
  6932. + if( sDistinct.eTnctType==WHERE_DISTINCT_UNORDERED ){
  6933. + explainTempTable(pParse, "DISTINCT");
  6934. + }
  6935. +
  6936. + /* If there is an ORDER BY clause, then we need to sort the results
  6937. + ** and send them to the callback one by one.
  6938. + */
  6939. + if( sSort.pOrderBy ){
  6940. + explainTempTable(pParse,
  6941. + sSort.nOBSat>0 ? "RIGHT PART OF ORDER BY":"ORDER BY");
  6942. + assert( p->pEList==pEList );
  6943. + generateSortTail(pParse, p, &sSort, pEList->nExpr, pDest);
  6944. + }
  6945. +
  6946. + /* Jump here to skip this query
  6947. + */
  6948. + sqlite3VdbeResolveLabel(v, iEnd);
  6949. +
  6950. + /* The SELECT has been coded. If there is an error in the Parse structure,
  6951. + ** set the return code to 1. Otherwise 0. */
  6952. + rc = (pParse->nErr>0);
  6953. +
  6954. + /* Control jumps to here if an error is encountered above, or upon
  6955. + ** successful coding of the SELECT.
  6956. + */
  6957. +select_end:
  6958. + sqlite3ExprListDelete(db, pMinMaxOrderBy);
  6959. + sqlite3DbFree(db, sAggInfo.aCol);
  6960. +#ifdef SQLITE_DEBUG
  6961. + for(i=0; i<sAggInfo.nFunc; i++){
  6962. + assert( sAggInfo.aFunc[i].pExpr!=0 );
  6963. + assert( sAggInfo.aFunc[i].pExpr->pAggInfo==&sAggInfo );
  6964. + sAggInfo.aFunc[i].pExpr->pAggInfo = 0;
  6965. + }
  6966. + sAggInfo.iAggMagic = 0;
  6967. +#endif
  6968. + sqlite3DbFree(db, sAggInfo.aFunc);
  6969. +#if SELECTTRACE_ENABLED
  6970. + SELECTTRACE(0x1,pParse,p,("end processing\n"));
  6971. + if( (sqlite3SelectTrace & 0x2000)!=0 && ExplainQueryPlanParent(pParse)==0 ){
  6972. + sqlite3TreeViewSelect(0, p, 0);
  6973. + }
  6974. +#endif
  6975. + ExplainQueryPlanPop(pParse);
  6976. + return rc;
  6977. +}
  6978. diff -Npur sqlite-version-3.32.2/src/sqliteInt.h sqlite-version-3.32.2-patched/src/sqliteInt.h
  6979. --- sqlite-version-3.32.2/src/sqliteInt.h 2020-06-04 20:58:43.000000000 +0800
  6980. +++ sqlite-version-3.32.2-patched/src/sqliteInt.h 2020-07-08 10:00:50.899152517 +0800
  6981. @@ -976,7 +976,12 @@ typedef INT16_TYPE LogEst;
  6982. */
  6983. #if defined(SQLITE_ENABLE_SELECTTRACE)
  6984. # define SELECTTRACE_ENABLED 1
  6985. +# define SELECTTRACE(K,P,S,X) \
  6986. + if(sqlite3SelectTrace&(K)) \
  6987. + sqlite3DebugPrintf("%u/%d/%p: ",(S)->selId,(P)->addrExplain,(S)),\
  6988. + sqlite3DebugPrintf X
  6989. #else
  6990. +# define SELECTTRACE(K,P,S,X)
  6991. # define SELECTTRACE_ENABLED 0
  6992. #endif
  6993. @@ -2523,9 +2528,24 @@ struct AggInfo {
  6994. int iDistinct; /* Ephemeral table used to enforce DISTINCT */
  6995. } *aFunc;
  6996. int nFunc; /* Number of entries in aFunc[] */
  6997. +#ifdef SQLITE_DEBUG
  6998. + u32 iAggMagic; /* Sanity checking constant */
  6999. +#endif
  7000. };
  7001. /*
  7002. +** Allowed values for AggInfo.iAggMagic
  7003. +*/
  7004. +#define SQLITE_AGGMAGIC_VALID 0x05cadade
  7005. +
  7006. +/*
  7007. +** True if the AggInfo object is valid. Used inside of assert() only.
  7008. +*/
  7009. +#ifdef SQLITE_DEBUG
  7010. +# define AggInfoValid(P) ((P)->iAggMagic==SQLITE_AGGMAGIC_VALID)
  7011. +#endif
  7012. +
  7013. +/*
  7014. ** The datatype ynVar is a signed integer, either 16-bit or 32-bit.
  7015. ** Usually it is 16-bits. But if SQLITE_MAX_VARIABLE_NUMBER is greater
  7016. ** than 32767 we have to make it 32-bit. 16-bit is preferred because
  7017. @@ -3105,6 +3125,7 @@ struct Select {
  7018. #define SF_WhereBegin 0x0080000 /* Really a WhereBegin() call. Debug Only */
  7019. #define SF_WinRewrite 0x0100000 /* Window function rewrite accomplished */
  7020. #define SF_View 0x0200000 /* SELECT statement is a view */
  7021. +#define SF_NoopOrderBy 0x0400000 /* ORDER BY is ignored for this query */
  7022. /*
  7023. ** The results of a SELECT can be distributed in several ways, as defined
  7024. @@ -4546,10 +4567,11 @@ extern const unsigned char sqlite3UpperT
  7025. extern const unsigned char sqlite3CtypeMap[];
  7026. extern SQLITE_WSD struct Sqlite3Config sqlite3Config;
  7027. extern FuncDefHash sqlite3BuiltinFunctions;
  7028. +extern u32 sqlite3SelectTrace;
  7029. #ifndef SQLITE_OMIT_WSD
  7030. extern int sqlite3PendingByte;
  7031. #endif
  7032. -#endif
  7033. +#endif /* !defined(SQLITE_AMALGAMATION) */
  7034. #ifdef VDBE_PROFILE
  7035. extern sqlite3_uint64 sqlite3NProfileCnt;
  7036. #endif
  7037. diff -Npur sqlite-version-3.32.2/src/sqliteInt.h.orig sqlite-version-3.32.2-patched/src/sqliteInt.h.orig
  7038. --- sqlite-version-3.32.2/src/sqliteInt.h.orig 1970-01-01 08:00:00.000000000 +0800
  7039. +++ sqlite-version-3.32.2-patched/src/sqliteInt.h.orig 2020-07-08 10:00:47.367088648 +0800
  7040. @@ -0,0 +1,4977 @@
  7041. +/*
  7042. +** 2001 September 15
  7043. +**
  7044. +** The author disclaims copyright to this source code. In place of
  7045. +** a legal notice, here is a blessing:
  7046. +**
  7047. +** May you do good and not evil.
  7048. +** May you find forgiveness for yourself and forgive others.
  7049. +** May you share freely, never taking more than you give.
  7050. +**
  7051. +*************************************************************************
  7052. +** Internal interface definitions for SQLite.
  7053. +**
  7054. +*/
  7055. +#ifndef SQLITEINT_H
  7056. +#define SQLITEINT_H
  7057. +
  7058. +/* Special Comments:
  7059. +**
  7060. +** Some comments have special meaning to the tools that measure test
  7061. +** coverage:
  7062. +**
  7063. +** NO_TEST - The branches on this line are not
  7064. +** measured by branch coverage. This is
  7065. +** used on lines of code that actually
  7066. +** implement parts of coverage testing.
  7067. +**
  7068. +** OPTIMIZATION-IF-TRUE - This branch is allowed to alway be false
  7069. +** and the correct answer is still obtained,
  7070. +** though perhaps more slowly.
  7071. +**
  7072. +** OPTIMIZATION-IF-FALSE - This branch is allowed to alway be true
  7073. +** and the correct answer is still obtained,
  7074. +** though perhaps more slowly.
  7075. +**
  7076. +** PREVENTS-HARMLESS-OVERREAD - This branch prevents a buffer overread
  7077. +** that would be harmless and undetectable
  7078. +** if it did occur.
  7079. +**
  7080. +** In all cases, the special comment must be enclosed in the usual
  7081. +** slash-asterisk...asterisk-slash comment marks, with no spaces between the
  7082. +** asterisks and the comment text.
  7083. +*/
  7084. +
  7085. +/*
  7086. +** Make sure the Tcl calling convention macro is defined. This macro is
  7087. +** only used by test code and Tcl integration code.
  7088. +*/
  7089. +#ifndef SQLITE_TCLAPI
  7090. +# define SQLITE_TCLAPI
  7091. +#endif
  7092. +
  7093. +/*
  7094. +** Include the header file used to customize the compiler options for MSVC.
  7095. +** This should be done first so that it can successfully prevent spurious
  7096. +** compiler warnings due to subsequent content in this file and other files
  7097. +** that are included by this file.
  7098. +*/
  7099. +#include "msvc.h"
  7100. +
  7101. +/*
  7102. +** Special setup for VxWorks
  7103. +*/
  7104. +#include "vxworks.h"
  7105. +
  7106. +/*
  7107. +** These #defines should enable >2GB file support on POSIX if the
  7108. +** underlying operating system supports it. If the OS lacks
  7109. +** large file support, or if the OS is windows, these should be no-ops.
  7110. +**
  7111. +** Ticket #2739: The _LARGEFILE_SOURCE macro must appear before any
  7112. +** system #includes. Hence, this block of code must be the very first
  7113. +** code in all source files.
  7114. +**
  7115. +** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
  7116. +** on the compiler command line. This is necessary if you are compiling
  7117. +** on a recent machine (ex: Red Hat 7.2) but you want your code to work
  7118. +** on an older machine (ex: Red Hat 6.0). If you compile on Red Hat 7.2
  7119. +** without this option, LFS is enable. But LFS does not exist in the kernel
  7120. +** in Red Hat 6.0, so the code won't work. Hence, for maximum binary
  7121. +** portability you should omit LFS.
  7122. +**
  7123. +** The previous paragraph was written in 2005. (This paragraph is written
  7124. +** on 2008-11-28.) These days, all Linux kernels support large files, so
  7125. +** you should probably leave LFS enabled. But some embedded platforms might
  7126. +** lack LFS in which case the SQLITE_DISABLE_LFS macro might still be useful.
  7127. +**
  7128. +** Similar is true for Mac OS X. LFS is only supported on Mac OS X 9 and later.
  7129. +*/
  7130. +#ifndef SQLITE_DISABLE_LFS
  7131. +# define _LARGE_FILE 1
  7132. +# ifndef _FILE_OFFSET_BITS
  7133. +# define _FILE_OFFSET_BITS 64
  7134. +# endif
  7135. +# define _LARGEFILE_SOURCE 1
  7136. +#endif
  7137. +
  7138. +/* The GCC_VERSION and MSVC_VERSION macros are used to
  7139. +** conditionally include optimizations for each of these compilers. A
  7140. +** value of 0 means that compiler is not being used. The
  7141. +** SQLITE_DISABLE_INTRINSIC macro means do not use any compiler-specific
  7142. +** optimizations, and hence set all compiler macros to 0
  7143. +**
  7144. +** There was once also a CLANG_VERSION macro. However, we learn that the
  7145. +** version numbers in clang are for "marketing" only and are inconsistent
  7146. +** and unreliable. Fortunately, all versions of clang also recognize the
  7147. +** gcc version numbers and have reasonable settings for gcc version numbers,
  7148. +** so the GCC_VERSION macro will be set to a correct non-zero value even
  7149. +** when compiling with clang.
  7150. +*/
  7151. +#if defined(__GNUC__) && !defined(SQLITE_DISABLE_INTRINSIC)
  7152. +# define GCC_VERSION (__GNUC__*1000000+__GNUC_MINOR__*1000+__GNUC_PATCHLEVEL__)
  7153. +#else
  7154. +# define GCC_VERSION 0
  7155. +#endif
  7156. +#if defined(_MSC_VER) && !defined(SQLITE_DISABLE_INTRINSIC)
  7157. +# define MSVC_VERSION _MSC_VER
  7158. +#else
  7159. +# define MSVC_VERSION 0
  7160. +#endif
  7161. +
  7162. +/* Needed for various definitions... */
  7163. +#if defined(__GNUC__) && !defined(_GNU_SOURCE)
  7164. +# define _GNU_SOURCE
  7165. +#endif
  7166. +
  7167. +#if defined(__OpenBSD__) && !defined(_BSD_SOURCE)
  7168. +# define _BSD_SOURCE
  7169. +#endif
  7170. +
  7171. +/*
  7172. +** For MinGW, check to see if we can include the header file containing its
  7173. +** version information, among other things. Normally, this internal MinGW
  7174. +** header file would [only] be included automatically by other MinGW header
  7175. +** files; however, the contained version information is now required by this
  7176. +** header file to work around binary compatibility issues (see below) and
  7177. +** this is the only known way to reliably obtain it. This entire #if block
  7178. +** would be completely unnecessary if there was any other way of detecting
  7179. +** MinGW via their preprocessor (e.g. if they customized their GCC to define
  7180. +** some MinGW-specific macros). When compiling for MinGW, either the
  7181. +** _HAVE_MINGW_H or _HAVE__MINGW_H (note the extra underscore) macro must be
  7182. +** defined; otherwise, detection of conditions specific to MinGW will be
  7183. +** disabled.
  7184. +*/
  7185. +#if defined(_HAVE_MINGW_H)
  7186. +# include "mingw.h"
  7187. +#elif defined(_HAVE__MINGW_H)
  7188. +# include "_mingw.h"
  7189. +#endif
  7190. +
  7191. +/*
  7192. +** For MinGW version 4.x (and higher), check to see if the _USE_32BIT_TIME_T
  7193. +** define is required to maintain binary compatibility with the MSVC runtime
  7194. +** library in use (e.g. for Windows XP).
  7195. +*/
  7196. +#if !defined(_USE_32BIT_TIME_T) && !defined(_USE_64BIT_TIME_T) && \
  7197. + defined(_WIN32) && !defined(_WIN64) && \
  7198. + defined(__MINGW_MAJOR_VERSION) && __MINGW_MAJOR_VERSION >= 4 && \
  7199. + defined(__MSVCRT__)
  7200. +# define _USE_32BIT_TIME_T
  7201. +#endif
  7202. +
  7203. +/* The public SQLite interface. The _FILE_OFFSET_BITS macro must appear
  7204. +** first in QNX. Also, the _USE_32BIT_TIME_T macro must appear first for
  7205. +** MinGW.
  7206. +*/
  7207. +#include "sqlite3.h"
  7208. +
  7209. +/*
  7210. +** Include the configuration header output by 'configure' if we're using the
  7211. +** autoconf-based build
  7212. +*/
  7213. +#if defined(_HAVE_SQLITE_CONFIG_H) && !defined(SQLITECONFIG_H)
  7214. +#include "config.h"
  7215. +#define SQLITECONFIG_H 1
  7216. +#endif
  7217. +
  7218. +#include "sqliteLimit.h"
  7219. +
  7220. +/* Disable nuisance warnings on Borland compilers */
  7221. +#if defined(__BORLANDC__)
  7222. +#pragma warn -rch /* unreachable code */
  7223. +#pragma warn -ccc /* Condition is always true or false */
  7224. +#pragma warn -aus /* Assigned value is never used */
  7225. +#pragma warn -csu /* Comparing signed and unsigned */
  7226. +#pragma warn -spa /* Suspicious pointer arithmetic */
  7227. +#endif
  7228. +
  7229. +/*
  7230. +** WAL mode depends on atomic aligned 32-bit loads and stores in a few
  7231. +** places. The following macros try to make this explicit.
  7232. +*/
  7233. +#ifndef __has_feature
  7234. +# define __has_feature(x) 0 /* compatibility with non-clang compilers */
  7235. +#endif
  7236. +#if GCC_VERSION>=4007000 || __has_feature(c_atomic)
  7237. +# define AtomicLoad(PTR) __atomic_load_n((PTR),__ATOMIC_RELAXED)
  7238. +# define AtomicStore(PTR,VAL) __atomic_store_n((PTR),(VAL),__ATOMIC_RELAXED)
  7239. +#else
  7240. +# define AtomicLoad(PTR) (*(PTR))
  7241. +# define AtomicStore(PTR,VAL) (*(PTR) = (VAL))
  7242. +#endif
  7243. +
  7244. +/*
  7245. +** Include standard header files as necessary
  7246. +*/
  7247. +#ifdef HAVE_STDINT_H
  7248. +#include <stdint.h>
  7249. +#endif
  7250. +#ifdef HAVE_INTTYPES_H
  7251. +#include <inttypes.h>
  7252. +#endif
  7253. +
  7254. +/*
  7255. +** The following macros are used to cast pointers to integers and
  7256. +** integers to pointers. The way you do this varies from one compiler
  7257. +** to the next, so we have developed the following set of #if statements
  7258. +** to generate appropriate macros for a wide range of compilers.
  7259. +**
  7260. +** The correct "ANSI" way to do this is to use the intptr_t type.
  7261. +** Unfortunately, that typedef is not available on all compilers, or
  7262. +** if it is available, it requires an #include of specific headers
  7263. +** that vary from one machine to the next.
  7264. +**
  7265. +** Ticket #3860: The llvm-gcc-4.2 compiler from Apple chokes on
  7266. +** the ((void*)&((char*)0)[X]) construct. But MSVC chokes on ((void*)(X)).
  7267. +** So we have to define the macros in different ways depending on the
  7268. +** compiler.
  7269. +*/
  7270. +#if defined(HAVE_STDINT_H) /* Use this case if we have ANSI headers */
  7271. +# define SQLITE_INT_TO_PTR(X) ((void*)(intptr_t)(X))
  7272. +# define SQLITE_PTR_TO_INT(X) ((int)(intptr_t)(X))
  7273. +#elif defined(__PTRDIFF_TYPE__) /* This case should work for GCC */
  7274. +# define SQLITE_INT_TO_PTR(X) ((void*)(__PTRDIFF_TYPE__)(X))
  7275. +# define SQLITE_PTR_TO_INT(X) ((int)(__PTRDIFF_TYPE__)(X))
  7276. +#elif !defined(__GNUC__) /* Works for compilers other than LLVM */
  7277. +# define SQLITE_INT_TO_PTR(X) ((void*)&((char*)0)[X])
  7278. +# define SQLITE_PTR_TO_INT(X) ((int)(((char*)X)-(char*)0))
  7279. +#else /* Generates a warning - but it always works */
  7280. +# define SQLITE_INT_TO_PTR(X) ((void*)(X))
  7281. +# define SQLITE_PTR_TO_INT(X) ((int)(X))
  7282. +#endif
  7283. +
  7284. +/*
  7285. +** A macro to hint to the compiler that a function should not be
  7286. +** inlined.
  7287. +*/
  7288. +#if defined(__GNUC__)
  7289. +# define SQLITE_NOINLINE __attribute__((noinline))
  7290. +#elif defined(_MSC_VER) && _MSC_VER>=1310
  7291. +# define SQLITE_NOINLINE __declspec(noinline)
  7292. +#else
  7293. +# define SQLITE_NOINLINE
  7294. +#endif
  7295. +
  7296. +/*
  7297. +** Make sure that the compiler intrinsics we desire are enabled when
  7298. +** compiling with an appropriate version of MSVC unless prevented by
  7299. +** the SQLITE_DISABLE_INTRINSIC define.
  7300. +*/
  7301. +#if !defined(SQLITE_DISABLE_INTRINSIC)
  7302. +# if defined(_MSC_VER) && _MSC_VER>=1400
  7303. +# if !defined(_WIN32_WCE)
  7304. +# include <intrin.h>
  7305. +# pragma intrinsic(_byteswap_ushort)
  7306. +# pragma intrinsic(_byteswap_ulong)
  7307. +# pragma intrinsic(_byteswap_uint64)
  7308. +# pragma intrinsic(_ReadWriteBarrier)
  7309. +# else
  7310. +# include <cmnintrin.h>
  7311. +# endif
  7312. +# endif
  7313. +#endif
  7314. +
  7315. +/*
  7316. +** The SQLITE_THREADSAFE macro must be defined as 0, 1, or 2.
  7317. +** 0 means mutexes are permanently disable and the library is never
  7318. +** threadsafe. 1 means the library is serialized which is the highest
  7319. +** level of threadsafety. 2 means the library is multithreaded - multiple
  7320. +** threads can use SQLite as long as no two threads try to use the same
  7321. +** database connection at the same time.
  7322. +**
  7323. +** Older versions of SQLite used an optional THREADSAFE macro.
  7324. +** We support that for legacy.
  7325. +**
  7326. +** To ensure that the correct value of "THREADSAFE" is reported when querying
  7327. +** for compile-time options at runtime (e.g. "PRAGMA compile_options"), this
  7328. +** logic is partially replicated in ctime.c. If it is updated here, it should
  7329. +** also be updated there.
  7330. +*/
  7331. +#if !defined(SQLITE_THREADSAFE)
  7332. +# if defined(THREADSAFE)
  7333. +# define SQLITE_THREADSAFE THREADSAFE
  7334. +# else
  7335. +# define SQLITE_THREADSAFE 1 /* IMP: R-07272-22309 */
  7336. +# endif
  7337. +#endif
  7338. +
  7339. +/*
  7340. +** Powersafe overwrite is on by default. But can be turned off using
  7341. +** the -DSQLITE_POWERSAFE_OVERWRITE=0 command-line option.
  7342. +*/
  7343. +#ifndef SQLITE_POWERSAFE_OVERWRITE
  7344. +# define SQLITE_POWERSAFE_OVERWRITE 1
  7345. +#endif
  7346. +
  7347. +/*
  7348. +** EVIDENCE-OF: R-25715-37072 Memory allocation statistics are enabled by
  7349. +** default unless SQLite is compiled with SQLITE_DEFAULT_MEMSTATUS=0 in
  7350. +** which case memory allocation statistics are disabled by default.
  7351. +*/
  7352. +#if !defined(SQLITE_DEFAULT_MEMSTATUS)
  7353. +# define SQLITE_DEFAULT_MEMSTATUS 1
  7354. +#endif
  7355. +
  7356. +/*
  7357. +** Exactly one of the following macros must be defined in order to
  7358. +** specify which memory allocation subsystem to use.
  7359. +**
  7360. +** SQLITE_SYSTEM_MALLOC // Use normal system malloc()
  7361. +** SQLITE_WIN32_MALLOC // Use Win32 native heap API
  7362. +** SQLITE_ZERO_MALLOC // Use a stub allocator that always fails
  7363. +** SQLITE_MEMDEBUG // Debugging version of system malloc()
  7364. +**
  7365. +** On Windows, if the SQLITE_WIN32_MALLOC_VALIDATE macro is defined and the
  7366. +** assert() macro is enabled, each call into the Win32 native heap subsystem
  7367. +** will cause HeapValidate to be called. If heap validation should fail, an
  7368. +** assertion will be triggered.
  7369. +**
  7370. +** If none of the above are defined, then set SQLITE_SYSTEM_MALLOC as
  7371. +** the default.
  7372. +*/
  7373. +#if defined(SQLITE_SYSTEM_MALLOC) \
  7374. + + defined(SQLITE_WIN32_MALLOC) \
  7375. + + defined(SQLITE_ZERO_MALLOC) \
  7376. + + defined(SQLITE_MEMDEBUG)>1
  7377. +# error "Two or more of the following compile-time configuration options\
  7378. + are defined but at most one is allowed:\
  7379. + SQLITE_SYSTEM_MALLOC, SQLITE_WIN32_MALLOC, SQLITE_MEMDEBUG,\
  7380. + SQLITE_ZERO_MALLOC"
  7381. +#endif
  7382. +#if defined(SQLITE_SYSTEM_MALLOC) \
  7383. + + defined(SQLITE_WIN32_MALLOC) \
  7384. + + defined(SQLITE_ZERO_MALLOC) \
  7385. + + defined(SQLITE_MEMDEBUG)==0
  7386. +# define SQLITE_SYSTEM_MALLOC 1
  7387. +#endif
  7388. +
  7389. +/*
  7390. +** If SQLITE_MALLOC_SOFT_LIMIT is not zero, then try to keep the
  7391. +** sizes of memory allocations below this value where possible.
  7392. +*/
  7393. +#if !defined(SQLITE_MALLOC_SOFT_LIMIT)
  7394. +# define SQLITE_MALLOC_SOFT_LIMIT 1024
  7395. +#endif
  7396. +
  7397. +/*
  7398. +** We need to define _XOPEN_SOURCE as follows in order to enable
  7399. +** recursive mutexes on most Unix systems and fchmod() on OpenBSD.
  7400. +** But _XOPEN_SOURCE define causes problems for Mac OS X, so omit
  7401. +** it.
  7402. +*/
  7403. +#if !defined(_XOPEN_SOURCE) && !defined(__DARWIN__) && !defined(__APPLE__)
  7404. +# define _XOPEN_SOURCE 600
  7405. +#endif
  7406. +
  7407. +/*
  7408. +** NDEBUG and SQLITE_DEBUG are opposites. It should always be true that
  7409. +** defined(NDEBUG)==!defined(SQLITE_DEBUG). If this is not currently true,
  7410. +** make it true by defining or undefining NDEBUG.
  7411. +**
  7412. +** Setting NDEBUG makes the code smaller and faster by disabling the
  7413. +** assert() statements in the code. So we want the default action
  7414. +** to be for NDEBUG to be set and NDEBUG to be undefined only if SQLITE_DEBUG
  7415. +** is set. Thus NDEBUG becomes an opt-in rather than an opt-out
  7416. +** feature.
  7417. +*/
  7418. +#if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
  7419. +# define NDEBUG 1
  7420. +#endif
  7421. +#if defined(NDEBUG) && defined(SQLITE_DEBUG)
  7422. +# undef NDEBUG
  7423. +#endif
  7424. +
  7425. +/*
  7426. +** Enable SQLITE_ENABLE_EXPLAIN_COMMENTS if SQLITE_DEBUG is turned on.
  7427. +*/
  7428. +#if !defined(SQLITE_ENABLE_EXPLAIN_COMMENTS) && defined(SQLITE_DEBUG)
  7429. +# define SQLITE_ENABLE_EXPLAIN_COMMENTS 1
  7430. +#endif
  7431. +
  7432. +/*
  7433. +** The testcase() macro is used to aid in coverage testing. When
  7434. +** doing coverage testing, the condition inside the argument to
  7435. +** testcase() must be evaluated both true and false in order to
  7436. +** get full branch coverage. The testcase() macro is inserted
  7437. +** to help ensure adequate test coverage in places where simple
  7438. +** condition/decision coverage is inadequate. For example, testcase()
  7439. +** can be used to make sure boundary values are tested. For
  7440. +** bitmask tests, testcase() can be used to make sure each bit
  7441. +** is significant and used at least once. On switch statements
  7442. +** where multiple cases go to the same block of code, testcase()
  7443. +** can insure that all cases are evaluated.
  7444. +**
  7445. +*/
  7446. +#ifdef SQLITE_COVERAGE_TEST
  7447. + void sqlite3Coverage(int);
  7448. +# define testcase(X) if( X ){ sqlite3Coverage(__LINE__); }
  7449. +#else
  7450. +# define testcase(X)
  7451. +#endif
  7452. +
  7453. +/*
  7454. +** The TESTONLY macro is used to enclose variable declarations or
  7455. +** other bits of code that are needed to support the arguments
  7456. +** within testcase() and assert() macros.
  7457. +*/
  7458. +#if !defined(NDEBUG) || defined(SQLITE_COVERAGE_TEST)
  7459. +# define TESTONLY(X) X
  7460. +#else
  7461. +# define TESTONLY(X)
  7462. +#endif
  7463. +
  7464. +/*
  7465. +** Sometimes we need a small amount of code such as a variable initialization
  7466. +** to setup for a later assert() statement. We do not want this code to
  7467. +** appear when assert() is disabled. The following macro is therefore
  7468. +** used to contain that setup code. The "VVA" acronym stands for
  7469. +** "Verification, Validation, and Accreditation". In other words, the
  7470. +** code within VVA_ONLY() will only run during verification processes.
  7471. +*/
  7472. +#ifndef NDEBUG
  7473. +# define VVA_ONLY(X) X
  7474. +#else
  7475. +# define VVA_ONLY(X)
  7476. +#endif
  7477. +
  7478. +/*
  7479. +** The ALWAYS and NEVER macros surround boolean expressions which
  7480. +** are intended to always be true or false, respectively. Such
  7481. +** expressions could be omitted from the code completely. But they
  7482. +** are included in a few cases in order to enhance the resilience
  7483. +** of SQLite to unexpected behavior - to make the code "self-healing"
  7484. +** or "ductile" rather than being "brittle" and crashing at the first
  7485. +** hint of unplanned behavior.
  7486. +**
  7487. +** In other words, ALWAYS and NEVER are added for defensive code.
  7488. +**
  7489. +** When doing coverage testing ALWAYS and NEVER are hard-coded to
  7490. +** be true and false so that the unreachable code they specify will
  7491. +** not be counted as untested code.
  7492. +*/
  7493. +#if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
  7494. +# define ALWAYS(X) (1)
  7495. +# define NEVER(X) (0)
  7496. +#elif !defined(NDEBUG)
  7497. +# define ALWAYS(X) ((X)?1:(assert(0),0))
  7498. +# define NEVER(X) ((X)?(assert(0),1):0)
  7499. +#else
  7500. +# define ALWAYS(X) (X)
  7501. +# define NEVER(X) (X)
  7502. +#endif
  7503. +
  7504. +/*
  7505. +** The harmless(X) macro indicates that expression X is usually false
  7506. +** but can be true without causing any problems, but we don't know of
  7507. +** any way to cause X to be true.
  7508. +**
  7509. +** In debugging and testing builds, this macro will abort if X is ever
  7510. +** true. In this way, developers are alerted to a possible test case
  7511. +** that causes X to be true. If a harmless macro ever fails, that is
  7512. +** an opportunity to change the macro into a testcase() and add a new
  7513. +** test case to the test suite.
  7514. +**
  7515. +** For normal production builds, harmless(X) is a no-op, since it does
  7516. +** not matter whether expression X is true or false.
  7517. +*/
  7518. +#ifdef SQLITE_DEBUG
  7519. +# define harmless(X) assert(!(X));
  7520. +#else
  7521. +# define harmless(X)
  7522. +#endif
  7523. +
  7524. +/*
  7525. +** Some conditionals are optimizations only. In other words, if the
  7526. +** conditionals are replaced with a constant 1 (true) or 0 (false) then
  7527. +** the correct answer is still obtained, though perhaps not as quickly.
  7528. +**
  7529. +** The following macros mark these optimizations conditionals.
  7530. +*/
  7531. +#if defined(SQLITE_MUTATION_TEST)
  7532. +# define OK_IF_ALWAYS_TRUE(X) (1)
  7533. +# define OK_IF_ALWAYS_FALSE(X) (0)
  7534. +#else
  7535. +# define OK_IF_ALWAYS_TRUE(X) (X)
  7536. +# define OK_IF_ALWAYS_FALSE(X) (X)
  7537. +#endif
  7538. +
  7539. +/*
  7540. +** Some malloc failures are only possible if SQLITE_TEST_REALLOC_STRESS is
  7541. +** defined. We need to defend against those failures when testing with
  7542. +** SQLITE_TEST_REALLOC_STRESS, but we don't want the unreachable branches
  7543. +** during a normal build. The following macro can be used to disable tests
  7544. +** that are always false except when SQLITE_TEST_REALLOC_STRESS is set.
  7545. +*/
  7546. +#if defined(SQLITE_TEST_REALLOC_STRESS)
  7547. +# define ONLY_IF_REALLOC_STRESS(X) (X)
  7548. +#elif !defined(NDEBUG)
  7549. +# define ONLY_IF_REALLOC_STRESS(X) ((X)?(assert(0),1):0)
  7550. +#else
  7551. +# define ONLY_IF_REALLOC_STRESS(X) (0)
  7552. +#endif
  7553. +
  7554. +/*
  7555. +** Declarations used for tracing the operating system interfaces.
  7556. +*/
  7557. +#if defined(SQLITE_FORCE_OS_TRACE) || defined(SQLITE_TEST) || \
  7558. + (defined(SQLITE_DEBUG) && SQLITE_OS_WIN)
  7559. + extern int sqlite3OSTrace;
  7560. +# define OSTRACE(X) if( sqlite3OSTrace ) sqlite3DebugPrintf X
  7561. +# define SQLITE_HAVE_OS_TRACE
  7562. +#else
  7563. +# define OSTRACE(X)
  7564. +# undef SQLITE_HAVE_OS_TRACE
  7565. +#endif
  7566. +
  7567. +/*
  7568. +** Is the sqlite3ErrName() function needed in the build? Currently,
  7569. +** it is needed by "mutex_w32.c" (when debugging), "os_win.c" (when
  7570. +** OSTRACE is enabled), and by several "test*.c" files (which are
  7571. +** compiled using SQLITE_TEST).
  7572. +*/
  7573. +#if defined(SQLITE_HAVE_OS_TRACE) || defined(SQLITE_TEST) || \
  7574. + (defined(SQLITE_DEBUG) && SQLITE_OS_WIN)
  7575. +# define SQLITE_NEED_ERR_NAME
  7576. +#else
  7577. +# undef SQLITE_NEED_ERR_NAME
  7578. +#endif
  7579. +
  7580. +/*
  7581. +** SQLITE_ENABLE_EXPLAIN_COMMENTS is incompatible with SQLITE_OMIT_EXPLAIN
  7582. +*/
  7583. +#ifdef SQLITE_OMIT_EXPLAIN
  7584. +# undef SQLITE_ENABLE_EXPLAIN_COMMENTS
  7585. +#endif
  7586. +
  7587. +/*
  7588. +** Return true (non-zero) if the input is an integer that is too large
  7589. +** to fit in 32-bits. This macro is used inside of various testcase()
  7590. +** macros to verify that we have tested SQLite for large-file support.
  7591. +*/
  7592. +#define IS_BIG_INT(X) (((X)&~(i64)0xffffffff)!=0)
  7593. +
  7594. +/*
  7595. +** The macro unlikely() is a hint that surrounds a boolean
  7596. +** expression that is usually false. Macro likely() surrounds
  7597. +** a boolean expression that is usually true. These hints could,
  7598. +** in theory, be used by the compiler to generate better code, but
  7599. +** currently they are just comments for human readers.
  7600. +*/
  7601. +#define likely(X) (X)
  7602. +#define unlikely(X) (X)
  7603. +
  7604. +#include "hash.h"
  7605. +#include "parse.h"
  7606. +#include <stdio.h>
  7607. +#include <stdlib.h>
  7608. +#include <string.h>
  7609. +#include <assert.h>
  7610. +#include <stddef.h>
  7611. +
  7612. +/*
  7613. +** Use a macro to replace memcpy() if compiled with SQLITE_INLINE_MEMCPY.
  7614. +** This allows better measurements of where memcpy() is used when running
  7615. +** cachegrind. But this macro version of memcpy() is very slow so it
  7616. +** should not be used in production. This is a performance measurement
  7617. +** hack only.
  7618. +*/
  7619. +#ifdef SQLITE_INLINE_MEMCPY
  7620. +# define memcpy(D,S,N) {char*xxd=(char*)(D);const char*xxs=(const char*)(S);\
  7621. + int xxn=(N);while(xxn-->0)*(xxd++)=*(xxs++);}
  7622. +#endif
  7623. +
  7624. +/*
  7625. +** If compiling for a processor that lacks floating point support,
  7626. +** substitute integer for floating-point
  7627. +*/
  7628. +#ifdef SQLITE_OMIT_FLOATING_POINT
  7629. +# define double sqlite_int64
  7630. +# define float sqlite_int64
  7631. +# define LONGDOUBLE_TYPE sqlite_int64
  7632. +# ifndef SQLITE_BIG_DBL
  7633. +# define SQLITE_BIG_DBL (((sqlite3_int64)1)<<50)
  7634. +# endif
  7635. +# define SQLITE_OMIT_DATETIME_FUNCS 1
  7636. +# define SQLITE_OMIT_TRACE 1
  7637. +# undef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
  7638. +# undef SQLITE_HAVE_ISNAN
  7639. +#endif
  7640. +#ifndef SQLITE_BIG_DBL
  7641. +# define SQLITE_BIG_DBL (1e99)
  7642. +#endif
  7643. +
  7644. +/*
  7645. +** OMIT_TEMPDB is set to 1 if SQLITE_OMIT_TEMPDB is defined, or 0
  7646. +** afterward. Having this macro allows us to cause the C compiler
  7647. +** to omit code used by TEMP tables without messy #ifndef statements.
  7648. +*/
  7649. +#ifdef SQLITE_OMIT_TEMPDB
  7650. +#define OMIT_TEMPDB 1
  7651. +#else
  7652. +#define OMIT_TEMPDB 0
  7653. +#endif
  7654. +
  7655. +/*
  7656. +** The "file format" number is an integer that is incremented whenever
  7657. +** the VDBE-level file format changes. The following macros define the
  7658. +** the default file format for new databases and the maximum file format
  7659. +** that the library can read.
  7660. +*/
  7661. +#define SQLITE_MAX_FILE_FORMAT 4
  7662. +#ifndef SQLITE_DEFAULT_FILE_FORMAT
  7663. +# define SQLITE_DEFAULT_FILE_FORMAT 4
  7664. +#endif
  7665. +
  7666. +/*
  7667. +** Determine whether triggers are recursive by default. This can be
  7668. +** changed at run-time using a pragma.
  7669. +*/
  7670. +#ifndef SQLITE_DEFAULT_RECURSIVE_TRIGGERS
  7671. +# define SQLITE_DEFAULT_RECURSIVE_TRIGGERS 0
  7672. +#endif
  7673. +
  7674. +/*
  7675. +** Provide a default value for SQLITE_TEMP_STORE in case it is not specified
  7676. +** on the command-line
  7677. +*/
  7678. +#ifndef SQLITE_TEMP_STORE
  7679. +# define SQLITE_TEMP_STORE 1
  7680. +#endif
  7681. +
  7682. +/*
  7683. +** If no value has been provided for SQLITE_MAX_WORKER_THREADS, or if
  7684. +** SQLITE_TEMP_STORE is set to 3 (never use temporary files), set it
  7685. +** to zero.
  7686. +*/
  7687. +#if SQLITE_TEMP_STORE==3 || SQLITE_THREADSAFE==0
  7688. +# undef SQLITE_MAX_WORKER_THREADS
  7689. +# define SQLITE_MAX_WORKER_THREADS 0
  7690. +#endif
  7691. +#ifndef SQLITE_MAX_WORKER_THREADS
  7692. +# define SQLITE_MAX_WORKER_THREADS 8
  7693. +#endif
  7694. +#ifndef SQLITE_DEFAULT_WORKER_THREADS
  7695. +# define SQLITE_DEFAULT_WORKER_THREADS 0
  7696. +#endif
  7697. +#if SQLITE_DEFAULT_WORKER_THREADS>SQLITE_MAX_WORKER_THREADS
  7698. +# undef SQLITE_MAX_WORKER_THREADS
  7699. +# define SQLITE_MAX_WORKER_THREADS SQLITE_DEFAULT_WORKER_THREADS
  7700. +#endif
  7701. +
  7702. +/*
  7703. +** The default initial allocation for the pagecache when using separate
  7704. +** pagecaches for each database connection. A positive number is the
  7705. +** number of pages. A negative number N translations means that a buffer
  7706. +** of -1024*N bytes is allocated and used for as many pages as it will hold.
  7707. +**
  7708. +** The default value of "20" was choosen to minimize the run-time of the
  7709. +** speedtest1 test program with options: --shrink-memory --reprepare
  7710. +*/
  7711. +#ifndef SQLITE_DEFAULT_PCACHE_INITSZ
  7712. +# define SQLITE_DEFAULT_PCACHE_INITSZ 20
  7713. +#endif
  7714. +
  7715. +/*
  7716. +** Default value for the SQLITE_CONFIG_SORTERREF_SIZE option.
  7717. +*/
  7718. +#ifndef SQLITE_DEFAULT_SORTERREF_SIZE
  7719. +# define SQLITE_DEFAULT_SORTERREF_SIZE 0x7fffffff
  7720. +#endif
  7721. +
  7722. +/*
  7723. +** The compile-time options SQLITE_MMAP_READWRITE and
  7724. +** SQLITE_ENABLE_BATCH_ATOMIC_WRITE are not compatible with one another.
  7725. +** You must choose one or the other (or neither) but not both.
  7726. +*/
  7727. +#if defined(SQLITE_MMAP_READWRITE) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
  7728. +#error Cannot use both SQLITE_MMAP_READWRITE and SQLITE_ENABLE_BATCH_ATOMIC_WRITE
  7729. +#endif
  7730. +
  7731. +/*
  7732. +** GCC does not define the offsetof() macro so we'll have to do it
  7733. +** ourselves.
  7734. +*/
  7735. +#ifndef offsetof
  7736. +#define offsetof(STRUCTURE,FIELD) ((int)((char*)&((STRUCTURE*)0)->FIELD))
  7737. +#endif
  7738. +
  7739. +/*
  7740. +** Macros to compute minimum and maximum of two numbers.
  7741. +*/
  7742. +#ifndef MIN
  7743. +# define MIN(A,B) ((A)<(B)?(A):(B))
  7744. +#endif
  7745. +#ifndef MAX
  7746. +# define MAX(A,B) ((A)>(B)?(A):(B))
  7747. +#endif
  7748. +
  7749. +/*
  7750. +** Swap two objects of type TYPE.
  7751. +*/
  7752. +#define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;}
  7753. +
  7754. +/*
  7755. +** Check to see if this machine uses EBCDIC. (Yes, believe it or
  7756. +** not, there are still machines out there that use EBCDIC.)
  7757. +*/
  7758. +#if 'A' == '\301'
  7759. +# define SQLITE_EBCDIC 1
  7760. +#else
  7761. +# define SQLITE_ASCII 1
  7762. +#endif
  7763. +
  7764. +/*
  7765. +** Integers of known sizes. These typedefs might change for architectures
  7766. +** where the sizes very. Preprocessor macros are available so that the
  7767. +** types can be conveniently redefined at compile-type. Like this:
  7768. +**
  7769. +** cc '-DUINTPTR_TYPE=long long int' ...
  7770. +*/
  7771. +#ifndef UINT32_TYPE
  7772. +# ifdef HAVE_UINT32_T
  7773. +# define UINT32_TYPE uint32_t
  7774. +# else
  7775. +# define UINT32_TYPE unsigned int
  7776. +# endif
  7777. +#endif
  7778. +#ifndef UINT16_TYPE
  7779. +# ifdef HAVE_UINT16_T
  7780. +# define UINT16_TYPE uint16_t
  7781. +# else
  7782. +# define UINT16_TYPE unsigned short int
  7783. +# endif
  7784. +#endif
  7785. +#ifndef INT16_TYPE
  7786. +# ifdef HAVE_INT16_T
  7787. +# define INT16_TYPE int16_t
  7788. +# else
  7789. +# define INT16_TYPE short int
  7790. +# endif
  7791. +#endif
  7792. +#ifndef UINT8_TYPE
  7793. +# ifdef HAVE_UINT8_T
  7794. +# define UINT8_TYPE uint8_t
  7795. +# else
  7796. +# define UINT8_TYPE unsigned char
  7797. +# endif
  7798. +#endif
  7799. +#ifndef INT8_TYPE
  7800. +# ifdef HAVE_INT8_T
  7801. +# define INT8_TYPE int8_t
  7802. +# else
  7803. +# define INT8_TYPE signed char
  7804. +# endif
  7805. +#endif
  7806. +#ifndef LONGDOUBLE_TYPE
  7807. +# define LONGDOUBLE_TYPE long double
  7808. +#endif
  7809. +typedef sqlite_int64 i64; /* 8-byte signed integer */
  7810. +typedef sqlite_uint64 u64; /* 8-byte unsigned integer */
  7811. +typedef UINT32_TYPE u32; /* 4-byte unsigned integer */
  7812. +typedef UINT16_TYPE u16; /* 2-byte unsigned integer */
  7813. +typedef INT16_TYPE i16; /* 2-byte signed integer */
  7814. +typedef UINT8_TYPE u8; /* 1-byte unsigned integer */
  7815. +typedef INT8_TYPE i8; /* 1-byte signed integer */
  7816. +
  7817. +/*
  7818. +** SQLITE_MAX_U32 is a u64 constant that is the maximum u64 value
  7819. +** that can be stored in a u32 without loss of data. The value
  7820. +** is 0x00000000ffffffff. But because of quirks of some compilers, we
  7821. +** have to specify the value in the less intuitive manner shown:
  7822. +*/
  7823. +#define SQLITE_MAX_U32 ((((u64)1)<<32)-1)
  7824. +
  7825. +/*
  7826. +** The datatype used to store estimates of the number of rows in a
  7827. +** table or index. This is an unsigned integer type. For 99.9% of
  7828. +** the world, a 32-bit integer is sufficient. But a 64-bit integer
  7829. +** can be used at compile-time if desired.
  7830. +*/
  7831. +#ifdef SQLITE_64BIT_STATS
  7832. + typedef u64 tRowcnt; /* 64-bit only if requested at compile-time */
  7833. +#else
  7834. + typedef u32 tRowcnt; /* 32-bit is the default */
  7835. +#endif
  7836. +
  7837. +/*
  7838. +** Estimated quantities used for query planning are stored as 16-bit
  7839. +** logarithms. For quantity X, the value stored is 10*log2(X). This
  7840. +** gives a possible range of values of approximately 1.0e986 to 1e-986.
  7841. +** But the allowed values are "grainy". Not every value is representable.
  7842. +** For example, quantities 16 and 17 are both represented by a LogEst
  7843. +** of 40. However, since LogEst quantities are suppose to be estimates,
  7844. +** not exact values, this imprecision is not a problem.
  7845. +**
  7846. +** "LogEst" is short for "Logarithmic Estimate".
  7847. +**
  7848. +** Examples:
  7849. +** 1 -> 0 20 -> 43 10000 -> 132
  7850. +** 2 -> 10 25 -> 46 25000 -> 146
  7851. +** 3 -> 16 100 -> 66 1000000 -> 199
  7852. +** 4 -> 20 1000 -> 99 1048576 -> 200
  7853. +** 10 -> 33 1024 -> 100 4294967296 -> 320
  7854. +**
  7855. +** The LogEst can be negative to indicate fractional values.
  7856. +** Examples:
  7857. +**
  7858. +** 0.5 -> -10 0.1 -> -33 0.0625 -> -40
  7859. +*/
  7860. +typedef INT16_TYPE LogEst;
  7861. +
  7862. +/*
  7863. +** Set the SQLITE_PTRSIZE macro to the number of bytes in a pointer
  7864. +*/
  7865. +#ifndef SQLITE_PTRSIZE
  7866. +# if defined(__SIZEOF_POINTER__)
  7867. +# define SQLITE_PTRSIZE __SIZEOF_POINTER__
  7868. +# elif defined(i386) || defined(__i386__) || defined(_M_IX86) || \
  7869. + defined(_M_ARM) || defined(__arm__) || defined(__x86) || \
  7870. + (defined(__TOS_AIX__) && !defined(__64BIT__))
  7871. +# define SQLITE_PTRSIZE 4
  7872. +# else
  7873. +# define SQLITE_PTRSIZE 8
  7874. +# endif
  7875. +#endif
  7876. +
  7877. +/* The uptr type is an unsigned integer large enough to hold a pointer
  7878. +*/
  7879. +#if defined(HAVE_STDINT_H)
  7880. + typedef uintptr_t uptr;
  7881. +#elif SQLITE_PTRSIZE==4
  7882. + typedef u32 uptr;
  7883. +#else
  7884. + typedef u64 uptr;
  7885. +#endif
  7886. +
  7887. +/*
  7888. +** The SQLITE_WITHIN(P,S,E) macro checks to see if pointer P points to
  7889. +** something between S (inclusive) and E (exclusive).
  7890. +**
  7891. +** In other words, S is a buffer and E is a pointer to the first byte after
  7892. +** the end of buffer S. This macro returns true if P points to something
  7893. +** contained within the buffer S.
  7894. +*/
  7895. +#define SQLITE_WITHIN(P,S,E) (((uptr)(P)>=(uptr)(S))&&((uptr)(P)<(uptr)(E)))
  7896. +
  7897. +
  7898. +/*
  7899. +** Macros to determine whether the machine is big or little endian,
  7900. +** and whether or not that determination is run-time or compile-time.
  7901. +**
  7902. +** For best performance, an attempt is made to guess at the byte-order
  7903. +** using C-preprocessor macros. If that is unsuccessful, or if
  7904. +** -DSQLITE_BYTEORDER=0 is set, then byte-order is determined
  7905. +** at run-time.
  7906. +*/
  7907. +#ifndef SQLITE_BYTEORDER
  7908. +# if defined(i386) || defined(__i386__) || defined(_M_IX86) || \
  7909. + defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) || \
  7910. + defined(_M_AMD64) || defined(_M_ARM) || defined(__x86) || \
  7911. + defined(__ARMEL__) || defined(__AARCH64EL__) || defined(_M_ARM64)
  7912. +# define SQLITE_BYTEORDER 1234
  7913. +# elif defined(sparc) || defined(__ppc__) || \
  7914. + defined(__ARMEB__) || defined(__AARCH64EB__)
  7915. +# define SQLITE_BYTEORDER 4321
  7916. +# else
  7917. +# define SQLITE_BYTEORDER 0
  7918. +# endif
  7919. +#endif
  7920. +#if SQLITE_BYTEORDER==4321
  7921. +# define SQLITE_BIGENDIAN 1
  7922. +# define SQLITE_LITTLEENDIAN 0
  7923. +# define SQLITE_UTF16NATIVE SQLITE_UTF16BE
  7924. +#elif SQLITE_BYTEORDER==1234
  7925. +# define SQLITE_BIGENDIAN 0
  7926. +# define SQLITE_LITTLEENDIAN 1
  7927. +# define SQLITE_UTF16NATIVE SQLITE_UTF16LE
  7928. +#else
  7929. +# ifdef SQLITE_AMALGAMATION
  7930. + const int sqlite3one = 1;
  7931. +# else
  7932. + extern const int sqlite3one;
  7933. +# endif
  7934. +# define SQLITE_BIGENDIAN (*(char *)(&sqlite3one)==0)
  7935. +# define SQLITE_LITTLEENDIAN (*(char *)(&sqlite3one)==1)
  7936. +# define SQLITE_UTF16NATIVE (SQLITE_BIGENDIAN?SQLITE_UTF16BE:SQLITE_UTF16LE)
  7937. +#endif
  7938. +
  7939. +/*
  7940. +** Constants for the largest and smallest possible 64-bit signed integers.
  7941. +** These macros are designed to work correctly on both 32-bit and 64-bit
  7942. +** compilers.
  7943. +*/
  7944. +#define LARGEST_INT64 (0xffffffff|(((i64)0x7fffffff)<<32))
  7945. +#define SMALLEST_INT64 (((i64)-1) - LARGEST_INT64)
  7946. +
  7947. +/*
  7948. +** Round up a number to the next larger multiple of 8. This is used
  7949. +** to force 8-byte alignment on 64-bit architectures.
  7950. +*/
  7951. +#define ROUND8(x) (((x)+7)&~7)
  7952. +
  7953. +/*
  7954. +** Round down to the nearest multiple of 8
  7955. +*/
  7956. +#define ROUNDDOWN8(x) ((x)&~7)
  7957. +
  7958. +/*
  7959. +** Assert that the pointer X is aligned to an 8-byte boundary. This
  7960. +** macro is used only within assert() to verify that the code gets
  7961. +** all alignment restrictions correct.
  7962. +**
  7963. +** Except, if SQLITE_4_BYTE_ALIGNED_MALLOC is defined, then the
  7964. +** underlying malloc() implementation might return us 4-byte aligned
  7965. +** pointers. In that case, only verify 4-byte alignment.
  7966. +*/
  7967. +#ifdef SQLITE_4_BYTE_ALIGNED_MALLOC
  7968. +# define EIGHT_BYTE_ALIGNMENT(X) ((((char*)(X) - (char*)0)&3)==0)
  7969. +#else
  7970. +# define EIGHT_BYTE_ALIGNMENT(X) ((((char*)(X) - (char*)0)&7)==0)
  7971. +#endif
  7972. +
  7973. +/*
  7974. +** Disable MMAP on platforms where it is known to not work
  7975. +*/
  7976. +#if defined(__OpenBSD__) || defined(__QNXNTO__)
  7977. +# undef SQLITE_MAX_MMAP_SIZE
  7978. +# define SQLITE_MAX_MMAP_SIZE 0
  7979. +#endif
  7980. +
  7981. +/*
  7982. +** Default maximum size of memory used by memory-mapped I/O in the VFS
  7983. +*/
  7984. +#ifdef __APPLE__
  7985. +# include <TargetConditionals.h>
  7986. +#endif
  7987. +#ifndef SQLITE_MAX_MMAP_SIZE
  7988. +# if defined(__linux__) \
  7989. + || defined(_WIN32) \
  7990. + || (defined(__APPLE__) && defined(__MACH__)) \
  7991. + || defined(__sun) \
  7992. + || defined(__FreeBSD__) \
  7993. + || defined(__DragonFly__)
  7994. +# define SQLITE_MAX_MMAP_SIZE 0x7fff0000 /* 2147418112 */
  7995. +# else
  7996. +# define SQLITE_MAX_MMAP_SIZE 0
  7997. +# endif
  7998. +#endif
  7999. +
  8000. +/*
  8001. +** The default MMAP_SIZE is zero on all platforms. Or, even if a larger
  8002. +** default MMAP_SIZE is specified at compile-time, make sure that it does
  8003. +** not exceed the maximum mmap size.
  8004. +*/
  8005. +#ifndef SQLITE_DEFAULT_MMAP_SIZE
  8006. +# define SQLITE_DEFAULT_MMAP_SIZE 0
  8007. +#endif
  8008. +#if SQLITE_DEFAULT_MMAP_SIZE>SQLITE_MAX_MMAP_SIZE
  8009. +# undef SQLITE_DEFAULT_MMAP_SIZE
  8010. +# define SQLITE_DEFAULT_MMAP_SIZE SQLITE_MAX_MMAP_SIZE
  8011. +#endif
  8012. +
  8013. +/*
  8014. +** SELECTTRACE_ENABLED will be either 1 or 0 depending on whether or not
  8015. +** the Select query generator tracing logic is turned on.
  8016. +*/
  8017. +#if defined(SQLITE_ENABLE_SELECTTRACE)
  8018. +# define SELECTTRACE_ENABLED 1
  8019. +# define SELECTTRACE(K,P,S,X) \
  8020. + if(sqlite3SelectTrace&(K)) \
  8021. + sqlite3DebugPrintf("%u/%d/%p: ",(S)->selId,(P)->addrExplain,(S)),\
  8022. + sqlite3DebugPrintf X
  8023. +#else
  8024. +# define SELECTTRACE(K,P,S,X)
  8025. +# define SELECTTRACE_ENABLED 0
  8026. +#endif
  8027. +
  8028. +/*
  8029. +** An instance of the following structure is used to store the busy-handler
  8030. +** callback for a given sqlite handle.
  8031. +**
  8032. +** The sqlite.busyHandler member of the sqlite struct contains the busy
  8033. +** callback for the database handle. Each pager opened via the sqlite
  8034. +** handle is passed a pointer to sqlite.busyHandler. The busy-handler
  8035. +** callback is currently invoked only from within pager.c.
  8036. +*/
  8037. +typedef struct BusyHandler BusyHandler;
  8038. +struct BusyHandler {
  8039. + int (*xBusyHandler)(void *,int); /* The busy callback */
  8040. + void *pBusyArg; /* First arg to busy callback */
  8041. + int nBusy; /* Incremented with each busy call */
  8042. +};
  8043. +
  8044. +/*
  8045. +** Name of the master database table. The master database table
  8046. +** is a special table that holds the names and attributes of all
  8047. +** user tables and indices.
  8048. +*/
  8049. +#define MASTER_NAME "sqlite_master"
  8050. +#define TEMP_MASTER_NAME "sqlite_temp_master"
  8051. +
  8052. +/*
  8053. +** The root-page of the master database table.
  8054. +*/
  8055. +#define MASTER_ROOT 1
  8056. +
  8057. +/*
  8058. +** The name of the schema table.
  8059. +*/
  8060. +#define SCHEMA_TABLE(x) ((!OMIT_TEMPDB)&&(x==1)?TEMP_MASTER_NAME:MASTER_NAME)
  8061. +
  8062. +/*
  8063. +** A convenience macro that returns the number of elements in
  8064. +** an array.
  8065. +*/
  8066. +#define ArraySize(X) ((int)(sizeof(X)/sizeof(X[0])))
  8067. +
  8068. +/*
  8069. +** Determine if the argument is a power of two
  8070. +*/
  8071. +#define IsPowerOfTwo(X) (((X)&((X)-1))==0)
  8072. +
  8073. +/*
  8074. +** The following value as a destructor means to use sqlite3DbFree().
  8075. +** The sqlite3DbFree() routine requires two parameters instead of the
  8076. +** one parameter that destructors normally want. So we have to introduce
  8077. +** this magic value that the code knows to handle differently. Any
  8078. +** pointer will work here as long as it is distinct from SQLITE_STATIC
  8079. +** and SQLITE_TRANSIENT.
  8080. +*/
  8081. +#define SQLITE_DYNAMIC ((sqlite3_destructor_type)sqlite3MallocSize)
  8082. +
  8083. +/*
  8084. +** When SQLITE_OMIT_WSD is defined, it means that the target platform does
  8085. +** not support Writable Static Data (WSD) such as global and static variables.
  8086. +** All variables must either be on the stack or dynamically allocated from
  8087. +** the heap. When WSD is unsupported, the variable declarations scattered
  8088. +** throughout the SQLite code must become constants instead. The SQLITE_WSD
  8089. +** macro is used for this purpose. And instead of referencing the variable
  8090. +** directly, we use its constant as a key to lookup the run-time allocated
  8091. +** buffer that holds real variable. The constant is also the initializer
  8092. +** for the run-time allocated buffer.
  8093. +**
  8094. +** In the usual case where WSD is supported, the SQLITE_WSD and GLOBAL
  8095. +** macros become no-ops and have zero performance impact.
  8096. +*/
  8097. +#ifdef SQLITE_OMIT_WSD
  8098. + #define SQLITE_WSD const
  8099. + #define GLOBAL(t,v) (*(t*)sqlite3_wsd_find((void*)&(v), sizeof(v)))
  8100. + #define sqlite3GlobalConfig GLOBAL(struct Sqlite3Config, sqlite3Config)
  8101. + int sqlite3_wsd_init(int N, int J);
  8102. + void *sqlite3_wsd_find(void *K, int L);
  8103. +#else
  8104. + #define SQLITE_WSD
  8105. + #define GLOBAL(t,v) v
  8106. + #define sqlite3GlobalConfig sqlite3Config
  8107. +#endif
  8108. +
  8109. +/*
  8110. +** The following macros are used to suppress compiler warnings and to
  8111. +** make it clear to human readers when a function parameter is deliberately
  8112. +** left unused within the body of a function. This usually happens when
  8113. +** a function is called via a function pointer. For example the
  8114. +** implementation of an SQL aggregate step callback may not use the
  8115. +** parameter indicating the number of arguments passed to the aggregate,
  8116. +** if it knows that this is enforced elsewhere.
  8117. +**
  8118. +** When a function parameter is not used at all within the body of a function,
  8119. +** it is generally named "NotUsed" or "NotUsed2" to make things even clearer.
  8120. +** However, these macros may also be used to suppress warnings related to
  8121. +** parameters that may or may not be used depending on compilation options.
  8122. +** For example those parameters only used in assert() statements. In these
  8123. +** cases the parameters are named as per the usual conventions.
  8124. +*/
  8125. +#define UNUSED_PARAMETER(x) (void)(x)
  8126. +#define UNUSED_PARAMETER2(x,y) UNUSED_PARAMETER(x),UNUSED_PARAMETER(y)
  8127. +
  8128. +/*
  8129. +** Forward references to structures
  8130. +*/
  8131. +typedef struct AggInfo AggInfo;
  8132. +typedef struct AuthContext AuthContext;
  8133. +typedef struct AutoincInfo AutoincInfo;
  8134. +typedef struct Bitvec Bitvec;
  8135. +typedef struct CollSeq CollSeq;
  8136. +typedef struct Column Column;
  8137. +typedef struct Db Db;
  8138. +typedef struct Schema Schema;
  8139. +typedef struct Expr Expr;
  8140. +typedef struct ExprList ExprList;
  8141. +typedef struct FKey FKey;
  8142. +typedef struct FuncDestructor FuncDestructor;
  8143. +typedef struct FuncDef FuncDef;
  8144. +typedef struct FuncDefHash FuncDefHash;
  8145. +typedef struct IdList IdList;
  8146. +typedef struct Index Index;
  8147. +typedef struct IndexSample IndexSample;
  8148. +typedef struct KeyClass KeyClass;
  8149. +typedef struct KeyInfo KeyInfo;
  8150. +typedef struct Lookaside Lookaside;
  8151. +typedef struct LookasideSlot LookasideSlot;
  8152. +typedef struct Module Module;
  8153. +typedef struct NameContext NameContext;
  8154. +typedef struct Parse Parse;
  8155. +typedef struct PreUpdate PreUpdate;
  8156. +typedef struct PrintfArguments PrintfArguments;
  8157. +typedef struct RenameToken RenameToken;
  8158. +typedef struct RowSet RowSet;
  8159. +typedef struct Savepoint Savepoint;
  8160. +typedef struct Select Select;
  8161. +typedef struct SQLiteThread SQLiteThread;
  8162. +typedef struct SelectDest SelectDest;
  8163. +typedef struct SrcList SrcList;
  8164. +typedef struct sqlite3_str StrAccum; /* Internal alias for sqlite3_str */
  8165. +typedef struct Table Table;
  8166. +typedef struct TableLock TableLock;
  8167. +typedef struct Token Token;
  8168. +typedef struct TreeView TreeView;
  8169. +typedef struct Trigger Trigger;
  8170. +typedef struct TriggerPrg TriggerPrg;
  8171. +typedef struct TriggerStep TriggerStep;
  8172. +typedef struct UnpackedRecord UnpackedRecord;
  8173. +typedef struct Upsert Upsert;
  8174. +typedef struct VTable VTable;
  8175. +typedef struct VtabCtx VtabCtx;
  8176. +typedef struct Walker Walker;
  8177. +typedef struct WhereInfo WhereInfo;
  8178. +typedef struct Window Window;
  8179. +typedef struct With With;
  8180. +
  8181. +
  8182. +/*
  8183. +** The bitmask datatype defined below is used for various optimizations.
  8184. +**
  8185. +** Changing this from a 64-bit to a 32-bit type limits the number of
  8186. +** tables in a join to 32 instead of 64. But it also reduces the size
  8187. +** of the library by 738 bytes on ix86.
  8188. +*/
  8189. +#ifdef SQLITE_BITMASK_TYPE
  8190. + typedef SQLITE_BITMASK_TYPE Bitmask;
  8191. +#else
  8192. + typedef u64 Bitmask;
  8193. +#endif
  8194. +
  8195. +/*
  8196. +** The number of bits in a Bitmask. "BMS" means "BitMask Size".
  8197. +*/
  8198. +#define BMS ((int)(sizeof(Bitmask)*8))
  8199. +
  8200. +/*
  8201. +** A bit in a Bitmask
  8202. +*/
  8203. +#define MASKBIT(n) (((Bitmask)1)<<(n))
  8204. +#define MASKBIT64(n) (((u64)1)<<(n))
  8205. +#define MASKBIT32(n) (((unsigned int)1)<<(n))
  8206. +#define ALLBITS ((Bitmask)-1)
  8207. +
  8208. +/* A VList object records a mapping between parameters/variables/wildcards
  8209. +** in the SQL statement (such as $abc, @pqr, or :xyz) and the integer
  8210. +** variable number associated with that parameter. See the format description
  8211. +** on the sqlite3VListAdd() routine for more information. A VList is really
  8212. +** just an array of integers.
  8213. +*/
  8214. +typedef int VList;
  8215. +
  8216. +/*
  8217. +** Defer sourcing vdbe.h and btree.h until after the "u8" and
  8218. +** "BusyHandler" typedefs. vdbe.h also requires a few of the opaque
  8219. +** pointer types (i.e. FuncDef) defined above.
  8220. +*/
  8221. +#include "btree.h"
  8222. +#include "vdbe.h"
  8223. +#include "pager.h"
  8224. +#include "pcache.h"
  8225. +#include "os.h"
  8226. +#include "mutex.h"
  8227. +
  8228. +/* The SQLITE_EXTRA_DURABLE compile-time option used to set the default
  8229. +** synchronous setting to EXTRA. It is no longer supported.
  8230. +*/
  8231. +#ifdef SQLITE_EXTRA_DURABLE
  8232. +# warning Use SQLITE_DEFAULT_SYNCHRONOUS=3 instead of SQLITE_EXTRA_DURABLE
  8233. +# define SQLITE_DEFAULT_SYNCHRONOUS 3
  8234. +#endif
  8235. +
  8236. +/*
  8237. +** Default synchronous levels.
  8238. +**
  8239. +** Note that (for historcal reasons) the PAGER_SYNCHRONOUS_* macros differ
  8240. +** from the SQLITE_DEFAULT_SYNCHRONOUS value by 1.
  8241. +**
  8242. +** PAGER_SYNCHRONOUS DEFAULT_SYNCHRONOUS
  8243. +** OFF 1 0
  8244. +** NORMAL 2 1
  8245. +** FULL 3 2
  8246. +** EXTRA 4 3
  8247. +**
  8248. +** The "PRAGMA synchronous" statement also uses the zero-based numbers.
  8249. +** In other words, the zero-based numbers are used for all external interfaces
  8250. +** and the one-based values are used internally.
  8251. +*/
  8252. +#ifndef SQLITE_DEFAULT_SYNCHRONOUS
  8253. +# define SQLITE_DEFAULT_SYNCHRONOUS 2
  8254. +#endif
  8255. +#ifndef SQLITE_DEFAULT_WAL_SYNCHRONOUS
  8256. +# define SQLITE_DEFAULT_WAL_SYNCHRONOUS SQLITE_DEFAULT_SYNCHRONOUS
  8257. +#endif
  8258. +
  8259. +/*
  8260. +** Each database file to be accessed by the system is an instance
  8261. +** of the following structure. There are normally two of these structures
  8262. +** in the sqlite.aDb[] array. aDb[0] is the main database file and
  8263. +** aDb[1] is the database file used to hold temporary tables. Additional
  8264. +** databases may be attached.
  8265. +*/
  8266. +struct Db {
  8267. + char *zDbSName; /* Name of this database. (schema name, not filename) */
  8268. + Btree *pBt; /* The B*Tree structure for this database file */
  8269. + u8 safety_level; /* How aggressive at syncing data to disk */
  8270. + u8 bSyncSet; /* True if "PRAGMA synchronous=N" has been run */
  8271. + Schema *pSchema; /* Pointer to database schema (possibly shared) */
  8272. +};
  8273. +
  8274. +/*
  8275. +** An instance of the following structure stores a database schema.
  8276. +**
  8277. +** Most Schema objects are associated with a Btree. The exception is
  8278. +** the Schema for the TEMP databaes (sqlite3.aDb[1]) which is free-standing.
  8279. +** In shared cache mode, a single Schema object can be shared by multiple
  8280. +** Btrees that refer to the same underlying BtShared object.
  8281. +**
  8282. +** Schema objects are automatically deallocated when the last Btree that
  8283. +** references them is destroyed. The TEMP Schema is manually freed by
  8284. +** sqlite3_close().
  8285. +*
  8286. +** A thread must be holding a mutex on the corresponding Btree in order
  8287. +** to access Schema content. This implies that the thread must also be
  8288. +** holding a mutex on the sqlite3 connection pointer that owns the Btree.
  8289. +** For a TEMP Schema, only the connection mutex is required.
  8290. +*/
  8291. +struct Schema {
  8292. + int schema_cookie; /* Database schema version number for this file */
  8293. + int iGeneration; /* Generation counter. Incremented with each change */
  8294. + Hash tblHash; /* All tables indexed by name */
  8295. + Hash idxHash; /* All (named) indices indexed by name */
  8296. + Hash trigHash; /* All triggers indexed by name */
  8297. + Hash fkeyHash; /* All foreign keys by referenced table name */
  8298. + Table *pSeqTab; /* The sqlite_sequence table used by AUTOINCREMENT */
  8299. + u8 file_format; /* Schema format version for this file */
  8300. + u8 enc; /* Text encoding used by this database */
  8301. + u16 schemaFlags; /* Flags associated with this schema */
  8302. + int cache_size; /* Number of pages to use in the cache */
  8303. +};
  8304. +
  8305. +/*
  8306. +** These macros can be used to test, set, or clear bits in the
  8307. +** Db.pSchema->flags field.
  8308. +*/
  8309. +#define DbHasProperty(D,I,P) (((D)->aDb[I].pSchema->schemaFlags&(P))==(P))
  8310. +#define DbHasAnyProperty(D,I,P) (((D)->aDb[I].pSchema->schemaFlags&(P))!=0)
  8311. +#define DbSetProperty(D,I,P) (D)->aDb[I].pSchema->schemaFlags|=(P)
  8312. +#define DbClearProperty(D,I,P) (D)->aDb[I].pSchema->schemaFlags&=~(P)
  8313. +
  8314. +/*
  8315. +** Allowed values for the DB.pSchema->flags field.
  8316. +**
  8317. +** The DB_SchemaLoaded flag is set after the database schema has been
  8318. +** read into internal hash tables.
  8319. +**
  8320. +** DB_UnresetViews means that one or more views have column names that
  8321. +** have been filled out. If the schema changes, these column names might
  8322. +** changes and so the view will need to be reset.
  8323. +*/
  8324. +#define DB_SchemaLoaded 0x0001 /* The schema has been loaded */
  8325. +#define DB_UnresetViews 0x0002 /* Some views have defined column names */
  8326. +#define DB_ResetWanted 0x0008 /* Reset the schema when nSchemaLock==0 */
  8327. +
  8328. +/*
  8329. +** The number of different kinds of things that can be limited
  8330. +** using the sqlite3_limit() interface.
  8331. +*/
  8332. +#define SQLITE_N_LIMIT (SQLITE_LIMIT_WORKER_THREADS+1)
  8333. +
  8334. +/*
  8335. +** Lookaside malloc is a set of fixed-size buffers that can be used
  8336. +** to satisfy small transient memory allocation requests for objects
  8337. +** associated with a particular database connection. The use of
  8338. +** lookaside malloc provides a significant performance enhancement
  8339. +** (approx 10%) by avoiding numerous malloc/free requests while parsing
  8340. +** SQL statements.
  8341. +**
  8342. +** The Lookaside structure holds configuration information about the
  8343. +** lookaside malloc subsystem. Each available memory allocation in
  8344. +** the lookaside subsystem is stored on a linked list of LookasideSlot
  8345. +** objects.
  8346. +**
  8347. +** Lookaside allocations are only allowed for objects that are associated
  8348. +** with a particular database connection. Hence, schema information cannot
  8349. +** be stored in lookaside because in shared cache mode the schema information
  8350. +** is shared by multiple database connections. Therefore, while parsing
  8351. +** schema information, the Lookaside.bEnabled flag is cleared so that
  8352. +** lookaside allocations are not used to construct the schema objects.
  8353. +**
  8354. +** New lookaside allocations are only allowed if bDisable==0. When
  8355. +** bDisable is greater than zero, sz is set to zero which effectively
  8356. +** disables lookaside without adding a new test for the bDisable flag
  8357. +** in a performance-critical path. sz should be set by to szTrue whenever
  8358. +** bDisable changes back to zero.
  8359. +**
  8360. +** Lookaside buffers are initially held on the pInit list. As they are
  8361. +** used and freed, they are added back to the pFree list. New allocations
  8362. +** come off of pFree first, then pInit as a fallback. This dual-list
  8363. +** allows use to compute a high-water mark - the maximum number of allocations
  8364. +** outstanding at any point in the past - by subtracting the number of
  8365. +** allocations on the pInit list from the total number of allocations.
  8366. +**
  8367. +** Enhancement on 2019-12-12: Two-size-lookaside
  8368. +** The default lookaside configuration is 100 slots of 1200 bytes each.
  8369. +** The larger slot sizes are important for performance, but they waste
  8370. +** a lot of space, as most lookaside allocations are less than 128 bytes.
  8371. +** The two-size-lookaside enhancement breaks up the lookaside allocation
  8372. +** into two pools: One of 128-byte slots and the other of the default size
  8373. +** (1200-byte) slots. Allocations are filled from the small-pool first,
  8374. +** failing over to the full-size pool if that does not work. Thus more
  8375. +** lookaside slots are available while also using less memory.
  8376. +** This enhancement can be omitted by compiling with
  8377. +** SQLITE_OMIT_TWOSIZE_LOOKASIDE.
  8378. +*/
  8379. +struct Lookaside {
  8380. + u32 bDisable; /* Only operate the lookaside when zero */
  8381. + u16 sz; /* Size of each buffer in bytes */
  8382. + u16 szTrue; /* True value of sz, even if disabled */
  8383. + u8 bMalloced; /* True if pStart obtained from sqlite3_malloc() */
  8384. + u32 nSlot; /* Number of lookaside slots allocated */
  8385. + u32 anStat[3]; /* 0: hits. 1: size misses. 2: full misses */
  8386. + LookasideSlot *pInit; /* List of buffers not previously used */
  8387. + LookasideSlot *pFree; /* List of available buffers */
  8388. +#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
  8389. + LookasideSlot *pSmallInit; /* List of small buffers not prediously used */
  8390. + LookasideSlot *pSmallFree; /* List of available small buffers */
  8391. + void *pMiddle; /* First byte past end of full-size buffers and
  8392. + ** the first byte of LOOKASIDE_SMALL buffers */
  8393. +#endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
  8394. + void *pStart; /* First byte of available memory space */
  8395. + void *pEnd; /* First byte past end of available space */
  8396. +};
  8397. +struct LookasideSlot {
  8398. + LookasideSlot *pNext; /* Next buffer in the list of free buffers */
  8399. +};
  8400. +
  8401. +#define DisableLookaside db->lookaside.bDisable++;db->lookaside.sz=0
  8402. +#define EnableLookaside db->lookaside.bDisable--;\
  8403. + db->lookaside.sz=db->lookaside.bDisable?0:db->lookaside.szTrue
  8404. +
  8405. +/* Size of the smaller allocations in two-size lookside */
  8406. +#ifdef SQLITE_OMIT_TWOSIZE_LOOKASIDE
  8407. +# define LOOKASIDE_SMALL 0
  8408. +#else
  8409. +# define LOOKASIDE_SMALL 128
  8410. +#endif
  8411. +
  8412. +/*
  8413. +** A hash table for built-in function definitions. (Application-defined
  8414. +** functions use a regular table table from hash.h.)
  8415. +**
  8416. +** Hash each FuncDef structure into one of the FuncDefHash.a[] slots.
  8417. +** Collisions are on the FuncDef.u.pHash chain. Use the SQLITE_FUNC_HASH()
  8418. +** macro to compute a hash on the function name.
  8419. +*/
  8420. +#define SQLITE_FUNC_HASH_SZ 23
  8421. +struct FuncDefHash {
  8422. + FuncDef *a[SQLITE_FUNC_HASH_SZ]; /* Hash table for functions */
  8423. +};
  8424. +#define SQLITE_FUNC_HASH(C,L) (((C)+(L))%SQLITE_FUNC_HASH_SZ)
  8425. +
  8426. +#ifdef SQLITE_USER_AUTHENTICATION
  8427. +/*
  8428. +** Information held in the "sqlite3" database connection object and used
  8429. +** to manage user authentication.
  8430. +*/
  8431. +typedef struct sqlite3_userauth sqlite3_userauth;
  8432. +struct sqlite3_userauth {
  8433. + u8 authLevel; /* Current authentication level */
  8434. + int nAuthPW; /* Size of the zAuthPW in bytes */
  8435. + char *zAuthPW; /* Password used to authenticate */
  8436. + char *zAuthUser; /* User name used to authenticate */
  8437. +};
  8438. +
  8439. +/* Allowed values for sqlite3_userauth.authLevel */
  8440. +#define UAUTH_Unknown 0 /* Authentication not yet checked */
  8441. +#define UAUTH_Fail 1 /* User authentication failed */
  8442. +#define UAUTH_User 2 /* Authenticated as a normal user */
  8443. +#define UAUTH_Admin 3 /* Authenticated as an administrator */
  8444. +
  8445. +/* Functions used only by user authorization logic */
  8446. +int sqlite3UserAuthTable(const char*);
  8447. +int sqlite3UserAuthCheckLogin(sqlite3*,const char*,u8*);
  8448. +void sqlite3UserAuthInit(sqlite3*);
  8449. +void sqlite3CryptFunc(sqlite3_context*,int,sqlite3_value**);
  8450. +
  8451. +#endif /* SQLITE_USER_AUTHENTICATION */
  8452. +
  8453. +/*
  8454. +** typedef for the authorization callback function.
  8455. +*/
  8456. +#ifdef SQLITE_USER_AUTHENTICATION
  8457. + typedef int (*sqlite3_xauth)(void*,int,const char*,const char*,const char*,
  8458. + const char*, const char*);
  8459. +#else
  8460. + typedef int (*sqlite3_xauth)(void*,int,const char*,const char*,const char*,
  8461. + const char*);
  8462. +#endif
  8463. +
  8464. +#ifndef SQLITE_OMIT_DEPRECATED
  8465. +/* This is an extra SQLITE_TRACE macro that indicates "legacy" tracing
  8466. +** in the style of sqlite3_trace()
  8467. +*/
  8468. +#define SQLITE_TRACE_LEGACY 0x40 /* Use the legacy xTrace */
  8469. +#define SQLITE_TRACE_XPROFILE 0x80 /* Use the legacy xProfile */
  8470. +#else
  8471. +#define SQLITE_TRACE_LEGACY 0
  8472. +#define SQLITE_TRACE_XPROFILE 0
  8473. +#endif /* SQLITE_OMIT_DEPRECATED */
  8474. +#define SQLITE_TRACE_NONLEGACY_MASK 0x0f /* Normal flags */
  8475. +
  8476. +
  8477. +/*
  8478. +** Each database connection is an instance of the following structure.
  8479. +*/
  8480. +struct sqlite3 {
  8481. + sqlite3_vfs *pVfs; /* OS Interface */
  8482. + struct Vdbe *pVdbe; /* List of active virtual machines */
  8483. + CollSeq *pDfltColl; /* BINARY collseq for the database encoding */
  8484. + sqlite3_mutex *mutex; /* Connection mutex */
  8485. + Db *aDb; /* All backends */
  8486. + int nDb; /* Number of backends currently in use */
  8487. + u32 mDbFlags; /* flags recording internal state */
  8488. + u64 flags; /* flags settable by pragmas. See below */
  8489. + i64 lastRowid; /* ROWID of most recent insert (see above) */
  8490. + i64 szMmap; /* Default mmap_size setting */
  8491. + u32 nSchemaLock; /* Do not reset the schema when non-zero */
  8492. + unsigned int openFlags; /* Flags passed to sqlite3_vfs.xOpen() */
  8493. + int errCode; /* Most recent error code (SQLITE_*) */
  8494. + int errMask; /* & result codes with this before returning */
  8495. + int iSysErrno; /* Errno value from last system error */
  8496. + u16 dbOptFlags; /* Flags to enable/disable optimizations */
  8497. + u8 enc; /* Text encoding */
  8498. + u8 autoCommit; /* The auto-commit flag. */
  8499. + u8 temp_store; /* 1: file 2: memory 0: default */
  8500. + u8 mallocFailed; /* True if we have seen a malloc failure */
  8501. + u8 bBenignMalloc; /* Do not require OOMs if true */
  8502. + u8 dfltLockMode; /* Default locking-mode for attached dbs */
  8503. + signed char nextAutovac; /* Autovac setting after VACUUM if >=0 */
  8504. + u8 suppressErr; /* Do not issue error messages if true */
  8505. + u8 vtabOnConflict; /* Value to return for s3_vtab_on_conflict() */
  8506. + u8 isTransactionSavepoint; /* True if the outermost savepoint is a TS */
  8507. + u8 mTrace; /* zero or more SQLITE_TRACE flags */
  8508. + u8 noSharedCache; /* True if no shared-cache backends */
  8509. + u8 nSqlExec; /* Number of pending OP_SqlExec opcodes */
  8510. + int nextPagesize; /* Pagesize after VACUUM if >0 */
  8511. + u32 magic; /* Magic number for detect library misuse */
  8512. + int nChange; /* Value returned by sqlite3_changes() */
  8513. + int nTotalChange; /* Value returned by sqlite3_total_changes() */
  8514. + int aLimit[SQLITE_N_LIMIT]; /* Limits */
  8515. + int nMaxSorterMmap; /* Maximum size of regions mapped by sorter */
  8516. + struct sqlite3InitInfo { /* Information used during initialization */
  8517. + int newTnum; /* Rootpage of table being initialized */
  8518. + u8 iDb; /* Which db file is being initialized */
  8519. + u8 busy; /* TRUE if currently initializing */
  8520. + unsigned orphanTrigger : 1; /* Last statement is orphaned TEMP trigger */
  8521. + unsigned imposterTable : 1; /* Building an imposter table */
  8522. + unsigned reopenMemdb : 1; /* ATTACH is really a reopen using MemDB */
  8523. + char **azInit; /* "type", "name", and "tbl_name" columns */
  8524. + } init;
  8525. + int nVdbeActive; /* Number of VDBEs currently running */
  8526. + int nVdbeRead; /* Number of active VDBEs that read or write */
  8527. + int nVdbeWrite; /* Number of active VDBEs that read and write */
  8528. + int nVdbeExec; /* Number of nested calls to VdbeExec() */
  8529. + int nVDestroy; /* Number of active OP_VDestroy operations */
  8530. + int nExtension; /* Number of loaded extensions */
  8531. + void **aExtension; /* Array of shared library handles */
  8532. + int (*xTrace)(u32,void*,void*,void*); /* Trace function */
  8533. + void *pTraceArg; /* Argument to the trace function */
  8534. +#ifndef SQLITE_OMIT_DEPRECATED
  8535. + void (*xProfile)(void*,const char*,u64); /* Profiling function */
  8536. + void *pProfileArg; /* Argument to profile function */
  8537. +#endif
  8538. + void *pCommitArg; /* Argument to xCommitCallback() */
  8539. + int (*xCommitCallback)(void*); /* Invoked at every commit. */
  8540. + void *pRollbackArg; /* Argument to xRollbackCallback() */
  8541. + void (*xRollbackCallback)(void*); /* Invoked at every commit. */
  8542. + void *pUpdateArg;
  8543. + void (*xUpdateCallback)(void*,int, const char*,const char*,sqlite_int64);
  8544. + Parse *pParse; /* Current parse */
  8545. +#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
  8546. + void *pPreUpdateArg; /* First argument to xPreUpdateCallback */
  8547. + void (*xPreUpdateCallback)( /* Registered using sqlite3_preupdate_hook() */
  8548. + void*,sqlite3*,int,char const*,char const*,sqlite3_int64,sqlite3_int64
  8549. + );
  8550. + PreUpdate *pPreUpdate; /* Context for active pre-update callback */
  8551. +#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
  8552. +#ifndef SQLITE_OMIT_WAL
  8553. + int (*xWalCallback)(void *, sqlite3 *, const char *, int);
  8554. + void *pWalArg;
  8555. +#endif
  8556. + void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*);
  8557. + void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*);
  8558. + void *pCollNeededArg;
  8559. + sqlite3_value *pErr; /* Most recent error message */
  8560. + union {
  8561. + volatile int isInterrupted; /* True if sqlite3_interrupt has been called */
  8562. + double notUsed1; /* Spacer */
  8563. + } u1;
  8564. + Lookaside lookaside; /* Lookaside malloc configuration */
  8565. +#ifndef SQLITE_OMIT_AUTHORIZATION
  8566. + sqlite3_xauth xAuth; /* Access authorization function */
  8567. + void *pAuthArg; /* 1st argument to the access auth function */
  8568. +#endif
  8569. +#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
  8570. + int (*xProgress)(void *); /* The progress callback */
  8571. + void *pProgressArg; /* Argument to the progress callback */
  8572. + unsigned nProgressOps; /* Number of opcodes for progress callback */
  8573. +#endif
  8574. +#ifndef SQLITE_OMIT_VIRTUALTABLE
  8575. + int nVTrans; /* Allocated size of aVTrans */
  8576. + Hash aModule; /* populated by sqlite3_create_module() */
  8577. + VtabCtx *pVtabCtx; /* Context for active vtab connect/create */
  8578. + VTable **aVTrans; /* Virtual tables with open transactions */
  8579. + VTable *pDisconnect; /* Disconnect these in next sqlite3_prepare() */
  8580. +#endif
  8581. + Hash aFunc; /* Hash table of connection functions */
  8582. + Hash aCollSeq; /* All collating sequences */
  8583. + BusyHandler busyHandler; /* Busy callback */
  8584. + Db aDbStatic[2]; /* Static space for the 2 default backends */
  8585. + Savepoint *pSavepoint; /* List of active savepoints */
  8586. + int nAnalysisLimit; /* Number of index rows to ANALYZE */
  8587. + int busyTimeout; /* Busy handler timeout, in msec */
  8588. + int nSavepoint; /* Number of non-transaction savepoints */
  8589. + int nStatement; /* Number of nested statement-transactions */
  8590. + i64 nDeferredCons; /* Net deferred constraints this transaction. */
  8591. + i64 nDeferredImmCons; /* Net deferred immediate constraints */
  8592. + int *pnBytesFreed; /* If not NULL, increment this in DbFree() */
  8593. +#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
  8594. + /* The following variables are all protected by the STATIC_MASTER
  8595. + ** mutex, not by sqlite3.mutex. They are used by code in notify.c.
  8596. + **
  8597. + ** When X.pUnlockConnection==Y, that means that X is waiting for Y to
  8598. + ** unlock so that it can proceed.
  8599. + **
  8600. + ** When X.pBlockingConnection==Y, that means that something that X tried
  8601. + ** tried to do recently failed with an SQLITE_LOCKED error due to locks
  8602. + ** held by Y.
  8603. + */
  8604. + sqlite3 *pBlockingConnection; /* Connection that caused SQLITE_LOCKED */
  8605. + sqlite3 *pUnlockConnection; /* Connection to watch for unlock */
  8606. + void *pUnlockArg; /* Argument to xUnlockNotify */
  8607. + void (*xUnlockNotify)(void **, int); /* Unlock notify callback */
  8608. + sqlite3 *pNextBlocked; /* Next in list of all blocked connections */
  8609. +#endif
  8610. +#ifdef SQLITE_USER_AUTHENTICATION
  8611. + sqlite3_userauth auth; /* User authentication information */
  8612. +#endif
  8613. +};
  8614. +
  8615. +/*
  8616. +** A macro to discover the encoding of a database.
  8617. +*/
  8618. +#define SCHEMA_ENC(db) ((db)->aDb[0].pSchema->enc)
  8619. +#define ENC(db) ((db)->enc)
  8620. +
  8621. +/*
  8622. +** A u64 constant where the lower 32 bits are all zeros. Only the
  8623. +** upper 32 bits are included in the argument. Necessary because some
  8624. +** C-compilers still do not accept LL integer literals.
  8625. +*/
  8626. +#define HI(X) ((u64)(X)<<32)
  8627. +
  8628. +/*
  8629. +** Possible values for the sqlite3.flags.
  8630. +**
  8631. +** Value constraints (enforced via assert()):
  8632. +** SQLITE_FullFSync == PAGER_FULLFSYNC
  8633. +** SQLITE_CkptFullFSync == PAGER_CKPT_FULLFSYNC
  8634. +** SQLITE_CacheSpill == PAGER_CACHE_SPILL
  8635. +*/
  8636. +#define SQLITE_WriteSchema 0x00000001 /* OK to update SQLITE_MASTER */
  8637. +#define SQLITE_LegacyFileFmt 0x00000002 /* Create new databases in format 1 */
  8638. +#define SQLITE_FullColNames 0x00000004 /* Show full column names on SELECT */
  8639. +#define SQLITE_FullFSync 0x00000008 /* Use full fsync on the backend */
  8640. +#define SQLITE_CkptFullFSync 0x00000010 /* Use full fsync for checkpoint */
  8641. +#define SQLITE_CacheSpill 0x00000020 /* OK to spill pager cache */
  8642. +#define SQLITE_ShortColNames 0x00000040 /* Show short columns names */
  8643. +#define SQLITE_TrustedSchema 0x00000080 /* Allow unsafe functions and
  8644. + ** vtabs in the schema definition */
  8645. +#define SQLITE_NullCallback 0x00000100 /* Invoke the callback once if the */
  8646. + /* result set is empty */
  8647. +#define SQLITE_IgnoreChecks 0x00000200 /* Do not enforce check constraints */
  8648. +#define SQLITE_ReadUncommit 0x00000400 /* READ UNCOMMITTED in shared-cache */
  8649. +#define SQLITE_NoCkptOnClose 0x00000800 /* No checkpoint on close()/DETACH */
  8650. +#define SQLITE_ReverseOrder 0x00001000 /* Reverse unordered SELECTs */
  8651. +#define SQLITE_RecTriggers 0x00002000 /* Enable recursive triggers */
  8652. +#define SQLITE_ForeignKeys 0x00004000 /* Enforce foreign key constraints */
  8653. +#define SQLITE_AutoIndex 0x00008000 /* Enable automatic indexes */
  8654. +#define SQLITE_LoadExtension 0x00010000 /* Enable load_extension */
  8655. +#define SQLITE_LoadExtFunc 0x00020000 /* Enable load_extension() SQL func */
  8656. +#define SQLITE_EnableTrigger 0x00040000 /* True to enable triggers */
  8657. +#define SQLITE_DeferFKs 0x00080000 /* Defer all FK constraints */
  8658. +#define SQLITE_QueryOnly 0x00100000 /* Disable database changes */
  8659. +#define SQLITE_CellSizeCk 0x00200000 /* Check btree cell sizes on load */
  8660. +#define SQLITE_Fts3Tokenizer 0x00400000 /* Enable fts3_tokenizer(2) */
  8661. +#define SQLITE_EnableQPSG 0x00800000 /* Query Planner Stability Guarantee*/
  8662. +#define SQLITE_TriggerEQP 0x01000000 /* Show trigger EXPLAIN QUERY PLAN */
  8663. +#define SQLITE_ResetDatabase 0x02000000 /* Reset the database */
  8664. +#define SQLITE_LegacyAlter 0x04000000 /* Legacy ALTER TABLE behaviour */
  8665. +#define SQLITE_NoSchemaError 0x08000000 /* Do not report schema parse errors*/
  8666. +#define SQLITE_Defensive 0x10000000 /* Input SQL is likely hostile */
  8667. +#define SQLITE_DqsDDL 0x20000000 /* dbl-quoted strings allowed in DDL*/
  8668. +#define SQLITE_DqsDML 0x40000000 /* dbl-quoted strings allowed in DML*/
  8669. +#define SQLITE_EnableView 0x80000000 /* Enable the use of views */
  8670. +#define SQLITE_CountRows HI(0x00001) /* Count rows changed by INSERT, */
  8671. + /* DELETE, or UPDATE and return */
  8672. + /* the count using a callback. */
  8673. +
  8674. +/* Flags used only if debugging */
  8675. +#ifdef SQLITE_DEBUG
  8676. +#define SQLITE_SqlTrace HI(0x0100000) /* Debug print SQL as it executes */
  8677. +#define SQLITE_VdbeListing HI(0x0200000) /* Debug listings of VDBE progs */
  8678. +#define SQLITE_VdbeTrace HI(0x0400000) /* True to trace VDBE execution */
  8679. +#define SQLITE_VdbeAddopTrace HI(0x0800000) /* Trace sqlite3VdbeAddOp() calls */
  8680. +#define SQLITE_VdbeEQP HI(0x1000000) /* Debug EXPLAIN QUERY PLAN */
  8681. +#define SQLITE_ParserTrace HI(0x2000000) /* PRAGMA parser_trace=ON */
  8682. +#endif
  8683. +
  8684. +/*
  8685. +** Allowed values for sqlite3.mDbFlags
  8686. +*/
  8687. +#define DBFLAG_SchemaChange 0x0001 /* Uncommitted Hash table changes */
  8688. +#define DBFLAG_PreferBuiltin 0x0002 /* Preference to built-in funcs */
  8689. +#define DBFLAG_Vacuum 0x0004 /* Currently in a VACUUM */
  8690. +#define DBFLAG_VacuumInto 0x0008 /* Currently running VACUUM INTO */
  8691. +#define DBFLAG_SchemaKnownOk 0x0010 /* Schema is known to be valid */
  8692. +#define DBFLAG_InternalFunc 0x0020 /* Allow use of internal functions */
  8693. +#define DBFLAG_EncodingFixed 0x0040 /* No longer possible to change enc. */
  8694. +
  8695. +/*
  8696. +** Bits of the sqlite3.dbOptFlags field that are used by the
  8697. +** sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,...) interface to
  8698. +** selectively disable various optimizations.
  8699. +*/
  8700. +#define SQLITE_QueryFlattener 0x0001 /* Query flattening */
  8701. +#define SQLITE_WindowFunc 0x0002 /* Use xInverse for window functions */
  8702. +#define SQLITE_GroupByOrder 0x0004 /* GROUPBY cover of ORDERBY */
  8703. +#define SQLITE_FactorOutConst 0x0008 /* Constant factoring */
  8704. +#define SQLITE_DistinctOpt 0x0010 /* DISTINCT using indexes */
  8705. +#define SQLITE_CoverIdxScan 0x0020 /* Covering index scans */
  8706. +#define SQLITE_OrderByIdxJoin 0x0040 /* ORDER BY of joins via index */
  8707. +#define SQLITE_Transitive 0x0080 /* Transitive constraints */
  8708. +#define SQLITE_OmitNoopJoin 0x0100 /* Omit unused tables in joins */
  8709. +#define SQLITE_CountOfView 0x0200 /* The count-of-view optimization */
  8710. +#define SQLITE_CursorHints 0x0400 /* Add OP_CursorHint opcodes */
  8711. +#define SQLITE_Stat4 0x0800 /* Use STAT4 data */
  8712. + /* TH3 expects the Stat4 ^^^^^^ value to be 0x0800. Don't change it */
  8713. +#define SQLITE_PushDown 0x1000 /* The push-down optimization */
  8714. +#define SQLITE_SimplifyJoin 0x2000 /* Convert LEFT JOIN to JOIN */
  8715. +#define SQLITE_SkipScan 0x4000 /* Skip-scans */
  8716. +#define SQLITE_PropagateConst 0x8000 /* The constant propagation opt */
  8717. +#define SQLITE_AllOpts 0xffff /* All optimizations */
  8718. +
  8719. +/*
  8720. +** Macros for testing whether or not optimizations are enabled or disabled.
  8721. +*/
  8722. +#define OptimizationDisabled(db, mask) (((db)->dbOptFlags&(mask))!=0)
  8723. +#define OptimizationEnabled(db, mask) (((db)->dbOptFlags&(mask))==0)
  8724. +
  8725. +/*
  8726. +** Return true if it OK to factor constant expressions into the initialization
  8727. +** code. The argument is a Parse object for the code generator.
  8728. +*/
  8729. +#define ConstFactorOk(P) ((P)->okConstFactor)
  8730. +
  8731. +/*
  8732. +** Possible values for the sqlite.magic field.
  8733. +** The numbers are obtained at random and have no special meaning, other
  8734. +** than being distinct from one another.
  8735. +*/
  8736. +#define SQLITE_MAGIC_OPEN 0xa029a697 /* Database is open */
  8737. +#define SQLITE_MAGIC_CLOSED 0x9f3c2d33 /* Database is closed */
  8738. +#define SQLITE_MAGIC_SICK 0x4b771290 /* Error and awaiting close */
  8739. +#define SQLITE_MAGIC_BUSY 0xf03b7906 /* Database currently in use */
  8740. +#define SQLITE_MAGIC_ERROR 0xb5357930 /* An SQLITE_MISUSE error occurred */
  8741. +#define SQLITE_MAGIC_ZOMBIE 0x64cffc7f /* Close with last statement close */
  8742. +
  8743. +/*
  8744. +** Each SQL function is defined by an instance of the following
  8745. +** structure. For global built-in functions (ex: substr(), max(), count())
  8746. +** a pointer to this structure is held in the sqlite3BuiltinFunctions object.
  8747. +** For per-connection application-defined functions, a pointer to this
  8748. +** structure is held in the db->aHash hash table.
  8749. +**
  8750. +** The u.pHash field is used by the global built-ins. The u.pDestructor
  8751. +** field is used by per-connection app-def functions.
  8752. +*/
  8753. +struct FuncDef {
  8754. + i8 nArg; /* Number of arguments. -1 means unlimited */
  8755. + u32 funcFlags; /* Some combination of SQLITE_FUNC_* */
  8756. + void *pUserData; /* User data parameter */
  8757. + FuncDef *pNext; /* Next function with same name */
  8758. + void (*xSFunc)(sqlite3_context*,int,sqlite3_value**); /* func or agg-step */
  8759. + void (*xFinalize)(sqlite3_context*); /* Agg finalizer */
  8760. + void (*xValue)(sqlite3_context*); /* Current agg value */
  8761. + void (*xInverse)(sqlite3_context*,int,sqlite3_value**); /* inverse agg-step */
  8762. + const char *zName; /* SQL name of the function. */
  8763. + union {
  8764. + FuncDef *pHash; /* Next with a different name but the same hash */
  8765. + FuncDestructor *pDestructor; /* Reference counted destructor function */
  8766. + } u;
  8767. +};
  8768. +
  8769. +/*
  8770. +** This structure encapsulates a user-function destructor callback (as
  8771. +** configured using create_function_v2()) and a reference counter. When
  8772. +** create_function_v2() is called to create a function with a destructor,
  8773. +** a single object of this type is allocated. FuncDestructor.nRef is set to
  8774. +** the number of FuncDef objects created (either 1 or 3, depending on whether
  8775. +** or not the specified encoding is SQLITE_ANY). The FuncDef.pDestructor
  8776. +** member of each of the new FuncDef objects is set to point to the allocated
  8777. +** FuncDestructor.
  8778. +**
  8779. +** Thereafter, when one of the FuncDef objects is deleted, the reference
  8780. +** count on this object is decremented. When it reaches 0, the destructor
  8781. +** is invoked and the FuncDestructor structure freed.
  8782. +*/
  8783. +struct FuncDestructor {
  8784. + int nRef;
  8785. + void (*xDestroy)(void *);
  8786. + void *pUserData;
  8787. +};
  8788. +
  8789. +/*
  8790. +** Possible values for FuncDef.flags. Note that the _LENGTH and _TYPEOF
  8791. +** values must correspond to OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG. And
  8792. +** SQLITE_FUNC_CONSTANT must be the same as SQLITE_DETERMINISTIC. There
  8793. +** are assert() statements in the code to verify this.
  8794. +**
  8795. +** Value constraints (enforced via assert()):
  8796. +** SQLITE_FUNC_MINMAX == NC_MinMaxAgg == SF_MinMaxAgg
  8797. +** SQLITE_FUNC_LENGTH == OPFLAG_LENGTHARG
  8798. +** SQLITE_FUNC_TYPEOF == OPFLAG_TYPEOFARG
  8799. +** SQLITE_FUNC_CONSTANT == SQLITE_DETERMINISTIC from the API
  8800. +** SQLITE_FUNC_DIRECT == SQLITE_DIRECTONLY from the API
  8801. +** SQLITE_FUNC_UNSAFE == SQLITE_INNOCUOUS
  8802. +** SQLITE_FUNC_ENCMASK depends on SQLITE_UTF* macros in the API
  8803. +*/
  8804. +#define SQLITE_FUNC_ENCMASK 0x0003 /* SQLITE_UTF8, SQLITE_UTF16BE or UTF16LE */
  8805. +#define SQLITE_FUNC_LIKE 0x0004 /* Candidate for the LIKE optimization */
  8806. +#define SQLITE_FUNC_CASE 0x0008 /* Case-sensitive LIKE-type function */
  8807. +#define SQLITE_FUNC_EPHEM 0x0010 /* Ephemeral. Delete with VDBE */
  8808. +#define SQLITE_FUNC_NEEDCOLL 0x0020 /* sqlite3GetFuncCollSeq() might be called*/
  8809. +#define SQLITE_FUNC_LENGTH 0x0040 /* Built-in length() function */
  8810. +#define SQLITE_FUNC_TYPEOF 0x0080 /* Built-in typeof() function */
  8811. +#define SQLITE_FUNC_COUNT 0x0100 /* Built-in count(*) aggregate */
  8812. +/* 0x0200 -- available for reuse */
  8813. +#define SQLITE_FUNC_UNLIKELY 0x0400 /* Built-in unlikely() function */
  8814. +#define SQLITE_FUNC_CONSTANT 0x0800 /* Constant inputs give a constant output */
  8815. +#define SQLITE_FUNC_MINMAX 0x1000 /* True for min() and max() aggregates */
  8816. +#define SQLITE_FUNC_SLOCHNG 0x2000 /* "Slow Change". Value constant during a
  8817. + ** single query - might change over time */
  8818. +#define SQLITE_FUNC_TEST 0x4000 /* Built-in testing functions */
  8819. +#define SQLITE_FUNC_OFFSET 0x8000 /* Built-in sqlite_offset() function */
  8820. +#define SQLITE_FUNC_WINDOW 0x00010000 /* Built-in window-only function */
  8821. +#define SQLITE_FUNC_INTERNAL 0x00040000 /* For use by NestedParse() only */
  8822. +#define SQLITE_FUNC_DIRECT 0x00080000 /* Not for use in TRIGGERs or VIEWs */
  8823. +#define SQLITE_FUNC_SUBTYPE 0x00100000 /* Result likely to have sub-type */
  8824. +#define SQLITE_FUNC_UNSAFE 0x00200000 /* Function has side effects */
  8825. +#define SQLITE_FUNC_INLINE 0x00400000 /* Functions implemented in-line */
  8826. +
  8827. +/* Identifier numbers for each in-line function */
  8828. +#define INLINEFUNC_coalesce 0
  8829. +#define INLINEFUNC_implies_nonnull_row 1
  8830. +#define INLINEFUNC_expr_implies_expr 2
  8831. +#define INLINEFUNC_expr_compare 3
  8832. +#define INLINEFUNC_affinity 4
  8833. +#define INLINEFUNC_iif 5
  8834. +#define INLINEFUNC_unlikely 99 /* Default case */
  8835. +
  8836. +/*
  8837. +** The following three macros, FUNCTION(), LIKEFUNC() and AGGREGATE() are
  8838. +** used to create the initializers for the FuncDef structures.
  8839. +**
  8840. +** FUNCTION(zName, nArg, iArg, bNC, xFunc)
  8841. +** Used to create a scalar function definition of a function zName
  8842. +** implemented by C function xFunc that accepts nArg arguments. The
  8843. +** value passed as iArg is cast to a (void*) and made available
  8844. +** as the user-data (sqlite3_user_data()) for the function. If
  8845. +** argument bNC is true, then the SQLITE_FUNC_NEEDCOLL flag is set.
  8846. +**
  8847. +** VFUNCTION(zName, nArg, iArg, bNC, xFunc)
  8848. +** Like FUNCTION except it omits the SQLITE_FUNC_CONSTANT flag.
  8849. +**
  8850. +** SFUNCTION(zName, nArg, iArg, bNC, xFunc)
  8851. +** Like FUNCTION except it omits the SQLITE_FUNC_CONSTANT flag and
  8852. +** adds the SQLITE_DIRECTONLY flag.
  8853. +**
  8854. +** INLINE_FUNC(zName, nArg, iFuncId, mFlags)
  8855. +** zName is the name of a function that is implemented by in-line
  8856. +** byte code rather than by the usual callbacks. The iFuncId
  8857. +** parameter determines the function id. The mFlags parameter is
  8858. +** optional SQLITE_FUNC_ flags for this function.
  8859. +**
  8860. +** TEST_FUNC(zName, nArg, iFuncId, mFlags)
  8861. +** zName is the name of a test-only function implemented by in-line
  8862. +** byte code rather than by the usual callbacks. The iFuncId
  8863. +** parameter determines the function id. The mFlags parameter is
  8864. +** optional SQLITE_FUNC_ flags for this function.
  8865. +**
  8866. +** DFUNCTION(zName, nArg, iArg, bNC, xFunc)
  8867. +** Like FUNCTION except it omits the SQLITE_FUNC_CONSTANT flag and
  8868. +** adds the SQLITE_FUNC_SLOCHNG flag. Used for date & time functions
  8869. +** and functions like sqlite_version() that can change, but not during
  8870. +** a single query. The iArg is ignored. The user-data is always set
  8871. +** to a NULL pointer. The bNC parameter is not used.
  8872. +**
  8873. +** PURE_DATE(zName, nArg, iArg, bNC, xFunc)
  8874. +** Used for "pure" date/time functions, this macro is like DFUNCTION
  8875. +** except that it does set the SQLITE_FUNC_CONSTANT flags. iArg is
  8876. +** ignored and the user-data for these functions is set to an
  8877. +** arbitrary non-NULL pointer. The bNC parameter is not used.
  8878. +**
  8879. +** AGGREGATE(zName, nArg, iArg, bNC, xStep, xFinal)
  8880. +** Used to create an aggregate function definition implemented by
  8881. +** the C functions xStep and xFinal. The first four parameters
  8882. +** are interpreted in the same way as the first 4 parameters to
  8883. +** FUNCTION().
  8884. +**
  8885. +** WFUNCTION(zName, nArg, iArg, xStep, xFinal, xValue, xInverse)
  8886. +** Used to create an aggregate function definition implemented by
  8887. +** the C functions xStep and xFinal. The first four parameters
  8888. +** are interpreted in the same way as the first 4 parameters to
  8889. +** FUNCTION().
  8890. +**
  8891. +** LIKEFUNC(zName, nArg, pArg, flags)
  8892. +** Used to create a scalar function definition of a function zName
  8893. +** that accepts nArg arguments and is implemented by a call to C
  8894. +** function likeFunc. Argument pArg is cast to a (void *) and made
  8895. +** available as the function user-data (sqlite3_user_data()). The
  8896. +** FuncDef.flags variable is set to the value passed as the flags
  8897. +** parameter.
  8898. +*/
  8899. +#define FUNCTION(zName, nArg, iArg, bNC, xFunc) \
  8900. + {nArg, SQLITE_FUNC_CONSTANT|SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL), \
  8901. + SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, 0, #zName, {0} }
  8902. +#define VFUNCTION(zName, nArg, iArg, bNC, xFunc) \
  8903. + {nArg, SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL), \
  8904. + SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, 0, #zName, {0} }
  8905. +#define SFUNCTION(zName, nArg, iArg, bNC, xFunc) \
  8906. + {nArg, SQLITE_UTF8|SQLITE_DIRECTONLY|SQLITE_FUNC_UNSAFE, \
  8907. + SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, 0, #zName, {0} }
  8908. +#define INLINE_FUNC(zName, nArg, iArg, mFlags) \
  8909. + {nArg, SQLITE_UTF8|SQLITE_FUNC_INLINE|SQLITE_FUNC_CONSTANT|(mFlags), \
  8910. + SQLITE_INT_TO_PTR(iArg), 0, noopFunc, 0, 0, 0, #zName, {0} }
  8911. +#define TEST_FUNC(zName, nArg, iArg, mFlags) \
  8912. + {nArg, SQLITE_UTF8|SQLITE_FUNC_INTERNAL|SQLITE_FUNC_TEST| \
  8913. + SQLITE_FUNC_INLINE|SQLITE_FUNC_CONSTANT|(mFlags), \
  8914. + SQLITE_INT_TO_PTR(iArg), 0, noopFunc, 0, 0, 0, #zName, {0} }
  8915. +#define DFUNCTION(zName, nArg, iArg, bNC, xFunc) \
  8916. + {nArg, SQLITE_FUNC_SLOCHNG|SQLITE_UTF8, \
  8917. + 0, 0, xFunc, 0, 0, 0, #zName, {0} }
  8918. +#define PURE_DATE(zName, nArg, iArg, bNC, xFunc) \
  8919. + {nArg, SQLITE_FUNC_SLOCHNG|SQLITE_UTF8|SQLITE_FUNC_CONSTANT, \
  8920. + (void*)&sqlite3Config, 0, xFunc, 0, 0, 0, #zName, {0} }
  8921. +#define FUNCTION2(zName, nArg, iArg, bNC, xFunc, extraFlags) \
  8922. + {nArg,SQLITE_FUNC_CONSTANT|SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL)|extraFlags,\
  8923. + SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, 0, #zName, {0} }
  8924. +#define STR_FUNCTION(zName, nArg, pArg, bNC, xFunc) \
  8925. + {nArg, SQLITE_FUNC_SLOCHNG|SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL), \
  8926. + pArg, 0, xFunc, 0, 0, 0, #zName, }
  8927. +#define LIKEFUNC(zName, nArg, arg, flags) \
  8928. + {nArg, SQLITE_FUNC_CONSTANT|SQLITE_UTF8|flags, \
  8929. + (void *)arg, 0, likeFunc, 0, 0, 0, #zName, {0} }
  8930. +#define WAGGREGATE(zName, nArg, arg, nc, xStep, xFinal, xValue, xInverse, f) \
  8931. + {nArg, SQLITE_UTF8|(nc*SQLITE_FUNC_NEEDCOLL)|f, \
  8932. + SQLITE_INT_TO_PTR(arg), 0, xStep,xFinal,xValue,xInverse,#zName, {0}}
  8933. +#define INTERNAL_FUNCTION(zName, nArg, xFunc) \
  8934. + {nArg, SQLITE_FUNC_INTERNAL|SQLITE_UTF8|SQLITE_FUNC_CONSTANT, \
  8935. + 0, 0, xFunc, 0, 0, 0, #zName, {0} }
  8936. +
  8937. +
  8938. +/*
  8939. +** All current savepoints are stored in a linked list starting at
  8940. +** sqlite3.pSavepoint. The first element in the list is the most recently
  8941. +** opened savepoint. Savepoints are added to the list by the vdbe
  8942. +** OP_Savepoint instruction.
  8943. +*/
  8944. +struct Savepoint {
  8945. + char *zName; /* Savepoint name (nul-terminated) */
  8946. + i64 nDeferredCons; /* Number of deferred fk violations */
  8947. + i64 nDeferredImmCons; /* Number of deferred imm fk. */
  8948. + Savepoint *pNext; /* Parent savepoint (if any) */
  8949. +};
  8950. +
  8951. +/*
  8952. +** The following are used as the second parameter to sqlite3Savepoint(),
  8953. +** and as the P1 argument to the OP_Savepoint instruction.
  8954. +*/
  8955. +#define SAVEPOINT_BEGIN 0
  8956. +#define SAVEPOINT_RELEASE 1
  8957. +#define SAVEPOINT_ROLLBACK 2
  8958. +
  8959. +
  8960. +/*
  8961. +** Each SQLite module (virtual table definition) is defined by an
  8962. +** instance of the following structure, stored in the sqlite3.aModule
  8963. +** hash table.
  8964. +*/
  8965. +struct Module {
  8966. + const sqlite3_module *pModule; /* Callback pointers */
  8967. + const char *zName; /* Name passed to create_module() */
  8968. + int nRefModule; /* Number of pointers to this object */
  8969. + void *pAux; /* pAux passed to create_module() */
  8970. + void (*xDestroy)(void *); /* Module destructor function */
  8971. + Table *pEpoTab; /* Eponymous table for this module */
  8972. +};
  8973. +
  8974. +/*
  8975. +** Information about each column of an SQL table is held in an instance
  8976. +** of the Column structure, in the Table.aCol[] array.
  8977. +**
  8978. +** Definitions:
  8979. +**
  8980. +** "table column index" This is the index of the column in the
  8981. +** Table.aCol[] array, and also the index of
  8982. +** the column in the original CREATE TABLE stmt.
  8983. +**
  8984. +** "storage column index" This is the index of the column in the
  8985. +** record BLOB generated by the OP_MakeRecord
  8986. +** opcode. The storage column index is less than
  8987. +** or equal to the table column index. It is
  8988. +** equal if and only if there are no VIRTUAL
  8989. +** columns to the left.
  8990. +*/
  8991. +struct Column {
  8992. + char *zName; /* Name of this column, \000, then the type */
  8993. + Expr *pDflt; /* Default value or GENERATED ALWAYS AS value */
  8994. + char *zColl; /* Collating sequence. If NULL, use the default */
  8995. + u8 notNull; /* An OE_ code for handling a NOT NULL constraint */
  8996. + char affinity; /* One of the SQLITE_AFF_... values */
  8997. + u8 szEst; /* Estimated size of value in this column. sizeof(INT)==1 */
  8998. + u8 hName; /* Column name hash for faster lookup */
  8999. + u16 colFlags; /* Boolean properties. See COLFLAG_ defines below */
  9000. +};
  9001. +
  9002. +/* Allowed values for Column.colFlags:
  9003. +*/
  9004. +#define COLFLAG_PRIMKEY 0x0001 /* Column is part of the primary key */
  9005. +#define COLFLAG_HIDDEN 0x0002 /* A hidden column in a virtual table */
  9006. +#define COLFLAG_HASTYPE 0x0004 /* Type name follows column name */
  9007. +#define COLFLAG_UNIQUE 0x0008 /* Column def contains "UNIQUE" or "PK" */
  9008. +#define COLFLAG_SORTERREF 0x0010 /* Use sorter-refs with this column */
  9009. +#define COLFLAG_VIRTUAL 0x0020 /* GENERATED ALWAYS AS ... VIRTUAL */
  9010. +#define COLFLAG_STORED 0x0040 /* GENERATED ALWAYS AS ... STORED */
  9011. +#define COLFLAG_NOTAVAIL 0x0080 /* STORED column not yet calculated */
  9012. +#define COLFLAG_BUSY 0x0100 /* Blocks recursion on GENERATED columns */
  9013. +#define COLFLAG_GENERATED 0x0060 /* Combo: _STORED, _VIRTUAL */
  9014. +#define COLFLAG_NOINSERT 0x0062 /* Combo: _HIDDEN, _STORED, _VIRTUAL */
  9015. +
  9016. +/*
  9017. +** A "Collating Sequence" is defined by an instance of the following
  9018. +** structure. Conceptually, a collating sequence consists of a name and
  9019. +** a comparison routine that defines the order of that sequence.
  9020. +**
  9021. +** If CollSeq.xCmp is NULL, it means that the
  9022. +** collating sequence is undefined. Indices built on an undefined
  9023. +** collating sequence may not be read or written.
  9024. +*/
  9025. +struct CollSeq {
  9026. + char *zName; /* Name of the collating sequence, UTF-8 encoded */
  9027. + u8 enc; /* Text encoding handled by xCmp() */
  9028. + void *pUser; /* First argument to xCmp() */
  9029. + int (*xCmp)(void*,int, const void*, int, const void*);
  9030. + void (*xDel)(void*); /* Destructor for pUser */
  9031. +};
  9032. +
  9033. +/*
  9034. +** A sort order can be either ASC or DESC.
  9035. +*/
  9036. +#define SQLITE_SO_ASC 0 /* Sort in ascending order */
  9037. +#define SQLITE_SO_DESC 1 /* Sort in ascending order */
  9038. +#define SQLITE_SO_UNDEFINED -1 /* No sort order specified */
  9039. +
  9040. +/*
  9041. +** Column affinity types.
  9042. +**
  9043. +** These used to have mnemonic name like 'i' for SQLITE_AFF_INTEGER and
  9044. +** 't' for SQLITE_AFF_TEXT. But we can save a little space and improve
  9045. +** the speed a little by numbering the values consecutively.
  9046. +**
  9047. +** But rather than start with 0 or 1, we begin with 'A'. That way,
  9048. +** when multiple affinity types are concatenated into a string and
  9049. +** used as the P4 operand, they will be more readable.
  9050. +**
  9051. +** Note also that the numeric types are grouped together so that testing
  9052. +** for a numeric type is a single comparison. And the BLOB type is first.
  9053. +*/
  9054. +#define SQLITE_AFF_NONE 0x40 /* '@' */
  9055. +#define SQLITE_AFF_BLOB 0x41 /* 'A' */
  9056. +#define SQLITE_AFF_TEXT 0x42 /* 'B' */
  9057. +#define SQLITE_AFF_NUMERIC 0x43 /* 'C' */
  9058. +#define SQLITE_AFF_INTEGER 0x44 /* 'D' */
  9059. +#define SQLITE_AFF_REAL 0x45 /* 'E' */
  9060. +
  9061. +#define sqlite3IsNumericAffinity(X) ((X)>=SQLITE_AFF_NUMERIC)
  9062. +
  9063. +/*
  9064. +** The SQLITE_AFF_MASK values masks off the significant bits of an
  9065. +** affinity value.
  9066. +*/
  9067. +#define SQLITE_AFF_MASK 0x47
  9068. +
  9069. +/*
  9070. +** Additional bit values that can be ORed with an affinity without
  9071. +** changing the affinity.
  9072. +**
  9073. +** The SQLITE_NOTNULL flag is a combination of NULLEQ and JUMPIFNULL.
  9074. +** It causes an assert() to fire if either operand to a comparison
  9075. +** operator is NULL. It is added to certain comparison operators to
  9076. +** prove that the operands are always NOT NULL.
  9077. +*/
  9078. +#define SQLITE_KEEPNULL 0x08 /* Used by vector == or <> */
  9079. +#define SQLITE_JUMPIFNULL 0x10 /* jumps if either operand is NULL */
  9080. +#define SQLITE_STOREP2 0x20 /* Store result in reg[P2] rather than jump */
  9081. +#define SQLITE_NULLEQ 0x80 /* NULL=NULL */
  9082. +#define SQLITE_NOTNULL 0x90 /* Assert that operands are never NULL */
  9083. +
  9084. +/*
  9085. +** An object of this type is created for each virtual table present in
  9086. +** the database schema.
  9087. +**
  9088. +** If the database schema is shared, then there is one instance of this
  9089. +** structure for each database connection (sqlite3*) that uses the shared
  9090. +** schema. This is because each database connection requires its own unique
  9091. +** instance of the sqlite3_vtab* handle used to access the virtual table
  9092. +** implementation. sqlite3_vtab* handles can not be shared between
  9093. +** database connections, even when the rest of the in-memory database
  9094. +** schema is shared, as the implementation often stores the database
  9095. +** connection handle passed to it via the xConnect() or xCreate() method
  9096. +** during initialization internally. This database connection handle may
  9097. +** then be used by the virtual table implementation to access real tables
  9098. +** within the database. So that they appear as part of the callers
  9099. +** transaction, these accesses need to be made via the same database
  9100. +** connection as that used to execute SQL operations on the virtual table.
  9101. +**
  9102. +** All VTable objects that correspond to a single table in a shared
  9103. +** database schema are initially stored in a linked-list pointed to by
  9104. +** the Table.pVTable member variable of the corresponding Table object.
  9105. +** When an sqlite3_prepare() operation is required to access the virtual
  9106. +** table, it searches the list for the VTable that corresponds to the
  9107. +** database connection doing the preparing so as to use the correct
  9108. +** sqlite3_vtab* handle in the compiled query.
  9109. +**
  9110. +** When an in-memory Table object is deleted (for example when the
  9111. +** schema is being reloaded for some reason), the VTable objects are not
  9112. +** deleted and the sqlite3_vtab* handles are not xDisconnect()ed
  9113. +** immediately. Instead, they are moved from the Table.pVTable list to
  9114. +** another linked list headed by the sqlite3.pDisconnect member of the
  9115. +** corresponding sqlite3 structure. They are then deleted/xDisconnected
  9116. +** next time a statement is prepared using said sqlite3*. This is done
  9117. +** to avoid deadlock issues involving multiple sqlite3.mutex mutexes.
  9118. +** Refer to comments above function sqlite3VtabUnlockList() for an
  9119. +** explanation as to why it is safe to add an entry to an sqlite3.pDisconnect
  9120. +** list without holding the corresponding sqlite3.mutex mutex.
  9121. +**
  9122. +** The memory for objects of this type is always allocated by
  9123. +** sqlite3DbMalloc(), using the connection handle stored in VTable.db as
  9124. +** the first argument.
  9125. +*/
  9126. +struct VTable {
  9127. + sqlite3 *db; /* Database connection associated with this table */
  9128. + Module *pMod; /* Pointer to module implementation */
  9129. + sqlite3_vtab *pVtab; /* Pointer to vtab instance */
  9130. + int nRef; /* Number of pointers to this structure */
  9131. + u8 bConstraint; /* True if constraints are supported */
  9132. + u8 eVtabRisk; /* Riskiness of allowing hacker access */
  9133. + int iSavepoint; /* Depth of the SAVEPOINT stack */
  9134. + VTable *pNext; /* Next in linked list (see above) */
  9135. +};
  9136. +
  9137. +/* Allowed values for VTable.eVtabRisk
  9138. +*/
  9139. +#define SQLITE_VTABRISK_Low 0
  9140. +#define SQLITE_VTABRISK_Normal 1
  9141. +#define SQLITE_VTABRISK_High 2
  9142. +
  9143. +/*
  9144. +** The schema for each SQL table and view is represented in memory
  9145. +** by an instance of the following structure.
  9146. +*/
  9147. +struct Table {
  9148. + char *zName; /* Name of the table or view */
  9149. + Column *aCol; /* Information about each column */
  9150. + Index *pIndex; /* List of SQL indexes on this table. */
  9151. + Select *pSelect; /* NULL for tables. Points to definition if a view. */
  9152. + FKey *pFKey; /* Linked list of all foreign keys in this table */
  9153. + char *zColAff; /* String defining the affinity of each column */
  9154. + ExprList *pCheck; /* All CHECK constraints */
  9155. + /* ... also used as column name list in a VIEW */
  9156. + int tnum; /* Root BTree page for this table */
  9157. + u32 nTabRef; /* Number of pointers to this Table */
  9158. + u32 tabFlags; /* Mask of TF_* values */
  9159. + i16 iPKey; /* If not negative, use aCol[iPKey] as the rowid */
  9160. + i16 nCol; /* Number of columns in this table */
  9161. + i16 nNVCol; /* Number of columns that are not VIRTUAL */
  9162. + LogEst nRowLogEst; /* Estimated rows in table - from sqlite_stat1 table */
  9163. + LogEst szTabRow; /* Estimated size of each table row in bytes */
  9164. +#ifdef SQLITE_ENABLE_COSTMULT
  9165. + LogEst costMult; /* Cost multiplier for using this table */
  9166. +#endif
  9167. + u8 keyConf; /* What to do in case of uniqueness conflict on iPKey */
  9168. +#ifndef SQLITE_OMIT_ALTERTABLE
  9169. + int addColOffset; /* Offset in CREATE TABLE stmt to add a new column */
  9170. +#endif
  9171. +#ifndef SQLITE_OMIT_VIRTUALTABLE
  9172. + int nModuleArg; /* Number of arguments to the module */
  9173. + char **azModuleArg; /* 0: module 1: schema 2: vtab name 3...: args */
  9174. + VTable *pVTable; /* List of VTable objects. */
  9175. +#endif
  9176. + Trigger *pTrigger; /* List of triggers stored in pSchema */
  9177. + Schema *pSchema; /* Schema that contains this table */
  9178. + Table *pNextZombie; /* Next on the Parse.pZombieTab list */
  9179. +};
  9180. +
  9181. +/*
  9182. +** Allowed values for Table.tabFlags.
  9183. +**
  9184. +** TF_OOOHidden applies to tables or view that have hidden columns that are
  9185. +** followed by non-hidden columns. Example: "CREATE VIRTUAL TABLE x USING
  9186. +** vtab1(a HIDDEN, b);". Since "b" is a non-hidden column but "a" is hidden,
  9187. +** the TF_OOOHidden attribute would apply in this case. Such tables require
  9188. +** special handling during INSERT processing. The "OOO" means "Out Of Order".
  9189. +**
  9190. +** Constraints:
  9191. +**
  9192. +** TF_HasVirtual == COLFLAG_Virtual
  9193. +** TF_HasStored == COLFLAG_Stored
  9194. +*/
  9195. +#define TF_Readonly 0x0001 /* Read-only system table */
  9196. +#define TF_Ephemeral 0x0002 /* An ephemeral table */
  9197. +#define TF_HasPrimaryKey 0x0004 /* Table has a primary key */
  9198. +#define TF_Autoincrement 0x0008 /* Integer primary key is autoincrement */
  9199. +#define TF_HasStat1 0x0010 /* nRowLogEst set from sqlite_stat1 */
  9200. +#define TF_HasVirtual 0x0020 /* Has one or more VIRTUAL columns */
  9201. +#define TF_HasStored 0x0040 /* Has one or more STORED columns */
  9202. +#define TF_HasGenerated 0x0060 /* Combo: HasVirtual + HasStored */
  9203. +#define TF_WithoutRowid 0x0080 /* No rowid. PRIMARY KEY is the key */
  9204. +#define TF_StatsUsed 0x0100 /* Query planner decisions affected by
  9205. + ** Index.aiRowLogEst[] values */
  9206. +#define TF_NoVisibleRowid 0x0200 /* No user-visible "rowid" column */
  9207. +#define TF_OOOHidden 0x0400 /* Out-of-Order hidden columns */
  9208. +#define TF_HasNotNull 0x0800 /* Contains NOT NULL constraints */
  9209. +#define TF_Shadow 0x1000 /* True for a shadow table */
  9210. +
  9211. +/*
  9212. +** Test to see whether or not a table is a virtual table. This is
  9213. +** done as a macro so that it will be optimized out when virtual
  9214. +** table support is omitted from the build.
  9215. +*/
  9216. +#ifndef SQLITE_OMIT_VIRTUALTABLE
  9217. +# define IsVirtual(X) ((X)->nModuleArg)
  9218. +# define ExprIsVtab(X) \
  9219. + ((X)->op==TK_COLUMN && (X)->y.pTab!=0 && (X)->y.pTab->nModuleArg)
  9220. +#else
  9221. +# define IsVirtual(X) 0
  9222. +# define ExprIsVtab(X) 0
  9223. +#endif
  9224. +
  9225. +/*
  9226. +** Macros to determine if a column is hidden. IsOrdinaryHiddenColumn()
  9227. +** only works for non-virtual tables (ordinary tables and views) and is
  9228. +** always false unless SQLITE_ENABLE_HIDDEN_COLUMNS is defined. The
  9229. +** IsHiddenColumn() macro is general purpose.
  9230. +*/
  9231. +#if defined(SQLITE_ENABLE_HIDDEN_COLUMNS)
  9232. +# define IsHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0)
  9233. +# define IsOrdinaryHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0)
  9234. +#elif !defined(SQLITE_OMIT_VIRTUALTABLE)
  9235. +# define IsHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0)
  9236. +# define IsOrdinaryHiddenColumn(X) 0
  9237. +#else
  9238. +# define IsHiddenColumn(X) 0
  9239. +# define IsOrdinaryHiddenColumn(X) 0
  9240. +#endif
  9241. +
  9242. +
  9243. +/* Does the table have a rowid */
  9244. +#define HasRowid(X) (((X)->tabFlags & TF_WithoutRowid)==0)
  9245. +#define VisibleRowid(X) (((X)->tabFlags & TF_NoVisibleRowid)==0)
  9246. +
  9247. +/*
  9248. +** Each foreign key constraint is an instance of the following structure.
  9249. +**
  9250. +** A foreign key is associated with two tables. The "from" table is
  9251. +** the table that contains the REFERENCES clause that creates the foreign
  9252. +** key. The "to" table is the table that is named in the REFERENCES clause.
  9253. +** Consider this example:
  9254. +**
  9255. +** CREATE TABLE ex1(
  9256. +** a INTEGER PRIMARY KEY,
  9257. +** b INTEGER CONSTRAINT fk1 REFERENCES ex2(x)
  9258. +** );
  9259. +**
  9260. +** For foreign key "fk1", the from-table is "ex1" and the to-table is "ex2".
  9261. +** Equivalent names:
  9262. +**
  9263. +** from-table == child-table
  9264. +** to-table == parent-table
  9265. +**
  9266. +** Each REFERENCES clause generates an instance of the following structure
  9267. +** which is attached to the from-table. The to-table need not exist when
  9268. +** the from-table is created. The existence of the to-table is not checked.
  9269. +**
  9270. +** The list of all parents for child Table X is held at X.pFKey.
  9271. +**
  9272. +** A list of all children for a table named Z (which might not even exist)
  9273. +** is held in Schema.fkeyHash with a hash key of Z.
  9274. +*/
  9275. +struct FKey {
  9276. + Table *pFrom; /* Table containing the REFERENCES clause (aka: Child) */
  9277. + FKey *pNextFrom; /* Next FKey with the same in pFrom. Next parent of pFrom */
  9278. + char *zTo; /* Name of table that the key points to (aka: Parent) */
  9279. + FKey *pNextTo; /* Next with the same zTo. Next child of zTo. */
  9280. + FKey *pPrevTo; /* Previous with the same zTo */
  9281. + int nCol; /* Number of columns in this key */
  9282. + /* EV: R-30323-21917 */
  9283. + u8 isDeferred; /* True if constraint checking is deferred till COMMIT */
  9284. + u8 aAction[2]; /* ON DELETE and ON UPDATE actions, respectively */
  9285. + Trigger *apTrigger[2];/* Triggers for aAction[] actions */
  9286. + struct sColMap { /* Mapping of columns in pFrom to columns in zTo */
  9287. + int iFrom; /* Index of column in pFrom */
  9288. + char *zCol; /* Name of column in zTo. If NULL use PRIMARY KEY */
  9289. + } aCol[1]; /* One entry for each of nCol columns */
  9290. +};
  9291. +
  9292. +/*
  9293. +** SQLite supports many different ways to resolve a constraint
  9294. +** error. ROLLBACK processing means that a constraint violation
  9295. +** causes the operation in process to fail and for the current transaction
  9296. +** to be rolled back. ABORT processing means the operation in process
  9297. +** fails and any prior changes from that one operation are backed out,
  9298. +** but the transaction is not rolled back. FAIL processing means that
  9299. +** the operation in progress stops and returns an error code. But prior
  9300. +** changes due to the same operation are not backed out and no rollback
  9301. +** occurs. IGNORE means that the particular row that caused the constraint
  9302. +** error is not inserted or updated. Processing continues and no error
  9303. +** is returned. REPLACE means that preexisting database rows that caused
  9304. +** a UNIQUE constraint violation are removed so that the new insert or
  9305. +** update can proceed. Processing continues and no error is reported.
  9306. +**
  9307. +** RESTRICT, SETNULL, and CASCADE actions apply only to foreign keys.
  9308. +** RESTRICT is the same as ABORT for IMMEDIATE foreign keys and the
  9309. +** same as ROLLBACK for DEFERRED keys. SETNULL means that the foreign
  9310. +** key is set to NULL. CASCADE means that a DELETE or UPDATE of the
  9311. +** referenced table row is propagated into the row that holds the
  9312. +** foreign key.
  9313. +**
  9314. +** The following symbolic values are used to record which type
  9315. +** of action to take.
  9316. +*/
  9317. +#define OE_None 0 /* There is no constraint to check */
  9318. +#define OE_Rollback 1 /* Fail the operation and rollback the transaction */
  9319. +#define OE_Abort 2 /* Back out changes but do no rollback transaction */
  9320. +#define OE_Fail 3 /* Stop the operation but leave all prior changes */
  9321. +#define OE_Ignore 4 /* Ignore the error. Do not do the INSERT or UPDATE */
  9322. +#define OE_Replace 5 /* Delete existing record, then do INSERT or UPDATE */
  9323. +#define OE_Update 6 /* Process as a DO UPDATE in an upsert */
  9324. +#define OE_Restrict 7 /* OE_Abort for IMMEDIATE, OE_Rollback for DEFERRED */
  9325. +#define OE_SetNull 8 /* Set the foreign key value to NULL */
  9326. +#define OE_SetDflt 9 /* Set the foreign key value to its default */
  9327. +#define OE_Cascade 10 /* Cascade the changes */
  9328. +#define OE_Default 11 /* Do whatever the default action is */
  9329. +
  9330. +
  9331. +/*
  9332. +** An instance of the following structure is passed as the first
  9333. +** argument to sqlite3VdbeKeyCompare and is used to control the
  9334. +** comparison of the two index keys.
  9335. +**
  9336. +** Note that aSortOrder[] and aColl[] have nField+1 slots. There
  9337. +** are nField slots for the columns of an index then one extra slot
  9338. +** for the rowid at the end.
  9339. +*/
  9340. +struct KeyInfo {
  9341. + u32 nRef; /* Number of references to this KeyInfo object */
  9342. + u8 enc; /* Text encoding - one of the SQLITE_UTF* values */
  9343. + u16 nKeyField; /* Number of key columns in the index */
  9344. + u16 nAllField; /* Total columns, including key plus others */
  9345. + sqlite3 *db; /* The database connection */
  9346. + u8 *aSortFlags; /* Sort order for each column. */
  9347. + CollSeq *aColl[1]; /* Collating sequence for each term of the key */
  9348. +};
  9349. +
  9350. +/*
  9351. +** Allowed bit values for entries in the KeyInfo.aSortFlags[] array.
  9352. +*/
  9353. +#define KEYINFO_ORDER_DESC 0x01 /* DESC sort order */
  9354. +#define KEYINFO_ORDER_BIGNULL 0x02 /* NULL is larger than any other value */
  9355. +
  9356. +/*
  9357. +** This object holds a record which has been parsed out into individual
  9358. +** fields, for the purposes of doing a comparison.
  9359. +**
  9360. +** A record is an object that contains one or more fields of data.
  9361. +** Records are used to store the content of a table row and to store
  9362. +** the key of an index. A blob encoding of a record is created by
  9363. +** the OP_MakeRecord opcode of the VDBE and is disassembled by the
  9364. +** OP_Column opcode.
  9365. +**
  9366. +** An instance of this object serves as a "key" for doing a search on
  9367. +** an index b+tree. The goal of the search is to find the entry that
  9368. +** is closed to the key described by this object. This object might hold
  9369. +** just a prefix of the key. The number of fields is given by
  9370. +** pKeyInfo->nField.
  9371. +**
  9372. +** The r1 and r2 fields are the values to return if this key is less than
  9373. +** or greater than a key in the btree, respectively. These are normally
  9374. +** -1 and +1 respectively, but might be inverted to +1 and -1 if the b-tree
  9375. +** is in DESC order.
  9376. +**
  9377. +** The key comparison functions actually return default_rc when they find
  9378. +** an equals comparison. default_rc can be -1, 0, or +1. If there are
  9379. +** multiple entries in the b-tree with the same key (when only looking
  9380. +** at the first pKeyInfo->nFields,) then default_rc can be set to -1 to
  9381. +** cause the search to find the last match, or +1 to cause the search to
  9382. +** find the first match.
  9383. +**
  9384. +** The key comparison functions will set eqSeen to true if they ever
  9385. +** get and equal results when comparing this structure to a b-tree record.
  9386. +** When default_rc!=0, the search might end up on the record immediately
  9387. +** before the first match or immediately after the last match. The
  9388. +** eqSeen field will indicate whether or not an exact match exists in the
  9389. +** b-tree.
  9390. +*/
  9391. +struct UnpackedRecord {
  9392. + KeyInfo *pKeyInfo; /* Collation and sort-order information */
  9393. + Mem *aMem; /* Values */
  9394. + u16 nField; /* Number of entries in apMem[] */
  9395. + i8 default_rc; /* Comparison result if keys are equal */
  9396. + u8 errCode; /* Error detected by xRecordCompare (CORRUPT or NOMEM) */
  9397. + i8 r1; /* Value to return if (lhs < rhs) */
  9398. + i8 r2; /* Value to return if (lhs > rhs) */
  9399. + u8 eqSeen; /* True if an equality comparison has been seen */
  9400. +};
  9401. +
  9402. +
  9403. +/*
  9404. +** Each SQL index is represented in memory by an
  9405. +** instance of the following structure.
  9406. +**
  9407. +** The columns of the table that are to be indexed are described
  9408. +** by the aiColumn[] field of this structure. For example, suppose
  9409. +** we have the following table and index:
  9410. +**
  9411. +** CREATE TABLE Ex1(c1 int, c2 int, c3 text);
  9412. +** CREATE INDEX Ex2 ON Ex1(c3,c1);
  9413. +**
  9414. +** In the Table structure describing Ex1, nCol==3 because there are
  9415. +** three columns in the table. In the Index structure describing
  9416. +** Ex2, nColumn==2 since 2 of the 3 columns of Ex1 are indexed.
  9417. +** The value of aiColumn is {2, 0}. aiColumn[0]==2 because the
  9418. +** first column to be indexed (c3) has an index of 2 in Ex1.aCol[].
  9419. +** The second column to be indexed (c1) has an index of 0 in
  9420. +** Ex1.aCol[], hence Ex2.aiColumn[1]==0.
  9421. +**
  9422. +** The Index.onError field determines whether or not the indexed columns
  9423. +** must be unique and what to do if they are not. When Index.onError=OE_None,
  9424. +** it means this is not a unique index. Otherwise it is a unique index
  9425. +** and the value of Index.onError indicate the which conflict resolution
  9426. +** algorithm to employ whenever an attempt is made to insert a non-unique
  9427. +** element.
  9428. +**
  9429. +** While parsing a CREATE TABLE or CREATE INDEX statement in order to
  9430. +** generate VDBE code (as opposed to parsing one read from an sqlite_master
  9431. +** table as part of parsing an existing database schema), transient instances
  9432. +** of this structure may be created. In this case the Index.tnum variable is
  9433. +** used to store the address of a VDBE instruction, not a database page
  9434. +** number (it cannot - the database page is not allocated until the VDBE
  9435. +** program is executed). See convertToWithoutRowidTable() for details.
  9436. +*/
  9437. +struct Index {
  9438. + char *zName; /* Name of this index */
  9439. + i16 *aiColumn; /* Which columns are used by this index. 1st is 0 */
  9440. + LogEst *aiRowLogEst; /* From ANALYZE: Est. rows selected by each column */
  9441. + Table *pTable; /* The SQL table being indexed */
  9442. + char *zColAff; /* String defining the affinity of each column */
  9443. + Index *pNext; /* The next index associated with the same table */
  9444. + Schema *pSchema; /* Schema containing this index */
  9445. + u8 *aSortOrder; /* for each column: True==DESC, False==ASC */
  9446. + const char **azColl; /* Array of collation sequence names for index */
  9447. + Expr *pPartIdxWhere; /* WHERE clause for partial indices */
  9448. + ExprList *aColExpr; /* Column expressions */
  9449. + int tnum; /* DB Page containing root of this index */
  9450. + LogEst szIdxRow; /* Estimated average row size in bytes */
  9451. + u16 nKeyCol; /* Number of columns forming the key */
  9452. + u16 nColumn; /* Number of columns stored in the index */
  9453. + u8 onError; /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
  9454. + unsigned idxType:2; /* 0:Normal 1:UNIQUE, 2:PRIMARY KEY, 3:IPK */
  9455. + unsigned bUnordered:1; /* Use this index for == or IN queries only */
  9456. + unsigned uniqNotNull:1; /* True if UNIQUE and NOT NULL for all columns */
  9457. + unsigned isResized:1; /* True if resizeIndexObject() has been called */
  9458. + unsigned isCovering:1; /* True if this is a covering index */
  9459. + unsigned noSkipScan:1; /* Do not try to use skip-scan if true */
  9460. + unsigned hasStat1:1; /* aiRowLogEst values come from sqlite_stat1 */
  9461. + unsigned bNoQuery:1; /* Do not use this index to optimize queries */
  9462. + unsigned bAscKeyBug:1; /* True if the bba7b69f9849b5bf bug applies */
  9463. + unsigned bHasVCol:1; /* Index references one or more VIRTUAL columns */
  9464. +#ifdef SQLITE_ENABLE_STAT4
  9465. + int nSample; /* Number of elements in aSample[] */
  9466. + int nSampleCol; /* Size of IndexSample.anEq[] and so on */
  9467. + tRowcnt *aAvgEq; /* Average nEq values for keys not in aSample */
  9468. + IndexSample *aSample; /* Samples of the left-most key */
  9469. + tRowcnt *aiRowEst; /* Non-logarithmic stat1 data for this index */
  9470. + tRowcnt nRowEst0; /* Non-logarithmic number of rows in the index */
  9471. +#endif
  9472. + Bitmask colNotIdxed; /* 0 for unindexed columns in pTab */
  9473. +};
  9474. +
  9475. +/*
  9476. +** Allowed values for Index.idxType
  9477. +*/
  9478. +#define SQLITE_IDXTYPE_APPDEF 0 /* Created using CREATE INDEX */
  9479. +#define SQLITE_IDXTYPE_UNIQUE 1 /* Implements a UNIQUE constraint */
  9480. +#define SQLITE_IDXTYPE_PRIMARYKEY 2 /* Is the PRIMARY KEY for the table */
  9481. +#define SQLITE_IDXTYPE_IPK 3 /* INTEGER PRIMARY KEY index */
  9482. +
  9483. +/* Return true if index X is a PRIMARY KEY index */
  9484. +#define IsPrimaryKeyIndex(X) ((X)->idxType==SQLITE_IDXTYPE_PRIMARYKEY)
  9485. +
  9486. +/* Return true if index X is a UNIQUE index */
  9487. +#define IsUniqueIndex(X) ((X)->onError!=OE_None)
  9488. +
  9489. +/* The Index.aiColumn[] values are normally positive integer. But
  9490. +** there are some negative values that have special meaning:
  9491. +*/
  9492. +#define XN_ROWID (-1) /* Indexed column is the rowid */
  9493. +#define XN_EXPR (-2) /* Indexed column is an expression */
  9494. +
  9495. +/*
  9496. +** Each sample stored in the sqlite_stat4 table is represented in memory
  9497. +** using a structure of this type. See documentation at the top of the
  9498. +** analyze.c source file for additional information.
  9499. +*/
  9500. +struct IndexSample {
  9501. + void *p; /* Pointer to sampled record */
  9502. + int n; /* Size of record in bytes */
  9503. + tRowcnt *anEq; /* Est. number of rows where the key equals this sample */
  9504. + tRowcnt *anLt; /* Est. number of rows where key is less than this sample */
  9505. + tRowcnt *anDLt; /* Est. number of distinct keys less than this sample */
  9506. +};
  9507. +
  9508. +/*
  9509. +** Possible values to use within the flags argument to sqlite3GetToken().
  9510. +*/
  9511. +#define SQLITE_TOKEN_QUOTED 0x1 /* Token is a quoted identifier. */
  9512. +#define SQLITE_TOKEN_KEYWORD 0x2 /* Token is a keyword. */
  9513. +
  9514. +/*
  9515. +** Each token coming out of the lexer is an instance of
  9516. +** this structure. Tokens are also used as part of an expression.
  9517. +**
  9518. +** The memory that "z" points to is owned by other objects. Take care
  9519. +** that the owner of the "z" string does not deallocate the string before
  9520. +** the Token goes out of scope! Very often, the "z" points to some place
  9521. +** in the middle of the Parse.zSql text. But it might also point to a
  9522. +** static string.
  9523. +*/
  9524. +struct Token {
  9525. + const char *z; /* Text of the token. Not NULL-terminated! */
  9526. + unsigned int n; /* Number of characters in this token */
  9527. +};
  9528. +
  9529. +/*
  9530. +** An instance of this structure contains information needed to generate
  9531. +** code for a SELECT that contains aggregate functions.
  9532. +**
  9533. +** If Expr.op==TK_AGG_COLUMN or TK_AGG_FUNCTION then Expr.pAggInfo is a
  9534. +** pointer to this structure. The Expr.iAgg field is the index in
  9535. +** AggInfo.aCol[] or AggInfo.aFunc[] of information needed to generate
  9536. +** code for that node.
  9537. +**
  9538. +** AggInfo.pGroupBy and AggInfo.aFunc.pExpr point to fields within the
  9539. +** original Select structure that describes the SELECT statement. These
  9540. +** fields do not need to be freed when deallocating the AggInfo structure.
  9541. +*/
  9542. +struct AggInfo {
  9543. + u8 directMode; /* Direct rendering mode means take data directly
  9544. + ** from source tables rather than from accumulators */
  9545. + u8 useSortingIdx; /* In direct mode, reference the sorting index rather
  9546. + ** than the source table */
  9547. + int sortingIdx; /* Cursor number of the sorting index */
  9548. + int sortingIdxPTab; /* Cursor number of pseudo-table */
  9549. + int nSortingColumn; /* Number of columns in the sorting index */
  9550. + int mnReg, mxReg; /* Range of registers allocated for aCol and aFunc */
  9551. + ExprList *pGroupBy; /* The group by clause */
  9552. + struct AggInfo_col { /* For each column used in source tables */
  9553. + Table *pTab; /* Source table */
  9554. + int iTable; /* Cursor number of the source table */
  9555. + int iColumn; /* Column number within the source table */
  9556. + int iSorterColumn; /* Column number in the sorting index */
  9557. + int iMem; /* Memory location that acts as accumulator */
  9558. + Expr *pExpr; /* The original expression */
  9559. + } *aCol;
  9560. + int nColumn; /* Number of used entries in aCol[] */
  9561. + int nAccumulator; /* Number of columns that show through to the output.
  9562. + ** Additional columns are used only as parameters to
  9563. + ** aggregate functions */
  9564. + struct AggInfo_func { /* For each aggregate function */
  9565. + Expr *pExpr; /* Expression encoding the function */
  9566. + FuncDef *pFunc; /* The aggregate function implementation */
  9567. + int iMem; /* Memory location that acts as accumulator */
  9568. + int iDistinct; /* Ephemeral table used to enforce DISTINCT */
  9569. + } *aFunc;
  9570. + int nFunc; /* Number of entries in aFunc[] */
  9571. +#ifdef SQLITE_DEBUG
  9572. + u32 iAggMagic; /* Sanity checking constant */
  9573. +#endif
  9574. +};
  9575. +
  9576. +/*
  9577. +** Allowed values for AggInfo.iAggMagic
  9578. +*/
  9579. +#define SQLITE_AGGMAGIC_VALID 0x05cadade
  9580. +
  9581. +/*
  9582. +** True if the AggInfo object is valid. Used inside of assert() only.
  9583. +*/
  9584. +#ifdef SQLITE_DEBUG
  9585. +# define AggInfoValid(P) ((P)->iAggMagic==SQLITE_AGGMAGIC_VALID)
  9586. +#endif
  9587. +
  9588. +/*
  9589. +** The datatype ynVar is a signed integer, either 16-bit or 32-bit.
  9590. +** Usually it is 16-bits. But if SQLITE_MAX_VARIABLE_NUMBER is greater
  9591. +** than 32767 we have to make it 32-bit. 16-bit is preferred because
  9592. +** it uses less memory in the Expr object, which is a big memory user
  9593. +** in systems with lots of prepared statements. And few applications
  9594. +** need more than about 10 or 20 variables. But some extreme users want
  9595. +** to have prepared statements with over 32766 variables, and for them
  9596. +** the option is available (at compile-time).
  9597. +*/
  9598. +#if SQLITE_MAX_VARIABLE_NUMBER<32767
  9599. +typedef i16 ynVar;
  9600. +#else
  9601. +typedef int ynVar;
  9602. +#endif
  9603. +
  9604. +/*
  9605. +** Each node of an expression in the parse tree is an instance
  9606. +** of this structure.
  9607. +**
  9608. +** Expr.op is the opcode. The integer parser token codes are reused
  9609. +** as opcodes here. For example, the parser defines TK_GE to be an integer
  9610. +** code representing the ">=" operator. This same integer code is reused
  9611. +** to represent the greater-than-or-equal-to operator in the expression
  9612. +** tree.
  9613. +**
  9614. +** If the expression is an SQL literal (TK_INTEGER, TK_FLOAT, TK_BLOB,
  9615. +** or TK_STRING), then Expr.token contains the text of the SQL literal. If
  9616. +** the expression is a variable (TK_VARIABLE), then Expr.token contains the
  9617. +** variable name. Finally, if the expression is an SQL function (TK_FUNCTION),
  9618. +** then Expr.token contains the name of the function.
  9619. +**
  9620. +** Expr.pRight and Expr.pLeft are the left and right subexpressions of a
  9621. +** binary operator. Either or both may be NULL.
  9622. +**
  9623. +** Expr.x.pList is a list of arguments if the expression is an SQL function,
  9624. +** a CASE expression or an IN expression of the form "<lhs> IN (<y>, <z>...)".
  9625. +** Expr.x.pSelect is used if the expression is a sub-select or an expression of
  9626. +** the form "<lhs> IN (SELECT ...)". If the EP_xIsSelect bit is set in the
  9627. +** Expr.flags mask, then Expr.x.pSelect is valid. Otherwise, Expr.x.pList is
  9628. +** valid.
  9629. +**
  9630. +** An expression of the form ID or ID.ID refers to a column in a table.
  9631. +** For such expressions, Expr.op is set to TK_COLUMN and Expr.iTable is
  9632. +** the integer cursor number of a VDBE cursor pointing to that table and
  9633. +** Expr.iColumn is the column number for the specific column. If the
  9634. +** expression is used as a result in an aggregate SELECT, then the
  9635. +** value is also stored in the Expr.iAgg column in the aggregate so that
  9636. +** it can be accessed after all aggregates are computed.
  9637. +**
  9638. +** If the expression is an unbound variable marker (a question mark
  9639. +** character '?' in the original SQL) then the Expr.iTable holds the index
  9640. +** number for that variable.
  9641. +**
  9642. +** If the expression is a subquery then Expr.iColumn holds an integer
  9643. +** register number containing the result of the subquery. If the
  9644. +** subquery gives a constant result, then iTable is -1. If the subquery
  9645. +** gives a different answer at different times during statement processing
  9646. +** then iTable is the address of a subroutine that computes the subquery.
  9647. +**
  9648. +** If the Expr is of type OP_Column, and the table it is selecting from
  9649. +** is a disk table or the "old.*" pseudo-table, then pTab points to the
  9650. +** corresponding table definition.
  9651. +**
  9652. +** ALLOCATION NOTES:
  9653. +**
  9654. +** Expr objects can use a lot of memory space in database schema. To
  9655. +** help reduce memory requirements, sometimes an Expr object will be
  9656. +** truncated. And to reduce the number of memory allocations, sometimes
  9657. +** two or more Expr objects will be stored in a single memory allocation,
  9658. +** together with Expr.zToken strings.
  9659. +**
  9660. +** If the EP_Reduced and EP_TokenOnly flags are set when
  9661. +** an Expr object is truncated. When EP_Reduced is set, then all
  9662. +** the child Expr objects in the Expr.pLeft and Expr.pRight subtrees
  9663. +** are contained within the same memory allocation. Note, however, that
  9664. +** the subtrees in Expr.x.pList or Expr.x.pSelect are always separately
  9665. +** allocated, regardless of whether or not EP_Reduced is set.
  9666. +*/
  9667. +struct Expr {
  9668. + u8 op; /* Operation performed by this node */
  9669. + char affExpr; /* affinity, or RAISE type */
  9670. + u8 op2; /* TK_REGISTER/TK_TRUTH: original value of Expr.op
  9671. + ** TK_COLUMN: the value of p5 for OP_Column
  9672. + ** TK_AGG_FUNCTION: nesting depth
  9673. + ** TK_FUNCTION: NC_SelfRef flag if needs OP_PureFunc */
  9674. +#ifdef SQLITE_DEBUG
  9675. + u8 vvaFlags; /* Verification flags. */
  9676. +#endif
  9677. + u32 flags; /* Various flags. EP_* See below */
  9678. + union {
  9679. + char *zToken; /* Token value. Zero terminated and dequoted */
  9680. + int iValue; /* Non-negative integer value if EP_IntValue */
  9681. + } u;
  9682. +
  9683. + /* If the EP_TokenOnly flag is set in the Expr.flags mask, then no
  9684. + ** space is allocated for the fields below this point. An attempt to
  9685. + ** access them will result in a segfault or malfunction.
  9686. + *********************************************************************/
  9687. +
  9688. + Expr *pLeft; /* Left subnode */
  9689. + Expr *pRight; /* Right subnode */
  9690. + union {
  9691. + ExprList *pList; /* op = IN, EXISTS, SELECT, CASE, FUNCTION, BETWEEN */
  9692. + Select *pSelect; /* EP_xIsSelect and op = IN, EXISTS, SELECT */
  9693. + } x;
  9694. +
  9695. + /* If the EP_Reduced flag is set in the Expr.flags mask, then no
  9696. + ** space is allocated for the fields below this point. An attempt to
  9697. + ** access them will result in a segfault or malfunction.
  9698. + *********************************************************************/
  9699. +
  9700. +#if SQLITE_MAX_EXPR_DEPTH>0
  9701. + int nHeight; /* Height of the tree headed by this node */
  9702. +#endif
  9703. + int iTable; /* TK_COLUMN: cursor number of table holding column
  9704. + ** TK_REGISTER: register number
  9705. + ** TK_TRIGGER: 1 -> new, 0 -> old
  9706. + ** EP_Unlikely: 134217728 times likelihood
  9707. + ** TK_IN: ephemerial table holding RHS
  9708. + ** TK_SELECT_COLUMN: Number of columns on the LHS
  9709. + ** TK_SELECT: 1st register of result vector */
  9710. + ynVar iColumn; /* TK_COLUMN: column index. -1 for rowid.
  9711. + ** TK_VARIABLE: variable number (always >= 1).
  9712. + ** TK_SELECT_COLUMN: column of the result vector */
  9713. + i16 iAgg; /* Which entry in pAggInfo->aCol[] or ->aFunc[] */
  9714. + i16 iRightJoinTable; /* If EP_FromJoin, the right table of the join */
  9715. + AggInfo *pAggInfo; /* Used by TK_AGG_COLUMN and TK_AGG_FUNCTION */
  9716. + union {
  9717. + Table *pTab; /* TK_COLUMN: Table containing column. Can be NULL
  9718. + ** for a column of an index on an expression */
  9719. + Window *pWin; /* EP_WinFunc: Window/Filter defn for a function */
  9720. + struct { /* TK_IN, TK_SELECT, and TK_EXISTS */
  9721. + int iAddr; /* Subroutine entry address */
  9722. + int regReturn; /* Register used to hold return address */
  9723. + } sub;
  9724. + } y;
  9725. +};
  9726. +
  9727. +/*
  9728. +** The following are the meanings of bits in the Expr.flags field.
  9729. +** Value restrictions:
  9730. +**
  9731. +** EP_Agg == NC_HasAgg == SF_HasAgg
  9732. +** EP_Win == NC_HasWin
  9733. +*/
  9734. +#define EP_FromJoin 0x000001 /* Originates in ON/USING clause of outer join */
  9735. +#define EP_Distinct 0x000002 /* Aggregate function with DISTINCT keyword */
  9736. +#define EP_HasFunc 0x000004 /* Contains one or more functions of any kind */
  9737. +#define EP_FixedCol 0x000008 /* TK_Column with a known fixed value */
  9738. +#define EP_Agg 0x000010 /* Contains one or more aggregate functions */
  9739. +#define EP_VarSelect 0x000020 /* pSelect is correlated, not constant */
  9740. +#define EP_DblQuoted 0x000040 /* token.z was originally in "..." */
  9741. +#define EP_InfixFunc 0x000080 /* True for an infix function: LIKE, GLOB, etc */
  9742. +#define EP_Collate 0x000100 /* Tree contains a TK_COLLATE operator */
  9743. +#define EP_Commuted 0x000200 /* Comparison operator has been commuted */
  9744. +#define EP_IntValue 0x000400 /* Integer value contained in u.iValue */
  9745. +#define EP_xIsSelect 0x000800 /* x.pSelect is valid (otherwise x.pList is) */
  9746. +#define EP_Skip 0x001000 /* Operator does not contribute to affinity */
  9747. +#define EP_Reduced 0x002000 /* Expr struct EXPR_REDUCEDSIZE bytes only */
  9748. +#define EP_TokenOnly 0x004000 /* Expr struct EXPR_TOKENONLYSIZE bytes only */
  9749. +#define EP_Win 0x008000 /* Contains window functions */
  9750. +#define EP_MemToken 0x010000 /* Need to sqlite3DbFree() Expr.zToken */
  9751. + /* 0x020000 // available for reuse */
  9752. +#define EP_Unlikely 0x040000 /* unlikely() or likelihood() function */
  9753. +#define EP_ConstFunc 0x080000 /* A SQLITE_FUNC_CONSTANT or _SLOCHNG function */
  9754. +#define EP_CanBeNull 0x100000 /* Can be null despite NOT NULL constraint */
  9755. +#define EP_Subquery 0x200000 /* Tree contains a TK_SELECT operator */
  9756. +#define EP_Alias 0x400000 /* Is an alias for a result set column */
  9757. +#define EP_Leaf 0x800000 /* Expr.pLeft, .pRight, .u.pSelect all NULL */
  9758. +#define EP_WinFunc 0x1000000 /* TK_FUNCTION with Expr.y.pWin set */
  9759. +#define EP_Subrtn 0x2000000 /* Uses Expr.y.sub. TK_IN, _SELECT, or _EXISTS */
  9760. +#define EP_Quoted 0x4000000 /* TK_ID was originally quoted */
  9761. +#define EP_Static 0x8000000 /* Held in memory not obtained from malloc() */
  9762. +#define EP_IsTrue 0x10000000 /* Always has boolean value of TRUE */
  9763. +#define EP_IsFalse 0x20000000 /* Always has boolean value of FALSE */
  9764. +#define EP_FromDDL 0x40000000 /* Originates from sqlite_master */
  9765. + /* 0x80000000 // Available */
  9766. +
  9767. +/*
  9768. +** The EP_Propagate mask is a set of properties that automatically propagate
  9769. +** upwards into parent nodes.
  9770. +*/
  9771. +#define EP_Propagate (EP_Collate|EP_Subquery|EP_HasFunc)
  9772. +
  9773. +/*
  9774. +** These macros can be used to test, set, or clear bits in the
  9775. +** Expr.flags field.
  9776. +*/
  9777. +#define ExprHasProperty(E,P) (((E)->flags&(P))!=0)
  9778. +#define ExprHasAllProperty(E,P) (((E)->flags&(P))==(P))
  9779. +#define ExprSetProperty(E,P) (E)->flags|=(P)
  9780. +#define ExprClearProperty(E,P) (E)->flags&=~(P)
  9781. +#define ExprAlwaysTrue(E) (((E)->flags&(EP_FromJoin|EP_IsTrue))==EP_IsTrue)
  9782. +#define ExprAlwaysFalse(E) (((E)->flags&(EP_FromJoin|EP_IsFalse))==EP_IsFalse)
  9783. +
  9784. +
  9785. +/* Flags for use with Expr.vvaFlags
  9786. +*/
  9787. +#define EP_NoReduce 0x01 /* Cannot EXPRDUP_REDUCE this Expr */
  9788. +#define EP_Immutable 0x02 /* Do not change this Expr node */
  9789. +
  9790. +/* The ExprSetVVAProperty() macro is used for Verification, Validation,
  9791. +** and Accreditation only. It works like ExprSetProperty() during VVA
  9792. +** processes but is a no-op for delivery.
  9793. +*/
  9794. +#ifdef SQLITE_DEBUG
  9795. +# define ExprSetVVAProperty(E,P) (E)->vvaFlags|=(P)
  9796. +# define ExprHasVVAProperty(E,P) (((E)->vvaFlags&(P))!=0)
  9797. +# define ExprClearVVAProperties(E) (E)->vvaFlags = 0
  9798. +#else
  9799. +# define ExprSetVVAProperty(E,P)
  9800. +# define ExprHasVVAProperty(E,P) 0
  9801. +# define ExprClearVVAProperties(E)
  9802. +#endif
  9803. +
  9804. +/*
  9805. +** Macros to determine the number of bytes required by a normal Expr
  9806. +** struct, an Expr struct with the EP_Reduced flag set in Expr.flags
  9807. +** and an Expr struct with the EP_TokenOnly flag set.
  9808. +*/
  9809. +#define EXPR_FULLSIZE sizeof(Expr) /* Full size */
  9810. +#define EXPR_REDUCEDSIZE offsetof(Expr,iTable) /* Common features */
  9811. +#define EXPR_TOKENONLYSIZE offsetof(Expr,pLeft) /* Fewer features */
  9812. +
  9813. +/*
  9814. +** Flags passed to the sqlite3ExprDup() function. See the header comment
  9815. +** above sqlite3ExprDup() for details.
  9816. +*/
  9817. +#define EXPRDUP_REDUCE 0x0001 /* Used reduced-size Expr nodes */
  9818. +
  9819. +/*
  9820. +** True if the expression passed as an argument was a function with
  9821. +** an OVER() clause (a window function).
  9822. +*/
  9823. +#ifdef SQLITE_OMIT_WINDOWFUNC
  9824. +# define IsWindowFunc(p) 0
  9825. +#else
  9826. +# define IsWindowFunc(p) ( \
  9827. + ExprHasProperty((p), EP_WinFunc) && p->y.pWin->eFrmType!=TK_FILTER \
  9828. + )
  9829. +#endif
  9830. +
  9831. +/*
  9832. +** A list of expressions. Each expression may optionally have a
  9833. +** name. An expr/name combination can be used in several ways, such
  9834. +** as the list of "expr AS ID" fields following a "SELECT" or in the
  9835. +** list of "ID = expr" items in an UPDATE. A list of expressions can
  9836. +** also be used as the argument to a function, in which case the a.zName
  9837. +** field is not used.
  9838. +**
  9839. +** In order to try to keep memory usage down, the Expr.a.zEName field
  9840. +** is used for multiple purposes:
  9841. +**
  9842. +** eEName Usage
  9843. +** ---------- -------------------------
  9844. +** ENAME_NAME (1) the AS of result set column
  9845. +** (2) COLUMN= of an UPDATE
  9846. +**
  9847. +** ENAME_TAB DB.TABLE.NAME used to resolve names
  9848. +** of subqueries
  9849. +**
  9850. +** ENAME_SPAN Text of the original result set
  9851. +** expression.
  9852. +*/
  9853. +struct ExprList {
  9854. + int nExpr; /* Number of expressions on the list */
  9855. + struct ExprList_item { /* For each expression in the list */
  9856. + Expr *pExpr; /* The parse tree for this expression */
  9857. + char *zEName; /* Token associated with this expression */
  9858. + u8 sortFlags; /* Mask of KEYINFO_ORDER_* flags */
  9859. + unsigned eEName :2; /* Meaning of zEName */
  9860. + unsigned done :1; /* A flag to indicate when processing is finished */
  9861. + unsigned reusable :1; /* Constant expression is reusable */
  9862. + unsigned bSorterRef :1; /* Defer evaluation until after sorting */
  9863. + unsigned bNulls: 1; /* True if explicit "NULLS FIRST/LAST" */
  9864. + union {
  9865. + struct {
  9866. + u16 iOrderByCol; /* For ORDER BY, column number in result set */
  9867. + u16 iAlias; /* Index into Parse.aAlias[] for zName */
  9868. + } x;
  9869. + int iConstExprReg; /* Register in which Expr value is cached */
  9870. + } u;
  9871. + } a[1]; /* One slot for each expression in the list */
  9872. +};
  9873. +
  9874. +/*
  9875. +** Allowed values for Expr.a.eEName
  9876. +*/
  9877. +#define ENAME_NAME 0 /* The AS clause of a result set */
  9878. +#define ENAME_SPAN 1 /* Complete text of the result set expression */
  9879. +#define ENAME_TAB 2 /* "DB.TABLE.NAME" for the result set */
  9880. +
  9881. +/*
  9882. +** An instance of this structure can hold a simple list of identifiers,
  9883. +** such as the list "a,b,c" in the following statements:
  9884. +**
  9885. +** INSERT INTO t(a,b,c) VALUES ...;
  9886. +** CREATE INDEX idx ON t(a,b,c);
  9887. +** CREATE TRIGGER trig BEFORE UPDATE ON t(a,b,c) ...;
  9888. +**
  9889. +** The IdList.a.idx field is used when the IdList represents the list of
  9890. +** column names after a table name in an INSERT statement. In the statement
  9891. +**
  9892. +** INSERT INTO t(a,b,c) ...
  9893. +**
  9894. +** If "a" is the k-th column of table "t", then IdList.a[0].idx==k.
  9895. +*/
  9896. +struct IdList {
  9897. + struct IdList_item {
  9898. + char *zName; /* Name of the identifier */
  9899. + int idx; /* Index in some Table.aCol[] of a column named zName */
  9900. + } *a;
  9901. + int nId; /* Number of identifiers on the list */
  9902. +};
  9903. +
  9904. +/*
  9905. +** The following structure describes the FROM clause of a SELECT statement.
  9906. +** Each table or subquery in the FROM clause is a separate element of
  9907. +** the SrcList.a[] array.
  9908. +**
  9909. +** With the addition of multiple database support, the following structure
  9910. +** can also be used to describe a particular table such as the table that
  9911. +** is modified by an INSERT, DELETE, or UPDATE statement. In standard SQL,
  9912. +** such a table must be a simple name: ID. But in SQLite, the table can
  9913. +** now be identified by a database name, a dot, then the table name: ID.ID.
  9914. +**
  9915. +** The jointype starts out showing the join type between the current table
  9916. +** and the next table on the list. The parser builds the list this way.
  9917. +** But sqlite3SrcListShiftJoinType() later shifts the jointypes so that each
  9918. +** jointype expresses the join between the table and the previous table.
  9919. +**
  9920. +** In the colUsed field, the high-order bit (bit 63) is set if the table
  9921. +** contains more than 63 columns and the 64-th or later column is used.
  9922. +*/
  9923. +struct SrcList {
  9924. + int nSrc; /* Number of tables or subqueries in the FROM clause */
  9925. + u32 nAlloc; /* Number of entries allocated in a[] below */
  9926. + struct SrcList_item {
  9927. + Schema *pSchema; /* Schema to which this item is fixed */
  9928. + char *zDatabase; /* Name of database holding this table */
  9929. + char *zName; /* Name of the table */
  9930. + char *zAlias; /* The "B" part of a "A AS B" phrase. zName is the "A" */
  9931. + Table *pTab; /* An SQL table corresponding to zName */
  9932. + Select *pSelect; /* A SELECT statement used in place of a table name */
  9933. + int addrFillSub; /* Address of subroutine to manifest a subquery */
  9934. + int regReturn; /* Register holding return address of addrFillSub */
  9935. + int regResult; /* Registers holding results of a co-routine */
  9936. + struct {
  9937. + u8 jointype; /* Type of join between this table and the previous */
  9938. + unsigned notIndexed :1; /* True if there is a NOT INDEXED clause */
  9939. + unsigned isIndexedBy :1; /* True if there is an INDEXED BY clause */
  9940. + unsigned isTabFunc :1; /* True if table-valued-function syntax */
  9941. + unsigned isCorrelated :1; /* True if sub-query is correlated */
  9942. + unsigned viaCoroutine :1; /* Implemented as a co-routine */
  9943. + unsigned isRecursive :1; /* True for recursive reference in WITH */
  9944. + unsigned fromDDL :1; /* Comes from sqlite_master */
  9945. + } fg;
  9946. + int iCursor; /* The VDBE cursor number used to access this table */
  9947. + Expr *pOn; /* The ON clause of a join */
  9948. + IdList *pUsing; /* The USING clause of a join */
  9949. + Bitmask colUsed; /* Bit N (1<<N) set if column N of pTab is used */
  9950. + union {
  9951. + char *zIndexedBy; /* Identifier from "INDEXED BY <zIndex>" clause */
  9952. + ExprList *pFuncArg; /* Arguments to table-valued-function */
  9953. + } u1;
  9954. + Index *pIBIndex; /* Index structure corresponding to u1.zIndexedBy */
  9955. + } a[1]; /* One entry for each identifier on the list */
  9956. +};
  9957. +
  9958. +/*
  9959. +** Permitted values of the SrcList.a.jointype field
  9960. +*/
  9961. +#define JT_INNER 0x0001 /* Any kind of inner or cross join */
  9962. +#define JT_CROSS 0x0002 /* Explicit use of the CROSS keyword */
  9963. +#define JT_NATURAL 0x0004 /* True for a "natural" join */
  9964. +#define JT_LEFT 0x0008 /* Left outer join */
  9965. +#define JT_RIGHT 0x0010 /* Right outer join */
  9966. +#define JT_OUTER 0x0020 /* The "OUTER" keyword is present */
  9967. +#define JT_ERROR 0x0040 /* unknown or unsupported join type */
  9968. +
  9969. +
  9970. +/*
  9971. +** Flags appropriate for the wctrlFlags parameter of sqlite3WhereBegin()
  9972. +** and the WhereInfo.wctrlFlags member.
  9973. +**
  9974. +** Value constraints (enforced via assert()):
  9975. +** WHERE_USE_LIMIT == SF_FixedLimit
  9976. +*/
  9977. +#define WHERE_ORDERBY_NORMAL 0x0000 /* No-op */
  9978. +#define WHERE_ORDERBY_MIN 0x0001 /* ORDER BY processing for min() func */
  9979. +#define WHERE_ORDERBY_MAX 0x0002 /* ORDER BY processing for max() func */
  9980. +#define WHERE_ONEPASS_DESIRED 0x0004 /* Want to do one-pass UPDATE/DELETE */
  9981. +#define WHERE_ONEPASS_MULTIROW 0x0008 /* ONEPASS is ok with multiple rows */
  9982. +#define WHERE_DUPLICATES_OK 0x0010 /* Ok to return a row more than once */
  9983. +#define WHERE_OR_SUBCLAUSE 0x0020 /* Processing a sub-WHERE as part of
  9984. + ** the OR optimization */
  9985. +#define WHERE_GROUPBY 0x0040 /* pOrderBy is really a GROUP BY */
  9986. +#define WHERE_DISTINCTBY 0x0080 /* pOrderby is really a DISTINCT clause */
  9987. +#define WHERE_WANT_DISTINCT 0x0100 /* All output needs to be distinct */
  9988. +#define WHERE_SORTBYGROUP 0x0200 /* Support sqlite3WhereIsSorted() */
  9989. +#define WHERE_SEEK_TABLE 0x0400 /* Do not defer seeks on main table */
  9990. +#define WHERE_ORDERBY_LIMIT 0x0800 /* ORDERBY+LIMIT on the inner loop */
  9991. +#define WHERE_SEEK_UNIQ_TABLE 0x1000 /* Do not defer seeks if unique */
  9992. + /* 0x2000 not currently used */
  9993. +#define WHERE_USE_LIMIT 0x4000 /* Use the LIMIT in cost estimates */
  9994. + /* 0x8000 not currently used */
  9995. +
  9996. +/* Allowed return values from sqlite3WhereIsDistinct()
  9997. +*/
  9998. +#define WHERE_DISTINCT_NOOP 0 /* DISTINCT keyword not used */
  9999. +#define WHERE_DISTINCT_UNIQUE 1 /* No duplicates */
  10000. +#define WHERE_DISTINCT_ORDERED 2 /* All duplicates are adjacent */
  10001. +#define WHERE_DISTINCT_UNORDERED 3 /* Duplicates are scattered */
  10002. +
  10003. +/*
  10004. +** A NameContext defines a context in which to resolve table and column
  10005. +** names. The context consists of a list of tables (the pSrcList) field and
  10006. +** a list of named expression (pEList). The named expression list may
  10007. +** be NULL. The pSrc corresponds to the FROM clause of a SELECT or
  10008. +** to the table being operated on by INSERT, UPDATE, or DELETE. The
  10009. +** pEList corresponds to the result set of a SELECT and is NULL for
  10010. +** other statements.
  10011. +**
  10012. +** NameContexts can be nested. When resolving names, the inner-most
  10013. +** context is searched first. If no match is found, the next outer
  10014. +** context is checked. If there is still no match, the next context
  10015. +** is checked. This process continues until either a match is found
  10016. +** or all contexts are check. When a match is found, the nRef member of
  10017. +** the context containing the match is incremented.
  10018. +**
  10019. +** Each subquery gets a new NameContext. The pNext field points to the
  10020. +** NameContext in the parent query. Thus the process of scanning the
  10021. +** NameContext list corresponds to searching through successively outer
  10022. +** subqueries looking for a match.
  10023. +*/
  10024. +struct NameContext {
  10025. + Parse *pParse; /* The parser */
  10026. + SrcList *pSrcList; /* One or more tables used to resolve names */
  10027. + union {
  10028. + ExprList *pEList; /* Optional list of result-set columns */
  10029. + AggInfo *pAggInfo; /* Information about aggregates at this level */
  10030. + Upsert *pUpsert; /* ON CONFLICT clause information from an upsert */
  10031. + } uNC;
  10032. + NameContext *pNext; /* Next outer name context. NULL for outermost */
  10033. + int nRef; /* Number of names resolved by this context */
  10034. + int nErr; /* Number of errors encountered while resolving names */
  10035. + int ncFlags; /* Zero or more NC_* flags defined below */
  10036. + Select *pWinSelect; /* SELECT statement for any window functions */
  10037. +};
  10038. +
  10039. +/*
  10040. +** Allowed values for the NameContext, ncFlags field.
  10041. +**
  10042. +** Value constraints (all checked via assert()):
  10043. +** NC_HasAgg == SF_HasAgg == EP_Agg
  10044. +** NC_MinMaxAgg == SF_MinMaxAgg == SQLITE_FUNC_MINMAX
  10045. +** NC_HasWin == EP_Win
  10046. +**
  10047. +*/
  10048. +#define NC_AllowAgg 0x00001 /* Aggregate functions are allowed here */
  10049. +#define NC_PartIdx 0x00002 /* True if resolving a partial index WHERE */
  10050. +#define NC_IsCheck 0x00004 /* True if resolving a CHECK constraint */
  10051. +#define NC_GenCol 0x00008 /* True for a GENERATED ALWAYS AS clause */
  10052. +#define NC_HasAgg 0x00010 /* One or more aggregate functions seen */
  10053. +#define NC_IdxExpr 0x00020 /* True if resolving columns of CREATE INDEX */
  10054. +#define NC_SelfRef 0x0002e /* Combo: PartIdx, isCheck, GenCol, and IdxExpr */
  10055. +#define NC_VarSelect 0x00040 /* A correlated subquery has been seen */
  10056. +#define NC_UEList 0x00080 /* True if uNC.pEList is used */
  10057. +#define NC_UAggInfo 0x00100 /* True if uNC.pAggInfo is used */
  10058. +#define NC_UUpsert 0x00200 /* True if uNC.pUpsert is used */
  10059. +#define NC_MinMaxAgg 0x01000 /* min/max aggregates seen. See note above */
  10060. +#define NC_Complex 0x02000 /* True if a function or subquery seen */
  10061. +#define NC_AllowWin 0x04000 /* Window functions are allowed here */
  10062. +#define NC_HasWin 0x08000 /* One or more window functions seen */
  10063. +#define NC_IsDDL 0x10000 /* Resolving names in a CREATE statement */
  10064. +#define NC_InAggFunc 0x20000 /* True if analyzing arguments to an agg func */
  10065. +#define NC_FromDDL 0x40000 /* SQL text comes from sqlite_master */
  10066. +
  10067. +/*
  10068. +** An instance of the following object describes a single ON CONFLICT
  10069. +** clause in an upsert.
  10070. +**
  10071. +** The pUpsertTarget field is only set if the ON CONFLICT clause includes
  10072. +** conflict-target clause. (In "ON CONFLICT(a,b)" the "(a,b)" is the
  10073. +** conflict-target clause.) The pUpsertTargetWhere is the optional
  10074. +** WHERE clause used to identify partial unique indexes.
  10075. +**
  10076. +** pUpsertSet is the list of column=expr terms of the UPDATE statement.
  10077. +** The pUpsertSet field is NULL for a ON CONFLICT DO NOTHING. The
  10078. +** pUpsertWhere is the WHERE clause for the UPDATE and is NULL if the
  10079. +** WHERE clause is omitted.
  10080. +*/
  10081. +struct Upsert {
  10082. + ExprList *pUpsertTarget; /* Optional description of conflicting index */
  10083. + Expr *pUpsertTargetWhere; /* WHERE clause for partial index targets */
  10084. + ExprList *pUpsertSet; /* The SET clause from an ON CONFLICT UPDATE */
  10085. + Expr *pUpsertWhere; /* WHERE clause for the ON CONFLICT UPDATE */
  10086. + /* The fields above comprise the parse tree for the upsert clause.
  10087. + ** The fields below are used to transfer information from the INSERT
  10088. + ** processing down into the UPDATE processing while generating code.
  10089. + ** Upsert owns the memory allocated above, but not the memory below. */
  10090. + Index *pUpsertIdx; /* Constraint that pUpsertTarget identifies */
  10091. + SrcList *pUpsertSrc; /* Table to be updated */
  10092. + int regData; /* First register holding array of VALUES */
  10093. + int iDataCur; /* Index of the data cursor */
  10094. + int iIdxCur; /* Index of the first index cursor */
  10095. +};
  10096. +
  10097. +/*
  10098. +** An instance of the following structure contains all information
  10099. +** needed to generate code for a single SELECT statement.
  10100. +**
  10101. +** See the header comment on the computeLimitRegisters() routine for a
  10102. +** detailed description of the meaning of the iLimit and iOffset fields.
  10103. +**
  10104. +** addrOpenEphm[] entries contain the address of OP_OpenEphemeral opcodes.
  10105. +** These addresses must be stored so that we can go back and fill in
  10106. +** the P4_KEYINFO and P2 parameters later. Neither the KeyInfo nor
  10107. +** the number of columns in P2 can be computed at the same time
  10108. +** as the OP_OpenEphm instruction is coded because not
  10109. +** enough information about the compound query is known at that point.
  10110. +** The KeyInfo for addrOpenTran[0] and [1] contains collating sequences
  10111. +** for the result set. The KeyInfo for addrOpenEphm[2] contains collating
  10112. +** sequences for the ORDER BY clause.
  10113. +*/
  10114. +struct Select {
  10115. + u8 op; /* One of: TK_UNION TK_ALL TK_INTERSECT TK_EXCEPT */
  10116. + LogEst nSelectRow; /* Estimated number of result rows */
  10117. + u32 selFlags; /* Various SF_* values */
  10118. + int iLimit, iOffset; /* Memory registers holding LIMIT & OFFSET counters */
  10119. + u32 selId; /* Unique identifier number for this SELECT */
  10120. + int addrOpenEphm[2]; /* OP_OpenEphem opcodes related to this select */
  10121. + ExprList *pEList; /* The fields of the result */
  10122. + SrcList *pSrc; /* The FROM clause */
  10123. + Expr *pWhere; /* The WHERE clause */
  10124. + ExprList *pGroupBy; /* The GROUP BY clause */
  10125. + Expr *pHaving; /* The HAVING clause */
  10126. + ExprList *pOrderBy; /* The ORDER BY clause */
  10127. + Select *pPrior; /* Prior select in a compound select statement */
  10128. + Select *pNext; /* Next select to the left in a compound */
  10129. + Expr *pLimit; /* LIMIT expression. NULL means not used. */
  10130. + With *pWith; /* WITH clause attached to this select. Or NULL. */
  10131. +#ifndef SQLITE_OMIT_WINDOWFUNC
  10132. + Window *pWin; /* List of window functions */
  10133. + Window *pWinDefn; /* List of named window definitions */
  10134. +#endif
  10135. +};
  10136. +
  10137. +/*
  10138. +** Allowed values for Select.selFlags. The "SF" prefix stands for
  10139. +** "Select Flag".
  10140. +**
  10141. +** Value constraints (all checked via assert())
  10142. +** SF_HasAgg == NC_HasAgg
  10143. +** SF_MinMaxAgg == NC_MinMaxAgg == SQLITE_FUNC_MINMAX
  10144. +** SF_FixedLimit == WHERE_USE_LIMIT
  10145. +*/
  10146. +#define SF_Distinct 0x0000001 /* Output should be DISTINCT */
  10147. +#define SF_All 0x0000002 /* Includes the ALL keyword */
  10148. +#define SF_Resolved 0x0000004 /* Identifiers have been resolved */
  10149. +#define SF_Aggregate 0x0000008 /* Contains agg functions or a GROUP BY */
  10150. +#define SF_HasAgg 0x0000010 /* Contains aggregate functions */
  10151. +#define SF_UsesEphemeral 0x0000020 /* Uses the OpenEphemeral opcode */
  10152. +#define SF_Expanded 0x0000040 /* sqlite3SelectExpand() called on this */
  10153. +#define SF_HasTypeInfo 0x0000080 /* FROM subqueries have Table metadata */
  10154. +#define SF_Compound 0x0000100 /* Part of a compound query */
  10155. +#define SF_Values 0x0000200 /* Synthesized from VALUES clause */
  10156. +#define SF_MultiValue 0x0000400 /* Single VALUES term with multiple rows */
  10157. +#define SF_NestedFrom 0x0000800 /* Part of a parenthesized FROM clause */
  10158. +#define SF_MinMaxAgg 0x0001000 /* Aggregate containing min() or max() */
  10159. +#define SF_Recursive 0x0002000 /* The recursive part of a recursive CTE */
  10160. +#define SF_FixedLimit 0x0004000 /* nSelectRow set by a constant LIMIT */
  10161. +#define SF_MaybeConvert 0x0008000 /* Need convertCompoundSelectToSubquery() */
  10162. +#define SF_Converted 0x0010000 /* By convertCompoundSelectToSubquery() */
  10163. +#define SF_IncludeHidden 0x0020000 /* Include hidden columns in output */
  10164. +#define SF_ComplexResult 0x0040000 /* Result contains subquery or function */
  10165. +#define SF_WhereBegin 0x0080000 /* Really a WhereBegin() call. Debug Only */
  10166. +#define SF_WinRewrite 0x0100000 /* Window function rewrite accomplished */
  10167. +#define SF_View 0x0200000 /* SELECT statement is a view */
  10168. +
  10169. +/*
  10170. +** The results of a SELECT can be distributed in several ways, as defined
  10171. +** by one of the following macros. The "SRT" prefix means "SELECT Result
  10172. +** Type".
  10173. +**
  10174. +** SRT_Union Store results as a key in a temporary index
  10175. +** identified by pDest->iSDParm.
  10176. +**
  10177. +** SRT_Except Remove results from the temporary index pDest->iSDParm.
  10178. +**
  10179. +** SRT_Exists Store a 1 in memory cell pDest->iSDParm if the result
  10180. +** set is not empty.
  10181. +**
  10182. +** SRT_Discard Throw the results away. This is used by SELECT
  10183. +** statements within triggers whose only purpose is
  10184. +** the side-effects of functions.
  10185. +**
  10186. +** All of the above are free to ignore their ORDER BY clause. Those that
  10187. +** follow must honor the ORDER BY clause.
  10188. +**
  10189. +** SRT_Output Generate a row of output (using the OP_ResultRow
  10190. +** opcode) for each row in the result set.
  10191. +**
  10192. +** SRT_Mem Only valid if the result is a single column.
  10193. +** Store the first column of the first result row
  10194. +** in register pDest->iSDParm then abandon the rest
  10195. +** of the query. This destination implies "LIMIT 1".
  10196. +**
  10197. +** SRT_Set The result must be a single column. Store each
  10198. +** row of result as the key in table pDest->iSDParm.
  10199. +** Apply the affinity pDest->affSdst before storing
  10200. +** results. Used to implement "IN (SELECT ...)".
  10201. +**
  10202. +** SRT_EphemTab Create an temporary table pDest->iSDParm and store
  10203. +** the result there. The cursor is left open after
  10204. +** returning. This is like SRT_Table except that
  10205. +** this destination uses OP_OpenEphemeral to create
  10206. +** the table first.
  10207. +**
  10208. +** SRT_Coroutine Generate a co-routine that returns a new row of
  10209. +** results each time it is invoked. The entry point
  10210. +** of the co-routine is stored in register pDest->iSDParm
  10211. +** and the result row is stored in pDest->nDest registers
  10212. +** starting with pDest->iSdst.
  10213. +**
  10214. +** SRT_Table Store results in temporary table pDest->iSDParm.
  10215. +** SRT_Fifo This is like SRT_EphemTab except that the table
  10216. +** is assumed to already be open. SRT_Fifo has
  10217. +** the additional property of being able to ignore
  10218. +** the ORDER BY clause.
  10219. +**
  10220. +** SRT_DistFifo Store results in a temporary table pDest->iSDParm.
  10221. +** But also use temporary table pDest->iSDParm+1 as
  10222. +** a record of all prior results and ignore any duplicate
  10223. +** rows. Name means: "Distinct Fifo".
  10224. +**
  10225. +** SRT_Queue Store results in priority queue pDest->iSDParm (really
  10226. +** an index). Append a sequence number so that all entries
  10227. +** are distinct.
  10228. +**
  10229. +** SRT_DistQueue Store results in priority queue pDest->iSDParm only if
  10230. +** the same record has never been stored before. The
  10231. +** index at pDest->iSDParm+1 hold all prior stores.
  10232. +*/
  10233. +#define SRT_Union 1 /* Store result as keys in an index */
  10234. +#define SRT_Except 2 /* Remove result from a UNION index */
  10235. +#define SRT_Exists 3 /* Store 1 if the result is not empty */
  10236. +#define SRT_Discard 4 /* Do not save the results anywhere */
  10237. +#define SRT_Fifo 5 /* Store result as data with an automatic rowid */
  10238. +#define SRT_DistFifo 6 /* Like SRT_Fifo, but unique results only */
  10239. +#define SRT_Queue 7 /* Store result in an queue */
  10240. +#define SRT_DistQueue 8 /* Like SRT_Queue, but unique results only */
  10241. +
  10242. +/* The ORDER BY clause is ignored for all of the above */
  10243. +#define IgnorableOrderby(X) ((X->eDest)<=SRT_DistQueue)
  10244. +
  10245. +#define SRT_Output 9 /* Output each row of result */
  10246. +#define SRT_Mem 10 /* Store result in a memory cell */
  10247. +#define SRT_Set 11 /* Store results as keys in an index */
  10248. +#define SRT_EphemTab 12 /* Create transient tab and store like SRT_Table */
  10249. +#define SRT_Coroutine 13 /* Generate a single row of result */
  10250. +#define SRT_Table 14 /* Store result as data with an automatic rowid */
  10251. +
  10252. +/*
  10253. +** An instance of this object describes where to put of the results of
  10254. +** a SELECT statement.
  10255. +*/
  10256. +struct SelectDest {
  10257. + u8 eDest; /* How to dispose of the results. On of SRT_* above. */
  10258. + int iSDParm; /* A parameter used by the eDest disposal method */
  10259. + int iSdst; /* Base register where results are written */
  10260. + int nSdst; /* Number of registers allocated */
  10261. + char *zAffSdst; /* Affinity used when eDest==SRT_Set */
  10262. + ExprList *pOrderBy; /* Key columns for SRT_Queue and SRT_DistQueue */
  10263. +};
  10264. +
  10265. +/*
  10266. +** During code generation of statements that do inserts into AUTOINCREMENT
  10267. +** tables, the following information is attached to the Table.u.autoInc.p
  10268. +** pointer of each autoincrement table to record some side information that
  10269. +** the code generator needs. We have to keep per-table autoincrement
  10270. +** information in case inserts are done within triggers. Triggers do not
  10271. +** normally coordinate their activities, but we do need to coordinate the
  10272. +** loading and saving of autoincrement information.
  10273. +*/
  10274. +struct AutoincInfo {
  10275. + AutoincInfo *pNext; /* Next info block in a list of them all */
  10276. + Table *pTab; /* Table this info block refers to */
  10277. + int iDb; /* Index in sqlite3.aDb[] of database holding pTab */
  10278. + int regCtr; /* Memory register holding the rowid counter */
  10279. +};
  10280. +
  10281. +/*
  10282. +** At least one instance of the following structure is created for each
  10283. +** trigger that may be fired while parsing an INSERT, UPDATE or DELETE
  10284. +** statement. All such objects are stored in the linked list headed at
  10285. +** Parse.pTriggerPrg and deleted once statement compilation has been
  10286. +** completed.
  10287. +**
  10288. +** A Vdbe sub-program that implements the body and WHEN clause of trigger
  10289. +** TriggerPrg.pTrigger, assuming a default ON CONFLICT clause of
  10290. +** TriggerPrg.orconf, is stored in the TriggerPrg.pProgram variable.
  10291. +** The Parse.pTriggerPrg list never contains two entries with the same
  10292. +** values for both pTrigger and orconf.
  10293. +**
  10294. +** The TriggerPrg.aColmask[0] variable is set to a mask of old.* columns
  10295. +** accessed (or set to 0 for triggers fired as a result of INSERT
  10296. +** statements). Similarly, the TriggerPrg.aColmask[1] variable is set to
  10297. +** a mask of new.* columns used by the program.
  10298. +*/
  10299. +struct TriggerPrg {
  10300. + Trigger *pTrigger; /* Trigger this program was coded from */
  10301. + TriggerPrg *pNext; /* Next entry in Parse.pTriggerPrg list */
  10302. + SubProgram *pProgram; /* Program implementing pTrigger/orconf */
  10303. + int orconf; /* Default ON CONFLICT policy */
  10304. + u32 aColmask[2]; /* Masks of old.*, new.* columns accessed */
  10305. +};
  10306. +
  10307. +/*
  10308. +** The yDbMask datatype for the bitmask of all attached databases.
  10309. +*/
  10310. +#if SQLITE_MAX_ATTACHED>30
  10311. + typedef unsigned char yDbMask[(SQLITE_MAX_ATTACHED+9)/8];
  10312. +# define DbMaskTest(M,I) (((M)[(I)/8]&(1<<((I)&7)))!=0)
  10313. +# define DbMaskZero(M) memset((M),0,sizeof(M))
  10314. +# define DbMaskSet(M,I) (M)[(I)/8]|=(1<<((I)&7))
  10315. +# define DbMaskAllZero(M) sqlite3DbMaskAllZero(M)
  10316. +# define DbMaskNonZero(M) (sqlite3DbMaskAllZero(M)==0)
  10317. +#else
  10318. + typedef unsigned int yDbMask;
  10319. +# define DbMaskTest(M,I) (((M)&(((yDbMask)1)<<(I)))!=0)
  10320. +# define DbMaskZero(M) (M)=0
  10321. +# define DbMaskSet(M,I) (M)|=(((yDbMask)1)<<(I))
  10322. +# define DbMaskAllZero(M) (M)==0
  10323. +# define DbMaskNonZero(M) (M)!=0
  10324. +#endif
  10325. +
  10326. +/*
  10327. +** An SQL parser context. A copy of this structure is passed through
  10328. +** the parser and down into all the parser action routine in order to
  10329. +** carry around information that is global to the entire parse.
  10330. +**
  10331. +** The structure is divided into two parts. When the parser and code
  10332. +** generate call themselves recursively, the first part of the structure
  10333. +** is constant but the second part is reset at the beginning and end of
  10334. +** each recursion.
  10335. +**
  10336. +** The nTableLock and aTableLock variables are only used if the shared-cache
  10337. +** feature is enabled (if sqlite3Tsd()->useSharedData is true). They are
  10338. +** used to store the set of table-locks required by the statement being
  10339. +** compiled. Function sqlite3TableLock() is used to add entries to the
  10340. +** list.
  10341. +*/
  10342. +struct Parse {
  10343. + sqlite3 *db; /* The main database structure */
  10344. + char *zErrMsg; /* An error message */
  10345. + Vdbe *pVdbe; /* An engine for executing database bytecode */
  10346. + int rc; /* Return code from execution */
  10347. + u8 colNamesSet; /* TRUE after OP_ColumnName has been issued to pVdbe */
  10348. + u8 checkSchema; /* Causes schema cookie check after an error */
  10349. + u8 nested; /* Number of nested calls to the parser/code generator */
  10350. + u8 nTempReg; /* Number of temporary registers in aTempReg[] */
  10351. + u8 isMultiWrite; /* True if statement may modify/insert multiple rows */
  10352. + u8 mayAbort; /* True if statement may throw an ABORT exception */
  10353. + u8 hasCompound; /* Need to invoke convertCompoundSelectToSubquery() */
  10354. + u8 okConstFactor; /* OK to factor out constants */
  10355. + u8 disableLookaside; /* Number of times lookaside has been disabled */
  10356. + u8 disableVtab; /* Disable all virtual tables for this parse */
  10357. + int nRangeReg; /* Size of the temporary register block */
  10358. + int iRangeReg; /* First register in temporary register block */
  10359. + int nErr; /* Number of errors seen */
  10360. + int nTab; /* Number of previously allocated VDBE cursors */
  10361. + int nMem; /* Number of memory cells used so far */
  10362. + int szOpAlloc; /* Bytes of memory space allocated for Vdbe.aOp[] */
  10363. + int iSelfTab; /* Table associated with an index on expr, or negative
  10364. + ** of the base register during check-constraint eval */
  10365. + int nLabel; /* The *negative* of the number of labels used */
  10366. + int nLabelAlloc; /* Number of slots in aLabel */
  10367. + int *aLabel; /* Space to hold the labels */
  10368. + ExprList *pConstExpr;/* Constant expressions */
  10369. + Token constraintName;/* Name of the constraint currently being parsed */
  10370. + yDbMask writeMask; /* Start a write transaction on these databases */
  10371. + yDbMask cookieMask; /* Bitmask of schema verified databases */
  10372. + int regRowid; /* Register holding rowid of CREATE TABLE entry */
  10373. + int regRoot; /* Register holding root page number for new objects */
  10374. + int nMaxArg; /* Max args passed to user function by sub-program */
  10375. + int nSelect; /* Number of SELECT stmts. Counter for Select.selId */
  10376. +#ifndef SQLITE_OMIT_SHARED_CACHE
  10377. + int nTableLock; /* Number of locks in aTableLock */
  10378. + TableLock *aTableLock; /* Required table locks for shared-cache mode */
  10379. +#endif
  10380. + AutoincInfo *pAinc; /* Information about AUTOINCREMENT counters */
  10381. + Parse *pToplevel; /* Parse structure for main program (or NULL) */
  10382. + Table *pTriggerTab; /* Table triggers are being coded for */
  10383. + Parse *pParentParse; /* Parent parser if this parser is nested */
  10384. + int addrCrTab; /* Address of OP_CreateBtree opcode on CREATE TABLE */
  10385. + u32 nQueryLoop; /* Est number of iterations of a query (10*log2(N)) */
  10386. + u32 oldmask; /* Mask of old.* columns referenced */
  10387. + u32 newmask; /* Mask of new.* columns referenced */
  10388. + u8 eTriggerOp; /* TK_UPDATE, TK_INSERT or TK_DELETE */
  10389. + u8 eOrconf; /* Default ON CONFLICT policy for trigger steps */
  10390. + u8 disableTriggers; /* True to disable triggers */
  10391. +
  10392. + /**************************************************************************
  10393. + ** Fields above must be initialized to zero. The fields that follow,
  10394. + ** down to the beginning of the recursive section, do not need to be
  10395. + ** initialized as they will be set before being used. The boundary is
  10396. + ** determined by offsetof(Parse,aTempReg).
  10397. + **************************************************************************/
  10398. +
  10399. + int aTempReg[8]; /* Holding area for temporary registers */
  10400. + Token sNameToken; /* Token with unqualified schema object name */
  10401. +
  10402. + /************************************************************************
  10403. + ** Above is constant between recursions. Below is reset before and after
  10404. + ** each recursion. The boundary between these two regions is determined
  10405. + ** using offsetof(Parse,sLastToken) so the sLastToken field must be the
  10406. + ** first field in the recursive region.
  10407. + ************************************************************************/
  10408. +
  10409. + Token sLastToken; /* The last token parsed */
  10410. + ynVar nVar; /* Number of '?' variables seen in the SQL so far */
  10411. + u8 iPkSortOrder; /* ASC or DESC for INTEGER PRIMARY KEY */
  10412. + u8 explain; /* True if the EXPLAIN flag is found on the query */
  10413. +#if !(defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_OMIT_ALTERTABLE))
  10414. + u8 eParseMode; /* PARSE_MODE_XXX constant */
  10415. +#endif
  10416. +#ifndef SQLITE_OMIT_VIRTUALTABLE
  10417. + int nVtabLock; /* Number of virtual tables to lock */
  10418. +#endif
  10419. + int nHeight; /* Expression tree height of current sub-select */
  10420. +#ifndef SQLITE_OMIT_EXPLAIN
  10421. + int addrExplain; /* Address of current OP_Explain opcode */
  10422. +#endif
  10423. + VList *pVList; /* Mapping between variable names and numbers */
  10424. + Vdbe *pReprepare; /* VM being reprepared (sqlite3Reprepare()) */
  10425. + const char *zTail; /* All SQL text past the last semicolon parsed */
  10426. + Table *pNewTable; /* A table being constructed by CREATE TABLE */
  10427. + Index *pNewIndex; /* An index being constructed by CREATE INDEX.
  10428. + ** Also used to hold redundant UNIQUE constraints
  10429. + ** during a RENAME COLUMN */
  10430. + Trigger *pNewTrigger; /* Trigger under construct by a CREATE TRIGGER */
  10431. + const char *zAuthContext; /* The 6th parameter to db->xAuth callbacks */
  10432. +#ifndef SQLITE_OMIT_VIRTUALTABLE
  10433. + Token sArg; /* Complete text of a module argument */
  10434. + Table **apVtabLock; /* Pointer to virtual tables needing locking */
  10435. +#endif
  10436. + Table *pZombieTab; /* List of Table objects to delete after code gen */
  10437. + TriggerPrg *pTriggerPrg; /* Linked list of coded triggers */
  10438. + With *pWith; /* Current WITH clause, or NULL */
  10439. + With *pWithToFree; /* Free this WITH object at the end of the parse */
  10440. +#ifndef SQLITE_OMIT_ALTERTABLE
  10441. + RenameToken *pRename; /* Tokens subject to renaming by ALTER TABLE */
  10442. +#endif
  10443. +};
  10444. +
  10445. +#define PARSE_MODE_NORMAL 0
  10446. +#define PARSE_MODE_DECLARE_VTAB 1
  10447. +#define PARSE_MODE_RENAME 2
  10448. +#define PARSE_MODE_UNMAP 3
  10449. +
  10450. +/*
  10451. +** Sizes and pointers of various parts of the Parse object.
  10452. +*/
  10453. +#define PARSE_HDR_SZ offsetof(Parse,aTempReg) /* Recursive part w/o aColCache*/
  10454. +#define PARSE_RECURSE_SZ offsetof(Parse,sLastToken) /* Recursive part */
  10455. +#define PARSE_TAIL_SZ (sizeof(Parse)-PARSE_RECURSE_SZ) /* Non-recursive part */
  10456. +#define PARSE_TAIL(X) (((char*)(X))+PARSE_RECURSE_SZ) /* Pointer to tail */
  10457. +
  10458. +/*
  10459. +** Return true if currently inside an sqlite3_declare_vtab() call.
  10460. +*/
  10461. +#ifdef SQLITE_OMIT_VIRTUALTABLE
  10462. + #define IN_DECLARE_VTAB 0
  10463. +#else
  10464. + #define IN_DECLARE_VTAB (pParse->eParseMode==PARSE_MODE_DECLARE_VTAB)
  10465. +#endif
  10466. +
  10467. +#if defined(SQLITE_OMIT_ALTERTABLE)
  10468. + #define IN_RENAME_OBJECT 0
  10469. +#else
  10470. + #define IN_RENAME_OBJECT (pParse->eParseMode>=PARSE_MODE_RENAME)
  10471. +#endif
  10472. +
  10473. +#if defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_OMIT_ALTERTABLE)
  10474. + #define IN_SPECIAL_PARSE 0
  10475. +#else
  10476. + #define IN_SPECIAL_PARSE (pParse->eParseMode!=PARSE_MODE_NORMAL)
  10477. +#endif
  10478. +
  10479. +/*
  10480. +** An instance of the following structure can be declared on a stack and used
  10481. +** to save the Parse.zAuthContext value so that it can be restored later.
  10482. +*/
  10483. +struct AuthContext {
  10484. + const char *zAuthContext; /* Put saved Parse.zAuthContext here */
  10485. + Parse *pParse; /* The Parse structure */
  10486. +};
  10487. +
  10488. +/*
  10489. +** Bitfield flags for P5 value in various opcodes.
  10490. +**
  10491. +** Value constraints (enforced via assert()):
  10492. +** OPFLAG_LENGTHARG == SQLITE_FUNC_LENGTH
  10493. +** OPFLAG_TYPEOFARG == SQLITE_FUNC_TYPEOF
  10494. +** OPFLAG_BULKCSR == BTREE_BULKLOAD
  10495. +** OPFLAG_SEEKEQ == BTREE_SEEK_EQ
  10496. +** OPFLAG_FORDELETE == BTREE_FORDELETE
  10497. +** OPFLAG_SAVEPOSITION == BTREE_SAVEPOSITION
  10498. +** OPFLAG_AUXDELETE == BTREE_AUXDELETE
  10499. +*/
  10500. +#define OPFLAG_NCHANGE 0x01 /* OP_Insert: Set to update db->nChange */
  10501. + /* Also used in P2 (not P5) of OP_Delete */
  10502. +#define OPFLAG_NOCHNG 0x01 /* OP_VColumn nochange for UPDATE */
  10503. +#define OPFLAG_EPHEM 0x01 /* OP_Column: Ephemeral output is ok */
  10504. +#define OPFLAG_LASTROWID 0x20 /* Set to update db->lastRowid */
  10505. +#define OPFLAG_ISUPDATE 0x04 /* This OP_Insert is an sql UPDATE */
  10506. +#define OPFLAG_APPEND 0x08 /* This is likely to be an append */
  10507. +#define OPFLAG_USESEEKRESULT 0x10 /* Try to avoid a seek in BtreeInsert() */
  10508. +#define OPFLAG_ISNOOP 0x40 /* OP_Delete does pre-update-hook only */
  10509. +#define OPFLAG_LENGTHARG 0x40 /* OP_Column only used for length() */
  10510. +#define OPFLAG_TYPEOFARG 0x80 /* OP_Column only used for typeof() */
  10511. +#define OPFLAG_BULKCSR 0x01 /* OP_Open** used to open bulk cursor */
  10512. +#define OPFLAG_SEEKEQ 0x02 /* OP_Open** cursor uses EQ seek only */
  10513. +#define OPFLAG_FORDELETE 0x08 /* OP_Open should use BTREE_FORDELETE */
  10514. +#define OPFLAG_P2ISREG 0x10 /* P2 to OP_Open** is a register number */
  10515. +#define OPFLAG_PERMUTE 0x01 /* OP_Compare: use the permutation */
  10516. +#define OPFLAG_SAVEPOSITION 0x02 /* OP_Delete/Insert: save cursor pos */
  10517. +#define OPFLAG_AUXDELETE 0x04 /* OP_Delete: index in a DELETE op */
  10518. +#define OPFLAG_NOCHNG_MAGIC 0x6d /* OP_MakeRecord: serialtype 10 is ok */
  10519. +
  10520. +/*
  10521. + * Each trigger present in the database schema is stored as an instance of
  10522. + * struct Trigger.
  10523. + *
  10524. + * Pointers to instances of struct Trigger are stored in two ways.
  10525. + * 1. In the "trigHash" hash table (part of the sqlite3* that represents the
  10526. + * database). This allows Trigger structures to be retrieved by name.
  10527. + * 2. All triggers associated with a single table form a linked list, using the
  10528. + * pNext member of struct Trigger. A pointer to the first element of the
  10529. + * linked list is stored as the "pTrigger" member of the associated
  10530. + * struct Table.
  10531. + *
  10532. + * The "step_list" member points to the first element of a linked list
  10533. + * containing the SQL statements specified as the trigger program.
  10534. + */
  10535. +struct Trigger {
  10536. + char *zName; /* The name of the trigger */
  10537. + char *table; /* The table or view to which the trigger applies */
  10538. + u8 op; /* One of TK_DELETE, TK_UPDATE, TK_INSERT */
  10539. + u8 tr_tm; /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
  10540. + Expr *pWhen; /* The WHEN clause of the expression (may be NULL) */
  10541. + IdList *pColumns; /* If this is an UPDATE OF <column-list> trigger,
  10542. + the <column-list> is stored here */
  10543. + Schema *pSchema; /* Schema containing the trigger */
  10544. + Schema *pTabSchema; /* Schema containing the table */
  10545. + TriggerStep *step_list; /* Link list of trigger program steps */
  10546. + Trigger *pNext; /* Next trigger associated with the table */
  10547. +};
  10548. +
  10549. +/*
  10550. +** A trigger is either a BEFORE or an AFTER trigger. The following constants
  10551. +** determine which.
  10552. +**
  10553. +** If there are multiple triggers, you might of some BEFORE and some AFTER.
  10554. +** In that cases, the constants below can be ORed together.
  10555. +*/
  10556. +#define TRIGGER_BEFORE 1
  10557. +#define TRIGGER_AFTER 2
  10558. +
  10559. +/*
  10560. + * An instance of struct TriggerStep is used to store a single SQL statement
  10561. + * that is a part of a trigger-program.
  10562. + *
  10563. + * Instances of struct TriggerStep are stored in a singly linked list (linked
  10564. + * using the "pNext" member) referenced by the "step_list" member of the
  10565. + * associated struct Trigger instance. The first element of the linked list is
  10566. + * the first step of the trigger-program.
  10567. + *
  10568. + * The "op" member indicates whether this is a "DELETE", "INSERT", "UPDATE" or
  10569. + * "SELECT" statement. The meanings of the other members is determined by the
  10570. + * value of "op" as follows:
  10571. + *
  10572. + * (op == TK_INSERT)
  10573. + * orconf -> stores the ON CONFLICT algorithm
  10574. + * pSelect -> If this is an INSERT INTO ... SELECT ... statement, then
  10575. + * this stores a pointer to the SELECT statement. Otherwise NULL.
  10576. + * zTarget -> Dequoted name of the table to insert into.
  10577. + * pExprList -> If this is an INSERT INTO ... VALUES ... statement, then
  10578. + * this stores values to be inserted. Otherwise NULL.
  10579. + * pIdList -> If this is an INSERT INTO ... (<column-names>) VALUES ...
  10580. + * statement, then this stores the column-names to be
  10581. + * inserted into.
  10582. + *
  10583. + * (op == TK_DELETE)
  10584. + * zTarget -> Dequoted name of the table to delete from.
  10585. + * pWhere -> The WHERE clause of the DELETE statement if one is specified.
  10586. + * Otherwise NULL.
  10587. + *
  10588. + * (op == TK_UPDATE)
  10589. + * zTarget -> Dequoted name of the table to update.
  10590. + * pWhere -> The WHERE clause of the UPDATE statement if one is specified.
  10591. + * Otherwise NULL.
  10592. + * pExprList -> A list of the columns to update and the expressions to update
  10593. + * them to. See sqlite3Update() documentation of "pChanges"
  10594. + * argument.
  10595. + *
  10596. + */
  10597. +struct TriggerStep {
  10598. + u8 op; /* One of TK_DELETE, TK_UPDATE, TK_INSERT, TK_SELECT */
  10599. + u8 orconf; /* OE_Rollback etc. */
  10600. + Trigger *pTrig; /* The trigger that this step is a part of */
  10601. + Select *pSelect; /* SELECT statement or RHS of INSERT INTO SELECT ... */
  10602. + char *zTarget; /* Target table for DELETE, UPDATE, INSERT */
  10603. + Expr *pWhere; /* The WHERE clause for DELETE or UPDATE steps */
  10604. + ExprList *pExprList; /* SET clause for UPDATE */
  10605. + IdList *pIdList; /* Column names for INSERT */
  10606. + Upsert *pUpsert; /* Upsert clauses on an INSERT */
  10607. + char *zSpan; /* Original SQL text of this command */
  10608. + TriggerStep *pNext; /* Next in the link-list */
  10609. + TriggerStep *pLast; /* Last element in link-list. Valid for 1st elem only */
  10610. +};
  10611. +
  10612. +/*
  10613. +** The following structure contains information used by the sqliteFix...
  10614. +** routines as they walk the parse tree to make database references
  10615. +** explicit.
  10616. +*/
  10617. +typedef struct DbFixer DbFixer;
  10618. +struct DbFixer {
  10619. + Parse *pParse; /* The parsing context. Error messages written here */
  10620. + Schema *pSchema; /* Fix items to this schema */
  10621. + u8 bTemp; /* True for TEMP schema entries */
  10622. + const char *zDb; /* Make sure all objects are contained in this database */
  10623. + const char *zType; /* Type of the container - used for error messages */
  10624. + const Token *pName; /* Name of the container - used for error messages */
  10625. +};
  10626. +
  10627. +/*
  10628. +** An objected used to accumulate the text of a string where we
  10629. +** do not necessarily know how big the string will be in the end.
  10630. +*/
  10631. +struct sqlite3_str {
  10632. + sqlite3 *db; /* Optional database for lookaside. Can be NULL */
  10633. + char *zText; /* The string collected so far */
  10634. + u32 nAlloc; /* Amount of space allocated in zText */
  10635. + u32 mxAlloc; /* Maximum allowed allocation. 0 for no malloc usage */
  10636. + u32 nChar; /* Length of the string so far */
  10637. + u8 accError; /* SQLITE_NOMEM or SQLITE_TOOBIG */
  10638. + u8 printfFlags; /* SQLITE_PRINTF flags below */
  10639. +};
  10640. +#define SQLITE_PRINTF_INTERNAL 0x01 /* Internal-use-only converters allowed */
  10641. +#define SQLITE_PRINTF_SQLFUNC 0x02 /* SQL function arguments to VXPrintf */
  10642. +#define SQLITE_PRINTF_MALLOCED 0x04 /* True if xText is allocated space */
  10643. +
  10644. +#define isMalloced(X) (((X)->printfFlags & SQLITE_PRINTF_MALLOCED)!=0)
  10645. +
  10646. +
  10647. +/*
  10648. +** A pointer to this structure is used to communicate information
  10649. +** from sqlite3Init and OP_ParseSchema into the sqlite3InitCallback.
  10650. +*/
  10651. +typedef struct {
  10652. + sqlite3 *db; /* The database being initialized */
  10653. + char **pzErrMsg; /* Error message stored here */
  10654. + int iDb; /* 0 for main database. 1 for TEMP, 2.. for ATTACHed */
  10655. + int rc; /* Result code stored here */
  10656. + u32 mInitFlags; /* Flags controlling error messages */
  10657. + u32 nInitRow; /* Number of rows processed */
  10658. +} InitData;
  10659. +
  10660. +/*
  10661. +** Allowed values for mInitFlags
  10662. +*/
  10663. +#define INITFLAG_AlterTable 0x0001 /* This is a reparse after ALTER TABLE */
  10664. +
  10665. +/*
  10666. +** Structure containing global configuration data for the SQLite library.
  10667. +**
  10668. +** This structure also contains some state information.
  10669. +*/
  10670. +struct Sqlite3Config {
  10671. + int bMemstat; /* True to enable memory status */
  10672. + u8 bCoreMutex; /* True to enable core mutexing */
  10673. + u8 bFullMutex; /* True to enable full mutexing */
  10674. + u8 bOpenUri; /* True to interpret filenames as URIs */
  10675. + u8 bUseCis; /* Use covering indices for full-scans */
  10676. + u8 bSmallMalloc; /* Avoid large memory allocations if true */
  10677. + u8 bExtraSchemaChecks; /* Verify type,name,tbl_name in schema */
  10678. + int mxStrlen; /* Maximum string length */
  10679. + int neverCorrupt; /* Database is always well-formed */
  10680. + int szLookaside; /* Default lookaside buffer size */
  10681. + int nLookaside; /* Default lookaside buffer count */
  10682. + int nStmtSpill; /* Stmt-journal spill-to-disk threshold */
  10683. + sqlite3_mem_methods m; /* Low-level memory allocation interface */
  10684. + sqlite3_mutex_methods mutex; /* Low-level mutex interface */
  10685. + sqlite3_pcache_methods2 pcache2; /* Low-level page-cache interface */
  10686. + void *pHeap; /* Heap storage space */
  10687. + int nHeap; /* Size of pHeap[] */
  10688. + int mnReq, mxReq; /* Min and max heap requests sizes */
  10689. + sqlite3_int64 szMmap; /* mmap() space per open file */
  10690. + sqlite3_int64 mxMmap; /* Maximum value for szMmap */
  10691. + void *pPage; /* Page cache memory */
  10692. + int szPage; /* Size of each page in pPage[] */
  10693. + int nPage; /* Number of pages in pPage[] */
  10694. + int mxParserStack; /* maximum depth of the parser stack */
  10695. + int sharedCacheEnabled; /* true if shared-cache mode enabled */
  10696. + u32 szPma; /* Maximum Sorter PMA size */
  10697. + /* The above might be initialized to non-zero. The following need to always
  10698. + ** initially be zero, however. */
  10699. + int isInit; /* True after initialization has finished */
  10700. + int inProgress; /* True while initialization in progress */
  10701. + int isMutexInit; /* True after mutexes are initialized */
  10702. + int isMallocInit; /* True after malloc is initialized */
  10703. + int isPCacheInit; /* True after malloc is initialized */
  10704. + int nRefInitMutex; /* Number of users of pInitMutex */
  10705. + sqlite3_mutex *pInitMutex; /* Mutex used by sqlite3_initialize() */
  10706. + void (*xLog)(void*,int,const char*); /* Function for logging */
  10707. + void *pLogArg; /* First argument to xLog() */
  10708. +#ifdef SQLITE_ENABLE_SQLLOG
  10709. + void(*xSqllog)(void*,sqlite3*,const char*, int);
  10710. + void *pSqllogArg;
  10711. +#endif
  10712. +#ifdef SQLITE_VDBE_COVERAGE
  10713. + /* The following callback (if not NULL) is invoked on every VDBE branch
  10714. + ** operation. Set the callback using SQLITE_TESTCTRL_VDBE_COVERAGE.
  10715. + */
  10716. + void (*xVdbeBranch)(void*,unsigned iSrcLine,u8 eThis,u8 eMx); /* Callback */
  10717. + void *pVdbeBranchArg; /* 1st argument */
  10718. +#endif
  10719. +#ifdef SQLITE_ENABLE_DESERIALIZE
  10720. + sqlite3_int64 mxMemdbSize; /* Default max memdb size */
  10721. +#endif
  10722. +#ifndef SQLITE_UNTESTABLE
  10723. + int (*xTestCallback)(int); /* Invoked by sqlite3FaultSim() */
  10724. +#endif
  10725. + int bLocaltimeFault; /* True to fail localtime() calls */
  10726. + int iOnceResetThreshold; /* When to reset OP_Once counters */
  10727. + u32 szSorterRef; /* Min size in bytes to use sorter-refs */
  10728. + unsigned int iPrngSeed; /* Alternative fixed seed for the PRNG */
  10729. +};
  10730. +
  10731. +/*
  10732. +** This macro is used inside of assert() statements to indicate that
  10733. +** the assert is only valid on a well-formed database. Instead of:
  10734. +**
  10735. +** assert( X );
  10736. +**
  10737. +** One writes:
  10738. +**
  10739. +** assert( X || CORRUPT_DB );
  10740. +**
  10741. +** CORRUPT_DB is true during normal operation. CORRUPT_DB does not indicate
  10742. +** that the database is definitely corrupt, only that it might be corrupt.
  10743. +** For most test cases, CORRUPT_DB is set to false using a special
  10744. +** sqlite3_test_control(). This enables assert() statements to prove
  10745. +** things that are always true for well-formed databases.
  10746. +*/
  10747. +#define CORRUPT_DB (sqlite3Config.neverCorrupt==0)
  10748. +
  10749. +/*
  10750. +** Context pointer passed down through the tree-walk.
  10751. +*/
  10752. +struct Walker {
  10753. + Parse *pParse; /* Parser context. */
  10754. + int (*xExprCallback)(Walker*, Expr*); /* Callback for expressions */
  10755. + int (*xSelectCallback)(Walker*,Select*); /* Callback for SELECTs */
  10756. + void (*xSelectCallback2)(Walker*,Select*);/* Second callback for SELECTs */
  10757. + int walkerDepth; /* Number of subqueries */
  10758. + u16 eCode; /* A small processing code */
  10759. + union { /* Extra data for callback */
  10760. + NameContext *pNC; /* Naming context */
  10761. + int n; /* A counter */
  10762. + int iCur; /* A cursor number */
  10763. + SrcList *pSrcList; /* FROM clause */
  10764. + struct SrcCount *pSrcCount; /* Counting column references */
  10765. + struct CCurHint *pCCurHint; /* Used by codeCursorHint() */
  10766. + int *aiCol; /* array of column indexes */
  10767. + struct IdxCover *pIdxCover; /* Check for index coverage */
  10768. + struct IdxExprTrans *pIdxTrans; /* Convert idxed expr to column */
  10769. + ExprList *pGroupBy; /* GROUP BY clause */
  10770. + Select *pSelect; /* HAVING to WHERE clause ctx */
  10771. + struct WindowRewrite *pRewrite; /* Window rewrite context */
  10772. + struct WhereConst *pConst; /* WHERE clause constants */
  10773. + struct RenameCtx *pRename; /* RENAME COLUMN context */
  10774. + struct Table *pTab; /* Table of generated column */
  10775. + struct SrcList_item *pSrcItem; /* A single FROM clause item */
  10776. + } u;
  10777. +};
  10778. +
  10779. +/* Forward declarations */
  10780. +int sqlite3WalkExpr(Walker*, Expr*);
  10781. +int sqlite3WalkExprList(Walker*, ExprList*);
  10782. +int sqlite3WalkSelect(Walker*, Select*);
  10783. +int sqlite3WalkSelectExpr(Walker*, Select*);
  10784. +int sqlite3WalkSelectFrom(Walker*, Select*);
  10785. +int sqlite3ExprWalkNoop(Walker*, Expr*);
  10786. +int sqlite3SelectWalkNoop(Walker*, Select*);
  10787. +int sqlite3SelectWalkFail(Walker*, Select*);
  10788. +int sqlite3WalkerDepthIncrease(Walker*,Select*);
  10789. +void sqlite3WalkerDepthDecrease(Walker*,Select*);
  10790. +
  10791. +#ifdef SQLITE_DEBUG
  10792. +void sqlite3SelectWalkAssert2(Walker*, Select*);
  10793. +#endif
  10794. +
  10795. +/*
  10796. +** Return code from the parse-tree walking primitives and their
  10797. +** callbacks.
  10798. +*/
  10799. +#define WRC_Continue 0 /* Continue down into children */
  10800. +#define WRC_Prune 1 /* Omit children but continue walking siblings */
  10801. +#define WRC_Abort 2 /* Abandon the tree walk */
  10802. +
  10803. +/*
  10804. +** An instance of this structure represents a set of one or more CTEs
  10805. +** (common table expressions) created by a single WITH clause.
  10806. +*/
  10807. +struct With {
  10808. + int nCte; /* Number of CTEs in the WITH clause */
  10809. + With *pOuter; /* Containing WITH clause, or NULL */
  10810. + struct Cte { /* For each CTE in the WITH clause.... */
  10811. + char *zName; /* Name of this CTE */
  10812. + ExprList *pCols; /* List of explicit column names, or NULL */
  10813. + Select *pSelect; /* The definition of this CTE */
  10814. + const char *zCteErr; /* Error message for circular references */
  10815. + } a[1];
  10816. +};
  10817. +
  10818. +#ifdef SQLITE_DEBUG
  10819. +/*
  10820. +** An instance of the TreeView object is used for printing the content of
  10821. +** data structures on sqlite3DebugPrintf() using a tree-like view.
  10822. +*/
  10823. +struct TreeView {
  10824. + int iLevel; /* Which level of the tree we are on */
  10825. + u8 bLine[100]; /* Draw vertical in column i if bLine[i] is true */
  10826. +};
  10827. +#endif /* SQLITE_DEBUG */
  10828. +
  10829. +/*
  10830. +** This object is used in various ways, most (but not all) related to window
  10831. +** functions.
  10832. +**
  10833. +** (1) A single instance of this structure is attached to the
  10834. +** the Expr.y.pWin field for each window function in an expression tree.
  10835. +** This object holds the information contained in the OVER clause,
  10836. +** plus additional fields used during code generation.
  10837. +**
  10838. +** (2) All window functions in a single SELECT form a linked-list
  10839. +** attached to Select.pWin. The Window.pFunc and Window.pExpr
  10840. +** fields point back to the expression that is the window function.
  10841. +**
  10842. +** (3) The terms of the WINDOW clause of a SELECT are instances of this
  10843. +** object on a linked list attached to Select.pWinDefn.
  10844. +**
  10845. +** (4) For an aggregate function with a FILTER clause, an instance
  10846. +** of this object is stored in Expr.y.pWin with eFrmType set to
  10847. +** TK_FILTER. In this case the only field used is Window.pFilter.
  10848. +**
  10849. +** The uses (1) and (2) are really the same Window object that just happens
  10850. +** to be accessible in two different ways. Use case (3) are separate objects.
  10851. +*/
  10852. +struct Window {
  10853. + char *zName; /* Name of window (may be NULL) */
  10854. + char *zBase; /* Name of base window for chaining (may be NULL) */
  10855. + ExprList *pPartition; /* PARTITION BY clause */
  10856. + ExprList *pOrderBy; /* ORDER BY clause */
  10857. + u8 eFrmType; /* TK_RANGE, TK_GROUPS, TK_ROWS, or 0 */
  10858. + u8 eStart; /* UNBOUNDED, CURRENT, PRECEDING or FOLLOWING */
  10859. + u8 eEnd; /* UNBOUNDED, CURRENT, PRECEDING or FOLLOWING */
  10860. + u8 bImplicitFrame; /* True if frame was implicitly specified */
  10861. + u8 eExclude; /* TK_NO, TK_CURRENT, TK_TIES, TK_GROUP, or 0 */
  10862. + Expr *pStart; /* Expression for "<expr> PRECEDING" */
  10863. + Expr *pEnd; /* Expression for "<expr> FOLLOWING" */
  10864. + Window **ppThis; /* Pointer to this object in Select.pWin list */
  10865. + Window *pNextWin; /* Next window function belonging to this SELECT */
  10866. + Expr *pFilter; /* The FILTER expression */
  10867. + FuncDef *pFunc; /* The function */
  10868. + int iEphCsr; /* Partition buffer or Peer buffer */
  10869. + int regAccum; /* Accumulator */
  10870. + int regResult; /* Interim result */
  10871. + int csrApp; /* Function cursor (used by min/max) */
  10872. + int regApp; /* Function register (also used by min/max) */
  10873. + int regPart; /* Array of registers for PARTITION BY values */
  10874. + Expr *pOwner; /* Expression object this window is attached to */
  10875. + int nBufferCol; /* Number of columns in buffer table */
  10876. + int iArgCol; /* Offset of first argument for this function */
  10877. + int regOne; /* Register containing constant value 1 */
  10878. + int regStartRowid;
  10879. + int regEndRowid;
  10880. + u8 bExprArgs; /* Defer evaluation of window function arguments
  10881. + ** due to the SQLITE_SUBTYPE flag */
  10882. +};
  10883. +
  10884. +#ifndef SQLITE_OMIT_WINDOWFUNC
  10885. +void sqlite3WindowDelete(sqlite3*, Window*);
  10886. +void sqlite3WindowUnlinkFromSelect(Window*);
  10887. +void sqlite3WindowListDelete(sqlite3 *db, Window *p);
  10888. +Window *sqlite3WindowAlloc(Parse*, int, int, Expr*, int , Expr*, u8);
  10889. +void sqlite3WindowAttach(Parse*, Expr*, Window*);
  10890. +void sqlite3WindowLink(Select *pSel, Window *pWin);
  10891. +int sqlite3WindowCompare(Parse*, Window*, Window*, int);
  10892. +void sqlite3WindowCodeInit(Parse*, Select*);
  10893. +void sqlite3WindowCodeStep(Parse*, Select*, WhereInfo*, int, int);
  10894. +int sqlite3WindowRewrite(Parse*, Select*);
  10895. +int sqlite3ExpandSubquery(Parse*, struct SrcList_item*);
  10896. +void sqlite3WindowUpdate(Parse*, Window*, Window*, FuncDef*);
  10897. +Window *sqlite3WindowDup(sqlite3 *db, Expr *pOwner, Window *p);
  10898. +Window *sqlite3WindowListDup(sqlite3 *db, Window *p);
  10899. +void sqlite3WindowFunctions(void);
  10900. +void sqlite3WindowChain(Parse*, Window*, Window*);
  10901. +Window *sqlite3WindowAssemble(Parse*, Window*, ExprList*, ExprList*, Token*);
  10902. +#else
  10903. +# define sqlite3WindowDelete(a,b)
  10904. +# define sqlite3WindowFunctions()
  10905. +# define sqlite3WindowAttach(a,b,c)
  10906. +#endif
  10907. +
  10908. +/*
  10909. +** Assuming zIn points to the first byte of a UTF-8 character,
  10910. +** advance zIn to point to the first byte of the next UTF-8 character.
  10911. +*/
  10912. +#define SQLITE_SKIP_UTF8(zIn) { \
  10913. + if( (*(zIn++))>=0xc0 ){ \
  10914. + while( (*zIn & 0xc0)==0x80 ){ zIn++; } \
  10915. + } \
  10916. +}
  10917. +
  10918. +/*
  10919. +** The SQLITE_*_BKPT macros are substitutes for the error codes with
  10920. +** the same name but without the _BKPT suffix. These macros invoke
  10921. +** routines that report the line-number on which the error originated
  10922. +** using sqlite3_log(). The routines also provide a convenient place
  10923. +** to set a debugger breakpoint.
  10924. +*/
  10925. +int sqlite3ReportError(int iErr, int lineno, const char *zType);
  10926. +int sqlite3CorruptError(int);
  10927. +int sqlite3MisuseError(int);
  10928. +int sqlite3CantopenError(int);
  10929. +#define SQLITE_CORRUPT_BKPT sqlite3CorruptError(__LINE__)
  10930. +#define SQLITE_MISUSE_BKPT sqlite3MisuseError(__LINE__)
  10931. +#define SQLITE_CANTOPEN_BKPT sqlite3CantopenError(__LINE__)
  10932. +#ifdef SQLITE_DEBUG
  10933. + int sqlite3NomemError(int);
  10934. + int sqlite3IoerrnomemError(int);
  10935. +# define SQLITE_NOMEM_BKPT sqlite3NomemError(__LINE__)
  10936. +# define SQLITE_IOERR_NOMEM_BKPT sqlite3IoerrnomemError(__LINE__)
  10937. +#else
  10938. +# define SQLITE_NOMEM_BKPT SQLITE_NOMEM
  10939. +# define SQLITE_IOERR_NOMEM_BKPT SQLITE_IOERR_NOMEM
  10940. +#endif
  10941. +#if defined(SQLITE_DEBUG) || defined(SQLITE_ENABLE_CORRUPT_PGNO)
  10942. + int sqlite3CorruptPgnoError(int,Pgno);
  10943. +# define SQLITE_CORRUPT_PGNO(P) sqlite3CorruptPgnoError(__LINE__,(P))
  10944. +#else
  10945. +# define SQLITE_CORRUPT_PGNO(P) sqlite3CorruptError(__LINE__)
  10946. +#endif
  10947. +
  10948. +/*
  10949. +** FTS3 and FTS4 both require virtual table support
  10950. +*/
  10951. +#if defined(SQLITE_OMIT_VIRTUALTABLE)
  10952. +# undef SQLITE_ENABLE_FTS3
  10953. +# undef SQLITE_ENABLE_FTS4
  10954. +#endif
  10955. +
  10956. +/*
  10957. +** FTS4 is really an extension for FTS3. It is enabled using the
  10958. +** SQLITE_ENABLE_FTS3 macro. But to avoid confusion we also call
  10959. +** the SQLITE_ENABLE_FTS4 macro to serve as an alias for SQLITE_ENABLE_FTS3.
  10960. +*/
  10961. +#if defined(SQLITE_ENABLE_FTS4) && !defined(SQLITE_ENABLE_FTS3)
  10962. +# define SQLITE_ENABLE_FTS3 1
  10963. +#endif
  10964. +
  10965. +/*
  10966. +** The ctype.h header is needed for non-ASCII systems. It is also
  10967. +** needed by FTS3 when FTS3 is included in the amalgamation.
  10968. +*/
  10969. +#if !defined(SQLITE_ASCII) || \
  10970. + (defined(SQLITE_ENABLE_FTS3) && defined(SQLITE_AMALGAMATION))
  10971. +# include <ctype.h>
  10972. +#endif
  10973. +
  10974. +/*
  10975. +** The following macros mimic the standard library functions toupper(),
  10976. +** isspace(), isalnum(), isdigit() and isxdigit(), respectively. The
  10977. +** sqlite versions only work for ASCII characters, regardless of locale.
  10978. +*/
  10979. +#ifdef SQLITE_ASCII
  10980. +# define sqlite3Toupper(x) ((x)&~(sqlite3CtypeMap[(unsigned char)(x)]&0x20))
  10981. +# define sqlite3Isspace(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x01)
  10982. +# define sqlite3Isalnum(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x06)
  10983. +# define sqlite3Isalpha(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x02)
  10984. +# define sqlite3Isdigit(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x04)
  10985. +# define sqlite3Isxdigit(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x08)
  10986. +# define sqlite3Tolower(x) (sqlite3UpperToLower[(unsigned char)(x)])
  10987. +# define sqlite3Isquote(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x80)
  10988. +#else
  10989. +# define sqlite3Toupper(x) toupper((unsigned char)(x))
  10990. +# define sqlite3Isspace(x) isspace((unsigned char)(x))
  10991. +# define sqlite3Isalnum(x) isalnum((unsigned char)(x))
  10992. +# define sqlite3Isalpha(x) isalpha((unsigned char)(x))
  10993. +# define sqlite3Isdigit(x) isdigit((unsigned char)(x))
  10994. +# define sqlite3Isxdigit(x) isxdigit((unsigned char)(x))
  10995. +# define sqlite3Tolower(x) tolower((unsigned char)(x))
  10996. +# define sqlite3Isquote(x) ((x)=='"'||(x)=='\''||(x)=='['||(x)=='`')
  10997. +#endif
  10998. +int sqlite3IsIdChar(u8);
  10999. +
  11000. +/*
  11001. +** Internal function prototypes
  11002. +*/
  11003. +int sqlite3StrICmp(const char*,const char*);
  11004. +int sqlite3Strlen30(const char*);
  11005. +#define sqlite3Strlen30NN(C) (strlen(C)&0x3fffffff)
  11006. +char *sqlite3ColumnType(Column*,char*);
  11007. +#define sqlite3StrNICmp sqlite3_strnicmp
  11008. +
  11009. +int sqlite3MallocInit(void);
  11010. +void sqlite3MallocEnd(void);
  11011. +void *sqlite3Malloc(u64);
  11012. +void *sqlite3MallocZero(u64);
  11013. +void *sqlite3DbMallocZero(sqlite3*, u64);
  11014. +void *sqlite3DbMallocRaw(sqlite3*, u64);
  11015. +void *sqlite3DbMallocRawNN(sqlite3*, u64);
  11016. +char *sqlite3DbStrDup(sqlite3*,const char*);
  11017. +char *sqlite3DbStrNDup(sqlite3*,const char*, u64);
  11018. +char *sqlite3DbSpanDup(sqlite3*,const char*,const char*);
  11019. +void *sqlite3Realloc(void*, u64);
  11020. +void *sqlite3DbReallocOrFree(sqlite3 *, void *, u64);
  11021. +void *sqlite3DbRealloc(sqlite3 *, void *, u64);
  11022. +void sqlite3DbFree(sqlite3*, void*);
  11023. +void sqlite3DbFreeNN(sqlite3*, void*);
  11024. +int sqlite3MallocSize(void*);
  11025. +int sqlite3DbMallocSize(sqlite3*, void*);
  11026. +void *sqlite3PageMalloc(int);
  11027. +void sqlite3PageFree(void*);
  11028. +void sqlite3MemSetDefault(void);
  11029. +#ifndef SQLITE_UNTESTABLE
  11030. +void sqlite3BenignMallocHooks(void (*)(void), void (*)(void));
  11031. +#endif
  11032. +int sqlite3HeapNearlyFull(void);
  11033. +
  11034. +/*
  11035. +** On systems with ample stack space and that support alloca(), make
  11036. +** use of alloca() to obtain space for large automatic objects. By default,
  11037. +** obtain space from malloc().
  11038. +**
  11039. +** The alloca() routine never returns NULL. This will cause code paths
  11040. +** that deal with sqlite3StackAlloc() failures to be unreachable.
  11041. +*/
  11042. +#ifdef SQLITE_USE_ALLOCA
  11043. +# define sqlite3StackAllocRaw(D,N) alloca(N)
  11044. +# define sqlite3StackAllocZero(D,N) memset(alloca(N), 0, N)
  11045. +# define sqlite3StackFree(D,P)
  11046. +#else
  11047. +# define sqlite3StackAllocRaw(D,N) sqlite3DbMallocRaw(D,N)
  11048. +# define sqlite3StackAllocZero(D,N) sqlite3DbMallocZero(D,N)
  11049. +# define sqlite3StackFree(D,P) sqlite3DbFree(D,P)
  11050. +#endif
  11051. +
  11052. +/* Do not allow both MEMSYS5 and MEMSYS3 to be defined together. If they
  11053. +** are, disable MEMSYS3
  11054. +*/
  11055. +#ifdef SQLITE_ENABLE_MEMSYS5
  11056. +const sqlite3_mem_methods *sqlite3MemGetMemsys5(void);
  11057. +#undef SQLITE_ENABLE_MEMSYS3
  11058. +#endif
  11059. +#ifdef SQLITE_ENABLE_MEMSYS3
  11060. +const sqlite3_mem_methods *sqlite3MemGetMemsys3(void);
  11061. +#endif
  11062. +
  11063. +
  11064. +#ifndef SQLITE_MUTEX_OMIT
  11065. + sqlite3_mutex_methods const *sqlite3DefaultMutex(void);
  11066. + sqlite3_mutex_methods const *sqlite3NoopMutex(void);
  11067. + sqlite3_mutex *sqlite3MutexAlloc(int);
  11068. + int sqlite3MutexInit(void);
  11069. + int sqlite3MutexEnd(void);
  11070. +#endif
  11071. +#if !defined(SQLITE_MUTEX_OMIT) && !defined(SQLITE_MUTEX_NOOP)
  11072. + void sqlite3MemoryBarrier(void);
  11073. +#else
  11074. +# define sqlite3MemoryBarrier()
  11075. +#endif
  11076. +
  11077. +sqlite3_int64 sqlite3StatusValue(int);
  11078. +void sqlite3StatusUp(int, int);
  11079. +void sqlite3StatusDown(int, int);
  11080. +void sqlite3StatusHighwater(int, int);
  11081. +int sqlite3LookasideUsed(sqlite3*,int*);
  11082. +
  11083. +/* Access to mutexes used by sqlite3_status() */
  11084. +sqlite3_mutex *sqlite3Pcache1Mutex(void);
  11085. +sqlite3_mutex *sqlite3MallocMutex(void);
  11086. +
  11087. +#if defined(SQLITE_ENABLE_MULTITHREADED_CHECKS) && !defined(SQLITE_MUTEX_OMIT)
  11088. +void sqlite3MutexWarnOnContention(sqlite3_mutex*);
  11089. +#else
  11090. +# define sqlite3MutexWarnOnContention(x)
  11091. +#endif
  11092. +
  11093. +#ifndef SQLITE_OMIT_FLOATING_POINT
  11094. +# define EXP754 (((u64)0x7ff)<<52)
  11095. +# define MAN754 ((((u64)1)<<52)-1)
  11096. +# define IsNaN(X) (((X)&EXP754)==EXP754 && ((X)&MAN754)!=0)
  11097. + int sqlite3IsNaN(double);
  11098. +#else
  11099. +# define IsNaN(X) 0
  11100. +# define sqlite3IsNaN(X) 0
  11101. +#endif
  11102. +
  11103. +/*
  11104. +** An instance of the following structure holds information about SQL
  11105. +** functions arguments that are the parameters to the printf() function.
  11106. +*/
  11107. +struct PrintfArguments {
  11108. + int nArg; /* Total number of arguments */
  11109. + int nUsed; /* Number of arguments used so far */
  11110. + sqlite3_value **apArg; /* The argument values */
  11111. +};
  11112. +
  11113. +char *sqlite3MPrintf(sqlite3*,const char*, ...);
  11114. +char *sqlite3VMPrintf(sqlite3*,const char*, va_list);
  11115. +#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
  11116. + void sqlite3DebugPrintf(const char*, ...);
  11117. +#endif
  11118. +#if defined(SQLITE_TEST)
  11119. + void *sqlite3TestTextToPtr(const char*);
  11120. +#endif
  11121. +
  11122. +#if defined(SQLITE_DEBUG)
  11123. + void sqlite3TreeViewExpr(TreeView*, const Expr*, u8);
  11124. + void sqlite3TreeViewBareExprList(TreeView*, const ExprList*, const char*);
  11125. + void sqlite3TreeViewExprList(TreeView*, const ExprList*, u8, const char*);
  11126. + void sqlite3TreeViewSrcList(TreeView*, const SrcList*);
  11127. + void sqlite3TreeViewSelect(TreeView*, const Select*, u8);
  11128. + void sqlite3TreeViewWith(TreeView*, const With*, u8);
  11129. +#ifndef SQLITE_OMIT_WINDOWFUNC
  11130. + void sqlite3TreeViewWindow(TreeView*, const Window*, u8);
  11131. + void sqlite3TreeViewWinFunc(TreeView*, const Window*, u8);
  11132. +#endif
  11133. +#endif
  11134. +
  11135. +
  11136. +void sqlite3SetString(char **, sqlite3*, const char*);
  11137. +void sqlite3ErrorMsg(Parse*, const char*, ...);
  11138. +int sqlite3ErrorToParser(sqlite3*,int);
  11139. +void sqlite3Dequote(char*);
  11140. +void sqlite3DequoteExpr(Expr*);
  11141. +void sqlite3TokenInit(Token*,char*);
  11142. +int sqlite3KeywordCode(const unsigned char*, int);
  11143. +int sqlite3RunParser(Parse*, const char*, char **);
  11144. +void sqlite3FinishCoding(Parse*);
  11145. +int sqlite3GetTempReg(Parse*);
  11146. +void sqlite3ReleaseTempReg(Parse*,int);
  11147. +int sqlite3GetTempRange(Parse*,int);
  11148. +void sqlite3ReleaseTempRange(Parse*,int,int);
  11149. +void sqlite3ClearTempRegCache(Parse*);
  11150. +#ifdef SQLITE_DEBUG
  11151. +int sqlite3NoTempsInRange(Parse*,int,int);
  11152. +#endif
  11153. +Expr *sqlite3ExprAlloc(sqlite3*,int,const Token*,int);
  11154. +Expr *sqlite3Expr(sqlite3*,int,const char*);
  11155. +void sqlite3ExprAttachSubtrees(sqlite3*,Expr*,Expr*,Expr*);
  11156. +Expr *sqlite3PExpr(Parse*, int, Expr*, Expr*);
  11157. +void sqlite3PExprAddSelect(Parse*, Expr*, Select*);
  11158. +Expr *sqlite3ExprAnd(Parse*,Expr*, Expr*);
  11159. +Expr *sqlite3ExprSimplifiedAndOr(Expr*);
  11160. +Expr *sqlite3ExprFunction(Parse*,ExprList*, Token*, int);
  11161. +void sqlite3ExprFunctionUsable(Parse*,Expr*,FuncDef*);
  11162. +void sqlite3ExprAssignVarNumber(Parse*, Expr*, u32);
  11163. +void sqlite3ExprDelete(sqlite3*, Expr*);
  11164. +void sqlite3ExprUnmapAndDelete(Parse*, Expr*);
  11165. +ExprList *sqlite3ExprListAppend(Parse*,ExprList*,Expr*);
  11166. +ExprList *sqlite3ExprListAppendVector(Parse*,ExprList*,IdList*,Expr*);
  11167. +void sqlite3ExprListSetSortOrder(ExprList*,int,int);
  11168. +void sqlite3ExprListSetName(Parse*,ExprList*,Token*,int);
  11169. +void sqlite3ExprListSetSpan(Parse*,ExprList*,const char*,const char*);
  11170. +void sqlite3ExprListDelete(sqlite3*, ExprList*);
  11171. +u32 sqlite3ExprListFlags(const ExprList*);
  11172. +int sqlite3IndexHasDuplicateRootPage(Index*);
  11173. +int sqlite3Init(sqlite3*, char**);
  11174. +int sqlite3InitCallback(void*, int, char**, char**);
  11175. +int sqlite3InitOne(sqlite3*, int, char**, u32);
  11176. +void sqlite3Pragma(Parse*,Token*,Token*,Token*,int);
  11177. +#ifndef SQLITE_OMIT_VIRTUALTABLE
  11178. +Module *sqlite3PragmaVtabRegister(sqlite3*,const char *zName);
  11179. +#endif
  11180. +void sqlite3ResetAllSchemasOfConnection(sqlite3*);
  11181. +void sqlite3ResetOneSchema(sqlite3*,int);
  11182. +void sqlite3CollapseDatabaseArray(sqlite3*);
  11183. +void sqlite3CommitInternalChanges(sqlite3*);
  11184. +void sqlite3DeleteColumnNames(sqlite3*,Table*);
  11185. +int sqlite3ColumnsFromExprList(Parse*,ExprList*,i16*,Column**);
  11186. +void sqlite3SelectAddColumnTypeAndCollation(Parse*,Table*,Select*,char);
  11187. +Table *sqlite3ResultSetOfSelect(Parse*,Select*,char);
  11188. +void sqlite3OpenMasterTable(Parse *, int);
  11189. +Index *sqlite3PrimaryKeyIndex(Table*);
  11190. +i16 sqlite3TableColumnToIndex(Index*, i16);
  11191. +#ifdef SQLITE_OMIT_GENERATED_COLUMNS
  11192. +# define sqlite3TableColumnToStorage(T,X) (X) /* No-op pass-through */
  11193. +# define sqlite3StorageColumnToTable(T,X) (X) /* No-op pass-through */
  11194. +#else
  11195. + i16 sqlite3TableColumnToStorage(Table*, i16);
  11196. + i16 sqlite3StorageColumnToTable(Table*, i16);
  11197. +#endif
  11198. +void sqlite3StartTable(Parse*,Token*,Token*,int,int,int,int);
  11199. +#if SQLITE_ENABLE_HIDDEN_COLUMNS
  11200. + void sqlite3ColumnPropertiesFromName(Table*, Column*);
  11201. +#else
  11202. +# define sqlite3ColumnPropertiesFromName(T,C) /* no-op */
  11203. +#endif
  11204. +void sqlite3AddColumn(Parse*,Token*,Token*);
  11205. +void sqlite3AddNotNull(Parse*, int);
  11206. +void sqlite3AddPrimaryKey(Parse*, ExprList*, int, int, int);
  11207. +void sqlite3AddCheckConstraint(Parse*, Expr*);
  11208. +void sqlite3AddDefaultValue(Parse*,Expr*,const char*,const char*);
  11209. +void sqlite3AddCollateType(Parse*, Token*);
  11210. +void sqlite3AddGenerated(Parse*,Expr*,Token*);
  11211. +void sqlite3EndTable(Parse*,Token*,Token*,u8,Select*);
  11212. +int sqlite3ParseUri(const char*,const char*,unsigned int*,
  11213. + sqlite3_vfs**,char**,char **);
  11214. +#define sqlite3CodecQueryParameters(A,B,C) 0
  11215. +Btree *sqlite3DbNameToBtree(sqlite3*,const char*);
  11216. +
  11217. +#ifdef SQLITE_UNTESTABLE
  11218. +# define sqlite3FaultSim(X) SQLITE_OK
  11219. +#else
  11220. + int sqlite3FaultSim(int);
  11221. +#endif
  11222. +
  11223. +Bitvec *sqlite3BitvecCreate(u32);
  11224. +int sqlite3BitvecTest(Bitvec*, u32);
  11225. +int sqlite3BitvecTestNotNull(Bitvec*, u32);
  11226. +int sqlite3BitvecSet(Bitvec*, u32);
  11227. +void sqlite3BitvecClear(Bitvec*, u32, void*);
  11228. +void sqlite3BitvecDestroy(Bitvec*);
  11229. +u32 sqlite3BitvecSize(Bitvec*);
  11230. +#ifndef SQLITE_UNTESTABLE
  11231. +int sqlite3BitvecBuiltinTest(int,int*);
  11232. +#endif
  11233. +
  11234. +RowSet *sqlite3RowSetInit(sqlite3*);
  11235. +void sqlite3RowSetDelete(void*);
  11236. +void sqlite3RowSetClear(void*);
  11237. +void sqlite3RowSetInsert(RowSet*, i64);
  11238. +int sqlite3RowSetTest(RowSet*, int iBatch, i64);
  11239. +int sqlite3RowSetNext(RowSet*, i64*);
  11240. +
  11241. +void sqlite3CreateView(Parse*,Token*,Token*,Token*,ExprList*,Select*,int,int);
  11242. +
  11243. +#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
  11244. + int sqlite3ViewGetColumnNames(Parse*,Table*);
  11245. +#else
  11246. +# define sqlite3ViewGetColumnNames(A,B) 0
  11247. +#endif
  11248. +
  11249. +#if SQLITE_MAX_ATTACHED>30
  11250. + int sqlite3DbMaskAllZero(yDbMask);
  11251. +#endif
  11252. +void sqlite3DropTable(Parse*, SrcList*, int, int);
  11253. +void sqlite3CodeDropTable(Parse*, Table*, int, int);
  11254. +void sqlite3DeleteTable(sqlite3*, Table*);
  11255. +void sqlite3FreeIndex(sqlite3*, Index*);
  11256. +#ifndef SQLITE_OMIT_AUTOINCREMENT
  11257. + void sqlite3AutoincrementBegin(Parse *pParse);
  11258. + void sqlite3AutoincrementEnd(Parse *pParse);
  11259. +#else
  11260. +# define sqlite3AutoincrementBegin(X)
  11261. +# define sqlite3AutoincrementEnd(X)
  11262. +#endif
  11263. +void sqlite3Insert(Parse*, SrcList*, Select*, IdList*, int, Upsert*);
  11264. +#ifndef SQLITE_OMIT_GENERATED_COLUMNS
  11265. + void sqlite3ComputeGeneratedColumns(Parse*, int, Table*);
  11266. +#endif
  11267. +void *sqlite3ArrayAllocate(sqlite3*,void*,int,int*,int*);
  11268. +IdList *sqlite3IdListAppend(Parse*, IdList*, Token*);
  11269. +int sqlite3IdListIndex(IdList*,const char*);
  11270. +SrcList *sqlite3SrcListEnlarge(Parse*, SrcList*, int, int);
  11271. +SrcList *sqlite3SrcListAppend(Parse*, SrcList*, Token*, Token*);
  11272. +SrcList *sqlite3SrcListAppendFromTerm(Parse*, SrcList*, Token*, Token*,
  11273. + Token*, Select*, Expr*, IdList*);
  11274. +void sqlite3SrcListIndexedBy(Parse *, SrcList *, Token *);
  11275. +void sqlite3SrcListFuncArgs(Parse*, SrcList*, ExprList*);
  11276. +int sqlite3IndexedByLookup(Parse *, struct SrcList_item *);
  11277. +void sqlite3SrcListShiftJoinType(SrcList*);
  11278. +void sqlite3SrcListAssignCursors(Parse*, SrcList*);
  11279. +void sqlite3IdListDelete(sqlite3*, IdList*);
  11280. +void sqlite3SrcListDelete(sqlite3*, SrcList*);
  11281. +Index *sqlite3AllocateIndexObject(sqlite3*,i16,int,char**);
  11282. +void sqlite3CreateIndex(Parse*,Token*,Token*,SrcList*,ExprList*,int,Token*,
  11283. + Expr*, int, int, u8);
  11284. +void sqlite3DropIndex(Parse*, SrcList*, int);
  11285. +int sqlite3Select(Parse*, Select*, SelectDest*);
  11286. +Select *sqlite3SelectNew(Parse*,ExprList*,SrcList*,Expr*,ExprList*,
  11287. + Expr*,ExprList*,u32,Expr*);
  11288. +void sqlite3SelectDelete(sqlite3*, Select*);
  11289. +void sqlite3SelectReset(Parse*, Select*);
  11290. +Table *sqlite3SrcListLookup(Parse*, SrcList*);
  11291. +int sqlite3IsReadOnly(Parse*, Table*, int);
  11292. +void sqlite3OpenTable(Parse*, int iCur, int iDb, Table*, int);
  11293. +#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
  11294. +Expr *sqlite3LimitWhere(Parse*,SrcList*,Expr*,ExprList*,Expr*,char*);
  11295. +#endif
  11296. +void sqlite3DeleteFrom(Parse*, SrcList*, Expr*, ExprList*, Expr*);
  11297. +void sqlite3Update(Parse*, SrcList*, ExprList*,Expr*,int,ExprList*,Expr*,
  11298. + Upsert*);
  11299. +WhereInfo *sqlite3WhereBegin(Parse*,SrcList*,Expr*,ExprList*,ExprList*,u16,int);
  11300. +void sqlite3WhereEnd(WhereInfo*);
  11301. +LogEst sqlite3WhereOutputRowCount(WhereInfo*);
  11302. +int sqlite3WhereIsDistinct(WhereInfo*);
  11303. +int sqlite3WhereIsOrdered(WhereInfo*);
  11304. +int sqlite3WhereOrderByLimitOptLabel(WhereInfo*);
  11305. +int sqlite3WhereIsSorted(WhereInfo*);
  11306. +int sqlite3WhereContinueLabel(WhereInfo*);
  11307. +int sqlite3WhereBreakLabel(WhereInfo*);
  11308. +int sqlite3WhereOkOnePass(WhereInfo*, int*);
  11309. +#define ONEPASS_OFF 0 /* Use of ONEPASS not allowed */
  11310. +#define ONEPASS_SINGLE 1 /* ONEPASS valid for a single row update */
  11311. +#define ONEPASS_MULTI 2 /* ONEPASS is valid for multiple rows */
  11312. +int sqlite3WhereUsesDeferredSeek(WhereInfo*);
  11313. +void sqlite3ExprCodeLoadIndexColumn(Parse*, Index*, int, int, int);
  11314. +int sqlite3ExprCodeGetColumn(Parse*, Table*, int, int, int, u8);
  11315. +void sqlite3ExprCodeGetColumnOfTable(Vdbe*, Table*, int, int, int);
  11316. +void sqlite3ExprCodeMove(Parse*, int, int, int);
  11317. +void sqlite3ExprCode(Parse*, Expr*, int);
  11318. +#ifndef SQLITE_OMIT_GENERATED_COLUMNS
  11319. +void sqlite3ExprCodeGeneratedColumn(Parse*, Column*, int);
  11320. +#endif
  11321. +void sqlite3ExprCodeCopy(Parse*, Expr*, int);
  11322. +void sqlite3ExprCodeFactorable(Parse*, Expr*, int);
  11323. +int sqlite3ExprCodeRunJustOnce(Parse*, Expr*, int);
  11324. +int sqlite3ExprCodeTemp(Parse*, Expr*, int*);
  11325. +int sqlite3ExprCodeTarget(Parse*, Expr*, int);
  11326. +int sqlite3ExprCodeExprList(Parse*, ExprList*, int, int, u8);
  11327. +#define SQLITE_ECEL_DUP 0x01 /* Deep, not shallow copies */
  11328. +#define SQLITE_ECEL_FACTOR 0x02 /* Factor out constant terms */
  11329. +#define SQLITE_ECEL_REF 0x04 /* Use ExprList.u.x.iOrderByCol */
  11330. +#define SQLITE_ECEL_OMITREF 0x08 /* Omit if ExprList.u.x.iOrderByCol */
  11331. +void sqlite3ExprIfTrue(Parse*, Expr*, int, int);
  11332. +void sqlite3ExprIfFalse(Parse*, Expr*, int, int);
  11333. +void sqlite3ExprIfFalseDup(Parse*, Expr*, int, int);
  11334. +Table *sqlite3FindTable(sqlite3*,const char*, const char*);
  11335. +#define LOCATE_VIEW 0x01
  11336. +#define LOCATE_NOERR 0x02
  11337. +Table *sqlite3LocateTable(Parse*,u32 flags,const char*, const char*);
  11338. +Table *sqlite3LocateTableItem(Parse*,u32 flags,struct SrcList_item *);
  11339. +Index *sqlite3FindIndex(sqlite3*,const char*, const char*);
  11340. +void sqlite3UnlinkAndDeleteTable(sqlite3*,int,const char*);
  11341. +void sqlite3UnlinkAndDeleteIndex(sqlite3*,int,const char*);
  11342. +void sqlite3Vacuum(Parse*,Token*,Expr*);
  11343. +int sqlite3RunVacuum(char**, sqlite3*, int, sqlite3_value*);
  11344. +char *sqlite3NameFromToken(sqlite3*, Token*);
  11345. +int sqlite3ExprCompare(Parse*,Expr*, Expr*, int);
  11346. +int sqlite3ExprCompareSkip(Expr*, Expr*, int);
  11347. +int sqlite3ExprListCompare(ExprList*, ExprList*, int);
  11348. +int sqlite3ExprImpliesExpr(Parse*,Expr*, Expr*, int);
  11349. +int sqlite3ExprImpliesNonNullRow(Expr*,int);
  11350. +void sqlite3ExprAnalyzeAggregates(NameContext*, Expr*);
  11351. +void sqlite3ExprAnalyzeAggList(NameContext*,ExprList*);
  11352. +int sqlite3ExprCoveredByIndex(Expr*, int iCur, Index *pIdx);
  11353. +int sqlite3FunctionUsesThisSrc(Expr*, SrcList*);
  11354. +Vdbe *sqlite3GetVdbe(Parse*);
  11355. +#ifndef SQLITE_UNTESTABLE
  11356. +void sqlite3PrngSaveState(void);
  11357. +void sqlite3PrngRestoreState(void);
  11358. +#endif
  11359. +void sqlite3RollbackAll(sqlite3*,int);
  11360. +void sqlite3CodeVerifySchema(Parse*, int);
  11361. +void sqlite3CodeVerifyNamedSchema(Parse*, const char *zDb);
  11362. +void sqlite3BeginTransaction(Parse*, int);
  11363. +void sqlite3EndTransaction(Parse*,int);
  11364. +void sqlite3Savepoint(Parse*, int, Token*);
  11365. +void sqlite3CloseSavepoints(sqlite3 *);
  11366. +void sqlite3LeaveMutexAndCloseZombie(sqlite3*);
  11367. +u32 sqlite3IsTrueOrFalse(const char*);
  11368. +int sqlite3ExprIdToTrueFalse(Expr*);
  11369. +int sqlite3ExprTruthValue(const Expr*);
  11370. +int sqlite3ExprIsConstant(Expr*);
  11371. +int sqlite3ExprIsConstantNotJoin(Expr*);
  11372. +int sqlite3ExprIsConstantOrFunction(Expr*, u8);
  11373. +int sqlite3ExprIsConstantOrGroupBy(Parse*, Expr*, ExprList*);
  11374. +int sqlite3ExprIsTableConstant(Expr*,int);
  11375. +#ifdef SQLITE_ENABLE_CURSOR_HINTS
  11376. +int sqlite3ExprContainsSubquery(Expr*);
  11377. +#endif
  11378. +int sqlite3ExprIsInteger(Expr*, int*);
  11379. +int sqlite3ExprCanBeNull(const Expr*);
  11380. +int sqlite3ExprNeedsNoAffinityChange(const Expr*, char);
  11381. +int sqlite3IsRowid(const char*);
  11382. +void sqlite3GenerateRowDelete(
  11383. + Parse*,Table*,Trigger*,int,int,int,i16,u8,u8,u8,int);
  11384. +void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int, int*, int);
  11385. +int sqlite3GenerateIndexKey(Parse*, Index*, int, int, int, int*,Index*,int);
  11386. +void sqlite3ResolvePartIdxLabel(Parse*,int);
  11387. +int sqlite3ExprReferencesUpdatedColumn(Expr*,int*,int);
  11388. +void sqlite3GenerateConstraintChecks(Parse*,Table*,int*,int,int,int,int,
  11389. + u8,u8,int,int*,int*,Upsert*);
  11390. +#ifdef SQLITE_ENABLE_NULL_TRIM
  11391. + void sqlite3SetMakeRecordP5(Vdbe*,Table*);
  11392. +#else
  11393. +# define sqlite3SetMakeRecordP5(A,B)
  11394. +#endif
  11395. +void sqlite3CompleteInsertion(Parse*,Table*,int,int,int,int*,int,int,int);
  11396. +int sqlite3OpenTableAndIndices(Parse*, Table*, int, u8, int, u8*, int*, int*);
  11397. +void sqlite3BeginWriteOperation(Parse*, int, int);
  11398. +void sqlite3MultiWrite(Parse*);
  11399. +void sqlite3MayAbort(Parse*);
  11400. +void sqlite3HaltConstraint(Parse*, int, int, char*, i8, u8);
  11401. +void sqlite3UniqueConstraint(Parse*, int, Index*);
  11402. +void sqlite3RowidConstraint(Parse*, int, Table*);
  11403. +Expr *sqlite3ExprDup(sqlite3*,Expr*,int);
  11404. +ExprList *sqlite3ExprListDup(sqlite3*,ExprList*,int);
  11405. +SrcList *sqlite3SrcListDup(sqlite3*,SrcList*,int);
  11406. +IdList *sqlite3IdListDup(sqlite3*,IdList*);
  11407. +Select *sqlite3SelectDup(sqlite3*,Select*,int);
  11408. +FuncDef *sqlite3FunctionSearch(int,const char*);
  11409. +void sqlite3InsertBuiltinFuncs(FuncDef*,int);
  11410. +FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,u8,u8);
  11411. +void sqlite3RegisterBuiltinFunctions(void);
  11412. +void sqlite3RegisterDateTimeFunctions(void);
  11413. +void sqlite3RegisterPerConnectionBuiltinFunctions(sqlite3*);
  11414. +int sqlite3SafetyCheckOk(sqlite3*);
  11415. +int sqlite3SafetyCheckSickOrOk(sqlite3*);
  11416. +void sqlite3ChangeCookie(Parse*, int);
  11417. +
  11418. +#if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
  11419. +void sqlite3MaterializeView(Parse*, Table*, Expr*, ExprList*,Expr*,int);
  11420. +#endif
  11421. +
  11422. +#ifndef SQLITE_OMIT_TRIGGER
  11423. + void sqlite3BeginTrigger(Parse*, Token*,Token*,int,int,IdList*,SrcList*,
  11424. + Expr*,int, int);
  11425. + void sqlite3FinishTrigger(Parse*, TriggerStep*, Token*);
  11426. + void sqlite3DropTrigger(Parse*, SrcList*, int);
  11427. + void sqlite3DropTriggerPtr(Parse*, Trigger*);
  11428. + Trigger *sqlite3TriggersExist(Parse *, Table*, int, ExprList*, int *pMask);
  11429. + Trigger *sqlite3TriggerList(Parse *, Table *);
  11430. + void sqlite3CodeRowTrigger(Parse*, Trigger *, int, ExprList*, int, Table *,
  11431. + int, int, int);
  11432. + void sqlite3CodeRowTriggerDirect(Parse *, Trigger *, Table *, int, int, int);
  11433. + void sqliteViewTriggers(Parse*, Table*, Expr*, int, ExprList*);
  11434. + void sqlite3DeleteTriggerStep(sqlite3*, TriggerStep*);
  11435. + TriggerStep *sqlite3TriggerSelectStep(sqlite3*,Select*,
  11436. + const char*,const char*);
  11437. + TriggerStep *sqlite3TriggerInsertStep(Parse*,Token*, IdList*,
  11438. + Select*,u8,Upsert*,
  11439. + const char*,const char*);
  11440. + TriggerStep *sqlite3TriggerUpdateStep(Parse*,Token*,ExprList*, Expr*, u8,
  11441. + const char*,const char*);
  11442. + TriggerStep *sqlite3TriggerDeleteStep(Parse*,Token*, Expr*,
  11443. + const char*,const char*);
  11444. + void sqlite3DeleteTrigger(sqlite3*, Trigger*);
  11445. + void sqlite3UnlinkAndDeleteTrigger(sqlite3*,int,const char*);
  11446. + u32 sqlite3TriggerColmask(Parse*,Trigger*,ExprList*,int,int,Table*,int);
  11447. +# define sqlite3ParseToplevel(p) ((p)->pToplevel ? (p)->pToplevel : (p))
  11448. +# define sqlite3IsToplevel(p) ((p)->pToplevel==0)
  11449. +#else
  11450. +# define sqlite3TriggersExist(B,C,D,E,F) 0
  11451. +# define sqlite3DeleteTrigger(A,B)
  11452. +# define sqlite3DropTriggerPtr(A,B)
  11453. +# define sqlite3UnlinkAndDeleteTrigger(A,B,C)
  11454. +# define sqlite3CodeRowTrigger(A,B,C,D,E,F,G,H,I)
  11455. +# define sqlite3CodeRowTriggerDirect(A,B,C,D,E,F)
  11456. +# define sqlite3TriggerList(X, Y) 0
  11457. +# define sqlite3ParseToplevel(p) p
  11458. +# define sqlite3IsToplevel(p) 1
  11459. +# define sqlite3TriggerColmask(A,B,C,D,E,F,G) 0
  11460. +#endif
  11461. +
  11462. +int sqlite3JoinType(Parse*, Token*, Token*, Token*);
  11463. +void sqlite3SetJoinExpr(Expr*,int);
  11464. +void sqlite3CreateForeignKey(Parse*, ExprList*, Token*, ExprList*, int);
  11465. +void sqlite3DeferForeignKey(Parse*, int);
  11466. +#ifndef SQLITE_OMIT_AUTHORIZATION
  11467. + void sqlite3AuthRead(Parse*,Expr*,Schema*,SrcList*);
  11468. + int sqlite3AuthCheck(Parse*,int, const char*, const char*, const char*);
  11469. + void sqlite3AuthContextPush(Parse*, AuthContext*, const char*);
  11470. + void sqlite3AuthContextPop(AuthContext*);
  11471. + int sqlite3AuthReadCol(Parse*, const char *, const char *, int);
  11472. +#else
  11473. +# define sqlite3AuthRead(a,b,c,d)
  11474. +# define sqlite3AuthCheck(a,b,c,d,e) SQLITE_OK
  11475. +# define sqlite3AuthContextPush(a,b,c)
  11476. +# define sqlite3AuthContextPop(a) ((void)(a))
  11477. +#endif
  11478. +int sqlite3DbIsNamed(sqlite3 *db, int iDb, const char *zName);
  11479. +void sqlite3Attach(Parse*, Expr*, Expr*, Expr*);
  11480. +void sqlite3Detach(Parse*, Expr*);
  11481. +void sqlite3FixInit(DbFixer*, Parse*, int, const char*, const Token*);
  11482. +int sqlite3FixSrcList(DbFixer*, SrcList*);
  11483. +int sqlite3FixSelect(DbFixer*, Select*);
  11484. +int sqlite3FixExpr(DbFixer*, Expr*);
  11485. +int sqlite3FixExprList(DbFixer*, ExprList*);
  11486. +int sqlite3FixTriggerStep(DbFixer*, TriggerStep*);
  11487. +int sqlite3RealSameAsInt(double,sqlite3_int64);
  11488. +int sqlite3AtoF(const char *z, double*, int, u8);
  11489. +int sqlite3GetInt32(const char *, int*);
  11490. +int sqlite3Atoi(const char*);
  11491. +#ifndef SQLITE_OMIT_UTF16
  11492. +int sqlite3Utf16ByteLen(const void *pData, int nChar);
  11493. +#endif
  11494. +int sqlite3Utf8CharLen(const char *pData, int nByte);
  11495. +u32 sqlite3Utf8Read(const u8**);
  11496. +LogEst sqlite3LogEst(u64);
  11497. +LogEst sqlite3LogEstAdd(LogEst,LogEst);
  11498. +#ifndef SQLITE_OMIT_VIRTUALTABLE
  11499. +LogEst sqlite3LogEstFromDouble(double);
  11500. +#endif
  11501. +#if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || \
  11502. + defined(SQLITE_ENABLE_STAT4) || \
  11503. + defined(SQLITE_EXPLAIN_ESTIMATED_ROWS)
  11504. +u64 sqlite3LogEstToInt(LogEst);
  11505. +#endif
  11506. +VList *sqlite3VListAdd(sqlite3*,VList*,const char*,int,int);
  11507. +const char *sqlite3VListNumToName(VList*,int);
  11508. +int sqlite3VListNameToNum(VList*,const char*,int);
  11509. +
  11510. +/*
  11511. +** Routines to read and write variable-length integers. These used to
  11512. +** be defined locally, but now we use the varint routines in the util.c
  11513. +** file.
  11514. +*/
  11515. +int sqlite3PutVarint(unsigned char*, u64);
  11516. +u8 sqlite3GetVarint(const unsigned char *, u64 *);
  11517. +u8 sqlite3GetVarint32(const unsigned char *, u32 *);
  11518. +int sqlite3VarintLen(u64 v);
  11519. +
  11520. +/*
  11521. +** The common case is for a varint to be a single byte. They following
  11522. +** macros handle the common case without a procedure call, but then call
  11523. +** the procedure for larger varints.
  11524. +*/
  11525. +#define getVarint32(A,B) \
  11526. + (u8)((*(A)<(u8)0x80)?((B)=(u32)*(A)),1:sqlite3GetVarint32((A),(u32 *)&(B)))
  11527. +#define getVarint32NR(A,B) \
  11528. + B=(u32)*(A);if(B>=0x80)sqlite3GetVarint32((A),(u32*)&(B))
  11529. +#define putVarint32(A,B) \
  11530. + (u8)(((u32)(B)<(u32)0x80)?(*(A)=(unsigned char)(B)),1:\
  11531. + sqlite3PutVarint((A),(B)))
  11532. +#define getVarint sqlite3GetVarint
  11533. +#define putVarint sqlite3PutVarint
  11534. +
  11535. +
  11536. +const char *sqlite3IndexAffinityStr(sqlite3*, Index*);
  11537. +void sqlite3TableAffinity(Vdbe*, Table*, int);
  11538. +char sqlite3CompareAffinity(const Expr *pExpr, char aff2);
  11539. +int sqlite3IndexAffinityOk(const Expr *pExpr, char idx_affinity);
  11540. +char sqlite3TableColumnAffinity(Table*,int);
  11541. +char sqlite3ExprAffinity(const Expr *pExpr);
  11542. +int sqlite3Atoi64(const char*, i64*, int, u8);
  11543. +int sqlite3DecOrHexToI64(const char*, i64*);
  11544. +void sqlite3ErrorWithMsg(sqlite3*, int, const char*,...);
  11545. +void sqlite3Error(sqlite3*,int);
  11546. +void sqlite3SystemError(sqlite3*,int);
  11547. +void *sqlite3HexToBlob(sqlite3*, const char *z, int n);
  11548. +u8 sqlite3HexToInt(int h);
  11549. +int sqlite3TwoPartName(Parse *, Token *, Token *, Token **);
  11550. +
  11551. +#if defined(SQLITE_NEED_ERR_NAME)
  11552. +const char *sqlite3ErrName(int);
  11553. +#endif
  11554. +
  11555. +#ifdef SQLITE_ENABLE_DESERIALIZE
  11556. +int sqlite3MemdbInit(void);
  11557. +#endif
  11558. +
  11559. +const char *sqlite3ErrStr(int);
  11560. +int sqlite3ReadSchema(Parse *pParse);
  11561. +CollSeq *sqlite3FindCollSeq(sqlite3*,u8 enc, const char*,int);
  11562. +int sqlite3IsBinary(const CollSeq*);
  11563. +CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char*zName);
  11564. +void sqlite3SetTextEncoding(sqlite3 *db, u8);
  11565. +CollSeq *sqlite3ExprCollSeq(Parse *pParse, const Expr *pExpr);
  11566. +CollSeq *sqlite3ExprNNCollSeq(Parse *pParse, const Expr *pExpr);
  11567. +int sqlite3ExprCollSeqMatch(Parse*,const Expr*,const Expr*);
  11568. +Expr *sqlite3ExprAddCollateToken(Parse *pParse, Expr*, const Token*, int);
  11569. +Expr *sqlite3ExprAddCollateString(Parse*,Expr*,const char*);
  11570. +Expr *sqlite3ExprSkipCollate(Expr*);
  11571. +Expr *sqlite3ExprSkipCollateAndLikely(Expr*);
  11572. +int sqlite3CheckCollSeq(Parse *, CollSeq *);
  11573. +int sqlite3WritableSchema(sqlite3*);
  11574. +int sqlite3CheckObjectName(Parse*, const char*,const char*,const char*);
  11575. +void sqlite3VdbeSetChanges(sqlite3 *, int);
  11576. +int sqlite3AddInt64(i64*,i64);
  11577. +int sqlite3SubInt64(i64*,i64);
  11578. +int sqlite3MulInt64(i64*,i64);
  11579. +int sqlite3AbsInt32(int);
  11580. +#ifdef SQLITE_ENABLE_8_3_NAMES
  11581. +void sqlite3FileSuffix3(const char*, char*);
  11582. +#else
  11583. +# define sqlite3FileSuffix3(X,Y)
  11584. +#endif
  11585. +u8 sqlite3GetBoolean(const char *z,u8);
  11586. +
  11587. +const void *sqlite3ValueText(sqlite3_value*, u8);
  11588. +int sqlite3ValueBytes(sqlite3_value*, u8);
  11589. +void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8,
  11590. + void(*)(void*));
  11591. +void sqlite3ValueSetNull(sqlite3_value*);
  11592. +void sqlite3ValueFree(sqlite3_value*);
  11593. +#ifndef SQLITE_UNTESTABLE
  11594. +void sqlite3ResultIntReal(sqlite3_context*);
  11595. +#endif
  11596. +sqlite3_value *sqlite3ValueNew(sqlite3 *);
  11597. +#ifndef SQLITE_OMIT_UTF16
  11598. +char *sqlite3Utf16to8(sqlite3 *, const void*, int, u8);
  11599. +#endif
  11600. +int sqlite3ValueFromExpr(sqlite3 *, Expr *, u8, u8, sqlite3_value **);
  11601. +void sqlite3ValueApplyAffinity(sqlite3_value *, u8, u8);
  11602. +#ifndef SQLITE_AMALGAMATION
  11603. +extern const unsigned char sqlite3OpcodeProperty[];
  11604. +extern const char sqlite3StrBINARY[];
  11605. +extern const unsigned char sqlite3UpperToLower[];
  11606. +extern const unsigned char sqlite3CtypeMap[];
  11607. +extern SQLITE_WSD struct Sqlite3Config sqlite3Config;
  11608. +extern FuncDefHash sqlite3BuiltinFunctions;
  11609. +extern u32 sqlite3SelectTrace;
  11610. +#ifndef SQLITE_OMIT_WSD
  11611. +extern int sqlite3PendingByte;
  11612. +#endif
  11613. +#endif /* !defined(SQLITE_AMALGAMATION) */
  11614. +#ifdef VDBE_PROFILE
  11615. +extern sqlite3_uint64 sqlite3NProfileCnt;
  11616. +#endif
  11617. +void sqlite3RootPageMoved(sqlite3*, int, int, int);
  11618. +void sqlite3Reindex(Parse*, Token*, Token*);
  11619. +void sqlite3AlterFunctions(void);
  11620. +void sqlite3AlterRenameTable(Parse*, SrcList*, Token*);
  11621. +void sqlite3AlterRenameColumn(Parse*, SrcList*, Token*, Token*);
  11622. +int sqlite3GetToken(const unsigned char *, int *);
  11623. +void sqlite3NestedParse(Parse*, const char*, ...);
  11624. +void sqlite3ExpirePreparedStatements(sqlite3*, int);
  11625. +void sqlite3CodeRhsOfIN(Parse*, Expr*, int);
  11626. +int sqlite3CodeSubselect(Parse*, Expr*);
  11627. +void sqlite3SelectPrep(Parse*, Select*, NameContext*);
  11628. +void sqlite3SelectWrongNumTermsError(Parse *pParse, Select *p);
  11629. +int sqlite3MatchEName(
  11630. + const struct ExprList_item*,
  11631. + const char*,
  11632. + const char*,
  11633. + const char*
  11634. +);
  11635. +Bitmask sqlite3ExprColUsed(Expr*);
  11636. +u8 sqlite3StrIHash(const char*);
  11637. +int sqlite3ResolveExprNames(NameContext*, Expr*);
  11638. +int sqlite3ResolveExprListNames(NameContext*, ExprList*);
  11639. +void sqlite3ResolveSelectNames(Parse*, Select*, NameContext*);
  11640. +int sqlite3ResolveSelfReference(Parse*,Table*,int,Expr*,ExprList*);
  11641. +int sqlite3ResolveOrderGroupBy(Parse*, Select*, ExprList*, const char*);
  11642. +void sqlite3ColumnDefault(Vdbe *, Table *, int, int);
  11643. +void sqlite3AlterFinishAddColumn(Parse *, Token *);
  11644. +void sqlite3AlterBeginAddColumn(Parse *, SrcList *);
  11645. +void *sqlite3RenameTokenMap(Parse*, void*, Token*);
  11646. +void sqlite3RenameTokenRemap(Parse*, void *pTo, void *pFrom);
  11647. +void sqlite3RenameExprUnmap(Parse*, Expr*);
  11648. +void sqlite3RenameExprlistUnmap(Parse*, ExprList*);
  11649. +CollSeq *sqlite3GetCollSeq(Parse*, u8, CollSeq *, const char*);
  11650. +char sqlite3AffinityType(const char*, Column*);
  11651. +void sqlite3Analyze(Parse*, Token*, Token*);
  11652. +int sqlite3InvokeBusyHandler(BusyHandler*);
  11653. +int sqlite3FindDb(sqlite3*, Token*);
  11654. +int sqlite3FindDbName(sqlite3 *, const char *);
  11655. +int sqlite3AnalysisLoad(sqlite3*,int iDB);
  11656. +void sqlite3DeleteIndexSamples(sqlite3*,Index*);
  11657. +void sqlite3DefaultRowEst(Index*);
  11658. +void sqlite3RegisterLikeFunctions(sqlite3*, int);
  11659. +int sqlite3IsLikeFunction(sqlite3*,Expr*,int*,char*);
  11660. +void sqlite3SchemaClear(void *);
  11661. +Schema *sqlite3SchemaGet(sqlite3 *, Btree *);
  11662. +int sqlite3SchemaToIndex(sqlite3 *db, Schema *);
  11663. +KeyInfo *sqlite3KeyInfoAlloc(sqlite3*,int,int);
  11664. +void sqlite3KeyInfoUnref(KeyInfo*);
  11665. +KeyInfo *sqlite3KeyInfoRef(KeyInfo*);
  11666. +KeyInfo *sqlite3KeyInfoOfIndex(Parse*, Index*);
  11667. +KeyInfo *sqlite3KeyInfoFromExprList(Parse*, ExprList*, int, int);
  11668. +int sqlite3HasExplicitNulls(Parse*, ExprList*);
  11669. +
  11670. +#ifdef SQLITE_DEBUG
  11671. +int sqlite3KeyInfoIsWriteable(KeyInfo*);
  11672. +#endif
  11673. +int sqlite3CreateFunc(sqlite3 *, const char *, int, int, void *,
  11674. + void (*)(sqlite3_context*,int,sqlite3_value **),
  11675. + void (*)(sqlite3_context*,int,sqlite3_value **),
  11676. + void (*)(sqlite3_context*),
  11677. + void (*)(sqlite3_context*),
  11678. + void (*)(sqlite3_context*,int,sqlite3_value **),
  11679. + FuncDestructor *pDestructor
  11680. +);
  11681. +void sqlite3NoopDestructor(void*);
  11682. +void sqlite3OomFault(sqlite3*);
  11683. +void sqlite3OomClear(sqlite3*);
  11684. +int sqlite3ApiExit(sqlite3 *db, int);
  11685. +int sqlite3OpenTempDatabase(Parse *);
  11686. +
  11687. +void sqlite3StrAccumInit(StrAccum*, sqlite3*, char*, int, int);
  11688. +char *sqlite3StrAccumFinish(StrAccum*);
  11689. +void sqlite3SelectDestInit(SelectDest*,int,int);
  11690. +Expr *sqlite3CreateColumnExpr(sqlite3 *, SrcList *, int, int);
  11691. +
  11692. +void sqlite3BackupRestart(sqlite3_backup *);
  11693. +void sqlite3BackupUpdate(sqlite3_backup *, Pgno, const u8 *);
  11694. +
  11695. +#ifndef SQLITE_OMIT_SUBQUERY
  11696. +int sqlite3ExprCheckIN(Parse*, Expr*);
  11697. +#else
  11698. +# define sqlite3ExprCheckIN(x,y) SQLITE_OK
  11699. +#endif
  11700. +
  11701. +#ifdef SQLITE_ENABLE_STAT4
  11702. +int sqlite3Stat4ProbeSetValue(
  11703. + Parse*,Index*,UnpackedRecord**,Expr*,int,int,int*);
  11704. +int sqlite3Stat4ValueFromExpr(Parse*, Expr*, u8, sqlite3_value**);
  11705. +void sqlite3Stat4ProbeFree(UnpackedRecord*);
  11706. +int sqlite3Stat4Column(sqlite3*, const void*, int, int, sqlite3_value**);
  11707. +char sqlite3IndexColumnAffinity(sqlite3*, Index*, int);
  11708. +#endif
  11709. +
  11710. +/*
  11711. +** The interface to the LEMON-generated parser
  11712. +*/
  11713. +#ifndef SQLITE_AMALGAMATION
  11714. + void *sqlite3ParserAlloc(void*(*)(u64), Parse*);
  11715. + void sqlite3ParserFree(void*, void(*)(void*));
  11716. +#endif
  11717. +void sqlite3Parser(void*, int, Token);
  11718. +int sqlite3ParserFallback(int);
  11719. +#ifdef YYTRACKMAXSTACKDEPTH
  11720. + int sqlite3ParserStackPeak(void*);
  11721. +#endif
  11722. +
  11723. +void sqlite3AutoLoadExtensions(sqlite3*);
  11724. +#ifndef SQLITE_OMIT_LOAD_EXTENSION
  11725. + void sqlite3CloseExtensions(sqlite3*);
  11726. +#else
  11727. +# define sqlite3CloseExtensions(X)
  11728. +#endif
  11729. +
  11730. +#ifndef SQLITE_OMIT_SHARED_CACHE
  11731. + void sqlite3TableLock(Parse *, int, int, u8, const char *);
  11732. +#else
  11733. + #define sqlite3TableLock(v,w,x,y,z)
  11734. +#endif
  11735. +
  11736. +#ifdef SQLITE_TEST
  11737. + int sqlite3Utf8To8(unsigned char*);
  11738. +#endif
  11739. +
  11740. +#ifdef SQLITE_OMIT_VIRTUALTABLE
  11741. +# define sqlite3VtabClear(Y)
  11742. +# define sqlite3VtabSync(X,Y) SQLITE_OK
  11743. +# define sqlite3VtabRollback(X)
  11744. +# define sqlite3VtabCommit(X)
  11745. +# define sqlite3VtabInSync(db) 0
  11746. +# define sqlite3VtabLock(X)
  11747. +# define sqlite3VtabUnlock(X)
  11748. +# define sqlite3VtabModuleUnref(D,X)
  11749. +# define sqlite3VtabUnlockList(X)
  11750. +# define sqlite3VtabSavepoint(X, Y, Z) SQLITE_OK
  11751. +# define sqlite3GetVTable(X,Y) ((VTable*)0)
  11752. +#else
  11753. + void sqlite3VtabClear(sqlite3 *db, Table*);
  11754. + void sqlite3VtabDisconnect(sqlite3 *db, Table *p);
  11755. + int sqlite3VtabSync(sqlite3 *db, Vdbe*);
  11756. + int sqlite3VtabRollback(sqlite3 *db);
  11757. + int sqlite3VtabCommit(sqlite3 *db);
  11758. + void sqlite3VtabLock(VTable *);
  11759. + void sqlite3VtabUnlock(VTable *);
  11760. + void sqlite3VtabModuleUnref(sqlite3*,Module*);
  11761. + void sqlite3VtabUnlockList(sqlite3*);
  11762. + int sqlite3VtabSavepoint(sqlite3 *, int, int);
  11763. + void sqlite3VtabImportErrmsg(Vdbe*, sqlite3_vtab*);
  11764. + VTable *sqlite3GetVTable(sqlite3*, Table*);
  11765. + Module *sqlite3VtabCreateModule(
  11766. + sqlite3*,
  11767. + const char*,
  11768. + const sqlite3_module*,
  11769. + void*,
  11770. + void(*)(void*)
  11771. + );
  11772. +# define sqlite3VtabInSync(db) ((db)->nVTrans>0 && (db)->aVTrans==0)
  11773. +#endif
  11774. +int sqlite3ReadOnlyShadowTables(sqlite3 *db);
  11775. +#ifndef SQLITE_OMIT_VIRTUALTABLE
  11776. + int sqlite3ShadowTableName(sqlite3 *db, const char *zName);
  11777. + int sqlite3IsShadowTableOf(sqlite3*,Table*,const char*);
  11778. +#else
  11779. +# define sqlite3ShadowTableName(A,B) 0
  11780. +# define sqlite3IsShadowTableOf(A,B,C) 0
  11781. +#endif
  11782. +int sqlite3VtabEponymousTableInit(Parse*,Module*);
  11783. +void sqlite3VtabEponymousTableClear(sqlite3*,Module*);
  11784. +void sqlite3VtabMakeWritable(Parse*,Table*);
  11785. +void sqlite3VtabBeginParse(Parse*, Token*, Token*, Token*, int);
  11786. +void sqlite3VtabFinishParse(Parse*, Token*);
  11787. +void sqlite3VtabArgInit(Parse*);
  11788. +void sqlite3VtabArgExtend(Parse*, Token*);
  11789. +int sqlite3VtabCallCreate(sqlite3*, int, const char *, char **);
  11790. +int sqlite3VtabCallConnect(Parse*, Table*);
  11791. +int sqlite3VtabCallDestroy(sqlite3*, int, const char *);
  11792. +int sqlite3VtabBegin(sqlite3 *, VTable *);
  11793. +FuncDef *sqlite3VtabOverloadFunction(sqlite3 *,FuncDef*, int nArg, Expr*);
  11794. +sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context*);
  11795. +int sqlite3VdbeParameterIndex(Vdbe*, const char*, int);
  11796. +int sqlite3TransferBindings(sqlite3_stmt *, sqlite3_stmt *);
  11797. +void sqlite3ParserReset(Parse*);
  11798. +#ifdef SQLITE_ENABLE_NORMALIZE
  11799. +char *sqlite3Normalize(Vdbe*, const char*);
  11800. +#endif
  11801. +int sqlite3Reprepare(Vdbe*);
  11802. +void sqlite3ExprListCheckLength(Parse*, ExprList*, const char*);
  11803. +CollSeq *sqlite3ExprCompareCollSeq(Parse*,const Expr*);
  11804. +CollSeq *sqlite3BinaryCompareCollSeq(Parse *, const Expr*, const Expr*);
  11805. +int sqlite3TempInMemory(const sqlite3*);
  11806. +const char *sqlite3JournalModename(int);
  11807. +#ifndef SQLITE_OMIT_WAL
  11808. + int sqlite3Checkpoint(sqlite3*, int, int, int*, int*);
  11809. + int sqlite3WalDefaultHook(void*,sqlite3*,const char*,int);
  11810. +#endif
  11811. +#ifndef SQLITE_OMIT_CTE
  11812. + With *sqlite3WithAdd(Parse*,With*,Token*,ExprList*,Select*);
  11813. + void sqlite3WithDelete(sqlite3*,With*);
  11814. + void sqlite3WithPush(Parse*, With*, u8);
  11815. +#else
  11816. +#define sqlite3WithPush(x,y,z)
  11817. +#define sqlite3WithDelete(x,y)
  11818. +#endif
  11819. +#ifndef SQLITE_OMIT_UPSERT
  11820. + Upsert *sqlite3UpsertNew(sqlite3*,ExprList*,Expr*,ExprList*,Expr*);
  11821. + void sqlite3UpsertDelete(sqlite3*,Upsert*);
  11822. + Upsert *sqlite3UpsertDup(sqlite3*,Upsert*);
  11823. + int sqlite3UpsertAnalyzeTarget(Parse*,SrcList*,Upsert*);
  11824. + void sqlite3UpsertDoUpdate(Parse*,Upsert*,Table*,Index*,int);
  11825. +#else
  11826. +#define sqlite3UpsertNew(v,w,x,y,z) ((Upsert*)0)
  11827. +#define sqlite3UpsertDelete(x,y)
  11828. +#define sqlite3UpsertDup(x,y) ((Upsert*)0)
  11829. +#endif
  11830. +
  11831. +
  11832. +/* Declarations for functions in fkey.c. All of these are replaced by
  11833. +** no-op macros if OMIT_FOREIGN_KEY is defined. In this case no foreign
  11834. +** key functionality is available. If OMIT_TRIGGER is defined but
  11835. +** OMIT_FOREIGN_KEY is not, only some of the functions are no-oped. In
  11836. +** this case foreign keys are parsed, but no other functionality is
  11837. +** provided (enforcement of FK constraints requires the triggers sub-system).
  11838. +*/
  11839. +#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
  11840. + void sqlite3FkCheck(Parse*, Table*, int, int, int*, int);
  11841. + void sqlite3FkDropTable(Parse*, SrcList *, Table*);
  11842. + void sqlite3FkActions(Parse*, Table*, ExprList*, int, int*, int);
  11843. + int sqlite3FkRequired(Parse*, Table*, int*, int);
  11844. + u32 sqlite3FkOldmask(Parse*, Table*);
  11845. + FKey *sqlite3FkReferences(Table *);
  11846. +#else
  11847. + #define sqlite3FkActions(a,b,c,d,e,f)
  11848. + #define sqlite3FkCheck(a,b,c,d,e,f)
  11849. + #define sqlite3FkDropTable(a,b,c)
  11850. + #define sqlite3FkOldmask(a,b) 0
  11851. + #define sqlite3FkRequired(a,b,c,d) 0
  11852. + #define sqlite3FkReferences(a) 0
  11853. +#endif
  11854. +#ifndef SQLITE_OMIT_FOREIGN_KEY
  11855. + void sqlite3FkDelete(sqlite3 *, Table*);
  11856. + int sqlite3FkLocateIndex(Parse*,Table*,FKey*,Index**,int**);
  11857. +#else
  11858. + #define sqlite3FkDelete(a,b)
  11859. + #define sqlite3FkLocateIndex(a,b,c,d,e)
  11860. +#endif
  11861. +
  11862. +
  11863. +/*
  11864. +** Available fault injectors. Should be numbered beginning with 0.
  11865. +*/
  11866. +#define SQLITE_FAULTINJECTOR_MALLOC 0
  11867. +#define SQLITE_FAULTINJECTOR_COUNT 1
  11868. +
  11869. +/*
  11870. +** The interface to the code in fault.c used for identifying "benign"
  11871. +** malloc failures. This is only present if SQLITE_UNTESTABLE
  11872. +** is not defined.
  11873. +*/
  11874. +#ifndef SQLITE_UNTESTABLE
  11875. + void sqlite3BeginBenignMalloc(void);
  11876. + void sqlite3EndBenignMalloc(void);
  11877. +#else
  11878. + #define sqlite3BeginBenignMalloc()
  11879. + #define sqlite3EndBenignMalloc()
  11880. +#endif
  11881. +
  11882. +/*
  11883. +** Allowed return values from sqlite3FindInIndex()
  11884. +*/
  11885. +#define IN_INDEX_ROWID 1 /* Search the rowid of the table */
  11886. +#define IN_INDEX_EPH 2 /* Search an ephemeral b-tree */
  11887. +#define IN_INDEX_INDEX_ASC 3 /* Existing index ASCENDING */
  11888. +#define IN_INDEX_INDEX_DESC 4 /* Existing index DESCENDING */
  11889. +#define IN_INDEX_NOOP 5 /* No table available. Use comparisons */
  11890. +/*
  11891. +** Allowed flags for the 3rd parameter to sqlite3FindInIndex().
  11892. +*/
  11893. +#define IN_INDEX_NOOP_OK 0x0001 /* OK to return IN_INDEX_NOOP */
  11894. +#define IN_INDEX_MEMBERSHIP 0x0002 /* IN operator used for membership test */
  11895. +#define IN_INDEX_LOOP 0x0004 /* IN operator used as a loop */
  11896. +int sqlite3FindInIndex(Parse *, Expr *, u32, int*, int*, int*);
  11897. +
  11898. +int sqlite3JournalOpen(sqlite3_vfs *, const char *, sqlite3_file *, int, int);
  11899. +int sqlite3JournalSize(sqlite3_vfs *);
  11900. +#if defined(SQLITE_ENABLE_ATOMIC_WRITE) \
  11901. + || defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
  11902. + int sqlite3JournalCreate(sqlite3_file *);
  11903. +#endif
  11904. +
  11905. +int sqlite3JournalIsInMemory(sqlite3_file *p);
  11906. +void sqlite3MemJournalOpen(sqlite3_file *);
  11907. +
  11908. +void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p);
  11909. +#if SQLITE_MAX_EXPR_DEPTH>0
  11910. + int sqlite3SelectExprHeight(Select *);
  11911. + int sqlite3ExprCheckHeight(Parse*, int);
  11912. +#else
  11913. + #define sqlite3SelectExprHeight(x) 0
  11914. + #define sqlite3ExprCheckHeight(x,y)
  11915. +#endif
  11916. +
  11917. +u32 sqlite3Get4byte(const u8*);
  11918. +void sqlite3Put4byte(u8*, u32);
  11919. +
  11920. +#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
  11921. + void sqlite3ConnectionBlocked(sqlite3 *, sqlite3 *);
  11922. + void sqlite3ConnectionUnlocked(sqlite3 *db);
  11923. + void sqlite3ConnectionClosed(sqlite3 *db);
  11924. +#else
  11925. + #define sqlite3ConnectionBlocked(x,y)
  11926. + #define sqlite3ConnectionUnlocked(x)
  11927. + #define sqlite3ConnectionClosed(x)
  11928. +#endif
  11929. +
  11930. +#ifdef SQLITE_DEBUG
  11931. + void sqlite3ParserTrace(FILE*, char *);
  11932. +#endif
  11933. +#if defined(YYCOVERAGE)
  11934. + int sqlite3ParserCoverage(FILE*);
  11935. +#endif
  11936. +
  11937. +/*
  11938. +** If the SQLITE_ENABLE IOTRACE exists then the global variable
  11939. +** sqlite3IoTrace is a pointer to a printf-like routine used to
  11940. +** print I/O tracing messages.
  11941. +*/
  11942. +#ifdef SQLITE_ENABLE_IOTRACE
  11943. +# define IOTRACE(A) if( sqlite3IoTrace ){ sqlite3IoTrace A; }
  11944. + void sqlite3VdbeIOTraceSql(Vdbe*);
  11945. +SQLITE_API SQLITE_EXTERN void (SQLITE_CDECL *sqlite3IoTrace)(const char*,...);
  11946. +#else
  11947. +# define IOTRACE(A)
  11948. +# define sqlite3VdbeIOTraceSql(X)
  11949. +#endif
  11950. +
  11951. +/*
  11952. +** These routines are available for the mem2.c debugging memory allocator
  11953. +** only. They are used to verify that different "types" of memory
  11954. +** allocations are properly tracked by the system.
  11955. +**
  11956. +** sqlite3MemdebugSetType() sets the "type" of an allocation to one of
  11957. +** the MEMTYPE_* macros defined below. The type must be a bitmask with
  11958. +** a single bit set.
  11959. +**
  11960. +** sqlite3MemdebugHasType() returns true if any of the bits in its second
  11961. +** argument match the type set by the previous sqlite3MemdebugSetType().
  11962. +** sqlite3MemdebugHasType() is intended for use inside assert() statements.
  11963. +**
  11964. +** sqlite3MemdebugNoType() returns true if none of the bits in its second
  11965. +** argument match the type set by the previous sqlite3MemdebugSetType().
  11966. +**
  11967. +** Perhaps the most important point is the difference between MEMTYPE_HEAP
  11968. +** and MEMTYPE_LOOKASIDE. If an allocation is MEMTYPE_LOOKASIDE, that means
  11969. +** it might have been allocated by lookaside, except the allocation was
  11970. +** too large or lookaside was already full. It is important to verify
  11971. +** that allocations that might have been satisfied by lookaside are not
  11972. +** passed back to non-lookaside free() routines. Asserts such as the
  11973. +** example above are placed on the non-lookaside free() routines to verify
  11974. +** this constraint.
  11975. +**
  11976. +** All of this is no-op for a production build. It only comes into
  11977. +** play when the SQLITE_MEMDEBUG compile-time option is used.
  11978. +*/
  11979. +#ifdef SQLITE_MEMDEBUG
  11980. + void sqlite3MemdebugSetType(void*,u8);
  11981. + int sqlite3MemdebugHasType(void*,u8);
  11982. + int sqlite3MemdebugNoType(void*,u8);
  11983. +#else
  11984. +# define sqlite3MemdebugSetType(X,Y) /* no-op */
  11985. +# define sqlite3MemdebugHasType(X,Y) 1
  11986. +# define sqlite3MemdebugNoType(X,Y) 1
  11987. +#endif
  11988. +#define MEMTYPE_HEAP 0x01 /* General heap allocations */
  11989. +#define MEMTYPE_LOOKASIDE 0x02 /* Heap that might have been lookaside */
  11990. +#define MEMTYPE_PCACHE 0x04 /* Page cache allocations */
  11991. +
  11992. +/*
  11993. +** Threading interface
  11994. +*/
  11995. +#if SQLITE_MAX_WORKER_THREADS>0
  11996. +int sqlite3ThreadCreate(SQLiteThread**,void*(*)(void*),void*);
  11997. +int sqlite3ThreadJoin(SQLiteThread*, void**);
  11998. +#endif
  11999. +
  12000. +#if defined(SQLITE_ENABLE_DBPAGE_VTAB) || defined(SQLITE_TEST)
  12001. +int sqlite3DbpageRegister(sqlite3*);
  12002. +#endif
  12003. +#if defined(SQLITE_ENABLE_DBSTAT_VTAB) || defined(SQLITE_TEST)
  12004. +int sqlite3DbstatRegister(sqlite3*);
  12005. +#endif
  12006. +
  12007. +int sqlite3ExprVectorSize(Expr *pExpr);
  12008. +int sqlite3ExprIsVector(Expr *pExpr);
  12009. +Expr *sqlite3VectorFieldSubexpr(Expr*, int);
  12010. +Expr *sqlite3ExprForVectorField(Parse*,Expr*,int);
  12011. +void sqlite3VectorErrorMsg(Parse*, Expr*);
  12012. +
  12013. +#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
  12014. +const char **sqlite3CompileOptions(int *pnOpt);
  12015. +#endif
  12016. +
  12017. +#endif /* SQLITEINT_H */
  12018. diff -Npur sqlite-version-3.32.2/src/test1.c sqlite-version-3.32.2-patched/src/test1.c
  12019. --- sqlite-version-3.32.2/src/test1.c 2020-06-04 20:58:43.000000000 +0800
  12020. +++ sqlite-version-3.32.2-patched/src/test1.c 2020-07-08 10:00:47.371088720 +0800
  12021. @@ -8164,7 +8164,7 @@ int Sqlitetest1_Init(Tcl_Interp *interp)
  12022. #endif
  12023. #endif
  12024. #if defined(SQLITE_ENABLE_SELECTTRACE)
  12025. - extern int sqlite3SelectTrace;
  12026. + extern u32 sqlite3SelectTrace;
  12027. #endif
  12028. for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){
  12029. diff -Npur sqlite-version-3.32.2/src/window.c sqlite-version-3.32.2-patched/src/window.c
  12030. --- sqlite-version-3.32.2/src/window.c 2020-06-04 20:58:43.000000000 +0800
  12031. +++ sqlite-version-3.32.2-patched/src/window.c 2020-07-08 10:00:47.371088720 +0800
  12032. @@ -942,7 +942,7 @@ static int sqlite3WindowExtraAggFuncDept
  12033. */
  12034. int sqlite3WindowRewrite(Parse *pParse, Select *p){
  12035. int rc = SQLITE_OK;
  12036. - if( p->pWin && p->pPrior==0 && (p->selFlags & SF_WinRewrite)==0 ){
  12037. + if( ALWAYS(p->pWin && (p->selFlags & SF_WinRewrite)==0) ){
  12038. Vdbe *v = sqlite3GetVdbe(pParse);
  12039. sqlite3 *db = pParse->db;
  12040. Select *pSub = 0; /* The subquery */
  12041. diff -Npur sqlite-version-3.32.2/test/selectA.test sqlite-version-3.32.2-patched/test/selectA.test
  12042. --- sqlite-version-3.32.2/test/selectA.test 2020-06-04 20:58:43.000000000 +0800
  12043. +++ sqlite-version-3.32.2-patched/test/selectA.test 2020-07-08 10:00:50.899152517 +0800
  12044. @@ -1446,5 +1446,26 @@ do_execsql_test 6.1 {
  12045. SELECT * FROM (SELECT a FROM t1 UNION SELECT b FROM t2) WHERE a=a;
  12046. } {12345}
  12047. +# 2020-06-15 ticket 8f157e8010b22af0
  12048. +#
  12049. +reset_db
  12050. +do_execsql_test 7.1 {
  12051. + CREATE TABLE t1(c1); INSERT INTO t1 VALUES(12),(123),(1234),(NULL),('abc');
  12052. + CREATE TABLE t2(c2); INSERT INTO t2 VALUES(44),(55),(123);
  12053. + CREATE TABLE t3(c3,c4); INSERT INTO t3 VALUES(66,1),(123,2),(77,3);
  12054. + CREATE VIEW t4 AS SELECT c3 FROM t3;
  12055. + CREATE VIEW t5 AS SELECT c3 FROM t3 ORDER BY c4;
  12056. +}
  12057. +do_execsql_test 7.2 {
  12058. + SELECT * FROM t1, t2 WHERE c1=(SELECT 123 INTERSECT SELECT c2 FROM t4) AND c1=123;
  12059. +} {123 123}
  12060. +do_execsql_test 7.3 {
  12061. + SELECT * FROM t1, t2 WHERE c1=(SELECT 123 INTERSECT SELECT c2 FROM t5) AND c1=123;
  12062. +} {123 123}
  12063. +do_execsql_test 7.4 {
  12064. + CREATE TABLE a(b);
  12065. + CREATE VIEW c(d) AS SELECT b FROM a ORDER BY b;
  12066. + SELECT sum(d) OVER( PARTITION BY(SELECT 0 FROM c JOIN a WHERE b =(SELECT b INTERSECT SELECT d FROM c) AND b = 123)) FROM c;
  12067. +} {}
  12068. finish_test
  12069. diff -Npur sqlite-version-3.32.2/test/window1.test sqlite-version-3.32.2-patched/test/window1.test
  12070. --- sqlite-version-3.32.2/test/window1.test 2020-06-04 20:58:43.000000000 +0800
  12071. +++ sqlite-version-3.32.2-patched/test/window1.test 2020-07-08 10:00:47.371088720 +0800
  12072. @@ -1743,5 +1743,47 @@ do_execsql_test 53.0 {
  12073. WHERE a.c);
  12074. } {4 4 4 4}
  12075. +#-------------------------------------------------------------------------
  12076. +reset_db
  12077. +do_execsql_test 54.1 {
  12078. + CREATE TABLE t1(a VARCHAR(20), b FLOAT);
  12079. + INSERT INTO t1 VALUES('1',10.0);
  12080. +}
  12081. +
  12082. +do_execsql_test 54.2 {
  12083. + SELECT * FROM (
  12084. + SELECT sum(b) OVER() AS c FROM t1
  12085. + UNION
  12086. + SELECT b AS c FROM t1
  12087. + ) WHERE c>10;
  12088. +}
  12089. +
  12090. +do_execsql_test 54.3 {
  12091. + INSERT INTO t1 VALUES('2',5.0);
  12092. + INSERT INTO t1 VALUES('3',15.0);
  12093. +}
  12094. +
  12095. +do_execsql_test 54.4 {
  12096. + SELECT * FROM (
  12097. + SELECT sum(b) OVER() AS c FROM t1
  12098. + UNION
  12099. + SELECT b AS c FROM t1
  12100. + ) WHERE c>10;
  12101. +} {15.0 30.0}
  12102. +
  12103. +# 2020-06-05 ticket c8d3b9f0a750a529
  12104. +reset_db
  12105. +do_execsql_test 55.1 {
  12106. + CREATE TABLE a(b);
  12107. + SELECT
  12108. + (SELECT b FROM a
  12109. + GROUP BY b
  12110. + HAVING (SELECT COUNT()OVER() + lead(b)OVER(ORDER BY SUM(DISTINCT b) + b))
  12111. + )
  12112. + FROM a
  12113. + UNION
  12114. + SELECT 99
  12115. + ORDER BY 1;
  12116. +} {99}
  12117. finish_test