window_funcs.slt 189 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014
  1. # Copyright Materialize, Inc. and contributors. All rights reserved.
  2. #
  3. # Use of this software is governed by the Business Source License
  4. # included in the LICENSE file at the root of this repository.
  5. #
  6. # As of the Change Date specified in that file, in accordance with
  7. # the Business Source License, use of this software will be governed
  8. # by the Apache License, Version 2.0.
  9. # This test may seem simple, but it is surprisingly good at verifying that
  10. # logical timestamp handling for internal inputs is sane.
  11. mode cockroach
  12. statement ok
  13. CREATE TABLE t(x string);
  14. statement ok
  15. INSERT INTO t VALUES ('a'), ('b'), ('c');
  16. statement error db error: ERROR: window function pg_catalog\.row_number requires an OVER clause
  17. SELECT row_number()
  18. FROM t;
  19. statement error db error: ERROR: window function pg_catalog\.row_number requires an OVER clause
  20. SELECT *
  21. FROM t
  22. ORDER BY row_number();
  23. statement error db error: ERROR: window function pg_catalog\.row_number requires an OVER clause
  24. SELECT *
  25. FROM t
  26. QUALIFY row_number();
  27. statement error db error: ERROR: OVER clause not allowed on pg_catalog\.int8range\. The OVER clause can only be used with window functions \(including aggregations\)\.
  28. SELECT int8range(1, 1, '') OVER (PARTITION BY 1 ORDER BY 1);
  29. query IT
  30. SELECT row_number() OVER (ORDER BY x), x FROM t
  31. ORDER BY row_number
  32. ----
  33. 1 a
  34. 2 b
  35. 3 c
  36. query IT
  37. SELECT row_number() OVER (ORDER BY x DESC), x FROM t
  38. ORDER BY row_number
  39. ----
  40. 1 c
  41. 2 b
  42. 3 a
  43. statement ok
  44. DROP TABLE t;
  45. statement ok
  46. CREATE TABLE t(x string, y int);
  47. statement ok
  48. INSERT INTO t VALUES ('a', 98), ('b', 99), ('c', 98);
  49. query IT
  50. SELECT row_number() OVER (PARTITION BY y ORDER BY x), x FROM t
  51. ORDER BY row_number, x
  52. ----
  53. 1 a
  54. 1 b
  55. 2 c
  56. statement ok
  57. DROP TABLE t;
  58. statement ok
  59. CREATE TABLE t(x string, y int);
  60. statement ok
  61. INSERT INTO t VALUES ('a', 1), ('b', 2), ('c', 1);
  62. query IT
  63. SELECT row_number() OVER (PARTITION BY y ORDER BY x DESC), x FROM t
  64. ORDER BY row_number, x
  65. ----
  66. 1 b
  67. 1 c
  68. 2 a
  69. query IT
  70. SELECT row_number() OVER (PARTITION BY x ORDER BY x), x FROM t
  71. ORDER BY row_number, x
  72. ----
  73. 1 a
  74. 1 b
  75. 1 c
  76. query IT
  77. SELECT row_number() OVER (PARTITION BY NULL ORDER BY 10000) AS q, a1.x
  78. FROM t AS a1, t AS a2
  79. ORDER BY q DESC
  80. ----
  81. 9 c
  82. 8 c
  83. 7 c
  84. 6 b
  85. 5 b
  86. 4 b
  87. 3 a
  88. 2 a
  89. 1 a
  90. statement ok
  91. DROP TABLE t;
  92. statement ok
  93. CREATE TABLE t(x string);
  94. statement ok
  95. INSERT INTO t VALUES ('a');
  96. # Make sure a non-column expression following the window function is correctly
  97. # handled.
  98. query ITT
  99. SELECT row_number() OVER (PARTITION BY NULL) AS q, x, 'b'
  100. FROM t
  101. ----
  102. 1 a b
  103. # Regression test for database-issues#2730
  104. query II
  105. SELECT row_number() OVER (), row_number() OVER ()
  106. ----
  107. 1 1
  108. statement ok
  109. INSERT INTO t VALUES ('b');
  110. query II
  111. SELECT row_number() OVER (), row_number() OVER () from t
  112. ----
  113. 1 1
  114. 2 2
  115. statement ok
  116. DROP TABLE t;
  117. statement ok
  118. CREATE TABLE t(x string, y int);
  119. statement ok
  120. INSERT INTO t VALUES ('a', 1), ('b', 2), ('c', 1);
  121. query T multiline
  122. EXPLAIN RAW PLAN FOR
  123. SELECT row_number() OVER (PARTITION BY x ORDER BY x), x FROM t
  124. ORDER BY row_number, x
  125. ----
  126. Finish order_by=[#0 asc nulls_last, #1 asc nulls_last] output=[#0, #1]
  127. Project (#2, #0)
  128. Map (row_number() over (partition by [#0{x}] order by [#0{x} asc nulls_last]))
  129. Get materialize.public.t
  130. Target cluster: quickstart
  131. EOF
  132. query T multiline
  133. EXPLAIN DECORRELATED PLAN WITH(arity) FOR
  134. SELECT row_number() OVER (PARTITION BY x ORDER BY x), x FROM t
  135. ORDER BY row_number, x
  136. ----
  137. Finish order_by=[#0 asc nulls_last, #1 asc nulls_last] output=[#0, #1]
  138. Project (#3, #0) // { arity: 2 }
  139. Map (#2) // { arity: 4 }
  140. Project (#3..=#5) // { arity: 3 }
  141. Map (record_get[0](record_get[1](#2)), record_get[1](record_get[1](#2)), record_get[0](#2)) // { arity: 6 }
  142. FlatMap unnest_list(#1) // { arity: 3 }
  143. Reduce group_by=[#0] aggregates=[row_number[order_by=[#0 asc nulls_last]](row(list[row(#0, #1)], #0{x}))] // { arity: 2 }
  144. CrossJoin // { arity: 2 }
  145. Constant // { arity: 0 }
  146. - ()
  147. Get materialize.public.t // { arity: 2 }
  148. Target cluster: quickstart
  149. EOF
  150. #
  151. # Regression test for database-issues#2760
  152. #
  153. statement ok
  154. CREATE TABLE t1 (f1 INTEGER, f2 INTEGER);
  155. ----
  156. statement ok
  157. ----
  158. INSERT INTO t1 VALUES (1, 1), (2, 2), (4, 4);
  159. statement ok
  160. CREATE TABLE t2 (f1 INTEGER, f2 INTEGER);
  161. ----
  162. statement ok
  163. INSERT INTO t2 VALUES (1, 1), (1, 2), (2, 2);
  164. ----
  165. query I
  166. SELECT f1 FROM t1
  167. WHERE f1 IN (SELECT ROW_NUMBER() OVER () FROM t2);
  168. ----
  169. 1
  170. 2
  171. query T multiline
  172. EXPLAIN DECORRELATED PLAN WITH(arity) FOR SELECT f1 FROM t1
  173. WHERE f1 IN (SELECT ROW_NUMBER() OVER () FROM t2);
  174. ----
  175. With
  176. cte l0 =
  177. CrossJoin // { arity: 2 }
  178. Constant // { arity: 0 }
  179. - ()
  180. Get materialize.public.t1 // { arity: 2 }
  181. cte l1 =
  182. Distinct project=[#0] // { arity: 1 }
  183. Get l0 // { arity: 2 }
  184. cte l2 =
  185. Map (true) // { arity: 2 }
  186. Distinct project=[#0] // { arity: 1 }
  187. Filter (integer_to_bigint(#0{f1}) = #1{right_col0_0}) // { arity: 2 }
  188. Project (#0, #4) // { arity: 2 }
  189. Map (#3) // { arity: 5 }
  190. Project (#3..=#6) // { arity: 4 }
  191. Map (record_get[0](record_get[1](#2)), record_get[1](record_get[1](#2)), record_get[2](record_get[1](#2)), record_get[0](#2)) // { arity: 7 }
  192. FlatMap unnest_list(#1) // { arity: 3 }
  193. Reduce group_by=[#0] aggregates=[row_number[order_by=[]](row(list[row(#0, #1, #2)]))] // { arity: 2 }
  194. CrossJoin // { arity: 3 }
  195. Get l1 // { arity: 1 }
  196. Get materialize.public.t2 // { arity: 2 }
  197. Return // { arity: 1 }
  198. Project (#0) // { arity: 1 }
  199. Filter #2 // { arity: 3 }
  200. Project (#0, #1, #3) // { arity: 3 }
  201. Join on=(#0 = #2) // { arity: 4 }
  202. Get l0 // { arity: 2 }
  203. Union // { arity: 2 }
  204. Get l2 // { arity: 2 }
  205. CrossJoin // { arity: 2 }
  206. Project (#0) // { arity: 1 }
  207. Join on=(#0 = #1) // { arity: 2 }
  208. Union // { arity: 1 }
  209. Negate // { arity: 1 }
  210. Distinct project=[#0] // { arity: 1 }
  211. Get l2 // { arity: 2 }
  212. Distinct project=[#0] // { arity: 1 }
  213. Get l1 // { arity: 1 }
  214. Get l1 // { arity: 1 }
  215. Constant // { arity: 1 }
  216. - (false)
  217. Target cluster: quickstart
  218. EOF
  219. query T multiline
  220. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT f1 FROM t1
  221. WHERE f1 IN (SELECT ROW_NUMBER() OVER () FROM t2);
  222. ----
  223. Explained Query:
  224. With
  225. cte l0 =
  226. Project (#0{f1}) // { arity: 1 }
  227. ReadStorage materialize.public.t1 // { arity: 2 }
  228. Return // { arity: 1 }
  229. Project (#0{f1}) // { arity: 1 }
  230. Join on=(#0{f1} = #1) type=differential // { arity: 2 }
  231. implementation
  232. %1[#0]UKA » %0:l0[#0]K
  233. ArrangeBy keys=[[#0{f1}]] // { arity: 1 }
  234. Get l0 // { arity: 1 }
  235. ArrangeBy keys=[[#0]] // { arity: 1 }
  236. Distinct project=[record_get[0](record_get[1](#0))] // { arity: 1 }
  237. Project (#1) // { arity: 1 }
  238. Filter (integer_to_bigint(record_get[0](record_get[1](#1))) = record_get[0](#1)) // { arity: 2 }
  239. FlatMap unnest_list(#0{row_number}) // { arity: 2 }
  240. Project (#1{row_number}) // { arity: 1 }
  241. Reduce group_by=[#0{f1}] aggregates=[row_number[order_by=[]](row(list[row(#0{f1}, #1{f1}, #2{f2})]))] // { arity: 2 }
  242. CrossJoin type=differential // { arity: 3 }
  243. implementation
  244. %0[×] » %1:t2[×]
  245. ArrangeBy keys=[[]] // { arity: 1 }
  246. Distinct project=[#0{f1}] // { arity: 1 }
  247. Get l0 // { arity: 1 }
  248. ArrangeBy keys=[[]] // { arity: 2 }
  249. ReadStorage materialize.public.t2 // { arity: 2 }
  250. Source materialize.public.t1
  251. Source materialize.public.t2
  252. Target cluster: quickstart
  253. EOF
  254. query IIIII
  255. SELECT * FROM t1, LATERAL(SELECT t2.*, ROW_NUMBER() OVER() FROM t2 WHERE t1.f1 = t2.f1) AS foo;
  256. ----
  257. 1 1 1 1 1
  258. 1 1 1 2 2
  259. 2 2 2 2 1
  260. query IIIII
  261. SELECT * FROM t1, LATERAL(SELECT t2.*, ROW_NUMBER() OVER() FROM t2 WHERE t1.f1 = t2.f1 AND t1.f2 = t2.f2) AS foo;
  262. ----
  263. 1 1 1 1 1
  264. 2 2 2 2 1
  265. query IIIII
  266. SELECT * FROM t1, LATERAL(SELECT t2.*, ROW_NUMBER() OVER() FROM t2 WHERE t1.f2 = t2.f2) AS foo;
  267. ----
  268. 1 1 1 1 1
  269. 2 2 1 2 1
  270. 2 2 2 2 2
  271. query IIIII rowsort
  272. SELECT * FROM t2, LATERAL(SELECT t1.*, ROW_NUMBER() OVER() FROM t1 WHERE t1.f1 = t2.f1) AS foo;
  273. ----
  274. 1 1 1 1 1
  275. 1 2 1 1 1
  276. 2 2 2 2 1
  277. query IIIII
  278. SELECT * FROM t2, LATERAL(SELECT t1.*, ROW_NUMBER() OVER() FROM t1 WHERE t1.f1 = t2.f1 AND t1.f2 = t2.f2) AS foo;
  279. ----
  280. 1 1 1 1 1
  281. 2 2 2 2 1
  282. query IIIII rowsort
  283. SELECT * FROM t2, LATERAL(SELECT t1.*, ROW_NUMBER() OVER() FROM t1 WHERE t1.f2 = t2.f2) AS foo;
  284. ----
  285. 1 1 1 1 1
  286. 1 2 2 2 1
  287. 2 2 2 2 1
  288. # Check that the partition key comes after the outer columns in the grouping key
  289. query T multiline
  290. EXPLAIN DECORRELATED PLAN WITH(arity) FOR SELECT * FROM t2, LATERAL(SELECT t1.*, ROW_NUMBER() OVER() FROM t1 WHERE t1.f2 = t2.f2) AS foo;
  291. ----
  292. With
  293. cte l0 =
  294. CrossJoin // { arity: 2 }
  295. Constant // { arity: 0 }
  296. - ()
  297. Get materialize.public.t2 // { arity: 2 }
  298. Return // { arity: 5 }
  299. Project (#0, #1, #3..=#5) // { arity: 5 }
  300. Join on=(#1 = #2) // { arity: 6 }
  301. Get l0 // { arity: 2 }
  302. Project (#0..=#2, #4) // { arity: 4 }
  303. Map (#3) // { arity: 5 }
  304. Project (#3..=#6) // { arity: 4 }
  305. Map (record_get[0](record_get[1](#2)), record_get[1](record_get[1](#2)), record_get[2](record_get[1](#2)), record_get[0](#2)) // { arity: 7 }
  306. FlatMap unnest_list(#1) // { arity: 3 }
  307. Reduce group_by=[#0] aggregates=[row_number[order_by=[]](row(list[row(#0, #1, #2)]))] // { arity: 2 }
  308. Filter (#2{f2} = #0{f2}) // { arity: 3 }
  309. CrossJoin // { arity: 3 }
  310. Distinct project=[#1] // { arity: 1 }
  311. Get l0 // { arity: 2 }
  312. Get materialize.public.t1 // { arity: 2 }
  313. Target cluster: quickstart
  314. EOF
  315. query T multiline
  316. EXPLAIN DECORRELATED PLAN WITH(arity) FOR SELECT * FROM t2, LATERAL(SELECT t1.*, ROW_NUMBER() OVER(PARTITION BY f1) FROM t1 WHERE t1.f2 = t2.f2) AS foo;
  317. ----
  318. With
  319. cte l0 =
  320. CrossJoin // { arity: 2 }
  321. Constant // { arity: 0 }
  322. - ()
  323. Get materialize.public.t2 // { arity: 2 }
  324. Return // { arity: 5 }
  325. Project (#0, #1, #3..=#5) // { arity: 5 }
  326. Join on=(#1 = #2) // { arity: 6 }
  327. Get l0 // { arity: 2 }
  328. Project (#0..=#2, #4) // { arity: 4 }
  329. Map (#3) // { arity: 5 }
  330. Project (#4..=#7) // { arity: 4 }
  331. Map (record_get[0](record_get[1](#3)), record_get[1](record_get[1](#3)), record_get[2](record_get[1](#3)), record_get[0](#3)) // { arity: 8 }
  332. FlatMap unnest_list(#2) // { arity: 4 }
  333. Reduce group_by=[#0, #1] aggregates=[row_number[order_by=[]](row(list[row(#0, #1, #2)]))] // { arity: 3 }
  334. Filter (#2{f2} = #0{f2}) // { arity: 3 }
  335. CrossJoin // { arity: 3 }
  336. Distinct project=[#1] // { arity: 1 }
  337. Get l0 // { arity: 2 }
  338. Get materialize.public.t1 // { arity: 2 }
  339. Target cluster: quickstart
  340. EOF
  341. statement ok
  342. DROP TABLE t;
  343. statement ok
  344. CREATE TABLE t(x string);
  345. statement ok
  346. INSERT INTO t VALUES ('a'), ('b'), ('c');
  347. # Regression for database-issues#2962
  348. query error window functions are not allowed in ON
  349. SELECT * FROM t AS v JOIN t ON row_number() over () > 1;
  350. query error window functions are not allowed in WHERE
  351. SELECT * FROM t
  352. WHERE row_number() over () > 1;
  353. statement ok
  354. CREATE TABLE not_allowed_tests (
  355. v INT,
  356. w INT,
  357. k INT
  358. );
  359. query error window functions are not allowed in GROUP BY
  360. SELECT * FROM not_allowed_tests GROUP BY v, count(w) OVER ();
  361. query error window functions are not allowed in GROUP BY
  362. SELECT count(w) OVER () FROM not_allowed_tests GROUP BY 1;
  363. query error window functions are not allowed in RETURNING
  364. INSERT INTO not_allowed_tests (k, v) VALUES (99, 100) RETURNING sum(v) OVER ();
  365. query error window functions are not allowed in LIMIT
  366. SELECT sum(v) FROM not_allowed_tests GROUP BY k LIMIT sum(v) OVER ();
  367. query error db error: ERROR: window functions are not allowed in OFFSET \(function pg_catalog\.sum\)
  368. SELECT sum(v) FROM not_allowed_tests GROUP BY k LIMIT 1 OFFSET sum(v) OVER ();
  369. query error window functions are not allowed in VALUES
  370. INSERT INTO not_allowed_tests (k, v) VALUES (99, count(1) OVER ());
  371. query error window functions are not allowed in WHERE
  372. SELECT k FROM not_allowed_tests WHERE avg(k) OVER () > 1;
  373. query error window functions are not allowed in HAVING
  374. SELECT 1 FROM not_allowed_tests GROUP BY 1 HAVING sum(1) OVER (PARTITION BY 1) > 1;
  375. query T
  376. SELECT DISTINCT ON (row_number() OVER ()) *
  377. FROM t
  378. ORDER BY row_number() OVER ()
  379. ----
  380. a
  381. b
  382. c
  383. # rank and dense_rank
  384. query error db error: ERROR: function rank has 0 parameters, but was called with 1
  385. SELECT rank(x) OVER (ORDER BY x), dense_rank() OVER (ORDER BY x), x FROM t
  386. ORDER BY dense_rank;
  387. query IT
  388. SELECT dense_rank() OVER (ORDER BY x), x FROM t
  389. ORDER BY dense_rank
  390. ----
  391. 1 a
  392. 2 b
  393. 3 c
  394. statement ok
  395. INSERT INTO t VALUES ('b');
  396. query IT
  397. SELECT dense_rank() OVER (ORDER BY x), x FROM t
  398. ORDER BY dense_rank
  399. ----
  400. 1 a
  401. 2 b
  402. 2 b
  403. 3 c
  404. statement ok
  405. INSERT INTO t VALUES ('c');
  406. query IT
  407. SELECT dense_rank() OVER (ORDER BY x), x FROM t
  408. ORDER BY dense_rank
  409. ----
  410. 1 a
  411. 2 b
  412. 2 b
  413. 3 c
  414. 3 c
  415. statement ok
  416. DROP TABLE t;
  417. statement ok
  418. CREATE TABLE t(x string);
  419. statement ok
  420. INSERT INTO t VALUES ('a'), ('b'), ('c');
  421. query IT
  422. SELECT dense_rank() OVER (ORDER BY x DESC), x FROM t
  423. ORDER BY dense_rank
  424. ----
  425. 1 c
  426. 2 b
  427. 3 a
  428. query IIT rowsort
  429. SELECT rank() OVER (ORDER BY x), dense_rank() OVER (ORDER BY x), x FROM t
  430. ----
  431. 1 1 a
  432. 2 2 b
  433. 3 3 c
  434. query IIT
  435. SELECT rank() OVER (ORDER BY x DESC), dense_rank() OVER (ORDER BY x DESC), x FROM t
  436. ORDER BY dense_rank
  437. ----
  438. 1 1 c
  439. 2 2 b
  440. 3 3 a
  441. statement ok
  442. INSERT INTO t VALUES ('b');
  443. query IT
  444. SELECT dense_rank() OVER (ORDER BY x DESC), x FROM t
  445. ORDER BY dense_rank
  446. ----
  447. 1 c
  448. 2 b
  449. 2 b
  450. 3 a
  451. query IIT
  452. SELECT rank() OVER (ORDER BY x), dense_rank() OVER (ORDER BY x), x FROM t
  453. ----
  454. 1 1 a
  455. 2 2 b
  456. 2 2 b
  457. 4 3 c
  458. query IIT
  459. SELECT rank() OVER (ORDER BY x DESC), dense_rank() OVER (ORDER BY x DESC), x FROM t
  460. ORDER BY dense_rank
  461. ----
  462. 1 1 c
  463. 2 2 b
  464. 2 2 b
  465. 4 3 a
  466. statement ok
  467. INSERT INTO t VALUES ('c');
  468. query IT
  469. SELECT dense_rank() OVER (ORDER BY x DESC), x FROM t
  470. ORDER BY dense_rank
  471. ----
  472. 1 c
  473. 1 c
  474. 2 b
  475. 2 b
  476. 3 a
  477. query IIT
  478. SELECT rank() OVER (ORDER BY x DESC), dense_rank() OVER (ORDER BY x DESC), x FROM t
  479. ORDER BY dense_rank
  480. ----
  481. 1 1 c
  482. 1 1 c
  483. 3 2 b
  484. 3 2 b
  485. 5 3 a
  486. statement ok
  487. DROP TABLE t;
  488. statement ok
  489. CREATE TABLE t(x string, y int);
  490. statement ok
  491. INSERT INTO t VALUES ('a', 98), ('b', 99), ('c', 98);
  492. query IIT rowsort
  493. SELECT rank() OVER (PARTITION BY y ORDER BY x), dense_rank() OVER (PARTITION BY y ORDER BY x), x FROM t;
  494. ----
  495. 1 1 a
  496. 1 1 b
  497. 2 2 c
  498. query IT
  499. SELECT dense_rank() OVER (PARTITION BY y ORDER BY x), x FROM t
  500. ORDER BY dense_rank, x;
  501. ----
  502. 1 a
  503. 1 b
  504. 2 c
  505. statement ok
  506. INSERT INTO t VALUES ('a', 98), ('a', 99);
  507. query IIT
  508. SELECT rank() OVER (PARTITION BY y ORDER BY x), dense_rank() OVER (PARTITION BY y ORDER BY x), x FROM t;
  509. ----
  510. 1 1 a
  511. 1 1 a
  512. 1 1 a
  513. 2 2 b
  514. 3 2 c
  515. query IT
  516. SELECT dense_rank() OVER (PARTITION BY y ORDER BY x), x FROM t
  517. ORDER BY dense_rank, x;
  518. ----
  519. 1 a
  520. 1 a
  521. 1 a
  522. 2 b
  523. 2 c
  524. statement ok
  525. DROP TABLE t;
  526. statement ok
  527. CREATE TABLE t(x string, y int);
  528. statement ok
  529. INSERT INTO t VALUES ('a', 1), ('b', 2), ('c', 1);
  530. query IIT
  531. SELECT rank() OVER (PARTITION BY y ORDER BY x DESC), dense_rank() OVER (PARTITION BY y ORDER BY x DESC), x FROM t;
  532. ----
  533. 1 1 b
  534. 1 1 c
  535. 2 2 a
  536. query IT
  537. SELECT dense_rank() OVER (PARTITION BY y ORDER BY x DESC), x FROM t
  538. ORDER BY dense_rank, x;
  539. ----
  540. 1 b
  541. 1 c
  542. 2 a
  543. query IIT rowsort
  544. SELECT rank() OVER (PARTITION BY x ORDER BY x), dense_rank() OVER (PARTITION BY x ORDER BY x), x FROM t;
  545. ----
  546. 1 1 a
  547. 1 1 b
  548. 1 1 c
  549. query IT
  550. SELECT dense_rank() OVER (PARTITION BY x ORDER BY x), x FROM t
  551. ORDER BY dense_rank, x;
  552. ----
  553. 1 a
  554. 1 b
  555. 1 c
  556. query IIT rowsort
  557. SELECT rank() OVER (PARTITION BY NULL ORDER BY 10000), dense_rank() OVER (PARTITION BY NULL ORDER BY 10000) AS q, a1.x
  558. FROM t AS a1, t AS a2;
  559. ----
  560. 1 1 a
  561. 1 1 a
  562. 1 1 a
  563. 1 1 b
  564. 1 1 b
  565. 1 1 b
  566. 1 1 c
  567. 1 1 c
  568. 1 1 c
  569. query IT
  570. SELECT dense_rank() OVER (PARTITION BY NULL ORDER BY 10000) AS q, a1.x
  571. FROM t AS a1, t AS a2
  572. ORDER BY q DESC, a1.x DESC;
  573. ----
  574. 1 c
  575. 1 c
  576. 1 c
  577. 1 b
  578. 1 b
  579. 1 b
  580. 1 a
  581. 1 a
  582. 1 a
  583. statement ok
  584. DROP TABLE t;
  585. statement ok
  586. CREATE TABLE t(x string);
  587. statement ok
  588. INSERT INTO t VALUES ('a');
  589. # Make sure a non-column expression following the window function is correctly
  590. # handled.
  591. query IITT
  592. SELECT rank() OVER (PARTITION BY NULL), dense_rank() OVER (PARTITION BY NULL) AS q, x, 'b'
  593. FROM t;
  594. ----
  595. 1 1 a b
  596. query ITT
  597. SELECT dense_rank() OVER (PARTITION BY NULL) AS q, x, 'b'
  598. FROM t;
  599. ----
  600. 1 a b
  601. statement ok
  602. DROP TABLE t;
  603. statement ok
  604. CREATE TABLE t(x int, y string, z numeric);
  605. statement ok
  606. INSERT INTO t VALUES (1, 'a', 1.0), (2, 'a', 1.0), (2, 'a', 1.0), (3, 'a', 1.0), (4, 'b', 0), (4, 'b', 1), (1, 'c', 'NaN'), (2, 'c', 'NaN'), (3, 'c', 1.0);
  607. query IIITT
  608. SELECT rank() OVER (PARTITION BY y ORDER BY x DESC, z), dense_rank() OVER (PARTITION BY y ORDER BY x DESC, z), x, y, z
  609. FROM t;
  610. ----
  611. 1 1 3 a 1
  612. 1 1 3 c 1
  613. 1 1 4 b 0
  614. 2 2 2 a 1
  615. 2 2 2 a 1
  616. 2 2 2 c NaN
  617. 2 2 4 b 1
  618. 3 3 1 c NaN
  619. 4 3 1 a 1
  620. query IITT
  621. SELECT dense_rank() OVER (PARTITION BY y ORDER BY x DESC, z), x, y, z
  622. FROM t
  623. ORDER BY y, x DESC, z;
  624. ----
  625. 1 3 a 1
  626. 2 2 a 1
  627. 2 2 a 1
  628. 3 1 a 1
  629. 1 4 b 0
  630. 2 4 b 1
  631. 1 3 c 1
  632. 2 2 c NaN
  633. 3 1 c NaN
  634. # NaNs have the same rank
  635. query IIITT
  636. WITH t (x, y, z) AS (VALUES (1, 'a', 1.0), (2, 'a', 1.0), (2, 'a', 1.0), (3, 'a', 1.0), (4, 'b', 0), (4, 'b', 1), (1, 'c', 'NaN'), (2, 'c', 'NaN'), (3, 'c', 1.0))
  637. SELECT rank() OVER (PARTITION BY y ORDER BY z DESC), dense_rank() OVER (PARTITION BY y ORDER BY z DESC), x, y, z
  638. FROM t;
  639. ----
  640. 1 1 1 a 1
  641. 1 1 1 c NaN
  642. 1 1 2 a 1
  643. 1 1 2 a 1
  644. 1 1 2 c NaN
  645. 1 1 3 a 1
  646. 1 1 4 b 1
  647. 2 2 4 b 0
  648. 3 2 3 c 1
  649. query IITT
  650. SELECT dense_rank() OVER (PARTITION BY y ORDER BY z DESC), x, y, z
  651. FROM t
  652. ORDER BY y, z DESC, x;
  653. ----
  654. 1 1 a 1
  655. 1 2 a 1
  656. 1 2 a 1
  657. 1 3 a 1
  658. 1 4 b 1
  659. 2 4 b 0
  660. 1 1 c NaN
  661. 1 2 c NaN
  662. 2 3 c 1
  663. ## lag
  664. statement ok
  665. DROP TABLE t;
  666. statement ok
  667. CREATE TABLE t(x string);
  668. statement ok
  669. INSERT INTO t VALUES ('a'), ('b'), ('c');
  670. # Simple cases
  671. query TT
  672. SELECT lag(x) OVER (ORDER BY x), x FROM t
  673. ORDER BY x, lag
  674. ----
  675. NULL a
  676. a b
  677. b c
  678. statement ok
  679. INSERT INTO t VALUES ('b');
  680. query TT
  681. SELECT lag(x) OVER (ORDER BY x), x FROM t
  682. ORDER BY x, lag
  683. ----
  684. NULL a
  685. a b
  686. b b
  687. b c
  688. statement ok
  689. INSERT INTO t VALUES ('c');
  690. query TT
  691. SELECT lag(x) OVER (ORDER BY x), x FROM t
  692. ORDER BY x, lag
  693. ----
  694. NULL a
  695. a b
  696. b b
  697. b c
  698. c c
  699. statement ok
  700. DROP TABLE t;
  701. statement ok
  702. CREATE TABLE t(x string);
  703. statement ok
  704. INSERT INTO t VALUES ('a'), ('b'), ('c');
  705. query TT
  706. SELECT lag(x) OVER (ORDER BY x DESC), x FROM t
  707. ORDER BY x, lag
  708. ----
  709. b a
  710. c b
  711. NULL c
  712. statement ok
  713. INSERT INTO t VALUES ('b');
  714. query TT
  715. SELECT lag(x) OVER (ORDER BY x DESC), x FROM t
  716. ORDER BY x, lag
  717. ----
  718. b a
  719. b b
  720. c b
  721. NULL c
  722. statement ok
  723. INSERT INTO t VALUES ('c');
  724. query TT
  725. SELECT lag(x) OVER (ORDER BY x DESC), x FROM t
  726. ORDER BY x, lag
  727. ----
  728. b a
  729. b b
  730. c b
  731. c c
  732. NULL c
  733. statement ok
  734. DROP TABLE t;
  735. statement ok
  736. CREATE TABLE t(x string, y int);
  737. statement ok
  738. INSERT INTO t VALUES ('a', 98), ('b', 99), ('c', 98);
  739. query TT
  740. SELECT lag(x) OVER (PARTITION BY y ORDER BY x), x FROM t
  741. ORDER BY x, lag
  742. ----
  743. NULL a
  744. NULL b
  745. a c
  746. statement ok
  747. INSERT INTO t VALUES ('a', 98), ('a', 99);
  748. query TT
  749. SELECT lag(x) OVER (PARTITION BY y ORDER BY x), x FROM t
  750. ORDER BY x, lag
  751. ----
  752. a a
  753. NULL a
  754. NULL a
  755. a b
  756. a c
  757. statement ok
  758. DROP TABLE t;
  759. statement ok
  760. CREATE TABLE t(x string, y int);
  761. statement ok
  762. INSERT INTO t VALUES ('a', 1), ('b', 2), ('c', 1);
  763. query TT
  764. SELECT lag(x) OVER (PARTITION BY y ORDER BY x DESC), x FROM t
  765. ORDER BY x, lag
  766. ----
  767. c a
  768. NULL b
  769. NULL c
  770. query TT
  771. SELECT lag(x) OVER (PARTITION BY x ORDER BY x), x FROM t
  772. ORDER BY x, lag
  773. ----
  774. NULL a
  775. NULL b
  776. NULL c
  777. query TT
  778. SELECT lag(a1.x) OVER (PARTITION BY NULL ORDER BY 10000) AS q, a1.x
  779. FROM t AS a1, t AS a2
  780. ORDER BY q DESC, a1.x DESC
  781. ----
  782. NULL a
  783. c c
  784. c c
  785. b c
  786. b b
  787. b b
  788. a b
  789. a a
  790. a a
  791. statement ok
  792. DROP TABLE t;
  793. statement ok
  794. CREATE TABLE t(f1 int, f2 string, f3 numeric);
  795. statement ok
  796. INSERT INTO t VALUES (1, 'a', 1.0), (2, 'a', 1.0), (2, 'a', 1.0), (3, 'a', 1.0), (4, 'b', 0), (4, 'b', 1), (1, 'c', 'NaN'), (2, 'c', 'NaN'), (3, 'c', 1.0), (7, 'd', -5.0);
  797. query ITTT
  798. SELECT f1, f2, f3, lag(f1) OVER (PARTITION BY f2 ORDER BY f3 DESC, f1)
  799. FROM t
  800. ORDER BY f2 DESC, f3 DESC, f1, lag
  801. ----
  802. 7 d -5 NULL
  803. 1 c NaN NULL
  804. 2 c NaN 1
  805. 3 c 1 2
  806. 4 b 1 NULL
  807. 4 b 0 4
  808. 1 a 1 NULL
  809. 2 a 1 1
  810. 2 a 1 2
  811. 3 a 1 2
  812. query ITTT
  813. SELECT f1, f2, f3, lag(f1, 1) OVER (PARTITION BY f2 ORDER BY f3 DESC, f1)
  814. FROM t
  815. ORDER BY f2 DESC, f3 DESC, f1, lag
  816. ----
  817. 7 d -5 NULL
  818. 1 c NaN NULL
  819. 2 c NaN 1
  820. 3 c 1 2
  821. 4 b 1 NULL
  822. 4 b 0 4
  823. 1 a 1 NULL
  824. 2 a 1 1
  825. 2 a 1 2
  826. 3 a 1 2
  827. query ITTT
  828. SELECT f1, f2, f3, lag(f1, 1, NULL) OVER (PARTITION BY f2 ORDER BY f3 DESC, f1)
  829. FROM t
  830. ORDER BY f2 DESC, f3 DESC, f1, lag
  831. ----
  832. 7 d -5 NULL
  833. 1 c NaN NULL
  834. 2 c NaN 1
  835. 3 c 1 2
  836. 4 b 1 NULL
  837. 4 b 0 4
  838. 1 a 1 NULL
  839. 2 a 1 1
  840. 2 a 1 2
  841. 3 a 1 2
  842. # With default value
  843. query ITTT
  844. SELECT f1, f2, f3, lag(f1, 1, -1) OVER (PARTITION BY f2 ORDER BY f3 DESC, f1)
  845. FROM t
  846. ORDER BY f2 DESC, f3 DESC, f1, lag
  847. ----
  848. 7 d -5 -1
  849. 1 c NaN -1
  850. 2 c NaN 1
  851. 3 c 1 2
  852. 4 b 1 -1
  853. 4 b 0 4
  854. 1 a 1 -1
  855. 2 a 1 1
  856. 2 a 1 2
  857. 3 a 1 2
  858. # Complex expressions
  859. query ITTT
  860. SELECT f1, f2, f3, lag(f1 + coalesce(nullif(f3, 'NaN'), -10)) OVER (PARTITION BY f2 ORDER BY f3 DESC, f1)
  861. FROM t
  862. ORDER BY f2 DESC, f3 DESC, f1, lag
  863. ----
  864. 7 d -5 NULL
  865. 1 c NaN NULL
  866. 2 c NaN -9
  867. 3 c 1 -8
  868. 4 b 1 NULL
  869. 4 b 0 5
  870. 1 a 1 NULL
  871. 2 a 1 2
  872. 2 a 1 3
  873. 3 a 1 3
  874. # Nulls in the first argument
  875. query ITTT
  876. SELECT f1, f2, f3, lag(NULL::int) OVER (PARTITION BY f2 ORDER BY f3 DESC, f1)
  877. FROM t
  878. ORDER BY f2 DESC, f3 DESC, f1, lag
  879. ----
  880. 7 d -5 NULL
  881. 1 c NaN NULL
  882. 2 c NaN NULL
  883. 3 c 1 NULL
  884. 4 b 1 NULL
  885. 4 b 0 NULL
  886. 1 a 1 NULL
  887. 2 a 1 NULL
  888. 2 a 1 NULL
  889. 3 a 1 NULL
  890. query ITTT
  891. SELECT f1, f2, f3, lag(nullif(f1, 4)) OVER (PARTITION BY f2 ORDER BY f3 DESC, f1)
  892. FROM t
  893. ORDER BY f2 DESC, f3 DESC, f1, lag
  894. ----
  895. 7 d -5 NULL
  896. 1 c NaN NULL
  897. 2 c NaN 1
  898. 3 c 1 2
  899. 4 b 1 NULL
  900. 4 b 0 NULL
  901. 1 a 1 NULL
  902. 2 a 1 1
  903. 2 a 1 2
  904. 3 a 1 2
  905. # Nulls in the first argument with a default value in the third argument
  906. query ITTT
  907. SELECT f1, f2, f3, lag(NULL::int, 0) OVER (PARTITION BY f2 ORDER BY f3 DESC, f1)
  908. FROM t
  909. ORDER BY f2 DESC, f3 DESC, f1, lag
  910. ----
  911. 7 d -5 NULL
  912. 1 c NaN NULL
  913. 2 c NaN NULL
  914. 3 c 1 NULL
  915. 4 b 1 NULL
  916. 4 b 0 NULL
  917. 1 a 1 NULL
  918. 2 a 1 NULL
  919. 2 a 1 NULL
  920. 3 a 1 NULL
  921. # Zero offset
  922. query ITTT
  923. SELECT f1, f2, f3, lag(f1, 0) OVER (PARTITION BY f2 ORDER BY f3 DESC, f1)
  924. FROM t
  925. ORDER BY f2 DESC, f3 DESC, f1, lag
  926. ----
  927. 7 d -5 7
  928. 1 c NaN 1
  929. 2 c NaN 2
  930. 3 c 1 3
  931. 4 b 1 4
  932. 4 b 0 4
  933. 1 a 1 1
  934. 2 a 1 2
  935. 2 a 1 2
  936. 3 a 1 3
  937. query ITTT
  938. SELECT f1, f2, f3, lag(f1 + f3, 0) OVER (PARTITION BY f2 ORDER BY f3 DESC, f1)
  939. FROM t
  940. ORDER BY f2 DESC, f3 DESC, f1, lag
  941. ----
  942. 7 d -5 2
  943. 1 c NaN NaN
  944. 2 c NaN NaN
  945. 3 c 1 4
  946. 4 b 1 5
  947. 4 b 0 4
  948. 1 a 1 2
  949. 2 a 1 3
  950. 2 a 1 3
  951. 3 a 1 4
  952. # Positive offsets
  953. query ITTT
  954. SELECT f1, f2, f3, lag(f1, 2) OVER (PARTITION BY f2 ORDER BY f3 DESC, f1)
  955. FROM t
  956. ORDER BY f2 DESC, f3 DESC, f1, lag
  957. ----
  958. 7 d -5 NULL
  959. 1 c NaN NULL
  960. 2 c NaN NULL
  961. 3 c 1 1
  962. 4 b 1 NULL
  963. 4 b 0 NULL
  964. 1 a 1 NULL
  965. 2 a 1 1
  966. 2 a 1 NULL
  967. 3 a 1 2
  968. query ITTT
  969. SELECT f1, f2, f3, lag(f1 + f3, 2) OVER (PARTITION BY f2 ORDER BY f3 DESC, f1)
  970. FROM t
  971. ORDER BY f2 DESC, f3 DESC, f1, lag
  972. ----
  973. 7 d -5 NULL
  974. 1 c NaN NULL
  975. 2 c NaN NULL
  976. 3 c 1 NaN
  977. 4 b 1 NULL
  978. 4 b 0 NULL
  979. 1 a 1 NULL
  980. 2 a 1 2
  981. 2 a 1 NULL
  982. 3 a 1 3
  983. # Out of range positive offsets
  984. query ITTT
  985. SELECT f1, f2, f3, lag(f1, 10) OVER (PARTITION BY f2 ORDER BY f3 DESC, f1)
  986. FROM t
  987. ORDER BY f2 DESC, f3 DESC, f1, lag
  988. ----
  989. 7 d -5 NULL
  990. 1 c NaN NULL
  991. 2 c NaN NULL
  992. 3 c 1 NULL
  993. 4 b 1 NULL
  994. 4 b 0 NULL
  995. 1 a 1 NULL
  996. 2 a 1 NULL
  997. 2 a 1 NULL
  998. 3 a 1 NULL
  999. query ITTT
  1000. SELECT f1, f2, f3, lag(f1 + f3, 10) OVER (PARTITION BY f2 ORDER BY f3 DESC, f1)
  1001. FROM t
  1002. ORDER BY f2 DESC, f3 DESC, f1, lag
  1003. ----
  1004. 7 d -5 NULL
  1005. 1 c NaN NULL
  1006. 2 c NaN NULL
  1007. 3 c 1 NULL
  1008. 4 b 1 NULL
  1009. 4 b 0 NULL
  1010. 1 a 1 NULL
  1011. 2 a 1 NULL
  1012. 2 a 1 NULL
  1013. 3 a 1 NULL
  1014. query ITTT
  1015. SELECT f1, f2, f3, lag(f1 + f3, 10, 0) OVER (PARTITION BY f2 ORDER BY f3 DESC, f1)
  1016. FROM t
  1017. ORDER BY f2 DESC, f3 DESC, f1, lag
  1018. ----
  1019. 7 d -5 0
  1020. 1 c NaN 0
  1021. 2 c NaN 0
  1022. 3 c 1 0
  1023. 4 b 1 0
  1024. 4 b 0 0
  1025. 1 a 1 0
  1026. 2 a 1 0
  1027. 2 a 1 0
  1028. 3 a 1 0
  1029. # Negative offsets
  1030. query ITTT
  1031. SELECT f1, f2, f3, lag(f1, -1) OVER (PARTITION BY f2 ORDER BY f3 DESC, f1)
  1032. FROM t
  1033. ORDER BY f2 DESC, f3 DESC, f1, lag
  1034. ----
  1035. 7 d -5 NULL
  1036. 1 c NaN 2
  1037. 2 c NaN 3
  1038. 3 c 1 NULL
  1039. 4 b 1 4
  1040. 4 b 0 NULL
  1041. 1 a 1 2
  1042. 2 a 1 2
  1043. 2 a 1 3
  1044. 3 a 1 NULL
  1045. query ITTT
  1046. SELECT f1, f2, f3, lag(f1 + f3, -1) OVER (PARTITION BY f2 ORDER BY f3 DESC, f1)
  1047. FROM t
  1048. ORDER BY f2 DESC, f3 DESC, f1, lag
  1049. ----
  1050. 7 d -5 NULL
  1051. 1 c NaN NaN
  1052. 2 c NaN 4
  1053. 3 c 1 NULL
  1054. 4 b 1 4
  1055. 4 b 0 NULL
  1056. 1 a 1 3
  1057. 2 a 1 3
  1058. 2 a 1 4
  1059. 3 a 1 NULL
  1060. query ITTT
  1061. SELECT f1, f2, f3, lag(f1 + f3, -2) OVER (PARTITION BY f2 ORDER BY f3 DESC, f1)
  1062. FROM t
  1063. ORDER BY f2 DESC, f3 DESC, f1, lag
  1064. ----
  1065. 7 d -5 NULL
  1066. 1 c NaN 4
  1067. 2 c NaN NULL
  1068. 3 c 1 NULL
  1069. 4 b 1 NULL
  1070. 4 b 0 NULL
  1071. 1 a 1 3
  1072. 2 a 1 4
  1073. 2 a 1 NULL
  1074. 3 a 1 NULL
  1075. # Out of range negative offsets
  1076. query ITTT
  1077. SELECT f1, f2, f3, lag(f1, -10) OVER (PARTITION BY f2 ORDER BY f3 DESC, f1)
  1078. FROM t
  1079. ORDER BY f2 DESC, f3 DESC, f1, lag
  1080. ----
  1081. 7 d -5 NULL
  1082. 1 c NaN NULL
  1083. 2 c NaN NULL
  1084. 3 c 1 NULL
  1085. 4 b 1 NULL
  1086. 4 b 0 NULL
  1087. 1 a 1 NULL
  1088. 2 a 1 NULL
  1089. 2 a 1 NULL
  1090. 3 a 1 NULL
  1091. query ITTT
  1092. SELECT f1, f2, f3, lag(f1 + f3, -10) OVER (PARTITION BY f2 ORDER BY f3 DESC, f1)
  1093. FROM t
  1094. ORDER BY f2 DESC, f3 DESC, f1, lag
  1095. ----
  1096. 7 d -5 NULL
  1097. 1 c NaN NULL
  1098. 2 c NaN NULL
  1099. 3 c 1 NULL
  1100. 4 b 1 NULL
  1101. 4 b 0 NULL
  1102. 1 a 1 NULL
  1103. 2 a 1 NULL
  1104. 2 a 1 NULL
  1105. 3 a 1 NULL
  1106. query ITTT
  1107. SELECT f1, f2, f3, lag(f1, -10, 0) OVER (PARTITION BY f2 ORDER BY f3 DESC, f1)
  1108. FROM t
  1109. ORDER BY f2 DESC, f3 DESC, f1, lag
  1110. ----
  1111. 7 d -5 0
  1112. 1 c NaN 0
  1113. 2 c NaN 0
  1114. 3 c 1 0
  1115. 4 b 1 0
  1116. 4 b 0 0
  1117. 1 a 1 0
  1118. 2 a 1 0
  1119. 2 a 1 0
  1120. 3 a 1 0
  1121. # Variable per row offsets
  1122. query ITTT
  1123. SELECT f1, f2, f3, lag(f1, f1) OVER (PARTITION BY f2 ORDER BY f3 DESC, f1)
  1124. FROM t
  1125. ORDER BY f2 DESC, f3 DESC, f1, lag
  1126. ----
  1127. 7 d -5 NULL
  1128. 1 c NaN NULL
  1129. 2 c NaN NULL
  1130. 3 c 1 NULL
  1131. 4 b 1 NULL
  1132. 4 b 0 NULL
  1133. 1 a 1 NULL
  1134. 2 a 1 1
  1135. 2 a 1 NULL
  1136. 3 a 1 1
  1137. query ITTT
  1138. SELECT f1, f2, f3, lag(f1, f1 - 1) OVER (PARTITION BY f2 ORDER BY f3 DESC, f1)
  1139. FROM t
  1140. ORDER BY f2 DESC, f3 DESC, f1, lag
  1141. ----
  1142. 7 d -5 NULL
  1143. 1 c NaN 1
  1144. 2 c NaN 1
  1145. 3 c 1 1
  1146. 4 b 1 NULL
  1147. 4 b 0 NULL
  1148. 1 a 1 1
  1149. 2 a 1 1
  1150. 2 a 1 2
  1151. 3 a 1 2
  1152. # Null offsets
  1153. query ITTT
  1154. SELECT f1, f2, f3, lag(f1, NULL) OVER (PARTITION BY f2 ORDER BY f3 DESC, f1)
  1155. FROM t
  1156. ORDER BY f2 DESC, f3 DESC, f1, lag
  1157. ----
  1158. 7 d -5 NULL
  1159. 1 c NaN NULL
  1160. 2 c NaN NULL
  1161. 3 c 1 NULL
  1162. 4 b 1 NULL
  1163. 4 b 0 NULL
  1164. 1 a 1 NULL
  1165. 2 a 1 NULL
  1166. 2 a 1 NULL
  1167. 3 a 1 NULL
  1168. query ITTT
  1169. SELECT f1, f2, f3, lag(f1, nullif(f1, 1)) OVER (PARTITION BY f2 ORDER BY f3 DESC, f1)
  1170. FROM t
  1171. ORDER BY f2 DESC, f3 DESC, f1, lag
  1172. ----
  1173. 7 d -5 NULL
  1174. 1 c NaN NULL
  1175. 2 c NaN NULL
  1176. 3 c 1 NULL
  1177. 4 b 1 NULL
  1178. 4 b 0 NULL
  1179. 1 a 1 NULL
  1180. 2 a 1 1
  1181. 2 a 1 NULL
  1182. 3 a 1 1
  1183. # Null offset with default value
  1184. query ITTT
  1185. SELECT f1, f2, f3, lag(f1, NULL, -10) OVER (PARTITION BY f2 ORDER BY f3 DESC, f1)
  1186. FROM t
  1187. ORDER BY f2 DESC, f3 DESC, f1, lag
  1188. ----
  1189. 7 d -5 NULL
  1190. 1 c NaN NULL
  1191. 2 c NaN NULL
  1192. 3 c 1 NULL
  1193. 4 b 1 NULL
  1194. 4 b 0 NULL
  1195. 1 a 1 NULL
  1196. 2 a 1 NULL
  1197. 2 a 1 NULL
  1198. 3 a 1 NULL
  1199. # Variable per row default value
  1200. query ITTT
  1201. SELECT f1, f2, f3, lag(f1, 1, f3) OVER (PARTITION BY f2 ORDER BY f3 DESC, f1)
  1202. FROM t
  1203. ORDER BY f2 DESC, f3 DESC, f1, lag
  1204. ----
  1205. 7 d -5 -5
  1206. 1 c NaN NaN
  1207. 2 c NaN 1
  1208. 3 c 1 2
  1209. 4 b 1 1
  1210. 4 b 0 4
  1211. 1 a 1 1
  1212. 2 a 1 1
  1213. 2 a 1 2
  1214. 3 a 1 2
  1215. statement ok
  1216. DROP TABLE t;
  1217. statement ok
  1218. CREATE TABLE t(f1 int, f2 int);
  1219. statement ok
  1220. INSERT INTO t VALUES (1, 2), (1, 2), (3, 4);
  1221. # reduce_elision code path
  1222. # Default offset
  1223. query II
  1224. SELECT f1, lag(f1) OVER (PARTITION BY f1, f2)
  1225. FROM (SELECT DISTINCT f1, f2 FROM t) q
  1226. ----
  1227. 1 NULL
  1228. 3 NULL
  1229. query II
  1230. SELECT f1, lag(f1, 1) OVER (PARTITION BY f1, f2)
  1231. FROM (SELECT DISTINCT f1, f2 FROM t) q
  1232. ----
  1233. 1 NULL
  1234. 3 NULL
  1235. # Zero offset
  1236. query II
  1237. SELECT f1, lag(f1, 0) OVER (PARTITION BY f1, f2)
  1238. FROM (SELECT DISTINCT f1, f2 FROM t) q
  1239. ----
  1240. 1 1
  1241. 3 3
  1242. # Negative offset
  1243. query II
  1244. SELECT f1, lag(f1, -1) OVER (PARTITION BY f1, f2)
  1245. FROM (SELECT DISTINCT f1, f2 FROM t) q
  1246. ----
  1247. 1 NULL
  1248. 3 NULL
  1249. # Default value with offset 1
  1250. query II
  1251. SELECT f1, lag(f1, 1, 10) OVER (PARTITION BY f1, f2)
  1252. FROM (SELECT DISTINCT f1, f2 FROM t) q
  1253. ----
  1254. 1 10
  1255. 3 10
  1256. # Default value with offset 0
  1257. query II
  1258. SELECT f1, lag(f1, 0, 10) OVER (PARTITION BY f1, f2)
  1259. FROM (SELECT DISTINCT f1, f2 FROM t) q
  1260. ----
  1261. 1 1
  1262. 3 3
  1263. # Complex expression
  1264. query II
  1265. SELECT f1, lag(f1 * f2, 0, 10) OVER (PARTITION BY f1, f2)
  1266. FROM (SELECT DISTINCT f1, f2 FROM t) q
  1267. ----
  1268. 1 2
  1269. 3 12
  1270. query II
  1271. SELECT f1, lag(f1 * f2, 1, f1 * f2 + 1) OVER (PARTITION BY f1, f2)
  1272. FROM (SELECT DISTINCT f1, f2 FROM t) q
  1273. ----
  1274. 1 3
  1275. 3 13
  1276. # Complex offset
  1277. query II
  1278. SELECT f1, lag(f1 * f2, f1 - f1) OVER (PARTITION BY f1, f2)
  1279. FROM (SELECT DISTINCT f1, f2 FROM t) q
  1280. ----
  1281. 1 2
  1282. 3 12
  1283. query II rowsort
  1284. SELECT f1, lag(f1 * f2, f1 - 1) OVER (PARTITION BY f1, f2)
  1285. FROM (SELECT DISTINCT f1, f2 FROM t) q
  1286. ----
  1287. 3 NULL
  1288. 1 2
  1289. query II
  1290. SELECT f1, lag(f1 * f2, f2 - 2 * f1, f2) OVER (PARTITION BY f1, f2)
  1291. FROM (SELECT DISTINCT f1, f2 FROM t) q
  1292. ----
  1293. 1 2
  1294. 3 4
  1295. # Complex default value
  1296. query II
  1297. SELECT f1, lag(f1, 0, f1 * f2) OVER (PARTITION BY f1, f2)
  1298. FROM (SELECT DISTINCT f1, f2 FROM t) q
  1299. ----
  1300. 1 1
  1301. 3 3
  1302. query II
  1303. SELECT f1, lag(f1, 1, f1 * f2) OVER (PARTITION BY f1, f2)
  1304. FROM (SELECT DISTINCT f1, f2 FROM t) q
  1305. ----
  1306. 1 2
  1307. 3 12
  1308. # Null value in input relation
  1309. # This was caused by a bug in the reduce elision logic
  1310. statement ok
  1311. CREATE TABLE t3 (f1 INTEGER)
  1312. ----
  1313. statement ok
  1314. INSERT INTO t3 VALUES (NULL)
  1315. ----
  1316. query II
  1317. SELECT f1, lag(0, f1 , 0) OVER (PARTITION BY f1 ORDER BY f1) FROM t3 GROUP BY f1 ORDER BY 1
  1318. ----
  1319. NULL NULL
  1320. ## lead
  1321. statement ok
  1322. DROP TABLE t;
  1323. statement ok
  1324. CREATE TABLE t(x string);
  1325. statement ok
  1326. INSERT INTO t VALUES ('a'), ('b'), ('c');
  1327. # Simple cases
  1328. query TT
  1329. SELECT lead(x) OVER (ORDER BY x), x FROM t
  1330. ORDER BY x, lead
  1331. ----
  1332. b a
  1333. c b
  1334. NULL c
  1335. statement ok
  1336. INSERT INTO t VALUES ('b');
  1337. query TT
  1338. SELECT lead(x) OVER (ORDER BY x), x FROM t
  1339. ORDER BY x, lead
  1340. ----
  1341. b a
  1342. b b
  1343. c b
  1344. NULL c
  1345. statement ok
  1346. INSERT INTO t VALUES ('c');
  1347. query TT
  1348. SELECT lead(x) OVER (ORDER BY x), x FROM t
  1349. ORDER BY x, lead
  1350. ----
  1351. b a
  1352. b b
  1353. c b
  1354. c c
  1355. NULL c
  1356. statement ok
  1357. DROP TABLE t;
  1358. statement ok
  1359. CREATE TABLE t(x string);
  1360. statement ok
  1361. INSERT INTO t VALUES ('a'), ('b'), ('c');
  1362. query TT
  1363. SELECT lead(x) OVER (ORDER BY x DESC), x FROM t
  1364. ORDER BY x, lead
  1365. ----
  1366. NULL a
  1367. a b
  1368. b c
  1369. statement ok
  1370. INSERT INTO t VALUES ('b');
  1371. query TT
  1372. SELECT lead(x) OVER (ORDER BY x DESC), x FROM t
  1373. ORDER BY x, lead
  1374. ----
  1375. NULL a
  1376. a b
  1377. b b
  1378. b c
  1379. statement ok
  1380. INSERT INTO t VALUES ('c');
  1381. query TT
  1382. SELECT lead(x) OVER (ORDER BY x DESC), x FROM t
  1383. ORDER BY x, lead
  1384. ----
  1385. NULL a
  1386. a b
  1387. b b
  1388. b c
  1389. c c
  1390. statement ok
  1391. DROP TABLE t;
  1392. statement ok
  1393. CREATE TABLE t(x string, y int);
  1394. statement ok
  1395. INSERT INTO t VALUES ('a', 98), ('b', 99), ('c', 98);
  1396. query TT
  1397. SELECT lead(x) OVER (PARTITION BY y ORDER BY x), x FROM t
  1398. ORDER BY x, lead
  1399. ----
  1400. c a
  1401. NULL b
  1402. NULL c
  1403. statement ok
  1404. INSERT INTO t VALUES ('a', 98), ('a', 99);
  1405. query TT
  1406. SELECT lead(x) OVER (PARTITION BY y ORDER BY x), x FROM t
  1407. ORDER BY x, lead
  1408. ----
  1409. a a
  1410. b a
  1411. c a
  1412. NULL b
  1413. NULL c
  1414. statement ok
  1415. DROP TABLE t;
  1416. statement ok
  1417. CREATE TABLE t(x string, y int);
  1418. statement ok
  1419. INSERT INTO t VALUES ('a', 1), ('b', 2), ('c', 1);
  1420. query TT
  1421. SELECT lead(x) OVER (PARTITION BY y ORDER BY x DESC), x FROM t
  1422. ORDER BY x, lead
  1423. ----
  1424. NULL a
  1425. NULL b
  1426. a c
  1427. query TT
  1428. SELECT lead(x) OVER (PARTITION BY x ORDER BY x), x FROM t
  1429. ORDER BY x, lead
  1430. ----
  1431. NULL a
  1432. NULL b
  1433. NULL c
  1434. query TT
  1435. SELECT lead(a1.x) OVER (PARTITION BY NULL ORDER BY 10000) AS q, a1.x
  1436. FROM t AS a1, t AS a2
  1437. ORDER BY q DESC, a1.x DESC
  1438. ----
  1439. NULL c
  1440. c c
  1441. c c
  1442. c b
  1443. b b
  1444. b b
  1445. b a
  1446. a a
  1447. a a
  1448. statement ok
  1449. DROP TABLE t;
  1450. statement ok
  1451. CREATE TABLE t(f1 int, f2 string, f3 numeric);
  1452. statement ok
  1453. INSERT INTO t VALUES (1, 'a', 1.0), (2, 'a', 1.0), (2, 'a', 1.0), (3, 'a', 1.0), (4, 'b', 0), (4, 'b', 1), (1, 'c', 'NaN'), (2, 'c', 'NaN'), (3, 'c', 1.0), (7, 'd', -5.0);
  1454. query ITTT
  1455. SELECT f1, f2, f3, lead(f1) OVER (PARTITION BY f2 ORDER BY f3 DESC, f1)
  1456. FROM t
  1457. ORDER BY f2 DESC, f3 DESC, f1, lead
  1458. ----
  1459. 7 d -5 NULL
  1460. 1 c NaN 2
  1461. 2 c NaN 3
  1462. 3 c 1 NULL
  1463. 4 b 1 4
  1464. 4 b 0 NULL
  1465. 1 a 1 2
  1466. 2 a 1 2
  1467. 2 a 1 3
  1468. 3 a 1 NULL
  1469. query ITTT
  1470. SELECT f1, f2, f3, lead(f1, 1) OVER (PARTITION BY f2 ORDER BY f3 DESC, f1)
  1471. FROM t
  1472. ORDER BY f2 DESC, f3 DESC, f1, lead
  1473. ----
  1474. 7 d -5 NULL
  1475. 1 c NaN 2
  1476. 2 c NaN 3
  1477. 3 c 1 NULL
  1478. 4 b 1 4
  1479. 4 b 0 NULL
  1480. 1 a 1 2
  1481. 2 a 1 2
  1482. 2 a 1 3
  1483. 3 a 1 NULL
  1484. query ITTT
  1485. SELECT f1, f2, f3, lead(f1, 1, NULL) OVER (PARTITION BY f2 ORDER BY f3 DESC, f1)
  1486. FROM t
  1487. ORDER BY f2 DESC, f3 DESC, f1, lead
  1488. ----
  1489. 7 d -5 NULL
  1490. 1 c NaN 2
  1491. 2 c NaN 3
  1492. 3 c 1 NULL
  1493. 4 b 1 4
  1494. 4 b 0 NULL
  1495. 1 a 1 2
  1496. 2 a 1 2
  1497. 2 a 1 3
  1498. 3 a 1 NULL
  1499. # With default value
  1500. query ITTT
  1501. SELECT f1, f2, f3, lead(f1, 1, -1) OVER (PARTITION BY f2 ORDER BY f3 DESC, f1)
  1502. FROM t
  1503. ORDER BY f2 DESC, f3 DESC, f1, lead
  1504. ----
  1505. 7 d -5 -1
  1506. 1 c NaN 2
  1507. 2 c NaN 3
  1508. 3 c 1 -1
  1509. 4 b 1 4
  1510. 4 b 0 -1
  1511. 1 a 1 2
  1512. 2 a 1 2
  1513. 2 a 1 3
  1514. 3 a 1 -1
  1515. # Complex expressions
  1516. query ITTT
  1517. SELECT f1, f2, f3, lead(f1 + coalesce(nullif(f3, 'NaN'), -10)) OVER (PARTITION BY f2 ORDER BY f3 DESC, f1)
  1518. FROM t
  1519. ORDER BY f2 DESC, f3 DESC, f1, lead
  1520. ----
  1521. 7 d -5 NULL
  1522. 1 c NaN -8
  1523. 2 c NaN 4
  1524. 3 c 1 NULL
  1525. 4 b 1 4
  1526. 4 b 0 NULL
  1527. 1 a 1 3
  1528. 2 a 1 3
  1529. 2 a 1 4
  1530. 3 a 1 NULL
  1531. # Nulls in the first argument
  1532. query ITTT
  1533. SELECT f1, f2, f3, lead(NULL::int) OVER (PARTITION BY f2 ORDER BY f3 DESC, f1)
  1534. FROM t
  1535. ORDER BY f2 DESC, f3 DESC, f1, lead
  1536. ----
  1537. 7 d -5 NULL
  1538. 1 c NaN NULL
  1539. 2 c NaN NULL
  1540. 3 c 1 NULL
  1541. 4 b 1 NULL
  1542. 4 b 0 NULL
  1543. 1 a 1 NULL
  1544. 2 a 1 NULL
  1545. 2 a 1 NULL
  1546. 3 a 1 NULL
  1547. query ITTT
  1548. SELECT f1, f2, f3, lead(nullif(f1, 4)) OVER (PARTITION BY f2 ORDER BY f3 DESC, f1)
  1549. FROM t
  1550. ORDER BY f2 DESC, f3 DESC, f1, lead
  1551. ----
  1552. 7 d -5 NULL
  1553. 1 c NaN 2
  1554. 2 c NaN 3
  1555. 3 c 1 NULL
  1556. 4 b 1 NULL
  1557. 4 b 0 NULL
  1558. 1 a 1 2
  1559. 2 a 1 2
  1560. 2 a 1 3
  1561. 3 a 1 NULL
  1562. # Nulls in the first argument with a default value in the third argument
  1563. query ITTT
  1564. SELECT f1, f2, f3, lead(NULL::int, 0) OVER (PARTITION BY f2 ORDER BY f3 DESC, f1)
  1565. FROM t
  1566. ORDER BY f2 DESC, f3 DESC, f1, lead
  1567. ----
  1568. 7 d -5 NULL
  1569. 1 c NaN NULL
  1570. 2 c NaN NULL
  1571. 3 c 1 NULL
  1572. 4 b 1 NULL
  1573. 4 b 0 NULL
  1574. 1 a 1 NULL
  1575. 2 a 1 NULL
  1576. 2 a 1 NULL
  1577. 3 a 1 NULL
  1578. # Zero offset
  1579. query ITTT
  1580. SELECT f1, f2, f3, lead(f1, 0) OVER (PARTITION BY f2 ORDER BY f3 DESC, f1)
  1581. FROM t
  1582. ORDER BY f2 DESC, f3 DESC, f1, lead
  1583. ----
  1584. 7 d -5 7
  1585. 1 c NaN 1
  1586. 2 c NaN 2
  1587. 3 c 1 3
  1588. 4 b 1 4
  1589. 4 b 0 4
  1590. 1 a 1 1
  1591. 2 a 1 2
  1592. 2 a 1 2
  1593. 3 a 1 3
  1594. query ITTT
  1595. SELECT f1, f2, f3, lead(f1 + f3, 0) OVER (PARTITION BY f2 ORDER BY f3 DESC, f1)
  1596. FROM t
  1597. ORDER BY f2 DESC, f3 DESC, f1, lead
  1598. ----
  1599. 7 d -5 2
  1600. 1 c NaN NaN
  1601. 2 c NaN NaN
  1602. 3 c 1 4
  1603. 4 b 1 5
  1604. 4 b 0 4
  1605. 1 a 1 2
  1606. 2 a 1 3
  1607. 2 a 1 3
  1608. 3 a 1 4
  1609. # Positive offsets
  1610. query ITTT
  1611. SELECT f1, f2, f3, lead(f1, 2) OVER (PARTITION BY f2 ORDER BY f3 DESC, f1)
  1612. FROM t
  1613. ORDER BY f2 DESC, f3 DESC, f1, lead
  1614. ----
  1615. 7 d -5 NULL
  1616. 1 c NaN 3
  1617. 2 c NaN NULL
  1618. 3 c 1 NULL
  1619. 4 b 1 NULL
  1620. 4 b 0 NULL
  1621. 1 a 1 2
  1622. 2 a 1 3
  1623. 2 a 1 NULL
  1624. 3 a 1 NULL
  1625. query ITTT
  1626. SELECT f1, f2, f3, lead(f1 + f3, 2) OVER (PARTITION BY f2 ORDER BY f3 DESC, f1)
  1627. FROM t
  1628. ORDER BY f2 DESC, f3 DESC, f1, lead
  1629. ----
  1630. 7 d -5 NULL
  1631. 1 c NaN 4
  1632. 2 c NaN NULL
  1633. 3 c 1 NULL
  1634. 4 b 1 NULL
  1635. 4 b 0 NULL
  1636. 1 a 1 3
  1637. 2 a 1 4
  1638. 2 a 1 NULL
  1639. 3 a 1 NULL
  1640. # Out of range positive offsets
  1641. query ITTT
  1642. SELECT f1, f2, f3, lead(f1, 10) OVER (PARTITION BY f2 ORDER BY f3 DESC, f1)
  1643. FROM t
  1644. ORDER BY f2 DESC, f3 DESC, f1, lead
  1645. ----
  1646. 7 d -5 NULL
  1647. 1 c NaN NULL
  1648. 2 c NaN NULL
  1649. 3 c 1 NULL
  1650. 4 b 1 NULL
  1651. 4 b 0 NULL
  1652. 1 a 1 NULL
  1653. 2 a 1 NULL
  1654. 2 a 1 NULL
  1655. 3 a 1 NULL
  1656. query ITTT
  1657. SELECT f1, f2, f3, lead(f1 + f3, 10) OVER (PARTITION BY f2 ORDER BY f3 DESC, f1)
  1658. FROM t
  1659. ORDER BY f2 DESC, f3 DESC, f1, lead
  1660. ----
  1661. 7 d -5 NULL
  1662. 1 c NaN NULL
  1663. 2 c NaN NULL
  1664. 3 c 1 NULL
  1665. 4 b 1 NULL
  1666. 4 b 0 NULL
  1667. 1 a 1 NULL
  1668. 2 a 1 NULL
  1669. 2 a 1 NULL
  1670. 3 a 1 NULL
  1671. query ITTT
  1672. SELECT f1, f2, f3, lead(f1 + f3, 10, 0) OVER (PARTITION BY f2 ORDER BY f3 DESC, f1)
  1673. FROM t
  1674. ORDER BY f2 DESC, f3 DESC, f1, lead
  1675. ----
  1676. 7 d -5 0
  1677. 1 c NaN 0
  1678. 2 c NaN 0
  1679. 3 c 1 0
  1680. 4 b 1 0
  1681. 4 b 0 0
  1682. 1 a 1 0
  1683. 2 a 1 0
  1684. 2 a 1 0
  1685. 3 a 1 0
  1686. # Negative offsets
  1687. query ITTT
  1688. SELECT f1, f2, f3, lead(f1, -1) OVER (PARTITION BY f2 ORDER BY f3 DESC, f1)
  1689. FROM t
  1690. ORDER BY f2 DESC, f3 DESC, f1, lead
  1691. ----
  1692. 7 d -5 NULL
  1693. 1 c NaN NULL
  1694. 2 c NaN 1
  1695. 3 c 1 2
  1696. 4 b 1 NULL
  1697. 4 b 0 4
  1698. 1 a 1 NULL
  1699. 2 a 1 1
  1700. 2 a 1 2
  1701. 3 a 1 2
  1702. query ITTT
  1703. SELECT f1, f2, f3, lead(f1 + f3, -1) OVER (PARTITION BY f2 ORDER BY f3 DESC, f1)
  1704. FROM t
  1705. ORDER BY f2 DESC, f3 DESC, f1, lead
  1706. ----
  1707. 7 d -5 NULL
  1708. 1 c NaN NULL
  1709. 2 c NaN NaN
  1710. 3 c 1 NaN
  1711. 4 b 1 NULL
  1712. 4 b 0 5
  1713. 1 a 1 NULL
  1714. 2 a 1 2
  1715. 2 a 1 3
  1716. 3 a 1 3
  1717. query ITTT
  1718. SELECT f1, f2, f3, lead(f1 + f3, -2) OVER (PARTITION BY f2 ORDER BY f3 DESC, f1)
  1719. FROM t
  1720. ORDER BY f2 DESC, f3 DESC, f1, lead
  1721. ----
  1722. 7 d -5 NULL
  1723. 1 c NaN NULL
  1724. 2 c NaN NULL
  1725. 3 c 1 NaN
  1726. 4 b 1 NULL
  1727. 4 b 0 NULL
  1728. 1 a 1 NULL
  1729. 2 a 1 2
  1730. 2 a 1 NULL
  1731. 3 a 1 3
  1732. # Out of range negative offsets
  1733. query ITTT
  1734. SELECT f1, f2, f3, lead(f1, -10) OVER (PARTITION BY f2 ORDER BY f3 DESC, f1)
  1735. FROM t
  1736. ORDER BY f2 DESC, f3 DESC, f1, lead
  1737. ----
  1738. 7 d -5 NULL
  1739. 1 c NaN NULL
  1740. 2 c NaN NULL
  1741. 3 c 1 NULL
  1742. 4 b 1 NULL
  1743. 4 b 0 NULL
  1744. 1 a 1 NULL
  1745. 2 a 1 NULL
  1746. 2 a 1 NULL
  1747. 3 a 1 NULL
  1748. query ITTT
  1749. SELECT f1, f2, f3, lead(f1 + f3, -10) OVER (PARTITION BY f2 ORDER BY f3 DESC, f1)
  1750. FROM t
  1751. ORDER BY f2 DESC, f3 DESC, f1, lead
  1752. ----
  1753. 7 d -5 NULL
  1754. 1 c NaN NULL
  1755. 2 c NaN NULL
  1756. 3 c 1 NULL
  1757. 4 b 1 NULL
  1758. 4 b 0 NULL
  1759. 1 a 1 NULL
  1760. 2 a 1 NULL
  1761. 2 a 1 NULL
  1762. 3 a 1 NULL
  1763. query ITTT
  1764. SELECT f1, f2, f3, lead(f1, -10, 0) OVER (PARTITION BY f2 ORDER BY f3 DESC, f1)
  1765. FROM t
  1766. ORDER BY f2 DESC, f3 DESC, f1, lead
  1767. ----
  1768. 7 d -5 0
  1769. 1 c NaN 0
  1770. 2 c NaN 0
  1771. 3 c 1 0
  1772. 4 b 1 0
  1773. 4 b 0 0
  1774. 1 a 1 0
  1775. 2 a 1 0
  1776. 2 a 1 0
  1777. 3 a 1 0
  1778. # Variable per row offsets
  1779. query ITTT
  1780. SELECT f1, f2, f3, lead(f1, f1) OVER (PARTITION BY f2 ORDER BY f3 DESC, f1)
  1781. FROM t
  1782. ORDER BY f2 DESC, f3 DESC, f1, lead
  1783. ----
  1784. 7 d -5 NULL
  1785. 1 c NaN 2
  1786. 2 c NaN NULL
  1787. 3 c 1 NULL
  1788. 4 b 1 NULL
  1789. 4 b 0 NULL
  1790. 1 a 1 2
  1791. 2 a 1 3
  1792. 2 a 1 NULL
  1793. 3 a 1 NULL
  1794. query ITTT
  1795. SELECT f1, f2, f3, lead(f1, f1 - 1) OVER (PARTITION BY f2 ORDER BY f3 DESC, f1)
  1796. FROM t
  1797. ORDER BY f2 DESC, f3 DESC, f1, lead
  1798. ----
  1799. 7 d -5 NULL
  1800. 1 c NaN 1
  1801. 2 c NaN 3
  1802. 3 c 1 NULL
  1803. 4 b 1 NULL
  1804. 4 b 0 NULL
  1805. 1 a 1 1
  1806. 2 a 1 2
  1807. 2 a 1 3
  1808. 3 a 1 NULL
  1809. # Null offsets
  1810. query ITTT
  1811. SELECT f1, f2, f3, lead(f1, NULL) OVER (PARTITION BY f2 ORDER BY f3 DESC, f1)
  1812. FROM t
  1813. ORDER BY f2 DESC, f3 DESC, f1, lead
  1814. ----
  1815. 7 d -5 NULL
  1816. 1 c NaN NULL
  1817. 2 c NaN NULL
  1818. 3 c 1 NULL
  1819. 4 b 1 NULL
  1820. 4 b 0 NULL
  1821. 1 a 1 NULL
  1822. 2 a 1 NULL
  1823. 2 a 1 NULL
  1824. 3 a 1 NULL
  1825. query ITTT
  1826. SELECT f1, f2, f3, lead(f1, nullif(f1, 1)) OVER (PARTITION BY f2 ORDER BY f3 DESC, f1)
  1827. FROM t
  1828. ORDER BY f2 DESC, f3 DESC, f1, lead
  1829. ----
  1830. 7 d -5 NULL
  1831. 1 c NaN NULL
  1832. 2 c NaN NULL
  1833. 3 c 1 NULL
  1834. 4 b 1 NULL
  1835. 4 b 0 NULL
  1836. 1 a 1 NULL
  1837. 2 a 1 3
  1838. 2 a 1 NULL
  1839. 3 a 1 NULL
  1840. # Null offset with default value
  1841. query ITTT
  1842. SELECT f1, f2, f3, lead(f1, NULL, -10) OVER (PARTITION BY f2 ORDER BY f3 DESC, f1)
  1843. FROM t
  1844. ORDER BY f2 DESC, f3 DESC, f1, lead
  1845. ----
  1846. 7 d -5 NULL
  1847. 1 c NaN NULL
  1848. 2 c NaN NULL
  1849. 3 c 1 NULL
  1850. 4 b 1 NULL
  1851. 4 b 0 NULL
  1852. 1 a 1 NULL
  1853. 2 a 1 NULL
  1854. 2 a 1 NULL
  1855. 3 a 1 NULL
  1856. # Variable per row default value
  1857. query ITTT
  1858. SELECT f1, f2, f3, lead(f1, 1, f3) OVER (PARTITION BY f2 ORDER BY f3 DESC, f1)
  1859. FROM t
  1860. ORDER BY f2 DESC, f3 DESC, f1, lead
  1861. ----
  1862. 7 d -5 -5
  1863. 1 c NaN 2
  1864. 2 c NaN 3
  1865. 3 c 1 1
  1866. 4 b 1 4
  1867. 4 b 0 0
  1868. 1 a 1 2
  1869. 2 a 1 2
  1870. 2 a 1 3
  1871. 3 a 1 1
  1872. statement ok
  1873. DROP TABLE t;
  1874. statement ok
  1875. CREATE TABLE t(f1 int, f2 int);
  1876. statement ok
  1877. INSERT INTO t VALUES (1, 2), (1, 2), (3, 4);
  1878. # reduce_elision code path
  1879. # Default offset
  1880. query II
  1881. SELECT f1, lead(f1) OVER (PARTITION BY f1, f2)
  1882. FROM (SELECT DISTINCT f1, f2 FROM t) q
  1883. ORDER BY 1, 2
  1884. ----
  1885. 1 NULL
  1886. 3 NULL
  1887. query II
  1888. SELECT f1, lead(f1, 1) OVER (PARTITION BY f1, f2)
  1889. FROM (SELECT DISTINCT f1, f2 FROM t) q
  1890. ORDER BY 1, 2
  1891. ----
  1892. 1 NULL
  1893. 3 NULL
  1894. # Zero offset
  1895. query II
  1896. SELECT f1, lead(f1, 0) OVER (PARTITION BY f1, f2)
  1897. FROM (SELECT DISTINCT f1, f2 FROM t) q
  1898. ORDER BY 1, 2
  1899. ----
  1900. 1 1
  1901. 3 3
  1902. # Negative offset
  1903. query II
  1904. SELECT f1, lead(f1, -1) OVER (PARTITION BY f1, f2)
  1905. FROM (SELECT DISTINCT f1, f2 FROM t) q
  1906. ORDER BY 1, 2
  1907. ----
  1908. 1 NULL
  1909. 3 NULL
  1910. # Default value with offset 1
  1911. query II
  1912. SELECT f1, lead(f1, 1, 10) OVER (PARTITION BY f1, f2)
  1913. FROM (SELECT DISTINCT f1, f2 FROM t) q
  1914. ORDER BY 1, 2
  1915. ----
  1916. 1 10
  1917. 3 10
  1918. # Default value with offset 0
  1919. query II
  1920. SELECT f1, lead(f1, 0, 10) OVER (PARTITION BY f1, f2)
  1921. FROM (SELECT DISTINCT f1, f2 FROM t) q
  1922. ORDER BY 1, 2
  1923. ----
  1924. 1 1
  1925. 3 3
  1926. # Complex expression
  1927. query II
  1928. SELECT f1, lead(f1 * f2, 0, 10) OVER (PARTITION BY f1, f2)
  1929. FROM (SELECT DISTINCT f1, f2 FROM t) q
  1930. ORDER BY 1, 2
  1931. ----
  1932. 1 2
  1933. 3 12
  1934. query II
  1935. SELECT f1, lead(f1 * f2, 1, f1 * f2 + 1) OVER (PARTITION BY f1, f2)
  1936. FROM (SELECT DISTINCT f1, f2 FROM t) q
  1937. ORDER BY 1, 2
  1938. ----
  1939. 1 3
  1940. 3 13
  1941. # Complex offset
  1942. query II
  1943. SELECT f1, lead(f1 * f2, f1 - f1) OVER (PARTITION BY f1, f2)
  1944. FROM (SELECT DISTINCT f1, f2 FROM t) q
  1945. ORDER BY 1, 2
  1946. ----
  1947. 1 2
  1948. 3 12
  1949. query II
  1950. SELECT f1, lead(f1 * f2, f1 - 1) OVER (PARTITION BY f1, f2)
  1951. FROM (SELECT DISTINCT f1, f2 FROM t) q
  1952. ORDER BY 1, 2
  1953. ----
  1954. 1 2
  1955. 3 NULL
  1956. query II
  1957. SELECT f1, lead(f1 * f2, f2 - 2 * f1, f2) OVER (PARTITION BY f1, f2)
  1958. FROM (SELECT DISTINCT f1, f2 FROM t) q
  1959. ORDER BY 1, 2
  1960. ----
  1961. 1 2
  1962. 3 4
  1963. # Complex default value
  1964. query II
  1965. SELECT f1, lead(f1, 0, f1 * f2) OVER (PARTITION BY f1, f2)
  1966. FROM (SELECT DISTINCT f1, f2 FROM t) q
  1967. ORDER BY 1, 2
  1968. ----
  1969. 1 1
  1970. 3 3
  1971. query II
  1972. SELECT f1, lead(f1, 1, f1 * f2) OVER (PARTITION BY f1, f2)
  1973. FROM (SELECT DISTINCT f1, f2 FROM t) q
  1974. ORDER BY 1, 2
  1975. ----
  1976. 1 2
  1977. 3 12
  1978. # Null value in input relation
  1979. # This was caused by a bug in the reduce elision logic
  1980. statement ok
  1981. CREATE TABLE t4 (f1 INTEGER)
  1982. ----
  1983. statement ok
  1984. INSERT INTO t4 VALUES (NULL)
  1985. ----
  1986. query II
  1987. SELECT f1, lead(0, f1 , 0) OVER (PARTITION BY f1 ORDER BY f1) FROM t4 GROUP BY f1 ORDER BY 1
  1988. ----
  1989. NULL NULL
  1990. ## lag/lead ignore nulls
  1991. statement ok
  1992. create table t6(x int, y int);
  1993. statement ok
  1994. insert into t6 values (1,2), (3,null), (5,6), (7,8), (9, null), (11, null), (13, 14), (15, 16), (17, 18);
  1995. query IIIIIIIIIIIIIIIIIIII
  1996. select
  1997. x,
  1998. y,
  1999. lag(y) over (order by x) as lag1,
  2000. lag(y) respect nulls over (order by x) as lag1_resp,
  2001. lag(y) ignore nulls over (order by x) as lag1_ign,
  2002. lead(y) over (order by x) as lead1,
  2003. lead(y) ignore nulls over (order by x) as lead1_ign,
  2004. lag(y, 2) over (order by x) as lag2,
  2005. lag(y, 2) ignore nulls over (order by x) as lag2_ign,
  2006. lead(y, 2) over (order by x) as lead2,
  2007. lead(y, 2) ignore nulls over (order by x) as lead2_ign,
  2008. lag(y, 2, -1) ignore nulls over (order by x) as lag2_ign_def,
  2009. lead(y, 2, -1) ignore nulls over (order by x) as lead2_ign_def,
  2010. lag(y, 2, 16) ignore nulls over (order by x) as lag2_ign_def16,
  2011. lead(y, 2, 16) ignore nulls over (order by x) as lead2_ign_def16,
  2012. lag(y, -1, 100) ignore nulls over (order by x) as lag_neg_offset,
  2013. lead(y, -2, 100) ignore nulls over (order by x) as lead_neg_offset,
  2014. lag(y, y/5+1) ignore nulls over (order by x) as lag_dynamic_offset,
  2015. lead(y, y/5+1) ignore nulls over (order by x) as lead_dynamic_offset,
  2016. lag(y, null) ignore nulls over (order by x) as lag_literal_null_offset
  2017. from t6
  2018. order by x;
  2019. ----
  2020. 1 2 NULL NULL NULL NULL 6 NULL NULL 6 8 -1 8 16 8 6 100 NULL 6 NULL
  2021. 3 NULL 2 2 2 6 6 NULL NULL 8 8 -1 8 16 8 6 100 NULL NULL NULL
  2022. 5 6 NULL NULL 2 8 8 2 NULL NULL 14 -1 14 16 14 8 100 NULL 14 NULL
  2023. 7 8 6 6 6 NULL 14 NULL 2 NULL 16 2 16 2 16 14 2 2 16 NULL
  2024. 9 NULL 8 8 8 NULL 14 6 6 14 16 6 16 6 16 14 6 NULL NULL NULL
  2025. 11 NULL NULL NULL 8 14 14 8 6 16 16 6 16 6 16 14 6 NULL NULL NULL
  2026. 13 14 NULL NULL 8 16 16 NULL 6 18 18 6 18 6 18 16 6 2 NULL NULL
  2027. 15 16 14 14 14 18 18 NULL 8 NULL NULL 8 -1 8 16 18 8 2 NULL NULL
  2028. 17 18 16 16 16 NULL NULL 14 14 NULL NULL 14 -1 14 16 100 14 6 NULL NULL
  2029. # Same as above, but with a `partition by`
  2030. query IIIIIIIIIIIIIIIIIIII
  2031. select
  2032. x,
  2033. y,
  2034. lag(y) over (partition by x%4 order by x) as lag1,
  2035. lag(y) respect nulls over (partition by x%4 order by x) as lag1_resp,
  2036. lag(y) ignore nulls over (partition by x%4 order by x) as lag1_ign,
  2037. lead(y) over (partition by x%4 order by x) as lead1,
  2038. lead(y) ignore nulls over (partition by x%4 order by x) as lead1_ign,
  2039. lag(y, 2) over (partition by x%4 order by x) as lag2,
  2040. lag(y, 2) ignore nulls over (partition by x%4 order by x) as lag2_ign,
  2041. lead(y, 2) over (partition by x%4 order by x) as lead2,
  2042. lead(y, 2) ignore nulls over (partition by x%4 order by x) as lead2_ign,
  2043. lag(y, 2, -1) ignore nulls over (partition by x%4 order by x) as lag2_ign_def,
  2044. lead(y, 2, -1) ignore nulls over (partition by x%4 order by x) as lead2_ign_def,
  2045. lag(y, 2, 16) ignore nulls over (partition by x%4 order by x) as lag2_ign_def16,
  2046. lead(y, 2, 16) ignore nulls over (partition by x%4 order by x) as lead2_ign_def16,
  2047. lag(y, -1, 100) ignore nulls over (partition by x%4 order by x) as lag_neg_offset,
  2048. lead(y, -2, 100) ignore nulls over (partition by x%4 order by x) as lead_neg_offset,
  2049. lag(y, y/5+1) ignore nulls over (partition by x%4 order by x) as lag_dynamic_offset,
  2050. lead(y, y/5+1) ignore nulls over (partition by x%4 order by x) as lead_dynamic_offset,
  2051. lag(y, null) ignore nulls over (partition by x%4 order by x) as lag_literal_null_offset
  2052. from t6
  2053. order by x%4, x;
  2054. ----
  2055. 1 2 NULL NULL NULL 6 6 NULL NULL NULL 14 -1 14 16 14 6 100 NULL 6 NULL
  2056. 5 6 2 2 2 NULL 14 NULL NULL 14 18 -1 18 16 18 14 100 NULL 18 NULL
  2057. 9 NULL 6 6 6 14 14 2 2 18 18 2 18 2 18 14 2 NULL NULL NULL
  2058. 13 14 NULL NULL 6 18 18 6 2 NULL NULL 2 -1 2 16 18 2 NULL NULL NULL
  2059. 17 18 14 14 14 NULL NULL NULL 6 NULL NULL 6 -1 6 16 100 6 NULL NULL NULL
  2060. 3 NULL NULL NULL NULL 8 8 NULL NULL NULL 16 -1 16 16 16 8 100 NULL NULL NULL
  2061. 7 8 NULL NULL NULL NULL 16 NULL NULL 16 NULL -1 -1 16 16 16 100 NULL NULL NULL
  2062. 11 NULL 8 8 8 16 16 NULL NULL NULL NULL -1 -1 16 16 16 100 NULL NULL NULL
  2063. 15 16 NULL NULL 8 NULL NULL 8 NULL NULL NULL -1 -1 16 16 100 100 NULL NULL NULL
  2064. statement ok
  2065. CREATE VIEW t6_more_nulls AS
  2066. SELECT
  2067. x,
  2068. CASE WHEN y < 15 AND y > 7 THEN null ELSE y END AS y
  2069. FROM t6;
  2070. query IIIIIIII
  2071. select
  2072. x,
  2073. y,
  2074. lag(y) ignore nulls over (order by x) as lag1,
  2075. lag(y,2) ignore nulls over (order by x) as lag2,
  2076. lag(y,3) ignore nulls over (order by x) as lag3,
  2077. lead(y) ignore nulls over (order by x) as lead1,
  2078. lead(y,2) ignore nulls over (order by x) as lead2,
  2079. lead(y,3) ignore nulls over (order by x) as lead3
  2080. from t6_more_nulls
  2081. order by x;
  2082. ----
  2083. 1 2 NULL NULL NULL 6 16 18
  2084. 3 NULL 2 NULL NULL 6 16 18
  2085. 5 6 2 NULL NULL 16 18 NULL
  2086. 7 NULL 6 2 NULL 16 18 NULL
  2087. 9 NULL 6 2 NULL 16 18 NULL
  2088. 11 NULL 6 2 NULL 16 18 NULL
  2089. 13 NULL 6 2 NULL 16 18 NULL
  2090. 15 16 6 2 NULL 18 NULL NULL
  2091. 17 18 16 6 2 NULL NULL NULL
  2092. # Test the `offset = 0` code path. This currently doesn't work with nulls if IGNORE NULLS is specified,
  2093. # so we just filter out nulls for now.
  2094. # See https://materializeinc.slack.com/archives/C063H5S7NKE/p1724962369706729
  2095. # Also tests negative dynamic offsets.
  2096. statement ok
  2097. CREATE VIEW t6_no_nulls AS
  2098. SELECT
  2099. x,
  2100. COALESCE(y,-11) AS y
  2101. FROM t6;
  2102. query IIIIIII
  2103. SELECT
  2104. x,
  2105. y,
  2106. y/10,
  2107. lag(y, y/10) OVER (ORDER BY x),
  2108. lag(y, y/10) IGNORE NULLS OVER (ORDER BY x),
  2109. lead(y, y/10) OVER (ORDER BY x),
  2110. lead(y, y/10) IGNORE NULLS OVER (ORDER BY x)
  2111. FROM t6_no_nulls
  2112. ORDER BY x;
  2113. ----
  2114. 1 2 0 2 2 2 2
  2115. 3 -11 -1 6 6 2 2
  2116. 5 6 0 6 6 6 6
  2117. 7 8 0 8 8 8 8
  2118. 9 -11 -1 -11 -11 8 8
  2119. 11 -11 -1 14 14 -11 -11
  2120. 13 14 1 -11 -11 16 16
  2121. 15 16 1 14 14 18 18
  2122. 17 18 1 16 16 NULL NULL
  2123. query error db error: ERROR: IGNORE NULLS and RESPECT NULLS options for functions other than LAG and LEAD not yet supported
  2124. select first_value(x) ignore nulls over() from t6;
  2125. query error db error: ERROR: IGNORE NULLS and RESPECT NULLS options for functions other than LAG and LEAD not yet supported
  2126. select row_number() ignore nulls over();
  2127. query error db error: ERROR: Both IGNORE NULLS and RESPECT NULLS were given\.
  2128. select row_number() ignore nulls respect nulls over();
  2129. ## window frames
  2130. # Invalid frame start
  2131. query error frame start cannot be UNBOUNDED FOLLOWING
  2132. SELECT row_number() OVER (ROWS UNBOUNDED FOLLOWING)
  2133. # Invalid frame end
  2134. query error frame end cannot be UNBOUNDED PRECEDING
  2135. SELECT row_number() OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED PRECEDING)
  2136. # End frame can't be of a type that comes before the start bound
  2137. query error frame starting from current row cannot have preceding rows
  2138. SELECT row_number() OVER (ROWS BETWEEN CURRENT ROW AND 1 PRECEDING)
  2139. query error frame starting from following row cannot have preceding rows
  2140. SELECT row_number() OVER (ROWS BETWEEN 1 FOLLOWING AND 1 PRECEDING)
  2141. query error frame starting from following row cannot have preceding rows
  2142. SELECT row_number() OVER (ROWS BETWEEN 1 FOLLOWING AND CURRENT ROW)
  2143. # But end offsets can come before start offsets
  2144. query I
  2145. SELECT row_number() OVER (ROWS BETWEEN 1 PRECEDING AND 2 PRECEDING)
  2146. ----
  2147. 1
  2148. query I
  2149. SELECT row_number() OVER (ROWS BETWEEN 2 FOLLOWING AND 1 FOLLOWING)
  2150. ----
  2151. 1
  2152. # Negative offsets are not allowed
  2153. # Our error message is different from Postgres, so it's not checked here
  2154. query error
  2155. SELECT row_number() OVER (ROWS -1 PRECEDING)
  2156. query error
  2157. SELECT row_number() OVER (ROWS -1 FOLLOWING)
  2158. # Current implementation restrictions
  2159. # RANGE is not supported outside of the default frame
  2160. query error RANGE in non-default window frames not yet supported
  2161. SELECT row_number() OVER (RANGE BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING)
  2162. query error RANGE in non-default window frames not yet supported
  2163. SELECT row_number() OVER (RANGE BETWEEN UNBOUNDED PRECEDING AND 1 FOLLOWING)
  2164. query error RANGE in non-default window frames not yet supported
  2165. SELECT row_number() OVER (RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)
  2166. query error RANGE in non-default window frames not yet supported
  2167. SELECT row_number() OVER (RANGE BETWEEN 1 PRECEDING AND 1 PRECEDING)
  2168. query error RANGE in non-default window frames not yet supported
  2169. SELECT row_number() OVER (RANGE BETWEEN 1 PRECEDING AND CURRENT ROW)
  2170. query error RANGE in non-default window frames not yet supported
  2171. SELECT row_number() OVER (RANGE BETWEEN 1 PRECEDING AND 1 FOLLOWING)
  2172. query error RANGE in non-default window frames not yet supported
  2173. SELECT row_number() OVER (RANGE BETWEEN 1 PRECEDING AND UNBOUNDED FOLLOWING)
  2174. query error RANGE in non-default window frames not yet supported
  2175. SELECT row_number() OVER (RANGE BETWEEN CURRENT ROW AND CURRENT ROW)
  2176. query error RANGE in non-default window frames not yet supported
  2177. SELECT row_number() OVER (RANGE BETWEEN CURRENT ROW AND 1 FOLLOWING)
  2178. query error RANGE in non-default window frames not yet supported
  2179. SELECT row_number() OVER (RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
  2180. query error RANGE in non-default window frames not yet supported
  2181. SELECT row_number() OVER (RANGE BETWEEN 1 FOLLOWING AND 1 FOLLOWING)
  2182. query error RANGE in non-default window frames not yet supported
  2183. SELECT row_number() OVER (RANGE BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING)
  2184. # Default window frame works fine
  2185. query I
  2186. SELECT row_number() OVER ()
  2187. ----
  2188. 1
  2189. query I
  2190. SELECT row_number() OVER (RANGE UNBOUNDED PRECEDING)
  2191. ----
  2192. 1
  2193. query I
  2194. SELECT row_number() OVER (RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
  2195. ----
  2196. 1
  2197. # GROUPS is not supported at all
  2198. query error GROUPS in window frames not yet supported
  2199. SELECT row_number() OVER (GROUPS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING)
  2200. query error GROUPS in window frames not yet supported
  2201. SELECT row_number() OVER (GROUPS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
  2202. query error GROUPS in window frames not yet supported
  2203. SELECT row_number() OVER (GROUPS BETWEEN UNBOUNDED PRECEDING AND 1 FOLLOWING)
  2204. query error GROUPS in window frames not yet supported
  2205. SELECT row_number() OVER (GROUPS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)
  2206. query error GROUPS in window frames not yet supported
  2207. SELECT row_number() OVER (GROUPS BETWEEN 1 PRECEDING AND 1 PRECEDING)
  2208. query error GROUPS in window frames not yet supported
  2209. SELECT row_number() OVER (GROUPS BETWEEN 1 PRECEDING AND CURRENT ROW)
  2210. query error GROUPS in window frames not yet supported
  2211. SELECT row_number() OVER (GROUPS BETWEEN 1 PRECEDING AND 1 FOLLOWING)
  2212. query error GROUPS in window frames not yet supported
  2213. SELECT row_number() OVER (GROUPS BETWEEN 1 PRECEDING AND UNBOUNDED FOLLOWING)
  2214. query error GROUPS in window frames not yet supported
  2215. SELECT row_number() OVER (GROUPS BETWEEN CURRENT ROW AND CURRENT ROW)
  2216. query error GROUPS in window frames not yet supported
  2217. SELECT row_number() OVER (GROUPS BETWEEN CURRENT ROW AND 1 FOLLOWING)
  2218. query error GROUPS in window frames not yet supported
  2219. SELECT row_number() OVER (GROUPS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
  2220. query error GROUPS in window frames not yet supported
  2221. SELECT row_number() OVER (GROUPS BETWEEN 1 FOLLOWING AND 1 FOLLOWING)
  2222. query error GROUPS in window frames not yet supported
  2223. SELECT row_number() OVER (GROUPS BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING)
  2224. ## first_value
  2225. statement ok
  2226. DROP TABLE t;
  2227. statement ok
  2228. CREATE TABLE t(f1 int, f2 string, f3 int);
  2229. statement ok
  2230. INSERT INTO t VALUES (1, 'a', 1), (2, 'a', 2), (2, 'a', 3), (3, 'a', 4), (4, 'b', 5), (4, 'b', 6), (1, 'c', 7), (2, 'c', 8), (3, 'c', 9), (7, 'd', 10);
  2231. # Default frame (RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
  2232. query ITII
  2233. SELECT f1, f2, f3, first_value(f1) OVER (PARTITION BY f2 ORDER BY f1, f3)
  2234. FROM t
  2235. ORDER BY f2, f3, f1, first_value
  2236. ----
  2237. 1 a 1 1
  2238. 2 a 2 1
  2239. 2 a 3 1
  2240. 3 a 4 1
  2241. 4 b 5 4
  2242. 4 b 6 4
  2243. 1 c 7 1
  2244. 2 c 8 1
  2245. 3 c 9 1
  2246. 7 d 10 7
  2247. # ROWS BETWEEN UNBOUNDED PRECEDING AND x PRECEDING
  2248. query ITII
  2249. SELECT f1, f2, f3, first_value(f1) OVER (PARTITION BY f2 ORDER BY f1, f3 ROWS BETWEEN UNBOUNDED PRECEDING AND 0 PRECEDING)
  2250. FROM t
  2251. ORDER BY f2, f3, f1, first_value
  2252. ----
  2253. 1 a 1 1
  2254. 2 a 2 1
  2255. 2 a 3 1
  2256. 3 a 4 1
  2257. 4 b 5 4
  2258. 4 b 6 4
  2259. 1 c 7 1
  2260. 2 c 8 1
  2261. 3 c 9 1
  2262. 7 d 10 7
  2263. query ITII
  2264. SELECT f1, f2, f3, first_value(f1) OVER (PARTITION BY f2 ORDER BY f1, f3 ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING)
  2265. FROM t
  2266. ORDER BY f2, f3, f1, first_value
  2267. ----
  2268. 1 a 1 NULL
  2269. 2 a 2 1
  2270. 2 a 3 1
  2271. 3 a 4 1
  2272. 4 b 5 NULL
  2273. 4 b 6 4
  2274. 1 c 7 NULL
  2275. 2 c 8 1
  2276. 3 c 9 1
  2277. 7 d 10 NULL
  2278. query ITII
  2279. SELECT f1, f2, f3, first_value(f1) OVER (PARTITION BY f2 ORDER BY f1, f3 ROWS BETWEEN UNBOUNDED PRECEDING AND 2 PRECEDING)
  2280. FROM t
  2281. ORDER BY f2, f3, f1, first_value
  2282. ----
  2283. 1 a 1 NULL
  2284. 2 a 2 NULL
  2285. 2 a 3 1
  2286. 3 a 4 1
  2287. 4 b 5 NULL
  2288. 4 b 6 NULL
  2289. 1 c 7 NULL
  2290. 2 c 8 NULL
  2291. 3 c 9 1
  2292. 7 d 10 NULL
  2293. query ITII
  2294. SELECT f1, f2, f3, first_value(f1) OVER (PARTITION BY f2 ORDER BY f1, f3 ROWS BETWEEN UNBOUNDED PRECEDING AND 10 PRECEDING)
  2295. FROM t
  2296. ORDER BY f2, f3, f1, first_value
  2297. ----
  2298. 1 a 1 NULL
  2299. 2 a 2 NULL
  2300. 2 a 3 NULL
  2301. 3 a 4 NULL
  2302. 4 b 5 NULL
  2303. 4 b 6 NULL
  2304. 1 c 7 NULL
  2305. 2 c 8 NULL
  2306. 3 c 9 NULL
  2307. 7 d 10 NULL
  2308. # ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
  2309. query ITII
  2310. SELECT f1, f2, f3, first_value(f1) OVER (PARTITION BY f2 ORDER BY f1, f3 ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
  2311. FROM t
  2312. ORDER BY f2, f3, f1, first_value
  2313. ----
  2314. 1 a 1 1
  2315. 2 a 2 1
  2316. 2 a 3 1
  2317. 3 a 4 1
  2318. 4 b 5 4
  2319. 4 b 6 4
  2320. 1 c 7 1
  2321. 2 c 8 1
  2322. 3 c 9 1
  2323. 7 d 10 7
  2324. # ROWS BETWEEN UNBOUNDED PRECEDING AND x FOLLOWING
  2325. query ITII
  2326. SELECT f1, f2, f3, first_value(f1) OVER (PARTITION BY f2 ORDER BY f1, f3 ROWS BETWEEN UNBOUNDED PRECEDING AND 0 FOLLOWING)
  2327. FROM t
  2328. ORDER BY f2, f3, f1, first_value
  2329. ----
  2330. 1 a 1 1
  2331. 2 a 2 1
  2332. 2 a 3 1
  2333. 3 a 4 1
  2334. 4 b 5 4
  2335. 4 b 6 4
  2336. 1 c 7 1
  2337. 2 c 8 1
  2338. 3 c 9 1
  2339. 7 d 10 7
  2340. query ITII
  2341. SELECT f1, f2, f3, first_value(f1) OVER (PARTITION BY f2 ORDER BY f1, f3 ROWS BETWEEN UNBOUNDED PRECEDING AND 1 FOLLOWING)
  2342. FROM t
  2343. ORDER BY f2, f3, f1, first_value
  2344. ----
  2345. 1 a 1 1
  2346. 2 a 2 1
  2347. 2 a 3 1
  2348. 3 a 4 1
  2349. 4 b 5 4
  2350. 4 b 6 4
  2351. 1 c 7 1
  2352. 2 c 8 1
  2353. 3 c 9 1
  2354. 7 d 10 7
  2355. query ITII
  2356. SELECT f1, f2, f3, first_value(f1) OVER (PARTITION BY f2 ORDER BY f1, f3 ROWS BETWEEN UNBOUNDED PRECEDING AND 2 FOLLOWING)
  2357. FROM t
  2358. ORDER BY f2, f3, f1, first_value
  2359. ----
  2360. 1 a 1 1
  2361. 2 a 2 1
  2362. 2 a 3 1
  2363. 3 a 4 1
  2364. 4 b 5 4
  2365. 4 b 6 4
  2366. 1 c 7 1
  2367. 2 c 8 1
  2368. 3 c 9 1
  2369. 7 d 10 7
  2370. query ITII
  2371. SELECT f1, f2, f3, first_value(f1) OVER (PARTITION BY f2 ORDER BY f1, f3 ROWS BETWEEN UNBOUNDED PRECEDING AND 10 FOLLOWING)
  2372. FROM t
  2373. ORDER BY f2, f3, f1, first_value
  2374. ----
  2375. 1 a 1 1
  2376. 2 a 2 1
  2377. 2 a 3 1
  2378. 3 a 4 1
  2379. 4 b 5 4
  2380. 4 b 6 4
  2381. 1 c 7 1
  2382. 2 c 8 1
  2383. 3 c 9 1
  2384. 7 d 10 7
  2385. # ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
  2386. query ITII
  2387. SELECT f1, f2, f3, first_value(f1) OVER (PARTITION BY f2 ORDER BY f1, f3 ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)
  2388. FROM t
  2389. ORDER BY f2, f3, f1, first_value
  2390. ----
  2391. 1 a 1 1
  2392. 2 a 2 1
  2393. 2 a 3 1
  2394. 3 a 4 1
  2395. 4 b 5 4
  2396. 4 b 6 4
  2397. 1 c 7 1
  2398. 2 c 8 1
  2399. 3 c 9 1
  2400. 7 d 10 7
  2401. # ROWS BETWEEN 0 PRECEDING AND x PRECEDING
  2402. query ITII
  2403. SELECT f1, f2, f3, first_value(f1) OVER (PARTITION BY f2 ORDER BY f1, f3 ROWS BETWEEN 0 PRECEDING AND 1 PRECEDING)
  2404. FROM t
  2405. ORDER BY f2, f3, f1, first_value
  2406. ----
  2407. 1 a 1 NULL
  2408. 2 a 2 NULL
  2409. 2 a 3 NULL
  2410. 3 a 4 NULL
  2411. 4 b 5 NULL
  2412. 4 b 6 NULL
  2413. 1 c 7 NULL
  2414. 2 c 8 NULL
  2415. 3 c 9 NULL
  2416. 7 d 10 NULL
  2417. query ITII
  2418. SELECT f1, f2, f3, first_value(f1) OVER (PARTITION BY f2 ORDER BY f1, f3 ROWS BETWEEN 0 PRECEDING AND 2 PRECEDING)
  2419. FROM t
  2420. ORDER BY f2, f3, f1, first_value
  2421. ----
  2422. 1 a 1 NULL
  2423. 2 a 2 NULL
  2424. 2 a 3 NULL
  2425. 3 a 4 NULL
  2426. 4 b 5 NULL
  2427. 4 b 6 NULL
  2428. 1 c 7 NULL
  2429. 2 c 8 NULL
  2430. 3 c 9 NULL
  2431. 7 d 10 NULL
  2432. query ITII
  2433. SELECT f1, f2, f3, first_value(f1) OVER (PARTITION BY f2 ORDER BY f1, f3 ROWS BETWEEN 0 PRECEDING AND 10 PRECEDING)
  2434. FROM t
  2435. ORDER BY f2, f3, f1, first_value
  2436. ----
  2437. 1 a 1 NULL
  2438. 2 a 2 NULL
  2439. 2 a 3 NULL
  2440. 3 a 4 NULL
  2441. 4 b 5 NULL
  2442. 4 b 6 NULL
  2443. 1 c 7 NULL
  2444. 2 c 8 NULL
  2445. 3 c 9 NULL
  2446. 7 d 10 NULL
  2447. # ROWS BETWEEN x PRECEDING AND 0 PRECEDING
  2448. query ITII
  2449. SELECT f1, f2, f3, first_value(f1) OVER (PARTITION BY f2 ORDER BY f1, f3 ROWS BETWEEN 1 PRECEDING AND 0 PRECEDING)
  2450. FROM t
  2451. ORDER BY f2, f3, f1, first_value
  2452. ----
  2453. 1 a 1 1
  2454. 2 a 2 1
  2455. 2 a 3 2
  2456. 3 a 4 2
  2457. 4 b 5 4
  2458. 4 b 6 4
  2459. 1 c 7 1
  2460. 2 c 8 1
  2461. 3 c 9 2
  2462. 7 d 10 7
  2463. query ITII
  2464. SELECT f1, f2, f3, first_value(f1) OVER (PARTITION BY f2 ORDER BY f1, f3 ROWS BETWEEN 2 PRECEDING AND 0 PRECEDING)
  2465. FROM t
  2466. ORDER BY f2, f3, f1, first_value
  2467. ----
  2468. 1 a 1 1
  2469. 2 a 2 1
  2470. 2 a 3 1
  2471. 3 a 4 2
  2472. 4 b 5 4
  2473. 4 b 6 4
  2474. 1 c 7 1
  2475. 2 c 8 1
  2476. 3 c 9 1
  2477. 7 d 10 7
  2478. query ITII
  2479. SELECT f1, f2, f3, first_value(f1) OVER (PARTITION BY f2 ORDER BY f1, f3 ROWS BETWEEN 10 PRECEDING AND 0 PRECEDING)
  2480. FROM t
  2481. ORDER BY f2, f3, f1, first_value
  2482. ----
  2483. 1 a 1 1
  2484. 2 a 2 1
  2485. 2 a 3 1
  2486. 3 a 4 1
  2487. 4 b 5 4
  2488. 4 b 6 4
  2489. 1 c 7 1
  2490. 2 c 8 1
  2491. 3 c 9 1
  2492. 7 d 10 7
  2493. # ROWS BETWEEN x PRECEDING AND y PRECEDING, where x < y
  2494. query ITII
  2495. SELECT f1, f2, f3, first_value(f1) OVER (PARTITION BY f2 ORDER BY f1, f3 ROWS BETWEEN 0 PRECEDING AND 1 PRECEDING)
  2496. FROM t
  2497. ORDER BY f2, f3, f1, first_value
  2498. ----
  2499. 1 a 1 NULL
  2500. 2 a 2 NULL
  2501. 2 a 3 NULL
  2502. 3 a 4 NULL
  2503. 4 b 5 NULL
  2504. 4 b 6 NULL
  2505. 1 c 7 NULL
  2506. 2 c 8 NULL
  2507. 3 c 9 NULL
  2508. 7 d 10 NULL
  2509. query ITII
  2510. SELECT f1, f2, f3, first_value(f1) OVER (PARTITION BY f2 ORDER BY f1, f3 ROWS BETWEEN 1 PRECEDING AND 2 PRECEDING)
  2511. FROM t
  2512. ORDER BY f2, f3, f1, first_value
  2513. ----
  2514. 1 a 1 NULL
  2515. 2 a 2 NULL
  2516. 2 a 3 NULL
  2517. 3 a 4 NULL
  2518. 4 b 5 NULL
  2519. 4 b 6 NULL
  2520. 1 c 7 NULL
  2521. 2 c 8 NULL
  2522. 3 c 9 NULL
  2523. 7 d 10 NULL
  2524. # ROWS BETWEEN x PRECEDING AND y PRECEDING, where x > y
  2525. query ITII
  2526. SELECT f1, f2, f3, first_value(f1) OVER (PARTITION BY f2 ORDER BY f1, f3 ROWS BETWEEN 1 PRECEDING AND 0 PRECEDING)
  2527. FROM t
  2528. ORDER BY f2, f3, f1, first_value
  2529. ----
  2530. 1 a 1 1
  2531. 2 a 2 1
  2532. 2 a 3 2
  2533. 3 a 4 2
  2534. 4 b 5 4
  2535. 4 b 6 4
  2536. 1 c 7 1
  2537. 2 c 8 1
  2538. 3 c 9 2
  2539. 7 d 10 7
  2540. query ITII
  2541. SELECT f1, f2, f3, first_value(f1) OVER (PARTITION BY f2 ORDER BY f1, f3 ROWS BETWEEN 2 PRECEDING AND 1 PRECEDING)
  2542. FROM t
  2543. ORDER BY f2, f3, f1, first_value
  2544. ----
  2545. 1 a 1 NULL
  2546. 2 a 2 1
  2547. 2 a 3 1
  2548. 3 a 4 2
  2549. 4 b 5 NULL
  2550. 4 b 6 4
  2551. 1 c 7 NULL
  2552. 2 c 8 1
  2553. 3 c 9 1
  2554. 7 d 10 NULL
  2555. # ROWS BETWEEN x PRECEDING AND y PRECEDING, where x == y
  2556. query ITII
  2557. SELECT f1, f2, f3, first_value(f1) OVER (PARTITION BY f2 ORDER BY f1, f3 ROWS BETWEEN 0 PRECEDING AND 0 PRECEDING)
  2558. FROM t
  2559. ORDER BY f2, f3, f1, first_value
  2560. ----
  2561. 1 a 1 1
  2562. 2 a 2 2
  2563. 2 a 3 2
  2564. 3 a 4 3
  2565. 4 b 5 4
  2566. 4 b 6 4
  2567. 1 c 7 1
  2568. 2 c 8 2
  2569. 3 c 9 3
  2570. 7 d 10 7
  2571. query ITII
  2572. SELECT f1, f2, f3, first_value(f1) OVER (PARTITION BY f2 ORDER BY f1, f3 ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING)
  2573. FROM t
  2574. ORDER BY f2, f3, f1, first_value
  2575. ----
  2576. 1 a 1 NULL
  2577. 2 a 2 1
  2578. 2 a 3 2
  2579. 3 a 4 2
  2580. 4 b 5 NULL
  2581. 4 b 6 4
  2582. 1 c 7 NULL
  2583. 2 c 8 1
  2584. 3 c 9 2
  2585. 7 d 10 NULL
  2586. query ITII
  2587. SELECT f1, f2, f3, first_value(f1) OVER (PARTITION BY f2 ORDER BY f1, f3 ROWS BETWEEN 2 PRECEDING AND 2 PRECEDING)
  2588. FROM t
  2589. ORDER BY f2, f3, f1, first_value
  2590. ----
  2591. 1 a 1 NULL
  2592. 2 a 2 NULL
  2593. 2 a 3 1
  2594. 3 a 4 2
  2595. 4 b 5 NULL
  2596. 4 b 6 NULL
  2597. 1 c 7 NULL
  2598. 2 c 8 NULL
  2599. 3 c 9 1
  2600. 7 d 10 NULL
  2601. query ITII
  2602. SELECT f1, f2, f3, first_value(f1) OVER (PARTITION BY f2 ORDER BY f1, f3 ROWS BETWEEN 10 PRECEDING AND 10 PRECEDING)
  2603. FROM t
  2604. ORDER BY f2, f3, f1, first_value
  2605. ----
  2606. 1 a 1 NULL
  2607. 2 a 2 NULL
  2608. 2 a 3 NULL
  2609. 3 a 4 NULL
  2610. 4 b 5 NULL
  2611. 4 b 6 NULL
  2612. 1 c 7 NULL
  2613. 2 c 8 NULL
  2614. 3 c 9 NULL
  2615. 7 d 10 NULL
  2616. # ROWS BETWEEN x PRECEDING AND CURRENT ROW
  2617. query ITII
  2618. SELECT f1, f2, f3, first_value(f1) OVER (PARTITION BY f2 ORDER BY f1, f3 ROWS BETWEEN 0 PRECEDING AND CURRENT ROW)
  2619. FROM t
  2620. ORDER BY f2, f3, f1, first_value
  2621. ----
  2622. 1 a 1 1
  2623. 2 a 2 2
  2624. 2 a 3 2
  2625. 3 a 4 3
  2626. 4 b 5 4
  2627. 4 b 6 4
  2628. 1 c 7 1
  2629. 2 c 8 2
  2630. 3 c 9 3
  2631. 7 d 10 7
  2632. query ITII
  2633. SELECT f1, f2, f3, first_value(f1) OVER (PARTITION BY f2 ORDER BY f1, f3 ROWS BETWEEN 1 PRECEDING AND CURRENT ROW)
  2634. FROM t
  2635. ORDER BY f2, f3, f1, first_value
  2636. ----
  2637. 1 a 1 1
  2638. 2 a 2 1
  2639. 2 a 3 2
  2640. 3 a 4 2
  2641. 4 b 5 4
  2642. 4 b 6 4
  2643. 1 c 7 1
  2644. 2 c 8 1
  2645. 3 c 9 2
  2646. 7 d 10 7
  2647. query ITII
  2648. SELECT f1, f2, f3, first_value(f1) OVER (PARTITION BY f2 ORDER BY f1, f3 ROWS BETWEEN 2 PRECEDING AND CURRENT ROW)
  2649. FROM t
  2650. ORDER BY f2, f3, f1, first_value
  2651. ----
  2652. 1 a 1 1
  2653. 2 a 2 1
  2654. 2 a 3 1
  2655. 3 a 4 2
  2656. 4 b 5 4
  2657. 4 b 6 4
  2658. 1 c 7 1
  2659. 2 c 8 1
  2660. 3 c 9 1
  2661. 7 d 10 7
  2662. query ITII
  2663. SELECT f1, f2, f3, first_value(f1) OVER (PARTITION BY f2 ORDER BY f1, f3 ROWS BETWEEN 10 PRECEDING AND CURRENT ROW)
  2664. FROM t
  2665. ORDER BY f2, f3, f1, first_value
  2666. ----
  2667. 1 a 1 1
  2668. 2 a 2 1
  2669. 2 a 3 1
  2670. 3 a 4 1
  2671. 4 b 5 4
  2672. 4 b 6 4
  2673. 1 c 7 1
  2674. 2 c 8 1
  2675. 3 c 9 1
  2676. 7 d 10 7
  2677. # ROWS BETWEEN x PRECEDING AND x FOLLOWING
  2678. # Equivalent to ROWS BETWEEN x PRECEDING AND CURRENT ROW for first_value
  2679. query ITII
  2680. SELECT f1, f2, f3, first_value(f1) OVER (PARTITION BY f2 ORDER BY f1, f3 ROWS BETWEEN 0 PRECEDING AND 1 FOLLOWING)
  2681. FROM t
  2682. ORDER BY f2, f3, f1, first_value
  2683. ----
  2684. 1 a 1 1
  2685. 2 a 2 2
  2686. 2 a 3 2
  2687. 3 a 4 3
  2688. 4 b 5 4
  2689. 4 b 6 4
  2690. 1 c 7 1
  2691. 2 c 8 2
  2692. 3 c 9 3
  2693. 7 d 10 7
  2694. query ITII
  2695. SELECT f1, f2, f3, first_value(f1) OVER (PARTITION BY f2 ORDER BY f1, f3 ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING)
  2696. FROM t
  2697. ORDER BY f2, f3, f1, first_value
  2698. ----
  2699. 1 a 1 1
  2700. 2 a 2 1
  2701. 2 a 3 2
  2702. 3 a 4 2
  2703. 4 b 5 4
  2704. 4 b 6 4
  2705. 1 c 7 1
  2706. 2 c 8 1
  2707. 3 c 9 2
  2708. 7 d 10 7
  2709. query ITII
  2710. SELECT f1, f2, f3, first_value(f1) OVER (PARTITION BY f2 ORDER BY f1, f3 ROWS BETWEEN 2 PRECEDING AND 1 FOLLOWING)
  2711. FROM t
  2712. ORDER BY f2, f3, f1, first_value
  2713. ----
  2714. 1 a 1 1
  2715. 2 a 2 1
  2716. 2 a 3 1
  2717. 3 a 4 2
  2718. 4 b 5 4
  2719. 4 b 6 4
  2720. 1 c 7 1
  2721. 2 c 8 1
  2722. 3 c 9 1
  2723. 7 d 10 7
  2724. query ITII
  2725. SELECT f1, f2, f3, first_value(f1) OVER (PARTITION BY f2 ORDER BY f1, f3 ROWS BETWEEN 10 PRECEDING AND 1 FOLLOWING)
  2726. FROM t
  2727. ORDER BY f2, f3, f1, first_value
  2728. ----
  2729. 1 a 1 1
  2730. 2 a 2 1
  2731. 2 a 3 1
  2732. 3 a 4 1
  2733. 4 b 5 4
  2734. 4 b 6 4
  2735. 1 c 7 1
  2736. 2 c 8 1
  2737. 3 c 9 1
  2738. 7 d 10 7
  2739. # ROWS BETWEEN x PRECEDING AND UNBOUNDED FOLLOWING
  2740. # Equivalent to ROWS BETWEEN x PRECEDING AND CURRENT ROW for first_value
  2741. query ITII
  2742. SELECT f1, f2, f3, first_value(f1) OVER (PARTITION BY f2 ORDER BY f1, f3 ROWS BETWEEN 0 PRECEDING AND UNBOUNDED FOLLOWING)
  2743. FROM t
  2744. ORDER BY f2, f3, f1, first_value
  2745. ----
  2746. 1 a 1 1
  2747. 2 a 2 2
  2748. 2 a 3 2
  2749. 3 a 4 3
  2750. 4 b 5 4
  2751. 4 b 6 4
  2752. 1 c 7 1
  2753. 2 c 8 2
  2754. 3 c 9 3
  2755. 7 d 10 7
  2756. query ITII
  2757. SELECT f1, f2, f3, first_value(f1) OVER (PARTITION BY f2 ORDER BY f1, f3 ROWS BETWEEN 1 PRECEDING AND UNBOUNDED FOLLOWING)
  2758. FROM t
  2759. ORDER BY f2, f3, f1, first_value
  2760. ----
  2761. 1 a 1 1
  2762. 2 a 2 1
  2763. 2 a 3 2
  2764. 3 a 4 2
  2765. 4 b 5 4
  2766. 4 b 6 4
  2767. 1 c 7 1
  2768. 2 c 8 1
  2769. 3 c 9 2
  2770. 7 d 10 7
  2771. query ITII
  2772. SELECT f1, f2, f3, first_value(f1) OVER (PARTITION BY f2 ORDER BY f1, f3 ROWS BETWEEN 2 PRECEDING AND UNBOUNDED FOLLOWING)
  2773. FROM t
  2774. ORDER BY f2, f3, f1, first_value
  2775. ----
  2776. 1 a 1 1
  2777. 2 a 2 1
  2778. 2 a 3 1
  2779. 3 a 4 2
  2780. 4 b 5 4
  2781. 4 b 6 4
  2782. 1 c 7 1
  2783. 2 c 8 1
  2784. 3 c 9 1
  2785. 7 d 10 7
  2786. query ITII
  2787. SELECT f1, f2, f3, first_value(f1) OVER (PARTITION BY f2 ORDER BY f1, f3 ROWS BETWEEN 10 PRECEDING AND UNBOUNDED FOLLOWING)
  2788. FROM t
  2789. ORDER BY f2, f3, f1, first_value
  2790. ----
  2791. 1 a 1 1
  2792. 2 a 2 1
  2793. 2 a 3 1
  2794. 3 a 4 1
  2795. 4 b 5 4
  2796. 4 b 6 4
  2797. 1 c 7 1
  2798. 2 c 8 1
  2799. 3 c 9 1
  2800. 7 d 10 7
  2801. # ROWS BETWEEN CURRENT ROW AND CURRENT ROW
  2802. # Always returns current row
  2803. query ITII
  2804. SELECT f1, f2, f3, first_value(f1) OVER (PARTITION BY f2 ORDER BY f1, f3 ROWS BETWEEN CURRENT ROW AND CURRENT ROW)
  2805. FROM t
  2806. ORDER BY f2, f3, f1, first_value
  2807. ----
  2808. 1 a 1 1
  2809. 2 a 2 2
  2810. 2 a 3 2
  2811. 3 a 4 3
  2812. 4 b 5 4
  2813. 4 b 6 4
  2814. 1 c 7 1
  2815. 2 c 8 2
  2816. 3 c 9 3
  2817. 7 d 10 7
  2818. # ROWS BETWEEN CURRENT ROW AND x FOLLOWING
  2819. # Always returns current row
  2820. query ITII
  2821. SELECT f1, f2, f3, first_value(f1) OVER (PARTITION BY f2 ORDER BY f1, f3 ROWS BETWEEN CURRENT ROW AND 0 FOLLOWING)
  2822. FROM t
  2823. ORDER BY f2, f3, f1, first_value
  2824. ----
  2825. 1 a 1 1
  2826. 2 a 2 2
  2827. 2 a 3 2
  2828. 3 a 4 3
  2829. 4 b 5 4
  2830. 4 b 6 4
  2831. 1 c 7 1
  2832. 2 c 8 2
  2833. 3 c 9 3
  2834. 7 d 10 7
  2835. query ITII
  2836. SELECT f1, f2, f3, first_value(f1) OVER (PARTITION BY f2 ORDER BY f1, f3 ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING)
  2837. FROM t
  2838. ORDER BY f2, f3, f1, first_value
  2839. ----
  2840. 1 a 1 1
  2841. 2 a 2 2
  2842. 2 a 3 2
  2843. 3 a 4 3
  2844. 4 b 5 4
  2845. 4 b 6 4
  2846. 1 c 7 1
  2847. 2 c 8 2
  2848. 3 c 9 3
  2849. 7 d 10 7
  2850. query ITII
  2851. SELECT f1, f2, f3, first_value(f1) OVER (PARTITION BY f2 ORDER BY f1, f3 ROWS BETWEEN CURRENT ROW AND 2 FOLLOWING)
  2852. FROM t
  2853. ORDER BY f2, f3, f1, first_value
  2854. ----
  2855. 1 a 1 1
  2856. 2 a 2 2
  2857. 2 a 3 2
  2858. 3 a 4 3
  2859. 4 b 5 4
  2860. 4 b 6 4
  2861. 1 c 7 1
  2862. 2 c 8 2
  2863. 3 c 9 3
  2864. 7 d 10 7
  2865. query ITII
  2866. SELECT f1, f2, f3, first_value(f1) OVER (PARTITION BY f2 ORDER BY f1, f3 ROWS BETWEEN CURRENT ROW AND 10 FOLLOWING)
  2867. FROM t
  2868. ORDER BY f2, f3, f1, first_value
  2869. ----
  2870. 1 a 1 1
  2871. 2 a 2 2
  2872. 2 a 3 2
  2873. 3 a 4 3
  2874. 4 b 5 4
  2875. 4 b 6 4
  2876. 1 c 7 1
  2877. 2 c 8 2
  2878. 3 c 9 3
  2879. 7 d 10 7
  2880. # ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
  2881. # Always returns current row
  2882. query ITII
  2883. SELECT f1, f2, f3, first_value(f1) OVER (PARTITION BY f2 ORDER BY f1, f3 ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
  2884. FROM t
  2885. ORDER BY f2, f3, f1, first_value
  2886. ----
  2887. 1 a 1 1
  2888. 2 a 2 2
  2889. 2 a 3 2
  2890. 3 a 4 3
  2891. 4 b 5 4
  2892. 4 b 6 4
  2893. 1 c 7 1
  2894. 2 c 8 2
  2895. 3 c 9 3
  2896. 7 d 10 7
  2897. # ROWS BETWEEN 0 FOLLOWING AND x FOLLOWING
  2898. query ITII
  2899. SELECT f1, f2, f3, first_value(f1) OVER (PARTITION BY f2 ORDER BY f1, f3 ROWS BETWEEN 0 FOLLOWING AND 1 FOLLOWING)
  2900. FROM t
  2901. ORDER BY f2, f3, f1, first_value
  2902. ----
  2903. 1 a 1 1
  2904. 2 a 2 2
  2905. 2 a 3 2
  2906. 3 a 4 3
  2907. 4 b 5 4
  2908. 4 b 6 4
  2909. 1 c 7 1
  2910. 2 c 8 2
  2911. 3 c 9 3
  2912. 7 d 10 7
  2913. query ITII
  2914. SELECT f1, f2, f3, first_value(f1) OVER (PARTITION BY f2 ORDER BY f1, f3 ROWS BETWEEN 0 FOLLOWING AND 2 FOLLOWING)
  2915. FROM t
  2916. ORDER BY f2, f3, f1, first_value
  2917. ----
  2918. 1 a 1 1
  2919. 2 a 2 2
  2920. 2 a 3 2
  2921. 3 a 4 3
  2922. 4 b 5 4
  2923. 4 b 6 4
  2924. 1 c 7 1
  2925. 2 c 8 2
  2926. 3 c 9 3
  2927. 7 d 10 7
  2928. query ITII
  2929. SELECT f1, f2, f3, first_value(f1) OVER (PARTITION BY f2 ORDER BY f1, f3 ROWS BETWEEN 0 FOLLOWING AND 10 FOLLOWING)
  2930. FROM t
  2931. ORDER BY f2, f3, f1, first_value
  2932. ----
  2933. 1 a 1 1
  2934. 2 a 2 2
  2935. 2 a 3 2
  2936. 3 a 4 3
  2937. 4 b 5 4
  2938. 4 b 6 4
  2939. 1 c 7 1
  2940. 2 c 8 2
  2941. 3 c 9 3
  2942. 7 d 10 7
  2943. # ROWS BETWEEN x FOLLOWING AND 0 FOLLOWING
  2944. query ITII
  2945. SELECT f1, f2, f3, first_value(f1) OVER (PARTITION BY f2 ORDER BY f1, f3 ROWS BETWEEN 1 FOLLOWING AND 0 FOLLOWING)
  2946. FROM t
  2947. ORDER BY f2, f3, f1, first_value
  2948. ----
  2949. 1 a 1 NULL
  2950. 2 a 2 NULL
  2951. 2 a 3 NULL
  2952. 3 a 4 NULL
  2953. 4 b 5 NULL
  2954. 4 b 6 NULL
  2955. 1 c 7 NULL
  2956. 2 c 8 NULL
  2957. 3 c 9 NULL
  2958. 7 d 10 NULL
  2959. query ITII
  2960. SELECT f1, f2, f3, first_value(f1) OVER (PARTITION BY f2 ORDER BY f1, f3 ROWS BETWEEN 2 FOLLOWING AND 0 FOLLOWING)
  2961. FROM t
  2962. ORDER BY f2, f3, f1, first_value
  2963. ----
  2964. 1 a 1 NULL
  2965. 2 a 2 NULL
  2966. 2 a 3 NULL
  2967. 3 a 4 NULL
  2968. 4 b 5 NULL
  2969. 4 b 6 NULL
  2970. 1 c 7 NULL
  2971. 2 c 8 NULL
  2972. 3 c 9 NULL
  2973. 7 d 10 NULL
  2974. query ITII
  2975. SELECT f1, f2, f3, first_value(f1) OVER (PARTITION BY f2 ORDER BY f1, f3 ROWS BETWEEN 10 FOLLOWING AND 0 FOLLOWING)
  2976. FROM t
  2977. ORDER BY f2, f3, f1, first_value
  2978. ----
  2979. 1 a 1 NULL
  2980. 2 a 2 NULL
  2981. 2 a 3 NULL
  2982. 3 a 4 NULL
  2983. 4 b 5 NULL
  2984. 4 b 6 NULL
  2985. 1 c 7 NULL
  2986. 2 c 8 NULL
  2987. 3 c 9 NULL
  2988. 7 d 10 NULL
  2989. # ROWS BETWEEN x FOLLOWING AND y FOLLOWING, where x < y
  2990. query ITII
  2991. SELECT f1, f2, f3, first_value(f1) OVER (PARTITION BY f2 ORDER BY f1, f3 ROWS BETWEEN 0 FOLLOWING AND 1 FOLLOWING)
  2992. FROM t
  2993. ORDER BY f2, f3, f1, first_value
  2994. ----
  2995. 1 a 1 1
  2996. 2 a 2 2
  2997. 2 a 3 2
  2998. 3 a 4 3
  2999. 4 b 5 4
  3000. 4 b 6 4
  3001. 1 c 7 1
  3002. 2 c 8 2
  3003. 3 c 9 3
  3004. 7 d 10 7
  3005. query ITII
  3006. SELECT f1, f2, f3, first_value(f1) OVER (PARTITION BY f2 ORDER BY f1, f3 ROWS BETWEEN 1 FOLLOWING AND 2 FOLLOWING)
  3007. FROM t
  3008. ORDER BY f2, f3, f1, first_value
  3009. ----
  3010. 1 a 1 2
  3011. 2 a 2 2
  3012. 2 a 3 3
  3013. 3 a 4 NULL
  3014. 4 b 5 4
  3015. 4 b 6 NULL
  3016. 1 c 7 2
  3017. 2 c 8 3
  3018. 3 c 9 NULL
  3019. 7 d 10 NULL
  3020. # ROWS BETWEEN x FOLLOWING AND y FOLLOWING, where x > y
  3021. query ITII
  3022. SELECT f1, f2, f3, first_value(f1) OVER (PARTITION BY f2 ORDER BY f1, f3 ROWS BETWEEN 1 FOLLOWING AND 0 FOLLOWING)
  3023. FROM t
  3024. ORDER BY f2, f3, f1, first_value
  3025. ----
  3026. 1 a 1 NULL
  3027. 2 a 2 NULL
  3028. 2 a 3 NULL
  3029. 3 a 4 NULL
  3030. 4 b 5 NULL
  3031. 4 b 6 NULL
  3032. 1 c 7 NULL
  3033. 2 c 8 NULL
  3034. 3 c 9 NULL
  3035. 7 d 10 NULL
  3036. query ITII
  3037. SELECT f1, f2, f3, first_value(f1) OVER (PARTITION BY f2 ORDER BY f1, f3 ROWS BETWEEN 2 FOLLOWING AND 1 FOLLOWING)
  3038. FROM t
  3039. ORDER BY f2, f3, f1, first_value
  3040. ----
  3041. 1 a 1 NULL
  3042. 2 a 2 NULL
  3043. 2 a 3 NULL
  3044. 3 a 4 NULL
  3045. 4 b 5 NULL
  3046. 4 b 6 NULL
  3047. 1 c 7 NULL
  3048. 2 c 8 NULL
  3049. 3 c 9 NULL
  3050. 7 d 10 NULL
  3051. # ROWS BETWEEN x FOLLOWING AND y FOLLOWING, where x == y
  3052. query ITII
  3053. SELECT f1, f2, f3, first_value(f1) OVER (PARTITION BY f2 ORDER BY f1, f3 ROWS BETWEEN 0 FOLLOWING AND 0 FOLLOWING)
  3054. FROM t
  3055. ORDER BY f2, f3, f1, first_value
  3056. ----
  3057. 1 a 1 1
  3058. 2 a 2 2
  3059. 2 a 3 2
  3060. 3 a 4 3
  3061. 4 b 5 4
  3062. 4 b 6 4
  3063. 1 c 7 1
  3064. 2 c 8 2
  3065. 3 c 9 3
  3066. 7 d 10 7
  3067. query ITII
  3068. SELECT f1, f2, f3, first_value(f1) OVER (PARTITION BY f2 ORDER BY f1, f3 ROWS BETWEEN 1 FOLLOWING AND 1 FOLLOWING)
  3069. FROM t
  3070. ORDER BY f2, f3, f1, first_value
  3071. ----
  3072. 1 a 1 2
  3073. 2 a 2 2
  3074. 2 a 3 3
  3075. 3 a 4 NULL
  3076. 4 b 5 4
  3077. 4 b 6 NULL
  3078. 1 c 7 2
  3079. 2 c 8 3
  3080. 3 c 9 NULL
  3081. 7 d 10 NULL
  3082. query ITII
  3083. SELECT f1, f2, f3, first_value(f1) OVER (PARTITION BY f2 ORDER BY f1, f3 ROWS BETWEEN 2 FOLLOWING AND 2 FOLLOWING)
  3084. FROM t
  3085. ORDER BY f2, f3, f1, first_value
  3086. ----
  3087. 1 a 1 2
  3088. 2 a 2 3
  3089. 2 a 3 NULL
  3090. 3 a 4 NULL
  3091. 4 b 5 NULL
  3092. 4 b 6 NULL
  3093. 1 c 7 3
  3094. 2 c 8 NULL
  3095. 3 c 9 NULL
  3096. 7 d 10 NULL
  3097. query ITII
  3098. SELECT f1, f2, f3, first_value(f1) OVER (PARTITION BY f2 ORDER BY f1, f3 ROWS BETWEEN 10 FOLLOWING AND 10 FOLLOWING)
  3099. FROM t
  3100. ORDER BY f2, f3, f1, first_value
  3101. ----
  3102. 1 a 1 NULL
  3103. 2 a 2 NULL
  3104. 2 a 3 NULL
  3105. 3 a 4 NULL
  3106. 4 b 5 NULL
  3107. 4 b 6 NULL
  3108. 1 c 7 NULL
  3109. 2 c 8 NULL
  3110. 3 c 9 NULL
  3111. 7 d 10 NULL
  3112. # ROWS BETWEEN x FOLLOWING AND UNBOUNDED FOLLOWING
  3113. query ITII
  3114. SELECT f1, f2, f3, first_value(f1) OVER (PARTITION BY f2 ORDER BY f1, f3 ROWS BETWEEN 0 FOLLOWING AND UNBOUNDED FOLLOWING)
  3115. FROM t
  3116. ORDER BY f2, f3, f1, first_value
  3117. ----
  3118. 1 a 1 1
  3119. 2 a 2 2
  3120. 2 a 3 2
  3121. 3 a 4 3
  3122. 4 b 5 4
  3123. 4 b 6 4
  3124. 1 c 7 1
  3125. 2 c 8 2
  3126. 3 c 9 3
  3127. 7 d 10 7
  3128. query ITII
  3129. SELECT f1, f2, f3, first_value(f1) OVER (PARTITION BY f2 ORDER BY f1, f3 ROWS BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING)
  3130. FROM t
  3131. ORDER BY f2, f3, f1, first_value
  3132. ----
  3133. 1 a 1 2
  3134. 2 a 2 2
  3135. 2 a 3 3
  3136. 3 a 4 NULL
  3137. 4 b 5 4
  3138. 4 b 6 NULL
  3139. 1 c 7 2
  3140. 2 c 8 3
  3141. 3 c 9 NULL
  3142. 7 d 10 NULL
  3143. query ITII
  3144. SELECT f1, f2, f3, first_value(f1) OVER (PARTITION BY f2 ORDER BY f1, f3 ROWS BETWEEN 2 FOLLOWING AND UNBOUNDED FOLLOWING)
  3145. FROM t
  3146. ORDER BY f2, f3, f1, first_value
  3147. ----
  3148. 1 a 1 2
  3149. 2 a 2 3
  3150. 2 a 3 NULL
  3151. 3 a 4 NULL
  3152. 4 b 5 NULL
  3153. 4 b 6 NULL
  3154. 1 c 7 3
  3155. 2 c 8 NULL
  3156. 3 c 9 NULL
  3157. 7 d 10 NULL
  3158. query ITII
  3159. SELECT f1, f2, f3, first_value(f1) OVER (PARTITION BY f2 ORDER BY f1, f3 ROWS BETWEEN 10 FOLLOWING AND UNBOUNDED FOLLOWING)
  3160. FROM t
  3161. ORDER BY f2, f3, f1, first_value
  3162. ----
  3163. 1 a 1 NULL
  3164. 2 a 2 NULL
  3165. 2 a 3 NULL
  3166. 3 a 4 NULL
  3167. 4 b 5 NULL
  3168. 4 b 6 NULL
  3169. 1 c 7 NULL
  3170. 2 c 8 NULL
  3171. 3 c 9 NULL
  3172. 7 d 10 NULL
  3173. # Test offset limits
  3174. query error db error: ERROR: Window frame offsets greater than 1000000 are currently not supported
  3175. SELECT f1, f2, f3, first_value(f1) OVER (PARTITION BY f2 ORDER BY f1, f3 ROWS BETWEEN 100 FOLLOWING AND 1000001 FOLLOWING)
  3176. # Test near-overflow behavior on offsets
  3177. # u64::MAX FOLLOWING
  3178. query error db error: ERROR: Window frame offsets greater than 1000000 are currently not supported
  3179. SELECT f1, f2, f3, first_value(f1) OVER (PARTITION BY f2 ORDER BY f1, f3 ROWS BETWEEN 18446744073709551615 FOLLOWING AND 18446744073709551615 FOLLOWING)
  3180. FROM t
  3181. ORDER BY f2, f3, f1, first_value
  3182. query error db error: ERROR: Window frame offsets greater than 1000000 are currently not supported
  3183. SELECT f1, f2, f3, last_value(f1) OVER (PARTITION BY f2 ORDER BY f1 DESC, f3 DESC ROWS BETWEEN 1000001 PRECEDING AND 100 FOLLOWING)
  3184. query error db error: ERROR: Window frame offsets greater than 1000000 are currently not supported
  3185. SELECT f1, f2, f3, first_value(f1) OVER (PARTITION BY f2 ORDER BY f1, f3 ROWS BETWEEN 18446744073709551614 FOLLOWING AND 18446744073709551615 FOLLOWING)
  3186. FROM t
  3187. ORDER BY f2, f3, f1, last_value
  3188. query error Expected literal unsigned integer, found operator "\-"
  3189. SELECT f1, f2, f3, last_value(f1) OVER (PARTITION BY f2 ORDER BY f1 DESC, f3 DESC ROWS BETWEEN 10 PRECEDING AND -1 FOLLOWING)
  3190. FROM t
  3191. ORDER BY f2, f3, f1, last_value
  3192. # u64::MAX PRECEDING
  3193. query error db error: ERROR: Window frame offsets greater than 1000000 are currently not supported
  3194. SELECT f1, f2, f3, first_value(f1) OVER (PARTITION BY f2 ORDER BY f1, f3 ROWS BETWEEN 18446744073709551615 PRECEDING AND 18446744073709551615 PRECEDING)
  3195. FROM t
  3196. ORDER BY f2, f3, f1, first_value
  3197. query error db error: ERROR: Window frame offsets greater than 1000000 are currently not supported
  3198. SELECT f1, f2, f3, first_value(f1) OVER (PARTITION BY f2 ORDER BY f1, f3 ROWS BETWEEN 18446744073709551615 PRECEDING AND 18446744073709551614 PRECEDING)
  3199. FROM t
  3200. ORDER BY f2, f3, f1, first_value
  3201. query ITII
  3202. SELECT f1, f2, f3, first_value(f1) OVER (PARTITION BY f2 ORDER BY f1, f3 ROWS BETWEEN 1000 PRECEDING AND 1000 PRECEDING)
  3203. FROM t
  3204. ORDER BY f2, f3, f1, first_value
  3205. ----
  3206. 1 a 1 NULL
  3207. 2 a 2 NULL
  3208. 2 a 3 NULL
  3209. 3 a 4 NULL
  3210. 4 b 5 NULL
  3211. 4 b 6 NULL
  3212. 1 c 7 NULL
  3213. 2 c 8 NULL
  3214. 3 c 9 NULL
  3215. 7 d 10 NULL
  3216. query ITII
  3217. SELECT f1, f2, f3, first_value(f1) OVER (PARTITION BY f2 ORDER BY f1, f3 ROWS BETWEEN 1000 PRECEDING AND 999 PRECEDING)
  3218. FROM t
  3219. ORDER BY f2, f3, f1, first_value
  3220. ----
  3221. 1 a 1 NULL
  3222. 2 a 2 NULL
  3223. 2 a 3 NULL
  3224. 3 a 4 NULL
  3225. 4 b 5 NULL
  3226. 4 b 6 NULL
  3227. 1 c 7 NULL
  3228. 2 c 8 NULL
  3229. 3 c 9 NULL
  3230. 7 d 10 NULL
  3231. # reduce_elision code path
  3232. statement ok
  3233. CREATE TABLE t5 (f1 INTEGER)
  3234. ----
  3235. statement ok
  3236. INSERT INTO t5 VALUES (1)
  3237. ----
  3238. # Default frame, includes current row
  3239. query II
  3240. SELECT f1, first_value(f1) OVER (PARTITION BY f1)
  3241. FROM t5
  3242. GROUP BY f1
  3243. ----
  3244. 1 1
  3245. # Frame starting at UNBOUNDED PRECEDING
  3246. query II
  3247. SELECT f1, first_value(f1) OVER (PARTITION BY f1 ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING)
  3248. FROM t5
  3249. GROUP BY f1
  3250. ----
  3251. 1 NULL
  3252. query II
  3253. SELECT f1, first_value(f1) OVER (PARTITION BY f1 ROWS BETWEEN UNBOUNDED PRECEDING AND 0 PRECEDING)
  3254. FROM t5
  3255. GROUP BY f1
  3256. ----
  3257. 1 1
  3258. query II
  3259. SELECT f1, first_value(f1) OVER (PARTITION BY f1 ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
  3260. FROM t5
  3261. GROUP BY f1
  3262. ----
  3263. 1 1
  3264. query II
  3265. SELECT f1, first_value(f1) OVER (PARTITION BY f1 ROWS BETWEEN UNBOUNDED PRECEDING AND 0 FOLLOWING)
  3266. FROM t5
  3267. GROUP BY f1
  3268. ----
  3269. 1 1
  3270. query II
  3271. SELECT f1, first_value(f1) OVER (PARTITION BY f1 ROWS BETWEEN UNBOUNDED PRECEDING AND 1 FOLLOWING)
  3272. FROM t5
  3273. GROUP BY f1
  3274. ----
  3275. 1 1
  3276. query II
  3277. SELECT f1, first_value(f1) OVER (PARTITION BY f1 ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)
  3278. FROM t5
  3279. GROUP BY f1
  3280. ----
  3281. 1 1
  3282. # Frame starting at 1 PRECEDING
  3283. query II
  3284. SELECT f1, first_value(f1) OVER (PARTITION BY f1 ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING)
  3285. FROM t5
  3286. GROUP BY f1
  3287. ----
  3288. 1 NULL
  3289. query II
  3290. SELECT f1, first_value(f1) OVER (PARTITION BY f1 ROWS BETWEEN 1 PRECEDING AND 0 PRECEDING)
  3291. FROM t5
  3292. GROUP BY f1
  3293. ----
  3294. 1 1
  3295. query II
  3296. SELECT f1, first_value(f1) OVER (PARTITION BY f1 ROWS BETWEEN 1 PRECEDING AND CURRENT ROW)
  3297. FROM t5
  3298. GROUP BY f1
  3299. ----
  3300. 1 1
  3301. query II
  3302. SELECT f1, first_value(f1) OVER (PARTITION BY f1 ROWS BETWEEN 1 PRECEDING AND 0 FOLLOWING)
  3303. FROM t5
  3304. GROUP BY f1
  3305. ----
  3306. 1 1
  3307. query II
  3308. SELECT f1, first_value(f1) OVER (PARTITION BY f1 ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING)
  3309. FROM t5
  3310. GROUP BY f1
  3311. ----
  3312. 1 1
  3313. query II
  3314. SELECT f1, first_value(f1) OVER (PARTITION BY f1 ROWS BETWEEN 1 PRECEDING AND UNBOUNDED FOLLOWING)
  3315. FROM t5
  3316. GROUP BY f1
  3317. ----
  3318. 1 1
  3319. # Frame starting at 0 PRECEDING
  3320. query II
  3321. SELECT f1, first_value(f1) OVER (PARTITION BY f1 ROWS BETWEEN 0 PRECEDING AND 1 PRECEDING)
  3322. FROM t5
  3323. GROUP BY f1
  3324. ----
  3325. 1 NULL
  3326. query II
  3327. SELECT f1, first_value(f1) OVER (PARTITION BY f1 ROWS BETWEEN 0 PRECEDING AND 0 PRECEDING)
  3328. FROM t5
  3329. GROUP BY f1
  3330. ----
  3331. 1 1
  3332. query II
  3333. SELECT f1, first_value(f1) OVER (PARTITION BY f1 ROWS BETWEEN 0 PRECEDING AND CURRENT ROW)
  3334. FROM t5
  3335. GROUP BY f1
  3336. ----
  3337. 1 1
  3338. query II
  3339. SELECT f1, first_value(f1) OVER (PARTITION BY f1 ROWS BETWEEN 0 PRECEDING AND 0 FOLLOWING)
  3340. FROM t5
  3341. GROUP BY f1
  3342. ----
  3343. 1 1
  3344. query II
  3345. SELECT f1, first_value(f1) OVER (PARTITION BY f1 ROWS BETWEEN 0 PRECEDING AND 1 FOLLOWING)
  3346. FROM t5
  3347. GROUP BY f1
  3348. ----
  3349. 1 1
  3350. query II
  3351. SELECT f1, first_value(f1) OVER (PARTITION BY f1 ROWS BETWEEN 0 PRECEDING AND UNBOUNDED FOLLOWING)
  3352. FROM t5
  3353. GROUP BY f1
  3354. ----
  3355. 1 1
  3356. # Frame starting at CURRENT ROW
  3357. query II
  3358. SELECT f1, first_value(f1) OVER (PARTITION BY f1 ROWS BETWEEN CURRENT ROW AND CURRENT ROW)
  3359. FROM t5
  3360. GROUP BY f1
  3361. ----
  3362. 1 1
  3363. query II
  3364. SELECT f1, first_value(f1) OVER (PARTITION BY f1 ROWS BETWEEN CURRENT ROW AND 0 FOLLOWING)
  3365. FROM t5
  3366. GROUP BY f1
  3367. ----
  3368. 1 1
  3369. query II
  3370. SELECT f1, first_value(f1) OVER (PARTITION BY f1 ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING)
  3371. FROM t5
  3372. GROUP BY f1
  3373. ----
  3374. 1 1
  3375. query II
  3376. SELECT f1, first_value(f1) OVER (PARTITION BY f1 ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
  3377. FROM t5
  3378. GROUP BY f1
  3379. ----
  3380. 1 1
  3381. # Frame starting at 0 FOLLOWING
  3382. query II
  3383. SELECT f1, first_value(f1) OVER (PARTITION BY f1 ROWS BETWEEN 0 FOLLOWING AND 0 FOLLOWING)
  3384. FROM t5
  3385. GROUP BY f1
  3386. ----
  3387. 1 1
  3388. query II
  3389. SELECT f1, first_value(f1) OVER (PARTITION BY f1 ROWS BETWEEN 0 FOLLOWING AND 1 FOLLOWING)
  3390. FROM t5
  3391. GROUP BY f1
  3392. ----
  3393. 1 1
  3394. query II
  3395. SELECT f1, first_value(f1) OVER (PARTITION BY f1 ROWS BETWEEN 0 FOLLOWING AND UNBOUNDED FOLLOWING)
  3396. FROM t5
  3397. GROUP BY f1
  3398. ----
  3399. 1 1
  3400. # Frame starting at 1 FOLLOWING
  3401. query II
  3402. SELECT f1, first_value(f1) OVER (PARTITION BY f1 ROWS BETWEEN 1 FOLLOWING AND 0 FOLLOWING)
  3403. FROM t5
  3404. GROUP BY f1
  3405. ----
  3406. 1 NULL
  3407. query II
  3408. SELECT f1, first_value(f1) OVER (PARTITION BY f1 ROWS BETWEEN 1 FOLLOWING AND 1 FOLLOWING)
  3409. FROM t5
  3410. GROUP BY f1
  3411. ----
  3412. 1 NULL
  3413. query II
  3414. SELECT f1, first_value(f1) OVER (PARTITION BY f1 ROWS BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING)
  3415. FROM t5
  3416. GROUP BY f1
  3417. ----
  3418. 1 NULL
  3419. ## last_value
  3420. # Default frame (RANGE BETWEEN UNBOUNDED FOLLOWING AND CURRENT ROW)
  3421. query ITII
  3422. SELECT f1, f2, f3, last_value(f1) OVER (PARTITION BY f2 ORDER BY f1 DESC, f3 DESC)
  3423. FROM t
  3424. ORDER BY f2, f3, f1, last_value
  3425. ----
  3426. 1 a 1 1
  3427. 2 a 2 2
  3428. 2 a 3 2
  3429. 3 a 4 3
  3430. 4 b 5 4
  3431. 4 b 6 4
  3432. 1 c 7 1
  3433. 2 c 8 2
  3434. 3 c 9 3
  3435. 7 d 10 7
  3436. # Default frame with large peer group
  3437. # Note: there are multiple valid results of this query (because the default frame is up through the current row's last
  3438. # ORDER BY peer, and the ordering among ORDER BY peers is unspecified), but we make the result stable by internally
  3439. # always adding all remaining columns into our orderings.
  3440. query ITII
  3441. SELECT f1, f2, f3, last_value(f3) OVER (PARTITION BY f2 ORDER BY f1)
  3442. FROM t
  3443. ORDER BY f2, f3, f1, last_value
  3444. ----
  3445. 1 a 1 1
  3446. 2 a 2 3
  3447. 2 a 3 3
  3448. 3 a 4 4
  3449. 4 b 5 6
  3450. 4 b 6 6
  3451. 1 c 7 7
  3452. 2 c 8 8
  3453. 3 c 9 9
  3454. 7 d 10 10
  3455. # ROWS BETWEEN x FOLLOWING AND UNBOUNDED FOLLOWING
  3456. query ITII
  3457. SELECT f1, f2, f3, last_value(f1) OVER (PARTITION BY f2 ORDER BY f1 DESC, f3 DESC ROWS BETWEEN 0 FOLLOWING AND UNBOUNDED FOLLOWING)
  3458. FROM t
  3459. ORDER BY f2, f3, f1, last_value
  3460. ----
  3461. 1 a 1 1
  3462. 2 a 2 1
  3463. 2 a 3 1
  3464. 3 a 4 1
  3465. 4 b 5 4
  3466. 4 b 6 4
  3467. 1 c 7 1
  3468. 2 c 8 1
  3469. 3 c 9 1
  3470. 7 d 10 7
  3471. query ITII
  3472. SELECT f1, f2, f3, last_value(f1) OVER (PARTITION BY f2 ORDER BY f1 DESC, f3 DESC ROWS BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING)
  3473. FROM t
  3474. ORDER BY f2, f3, f1, last_value
  3475. ----
  3476. 1 a 1 NULL
  3477. 2 a 2 1
  3478. 2 a 3 1
  3479. 3 a 4 1
  3480. 4 b 5 NULL
  3481. 4 b 6 4
  3482. 1 c 7 NULL
  3483. 2 c 8 1
  3484. 3 c 9 1
  3485. 7 d 10 NULL
  3486. query ITII
  3487. SELECT f1, f2, f3, last_value(f1) OVER (PARTITION BY f2 ORDER BY f1 DESC, f3 DESC ROWS BETWEEN 2 FOLLOWING AND UNBOUNDED FOLLOWING)
  3488. FROM t
  3489. ORDER BY f2, f3, f1, last_value
  3490. ----
  3491. 1 a 1 NULL
  3492. 2 a 2 NULL
  3493. 2 a 3 1
  3494. 3 a 4 1
  3495. 4 b 5 NULL
  3496. 4 b 6 NULL
  3497. 1 c 7 NULL
  3498. 2 c 8 NULL
  3499. 3 c 9 1
  3500. 7 d 10 NULL
  3501. query ITII
  3502. SELECT f1, f2, f3, last_value(f1) OVER (PARTITION BY f2 ORDER BY f1 DESC, f3 DESC ROWS BETWEEN 10 FOLLOWING AND UNBOUNDED FOLLOWING)
  3503. FROM t
  3504. ORDER BY f2, f3, f1, last_value
  3505. ----
  3506. 1 a 1 NULL
  3507. 2 a 2 NULL
  3508. 2 a 3 NULL
  3509. 3 a 4 NULL
  3510. 4 b 5 NULL
  3511. 4 b 6 NULL
  3512. 1 c 7 NULL
  3513. 2 c 8 NULL
  3514. 3 c 9 NULL
  3515. 7 d 10 NULL
  3516. # ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
  3517. query ITII
  3518. SELECT f1, f2, f3, last_value(f1) OVER (PARTITION BY f2 ORDER BY f1 DESC, f3 DESC ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
  3519. FROM t
  3520. ORDER BY f2, f3, f1, last_value
  3521. ----
  3522. 1 a 1 1
  3523. 2 a 2 1
  3524. 2 a 3 1
  3525. 3 a 4 1
  3526. 4 b 5 4
  3527. 4 b 6 4
  3528. 1 c 7 1
  3529. 2 c 8 1
  3530. 3 c 9 1
  3531. 7 d 10 7
  3532. # ROWS BETWEEN x PRECEDING AND UNBOUNDED FOLLOWING
  3533. query ITII
  3534. SELECT f1, f2, f3, last_value(f1) OVER (PARTITION BY f2 ORDER BY f1 DESC, f3 DESC ROWS BETWEEN 0 PRECEDING AND UNBOUNDED FOLLOWING)
  3535. FROM t
  3536. ORDER BY f2, f3, f1, last_value
  3537. ----
  3538. 1 a 1 1
  3539. 2 a 2 1
  3540. 2 a 3 1
  3541. 3 a 4 1
  3542. 4 b 5 4
  3543. 4 b 6 4
  3544. 1 c 7 1
  3545. 2 c 8 1
  3546. 3 c 9 1
  3547. 7 d 10 7
  3548. query ITII
  3549. SELECT f1, f2, f3, last_value(f1) OVER (PARTITION BY f2 ORDER BY f1 DESC, f3 DESC ROWS BETWEEN 1 PRECEDING AND UNBOUNDED FOLLOWING)
  3550. FROM t
  3551. ORDER BY f2, f3, f1, last_value
  3552. ----
  3553. 1 a 1 1
  3554. 2 a 2 1
  3555. 2 a 3 1
  3556. 3 a 4 1
  3557. 4 b 5 4
  3558. 4 b 6 4
  3559. 1 c 7 1
  3560. 2 c 8 1
  3561. 3 c 9 1
  3562. 7 d 10 7
  3563. query ITII
  3564. SELECT f1, f2, f3, last_value(f1) OVER (PARTITION BY f2 ORDER BY f1 DESC, f3 DESC ROWS BETWEEN 2 PRECEDING AND UNBOUNDED FOLLOWING)
  3565. FROM t
  3566. ORDER BY f2, f3, f1, last_value
  3567. ----
  3568. 1 a 1 1
  3569. 2 a 2 1
  3570. 2 a 3 1
  3571. 3 a 4 1
  3572. 4 b 5 4
  3573. 4 b 6 4
  3574. 1 c 7 1
  3575. 2 c 8 1
  3576. 3 c 9 1
  3577. 7 d 10 7
  3578. query ITII
  3579. SELECT f1, f2, f3, last_value(f1) OVER (PARTITION BY f2 ORDER BY f1 DESC, f3 DESC ROWS BETWEEN 10 PRECEDING AND UNBOUNDED FOLLOWING)
  3580. FROM t
  3581. ORDER BY f2, f3, f1, last_value
  3582. ----
  3583. 1 a 1 1
  3584. 2 a 2 1
  3585. 2 a 3 1
  3586. 3 a 4 1
  3587. 4 b 5 4
  3588. 4 b 6 4
  3589. 1 c 7 1
  3590. 2 c 8 1
  3591. 3 c 9 1
  3592. 7 d 10 7
  3593. # ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
  3594. query ITII
  3595. SELECT f1, f2, f3, last_value(f1) OVER (PARTITION BY f2 ORDER BY f1 DESC, f3 DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)
  3596. FROM t
  3597. ORDER BY f2, f3, f1, last_value
  3598. ----
  3599. 1 a 1 1
  3600. 2 a 2 1
  3601. 2 a 3 1
  3602. 3 a 4 1
  3603. 4 b 5 4
  3604. 4 b 6 4
  3605. 1 c 7 1
  3606. 2 c 8 1
  3607. 3 c 9 1
  3608. 7 d 10 7
  3609. # ROWS BETWEEN x FOLLOWING AND 0 FOLLOWING
  3610. query ITII
  3611. SELECT f1, f2, f3, last_value(f1) OVER (PARTITION BY f2 ORDER BY f1 DESC, f3 DESC ROWS BETWEEN 1 FOLLOWING AND 0 FOLLOWING)
  3612. FROM t
  3613. ORDER BY f2, f3, f1, last_value
  3614. ----
  3615. 1 a 1 NULL
  3616. 2 a 2 NULL
  3617. 2 a 3 NULL
  3618. 3 a 4 NULL
  3619. 4 b 5 NULL
  3620. 4 b 6 NULL
  3621. 1 c 7 NULL
  3622. 2 c 8 NULL
  3623. 3 c 9 NULL
  3624. 7 d 10 NULL
  3625. query ITII
  3626. SELECT f1, f2, f3, last_value(f1) OVER (PARTITION BY f2 ORDER BY f1 DESC, f3 DESC ROWS BETWEEN 2 FOLLOWING AND 0 FOLLOWING)
  3627. FROM t
  3628. ORDER BY f2, f3, f1, last_value
  3629. ----
  3630. 1 a 1 NULL
  3631. 2 a 2 NULL
  3632. 2 a 3 NULL
  3633. 3 a 4 NULL
  3634. 4 b 5 NULL
  3635. 4 b 6 NULL
  3636. 1 c 7 NULL
  3637. 2 c 8 NULL
  3638. 3 c 9 NULL
  3639. 7 d 10 NULL
  3640. query ITII
  3641. SELECT f1, f2, f3, last_value(f1) OVER (PARTITION BY f2 ORDER BY f1 DESC, f3 DESC ROWS BETWEEN 10 FOLLOWING AND 0 FOLLOWING)
  3642. FROM t
  3643. ORDER BY f2, f3, f1, last_value
  3644. ----
  3645. 1 a 1 NULL
  3646. 2 a 2 NULL
  3647. 2 a 3 NULL
  3648. 3 a 4 NULL
  3649. 4 b 5 NULL
  3650. 4 b 6 NULL
  3651. 1 c 7 NULL
  3652. 2 c 8 NULL
  3653. 3 c 9 NULL
  3654. 7 d 10 NULL
  3655. # ROWS BETWEEN 0 FOLLOWING AND x FOLLOWING
  3656. query ITII
  3657. SELECT f1, f2, f3, last_value(f1) OVER (PARTITION BY f2 ORDER BY f1 DESC, f3 DESC ROWS BETWEEN 0 FOLLOWING AND 1 FOLLOWING)
  3658. FROM t
  3659. ORDER BY f2, f3, f1, last_value
  3660. ----
  3661. 1 a 1 1
  3662. 2 a 2 1
  3663. 2 a 3 2
  3664. 3 a 4 2
  3665. 4 b 5 4
  3666. 4 b 6 4
  3667. 1 c 7 1
  3668. 2 c 8 1
  3669. 3 c 9 2
  3670. 7 d 10 7
  3671. query ITII
  3672. SELECT f1, f2, f3, last_value(f1) OVER (PARTITION BY f2 ORDER BY f1 DESC, f3 DESC ROWS BETWEEN 0 FOLLOWING AND 2 FOLLOWING)
  3673. FROM t
  3674. ORDER BY f2, f3, f1, last_value
  3675. ----
  3676. 1 a 1 1
  3677. 2 a 2 1
  3678. 2 a 3 1
  3679. 3 a 4 2
  3680. 4 b 5 4
  3681. 4 b 6 4
  3682. 1 c 7 1
  3683. 2 c 8 1
  3684. 3 c 9 1
  3685. 7 d 10 7
  3686. query ITII
  3687. SELECT f1, f2, f3, last_value(f1) OVER (PARTITION BY f2 ORDER BY f1 DESC, f3 DESC ROWS BETWEEN 0 FOLLOWING AND 10 FOLLOWING)
  3688. FROM t
  3689. ORDER BY f2, f3, f1, last_value
  3690. ----
  3691. 1 a 1 1
  3692. 2 a 2 1
  3693. 2 a 3 1
  3694. 3 a 4 1
  3695. 4 b 5 4
  3696. 4 b 6 4
  3697. 1 c 7 1
  3698. 2 c 8 1
  3699. 3 c 9 1
  3700. 7 d 10 7
  3701. # ROWS BETWEEN y FOLLOWING AND x FOLLOWING, where x < y
  3702. query ITII
  3703. SELECT f1, f2, f3, last_value(f1) OVER (PARTITION BY f2 ORDER BY f1 DESC, f3 DESC ROWS BETWEEN 1 FOLLOWING AND 0 FOLLOWING)
  3704. FROM t
  3705. ORDER BY f2, f3, f1, last_value
  3706. ----
  3707. 1 a 1 NULL
  3708. 2 a 2 NULL
  3709. 2 a 3 NULL
  3710. 3 a 4 NULL
  3711. 4 b 5 NULL
  3712. 4 b 6 NULL
  3713. 1 c 7 NULL
  3714. 2 c 8 NULL
  3715. 3 c 9 NULL
  3716. 7 d 10 NULL
  3717. query ITII
  3718. SELECT f1, f2, f3, last_value(f1) OVER (PARTITION BY f2 ORDER BY f1 DESC, f3 DESC ROWS BETWEEN 2 FOLLOWING AND 1 FOLLOWING)
  3719. FROM t
  3720. ORDER BY f2, f3, f1, last_value
  3721. ----
  3722. 1 a 1 NULL
  3723. 2 a 2 NULL
  3724. 2 a 3 NULL
  3725. 3 a 4 NULL
  3726. 4 b 5 NULL
  3727. 4 b 6 NULL
  3728. 1 c 7 NULL
  3729. 2 c 8 NULL
  3730. 3 c 9 NULL
  3731. 7 d 10 NULL
  3732. # ROWS BETWEEN y FOLLOWING AND x FOLLOWING, where x > y
  3733. query ITII
  3734. SELECT f1, f2, f3, last_value(f1) OVER (PARTITION BY f2 ORDER BY f1 DESC, f3 DESC ROWS BETWEEN 0 FOLLOWING AND 1 FOLLOWING)
  3735. FROM t
  3736. ORDER BY f2, f3, f1, last_value
  3737. ----
  3738. 1 a 1 1
  3739. 2 a 2 1
  3740. 2 a 3 2
  3741. 3 a 4 2
  3742. 4 b 5 4
  3743. 4 b 6 4
  3744. 1 c 7 1
  3745. 2 c 8 1
  3746. 3 c 9 2
  3747. 7 d 10 7
  3748. query ITII
  3749. SELECT f1, f2, f3, last_value(f1) OVER (PARTITION BY f2 ORDER BY f1 DESC, f3 DESC ROWS BETWEEN 1 FOLLOWING AND 2 FOLLOWING)
  3750. FROM t
  3751. ORDER BY f2, f3, f1, last_value
  3752. ----
  3753. 1 a 1 NULL
  3754. 2 a 2 1
  3755. 2 a 3 1
  3756. 3 a 4 2
  3757. 4 b 5 NULL
  3758. 4 b 6 4
  3759. 1 c 7 NULL
  3760. 2 c 8 1
  3761. 3 c 9 1
  3762. 7 d 10 NULL
  3763. # ROWS BETWEEN x FOLLOWING AND y FOLLOWING, where x == y
  3764. query ITII
  3765. SELECT f1, f2, f3, last_value(f1) OVER (PARTITION BY f2 ORDER BY f1 DESC, f3 DESC ROWS BETWEEN 0 FOLLOWING AND 0 FOLLOWING)
  3766. FROM t
  3767. ORDER BY f2, f3, f1, last_value
  3768. ----
  3769. 1 a 1 1
  3770. 2 a 2 2
  3771. 2 a 3 2
  3772. 3 a 4 3
  3773. 4 b 5 4
  3774. 4 b 6 4
  3775. 1 c 7 1
  3776. 2 c 8 2
  3777. 3 c 9 3
  3778. 7 d 10 7
  3779. query ITII
  3780. SELECT f1, f2, f3, last_value(f1) OVER (PARTITION BY f2 ORDER BY f1 DESC, f3 DESC ROWS BETWEEN 1 FOLLOWING AND 1 FOLLOWING)
  3781. FROM t
  3782. ORDER BY f2, f3, f1, last_value
  3783. ----
  3784. 1 a 1 NULL
  3785. 2 a 2 1
  3786. 2 a 3 2
  3787. 3 a 4 2
  3788. 4 b 5 NULL
  3789. 4 b 6 4
  3790. 1 c 7 NULL
  3791. 2 c 8 1
  3792. 3 c 9 2
  3793. 7 d 10 NULL
  3794. query ITII
  3795. SELECT f1, f2, f3, last_value(f1) OVER (PARTITION BY f2 ORDER BY f1 DESC, f3 DESC ROWS BETWEEN 2 FOLLOWING AND 2 FOLLOWING)
  3796. FROM t
  3797. ORDER BY f2, f3, f1, last_value
  3798. ----
  3799. 1 a 1 NULL
  3800. 2 a 2 NULL
  3801. 2 a 3 1
  3802. 3 a 4 2
  3803. 4 b 5 NULL
  3804. 4 b 6 NULL
  3805. 1 c 7 NULL
  3806. 2 c 8 NULL
  3807. 3 c 9 1
  3808. 7 d 10 NULL
  3809. query ITII
  3810. SELECT f1, f2, f3, last_value(f1) OVER (PARTITION BY f2 ORDER BY f1 DESC, f3 DESC ROWS BETWEEN 10 FOLLOWING AND 10 FOLLOWING)
  3811. FROM t
  3812. ORDER BY f2, f3, f1, last_value
  3813. ----
  3814. 1 a 1 NULL
  3815. 2 a 2 NULL
  3816. 2 a 3 NULL
  3817. 3 a 4 NULL
  3818. 4 b 5 NULL
  3819. 4 b 6 NULL
  3820. 1 c 7 NULL
  3821. 2 c 8 NULL
  3822. 3 c 9 NULL
  3823. 7 d 10 NULL
  3824. # ROWS BETWEEN CURRENT ROW AND x FOLLOWING
  3825. query ITII
  3826. SELECT f1, f2, f3, last_value(f1) OVER (PARTITION BY f2 ORDER BY f1 DESC, f3 DESC ROWS BETWEEN CURRENT ROW AND 0 FOLLOWING)
  3827. FROM t
  3828. ORDER BY f2, f3, f1, last_value
  3829. ----
  3830. 1 a 1 1
  3831. 2 a 2 2
  3832. 2 a 3 2
  3833. 3 a 4 3
  3834. 4 b 5 4
  3835. 4 b 6 4
  3836. 1 c 7 1
  3837. 2 c 8 2
  3838. 3 c 9 3
  3839. 7 d 10 7
  3840. query ITII
  3841. SELECT f1, f2, f3, last_value(f1) OVER (PARTITION BY f2 ORDER BY f1 DESC, f3 DESC ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING)
  3842. FROM t
  3843. ORDER BY f2, f3, f1, last_value
  3844. ----
  3845. 1 a 1 1
  3846. 2 a 2 1
  3847. 2 a 3 2
  3848. 3 a 4 2
  3849. 4 b 5 4
  3850. 4 b 6 4
  3851. 1 c 7 1
  3852. 2 c 8 1
  3853. 3 c 9 2
  3854. 7 d 10 7
  3855. query ITII
  3856. SELECT f1, f2, f3, last_value(f1) OVER (PARTITION BY f2 ORDER BY f1 DESC, f3 DESC ROWS BETWEEN CURRENT ROW AND 2 FOLLOWING)
  3857. FROM t
  3858. ORDER BY f2, f3, f1, last_value
  3859. ----
  3860. 1 a 1 1
  3861. 2 a 2 1
  3862. 2 a 3 1
  3863. 3 a 4 2
  3864. 4 b 5 4
  3865. 4 b 6 4
  3866. 1 c 7 1
  3867. 2 c 8 1
  3868. 3 c 9 1
  3869. 7 d 10 7
  3870. query ITII
  3871. SELECT f1, f2, f3, last_value(f1) OVER (PARTITION BY f2 ORDER BY f1 DESC, f3 DESC ROWS BETWEEN CURRENT ROW AND 10 FOLLOWING)
  3872. FROM t
  3873. ORDER BY f2, f3, f1, last_value
  3874. ----
  3875. 1 a 1 1
  3876. 2 a 2 1
  3877. 2 a 3 1
  3878. 3 a 4 1
  3879. 4 b 5 4
  3880. 4 b 6 4
  3881. 1 c 7 1
  3882. 2 c 8 1
  3883. 3 c 9 1
  3884. 7 d 10 7
  3885. # ROWS BETWEEN x PRECEDING AND x FOLLOWING
  3886. # Equivalent to ROWS BETWEEN CURRENT ROW AND x FOLLOWING for last_value
  3887. query ITII
  3888. SELECT f1, f2, f3, last_value(f1) OVER (PARTITION BY f2 ORDER BY f1 DESC, f3 DESC ROWS BETWEEN 1 PRECEDING AND 0 FOLLOWING)
  3889. FROM t
  3890. ORDER BY f2, f3, f1, last_value
  3891. ----
  3892. 1 a 1 1
  3893. 2 a 2 2
  3894. 2 a 3 2
  3895. 3 a 4 3
  3896. 4 b 5 4
  3897. 4 b 6 4
  3898. 1 c 7 1
  3899. 2 c 8 2
  3900. 3 c 9 3
  3901. 7 d 10 7
  3902. query ITII
  3903. SELECT f1, f2, f3, last_value(f1) OVER (PARTITION BY f2 ORDER BY f1 DESC, f3 DESC ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING)
  3904. FROM t
  3905. ORDER BY f2, f3, f1, last_value
  3906. ----
  3907. 1 a 1 1
  3908. 2 a 2 1
  3909. 2 a 3 2
  3910. 3 a 4 2
  3911. 4 b 5 4
  3912. 4 b 6 4
  3913. 1 c 7 1
  3914. 2 c 8 1
  3915. 3 c 9 2
  3916. 7 d 10 7
  3917. query ITII
  3918. SELECT f1, f2, f3, last_value(f1) OVER (PARTITION BY f2 ORDER BY f1 DESC, f3 DESC ROWS BETWEEN 1 PRECEDING AND 2 FOLLOWING)
  3919. FROM t
  3920. ORDER BY f2, f3, f1, last_value
  3921. ----
  3922. 1 a 1 1
  3923. 2 a 2 1
  3924. 2 a 3 1
  3925. 3 a 4 2
  3926. 4 b 5 4
  3927. 4 b 6 4
  3928. 1 c 7 1
  3929. 2 c 8 1
  3930. 3 c 9 1
  3931. 7 d 10 7
  3932. query ITII
  3933. SELECT f1, f2, f3, last_value(f1) OVER (PARTITION BY f2 ORDER BY f1 DESC, f3 DESC ROWS BETWEEN 1 PRECEDING AND 10 FOLLOWING)
  3934. FROM t
  3935. ORDER BY f2, f3, f1, last_value
  3936. ----
  3937. 1 a 1 1
  3938. 2 a 2 1
  3939. 2 a 3 1
  3940. 3 a 4 1
  3941. 4 b 5 4
  3942. 4 b 6 4
  3943. 1 c 7 1
  3944. 2 c 8 1
  3945. 3 c 9 1
  3946. 7 d 10 7
  3947. # ROWS BETWEEN UNBOUNDED PRECEDING AND x FOLLOWING
  3948. # Equivalent to ROWS BETWEEN CURRENT ROW AND x FOLLOWING for last_value
  3949. query ITII
  3950. SELECT f1, f2, f3, last_value(f1) OVER (PARTITION BY f2 ORDER BY f1 DESC, f3 DESC ROWS BETWEEN UNBOUNDED PRECEDING AND 0 FOLLOWING)
  3951. FROM t
  3952. ORDER BY f2, f3, f1, last_value
  3953. ----
  3954. 1 a 1 1
  3955. 2 a 2 2
  3956. 2 a 3 2
  3957. 3 a 4 3
  3958. 4 b 5 4
  3959. 4 b 6 4
  3960. 1 c 7 1
  3961. 2 c 8 2
  3962. 3 c 9 3
  3963. 7 d 10 7
  3964. query ITII
  3965. SELECT f1, f2, f3, last_value(f1) OVER (PARTITION BY f2 ORDER BY f1 DESC, f3 DESC ROWS BETWEEN UNBOUNDED PRECEDING AND 1 FOLLOWING)
  3966. FROM t
  3967. ORDER BY f2, f3, f1, last_value
  3968. ----
  3969. 1 a 1 1
  3970. 2 a 2 1
  3971. 2 a 3 2
  3972. 3 a 4 2
  3973. 4 b 5 4
  3974. 4 b 6 4
  3975. 1 c 7 1
  3976. 2 c 8 1
  3977. 3 c 9 2
  3978. 7 d 10 7
  3979. query ITII
  3980. SELECT f1, f2, f3, last_value(f1) OVER (PARTITION BY f2 ORDER BY f1 DESC, f3 DESC ROWS BETWEEN UNBOUNDED PRECEDING AND 2 FOLLOWING)
  3981. FROM t
  3982. ORDER BY f2, f3, f1, last_value
  3983. ----
  3984. 1 a 1 1
  3985. 2 a 2 1
  3986. 2 a 3 1
  3987. 3 a 4 2
  3988. 4 b 5 4
  3989. 4 b 6 4
  3990. 1 c 7 1
  3991. 2 c 8 1
  3992. 3 c 9 1
  3993. 7 d 10 7
  3994. query ITII
  3995. SELECT f1, f2, f3, last_value(f1) OVER (PARTITION BY f2 ORDER BY f1 DESC, f3 DESC ROWS BETWEEN UNBOUNDED PRECEDING AND 10 FOLLOWING)
  3996. FROM t
  3997. ORDER BY f2, f3, f1, last_value
  3998. ----
  3999. 1 a 1 1
  4000. 2 a 2 1
  4001. 2 a 3 1
  4002. 3 a 4 1
  4003. 4 b 5 4
  4004. 4 b 6 4
  4005. 1 c 7 1
  4006. 2 c 8 1
  4007. 3 c 9 1
  4008. 7 d 10 7
  4009. # ROWS BETWEEN CURRENT ROW AND CURRENT ROW
  4010. # Always returns current row
  4011. query ITII
  4012. SELECT f1, f2, f3, last_value(f1) OVER (PARTITION BY f2 ORDER BY f1 DESC, f3 DESC ROWS BETWEEN CURRENT ROW AND CURRENT ROW)
  4013. FROM t
  4014. ORDER BY f2, f3, f1, last_value
  4015. ----
  4016. 1 a 1 1
  4017. 2 a 2 2
  4018. 2 a 3 2
  4019. 3 a 4 3
  4020. 4 b 5 4
  4021. 4 b 6 4
  4022. 1 c 7 1
  4023. 2 c 8 2
  4024. 3 c 9 3
  4025. 7 d 10 7
  4026. # ROWS BETWEEN x PRECEDING AND CURRENT ROW
  4027. # Always returns current row
  4028. query ITII
  4029. SELECT f1, f2, f3, last_value(f1) OVER (PARTITION BY f2 ORDER BY f1 DESC, f3 DESC ROWS BETWEEN 0 PRECEDING AND CURRENT ROW)
  4030. FROM t
  4031. ORDER BY f2, f3, f1, last_value
  4032. ----
  4033. 1 a 1 1
  4034. 2 a 2 2
  4035. 2 a 3 2
  4036. 3 a 4 3
  4037. 4 b 5 4
  4038. 4 b 6 4
  4039. 1 c 7 1
  4040. 2 c 8 2
  4041. 3 c 9 3
  4042. 7 d 10 7
  4043. query ITII
  4044. SELECT f1, f2, f3, last_value(f1) OVER (PARTITION BY f2 ORDER BY f1 DESC, f3 DESC ROWS BETWEEN 1 PRECEDING AND CURRENT ROW)
  4045. FROM t
  4046. ORDER BY f2, f3, f1, last_value
  4047. ----
  4048. 1 a 1 1
  4049. 2 a 2 2
  4050. 2 a 3 2
  4051. 3 a 4 3
  4052. 4 b 5 4
  4053. 4 b 6 4
  4054. 1 c 7 1
  4055. 2 c 8 2
  4056. 3 c 9 3
  4057. 7 d 10 7
  4058. query ITII
  4059. SELECT f1, f2, f3, last_value(f1) OVER (PARTITION BY f2 ORDER BY f1 DESC, f3 DESC ROWS BETWEEN 2 PRECEDING AND CURRENT ROW)
  4060. FROM t
  4061. ORDER BY f2, f3, f1, last_value
  4062. ----
  4063. 1 a 1 1
  4064. 2 a 2 2
  4065. 2 a 3 2
  4066. 3 a 4 3
  4067. 4 b 5 4
  4068. 4 b 6 4
  4069. 1 c 7 1
  4070. 2 c 8 2
  4071. 3 c 9 3
  4072. 7 d 10 7
  4073. query ITII
  4074. SELECT f1, f2, f3, last_value(f1) OVER (PARTITION BY f2 ORDER BY f1 DESC, f3 DESC ROWS BETWEEN 10 PRECEDING AND CURRENT ROW)
  4075. FROM t
  4076. ORDER BY f2, f3, f1, last_value
  4077. ----
  4078. 1 a 1 1
  4079. 2 a 2 2
  4080. 2 a 3 2
  4081. 3 a 4 3
  4082. 4 b 5 4
  4083. 4 b 6 4
  4084. 1 c 7 1
  4085. 2 c 8 2
  4086. 3 c 9 3
  4087. 7 d 10 7
  4088. # ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
  4089. # Always returns current row
  4090. query ITII
  4091. SELECT f1, f2, f3, last_value(f1) OVER (PARTITION BY f2 ORDER BY f1 DESC, f3 DESC ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
  4092. FROM t
  4093. ORDER BY f2, f3, f1, last_value
  4094. ----
  4095. 1 a 1 1
  4096. 2 a 2 2
  4097. 2 a 3 2
  4098. 3 a 4 3
  4099. 4 b 5 4
  4100. 4 b 6 4
  4101. 1 c 7 1
  4102. 2 c 8 2
  4103. 3 c 9 3
  4104. 7 d 10 7
  4105. # ROWS BETWEEN x PRECEDING AND 0 PRECEDING
  4106. query ITII
  4107. SELECT f1, f2, f3, last_value(f1) OVER (PARTITION BY f2 ORDER BY f1 DESC, f3 DESC ROWS BETWEEN 1 PRECEDING AND 0 PRECEDING)
  4108. FROM t
  4109. ORDER BY f2, f3, f1, last_value
  4110. ----
  4111. 1 a 1 1
  4112. 2 a 2 2
  4113. 2 a 3 2
  4114. 3 a 4 3
  4115. 4 b 5 4
  4116. 4 b 6 4
  4117. 1 c 7 1
  4118. 2 c 8 2
  4119. 3 c 9 3
  4120. 7 d 10 7
  4121. query ITII
  4122. SELECT f1, f2, f3, last_value(f1) OVER (PARTITION BY f2 ORDER BY f1 DESC, f3 DESC ROWS BETWEEN 2 PRECEDING AND 0 PRECEDING)
  4123. FROM t
  4124. ORDER BY f2, f3, f1, last_value
  4125. ----
  4126. 1 a 1 1
  4127. 2 a 2 2
  4128. 2 a 3 2
  4129. 3 a 4 3
  4130. 4 b 5 4
  4131. 4 b 6 4
  4132. 1 c 7 1
  4133. 2 c 8 2
  4134. 3 c 9 3
  4135. 7 d 10 7
  4136. query ITII
  4137. SELECT f1, f2, f3, last_value(f1) OVER (PARTITION BY f2 ORDER BY f1 DESC, f3 DESC ROWS BETWEEN 10 PRECEDING AND 0 PRECEDING)
  4138. FROM t
  4139. ORDER BY f2, f3, f1, last_value
  4140. ----
  4141. 1 a 1 1
  4142. 2 a 2 2
  4143. 2 a 3 2
  4144. 3 a 4 3
  4145. 4 b 5 4
  4146. 4 b 6 4
  4147. 1 c 7 1
  4148. 2 c 8 2
  4149. 3 c 9 3
  4150. 7 d 10 7
  4151. # ROWS BETWEEN 0 PRECEDING AND x PRECEDING
  4152. query ITII
  4153. SELECT f1, f2, f3, last_value(f1) OVER (PARTITION BY f2 ORDER BY f1 DESC, f3 DESC ROWS BETWEEN 0 PRECEDING AND 1 PRECEDING)
  4154. FROM t
  4155. ORDER BY f2, f3, f1, last_value
  4156. ----
  4157. 1 a 1 NULL
  4158. 2 a 2 NULL
  4159. 2 a 3 NULL
  4160. 3 a 4 NULL
  4161. 4 b 5 NULL
  4162. 4 b 6 NULL
  4163. 1 c 7 NULL
  4164. 2 c 8 NULL
  4165. 3 c 9 NULL
  4166. 7 d 10 NULL
  4167. query ITII
  4168. SELECT f1, f2, f3, last_value(f1) OVER (PARTITION BY f2 ORDER BY f1 DESC, f3 DESC ROWS BETWEEN 0 PRECEDING AND 2 PRECEDING)
  4169. FROM t
  4170. ORDER BY f2, f3, f1, last_value
  4171. ----
  4172. 1 a 1 NULL
  4173. 2 a 2 NULL
  4174. 2 a 3 NULL
  4175. 3 a 4 NULL
  4176. 4 b 5 NULL
  4177. 4 b 6 NULL
  4178. 1 c 7 NULL
  4179. 2 c 8 NULL
  4180. 3 c 9 NULL
  4181. 7 d 10 NULL
  4182. query ITII
  4183. SELECT f1, f2, f3, last_value(f1) OVER (PARTITION BY f2 ORDER BY f1 DESC, f3 DESC ROWS BETWEEN 0 PRECEDING AND 10 PRECEDING)
  4184. FROM t
  4185. ORDER BY f2, f3, f1, last_value
  4186. ----
  4187. 1 a 1 NULL
  4188. 2 a 2 NULL
  4189. 2 a 3 NULL
  4190. 3 a 4 NULL
  4191. 4 b 5 NULL
  4192. 4 b 6 NULL
  4193. 1 c 7 NULL
  4194. 2 c 8 NULL
  4195. 3 c 9 NULL
  4196. 7 d 10 NULL
  4197. # ROWS BETWEEN y PRECEDING AND x PRECEDING, where x < y
  4198. query ITII
  4199. SELECT f1, f2, f3, last_value(f1) OVER (PARTITION BY f2 ORDER BY f1 DESC, f3 DESC ROWS BETWEEN 1 PRECEDING AND 0 PRECEDING)
  4200. FROM t
  4201. ORDER BY f2, f3, f1, last_value
  4202. ----
  4203. 1 a 1 1
  4204. 2 a 2 2
  4205. 2 a 3 2
  4206. 3 a 4 3
  4207. 4 b 5 4
  4208. 4 b 6 4
  4209. 1 c 7 1
  4210. 2 c 8 2
  4211. 3 c 9 3
  4212. 7 d 10 7
  4213. query ITII
  4214. SELECT f1, f2, f3, last_value(f1) OVER (PARTITION BY f2 ORDER BY f1 DESC, f3 DESC ROWS BETWEEN 2 PRECEDING AND 1 PRECEDING)
  4215. FROM t
  4216. ORDER BY f2, f3, f1, last_value
  4217. ----
  4218. 1 a 1 2
  4219. 2 a 2 2
  4220. 2 a 3 3
  4221. 3 a 4 NULL
  4222. 4 b 5 4
  4223. 4 b 6 NULL
  4224. 1 c 7 2
  4225. 2 c 8 3
  4226. 3 c 9 NULL
  4227. 7 d 10 NULL
  4228. # ROWS BETWEEN y PRECEDING AND x PRECEDING, where x > y
  4229. query ITII
  4230. SELECT f1, f2, f3, last_value(f1) OVER (PARTITION BY f2 ORDER BY f1 DESC, f3 DESC ROWS BETWEEN 0 PRECEDING AND 1 PRECEDING)
  4231. FROM t
  4232. ORDER BY f2, f3, f1, last_value
  4233. ----
  4234. 1 a 1 NULL
  4235. 2 a 2 NULL
  4236. 2 a 3 NULL
  4237. 3 a 4 NULL
  4238. 4 b 5 NULL
  4239. 4 b 6 NULL
  4240. 1 c 7 NULL
  4241. 2 c 8 NULL
  4242. 3 c 9 NULL
  4243. 7 d 10 NULL
  4244. query ITII
  4245. SELECT f1, f2, f3, last_value(f1) OVER (PARTITION BY f2 ORDER BY f1 DESC, f3 DESC ROWS BETWEEN 1 PRECEDING AND 2 PRECEDING)
  4246. FROM t
  4247. ORDER BY f2, f3, f1, last_value
  4248. ----
  4249. 1 a 1 NULL
  4250. 2 a 2 NULL
  4251. 2 a 3 NULL
  4252. 3 a 4 NULL
  4253. 4 b 5 NULL
  4254. 4 b 6 NULL
  4255. 1 c 7 NULL
  4256. 2 c 8 NULL
  4257. 3 c 9 NULL
  4258. 7 d 10 NULL
  4259. # ROWS BETWEEN y PRECEDING AND x PRECEDING, where x == y
  4260. query ITII
  4261. SELECT f1, f2, f3, last_value(f1) OVER (PARTITION BY f2 ORDER BY f1 DESC, f3 DESC ROWS BETWEEN 0 PRECEDING AND 0 PRECEDING)
  4262. FROM t
  4263. ORDER BY f2, f3, f1, last_value
  4264. ----
  4265. 1 a 1 1
  4266. 2 a 2 2
  4267. 2 a 3 2
  4268. 3 a 4 3
  4269. 4 b 5 4
  4270. 4 b 6 4
  4271. 1 c 7 1
  4272. 2 c 8 2
  4273. 3 c 9 3
  4274. 7 d 10 7
  4275. query ITII
  4276. SELECT f1, f2, f3, last_value(f1) OVER (PARTITION BY f2 ORDER BY f1 DESC, f3 DESC ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING)
  4277. FROM t
  4278. ORDER BY f2, f3, f1, last_value
  4279. ----
  4280. 1 a 1 2
  4281. 2 a 2 2
  4282. 2 a 3 3
  4283. 3 a 4 NULL
  4284. 4 b 5 4
  4285. 4 b 6 NULL
  4286. 1 c 7 2
  4287. 2 c 8 3
  4288. 3 c 9 NULL
  4289. 7 d 10 NULL
  4290. query ITII
  4291. SELECT f1, f2, f3, last_value(f1) OVER (PARTITION BY f2 ORDER BY f1 DESC, f3 DESC ROWS BETWEEN 2 PRECEDING AND 2 PRECEDING)
  4292. FROM t
  4293. ORDER BY f2, f3, f1, last_value
  4294. ----
  4295. 1 a 1 2
  4296. 2 a 2 3
  4297. 2 a 3 NULL
  4298. 3 a 4 NULL
  4299. 4 b 5 NULL
  4300. 4 b 6 NULL
  4301. 1 c 7 3
  4302. 2 c 8 NULL
  4303. 3 c 9 NULL
  4304. 7 d 10 NULL
  4305. query ITII
  4306. SELECT f1, f2, f3, last_value(f1) OVER (PARTITION BY f2 ORDER BY f1 DESC, f3 DESC ROWS BETWEEN 10 PRECEDING AND 10 PRECEDING)
  4307. FROM t
  4308. ORDER BY f2, f3, f1, last_value
  4309. ----
  4310. 1 a 1 NULL
  4311. 2 a 2 NULL
  4312. 2 a 3 NULL
  4313. 3 a 4 NULL
  4314. 4 b 5 NULL
  4315. 4 b 6 NULL
  4316. 1 c 7 NULL
  4317. 2 c 8 NULL
  4318. 3 c 9 NULL
  4319. 7 d 10 NULL
  4320. # ROWS BETWEEN UNBOUNDED PRECEDING AND x PRECEDING
  4321. query ITII
  4322. SELECT f1, f2, f3, last_value(f1) OVER (PARTITION BY f2 ORDER BY f1 DESC, f3 DESC ROWS BETWEEN UNBOUNDED PRECEDING AND 0 PRECEDING)
  4323. FROM t
  4324. ORDER BY f2, f3, f1, last_value
  4325. ----
  4326. 1 a 1 1
  4327. 2 a 2 2
  4328. 2 a 3 2
  4329. 3 a 4 3
  4330. 4 b 5 4
  4331. 4 b 6 4
  4332. 1 c 7 1
  4333. 2 c 8 2
  4334. 3 c 9 3
  4335. 7 d 10 7
  4336. query ITII
  4337. SELECT f1, f2, f3, last_value(f1) OVER (PARTITION BY f2 ORDER BY f1 DESC, f3 DESC ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING)
  4338. FROM t
  4339. ORDER BY f2, f3, f1, last_value
  4340. ----
  4341. 1 a 1 2
  4342. 2 a 2 2
  4343. 2 a 3 3
  4344. 3 a 4 NULL
  4345. 4 b 5 4
  4346. 4 b 6 NULL
  4347. 1 c 7 2
  4348. 2 c 8 3
  4349. 3 c 9 NULL
  4350. 7 d 10 NULL
  4351. query ITII
  4352. SELECT f1, f2, f3, last_value(f1) OVER (PARTITION BY f2 ORDER BY f1 DESC, f3 DESC ROWS BETWEEN UNBOUNDED PRECEDING AND 2 PRECEDING)
  4353. FROM t
  4354. ORDER BY f2, f3, f1, last_value
  4355. ----
  4356. 1 a 1 2
  4357. 2 a 2 3
  4358. 2 a 3 NULL
  4359. 3 a 4 NULL
  4360. 4 b 5 NULL
  4361. 4 b 6 NULL
  4362. 1 c 7 3
  4363. 2 c 8 NULL
  4364. 3 c 9 NULL
  4365. 7 d 10 NULL
  4366. query ITII
  4367. SELECT f1, f2, f3, last_value(f1) OVER (PARTITION BY f2 ORDER BY f1 DESC, f3 DESC ROWS BETWEEN UNBOUNDED PRECEDING AND 10 PRECEDING)
  4368. FROM t
  4369. ORDER BY f2, f3, f1, last_value
  4370. ----
  4371. 1 a 1 NULL
  4372. 2 a 2 NULL
  4373. 2 a 3 NULL
  4374. 3 a 4 NULL
  4375. 4 b 5 NULL
  4376. 4 b 6 NULL
  4377. 1 c 7 NULL
  4378. 2 c 8 NULL
  4379. 3 c 9 NULL
  4380. 7 d 10 NULL
  4381. # Test near-overflow behavior on offsets
  4382. # u64::MAX FOLLOWING
  4383. # u64::MAX PRECEDING
  4384. query error db error: ERROR: Window frame offsets greater than 1000000 are currently not supported
  4385. SELECT f1, f2, f3, last_value(f1) OVER (PARTITION BY f2 ORDER BY f1 DESC, f3 DESC ROWS BETWEEN 18446744073709551615 FOLLOWING AND 18446744073709551615 FOLLOWING)
  4386. FROM t
  4387. ORDER BY f2, f3, f1, last_value
  4388. query error db error: ERROR: Window frame offsets greater than 1000000 are currently not supported
  4389. SELECT f1, f2, f3, last_value(f1) OVER (PARTITION BY f2 ORDER BY f1 DESC, f3 DESC ROWS BETWEEN 18446744073709551614 FOLLOWING AND 18446744073709551615 FOLLOWING)
  4390. FROM t
  4391. ORDER BY f2, f3, f1, last_value
  4392. query ITII
  4393. SELECT f1, f2, f3, last_value(f1) OVER (PARTITION BY f2 ORDER BY f1 DESC, f3 DESC ROWS BETWEEN 1000 FOLLOWING AND 1000 FOLLOWING)
  4394. FROM t
  4395. ORDER BY f2, f3, f1, last_value
  4396. ----
  4397. 1 a 1 NULL
  4398. 2 a 2 NULL
  4399. 2 a 3 NULL
  4400. 3 a 4 NULL
  4401. 4 b 5 NULL
  4402. 4 b 6 NULL
  4403. 1 c 7 NULL
  4404. 2 c 8 NULL
  4405. 3 c 9 NULL
  4406. 7 d 10 NULL
  4407. query ITII
  4408. SELECT f1, f2, f3, last_value(f1) OVER (PARTITION BY f2 ORDER BY f1 DESC, f3 DESC ROWS BETWEEN 1000 FOLLOWING AND 1001 FOLLOWING)
  4409. FROM t
  4410. ORDER BY f2, f3, f1, last_value
  4411. ----
  4412. 1 a 1 NULL
  4413. 2 a 2 NULL
  4414. 2 a 3 NULL
  4415. 3 a 4 NULL
  4416. 4 b 5 NULL
  4417. 4 b 6 NULL
  4418. 1 c 7 NULL
  4419. 2 c 8 NULL
  4420. 3 c 9 NULL
  4421. 7 d 10 NULL
  4422. # reduce_elision code path
  4423. # Using the same table as first_value
  4424. # Default frame, includes current row
  4425. query II
  4426. SELECT f1, last_value(f1) OVER (PARTITION BY f1)
  4427. FROM t5
  4428. GROUP BY f1
  4429. ----
  4430. 1 1
  4431. # Frame ending at UNBOUNDED FOLLOWING
  4432. query II
  4433. SELECT f1, last_value(f1) OVER (PARTITION BY f1 ROWS BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING)
  4434. FROM t5
  4435. GROUP BY f1
  4436. ----
  4437. 1 NULL
  4438. query II
  4439. SELECT f1, last_value(f1) OVER (PARTITION BY f1 ROWS BETWEEN 0 FOLLOWING AND UNBOUNDED FOLLOWING)
  4440. FROM t5
  4441. GROUP BY f1
  4442. ----
  4443. 1 1
  4444. query II
  4445. SELECT f1, last_value(f1) OVER (PARTITION BY f1 ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
  4446. FROM t5
  4447. GROUP BY f1
  4448. ----
  4449. 1 1
  4450. query II
  4451. SELECT f1, last_value(f1) OVER (PARTITION BY f1 ROWS BETWEEN 0 PRECEDING AND UNBOUNDED FOLLOWING)
  4452. FROM t5
  4453. GROUP BY f1
  4454. ----
  4455. 1 1
  4456. query II
  4457. SELECT f1, last_value(f1) OVER (PARTITION BY f1 ROWS BETWEEN 1 PRECEDING AND UNBOUNDED FOLLOWING)
  4458. FROM t5
  4459. GROUP BY f1
  4460. ----
  4461. 1 1
  4462. query II
  4463. SELECT f1, last_value(f1) OVER (PARTITION BY f1 ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)
  4464. FROM t5
  4465. GROUP BY f1
  4466. ----
  4467. 1 1
  4468. # Frame ending at 1 FOLLOWING
  4469. query II
  4470. SELECT f1, last_value(f1) OVER (PARTITION BY f1 ROWS BETWEEN 1 FOLLOWING AND 1 FOLLOWING)
  4471. FROM t5
  4472. GROUP BY f1
  4473. ----
  4474. 1 NULL
  4475. query II
  4476. SELECT f1, last_value(f1) OVER (PARTITION BY f1 ROWS BETWEEN 0 FOLLOWING AND 1 FOLLOWING)
  4477. FROM t5
  4478. GROUP BY f1
  4479. ----
  4480. 1 1
  4481. query II
  4482. SELECT f1, last_value(f1) OVER (PARTITION BY f1 ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING)
  4483. FROM t5
  4484. GROUP BY f1
  4485. ----
  4486. 1 1
  4487. query II
  4488. SELECT f1, last_value(f1) OVER (PARTITION BY f1 ROWS BETWEEN 0 PRECEDING AND 1 FOLLOWING)
  4489. FROM t5
  4490. GROUP BY f1
  4491. ----
  4492. 1 1
  4493. query II
  4494. SELECT f1, last_value(f1) OVER (PARTITION BY f1 ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING)
  4495. FROM t5
  4496. GROUP BY f1
  4497. ----
  4498. 1 1
  4499. query II
  4500. SELECT f1, last_value(f1) OVER (PARTITION BY f1 ROWS BETWEEN UNBOUNDED PRECEDING AND 1 FOLLOWING)
  4501. FROM t5
  4502. GROUP BY f1
  4503. ----
  4504. 1 1
  4505. # Frame ending at 0 FOLLOWING
  4506. query II
  4507. SELECT f1, last_value(f1) OVER (PARTITION BY f1 ROWS BETWEEN 1 FOLLOWING AND 0 FOLLOWING)
  4508. FROM t5
  4509. GROUP BY f1
  4510. ----
  4511. 1 NULL
  4512. query II
  4513. SELECT f1, last_value(f1) OVER (PARTITION BY f1 ROWS BETWEEN 0 FOLLOWING AND 0 FOLLOWING)
  4514. FROM t5
  4515. GROUP BY f1
  4516. ----
  4517. 1 1
  4518. query II
  4519. SELECT f1, last_value(f1) OVER (PARTITION BY f1 ROWS BETWEEN CURRENT ROW AND 0 FOLLOWING)
  4520. FROM t5
  4521. GROUP BY f1
  4522. ----
  4523. 1 1
  4524. query II
  4525. SELECT f1, last_value(f1) OVER (PARTITION BY f1 ROWS BETWEEN 0 PRECEDING AND 0 FOLLOWING)
  4526. FROM t5
  4527. GROUP BY f1
  4528. ----
  4529. 1 1
  4530. query II
  4531. SELECT f1, last_value(f1) OVER (PARTITION BY f1 ROWS BETWEEN 1 PRECEDING AND 0 FOLLOWING)
  4532. FROM t5
  4533. GROUP BY f1
  4534. ----
  4535. 1 1
  4536. query II
  4537. SELECT f1, last_value(f1) OVER (PARTITION BY f1 ROWS BETWEEN UNBOUNDED PRECEDING AND 0 FOLLOWING)
  4538. FROM t5
  4539. GROUP BY f1
  4540. ----
  4541. 1 1
  4542. # Frame ending at CURRENT ROW
  4543. query II
  4544. SELECT f1, last_value(f1) OVER (PARTITION BY f1 ROWS BETWEEN CURRENT ROW AND CURRENT ROW)
  4545. FROM t5
  4546. GROUP BY f1
  4547. ----
  4548. 1 1
  4549. query II
  4550. SELECT f1, last_value(f1) OVER (PARTITION BY f1 ROWS BETWEEN 0 PRECEDING AND CURRENT ROW)
  4551. FROM t5
  4552. GROUP BY f1
  4553. ----
  4554. 1 1
  4555. query II
  4556. SELECT f1, last_value(f1) OVER (PARTITION BY f1 ROWS BETWEEN 1 PRECEDING AND CURRENT ROW)
  4557. FROM t5
  4558. GROUP BY f1
  4559. ----
  4560. 1 1
  4561. query II
  4562. SELECT f1, last_value(f1) OVER (PARTITION BY f1 ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
  4563. FROM t5
  4564. GROUP BY f1
  4565. ----
  4566. 1 1
  4567. # Frame ending at 0 PRECEDING
  4568. query II
  4569. SELECT f1, last_value(f1) OVER (PARTITION BY f1 ROWS BETWEEN 0 PRECEDING AND 0 PRECEDING)
  4570. FROM t5
  4571. GROUP BY f1
  4572. ----
  4573. 1 1
  4574. query II
  4575. SELECT f1, last_value(f1) OVER (PARTITION BY f1 ROWS BETWEEN 1 PRECEDING AND 0 PRECEDING)
  4576. FROM t5
  4577. GROUP BY f1
  4578. ----
  4579. 1 1
  4580. query II
  4581. SELECT f1, last_value(f1) OVER (PARTITION BY f1 ROWS BETWEEN UNBOUNDED PRECEDING AND 0 PRECEDING)
  4582. FROM t5
  4583. GROUP BY f1
  4584. ----
  4585. 1 1
  4586. # Frame ending at 1 PRECEDING
  4587. query II
  4588. SELECT f1, last_value(f1) OVER (PARTITION BY f1 ROWS BETWEEN 0 PRECEDING AND 1 PRECEDING)
  4589. FROM t5
  4590. GROUP BY f1
  4591. ----
  4592. 1 NULL
  4593. query II
  4594. SELECT f1, last_value(f1) OVER (PARTITION BY f1 ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING)
  4595. FROM t5
  4596. GROUP BY f1
  4597. ----
  4598. 1 NULL
  4599. query II
  4600. SELECT f1, last_value(f1) OVER (PARTITION BY f1 ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING)
  4601. FROM t5
  4602. GROUP BY f1
  4603. ----
  4604. 1 NULL
  4605. # Check some HIR plans to verify that the lifting of window functions to the top of Maps is actually happening.
  4606. statement ok
  4607. CREATE TABLE foo (
  4608. a int,
  4609. b text
  4610. );
  4611. statement ok
  4612. INSERT INTO foo (a, b) VALUES (0, 'zero'), (1, 'one'), (2, 'two'), (3, 'three');
  4613. query T multiline
  4614. EXPLAIN RAW PLAN FOR
  4615. SELECT 3 + lag(a) OVER (ORDER BY a) + 5 + 27
  4616. FROM foo;
  4617. ----
  4618. Project (#3)
  4619. Map (lag(row(#0{a}, 1, null)) over (order by [#0{a} asc nulls_last]), (((3 + #2{?column?}) + 5) + 27))
  4620. Get materialize.public.foo
  4621. Target cluster: quickstart
  4622. EOF
  4623. query T multiline
  4624. EXPLAIN RAW PLAN FOR
  4625. SELECT -lag(10*right_a+3) OVER (ORDER BY right_a NULLS FIRST), -sum(foo.a), b
  4626. FROM foo LEFT JOIN (SELECT a AS right_a FROM foo WHERE a<2) ON foo.a = right_a
  4627. GROUP BY b, right_a;
  4628. ----
  4629. Project (#4, #5, #0)
  4630. Map (lag(row(((10 * #1{right_a}) + 3), 1, null)) over (order by [#1{right_a} asc nulls_first]), -(#3{?column?}), -(#2{?column?}))
  4631. Reduce group_by=[#3, #4] aggregates=[sum(#0{a})]
  4632. Map (#1{b}, #2{right_a})
  4633. LeftOuterJoin (#0{a} = #2{right_a})
  4634. Get materialize.public.foo
  4635. Project (#0)
  4636. Filter (#0{a} < 2)
  4637. Get materialize.public.foo
  4638. Target cluster: quickstart
  4639. EOF
  4640. query T multiline
  4641. EXPLAIN RAW PLAN FOR
  4642. SELECT 3 + lag(a) OVER (ORDER BY a) AS o
  4643. FROM foo
  4644. ORDER BY o;
  4645. ----
  4646. Finish order_by=[#0 asc nulls_last] output=[#0]
  4647. Project (#3)
  4648. Map (lag(row(#0{a}, 1, null)) over (order by [#0{a} asc nulls_last]), (3 + #2{?column?}))
  4649. Get materialize.public.foo
  4650. Target cluster: quickstart
  4651. EOF
  4652. query T multiline
  4653. EXPLAIN RAW PLAN FOR
  4654. SELECT
  4655. a,
  4656. 3 + lag(a) OVER (ORDER BY a),
  4657. 3 + lag(a) OVER (ORDER BY a),
  4658. lag(a) OVER (ORDER BY a),
  4659. lead(a) OVER (ORDER BY a),
  4660. 5 + lag(a) OVER (ORDER BY a)
  4661. FROM foo
  4662. ORDER BY a;
  4663. ----
  4664. Finish order_by=[#0 asc nulls_last] output=[#0..=#5]
  4665. Project (#0, #4, #4, #2, #3, #5)
  4666. Map (lag(row(#0{a}, 1, null)) over (order by [#0{a} asc nulls_last]), lead(row(#0{a}, 1, null)) over (order by [#0{a} asc nulls_last]), (3 + #2{?column?}), (5 + #2{?column?}))
  4667. Get materialize.public.foo
  4668. Target cluster: quickstart
  4669. EOF
  4670. query IIIIII nosort
  4671. SELECT
  4672. a,
  4673. 3 + lag(a) OVER (ORDER BY a),
  4674. 3 + lag(a) OVER (ORDER BY a),
  4675. lag(a) OVER (ORDER BY a),
  4676. lead(a) OVER (ORDER BY a),
  4677. 5 + lag(a) OVER (ORDER BY a)
  4678. FROM foo
  4679. ORDER BY a;
  4680. ----
  4681. 0 NULL NULL NULL 1 NULL
  4682. 1 3 3 0 2 5
  4683. 2 4 4 1 3 6
  4684. 3 5 5 2 NULL 7
  4685. # Two window function calls in the same HirScalarExpr, but both calls are the same
  4686. query T multiline
  4687. EXPLAIN RAW PLAN FOR
  4688. SELECT a, b, lag(a) OVER (ORDER BY a) + lag(a) OVER (ORDER BY a)
  4689. FROM foo
  4690. ORDER BY a;
  4691. ----
  4692. Finish order_by=[#0 asc nulls_last] output=[#0..=#2]
  4693. Project (#0, #1, #3)
  4694. Map (lag(row(#0{a}, 1, null)) over (order by [#0{a} asc nulls_last]), (#2{?column?} + #2{?column?}))
  4695. Get materialize.public.foo
  4696. Target cluster: quickstart
  4697. EOF
  4698. query ITI nosort
  4699. SELECT a, b, lag(a) OVER (ORDER BY a) + lag(a) OVER (ORDER BY a)
  4700. FROM foo
  4701. ORDER BY a;
  4702. ----
  4703. 0 zero NULL
  4704. 1 one 0
  4705. 2 two 2
  4706. 3 three 4
  4707. # Two window function calls in the same HirScalarExpr, and they are different
  4708. query T multiline
  4709. EXPLAIN RAW PLAN FOR
  4710. SELECT a, b, lag(a) OVER (ORDER BY a) + lead(a) OVER (ORDER BY a)
  4711. FROM foo
  4712. ORDER BY a;
  4713. ----
  4714. Finish order_by=[#0 asc nulls_last] output=[#0..=#2]
  4715. Project (#0, #1, #4)
  4716. Map (lead(row(#0{a}, 1, null)) over (order by [#0{a} asc nulls_last]), lag(row(#0{a}, 1, null)) over (order by [#0{a} asc nulls_last]), (#3{?column?} + #2{?column?}))
  4717. Get materialize.public.foo
  4718. Target cluster: quickstart
  4719. EOF
  4720. query ITI nosort
  4721. SELECT a, b, lag(a) OVER (ORDER BY a) + lead(a) OVER (ORDER BY a)
  4722. FROM foo
  4723. ORDER BY a;
  4724. ----
  4725. 0 zero NULL
  4726. 1 one 2
  4727. 2 two 4
  4728. 3 three NULL
  4729. # Window function in DISTINCT ON
  4730. query T multiline
  4731. EXPLAIN RAW PLAN FOR
  4732. SELECT DISTINCT ON(1 + lag(a, 1, 0) OVER (ORDER BY a)) *
  4733. FROM foo;
  4734. ----
  4735. Project (#0, #1)
  4736. TopK group_by=[#3] limit=1
  4737. Map (lag(row(#0{a}, 1, 0)) over (order by [#0{a} asc nulls_last]), (1 + #2{?column?}))
  4738. Get materialize.public.foo
  4739. Target cluster: quickstart
  4740. EOF
  4741. query IIT
  4742. SELECT 1 + lag(a, 1, 0) OVER (ORDER BY a), *
  4743. FROM foo
  4744. ORDER BY a;
  4745. ----
  4746. 1 0 zero
  4747. 1 1 one
  4748. 2 2 two
  4749. 3 3 three
  4750. query IT
  4751. SELECT DISTINCT ON(1 + lag(a, 1, 0) OVER (ORDER BY a)) *
  4752. FROM foo
  4753. ORDER BY 1 + lag(a, 1, 0) OVER (ORDER BY a);
  4754. ----
  4755. 0 zero
  4756. 2 two
  4757. 3 three
  4758. query IIT nosort
  4759. SELECT 1 + lag(a, 1, 0) OVER (ORDER BY a), *
  4760. FROM foo
  4761. ORDER BY 1 + lag(a, 1, 0) OVER (ORDER BY a), a ASC;
  4762. ----
  4763. 1 0 zero
  4764. 1 1 one
  4765. 2 2 two
  4766. 3 3 three
  4767. query IT nosort
  4768. SELECT DISTINCT ON(1 + lag(a, 1, 0) OVER (ORDER BY a)) *
  4769. FROM foo
  4770. ORDER BY 1 + lag(a, 1, 0) OVER (ORDER BY a), a ASC;
  4771. ----
  4772. 0 zero
  4773. 2 two
  4774. 3 three
  4775. query error db error: ERROR: SELECT DISTINCT ON expressions must match initial ORDER BY expressions
  4776. SELECT DISTINCT ON(1 + lag(a, 1, 0) OVER (ORDER BY a)) *
  4777. FROM foo
  4778. ORDER BY lag(a, 1, 0) OVER (ORDER BY a), a DESC;
  4779. query IIIT nosort
  4780. SELECT 1 + lag(a, 1, 0) OVER (ORDER BY a), lead(a) OVER (ORDER BY b) AS o, a, b
  4781. FROM foo
  4782. ORDER BY 1 + lag(a, 1, 0) OVER (ORDER BY a), o DESC;
  4783. ----
  4784. 1 NULL 0 zero
  4785. 1 3 1 one
  4786. 2 0 2 two
  4787. 3 2 3 three
  4788. query IIT nosort
  4789. SELECT DISTINCT ON(1 + lag(a, 1, 0) OVER (ORDER BY a)) lead(a) OVER (ORDER BY b) AS o, a, b
  4790. FROM foo
  4791. ORDER BY 1 + lag(a, 1, 0) OVER (ORDER BY a), o DESC;
  4792. ----
  4793. NULL 0 zero
  4794. 0 2 two
  4795. 2 3 three
  4796. query T multiline
  4797. EXPLAIN RAW PLAN FOR
  4798. SELECT DISTINCT ON(5 + lag(a) OVER (ORDER BY a)) lead(a) OVER (ORDER BY b) AS o
  4799. FROM foo
  4800. ORDER BY 5 + lag(a) OVER (ORDER BY a), o;
  4801. ----
  4802. Finish order_by=[#4 asc nulls_last, #3 asc nulls_last] output=[#3]
  4803. TopK group_by=[#4] order_by=[#3 asc nulls_last] limit=1
  4804. Map (lag(row(#0{a}, 1, null)) over (order by [#0{a} asc nulls_last]), lead(row(#0{a}, 1, null)) over (order by [#1{b} asc nulls_last]), (5 + #2{?column?}))
  4805. Get materialize.public.foo
  4806. Target cluster: quickstart
  4807. EOF
  4808. # Nested window function call in the argument of a window function call (Postgres doesn't allow this, but we do)
  4809. query ITI nosort
  4810. SELECT a, b, lag(lag(a) OVER (ORDER BY a)) OVER (ORDER BY a)
  4811. FROM foo
  4812. ORDER BY a;
  4813. ----
  4814. 0 zero NULL
  4815. 1 one NULL
  4816. 2 two 0
  4817. 3 three 1
  4818. query ITI nosort
  4819. SELECT a, b, lag(lag(a) OVER (PARTITION BY length(b) ORDER BY a)) OVER (ORDER BY a)
  4820. FROM foo
  4821. ORDER BY a;
  4822. ----
  4823. 0 zero NULL
  4824. 1 one NULL
  4825. 2 two NULL
  4826. 3 three 1
  4827. # The inner window function call also appears outside
  4828. query ITII nosort
  4829. SELECT a, b, lag(lag(a) OVER (ORDER BY a)) OVER (ORDER BY a), lag(a) OVER (ORDER BY a)
  4830. FROM foo
  4831. ORDER BY a;
  4832. ----
  4833. 0 zero NULL NULL
  4834. 1 one NULL 0
  4835. 2 two 0 1
  4836. 3 three 1 2
  4837. query ITII nosort
  4838. SELECT a, b, lag(a) OVER (ORDER BY a), lag(lag(a) OVER (ORDER BY a)) OVER (ORDER BY a)
  4839. FROM foo
  4840. ORDER BY a;
  4841. ----
  4842. 0 zero NULL NULL
  4843. 1 one 0 NULL
  4844. 2 two 1 0
  4845. 3 three 2 1
  4846. # Nested window function call in the PARTITION BY of a window function call (Postgres doesn't allow this, but we do)
  4847. query ITII nosort
  4848. SELECT a, b, lag(a) OVER (PARTITION BY lag(length(b)) OVER (ORDER BY a) ORDER BY a), lag(length(b)) OVER (ORDER BY a)
  4849. FROM foo
  4850. ORDER BY a;
  4851. ----
  4852. 0 zero NULL NULL
  4853. 1 one NULL 4
  4854. 2 two NULL 3
  4855. 3 three 2 3
  4856. query ITI nosort
  4857. SELECT a, b, lag(a) OVER (PARTITION BY lag(length(b)) OVER (ORDER BY a) ORDER BY a)
  4858. FROM foo
  4859. ORDER BY a;
  4860. ----
  4861. 0 zero NULL
  4862. 1 one NULL
  4863. 2 two NULL
  4864. 3 three 2
  4865. query ITII nosort
  4866. SELECT a, b, row_number() OVER (PARTITION BY lag(length(b)) OVER (ORDER BY a) ORDER BY a), lag(length(b)) OVER (ORDER BY a)
  4867. FROM foo
  4868. ORDER BY a;
  4869. ----
  4870. 0 zero 1 NULL
  4871. 1 one 1 4
  4872. 2 two 1 3
  4873. 3 three 2 3
  4874. # Nested window function calls in both the argument and the PARTITION BY of a window function call (Postgres doesn't
  4875. # allow this, but we do)
  4876. query ITI nosort
  4877. SELECT a, b, lead(lag(a) OVER (PARTITION BY lag(length(b)) OVER (ORDER BY a) ORDER BY a), 1, -5) OVER (ORDER BY a)
  4878. FROM foo
  4879. ORDER BY a;
  4880. ----
  4881. 0 zero NULL
  4882. 1 one NULL
  4883. 2 two 2
  4884. 3 three -5
  4885. # Nested window function calls in the ORDER BY of a window function call (Postgres doesn't allow this, but we do)
  4886. query ITII nosort
  4887. SELECT a, b, lag(a) OVER (ORDER BY lag(a) OVER (ORDER BY a)), lag(a) OVER (ORDER BY a)
  4888. FROM foo
  4889. ORDER BY lag(a) OVER (ORDER BY a);
  4890. ----
  4891. 1 one NULL 0
  4892. 2 two 1 1
  4893. 3 three 2 2
  4894. 0 zero 3 NULL
  4895. query ITI
  4896. SELECT a, b, lag(a) OVER (ORDER BY lag(a) OVER (ORDER BY a))
  4897. FROM foo
  4898. ORDER BY a;
  4899. ----
  4900. 0 zero 3
  4901. 1 one NULL
  4902. 2 two 1
  4903. 3 three 2
  4904. # Window function inside CASE WHEN
  4905. # Regression test for https://github.com/MaterializeInc/database-issues/issues/6250
  4906. statement ok
  4907. CREATE TABLE bools(cond bool, x int);
  4908. statement ok
  4909. INSERT INTO bools VALUES (true, 0), (false, 1), (false, 2);
  4910. # The window function call should be lifted outside of the If
  4911. query T multiline
  4912. EXPLAIN RAW PLAN FOR
  4913. SELECT
  4914. cond,
  4915. x,
  4916. CASE
  4917. WHEN cond THEN
  4918. first_value(x) OVER (ORDER BY x DESC)
  4919. ELSE
  4920. 42
  4921. END
  4922. FROM bools;
  4923. ----
  4924. Project (#0, #1, #3)
  4925. Map (first_value(#1{x}) over (order by [#1{x} desc nulls_first]), case when #0{cond} then #2{?column?} else 42 end)
  4926. Get materialize.public.bools
  4927. Target cluster: quickstart
  4928. EOF
  4929. query TII
  4930. SELECT
  4931. cond,
  4932. x,
  4933. CASE
  4934. WHEN cond THEN
  4935. first_value(x) OVER (ORDER BY x DESC)
  4936. ELSE
  4937. 42
  4938. END
  4939. FROM bools
  4940. ORDER BY x;
  4941. ----
  4942. true 0 2
  4943. false 1 42
  4944. false 2 42
  4945. statement ok
  4946. CREATE TABLE tcm(
  4947. chat_id int,
  4948. chat_message_id int,
  4949. message_tstamp date,
  4950. xxx_id int,
  4951. first_xxx_id int,
  4952. user_id int,
  4953. boolean_flag bool
  4954. );
  4955. statement ok
  4956. INSERT INTO tcm VALUES
  4957. (
  4958. 5814889,
  4959. 78687406,
  4960. '2023-08-04'::DATE,
  4961. 11111111,
  4962. null,
  4963. 22222221,
  4964. true
  4965. ),
  4966. (
  4967. 5814889,
  4968. 78707836,
  4969. '2023-08-05'::DATE,
  4970. 11111112,
  4971. 923095,
  4972. 22222222,
  4973. false
  4974. ),
  4975. (
  4976. 5814889,
  4977. 78708445,
  4978. '2023-08-06'::DATE,
  4979. 11111113,
  4980. 743482,
  4981. 22222223,
  4982. false
  4983. ),
  4984. (
  4985. 581488900,
  4986. 78687406,
  4987. '2023-08-04'::DATE,
  4988. 11111111,
  4989. null,
  4990. 22222221,
  4991. true
  4992. ),
  4993. (
  4994. 581488900,
  4995. 78707836,
  4996. '2023-08-05'::DATE,
  4997. 11111112,
  4998. 923095,
  4999. 22222222,
  5000. false
  5001. ),
  5002. (
  5003. 581488900,
  5004. 78708445,
  5005. '2023-08-06'::DATE,
  5006. 11111113,
  5007. 743482,
  5008. 22222223,
  5009. true
  5010. );
  5011. query IITIIIIT nosort
  5012. SELECT
  5013. tcm.chat_id AS chat_id,
  5014. tcm.chat_message_id AS chat_message_id,
  5015. tcm.message_tstamp AS message_tstamp,
  5016. tcm.first_xxx_id AS first_xxx_id,
  5017. lead(tcm.first_xxx_id) ignore nulls over (partition by tcm.chat_id order by tcm.message_tstamp) AS _lead,
  5018. lag(tcm.first_xxx_id) ignore nulls over (partition by tcm.chat_id order by tcm.message_tstamp) AS _lag,
  5019. CASE
  5020. WHEN tcm.boolean_flag THEN
  5021. coalesce(
  5022. lead(tcm.first_xxx_id) ignore nulls over (partition by tcm.chat_id order by tcm.message_tstamp),
  5023. lag(tcm.first_xxx_id) ignore nulls over (partition by tcm.chat_id order by tcm.message_tstamp),
  5024. tcm.xxx_id
  5025. )
  5026. ELSE tcm.user_id end AS xxx_id,
  5027. tcm.boolean_flag AS boolean_flag
  5028. FROM tcm
  5029. ORDER BY chat_id, message_tstamp;
  5030. ----
  5031. 5814889 78687406 2023-08-04 NULL 923095 NULL 923095 true
  5032. 5814889 78707836 2023-08-05 923095 743482 NULL 22222222 false
  5033. 5814889 78708445 2023-08-06 743482 NULL 923095 22222223 false
  5034. 581488900 78687406 2023-08-04 NULL 923095 NULL 923095 true
  5035. 581488900 78707836 2023-08-05 923095 743482 NULL 22222222 false
  5036. 581488900 78708445 2023-08-06 743482 NULL 923095 923095 true
  5037. query IITIIT nosort
  5038. SELECT
  5039. tcm.chat_id AS chat_id,
  5040. tcm.chat_message_id AS chat_message_id,
  5041. tcm.message_tstamp AS message_tstamp,
  5042. tcm.first_xxx_id AS first_xxx_id,
  5043. CASE
  5044. WHEN tcm.boolean_flag THEN
  5045. coalesce(
  5046. lead(tcm.first_xxx_id) ignore nulls over (partition by tcm.chat_id order by tcm.message_tstamp),
  5047. lag(tcm.first_xxx_id) ignore nulls over (partition by tcm.chat_id order by tcm.message_tstamp),
  5048. tcm.xxx_id
  5049. )
  5050. ELSE tcm.user_id end AS xxx_id,
  5051. tcm.boolean_flag AS boolean_flag
  5052. FROM tcm
  5053. ORDER BY chat_id, message_tstamp;
  5054. ----
  5055. 5814889 78687406 2023-08-04 NULL 923095 true
  5056. 5814889 78707836 2023-08-05 923095 22222222 false
  5057. 5814889 78708445 2023-08-06 743482 22222223 false
  5058. 581488900 78687406 2023-08-04 NULL 923095 true
  5059. 581488900 78707836 2023-08-05 923095 22222222 false
  5060. 581488900 78708445 2023-08-06 743482 923095 true
  5061. # Window func with (non-windowed) aggregations
  5062. query error db error: ERROR: window functions are not allowed in aggregate function \(function pg_catalog\.lag\)
  5063. EXPLAIN RAW PLAN FOR
  5064. SELECT sum(lag(a) OVER ())
  5065. FROM foo;
  5066. query III nosort
  5067. SELECT length(b), sum(a), lag(sum(a)) OVER (ORDER BY sum(a))
  5068. FROM foo
  5069. GROUP BY length(b)
  5070. ORDER BY sum(a), length(b);
  5071. ----
  5072. 4 0 NULL
  5073. 3 3 0
  5074. 5 3 3
  5075. query III nosort
  5076. SELECT length(b), sum(a), lag(sum(a)) OVER (ORDER BY length(b))
  5077. FROM foo
  5078. GROUP BY length(b)
  5079. ORDER BY length(b);
  5080. ----
  5081. 3 3 NULL
  5082. 4 0 3
  5083. 5 3 0
  5084. query III
  5085. SELECT * FROM
  5086. ((SELECT length(b), sum(a), lag(sum(a)) OVER (ORDER BY sum(a))
  5087. FROM foo
  5088. GROUP BY length(b))
  5089. UNION
  5090. (SELECT length(b), sum(a), lag(sum(a)) OVER (ORDER BY length(b))
  5091. FROM foo
  5092. GROUP BY length(b))) r(x, y, z)
  5093. ORDER BY x, y, z;
  5094. ----
  5095. 3 3 0
  5096. 3 3 NULL
  5097. 4 0 3
  5098. 4 0 NULL
  5099. 5 3 0
  5100. 5 3 3
  5101. query IIII nosort
  5102. SELECT *, row_number() OVER (PARTITION BY len ORDER BY sum DESC, lag NULLS FIRST) as row_num FROM (
  5103. (SELECT length(b) AS len, sum(a) AS sum, lag(sum(a)) OVER (ORDER BY sum(a)) AS lag
  5104. FROM foo
  5105. GROUP BY length(b))
  5106. UNION
  5107. (SELECT length(b), -1 + sum(a) + row_number() OVER (ORDER BY length(b)), lag(sum(a)) OVER (ORDER BY length(b))
  5108. FROM foo
  5109. GROUP BY length(b))
  5110. ) AS sq
  5111. ORDER BY len, row_num;
  5112. ----
  5113. 3 3 NULL 1
  5114. 3 3 0 2
  5115. 4 1 3 1
  5116. 4 0 NULL 2
  5117. 5 5 0 1
  5118. 5 3 3 2
  5119. ## Subqueries
  5120. # Correlated scalar subquery in the 2nd argument of lag
  5121. query ITI nosort
  5122. SELECT *,
  5123. lag(
  5124. outer_a,
  5125. (SELECT count(*) FROM foo WHERE length(b) = length(outer_b))::integer
  5126. ) OVER (ORDER BY outer_a)
  5127. FROM (
  5128. SELECT a AS outer_a, b AS outer_b
  5129. FROM foo
  5130. ) as fsq
  5131. ORDER BY outer_a;
  5132. ----
  5133. 0 zero NULL
  5134. 1 one NULL
  5135. 2 two 0
  5136. 3 three 2
  5137. # Add a correlated IN subquery at the 3rd argument of lag
  5138. query ITI nosort
  5139. SELECT *,
  5140. lag(
  5141. outer_a,
  5142. (SELECT count(*) FROM foo WHERE length(b) = length(outer_b))::integer,
  5143. CASE WHEN outer_a - 1 IN (SELECT a FrOM foo) THEN 100 ELSE 500 END
  5144. ) OVER (ORDER BY outer_a)
  5145. FROM (
  5146. SELECT a AS outer_a, b AS outer_b
  5147. FROM foo
  5148. ) as fsq
  5149. ORDER BY outer_a;
  5150. ----
  5151. 0 zero 500
  5152. 1 one 100
  5153. 2 two 0
  5154. 3 three 2
  5155. # Correlated subquery in the PARTITION BY.
  5156. # The same subquery is also present in the SELECT to make the output more human-friendly.
  5157. query IITI nosort
  5158. SELECT (SELECT count(*) FROM foo WHERE length(b) = length(outer_b)) as part, *,
  5159. lag(outer_a) OVER (
  5160. PARTITION BY (SELECT count(*) FROM foo WHERE length(b) = length(outer_b))
  5161. ORDER BY outer_a
  5162. )
  5163. FROM (
  5164. SELECT a AS outer_a, b AS outer_b
  5165. FROM foo
  5166. ) as fsq
  5167. ORDER BY part, outer_a;
  5168. ----
  5169. 1 0 zero NULL
  5170. 1 3 three 0
  5171. 2 1 one NULL
  5172. 2 2 two 1
  5173. # Same query as the previous, but the subquery is not present in the SELECT
  5174. query ITI nosort
  5175. SELECT *,
  5176. lag(outer_a) OVER (
  5177. PARTITION BY (SELECT count(*) FROM foo WHERE length(b) = length(outer_b))
  5178. ORDER BY outer_a
  5179. )
  5180. FROM (
  5181. SELECT a AS outer_a, b AS outer_b
  5182. FROM foo
  5183. ) as fsq
  5184. ORDER BY outer_a;
  5185. ----
  5186. 0 zero NULL
  5187. 1 one NULL
  5188. 2 two 1
  5189. 3 three 0
  5190. # Correlated subquery in the ORDER BY of a window function.
  5191. # The same subquery is also present in the SELECT to make the output more human-friendly.
  5192. query IITI nosort
  5193. SELECT (SELECT count(*) FROM foo WHERE length(outer_b) > a + 2) as ord, *,
  5194. lag(outer_a) OVER (
  5195. ORDER BY (SELECT count(*) FROM foo WHERE length(outer_b) > a + 2), outer_a
  5196. )
  5197. FROM (
  5198. SELECT a AS outer_a, b AS outer_b
  5199. FROM foo
  5200. ) as fsq
  5201. ORDER BY ord, outer_a;
  5202. ----
  5203. 1 1 one NULL
  5204. 1 2 two 1
  5205. 2 0 zero 2
  5206. 3 3 three 0
  5207. # Same query as the previous, but the subquery is not present in the SELECT
  5208. query ITI nosort
  5209. SELECT *,
  5210. lag(outer_a) OVER (
  5211. ORDER BY (SELECT count(*) FROM foo WHERE length(outer_b) > a + 2), outer_a
  5212. )
  5213. FROM (
  5214. SELECT a AS outer_a, b AS outer_b
  5215. FROM foo
  5216. ) as fsq
  5217. ORDER BY outer_a;
  5218. ----
  5219. 0 zero 2
  5220. 1 one NULL
  5221. 2 two 1
  5222. 3 three 0
  5223. # Window func in a correlated subquery, but the correlating column reference is outside the window function
  5224. query ITI nosort
  5225. SELECT *,
  5226. (
  5227. SELECT sum(lead_a)
  5228. FROM (
  5229. SELECT lead(a) OVER (ORDER BY a) AS lead_a
  5230. FROM foo
  5231. WHERE a <= outer_a AND a < 3
  5232. ) as fsq2
  5233. )
  5234. FROM (
  5235. SELECT a AS outer_a, b AS outer_b
  5236. FROM foo
  5237. ) as fsq
  5238. ORDER BY outer_a;
  5239. ----
  5240. 0 zero NULL
  5241. 1 one 1
  5242. 2 two 3
  5243. 3 three 3
  5244. # Similar to the previous one, but more window functions and aggregations
  5245. query ITI nosort
  5246. SELECT *,
  5247. (
  5248. SELECT sum(2 * w) + max(w)
  5249. FROM (
  5250. SELECT lead(a) OVER (ORDER BY a) + row_number() OVER (ORDER BY a) AS w
  5251. FROM foo
  5252. WHERE a <= outer_a AND a < 3
  5253. ) as fsq2
  5254. )
  5255. FROM (
  5256. SELECT a AS outer_a, b AS outer_b
  5257. FROM foo
  5258. ) as fsq
  5259. ORDER BY outer_a;
  5260. ----
  5261. 0 zero NULL
  5262. 1 one 6
  5263. 2 two 16
  5264. 3 three 16
  5265. # Window func in a correlated subquery, and the window function argument refers to the outer query
  5266. query ITI nosort
  5267. SELECT *,
  5268. (
  5269. SELECT sum(lead_a)
  5270. FROM (
  5271. SELECT lead(outer_a + a) OVER (ORDER BY a) AS lead_a
  5272. FROM foo
  5273. ) as fsq2
  5274. )
  5275. FROM (
  5276. SELECT a AS outer_a, b AS outer_b
  5277. FROM foo
  5278. ) as fsq
  5279. ORDER BY outer_a;
  5280. ----
  5281. 0 zero 6
  5282. 1 one 9
  5283. 2 two 12
  5284. 3 three 15
  5285. # Window func in a correlated subquery, and the window function's PARTITION BY refers to the outer query
  5286. query ITI
  5287. SELECT *,
  5288. (
  5289. SELECT sum(lead_a)
  5290. FROM (
  5291. SELECT 3 + lead(a) OVER (PARTITION BY 4 * a / (outer_a + length(outer_b)) ORDER BY a) AS lead_a
  5292. FROM foo
  5293. ) as fsq2
  5294. )
  5295. FROM (
  5296. (SELECT a AS outer_a, b AS outer_b
  5297. FROM foo)
  5298. UNION
  5299. (SELECT a + 2 AS outer_a, b AS outer_b
  5300. FROM foo)
  5301. UNION
  5302. (SELECT a + 5 AS outer_a, b AS outer_b
  5303. FROM foo)
  5304. ) as fsq
  5305. ORDER BY outer_a, outer_b;
  5306. ----
  5307. 0 zero NULL
  5308. 1 one NULL
  5309. 2 two 4
  5310. 2 zero 4
  5311. 3 one 4
  5312. 3 three 10
  5313. 4 two 10
  5314. 5 three 9
  5315. 5 zero 9
  5316. 6 one 9
  5317. 7 two 9
  5318. 8 three 15
  5319. # Let's check the HIR plan for the above query to see if the window function is lifted out from behind the `3 +`
  5320. query T multiline
  5321. EXPLAIN RAW PLAN FOR
  5322. SELECT *,
  5323. (
  5324. SELECT sum(lead_a)
  5325. FROM (
  5326. SELECT 3 + lead(a) OVER (PARTITION BY 4 * a / (outer_a + length(outer_b)) ORDER BY a) AS lead_a
  5327. FROM foo
  5328. ) as fsq2
  5329. )
  5330. FROM (
  5331. (SELECT a AS outer_a, b AS outer_b
  5332. FROM foo)
  5333. UNION
  5334. (SELECT a + 2 AS outer_a, b AS outer_b
  5335. FROM foo)
  5336. UNION
  5337. (SELECT a + 5 AS outer_a, b AS outer_b
  5338. FROM foo)
  5339. ) as fsq
  5340. ORDER BY outer_a, outer_b;
  5341. ----
  5342. Finish order_by=[#0 asc nulls_last, #1 asc nulls_last] output=[#0..=#2]
  5343. With
  5344. cte [l1 as subquery-1] =
  5345. Reduce aggregates=[sum(#0{lead_a})]
  5346. Project (#3)
  5347. Map (lead(row(#0{a}, 1, null)) over (partition by [((4 * #0{a}) / (#^0{outer_a} + char_length(#^1{outer_b})))] order by [#0{a} asc nulls_last]), (3 + #2{?column?}))
  5348. Get materialize.public.foo
  5349. Return
  5350. Map (select(Get l1))
  5351. Distinct
  5352. Union
  5353. Distinct
  5354. Union
  5355. Get materialize.public.foo
  5356. Project (#2, #1)
  5357. Map ((#0{a} + 2))
  5358. Get materialize.public.foo
  5359. Project (#2, #1)
  5360. Map ((#0{a} + 5))
  5361. Get materialize.public.foo
  5362. Target cluster: quickstart
  5363. EOF
  5364. ##################################
  5365. ## Window aggregations
  5366. ##################################
  5367. statement ok
  5368. CREATE TABLE t7(x INT, y INT);
  5369. # Plans
  5370. query T multiline
  5371. EXPLAIN DECORRELATED PLAN FOR
  5372. SELECT
  5373. sum(y-3) OVER (PARTITION BY 2*x, 3*y ORDER BY x+1, y+2 ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
  5374. FROM t7
  5375. ORDER BY x;
  5376. ----
  5377. Finish order_by=[#0 asc nulls_last] output=[#2]
  5378. Project (#0, #1, #3)
  5379. Map (#2)
  5380. Project (#4..=#6)
  5381. Map (record_get[0](record_get[1](#3)), record_get[1](record_get[1](#3)), record_get[0](#3))
  5382. FlatMap unnest_list(#2)
  5383. Reduce group_by=[#2, #3] aggregates=[window_agg[sum order_by=[#0 asc nulls_last, #1 asc nulls_last] rows between unbounded preceding and current row](row(row(row(#0, #1), (#1{y} - 3)), (#0{x} + 1), (#1{y} + 2)))]
  5384. Map ((2 * #0{x}), (3 * #1{y}))
  5385. CrossJoin
  5386. Constant
  5387. - ()
  5388. Get materialize.public.t7
  5389. Target cluster: quickstart
  5390. EOF
  5391. query T multiline
  5392. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR
  5393. SELECT
  5394. sum(y-3) OVER (PARTITION BY 2*x, 3*y ORDER BY x+1, y+2 ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
  5395. FROM t7
  5396. ORDER BY x;
  5397. ----
  5398. Explained Query:
  5399. Finish order_by=[#0 asc nulls_last] output=[#2]
  5400. Project (#3..=#5)
  5401. Map (record_get[1](#1), record_get[0](#2), record_get[1](#2), record_get[0](#1))
  5402. FlatMap unnest_list(#0{window_agg})
  5403. Project (#2{window_agg})
  5404. Reduce group_by=[(2 * #0{x}), (3 * #1{y})] aggregates=[window_agg[sum order_by=[#0{x} asc nulls_last, #1{y} asc nulls_last] rows between unbounded preceding and current row](row(row(row(#0{x}, #1{y}), (#1{y} - 3)), (#0{x} + 1), (#1{y} + 2)))]
  5405. ReadStorage materialize.public.t7
  5406. Source materialize.public.t7
  5407. Target cluster: quickstart
  5408. EOF
  5409. # Empty aggregations
  5410. query IIIIIT
  5411. SELECT
  5412. x,
  5413. y,
  5414. sum(y) OVER (ORDER BY x ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW),
  5415. max(y) OVER (ORDER BY x ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW),
  5416. min(y) OVER (ORDER BY x ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW),
  5417. array_agg(y) OVER (ORDER BY x ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
  5418. FROM t7
  5419. ORDER BY x;
  5420. ----
  5421. query IIIIIT
  5422. SELECT
  5423. x,
  5424. y,
  5425. sum(y) OVER (ORDER BY x ROWS BETWEEN 2 PRECEDING AND CURRENT ROW),
  5426. max(y) OVER (ORDER BY x ROWS BETWEEN 2 PRECEDING AND CURRENT ROW),
  5427. min(y) OVER (ORDER BY x ROWS BETWEEN 2 PRECEDING AND CURRENT ROW),
  5428. jsonb_agg(y) OVER (ORDER BY x ROWS BETWEEN 2 PRECEDING AND CURRENT ROW)
  5429. FROM t7
  5430. ORDER BY x;
  5431. ----
  5432. query IIIIIT
  5433. SELECT
  5434. x,
  5435. y,
  5436. sum(y) OVER (ORDER BY x ROWS BETWEEN 2 FOLLOWING AND 5 FOLLOWING),
  5437. max(y) OVER (ORDER BY x ROWS BETWEEN 2 FOLLOWING AND 5 FOLLOWING),
  5438. min(y) OVER (ORDER BY x ROWS BETWEEN 2 FOLLOWING AND 5 FOLLOWING),
  5439. list_agg(y) OVER (ORDER BY x ROWS BETWEEN 2 FOLLOWING AND 5 FOLLOWING)[1]
  5440. FROM t7
  5441. ORDER BY x;
  5442. ----
  5443. statement ok
  5444. INSERT INTO t7 VALUES (1,2), (3,null), (5,6), (7,8), (9, null), (11, null), (13, 14), (15, 16), (17, 18), (10, -40), (10, -50);
  5445. # Test various nestings
  5446. # Postgres also doesn't support this.
  5447. query error db error: ERROR: window functions are not allowed in aggregate function \(function pg_catalog\.sum\)
  5448. SELECT sum(sum(x) OVER ()) FROM t7;
  5449. # Other way around: grouped aggregate inside a window aggregate.
  5450. # What this query is asking us to do is to
  5451. # 1. First, perform the inner sum (with its GROUP BY), and then
  5452. # 2. the outer sum (with its OVER clause).
  5453. # (Postgres handles this the same.)
  5454. query I
  5455. SELECT sum(sum(x)) OVER (ORDER BY y) FROM t7 GROUP BY y;
  5456. ----
  5457. 10
  5458. 20
  5459. 21
  5460. 26
  5461. 33
  5462. 46
  5463. 61
  5464. 78
  5465. 101
  5466. # Window aggregation inside a window aggregation. We support this, and queries like this might actually make sense in
  5467. # some cases. (I think we already had a customer with some other window functions nested inside each other.)
  5468. # (Postgres doesn't support this.)
  5469. query I
  5470. SELECT sum(sum(x) OVER ()) OVER () FROM t7;
  5471. ----
  5472. 1111
  5473. 1111
  5474. 1111
  5475. 1111
  5476. 1111
  5477. 1111
  5478. 1111
  5479. 1111
  5480. 1111
  5481. 1111
  5482. 1111
  5483. query III
  5484. SELECT
  5485. x,
  5486. sum(x) OVER (ORDER BY x),
  5487. sum(sum(x) OVER (ORDER BY x)) OVER (ORDER BY x)
  5488. FROM (SELECT DISTINCT ON(x) * FROM t7);
  5489. ----
  5490. 1 1 1
  5491. 3 4 5
  5492. 5 9 14
  5493. 7 16 30
  5494. 9 25 55
  5495. 10 35 90
  5496. 11 46 136
  5497. 13 59 195
  5498. 15 74 269
  5499. 17 91 360
  5500. # Lots of different aggregation functions. This covers
  5501. # - basic, hierarchical, accumulable aggregations
  5502. # - count's null handling
  5503. # - count(*)
  5504. # - FILTER clause
  5505. # - Aggregation functions that are transformed away by `transform_ast.rs`: bool_and, bool_or.
  5506. # These are all supported by Postgres, and the output should be identical.
  5507. query IIIIIITIIIITT
  5508. SELECT
  5509. x-y,
  5510. x,
  5511. y,
  5512. sum(x+x) OVER (PARTITION BY x-y ORDER BY x ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING),
  5513. max(x*y+1) OVER (PARTITION BY x-y ORDER BY x ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING),
  5514. min(x*y+1) OVER (PARTITION BY x-y ORDER BY x ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING),
  5515. array_agg(2*x) OVER (PARTITION BY x-y ORDER BY x ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING),
  5516. count(2*x) OVER (PARTITION BY x-y ORDER BY x ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING),
  5517. count(CASE WHEN x%3 != 0 THEN x ELSE null END) OVER (PARTITION BY x-y ORDER BY x ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING),
  5518. count(*) OVER (PARTITION BY x-y ORDER BY x ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING),
  5519. sum(x) FILTER (WHERE x%3 != 0) OVER (PARTITION BY x-y ORDER BY x ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING),
  5520. bool_and(x%3 != 0) OVER (PARTITION BY x-y ORDER BY x ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING),
  5521. bool_or(x%3 = 0) OVER (PARTITION BY x-y ORDER BY x ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
  5522. FROM t7
  5523. ORDER BY x-y, x;
  5524. ----
  5525. -1 1 2 116 307 3 {2,10,14,26,30,34} 6 5 6 43 false true
  5526. -1 5 6 114 307 31 {10,14,26,30,34} 5 4 5 42 false true
  5527. -1 7 8 104 307 57 {14,26,30,34} 4 3 4 37 false true
  5528. -1 13 14 90 307 183 {26,30,34} 3 2 3 30 false true
  5529. -1 15 16 64 307 241 {30,34} 2 1 2 17 false true
  5530. -1 17 18 34 307 307 {34} 1 1 1 17 true false
  5531. 50 10 -40 20 -399 -399 {20} 1 1 1 10 true false
  5532. 60 10 -50 20 -499 -499 {20} 1 1 1 10 true false
  5533. NULL 3 NULL 46 NULL NULL {6,18,22} 3 1 3 11 false true
  5534. NULL 9 NULL 40 NULL NULL {18,22} 2 1 2 11 false true
  5535. NULL 11 NULL 22 NULL NULL {22} 1 1 1 11 true false
  5536. # Also supported by Postgres, but output text formatting is a bit different, so putting this in a separate test.
  5537. # avg, variance, var_pop, stddev, stddev_pop are transformed away by `transform_ast.rs`.
  5538. query IIITRRRRR
  5539. SELECT
  5540. x-y,
  5541. x,
  5542. y,
  5543. jsonb_agg(2*x) OVER (PARTITION BY x-y ORDER BY x ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING),
  5544. avg(x+y) OVER (PARTITION BY x-y ORDER BY x ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING),
  5545. variance(x+y) OVER (PARTITION BY x-y ORDER BY x ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING),
  5546. var_pop(x+y) OVER (PARTITION BY x-y ORDER BY x ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING),
  5547. stddev(x+y) OVER (PARTITION BY x-y ORDER BY x ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING),
  5548. stddev_pop(x+y) OVER (PARTITION BY x-y ORDER BY x ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
  5549. FROM t7
  5550. ORDER BY x-y, x;
  5551. ----
  5552. -1 1 2 [2,10,14,26,30,34] 20.3333333333333333333333333333333333333 157.866666666666666666666666666666666666 131.555555555555555555555555555555555555 12.5645002553490628940078294328743895394 11.469767022723502362305964713399618176
  5553. -1 5 6 [10,14,26,30,34] 23.8 107.2 85.76 10.3537432844358276565421773235938072887 9.26066952223218037884464808956388302968
  5554. -1 7 8 [14,26,30,34] 27 74.6666666666666666666666666666666666667 56 8.64098759787714697462128991478399554361 7.48331477354788277116749746463309860351
  5555. -1 13 14 [26,30,34] 31 16 10.6666666666666666666666666666666666667 4 3.26598632371090413092971209960785518929
  5556. -1 15 16 [30,34] 33 8 4 2.82842712474619009760337744841939615714 2
  5557. -1 17 18 [34] 35 NULL 0 NULL 0
  5558. 50 10 -40 [20] -30 NULL 0 NULL 0
  5559. 60 10 -50 [20] -40 NULL 0 NULL 0
  5560. NULL 3 NULL [6,18,22] NULL NULL NULL NULL NULL
  5561. NULL 9 NULL [18,22] NULL NULL NULL NULL NULL
  5562. NULL 11 NULL [22] NULL NULL NULL NULL NULL
  5563. # These are not supported by Postgres.
  5564. # - ORDER BY in aggr
  5565. # - list indexing after the OVER clause
  5566. query IIITTI
  5567. SELECT
  5568. x-y,
  5569. x,
  5570. y,
  5571. array_agg(x ORDER BY CASE WHEN y IS NOT NULL THEN y ELSE x%5 END DESC) OVER (PARTITION BY x-y ORDER BY x ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING),
  5572. array_agg(2*x) OVER (PARTITION BY x-y ORDER BY x ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING),
  5573. list_agg(2*x) OVER (PARTITION BY x-y ORDER BY x ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)[2]
  5574. FROM t7
  5575. ORDER BY x-y, x;
  5576. ----
  5577. -1 1 2 {17,15,13,7,5,1} {2,10,14,26,30,34} 10
  5578. -1 5 6 {17,15,13,7,5} {10,14,26,30,34} 14
  5579. -1 7 8 {17,15,13,7} {14,26,30,34} 26
  5580. -1 13 14 {17,15,13} {26,30,34} 30
  5581. -1 15 16 {17,15} {30,34} 34
  5582. -1 17 18 {17} {34} NULL
  5583. 50 10 -40 {10} {20} NULL
  5584. 60 10 -50 {10} {20} NULL
  5585. NULL 3 NULL {9,3,11} {6,18,22} 18
  5586. NULL 9 NULL {9,11} {18,22} 22
  5587. NULL 11 NULL {11} {22} NULL
  5588. # https://github.com/MaterializeInc/database-issues/issues/6626
  5589. query error db error: ERROR: DISTINCT in window aggregates not yet supported
  5590. SELECT
  5591. x-y,
  5592. x,
  5593. y,
  5594. array_agg(x/10) OVER (PARTITION BY x-y ORDER BY x ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING),
  5595. count(DISTINCT x/10) OVER (PARTITION BY x-y ORDER BY x ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
  5596. FROM t7
  5597. ORDER BY x-y, x;
  5598. # No ORDER BY in OVER clause.
  5599. query IIIIITTTT
  5600. SELECT
  5601. x,
  5602. y,
  5603. sum(y) OVER (),
  5604. max(y) OVER (),
  5605. min(y) OVER (),
  5606. array_agg(y ORDER BY y) OVER (),
  5607. array_agg(y ORDER BY y NULLS LAST) OVER (),
  5608. array_agg(y ORDER BY y NULLS FIRST) OVER (),
  5609. bool_and(x%3 != 0) OVER ()
  5610. FROM t7
  5611. ORDER BY x;
  5612. ----
  5613. 1 2 -26 18 -50 {-50,-40,2,6,8,14,16,18,NULL,NULL,NULL} {-50,-40,2,6,8,14,16,18,NULL,NULL,NULL} {NULL,NULL,NULL,-50,-40,2,6,8,14,16,18} false
  5614. 3 NULL -26 18 -50 {-50,-40,2,6,8,14,16,18,NULL,NULL,NULL} {-50,-40,2,6,8,14,16,18,NULL,NULL,NULL} {NULL,NULL,NULL,-50,-40,2,6,8,14,16,18} false
  5615. 5 6 -26 18 -50 {-50,-40,2,6,8,14,16,18,NULL,NULL,NULL} {-50,-40,2,6,8,14,16,18,NULL,NULL,NULL} {NULL,NULL,NULL,-50,-40,2,6,8,14,16,18} false
  5616. 7 8 -26 18 -50 {-50,-40,2,6,8,14,16,18,NULL,NULL,NULL} {-50,-40,2,6,8,14,16,18,NULL,NULL,NULL} {NULL,NULL,NULL,-50,-40,2,6,8,14,16,18} false
  5617. 9 NULL -26 18 -50 {-50,-40,2,6,8,14,16,18,NULL,NULL,NULL} {-50,-40,2,6,8,14,16,18,NULL,NULL,NULL} {NULL,NULL,NULL,-50,-40,2,6,8,14,16,18} false
  5618. 10 -50 -26 18 -50 {-50,-40,2,6,8,14,16,18,NULL,NULL,NULL} {-50,-40,2,6,8,14,16,18,NULL,NULL,NULL} {NULL,NULL,NULL,-50,-40,2,6,8,14,16,18} false
  5619. 10 -40 -26 18 -50 {-50,-40,2,6,8,14,16,18,NULL,NULL,NULL} {-50,-40,2,6,8,14,16,18,NULL,NULL,NULL} {NULL,NULL,NULL,-50,-40,2,6,8,14,16,18} false
  5620. 11 NULL -26 18 -50 {-50,-40,2,6,8,14,16,18,NULL,NULL,NULL} {-50,-40,2,6,8,14,16,18,NULL,NULL,NULL} {NULL,NULL,NULL,-50,-40,2,6,8,14,16,18} false
  5621. 13 14 -26 18 -50 {-50,-40,2,6,8,14,16,18,NULL,NULL,NULL} {-50,-40,2,6,8,14,16,18,NULL,NULL,NULL} {NULL,NULL,NULL,-50,-40,2,6,8,14,16,18} false
  5622. 15 16 -26 18 -50 {-50,-40,2,6,8,14,16,18,NULL,NULL,NULL} {-50,-40,2,6,8,14,16,18,NULL,NULL,NULL} {NULL,NULL,NULL,-50,-40,2,6,8,14,16,18} false
  5623. 17 18 -26 18 -50 {-50,-40,2,6,8,14,16,18,NULL,NULL,NULL} {-50,-40,2,6,8,14,16,18,NULL,NULL,NULL} {NULL,NULL,NULL,-50,-40,2,6,8,14,16,18} false
  5624. query IIIIIIT
  5625. SELECT
  5626. x-y+x/10,
  5627. x,
  5628. y,
  5629. sum(y) OVER (PARTITION BY x-y+x/10),
  5630. max(y) OVER (PARTITION BY x-y+x/10),
  5631. min(y) OVER (PARTITION BY x-y+x/10),
  5632. array_agg(y) OVER (PARTITION BY x-y+x/10)
  5633. FROM t7
  5634. ORDER BY x-y+x/10;
  5635. ----
  5636. -1 1 2 16 8 2 {2,6,8}
  5637. -1 5 6 16 8 2 {2,6,8}
  5638. -1 7 8 16 8 2 {2,6,8}
  5639. 0 13 14 48 18 14 {14,16,18}
  5640. 0 15 16 48 18 14 {14,16,18}
  5641. 0 17 18 48 18 14 {14,16,18}
  5642. 51 10 -40 -40 -40 -40 {-40}
  5643. 61 10 -50 -50 -50 -50 {-50}
  5644. NULL 3 NULL NULL NULL NULL {NULL,NULL,NULL}
  5645. NULL 9 NULL NULL NULL NULL {NULL,NULL,NULL}
  5646. NULL 11 NULL NULL NULL NULL {NULL,NULL,NULL}
  5647. # Test the situation when we don't have an ORDER BY, but we have a frame that doesn't include the current row.
  5648. # This verifies that the `order_by.is_empty()` if condition also includes `window_frame.includes_current_row()`.
  5649. query IIIIITR
  5650. SELECT
  5651. x-y+x/10,
  5652. x,
  5653. y,
  5654. sum(y) OVER (PARTITION BY x-y+x/10 ROWS BETWEEN 200 PRECEDING AND 100 PRECEDING),
  5655. count(y) OVER (PARTITION BY x-y+x/10 ROWS BETWEEN 200 PRECEDING AND 100 PRECEDING),
  5656. array_agg(y) OVER (PARTITION BY x-y+x/10 ROWS BETWEEN 200 PRECEDING AND 100 PRECEDING),
  5657. avg(y) OVER (PARTITION BY x-y+x/10 ROWS BETWEEN 200 PRECEDING AND 100 PRECEDING)
  5658. FROM t7
  5659. ORDER BY x, y;
  5660. ----
  5661. -1 1 2 NULL 0 NULL NULL
  5662. NULL 3 NULL NULL 0 NULL NULL
  5663. -1 5 6 NULL 0 NULL NULL
  5664. -1 7 8 NULL 0 NULL NULL
  5665. NULL 9 NULL NULL 0 NULL NULL
  5666. 61 10 -50 NULL 0 NULL NULL
  5667. 51 10 -40 NULL 0 NULL NULL
  5668. NULL 11 NULL NULL 0 NULL NULL
  5669. 0 13 14 NULL 0 NULL NULL
  5670. 0 15 16 NULL 0 NULL NULL
  5671. 0 17 18 NULL 0 NULL NULL
  5672. # ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
  5673. query IIIIIT
  5674. SELECT
  5675. x,
  5676. y,
  5677. sum(y) OVER (ORDER BY x ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW),
  5678. max(y) OVER (ORDER BY x ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW),
  5679. min(y) OVER (ORDER BY x ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW),
  5680. array_agg(y) OVER (ORDER BY x ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
  5681. FROM t7
  5682. ORDER BY x;
  5683. ----
  5684. 1 2 2 2 2 {2}
  5685. 3 NULL 2 2 2 {2,NULL}
  5686. 5 6 8 6 2 {2,6,NULL}
  5687. 7 8 16 8 2 {2,6,8,NULL}
  5688. 9 NULL 16 8 2 {2,6,8,NULL,NULL}
  5689. 10 -50 -34 8 -50 {-50,2,6,8,NULL,NULL}
  5690. 10 -40 -74 8 -50 {-50,-40,2,6,8,NULL,NULL}
  5691. 11 NULL -74 8 -50 {-50,-40,2,6,8,NULL,NULL,NULL}
  5692. 13 14 -60 14 -50 {-50,-40,2,6,8,14,NULL,NULL,NULL}
  5693. 15 16 -44 16 -50 {-50,-40,2,6,8,14,16,NULL,NULL,NULL}
  5694. 17 18 -26 18 -50 {-50,-40,2,6,8,14,16,18,NULL,NULL,NULL}
  5695. statement ok
  5696. CREATE TABLE t8(o1 INT, o2 INT, v INT);
  5697. statement ok
  5698. INSERT INTO t8 VALUES (1,1,-2), (1,1,-3), (1,2,-4), (1,3,-7), (1,3,-8), (2,1,-10), (2,2,-1000), (2,2,-1000), (2,2,-1000), (3,0,-100);
  5699. # Default frame, i.e. RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
  5700. query IIIIIIT
  5701. SELECT
  5702. o1,
  5703. o2,
  5704. v,
  5705. sum(v) OVER (ORDER BY o1, o2),
  5706. max(v) OVER (ORDER BY o1, o2),
  5707. min(v) OVER (ORDER BY o1, o2),
  5708. array_agg(v) OVER (ORDER BY o1, o2 RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
  5709. FROM t8
  5710. ORDER BY o1, o2;
  5711. ----
  5712. 1 1 -3 -5 -2 -3 {-3,-2}
  5713. 1 1 -2 -5 -2 -3 {-3,-2}
  5714. 1 2 -4 -9 -2 -4 {-4,-3,-2}
  5715. 1 3 -8 -24 -2 -8 {-8,-7,-4,-3,-2}
  5716. 1 3 -7 -24 -2 -8 {-8,-7,-4,-3,-2}
  5717. 2 1 -10 -34 -2 -10 {-10,-8,-7,-4,-3,-2}
  5718. 2 2 -1000 -3034 -2 -1000 {-1000,-1000,-1000,-10,-8,-7,-4,-3,-2}
  5719. 2 2 -1000 -3034 -2 -1000 {-1000,-1000,-1000,-10,-8,-7,-4,-3,-2}
  5720. 2 2 -1000 -3034 -2 -1000 {-1000,-1000,-1000,-10,-8,-7,-4,-3,-2}
  5721. 3 0 -100 -3134 -2 -1000 {-1000,-1000,-1000,-100,-10,-8,-7,-4,-3,-2}
  5722. query IIIIIIT
  5723. SELECT
  5724. o1,
  5725. o2,
  5726. v,
  5727. sum(v) OVER (PARTITION BY o1 ORDER BY o2),
  5728. max(v) OVER (PARTITION BY o1 ORDER BY o2),
  5729. min(v) OVER (PARTITION BY o1 ORDER BY o2),
  5730. array_agg(v) OVER (PARTITION BY o1 ORDER BY o2 RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
  5731. FROM t8
  5732. ORDER BY o1, o2;
  5733. ----
  5734. 1 1 -3 -5 -2 -3 {-3,-2}
  5735. 1 1 -2 -5 -2 -3 {-3,-2}
  5736. 1 2 -4 -9 -2 -4 {-4,-3,-2}
  5737. 1 3 -8 -24 -2 -8 {-8,-7,-4,-3,-2}
  5738. 1 3 -7 -24 -2 -8 {-8,-7,-4,-3,-2}
  5739. 2 1 -10 -10 -10 -10 {-10}
  5740. 2 2 -1000 -3010 -10 -1000 {-1000,-1000,-1000,-10}
  5741. 2 2 -1000 -3010 -10 -1000 {-1000,-1000,-1000,-10}
  5742. 2 2 -1000 -3010 -10 -1000 {-1000,-1000,-1000,-10}
  5743. 3 0 -100 -100 -100 -100 {-100}
  5744. # ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
  5745. # Note that x=10 occurs twice. This means that the ordering of these rows is undefined, so it can change between
  5746. # Materialize versions.
  5747. query IIIIIT
  5748. SELECT
  5749. x,
  5750. y,
  5751. sum(y) OVER (ORDER BY x ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING),
  5752. max(y) OVER (ORDER BY x ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING),
  5753. min(y) OVER (ORDER BY x ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING),
  5754. array_agg(y) OVER (ORDER BY x ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
  5755. FROM t7
  5756. ORDER BY x;
  5757. ----
  5758. 1 2 -26 18 -50 {-50,-40,2,6,8,14,16,18,NULL,NULL,NULL}
  5759. 3 NULL -28 18 -50 {-50,-40,6,8,14,16,18,NULL,NULL,NULL}
  5760. 5 6 -28 18 -50 {-50,-40,6,8,14,16,18,NULL,NULL}
  5761. 7 8 -34 18 -50 {-50,-40,8,14,16,18,NULL,NULL}
  5762. 9 NULL -42 18 -50 {-50,-40,14,16,18,NULL,NULL}
  5763. 10 -40 8 18 -40 {-40,14,16,18,NULL}
  5764. 10 -50 -42 18 -50 {-50,-40,14,16,18,NULL}
  5765. 11 NULL 48 18 14 {14,16,18,NULL}
  5766. 13 14 48 18 14 {14,16,18}
  5767. 15 16 34 18 16 {16,18}
  5768. 17 18 18 18 18 {18}
  5769. # RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
  5770. # TODO: Actually, we already have the rendering code for this. So, we just need to either hack planning a bit to add an
  5771. # exception for window aggregations, or add rendering support also for other window functions.
  5772. query error RANGE in non-default window frames not yet supported
  5773. SELECT
  5774. x,
  5775. y,
  5776. sum(y) OVER (ORDER BY x RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING),
  5777. max(y) OVER (ORDER BY x RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING),
  5778. min(y) OVER (ORDER BY x RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING),
  5779. array_agg(y) OVER (ORDER BY x RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
  5780. FROM t7
  5781. ORDER BY x;
  5782. # PARTITION BY
  5783. # ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
  5784. # Here, x=10 occurring twice does NOT mean that the result for these rows would be undefined.
  5785. query IIIIIIT
  5786. SELECT
  5787. x-y,
  5788. x,
  5789. y,
  5790. sum(x+x) OVER (PARTITION BY x-y ORDER BY x ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING),
  5791. max(x+x) OVER (PARTITION BY x-y ORDER BY x ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING),
  5792. min(x+x) OVER (PARTITION BY x-y ORDER BY x ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING),
  5793. array_agg(x+x) OVER (PARTITION BY x-y ORDER BY x ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
  5794. FROM t7
  5795. ORDER BY x-y, x;
  5796. ----
  5797. -1 1 2 116 34 2 {2,10,14,26,30,34}
  5798. -1 5 6 114 34 10 {10,14,26,30,34}
  5799. -1 7 8 104 34 14 {14,26,30,34}
  5800. -1 13 14 90 34 26 {26,30,34}
  5801. -1 15 16 64 34 30 {30,34}
  5802. -1 17 18 34 34 34 {34}
  5803. 50 10 -40 20 20 20 {20}
  5804. 60 10 -50 20 20 20 {20}
  5805. NULL 3 NULL 46 22 6 {6,18,22}
  5806. NULL 9 NULL 40 22 18 {18,22}
  5807. NULL 11 NULL 22 22 22 {22}
  5808. query IIIIIT
  5809. SELECT
  5810. x,
  5811. y,
  5812. sum(x+x) OVER (PARTITION BY x-y ORDER BY x ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING),
  5813. max(x+x) OVER (PARTITION BY x-y ORDER BY x ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING),
  5814. min(x+x) OVER (PARTITION BY x-y ORDER BY x ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING),
  5815. array_agg(x+x) OVER (PARTITION BY x-y ORDER BY x ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
  5816. FROM t7
  5817. ORDER BY x-y, x;
  5818. ----
  5819. 1 2 116 34 2 {2,10,14,26,30,34}
  5820. 5 6 114 34 10 {10,14,26,30,34}
  5821. 7 8 104 34 14 {14,26,30,34}
  5822. 13 14 90 34 26 {26,30,34}
  5823. 15 16 64 34 30 {30,34}
  5824. 17 18 34 34 34 {34}
  5825. 10 -40 20 20 20 {20}
  5826. 10 -50 20 20 20 {20}
  5827. 3 NULL 46 22 6 {6,18,22}
  5828. 9 NULL 40 22 18 {18,22}
  5829. 11 NULL 22 22 22 {22}
  5830. # PARTITION BY
  5831. # RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
  5832. query error RANGE in non-default window frames not yet supported
  5833. SELECT
  5834. x,
  5835. y,
  5836. sum(x+x) OVER (PARTITION BY x-y ORDER BY x RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING),
  5837. max(x+x) OVER (PARTITION BY x-y ORDER BY x RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING),
  5838. min(x+x) OVER (PARTITION BY x-y ORDER BY x RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING),
  5839. array_agg(x+x) OVER (PARTITION BY x-y ORDER BY x RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
  5840. FROM t7
  5841. ORDER BY x-y, x;
  5842. # ROWS BETWEEN offset PRECEDING AND offset FOLLOWING
  5843. query IIIIIT
  5844. SELECT
  5845. x,
  5846. y,
  5847. sum(x) OVER (ORDER BY CASE WHEN y IS NOT NULL THEN x*y ELSE x END ROWS BETWEEN 2 PRECEDING AND 1 FOLLOWING),
  5848. max(x) OVER (ORDER BY CASE WHEN y IS NOT NULL THEN x*y ELSE x END ROWS BETWEEN 2 PRECEDING AND 1 FOLLOWING),
  5849. min(x) OVER (ORDER BY CASE WHEN y IS NOT NULL THEN x*y ELSE x END ROWS BETWEEN 2 PRECEDING AND 1 FOLLOWING),
  5850. array_agg(x) OVER (ORDER BY CASE WHEN y IS NOT NULL THEN x*y ELSE x END ROWS BETWEEN 2 PRECEDING AND 1 FOLLOWING)
  5851. FROM t7
  5852. ORDER BY CASE WHEN y IS NOT NULL THEN x*y ELSE x END;
  5853. ----
  5854. 10 -50 20 10 10 {10,10}
  5855. 10 -40 21 10 1 {1,10,10}
  5856. 1 2 24 10 1 {1,3,10,10}
  5857. 3 NULL 23 10 1 {1,3,9,10}
  5858. 9 NULL 24 11 1 {1,3,9,11}
  5859. 11 NULL 28 11 3 {3,5,9,11}
  5860. 5 6 32 11 5 {5,7,9,11}
  5861. 7 8 36 13 5 {5,7,11,13}
  5862. 13 14 40 15 5 {5,7,13,15}
  5863. 15 16 52 17 7 {7,13,15,17}
  5864. 17 18 45 17 13 {13,15,17}
  5865. query IIIIIT
  5866. SELECT
  5867. x,
  5868. y,
  5869. sum(x) OVER (ORDER BY CASE WHEN y IS NOT NULL THEN x*y ELSE x END ROWS BETWEEN 0 PRECEDING AND 1 FOLLOWING),
  5870. max(x) OVER (ORDER BY CASE WHEN y IS NOT NULL THEN x*y ELSE x END ROWS BETWEEN 0 PRECEDING AND 1 FOLLOWING),
  5871. min(x) OVER (ORDER BY CASE WHEN y IS NOT NULL THEN x*y ELSE x END ROWS BETWEEN 0 PRECEDING AND 1 FOLLOWING),
  5872. array_agg(x) OVER (ORDER BY CASE WHEN y IS NOT NULL THEN x*y ELSE x END ROWS BETWEEN 0 PRECEDING AND 1 FOLLOWING)
  5873. FROM t7
  5874. ORDER BY CASE WHEN y IS NOT NULL THEN x*y ELSE x END;
  5875. ----
  5876. 10 -50 20 10 10 {10,10}
  5877. 10 -40 11 10 1 {1,10}
  5878. 1 2 4 3 1 {1,3}
  5879. 3 NULL 12 9 3 {3,9}
  5880. 9 NULL 20 11 9 {9,11}
  5881. 11 NULL 16 11 5 {5,11}
  5882. 5 6 12 7 5 {5,7}
  5883. 7 8 20 13 7 {7,13}
  5884. 13 14 28 15 13 {13,15}
  5885. 15 16 32 17 15 {15,17}
  5886. 17 18 17 17 17 {17}
  5887. query IIIIIT
  5888. SELECT
  5889. x,
  5890. y,
  5891. sum(x) OVER (ORDER BY CASE WHEN y IS NOT NULL THEN x*y ELSE x END ROWS BETWEEN 0 PRECEDING AND 0 FOLLOWING),
  5892. max(x) OVER (ORDER BY CASE WHEN y IS NOT NULL THEN x*y ELSE x END ROWS BETWEEN 0 PRECEDING AND 0 FOLLOWING),
  5893. min(x) OVER (ORDER BY CASE WHEN y IS NOT NULL THEN x*y ELSE x END ROWS BETWEEN 0 PRECEDING AND 0 FOLLOWING),
  5894. array_agg(x) OVER (ORDER BY CASE WHEN y IS NOT NULL THEN x*y ELSE x END ROWS BETWEEN 0 PRECEDING AND 0 FOLLOWING)
  5895. FROM t7
  5896. ORDER BY CASE WHEN y IS NOT NULL THEN x*y ELSE x END;
  5897. ----
  5898. 10 -50 10 10 10 {10}
  5899. 10 -40 10 10 10 {10}
  5900. 1 2 1 1 1 {1}
  5901. 3 NULL 3 3 3 {3}
  5902. 9 NULL 9 9 9 {9}
  5903. 11 NULL 11 11 11 {11}
  5904. 5 6 5 5 5 {5}
  5905. 7 8 7 7 7 {7}
  5906. 13 14 13 13 13 {13}
  5907. 15 16 15 15 15 {15}
  5908. 17 18 17 17 17 {17}
  5909. # mixed UNBOUNDED - OFFSET frames
  5910. query error db error: ERROR: mixed unbounded \- offset frames not yet supported
  5911. SELECT
  5912. sum(y) OVER (ORDER BY x ROWS BETWEEN UNBOUNDED PRECEDING AND 2 PRECEDING)
  5913. FROM t7;
  5914. query error db error: ERROR: mixed unbounded \- offset frames not yet supported
  5915. SELECT
  5916. sum(y) OVER (ORDER BY x ROWS BETWEEN UNBOUNDED PRECEDING AND 2 FOLLOWING)
  5917. FROM t7;
  5918. query error db error: ERROR: mixed unbounded \- offset frames not yet supported
  5919. SELECT
  5920. sum(y) OVER (ORDER BY x ROWS BETWEEN 2 PRECEDING AND UNBOUNDED FOLLOWING)
  5921. FROM t7;
  5922. query error db error: ERROR: mixed unbounded \- offset frames not yet supported
  5923. SELECT
  5924. sum(y) OVER (ORDER BY x ROWS BETWEEN 3 FOLLOWING AND UNBOUNDED FOLLOWING)
  5925. FROM t7;
  5926. # And a parse error as a bonus
  5927. query error Expected PRECEDING or FOLLOWING, found UNBOUNDED
  5928. SELECT
  5929. sum(y) OVER (ORDER BY x ROWS BETWEEN 3 FOLLOWING AND 2 UNBOUNDED FOLLOWING)
  5930. FROM t7;
  5931. # ROWS BETWEEN offset PRECEDING AND offset PRECEDING
  5932. query IIIIIT
  5933. SELECT
  5934. x,
  5935. y,
  5936. sum(x) OVER (ORDER BY CASE WHEN y IS NOT NULL THEN x*y ELSE x END ROWS BETWEEN 3 PRECEDING AND 1 PRECEDING),
  5937. max(x) OVER (ORDER BY CASE WHEN y IS NOT NULL THEN x*y ELSE x END ROWS BETWEEN 3 PRECEDING AND 1 PRECEDING),
  5938. min(x) OVER (ORDER BY CASE WHEN y IS NOT NULL THEN x*y ELSE x END ROWS BETWEEN 3 PRECEDING AND 1 PRECEDING),
  5939. array_agg(x) OVER (ORDER BY CASE WHEN y IS NOT NULL THEN x*y ELSE x END ROWS BETWEEN 3 PRECEDING AND 1 PRECEDING)
  5940. FROM t7
  5941. ORDER BY CASE WHEN y IS NOT NULL THEN x*y ELSE x END;
  5942. ----
  5943. 10 -50 NULL NULL NULL NULL
  5944. 10 -40 10 10 10 {10}
  5945. 1 2 20 10 10 {10,10}
  5946. 3 NULL 21 10 1 {1,10,10}
  5947. 9 NULL 14 10 1 {1,3,10}
  5948. 11 NULL 13 9 1 {1,3,9}
  5949. 5 6 23 11 3 {3,9,11}
  5950. 7 8 25 11 5 {5,9,11}
  5951. 13 14 23 11 5 {5,7,11}
  5952. 15 16 25 13 5 {5,7,13}
  5953. 17 18 35 15 7 {7,13,15}
  5954. # ROWS BETWEEN offset FOLLOWING AND offset FOLLOWING
  5955. query IIIIIT
  5956. SELECT
  5957. x,
  5958. y,
  5959. sum(x) OVER (ORDER BY CASE WHEN y IS NOT NULL THEN x*y ELSE x END ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING),
  5960. max(x) OVER (ORDER BY CASE WHEN y IS NOT NULL THEN x*y ELSE x END ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING),
  5961. min(x) OVER (ORDER BY CASE WHEN y IS NOT NULL THEN x*y ELSE x END ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING),
  5962. array_agg(x) OVER (ORDER BY CASE WHEN y IS NOT NULL THEN x*y ELSE x END ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING)
  5963. FROM t7
  5964. ORDER BY CASE WHEN y IS NOT NULL THEN x*y ELSE x END;
  5965. ----
  5966. 10 -50 13 9 1 {1,3,9}
  5967. 10 -40 23 11 3 {3,9,11}
  5968. 1 2 25 11 5 {5,9,11}
  5969. 3 NULL 23 11 5 {5,7,11}
  5970. 9 NULL 25 13 5 {5,7,13}
  5971. 11 NULL 35 15 7 {7,13,15}
  5972. 5 6 45 17 13 {13,15,17}
  5973. 7 8 32 17 15 {15,17}
  5974. 13 14 17 17 17 {17}
  5975. 15 16 NULL NULL NULL NULL
  5976. 17 18 NULL NULL NULL NULL
  5977. # Frame end is later than frame start. All frames are empty.
  5978. query IIIIIT
  5979. SELECT
  5980. x,
  5981. y,
  5982. sum(x) OVER (ORDER BY CASE WHEN y IS NOT NULL THEN x*y ELSE x END ROWS BETWEEN 30 FOLLOWING AND 1 FOLLOWING),
  5983. max(x) OVER (ORDER BY CASE WHEN y IS NOT NULL THEN x*y ELSE x END ROWS BETWEEN 30 FOLLOWING AND 1 FOLLOWING),
  5984. min(x) OVER (ORDER BY CASE WHEN y IS NOT NULL THEN x*y ELSE x END ROWS BETWEEN 30 FOLLOWING AND 1 FOLLOWING),
  5985. array_agg(x) OVER (ORDER BY CASE WHEN y IS NOT NULL THEN x*y ELSE x END ROWS BETWEEN 30 FOLLOWING AND 1 FOLLOWING)
  5986. FROM t7
  5987. ORDER BY CASE WHEN y IS NOT NULL THEN x*y ELSE x END;
  5988. ----
  5989. 10 -50 NULL NULL NULL NULL
  5990. 10 -40 NULL NULL NULL NULL
  5991. 1 2 NULL NULL NULL NULL
  5992. 3 NULL NULL NULL NULL NULL
  5993. 9 NULL NULL NULL NULL NULL
  5994. 11 NULL NULL NULL NULL NULL
  5995. 5 6 NULL NULL NULL NULL
  5996. 7 8 NULL NULL NULL NULL
  5997. 13 14 NULL NULL NULL NULL
  5998. 15 16 NULL NULL NULL NULL
  5999. 17 18 NULL NULL NULL NULL
  6000. query IIIIIT
  6001. SELECT
  6002. x,
  6003. y,
  6004. sum(x) OVER (ORDER BY CASE WHEN y IS NOT NULL THEN x*y ELSE x END ROWS BETWEEN 2 FOLLOWING AND 1 FOLLOWING),
  6005. max(x) OVER (ORDER BY CASE WHEN y IS NOT NULL THEN x*y ELSE x END ROWS BETWEEN 2 FOLLOWING AND 1 FOLLOWING),
  6006. min(x) OVER (ORDER BY CASE WHEN y IS NOT NULL THEN x*y ELSE x END ROWS BETWEEN 2 FOLLOWING AND 1 FOLLOWING),
  6007. array_agg(x) OVER (ORDER BY CASE WHEN y IS NOT NULL THEN x*y ELSE x END ROWS BETWEEN 2 FOLLOWING AND 1 FOLLOWING)
  6008. FROM t7
  6009. ORDER BY CASE WHEN y IS NOT NULL THEN x*y ELSE x END;
  6010. ----
  6011. 10 -50 NULL NULL NULL NULL
  6012. 10 -40 NULL NULL NULL NULL
  6013. 1 2 NULL NULL NULL NULL
  6014. 3 NULL NULL NULL NULL NULL
  6015. 9 NULL NULL NULL NULL NULL
  6016. 11 NULL NULL NULL NULL NULL
  6017. 5 6 NULL NULL NULL NULL
  6018. 7 8 NULL NULL NULL NULL
  6019. 13 14 NULL NULL NULL NULL
  6020. 15 16 NULL NULL NULL NULL
  6021. 17 18 NULL NULL NULL NULL
  6022. # ROWS BETWEEN offset PRECEDING AND offset FOLLOWING
  6023. query IIIIIIT
  6024. SELECT
  6025. x-y+x/10,
  6026. x,
  6027. y,
  6028. sum(x+x) OVER (PARTITION BY x-y+x/10 ORDER BY x ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING),
  6029. max(x+x) OVER (PARTITION BY x-y+x/10 ORDER BY x ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING),
  6030. min(x+x) OVER (PARTITION BY x-y+x/10 ORDER BY x ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING),
  6031. array_agg(x+x) OVER (PARTITION BY x-y+x/10 ORDER BY x ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING)
  6032. FROM t7
  6033. ORDER BY x-y+x/10, x;
  6034. ----
  6035. -1 1 2 12 10 2 {2,10}
  6036. -1 5 6 26 14 2 {2,10,14}
  6037. -1 7 8 24 14 10 {10,14}
  6038. 0 13 14 56 30 26 {26,30}
  6039. 0 15 16 90 34 26 {26,30,34}
  6040. 0 17 18 64 34 30 {30,34}
  6041. 51 10 -40 20 20 20 {20}
  6042. 61 10 -50 20 20 20 {20}
  6043. NULL 3 NULL 24 18 6 {6,18}
  6044. NULL 9 NULL 46 22 6 {6,18,22}
  6045. NULL 11 NULL 40 22 18 {18,22}
  6046. query IIIIIIT
  6047. SELECT
  6048. x-y+x/10,
  6049. x,
  6050. y,
  6051. sum(x+x) OVER (PARTITION BY x-y+x/10 ORDER BY x ROWS BETWEEN 1000000 PRECEDING AND 1000000 FOLLOWING),
  6052. max(x+x) OVER (PARTITION BY x-y+x/10 ORDER BY x ROWS BETWEEN 1000000 PRECEDING AND 1000000 FOLLOWING),
  6053. min(x+x) OVER (PARTITION BY x-y+x/10 ORDER BY x ROWS BETWEEN 1000000 PRECEDING AND 1000000 FOLLOWING),
  6054. array_agg(x+x) OVER (PARTITION BY x-y+x/10 ORDER BY x ROWS BETWEEN 1000000 PRECEDING AND 1000000 FOLLOWING)
  6055. FROM t7
  6056. ORDER BY x-y+x/10, x;
  6057. ----
  6058. -1 1 2 26 14 2 {2,10,14}
  6059. -1 5 6 26 14 2 {2,10,14}
  6060. -1 7 8 26 14 2 {2,10,14}
  6061. 0 13 14 90 34 26 {26,30,34}
  6062. 0 15 16 90 34 26 {26,30,34}
  6063. 0 17 18 90 34 26 {26,30,34}
  6064. 51 10 -40 20 20 20 {20}
  6065. 61 10 -50 20 20 20 {20}
  6066. NULL 3 NULL 46 22 6 {6,18,22}
  6067. NULL 9 NULL 46 22 6 {6,18,22}
  6068. NULL 11 NULL 46 22 6 {6,18,22}
  6069. query IIIIIIT
  6070. SELECT
  6071. x-row_number() OVER (ORDER BY x+y),
  6072. x,
  6073. y,
  6074. sum(x+x) OVER (PARTITION BY x-row_number() OVER (ORDER BY x+y) ORDER BY x ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING),
  6075. max(x+x) OVER (PARTITION BY x-row_number() OVER (ORDER BY x+y) ORDER BY x ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING),
  6076. min(x+x) OVER (PARTITION BY x-row_number() OVER (ORDER BY x+y) ORDER BY x ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING),
  6077. array_agg(x+x) OVER (PARTITION BY x-row_number() OVER (ORDER BY x+y) ORDER BY x ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING)
  6078. FROM t7
  6079. ORDER BY x-row_number() OVER (ORDER BY x+y), x;
  6080. ----
  6081. -6 3 NULL 6 6 6 {6}
  6082. -2 1 2 2 2 2 {2}
  6083. -1 9 NULL 18 18 18 {18}
  6084. 0 11 NULL 22 22 22 {22}
  6085. 1 5 6 10 10 10 {10}
  6086. 2 7 8 14 14 14 {14}
  6087. 7 13 14 26 26 26 {26}
  6088. 8 10 -40 50 30 20 {20,30}
  6089. 8 15 16 50 30 20 {20,30}
  6090. 9 10 -50 54 34 20 {20,34}
  6091. 9 17 18 54 34 20 {20,34}
  6092. # Offset has to be a literal
  6093. query error Expected literal unsigned integer, found identifier "row_number"
  6094. SELECT
  6095. x-row_number() OVER (ORDER BY x+y),
  6096. x,
  6097. y,
  6098. sum(x+x) OVER (PARTITION BY x-row_number() OVER (ORDER BY x+y) ORDER BY x ROWS BETWEEN row_number() OVER (ORDER BY x+y) PRECEDING AND 0 FOLLOWING),
  6099. max(x+x) OVER (PARTITION BY x-row_number() OVER (ORDER BY x+y) ORDER BY x ROWS BETWEEN row_number() OVER (ORDER BY x+y) PRECEDING AND 0 FOLLOWING),
  6100. min(x+x) OVER (PARTITION BY x-row_number() OVER (ORDER BY x+y) ORDER BY x ROWS BETWEEN row_number() OVER (ORDER BY x+y) PRECEDING AND 0 FOLLOWING),
  6101. array_agg(x+x) OVER (PARTITION BY x-row_number() OVER (ORDER BY x+y) ORDER BY x ROWS BETWEEN row_number() OVER (ORDER BY x+y) PRECEDING AND 0 FOLLOWING)
  6102. FROM t7
  6103. ORDER BY x-row_number() OVER (ORDER BY x+y), x;
  6104. # ROWS BETWEEN offset PRECEDING AND CURRENT ROW
  6105. query IIIIIIT
  6106. SELECT
  6107. x-y+x/10,
  6108. x,
  6109. y,
  6110. sum(x+x) OVER (PARTITION BY x-y+x/10 ORDER BY x ROWS BETWEEN 2 PRECEDING AND CURRENT ROW),
  6111. max(x+x) OVER (PARTITION BY x-y+x/10 ORDER BY x ROWS BETWEEN 2 PRECEDING AND CURRENT ROW),
  6112. min(x+x) OVER (PARTITION BY x-y+x/10 ORDER BY x ROWS BETWEEN 2 PRECEDING AND CURRENT ROW),
  6113. array_agg(x+x) OVER (PARTITION BY x-y+x/10 ORDER BY x ROWS BETWEEN 2 PRECEDING AND CURRENT ROW)
  6114. FROM t7
  6115. ORDER BY x-y+x/10, x;
  6116. ----
  6117. -1 1 2 2 2 2 {2}
  6118. -1 5 6 12 10 2 {2,10}
  6119. -1 7 8 26 14 2 {2,10,14}
  6120. 0 13 14 26 26 26 {26}
  6121. 0 15 16 56 30 26 {26,30}
  6122. 0 17 18 90 34 26 {26,30,34}
  6123. 51 10 -40 20 20 20 {20}
  6124. 61 10 -50 20 20 20 {20}
  6125. NULL 3 NULL 6 6 6 {6}
  6126. NULL 9 NULL 24 18 6 {6,18}
  6127. NULL 11 NULL 46 22 6 {6,18,22}
  6128. # ROWS BETWEEN CURRENT ROW AND offset FOLLOWING
  6129. query IIIIIIT
  6130. SELECT
  6131. x-y+x/10,
  6132. x,
  6133. y,
  6134. sum(x+x) OVER (PARTITION BY x-y+x/10 ORDER BY x ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING),
  6135. max(x+x) OVER (PARTITION BY x-y+x/10 ORDER BY x ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING),
  6136. min(x+x) OVER (PARTITION BY x-y+x/10 ORDER BY x ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING),
  6137. array_agg(x+x) OVER (PARTITION BY x-y+x/10 ORDER BY x ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING)
  6138. FROM t7
  6139. ORDER BY x-y+x/10, x;
  6140. ----
  6141. -1 1 2 12 10 2 {2,10}
  6142. -1 5 6 24 14 10 {10,14}
  6143. -1 7 8 14 14 14 {14}
  6144. 0 13 14 56 30 26 {26,30}
  6145. 0 15 16 64 34 30 {30,34}
  6146. 0 17 18 34 34 34 {34}
  6147. 51 10 -40 20 20 20 {20}
  6148. 61 10 -50 20 20 20 {20}
  6149. NULL 3 NULL 24 18 6 {6,18}
  6150. NULL 9 NULL 40 22 18 {18,22}
  6151. NULL 11 NULL 22 22 22 {22}
  6152. # ROWS BETWEEN CURRENT ROW AND CURRENT ROW
  6153. query IIIIIIT
  6154. SELECT
  6155. x-y+x/10,
  6156. x,
  6157. y,
  6158. sum(x+x) OVER (PARTITION BY x-y+x/10 ORDER BY x ROWS BETWEEN CURRENT ROW AND CURRENT ROW),
  6159. max(x+x) OVER (PARTITION BY x-y+x/10 ORDER BY x ROWS BETWEEN CURRENT ROW AND CURRENT ROW),
  6160. min(x+x) OVER (PARTITION BY x-y+x/10 ORDER BY x ROWS BETWEEN CURRENT ROW AND CURRENT ROW),
  6161. array_agg(x+x) OVER (PARTITION BY x-y+x/10 ORDER BY x ROWS BETWEEN CURRENT ROW AND CURRENT ROW)
  6162. FROM t7
  6163. ORDER BY x-y+x/10, x;
  6164. ----
  6165. -1 1 2 2 2 2 {2}
  6166. -1 5 6 10 10 10 {10}
  6167. -1 7 8 14 14 14 {14}
  6168. 0 13 14 26 26 26 {26}
  6169. 0 15 16 30 30 30 {30}
  6170. 0 17 18 34 34 34 {34}
  6171. 51 10 -40 20 20 20 {20}
  6172. 61 10 -50 20 20 20 {20}
  6173. NULL 3 NULL 6 6 6 {6}
  6174. NULL 9 NULL 18 18 18 {18}
  6175. NULL 11 NULL 22 22 22 {22}
  6176. ## Tests for `AggregateExpr::on_unique`
  6177. # In the next several tests, the global aggregates make the input column to the window aggregate a key, so
  6178. # `ReduceElision` invokes `on_unique`.
  6179. #
  6180. # First, let's see that `ReduceElision` indeed kicks in, by observing that the Reduce of the window aggregation is not
  6181. # present in the plan. (There is 1 Reduce present from the global aggregation.)
  6182. #
  6183. # Note that in these tests that rely on the window function's PARTITION BY being a key, we unfortunately can't test more
  6184. # than one window function call in one test, because we currently forget the key information after a window function
  6185. # call (even when `ReduceElision` simplifies the window function call).
  6186. # TODO: Add an optimization that eliminates a Map-FlatMap pair where the Map is just creating a 1-element list on which
  6187. # the FlatMap is immediately calling `unnest_list`.
  6188. query T multiline
  6189. EXPLAIN OPTIMIZED PLAN WITH(keys, humanized expressions) AS VERBOSE TEXT FOR
  6190. SELECT
  6191. sum(sum(x)) OVER ()
  6192. FROM t7;
  6193. ----
  6194. Explained Query:
  6195. With
  6196. cte l0 =
  6197. Reduce aggregates=[sum(#0{x})] // { keys: "([])" }
  6198. Project (#0{x}) // { keys: "()" }
  6199. ReadStorage materialize.public.t7 // { keys: "()" }
  6200. Return // { keys: "()" }
  6201. Project (#2) // { keys: "()" }
  6202. Map (record_get[0](#1)) // { keys: "()" }
  6203. FlatMap unnest_list(#0) // { keys: "()" }
  6204. Project (#1) // { keys: "([])" }
  6205. Map (list[row(bigint_to_numeric(#0{sum_x}), row(#0{sum_x}))]) // { keys: "([])" }
  6206. Union // { keys: "([])" }
  6207. Get l0 // { keys: "([])" }
  6208. Map (null) // { keys: "()" }
  6209. Union // { keys: "()" }
  6210. Negate // { keys: "()" }
  6211. Project () // { keys: "([])" }
  6212. Get l0 // { keys: "([])" }
  6213. Constant // { keys: "([])" }
  6214. - ()
  6215. Source materialize.public.t7
  6216. Target cluster: quickstart
  6217. EOF
  6218. query I
  6219. SELECT
  6220. sum(sum(x)) OVER ()
  6221. FROM t7;
  6222. ----
  6223. 101
  6224. query I
  6225. SELECT
  6226. min(count(x)) OVER ()
  6227. FROM t7;
  6228. ----
  6229. 11
  6230. query I
  6231. SELECT
  6232. count(min(x)) OVER ()
  6233. FROM t7;
  6234. ----
  6235. 1
  6236. simple conn=mz_system,user=mz_system
  6237. ALTER SYSTEM SET unsafe_enable_table_keys = true
  6238. ----
  6239. COMPLETE 0
  6240. statement ok
  6241. CREATE TABLE t9(x INT UNIQUE, y INT);
  6242. statement ok
  6243. INSERT INTO t9 VALUES (1,2), (3,null), (5,6), (7,8), (9, null), (11, null), (13, 14), (15, 16), (17, 18);
  6244. # Still testing `AggregateExpr::on_unique`. Here, the uniqueness constraint on the table makes `ReduceElision` call
  6245. # `on_unique`. There should be 0 Reduce operations in the plan.
  6246. query T multiline
  6247. EXPLAIN OPTIMIZED PLAN WITH(keys, humanized expressions) AS VERBOSE TEXT FOR
  6248. SELECT
  6249. sum(y) OVER (PARTITION BY x)
  6250. FROM t9
  6251. ORDER BY x;
  6252. ----
  6253. Explained Query:
  6254. Finish order_by=[#0 asc nulls_last] output=[#2]
  6255. Project (#3..=#5) // { keys: "()" }
  6256. Map (record_get[1](#1), record_get[0](#2), record_get[1](#2), record_get[0](#1)) // { keys: "()" }
  6257. FlatMap unnest_list(#0) // { keys: "()" }
  6258. Project (#2) // { keys: "()" }
  6259. Map (list[row(integer_to_bigint(#1{y}), row(#0{x}, #1{y}))]) // { keys: "([0])" }
  6260. ReadStorage materialize.public.t9 // { keys: "([0])" }
  6261. Source materialize.public.t9
  6262. Target cluster: quickstart
  6263. EOF
  6264. query I
  6265. SELECT
  6266. sum(y) OVER (PARTITION BY x)
  6267. FROM t9
  6268. ORDER BY x;
  6269. ----
  6270. 2
  6271. NULL
  6272. 6
  6273. 8
  6274. NULL
  6275. NULL
  6276. 14
  6277. 16
  6278. 18
  6279. query I
  6280. SELECT
  6281. min(y) OVER (PARTITION BY x)
  6282. FROM t9
  6283. ORDER BY x;
  6284. ----
  6285. 2
  6286. NULL
  6287. 6
  6288. 8
  6289. NULL
  6290. NULL
  6291. 14
  6292. 16
  6293. 18
  6294. query I
  6295. SELECT
  6296. count(y) OVER (PARTITION BY x)
  6297. FROM t9
  6298. ORDER BY x;
  6299. ----
  6300. 1
  6301. 0
  6302. 1
  6303. 1
  6304. 0
  6305. 0
  6306. 1
  6307. 1
  6308. 1
  6309. query I
  6310. SELECT
  6311. count(*) OVER (PARTITION BY x)
  6312. FROM t9
  6313. ORDER BY x;
  6314. ----
  6315. 1
  6316. 1
  6317. 1
  6318. 1
  6319. 1
  6320. 1
  6321. 1
  6322. 1
  6323. 1
  6324. query T
  6325. SELECT
  6326. array_agg(y) OVER (PARTITION BY x)
  6327. FROM t9
  6328. ORDER BY x;
  6329. ----
  6330. {2}
  6331. {NULL}
  6332. {6}
  6333. {8}
  6334. {NULL}
  6335. {NULL}
  6336. {14}
  6337. {16}
  6338. {18}
  6339. query R
  6340. SELECT
  6341. avg(y+y) OVER (PARTITION BY x)
  6342. FROM t9
  6343. ORDER BY x;
  6344. ----
  6345. 4
  6346. NULL
  6347. 12
  6348. 16
  6349. NULL
  6350. NULL
  6351. 28
  6352. 32
  6353. 36
  6354. # Test that, for window aggregations, `on_unique` is properly calling `wrapped_aggregate.default()` when the frame is
  6355. # empty (and not just returns null as the other window function cases in `on_unique`).
  6356. query I
  6357. SELECT
  6358. count(y) OVER (PARTITION BY x ROWS BETWEEN 2 PRECEDING AND 1 PRECEDING)
  6359. FROM t9
  6360. ORDER BY x;
  6361. ----
  6362. 0
  6363. 0
  6364. 0
  6365. 0
  6366. 0
  6367. 0
  6368. 0
  6369. 0
  6370. 0
  6371. query I
  6372. SELECT
  6373. sum(y) OVER (PARTITION BY x ROWS BETWEEN 2 PRECEDING AND 1 PRECEDING)
  6374. FROM t9
  6375. ORDER BY x;
  6376. ----
  6377. NULL
  6378. NULL
  6379. NULL
  6380. NULL
  6381. NULL
  6382. NULL
  6383. NULL
  6384. NULL
  6385. NULL
  6386. query II
  6387. SELECT
  6388. count(y) OVER (PARTITION BY x ROWS BETWEEN 2 PRECEDING AND 1 PRECEDING),
  6389. sum(y) OVER (PARTITION BY x ROWS BETWEEN 2 PRECEDING AND 1 PRECEDING)
  6390. FROM t9
  6391. ORDER BY x;
  6392. ----
  6393. 0 NULL
  6394. 0 NULL
  6395. 0 NULL
  6396. 0 NULL
  6397. 0 NULL
  6398. 0 NULL
  6399. 0 NULL
  6400. 0 NULL
  6401. 0 NULL
  6402. query T
  6403. SELECT
  6404. array_agg(y) OVER (PARTITION BY x ROWS BETWEEN 2 PRECEDING AND 1 PRECEDING)
  6405. FROM t9
  6406. ORDER BY x;
  6407. ----
  6408. NULL
  6409. NULL
  6410. NULL
  6411. NULL
  6412. NULL
  6413. NULL
  6414. NULL
  6415. NULL
  6416. NULL
  6417. query R
  6418. SELECT
  6419. avg(y+y) OVER (PARTITION BY x ROWS BETWEEN 2 PRECEDING AND 1 PRECEDING)
  6420. FROM t9
  6421. ORDER BY x;
  6422. ----
  6423. NULL
  6424. NULL
  6425. NULL
  6426. NULL
  6427. NULL
  6428. NULL
  6429. NULL
  6430. NULL
  6431. NULL
  6432. # Test `on_unique` for lag/lead
  6433. query T multiline
  6434. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR
  6435. SELECT
  6436. lag(x,y,100) OVER (PARTITION BY x ORDER BY y+y)
  6437. FROM t9;
  6438. ----
  6439. Explained Query:
  6440. Project (#2)
  6441. Map (record_get[0](#1))
  6442. FlatMap unnest_list(#0)
  6443. Project (#2)
  6444. Map (list[row(case when (#1{y}) IS NULL then null else case when (#1{y} = 0) then #0{x} else 100 end end, row(#0{x}, #1{y}))])
  6445. ReadStorage materialize.public.t9
  6446. Source materialize.public.t9
  6447. Target cluster: quickstart
  6448. EOF
  6449. query I rowsort
  6450. SELECT
  6451. lag(x,y,100) OVER (PARTITION BY x ORDER BY y+y)
  6452. FROM t9;
  6453. ----
  6454. NULL
  6455. NULL
  6456. NULL
  6457. 100
  6458. 100
  6459. 100
  6460. 100
  6461. 100
  6462. 100
  6463. query I rowsort
  6464. SELECT
  6465. lead(x,y-2,120) OVER (PARTITION BY x ORDER BY x)
  6466. FROM t9;
  6467. ----
  6468. NULL
  6469. NULL
  6470. NULL
  6471. 1
  6472. 120
  6473. 120
  6474. 120
  6475. 120
  6476. 120
  6477. # Test `on_unique` for `FusedValueWindowFunc`. There should be NO window function call in the following plan.
  6478. query T multiline
  6479. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR
  6480. SELECT
  6481. x,
  6482. y,
  6483. lag(x,y,100) OVER (PARTITION BY x ORDER BY y+y),
  6484. lag(x,y,110) OVER (PARTITION BY x ORDER BY y+y),
  6485. lag(x,y,100) OVER (PARTITION BY x ORDER BY y+y),
  6486. first_value(y) OVER (PARTITION BY x ORDER BY y+y),
  6487. last_value(y) OVER (PARTITION BY x ORDER BY y+y),
  6488. lead(x,y-2,120) OVER (PARTITION BY x ORDER BY y+y)
  6489. FROM t9
  6490. ORDER BY x;
  6491. ----
  6492. Explained Query:
  6493. Finish order_by=[#0 asc nulls_last] output=[#0..=#7]
  6494. Project (#3, #4, #9, #10, #9, #8, #7, #6)
  6495. Map (record_get[1](#1), record_get[0](#2), record_get[1](#2), record_get[0](#1), record_get[0](#5), record_get[1](#5), record_get[2](#5), record_get[3](#5), record_get[4](#5))
  6496. FlatMap unnest_list(#0)
  6497. Project (#3)
  6498. Map ((#1{y}) IS NULL, list[row(row(case when #2 then null else case when (0 = (#1{y} - 2)) then #0{x} else 120 end end, #1{y}, #1{y}, case when #2 then null else case when (#1{y} = 0) then #0{x} else 100 end end, case when #2 then null else case when (#1{y} = 0) then #0{x} else 110 end end), row(#0{x}, #1{y}))])
  6499. ReadStorage materialize.public.t9
  6500. Source materialize.public.t9
  6501. Target cluster: quickstart
  6502. EOF
  6503. query IIIIIIII
  6504. SELECT
  6505. x,
  6506. y,
  6507. lag(x,y,100) OVER (PARTITION BY x ORDER BY y+y),
  6508. lag(x,y,110) OVER (PARTITION BY x ORDER BY y+y),
  6509. lag(x,y,100) OVER (PARTITION BY x ORDER BY y+y),
  6510. first_value(y) OVER (PARTITION BY x ORDER BY y+y),
  6511. last_value(y) OVER (PARTITION BY x ORDER BY y+y),
  6512. lead(x,y-2,120) OVER (PARTITION BY x ORDER BY y+y)
  6513. FROM t9
  6514. ORDER BY x;
  6515. ----
  6516. 1 2 100 110 100 2 2 1
  6517. 3 NULL NULL NULL NULL NULL NULL NULL
  6518. 5 6 100 110 100 6 6 120
  6519. 7 8 100 110 100 8 8 120
  6520. 9 NULL NULL NULL NULL NULL NULL NULL
  6521. 11 NULL NULL NULL NULL NULL NULL NULL
  6522. 13 14 100 110 100 14 14 120
  6523. 15 16 100 110 100 16 16 120
  6524. 17 18 100 110 100 18 18 120
  6525. ## Complex tests with array_agg and then unnesting the array
  6526. query R
  6527. SELECT avg(u)
  6528. FROM (
  6529. SELECT x, unnest(l) AS u
  6530. FROM (
  6531. SELECT x, array_agg(y) OVER (PARTITION BY x%5) AS l
  6532. FROM t7
  6533. ) AS ff1
  6534. ) AS ff2
  6535. GROUP BY x%5
  6536. ORDER BY x%5;
  6537. ----
  6538. -17
  6539. 2
  6540. 13
  6541. 14
  6542. NULL
  6543. query IBRR
  6544. WITH
  6545. array_agg_unnest_grouped_agg AS (
  6546. SELECT x%5 AS k1, avg(u) AS avg1
  6547. FROM (
  6548. SELECT x, unnest(l) AS u
  6549. FROM (
  6550. SELECT x, array_agg(y) OVER (PARTITION BY x%5) AS l
  6551. FROM t7
  6552. ) AS ff1
  6553. ) AS ff2
  6554. GROUP BY x%5
  6555. ),
  6556. grouped_agg AS (
  6557. SELECT x%5 AS k2, avg(y) AS avg2
  6558. FROM t7
  6559. GROUP BY x%5
  6560. )
  6561. SELECT k1, avg1 = avg2, avg1, avg2
  6562. FROM array_agg_unnest_grouped_agg, grouped_agg
  6563. WHERE k1 = k2 AND avg1 IS NOT NULL
  6564. ORDER BY k1;
  6565. ----
  6566. 0 true -17 -17
  6567. 1 true 2 2
  6568. 2 true 13 13
  6569. 3 true 14 14
  6570. query I
  6571. WITH
  6572. array_agg AS (
  6573. SELECT x%5, array_agg(y) OVER (PARTITION BY x%5) AS l
  6574. FROM t7
  6575. )
  6576. SELECT s
  6577. FROM
  6578. array_agg,
  6579. LATERAL (
  6580. SELECT sum(ul) AS s
  6581. FROM (SELECT unnest(l) AS ul FROM array_agg) AS sq1
  6582. ) AS sq2;
  6583. ----
  6584. -188
  6585. -188
  6586. -188
  6587. -188
  6588. -188
  6589. -188
  6590. -188
  6591. -188
  6592. -188
  6593. -188
  6594. -188
  6595. query IRRB
  6596. WITH
  6597. simple AS (
  6598. SELECT
  6599. x%5 AS x5_simple,
  6600. avg(y) OVER (PARTITION BY x%5) AS avg_simple
  6601. FROM t7
  6602. ),
  6603. complicated AS ( -- array_agg, then do an unnest and global agg in a subquery in a SELECT
  6604. WITH
  6605. array_agg AS (
  6606. SELECT
  6607. x%5 AS x5,
  6608. array_agg(y) OVER (PARTITION BY x%5) AS l
  6609. FROM t7
  6610. )
  6611. SELECT
  6612. x5 AS x5_complicated,
  6613. (
  6614. SELECT avg(uy)
  6615. FROM unnest(l) AS uy
  6616. ) AS avg_complicated
  6617. FROM array_agg
  6618. )
  6619. SELECT DISTINCT
  6620. x5_simple,
  6621. avg_simple,
  6622. avg_complicated,
  6623. avg_simple = avg_complicated
  6624. FROM simple, complicated
  6625. WHERE x5_simple = x5_complicated
  6626. ORDER BY x5_simple;
  6627. ----
  6628. 0 -17 -17 true
  6629. 1 2 2 true
  6630. 2 13 13 true
  6631. 3 14 14 true
  6632. 4 NULL NULL NULL
  6633. # Regression test for https://github.com/MaterializeInc/materialize/pull/22270#issuecomment-1761432124
  6634. statement ok
  6635. SET cluster_replica = r1
  6636. statement error db error: ERROR: Evaluation error: "4294967295 \* 2" uint4 out of range
  6637. select max(1) over (partition by 1 order by 1, 1)
  6638. from
  6639. (select
  6640. subq_2."c9" as c0
  6641. from
  6642. (select
  6643. subq_1."c0" as c0,
  6644. subq_1."c2" as c1,
  6645. subq_1."c4" as c2,
  6646. subq_1."c2" as c3,
  6647. subq_1."c1" as c4,
  6648. subq_1."c0" as c5,
  6649. subq_1."c5" as c6,
  6650. subq_1."c0" as c7,
  6651. subq_1."c5" as c8,
  6652. subq_1."c1" as c9,
  6653. subq_1."c2" as c10,
  6654. 87 as c11,
  6655. subq_1."c1" as c12
  6656. from
  6657. (select
  6658. subq_0."c1" as c0,
  6659. subq_0."c0" as c1,
  6660. subq_0."c1" as c2,
  6661. subq_0."c0" as c3,
  6662. subq_0."c1" as c4,
  6663. subq_0."c0" as c5
  6664. from
  6665. (select
  6666. ref_0."form_of_use" as c0,
  6667. ref_0."default_collate_schema" as c1
  6668. from
  6669. information_schema.character_sets as ref_0
  6670. ) as subq_0
  6671. ) as subq_1
  6672. ) as subq_2
  6673. ) as subq_3
  6674. where (case when (((select "collection_timestamp" from mz_internal.mz_storage_usage_by_shard limit 1 offset 4)
  6675. ) < ((select pg_catalog.min("occurred_at") from mz_internal.mz_source_status_history)
  6676. ))
  6677. and ((case when subq_3."c0" is NULL then TIMESTAMPTZ '2023-01-01 01:23:45+06' else TIMESTAMPTZ '2023-01-01 01:23:45+06' end
  6678. ) <> (pg_catalog.now())) then case when (((select pg_catalog.sum("connection_id") from mz_internal.mz_sessions)
  6679. ) - (0::uint8)) >= ((select "worker_id" from mz_introspection.mz_arrangement_sharing_per_worker limit 1 offset 3)
  6680. ) then numrange(0,0) else numrange(0,0) end
  6681. else case when (((select pg_catalog.sum("connection_id") from mz_internal.mz_sessions)
  6682. ) - (0::uint8)) >= ((select "worker_id" from mz_introspection.mz_arrangement_sharing_per_worker limit 1 offset 3)
  6683. ) then numrange(0,0) else numrange(0,0) end
  6684. end
  6685. ) >= (case when (0::uint4) > (pg_catalog.mod(
  6686. CAST((select 1 from mz_internal.mz_subscriptions limit 1 offset 2)
  6687. as uint4),
  6688. CAST((4294967295::uint4) * (2::uint4) as uint4))) then case when (mz_catalog.map_length(
  6689. mz_internal.mz_role_oid_memberships())) = ((select pg_catalog.min("rolconnlimit") from pg_catalog.pg_roles)
  6690. ) then case when (null::uint4) = (null::uint4) then numrange(0,0) else numrange(0,0) end
  6691. else case when (null::uint4) = (null::uint4) then numrange(0,0) else numrange(0,0) end
  6692. end
  6693. else case when (mz_catalog.map_length(
  6694. mz_internal.mz_role_oid_memberships())) = ((select pg_catalog.min("rolconnlimit") from pg_catalog.pg_roles)
  6695. ) then case when (null::uint4) = (null::uint4) then numrange(0,0) else numrange(0,0) end
  6696. else case when (null::uint4) = (null::uint4) then numrange(0,0) else numrange(0,0) end
  6697. end
  6698. end
  6699. );
  6700. ## Tests for window function fusion
  6701. query T multiline
  6702. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR
  6703. SELECT
  6704. *,
  6705. lag(x) OVER (ORDER BY x),
  6706. first_value(x) OVER (ORDER BY x),
  6707. last_value(x) OVER (ORDER BY x),
  6708. x*y,
  6709. lag(y) OVER (ORDER BY x),
  6710. lag(x+x,1,null) OVER (PARTITION BY x ORDER BY y),
  6711. lead(x,2,null) OVER (PARTITION BY x ORDER BY -y NULLS FIRST),
  6712. lead(x,2,null) OVER (PARTITION BY x ORDER BY -y NULLS LAST),
  6713. x+y,
  6714. lag(x+x,2,null) OVER (PARTITION BY x ORDER BY y),
  6715. sum(x) OVER (ORDER BY x),
  6716. min(x) OVER (ORDER BY x),
  6717. max(x) OVER (ORDER BY y)
  6718. FROM t7
  6719. ORDER BY x,y;
  6720. ----
  6721. Explained Query:
  6722. Finish order_by=[#0 asc nulls_last, #1 asc nulls_last] output=[#0..=#14]
  6723. Project (#3, #4, #16, #15, #14, #17, #13, #9, #11, #10, #18, #8, #7, #6, #5)
  6724. Map (record_get[1](#1), record_get[0](#2), record_get[1](#2), record_get[2](#2), record_get[4](#2), record_get[5](#2), record_get[7](#2), record_get[8](#2), record_get[10](#2), record_get[12](#2), record_get[0](#1), record_get[0](#12), record_get[1](#12), record_get[2](#12), record_get[3](#12), (#3{x} * #4{y}), (#3{x} + #4{y}))
  6725. FlatMap unnest_list(#0{fused_value_window_func})
  6726. Reduce aggregates=[fused_value_window_func[lag[order_by=[#0 asc nulls_last]], last_value[order_by=[#0 asc nulls_last]], first_value[order_by=[#0 asc nulls_last]], lag[order_by=[#0 asc nulls_last]] order_by=[#0 asc nulls_last]](row(row(row(record_get[0](record_get[1](#0)), record_get[1](record_get[1](#0)), record_get[2](record_get[1](#0)), record_get[3](record_get[1](#0)), record_get[4](record_get[1](#0)), record_get[5](record_get[1](#0)), record_get[6](record_get[1](#0)), record_get[7](record_get[1](#0)), record_get[8](record_get[1](#0)), record_get[9](record_get[1](#0)), record_get[10](record_get[1](#0)), record_get[0](#0), record_get[0](#0)), row(row(record_get[1](record_get[1](#0)), 1, null), record_get[0](record_get[1](#0)), record_get[0](record_get[1](#0)), row(record_get[0](record_get[1](#0)), 1, null))), record_get[0](record_get[1](#0))))]
  6727. Project (#1)
  6728. FlatMap unnest_list(#0{lead})
  6729. Project (#1{lead})
  6730. Reduce group_by=[record_get[0](record_get[1](#0))] aggregates=[lead[order_by=[#0 asc nulls_first]](row(row(row(record_get[0](record_get[1](#0)), record_get[1](record_get[1](#0)), record_get[2](record_get[1](#0)), record_get[3](record_get[1](#0)), record_get[4](record_get[1](#0)), record_get[5](record_get[1](#0)), record_get[6](record_get[1](#0)), record_get[7](record_get[1](#0)), record_get[8](record_get[1](#0)), record_get[0](#0), record_get[0](#0)), row(record_get[0](record_get[1](#0)), 2, null)), -(record_get[1](record_get[1](#0)))))]
  6731. Project (#1)
  6732. FlatMap unnest_list(#0{lead})
  6733. Project (#1{lead})
  6734. Reduce group_by=[record_get[0](record_get[1](#0))] aggregates=[lead[order_by=[#0 asc nulls_last]](row(row(row(record_get[0](record_get[1](#0)), record_get[1](record_get[1](#0)), record_get[2](record_get[1](#0)), record_get[3](record_get[1](#0)), record_get[4](record_get[1](#0)), record_get[5](record_get[1](#0)), record_get[0](#0), record_get[0](record_get[0](#0)), record_get[1](record_get[0](#0))), row(record_get[0](record_get[1](#0)), 2, null)), -(record_get[1](record_get[1](#0)))))]
  6735. Project (#1)
  6736. FlatMap unnest_list(#0{fused_value_window_func})
  6737. Project (#1{fused_value_window_func})
  6738. Reduce group_by=[record_get[0](record_get[1](#0))] aggregates=[fused_value_window_func[lag[order_by=[#0 asc nulls_last]], lag[order_by=[#0 asc nulls_last]] order_by=[#0 asc nulls_last]](row(row(row(record_get[0](record_get[1](#0)), record_get[1](record_get[1](#0)), record_get[3](record_get[1](#0)), record_get[0](#0), record_get[0](record_get[0](#0)), record_get[1](record_get[0](#0))), row(row((record_get[0](record_get[1](#0)) + record_get[0](record_get[1](#0))), 2, null), row((record_get[0](record_get[1](#0)) + record_get[0](record_get[1](#0))), 1, null))), record_get[1](record_get[1](#0))))]
  6739. Project (#1)
  6740. FlatMap unnest_list(#0{fused_window_agg})
  6741. Reduce aggregates=[fused_window_agg(row(row(row(record_get[0](record_get[1](#0)), record_get[1](record_get[1](#0)), record_get[0](#0), record_get[0](#0)), row(record_get[0](record_get[1](#0)), record_get[0](record_get[1](#0)))), record_get[0](record_get[1](#0))))]
  6742. Project (#1)
  6743. FlatMap unnest_list(#0{window_agg})
  6744. Reduce aggregates=[window_agg[max order_by=[#0{x} asc nulls_last]](row(row(row(#0{x}, #1{y}), #0{x}), #1{y}))]
  6745. ReadStorage materialize.public.t7
  6746. Source materialize.public.t7
  6747. Target cluster: quickstart
  6748. EOF
  6749. query IIIIIIIIIIIIIII
  6750. SELECT
  6751. *,
  6752. lag(x) OVER (ORDER BY x),
  6753. first_value(x) OVER (ORDER BY x),
  6754. last_value(x) OVER (ORDER BY x),
  6755. x*y,
  6756. lag(y) OVER (ORDER BY x),
  6757. lag(x+x,1,null) OVER (PARTITION BY x ORDER BY y),
  6758. lead(x,2,null) OVER (PARTITION BY x ORDER BY -y NULLS FIRST),
  6759. lead(x,2,null) OVER (PARTITION BY x ORDER BY -y NULLS LAST),
  6760. x+y,
  6761. lag(x+x,2,null) OVER (PARTITION BY x ORDER BY y),
  6762. sum(x) OVER (ORDER BY x),
  6763. min(x) OVER (ORDER BY x),
  6764. max(x) OVER (ORDER BY y)
  6765. FROM t7
  6766. ORDER BY x,y;
  6767. ----
  6768. 1 2 NULL 1 1 2 NULL NULL NULL NULL 3 NULL 1 1 10
  6769. 3 NULL 1 1 3 NULL 2 NULL NULL NULL NULL NULL 4 1 17
  6770. 5 6 3 1 5 30 NULL NULL NULL NULL 11 NULL 9 1 10
  6771. 7 8 5 1 7 56 6 NULL NULL NULL 15 NULL 16 1 10
  6772. 9 NULL 7 1 9 NULL 8 NULL NULL NULL NULL NULL 25 1 17
  6773. 10 -50 9 1 10 -500 NULL NULL NULL NULL -40 NULL 45 1 10
  6774. 10 -40 10 1 10 -400 -50 20 NULL NULL -30 NULL 45 1 10
  6775. 11 NULL 10 1 11 NULL -40 NULL NULL NULL NULL NULL 56 1 17
  6776. 13 14 11 1 13 182 NULL NULL NULL NULL 27 NULL 69 1 13
  6777. 15 16 13 1 15 240 14 NULL NULL NULL 31 NULL 84 1 15
  6778. 17 18 15 1 17 306 16 NULL NULL NULL 35 NULL 101 1 17
  6779. # In the following query, currently we only fuse the two inner `lag`s.
  6780. # In theory, we could also fuse the two `last_value`s.
  6781. query T multiline
  6782. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR
  6783. SELECT
  6784. *,
  6785. first_value(
  6786. lag(x*x,y,1111) OVER (PARTITION BY x ORDER BY y)
  6787. ) OVER (PARTITION BY x ORDER BY y),
  6788. x*y,
  6789. last_value(
  6790. lag(x*x,y,2222) OVER (PARTITION BY x ORDER BY y)
  6791. ) OVER (PARTITION BY x ORDER BY y+y),
  6792. last_value(x+y) OVER (PARTITION BY x ORDER BY y+y),
  6793. lag(y) OVER (ORDER BY x),
  6794. x+y
  6795. FROM t7
  6796. ORDER BY x,y;
  6797. ----
  6798. Explained Query:
  6799. Finish order_by=[#0 asc nulls_last, #1 asc nulls_last] output=[#0..=#7]
  6800. Project (#3, #4, #8, #9, #7, #6, #5, #10)
  6801. Map (record_get[1](#1), record_get[0](#2), record_get[1](#2), record_get[2](#2), record_get[3](#2), record_get[8](#2), record_get[0](#1), (#3{x} * #4{y}), (#3{x} + #4{y}))
  6802. FlatMap unnest_list(#0{first_value})
  6803. Project (#1{first_value})
  6804. Reduce group_by=[record_get[0](record_get[1](#0))] aggregates=[first_value[order_by=[#0 asc nulls_last]](row(row(row(record_get[0](record_get[1](#0)), record_get[1](record_get[1](#0)), record_get[2](record_get[1](#0)), record_get[3](record_get[1](#0)), record_get[4](record_get[1](#0)), record_get[5](record_get[1](#0)), record_get[6](record_get[1](#0)), record_get[0](#0), record_get[0](#0)), record_get[6](record_get[1](#0))), record_get[1](record_get[1](#0))))]
  6805. Project (#1)
  6806. FlatMap unnest_list(#0{last_value})
  6807. Project (#1{last_value})
  6808. Reduce group_by=[record_get[0](record_get[1](#0))] aggregates=[last_value[order_by=[#0 asc nulls_last]](row(row(row(record_get[0](record_get[1](#0)), record_get[1](record_get[1](#0)), record_get[3](record_get[1](#0)), record_get[5](record_get[1](#0)), record_get[0](#0), record_get[0](record_get[0](#0)), record_get[1](record_get[0](#0))), record_get[0](record_get[0](#0))), (record_get[1](record_get[1](#0)) + record_get[1](record_get[1](#0)))))]
  6809. Project (#1)
  6810. FlatMap unnest_list(#0{fused_value_window_func})
  6811. Project (#1{fused_value_window_func})
  6812. Reduce group_by=[record_get[0](record_get[1](#0))] aggregates=[fused_value_window_func[lag[order_by=[#0 asc nulls_last]], lag[order_by=[#0 asc nulls_last]] order_by=[#0 asc nulls_last]](row(row(row(record_get[0](record_get[1](#0)), record_get[1](record_get[1](#0)), record_get[2](record_get[1](#0)), record_get[3](record_get[1](#0)), record_get[0](#0), record_get[0](#0)), row(row((record_get[0](record_get[1](#0)) * record_get[0](record_get[1](#0))), record_get[1](record_get[1](#0)), 2222), row((record_get[0](record_get[1](#0)) * record_get[0](record_get[1](#0))), record_get[1](record_get[1](#0)), 1111))), record_get[1](record_get[1](#0))))]
  6813. Project (#1)
  6814. FlatMap unnest_list(#0{last_value})
  6815. Project (#1{last_value})
  6816. Reduce group_by=[record_get[0](record_get[1](#0))] aggregates=[last_value[order_by=[#0 asc nulls_last]](row(row(row(record_get[0](record_get[1](#0)), record_get[1](record_get[1](#0)), record_get[0](#0), record_get[0](#0)), (record_get[0](record_get[1](#0)) + record_get[1](record_get[1](#0)))), (record_get[1](record_get[1](#0)) + record_get[1](record_get[1](#0)))))]
  6817. Project (#1)
  6818. FlatMap unnest_list(#0{lag})
  6819. Reduce aggregates=[lag[order_by=[#0{x} asc nulls_last]](row(row(row(#0{x}, #1{y}), row(#1{y}, 1, null)), #0{x}))]
  6820. ReadStorage materialize.public.t7
  6821. Source materialize.public.t7
  6822. Target cluster: quickstart
  6823. EOF
  6824. query IIIIIIII
  6825. SELECT
  6826. *,
  6827. first_value(
  6828. lag(x*x,y,1111) OVER (PARTITION BY x ORDER BY y)
  6829. ) OVER (PARTITION BY x ORDER BY y),
  6830. x*y,
  6831. last_value(
  6832. lag(x*x,y,2222) OVER (PARTITION BY x ORDER BY y)
  6833. ) OVER (PARTITION BY x ORDER BY y+y),
  6834. last_value(x+y) OVER (PARTITION BY x ORDER BY y+y),
  6835. lag(y) OVER (ORDER BY x),
  6836. x+y
  6837. FROM t7
  6838. ORDER BY x,y;
  6839. ----
  6840. 1 2 1111 2 2222 3 NULL 3
  6841. 3 NULL NULL NULL NULL NULL 2 NULL
  6842. 5 6 1111 30 2222 11 NULL 11
  6843. 7 8 1111 56 2222 15 6 15
  6844. 9 NULL NULL NULL NULL NULL 8 NULL
  6845. 10 -50 1111 -500 2222 -40 NULL -40
  6846. 10 -40 1111 -400 2222 -30 -50 -30
  6847. 11 NULL NULL NULL NULL NULL -40 NULL
  6848. 13 14 1111 182 2222 27 NULL 27
  6849. 15 16 1111 240 2222 31 14 31
  6850. 17 18 1111 306 2222 35 16 35
  6851. query T multiline
  6852. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR
  6853. SELECT
  6854. *,
  6855. lag(x) OVER (),
  6856. lag(y) OVER (),
  6857. sum(x) OVER (),
  6858. min(x) OVER ()
  6859. FROM t7
  6860. ORDER BY x,y;
  6861. ----
  6862. Explained Query:
  6863. Finish order_by=[#0 asc nulls_last, #1 asc nulls_last] output=[#0..=#5]
  6864. Project (#3, #4, #9, #8, #6, #5)
  6865. Map (record_get[1](#1), record_get[0](#2), record_get[1](#2), record_get[3](#2), record_get[4](#2), record_get[0](#1), record_get[0](#7), record_get[1](#7))
  6866. FlatMap unnest_list(#0{fused_value_window_func})
  6867. Reduce aggregates=[fused_value_window_func[lag[order_by=[]], lag[order_by=[]] order_by=[]](row(row(row(record_get[0](record_get[1](#0)), record_get[1](record_get[1](#0)), record_get[0](#0), record_get[0](record_get[0](#0)), record_get[1](record_get[0](#0))), row(row(record_get[1](record_get[1](#0)), 1, null), row(record_get[0](record_get[1](#0)), 1, null)))))]
  6868. Project (#1)
  6869. FlatMap unnest_list(#0{fused_window_agg})
  6870. Reduce aggregates=[fused_window_agg(row(row(row(#0{x}, #1{y}), row(#0{x}, #0{x}))))]
  6871. ReadStorage materialize.public.t7
  6872. Source materialize.public.t7
  6873. Target cluster: quickstart
  6874. EOF
  6875. # Test the situation when the MFP above is fused into the Reduce, and the MFP can error.
  6876. query III
  6877. SELECT *
  6878. FROM (
  6879. SELECT *, lag(x) OVER (ORDER BY x) AS l
  6880. FROM t7
  6881. )
  6882. WHERE l/2 < 7
  6883. ORDER BY x,y,l;
  6884. ----
  6885. 3 NULL 1
  6886. 5 6 3
  6887. 7 8 5
  6888. 9 NULL 7
  6889. 10 -50 9
  6890. 10 -40 10
  6891. 11 NULL 10
  6892. 13 14 11
  6893. 15 16 13
  6894. # Test the situation when the MFP above is fused into the Reduce, but the MFP can't error.
  6895. query III
  6896. SELECT *
  6897. FROM (
  6898. SELECT *, lag(x) OVER (ORDER BY x) AS l
  6899. FROM t7
  6900. )
  6901. WHERE l < 14
  6902. ORDER BY x,y,l;
  6903. ----
  6904. 3 NULL 1
  6905. 5 6 3
  6906. 7 8 5
  6907. 9 NULL 7
  6908. 10 -50 9
  6909. 10 -40 10
  6910. 11 NULL 10
  6911. 13 14 11
  6912. 15 16 13
  6913. ## Check some LIR plans that the optimization of fusing `Reduce` with `FlatMap UnnestList` happens.
  6914. ## https://github.com/MaterializeInc/materialize/pull/29554
  6915. ## These should show `fused_unnest_list=true`.
  6916. # Simple situation
  6917. query T multiline
  6918. EXPLAIN PHYSICAL PLAN AS VERBOSE TEXT FOR
  6919. SELECT
  6920. lead(x,y-2,120) OVER (ORDER BY x)
  6921. FROM t9;
  6922. ----
  6923. Explained Query:
  6924. Mfp
  6925. project=(#1)
  6926. map=(record_get[0](#0))
  6927. input_key=
  6928. Reduce::Basic
  6929. aggr=(0, lead[order_by=[#0 asc nulls_last]](row(row(row(#0, #1), row(#0{x}, (#1{y} - 2), 120)), #0{x})), fused_unnest_list=true)
  6930. val_plan
  6931. project=(#2)
  6932. map=(row(row(row(#0, #1), row(#0{x}, (#1{y} - 2), 120)), #0{x}))
  6933. key_plan
  6934. project=()
  6935. Get::PassArrangements materialize.public.t9
  6936. raw=true
  6937. Source materialize.public.t9
  6938. Target cluster: quickstart
  6939. EOF
  6940. # PARTITION BY
  6941. query T multiline
  6942. EXPLAIN PHYSICAL PLAN AS VERBOSE TEXT FOR
  6943. SELECT
  6944. lead(x,y-2,120) OVER (PARTITION BY x/2 ORDER BY x)
  6945. FROM t9;
  6946. ----
  6947. Explained Query:
  6948. Mfp
  6949. project=(#2)
  6950. map=(record_get[0](#1))
  6951. input_key=#0
  6952. Reduce::Basic
  6953. aggr=(0, lead[order_by=[#0 asc nulls_last]](row(row(row(#0, #1), row(#0{x}, (#1{y} - 2), 120)), #0{x})), fused_unnest_list=true)
  6954. val_plan
  6955. project=(#2)
  6956. map=(row(row(row(#0, #1), row(#0{x}, (#1{y} - 2), 120)), #0{x}))
  6957. key_plan
  6958. project=(#2)
  6959. map=((#0{x} / 2))
  6960. Get::PassArrangements materialize.public.t9
  6961. raw=true
  6962. Source materialize.public.t9
  6963. Target cluster: quickstart
  6964. EOF
  6965. # PARTITION BY multiple columns
  6966. query T multiline
  6967. EXPLAIN PHYSICAL PLAN AS VERBOSE TEXT FOR
  6968. SELECT
  6969. lead(x,y-2,120) OVER (PARTITION BY x/2, y ORDER BY x)
  6970. FROM t9;
  6971. ----
  6972. Explained Query:
  6973. Mfp
  6974. project=(#3)
  6975. map=(record_get[0](#2))
  6976. input_key=#0, #1
  6977. Reduce::Basic
  6978. aggr=(0, lead[order_by=[#0 asc nulls_last]](row(row(row(#0, #1), row(#0{x}, (#1{y} - 2), 120)), #0{x})), fused_unnest_list=true)
  6979. val_plan
  6980. project=(#2)
  6981. map=(row(row(row(#0, #1), row(#0{x}, (#1{y} - 2), 120)), #0{x}))
  6982. key_plan
  6983. project=(#2, #1)
  6984. map=((#0{x} / 2))
  6985. Get::PassArrangements materialize.public.t9
  6986. raw=true
  6987. Source materialize.public.t9
  6988. Target cluster: quickstart
  6989. EOF
  6990. # row_number()
  6991. query T multiline
  6992. EXPLAIN PHYSICAL PLAN AS VERBOSE TEXT FOR
  6993. SELECT
  6994. row_number() OVER (ORDER BY x),
  6995. x
  6996. FROM t7
  6997. ORDER BY row_number
  6998. ----
  6999. Explained Query:
  7000. Finish order_by=[#0 asc nulls_last] output=[#0, #1]
  7001. Mfp
  7002. project=(#2, #1)
  7003. map=(record_get[0](record_get[1](#0)), record_get[0](#0))
  7004. input_key=
  7005. Reduce::Basic
  7006. aggr=(0, row_number[order_by=[#0 asc nulls_last]](row(list[row(#0, #1)], #0{x})), fused_unnest_list=true)
  7007. val_plan
  7008. project=(#2)
  7009. map=(row(list[row(#0, #1)], #0{x}))
  7010. key_plan
  7011. project=()
  7012. Get::PassArrangements materialize.public.t7
  7013. raw=true
  7014. Source materialize.public.t7
  7015. Target cluster: quickstart
  7016. EOF
  7017. # MFP after is fused into the Reduce
  7018. query T multiline
  7019. EXPLAIN PHYSICAL PLAN AS VERBOSE TEXT FOR
  7020. SELECT *
  7021. FROM (
  7022. SELECT *, lag(x) OVER (ORDER BY x) AS l
  7023. FROM t7
  7024. )
  7025. WHERE l < 14
  7026. ORDER BY x,y,l;
  7027. ----
  7028. Explained Query:
  7029. Finish order_by=[#0 asc nulls_last, #1 asc nulls_last, #2 asc nulls_last] output=[#0..=#2]
  7030. Mfp
  7031. project=(#3, #4, #1)
  7032. map=(record_get[0](#0), record_get[1](#0), record_get[0](#2), record_get[1](#2))
  7033. input_key=
  7034. Reduce::Basic
  7035. aggr=(0, lag[order_by=[#0 asc nulls_last]](row(row(row(#0, #1), row(#0{x}, 1, null)), #0{x})), fused_unnest_list=true)
  7036. val_plan
  7037. project=(#2)
  7038. map=(row(row(row(#0, #1), row(#0{x}, 1, null)), #0{x}))
  7039. key_plan
  7040. project=()
  7041. mfp_after
  7042. filter=((record_get[0](#0) < 14))
  7043. Get::PassArrangements materialize.public.t7
  7044. raw=true
  7045. Source materialize.public.t7
  7046. Target cluster: quickstart
  7047. EOF
  7048. # Two lags fused with each other + Reduce-FlatMap fusion.
  7049. query T multiline
  7050. EXPLAIN PHYSICAL PLAN AS VERBOSE TEXT FOR
  7051. SELECT
  7052. *,
  7053. lag(x) OVER (),
  7054. lag(y) OVER ()
  7055. FROM t7
  7056. ORDER BY x,y;
  7057. ----
  7058. Explained Query:
  7059. Finish order_by=[#0 asc nulls_last, #1 asc nulls_last] output=[#0..=#3]
  7060. Mfp
  7061. project=(#2, #3, #6, #5)
  7062. map=(record_get[1](#0), record_get[0](#1), record_get[1](#1), record_get[0](#0), record_get[0](#4), record_get[1](#4))
  7063. input_key=
  7064. Reduce::Basic
  7065. aggr=(0, fused_value_window_func[lag[order_by=[]], lag[order_by=[]] order_by=[]](row(row(row(#0, #1), row(row(#1{y}, 1, null), row(#0{x}, 1, null))))), fused_unnest_list=true)
  7066. val_plan
  7067. project=(#2)
  7068. map=(row(row(row(#0, #1), row(row(#1{y}, 1, null), row(#0{x}, 1, null)))))
  7069. key_plan
  7070. project=()
  7071. Get::PassArrangements materialize.public.t7
  7072. raw=true
  7073. Source materialize.public.t7
  7074. Target cluster: quickstart
  7075. EOF
  7076. ## Window functions on big relations.
  7077. statement ok
  7078. CREATE TABLE t10(t timestamptz, d1 int, d2 text, d3 int, d4 uint8, d5 int, d6 text);
  7079. statement ok
  7080. INSERT INTO t10
  7081. SELECT generate_series(1,1000)::mz_timestamp::timestamptz, 5, 'aaa', 7, 8, 9, 'bbb';
  7082. statement ok
  7083. CREATE VIEW v1 AS
  7084. SELECT t, d1, d2, EXTRACT(MILLISECOND FROM t) * 91 % 1223 AS d3, d4, d5, d6
  7085. FROM t10;
  7086. statement ok
  7087. CREATE MATERIALIZED VIEW mv1 AS
  7088. SELECT t, d1, d2, d3, d4, d6, lag(d1) OVER (ORDER BY t) - d1 AS r
  7089. FROM v1;
  7090. query I
  7091. SELECT sum(r)
  7092. FROM mv1;
  7093. ----
  7094. 0
  7095. statement ok
  7096. CREATE MATERIALIZED VIEW mv2 AS
  7097. SELECT *, row_number() OVER (ORDER BY t) AS rn
  7098. FROM mv1;
  7099. query I
  7100. SELECT sum(r)
  7101. FROM (
  7102. SELECT rn - lag(rn) OVER (ORDER BY t) AS r
  7103. FROM mv2
  7104. );
  7105. ----
  7106. 999
  7107. query I
  7108. SELECT sum(lv)
  7109. FROM (
  7110. SELECT last_value(rn) OVER () AS lv
  7111. FROM mv2
  7112. );
  7113. ----
  7114. 1000000
  7115. statement ok
  7116. DELETE FROM t10
  7117. statement ok
  7118. INSERT INTO t10
  7119. SELECT generate_series(1,70000)::mz_timestamp::timestamptz, 5, 'aaa', 7, 8, 9, 'bbb';
  7120. query I
  7121. SELECT sum(r)
  7122. FROM mv1;
  7123. ----
  7124. 0
  7125. query I
  7126. SELECT sum(r)
  7127. FROM (
  7128. SELECT rn - lag(rn) OVER (ORDER BY t) AS r
  7129. FROM mv2
  7130. );
  7131. ----
  7132. 69999
  7133. ## QUALIFY
  7134. query II
  7135. SELECT *
  7136. FROM t6
  7137. QUALIFY row_number() OVER (PARTITION BY x%3 ORDER BY y) = 1;
  7138. ----
  7139. 1 2
  7140. 5 6
  7141. 15 16
  7142. query IIII rowsort
  7143. SELECT *, x%3, row_number() OVER (PARTITION BY x%3 ORDER BY y)
  7144. FROM t6
  7145. QUALIFY row_number() OVER (PARTITION BY x%3 ORDER BY y) = 1;
  7146. ----
  7147. 15 16 0 1
  7148. 1 2 1 1
  7149. 5 6 2 1
  7150. query II
  7151. SELECT *
  7152. FROM t6
  7153. QUALIFY sum(x) OVER (ORDER BY y) = 26;
  7154. ----
  7155. 13 14
  7156. query II
  7157. SELECT *
  7158. FROM (SELECT * FROM t6 UNION ALL SELECT * FROM t6)
  7159. QUALIFY sum(x) OVER (ORDER BY y) = 26;
  7160. ----
  7161. 7 8
  7162. 7 8
  7163. query I
  7164. SELECT avg(u)
  7165. FROM (
  7166. SELECT x, unnest(l) AS u
  7167. FROM (
  7168. SELECT x, array_agg(y) OVER (PARTITION BY x%5) AS l
  7169. FROM t7
  7170. ) AS ff1
  7171. ) AS ff2
  7172. GROUP BY x%5
  7173. QUALIFY sum(avg(u)) OVER (ORDER BY x%5) = -15
  7174. ORDER BY x%5;
  7175. ----
  7176. 2
  7177. query II
  7178. SELECT *
  7179. FROM (
  7180. SELECT x, unnest(l) AS u
  7181. FROM (
  7182. SELECT x, array_agg(y) OVER (PARTITION BY x%5) AS l
  7183. FROM t7
  7184. ) AS ff1
  7185. )
  7186. QUALIFY x-lag(x) OVER (ORDER BY x) = 0 AND lag(x) OVER (ORDER BY x) - lag(lag(x) OVER (ORDER BY x)) OVER (ORDER BY x) = 0
  7187. ORDER BY x,u;
  7188. ----
  7189. 5 6
  7190. 5 16
  7191. 10 -40
  7192. 10 -40
  7193. 10 6
  7194. 10 6
  7195. 10 16
  7196. 10 16
  7197. 15 6
  7198. 15 16
  7199. query III
  7200. SELECT *, sum(x-9) OVER (ORDER BY x, u)
  7201. FROM
  7202. (
  7203. SELECT DISTINCT *
  7204. FROM (
  7205. SELECT x, unnest(l) AS u
  7206. FROM (
  7207. SELECT x, array_agg(y) OVER (PARTITION BY x%5) AS l
  7208. FROM t7
  7209. ) AS ff1
  7210. )
  7211. QUALIFY x-lag(x) OVER (ORDER BY x) = 0 AND lag(x) OVER (ORDER BY x) - lag(lag(x) OVER (ORDER BY x)) OVER (ORDER BY x) = 0
  7212. )
  7213. QUALIFY sum(x-9) OVER (ORDER BY x, u) > -6
  7214. ORDER BY x,u;
  7215. ----
  7216. 5 6 -4
  7217. 10 16 -5
  7218. 15 6 1
  7219. 15 16 7