sql_ut.cpp 349 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240
  1. #include "sql_ut.h"
  2. #include "format/sql_format.h"
  3. #include "lexer/lexer.h"
  4. #include <yql/essentials/providers/common/provider/yql_provider_names.h>
  5. #include <yql/essentials/sql/sql.h>
  6. #include <yql/essentials/sql/v1/sql.h>
  7. #include <util/generic/map.h>
  8. #include <library/cpp/testing/unittest/registar.h>
  9. #include <util/string/split.h>
  10. #include <format>
  11. using namespace NSQLTranslation;
  12. namespace {
  13. TParsedTokenList Tokenize(const TString& query) {
  14. auto lexer = NSQLTranslationV1::MakeLexer(true, false);
  15. TParsedTokenList tokens;
  16. NYql::TIssues issues;
  17. UNIT_ASSERT_C(Tokenize(*lexer, query, "Query", tokens, issues, SQL_MAX_PARSER_ERRORS),
  18. issues.ToString());
  19. return tokens;
  20. }
  21. TString ToString(const TParsedTokenList& tokens) {
  22. TStringBuilder reconstructedQuery;
  23. for (const auto& token : tokens) {
  24. if (token.Name == "WS" || token.Name == "EOF") {
  25. continue;
  26. }
  27. if (!reconstructedQuery.empty()) {
  28. reconstructedQuery << ' ';
  29. }
  30. reconstructedQuery << token.Content;
  31. }
  32. return reconstructedQuery;
  33. }
  34. }
  35. Y_UNIT_TEST_SUITE(AnsiMode) {
  36. Y_UNIT_TEST(PragmaAnsi) {
  37. UNIT_ASSERT(SqlToYql("PRAGMA ANSI 2016;").IsOk());
  38. }
  39. }
  40. Y_UNIT_TEST_SUITE(SqlParsingOnly) {
  41. ///This function is used in BACKWARD COMPATIBILITY tests below that LIMIT the sets of token that CAN NOT be used
  42. ///as identifiers in different contexts in a SQL request
  43. ///\return list of tokens that failed this check
  44. TVector<TString> ValidateTokens(const THashSet<TString>& forbidden, const std::function<TString (const TString& )>& makeRequest) {
  45. THashMap<TString, bool> allTokens;
  46. for (const auto& t: NSQLFormat::GetKeywords()) {
  47. allTokens[t] = !forbidden.contains((t));
  48. }
  49. for (const auto& f: forbidden) {
  50. UNIT_ASSERT(allTokens.contains(f)); //check that forbidden list contains tokens only(argument check)
  51. }
  52. TVector<TString> failed;
  53. for (const auto& [token, allowed]: allTokens) {
  54. if (SqlToYql(makeRequest(token)).IsOk() != allowed)
  55. failed.push_back(token);
  56. }
  57. return failed;
  58. }
  59. Y_UNIT_TEST(TokensAsColumnName) { //id_expr
  60. auto failed = ValidateTokens({
  61. "ALL", "ANY", "AS", "ASSUME", "ASYMMETRIC", "AUTOMAP", "BETWEEN", "BITCAST",
  62. "CALLABLE", "CASE", "CAST", "CUBE", "CURRENT_DATE", "CURRENT_TIME", "CURRENT_TIMESTAMP",
  63. "DICT", "DISTINCT", "ENUM", "ERASE", "EXCEPT", "EXISTS", "FLOW", "FROM", "FULL",
  64. "HAVING", "HOP", "INTERSECT", "JSON_EXISTS", "JSON_QUERY", "JSON_VALUE", "LIMIT", "LIST",
  65. "NOT", "OPTIONAL", "PROCESS", "REDUCE", "REPEATABLE", "RESOURCE", "RETURN", "RETURNING", "ROLLUP",
  66. "SELECT", "SET", "STREAM", "STRUCT", "SYMMETRIC", "TAGGED", "TUPLE", "UNBOUNDED",
  67. "UNION", "VARIANT", "WHEN", "WHERE", "WINDOW", "WITHOUT"
  68. },
  69. [](const TString& token){
  70. TStringBuilder req;
  71. req << "SELECT " << token << " FROM Plato.Input";
  72. return req;
  73. }
  74. );
  75. UNIT_ASSERT_VALUES_EQUAL(failed, TVector<TString>{});
  76. }
  77. Y_UNIT_TEST(TokensAsWithoutColumnName) { //id_without
  78. auto failed = ValidateTokens({
  79. "ALL", "AS", "ASSUME", "ASYMMETRIC", "AUTOMAP", "BETWEEN", "BITCAST",
  80. "CALLABLE", "CASE", "CAST", "CUBE", "CURRENT_DATE", "CURRENT_TIME", "CURRENT_TIMESTAMP",
  81. "DICT", "DISTINCT", "EMPTY_ACTION", "ENUM", "EXCEPT", "EXISTS", "FALSE", "FLOW", "FROM", "FULL",
  82. "HAVING", "HOP", "INTERSECT", "JSON_EXISTS", "JSON_QUERY", "JSON_VALUE", "LIMIT", "LIST",
  83. "NOT", "NULL", "OPTIONAL", "PROCESS", "REDUCE", "REPEATABLE", "RESOURCE", "RETURN", "RETURNING", "ROLLUP",
  84. "SELECT", "SET", "STRUCT", "SYMMETRIC", "TAGGED", "TRUE", "TUPLE", "UNBOUNDED",
  85. "UNION", "VARIANT", "WHEN", "WHERE", "WINDOW", "WITHOUT"
  86. },
  87. [](const TString& token){
  88. TStringBuilder req;
  89. req << "SELECT * WITHOUT " << token << " FROM Plato.Input";
  90. return req;
  91. }
  92. );
  93. UNIT_ASSERT_VALUES_EQUAL(failed, TVector<TString>{});
  94. }
  95. Y_UNIT_TEST(TokensAsColumnNameInAddColumn) { //id_schema
  96. auto failed = ValidateTokens({
  97. "ANY", "AUTOMAP", "CALLABLE", "COLUMN", "DICT", "ENUM", "ERASE", "FALSE", "FLOW",
  98. "LIST", "OPTIONAL", "REPEATABLE", "RESOURCE",
  99. "SET", "STREAM", "STRUCT", "TAGGED", "TRUE", "TUPLE", "VARIANT"
  100. },
  101. [](const TString& token){
  102. TStringBuilder req;
  103. req << "ALTER TABLE Plato.Input ADD COLUMN " << token << " Bool";
  104. return req;
  105. }
  106. );
  107. UNIT_ASSERT_VALUES_EQUAL(failed, TVector<TString>{});
  108. }
  109. Y_UNIT_TEST(TokensAsColumnAlias) {
  110. auto failed = ValidateTokens({
  111. "AUTOMAP", "FALSE",
  112. "REPEATABLE", "TRUE"
  113. },
  114. [](const TString& token){
  115. TStringBuilder req;
  116. req << "SELECT Col as " << token << " FROM Plato.Input";
  117. return req;
  118. }
  119. );
  120. UNIT_ASSERT_VALUES_EQUAL(failed, TVector<TString>{});
  121. }
  122. Y_UNIT_TEST(TokensAsTableName) { //id_table_or_type
  123. auto failed = ValidateTokens({
  124. "ANY", "AUTOMAP", "COLUMN", "ERASE", "FALSE",
  125. "REPEATABLE", "STREAM", "TRUE"
  126. },
  127. [](const TString& token){
  128. TStringBuilder req;
  129. req << "SELECT * FROM Plato." << token;
  130. return req;
  131. }
  132. );
  133. UNIT_ASSERT_VALUES_EQUAL(failed, TVector<TString>{});
  134. }
  135. Y_UNIT_TEST(TokensAsTableAlias) { //id_table
  136. auto failed = ValidateTokens({
  137. "AUTOMAP", "CALLABLE", "DICT", "ENUM","FALSE", "FLOW",
  138. "LIST", "OPTIONAL", "REPEATABLE", "RESOURCE",
  139. "SET", "STRUCT", "TAGGED", "TRUE", "TUPLE", "VARIANT"
  140. },
  141. [](const TString& token){
  142. TStringBuilder req;
  143. req << "SELECT * FROM Plato.Input AS " << token;
  144. return req;
  145. }
  146. );
  147. UNIT_ASSERT_VALUES_EQUAL(failed, TVector<TString>{});
  148. }
  149. Y_UNIT_TEST(TokensAsHints) { //id_hint
  150. auto failed = ValidateTokens({
  151. "AUTOMAP", "CALLABLE", "COLUMNS", "DICT", "ENUM", "FALSE", "FLOW",
  152. "LIST", "OPTIONAL", "REPEATABLE", "RESOURCE",
  153. "SCHEMA", "SET", "STRUCT", "TAGGED", "TRUE", "TUPLE", "VARIANT"
  154. },
  155. [](const TString& token){
  156. TStringBuilder req;
  157. req << "SELECT * FROM Plato.Input WITH " << token;
  158. return req;
  159. }
  160. );
  161. UNIT_ASSERT_VALUES_EQUAL(failed, TVector<TString>{});
  162. }
  163. Y_UNIT_TEST(TokensAsWindow) { //id_window
  164. auto failed = ValidateTokens({
  165. "AUTOMAP", "CALLABLE", "DICT", "ENUM", "FALSE", "FLOW", "GROUPS", "LIST", "OPTIONAL",
  166. "RANGE", "REPEATABLE", "RESOURCE", "ROWS", "SET", "STRUCT", "TAGGED" ,"TRUE", "TUPLE", "VARIANT"
  167. },
  168. [](const TString& token){
  169. TStringBuilder req;
  170. req << "SELECT * FROM Plato.Input WINDOW " << token << " AS ()";
  171. return req;
  172. }
  173. );
  174. UNIT_ASSERT_VALUES_EQUAL(failed, TVector<TString>{});
  175. }
  176. Y_UNIT_TEST(TokensAsIdExprIn) { //id_expr_in
  177. auto failed = ValidateTokens({
  178. "ALL", "ANY", "AS", "ASSUME", "ASYMMETRIC", "AUTOMAP", "BETWEEN", "BITCAST",
  179. "CALLABLE", "CASE", "CAST", "COMPACT", "CUBE", "CURRENT_DATE", "CURRENT_TIME", "CURRENT_TIMESTAMP",
  180. "DICT", "DISTINCT", "ENUM", "ERASE", "EXCEPT", "EXISTS", "FLOW", "FROM", "FULL",
  181. "HAVING", "HOP", "INTERSECT", "JSON_EXISTS", "JSON_QUERY", "JSON_VALUE", "LIMIT", "LIST",
  182. "NOT", "OPTIONAL", "PROCESS", "REDUCE", "REPEATABLE", "RESOURCE", "RETURN", "RETURNING", "ROLLUP",
  183. "SELECT", "SET", "STREAM", "STRUCT", "SYMMETRIC", "TAGGED", "TUPLE", "UNBOUNDED",
  184. "UNION", "VARIANT", "WHEN", "WHERE", "WINDOW", "WITHOUT"
  185. },
  186. [](const TString& token){
  187. TStringBuilder req;
  188. req << "SELECT * FROM Plato.Input WHERE q IN " << token;
  189. return req;
  190. }
  191. );
  192. UNIT_ASSERT_VALUES_EQUAL(failed, TVector<TString>{});
  193. }
  194. Y_UNIT_TEST(TableHints) {
  195. UNIT_ASSERT(SqlToYql("SELECT * FROM plato.Input WITH INFER_SCHEMA").IsOk());
  196. UNIT_ASSERT(SqlToYql("SELECT * FROM plato.Input WITH (INFER_SCHEMA)").IsOk());
  197. }
  198. Y_UNIT_TEST(InNoHints) {
  199. TString query = "SELECT * FROM plato.Input WHERE key IN (1,2,3)";
  200. VerifySqlInHints(query, { "'('('warnNoAnsi))" }, {});
  201. VerifySqlInHints(query, { "'()" }, false);
  202. VerifySqlInHints(query, { "'('('ansi))" }, true);
  203. }
  204. Y_UNIT_TEST(InHintCompact) {
  205. // should parse COMPACT as hint
  206. TString query = "SELECT * FROM plato.Input WHERE key IN COMPACT(1, 2, 3)";
  207. VerifySqlInHints(query, { "'('isCompact)" });
  208. }
  209. Y_UNIT_TEST(InHintSubquery) {
  210. // should parse tableSource as hint
  211. TString query = "$subq = (SELECT key FROM plato.Input); SELECT * FROM plato.Input WHERE key IN $subq";
  212. VerifySqlInHints(query, { "'('tableSource)" });
  213. }
  214. Y_UNIT_TEST(InHintCompactSubquery) {
  215. TString query = "$subq = (SELECT key FROM plato.Input); SELECT * FROM plato.Input WHERE key IN COMPACT $subq";
  216. VerifySqlInHints(query, { "'('isCompact)", "'('tableSource)" });
  217. }
  218. Y_UNIT_TEST(CompactKeywordNotReservedForNames) {
  219. UNIT_ASSERT(SqlToYql("SELECT COMPACT FROM plato.Input WHERE COMPACT IN COMPACT(1, 2, 3)").IsOk());
  220. UNIT_ASSERT(SqlToYql("USE plato; SELECT * FROM COMPACT").IsOk());
  221. }
  222. Y_UNIT_TEST(FamilyKeywordNotReservedForNames) {
  223. // FIXME: check if we can get old behaviour
  224. //UNIT_ASSERT(SqlToYql("USE plato; CREATE TABLE FAMILY (FAMILY Uint32, PRIMARY KEY (FAMILY));").IsOk());
  225. //UNIT_ASSERT(SqlToYql("USE plato; SELECT FAMILY FROM FAMILY").IsOk());
  226. UNIT_ASSERT(SqlToYql("USE plato; SELECT FAMILY FROM Input").IsOk());
  227. }
  228. Y_UNIT_TEST(ResetKeywordNotReservedForNames) {
  229. UNIT_ASSERT(SqlToYql("USE plato; CREATE TABLE RESET (RESET Uint32, PRIMARY KEY (RESET));").IsOk());
  230. UNIT_ASSERT(SqlToYql("USE plato; SELECT RESET FROM RESET").IsOk());
  231. }
  232. Y_UNIT_TEST(SyncKeywordNotReservedForNames) {
  233. UNIT_ASSERT(SqlToYql("USE plato; CREATE TABLE SYNC (SYNC Uint32, PRIMARY KEY (SYNC));").IsOk());
  234. UNIT_ASSERT(SqlToYql("USE plato; SELECT SYNC FROM SYNC").IsOk());
  235. }
  236. Y_UNIT_TEST(AsyncKeywordNotReservedForNames) {
  237. UNIT_ASSERT(SqlToYql("USE plato; CREATE TABLE ASYNC (ASYNC Uint32, PRIMARY KEY (ASYNC));").IsOk());
  238. UNIT_ASSERT(SqlToYql("USE plato; SELECT ASYNC FROM ASYNC").IsOk());
  239. }
  240. Y_UNIT_TEST(DisableKeywordNotReservedForNames) {
  241. UNIT_ASSERT(SqlToYql("USE plato; CREATE TABLE DISABLE (DISABLE Uint32, PRIMARY KEY (DISABLE));").IsOk());
  242. UNIT_ASSERT(SqlToYql("USE plato; SELECT DISABLE FROM DISABLE").IsOk());
  243. }
  244. Y_UNIT_TEST(ChangefeedKeywordNotReservedForNames) {
  245. UNIT_ASSERT(SqlToYql("USE plato; CREATE TABLE CHANGEFEED (CHANGEFEED Uint32, PRIMARY KEY (CHANGEFEED));").IsOk());
  246. UNIT_ASSERT(SqlToYql("USE plato; SELECT CHANGEFEED FROM CHANGEFEED").IsOk());
  247. }
  248. Y_UNIT_TEST(ReplicationKeywordNotReservedForNames) {
  249. UNIT_ASSERT(SqlToYql("USE plato; CREATE TABLE REPLICATION (REPLICATION Uint32, PRIMARY KEY (REPLICATION));").IsOk());
  250. UNIT_ASSERT(SqlToYql("USE plato; SELECT REPLICATION FROM REPLICATION").IsOk());
  251. }
  252. Y_UNIT_TEST(TransferKeywordNotReservedForNames) {
  253. UNIT_ASSERT(SqlToYql("USE plato; CREATE TABLE TRANSFER (TRANSFER Uint32, PRIMARY KEY (TRANSFER));").IsOk());
  254. UNIT_ASSERT(SqlToYql("USE plato; SELECT TRANSFER FROM TRANSFER").IsOk());
  255. }
  256. Y_UNIT_TEST(SecondsKeywordNotReservedForNames) {
  257. UNIT_ASSERT(SqlToYql("USE plato; CREATE TABLE SECONDS (SECONDS Uint32, PRIMARY KEY (SECONDS));").IsOk());
  258. UNIT_ASSERT(SqlToYql("USE plato; SELECT SECONDS FROM SECONDS").IsOk());
  259. }
  260. Y_UNIT_TEST(MillisecondsKeywordNotReservedForNames) {
  261. UNIT_ASSERT(SqlToYql("USE plato; CREATE TABLE MILLISECONDS (MILLISECONDS Uint32, PRIMARY KEY (MILLISECONDS));").IsOk());
  262. UNIT_ASSERT(SqlToYql("USE plato; SELECT MILLISECONDS FROM MILLISECONDS").IsOk());
  263. }
  264. Y_UNIT_TEST(MicrosecondsKeywordNotReservedForNames) {
  265. UNIT_ASSERT(SqlToYql("USE plato; CREATE TABLE MICROSECONDS (MICROSECONDS Uint32, PRIMARY KEY (MICROSECONDS));").IsOk());
  266. UNIT_ASSERT(SqlToYql("USE plato; SELECT MICROSECONDS FROM MICROSECONDS").IsOk());
  267. }
  268. Y_UNIT_TEST(NanosecondsKeywordNotReservedForNames) {
  269. UNIT_ASSERT(SqlToYql("USE plato; CREATE TABLE NANOSECONDS (NANOSECONDS Uint32, PRIMARY KEY (NANOSECONDS));").IsOk());
  270. UNIT_ASSERT(SqlToYql("USE plato; SELECT NANOSECONDS FROM NANOSECONDS").IsOk());
  271. }
  272. Y_UNIT_TEST(Jubilee) {
  273. NYql::TAstParseResult res = SqlToYql("USE plato; INSERT INTO Arcadia (r2000000) VALUES (\"2M GET!!!\");");
  274. UNIT_ASSERT(res.Root);
  275. }
  276. Y_UNIT_TEST(QualifiedAsteriskBefore) {
  277. NYql::TAstParseResult res = SqlToYql(
  278. "PRAGMA DisableSimpleColumns;"
  279. "select interested_table.*, LENGTH(value) AS megahelpful_len from plato.Input as interested_table;"
  280. );
  281. UNIT_ASSERT(res.Root);
  282. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  283. static bool seenStar = false;
  284. if (word == "FlattenMembers") {
  285. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("interested_table."));
  286. } else if (word == "SqlProjectItem") {
  287. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find(Quote("megahelpful_len")));
  288. UNIT_ASSERT_VALUES_EQUAL(seenStar, true);
  289. } else if (word == "SqlProjectStarItem") {
  290. seenStar = true;
  291. }
  292. };
  293. TWordCountHive elementStat = {{TString("FlattenMembers"), 0}, {TString("SqlProjectItem"), 0}, {TString("SqlProjectStarItem"), 0}};
  294. VerifyProgram(res, elementStat, verifyLine);
  295. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["FlattenMembers"]);
  296. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["SqlProjectItem"]);
  297. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["SqlProjectStarItem"]);
  298. }
  299. Y_UNIT_TEST(QualifiedAsteriskAfter) {
  300. NYql::TAstParseResult res = SqlToYql(
  301. "PRAGMA DisableSimpleColumns;"
  302. "select LENGTH(value) AS megahelpful_len, interested_table.* from plato.Input as interested_table;"
  303. );
  304. UNIT_ASSERT(res.Root);
  305. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  306. static bool seenStar = false;
  307. if (word == "FlattenMembers") {
  308. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("interested_table."));
  309. } else if (word == "SqlProjectItem") {
  310. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find(Quote("megahelpful_len")));
  311. UNIT_ASSERT_VALUES_EQUAL(seenStar, false);
  312. } else if (word == "SqlProjectStarItem") {
  313. seenStar = true;
  314. }
  315. };
  316. TWordCountHive elementStat = {{TString("FlattenMembers"), 0}, {TString("SqlProjectItem"), 0}, {TString("SqlProjectStarItem"), 0}};
  317. VerifyProgram(res, elementStat, verifyLine);
  318. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["FlattenMembers"]);
  319. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["SqlProjectItem"]);
  320. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["SqlProjectStarItem"]);
  321. }
  322. Y_UNIT_TEST(QualifiedMembers) {
  323. NYql::TAstParseResult res = SqlToYql("select interested_table.key, interested_table.value from plato.Input as interested_table;");
  324. UNIT_ASSERT(res.Root);
  325. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  326. const bool fieldKey = TString::npos != line.find(Quote("key"));
  327. const bool fieldValue = TString::npos != line.find(Quote("value"));
  328. const bool refOnTable = TString::npos != line.find("interested_table.");
  329. if (word == "SqlProjectItem") {
  330. UNIT_ASSERT(fieldKey || fieldValue);
  331. UNIT_ASSERT(!refOnTable);
  332. } else if (word == "Write!") {
  333. UNIT_ASSERT(fieldKey && fieldValue && !refOnTable);
  334. }
  335. };
  336. TWordCountHive elementStat = {{TString("SqlProjectStarItem"), 0}, {TString("SqlProjectItem"), 0}, {TString("Write!"), 0}};
  337. VerifyProgram(res, elementStat, verifyLine);
  338. UNIT_ASSERT_VALUES_EQUAL(0, elementStat["SqlProjectStarItem"]);
  339. UNIT_ASSERT_VALUES_EQUAL(2, elementStat["SqlProjectItem"]);
  340. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write!"]);
  341. }
  342. Y_UNIT_TEST(ExplainQueryPlan) {
  343. UNIT_ASSERT(SqlToYql("EXPLAIN SELECT 1;").IsOk());
  344. UNIT_ASSERT(SqlToYql("EXPLAIN QUERY PLAN SELECT 1;").IsOk());
  345. }
  346. Y_UNIT_TEST(JoinParseCorrect) {
  347. NYql::TAstParseResult res = SqlToYql(
  348. "PRAGMA DisableSimpleColumns;"
  349. " SELECT table_bb.*, table_aa.key as megakey"
  350. " FROM plato.Input AS table_aa"
  351. " JOIN plato.Input AS table_bb"
  352. " ON table_aa.value == table_bb.value;"
  353. );
  354. UNIT_ASSERT(res.Root);
  355. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  356. if (word == "SelectMembers") {
  357. UNIT_ASSERT_VALUES_EQUAL(TString::npos, line.find("table_aa."));
  358. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("table_bb."));
  359. } else if (word == "SqlProjectItem") {
  360. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find(Quote("megakey")));
  361. } else if (word == "SqlColumn") {
  362. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find(Quote("table_aa")));
  363. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find(Quote("key")));
  364. }
  365. };
  366. TWordCountHive elementStat = {{TString("SqlProjectItem"), 0}, {TString("SqlProjectStarItem"), 0}, {TString("SelectMembers"), 0}, {TString("SqlColumn"), 0}};
  367. VerifyProgram(res, elementStat, verifyLine);
  368. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["SqlProjectItem"]);
  369. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["SqlProjectStarItem"]);
  370. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["SelectMembers"]);
  371. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["SqlColumn"]);
  372. }
  373. Y_UNIT_TEST(Join3Table) {
  374. NYql::TAstParseResult res = SqlToYql(
  375. " PRAGMA DisableSimpleColumns;"
  376. " SELECT table_bb.*, table_aa.key as gigakey, table_cc.* "
  377. " FROM plato.Input AS table_aa"
  378. " JOIN plato.Input AS table_bb ON table_aa.key == table_bb.key"
  379. " JOIN plato.Input AS table_cc ON table_aa.subkey == table_cc.subkey;"
  380. );
  381. Err2Str(res);
  382. UNIT_ASSERT(res.Root);
  383. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  384. if (word == "SelectMembers") {
  385. UNIT_ASSERT_VALUES_EQUAL(TString::npos, line.find("table_aa."));
  386. UNIT_ASSERT(line.find("table_bb.") != TString::npos || line.find("table_cc.") != TString::npos);
  387. } else if (word == "SqlProjectItem") {
  388. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find(Quote("gigakey")));
  389. } else if (word == "SqlColumn") {
  390. const auto posTableAA = line.find(Quote("table_aa"));
  391. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, posTableAA);
  392. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find(Quote("key")));
  393. UNIT_ASSERT_VALUES_EQUAL(TString::npos, line.find("table_aa", posTableAA + 3));
  394. }
  395. };
  396. TWordCountHive elementStat = {{TString("SqlProjectItem"), 0}, {TString("SqlProjectStarItem"), 0}, {TString("SelectMembers"), 0}, {TString("SqlColumn"), 0}};
  397. VerifyProgram(res, elementStat, verifyLine);
  398. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["SqlProjectItem"]);
  399. UNIT_ASSERT_VALUES_EQUAL(2, elementStat["SqlProjectStarItem"]);
  400. UNIT_ASSERT_VALUES_EQUAL(2, elementStat["SelectMembers"]);
  401. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["SqlColumn"]);
  402. }
  403. Y_UNIT_TEST(DisabledJoinCartesianProduct) {
  404. NYql::TAstParseResult res = SqlToYql("pragma DisableAnsiImplicitCrossJoin; use plato; select * from A,B,C");
  405. UNIT_ASSERT(!res.Root);
  406. UNIT_ASSERT_STRINGS_EQUAL(res.Issues.ToString(), "<main>:1:67: Error: Cartesian product of tables is disabled. Please use explicit CROSS JOIN or enable it via PRAGMA AnsiImplicitCrossJoin\n");
  407. }
  408. Y_UNIT_TEST(JoinCartesianProduct) {
  409. NYql::TAstParseResult res = SqlToYql("pragma AnsiImplicitCrossJoin; use plato; select * from A,B,C");
  410. UNIT_ASSERT(res.Root);
  411. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  412. if (word == "EquiJoin") {
  413. auto pos = line.find("Cross");
  414. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, pos);
  415. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("Cross", pos + 1));
  416. }
  417. };
  418. TWordCountHive elementStat = {{TString("EquiJoin"), 0}};
  419. VerifyProgram(res, elementStat, verifyLine);
  420. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["EquiJoin"]);
  421. }
  422. Y_UNIT_TEST(JoinWithoutConcreteColumns) {
  423. NYql::TAstParseResult res = SqlToYql(
  424. " use plato;"
  425. " SELECT a.v, b.value"
  426. " FROM `Input1` VIEW `ksv` AS a"
  427. " JOIN `Input2` AS b"
  428. " ON a.k == b.key;"
  429. );
  430. UNIT_ASSERT(res.Root);
  431. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  432. if (word == "SqlProjectItem") {
  433. UNIT_ASSERT(line.find(Quote("a.v")) != TString::npos || line.find(Quote("b.value")) != TString::npos);
  434. } else if (word == "SqlColumn") {
  435. const auto posTableA = line.find(Quote("a"));
  436. const auto posTableB = line.find(Quote("b"));
  437. if (posTableA != TString::npos) {
  438. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find(Quote("v")));
  439. } else {
  440. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, posTableB);
  441. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find(Quote("value")));
  442. }
  443. }
  444. };
  445. TWordCountHive elementStat = {{TString("SqlProjectStarItem"), 0}, {TString("SqlProjectItem"), 0}, {TString("SqlColumn"), 0}};
  446. VerifyProgram(res, elementStat, verifyLine);
  447. UNIT_ASSERT_VALUES_EQUAL(0, elementStat["SqlProjectStarItem"]);
  448. UNIT_ASSERT_VALUES_EQUAL(2, elementStat["SqlProjectItem"]);
  449. UNIT_ASSERT_VALUES_EQUAL(2, elementStat["SqlColumn"]);
  450. }
  451. Y_UNIT_TEST(JoinWithSameValues) {
  452. NYql::TAstParseResult res = SqlToYql("SELECT a.value, b.value FROM plato.Input AS a JOIN plato.Input as b ON a.key == b.key;");
  453. UNIT_ASSERT(res.Root);
  454. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  455. if (word == "SqlProjectItem") {
  456. const bool isValueFromA = TString::npos != line.find(Quote("a.value"));
  457. const bool isValueFromB = TString::npos != line.find(Quote("b.value"));
  458. UNIT_ASSERT(isValueFromA || isValueFromB);
  459. } if (word == "Write!") {
  460. UNIT_ASSERT_VALUES_EQUAL(TString::npos, line.find("a.a."));
  461. UNIT_ASSERT_VALUES_EQUAL(TString::npos, line.find("b.b."));
  462. }
  463. };
  464. TWordCountHive elementStat = {{TString("SqlProjectStarItem"), 0}, {TString("SqlProjectItem"), 0}, {"Write!", 0}};
  465. VerifyProgram(res, elementStat, verifyLine);
  466. UNIT_ASSERT_VALUES_EQUAL(0, elementStat["SqlProjectStarItem"]);
  467. UNIT_ASSERT_VALUES_EQUAL(2, elementStat["SqlProjectItem"]);
  468. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write!"]);
  469. }
  470. Y_UNIT_TEST(SameColumnsForDifferentTables) {
  471. NYql::TAstParseResult res = SqlToYql("SELECT a.key, b.key FROM plato.Input as a JOIN plato.Input as b on a.key==b.key;");
  472. UNIT_ASSERT(res.Root);
  473. }
  474. Y_UNIT_TEST(SameColumnsForDifferentTablesFullJoin) {
  475. NYql::TAstParseResult res = SqlToYql("SELECT a.key, b.key, a.value, b.value FROM plato.Input AS a FULL JOIN plato.Input AS b USING(key);");
  476. UNIT_ASSERT(res.Root);
  477. }
  478. Y_UNIT_TEST(JoinStreamLookupStrategyHint) {
  479. {
  480. NYql::TAstParseResult res = SqlToYql("SELECT * FROM plato.Input AS a JOIN /*+ StreamLookup() */ plato.Input AS b USING(key);");
  481. UNIT_ASSERT(res.Root);
  482. }
  483. //case insensitive
  484. {
  485. NYql::TAstParseResult res = SqlToYql("SELECT * FROM plato.Input AS a JOIN /*+ streamlookup() */ plato.Input AS b USING(key);");
  486. UNIT_ASSERT(res.Root);
  487. }
  488. }
  489. Y_UNIT_TEST(JoinConflictingStrategyHint) {
  490. {
  491. NYql::TAstParseResult res = SqlToYql("SELECT * FROM plato.Input AS a JOIN /*+ StreamLookup() */ /*+ Merge() */ plato.Input AS b USING(key);");
  492. UNIT_ASSERT(!res.Root);
  493. UNIT_ASSERT_STRINGS_EQUAL(res.Issues.ToString(), "<main>:1:91: Error: Conflicting join strategy hints\n");
  494. }
  495. }
  496. Y_UNIT_TEST(JoinDuplicatingStrategyHint) {
  497. {
  498. NYql::TAstParseResult res = SqlToYql("SELECT * FROM plato.Input AS a JOIN /*+ StreamLookup() */ /*+ StreamLookup() */ plato.Input AS b USING(key);");
  499. UNIT_ASSERT(!res.Root);
  500. UNIT_ASSERT_STRINGS_EQUAL(res.Issues.ToString(), "<main>:1:98: Error: Duplicate join strategy hint\n");
  501. }
  502. }
  503. Y_UNIT_TEST(WarnCrossJoinStrategyHint) {
  504. NYql::TAstParseResult res = SqlToYql("SELECT * FROM plato.Input AS a CROSS JOIN /*+ merge() */ plato.Input AS b;");
  505. UNIT_ASSERT(res.Root);
  506. UNIT_ASSERT_STRINGS_EQUAL(res.Issues.ToString(), "<main>:1:32: Warning: Non-default join strategy will not be used for CROSS JOIN, code: 4534\n");
  507. }
  508. Y_UNIT_TEST(WarnCartesianProductStrategyHint) {
  509. NYql::TAstParseResult res = SqlToYql("pragma AnsiImplicitCrossJoin; use plato; SELECT * FROM A, /*+ merge() */ B;");
  510. UNIT_ASSERT(res.Root);
  511. UNIT_ASSERT_STRINGS_EQUAL(res.Issues.ToString(), "<main>:1:74: Warning: Non-default join strategy will not be used for CROSS JOIN, code: 4534\n");
  512. }
  513. Y_UNIT_TEST(WarnUnknownJoinStrategyHint) {
  514. NYql::TAstParseResult res = SqlToYql("SELECT * FROM plato.Input AS a JOIN /*+ xmerge() */ plato.Input AS b USING (key);");
  515. UNIT_ASSERT(res.Root);
  516. UNIT_ASSERT_STRINGS_EQUAL(res.Issues.ToString(), "<main>:1:41: Warning: Unsupported join hint: xmerge, code: 4534\n");
  517. }
  518. Y_UNIT_TEST(ReverseLabels) {
  519. NYql::TAstParseResult res = SqlToYql("select in.key as subkey, subkey as key from plato.Input as in;");
  520. UNIT_ASSERT(res.Root);
  521. }
  522. Y_UNIT_TEST(AutogenerationAliasWithoutCollisionConflict1) {
  523. NYql::TAstParseResult res = SqlToYql("select LENGTH(Value), key as column1 from plato.Input;");
  524. UNIT_ASSERT(res.Root);
  525. }
  526. Y_UNIT_TEST(AutogenerationAliasWithoutCollision2Conflict2) {
  527. NYql::TAstParseResult res = SqlToYql("select key as column0, LENGTH(Value) from plato.Input;");
  528. UNIT_ASSERT(res.Root);
  529. }
  530. Y_UNIT_TEST(InputAliasForQualifiedAsterisk) {
  531. NYql::TAstParseResult res = SqlToYql("use plato; select zyuzya.*, key from plato.Input as zyuzya;");
  532. UNIT_ASSERT(res.Root);
  533. }
  534. Y_UNIT_TEST(SelectSupportsResultColumnsWithTrailingComma) {
  535. NYql::TAstParseResult res = SqlToYql("select a, b, c, from plato.Input;");
  536. UNIT_ASSERT(res.Root);
  537. }
  538. Y_UNIT_TEST(SelectOrderByLabeledColumn) {
  539. NYql::TAstParseResult res = SqlToYql("pragma DisableOrderedColumns; select key as goal from plato.Input order by goal");
  540. UNIT_ASSERT(res.Root);
  541. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  542. if (word == "DataSource") {
  543. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("plato"));
  544. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("Input"));
  545. UNIT_ASSERT_VALUES_EQUAL(TString::npos, line.find("goal"));
  546. } else if (word == "Sort") {
  547. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("goal"));
  548. UNIT_ASSERT_VALUES_EQUAL(TString::npos, line.find("key"));
  549. }
  550. };
  551. TWordCountHive elementStat = {{TString("DataSource"), 0}, {TString("Sort"), 0}};
  552. VerifyProgram(res, elementStat, verifyLine);
  553. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["DataSource"]);
  554. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Sort"]);
  555. }
  556. Y_UNIT_TEST(SelectOrderBySimpleExpr) {
  557. NYql::TAstParseResult res = SqlToYql("select a from plato.Input order by a + a");
  558. UNIT_ASSERT(res.Root);
  559. }
  560. Y_UNIT_TEST(SelectAssumeOrderByTableRowAccess) {
  561. NYql::TAstParseResult res = SqlToYql("$key = 'foo';select * from plato.Input assume order by TableRow().$key");
  562. UNIT_ASSERT(res.Root);
  563. }
  564. Y_UNIT_TEST(SelectOrderByDuplicateLabels) {
  565. NYql::TAstParseResult res = SqlToYql("select a from plato.Input order by a, a");
  566. UNIT_ASSERT(res.Root);
  567. }
  568. Y_UNIT_TEST(SelectOrderByExpression) {
  569. NYql::TAstParseResult res = SqlToYql("select * from plato.Input as i order by cast(key as uint32) + cast(subkey as uint32)");
  570. UNIT_ASSERT(res.Root);
  571. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  572. if (word == "Sort") {
  573. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("\"+MayWarn\""));
  574. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("key"));
  575. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("subkey"));
  576. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("(Bool 'true)"));
  577. UNIT_ASSERT_VALUES_EQUAL(TString::npos, line.find("i.key"));
  578. UNIT_ASSERT_VALUES_EQUAL(TString::npos, line.find("i.subkey"));
  579. }
  580. };
  581. TWordCountHive elementStat = {{TString("Sort"), 0}};
  582. VerifyProgram(res, elementStat, verifyLine);
  583. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Sort"]);
  584. }
  585. Y_UNIT_TEST(SelectOrderByExpressionDesc) {
  586. NYql::TAstParseResult res = SqlToYql("pragma disablesimplecolumns; select i.*, key, subkey from plato.Input as i order by cast(i.key as uint32) - cast(i.subkey as uint32) desc");
  587. UNIT_ASSERT(res.Root);
  588. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  589. if (word == "Sort") {
  590. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("\"-MayWarn\""));
  591. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("\"key\""));
  592. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("\"subkey\""));
  593. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("(Bool 'false)"));
  594. } else if (word == "Write!") {
  595. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("'columns"));
  596. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("\"key\""));
  597. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("\"subkey\""));
  598. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("prefix"));
  599. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("\"i.\""));
  600. }
  601. };
  602. TWordCountHive elementStat = {{TString("Sort"), 0}, {TString("Write!"), 0}};
  603. VerifyProgram(res, elementStat, verifyLine);
  604. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Sort"]);
  605. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write!"]);
  606. }
  607. Y_UNIT_TEST(SelectOrderByExpressionAsc) {
  608. NYql::TAstParseResult res = SqlToYql("select i.key, i.subkey from plato.Input as i order by cast(key as uint32) % cast(i.subkey as uint32) asc");
  609. UNIT_ASSERT(res.Root);
  610. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  611. if (word == "Sort") {
  612. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("\"%MayWarn\""));
  613. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("\"key\""));
  614. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("\"subkey\""));
  615. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("(Bool 'true)"));
  616. } else if (word == "Write!") {
  617. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("'columns"));
  618. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("\"key\""));
  619. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("\"subkey\""));
  620. UNIT_ASSERT_VALUES_EQUAL(TString::npos, line.find("i."));
  621. }
  622. };
  623. TWordCountHive elementStat = {{TString("Sort"), 0}, {TString("Write!"), 0}};
  624. VerifyProgram(res, elementStat, verifyLine);
  625. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Sort"]);
  626. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write!"]);
  627. }
  628. Y_UNIT_TEST(ReferenceToKeyInSubselect) {
  629. NYql::TAstParseResult res = SqlToYql("select b.key from (select a.key from plato.Input as a) as b;");
  630. UNIT_ASSERT(res.Root);
  631. }
  632. Y_UNIT_TEST(OrderByCastValue) {
  633. NYql::TAstParseResult res = SqlToYql("select i.key, i.subkey from plato.Input as i order by cast(key as uint32) desc;");
  634. UNIT_ASSERT(res.Root);
  635. }
  636. Y_UNIT_TEST(GroupByCastValue) {
  637. NYql::TAstParseResult res = SqlToYql("select count(1) from plato.Input as i group by cast(key as uint8);");
  638. UNIT_ASSERT(res.Root);
  639. }
  640. Y_UNIT_TEST(KeywordInSelectColumns) {
  641. NYql::TAstParseResult res = SqlToYql("select in, s.check from (select 1 as in, \"test\" as check) as s;");
  642. UNIT_ASSERT(res.Root);
  643. }
  644. Y_UNIT_TEST(SelectAllGroupBy) {
  645. NYql::TAstParseResult res = SqlToYql("select * from plato.Input group by subkey;");
  646. UNIT_ASSERT(res.Root);
  647. }
  648. Y_UNIT_TEST(CreateObjectWithFeatures) {
  649. NYql::TAstParseResult res = SqlToYql("USE plato; CREATE OBJECT secretId (TYPE SECRET) WITH (Key1=Value1, K2=V2);");
  650. UNIT_ASSERT(res.Root);
  651. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  652. if (word == "Write") {
  653. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("'('\"K2\" '\"V2\") '('\"Key1\" '\"Value1\")"));
  654. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("createObject"));
  655. }
  656. };
  657. TWordCountHive elementStat = { {TString("Write"), 0}, {TString("SECRET"), 0} };
  658. VerifyProgram(res, elementStat, verifyLine);
  659. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  660. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["SECRET"]);
  661. }
  662. Y_UNIT_TEST(CreateObjectIfNotExists) {
  663. NYql::TAstParseResult res = SqlToYql("USE plato; CREATE OBJECT IF NOT EXISTS secretId (TYPE SECRET) WITH (Key1=Value1, K2=V2);");
  664. UNIT_ASSERT(res.Root);
  665. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  666. if (word == "Write") {
  667. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("createObjectIfNotExists"));
  668. }
  669. };
  670. TWordCountHive elementStat = { {TString("Write"), 0}, {TString("SECRET"), 0} };
  671. VerifyProgram(res, elementStat, verifyLine);
  672. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  673. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["SECRET"]);
  674. }
  675. Y_UNIT_TEST(CreateObjectWithFeaturesStrings) {
  676. NYql::TAstParseResult res = SqlToYql("USE plato; CREATE OBJECT secretId (TYPE SECRET) WITH (Key1=\"Value1\", K2='V2', K3=V3, K4='', K5=`aaa`, K6='a\\'aa');");
  677. UNIT_ASSERT(res.Root);
  678. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  679. if (word == "Write") {
  680. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("'('\"K2\" '\"V2\") '('\"K3\" '\"V3\") '('\"K4\" '\"\") '('\"K5\" '\"aaa\") '('\"K6\" '\"a'aa\") '('\"Key1\" '\"Value1\")"));
  681. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("createObject"));
  682. }
  683. };
  684. TWordCountHive elementStat = {{TString("Write"), 0}, {TString("SECRET"), 0}};
  685. VerifyProgram(res, elementStat, verifyLine);
  686. }
  687. Y_UNIT_TEST(UpsertObjectWithFeatures) {
  688. NYql::TAstParseResult res = SqlToYql("USE plato; UPSERT OBJECT secretId (TYPE SECRET) WITH (Key1=Value1, K2=V2);");
  689. UNIT_ASSERT(res.Root);
  690. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  691. if (word == "Write") {
  692. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("'('\"K2\" '\"V2\") '('\"Key1\" '\"Value1\")"));
  693. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("upsertObject"));
  694. }
  695. };
  696. TWordCountHive elementStat = { {TString("Write"), 0}, {TString("SECRET"), 0} };
  697. VerifyProgram(res, elementStat, verifyLine);
  698. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  699. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["SECRET"]);
  700. }
  701. Y_UNIT_TEST(CreateObjectWithFeaturesAndFlags) {
  702. NYql::TAstParseResult res = SqlToYql("USE plato; CREATE OBJECT secretId (TYPE SECRET) WITH (Key1=Value1, K2=V2, RECURSE);");
  703. UNIT_ASSERT(res.Root);
  704. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  705. if (word == "Write") {
  706. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("'('\"Key1\" '\"Value1\") '('\"RECURSE\")"));
  707. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("createObject"));
  708. }
  709. };
  710. TWordCountHive elementStat = { {TString("Write"), 0}, {TString("SECRET"), 0} };
  711. VerifyProgram(res, elementStat, verifyLine);
  712. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  713. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["SECRET"]);
  714. }
  715. Y_UNIT_TEST(Select1Type) {
  716. NYql::TAstParseResult res = SqlToYql("SELECT 1 type;");
  717. UNIT_ASSERT(res.Root);
  718. }
  719. Y_UNIT_TEST(SelectTableType) {
  720. NYql::TAstParseResult res = SqlToYql("USE plato; SELECT * from T type;");
  721. UNIT_ASSERT(res.Root);
  722. }
  723. Y_UNIT_TEST(CreateObjectNoFeatures) {
  724. NYql::TAstParseResult res = SqlToYql("USE plato; CREATE OBJECT secretId (TYPE SECRET);");
  725. UNIT_ASSERT(res.Root);
  726. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  727. if (word == "Write") {
  728. UNIT_ASSERT_VALUES_EQUAL(TString::npos, line.find("'features"));
  729. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("createObject"));
  730. }
  731. };
  732. TWordCountHive elementStat = { {TString("Write"), 0}, {TString("SECRET"), 0} };
  733. VerifyProgram(res, elementStat, verifyLine);
  734. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  735. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["SECRET"]);
  736. }
  737. Y_UNIT_TEST(AlterObjectWithFeatures) {
  738. NYql::TAstParseResult res = SqlToYql(
  739. "USE plato;\n"
  740. "declare $path as String;\n"
  741. "ALTER OBJECT secretId (TYPE SECRET) SET (Key1=$path, K2=V2);"
  742. );
  743. UNIT_ASSERT(res.Root);
  744. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  745. if (word == "Write") {
  746. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("'features"));
  747. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("'\"Key1\" (EvaluateAtom \"$path\""));
  748. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("'\"K2\" '\"V2\""));
  749. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("alterObject"));
  750. }
  751. };
  752. TWordCountHive elementStat = { {TString("Write"), 0}, {TString("SECRET"), 0} };
  753. VerifyProgram(res, elementStat, verifyLine);
  754. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  755. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["SECRET"]);
  756. }
  757. Y_UNIT_TEST(AlterObjectNoFeatures) {
  758. NYql::TAstParseResult res = SqlToYql("USE plato; ALTER OBJECT secretId (TYPE SECRET);");
  759. UNIT_ASSERT(!res.Root);
  760. }
  761. Y_UNIT_TEST(DropObjectNoFeatures) {
  762. NYql::TAstParseResult res = SqlToYql("USE plato; DROP OBJECT secretId (TYPE SECRET);");
  763. UNIT_ASSERT(res.Root);
  764. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  765. if (word == "Write") {
  766. UNIT_ASSERT_VALUES_EQUAL(TString::npos, line.find("'features"));
  767. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("dropObject"));
  768. }
  769. };
  770. TWordCountHive elementStat = { {TString("Write"), 0}, {TString("SECRET"), 0} };
  771. VerifyProgram(res, elementStat, verifyLine);
  772. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  773. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["SECRET"]);
  774. }
  775. Y_UNIT_TEST(DropObjectWithFeatures) {
  776. NYql::TAstParseResult res = SqlToYql("USE plato; DROP OBJECT secretId (TYPE SECRET) WITH (A, B, C);");
  777. UNIT_ASSERT(res.Root);
  778. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  779. if (word == "Write") {
  780. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("'features"));
  781. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("dropObject"));
  782. }
  783. };
  784. TWordCountHive elementStat = { {TString("Write"), 0}, {TString("SECRET"), 0} };
  785. VerifyProgram(res, elementStat, verifyLine);
  786. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  787. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["SECRET"]);
  788. }
  789. Y_UNIT_TEST(DropObjectWithOneOption) {
  790. NYql::TAstParseResult res = SqlToYql("USE plato; DROP OBJECT secretId (TYPE SECRET) WITH OVERRIDE;");
  791. UNIT_ASSERT(res.Root);
  792. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  793. if (word == "Write") {
  794. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("'features"));
  795. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("'\"OVERRIDE\""));
  796. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("dropObject"));
  797. }
  798. };
  799. TWordCountHive elementStat = { {TString("Write"), 0}, {TString("SECRET"), 0} };
  800. VerifyProgram(res, elementStat, verifyLine);
  801. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  802. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["SECRET"]);
  803. }
  804. Y_UNIT_TEST(DropObjectIfExists) {
  805. NYql::TAstParseResult res = SqlToYql("USE plato; DROP OBJECT IF EXISTS secretId (TYPE SECRET);");
  806. UNIT_ASSERT(res.Root);
  807. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  808. if (word == "Write") {
  809. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("dropObjectIfExists"));
  810. }
  811. };
  812. TWordCountHive elementStat = { {TString("Write"), 0}, {TString("SECRET"), 0} };
  813. VerifyProgram(res, elementStat, verifyLine);
  814. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  815. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["SECRET"]);
  816. }
  817. Y_UNIT_TEST(PrimaryKeyParseCorrect) {
  818. NYql::TAstParseResult res = SqlToYql("USE plato; CREATE TABLE tableName (Key Uint32, Subkey Int64, Value String, PRIMARY KEY (Key, Subkey));");
  819. UNIT_ASSERT(res.Root);
  820. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  821. if (word == "Write") {
  822. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("\"Key\""));
  823. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("\"Subkey\""));
  824. }
  825. };
  826. TWordCountHive elementStat = {{TString("Write"), 0}, {TString("primarykey"), 0}};
  827. VerifyProgram(res, elementStat, verifyLine);
  828. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  829. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["primarykey"]);
  830. }
  831. Y_UNIT_TEST(AlterDatabaseAst) {
  832. NYql::TAstParseResult request = SqlToYql("USE plato; ALTER DATABASE `/Root/test` OWNER TO user1;");
  833. UNIT_ASSERT(request.IsOk());
  834. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  835. Y_UNUSED(word);
  836. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find(
  837. R"(let world (Write! world sink (Key '('databasePath (String '"/Root/test"))) (Void) '('('mode 'alterDatabase) '('owner '"user1"))))"
  838. ));
  839. };
  840. TWordCountHive elementStat({TString("\'mode \'alterDatabase")});
  841. VerifyProgram(request, elementStat, verifyLine);
  842. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["\'mode \'alterDatabase"]);
  843. }
  844. Y_UNIT_TEST(CreateTableNonNullableYqlTypeAstCorrect) {
  845. NYql::TAstParseResult res = SqlToYql("USE plato; CREATE TABLE t (a int32 not null);");
  846. UNIT_ASSERT(res.Root);
  847. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  848. if (word == "Write!") {
  849. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos,
  850. line.find(R"__((Write! world sink (Key '('tablescheme (String '"t"))) values '('('mode 'create) '('columns '('('"a" (DataType 'Int32) '('columnConstrains '('('not_null))) '())))))))__"));
  851. }
  852. };
  853. TWordCountHive elementStat = {{TString("Write!"), 0}};
  854. VerifyProgram(res, elementStat, verifyLine);
  855. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write!"]);
  856. }
  857. Y_UNIT_TEST(CreateTableNullableYqlTypeAstCorrect) {
  858. NYql::TAstParseResult res = SqlToYql("USE plato; CREATE TABLE t (a int32);");
  859. UNIT_ASSERT(res.Root);
  860. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  861. if (word == "Write!") {
  862. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos,
  863. line.find(R"__((Write! world sink (Key '('tablescheme (String '"t"))) values '('('mode 'create) '('columns '('('"a" (AsOptionalType (DataType 'Int32)) '('columnConstrains '()) '()))))))__"));
  864. }
  865. };
  866. TWordCountHive elementStat = {{TString("Write!"), 0}};
  867. VerifyProgram(res, elementStat, verifyLine);
  868. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write!"]);
  869. }
  870. Y_UNIT_TEST(CreateTableNonNullablePgTypeAstCorrect) {
  871. NYql::TAstParseResult res = SqlToYql("USE plato; CREATE TABLE t (a pg_int4 not null);");
  872. UNIT_ASSERT(res.Root);
  873. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  874. if (word == "Write!") {
  875. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos,
  876. line.find(R"__((Write! world sink (Key '('tablescheme (String '"t"))) values '('('mode 'create) '('columns '('('"a" (PgType '_int4) '('columnConstrains '('('not_null))) '())))))))__"));
  877. }
  878. };
  879. TWordCountHive elementStat = {{TString("Write!"), 0}};
  880. VerifyProgram(res, elementStat, verifyLine);
  881. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write!"]);
  882. }
  883. Y_UNIT_TEST(CreateTableNullablePgTypeAstCorrect) {
  884. NYql::TAstParseResult res = SqlToYql("USE plato; CREATE TABLE t (a pg_int4);");
  885. UNIT_ASSERT(res.Root);
  886. res.Root->PrettyPrintTo(Cout, PRETTY_FLAGS);
  887. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  888. if (word == "Write!") {
  889. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos,
  890. line.find(R"__((Write! world sink (Key '('tablescheme (String '"t"))) values '('('mode 'create) '('columns '('('"a" (AsOptionalType (PgType '_int4)) '('columnConstrains '()) '()))))))__"));
  891. }
  892. };
  893. TWordCountHive elementStat = {{TString("Write!"), 0}};
  894. VerifyProgram(res, elementStat, verifyLine);
  895. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write!"]);
  896. }
  897. Y_UNIT_TEST(CreateTableNullPkColumnsAreAllowed) {
  898. NYql::TAstParseResult res = SqlToYql("USE plato; CREATE TABLE t (a int32, primary key(a));");
  899. UNIT_ASSERT(res.Root);
  900. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  901. if (word == "Write!") {
  902. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos,
  903. line.find(R"__((Write! world sink (Key '('tablescheme (String '"t"))) values '('('mode 'create) '('columns '('('"a" (AsOptionalType (DataType 'Int32)) '('columnConstrains '()) '()))) '('primarykey '('"a")))))__"));
  904. }
  905. };
  906. TWordCountHive elementStat = {{TString("Write!"), 0}};
  907. VerifyProgram(res, elementStat, verifyLine);
  908. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write!"]);
  909. }
  910. Y_UNIT_TEST(CreateTableNotNullPkColumnsAreIdempotentAstCorrect) {
  911. NYql::TAstParseResult res = SqlToYql("USE plato; CREATE TABLE t (a int32 not null, primary key(a));");
  912. UNIT_ASSERT(res.Root);
  913. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  914. if (word == "Write!") {
  915. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos,
  916. line.find(R"__((Write! world sink (Key '('tablescheme (String '"t"))) values '('('mode 'create) '('columns '('('"a" (DataType 'Int32) '('columnConstrains '('('not_null))) '()))) '('primarykey '('"a"))))))__"));
  917. }
  918. };
  919. TWordCountHive elementStat = {{TString("Write!"), 0}};
  920. VerifyProgram(res, elementStat, verifyLine);
  921. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write!"]);
  922. }
  923. Y_UNIT_TEST(CreateTableWithIfNotExists) {
  924. NYql::TAstParseResult res = SqlToYql("USE plato; CREATE TABLE IF NOT EXISTS t (a int32, primary key(a));");
  925. UNIT_ASSERT(res.Root);
  926. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  927. if (word == "Write!") {
  928. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos,
  929. line.find(R"__((Write! world sink (Key '('tablescheme (String '"t"))) values '('('mode 'create_if_not_exists) '('columns '('('"a" (AsOptionalType (DataType 'Int32)) '('columnConstrains '()) '()))) '('primarykey '('"a")))))__"));
  930. }
  931. };
  932. TWordCountHive elementStat = {{TString("Write!"), 0}};
  933. VerifyProgram(res, elementStat, verifyLine);
  934. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write!"]);
  935. }
  936. Y_UNIT_TEST(CreateTempTable) {
  937. NYql::TAstParseResult res = SqlToYql("USE plato; CREATE TEMP TABLE t (a int32, primary key(a));");
  938. UNIT_ASSERT(res.Root);
  939. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  940. if (word == "Write!") {
  941. UNIT_ASSERT_VALUES_UNEQUAL_C(TString::npos,
  942. line.find(R"__((Write! world sink (Key '('tablescheme (String '"t"))) values '('('mode 'create) '('columns '('('"a" (AsOptionalType (DataType 'Int32)) '('columnConstrains '()) '()))) '('primarykey '('"a")) '('temporary))))__"), line);
  943. }
  944. };
  945. TWordCountHive elementStat = {{TString("Write!"), 0}};
  946. VerifyProgram(res, elementStat, verifyLine);
  947. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write!"]);
  948. }
  949. Y_UNIT_TEST(CreateTemporaryTable) {
  950. NYql::TAstParseResult res = SqlToYql("USE plato; CREATE TEMPORARY TABLE t (a int32, primary key(a));");
  951. UNIT_ASSERT(res.Root);
  952. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  953. if (word == "Write!") {
  954. UNIT_ASSERT_VALUES_UNEQUAL_C(TString::npos,
  955. line.find(R"__((Write! world sink (Key '('tablescheme (String '"t"))) values '('('mode 'create) '('columns '('('"a" (AsOptionalType (DataType 'Int32)) '('columnConstrains '()) '()))) '('primarykey '('"a")) '('temporary))))__"), line);
  956. }
  957. };
  958. TWordCountHive elementStat = {{TString("Write!"), 0}};
  959. VerifyProgram(res, elementStat, verifyLine);
  960. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write!"]);
  961. }
  962. Y_UNIT_TEST(CreateTableWithoutTypes) {
  963. NYql::TAstParseResult res = SqlToYql("USE plato; CREATE TABLE t (a, primary key(a));");
  964. UNIT_ASSERT(!res.Root);
  965. }
  966. Y_UNIT_TEST(CreateTableAsSelectWithTypes) {
  967. NYql::TAstParseResult res = SqlToYql("USE plato; CREATE TABLE t (a int32, primary key(a)) AS SELECT * FROM ts;");
  968. UNIT_ASSERT(!res.Root);
  969. }
  970. Y_UNIT_TEST(CreateTableAsSelect) {
  971. NYql::TAstParseResult res = SqlToYql("USE plato; CREATE TABLE t (a, b, primary key(a)) AS SELECT * FROM ts;");
  972. UNIT_ASSERT_C(res.Root, res.Issues.ToString());
  973. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  974. if (word == "Write!") {
  975. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos,
  976. line.find(R"__((let world (Write! world sink (Key '('tablescheme (String '"t"))) values '('('mode 'create) '('columns '('('"a") '('"b"))) '('primarykey '('"a"))))))__"));
  977. }
  978. if (word == "Read!") {
  979. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos,
  980. line.find(R"__((Read! world (DataSource '"yt" '"plato") (MrTableConcat (Key '('table (String '"ts")))))__"));
  981. }
  982. };
  983. TWordCountHive elementStat = {{TString("Write!"), 0}, {TString("Read!"), 0}};
  984. VerifyProgram(res, elementStat, verifyLine);
  985. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write!"]);
  986. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Read!"]);
  987. }
  988. Y_UNIT_TEST(CreateTableAsSelectOnlyPrimary) {
  989. NYql::TAstParseResult res = SqlToYql("USE plato; CREATE TABLE t (primary key(a)) AS SELECT * FROM ts;");
  990. UNIT_ASSERT_C(res.Root, res.Issues.ToString());
  991. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  992. if (word == "Write!") {
  993. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos,
  994. line.find(R"__((let world (Write! world sink (Key '('tablescheme (String '"t"))) values '('('mode 'create) '('columns '()) '('primarykey '('"a"))))))__"));
  995. }
  996. if (word == "Read!") {
  997. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos,
  998. line.find(R"__((Read! world (DataSource '"yt" '"plato") (MrTableConcat (Key '('table (String '"ts")))))__"));
  999. }
  1000. };
  1001. TWordCountHive elementStat = {{TString("Write!"), 0}, {TString("Read!"), 0}};
  1002. VerifyProgram(res, elementStat, verifyLine);
  1003. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write!"]);
  1004. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Read!"]);
  1005. }
  1006. Y_UNIT_TEST(CreateTableAsValuesFail) {
  1007. NYql::TAstParseResult res = SqlToYql("USE plato; CREATE TABLE t (a, primary key(a)) AS VALUES (1), (2);");
  1008. UNIT_ASSERT(!res.Root);
  1009. }
  1010. Y_UNIT_TEST(CreateTableDuplicatedPkColumnsFail) {
  1011. NYql::TAstParseResult res = SqlToYql("USE plato; CREATE TABLE t (a int32 not null, primary key(a, a));");
  1012. UNIT_ASSERT(!res.Root);
  1013. }
  1014. Y_UNIT_TEST(DeleteFromTableByKey) {
  1015. NYql::TAstParseResult res = SqlToYql("delete from plato.Input where key = 200;", 10, "kikimr");
  1016. UNIT_ASSERT(res.Root);
  1017. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  1018. if (word == "Write") {
  1019. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("('mode 'delete)"));
  1020. }
  1021. };
  1022. TWordCountHive elementStat = {{TString("Write"), 0}};
  1023. VerifyProgram(res, elementStat, verifyLine);
  1024. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  1025. }
  1026. Y_UNIT_TEST(DeleteFromTable) {
  1027. NYql::TAstParseResult res = SqlToYql("delete from plato.Input;", 10, "kikimr");
  1028. UNIT_ASSERT(res.Root);
  1029. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  1030. if (word == "Write") {
  1031. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("('mode 'delete)"));
  1032. }
  1033. };
  1034. TWordCountHive elementStat = {{TString("Write"), 0}};
  1035. VerifyProgram(res, elementStat, verifyLine);
  1036. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  1037. }
  1038. Y_UNIT_TEST(DeleteFromTableBatch) {
  1039. NYql::TAstParseResult res = SqlToYql("batch delete from plato.Input;", 10, "kikimr");
  1040. UNIT_ASSERT(res.Root);
  1041. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  1042. if (word == "Write") {
  1043. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("('mode 'delete)"));
  1044. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("'('is_batch 'true)"));
  1045. }
  1046. };
  1047. TWordCountHive elementStat = {{TString("Write"), 0}};
  1048. VerifyProgram(res, elementStat, verifyLine);
  1049. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  1050. }
  1051. Y_UNIT_TEST(DeleteFromTableOnValues) {
  1052. NYql::TAstParseResult res = SqlToYql("delete from plato.Input on (key) values (1);",
  1053. 10, "kikimr");
  1054. UNIT_ASSERT(res.Root);
  1055. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  1056. if (word == "Write") {
  1057. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("('mode 'delete_on)"));
  1058. }
  1059. };
  1060. TWordCountHive elementStat = {{TString("Write"), 0}};
  1061. VerifyProgram(res, elementStat, verifyLine);
  1062. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  1063. }
  1064. Y_UNIT_TEST(DeleteFromTableOnSelect) {
  1065. NYql::TAstParseResult res = SqlToYql(
  1066. "delete from plato.Input on select key from plato.Input where value > 0;", 10, "kikimr");
  1067. UNIT_ASSERT(res.Root);
  1068. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  1069. if (word == "Write") {
  1070. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("('mode 'delete_on)"));
  1071. }
  1072. };
  1073. TWordCountHive elementStat = {{TString("Write"), 0}};
  1074. VerifyProgram(res, elementStat, verifyLine);
  1075. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  1076. }
  1077. Y_UNIT_TEST(DeleteFromTableOnBatch) {
  1078. NYql::TAstParseResult res = SqlToYql("batch delete from plato.Input on (key) values (1);",
  1079. 10, "kikimr");
  1080. UNIT_ASSERT(!res.Root);
  1081. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:6: Error: BATCH DELETE is unsupported with ON\n");
  1082. }
  1083. Y_UNIT_TEST(UpdateByValues) {
  1084. NYql::TAstParseResult res = SqlToYql("update plato.Input set key = 777, value = 'cool' where key = 200;", 10, "kikimr");
  1085. UNIT_ASSERT(res.Root);
  1086. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  1087. if (word == "Write") {
  1088. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("('mode 'update)"));
  1089. } else if (word == "AsStruct") {
  1090. const bool isKey = line.find("key") != TString::npos;
  1091. const bool isValue = line.find("value") != TString::npos;
  1092. UNIT_ASSERT(isKey || isValue);
  1093. if (isKey) {
  1094. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find(Quote("777")));
  1095. } else if (isValue) {
  1096. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find(Quote("cool")));
  1097. }
  1098. }
  1099. };
  1100. TWordCountHive elementStat = {{TString("Write"), 0}, {TString("AsStruct"), 0}};
  1101. VerifyProgram(res, elementStat, verifyLine);
  1102. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  1103. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["AsStruct"]);
  1104. }
  1105. Y_UNIT_TEST(UpdateByValuesBatch) {
  1106. NYql::TAstParseResult res = SqlToYql("batch update plato.Input set key = 777, value = 'cool' where key = 200;", 10, "kikimr");
  1107. UNIT_ASSERT(res.Root);
  1108. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  1109. if (word == "Write") {
  1110. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("('mode 'update)"));
  1111. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("'('is_batch 'true)"));
  1112. }
  1113. };
  1114. TWordCountHive elementStat = {{TString("Write"), 0}};
  1115. VerifyProgram(res, elementStat, verifyLine);
  1116. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  1117. }
  1118. Y_UNIT_TEST(UpdateByMultiValues) {
  1119. NYql::TAstParseResult res = SqlToYql("update plato.Input set (key, value, subkey) = ('2','ddd',':') where key = 200;", 10, "kikimr");
  1120. UNIT_ASSERT(res.Root);
  1121. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  1122. if (word == "Write") {
  1123. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("('mode 'update)"));
  1124. } else if (word == "AsStruct") {
  1125. const bool isKey = line.find("key") != TString::npos;
  1126. const bool isSubkey = line.find("subkey") != TString::npos;
  1127. const bool isValue = line.find("value") != TString::npos;
  1128. UNIT_ASSERT(isKey || isSubkey || isValue);
  1129. if (isKey && !isSubkey) {
  1130. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find(Quote("2")));
  1131. } else if (isSubkey) {
  1132. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find(Quote(":")));
  1133. } else if (isValue) {
  1134. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find(Quote("ddd")));
  1135. }
  1136. }
  1137. };
  1138. TWordCountHive elementStat = {{TString("Write"), 0}, {TString("AsStruct"), 0}};
  1139. VerifyProgram(res, elementStat, verifyLine);
  1140. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  1141. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["AsStruct"]);
  1142. }
  1143. Y_UNIT_TEST(UpdateBySelect) {
  1144. NYql::TAstParseResult res = SqlToYql("update plato.Input set (key, value, subkey) = (select key, value, subkey from plato.Input where key = 911) where key = 200;", 10, "kikimr");
  1145. UNIT_ASSERT(res.Root);
  1146. int lineIndex = 0;
  1147. int writeLineIndex = -1;
  1148. bool found = false;
  1149. TVerifyLineFunc verifyLine = [&lineIndex, &writeLineIndex, &found](const TString& word, const TString& line) {
  1150. if (word == "Write") {
  1151. writeLineIndex = lineIndex;
  1152. found = line.find("('mode 'update)") != TString::npos;
  1153. } else if (word == "mode") {
  1154. found |= lineIndex == writeLineIndex + 1 && line.find("('mode 'update)") != TString::npos;
  1155. UNIT_ASSERT(found);
  1156. }
  1157. ++lineIndex;
  1158. };
  1159. TWordCountHive elementStat = {{TString("Write"), 0}, {TString("mode"), 0}};
  1160. VerifyProgram(res, elementStat, verifyLine);
  1161. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  1162. }
  1163. Y_UNIT_TEST(UpdateSelfModifyAll) {
  1164. NYql::TAstParseResult res = SqlToYql("update plato.Input set subkey = subkey + 's';", 10, "kikimr");
  1165. UNIT_ASSERT(res.Root);
  1166. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  1167. if (word == "Write") {
  1168. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("('mode 'update)"));
  1169. } else if (word == "AsStruct") {
  1170. const bool isSubkey = line.find("subkey") != TString::npos;
  1171. UNIT_ASSERT(isSubkey);
  1172. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find(Quote("subkey")));
  1173. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find(Quote("s")));
  1174. }
  1175. };
  1176. TWordCountHive elementStat = {{TString("Write"), 0}, {TString("AsStruct"), 0}};
  1177. VerifyProgram(res, elementStat, verifyLine);
  1178. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  1179. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["AsStruct"]);
  1180. }
  1181. Y_UNIT_TEST(UpdateOnValues) {
  1182. NYql::TAstParseResult res = SqlToYql("update plato.Input on (key, value) values (5, 'cool')", 10, "kikimr");
  1183. UNIT_ASSERT(res.Root);
  1184. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  1185. if (word == "Write") {
  1186. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("('mode 'update_on)"));
  1187. }
  1188. };
  1189. TWordCountHive elementStat = {{TString("Write"), 0}};
  1190. VerifyProgram(res, elementStat, verifyLine);
  1191. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  1192. }
  1193. Y_UNIT_TEST(UpdateOnSelect) {
  1194. NYql::TAstParseResult res = SqlToYql(
  1195. "update plato.Input on select key, value + 1 as value from plato.Input", 10, "kikimr");
  1196. UNIT_ASSERT(res.Root);
  1197. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  1198. if (word == "Write") {
  1199. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("('mode 'update_on)"));
  1200. }
  1201. };
  1202. TWordCountHive elementStat = {{TString("Write"), 0}};
  1203. VerifyProgram(res, elementStat, verifyLine);
  1204. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  1205. }
  1206. Y_UNIT_TEST(UpdateOnBatch) {
  1207. NYql::TAstParseResult res = SqlToYql("batch update plato.Input on (key, value) values (5, 'cool')", 10, "kikimr");
  1208. UNIT_ASSERT(!res.Root);
  1209. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:6: Error: BATCH UPDATE is unsupported with ON\n");
  1210. }
  1211. Y_UNIT_TEST(UnionAllTest) {
  1212. NYql::TAstParseResult res = SqlToYql("PRAGMA DisableEmitUnionMerge; SELECT key FROM plato.Input UNION ALL select subkey FROM plato.Input;");
  1213. UNIT_ASSERT(res.Root);
  1214. TWordCountHive elementStat = {{TString("UnionAll"), 0}};
  1215. VerifyProgram(res, elementStat, {});
  1216. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["UnionAll"]);
  1217. }
  1218. Y_UNIT_TEST(UnionAllMergeTest) {
  1219. NYql::TAstParseResult res = SqlToYql("PRAGMA EmitUnionMerge; SELECT key FROM plato.Input UNION ALL select subkey FROM plato.Input;");
  1220. UNIT_ASSERT(res.Root);
  1221. TWordCountHive elementStat = {{TString("UnionMerge"), 0}};
  1222. VerifyProgram(res, elementStat, {});
  1223. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["UnionMerge"]);
  1224. }
  1225. Y_UNIT_TEST(UnionTest) {
  1226. NYql::TAstParseResult res = SqlToYql("SELECT key FROM plato.Input UNION select subkey FROM plato.Input;");
  1227. UNIT_ASSERT(res.Root);
  1228. TWordCountHive elementStat = {{TString("Union"), 0}};
  1229. VerifyProgram(res, elementStat, {});
  1230. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Union"]);
  1231. }
  1232. Y_UNIT_TEST(UnionAggregationTest) {
  1233. NYql::TAstParseResult res = SqlToYql(R"(
  1234. PRAGMA DisableEmitUnionMerge;
  1235. SELECT 1
  1236. UNION ALL
  1237. SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1
  1238. UNION
  1239. SELECT 1 UNION SELECT 1 UNION SELECT 1 UNION SELECT 1
  1240. UNION ALL
  1241. SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1;
  1242. )");
  1243. UNIT_ASSERT(res.Root);
  1244. TWordCountHive elementStat = {{TString("Union"), 0}, {TString("UnionAll"), 0}};
  1245. VerifyProgram(res, elementStat, {});
  1246. UNIT_ASSERT_VALUES_EQUAL(2, elementStat["UnionAll"]);
  1247. UNIT_ASSERT_VALUES_EQUAL(3, elementStat["Union"]);
  1248. }
  1249. Y_UNIT_TEST(UnionMergeAggregationTest) {
  1250. NYql::TAstParseResult res = SqlToYql(R"(
  1251. PRAGMA EmitUnionMerge;
  1252. SELECT 1
  1253. UNION ALL
  1254. SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1
  1255. UNION
  1256. SELECT 1 UNION SELECT 1 UNION SELECT 1 UNION SELECT 1
  1257. UNION ALL
  1258. SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1;
  1259. )");
  1260. UNIT_ASSERT(res.Root);
  1261. TWordCountHive elementStat = {{TString("Union"), 0}, {TString("UnionMerge"), 0}};
  1262. VerifyProgram(res, elementStat, {});
  1263. UNIT_ASSERT_VALUES_EQUAL(2, elementStat["UnionMerge"]);
  1264. UNIT_ASSERT_VALUES_EQUAL(3, elementStat["Union"]);
  1265. }
  1266. Y_UNIT_TEST(DeclareDecimalParameter) {
  1267. NYql::TAstParseResult res = SqlToYql("declare $value as Decimal(22,9); select $value as cnt;");
  1268. UNIT_ASSERT(res.Root);
  1269. }
  1270. Y_UNIT_TEST(SimpleGroupBy) {
  1271. NYql::TAstParseResult res = SqlToYql("select count(1),z from plato.Input group by key as z order by z;");
  1272. UNIT_ASSERT(res.Root);
  1273. }
  1274. Y_UNIT_TEST(EmptyColumnName0) {
  1275. /// Now it's parsed well and error occur on validate step like "4:31:Empty struct member name is not allowed" in "4:31:Function: AddMember"
  1276. NYql::TAstParseResult res = SqlToYql("insert into plato.Output (``, list1) values (0, AsList(0, 1, 2));");
  1277. /// Verify that parsed well without crash
  1278. UNIT_ASSERT(res.Root);
  1279. }
  1280. Y_UNIT_TEST(KikimrRollback) {
  1281. NYql::TAstParseResult res = SqlToYql("use plato; select * from Input; rollback;", 10, "kikimr");
  1282. UNIT_ASSERT(res.Root);
  1283. TWordCountHive elementStat = {{TString("rollback"), 0}};
  1284. VerifyProgram(res, elementStat);
  1285. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["rollback"]);
  1286. }
  1287. Y_UNIT_TEST(PragmaFile) {
  1288. NYql::TAstParseResult res = SqlToYql(R"(pragma file("HW", "sbr:181041334");)");
  1289. UNIT_ASSERT(res.Root);
  1290. TWordCountHive elementStat = {{TString(R"((let world (Configure! world (DataSource '"config") '"AddFileByUrl" '"HW" '"sbr:181041334")))"), 0}};
  1291. VerifyProgram(res, elementStat);
  1292. UNIT_ASSERT_VALUES_EQUAL(1, elementStat.cbegin()->second);
  1293. }
  1294. Y_UNIT_TEST(DoNotCrashOnNamedInFilter) {
  1295. NYql::TAstParseResult res = SqlToYql("USE plato; $all = ($table_name) -> { return true; }; SELECT * FROM FILTER(Input, $all)");
  1296. UNIT_ASSERT(res.Root);
  1297. }
  1298. Y_UNIT_TEST(PragmasFileAndUdfOrder) {
  1299. NYql::TAstParseResult res = SqlToYql(R"(
  1300. PRAGMA file("libvideoplayers_udf.so", "https://proxy.sandbox.yandex-team.ru/235185290");
  1301. PRAGMA udf("libvideoplayers_udf.so");
  1302. )");
  1303. UNIT_ASSERT(res.Root);
  1304. const auto programm = GetPrettyPrint(res);
  1305. const auto file = programm.find("AddFileByUrl");
  1306. const auto udfs = programm.find("ImportUdfs");
  1307. UNIT_ASSERT(file < udfs);
  1308. }
  1309. Y_UNIT_TEST(ProcessUserType) {
  1310. NYql::TAstParseResult res = SqlToYql("process plato.Input using Kikimr::PushData(TableRows());", 1, TString(NYql::KikimrProviderName));
  1311. UNIT_ASSERT(res.Root);
  1312. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  1313. if (word == "Kikimr.PushData") {
  1314. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("TupleType"));
  1315. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("TypeOf"));
  1316. }
  1317. };
  1318. TWordCountHive elementStat = {{TString("Kikimr.PushData"), 0}};
  1319. VerifyProgram(res, elementStat, verifyLine);
  1320. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Kikimr.PushData"]);
  1321. }
  1322. Y_UNIT_TEST(ProcessUserTypeAuth) {
  1323. NYql::TAstParseResult res = SqlToYql("process plato.Input using YDB::PushData(TableRows(), AsTuple('oauth', SecureParam('api:oauth')));", 1, TString(NYql::KikimrProviderName));
  1324. UNIT_ASSERT(res.Root);
  1325. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  1326. if (word == "YDB.PushData") {
  1327. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("TupleType"));
  1328. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("TypeOf"));
  1329. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("api:oauth"));
  1330. }
  1331. };
  1332. TWordCountHive elementStat = {{TString("YDB.PushData"), 0}};
  1333. VerifyProgram(res, elementStat, verifyLine);
  1334. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["YDB.PushData"]);
  1335. }
  1336. Y_UNIT_TEST(SelectStreamRtmr) {
  1337. NYql::TAstParseResult res = SqlToYql(
  1338. "USE plato; INSERT INTO Output SELECT STREAM key FROM Input;",
  1339. 10, TString(NYql::RtmrProviderName));
  1340. UNIT_ASSERT(res.Root);
  1341. res = SqlToYql(
  1342. "USE plato; INSERT INTO Output SELECT key FROM Input;",
  1343. 10, TString(NYql::RtmrProviderName));
  1344. UNIT_ASSERT(res.Root);
  1345. }
  1346. Y_UNIT_TEST(SelectStreamRtmrJoinWithYt) {
  1347. NYql::TAstParseResult res = SqlToYql(
  1348. "USE plato; INSERT INTO Output SELECT STREAM key FROM Input LEFT JOIN hahn.ttt as t ON Input.key = t.Name;",
  1349. 10, TString(NYql::RtmrProviderName));
  1350. UNIT_ASSERT(res.Root);
  1351. }
  1352. Y_UNIT_TEST(SelectStreamNonRtmr) {
  1353. NYql::TAstParseResult res = SqlToYql(
  1354. "USE plato; INSERT INTO Output SELECT STREAM key FROM Input;",
  1355. 10);
  1356. UNIT_ASSERT(!res.Root);
  1357. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:31: Error: SELECT STREAM is unsupported for non-streaming sources\n");
  1358. }
  1359. Y_UNIT_TEST(GroupByHopRtmr) {
  1360. NYql::TAstParseResult res = SqlToYql(R"(
  1361. USE plato; INSERT INTO Output SELECT key, SUM(value) AS value FROM Input
  1362. GROUP BY key, HOP(subkey, "PT10S", "PT30S", "PT20S");
  1363. )", 10, TString(NYql::RtmrProviderName));
  1364. UNIT_ASSERT(res.Root);
  1365. }
  1366. Y_UNIT_TEST(GroupByHopRtmrSubquery) {
  1367. // 'use plato' intentially avoided
  1368. NYql::TAstParseResult res = SqlToYql(R"(
  1369. SELECT COUNT(*) AS value FROM (SELECT * FROM plato.Input)
  1370. GROUP BY HOP(Data, "PT10S", "PT30S", "PT20S")
  1371. )", 10, TString(NYql::RtmrProviderName));
  1372. UNIT_ASSERT(res.Root);
  1373. }
  1374. Y_UNIT_TEST(GroupByHopRtmrSubqueryBinding) {
  1375. NYql::TAstParseResult res = SqlToYql(R"(
  1376. USE plato;
  1377. $q = SELECT * FROM Input;
  1378. INSERT INTO Output SELECT STREAM * FROM (
  1379. SELECT COUNT(*) AS value FROM $q
  1380. GROUP BY HOP(Data, "PT10S", "PT30S", "PT20S")
  1381. );
  1382. )", 10, TString(NYql::RtmrProviderName));
  1383. UNIT_ASSERT(res.Root);
  1384. }
  1385. Y_UNIT_TEST(GroupByNoHopRtmr) {
  1386. NYql::TAstParseResult res = SqlToYql(R"(
  1387. USE plato; INSERT INTO Output SELECT STREAM key, SUM(value) AS value FROM Input
  1388. GROUP BY key;
  1389. )", 10, TString(NYql::RtmrProviderName));
  1390. UNIT_ASSERT(!res.Root);
  1391. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:3:22: Error: Streaming group by query must have a hopping window specification.\n");
  1392. }
  1393. Y_UNIT_TEST(KikimrInserts) {
  1394. NYql::TAstParseResult res = SqlToYql(R"(
  1395. USE plato;
  1396. INSERT INTO Output SELECT key, value FROM Input;
  1397. INSERT OR ABORT INTO Output SELECT key, value FROM Input;
  1398. INSERT OR IGNORE INTO Output SELECT key, value FROM Input;
  1399. INSERT OR REVERT INTO Output SELECT key, value FROM Input;
  1400. )", 10, TString(NYql::KikimrProviderName));
  1401. UNIT_ASSERT(res.Root);
  1402. }
  1403. Y_UNIT_TEST(WarnMissingIsBeforeNotNull) {
  1404. NYql::TAstParseResult res = SqlToYql("select 1 NOT NULL");
  1405. UNIT_ASSERT(res.Root);
  1406. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:8: Warning: Missing IS keyword before NOT NULL, code: 4507\n");
  1407. }
  1408. Y_UNIT_TEST(Subqueries) {
  1409. NYql::TAstParseResult res = SqlToYql(R"(
  1410. USE plato;
  1411. $sq1 = (SELECT * FROM plato.Input);
  1412. $sq2 = SELECT * FROM plato.Input;
  1413. $squ1 = (
  1414. SELECT * FROM plato.Input
  1415. UNION ALL
  1416. SELECT * FROM plato.Input
  1417. );
  1418. $squ2 =
  1419. SELECT * FROM plato.Input
  1420. UNION ALL
  1421. SELECT * FROM plato.Input;
  1422. $squ3 = (
  1423. (SELECT * FROM plato.Input)
  1424. UNION ALL
  1425. (SELECT * FROM plato.Input)
  1426. );
  1427. SELECT * FROM $sq1;
  1428. SELECT * FROM $sq2;
  1429. SELECT * FROM $squ1;
  1430. SELECT * FROM $squ2;
  1431. SELECT * FROM $squ3;
  1432. )");
  1433. UNIT_ASSERT(res.Root);
  1434. }
  1435. Y_UNIT_TEST(SubqueriesJoin) {
  1436. NYql::TAstParseResult res = SqlToYql(R"(
  1437. USE plato;
  1438. $left = SELECT * FROM plato.Input1 WHERE value != "BadValue";
  1439. $right = SELECT * FROM plato.Input2;
  1440. SELECT * FROM $left AS l
  1441. JOIN $right AS r
  1442. ON l.key == r.key;
  1443. )");
  1444. UNIT_ASSERT(res.Root);
  1445. }
  1446. Y_UNIT_TEST(AnyInBackticksAsTableName) {
  1447. NYql::TAstParseResult res = SqlToYql("use plato; select * from `any`;");
  1448. UNIT_ASSERT(res.Root);
  1449. }
  1450. Y_UNIT_TEST(AnyJoinForTableAndSubQuery) {
  1451. NYql::TAstParseResult res = SqlToYql(R"(
  1452. USE plato;
  1453. $r = SELECT * FROM plato.Input2;
  1454. SELECT * FROM ANY plato.Input1 AS l
  1455. LEFT JOIN ANY $r AS r
  1456. USING (key);
  1457. )");
  1458. UNIT_ASSERT(res.Root);
  1459. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  1460. if (word == "EquiJoin") {
  1461. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("'('left 'any)"));
  1462. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("'('right 'any)"));
  1463. }
  1464. };
  1465. TWordCountHive elementStat = {{TString("left"), 0}, {TString("right"), 0}};
  1466. VerifyProgram(res, elementStat, verifyLine);
  1467. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["left"]);
  1468. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["right"]);
  1469. }
  1470. Y_UNIT_TEST(AnyJoinForTableAndTableSource) {
  1471. NYql::TAstParseResult res = SqlToYql(R"(
  1472. USE plato;
  1473. $r = AsList(
  1474. AsStruct("aaa" as key, "bbb" as subkey, "ccc" as value)
  1475. );
  1476. SELECT * FROM ANY plato.Input1 AS l
  1477. LEFT JOIN ANY AS_TABLE($r) AS r
  1478. USING (key);
  1479. )");
  1480. UNIT_ASSERT(res.Root);
  1481. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  1482. if (word == "EquiJoin") {
  1483. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("'('left 'any)"));
  1484. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("'('right 'any)"));
  1485. }
  1486. };
  1487. TWordCountHive elementStat = {{TString("left"), 0}, {TString("right"), 0}};
  1488. VerifyProgram(res, elementStat, verifyLine);
  1489. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["left"]);
  1490. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["right"]);
  1491. }
  1492. Y_UNIT_TEST(AnyJoinNested) {
  1493. NYql::TAstParseResult res = SqlToYql(R"(
  1494. USE plato;
  1495. FROM ANY Input1 as a
  1496. JOIN Input2 as b ON a.key = b.key
  1497. LEFT JOIN ANY Input3 as c ON a.key = c.key
  1498. RIGHT JOIN ANY Input4 as d ON d.key = b.key
  1499. CROSS JOIN Input5
  1500. SELECT *;
  1501. )");
  1502. UNIT_ASSERT(res.Root);
  1503. TWordCountHive elementStat = {{TString("left"), 0}, {TString("right"), 0}};
  1504. VerifyProgram(res, elementStat);
  1505. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["left"]);
  1506. UNIT_ASSERT_VALUES_EQUAL(2, elementStat["right"]);
  1507. }
  1508. Y_UNIT_TEST(InlineAction) {
  1509. NYql::TAstParseResult res = SqlToYql(
  1510. "do begin\n"
  1511. " select 1\n"
  1512. "; end do\n");
  1513. UNIT_ASSERT(res.Root);
  1514. UNIT_ASSERT_NO_DIFF(Err2Str(res), "");
  1515. }
  1516. Y_UNIT_TEST(FlattenByCorrelationName) {
  1517. UNIT_ASSERT(SqlToYql("select * from plato.Input as t flatten by t.x").IsOk());
  1518. UNIT_ASSERT(SqlToYql("select * from plato.Input as t flatten by t -- same as flatten by t.t").IsOk());
  1519. }
  1520. Y_UNIT_TEST(DiscoveryMode) {
  1521. UNIT_ASSERT(SqlToYqlWithMode("insert into plato.Output select * from plato.Input", NSQLTranslation::ESqlMode::DISCOVERY).IsOk());
  1522. UNIT_ASSERT(SqlToYqlWithMode("select * from plato.concat(Input1, Input2)", NSQLTranslation::ESqlMode::DISCOVERY).IsOk());
  1523. UNIT_ASSERT(SqlToYqlWithMode("select * from plato.each(AsList(\"Input1\", \"Input2\"))", NSQLTranslation::ESqlMode::DISCOVERY).IsOk());
  1524. }
  1525. Y_UNIT_TEST(CubeWithAutoGeneratedLikeColumnName) {
  1526. UNIT_ASSERT(SqlToYql("select key,subkey,group from plato.Input group by cube(key,subkey,group)").IsOk());
  1527. }
  1528. Y_UNIT_TEST(CubeWithAutoGeneratedLikeAlias) {
  1529. UNIT_ASSERT(SqlToYql("select key,subkey,group from plato.Input group by cube(key,subkey,value as group)").IsOk());
  1530. }
  1531. Y_UNIT_TEST(FilterCanBeUsedAsColumnIdOrBind) {
  1532. UNIT_ASSERT(SqlToYql("select filter from plato.Input").IsOk());
  1533. UNIT_ASSERT(SqlToYql("select 1 as filter").IsOk());
  1534. UNIT_ASSERT(SqlToYql("$filter = 1; select $filter").IsOk());
  1535. }
  1536. Y_UNIT_TEST(DuplicateSemicolonsAreAllowedBetweenTopLevelStatements) {
  1537. UNIT_ASSERT(SqlToYql(";;select 1; ; select 2;/*comment*/;select 3;;--comment\n;select 4;;").IsOk());
  1538. }
  1539. Y_UNIT_TEST(DuplicateAndMissingTrailingSemicolonsAreAllowedBetweenActionStatements) {
  1540. TString req =
  1541. "define action $action($b,$c) as\n"
  1542. " ;;$d = $b + $c;\n"
  1543. " select $b;\n"
  1544. " select $c;;\n"
  1545. " select $d,\n"
  1546. "end define;\n"
  1547. "\n"
  1548. "do $action(1,2);";
  1549. UNIT_ASSERT(SqlToYql(req).IsOk());
  1550. }
  1551. Y_UNIT_TEST(DuplicateAndMissingTrailingSemicolonsAreAllowedBetweenInlineActionStatements) {
  1552. TString req =
  1553. "do begin\n"
  1554. " ;select 1,\n"
  1555. "end do;\n"
  1556. "evaluate for $i in AsList(1,2,3) do begin\n"
  1557. " select $i;;\n"
  1558. " select $i + $i;;\n"
  1559. "end do;";
  1560. UNIT_ASSERT(SqlToYql(req).IsOk());
  1561. }
  1562. Y_UNIT_TEST(DuplicateSemicolonsAreAllowedBetweenLambdaStatements) {
  1563. TString req =
  1564. "$x=1;\n"
  1565. "$foo = ($a, $b)->{\n"
  1566. " ;;$v = $a + $b;\n"
  1567. " $bar = ($c) -> {; return $c << $x};;\n"
  1568. " return $bar($v);;\n"
  1569. "};\n"
  1570. "select $foo(1,2);";
  1571. UNIT_ASSERT(SqlToYql(req).IsOk());
  1572. }
  1573. Y_UNIT_TEST(StringLiteralWithEscapedBackslash) {
  1574. NYql::TAstParseResult res1 = SqlToYql(R"foo(SELECT 'a\\';)foo");
  1575. NYql::TAstParseResult res2 = SqlToYql(R"foo(SELECT "a\\";)foo");
  1576. UNIT_ASSERT(res1.Root);
  1577. UNIT_ASSERT(res2.Root);
  1578. TWordCountHive elementStat = {{TString("a\\"), 0}};
  1579. VerifyProgram(res1, elementStat);
  1580. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["a\\"]);
  1581. VerifyProgram(res2, elementStat);
  1582. UNIT_ASSERT_VALUES_EQUAL(2, elementStat["a\\"]);
  1583. }
  1584. Y_UNIT_TEST(StringMultiLineLiteralWithEscapes) {
  1585. UNIT_ASSERT(SqlToYql("SELECT @@@foo@@@@bar@@@").IsOk());
  1586. UNIT_ASSERT(SqlToYql("SELECT @@@@@@@@@").IsOk());
  1587. }
  1588. Y_UNIT_TEST(StringMultiLineLiteralConsequitiveAt) {
  1589. UNIT_ASSERT(!SqlToYql("SELECT @").IsOk());
  1590. UNIT_ASSERT(!SqlToYql("SELECT @@").IsOk());
  1591. UNIT_ASSERT(!SqlToYql("SELECT @@@").IsOk());
  1592. UNIT_ASSERT( SqlToYql("SELECT @@@@").IsOk());
  1593. UNIT_ASSERT( SqlToYql("SELECT @@@@@").IsOk());
  1594. UNIT_ASSERT(!SqlToYql("SELECT @@@@@@").IsOk());
  1595. UNIT_ASSERT(!SqlToYql("SELECT @@@@@@@").IsOk());
  1596. UNIT_ASSERT( SqlToYql("SELECT @@@@@@@@").IsOk());
  1597. UNIT_ASSERT( SqlToYql("SELECT @@@@@@@@@").IsOk());
  1598. UNIT_ASSERT(!SqlToYql("SELECT @@@@@@@@@@").IsOk());
  1599. }
  1600. Y_UNIT_TEST(ConstnessForListDictSetCreate) {
  1601. auto req = "$foo = ($x, $y) -> (\"aaaa\");\n"
  1602. "\n"
  1603. "select\n"
  1604. " $foo(sum(key), ListCreate(String)),\n"
  1605. " $foo(sum(key), DictCreate(String, String)),\n"
  1606. " $foo(sum(key), SetCreate(String)),\n"
  1607. "from (select 1 as key);";
  1608. UNIT_ASSERT(SqlToYql(req).IsOk());
  1609. }
  1610. Y_UNIT_TEST(CanUseEmptyTupleInWindowPartitionBy) {
  1611. auto req = "select sum(key) over w\n"
  1612. "from plato.Input\n"
  1613. "window w as (partition compact by (), (subkey), (), value || value as dvalue);";
  1614. UNIT_ASSERT(SqlToYql(req).IsOk());
  1615. }
  1616. Y_UNIT_TEST(DenyAnsiOrderByLimitLegacyMode) {
  1617. auto req = "pragma DisableAnsiOrderByLimitInUnionAll;\n"
  1618. "use plato;\n"
  1619. "\n"
  1620. "select * from Input order by key limit 10\n"
  1621. "union all\n"
  1622. "select * from Input order by key limit 1;";
  1623. auto res = SqlToYql(req);
  1624. UNIT_ASSERT(!res.Root);
  1625. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:8: Error: DisableAnsiOrderByLimitInUnionAll pragma is deprecated and no longer supported\n");
  1626. }
  1627. Y_UNIT_TEST(ReduceUsingUdfWithShortcutsWorks) {
  1628. auto req = "use plato;\n"
  1629. "\n"
  1630. "$arg = 'foo';\n"
  1631. "$func = XXX::YYY($arg);\n"
  1632. "\n"
  1633. "REDUCE Input ON key using $func(subkey);\n"
  1634. "REDUCE Input ON key using $func(UUU::VVV(TableRow()));\n";
  1635. UNIT_ASSERT(SqlToYql(req).IsOk());
  1636. req = "use plato;\n"
  1637. "\n"
  1638. "$arg = 'foo';\n"
  1639. "$func = XXX::YYY($arg);\n"
  1640. "\n"
  1641. "REDUCE Input ON key using all $func(subkey);\n"
  1642. "REDUCE Input ON key using all $func(UUU::VVV(TableRow()));";
  1643. UNIT_ASSERT(SqlToYql(req).IsOk());
  1644. }
  1645. Y_UNIT_TEST(YsonDisableStrict) {
  1646. UNIT_ASSERT(SqlToYql("pragma yson.DisableStrict = \"false\";").IsOk());
  1647. UNIT_ASSERT(SqlToYql("pragma yson.DisableStrict;").IsOk());
  1648. }
  1649. Y_UNIT_TEST(YsonStrict) {
  1650. UNIT_ASSERT(SqlToYql("pragma yson.Strict = \"false\";").IsOk());
  1651. UNIT_ASSERT(SqlToYql("pragma yson.Strict;").IsOk());
  1652. }
  1653. Y_UNIT_TEST(JoinByTuple) {
  1654. auto req = "use plato;\n"
  1655. "\n"
  1656. "select * from T1 as a\n"
  1657. "join T2 as b\n"
  1658. "on AsTuple(a.key, a.subkey) = AsTuple(b.key, b.subkey);";
  1659. UNIT_ASSERT(SqlToYql(req).IsOk());
  1660. }
  1661. Y_UNIT_TEST(JoinByStruct) {
  1662. auto req = "use plato;\n"
  1663. "\n"
  1664. "select * from T1 as a\n"
  1665. "join T2 as b\n"
  1666. "on AsStruct(a.key as k, a.subkey as sk) = AsStruct(b.key as k, b.subkey as sk);";
  1667. UNIT_ASSERT(SqlToYql(req).IsOk());
  1668. }
  1669. Y_UNIT_TEST(JoinByUdf) {
  1670. auto req = "use plato;\n"
  1671. "\n"
  1672. "select a.align\n"
  1673. "from T1 as a\n"
  1674. "join T2 as b\n"
  1675. "on Yson::SerializeJsonEncodeUtf8(a.align)=b.align;";
  1676. UNIT_ASSERT(SqlToYql(req).IsOk());
  1677. }
  1678. Y_UNIT_TEST(EscapedIdentifierAsLambdaArg) {
  1679. auto req = "$f = ($`foo bar`, $x) -> { return $`foo bar` + $x; };\n"
  1680. "\n"
  1681. "select $f(1, 2);";
  1682. auto res = SqlToYql(req);
  1683. UNIT_ASSERT(res.Root);
  1684. const auto programm = GetPrettyPrint(res);
  1685. auto expected = "(lambda '(\"$foo bar\" \"$x\")";
  1686. UNIT_ASSERT(programm.find(expected) != TString::npos);
  1687. }
  1688. Y_UNIT_TEST(UdfSyntaxSugarOnlyCallable) {
  1689. auto req = "SELECT Udf(DateTime::FromString)('2022-01-01');";
  1690. auto res = SqlToYql(req);
  1691. UNIT_ASSERT(res.Root);
  1692. const auto programm = GetPrettyPrint(res);
  1693. auto expected = "(SqlCall '\"DateTime.FromString\" '((PositionalArgs (String '\"2022-01-01\")) (AsStruct)) (TupleType (TypeOf '((String '\"2022-01-01\"))) (TypeOf (AsStruct)) (TupleType)))";
  1694. UNIT_ASSERT(programm.find(expected) != TString::npos);
  1695. }
  1696. Y_UNIT_TEST(UdfSyntaxSugarTypeNoRun) {
  1697. auto req = "SELECT Udf(DateTime::FromString, String, Tuple<Int32, Float>, 'foo' as TypeConfig)('2022-01-01');";
  1698. auto res = SqlToYql(req);
  1699. UNIT_ASSERT(res.Root);
  1700. const auto programm = GetPrettyPrint(res);
  1701. auto expected = "(SqlCall '\"DateTime.FromString\" '((PositionalArgs (String '\"2022-01-01\")) (AsStruct)) (TupleType (TypeOf '((String '\"2022-01-01\"))) (TypeOf (AsStruct)) (TupleType (DataType 'String) (TupleType (DataType 'Int32) (DataType 'Float)))) '\"foo\")";
  1702. UNIT_ASSERT(programm.find(expected) != TString::npos);
  1703. }
  1704. Y_UNIT_TEST(UdfSyntaxSugarRunNoType) {
  1705. auto req = "SELECT Udf(DateTime::FromString, String, Tuple<Int32, Float>, Void() as RunConfig)('2022-01-01');";
  1706. auto res = SqlToYql(req);
  1707. UNIT_ASSERT(res.Root);
  1708. const auto programm = GetPrettyPrint(res);
  1709. auto expected = "(SqlCall '\"DateTime.FromString\" '((PositionalArgs (String '\"2022-01-01\")) (AsStruct)) (TupleType (TypeOf '((String '\"2022-01-01\"))) (TypeOf (AsStruct)) (TupleType (DataType 'String) (TupleType (DataType 'Int32) (DataType 'Float)))) '\"\" (Void))";
  1710. UNIT_ASSERT(programm.find(expected) != TString::npos);
  1711. }
  1712. Y_UNIT_TEST(UdfSyntaxSugarFullTest) {
  1713. auto req = "SELECT Udf(DateTime::FromString, String, Tuple<Int32, Float>, 'foo' as TypeConfig, Void() As RunConfig)('2022-01-01');";
  1714. auto res = SqlToYql(req);
  1715. UNIT_ASSERT(res.Root);
  1716. const auto programm = GetPrettyPrint(res);
  1717. auto expected = "(SqlCall '\"DateTime.FromString\" '((PositionalArgs (String '\"2022-01-01\")) (AsStruct)) (TupleType (TypeOf '((String '\"2022-01-01\"))) (TypeOf (AsStruct)) (TupleType (DataType 'String) (TupleType (DataType 'Int32) (DataType 'Float)))) '\"foo\" (Void))";
  1718. UNIT_ASSERT(programm.find(expected) != TString::npos);
  1719. }
  1720. Y_UNIT_TEST(UdfSyntaxSugarOtherRunConfigs) {
  1721. auto req = "SELECT Udf(DateTime::FromString, String, Tuple<Int32, Float>, 'foo' as TypeConfig, '55' As RunConfig)('2022-01-01');";
  1722. auto res = SqlToYql(req);
  1723. UNIT_ASSERT(res.Root);
  1724. const auto programm = GetPrettyPrint(res);
  1725. auto expected = "(SqlCall '\"DateTime.FromString\" '((PositionalArgs (String '\"2022-01-01\")) (AsStruct)) (TupleType (TypeOf '((String '\"2022-01-01\"))) (TypeOf (AsStruct)) (TupleType (DataType 'String) (TupleType (DataType 'Int32) (DataType 'Float)))) '\"foo\" (String '\"55\"))";
  1726. UNIT_ASSERT(programm.find(expected) != TString::npos);
  1727. }
  1728. Y_UNIT_TEST(UdfSyntaxSugarOtherRunConfigs2) {
  1729. auto req = "SELECT Udf(DateTime::FromString, String, Tuple<Int32, Float>, 'foo' as TypeConfig, AsTuple(32, 'no', AsStruct(1e-9 As SomeFloat)) As RunConfig)('2022-01-01');";
  1730. auto res = SqlToYql(req);
  1731. UNIT_ASSERT(res.Root);
  1732. const auto programm = GetPrettyPrint(res);
  1733. auto expected = "(SqlCall '\"DateTime.FromString\" '((PositionalArgs (String '\"2022-01-01\")) (AsStruct)) (TupleType (TypeOf '((String '\"2022-01-01\"))) (TypeOf (AsStruct)) (TupleType (DataType 'String) (TupleType (DataType 'Int32) (DataType 'Float)))) '\"foo\" '((Int32 '\"32\") (String '\"no\") (AsStruct '('\"SomeFloat\" (Double '\"1e-9\")))))";
  1734. UNIT_ASSERT(programm.find(expected) != TString::npos);
  1735. }
  1736. Y_UNIT_TEST(UdfSyntaxSugarOptional) {
  1737. auto req = "SELECT Udf(DateTime::FromString, String?, Int32??, Tuple<Int32, Float>, \"foo\" as TypeConfig, Void() As RunConfig)(\"2022-01-01\");";
  1738. auto res = SqlToYql(req);
  1739. UNIT_ASSERT(res.Root);
  1740. const auto programm = GetPrettyPrint(res);
  1741. auto expected = "(SqlCall '\"DateTime.FromString\" '((PositionalArgs (String '\"2022-01-01\")) (AsStruct)) (TupleType (TypeOf '((String '\"2022-01-01\"))) (TypeOf (AsStruct)) (TupleType (OptionalType (DataType 'String)) (OptionalType (OptionalType (DataType 'Int32))) (TupleType (DataType 'Int32) (DataType 'Float)))) '\"foo\" (Void))";
  1742. UNIT_ASSERT(programm.find(expected) != TString::npos);
  1743. }
  1744. Y_UNIT_TEST(CompactionPolicyParseCorrect) {
  1745. NYql::TAstParseResult res = SqlToYql(
  1746. R"( USE plato;
  1747. CREATE TABLE tableName (Key Uint32, Value String, PRIMARY KEY (Key))
  1748. WITH ( COMPACTION_POLICY = "SomeCompactionPreset" );)"
  1749. );
  1750. UNIT_ASSERT(res.Root);
  1751. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  1752. if (word == "Write") {
  1753. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("compactionPolicy"));
  1754. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("SomeCompactionPreset"));
  1755. }
  1756. };
  1757. TWordCountHive elementStat = { {TString("Write"), 0} };
  1758. VerifyProgram(res, elementStat, verifyLine);
  1759. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  1760. }
  1761. Y_UNIT_TEST(AutoPartitioningBySizeParseCorrect) {
  1762. NYql::TAstParseResult res = SqlToYql(
  1763. R"( USE plato;
  1764. CREATE TABLE tableName (Key Uint32, Value String, PRIMARY KEY (Key))
  1765. WITH ( AUTO_PARTITIONING_BY_SIZE = ENABLED );)"
  1766. );
  1767. UNIT_ASSERT(res.Root);
  1768. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  1769. if (word == "Write") {
  1770. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("autoPartitioningBySize"));
  1771. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("ENABLED"));
  1772. }
  1773. };
  1774. TWordCountHive elementStat = { {TString("Write"), 0} };
  1775. VerifyProgram(res, elementStat, verifyLine);
  1776. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  1777. }
  1778. Y_UNIT_TEST(UniformPartitionsParseCorrect) {
  1779. NYql::TAstParseResult res = SqlToYql(
  1780. R"( USE plato;
  1781. CREATE TABLE tableName (Key Uint32, Value String, PRIMARY KEY (Key))
  1782. WITH ( UNIFORM_PARTITIONS = 16 );)"
  1783. );
  1784. UNIT_ASSERT(res.Root);
  1785. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  1786. if (word == "Write") {
  1787. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("uniformPartitions"));
  1788. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("16"));
  1789. }
  1790. };
  1791. TWordCountHive elementStat = { {TString("Write"), 0} };
  1792. VerifyProgram(res, elementStat, verifyLine);
  1793. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  1794. }
  1795. Y_UNIT_TEST(DateTimeTtlParseCorrect) {
  1796. NYql::TAstParseResult res = SqlToYql(
  1797. R"( USE plato;
  1798. CREATE TABLE tableName (Key Uint32, CreatedAt Timestamp, PRIMARY KEY (Key))
  1799. WITH (TTL = Interval("P1D") On CreatedAt);)"
  1800. );
  1801. UNIT_ASSERT(res.Root);
  1802. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  1803. if (word == "Write") {
  1804. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("setTtlSettings"));
  1805. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("tiers"));
  1806. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("evictionDelay"));
  1807. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("86400000"));
  1808. }
  1809. };
  1810. TWordCountHive elementStat = { {TString("Write"), 0} };
  1811. VerifyProgram(res, elementStat, verifyLine);
  1812. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  1813. }
  1814. Y_UNIT_TEST(IntTtlParseCorrect) {
  1815. NYql::TAstParseResult res = SqlToYql(
  1816. R"( USE plato;
  1817. CREATE TABLE tableName (Key Uint32, CreatedAt Uint32, PRIMARY KEY (Key))
  1818. WITH (TTL = Interval("P1D") On CreatedAt AS SECONDS);)"
  1819. );
  1820. UNIT_ASSERT(res.Root);
  1821. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  1822. if (word == "Write") {
  1823. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("setTtlSettings"));
  1824. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("tiers"));
  1825. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("evictionDelay"));
  1826. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("86400000"));
  1827. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("columnUnit"));
  1828. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("seconds"));
  1829. }
  1830. };
  1831. TWordCountHive elementStat = { {TString("Write"), 0} };
  1832. VerifyProgram(res, elementStat, verifyLine);
  1833. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  1834. }
  1835. Y_UNIT_TEST(TtlTieringParseCorrect) {
  1836. NYql::TAstParseResult res = SqlToYql(
  1837. R"( USE plato;
  1838. CREATE TABLE tableName (Key Uint32, CreatedAt Uint32, PRIMARY KEY (Key))
  1839. WITH (TTL =
  1840. Interval("P1D") TO EXTERNAL DATA SOURCE Tier1,
  1841. Interval("P2D") TO EXTERNAL DATA SOURCE Tier2,
  1842. Interval("P30D") DELETE
  1843. ON CreatedAt AS SECONDS);)"
  1844. );
  1845. UNIT_ASSERT(res.Root);
  1846. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  1847. if (word == "Write") {
  1848. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("setTtlSettings"));
  1849. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("tiers"));
  1850. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("evictionDelay"));
  1851. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("storageName"));
  1852. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("Tier1"));
  1853. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("Tier2"));
  1854. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("86400000"));
  1855. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("172800000"));
  1856. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("2592000000"));
  1857. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("columnUnit"));
  1858. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("seconds"));
  1859. }
  1860. };
  1861. TWordCountHive elementStat = { {TString("Write"), 0} };
  1862. VerifyProgram(res, elementStat, verifyLine);
  1863. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  1864. }
  1865. Y_UNIT_TEST(TtlTieringWithOtherActionsParseCorrect) {
  1866. NYql::TAstParseResult res = SqlToYql(
  1867. R"( USE plato;
  1868. ALTER TABLE tableName
  1869. ADD FAMILY cold (DATA = "rot"),
  1870. SET TTL
  1871. Interval("P1D") TO EXTERNAL DATA SOURCE Tier1,
  1872. Interval("P2D") TO EXTERNAL DATA SOURCE Tier2,
  1873. Interval("P30D") DELETE
  1874. ON CreatedAt,
  1875. ALTER COLUMN payload_v2 SET FAMILY cold,
  1876. ALTER FAMILY default SET DATA "ssd"
  1877. ;)"
  1878. );
  1879. UNIT_ASSERT(res.Root);
  1880. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  1881. if (word == "Write") {
  1882. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("addColumnFamilies"));
  1883. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("cold"));
  1884. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("alterColumnFamilies"));
  1885. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("default"));
  1886. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("setTtlSettings"));
  1887. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("tiers"));
  1888. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("evictionDelay"));
  1889. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("storageName"));
  1890. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("Tier1"));
  1891. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("Tier2"));
  1892. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("86400000"));
  1893. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("172800000"));
  1894. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("2592000000"));
  1895. }
  1896. };
  1897. TWordCountHive elementStat = { {TString("Write"), 0} };
  1898. VerifyProgram(res, elementStat, verifyLine);
  1899. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  1900. }
  1901. Y_UNIT_TEST(TieringParseCorrect) {
  1902. NYql::TAstParseResult res = SqlToYql(
  1903. R"( USE plato;
  1904. CREATE TABLE tableName (Key Uint32, Value String, PRIMARY KEY (Key))
  1905. WITH ( TIERING = 'my_tiering' );)"
  1906. );
  1907. UNIT_ASSERT(res.Root);
  1908. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  1909. if (word == "Write") {
  1910. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("tiering"));
  1911. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("my_tiering"));
  1912. }
  1913. };
  1914. TWordCountHive elementStat = { {TString("Write"), 0} };
  1915. VerifyProgram(res, elementStat, verifyLine);
  1916. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  1917. }
  1918. Y_UNIT_TEST(StoreExternalBlobsParseCorrect) {
  1919. NYql::TAstParseResult res = SqlToYql(
  1920. R"( USE plato;
  1921. CREATE TABLE tableName (Key Uint32, Value String, PRIMARY KEY (Key))
  1922. WITH ( STORE_EXTERNAL_BLOBS = ENABLED );)"
  1923. );
  1924. UNIT_ASSERT(res.Root);
  1925. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  1926. if (word == "Write") {
  1927. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("storeExternalBlobs"));
  1928. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("ENABLED"));
  1929. }
  1930. };
  1931. TWordCountHive elementStat = { {TString("Write"), 0} };
  1932. VerifyProgram(res, elementStat, verifyLine);
  1933. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  1934. }
  1935. Y_UNIT_TEST(DefaultValueColumn2) {
  1936. auto res = SqlToYql(R"( use plato;
  1937. $lambda = () -> {
  1938. RETURN CAST(RandomUuid(2) as String)
  1939. };
  1940. CREATE TABLE tableName (
  1941. Key Uint32 DEFAULT RandomNumber(1),
  1942. Value String DEFAULT $lambda,
  1943. PRIMARY KEY (Key)
  1944. );
  1945. )");
  1946. UNIT_ASSERT_C(res.Root, Err2Str(res));
  1947. const auto program = GetPrettyPrint(res);
  1948. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, program.find("RandomNumber"));
  1949. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, program.find("RandomUuid"));
  1950. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, program.find("columnConstrains"));
  1951. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, program.find("columnConstrains"));
  1952. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, program.find("Write"));
  1953. #if 0
  1954. Cerr << program << Endl;
  1955. #endif
  1956. TWordCountHive elementStat = { {TString("Write"), 0} };
  1957. VerifyProgram(res, elementStat);
  1958. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  1959. }
  1960. Y_UNIT_TEST(DefaultValueColumn3) {
  1961. auto res = SqlToYql(R"( use plato;
  1962. CREATE TABLE tableName (
  1963. database_id Utf8,
  1964. cloud_id Utf8,
  1965. global_id Utf8 DEFAULT database_id || "=====",
  1966. PRIMARY KEY (database_id)
  1967. );
  1968. )");
  1969. UNIT_ASSERT_VALUES_EQUAL(Err2Str(res), "<main>:6:40: Error: Column reference \"database_id\" is not allowed in current scope\n");
  1970. UNIT_ASSERT(!res.Root);
  1971. }
  1972. Y_UNIT_TEST(DefaultValueColumn) {
  1973. auto res = SqlToYql(R"( use plato;
  1974. CREATE TABLE tableName (
  1975. Key Uint32 FAMILY cold DEFAULT 5,
  1976. Value String FAMILY default DEFAULT "empty",
  1977. PRIMARY KEY (Key),
  1978. FAMILY default (
  1979. DATA = "test",
  1980. COMPRESSION = "lz4"
  1981. ),
  1982. FAMILY cold (
  1983. DATA = "test",
  1984. COMPRESSION = "off"
  1985. )
  1986. );
  1987. )");
  1988. UNIT_ASSERT_C(res.Root, Err2Str(res));
  1989. #if 0
  1990. const auto program = GetPrettyPrint(res);
  1991. Cerr << program << Endl;
  1992. #endif
  1993. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  1994. if (word == "Write") {
  1995. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("default"));
  1996. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("columnConstrains"));
  1997. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("columnFamilies"));
  1998. }
  1999. };
  2000. TWordCountHive elementStat = { {TString("Write"), 0} };
  2001. VerifyProgram(res, elementStat, verifyLine);
  2002. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  2003. }
  2004. Y_UNIT_TEST(ChangefeedParseCorrect) {
  2005. auto res = SqlToYql(R"( USE plato;
  2006. CREATE TABLE tableName (
  2007. Key Uint32, PRIMARY KEY (Key),
  2008. CHANGEFEED feedName WITH (
  2009. MODE = 'KEYS_ONLY',
  2010. FORMAT = 'json',
  2011. INITIAL_SCAN = TRUE,
  2012. VIRTUAL_TIMESTAMPS = FALSE,
  2013. BARRIERS_INTERVAL = Interval("PT1S"),
  2014. RETENTION_PERIOD = Interval("P1D"),
  2015. TOPIC_MIN_ACTIVE_PARTITIONS = 10,
  2016. AWS_REGION = 'aws:region'
  2017. )
  2018. );
  2019. )");
  2020. UNIT_ASSERT_C(res.Root, Err2Str(res));
  2021. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  2022. if (word == "Write") {
  2023. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("changefeed"));
  2024. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("mode"));
  2025. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("KEYS_ONLY"));
  2026. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("format"));
  2027. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("json"));
  2028. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("initial_scan"));
  2029. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("true"));
  2030. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("virtual_timestamps"));
  2031. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("false"));
  2032. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("barriers_interval"));
  2033. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("retention_period"));
  2034. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("topic_min_active_partitions"));
  2035. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("aws_region"));
  2036. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("aws:region"));
  2037. }
  2038. };
  2039. TWordCountHive elementStat = { {TString("Write"), 0} };
  2040. VerifyProgram(res, elementStat, verifyLine);
  2041. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  2042. }
  2043. Y_UNIT_TEST(CloneForAsTableWorksWithCube) {
  2044. UNIT_ASSERT(SqlToYql("SELECT * FROM AS_TABLE([<|k1:1, k2:1|>]) GROUP BY CUBE(k1, k2);").IsOk());
  2045. }
  2046. Y_UNIT_TEST(WindowPartitionByColumnProperlyEscaped) {
  2047. NYql::TAstParseResult res = SqlToYql("SELECT SUM(key) OVER w FROM plato.Input WINDOW w AS (PARTITION BY `column with space`);");
  2048. UNIT_ASSERT(res.Root);
  2049. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  2050. if (word == "CalcOverWindow") {
  2051. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("\"column with space\""));
  2052. }
  2053. };
  2054. TWordCountHive elementStat = { {TString("CalcOverWindow"), 0} };
  2055. VerifyProgram(res, elementStat, verifyLine);
  2056. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["CalcOverWindow"]);
  2057. }
  2058. Y_UNIT_TEST(WindowPartitionByExpressionWithoutAliasesAreAllowed) {
  2059. NYql::TAstParseResult res = SqlToYql("SELECT SUM(key) OVER w FROM plato.Input as i WINDOW w AS (PARTITION BY ii.subkey);");
  2060. UNIT_ASSERT(res.Root);
  2061. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  2062. if (word == "AddMember") {
  2063. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("AddMember row 'group_w_0 (SqlAccess 'struct (Member row '\"ii\")"));
  2064. }
  2065. if (word == "CalcOverWindow") {
  2066. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("CalcOverWindow core '('\"group_w_0\")"));
  2067. }
  2068. };
  2069. TWordCountHive elementStat = { {TString("CalcOverWindow"), 0}, {TString("AddMember"), 0} };
  2070. VerifyProgram(res, elementStat, verifyLine);
  2071. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["CalcOverWindow"]);
  2072. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["AddMember"]);
  2073. }
  2074. Y_UNIT_TEST(PqReadByAfterUse) {
  2075. ExpectFailWithError("use plato; pragma PqReadBy='plato2';",
  2076. "<main>:1:28: Error: Cluster in PqReadPqBy pragma differs from cluster specified in USE statement: plato2 != plato\n");
  2077. UNIT_ASSERT(SqlToYql("pragma PqReadBy='plato2';").IsOk());
  2078. UNIT_ASSERT(SqlToYql("pragma PqReadBy='plato2'; use plato;").IsOk());
  2079. UNIT_ASSERT(SqlToYql("$x='plato'; use rtmr:$x; pragma PqReadBy='plato2';").IsOk());
  2080. UNIT_ASSERT(SqlToYql("use plato; pragma PqReadBy='dq';").IsOk());
  2081. }
  2082. Y_UNIT_TEST(MrObject) {
  2083. NYql::TAstParseResult res = SqlToYql(
  2084. "declare $path as String;\n"
  2085. "select * from plato.object($path, `format`, \"comp\" || \"ression\" as compression, 1 as bar) with schema (Int32 as y, String as x)"
  2086. );
  2087. UNIT_ASSERT(res.Root);
  2088. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  2089. if (word == "MrObject") {
  2090. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos,
  2091. line.find(R"__((MrObject (EvaluateAtom "$path") '"format" '('('"compression" (Concat (String '"comp") (String '"ression"))) '('"bar" (Int32 '"1")))))__"));
  2092. } else if (word == "userschema") {
  2093. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos,
  2094. line.find(R"__('('('"userschema" (StructType '('"y" (DataType 'Int32)) '('"x" (DataType 'String))) '('"y" '"x"))))__"));
  2095. }
  2096. };
  2097. TWordCountHive elementStat = {{TString("MrObject"), 0}, {TString("userschema"), 0}};
  2098. VerifyProgram(res, elementStat, verifyLine);
  2099. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["MrObject"]);
  2100. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["userschema"]);
  2101. }
  2102. Y_UNIT_TEST(TableBindings) {
  2103. NSQLTranslation::TTranslationSettings settings = GetSettingsWithS3Binding("foo");
  2104. NYql::TAstParseResult res = SqlToYqlWithSettings(
  2105. "select * from bindings.foo",
  2106. settings
  2107. );
  2108. UNIT_ASSERT(res.Root);
  2109. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  2110. if (word == "MrObject") {
  2111. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos,
  2112. line.find(R"__((MrTableConcat (Key '('table (String '"path")))) (Void) '('('"bar" '"1") '('"compression" '"ccompression") '('"format" '"format") '('"partitionedby" '"key" '"subkey") '('"userschema" (SqlTypeFromYson)__"));
  2113. }
  2114. };
  2115. TWordCountHive elementStat = {{TString("MrTableConcat"), 0}};
  2116. VerifyProgram(res, elementStat, verifyLine);
  2117. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["MrTableConcat"]);
  2118. settings.DefaultCluster = "plato";
  2119. settings.BindingsMode = NSQLTranslation::EBindingsMode::DISABLED;
  2120. res = SqlToYqlWithSettings(
  2121. "select * from bindings.foo",
  2122. settings
  2123. );
  2124. UNIT_ASSERT_VALUES_EQUAL(Err2Str(res), "<main>:1:15: Error: Please remove 'bindings.' from your query, the support for this syntax has ended, code: 4601\n");
  2125. UNIT_ASSERT(!res.Root);
  2126. settings.BindingsMode = NSQLTranslation::EBindingsMode::DROP;
  2127. res = SqlToYqlWithSettings(
  2128. "select * from bindings.foo",
  2129. settings
  2130. );
  2131. UNIT_ASSERT(res.Root);
  2132. TVerifyLineFunc verifyLine2 = [](const TString& word, const TString& line) {
  2133. if (word == "MrTableConcat") {
  2134. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos,
  2135. line.find(R"__((MrTableConcat (Key '('table (String '"foo")))) (Void) '())))__"));
  2136. }
  2137. };
  2138. TWordCountHive elementStat2 = {{TString("MrTableConcat"), 0}};
  2139. VerifyProgram(res, elementStat2, verifyLine2);
  2140. UNIT_ASSERT_VALUES_EQUAL(1, elementStat2["MrTableConcat"]);
  2141. settings.BindingsMode = NSQLTranslation::EBindingsMode::DROP_WITH_WARNING;
  2142. res = SqlToYqlWithSettings(
  2143. "select * from bindings.foo",
  2144. settings
  2145. );
  2146. UNIT_ASSERT_VALUES_EQUAL(Err2Str(res), "<main>:1:15: Warning: Please remove 'bindings.' from your query, the support for this syntax will be dropped soon, code: 4538\n");
  2147. UNIT_ASSERT(res.Root);
  2148. TWordCountHive elementStat3 = {{TString("MrTableConcat"), 0}};
  2149. VerifyProgram(res, elementStat3, verifyLine2);
  2150. UNIT_ASSERT_VALUES_EQUAL(1, elementStat3["MrTableConcat"]);
  2151. }
  2152. Y_UNIT_TEST(TableBindingsWithInsert) {
  2153. NSQLTranslation::TTranslationSettings settings = GetSettingsWithS3Binding("foo");
  2154. NYql::TAstParseResult res = SqlToYqlWithSettings(
  2155. "insert into bindings.foo with truncate (x, y) values (1, 2);",
  2156. settings
  2157. );
  2158. UNIT_ASSERT(res.Root);
  2159. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  2160. if (word == "Write!") {
  2161. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos,
  2162. line.find(R"__((Write! world sink (Key '('table (String '"path"))) values '('('"bar" '"1") '('"compression" '"ccompression") '('"format" '"format") '('"partitionedby" '"key" '"subkey") '('"userschema" (SqlTypeFromYson)__"));
  2163. }
  2164. };
  2165. TWordCountHive elementStat = {{TString("Write!"), 0}};
  2166. VerifyProgram(res, elementStat, verifyLine);
  2167. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write!"]);
  2168. settings.DefaultCluster = "plato";
  2169. settings.BindingsMode = NSQLTranslation::EBindingsMode::DISABLED;
  2170. res = SqlToYqlWithSettings(
  2171. "insert into bindings.foo with truncate (x, y) values (1, 2);",
  2172. settings
  2173. );
  2174. UNIT_ASSERT_VALUES_EQUAL(Err2Str(res), "<main>:1:13: Error: Please remove 'bindings.' from your query, the support for this syntax has ended, code: 4601\n");
  2175. UNIT_ASSERT(!res.Root);
  2176. settings.BindingsMode = NSQLTranslation::EBindingsMode::DROP;
  2177. res = SqlToYqlWithSettings(
  2178. "insert into bindings.foo with truncate (x, y) values (1, 2);",
  2179. settings
  2180. );
  2181. UNIT_ASSERT_VALUES_EQUAL(Err2Str(res), "");
  2182. UNIT_ASSERT(res.Root);
  2183. TVerifyLineFunc verifyLine2 = [](const TString& word, const TString& line) {
  2184. if (word == "Write!") {
  2185. //UNIT_ASSERT_VALUES_EQUAL(line, "");
  2186. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos,
  2187. line.find(R"__((Write! world sink (Key '('table (String '"foo"))) values '('('mode 'renew)))__"));
  2188. }
  2189. };
  2190. TWordCountHive elementStat2 = {{TString("Write!"), 0}};
  2191. VerifyProgram(res, elementStat2, verifyLine2);
  2192. UNIT_ASSERT_VALUES_EQUAL(1, elementStat2["Write!"]);
  2193. settings.BindingsMode = NSQLTranslation::EBindingsMode::DROP_WITH_WARNING;
  2194. res = SqlToYqlWithSettings(
  2195. "insert into bindings.foo with truncate (x, y) values (1, 2);",
  2196. settings
  2197. );
  2198. UNIT_ASSERT_VALUES_EQUAL(Err2Str(res), "<main>:1:13: Warning: Please remove 'bindings.' from your query, the support for this syntax will be dropped soon, code: 4538\n");
  2199. UNIT_ASSERT(res.Root);
  2200. TWordCountHive elementStat3 = {{TString("Write!"), 0}};
  2201. VerifyProgram(res, elementStat3, verifyLine2);
  2202. UNIT_ASSERT_VALUES_EQUAL(1, elementStat3["Write!"]);
  2203. }
  2204. Y_UNIT_TEST(TrailingCommaInWithout) {
  2205. UNIT_ASSERT(SqlToYql("SELECT * WITHOUT stream, FROM plato.Input").IsOk());
  2206. UNIT_ASSERT(SqlToYql("SELECT a.* WITHOUT a.intersect, FROM plato.Input AS a").IsOk());
  2207. UNIT_ASSERT(SqlToYql("SELECT a.* WITHOUT col1, col2, a.col3, FROM plato.Input AS a").IsOk());
  2208. }
  2209. Y_UNIT_TEST(NoStackOverflowOnBigCaseStatement) {
  2210. TStringBuilder req;
  2211. req << "select case 1 + 123";
  2212. for (size_t i = 0; i < 20000; ++i) {
  2213. req << " when " << i << " then " << i + 1;
  2214. }
  2215. req << " else 100500 end;";
  2216. UNIT_ASSERT(SqlToYql(req).IsOk());
  2217. }
  2218. Y_UNIT_TEST(CollectPreaggregatedInListLiteral) {
  2219. UNIT_ASSERT(SqlToYql("SELECT [COUNT(DISTINCT a+b)] FROM plato.Input").IsOk());
  2220. }
  2221. Y_UNIT_TEST(SmartParenInGroupByClause) {
  2222. UNIT_ASSERT(SqlToYql("SELECT * FROM plato.Input GROUP BY (k, v)").IsOk());
  2223. }
  2224. Y_UNIT_TEST(AlterTableRenameToIsCorrect) {
  2225. UNIT_ASSERT(SqlToYql("USE plato; ALTER TABLE table RENAME TO moved").IsOk());
  2226. }
  2227. Y_UNIT_TEST(AlterTableAddDropColumnIsCorrect) {
  2228. UNIT_ASSERT(SqlToYql("USE plato; ALTER TABLE table ADD COLUMN addc uint64, DROP COLUMN dropc, ADD addagain uint64").IsOk());
  2229. }
  2230. Y_UNIT_TEST(AlterTableSetTTLIsCorrect) {
  2231. UNIT_ASSERT(SqlToYql("USE plato; ALTER TABLE table SET (TTL = Interval(\"PT3H\") ON column)").IsOk());
  2232. UNIT_ASSERT(SqlToYql("USE plato; ALTER TABLE table SET (TTL = Interval(\"PT3H\") ON column AS SECONDS)").IsOk());
  2233. }
  2234. Y_UNIT_TEST(AlterTableSetTieringIsCorrect) {
  2235. UNIT_ASSERT(SqlToYql("USE plato; ALTER TABLE table SET (TIERING = 'my_tiering')").IsOk());
  2236. }
  2237. Y_UNIT_TEST(AlterTableAddChangefeedIsCorrect) {
  2238. UNIT_ASSERT(SqlToYql("USE plato; ALTER TABLE table ADD CHANGEFEED feed WITH (MODE = 'UPDATES', FORMAT = 'json')").IsOk());
  2239. }
  2240. Y_UNIT_TEST(AlterTableAlterChangefeedIsCorrect) {
  2241. UNIT_ASSERT(SqlToYql("USE plato; ALTER TABLE table ALTER CHANGEFEED feed DISABLE").IsOk());
  2242. ExpectFailWithError("USE plato; ALTER TABLE table ALTER CHANGEFEED feed SET (FORMAT = 'proto');",
  2243. "<main>:1:57: Error: FORMAT alter is not supported\n");
  2244. }
  2245. Y_UNIT_TEST(AlterTableDropChangefeedIsCorrect) {
  2246. UNIT_ASSERT(SqlToYql("USE plato; ALTER TABLE table DROP CHANGEFEED feed").IsOk());
  2247. }
  2248. Y_UNIT_TEST(AlterTableSetPartitioningIsCorrect) {
  2249. UNIT_ASSERT(SqlToYql("USE plato; ALTER TABLE table SET (AUTO_PARTITIONING_BY_SIZE = DISABLED)").IsOk());
  2250. }
  2251. Y_UNIT_TEST(AlterTableAddIndexWithIsNotSupported) {
  2252. ExpectFailWithFuzzyError("USE plato; ALTER TABLE table ADD INDEX idx GLOBAL ON (col) WITH (a=b)",
  2253. "<main>:1:40: Error: with: alternative is not implemented yet: \\d+:\\d+: global_index\\n");
  2254. }
  2255. Y_UNIT_TEST(AlterTableAddIndexLocalIsNotSupported) {
  2256. ExpectFailWithFuzzyError("USE plato; ALTER TABLE table ADD INDEX idx LOCAL ON (col)",
  2257. "<main>:1:40: Error: local: alternative is not implemented yet: \\d+:\\d+: local_index\\n");
  2258. }
  2259. Y_UNIT_TEST(CreateTableAddIndexVector) {
  2260. const auto result = SqlToYql(R"(USE plato;
  2261. CREATE TABLE table (
  2262. pk INT32 NOT NULL,
  2263. col String,
  2264. INDEX idx GLOBAL USING vector_kmeans_tree
  2265. ON (col) COVER (col)
  2266. WITH (distance=cosine, vector_type=float, vector_dimension=1024,),
  2267. PRIMARY KEY (pk))
  2268. )");
  2269. UNIT_ASSERT_C(result.IsOk(), result.Issues.ToString());
  2270. }
  2271. Y_UNIT_TEST(AlterTableAddIndexVector) {
  2272. const auto result = SqlToYql(R"(USE plato;
  2273. ALTER TABLE table ADD INDEX idx
  2274. GLOBAL USING vector_kmeans_tree
  2275. ON (col) COVER (col)
  2276. WITH (distance=cosine, vector_type="float", vector_dimension=1024)
  2277. )");
  2278. UNIT_ASSERT_C(result.IsOk(), result.Issues.ToString());
  2279. }
  2280. Y_UNIT_TEST(AlterTableAddIndexUnknownSubtype) {
  2281. ExpectFailWithError("USE plato; ALTER TABLE table ADD INDEX idx GLOBAL USING unknown ON (col)",
  2282. "<main>:1:57: Error: UNKNOWN index subtype is not supported\n");
  2283. }
  2284. Y_UNIT_TEST(AlterTableAddIndexMissedParameter) {
  2285. ExpectFailWithError(R"(USE plato;
  2286. ALTER TABLE table ADD INDEX idx
  2287. GLOBAL USING vector_kmeans_tree
  2288. ON (col)
  2289. WITH (distance=cosine, vector_type=float)
  2290. )",
  2291. "<main>:5:52: Error: vector_dimension should be set\n");
  2292. }
  2293. Y_UNIT_TEST(AlterTableAlterIndexSetPartitioningIsCorrect) {
  2294. const auto result = SqlToYql("USE plato; ALTER TABLE table ALTER INDEX index SET AUTO_PARTITIONING_MIN_PARTITIONS_COUNT 10");
  2295. UNIT_ASSERT_C(result.IsOk(), result.Issues.ToString());
  2296. }
  2297. Y_UNIT_TEST(AlterTableAlterIndexSetMultiplePartitioningSettings) {
  2298. const auto result = SqlToYql("USE plato; ALTER TABLE table ALTER INDEX index SET "
  2299. "(AUTO_PARTITIONING_BY_LOAD = ENABLED, AUTO_PARTITIONING_MIN_PARTITIONS_COUNT = 10)"
  2300. );
  2301. UNIT_ASSERT_C(result.IsOk(), result.Issues.ToString());
  2302. }
  2303. Y_UNIT_TEST(AlterTableAlterIndexResetPartitioningIsNotSupported) {
  2304. ExpectFailWithError("USE plato; ALTER TABLE table ALTER INDEX index RESET (AUTO_PARTITIONING_MIN_PARTITIONS_COUNT)",
  2305. "<main>:1:55: Error: AUTO_PARTITIONING_MIN_PARTITIONS_COUNT reset is not supported\n"
  2306. );
  2307. }
  2308. Y_UNIT_TEST(AlterTableAlterColumnDropNotNullAstCorrect) {
  2309. auto reqSetNull = SqlToYql(R"(
  2310. USE plato;
  2311. CREATE TABLE tableName (
  2312. id Uint32,
  2313. val Uint32 NOT NULL,
  2314. PRIMARY KEY (id)
  2315. );
  2316. COMMIT;
  2317. ALTER TABLE tableName ALTER COLUMN val DROP NOT NULL;
  2318. )");
  2319. UNIT_ASSERT(reqSetNull.IsOk());
  2320. UNIT_ASSERT(reqSetNull.Root);
  2321. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  2322. Y_UNUSED(word);
  2323. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find(
  2324. R"(let world (Write! world sink (Key '('tablescheme (String '"tableName"))) (Void) '('('mode 'alter) '('actions '('('alterColumns '('('"val" '('changeColumnConstraints '('('drop_not_null)))))))))))"
  2325. ));
  2326. };
  2327. TWordCountHive elementStat({TString("\'mode \'alter")});
  2328. VerifyProgram(reqSetNull, elementStat, verifyLine);
  2329. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["\'mode \'alter"]);
  2330. }
  2331. Y_UNIT_TEST(AlterSequence) {
  2332. UNIT_ASSERT(SqlToYql(R"(
  2333. USE plato;
  2334. ALTER SEQUENCE sequence START WITH 10 INCREMENT 2 RESTART WITH 5;
  2335. )").IsOk());
  2336. UNIT_ASSERT(SqlToYql(R"(
  2337. USE plato;
  2338. ALTER SEQUENCE sequence INCREMENT 2;
  2339. )").IsOk());
  2340. UNIT_ASSERT(SqlToYql(R"(
  2341. USE plato;
  2342. ALTER SEQUENCE sequence INCREMENT 2 START 1000;
  2343. )").IsOk());
  2344. UNIT_ASSERT(SqlToYql(R"(
  2345. USE plato;
  2346. ALTER SEQUENCE sequence RESTART START 1000;
  2347. )").IsOk());
  2348. UNIT_ASSERT(SqlToYql(R"(
  2349. USE plato;
  2350. ALTER SEQUENCE IF EXISTS sequence INCREMENT 1000 START 100 RESTART;
  2351. )").IsOk());
  2352. UNIT_ASSERT(SqlToYql(R"(
  2353. USE plato;
  2354. ALTER SEQUENCE IF EXISTS sequence RESTART 1000 START WITH 100 INCREMENT BY 7;
  2355. )").IsOk());
  2356. }
  2357. Y_UNIT_TEST(AlterSequenceIncorrect) {
  2358. {
  2359. NYql::TAstParseResult res = SqlToYql("USE plato; ALTER SEQUENCE sequence START WITH 10 INCREMENT 2 RESTART WITH 5 RESTART;");
  2360. UNIT_ASSERT(!res.Root);
  2361. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:75: Error: Restart value defined more than once\n");
  2362. }
  2363. {
  2364. NYql::TAstParseResult res = SqlToYql("USE plato; ALTER SEQUENCE sequence START WITH 10 INCREMENT 2 START 100 RESTART WITH 5;");
  2365. UNIT_ASSERT(!res.Root);
  2366. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:60: Error: Start value defined more than once\n");
  2367. }
  2368. {
  2369. NYql::TAstParseResult res = SqlToYql("USE plato; ALTER SEQUENCE sequence INCREMENT BY 7 START WITH 10 INCREMENT 2 RESTART WITH 5 RESTART;");
  2370. UNIT_ASSERT(!res.Root);
  2371. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:62: Error: Increment defined more than once\n");
  2372. }
  2373. {
  2374. NYql::TAstParseResult res = SqlToYql("USE plato; ALTER SEQUENCE sequence RESTART WITH 100 START WITH 10 INCREMENT 2 RESTART WITH 5;");
  2375. UNIT_ASSERT(!res.Root);
  2376. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:77: Error: Restart value defined more than once\n");
  2377. }
  2378. {
  2379. NYql::TAstParseResult res = SqlToYql("USE plato; ALTER SEQUENCE sequence RESTART WITH 1234234543563435151456 START WITH 10 INCREMENT 2;");
  2380. UNIT_ASSERT(!res.Root);
  2381. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:49: Error: Failed to parse number from string: 1234234543563435151456, number limit overflow\n");
  2382. }
  2383. {
  2384. NYql::TAstParseResult res = SqlToYql("USE plato; ALTER SEQUENCE sequence RESTART WITH 1 START WITH 9223372036854775817 INCREMENT 4;");
  2385. UNIT_ASSERT(!res.Root);
  2386. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:12: Error: Start value: 9223372036854775817 cannot be greater than max value: 9223372036854775807\n");
  2387. }
  2388. {
  2389. NYql::TAstParseResult res = SqlToYql("USE plato; ALTER SEQUENCE sequence RESTART WITH 9223372036854775827 START WITH 5 INCREMENT 4;");
  2390. UNIT_ASSERT(!res.Root);
  2391. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:12: Error: Restart value: 9223372036854775827 cannot be greater than max value: 9223372036854775807\n");
  2392. }
  2393. {
  2394. NYql::TAstParseResult res = SqlToYql("USE plato; ALTER SEQUENCE sequence RESTART WITH 1 START WITH 4 INCREMENT 0;");
  2395. UNIT_ASSERT(!res.Root);
  2396. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:12: Error: Increment must not be zero\n");
  2397. }
  2398. {
  2399. NYql::TAstParseResult res = SqlToYql("USE plato; ALTER SEQUENCE sequence RESTART WITH 0 START WITH 4 INCREMENT 1;");
  2400. UNIT_ASSERT(!res.Root);
  2401. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:12: Error: Restart value: 0 cannot be less than min value: 1\n");
  2402. }
  2403. {
  2404. NYql::TAstParseResult res = SqlToYql("USE plato; ALTER SEQUENCE sequence RESTART WITH 1 START WITH 0 INCREMENT 1;");
  2405. UNIT_ASSERT(!res.Root);
  2406. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:12: Error: Start value: 0 cannot be less than min value: 1\n");
  2407. }
  2408. {
  2409. NYql::TAstParseResult res = SqlToYql("USE plato; ALTER SEQUENCE sequence RESTART WITH 1 START WITH 1 INCREMENT 9223372036854775837;");
  2410. UNIT_ASSERT(!res.Root);
  2411. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:12: Error: Increment: 9223372036854775837 cannot be greater than max value: 9223372036854775807\n");
  2412. }
  2413. }
  2414. Y_UNIT_TEST(AlterSequenceCorrect) {
  2415. {
  2416. NYql::TAstParseResult res = SqlToYql("USE plato; ALTER SEQUENCE sequence START WITH 10 INCREMENT 2 RESTART WITH 5;");
  2417. UNIT_ASSERT(res.Root);
  2418. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  2419. if (word == "Write") {
  2420. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("sequence"));
  2421. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("alter"));
  2422. UNIT_ASSERT_VALUES_EQUAL(TString::npos, line.find("alter_if_exists"));
  2423. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("start"));
  2424. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("increment"));
  2425. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("restart"));
  2426. }
  2427. };
  2428. TWordCountHive elementStat = { {TString("Write"), 0}};
  2429. VerifyProgram(res, elementStat, verifyLine);
  2430. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  2431. }
  2432. {
  2433. NYql::TAstParseResult res = SqlToYql("USE plato; ALTER SEQUENCE IF EXISTS sequence INCREMENT 2 RESTART;");
  2434. UNIT_ASSERT(res.Root);
  2435. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  2436. if (word == "Write") {
  2437. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("sequence"));
  2438. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("alter_if_exists"));
  2439. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("increment"));
  2440. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("restart"));
  2441. }
  2442. };
  2443. TWordCountHive elementStat = { {TString("Write"), 0}};
  2444. VerifyProgram(res, elementStat, verifyLine);
  2445. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  2446. }
  2447. {
  2448. NYql::TAstParseResult res = SqlToYql("USE plato; ALTER SEQUENCE IF EXISTS sequence START 10 INCREMENT BY 2;");
  2449. UNIT_ASSERT(res.Root);
  2450. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  2451. if (word == "Write") {
  2452. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("sequence"));
  2453. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("alter_if_exists"));
  2454. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("start"));
  2455. UNIT_ASSERT_VALUES_EQUAL(TString::npos, line.find("restart"));
  2456. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("increment"));
  2457. }
  2458. };
  2459. TWordCountHive elementStat = { {TString("Write"), 0}};
  2460. VerifyProgram(res, elementStat, verifyLine);
  2461. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  2462. }
  2463. }
  2464. Y_UNIT_TEST(ShowCreateTable) {
  2465. NYql::TAstParseResult res = SqlToYql(R"(
  2466. USE plato;
  2467. SHOW CREATE TABLE user;
  2468. )");
  2469. UNIT_ASSERT(res.Root);
  2470. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  2471. if (word == "Read") {
  2472. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("showCreateTable"));
  2473. }
  2474. };
  2475. TWordCountHive elementStat = {{TString("Read"), 0}, {TString("showCreateTable"), 0}};
  2476. VerifyProgram(res, elementStat, verifyLine);
  2477. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Read"]);
  2478. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["showCreateTable"]);
  2479. }
  2480. Y_UNIT_TEST(OptionalAliases) {
  2481. UNIT_ASSERT(SqlToYql("USE plato; SELECT foo FROM (SELECT key foo FROM Input);").IsOk());
  2482. UNIT_ASSERT(SqlToYql("USE plato; SELECT a.x FROM Input1 a JOIN Input2 b ON a.key = b.key;").IsOk());
  2483. UNIT_ASSERT(SqlToYql("USE plato; SELECT a.x FROM (VALUES (1,2), (3,4)) a(x,key) JOIN Input b ON a.key = b.key;").IsOk());
  2484. }
  2485. Y_UNIT_TEST(TableNameConstness) {
  2486. UNIT_ASSERT(SqlToYql("USE plato; $path = 'foo'; SELECT TableName($path), count(*) FROM Input;").IsOk());
  2487. UNIT_ASSERT(SqlToYql("$path = 'foo'; SELECT TableName($path, 'yt'), count(*) FROM plato.Input;").IsOk());
  2488. ExpectFailWithError("USE plato; SELECT TableName(), count(*) FROM plato.Input;",
  2489. "<main>:1:19: Error: Expression has to be an aggregation function or key column, because aggregation is used elsewhere in this subquery\n");
  2490. }
  2491. Y_UNIT_TEST(UseShouldWorkAsColumnName) {
  2492. UNIT_ASSERT(SqlToYql("select use from (select 1 as use);").IsOk());
  2493. }
  2494. Y_UNIT_TEST(TrueFalseWorkAfterDollar) {
  2495. UNIT_ASSERT(SqlToYql("$ true = false; SELECT $ true or false;").IsOk());
  2496. UNIT_ASSERT(SqlToYql("$False = 0; SELECT $False;").IsOk());
  2497. }
  2498. Y_UNIT_TEST(WithSchemaEquals) {
  2499. UNIT_ASSERT(SqlToYql("select * from plato.T with schema Struct<a:Int32, b:String>;").IsOk());
  2500. UNIT_ASSERT(SqlToYql("select * from plato.T with columns = Struct<a:Int32, b:String>;").IsOk());
  2501. }
  2502. Y_UNIT_TEST(WithNonStructSchemaS3) {
  2503. NSQLTranslation::TTranslationSettings settings;
  2504. settings.ClusterMapping["s3bucket"] = NYql::S3ProviderName;
  2505. UNIT_ASSERT(SqlToYql("select * from s3bucket.`foo` with schema (col1 Int32, String as col2, Int64 as col3);", settings).IsOk());
  2506. }
  2507. Y_UNIT_TEST(AllowNestedTuplesInGroupBy) {
  2508. NYql::TAstParseResult res = SqlToYql("select count(*) from plato.Input group by 1 + (x, y, z);");
  2509. UNIT_ASSERT(res.Root);
  2510. TVerifyLineFunc verifyLine = [&](const TString& word, const TString& line) {
  2511. Y_UNUSED(word);
  2512. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("(Aggregate core '('\"group0\")"));
  2513. };
  2514. TWordCountHive elementStat({"Aggregate"});
  2515. VerifyProgram(res, elementStat, verifyLine);
  2516. UNIT_ASSERT(elementStat["Aggregate"] == 1);
  2517. }
  2518. Y_UNIT_TEST(AllowGroupByWithParens) {
  2519. NYql::TAstParseResult res = SqlToYql("select count(*) from plato.Input group by (x, y as alias1, z);");
  2520. UNIT_ASSERT(res.Root);
  2521. TVerifyLineFunc verifyLine = [&](const TString& word, const TString& line) {
  2522. Y_UNUSED(word);
  2523. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("(Aggregate core '('\"x\" '\"alias1\" '\"z\")"));
  2524. };
  2525. TWordCountHive elementStat({"Aggregate"});
  2526. VerifyProgram(res, elementStat, verifyLine);
  2527. UNIT_ASSERT(elementStat["Aggregate"] == 1);
  2528. }
  2529. Y_UNIT_TEST(CreateAsyncReplicationParseCorrect) {
  2530. auto req = R"(
  2531. USE plato;
  2532. CREATE ASYNC REPLICATION MyReplication
  2533. FOR table1 AS table2, table3 AS table4
  2534. WITH (
  2535. CONNECTION_STRING = "grpc://localhost:2135/?database=/MyDatabase",
  2536. ENDPOINT = "localhost:2135",
  2537. DATABASE = "/MyDatabase"
  2538. );
  2539. )";
  2540. auto res = SqlToYql(req);
  2541. UNIT_ASSERT(res.Root);
  2542. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  2543. if (word == "Write") {
  2544. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("MyReplication"));
  2545. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("create"));
  2546. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("table1"));
  2547. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("table2"));
  2548. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("table3"));
  2549. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("table4"));
  2550. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("connection_string"));
  2551. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("grpc://localhost:2135/?database=/MyDatabase"));
  2552. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("endpoint"));
  2553. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("localhost:2135"));
  2554. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("database"));
  2555. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("/MyDatabase"));
  2556. }
  2557. };
  2558. TWordCountHive elementStat = { {TString("Write"), 0}};
  2559. VerifyProgram(res, elementStat, verifyLine);
  2560. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  2561. }
  2562. Y_UNIT_TEST(CreateAsyncReplicationUnsupportedSettings) {
  2563. auto reqTpl = R"(
  2564. USE plato;
  2565. CREATE ASYNC REPLICATION MyReplication
  2566. FOR table1 AS table2, table3 AS table4
  2567. WITH (
  2568. %s = "%s"
  2569. )
  2570. )";
  2571. auto settings = THashMap<TString, TString>{
  2572. {"STATE", "DONE"},
  2573. {"FAILOVER_MODE", "FORCE"},
  2574. };
  2575. for (const auto& [k, v] : settings) {
  2576. auto req = Sprintf(reqTpl, k.c_str(), v.c_str());
  2577. auto res = SqlToYql(req);
  2578. UNIT_ASSERT(!res.Root);
  2579. UNIT_ASSERT_NO_DIFF(Err2Str(res), Sprintf("<main>:6:%zu: Error: %s is not supported in CREATE\n", 20 + k.size(), k.c_str()));
  2580. }
  2581. }
  2582. Y_UNIT_TEST(AsyncReplicationInvalidCommitInterval) {
  2583. auto req = R"(
  2584. USE plato;
  2585. CREATE ASYNC REPLICATION MyReplication
  2586. FOR table1 AS table2, table3 AS table4
  2587. WITH (
  2588. COMMIT_INTERVAL = "FOO"
  2589. );
  2590. )";
  2591. auto res = SqlToYql(req);
  2592. UNIT_ASSERT(!res.Root);
  2593. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:6:35: Error: Literal of Interval type is expected for COMMIT_INTERVAL\n");
  2594. }
  2595. Y_UNIT_TEST(AlterAsyncReplicationParseCorrect) {
  2596. auto req = R"(
  2597. USE plato;
  2598. ALTER ASYNC REPLICATION MyReplication
  2599. SET (
  2600. STATE = "DONE",
  2601. FAILOVER_MODE = "FORCE"
  2602. );
  2603. )";
  2604. auto res = SqlToYql(req);
  2605. UNIT_ASSERT(res.Root);
  2606. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  2607. if (word == "Write") {
  2608. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("MyReplication"));
  2609. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("alter"));
  2610. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("state"));
  2611. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("DONE"));
  2612. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("failover_mode"));
  2613. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("FORCE"));
  2614. }
  2615. };
  2616. TWordCountHive elementStat = { {TString("Write"), 0}};
  2617. VerifyProgram(res, elementStat, verifyLine);
  2618. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  2619. }
  2620. Y_UNIT_TEST(AlterAsyncReplicationSettings) {
  2621. auto reqTpl = R"(
  2622. USE plato;
  2623. ALTER ASYNC REPLICATION MyReplication
  2624. SET (
  2625. %s = "%s"
  2626. )
  2627. )";
  2628. auto settings = THashMap<TString, TString>{
  2629. {"connection_string", "grpc://localhost:2135/?database=/MyDatabase"},
  2630. {"endpoint", "localhost:2135"},
  2631. {"database", "/MyDatabase"},
  2632. {"token", "foo"},
  2633. {"token_secret_name", "foo_secret_name"},
  2634. {"user", "user"},
  2635. {"password", "bar"},
  2636. {"password_secret_name", "bar_secret_name"},
  2637. };
  2638. for (const auto& [k, v] : settings) {
  2639. auto req = Sprintf(reqTpl, k.c_str(), v.c_str());
  2640. auto res = SqlToYql(req);
  2641. UNIT_ASSERT(res.Root);
  2642. TVerifyLineFunc verifyLine = [&k, &v](const TString& word, const TString& line) {
  2643. if (word == "Write") {
  2644. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("MyReplication"));
  2645. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("alter"));
  2646. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find(k));
  2647. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find(v));
  2648. }
  2649. };
  2650. TWordCountHive elementStat = { {TString("Write"), 0}};
  2651. VerifyProgram(res, elementStat, verifyLine);
  2652. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  2653. }
  2654. }
  2655. Y_UNIT_TEST(AlterAsyncReplicationUnsupportedSettings) {
  2656. {
  2657. auto req = R"(
  2658. USE plato;
  2659. ALTER ASYNC REPLICATION MyReplication SET (CONSISTENCY_LEVEL = "GLOBAL");
  2660. )";
  2661. auto res = SqlToYql(req);
  2662. UNIT_ASSERT(!res.Root);
  2663. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:3:80: Error: CONSISTENCY_LEVEL is not supported in ALTER\n");
  2664. }
  2665. {
  2666. auto req = R"(
  2667. USE plato;
  2668. ALTER ASYNC REPLICATION MyReplication SET (COMMIT_INTERVAL = Interval("PT10S"));
  2669. )";
  2670. auto res = SqlToYql(req);
  2671. UNIT_ASSERT(!res.Root);
  2672. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:3:87: Error: COMMIT_INTERVAL is not supported in ALTER\n");
  2673. }
  2674. }
  2675. Y_UNIT_TEST(AsyncReplicationInvalidSettings) {
  2676. auto req = R"(
  2677. USE plato;
  2678. ALTER ASYNC REPLICATION MyReplication SET (FOO = "BAR");
  2679. )";
  2680. auto res = SqlToYql(req);
  2681. UNIT_ASSERT(!res.Root);
  2682. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:3:62: Error: Unknown replication setting: FOO\n");
  2683. }
  2684. Y_UNIT_TEST(DropAsyncReplicationParseCorrect) {
  2685. auto req = R"(
  2686. USE plato;
  2687. DROP ASYNC REPLICATION MyReplication;
  2688. )";
  2689. auto res = SqlToYql(req);
  2690. UNIT_ASSERT(res.Root);
  2691. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  2692. if (word == "Write") {
  2693. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("MyReplication"));
  2694. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("drop"));
  2695. }
  2696. };
  2697. TWordCountHive elementStat = { {TString("Write"), 0}};
  2698. VerifyProgram(res, elementStat, verifyLine);
  2699. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  2700. }
  2701. Y_UNIT_TEST(DropAsyncReplicationCascade) {
  2702. auto req = R"(
  2703. USE plato;
  2704. DROP ASYNC REPLICATION MyReplication CASCADE;
  2705. )";
  2706. auto res = SqlToYql(req);
  2707. UNIT_ASSERT(res.Root);
  2708. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  2709. if (word == "Write") {
  2710. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("dropCascade"));
  2711. }
  2712. };
  2713. TWordCountHive elementStat = { {TString("Write"), 0}};
  2714. VerifyProgram(res, elementStat, verifyLine);
  2715. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  2716. }
  2717. Y_UNIT_TEST(PragmaCompactGroupBy) {
  2718. auto req = "PRAGMA CompactGroupBy; SELECT key, COUNT(*) FROM plato.Input GROUP BY key;";
  2719. auto res = SqlToYql(req);
  2720. UNIT_ASSERT(res.Root);
  2721. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  2722. if (word == "Aggregate") {
  2723. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("'('compact)"));
  2724. }
  2725. };
  2726. TWordCountHive elementStat = { {TString("Aggregate"), 0}};
  2727. VerifyProgram(res, elementStat, verifyLine);
  2728. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Aggregate"]);
  2729. }
  2730. Y_UNIT_TEST(PragmaDisableCompactGroupBy) {
  2731. auto req = "PRAGMA DisableCompactGroupBy; SELECT key, COUNT(*) FROM plato.Input GROUP /*+ compact() */ BY key;";
  2732. auto res = SqlToYql(req);
  2733. UNIT_ASSERT(res.Root);
  2734. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  2735. if (word == "Aggregate") {
  2736. UNIT_ASSERT_VALUES_EQUAL(TString::npos, line.find("'('compact)"));
  2737. }
  2738. };
  2739. TWordCountHive elementStat = { {TString("Aggregate"), 0}};
  2740. VerifyProgram(res, elementStat, verifyLine);
  2741. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Aggregate"]);
  2742. }
  2743. Y_UNIT_TEST(AutoSampleWorksWithNamedSubquery) {
  2744. UNIT_ASSERT(SqlToYql("$src = select * from plato.Input; select * from $src sample 0.2").IsOk());
  2745. }
  2746. Y_UNIT_TEST(AutoSampleWorksWithSubquery) {
  2747. UNIT_ASSERT(SqlToYql("select * from (select * from plato.Input) sample 0.2").IsOk());
  2748. }
  2749. Y_UNIT_TEST(CreateTableTrailingComma) {
  2750. UNIT_ASSERT(SqlToYql("USE plato; CREATE TABLE tableName (Key Uint32, PRIMARY KEY (Key),);").IsOk());
  2751. UNIT_ASSERT(SqlToYql("USE plato; CREATE TABLE tableName (Key Uint32,);").IsOk());
  2752. }
  2753. Y_UNIT_TEST(BetweenSymmetric) {
  2754. UNIT_ASSERT(SqlToYql("select 3 between symmetric 5 and 4;").IsOk());
  2755. UNIT_ASSERT(SqlToYql("select 3 between asymmetric 5 and 4;").IsOk());
  2756. UNIT_ASSERT(SqlToYql("use plato; select key between symmetric and and and from Input;").IsOk());
  2757. UNIT_ASSERT(SqlToYql("use plato; select key between and and and from Input;").IsOk());
  2758. }
  2759. }
  2760. Y_UNIT_TEST_SUITE(ExternalFunction) {
  2761. Y_UNIT_TEST(ValidUseFunctions) {
  2762. UNIT_ASSERT(SqlToYql(
  2763. "PROCESS plato.Input"
  2764. " USING EXTERNAL FUNCTION('YANDEX-CLOUD', 'foo', <|a: 123, b: a + 641|>)"
  2765. " WITH INPUT_TYPE=Struct<a:Int32>, OUTPUT_TYPE=Struct<b:Int32>,"
  2766. " CONCURRENCY=3, OPTIMIZE_FOR='CALLS'").IsOk());
  2767. // use CALLS without quotes, as keyword
  2768. UNIT_ASSERT(SqlToYql(
  2769. "PROCESS plato.Input"
  2770. " USING EXTERNAL FUNCTION('YANDEX-CLOUD', 'foo')"
  2771. " WITH INPUT_TYPE=Struct<a:Int32>, OUTPUT_TYPE=Struct<b:Int32>,"
  2772. " OPTIMIZE_FOR=CALLS").IsOk());
  2773. UNIT_ASSERT(SqlToYql(
  2774. "PROCESS plato.Input"
  2775. " USING EXTERNAL FUNCTION('YANDEX-CLOUD', 'foo', TableRow())"
  2776. " WITH INPUT_TYPE=Struct<a:Int32>, OUTPUT_TYPE=Struct<b:Int32>,"
  2777. " CONCURRENCY=3").IsOk());
  2778. UNIT_ASSERT(SqlToYql(
  2779. "PROCESS plato.Input"
  2780. " USING EXTERNAL FUNCTION('YANDEX-CLOUD', 'foo')"
  2781. " WITH INPUT_TYPE=Struct<a:Int32>, OUTPUT_TYPE=Struct<b:Int32>,"
  2782. " CONCURRENCY=3, BATCH_SIZE=1000000, CONNECTION='yc-folder34fse-con',"
  2783. " INIT=[0, 900]").IsOk());
  2784. UNIT_ASSERT(SqlToYql(
  2785. "PROCESS plato.Input"
  2786. " USING EXTERNAL FUNCTION('YANDEX-CLOUD', 'bar', TableRow())"
  2787. " WITH UNKNOWN_PARAM_1='837747712', UNKNOWN_PARAM_2=Tuple<Uint16, Utf8>,"
  2788. " INPUT_TYPE=Struct<a:Int32>, OUTPUT_TYPE=Struct<b:Int32>").IsOk());
  2789. }
  2790. Y_UNIT_TEST(InValidUseFunctions) {
  2791. ExpectFailWithError("PROCESS plato.Input USING some::udf(*) WITH INPUT_TYPE=Struct<a:Int32>",
  2792. "<main>:1:33: Error: PROCESS without USING EXTERNAL FUNCTION doesn't allow WITH block\n");
  2793. ExpectFailWithError("PROCESS plato.Input USING EXTERNAL FUNCTION('YANDEX-CLOUD', 'jhhjfh88134d')"
  2794. " WITH INPUT_TYPE=Struct<a:Int32>, OUTPUT_TYPE=Struct<b:Int32>"
  2795. " ASSUME ORDER BY key",
  2796. "<main>:1:129: Error: PROCESS with USING EXTERNAL FUNCTION doesn't allow ASSUME block\n");
  2797. ExpectFailWithError("PROCESS plato.Input USING EXTERNAL FUNCTION('YANDEX-CLOUD', 'foo', 'bar', 'baz')",
  2798. "<main>:1:15: Error: EXTERNAL FUNCTION requires from 2 to 3 arguments, but got: 4\n");
  2799. ExpectFailWithError("PROCESS plato.Input\n"
  2800. " USING EXTERNAL FUNCTION('YANDEX-CLOUD', 'foo', <|field_1: a1, field_b: b1|>)\n"
  2801. " WITH INPUT_TYPE=Struct<a:Int32>, OUTPUT_TYPE=Struct<b:Int32>,\n"
  2802. " CONCURRENCY=3, BATCH_SIZE=1000000, CONNECTION='yc-folder34fse-con',\n"
  2803. " CONCURRENCY=5, INPUT_TYPE=Struct<b:Bool>,\n"
  2804. " INIT=[0, 900]\n",
  2805. "<main>:5:2: Error: WITH \"CONCURRENCY\" clause should be specified only once\n"
  2806. "<main>:5:17: Error: WITH \"INPUT_TYPE\" clause should be specified only once\n");
  2807. }
  2808. }
  2809. Y_UNIT_TEST_SUITE(SqlToYQLErrors) {
  2810. Y_UNIT_TEST(UdfSyntaxSugarMissingCall) {
  2811. auto req = "SELECT Udf(DateTime::FromString, \"foo\" as RunConfig);";
  2812. auto res = SqlToYql(req);
  2813. TString a1 = Err2Str(res);
  2814. TString a2("<main>:1:8: Error: Abstract Udf Node can't be used as a part of expression.\n");
  2815. UNIT_ASSERT_NO_DIFF(a1, a2);
  2816. }
  2817. Y_UNIT_TEST(UdfSyntaxSugarIsNotCallable) {
  2818. auto req = "SELECT Udf(123, \"foo\" as RunConfig);";
  2819. auto res = SqlToYql(req);
  2820. TString a1 = Err2Str(res);
  2821. TString a2("<main>:1:8: Error: Udf: first argument must be a callable, like Foo::Bar\n");
  2822. UNIT_ASSERT_NO_DIFF(a1, a2);
  2823. }
  2824. Y_UNIT_TEST(UdfSyntaxSugarNoArgs) {
  2825. auto req = "SELECT Udf()();";
  2826. auto res = SqlToYql(req);
  2827. TString a1 = Err2Str(res);
  2828. TString a2("<main>:1:8: Error: Udf: expected at least one argument\n");
  2829. UNIT_ASSERT_NO_DIFF(a1, a2);
  2830. }
  2831. Y_UNIT_TEST(StrayUTF8) {
  2832. /// 'c' in plato is russian here
  2833. NYql::TAstParseResult res = SqlToYql("select * from сedar.Input");
  2834. UNIT_ASSERT(!res.Root);
  2835. TString a1 = Err2Str(res);
  2836. TString a2(R"foo(<main>:1:14: Error: Unexpected character 'с' (Unicode character <1089>) : cannot match to any predicted input...
  2837. <main>:1:15: Error: Unexpected character : cannot match to any predicted input...
  2838. )foo");
  2839. UNIT_ASSERT_NO_DIFF(a1, a2);
  2840. }
  2841. Y_UNIT_TEST(IvalidStringLiteralWithEscapedBackslash) {
  2842. NYql::TAstParseResult res1 = SqlToYql(R"foo($bar = 'a\\'b';)foo");
  2843. NYql::TAstParseResult res2 = SqlToYql(R"foo($bar = "a\\"b";)foo");
  2844. UNIT_ASSERT(!res1.Root);
  2845. UNIT_ASSERT(!res2.Root);
  2846. UNIT_ASSERT_NO_DIFF(Err2Str(res1), "<main>:1:15: Error: Unexpected character : syntax error...\n\n");
  2847. UNIT_ASSERT_NO_DIFF(Err2Str(res2), "<main>:1:15: Error: Unexpected character : syntax error...\n\n");
  2848. }
  2849. Y_UNIT_TEST(InvalidHexInStringLiteral) {
  2850. NYql::TAstParseResult res = SqlToYql("select \"foo\\x1\\xfe\"");
  2851. UNIT_ASSERT(!res.Root);
  2852. TString a1 = Err2Str(res);
  2853. TString a2 = "<main>:1:15: Error: Failed to parse string literal: Invalid hexadecimal value\n";
  2854. UNIT_ASSERT_NO_DIFF(a1, a2);
  2855. }
  2856. Y_UNIT_TEST(InvalidOctalInMultilineStringLiteral) {
  2857. NYql::TAstParseResult res = SqlToYql("select \"foo\n"
  2858. "bar\n"
  2859. "\\01\"");
  2860. UNIT_ASSERT(!res.Root);
  2861. TString a1 = Err2Str(res);
  2862. TString a2 = "<main>:3:4: Error: Failed to parse string literal: Invalid octal value\n";
  2863. UNIT_ASSERT_NO_DIFF(a1, a2);
  2864. }
  2865. Y_UNIT_TEST(InvalidDoubleAtString) {
  2866. NYql::TAstParseResult res = SqlToYql("select @@@@@@");
  2867. UNIT_ASSERT(!res.Root);
  2868. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:13: Error: Unexpected character : syntax error...\n\n");
  2869. }
  2870. Y_UNIT_TEST(InvalidDoubleAtStringWhichWasAcceptedEarlier) {
  2871. NYql::TAstParseResult res = SqlToYql("SELECT @@foo@@ @ @@bar@@");
  2872. UNIT_ASSERT(!res.Root);
  2873. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:7: Error: Unexpected token '@@foo@@' : cannot match to any predicted input...\n\n");
  2874. }
  2875. Y_UNIT_TEST(InvalidStringFromTable) {
  2876. NYql::TAstParseResult res = SqlToYql("select \"FOO\"\"BAR from plato.foo");
  2877. UNIT_ASSERT(!res.Root);
  2878. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:31: Error: Unexpected character : syntax error...\n\n");
  2879. }
  2880. Y_UNIT_TEST(InvalidDoubleAtStringFromTable) {
  2881. NYql::TAstParseResult res = SqlToYql("select @@@@@@ from plato.foo");
  2882. UNIT_ASSERT(!res.Root);
  2883. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:28: Error: Unexpected character : syntax error...\n\n");
  2884. }
  2885. Y_UNIT_TEST(SelectInvalidSyntax) {
  2886. NYql::TAstParseResult res = SqlToYql("select 1 form Wat");
  2887. UNIT_ASSERT(!res.Root);
  2888. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:14: Error: Unexpected token 'Wat' : cannot match to any predicted input...\n\n");
  2889. }
  2890. Y_UNIT_TEST(SelectNoCluster) {
  2891. NYql::TAstParseResult res = SqlToYql("select foo from bar");
  2892. UNIT_ASSERT(!res.Root);
  2893. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:1: Error: No cluster name given and no default cluster is selected\n");
  2894. }
  2895. Y_UNIT_TEST(SelectDuplicateColumns) {
  2896. NYql::TAstParseResult res = SqlToYql("select a, a from plato.Input");
  2897. UNIT_ASSERT(!res.Root);
  2898. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:11: Error: Unable to use duplicate column names. Collision in name: a\n");
  2899. }
  2900. Y_UNIT_TEST(SelectDuplicateLabels) {
  2901. NYql::TAstParseResult res = SqlToYql("select a as foo, b as foo from plato.Input");
  2902. UNIT_ASSERT(!res.Root);
  2903. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:18: Error: Unable to use duplicate column names. Collision in name: foo\n");
  2904. }
  2905. Y_UNIT_TEST(SelectCaseWithoutThen) {
  2906. NYql::TAstParseResult res = SqlToYql("select case when true 1;");
  2907. UNIT_ASSERT(!res.Root);
  2908. UNIT_ASSERT_NO_DIFF(Err2Str(res),
  2909. "<main>:1:22: Error: Unexpected token absence : Missing THEN \n\n"
  2910. "<main>:1:23: Error: Unexpected token absence : Missing END \n\n"
  2911. );
  2912. }
  2913. Y_UNIT_TEST(SelectComplexCaseWithoutThen) {
  2914. NYql::TAstParseResult res = SqlToYql(
  2915. "SELECT *\n"
  2916. "FROM plato.Input AS a\n"
  2917. "WHERE CASE WHEN a.key = \"foo\" a.subkey ELSE a.value END\n"
  2918. );
  2919. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:3:30: Error: Unexpected token absence : Missing THEN \n\n");
  2920. }
  2921. Y_UNIT_TEST(SelectCaseWithoutEnd) {
  2922. NYql::TAstParseResult res = SqlToYql("select case a when b then c end from plato.Input");
  2923. UNIT_ASSERT(!res.Root);
  2924. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:8: Error: ELSE is required\n");
  2925. }
  2926. Y_UNIT_TEST(SelectWithBadAggregationNoInput) {
  2927. NYql::TAstParseResult res = SqlToYql("select a, Min(b), c");
  2928. UNIT_ASSERT(!res.Root);
  2929. UNIT_ASSERT_NO_DIFF(Err2Str(res),
  2930. "<main>:1:1: Error: Column references are not allowed without FROM\n"
  2931. "<main>:1:8: Error: Column reference 'a'\n"
  2932. "<main>:1:1: Error: Column references are not allowed without FROM\n"
  2933. "<main>:1:15: Error: Column reference 'b'\n"
  2934. "<main>:1:1: Error: Column references are not allowed without FROM\n"
  2935. "<main>:1:19: Error: Column reference 'c'\n"
  2936. );
  2937. }
  2938. Y_UNIT_TEST(SelectWithBadAggregation) {
  2939. ExpectFailWithError("select count(*), 1 + key from plato.Input",
  2940. "<main>:1:22: Error: Column `key` must either be a key column in GROUP BY or it should be used in aggregation function\n");
  2941. }
  2942. Y_UNIT_TEST(SelectWithBadAggregatedTerms) {
  2943. ExpectFailWithError("select key, 2 * subkey from plato.Input group by key",
  2944. "<main>:1:17: Error: Column `subkey` must either be a key column in GROUP BY or it should be used in aggregation function\n");
  2945. }
  2946. Y_UNIT_TEST(SelectDistinctWithBadAggregation) {
  2947. ExpectFailWithError("select distinct count(*), 1 + key from plato.Input",
  2948. "<main>:1:31: Error: Column `key` must either be a key column in GROUP BY or it should be used in aggregation function\n");
  2949. ExpectFailWithError("select distinct key, 2 * subkey from plato.Input group by key",
  2950. "<main>:1:26: Error: Column `subkey` must either be a key column in GROUP BY or it should be used in aggregation function\n");
  2951. }
  2952. Y_UNIT_TEST(SelectWithBadAggregationInHaving) {
  2953. ExpectFailWithError("select key from plato.Input group by key\n"
  2954. "having \"f\" || value == \"foo\"",
  2955. "<main>:2:15: Error: Column `value` must either be a key column in GROUP BY or it should be used in aggregation function\n");
  2956. }
  2957. Y_UNIT_TEST(JoinWithNonAggregatedColumnInProjection) {
  2958. ExpectFailWithError("select a.key, 1 + b.subkey\n"
  2959. "from plato.Input1 as a join plato.Input2 as b using(key)\n"
  2960. "group by a.key;",
  2961. "<main>:1:19: Error: Column `b.subkey` must either be a key column in GROUP BY or it should be used in aggregation function\n");
  2962. ExpectFailWithError("select a.key, 1 + b.subkey.x\n"
  2963. "from plato.Input1 as a join plato.Input2 as b using(key)\n"
  2964. "group by a.key;",
  2965. "<main>:1:19: Error: Column must either be a key column in GROUP BY or it should be used in aggregation function\n");
  2966. }
  2967. Y_UNIT_TEST(SelectWithBadAggregatedTermsWithSources) {
  2968. ExpectFailWithError("select key, 1 + a.subkey\n"
  2969. "from plato.Input1 as a\n"
  2970. "group by a.key;",
  2971. "<main>:1:17: Error: Column `a.subkey` must either be a key column in GROUP BY or it should be used in aggregation function\n");
  2972. ExpectFailWithError("select key, 1 + a.subkey.x\n"
  2973. "from plato.Input1 as a\n"
  2974. "group by a.key;",
  2975. "<main>:1:17: Error: Column must either be a key column in GROUP BY or it should be used in aggregation function\n");
  2976. }
  2977. Y_UNIT_TEST(WarnForAggregationBySelectAlias) {
  2978. NYql::TAstParseResult res = SqlToYql("select c + 1 as c from plato.Input\n"
  2979. "group by c");
  2980. UNIT_ASSERT(res.Root);
  2981. UNIT_ASSERT_NO_DIFF(Err2Str(res),
  2982. "<main>:2:11: Warning: GROUP BY will aggregate by column `c` instead of aggregating by SELECT expression with same alias, code: 4532\n"
  2983. "<main>:1:10: Warning: You should probably use alias in GROUP BY instead of using it here. Please consult documentation for more details, code: 4532\n");
  2984. res = SqlToYql("select c + 1 as c from plato.Input\n"
  2985. "group by Math::Floor(c + 2) as c;");
  2986. UNIT_ASSERT(res.Root);
  2987. UNIT_ASSERT_NO_DIFF(Err2Str(res),
  2988. "<main>:2:22: Warning: GROUP BY will aggregate by column `c` instead of aggregating by SELECT expression with same alias, code: 4532\n"
  2989. "<main>:1:10: Warning: You should probably use alias in GROUP BY instead of using it here. Please consult documentation for more details, code: 4532\n");
  2990. }
  2991. Y_UNIT_TEST(NoWarnForAggregationBySelectAliasWhenAggrFunctionsAreUsedInAlias) {
  2992. NYql::TAstParseResult res = SqlToYql("select\n"
  2993. " cast(avg(val) as int) as value,\n"
  2994. " value as key\n"
  2995. "from\n"
  2996. " plato.Input\n"
  2997. "group by value");
  2998. UNIT_ASSERT(res.Root);
  2999. UNIT_ASSERT(res.Issues.Size() == 0);
  3000. res = SqlToYql("select\n"
  3001. " cast(avg(val) over w as int) as value,\n"
  3002. " value as key\n"
  3003. "from\n"
  3004. " plato.Input\n"
  3005. "group by value\n"
  3006. "window w as ()");
  3007. UNIT_ASSERT(res.Root);
  3008. UNIT_ASSERT(res.Issues.Size() == 0);
  3009. }
  3010. Y_UNIT_TEST(NoWarnForAggregationBySelectAliasWhenQualifiedNameIsUsed) {
  3011. NYql::TAstParseResult res = SqlToYql("select\n"
  3012. " Unwrap(a.key) as key\n"
  3013. "from plato.Input as a\n"
  3014. "join plato.Input2 as b using(k)\n"
  3015. "group by a.key;");
  3016. UNIT_ASSERT(res.Root);
  3017. UNIT_ASSERT(res.Issues.Size() == 0);
  3018. res = SqlToYql("select Unwrap(a.key) as key\n"
  3019. "from plato.Input as a\n"
  3020. "group by a.key;");
  3021. UNIT_ASSERT(res.Root);
  3022. UNIT_ASSERT(res.Issues.Size() == 0);
  3023. }
  3024. Y_UNIT_TEST(NoWarnForAggregationBySelectAliasWhenTrivialRenamingIsUsed) {
  3025. NYql::TAstParseResult res = SqlToYql("select a.key as key\n"
  3026. "from plato.Input as a\n"
  3027. "group by key;");
  3028. UNIT_ASSERT(res.Root);
  3029. UNIT_ASSERT(res.Issues.Size() == 0);
  3030. res = SqlToYql("select key as key\n"
  3031. "from plato.Input\n"
  3032. "group by key;");
  3033. UNIT_ASSERT(res.Root);
  3034. UNIT_ASSERT(res.Issues.Size() == 0);
  3035. }
  3036. Y_UNIT_TEST(ErrorByAggregatingByExpressionWithSameExpressionInSelect) {
  3037. ExpectFailWithError("select k * 2 from plato.Input group by k * 2",
  3038. "<main>:1:8: Error: Column `k` must either be a key column in GROUP BY or it should be used in aggregation function\n");
  3039. }
  3040. Y_UNIT_TEST(ErrorForAggregationBySelectAlias) {
  3041. ExpectFailWithError("select key, Math::Floor(1.1 + a.subkey) as foo\n"
  3042. "from plato.Input as a\n"
  3043. "group by a.key, foo;",
  3044. "<main>:3:17: Warning: GROUP BY will aggregate by column `foo` instead of aggregating by SELECT expression with same alias, code: 4532\n"
  3045. "<main>:1:19: Warning: You should probably use alias in GROUP BY instead of using it here. Please consult documentation for more details, code: 4532\n"
  3046. "<main>:1:31: Error: Column `a.subkey` must either be a key column in GROUP BY or it should be used in aggregation function\n");
  3047. ExpectFailWithError("select c + 1 as c from plato.Input\n"
  3048. "group by Math::Floor(c + 2);",
  3049. "<main>:2:22: Warning: GROUP BY will aggregate by column `c` instead of aggregating by SELECT expression with same alias, code: 4532\n"
  3050. "<main>:1:10: Warning: You should probably use alias in GROUP BY instead of using it here. Please consult documentation for more details, code: 4532\n"
  3051. "<main>:1:8: Error: Column `c` must either be a key column in GROUP BY or it should be used in aggregation function\n");
  3052. }
  3053. Y_UNIT_TEST(ExplainQueryPlan) {
  3054. NYql::TAstParseResult res = SqlToYql("EXPLAIN Q U E R Y PLAN SELECT 1;");
  3055. UNIT_ASSERT(!res.Root);
  3056. UNIT_ASSERT_STRING_CONTAINS(Err2Str(res), "<main>:1:8: Error: Unexpected token 'Q' : cannot match to any predicted input");
  3057. }
  3058. Y_UNIT_TEST(SelectWithDuplicateGroupingColumns) {
  3059. NYql::TAstParseResult res = SqlToYql("select c from plato.Input group by c, c");
  3060. UNIT_ASSERT(!res.Root);
  3061. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:8: Error: Duplicate grouping column: c\n");
  3062. }
  3063. Y_UNIT_TEST(SelectWithBadAggregationInGrouping) {
  3064. NYql::TAstParseResult res = SqlToYql("select a, Min(b), c group by c");
  3065. UNIT_ASSERT(!res.Root);
  3066. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:1: Error: Column references are not allowed without FROM\n"
  3067. "<main>:1:30: Error: Column reference 'c'\n");
  3068. }
  3069. Y_UNIT_TEST(SelectWithOpOnBadAggregation) {
  3070. ExpectFailWithError("select 1 + a + Min(b) from plato.Input",
  3071. "<main>:1:12: Error: Column `a` must either be a key column in GROUP BY or it should be used in aggregation function\n");
  3072. }
  3073. Y_UNIT_TEST(SelectOrderByConstantNum) {
  3074. NYql::TAstParseResult res = SqlToYql("select a from plato.Input order by 1");
  3075. UNIT_ASSERT(!res.Root);
  3076. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:36: Error: Unable to ORDER BY constant expression\n");
  3077. }
  3078. Y_UNIT_TEST(SelectOrderByConstantExpr) {
  3079. NYql::TAstParseResult res = SqlToYql("select a from plato.Input order by 1 * 42");
  3080. UNIT_ASSERT(!res.Root);
  3081. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:38: Error: Unable to ORDER BY constant expression\n");
  3082. }
  3083. Y_UNIT_TEST(SelectOrderByConstantString) {
  3084. NYql::TAstParseResult res = SqlToYql("select a from plato.Input order by \"nest\"");
  3085. UNIT_ASSERT(!res.Root);
  3086. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:36: Error: Unable to ORDER BY constant expression\n");
  3087. }
  3088. Y_UNIT_TEST(SelectOrderByAggregated) {
  3089. NYql::TAstParseResult res = SqlToYql("select a from plato.Input order by min(a)");
  3090. UNIT_ASSERT(!res.Root);
  3091. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:36: Error: Unable to ORDER BY aggregated values\n");
  3092. }
  3093. Y_UNIT_TEST(ErrorInOrderByExpresison) {
  3094. NYql::TAstParseResult res = SqlToYql("select key, value from plato.Input order by (key as zey)");
  3095. UNIT_ASSERT(!res.Root);
  3096. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:45: Error: You should use in ORDER BY column name, qualified field, callable function or expression\n");
  3097. }
  3098. Y_UNIT_TEST(ErrorsInOrderByWhenColumnIsMissingInProjection) {
  3099. ExpectFailWithError("select subkey from (select 1 as subkey) order by key", "<main>:1:50: Error: Column key is not in source column set\n");
  3100. ExpectFailWithError("select subkey from plato.Input as a order by x.key", "<main>:1:46: Error: Unknown correlation name: x\n");
  3101. ExpectFailWithError("select distinct a, b from plato.Input order by c", "<main>:1:48: Error: Column c is not in source column set. Did you mean a?\n");
  3102. ExpectFailWithError("select count(*) as a from plato.Input order by c", "<main>:1:48: Error: Column c is not in source column set. Did you mean a?\n");
  3103. ExpectFailWithError("select count(*) as a, b, from plato.Input group by b order by c", "<main>:1:63: Error: Column c is not in source column set. Did you mean a?\n");
  3104. UNIT_ASSERT(SqlToYql("select a, b from plato.Input order by c").IsOk());
  3105. }
  3106. Y_UNIT_TEST(SelectAggregatedWhere) {
  3107. NYql::TAstParseResult res = SqlToYql("select * from plato.Input where count(key)");
  3108. UNIT_ASSERT(!res.Root);
  3109. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:33: Error: Can not use aggregated values in filtering\n");
  3110. }
  3111. Y_UNIT_TEST(DoubleFrom) {
  3112. NYql::TAstParseResult res = SqlToYql("from plato.Input select * from plato.Input");
  3113. UNIT_ASSERT(!res.Root);
  3114. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:27: Error: Only one FROM clause is allowed\n");
  3115. }
  3116. Y_UNIT_TEST(SelectJoinMissingCorrName) {
  3117. NYql::TAstParseResult res = SqlToYql("select * from plato.Input1 as a join plato.Input2 as b on a.key == key");
  3118. UNIT_ASSERT(!res.Root);
  3119. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:65: Error: JOIN: column requires correlation name\n");
  3120. }
  3121. Y_UNIT_TEST(SelectJoinMissingCorrName1) {
  3122. NYql::TAstParseResult res = SqlToYql(
  3123. "use plato;\n"
  3124. "$foo = select * from Input1;\n"
  3125. "select * from Input2 join $foo USING(key);\n"
  3126. );
  3127. UNIT_ASSERT(!res.Root);
  3128. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:3:27: Error: JOIN: missing correlation name for source\n");
  3129. }
  3130. Y_UNIT_TEST(SelectJoinMissingCorrName2) {
  3131. NYql::TAstParseResult res = SqlToYql(
  3132. "use plato;\n"
  3133. "$foo = select * from Input1;\n"
  3134. "select * from Input2 cross join $foo;\n"
  3135. );
  3136. UNIT_ASSERT(!res.Root);
  3137. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:3:33: Error: JOIN: missing correlation name for source\n");
  3138. }
  3139. Y_UNIT_TEST(SelectJoinEmptyCorrNames) {
  3140. NYql::TAstParseResult res = SqlToYql(
  3141. "$left = (SELECT * FROM plato.Input1 LIMIT 2);\n"
  3142. "$right = (SELECT * FROM plato.Input2 LIMIT 2);\n"
  3143. "SELECT * FROM $left FULL JOIN $right USING (key);\n"
  3144. );
  3145. UNIT_ASSERT(!res.Root);
  3146. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:3:45: Error: At least one correlation name is required in join\n");
  3147. }
  3148. Y_UNIT_TEST(SelectJoinSameCorrNames) {
  3149. NYql::TAstParseResult res = SqlToYql("SELECT Input.key FROM plato.Input JOIN plato.Input1 ON Input.key == Input.subkey\n");
  3150. UNIT_ASSERT(!res.Root);
  3151. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:66: Error: JOIN: different correlation names are required for joined tables\n");
  3152. }
  3153. Y_UNIT_TEST(SelectJoinConstPredicateArg) {
  3154. NYql::TAstParseResult res = SqlToYql("SELECT * FROM plato.Input1 as A JOIN plato.Input2 as B ON A.key == B.key AND A.subkey == \"wtf\"\n");
  3155. UNIT_ASSERT(!res.Root);
  3156. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:87: Error: JOIN: each equality predicate argument must depend on exactly one JOIN input\n");
  3157. }
  3158. Y_UNIT_TEST(SelectJoinNonEqualityPredicate) {
  3159. NYql::TAstParseResult res = SqlToYql("SELECT * FROM plato.Input1 as A JOIN plato.Input2 as B ON A.key == B.key AND A.subkey > B.subkey\n");
  3160. UNIT_ASSERT(!res.Root);
  3161. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:87: Error: JOIN ON expression must be a conjunction of equality predicates\n");
  3162. }
  3163. Y_UNIT_TEST(SelectEquiJoinCorrNameOutOfScope) {
  3164. NYql::TAstParseResult res = SqlToYql(
  3165. "PRAGMA equijoin;\n"
  3166. "SELECT * FROM plato.A JOIN plato.B ON A.key == C.key JOIN plato.C ON A.subkey == C.subkey;\n"
  3167. );
  3168. UNIT_ASSERT(!res.Root);
  3169. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:2:45: Error: JOIN: can not use source: C in equality predicate, it is out of current join scope\n");
  3170. }
  3171. Y_UNIT_TEST(SelectEquiJoinNoRightSource) {
  3172. NYql::TAstParseResult res = SqlToYql(
  3173. "PRAGMA equijoin;\n"
  3174. "SELECT * FROM plato.A JOIN plato.B ON A.key == B.key JOIN plato.C ON A.subkey == B.subkey;\n"
  3175. );
  3176. UNIT_ASSERT(!res.Root);
  3177. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:2:79: Error: JOIN ON equality predicate must have one of its arguments from the rightmost source\n");
  3178. }
  3179. Y_UNIT_TEST(SelectEquiJoinOuterWithoutType) {
  3180. NYql::TAstParseResult res = SqlToYql(
  3181. "SELECT * FROM plato.A Outer JOIN plato.B ON A.key == B.key;\n"
  3182. );
  3183. UNIT_ASSERT(!res.Root);
  3184. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:23: Error: Invalid join type: OUTER JOIN. OUTER keyword is optional and can only be used after LEFT, RIGHT or FULL\n");
  3185. }
  3186. Y_UNIT_TEST(SelectEquiJoinOuterWithWrongType) {
  3187. NYql::TAstParseResult res = SqlToYql(
  3188. "SELECT * FROM plato.A LEFT semi OUTER JOIN plato.B ON A.key == B.key;\n"
  3189. );
  3190. UNIT_ASSERT(!res.Root);
  3191. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:33: Error: Invalid join type: LEFT SEMI OUTER JOIN. OUTER keyword is optional and can only be used after LEFT, RIGHT or FULL\n");
  3192. }
  3193. Y_UNIT_TEST(InsertNoCluster) {
  3194. NYql::TAstParseResult res = SqlToYql("insert into Output (foo) values (1)");
  3195. UNIT_ASSERT(!res.Root);
  3196. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:1: Error: No cluster name given and no default cluster is selected\n");
  3197. }
  3198. Y_UNIT_TEST(InsertValuesNoLabels) {
  3199. NYql::TAstParseResult res = SqlToYql("insert into plato.Output values (1)");
  3200. UNIT_ASSERT(!res.Root);
  3201. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:19: Error: INSERT INTO ... VALUES requires specification of table columns\n");
  3202. }
  3203. Y_UNIT_TEST(UpsertValuesNoLabelsKikimr) {
  3204. NYql::TAstParseResult res = SqlToYql("upsert into plato.Output values (1)", 10, TString(NYql::KikimrProviderName));
  3205. UNIT_ASSERT(!res.Root);
  3206. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:19: Error: UPSERT INTO ... VALUES requires specification of table columns\n");
  3207. }
  3208. Y_UNIT_TEST(ReplaceValuesNoLabelsKikimr) {
  3209. NYql::TAstParseResult res = SqlToYql("replace into plato.Output values (1)", 10, TString(NYql::KikimrProviderName));
  3210. UNIT_ASSERT(!res.Root);
  3211. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:20: Error: REPLACE INTO ... VALUES requires specification of table columns\n");
  3212. }
  3213. Y_UNIT_TEST(InsertValuesInvalidLabels) {
  3214. NYql::TAstParseResult res = SqlToYql("insert into plato.Output (foo) values (1, 2)");
  3215. UNIT_ASSERT(!res.Root);
  3216. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:27: Error: VALUES have 2 columns, INSERT INTO expects: 1\n");
  3217. }
  3218. Y_UNIT_TEST(BuiltinFileOpNoArgs) {
  3219. NYql::TAstParseResult res = SqlToYql("select FilePath()");
  3220. UNIT_ASSERT(!res.Root);
  3221. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:8: Error: FilePath() requires exactly 1 arguments, given: 0\n");
  3222. }
  3223. Y_UNIT_TEST(ProcessWithHaving) {
  3224. NYql::TAstParseResult res = SqlToYql("process plato.Input using some::udf(value) having value == 1");
  3225. UNIT_ASSERT(!res.Root);
  3226. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:15: Error: PROCESS does not allow HAVING yet! You may request it on yql@ maillist.\n");
  3227. }
  3228. Y_UNIT_TEST(ReduceNoBy) {
  3229. NYql::TAstParseResult res = SqlToYql("reduce plato.Input using some::udf(value)");
  3230. UNIT_ASSERT(!res.Root);
  3231. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:19: Error: Unexpected token absence : Missing ON \n\n<main>:1:25: Error: Unexpected token absence : Missing USING \n\n");
  3232. }
  3233. Y_UNIT_TEST(ReduceDistinct) {
  3234. NYql::TAstParseResult res = SqlToYql("reduce plato.Input on key using some::udf(distinct value)");
  3235. UNIT_ASSERT(!res.Root);
  3236. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:43: Error: DISTINCT can not be used in PROCESS/REDUCE\n");
  3237. }
  3238. Y_UNIT_TEST(CreateTableWithView) {
  3239. NYql::TAstParseResult res = SqlToYql("CREATE TABLE plato.foo:bar (key INT);");
  3240. UNIT_ASSERT(!res.Root);
  3241. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:22: Error: Unexpected token ':' : syntax error...\n\n");
  3242. }
  3243. Y_UNIT_TEST(AsteriskWithSomethingAfter) {
  3244. NYql::TAstParseResult res = SqlToYql("select *, LENGTH(value) from plato.Input;");
  3245. UNIT_ASSERT(!res.Root);
  3246. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:8: Error: Unable to use plain '*' with other projection items. Please use qualified asterisk instead: '<table>.*' (<table> can be either table name or table alias).\n");
  3247. }
  3248. Y_UNIT_TEST(AsteriskWithSomethingBefore) {
  3249. NYql::TAstParseResult res = SqlToYql("select LENGTH(value), * from plato.Input;");
  3250. UNIT_ASSERT(!res.Root);
  3251. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:23: Error: Unable to use plain '*' with other projection items. Please use qualified asterisk instead: '<table>.*' (<table> can be either table name or table alias).\n");
  3252. }
  3253. Y_UNIT_TEST(DuplicatedQualifiedAsterisk) {
  3254. NYql::TAstParseResult res = SqlToYql("select in.*, key, in.* from plato.Input as in;");
  3255. UNIT_ASSERT(!res.Root);
  3256. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:19: Error: Unable to use twice same quialified asterisk. Invalid source: in\n");
  3257. }
  3258. Y_UNIT_TEST(BrokenLabel) {
  3259. NYql::TAstParseResult res = SqlToYql("select in.*, key as `funny.label` from plato.Input as in;");
  3260. UNIT_ASSERT(!res.Root);
  3261. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:14: Error: Unable to use '.' in column name. Invalid column name: funny.label\n");
  3262. }
  3263. Y_UNIT_TEST(KeyConflictDetect0) {
  3264. NYql::TAstParseResult res = SqlToYql("select key, in.key as key from plato.Input as in;");
  3265. UNIT_ASSERT(!res.Root);
  3266. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:13: Error: Unable to use duplicate column names. Collision in name: key\n");
  3267. }
  3268. Y_UNIT_TEST(KeyConflictDetect1) {
  3269. NYql::TAstParseResult res = SqlToYql("select length(key) as key, key from plato.Input;");
  3270. UNIT_ASSERT(!res.Root);
  3271. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:28: Error: Unable to use duplicate column names. Collision in name: key\n");
  3272. }
  3273. Y_UNIT_TEST(KeyConflictDetect2) {
  3274. NYql::TAstParseResult res = SqlToYql("select key, in.key from plato.Input as in;");
  3275. UNIT_ASSERT(!res.Root);
  3276. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:1: Error: Duplicate column: key\n");
  3277. }
  3278. Y_UNIT_TEST(AutogenerationAliasWithCollisionConflict1) {
  3279. UNIT_ASSERT(SqlToYql("select LENGTH(Value), key as column0 from plato.Input;").IsOk());
  3280. }
  3281. Y_UNIT_TEST(AutogenerationAliasWithCollisionConflict2) {
  3282. UNIT_ASSERT(SqlToYql("select key as column1, LENGTH(Value) from plato.Input;").IsOk());
  3283. }
  3284. Y_UNIT_TEST(MissedSourceTableForQualifiedAsteriskOnSimpleSelect) {
  3285. NYql::TAstParseResult res = SqlToYql("use plato; select Intop.*, Input.key from plato.Input;");
  3286. UNIT_ASSERT(!res.Root);
  3287. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:19: Error: Unknown correlation name: Intop\n");
  3288. }
  3289. Y_UNIT_TEST(MissedSourceTableForQualifiedAsteriskOnJoin) {
  3290. NYql::TAstParseResult res = SqlToYql("use plato; select tmissed.*, t2.*, t1.key from plato.Input as t1 join plato.Input as t2 on t1.key==t2.key;");
  3291. UNIT_ASSERT(!res.Root);
  3292. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:19: Error: Unknown correlation name for asterisk: tmissed\n");
  3293. }
  3294. Y_UNIT_TEST(UnableToReferenceOnNotExistSubcolumn) {
  3295. NYql::TAstParseResult res = SqlToYql("select b.subkey from (select key from plato.Input as a) as b;");
  3296. UNIT_ASSERT(!res.Root);
  3297. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:8: Error: Column subkey is not in source column set\n");
  3298. }
  3299. Y_UNIT_TEST(ConflictOnSameNameWithQualify0) {
  3300. NYql::TAstParseResult res = SqlToYql("select in.key, in.key as key from plato.Input as in;");
  3301. UNIT_ASSERT(!res.Root);
  3302. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:1: Error: Duplicate column: key\n");
  3303. }
  3304. Y_UNIT_TEST(ConflictOnSameNameWithQualify1) {
  3305. NYql::TAstParseResult res = SqlToYql("select in.key, length(key) as key from plato.Input as in;");
  3306. UNIT_ASSERT(!res.Root);
  3307. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:1: Error: Duplicate column: key\n");
  3308. }
  3309. Y_UNIT_TEST(ConflictOnSameNameWithQualify2) {
  3310. NYql::TAstParseResult res = SqlToYql("select key, in.key from plato.Input as in;");
  3311. UNIT_ASSERT(!res.Root);
  3312. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:1: Error: Duplicate column: key\n");
  3313. }
  3314. Y_UNIT_TEST(ConflictOnSameNameWithQualify3) {
  3315. NYql::TAstParseResult res = SqlToYql("select in.key, subkey as key from plato.Input as in;");
  3316. UNIT_ASSERT(!res.Root);
  3317. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:1: Error: Duplicate column: key\n");
  3318. }
  3319. Y_UNIT_TEST(SelectFlattenBySameColumns) {
  3320. NYql::TAstParseResult res = SqlToYql("select key from plato.Input flatten by (key, key as kk)");
  3321. UNIT_ASSERT(!res.Root);
  3322. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:46: Error: Duplicate column name found: key in FlattenBy section\n");
  3323. }
  3324. Y_UNIT_TEST(SelectFlattenBySameAliases) {
  3325. NYql::TAstParseResult res = SqlToYql("select key from plato.Input flatten by (key as kk, subkey as kk);");
  3326. UNIT_ASSERT(!res.Root);
  3327. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:52: Error: Duplicate alias found: kk in FlattenBy section\n");
  3328. }
  3329. Y_UNIT_TEST(SelectFlattenByExprSameAliases) {
  3330. NYql::TAstParseResult res = SqlToYql("select key from plato.Input flatten by (key as kk, ListSkip(subkey,1) as kk);");
  3331. UNIT_ASSERT(!res.Root);
  3332. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:52: Error: Collision between alias and column name: kk in FlattenBy section\n");
  3333. }
  3334. Y_UNIT_TEST(SelectFlattenByConflictNameAndAlias0) {
  3335. NYql::TAstParseResult res = SqlToYql("select key from plato.Input flatten by (key, subkey as key);");
  3336. UNIT_ASSERT(!res.Root);
  3337. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:46: Error: Collision between alias and column name: key in FlattenBy section\n");
  3338. }
  3339. Y_UNIT_TEST(SelectFlattenByConflictNameAndAlias1) {
  3340. NYql::TAstParseResult res = SqlToYql("select key from plato.Input flatten by (key as kk, subkey as key);");
  3341. UNIT_ASSERT(!res.Root);
  3342. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:52: Error: Collision between alias and column name: key in FlattenBy section\n");
  3343. }
  3344. Y_UNIT_TEST(SelectFlattenByExprConflictNameAndAlias1) {
  3345. NYql::TAstParseResult res = SqlToYql("select key from plato.Input flatten by (key as kk, ListSkip(subkey,1) as key);");
  3346. UNIT_ASSERT(!res.Root);
  3347. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:52: Error: Duplicate column name found: key in FlattenBy section\n");
  3348. }
  3349. Y_UNIT_TEST(SelectFlattenByUnnamedExpr) {
  3350. NYql::TAstParseResult res = SqlToYql("select key from plato.Input flatten by (key, ListSkip(key, 1))");
  3351. UNIT_ASSERT(!res.Root);
  3352. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:46: Error: Unnamed expression after FLATTEN BY is not allowed\n");
  3353. }
  3354. Y_UNIT_TEST(UseInOnStrings) {
  3355. NYql::TAstParseResult res = SqlToYql("select * from plato.Input where \"foo\" in \"foovalue\";");
  3356. UNIT_ASSERT(!res.Root);
  3357. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:42: Error: Unable to use IN predicate with string argument, it won't search substring - "
  3358. "expecting tuple, list, dict or single column table source\n");
  3359. }
  3360. Y_UNIT_TEST(UseSubqueryInScalarContextInsideIn) {
  3361. NYql::TAstParseResult res = SqlToYql("$q = (select key from plato.Input); select * from plato.Input where subkey in ($q);");
  3362. UNIT_ASSERT(res.Root);
  3363. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:79: Warning: Using subrequest in scalar context after IN, "
  3364. "perhaps you should remove parenthesis here, code: 4501\n");
  3365. }
  3366. Y_UNIT_TEST(InHintsWithKeywordClash) {
  3367. NYql::TAstParseResult res = SqlToYql("SELECT COMPACT FROM plato.Input WHERE COMPACT IN COMPACT `COMPACT`(1,2,3)");
  3368. UNIT_ASSERT(!res.Root);
  3369. // should try to parse last compact as call expression
  3370. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:58: Error: Unknown builtin: COMPACT\n");
  3371. }
  3372. Y_UNIT_TEST(ErrorColumnPosition) {
  3373. NYql::TAstParseResult res = SqlToYql(
  3374. "USE plato;\n"
  3375. "SELECT \n"
  3376. "value FROM (\n"
  3377. "select key from Input\n"
  3378. ");\n"
  3379. );
  3380. UNIT_ASSERT(!res.Root);
  3381. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:3:1: Error: Column value is not in source column set\n");
  3382. }
  3383. Y_UNIT_TEST(PrimaryViewAbortMapReduce) {
  3384. NYql::TAstParseResult res = SqlToYql("SELECT key FROM plato.Input VIEW PRIMARY KEY");
  3385. UNIT_ASSERT(!res.Root);
  3386. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:17: Error: primary view is not supported for yt tables\n");
  3387. }
  3388. Y_UNIT_TEST(InsertAbortMapReduce) {
  3389. NYql::TAstParseResult res = SqlToYql("INSERT OR ABORT INTO plato.Output SELECT key FROM plato.Input");
  3390. UNIT_ASSERT(!res.Root);
  3391. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:0: Error: INSERT OR ABORT INTO is not supported for yt tables\n");
  3392. }
  3393. Y_UNIT_TEST(ReplaceIntoMapReduce) {
  3394. NYql::TAstParseResult res = SqlToYql("REPLACE INTO plato.Output SELECT key FROM plato.Input");
  3395. UNIT_ASSERT(!res.Root);
  3396. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:0: Error: Meaning of REPLACE INTO has been changed, now you should use INSERT INTO <table> WITH TRUNCATE ... for yt\n");
  3397. }
  3398. Y_UNIT_TEST(UpsertIntoMapReduce) {
  3399. NYql::TAstParseResult res = SqlToYql("UPSERT INTO plato.Output SELECT key FROM plato.Input");
  3400. UNIT_ASSERT(!res.Root);
  3401. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:0: Error: UPSERT INTO is not supported for yt tables\n");
  3402. }
  3403. Y_UNIT_TEST(UpdateMapReduce) {
  3404. NYql::TAstParseResult res = SqlToYql("UPDATE plato.Output SET value = value + 1 WHERE key < 1");
  3405. UNIT_ASSERT(!res.Root);
  3406. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:0: Error: UPDATE is unsupported for yt\n");
  3407. }
  3408. Y_UNIT_TEST(DeleteMapReduce) {
  3409. NYql::TAstParseResult res = SqlToYql("DELETE FROM plato.Output WHERE key < 1");
  3410. UNIT_ASSERT(!res.Root);
  3411. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:0: Error: DELETE is unsupported for yt\n");
  3412. }
  3413. Y_UNIT_TEST(ReplaceIntoWithTruncate) {
  3414. NYql::TAstParseResult res = SqlToYql("REPLACE INTO plato.Output WITH TRUNCATE SELECT key FROM plato.Input");
  3415. UNIT_ASSERT(!res.Root);
  3416. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:32: Error: Unable REPLACE INTO with truncate mode\n");
  3417. }
  3418. Y_UNIT_TEST(UpsertIntoWithTruncate) {
  3419. NYql::TAstParseResult res = SqlToYql("UPSERT INTO plato.Output WITH TRUNCATE SELECT key FROM plato.Input");
  3420. UNIT_ASSERT(!res.Root);
  3421. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:31: Error: Unable UPSERT INTO with truncate mode\n");
  3422. }
  3423. Y_UNIT_TEST(InsertIntoWithTruncateKikimr) {
  3424. NYql::TAstParseResult res = SqlToYql("INSERT INTO plato.Output WITH TRUNCATE SELECT key FROM plato.Input", 10, TString(NYql::KikimrProviderName));
  3425. UNIT_ASSERT(!res.Root);
  3426. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:0: Error: INSERT INTO WITH TRUNCATE is not supported for kikimr tables\n");
  3427. }
  3428. Y_UNIT_TEST(InsertIntoWithWrongArgumentCount) {
  3429. NYql::TAstParseResult res = SqlToYql("insert into plato.Output with truncate (key, value, subkey) values (5, '1', '2', '3');");
  3430. UNIT_ASSERT(!res.Root);
  3431. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:53: Error: VALUES have 4 columns, INSERT INTO ... WITH TRUNCATE expects: 3\n");
  3432. }
  3433. Y_UNIT_TEST(UpsertWithWrongArgumentCount) {
  3434. NYql::TAstParseResult res = SqlToYql("upsert into plato.Output (key, value, subkey) values (2, '3');", 10, TString(NYql::KikimrProviderName));
  3435. UNIT_ASSERT(!res.Root);
  3436. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:39: Error: VALUES have 2 columns, UPSERT INTO expects: 3\n");
  3437. }
  3438. Y_UNIT_TEST(GroupingSetByExprWithoutAlias) {
  3439. NYql::TAstParseResult res = SqlToYql("SELECT key FROM plato.Input GROUP BY GROUPING SETS (cast(key as uint32), subkey);");
  3440. UNIT_ASSERT(!res.Root);
  3441. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:53: Error: Unnamed expressions are not supported in GROUPING SETS. Please use '<expr> AS <name>'.\n");
  3442. }
  3443. Y_UNIT_TEST(GroupingSetByExprWithoutAlias2) {
  3444. NYql::TAstParseResult res = SqlToYql("SELECT key FROM plato.Input GROUP BY subkey || subkey, GROUPING SETS (\n"
  3445. "cast(key as uint32), subkey);");
  3446. UNIT_ASSERT(!res.Root);
  3447. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:2:1: Error: Unnamed expressions are not supported in GROUPING SETS. Please use '<expr> AS <name>'.\n");
  3448. }
  3449. Y_UNIT_TEST(CubeByExprWithoutAlias) {
  3450. NYql::TAstParseResult res = SqlToYql("SELECT key FROM plato.Input GROUP BY CUBE (key, subkey / key);");
  3451. UNIT_ASSERT(!res.Root);
  3452. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:56: Error: Unnamed expressions are not supported in CUBE. Please use '<expr> AS <name>'.\n");
  3453. }
  3454. Y_UNIT_TEST(RollupByExprWithoutAlias) {
  3455. NYql::TAstParseResult res = SqlToYql("SELECT key FROM plato.Input GROUP BY ROLLUP (subkey / key);");
  3456. UNIT_ASSERT(!res.Root);
  3457. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:53: Error: Unnamed expressions are not supported in ROLLUP. Please use '<expr> AS <name>'.\n");
  3458. }
  3459. Y_UNIT_TEST(GroupByHugeCubeDeniedNoPragma) {
  3460. NYql::TAstParseResult res = SqlToYql("SELECT key FROM plato.Input GROUP BY CUBE (key, subkey, value, key + subkey as sum, key - subkey as sub, key + val as keyval);");
  3461. UNIT_ASSERT(!res.Root);
  3462. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:119: Error: GROUP BY CUBE is allowed only for 5 columns, but you use 6\n");
  3463. }
  3464. Y_UNIT_TEST(GroupByInvalidPragma) {
  3465. NYql::TAstParseResult res = SqlToYql("PRAGMA GroupByCubeLimit = '-4';");
  3466. UNIT_ASSERT(!res.Root);
  3467. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:27: Error: Expected unsigned integer literal as a single argument for: GroupByCubeLimit\n");
  3468. }
  3469. Y_UNIT_TEST(GroupByHugeCubeDeniedPragme) {
  3470. NYql::TAstParseResult res = SqlToYql("PRAGMA GroupByCubeLimit = '4'; SELECT key FROM plato.Input GROUP BY CUBE (key, subkey, value, key + subkey as sum, key - subkey as sub);");
  3471. UNIT_ASSERT(!res.Root);
  3472. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:132: Error: GROUP BY CUBE is allowed only for 4 columns, but you use 5\n");
  3473. }
  3474. Y_UNIT_TEST(GroupByFewBigCubes) {
  3475. NYql::TAstParseResult res = SqlToYql("SELECT key FROM plato.Input GROUP BY CUBE(key, subkey, key + subkey as sum), CUBE(value, value + key + subkey as total);");
  3476. UNIT_ASSERT(!res.Root);
  3477. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:1: Error: Unable to GROUP BY more than 64 groups, you try use 80 groups\n");
  3478. }
  3479. Y_UNIT_TEST(GroupByFewBigCubesWithPragmaLimit) {
  3480. NYql::TAstParseResult res = SqlToYql("PRAGMA GroupByLimit = '16'; SELECT key FROM plato.Input GROUP BY GROUPING SETS(key, subkey, key + subkey as sum), ROLLUP(value, value + key + subkey as total);");
  3481. UNIT_ASSERT(!res.Root);
  3482. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:29: Error: Unable to GROUP BY more than 16 groups, you try use 18 groups\n");
  3483. }
  3484. Y_UNIT_TEST(NoGroupingColumn0) {
  3485. NYql::TAstParseResult res = SqlToYql(
  3486. "select count(1), key_first, val_first, grouping(key_first, val_first, nomind) as group\n"
  3487. "from plato.Input group by grouping sets (cast(key as uint32) /100 as key_first, Substring(value, 1, 1) as val_first);");
  3488. UNIT_ASSERT(!res.Root);
  3489. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:71: Error: Column 'nomind' is not a grouping column\n");
  3490. }
  3491. Y_UNIT_TEST(NoGroupingColumn1) {
  3492. NYql::TAstParseResult res = SqlToYql("select count(1), grouping(key, value) as group_duo from plato.Input group by cube (key, subkey);");
  3493. UNIT_ASSERT(!res.Root);
  3494. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:32: Error: Column 'value' is not a grouping column\n");
  3495. }
  3496. Y_UNIT_TEST(EmptyAccess0) {
  3497. NYql::TAstParseResult res = SqlToYql("insert into plato.Output (list0, list1) values (AsList(0, 1, 2), AsList(``));");
  3498. UNIT_ASSERT(!res.Root);
  3499. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:73: Error: Column reference \"\" is not allowed in current scope\n");
  3500. }
  3501. Y_UNIT_TEST(EmptyAccess1) {
  3502. NYql::TAstParseResult res = SqlToYql("insert into plato.Output (list0, list1) values (AsList(0, 1, 2), ``);");
  3503. UNIT_ASSERT(!res.Root);
  3504. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:66: Error: Column reference \"\" is not allowed in current scope\n");
  3505. }
  3506. Y_UNIT_TEST(UseUnknownColumnInInsert) {
  3507. NYql::TAstParseResult res = SqlToYql("insert into plato.Output (list0, list1) values (AsList(0, 1, 2), AsList(`test`));");
  3508. UNIT_ASSERT(!res.Root);
  3509. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:73: Error: Column reference \"test\" is not allowed in current scope\n");
  3510. }
  3511. Y_UNIT_TEST(GroupByEmptyColumn) {
  3512. NYql::TAstParseResult res = SqlToYql("select count(1) from plato.Input group by ``;");
  3513. UNIT_ASSERT(!res.Root);
  3514. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:43: Error: Column name can not be empty\n");
  3515. }
  3516. Y_UNIT_TEST(ConvertNumberOutOfBase) {
  3517. NYql::TAstParseResult res = SqlToYql("select 0o80l;");
  3518. UNIT_ASSERT(!res.Root);
  3519. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:8: Error: Failed to parse number from string: 0o80l, char: '8' is out of base: 8\n");
  3520. }
  3521. Y_UNIT_TEST(ConvertNumberOutOfRangeForInt64ButFitsInUint64) {
  3522. NYql::TAstParseResult res = SqlToYql("select 0xc000000000000000l;");
  3523. UNIT_ASSERT(!res.Root);
  3524. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:8: Error: Failed to parse 13835058055282163712 as integer literal of Int64 type: value out of range for Int64\n");
  3525. }
  3526. Y_UNIT_TEST(ConvertNumberOutOfRangeUint64) {
  3527. NYql::TAstParseResult res = SqlToYql("select 0xc0000000000000000l;");
  3528. UNIT_ASSERT(!res.Root);
  3529. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:8: Error: Failed to parse number from string: 0xc0000000000000000l, number limit overflow\n");
  3530. res = SqlToYql("select 1234234543563435151456;\n");
  3531. UNIT_ASSERT(!res.Root);
  3532. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:8: Error: Failed to parse number from string: 1234234543563435151456, number limit overflow\n");
  3533. }
  3534. Y_UNIT_TEST(ConvertNumberNegativeOutOfRange) {
  3535. NYql::TAstParseResult res = SqlToYql("select -9223372036854775808;\n"
  3536. "select -9223372036854775809;");
  3537. UNIT_ASSERT(!res.Root);
  3538. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:2:8: Error: Failed to parse negative integer: -9223372036854775809, number limit overflow\n");
  3539. }
  3540. Y_UNIT_TEST(InvaildUsageReal0) {
  3541. NYql::TAstParseResult res = SqlToYql("select .0;");
  3542. UNIT_ASSERT(!res.Root);
  3543. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:7: Error: Unexpected token '.' : cannot match to any predicted input...\n\n");
  3544. }
  3545. Y_UNIT_TEST(InvaildUsageReal1) {
  3546. NYql::TAstParseResult res = SqlToYql("select .0f;");
  3547. UNIT_ASSERT(!res.Root);
  3548. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:7: Error: Unexpected token '.' : cannot match to any predicted input...\n\n");
  3549. }
  3550. Y_UNIT_TEST(InvaildUsageWinFunctionWithoutWindow) {
  3551. NYql::TAstParseResult res = SqlToYql("select lead(key, 2) from plato.Input;");
  3552. UNIT_ASSERT(!res.Root);
  3553. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:8: Error: Failed to use window function Lead without window specification\n");
  3554. }
  3555. Y_UNIT_TEST(DropTableWithIfExists) {
  3556. NYql::TAstParseResult res = SqlToYql("DROP TABLE IF EXISTS plato.foo;");
  3557. UNIT_ASSERT(res.Root);
  3558. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  3559. if (word == "Write") {
  3560. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("drop_if_exists"));
  3561. }
  3562. };
  3563. TWordCountHive elementStat = { {TString("Write"), 0}};
  3564. VerifyProgram(res, elementStat, verifyLine);
  3565. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  3566. }
  3567. Y_UNIT_TEST(TooManyErrors) {
  3568. const char* q = R"(
  3569. USE plato;
  3570. select A, B, C, D, E, F, G, H, I, J, K, L, M, N from (select b from `abc`);
  3571. )";
  3572. NYql::TAstParseResult res = SqlToYql(q, 10);
  3573. UNIT_ASSERT(!res.Root);
  3574. UNIT_ASSERT_NO_DIFF(Err2Str(res),
  3575. R"(<main>:3:16: Error: Column A is not in source column set. Did you mean b?
  3576. <main>:3:19: Error: Column B is not in source column set. Did you mean b?
  3577. <main>:3:22: Error: Column C is not in source column set. Did you mean b?
  3578. <main>:3:25: Error: Column D is not in source column set. Did you mean b?
  3579. <main>:3:28: Error: Column E is not in source column set. Did you mean b?
  3580. <main>:3:31: Error: Column F is not in source column set. Did you mean b?
  3581. <main>:3:34: Error: Column G is not in source column set. Did you mean b?
  3582. <main>:3:37: Error: Column H is not in source column set. Did you mean b?
  3583. <main>:3:40: Error: Column I is not in source column set. Did you mean b?
  3584. <main>: Error: Too many issues, code: 1
  3585. )");
  3586. };
  3587. Y_UNIT_TEST(ShouldCloneBindingForNamedParameter) {
  3588. NYql::TAstParseResult res = SqlToYql(R"($f = () -> {
  3589. $value_type = TypeOf(1);
  3590. $pair_type = StructType(
  3591. TypeOf("2") AS key,
  3592. $value_type AS value
  3593. );
  3594. RETURN TupleType(
  3595. ListType($value_type),
  3596. $pair_type);
  3597. };
  3598. select FormatType($f());
  3599. )");
  3600. UNIT_ASSERT(res.Root);
  3601. }
  3602. Y_UNIT_TEST(BlockedInvalidFrameBounds) {
  3603. auto check = [](const TString& frame, const TString& err) {
  3604. const TString prefix = "SELECT SUM(x) OVER w FROM plato.Input WINDOW w AS (PARTITION BY key ORDER BY subkey\n";
  3605. NYql::TAstParseResult res = SqlToYql(prefix + frame + ")");
  3606. UNIT_ASSERT(!res.Root);
  3607. UNIT_ASSERT_NO_DIFF(Err2Str(res), err);
  3608. };
  3609. check("ROWS UNBOUNDED FOLLOWING", "<main>:2:5: Error: Frame cannot start from UNBOUNDED FOLLOWING\n");
  3610. check("ROWS BETWEEN 5 PRECEDING AND UNBOUNDED PRECEDING", "<main>:2:29: Error: Frame cannot end with UNBOUNDED PRECEDING\n");
  3611. check("ROWS BETWEEN CURRENT ROW AND 5 PRECEDING", "<main>:2:13: Error: Frame cannot start from CURRENT ROW and end with PRECEDING\n");
  3612. check("ROWS BETWEEN 5 FOLLOWING AND CURRENT ROW", "<main>:2:14: Error: Frame cannot start from FOLLOWING and end with CURRENT ROW\n");
  3613. check("ROWS BETWEEN 5 FOLLOWING AND 5 PRECEDING", "<main>:2:14: Error: Frame cannot start from FOLLOWING and end with PRECEDING\n");
  3614. }
  3615. Y_UNIT_TEST(BlockedRangeValueWithoutSingleOrderBy) {
  3616. UNIT_ASSERT(SqlToYql("SELECT COUNT(*) OVER (RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) FROM plato.Input").IsOk());
  3617. UNIT_ASSERT(SqlToYql("SELECT COUNT(*) OVER (RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM plato.Input").IsOk());
  3618. auto res = SqlToYql("SELECT COUNT(*) OVER (RANGE 5 PRECEDING) FROM plato.Input");
  3619. UNIT_ASSERT(!res.Root);
  3620. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:29: Error: RANGE with <offset> PRECEDING/FOLLOWING requires exactly one expression in ORDER BY partition clause\n");
  3621. res = SqlToYql("SELECT COUNT(*) OVER (ORDER BY key, value RANGE 5 PRECEDING) FROM plato.Input");
  3622. UNIT_ASSERT(!res.Root);
  3623. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:49: Error: RANGE with <offset> PRECEDING/FOLLOWING requires exactly one expression in ORDER BY partition clause\n");
  3624. }
  3625. Y_UNIT_TEST(NoColumnsInFrameBounds) {
  3626. NYql::TAstParseResult res = SqlToYql(
  3627. "SELECT SUM(x) OVER w FROM plato.Input WINDOW w AS (ROWS BETWEEN\n"
  3628. " 1 + key PRECEDING AND 2 + key FOLLOWING);");
  3629. UNIT_ASSERT(!res.Root);
  3630. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:2:6: Error: Column reference \"key\" is not allowed in current scope\n");
  3631. }
  3632. Y_UNIT_TEST(WarnOnEmptyFrameBounds) {
  3633. NYql::TAstParseResult res = SqlToYql(
  3634. "SELECT SUM(x) OVER w FROM plato.Input WINDOW w AS (PARTITION BY key ORDER BY subkey\n"
  3635. "ROWS BETWEEN 10 FOLLOWING AND 5 FOLLOWING)");
  3636. UNIT_ASSERT(res.Root);
  3637. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:2:14: Warning: Used frame specification implies empty window frame, code: 4520\n");
  3638. }
  3639. Y_UNIT_TEST(WarnOnRankWithUnorderedWindow) {
  3640. NYql::TAstParseResult res = SqlToYql("SELECT RANK() OVER w FROM plato.Input WINDOW w AS ()");
  3641. UNIT_ASSERT(res.Root);
  3642. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:8: Warning: Rank() is used with unordered window - all rows will be considered equal to each other, code: 4521\n");
  3643. }
  3644. Y_UNIT_TEST(WarnOnRankExprWithUnorderedWindow) {
  3645. NYql::TAstParseResult res = SqlToYql("SELECT RANK(key) OVER w FROM plato.Input WINDOW w AS ()");
  3646. UNIT_ASSERT(res.Root);
  3647. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:8: Warning: Rank(<expression>) is used with unordered window - the result is likely to be undefined, code: 4521\n");
  3648. }
  3649. Y_UNIT_TEST(AnyAsTableName) {
  3650. NYql::TAstParseResult res = SqlToYql("use plato; select * from any;");
  3651. UNIT_ASSERT(!res.Root);
  3652. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:28: Error: Unexpected token ';' : syntax error...\n\n");
  3653. }
  3654. Y_UNIT_TEST(IncorrectOrderOfLambdaOptionalArgs) {
  3655. NYql::TAstParseResult res = SqlToYql("$f = ($x?, $y)->($x + $y); select $f(1);");
  3656. UNIT_ASSERT(!res.Root);
  3657. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:12: Error: Non-optional argument can not follow optional one\n");
  3658. }
  3659. Y_UNIT_TEST(IncorrectOrderOfActionOptionalArgs) {
  3660. NYql::TAstParseResult res = SqlToYql("define action $f($x?, $y) as select $x,$y; end define; do $f(1);");
  3661. UNIT_ASSERT(!res.Root);
  3662. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:23: Error: Non-optional argument can not follow optional one\n");
  3663. }
  3664. Y_UNIT_TEST(NotAllowedQuestionOnNamedNode) {
  3665. NYql::TAstParseResult res = SqlToYql("$f = 1; select $f?;");
  3666. UNIT_ASSERT(!res.Root);
  3667. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:18: Error: Unexpected token '?' at the end of expression\n");
  3668. }
  3669. Y_UNIT_TEST(AnyAndCrossJoin) {
  3670. NYql::TAstParseResult res = SqlToYql("use plato; select * from any Input1 cross join Input2");
  3671. UNIT_ASSERT(!res.Root);
  3672. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:26: Error: ANY should not be used with Cross JOIN\n");
  3673. res = SqlToYql("use plato; select * from Input1 cross join any Input2");
  3674. UNIT_ASSERT(!res.Root);
  3675. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:44: Error: ANY should not be used with Cross JOIN\n");
  3676. }
  3677. Y_UNIT_TEST(AnyWithCartesianProduct) {
  3678. NYql::TAstParseResult res = SqlToYql("pragma AnsiImplicitCrossJoin; use plato; select * from any Input1, Input2");
  3679. UNIT_ASSERT(!res.Root);
  3680. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:56: Error: ANY should not be used with Cross JOIN\n");
  3681. res = SqlToYql("pragma AnsiImplicitCrossJoin; use plato; select * from Input1, any Input2");
  3682. UNIT_ASSERT(!res.Root);
  3683. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:64: Error: ANY should not be used with Cross JOIN\n");
  3684. }
  3685. Y_UNIT_TEST(ErrorPlainEndAsInlineActionTerminator) {
  3686. NYql::TAstParseResult res = SqlToYql(
  3687. "do begin\n"
  3688. " select 1\n"
  3689. "; end\n");
  3690. UNIT_ASSERT(!res.Root);
  3691. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:4:0: Error: Unexpected token absence : Missing DO \n\n");
  3692. }
  3693. Y_UNIT_TEST(ErrorMultiWayJoinWithUsing) {
  3694. NYql::TAstParseResult res = SqlToYql(
  3695. "USE plato;\n"
  3696. "PRAGMA DisableSimpleColumns;\n"
  3697. "SELECT *\n"
  3698. "FROM Input1 AS a\n"
  3699. "JOIN Input2 AS b USING(key)\n"
  3700. "JOIN Input3 AS c ON a.key = c.key;\n"
  3701. );
  3702. UNIT_ASSERT(!res.Root);
  3703. UNIT_ASSERT_NO_DIFF(Err2Str(res),
  3704. "<main>:5:24: Error: Multi-way JOINs should be connected with ON clause instead of USING clause\n"
  3705. );
  3706. }
  3707. Y_UNIT_TEST(RequireLabelInFlattenByWithDot) {
  3708. NYql::TAstParseResult res = SqlToYql("select * from plato.Input flatten by x.y");
  3709. UNIT_ASSERT(!res.Root);
  3710. UNIT_ASSERT_NO_DIFF(Err2Str(res),
  3711. "<main>:1:40: Error: Unnamed expression after FLATTEN BY is not allowed\n"
  3712. );
  3713. }
  3714. Y_UNIT_TEST(WarnUnnamedColumns) {
  3715. NYql::TAstParseResult res = SqlToYql(
  3716. "PRAGMA WarnUnnamedColumns;\n"
  3717. "\n"
  3718. "SELECT key, subkey, key || subkey FROM plato.Input ORDER BY subkey;\n");
  3719. UNIT_ASSERT(res.Root);
  3720. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:3:28: Warning: Autogenerated column name column2 will be used for expression, code: 4516\n");
  3721. }
  3722. Y_UNIT_TEST(WarnSourceColumnMismatch) {
  3723. NYql::TAstParseResult res = SqlToYql(
  3724. "insert into plato.Output (key, subkey, new_value, one_more_value) select key as Key, subkey, value, \"x\" from plato.Input;");
  3725. UNIT_ASSERT(res.Root);
  3726. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:51: Warning: Column names in SELECT don't match column specification in parenthesis. \"key\" doesn't match \"Key\". \"new_value\" doesn't match \"value\", code: 4517\n");
  3727. }
  3728. Y_UNIT_TEST(YtCaseInsensitive) {
  3729. NYql::TAstParseResult res = SqlToYql("select * from PlatO.foo;");
  3730. UNIT_ASSERT(res.Root);
  3731. res = SqlToYql("use PlatO; select * from foo;");
  3732. UNIT_ASSERT(res.Root);
  3733. }
  3734. Y_UNIT_TEST(KikimrCaseSensitive) {
  3735. NYql::TAstParseResult res = SqlToYql("select * from PlatO.foo;", 10, "kikimr");
  3736. UNIT_ASSERT(!res.Root);
  3737. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:15: Error: Unknown cluster: PlatO\n");
  3738. res = SqlToYql("use PlatO; select * from foo;", 10, "kikimr");
  3739. UNIT_ASSERT(!res.Root);
  3740. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:5: Error: Unknown cluster: PlatO\n");
  3741. }
  3742. Y_UNIT_TEST(DiscoveryModeForbidden) {
  3743. NYql::TAstParseResult res = SqlToYqlWithMode("insert into plato.Output select * from plato.range(\"\", Input1, Input4)", NSQLTranslation::ESqlMode::DISCOVERY);
  3744. UNIT_ASSERT(!res.Root);
  3745. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:40: Error: range is not allowed in Discovery mode, code: 4600\n");
  3746. res = SqlToYqlWithMode("insert into plato.Output select * from plato.like(\"\", \"Input%\")", NSQLTranslation::ESqlMode::DISCOVERY);
  3747. UNIT_ASSERT(!res.Root);
  3748. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:40: Error: like is not allowed in Discovery mode, code: 4600\n");
  3749. res = SqlToYqlWithMode("insert into plato.Output select * from plato.regexp(\"\", \"Input.\")", NSQLTranslation::ESqlMode::DISCOVERY);
  3750. UNIT_ASSERT(!res.Root);
  3751. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:40: Error: regexp is not allowed in Discovery mode, code: 4600\n");
  3752. res = SqlToYqlWithMode("insert into plato.Output select * from plato.filter(\"\", ($name) -> { return find($name, \"Input\") is not null; })", NSQLTranslation::ESqlMode::DISCOVERY);
  3753. UNIT_ASSERT(!res.Root);
  3754. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:40: Error: filter is not allowed in Discovery mode, code: 4600\n");
  3755. res = SqlToYqlWithMode("select Path from plato.folder(\"\") where Type == \"table\"", NSQLTranslation::ESqlMode::DISCOVERY);
  3756. UNIT_ASSERT(!res.Root);
  3757. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:18: Error: folder is not allowed in Discovery mode, code: 4600\n");
  3758. }
  3759. Y_UNIT_TEST(YsonFuncWithoutArgs) {
  3760. UNIT_ASSERT(SqlToYql("SELECT Yson::SerializeText(Yson::From());").IsOk());
  3761. }
  3762. Y_UNIT_TEST(CanNotUseOrderByInNonLastSelectInUnionAllChain) {
  3763. auto req = "pragma AnsiOrderByLimitInUnionAll;\n"
  3764. "use plato;\n"
  3765. "\n"
  3766. "select * from Input order by key\n"
  3767. "union all\n"
  3768. "select * from Input order by key limit 1;";
  3769. auto res = SqlToYql(req);
  3770. UNIT_ASSERT(!res.Root);
  3771. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:4:21: Error: ORDER BY within UNION ALL is only allowed after last subquery\n");
  3772. }
  3773. Y_UNIT_TEST(CanNotUseLimitInNonLastSelectInUnionAllChain) {
  3774. auto req = "pragma AnsiOrderByLimitInUnionAll;\n"
  3775. "use plato;\n"
  3776. "\n"
  3777. "select * from Input limit 1\n"
  3778. "union all\n"
  3779. "select * from Input order by key limit 1;";
  3780. auto res = SqlToYql(req);
  3781. UNIT_ASSERT(!res.Root);
  3782. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:4:21: Error: LIMIT within UNION ALL is only allowed after last subquery\n");
  3783. }
  3784. Y_UNIT_TEST(CanNotUseDiscardInNonFirstSelectInUnionAllChain) {
  3785. auto req = "pragma AnsiOrderByLimitInUnionAll;\n"
  3786. "use plato;\n"
  3787. "\n"
  3788. "select * from Input\n"
  3789. "union all\n"
  3790. "discard select * from Input;";
  3791. auto res = SqlToYql(req);
  3792. UNIT_ASSERT(!res.Root);
  3793. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:6:1: Error: DISCARD within UNION ALL is only allowed before first subquery\n");
  3794. }
  3795. Y_UNIT_TEST(CanNotUseIntoResultInNonLastSelectInUnionAllChain) {
  3796. auto req = "use plato;\n"
  3797. "pragma AnsiOrderByLimitInUnionAll;\n"
  3798. "\n"
  3799. "select * from Input\n"
  3800. "union all\n"
  3801. "discard select * from Input;";
  3802. auto res = SqlToYql(req);
  3803. UNIT_ASSERT(!res.Root);
  3804. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:6:1: Error: DISCARD within UNION ALL is only allowed before first subquery\n");
  3805. }
  3806. Y_UNIT_TEST(YsonStrictInvalidPragma) {
  3807. auto res = SqlToYql("pragma yson.Strict = \"wrong\";");
  3808. UNIT_ASSERT(!res.Root);
  3809. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:22: Error: Expected 'true', 'false' or no parameter for: Strict\n");
  3810. }
  3811. Y_UNIT_TEST(WarnTableNameInSomeContexts) {
  3812. UNIT_ASSERT(SqlToYql("use plato; select TableName() from Input;").IsOk());
  3813. UNIT_ASSERT(SqlToYql("use plato; select TableName(\"aaaa\");").IsOk());
  3814. UNIT_ASSERT(SqlToYql("select TableName(\"aaaa\", \"yt\");").IsOk());
  3815. auto res = SqlToYql("select TableName() from plato.Input;");
  3816. UNIT_ASSERT(!res.Root);
  3817. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:8: Error: TableName requires either service name as second argument or current cluster name\n");
  3818. res = SqlToYql("use plato;\n"
  3819. "select TableName() from Input1 as a join Input2 as b using(key);");
  3820. UNIT_ASSERT(res.Root);
  3821. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:2:8: Warning: TableName() may produce empty result when used in ambiguous context (with JOIN), code: 4525\n");
  3822. res = SqlToYql("use plato;\n"
  3823. "select SOME(TableName()), key from Input group by key;");
  3824. UNIT_ASSERT(res.Root);
  3825. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:2:13: Warning: TableName() will produce empty result when used with aggregation.\n"
  3826. "Please consult documentation for possible workaround, code: 4525\n");
  3827. }
  3828. Y_UNIT_TEST(WarnOnDistincWithHavingWithoutAggregations) {
  3829. auto res = SqlToYql("select distinct key from plato.Input having key != '0';");
  3830. UNIT_ASSERT(res.Root);
  3831. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:49: Warning: The usage of HAVING without aggregations with SELECT DISTINCT is non-standard and will stop working soon. Please use WHERE instead., code: 4526\n");
  3832. }
  3833. Y_UNIT_TEST(FlattenByExprWithNestedNull) {
  3834. auto res = SqlToYql("USE plato;\n"
  3835. "\n"
  3836. "SELECT * FROM (SELECT 1 AS region_id)\n"
  3837. "FLATTEN BY (\n"
  3838. " CAST($unknown(region_id) AS List<String>) AS region\n"
  3839. ")");
  3840. UNIT_ASSERT(!res.Root);
  3841. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:5:10: Error: Unknown name: $unknown\n");
  3842. }
  3843. Y_UNIT_TEST(EmptySymbolNameIsForbidden) {
  3844. auto req = " $`` = 1; select $``;";
  3845. auto res = SqlToYql(req);
  3846. UNIT_ASSERT(!res.Root);
  3847. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:5: Error: Empty symbol name is not allowed\n");
  3848. }
  3849. Y_UNIT_TEST(WarnOnBinaryOpWithNullArg) {
  3850. auto req = "select * from plato.Input where cast(key as Int32) != NULL";
  3851. auto res = SqlToYql(req);
  3852. UNIT_ASSERT(res.Root);
  3853. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:52: Warning: Binary operation != will return NULL here, code: 4529\n");
  3854. req = "select 1 or null";
  3855. res = SqlToYql(req);
  3856. UNIT_ASSERT(res.Root);
  3857. UNIT_ASSERT_NO_DIFF(Err2Str(res), "");
  3858. }
  3859. Y_UNIT_TEST(ErrorIfTableSampleArgUsesColumns) {
  3860. auto req = "SELECT key FROM plato.Input TABLESAMPLE BERNOULLI(MIN_OF(100.0, CAST(subkey as Int32)));";
  3861. auto res = SqlToYql(req);
  3862. UNIT_ASSERT(!res.Root);
  3863. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:70: Error: Column reference \"subkey\" is not allowed in current scope\n");
  3864. }
  3865. Y_UNIT_TEST(DerivedColumnListForSelectIsNotSupportedYet) {
  3866. auto req = "SELECT a,b,c FROM plato.Input as t(x,y,z);";
  3867. auto res = SqlToYql(req);
  3868. UNIT_ASSERT(!res.Root);
  3869. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:35: Error: Derived column list is only supported for VALUES\n");
  3870. }
  3871. Y_UNIT_TEST(ErrorIfValuesHasDifferentCountOfColumns) {
  3872. auto req = "VALUES (1,2,3), (4,5);";
  3873. auto res = SqlToYql(req);
  3874. UNIT_ASSERT(!res.Root);
  3875. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:17: Error: All VALUES items should have same size: expecting 3, got 2\n");
  3876. }
  3877. Y_UNIT_TEST(ErrorIfDerivedColumnSizeExceedValuesColumnCount) {
  3878. auto req = "SELECT * FROM(VALUES (1,2), (3,4)) as t(x,y,z);";
  3879. auto res = SqlToYql(req);
  3880. UNIT_ASSERT(!res.Root);
  3881. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:40: Error: Derived column list size exceeds column count in VALUES\n");
  3882. }
  3883. Y_UNIT_TEST(WarnoOnAutogeneratedNamesForValues) {
  3884. auto req = "PRAGMA WarnUnnamedColumns;\n"
  3885. "SELECT * FROM (VALUES (1,2,3,4), (5,6,7,8)) as t(x,y);";
  3886. auto res = SqlToYql(req);
  3887. UNIT_ASSERT(res.Root);
  3888. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:2:16: Warning: Autogenerated column names column2...column3 will be used here, code: 4516\n");
  3889. }
  3890. Y_UNIT_TEST(ErrUnionAllWithOrderByWithoutExplicitLegacyMode) {
  3891. auto req = "use plato;\n"
  3892. "\n"
  3893. "select * from Input order by key\n"
  3894. "union all\n"
  3895. "select * from Input order by key;";
  3896. auto res = SqlToYql(req);
  3897. UNIT_ASSERT(!res.Root);
  3898. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:3:21: Error: ORDER BY within UNION ALL is only allowed after last subquery\n");
  3899. }
  3900. Y_UNIT_TEST(ErrUnionAllWithLimitWithoutExplicitLegacyMode) {
  3901. auto req = "use plato;\n"
  3902. "\n"
  3903. "select * from Input limit 10\n"
  3904. "union all\n"
  3905. "select * from Input limit 1;";
  3906. auto res = SqlToYql(req);
  3907. UNIT_ASSERT(!res.Root);
  3908. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:3:21: Error: LIMIT within UNION ALL is only allowed after last subquery\n");
  3909. }
  3910. Y_UNIT_TEST(ErrUnionAllWithIntoResultWithoutExplicitLegacyMode) {
  3911. auto req = "use plato;\n"
  3912. "\n"
  3913. "select * from Input into result aaa\n"
  3914. "union all\n"
  3915. "select * from Input;";
  3916. auto res = SqlToYql(req);
  3917. UNIT_ASSERT(!res.Root);
  3918. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:3:21: Error: INTO RESULT within UNION ALL is only allowed after last subquery\n");
  3919. }
  3920. Y_UNIT_TEST(ErrUnionAllWithDiscardWithoutExplicitLegacyMode) {
  3921. auto req = "use plato;\n"
  3922. "\n"
  3923. "select * from Input\n"
  3924. "union all\n"
  3925. "discard select * from Input;";
  3926. auto res = SqlToYql(req);
  3927. UNIT_ASSERT(!res.Root);
  3928. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:5:1: Error: DISCARD within UNION ALL is only allowed before first subquery\n");
  3929. }
  3930. Y_UNIT_TEST(ErrUnionAllKeepsIgnoredOrderByWarning) {
  3931. auto req = "use plato;\n"
  3932. "\n"
  3933. "SELECT * FROM (\n"
  3934. " SELECT * FROM Input\n"
  3935. " UNION ALL\n"
  3936. " SELECT t.* FROM Input AS t ORDER BY t.key\n"
  3937. ");";
  3938. auto res = SqlToYql(req);
  3939. UNIT_ASSERT(!res.Root);
  3940. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:4:3: Warning: ORDER BY without LIMIT in subquery will be ignored, code: 4504\n"
  3941. "<main>:6:39: Error: Unknown correlation name: t\n");
  3942. }
  3943. Y_UNIT_TEST(ErrOrderByIgnoredButCheckedForMissingColumns) {
  3944. auto req = "$src = SELECT key FROM (SELECT 1 as key, 2 as subkey) ORDER BY x; SELECT * FROM $src;";
  3945. ExpectFailWithError(req, "<main>:1:8: Warning: ORDER BY without LIMIT in subquery will be ignored, code: 4504\n"
  3946. "<main>:1:64: Error: Column x is not in source column set\n");
  3947. req = "$src = SELECT key FROM plato.Input ORDER BY x; SELECT * FROM $src;";
  3948. auto res = SqlToYql(req);
  3949. UNIT_ASSERT(res.Root);
  3950. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:8: Warning: ORDER BY without LIMIT in subquery will be ignored, code: 4504\n");
  3951. }
  3952. Y_UNIT_TEST(InvalidTtlInterval) {
  3953. auto req = R"(
  3954. USE plato;
  3955. CREATE TABLE tableName (Key Uint32, CreatedAt Timestamp, PRIMARY KEY (Key))
  3956. WITH (TTL = 1 On CreatedAt);
  3957. )";
  3958. auto res = SqlToYql(req);
  3959. UNIT_ASSERT(!res.Root);
  3960. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:4:25: Error: Literal of Interval type is expected for TTL\n"
  3961. "<main>:4:25: Error: Invalid TTL settings\n");
  3962. }
  3963. Y_UNIT_TEST(InvalidTtlUnit) {
  3964. auto req = R"(
  3965. USE plato;
  3966. CREATE TABLE tableName (Key Uint32, CreatedAt Uint32, PRIMARY KEY (Key))
  3967. WITH (TTL = Interval("P1D") On CreatedAt AS PICOSECONDS);
  3968. )";
  3969. auto res = SqlToYql(req);
  3970. UNIT_ASSERT(!res.Root);
  3971. UNIT_ASSERT_STRING_CONTAINS(Err2Str(res), "<main>:4:56: Error: Unexpected token 'PICOSECONDS'");
  3972. }
  3973. Y_UNIT_TEST(InvalidChangefeedSink) {
  3974. auto req = R"(
  3975. USE plato;
  3976. CREATE TABLE tableName (
  3977. Key Uint32, PRIMARY KEY (Key),
  3978. CHANGEFEED feedName WITH (SINK_TYPE = "S3", MODE = "KEYS_ONLY", FORMAT = "json")
  3979. );
  3980. )";
  3981. auto res = SqlToYql(req);
  3982. UNIT_ASSERT(!res.Root);
  3983. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:5:55: Error: Unknown changefeed sink type: S3\n");
  3984. }
  3985. Y_UNIT_TEST(InvalidChangefeedSettings) {
  3986. auto req = R"(
  3987. USE plato;
  3988. CREATE TABLE tableName (
  3989. Key Uint32, PRIMARY KEY (Key),
  3990. CHANGEFEED feedName WITH (SINK_TYPE = "local", FOO = "bar")
  3991. );
  3992. )";
  3993. auto res = SqlToYql(req);
  3994. UNIT_ASSERT(!res.Root);
  3995. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:5:64: Error: Unknown changefeed setting: FOO\n");
  3996. }
  3997. Y_UNIT_TEST(InvalidChangefeedInitialScan) {
  3998. auto req = R"(
  3999. USE plato;
  4000. CREATE TABLE tableName (
  4001. Key Uint32, PRIMARY KEY (Key),
  4002. CHANGEFEED feedName WITH (MODE = "KEYS_ONLY", FORMAT = "json", INITIAL_SCAN = "foo")
  4003. );
  4004. )";
  4005. auto res = SqlToYql(req);
  4006. UNIT_ASSERT(!res.Root);
  4007. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:5:95: Error: Literal of Bool type is expected for INITIAL_SCAN\n");
  4008. }
  4009. Y_UNIT_TEST(InvalidChangefeedVirtualTimestamps) {
  4010. auto req = R"(
  4011. USE plato;
  4012. CREATE TABLE tableName (
  4013. Key Uint32, PRIMARY KEY (Key),
  4014. CHANGEFEED feedName WITH (MODE = "KEYS_ONLY", FORMAT = "json", VIRTUAL_TIMESTAMPS = "foo")
  4015. );
  4016. )";
  4017. auto res = SqlToYql(req);
  4018. UNIT_ASSERT(!res.Root);
  4019. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:5:101: Error: Literal of Bool type is expected for VIRTUAL_TIMESTAMPS\n");
  4020. }
  4021. Y_UNIT_TEST(InvalidChangefeedResolvedTimestamps) {
  4022. auto req = R"(
  4023. USE plato;
  4024. CREATE TABLE tableName (
  4025. Key Uint32, PRIMARY KEY (Key),
  4026. CHANGEFEED feedName WITH (MODE = "KEYS_ONLY", FORMAT = "json", BARRIERS_INTERVAL = "foo")
  4027. );
  4028. )";
  4029. auto res = SqlToYql(req);
  4030. UNIT_ASSERT(!res.Root);
  4031. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:5:100: Error: Literal of Interval type is expected for BARRIERS_INTERVAL\n");
  4032. }
  4033. Y_UNIT_TEST(InvalidChangefeedRetentionPeriod) {
  4034. auto req = R"(
  4035. USE plato;
  4036. CREATE TABLE tableName (
  4037. Key Uint32, PRIMARY KEY (Key),
  4038. CHANGEFEED feedName WITH (MODE = "KEYS_ONLY", FORMAT = "json", RETENTION_PERIOD = "foo")
  4039. );
  4040. )";
  4041. auto res = SqlToYql(req);
  4042. UNIT_ASSERT(!res.Root);
  4043. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:5:99: Error: Literal of Interval type is expected for RETENTION_PERIOD\n");
  4044. }
  4045. Y_UNIT_TEST(InvalidChangefeedTopicPartitions) {
  4046. auto req = R"(
  4047. USE plato;
  4048. CREATE TABLE tableName (
  4049. Key Uint32, PRIMARY KEY (Key),
  4050. CHANGEFEED feedName WITH (MODE = "KEYS_ONLY", FORMAT = "json", TOPIC_MIN_ACTIVE_PARTITIONS = "foo")
  4051. );
  4052. )";
  4053. auto res = SqlToYql(req);
  4054. UNIT_ASSERT(!res.Root);
  4055. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:5:110: Error: Literal of integer type is expected for TOPIC_MIN_ACTIVE_PARTITIONS\n");
  4056. }
  4057. Y_UNIT_TEST(InvalidChangefeedAwsRegion) {
  4058. auto req = R"(
  4059. USE plato;
  4060. CREATE TABLE tableName (
  4061. Key Uint32, PRIMARY KEY (Key),
  4062. CHANGEFEED feedName WITH (MODE = "KEYS_ONLY", FORMAT = "json", AWS_REGION = true)
  4063. );
  4064. )";
  4065. auto res = SqlToYql(req);
  4066. UNIT_ASSERT(!res.Root);
  4067. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:5:93: Error: Literal of String type is expected for AWS_REGION\n");
  4068. }
  4069. Y_UNIT_TEST(ErrJoinWithGroupingSetsWithoutCorrelationName) {
  4070. auto req = "USE plato;\n"
  4071. "\n"
  4072. "SELECT k1, k2, subkey\n"
  4073. "FROM T1 AS a JOIN T2 AS b USING (key)\n"
  4074. "GROUP BY GROUPING SETS(\n"
  4075. " (a.key as k1, b.subkey as k2),\n"
  4076. " (k1),\n"
  4077. " (subkey)\n"
  4078. ");";
  4079. auto res = SqlToYql(req);
  4080. UNIT_ASSERT(!res.Root);
  4081. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:8:4: Error: Columns in grouping sets should have correlation name, error in key: subkey\n");
  4082. }
  4083. Y_UNIT_TEST(ErrJoinWithGroupByWithoutCorrelationName) {
  4084. auto req = "USE plato;\n"
  4085. "\n"
  4086. "SELECT k1, k2,\n"
  4087. " value\n"
  4088. "FROM T1 AS a JOIN T2 AS b USING (key)\n"
  4089. "GROUP BY a.key as k1, b.subkey as k2,\n"
  4090. " value;";
  4091. ExpectFailWithError(req,
  4092. "<main>:7:5: Error: Columns in GROUP BY should have correlation name, error in key: value\n");
  4093. }
  4094. Y_UNIT_TEST(ErrWithMissingFrom) {
  4095. auto req = "select 1 as key where 1 > 1;";
  4096. auto res = SqlToYql(req);
  4097. UNIT_ASSERT(!res.Root);
  4098. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:25: Error: Filtering is not allowed without FROM\n");
  4099. req = "select 1 + count(*);";
  4100. res = SqlToYql(req);
  4101. UNIT_ASSERT(!res.Root);
  4102. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:12: Error: Aggregation is not allowed without FROM\n");
  4103. req = "select 1 as key, subkey + value;";
  4104. res = SqlToYql(req);
  4105. UNIT_ASSERT(!res.Root);
  4106. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:1: Error: Column references are not allowed without FROM\n"
  4107. "<main>:1:18: Error: Column reference 'subkey'\n"
  4108. "<main>:1:1: Error: Column references are not allowed without FROM\n"
  4109. "<main>:1:27: Error: Column reference 'value'\n");
  4110. req = "select count(1) group by key;";
  4111. res = SqlToYql(req);
  4112. UNIT_ASSERT(!res.Root);
  4113. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:1: Error: Column references are not allowed without FROM\n"
  4114. "<main>:1:26: Error: Column reference 'key'\n");
  4115. }
  4116. Y_UNIT_TEST(ErrWithMissingFromForWindow) {
  4117. auto req = "$c = () -> (1 + count(1) over w);\n"
  4118. "select $c();";
  4119. ExpectFailWithError(req,
  4120. "<main>:1:9: Error: Window and aggregation functions are not allowed in this context\n"
  4121. "<main>:1:17: Error: Failed to use aggregation function Count without window specification or in wrong place\n");
  4122. req = "$c = () -> (1 + lead(1) over w);\n"
  4123. "select $c();";
  4124. ExpectFailWithError(req,
  4125. "<main>:1:17: Error: Window functions are not allowed in this context\n"
  4126. "<main>:1:17: Error: Failed to use window function Lead without window specification or in wrong place\n");
  4127. req = "select 1 + count(1) over w window w as ();";
  4128. ExpectFailWithError(req,
  4129. "<main>:1:1: Error: Window and aggregation functions are not allowed without FROM\n"
  4130. "<main>:1:12: Error: Failed to use aggregation function Count without window specification or in wrong place\n");
  4131. req = "select 1 + lead(1) over w window w as ();";
  4132. ExpectFailWithError(req,
  4133. "<main>:1:12: Error: Window functions are not allowed without FROM\n"
  4134. "<main>:1:12: Error: Failed to use window function Lead without window specification or in wrong place\n");
  4135. }
  4136. Y_UNIT_TEST(ErrWithMissingFromForInplaceWindow) {
  4137. auto req = "$c = () -> (1 + count(1) over ());\n"
  4138. "select $c();";
  4139. ExpectFailWithError(req,
  4140. "<main>:1:26: Error: Window and aggregation functions are not allowed in this context\n");
  4141. req = "$c = () -> (1 + lead(1) over (rows between unbounded preceding and current row));\n"
  4142. "select $c();";
  4143. ExpectFailWithError(req,
  4144. "<main>:1:25: Error: Window and aggregation functions are not allowed in this context\n");
  4145. req = "select 1 + count(1) over ();";
  4146. ExpectFailWithError(req,
  4147. "<main>:1:1: Error: Window and aggregation functions are not allowed without FROM\n"
  4148. "<main>:1:12: Error: Failed to use aggregation function Count without window specification or in wrong place\n");
  4149. req = "select 1 + lead(1) over (rows between current row and unbounded following);";
  4150. ExpectFailWithError(req,
  4151. "<main>:1:12: Error: Window functions are not allowed without FROM\n"
  4152. "<main>:1:12: Error: Failed to use window function Lead without window specification or in wrong place\n");
  4153. }
  4154. Y_UNIT_TEST(ErrDistinctInWrongPlace) {
  4155. auto req = "select Some::Udf(distinct key) from plato.Input;";
  4156. ExpectFailWithError(req,
  4157. "<main>:1:18: Error: DISTINCT can only be used in aggregation functions\n");
  4158. req = "select sum(key)(distinct foo) from plato.Input;";
  4159. ExpectFailWithError(req,
  4160. "<main>:1:17: Error: DISTINCT can only be used in aggregation functions\n");
  4161. req = "select len(distinct foo) from plato.Input;";
  4162. ExpectFailWithError(req,
  4163. "<main>:1:8: Error: DISTINCT can only be used in aggregation functions\n");
  4164. req = "$foo = ($x) -> ($x); select $foo(distinct key) from plato.Input;";
  4165. ExpectFailWithError(req,
  4166. "<main>:1:34: Error: DISTINCT can only be used in aggregation functions\n");
  4167. }
  4168. Y_UNIT_TEST(ErrForNotSingleChildInInlineAST) {
  4169. ExpectFailWithError("select YQL::\"\"",
  4170. "<main>:1:8: Error: Failed to parse YQL: expecting AST root node with single child, but got 0\n");
  4171. ExpectFailWithError("select YQL::@@ \t@@",
  4172. "<main>:1:8: Error: Failed to parse YQL: expecting AST root node with single child, but got 0\n");
  4173. auto req = "$lambda = YQL::@@(lambda '(x)(+ x x)) (lambda '(y)(+ y y))@@;\n"
  4174. "select ListMap([1, 2, 3], $lambda);";
  4175. ExpectFailWithError(req,
  4176. "<main>:1:11: Error: Failed to parse YQL: expecting AST root node with single child, but got 2\n");
  4177. }
  4178. Y_UNIT_TEST(ErrEmptyColumnName) {
  4179. ExpectFailWithError("select * without \"\" from plato.Input",
  4180. "<main>:1:18: Error: String literal can not be used here\n");
  4181. ExpectFailWithError("select * without `` from plato.Input;",
  4182. "<main>:1:18: Error: Empty column name is not allowed\n");
  4183. ExpectFailWithErrorForAnsiLexer("select * without \"\" from plato.Input",
  4184. "<main>:1:18: Error: Empty column name is not allowed\n");
  4185. ExpectFailWithErrorForAnsiLexer("select * without `` from plato.Input;",
  4186. "<main>:1:18: Error: Empty column name is not allowed\n");
  4187. }
  4188. Y_UNIT_TEST(ErrOnNonZeroArgumentsForTableRows) {
  4189. ExpectFailWithError("$udf=\"\";process plato.Input using $udf(TableRows(k))",
  4190. "<main>:1:40: Error: TableRows requires exactly 0 arguments\n");
  4191. }
  4192. Y_UNIT_TEST(ErrGroupByWithAggregationFunctionAndDistinctExpr) {
  4193. ExpectFailWithError("select * from plato.Input group by count(distinct key|key)",
  4194. "<main>:1:36: Error: Unable to GROUP BY aggregated values\n");
  4195. }
  4196. // FIXME: check if we can get old behaviour
  4197. #if 0
  4198. Y_UNIT_TEST(ErrWithSchemaWithColumnsWithoutType) {
  4199. ExpectFailWithError("select * from plato.Input with COLUMNs",
  4200. "<main>:1:32: Error: Expected type after COLUMNS\n"
  4201. "<main>:1:32: Error: Failed to parse table hints\n");
  4202. ExpectFailWithError("select * from plato.Input with scheMa",
  4203. "<main>:1:32: Error: Expected type after SCHEMA\n"
  4204. "<main>:1:32: Error: Failed to parse table hints\n");
  4205. }
  4206. #endif
  4207. Y_UNIT_TEST(ErrCollectPreaggregatedInListLiteralWithoutFrom) {
  4208. ExpectFailWithError("SELECT([VARIANCE(DISTINCT[])])",
  4209. "<main>:1:1: Error: Column references are not allowed without FROM\n"
  4210. "<main>:1:9: Error: Column reference '_yql_preagg_Variance0'\n");
  4211. }
  4212. Y_UNIT_TEST(ErrGroupBySmartParenAsTuple) {
  4213. ExpectFailWithError("SELECT * FROM plato.Input GROUP BY (k, v,)",
  4214. "<main>:1:41: Error: Unexpected trailing comma in grouping elements list\n");
  4215. }
  4216. Y_UNIT_TEST(HandleNestedSmartParensInGroupBy) {
  4217. ExpectFailWithError("SELECT * FROM plato.Input GROUP BY (+() as k)",
  4218. "<main>:1:37: Error: Unable to GROUP BY constant expression\n");
  4219. }
  4220. Y_UNIT_TEST(ErrRenameWithAddColumn) {
  4221. ExpectFailWithError("USE plato; ALTER TABLE table RENAME TO moved, ADD COLUMN addc uint64",
  4222. "<main>:1:40: Error: RENAME TO can not be used together with another table action\n");
  4223. }
  4224. Y_UNIT_TEST(ErrAddColumnAndRename) {
  4225. // FIXME: fix positions in ALTER TABLE
  4226. ExpectFailWithError("USE plato; ALTER TABLE table ADD COLUMN addc uint64, RENAME TO moved",
  4227. "<main>:1:46: Error: RENAME TO can not be used together with another table action\n");
  4228. }
  4229. Y_UNIT_TEST(InvalidUuidValue) {
  4230. ExpectFailWithError("SELECT Uuid('123e4567ae89ba12d3aa456a426614174ab0')",
  4231. "<main>:1:8: Error: Invalid value \"123e4567ae89ba12d3aa456a426614174ab0\" for type Uuid\n");
  4232. ExpectFailWithError("SELECT Uuid('123e4567ae89b-12d3-a456-426614174000')",
  4233. "<main>:1:8: Error: Invalid value \"123e4567ae89b-12d3-a456-426614174000\" for type Uuid\n");
  4234. }
  4235. Y_UNIT_TEST(WindowFunctionWithoutOver) {
  4236. ExpectFailWithError("SELECT LAST_VALUE(foo) FROM plato.Input",
  4237. "<main>:1:8: Error: Can't use window function LastValue without window specification (OVER keyword is missing)\n");
  4238. ExpectFailWithError("SELECT LAST_VALUE(foo) FROM plato.Input GROUP BY key",
  4239. "<main>:1:8: Error: Can't use window function LastValue without window specification (OVER keyword is missing)\n");
  4240. }
  4241. Y_UNIT_TEST(CreateAlterUserWithLoginNoLogin) {
  4242. auto reqCreateUser = SqlToYql(R"(
  4243. USE plato;
  4244. CREATE USER user1;
  4245. )");
  4246. UNIT_ASSERT(reqCreateUser.IsOk());
  4247. auto reqAlterUser = SqlToYql(R"(
  4248. USE plato;
  4249. ALTER USER user1;
  4250. )");
  4251. UNIT_ASSERT(!reqAlterUser.IsOk());
  4252. UNIT_ASSERT_STRING_CONTAINS(reqAlterUser.Issues.ToString(), "Error: Unexpected token ';' : cannot match to any predicted input...");
  4253. auto reqPasswordAndLogin = SqlToYql(R"(
  4254. USE plato;
  4255. CREATE USER user1 PASSWORD '123' LOGIN;
  4256. )");
  4257. UNIT_ASSERT(reqPasswordAndLogin.IsOk());
  4258. auto reqPasswordAndNoLogin = SqlToYql(R"(
  4259. USE plato;
  4260. CREATE USER user1 PASSWORD '123' NOLOGIN;
  4261. )");
  4262. UNIT_ASSERT(reqPasswordAndNoLogin.IsOk());
  4263. auto reqLogin = SqlToYql(R"(
  4264. USE plato;
  4265. CREATE USER user1 LOGIN;
  4266. )");
  4267. UNIT_ASSERT(reqLogin.IsOk());
  4268. auto reqNoLogin = SqlToYql(R"(
  4269. USE plato;
  4270. CREATE USER user1 NOLOGIN;
  4271. )");
  4272. UNIT_ASSERT(reqNoLogin.IsOk());
  4273. auto reqLoginNoLogin = SqlToYql(R"(
  4274. USE plato;
  4275. CREATE USER user1 LOGIN NOLOGIN;
  4276. )");
  4277. UNIT_ASSERT(!reqLoginNoLogin.IsOk());
  4278. UNIT_ASSERT_STRING_CONTAINS(reqLoginNoLogin.Issues.ToString(), "Error: Conflicting or redundant options");
  4279. auto reqAlterLoginNoLogin = SqlToYql(R"(
  4280. USE plato;
  4281. CREATE USER user1 LOGIN;
  4282. ALTER USER user1 NOLOGIN;
  4283. )");
  4284. UNIT_ASSERT(reqAlterLoginNoLogin.IsOk());
  4285. auto reqAlterLoginNoLoginWithPassword = SqlToYql(R"(
  4286. USE plato;
  4287. CREATE USER user1 LOGIN;
  4288. ALTER USER user1 PASSWORD '321' NOLOGIN;
  4289. )");
  4290. UNIT_ASSERT(reqAlterLoginNoLoginWithPassword.IsOk());
  4291. }
  4292. Y_UNIT_TEST(CreateUserWithHash) {
  4293. auto reqCreateUser = SqlToYql(R"(
  4294. USE plato;
  4295. CREATE USER user1 HASH '{
  4296. "hash": "p4ffeMugohqyBwyckYCK1TjJfz3LIHbKiGL+t+oEhzw=",
  4297. "salt": "U+tzBtgo06EBQCjlARA6Jg==",
  4298. "type": "argon2id"
  4299. }';
  4300. )");
  4301. UNIT_ASSERT(reqCreateUser.IsOk());
  4302. auto reqCreateUserWithNoLogin = SqlToYql(R"(
  4303. USE plato;
  4304. CREATE USER user1 HASH '{
  4305. "hash": "p4ffeMugohqyBwyckYCK1TjJfz3LIHbKiGL+t+oEhzw=",
  4306. "salt": "U+tzBtgo06EBQCjlARA6Jg==",
  4307. "type": "argon2id"
  4308. }'
  4309. NOLOGIN;
  4310. )");
  4311. UNIT_ASSERT(reqCreateUserWithNoLogin.IsOk());
  4312. auto reqCreateUserWithPassword = SqlToYql(R"(
  4313. USE plato;
  4314. CREATE USER user1 HASH '{
  4315. "hash": "p4ffeMugohqyBwyckYCK1TjJfz3LIHbKiGL+t+oEhzw=",
  4316. "salt": "U+tzBtgo06EBQCjlARA6Jg==",
  4317. "type": "argon2id"
  4318. }'
  4319. PASSWORD '123';
  4320. )");
  4321. UNIT_ASSERT(!reqCreateUserWithPassword.IsOk());
  4322. UNIT_ASSERT_STRING_CONTAINS(reqCreateUserWithPassword.Issues.ToString(), "Error: Conflicting or redundant options");
  4323. auto reqAlterUser = SqlToYql(R"(
  4324. USE plato;
  4325. CREATE USER user1;
  4326. ALTER USER user1 HASH '{
  4327. "hash": "p4ffeMugohqyBwyckYCK1TjJfz3LIHbKiGL+t+oEhzw=",
  4328. "salt": "U+tzBtgo06EBQCjlARA6Jg==",
  4329. "type": "argon2id"
  4330. }';
  4331. )");
  4332. UNIT_ASSERT(reqAlterUser.IsOk());
  4333. }
  4334. Y_UNIT_TEST(CreateAlterUserWithoutCluster) {
  4335. ExpectFailWithError("\n CREATE USER user ENCRYPTED PASSWORD 'foobar';", "<main>:2:2: Error: USE statement is missing - no default cluster is selected\n");
  4336. ExpectFailWithError("ALTER USER CURRENT_USER RENAME TO $foo;", "<main>:1:1: Error: USE statement is missing - no default cluster is selected\n");
  4337. }
  4338. Y_UNIT_TEST(ModifyPermissionsWithoutCluster) {
  4339. ExpectFailWithError("\n GRANT CONNECT ON `/Root` TO user;", "<main>:2:2: Error: USE statement is missing - no default cluster is selected\n");
  4340. ExpectFailWithError("\n REVOKE MANAGE ON `/Root` FROM user;", "<main>:2:2: Error: USE statement is missing - no default cluster is selected\n");
  4341. }
  4342. Y_UNIT_TEST(ReservedRoleNames) {
  4343. ExpectFailWithError("USE plato; CREATE USER current_User;", "<main>:1:24: Error: System role CURRENT_USER can not be used here\n");
  4344. ExpectFailWithError("USE plato; ALTER USER current_User RENAME TO Current_role", "<main>:1:46: Error: System role CURRENT_ROLE can not be used here\n");
  4345. UNIT_ASSERT(SqlToYql("USE plato; DROP GROUP IF EXISTS a, b, c, current_User;").IsOk());
  4346. }
  4347. Y_UNIT_TEST(DisableClassicDivisionWithError) {
  4348. ExpectFailWithError("pragma ClassicDivision = 'false'; select $foo / 30;", "<main>:1:42: Error: Unknown name: $foo\n");
  4349. }
  4350. Y_UNIT_TEST(AggregationOfAgrregatedDistinctExpr) {
  4351. ExpectFailWithError("select sum(sum(distinct x + 1)) from plato.Input", "<main>:1:12: Error: Aggregation of aggregated values is forbidden\n");
  4352. }
  4353. Y_UNIT_TEST(WarnForUnusedSqlHint) {
  4354. NYql::TAstParseResult res = SqlToYql("select * from plato.Input1 as a join /*+ merge() */ plato.Input2 as b using(key);\n"
  4355. "select --+ foo(bar)\n"
  4356. " 1;");
  4357. UNIT_ASSERT(res.Root);
  4358. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:2:23: Warning: Hint foo will not be used, code: 4534\n");
  4359. }
  4360. Y_UNIT_TEST(WarnForDeprecatedSchema) {
  4361. NSQLTranslation::TTranslationSettings settings;
  4362. settings.ClusterMapping["s3bucket"] = NYql::S3ProviderName;
  4363. NYql::TAstParseResult res = SqlToYql("select * from s3bucket.`foo` with schema (col1 Int32, String as col2, Int64 as col3);", settings);
  4364. UNIT_ASSERT(res.Root);
  4365. UNIT_ASSERT_STRING_CONTAINS(res.Issues.ToString(), "Warning: Deprecated syntax for positional schema: please use 'column type' instead of 'type AS column', code: 4535\n");
  4366. }
  4367. Y_UNIT_TEST(ErrorOnColumnNameInMaxByLimit) {
  4368. ExpectFailWithError(
  4369. "SELECT AGGREGATE_BY(AsTuple(value, key), AggregationFactory(\"MAX_BY\", subkey)) FROM plato.Input;",
  4370. "<main>:1:42: Error: Source does not allow column references\n"
  4371. "<main>:1:71: Error: Column reference 'subkey'\n");
  4372. }
  4373. Y_UNIT_TEST(ErrorInLibraryWithTopLevelNamedSubquery) {
  4374. TString withUnusedSubq = "$unused = select max(key) from plato.Input;\n"
  4375. "\n"
  4376. "define subquery $foo() as\n"
  4377. " $count = select count(*) from plato.Input;\n"
  4378. " select * from plato.Input limit $count / 2;\n"
  4379. "end define;\n"
  4380. "export $foo;\n";
  4381. UNIT_ASSERT(SqlToYqlWithMode(withUnusedSubq, NSQLTranslation::ESqlMode::LIBRARY).IsOk());
  4382. TString withTopLevelSubq = "$count = select count(*) from plato.Input;\n"
  4383. "\n"
  4384. "define subquery $foo() as\n"
  4385. " select * from plato.Input limit $count / 2;\n"
  4386. "end define;\n"
  4387. "export $foo;\n";
  4388. auto res = SqlToYqlWithMode(withTopLevelSubq, NSQLTranslation::ESqlMode::LIBRARY);
  4389. UNIT_ASSERT(!res.Root);
  4390. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:17: Error: Named subquery can not be used as a top level statement in libraries\n");
  4391. }
  4392. Y_UNIT_TEST(SessionStartAndSessionStateShouldSurviveSessionWindowArgsError){
  4393. TString query = R"(
  4394. $init = ($_row) -> (min(1, 2)); -- error: aggregation func min() can not be used here
  4395. $calculate = ($_row, $_state) -> (1);
  4396. $update = ($_row, $_state) -> (2);
  4397. SELECT
  4398. SessionStart() over w as session_start,
  4399. SessionState() over w as session_state,
  4400. FROM plato.Input as t
  4401. WINDOW w AS (
  4402. PARTITION BY user, SessionWindow(ts + 1, $init, $update, $calculate)
  4403. )
  4404. )";
  4405. ExpectFailWithError(query, "<main>:2:33: Error: Aggregation function Min requires exactly 1 argument(s), given: 2\n");
  4406. }
  4407. Y_UNIT_TEST(ScalarContextUsage1) {
  4408. TString query = R"(
  4409. $a = (select 1 as x, 2 as y);
  4410. select 1 + $a;
  4411. )";
  4412. ExpectFailWithError(query, "<main>:2:39: Error: Source used in expression should contain one concrete column\n"
  4413. "<main>:3:24: Error: Source is used here\n");
  4414. }
  4415. Y_UNIT_TEST(ScalarContextUsage2) {
  4416. TString query = R"(
  4417. use plato;
  4418. $a = (select 1 as x, 2 as y);
  4419. select * from concat($a);
  4420. )";
  4421. ExpectFailWithError(query, "<main>:3:39: Error: Source used in expression should contain one concrete column\n"
  4422. "<main>:4:34: Error: Source is used here\n");
  4423. }
  4424. Y_UNIT_TEST(ScalarContextUsage3) {
  4425. TString query = R"(
  4426. use plato;
  4427. $a = (select 1 as x, 2 as y);
  4428. select * from range($a);
  4429. )";
  4430. ExpectFailWithError(query, "<main>:3:39: Error: Source used in expression should contain one concrete column\n"
  4431. "<main>:4:33: Error: Source is used here\n");
  4432. }
  4433. Y_UNIT_TEST(ScalarContextUsage4) {
  4434. TString query = R"(
  4435. use plato;
  4436. $a = (select 1 as x, 2 as y);
  4437. insert into $a select 1;
  4438. )";
  4439. ExpectFailWithError(query, "<main>:3:39: Error: Source used in expression should contain one concrete column\n"
  4440. "<main>:4:25: Error: Source is used here\n");
  4441. }
  4442. }
  4443. void CheckUnused(const TString& req, const TString& symbol, unsigned row, unsigned col) {
  4444. auto res = SqlToYql(req);
  4445. UNIT_ASSERT(res.Root);
  4446. UNIT_ASSERT_NO_DIFF(Err2Str(res), TStringBuilder() << "<main>:" << row << ":" << col << ": Warning: Symbol " << symbol << " is not used, code: 4527\n");
  4447. }
  4448. Y_UNIT_TEST_SUITE(WarnUnused) {
  4449. Y_UNIT_TEST(ActionOrSubquery) {
  4450. TString req = " $a()\n"
  4451. "as select 1;\n"
  4452. "end define;\n"
  4453. "\n"
  4454. "select 1;";
  4455. CheckUnused("define action\n" + req, "$a", 2, 3);
  4456. CheckUnused("define subquery\n" + req, "$a", 2, 3);
  4457. }
  4458. Y_UNIT_TEST(Import) {
  4459. TString req = "import lib1 symbols\n"
  4460. " $sqr;\n"
  4461. "select 1;";
  4462. CheckUnused(req, "$sqr", 2, 3);
  4463. req = "import lib1 symbols\n"
  4464. " $sqr as\n"
  4465. " $sq;\n"
  4466. "select 1;";
  4467. CheckUnused(req, "$sq", 3, 5);
  4468. }
  4469. Y_UNIT_TEST(NamedNodeStatement) {
  4470. TString req = " $a, $a = AsTuple(1, 2);\n"
  4471. "select $a;";
  4472. CheckUnused(req, "$a", 1, 2);
  4473. req = "$a, $b = AsTuple(1, 2);\n"
  4474. "select $a;";
  4475. CheckUnused(req, "$b", 1, 6);
  4476. CheckUnused(" $a = 1; $a = 2; select $a;", "$a", 1, 2);
  4477. }
  4478. Y_UNIT_TEST(Declare) {
  4479. CheckUnused("declare $a as String;select 1;", "$a", 1, 9);
  4480. }
  4481. Y_UNIT_TEST(ActionParams) {
  4482. TString req = "define action $a($x, $y) as\n"
  4483. " select $x;\n"
  4484. "end define;\n"
  4485. "\n"
  4486. "do $a(1,2);";
  4487. CheckUnused(req, "$y", 1, 22);
  4488. }
  4489. Y_UNIT_TEST(SubqueryParams) {
  4490. TString req = "use plato;\n"
  4491. "define subquery $q($name, $x) as\n"
  4492. " select * from $name;\n"
  4493. "end define;\n"
  4494. "\n"
  4495. "select * from $q(\"Input\", 1);";
  4496. CheckUnused(req, "$x", 2, 27);
  4497. }
  4498. Y_UNIT_TEST(For) {
  4499. TString req = "define action $a() as\n"
  4500. " select 1;\n"
  4501. "end define;\n"
  4502. "\n"
  4503. "for $i in ListFromRange(1, 10)\n"
  4504. "do $a();";
  4505. CheckUnused(req, "$i", 5, 5);
  4506. }
  4507. Y_UNIT_TEST(LambdaParams) {
  4508. TString req = "$lambda = ($x, $y) -> ($x);\n"
  4509. "select $lambda(1, 2);";
  4510. CheckUnused(req, "$y", 1, 16);
  4511. }
  4512. Y_UNIT_TEST(InsideLambdaBody) {
  4513. TString req = "$lambda = () -> {\n"
  4514. " $x = 1; return 1;\n"
  4515. "};\n"
  4516. "select $lambda();";
  4517. CheckUnused(req, "$x", 2, 3);
  4518. req = "$lambda = () -> {\n"
  4519. " $x = 1; $x = 2; return $x;\n"
  4520. "};\n"
  4521. "select $lambda();";
  4522. CheckUnused(req, "$x", 2, 3);
  4523. }
  4524. Y_UNIT_TEST(InsideAction) {
  4525. TString req = "define action $a() as\n"
  4526. " $x = 1; select 1;\n"
  4527. "end define;\n"
  4528. "\n"
  4529. "do $a();";
  4530. CheckUnused(req, "$x", 2, 3);
  4531. req = "define action $a() as\n"
  4532. " $x = 1; $x = 2; select $x;\n"
  4533. "end define;\n"
  4534. "\n"
  4535. "do $a();";
  4536. CheckUnused(req, "$x", 2, 3);
  4537. }
  4538. Y_UNIT_TEST(NoWarnOnNestedActions) {
  4539. auto req = "pragma warning(\"error\", \"4527\");\n"
  4540. "define action $action($b) as\n"
  4541. " define action $aaa() as\n"
  4542. " select $b;\n"
  4543. " end define;\n"
  4544. " do $aaa();\n"
  4545. "end define;\n"
  4546. "\n"
  4547. "do $action(1);";
  4548. UNIT_ASSERT(SqlToYql(req).IsOk());
  4549. }
  4550. Y_UNIT_TEST(NoWarnForUsageAfterSubquery) {
  4551. auto req = "use plato;\n"
  4552. "pragma warning(\"error\", \"4527\");\n"
  4553. "\n"
  4554. "$a = 1;\n"
  4555. "\n"
  4556. "define subquery $q($table) as\n"
  4557. " select * from $table;\n"
  4558. "end define;\n"
  4559. "\n"
  4560. "select * from $q(\"Input\");\n"
  4561. "select $a;";
  4562. UNIT_ASSERT(SqlToYql(req).IsOk());
  4563. }
  4564. }
  4565. Y_UNIT_TEST_SUITE(AnonymousNames) {
  4566. Y_UNIT_TEST(ReferenceAnonymousVariableIsForbidden) {
  4567. auto req = "$_ = 1; select $_;";
  4568. auto res = SqlToYql(req);
  4569. UNIT_ASSERT(!res.Root);
  4570. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:16: Error: Unable to reference anonymous name $_\n");
  4571. req = "$`_` = 1; select $`_`;";
  4572. res = SqlToYql(req);
  4573. UNIT_ASSERT(!res.Root);
  4574. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:18: Error: Unable to reference anonymous name $_\n");
  4575. }
  4576. Y_UNIT_TEST(Declare) {
  4577. auto req = "declare $_ as String;";
  4578. auto res = SqlToYql(req);
  4579. UNIT_ASSERT(!res.Root);
  4580. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:9: Error: Can not use anonymous name '$_' in DECLARE statement\n");
  4581. }
  4582. Y_UNIT_TEST(ActionSubquery) {
  4583. auto req = "define action $_() as select 1; end define;";
  4584. auto res = SqlToYql(req);
  4585. UNIT_ASSERT(!res.Root);
  4586. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:15: Error: Can not use anonymous name '$_' as ACTION name\n");
  4587. req = "define subquery $_() as select 1; end define;";
  4588. res = SqlToYql(req);
  4589. UNIT_ASSERT(!res.Root);
  4590. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:17: Error: Can not use anonymous name '$_' as SUBQUERY name\n");
  4591. }
  4592. Y_UNIT_TEST(Import) {
  4593. auto req = "import lib symbols $sqr as $_;";
  4594. auto res = SqlToYql(req);
  4595. UNIT_ASSERT(!res.Root);
  4596. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:28: Error: Can not import anonymous name $_\n");
  4597. }
  4598. Y_UNIT_TEST(Export) {
  4599. auto req = "export $_;";
  4600. auto res = SqlToYqlWithMode(req, NSQLTranslation::ESqlMode::LIBRARY);
  4601. UNIT_ASSERT(!res.Root);
  4602. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:8: Error: Can not export anonymous name $_\n");
  4603. }
  4604. Y_UNIT_TEST(AnonymousInActionArgs) {
  4605. auto req = "pragma warning(\"error\", \"4527\");\n"
  4606. "define action $a($_, $y, $_) as\n"
  4607. " select $y;\n"
  4608. "end define;\n"
  4609. "\n"
  4610. "do $a(1,2,3);";
  4611. UNIT_ASSERT(SqlToYql(req).IsOk());
  4612. }
  4613. Y_UNIT_TEST(AnonymousInSubqueryArgs) {
  4614. auto req = "use plato;\n"
  4615. "pragma warning(\"error\", \"4527\");\n"
  4616. "define subquery $q($_, $y, $_) as\n"
  4617. " select * from $y;\n"
  4618. "end define;\n"
  4619. "\n"
  4620. "select * from $q(1,\"Input\",3);";
  4621. UNIT_ASSERT(SqlToYql(req).IsOk());
  4622. }
  4623. Y_UNIT_TEST(AnonymousInLambdaArgs) {
  4624. auto req = "pragma warning(\"error\", \"4527\");\n"
  4625. "$lambda = ($_, $x, $_) -> ($x);\n"
  4626. "select $lambda(1,2,3);";
  4627. UNIT_ASSERT(SqlToYql(req).IsOk());
  4628. }
  4629. Y_UNIT_TEST(AnonymousInFor) {
  4630. auto req = "pragma warning(\"error\", \"4527\");\n"
  4631. "evaluate for $_ in ListFromRange(1, 10) do begin select 1; end do;";
  4632. UNIT_ASSERT(SqlToYql(req).IsOk());
  4633. }
  4634. Y_UNIT_TEST(Assignment) {
  4635. auto req = "pragma warning(\"error\", \"4527\");\n"
  4636. "$_ = 1;\n"
  4637. "$_, $x, $_ = AsTuple(1,2,3);\n"
  4638. "select $x;";
  4639. UNIT_ASSERT(SqlToYql(req).IsOk());
  4640. }
  4641. }
  4642. Y_UNIT_TEST_SUITE(JsonValue) {
  4643. Y_UNIT_TEST(JsonValueArgumentCount) {
  4644. NYql::TAstParseResult res = SqlToYql("select JSON_VALUE(CAST(@@{\"key\": 1238}@@ as Json));");
  4645. UNIT_ASSERT(!res.Root);
  4646. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:49: Error: Unexpected token ')' : syntax error...\n\n");
  4647. }
  4648. Y_UNIT_TEST(JsonValueJsonPathMustBeLiteralString) {
  4649. NYql::TAstParseResult res = SqlToYql("$jsonPath = \"strict $.key\"; select JSON_VALUE(CAST(@@{\"key\": 1238}@@ as Json), $jsonPath);");
  4650. UNIT_ASSERT(!res.Root);
  4651. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:79: Error: Unexpected token absence : Missing STRING_VALUE \n\n");
  4652. }
  4653. Y_UNIT_TEST(JsonValueTranslation) {
  4654. NYql::TAstParseResult res = SqlToYql("select JSON_VALUE(CAST(@@{\"key\": 1238}@@ as Json), \"strict $.key\");");
  4655. UNIT_ASSERT(res.Root);
  4656. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  4657. Y_UNUSED(word);
  4658. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("'\"strict $.key\""));
  4659. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("SafeCast"));
  4660. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("DataType 'Json"));
  4661. };
  4662. TWordCountHive elementStat({"JsonValue"});
  4663. VerifyProgram(res, elementStat, verifyLine);
  4664. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["JsonValue"]);
  4665. }
  4666. Y_UNIT_TEST(JsonValueReturningSection) {
  4667. for (const auto& typeName : {"Bool", "Int64", "Double", "String"}) {
  4668. NYql::TAstParseResult res = SqlToYql(
  4669. TStringBuilder() << "select JSON_VALUE(CAST(@@{\"key\": 1238}@@ as Json), \"strict $.key\" RETURNING " << typeName << ");"
  4670. );
  4671. UNIT_ASSERT(res.Root);
  4672. TVerifyLineFunc verifyLine = [&](const TString& word, const TString& line) {
  4673. Y_UNUSED(word);
  4674. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("'\"strict $.key\""));
  4675. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("SafeCast"));
  4676. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("DataType 'Json"));
  4677. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find(TStringBuilder() << "DataType '" << typeName));
  4678. };
  4679. TWordCountHive elementStat({typeName});
  4680. VerifyProgram(res, elementStat, verifyLine);
  4681. UNIT_ASSERT(elementStat[typeName] > 0);
  4682. }
  4683. }
  4684. Y_UNIT_TEST(JsonValueInvalidReturningType) {
  4685. NYql::TAstParseResult res = SqlToYql("select JSON_VALUE(CAST(@@{'key': 1238}@@ as Json), 'strict $.key' RETURNING invalid);");
  4686. UNIT_ASSERT(!res.Root);
  4687. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:77: Error: Unknown simple type 'invalid'\n");
  4688. }
  4689. Y_UNIT_TEST(JsonValueAndReturningInExpressions) {
  4690. NYql::TAstParseResult res = SqlToYql(
  4691. "USE plato\n;"
  4692. "$json_value = \"some string\";\n"
  4693. "SELECT $json_value;\n"
  4694. "SELECT 1 as json_value;\n"
  4695. "SELECT $json_value as json_value;\n"
  4696. "$returning = \"another string\";\n"
  4697. "SELECT $returning;\n"
  4698. "SELECT 1 as returning;\n"
  4699. "SELECT $returning as returning;\n"
  4700. );
  4701. UNIT_ASSERT(res.Root);
  4702. }
  4703. Y_UNIT_TEST(JsonValueValidCaseHandlers) {
  4704. const TVector<std::pair<TString, TString>> testCases = {
  4705. {"", "'DefaultValue (Null)"},
  4706. {"NULL", "'DefaultValue (Null)"},
  4707. {"ERROR", "'Error (Null)"},
  4708. {"DEFAULT 123", "'DefaultValue (Int32 '\"123\")"},
  4709. };
  4710. for (const auto& onEmpty : testCases) {
  4711. for (const auto& onError : testCases) {
  4712. TStringBuilder query;
  4713. query << "$json = CAST(@@{\"key\": 1238}@@ as Json);\n"
  4714. << "SELECT JSON_VALUE($json, \"strict $.key\"";
  4715. if (!onEmpty.first.empty()) {
  4716. query << " " << onEmpty.first << " ON EMPTY";
  4717. }
  4718. if (!onError.first.empty()) {
  4719. query << " " << onError.first << " ON ERROR";
  4720. }
  4721. query << ");\n";
  4722. NYql::TAstParseResult res = SqlToYql(query);
  4723. UNIT_ASSERT(res.Root);
  4724. TVerifyLineFunc verifyLine = [&](const TString& word, const TString& line) {
  4725. Y_UNUSED(word);
  4726. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find(onEmpty.second + " " + onError.second));
  4727. };
  4728. TWordCountHive elementStat({"JsonValue"});
  4729. VerifyProgram(res, elementStat, verifyLine);
  4730. UNIT_ASSERT(elementStat["JsonValue"] > 0);
  4731. }
  4732. }
  4733. }
  4734. Y_UNIT_TEST(JsonValueTooManyCaseHandlers) {
  4735. NYql::TAstParseResult res = SqlToYql(
  4736. "select JSON_VALUE(CAST(@@{\"key\": 1238}@@ as Json), \"strict $.key\" NULL ON EMPTY NULL ON ERROR NULL ON EMPTY);\n"
  4737. );
  4738. UNIT_ASSERT(!res.Root);
  4739. UNIT_ASSERT_NO_DIFF(
  4740. Err2Str(res),
  4741. "<main>:1:52: Error: Only 1 ON EMPTY and/or 1 ON ERROR clause is expected\n"
  4742. );
  4743. }
  4744. Y_UNIT_TEST(JsonValueTooManyOnEmpty) {
  4745. NYql::TAstParseResult res = SqlToYql(
  4746. "select JSON_VALUE(CAST(@@{\"key\": 1238}@@ as Json), \"strict $.key\" NULL ON EMPTY NULL ON EMPTY);\n"
  4747. );
  4748. UNIT_ASSERT(!res.Root);
  4749. UNIT_ASSERT_NO_DIFF(
  4750. Err2Str(res),
  4751. "<main>:1:52: Error: Only 1 ON EMPTY clause is expected\n"
  4752. );
  4753. }
  4754. Y_UNIT_TEST(JsonValueTooManyOnError) {
  4755. NYql::TAstParseResult res = SqlToYql(
  4756. "select JSON_VALUE(CAST(@@{\"key\": 1238}@@ as Json), \"strict $.key\" NULL ON ERROR NULL ON ERROR);\n"
  4757. );
  4758. UNIT_ASSERT(!res.Root);
  4759. UNIT_ASSERT_NO_DIFF(
  4760. Err2Str(res),
  4761. "<main>:1:52: Error: Only 1 ON ERROR clause is expected\n"
  4762. );
  4763. }
  4764. Y_UNIT_TEST(JsonValueOnEmptyAfterOnError) {
  4765. NYql::TAstParseResult res = SqlToYql(
  4766. "select JSON_VALUE(CAST(@@{\"key\": 1238}@@ as Json), \"strict $.key\" NULL ON ERROR NULL ON EMPTY);\n"
  4767. );
  4768. UNIT_ASSERT(!res.Root);
  4769. UNIT_ASSERT_NO_DIFF(
  4770. Err2Str(res),
  4771. "<main>:1:52: Error: ON EMPTY clause must be before ON ERROR clause\n"
  4772. );
  4773. }
  4774. Y_UNIT_TEST(JsonValueNullInput) {
  4775. NYql::TAstParseResult res = SqlToYql(R"(SELECT JSON_VALUE(NULL, "strict $.key");)");
  4776. UNIT_ASSERT(res.Root);
  4777. TVerifyLineFunc verifyLine = [&](const TString& word, const TString& line) {
  4778. Y_UNUSED(word);
  4779. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("(Nothing (OptionalType (DataType 'Json)))"));
  4780. };
  4781. TWordCountHive elementStat({"JsonValue"});
  4782. VerifyProgram(res, elementStat, verifyLine);
  4783. UNIT_ASSERT(elementStat["JsonValue"] > 0);
  4784. }
  4785. }
  4786. Y_UNIT_TEST_SUITE(JsonExists) {
  4787. Y_UNIT_TEST(JsonExistsValidHandlers) {
  4788. const TVector<std::pair<TString, TString>> testCases = {
  4789. {"", "(Just (Bool '\"false\"))"},
  4790. {"TRUE ON ERROR", "(Just (Bool '\"true\"))"},
  4791. {"FALSE ON ERROR", "(Just (Bool '\"false\"))"},
  4792. {"UNKNOWN ON ERROR", "(Nothing (OptionalType (DataType 'Bool)))"},
  4793. // NOTE: in this case we expect arguments of JsonExists callable to end immediately
  4794. // after variables. This parenthesis at the end of the expression is left on purpose
  4795. {"ERROR ON ERROR", "(Utf8 '\"strict $.key\") (JsonVariables))"},
  4796. };
  4797. for (const auto& item : testCases) {
  4798. NYql::TAstParseResult res = SqlToYql(
  4799. TStringBuilder() << R"(
  4800. $json = CAST(@@{"key": 1238}@@ as Json);
  4801. SELECT JSON_EXISTS($json, "strict $.key" )" << item.first << ");\n"
  4802. );
  4803. UNIT_ASSERT(res.Root);
  4804. TVerifyLineFunc verifyLine = [&](const TString& word, const TString& line) {
  4805. Y_UNUSED(word);
  4806. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find(item.second));
  4807. };
  4808. TWordCountHive elementStat({"JsonExists"});
  4809. VerifyProgram(res, elementStat, verifyLine);
  4810. UNIT_ASSERT(elementStat["JsonExists"] > 0);
  4811. }
  4812. }
  4813. Y_UNIT_TEST(JsonExistsInvalidHandler) {
  4814. NYql::TAstParseResult res = SqlToYql(R"(
  4815. $json = CAST(@@{"key": 1238}@@ as Json);
  4816. $default = false;
  4817. SELECT JSON_EXISTS($json, "strict $.key" $default ON ERROR);
  4818. )");
  4819. UNIT_ASSERT(!res.Root);
  4820. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:4:53: Error: Unexpected token absence : Missing RPAREN \n\n");
  4821. }
  4822. Y_UNIT_TEST(JsonExistsNullInput) {
  4823. NYql::TAstParseResult res = SqlToYql(R"(SELECT JSON_EXISTS(NULL, "strict $.key");)");
  4824. UNIT_ASSERT(res.Root);
  4825. TVerifyLineFunc verifyLine = [&](const TString& word, const TString& line) {
  4826. Y_UNUSED(word);
  4827. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("(Nothing (OptionalType (DataType 'Json)))"));
  4828. };
  4829. TWordCountHive elementStat({"JsonExists"});
  4830. VerifyProgram(res, elementStat, verifyLine);
  4831. UNIT_ASSERT(elementStat["JsonExists"] > 0);
  4832. }
  4833. }
  4834. Y_UNIT_TEST_SUITE(JsonQuery) {
  4835. Y_UNIT_TEST(JsonQueryValidHandlers) {
  4836. using TTestSuite = const TVector<std::pair<TString, TString>>;
  4837. TTestSuite wrapCases = {
  4838. {"", "'NoWrap"},
  4839. {"WITHOUT WRAPPER", "'NoWrap"},
  4840. {"WITHOUT ARRAY WRAPPER", "'NoWrap"},
  4841. {"WITH WRAPPER", "'Wrap"},
  4842. {"WITH ARRAY WRAPPER", "'Wrap"},
  4843. {"WITH UNCONDITIONAL WRAPPER", "'Wrap"},
  4844. {"WITH UNCONDITIONAL ARRAY WRAPPER", "'Wrap"},
  4845. {"WITH CONDITIONAL WRAPPER", "'ConditionalWrap"},
  4846. {"WITH CONDITIONAL ARRAY WRAPPER", "'ConditionalWrap"},
  4847. };
  4848. TTestSuite handlerCases = {
  4849. {"", "'Null"},
  4850. {"ERROR", "'Error"},
  4851. {"NULL", "'Null"},
  4852. {"EMPTY ARRAY", "'EmptyArray"},
  4853. {"EMPTY OBJECT", "'EmptyObject"},
  4854. };
  4855. for (const auto& wrap : wrapCases) {
  4856. for (const auto& onError : handlerCases) {
  4857. for (const auto& onEmpty : handlerCases) {
  4858. TStringBuilder query;
  4859. query << R"($json = CAST(@@{"key": [123]}@@ as Json);
  4860. SELECT JSON_QUERY($json, "strict $.key" )" << wrap.first;
  4861. if (!onEmpty.first.empty()) {
  4862. if (wrap.first.StartsWith("WITH ")) {
  4863. continue;
  4864. }
  4865. query << " " << onEmpty.first << " ON EMPTY";
  4866. }
  4867. if (!onError.first.empty()) {
  4868. query << " " << onError.first << " ON ERROR";
  4869. }
  4870. query << ");\n";
  4871. NYql::TAstParseResult res = SqlToYql(query);
  4872. UNIT_ASSERT(res.Root);
  4873. TVerifyLineFunc verifyLine = [&](const TString& word, const TString& line) {
  4874. Y_UNUSED(word);
  4875. const TString args = TStringBuilder() << wrap.second << " " << onEmpty.second << " " << onError.second;
  4876. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find(args));
  4877. };
  4878. Cout << wrap.first << " " << onEmpty.first << " " << onError.first << Endl;
  4879. TWordCountHive elementStat({"JsonQuery"});
  4880. VerifyProgram(res, elementStat, verifyLine);
  4881. UNIT_ASSERT(elementStat["JsonQuery"] > 0);
  4882. }
  4883. }
  4884. }
  4885. }
  4886. Y_UNIT_TEST(JsonQueryOnEmptyWithWrapper) {
  4887. NYql::TAstParseResult res = SqlToYql(R"(
  4888. $json = CAST(@@{"key": 1238}@@ as Json);
  4889. SELECT JSON_QUERY($json, "strict $" WITH ARRAY WRAPPER EMPTY ARRAY ON EMPTY);
  4890. )");
  4891. UNIT_ASSERT(!res.Root);
  4892. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:3:38: Error: ON EMPTY is prohibited because WRAPPER clause is specified\n");
  4893. }
  4894. Y_UNIT_TEST(JsonQueryNullInput) {
  4895. NYql::TAstParseResult res = SqlToYql(R"(SELECT JSON_QUERY(NULL, "strict $.key");)");
  4896. UNIT_ASSERT(res.Root);
  4897. TVerifyLineFunc verifyLine = [&](const TString& word, const TString& line) {
  4898. Y_UNUSED(word);
  4899. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("(Nothing (OptionalType (DataType 'Json)))"));
  4900. };
  4901. TWordCountHive elementStat({"JsonQuery"});
  4902. VerifyProgram(res, elementStat, verifyLine);
  4903. UNIT_ASSERT(elementStat["JsonQuery"] > 0);
  4904. }
  4905. }
  4906. Y_UNIT_TEST_SUITE(JsonPassing) {
  4907. Y_UNIT_TEST(SupportedVariableTypes) {
  4908. const TVector<TString> functions = {"JSON_EXISTS", "JSON_VALUE", "JSON_QUERY"};
  4909. for (const auto& function : functions) {
  4910. const auto query = Sprintf(R"(
  4911. pragma CompactNamedExprs;
  4912. $json = CAST(@@{"key": 1238}@@ as Json);
  4913. SELECT %s(
  4914. $json,
  4915. "strict $.key"
  4916. PASSING
  4917. "string" as var1,
  4918. 1.234 as var2,
  4919. CAST(1 as Int64) as var3,
  4920. true as var4,
  4921. $json as var5
  4922. ))",
  4923. function.data()
  4924. );
  4925. NYql::TAstParseResult res = SqlToYql(query);
  4926. UNIT_ASSERT(res.Root);
  4927. TVerifyLineFunc verifyLine = [&](const TString& word, const TString& line) {
  4928. Y_UNUSED(word);
  4929. UNIT_ASSERT_VALUES_UNEQUAL_C(TString::npos, line.find(R"('('"var1" (String '"string")))"), "Cannot find `var1`");
  4930. UNIT_ASSERT_VALUES_UNEQUAL_C(TString::npos, line.find(R"('('"var2" (Double '"1.234")))"), "Cannot find `var2`");
  4931. UNIT_ASSERT_VALUES_UNEQUAL_C(TString::npos, line.find(R"('('"var3" (SafeCast (Int32 '"1") (DataType 'Int64))))"), "Cannot find `var3`");
  4932. UNIT_ASSERT_VALUES_UNEQUAL_C(TString::npos, line.find(R"('('"var4" (Bool '"true")))"), "Cannot find `var4`");
  4933. UNIT_ASSERT_VALUES_UNEQUAL_C(TString::npos, line.find(R"('('"var5" namedexprnode0))"), "Cannot find `var5`");
  4934. };
  4935. TWordCountHive elementStat({"JsonVariables"});
  4936. VerifyProgram(res, elementStat, verifyLine);
  4937. UNIT_ASSERT(elementStat["JsonVariables"] > 0);
  4938. }
  4939. }
  4940. Y_UNIT_TEST(ValidVariableNames) {
  4941. const TVector<TString> functions = {"JSON_EXISTS", "JSON_VALUE", "JSON_QUERY"};
  4942. for (const auto& function : functions) {
  4943. const auto query = Sprintf(R"(
  4944. $json = CAST(@@{"key": 1238}@@ as Json);
  4945. SELECT %s(
  4946. $json,
  4947. "strict $.key"
  4948. PASSING
  4949. "one" as var1,
  4950. "two" as "VaR2",
  4951. "three" as `var3`,
  4952. "four" as VaR4
  4953. ))",
  4954. function.data()
  4955. );
  4956. NYql::TAstParseResult res = SqlToYql(query);
  4957. UNIT_ASSERT(res.Root);
  4958. TVerifyLineFunc verifyLine = [&](const TString& word, const TString& line) {
  4959. Y_UNUSED(word);
  4960. UNIT_ASSERT_VALUES_UNEQUAL_C(TString::npos, line.find(R"('('"var1" (String '"one")))"), "Cannot find `var1`");
  4961. UNIT_ASSERT_VALUES_UNEQUAL_C(TString::npos, line.find(R"('('"VaR2" (String '"two")))"), "Cannot find `VaR2`");
  4962. UNIT_ASSERT_VALUES_UNEQUAL_C(TString::npos, line.find(R"('('"var3" (String '"three")))"), "Cannot find `var3`");
  4963. UNIT_ASSERT_VALUES_UNEQUAL_C(TString::npos, line.find(R"('('"VaR4" (String '"four")))"), "Cannot find `VaR4`");
  4964. };
  4965. TWordCountHive elementStat({"JsonVariables"});
  4966. VerifyProgram(res, elementStat, verifyLine);
  4967. UNIT_ASSERT(elementStat["JsonVariables"] > 0);
  4968. }
  4969. }
  4970. }
  4971. Y_UNIT_TEST_SUITE(MigrationToJsonApi) {
  4972. Y_UNIT_TEST(WarningOnDeprecatedJsonUdf) {
  4973. NYql::TAstParseResult res = SqlToYql(R"(
  4974. $json = CAST(@@{"key": 1234}@@ as Json);
  4975. SELECT Json::Parse($json);
  4976. )");
  4977. UNIT_ASSERT(res.Root);
  4978. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:3:26: Warning: Json UDF is deprecated. Please use JSON API instead, code: 4506\n");
  4979. }
  4980. }
  4981. Y_UNIT_TEST_SUITE(AnsiIdentsNegative) {
  4982. Y_UNIT_TEST(EnableAnsiLexerFromRequestSpecialComments) {
  4983. auto req = "\n"
  4984. "\t --!ansi_lexer \n"
  4985. "-- Some comment\n"
  4986. "-- another comment\n"
  4987. "pragma SimpleColumns;\n"
  4988. "\n"
  4989. "select 1, '''' as empty;";
  4990. auto res = SqlToYql(req);
  4991. UNIT_ASSERT(res.IsOk());
  4992. UNIT_ASSERT(res.Issues.Size() == 0);
  4993. }
  4994. Y_UNIT_TEST(AnsiLexerShouldNotBeEnabledHere) {
  4995. auto req = "$str = '\n"
  4996. "--!ansi_lexer\n"
  4997. "--!syntax_v1\n"
  4998. "';\n"
  4999. "\n"
  5000. "select 1, $str, \"\" as empty;";
  5001. auto res = SqlToYql(req);
  5002. UNIT_ASSERT(res.IsOk());
  5003. UNIT_ASSERT(res.Issues.Size() == 0);
  5004. }
  5005. Y_UNIT_TEST(DoubleQuotesInDictsTuplesOrLists) {
  5006. auto req = "$d = { 'a': 1, \"b\": 2, 'c': 3,};";
  5007. auto res = SqlToYqlWithAnsiLexer(req);
  5008. UNIT_ASSERT(!res.Root);
  5009. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:16: Error: Column reference \"b\" is not allowed in current scope\n");
  5010. req = "$t = (1, 2, \"a\");";
  5011. res = SqlToYqlWithAnsiLexer(req);
  5012. UNIT_ASSERT(!res.Root);
  5013. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:13: Error: Column reference \"a\" is not allowed in current scope\n");
  5014. req = "$l = ['a', 'b', \"c\"];";
  5015. res = SqlToYqlWithAnsiLexer(req);
  5016. UNIT_ASSERT(!res.Root);
  5017. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:17: Error: Column reference \"c\" is not allowed in current scope\n");
  5018. }
  5019. Y_UNIT_TEST(MultilineComments) {
  5020. auto req = "/*/**/ select 1;";
  5021. auto res = SqlToYql(req);
  5022. UNIT_ASSERT(res.Root);
  5023. res = SqlToYqlWithAnsiLexer(req);
  5024. UNIT_ASSERT(!res.Root);
  5025. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:16: Error: Unexpected character : syntax error...\n\n");
  5026. req = "/*\n"
  5027. "--/*\n"
  5028. "*/ select 1;";
  5029. res = SqlToYql(req);
  5030. UNIT_ASSERT(res.Root);
  5031. res = SqlToYqlWithAnsiLexer(req);
  5032. UNIT_ASSERT(!res.Root);
  5033. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:3:12: Error: Unexpected character : syntax error...\n\n");
  5034. req = "/*\n"
  5035. "/*\n"
  5036. "--*/\n"
  5037. "*/ select 1;";
  5038. res = SqlToYql(req);
  5039. UNIT_ASSERT(!res.Root);
  5040. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:4:0: Error: Unexpected token '*' : cannot match to any predicted input...\n\n");
  5041. res = SqlToYqlWithAnsiLexer(req);
  5042. UNIT_ASSERT(res.Root);
  5043. }
  5044. }
  5045. Y_UNIT_TEST_SUITE(AnsiOptionalAs) {
  5046. Y_UNIT_TEST(OptionalAsInProjection) {
  5047. UNIT_ASSERT(SqlToYql("PRAGMA AnsiOptionalAs; SELECT a b, c FROM plato.Input;").IsOk());
  5048. ExpectFailWithError("PRAGMA DisableAnsiOptionalAs;\n"
  5049. "SELECT a b, c FROM plato.Input;",
  5050. "<main>:2:10: Error: Expecting mandatory AS here. Did you miss comma? Please add PRAGMA AnsiOptionalAs; for ANSI compatibility\n");
  5051. }
  5052. Y_UNIT_TEST(OptionalAsWithKeywords) {
  5053. UNIT_ASSERT(SqlToYql("PRAGMA AnsiOptionalAs; SELECT a type, b data, c source FROM plato.Input;").IsOk());
  5054. }
  5055. }
  5056. Y_UNIT_TEST_SUITE(SessionWindowNegative) {
  5057. Y_UNIT_TEST(SessionWindowWithoutSource) {
  5058. ExpectFailWithError("SELECT 1 + SessionWindow(ts, 32);",
  5059. "<main>:1:12: Error: SessionWindow requires data source\n");
  5060. }
  5061. Y_UNIT_TEST(SessionWindowInProjection) {
  5062. ExpectFailWithError("SELECT 1 + SessionWindow(ts, 32) from plato.Input;",
  5063. "<main>:1:12: Error: SessionWindow can only be used as a top-level GROUP BY / PARTITION BY expression\n");
  5064. }
  5065. Y_UNIT_TEST(SessionWindowWithNonConstSecondArg) {
  5066. ExpectFailWithError(
  5067. "SELECT key, session_start FROM plato.Input\n"
  5068. "GROUP BY SessionWindow(ts, 32 + subkey) as session_start, key;",
  5069. "<main>:2:10: Error: Source does not allow column references\n"
  5070. "<main>:2:33: Error: Column reference 'subkey'\n");
  5071. }
  5072. Y_UNIT_TEST(SessionWindowWithWrongNumberOfArgs) {
  5073. ExpectFailWithError("SELECT * FROM plato.Input GROUP BY SessionWindow()",
  5074. "<main>:1:36: Error: SessionWindow requires either two or four arguments\n");
  5075. ExpectFailWithError("SELECT * FROM plato.Input GROUP BY SessionWindow(key, subkey, 100)",
  5076. "<main>:1:36: Error: SessionWindow requires either two or four arguments\n");
  5077. }
  5078. Y_UNIT_TEST(DuplicateSessionWindow) {
  5079. ExpectFailWithError(
  5080. "SELECT\n"
  5081. " *\n"
  5082. "FROM plato.Input\n"
  5083. "GROUP BY\n"
  5084. " SessionWindow(ts, 10),\n"
  5085. " user,\n"
  5086. " SessionWindow(ts, 20)\n"
  5087. ";",
  5088. "<main>:7:5: Error: Duplicate session window specification:\n"
  5089. "<main>:5:5: Error: Previous session window is declared here\n");
  5090. ExpectFailWithError(
  5091. "SELECT\n"
  5092. " MIN(key) over w\n"
  5093. "FROM plato.Input\n"
  5094. "WINDOW w AS (\n"
  5095. " PARTITION BY SessionWindow(ts, 10), user,\n"
  5096. " SessionWindow(ts, 20)\n"
  5097. ");",
  5098. "<main>:6:5: Error: Duplicate session window specification:\n"
  5099. "<main>:5:18: Error: Previous session window is declared here\n");
  5100. }
  5101. Y_UNIT_TEST(SessionStartStateWithoutSource) {
  5102. ExpectFailWithError("SELECT 1 + SessionStart();",
  5103. "<main>:1:12: Error: SessionStart requires data source\n");
  5104. ExpectFailWithError("SELECT 1 + SessionState();",
  5105. "<main>:1:12: Error: SessionState requires data source\n");
  5106. }
  5107. Y_UNIT_TEST(SessionStartStateWithoutGroupByOrWindow) {
  5108. ExpectFailWithError("SELECT 1 + SessionStart() from plato.Input;",
  5109. "<main>:1:12: Error: SessionStart can not be used without aggregation by SessionWindow\n");
  5110. ExpectFailWithError("SELECT 1 + SessionState() from plato.Input;",
  5111. "<main>:1:12: Error: SessionState can not be used without aggregation by SessionWindow\n");
  5112. }
  5113. Y_UNIT_TEST(SessionStartStateWithGroupByWithoutSession) {
  5114. ExpectFailWithError("SELECT 1 + SessionStart() from plato.Input group by user;",
  5115. "<main>:1:12: Error: SessionStart can not be used here: SessionWindow specification is missing in GROUP BY\n");
  5116. ExpectFailWithError("SELECT 1 + SessionState() from plato.Input group by user;",
  5117. "<main>:1:12: Error: SessionState can not be used here: SessionWindow specification is missing in GROUP BY\n");
  5118. }
  5119. Y_UNIT_TEST(SessionStartStateWithoutOverWithWindowWithoutSession) {
  5120. ExpectFailWithError("SELECT 1 + SessionStart(), MIN(key) over w from plato.Input window w as ()",
  5121. "<main>:1:12: Error: SessionStart can not be used without aggregation by SessionWindow. Maybe you forgot to add OVER `window_name`?\n");
  5122. ExpectFailWithError("SELECT 1 + SessionState(), MIN(key) over w from plato.Input window w as ()",
  5123. "<main>:1:12: Error: SessionState can not be used without aggregation by SessionWindow. Maybe you forgot to add OVER `window_name`?\n");
  5124. }
  5125. Y_UNIT_TEST(SessionStartStateWithWindowWithoutSession) {
  5126. ExpectFailWithError("SELECT 1 + SessionStart() over w, MIN(key) over w from plato.Input window w as ()",
  5127. "<main>:1:12: Error: SessionStart can not be used with window w: SessionWindow specification is missing in PARTITION BY\n");
  5128. ExpectFailWithError("SELECT 1 + SessionState() over w, MIN(key) over w from plato.Input window w as ()",
  5129. "<main>:1:12: Error: SessionState can not be used with window w: SessionWindow specification is missing in PARTITION BY\n");
  5130. }
  5131. Y_UNIT_TEST(SessionStartStateWithSessionedWindow) {
  5132. ExpectFailWithError("SELECT 1 + SessionStart(), MIN(key) over w from plato.Input group by key window w as (partition by SessionWindow(ts, 1)) ",
  5133. "<main>:1:12: Error: SessionStart can not be used here: SessionWindow specification is missing in GROUP BY. Maybe you forgot to add OVER `window_name`?\n");
  5134. ExpectFailWithError("SELECT 1 + SessionState(), MIN(key) over w from plato.Input group by key window w as (partition by SessionWindow(ts, 1)) ",
  5135. "<main>:1:12: Error: SessionState can not be used here: SessionWindow specification is missing in GROUP BY. Maybe you forgot to add OVER `window_name`?\n");
  5136. }
  5137. Y_UNIT_TEST(AggregationBySessionStateIsNotSupportedYet) {
  5138. ExpectFailWithError("SELECT SOME(1 + SessionState()), key from plato.Input group by key, SessionWindow(ts, 1);",
  5139. "<main>:1:17: Error: SessionState with GROUP BY is not supported yet\n");
  5140. }
  5141. Y_UNIT_TEST(SessionWindowInRtmr) {
  5142. NYql::TAstParseResult res = SqlToYql(
  5143. "SELECT * FROM plato.Input GROUP BY SessionWindow(ts, 10);",
  5144. 10, TString(NYql::RtmrProviderName));
  5145. UNIT_ASSERT(!res.Root);
  5146. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:54: Error: Streaming group by query must have a hopping window specification.\n");
  5147. res = SqlToYql(R"(
  5148. SELECT key, SUM(value) AS value FROM plato.Input
  5149. GROUP BY key, HOP(subkey, "PT10S", "PT30S", "PT20S"), SessionWindow(ts, 10);
  5150. )", 10, TString(NYql::RtmrProviderName));
  5151. UNIT_ASSERT(!res.Root);
  5152. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:2:13: Error: SessionWindow is unsupported for streaming sources\n");
  5153. }
  5154. }
  5155. Y_UNIT_TEST_SUITE(LibraSqlSugar) {
  5156. auto makeResult = [](TStringBuf settings) {
  5157. return SqlToYql(
  5158. TStringBuilder()
  5159. << settings
  5160. << "\n$udf1 = MyLibra::MakeLibraPreprocessor($settings);"
  5161. << "\n$udf2 = CustomLibra::MakeLibraPreprocessor($settings);"
  5162. << "\nPROCESS plato.Input USING $udf1(TableRow())"
  5163. << "\nUNION ALL"
  5164. << "\nPROCESS plato.Input USING $udf2(TableRow());"
  5165. );
  5166. };
  5167. Y_UNIT_TEST(EmptySettings) {
  5168. auto res = makeResult(R"(
  5169. $settings = AsStruct();
  5170. )");
  5171. UNIT_ASSERT(res.IsOk());
  5172. }
  5173. Y_UNIT_TEST(OnlyEntities) {
  5174. auto res = makeResult(R"(
  5175. $settings = AsStruct(
  5176. AsList("A", "B", "C") AS Entities
  5177. );
  5178. )");
  5179. UNIT_ASSERT(res.IsOk());
  5180. }
  5181. Y_UNIT_TEST(EntitiesWithStrategy) {
  5182. auto res = makeResult(R"(
  5183. $settings = AsStruct(
  5184. AsList("A", "B", "C") AS Entities,
  5185. "blacklist" AS EntitiesStrategy
  5186. );
  5187. )");
  5188. UNIT_ASSERT(res.IsOk());
  5189. }
  5190. Y_UNIT_TEST(AllSettings) {
  5191. auto res = makeResult(R"(
  5192. $settings = AsStruct(
  5193. AsList("A", "B", "C") AS Entities,
  5194. "whitelist" AS EntitiesStrategy,
  5195. "path" AS BlockstatDict,
  5196. false AS ParseWithFat,
  5197. "map" AS Mode
  5198. );
  5199. )");
  5200. UNIT_ASSERT(res.IsOk());
  5201. }
  5202. Y_UNIT_TEST(BadStrategy) {
  5203. auto res = makeResult(R"(
  5204. $settings = AsStruct("bad" AS EntitiesStrategy);
  5205. )");
  5206. UNIT_ASSERT_STRING_CONTAINS(
  5207. Err2Str(res),
  5208. "Error: MakeLibraPreprocessor got invalid entities strategy: expected 'whitelist' or 'blacklist'"
  5209. );
  5210. }
  5211. Y_UNIT_TEST(BadEntities) {
  5212. auto res = makeResult(R"(
  5213. $settings = AsStruct(AsList("A", 1) AS Entities);
  5214. )");
  5215. UNIT_ASSERT_STRING_CONTAINS(Err2Str(res), "Error: MakeLibraPreprocessor entity must be string literal");
  5216. }
  5217. }
  5218. Y_UNIT_TEST_SUITE(TrailingQuestionsNegative) {
  5219. Y_UNIT_TEST(Basic) {
  5220. ExpectFailWithError("SELECT 1?;", "<main>:1:9: Error: Unexpected token '?' at the end of expression\n");
  5221. ExpectFailWithError("SELECT 1? + 1;", "<main>:1:10: Error: Unexpected token '+' : cannot match to any predicted input...\n\n");
  5222. ExpectFailWithError("SELECT 1 + 1??? < 2", "<main>:1:13: Error: Unexpected token '?' at the end of expression\n");
  5223. ExpectFailWithError("SELECT 1? > 2? > 3?",
  5224. "<main>:1:11: Error: Unexpected token '?' at the end of expression\n"
  5225. "<main>:1:16: Error: Unexpected token '?' at the end of expression\n"
  5226. "<main>:1:21: Error: Unexpected token '?' at the end of expression\n");
  5227. }
  5228. Y_UNIT_TEST(SmartParen) {
  5229. ExpectFailWithError("$x = 1; SELECT (Int32?, $x?)", "<main>:1:27: Error: Unexpected token '?' at the end of expression\n");
  5230. ExpectFailWithError("SELECT (Int32, foo?)", "<main>:1:19: Error: Unexpected token '?' at the end of expression\n");
  5231. }
  5232. Y_UNIT_TEST(LambdaOptArgs) {
  5233. ExpectFailWithError("$l = ($x, $y?, $z??, $t?) -> ($x);", "<main>:1:18: Error: Expecting at most one '?' token here (for optional lambda parameters), but got 2\n");
  5234. }
  5235. }
  5236. Y_UNIT_TEST_SUITE(FlexibleTypes) {
  5237. Y_UNIT_TEST(AssumeOrderByType) {
  5238. UNIT_ASSERT(SqlToYql("PRAGMA FlexibleTypes; SELECT 1 AS int32 ASSUME ORDER BY int32").IsOk());
  5239. }
  5240. Y_UNIT_TEST(GroupingSets) {
  5241. UNIT_ASSERT(SqlToYql("PRAGMA FlexibleTypes; SELECT COUNT(*) AS cnt, text, uuid FROM plato.Input GROUP BY GROUPING SETS((uuid), (uuid, text));").IsOk());
  5242. }
  5243. Y_UNIT_TEST(WeakField) {
  5244. UNIT_ASSERT(SqlToYql("PRAGMA FlexibleTypes; SELECT WeakField(text, string) as text FROM plato.Input").IsOk());
  5245. }
  5246. Y_UNIT_TEST(Aggregation1) {
  5247. TString q =
  5248. "PRAGMA FlexibleTypes;\n"
  5249. "$foo = ($x, $const, $type) -> ($x || $const || FormatType($type));\n"
  5250. "SELECT $foo(SOME(x), 'aaa', String) FROM plato.Input GROUP BY y;";
  5251. UNIT_ASSERT(SqlToYql(q).IsOk());
  5252. }
  5253. Y_UNIT_TEST(Aggregation2) {
  5254. TString q =
  5255. "PRAGMA FlexibleTypes;\n"
  5256. "SELECT 1 + String + MAX(key) FROM plato.Input;";
  5257. UNIT_ASSERT(SqlToYql(q).IsOk());
  5258. }
  5259. }
  5260. Y_UNIT_TEST_SUITE(ExternalDeclares) {
  5261. Y_UNIT_TEST(BasicUsage) {
  5262. NSQLTranslation::TTranslationSettings settings;
  5263. settings.DeclaredNamedExprs["foo"] = "String";
  5264. auto res = SqlToYqlWithSettings("select $foo;", settings);
  5265. UNIT_ASSERT(res.IsOk());
  5266. UNIT_ASSERT(res.Issues.Size() == 0);
  5267. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  5268. if (word == "declare") {
  5269. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find(R"__((declare "$foo" (DataType 'String)))__"));
  5270. }
  5271. };
  5272. TWordCountHive elementStat = {{TString("declare"), 0}};
  5273. VerifyProgram(res, elementStat, verifyLine);
  5274. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["declare"]);
  5275. }
  5276. Y_UNIT_TEST(DeclareOverrides) {
  5277. NSQLTranslation::TTranslationSettings settings;
  5278. settings.DeclaredNamedExprs["foo"] = "String";
  5279. auto res = SqlToYqlWithSettings("declare $foo as Int32; select $foo;", settings);
  5280. UNIT_ASSERT(res.IsOk());
  5281. UNIT_ASSERT(res.Issues.Size() == 0);
  5282. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  5283. if (word == "declare") {
  5284. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find(R"__((declare "$foo" (DataType 'Int32)))__"));
  5285. }
  5286. };
  5287. TWordCountHive elementStat = {{TString("declare"), 0}};
  5288. VerifyProgram(res, elementStat, verifyLine);
  5289. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["declare"]);
  5290. }
  5291. Y_UNIT_TEST(UnusedDeclareDoesNotProduceWarning) {
  5292. NSQLTranslation::TTranslationSettings settings;
  5293. settings.DeclaredNamedExprs["foo"] = "String";
  5294. auto res = SqlToYqlWithSettings("select 1;", settings);
  5295. UNIT_ASSERT(res.IsOk());
  5296. UNIT_ASSERT(res.Issues.Size() == 0);
  5297. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  5298. if (word == "declare") {
  5299. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find(R"__((declare "$foo" (DataType 'String)))__"));
  5300. }
  5301. };
  5302. TWordCountHive elementStat = {{TString("declare"), 0}};
  5303. VerifyProgram(res, elementStat, verifyLine);
  5304. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["declare"]);
  5305. }
  5306. Y_UNIT_TEST(DeclaresWithInvalidTypesFails) {
  5307. NSQLTranslation::TTranslationSettings settings;
  5308. settings.DeclaredNamedExprs["foo"] = "List<BadType>";
  5309. auto res = SqlToYqlWithSettings("select 1;", settings);
  5310. UNIT_ASSERT(!res.Root);
  5311. UNIT_ASSERT_NO_DIFF(Err2Str(res),
  5312. "<main>:0:5: Error: Unknown type: 'BadType'\n"
  5313. "<main>: Error: Failed to parse type for externally declared name 'foo'\n");
  5314. }
  5315. }
  5316. Y_UNIT_TEST_SUITE(ExternalDataSource) {
  5317. Y_UNIT_TEST(CreateExternalDataSourceWithAuthNone) {
  5318. NYql::TAstParseResult res = SqlToYql(R"sql(
  5319. USE plato;
  5320. CREATE EXTERNAL DATA SOURCE MyDataSource WITH (
  5321. SOURCE_TYPE="ObjectStorage",
  5322. LOCATION="my-bucket",
  5323. AUTH_METHOD="NONE"
  5324. );
  5325. )sql");
  5326. UNIT_ASSERT(res.Root);
  5327. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  5328. if (word == "Write") {
  5329. UNIT_ASSERT_STRING_CONTAINS(line, R"#('('('"auth_method" '"NONE") '('"location" '"my-bucket") '('"source_type" '"ObjectStorage"))#");
  5330. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("createObject"));
  5331. }
  5332. };
  5333. TWordCountHive elementStat = { {TString("Write"), 0} };
  5334. VerifyProgram(res, elementStat, verifyLine);
  5335. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  5336. }
  5337. Y_UNIT_TEST(CreateExternalDataSourceWithAuthServiceAccount) {
  5338. NYql::TAstParseResult res = SqlToYql(R"sql(
  5339. USE plato;
  5340. CREATE EXTERNAL DATA SOURCE MyDataSource WITH (
  5341. SOURCE_TYPE="ObjectStorage",
  5342. LOCATION="my-bucket",
  5343. AUTH_METHOD="SERVICE_ACCOUNT",
  5344. SERVICE_ACCOUNT_ID="sa",
  5345. SERVICE_ACCOUNT_SECRET_NAME="sa_secret_name"
  5346. );
  5347. )sql");
  5348. UNIT_ASSERT(res.Root);
  5349. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  5350. if (word == "Write") {
  5351. UNIT_ASSERT_STRING_CONTAINS(line, R"#('('('"auth_method" '"SERVICE_ACCOUNT") '('"location" '"my-bucket") '('"service_account_id" '"sa") '('"service_account_secret_name" '"sa_secret_name") '('"source_type" '"ObjectStorage"))#");
  5352. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("createObject"));
  5353. }
  5354. };
  5355. TWordCountHive elementStat = { {TString("Write"), 0} };
  5356. VerifyProgram(res, elementStat, verifyLine);
  5357. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  5358. }
  5359. Y_UNIT_TEST(CreateExternalDataSourceWithBasic) {
  5360. NYql::TAstParseResult res = SqlToYql(R"sql(
  5361. USE plato;
  5362. CREATE EXTERNAL DATA SOURCE MyDataSource WITH (
  5363. SOURCE_TYPE="PostgreSQL",
  5364. LOCATION="protocol://host:port/",
  5365. AUTH_METHOD="BASIC",
  5366. LOGIN="admin",
  5367. PASSWORD_SECRET_NAME="secret_name"
  5368. );
  5369. )sql");
  5370. UNIT_ASSERT(res.Root);
  5371. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  5372. if (word == "Write") {
  5373. UNIT_ASSERT_STRING_CONTAINS(line, R"#('('('"auth_method" '"BASIC") '('"location" '"protocol://host:port/") '('"login" '"admin") '('"password_secret_name" '"secret_name") '('"source_type" '"PostgreSQL"))#");
  5374. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("createObject"));
  5375. }
  5376. };
  5377. TWordCountHive elementStat = { {TString("Write"), 0} };
  5378. VerifyProgram(res, elementStat, verifyLine);
  5379. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  5380. }
  5381. Y_UNIT_TEST(CreateExternalDataSourceWithMdbBasic) {
  5382. NYql::TAstParseResult res = SqlToYql(R"sql(
  5383. USE plato;
  5384. CREATE EXTERNAL DATA SOURCE MyDataSource WITH (
  5385. SOURCE_TYPE="PostgreSQL",
  5386. LOCATION="protocol://host:port/",
  5387. AUTH_METHOD="MDB_BASIC",
  5388. SERVICE_ACCOUNT_ID="sa",
  5389. SERVICE_ACCOUNT_SECRET_NAME="sa_secret_name",
  5390. LOGIN="admin",
  5391. PASSWORD_SECRET_NAME="secret_name"
  5392. );
  5393. )sql");
  5394. UNIT_ASSERT(res.Root);
  5395. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  5396. if (word == "Write") {
  5397. UNIT_ASSERT_STRING_CONTAINS(line, R"#('('('"auth_method" '"MDB_BASIC") '('"location" '"protocol://host:port/") '('"login" '"admin") '('"password_secret_name" '"secret_name") '('"service_account_id" '"sa") '('"service_account_secret_name" '"sa_secret_name") '('"source_type" '"PostgreSQL"))#");
  5398. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("createObject"));
  5399. }
  5400. };
  5401. TWordCountHive elementStat = { {TString("Write"), 0} };
  5402. VerifyProgram(res, elementStat, verifyLine);
  5403. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  5404. }
  5405. Y_UNIT_TEST(CreateExternalDataSourceWithAws) {
  5406. NYql::TAstParseResult res = SqlToYql(R"sql(
  5407. USE plato;
  5408. CREATE EXTERNAL DATA SOURCE MyDataSource WITH (
  5409. SOURCE_TYPE="PostgreSQL",
  5410. LOCATION="protocol://host:port/",
  5411. AUTH_METHOD="AWS",
  5412. AWS_ACCESS_KEY_ID_SECRET_NAME="secred_id_name",
  5413. AWS_SECRET_ACCESS_KEY_SECRET_NAME="secret_key_name",
  5414. AWS_REGION="ru-central-1"
  5415. );
  5416. )sql");
  5417. UNIT_ASSERT(res.Root);
  5418. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  5419. if (word == "Write") {
  5420. UNIT_ASSERT_STRING_CONTAINS(line, R"#('('('"auth_method" '"AWS") '('"aws_access_key_id_secret_name" '"secred_id_name") '('"aws_region" '"ru-central-1") '('"aws_secret_access_key_secret_name" '"secret_key_name") '('"location" '"protocol://host:port/") '('"source_type" '"PostgreSQL"))#");
  5421. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("createObject"));
  5422. }
  5423. };
  5424. TWordCountHive elementStat = { {TString("Write"), 0} };
  5425. VerifyProgram(res, elementStat, verifyLine);
  5426. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  5427. }
  5428. Y_UNIT_TEST(CreateExternalDataSourceWithToken) {
  5429. NYql::TAstParseResult res = SqlToYql(R"sql(
  5430. USE plato;
  5431. CREATE EXTERNAL DATA SOURCE MyDataSource WITH (
  5432. SOURCE_TYPE="YT",
  5433. LOCATION="protocol://host:port/",
  5434. AUTH_METHOD="TOKEN",
  5435. TOKEN_SECRET_NAME="token_name"
  5436. );
  5437. )sql");
  5438. UNIT_ASSERT(res.Root);
  5439. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  5440. if (word == "Write") {
  5441. UNIT_ASSERT_STRING_CONTAINS(line, R"#('('('"auth_method" '"TOKEN") '('"location" '"protocol://host:port/") '('"source_type" '"YT") '('"token_secret_name" '"token_name"))#");
  5442. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("createObject"));
  5443. }
  5444. };
  5445. TWordCountHive elementStat = { {TString("Write"), 0} };
  5446. VerifyProgram(res, elementStat, verifyLine);
  5447. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  5448. }
  5449. Y_UNIT_TEST(CreateExternalDataSourceWithTablePrefix) {
  5450. NYql::TAstParseResult res = SqlToYql(R"sql(
  5451. USE plato;
  5452. pragma TablePathPrefix='/aba';
  5453. CREATE EXTERNAL DATA SOURCE MyDataSource WITH (
  5454. SOURCE_TYPE="ObjectStorage",
  5455. LOCATION="my-bucket",
  5456. AUTH_METHOD="NONE"
  5457. );
  5458. )sql");
  5459. UNIT_ASSERT(res.Root);
  5460. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  5461. if (word == "Write") {
  5462. UNIT_ASSERT_STRING_CONTAINS(line, "/aba/MyDataSource");
  5463. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("createObject"));
  5464. }
  5465. };
  5466. TWordCountHive elementStat = { {TString("Write"), 0} };
  5467. VerifyProgram(res, elementStat, verifyLine);
  5468. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  5469. }
  5470. Y_UNIT_TEST(CreateExternalDataSourceIfNotExists) {
  5471. NYql::TAstParseResult res = SqlToYql(R"sql(
  5472. USE plato;
  5473. CREATE EXTERNAL DATA SOURCE IF NOT EXISTS MyDataSource WITH (
  5474. SOURCE_TYPE="ObjectStorage",
  5475. LOCATION="my-bucket",
  5476. AUTH_METHOD="NONE"
  5477. );
  5478. )sql");
  5479. UNIT_ASSERT(res.Root);
  5480. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  5481. if (word == "Write") {
  5482. UNIT_ASSERT_STRING_CONTAINS(line, R"#('('('"auth_method" '"NONE") '('"location" '"my-bucket") '('"source_type" '"ObjectStorage"))#");
  5483. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("createObjectIfNotExists"));
  5484. }
  5485. };
  5486. TWordCountHive elementStat = { {TString("Write"), 0} };
  5487. VerifyProgram(res, elementStat, verifyLine);
  5488. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  5489. }
  5490. Y_UNIT_TEST(AlterExternalDataSource) {
  5491. NYql::TAstParseResult res = SqlToYql(R"sql(
  5492. USE plato;
  5493. ALTER EXTERNAL DATA SOURCE MyDataSource
  5494. SET (SOURCE_TYPE = "ObjectStorage", Login = "Admin"),
  5495. SET Location "bucket",
  5496. RESET (Auth_Method, Service_Account_Id, Service_Account_Secret_Name);
  5497. )sql");
  5498. UNIT_ASSERT_C(res.Root, res.Issues.ToString());
  5499. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  5500. if (word == "Write") {
  5501. UNIT_ASSERT_STRING_CONTAINS(line, R"#(('mode 'alterObject))#");
  5502. UNIT_ASSERT_STRING_CONTAINS(line, R"#('('features '('('"location" '"bucket") '('"login" '"Admin") '('"source_type" '"ObjectStorage"))))#");
  5503. UNIT_ASSERT_STRING_CONTAINS(line, R"#('('resetFeatures '('"auth_method" '"service_account_id" '"service_account_secret_name")))#");
  5504. }
  5505. };
  5506. TWordCountHive elementStat = { {TString("Write"), 0} };
  5507. VerifyProgram(res, elementStat, verifyLine);
  5508. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  5509. }
  5510. Y_UNIT_TEST(CreateExternalDataSourceOrReplace) {
  5511. NYql::TAstParseResult res = SqlToYql(R"(
  5512. USE plato;
  5513. CREATE OR REPLACE EXTERNAL DATA SOURCE MyDataSource WITH (
  5514. SOURCE_TYPE="ObjectStorage",
  5515. LOCATION="my-bucket",
  5516. AUTH_METHOD="NONE"
  5517. );
  5518. )");
  5519. UNIT_ASSERT(res.Root);
  5520. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  5521. if (word == "Write") {
  5522. UNIT_ASSERT_STRING_CONTAINS(line, R"#('('('"auth_method" '"NONE") '('"location" '"my-bucket") '('"source_type" '"ObjectStorage"))#");
  5523. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("createObjectOrReplace"));
  5524. }
  5525. };
  5526. TWordCountHive elementStat = { {TString("Write"), 0} };
  5527. VerifyProgram(res, elementStat, verifyLine);
  5528. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  5529. }
  5530. Y_UNIT_TEST(CreateOrReplaceForUnsupportedTableTypesShouldFail) {
  5531. ExpectFailWithError(R"sql(
  5532. USE plato;
  5533. CREATE OR REPLACE TABLE t (a int32 not null, primary key(a, a));
  5534. )sql" , "<main>:3:23: Error: OR REPLACE feature is supported only for EXTERNAL DATA SOURCE and EXTERNAL TABLE\n");
  5535. ExpectFailWithError(R"sql(
  5536. USE plato;
  5537. CREATE OR REPLACE TABLE t (
  5538. Key Uint64,
  5539. Value1 String,
  5540. PRIMARY KEY (Key)
  5541. )
  5542. WITH (
  5543. STORE = COLUMN,
  5544. AUTO_PARTITIONING_MIN_PARTITIONS_COUNT = 10
  5545. );
  5546. )sql" , "<main>:3:23: Error: OR REPLACE feature is supported only for EXTERNAL DATA SOURCE and EXTERNAL TABLE\n");
  5547. }
  5548. Y_UNIT_TEST(CreateExternalDataSourceWithBadArguments) {
  5549. ExpectFailWithError(R"sql(
  5550. USE plato;
  5551. CREATE EXTERNAL DATA SOURCE MyDataSource;
  5552. )sql" , "<main>:3:56: Error: Unexpected token ';' : syntax error...\n\n");
  5553. ExpectFailWithError(R"sql(
  5554. USE plato;
  5555. CREATE EXTERNAL DATA SOURCE MyDataSource WITH (
  5556. LOCATION="my-bucket",
  5557. AUTH_METHOD="NONE"
  5558. );
  5559. )sql" , "<main>:5:33: Error: SOURCE_TYPE requires key\n");
  5560. ExpectFailWithError(R"sql(
  5561. USE plato;
  5562. CREATE EXTERNAL DATA SOURCE MyDataSource WITH (
  5563. SOURCE_TYPE="ObjectStorage",
  5564. LOCATION="my-bucket"
  5565. );
  5566. )sql" , "<main>:5:30: Error: AUTH_METHOD requires key\n");
  5567. ExpectFailWithError(R"sql(
  5568. USE plato;
  5569. CREATE EXTERNAL DATA SOURCE MyDataSource WITH (
  5570. SOURCE_TYPE="ObjectStorage",
  5571. LOCATION="my-bucket",
  5572. AUTH_METHOD="NONE1"
  5573. );
  5574. )sql" , "<main>:6:33: Error: Unknown AUTH_METHOD = NONE1\n");
  5575. ExpectFailWithError(R"sql(
  5576. USE plato;
  5577. CREATE EXTERNAL DATA SOURCE MyDataSource WITH (
  5578. SOURCE_TYPE="ObjectStorage",
  5579. LOCATION="my-bucket",
  5580. AUTH_METHOD="SERVICE_ACCOUNT"
  5581. );
  5582. )sql" , "<main>:6:33: Error: SERVICE_ACCOUNT_ID requires key\n");
  5583. ExpectFailWithError(R"sql(
  5584. USE plato;
  5585. CREATE EXTERNAL DATA SOURCE MyDataSource WITH (
  5586. SOURCE_TYPE="ObjectStorage",
  5587. LOCATION="my-bucket",
  5588. AUTH_METHOD="SERVICE_ACCOUNT",
  5589. SERVICE_ACCOUNT_ID="s1"
  5590. );
  5591. )sql" , "<main>:7:40: Error: SERVICE_ACCOUNT_SECRET_NAME requires key\n");
  5592. ExpectFailWithError(R"sql(
  5593. USE plato;
  5594. CREATE EXTERNAL DATA SOURCE MyDataSource WITH (
  5595. SOURCE_TYPE="ObjectStorage",
  5596. LOCATION="my-bucket",
  5597. AUTH_METHOD="SERVICE_ACCOUNT",
  5598. SERVICE_ACCOUNT_SECRET_NAME="s1"
  5599. );
  5600. )sql" , "<main>:7:49: Error: SERVICE_ACCOUNT_ID requires key\n");
  5601. ExpectFailWithError(R"sql(
  5602. USE plato;
  5603. CREATE EXTERNAL DATA SOURCE MyDataSource WITH (
  5604. SOURCE_TYPE="PostgreSQL",
  5605. LOCATION="protocol://host:port/",
  5606. AUTH_METHOD="BASIC",
  5607. LOGIN="admin"
  5608. );
  5609. )sql" , "<main>:7:27: Error: PASSWORD_SECRET_NAME requires key\n");
  5610. ExpectFailWithError(R"sql(
  5611. USE plato;
  5612. CREATE EXTERNAL DATA SOURCE MyDataSource WITH (
  5613. SOURCE_TYPE="PostgreSQL",
  5614. LOCATION="protocol://host:port/",
  5615. AUTH_METHOD="BASIC",
  5616. PASSWORD_SECRET_NAME="secret_name"
  5617. );
  5618. )sql" , "<main>:7:42: Error: LOGIN requires key\n");
  5619. ExpectFailWithError(R"sql(
  5620. USE plato;
  5621. CREATE EXTERNAL DATA SOURCE MyDataSource WITH (
  5622. SOURCE_TYPE="PostgreSQL",
  5623. LOCATION="protocol://host:port/",
  5624. AUTH_METHOD="MDB_BASIC",
  5625. SERVICE_ACCOUNT_SECRET_NAME="sa_secret_name",
  5626. LOGIN="admin",
  5627. PASSWORD_SECRET_NAME="secret_name"
  5628. );
  5629. )sql" , "<main>:9:42: Error: SERVICE_ACCOUNT_ID requires key\n");
  5630. ExpectFailWithError(R"sql(
  5631. USE plato;
  5632. CREATE EXTERNAL DATA SOURCE MyDataSource WITH (
  5633. SOURCE_TYPE="PostgreSQL",
  5634. LOCATION="protocol://host:port/",
  5635. AUTH_METHOD="MDB_BASIC",
  5636. SERVICE_ACCOUNT_ID="sa",
  5637. LOGIN="admin",
  5638. PASSWORD_SECRET_NAME="secret_name"
  5639. );
  5640. )sql" , "<main>:9:42: Error: SERVICE_ACCOUNT_SECRET_NAME requires key\n");
  5641. ExpectFailWithError(R"sql(
  5642. USE plato;
  5643. CREATE EXTERNAL DATA SOURCE MyDataSource WITH (
  5644. SOURCE_TYPE="PostgreSQL",
  5645. LOCATION="protocol://host:port/",
  5646. AUTH_METHOD="MDB_BASIC",
  5647. SERVICE_ACCOUNT_ID="sa",
  5648. SERVICE_ACCOUNT_SECRET_NAME="sa_secret_name",
  5649. PASSWORD_SECRET_NAME="secret_name"
  5650. );
  5651. )sql" , "<main>:9:42: Error: LOGIN requires key\n");
  5652. ExpectFailWithError(R"sql(
  5653. USE plato;
  5654. CREATE EXTERNAL DATA SOURCE MyDataSource WITH (
  5655. SOURCE_TYPE="PostgreSQL",
  5656. LOCATION="protocol://host:port/",
  5657. AUTH_METHOD="MDB_BASIC",
  5658. SERVICE_ACCOUNT_ID="sa",
  5659. SERVICE_ACCOUNT_SECRET_NAME="sa_secret_name",
  5660. LOGIN="admin"
  5661. );
  5662. )sql" , "<main>:9:27: Error: PASSWORD_SECRET_NAME requires key\n");
  5663. ExpectFailWithError(R"sql(
  5664. USE plato;
  5665. CREATE EXTERNAL DATA SOURCE MyDataSource WITH (
  5666. SOURCE_TYPE="PostgreSQL",
  5667. LOCATION="protocol://host:port/",
  5668. AUTH_METHOD="AWS",
  5669. AWS_SECRET_ACCESS_KEY_SECRET_NAME="secret_key_name",
  5670. AWS_REGION="ru-central-1"
  5671. );
  5672. )sql" , "<main>:8:32: Error: AWS_ACCESS_KEY_ID_SECRET_NAME requires key\n");
  5673. ExpectFailWithError(R"sql(
  5674. USE plato;
  5675. CREATE EXTERNAL DATA SOURCE MyDataSource WITH (
  5676. SOURCE_TYPE="PostgreSQL",
  5677. LOCATION="protocol://host:port/",
  5678. AUTH_METHOD="AWS",
  5679. AWS_ACCESS_KEY_ID_SECRET_NAME="secred_id_name",
  5680. AWS_REGION="ru-central-1"
  5681. );
  5682. )sql" , "<main>:8:32: Error: AWS_SECRET_ACCESS_KEY_SECRET_NAME requires key\n");
  5683. ExpectFailWithError(R"sql(
  5684. USE plato;
  5685. CREATE EXTERNAL DATA SOURCE MyDataSource WITH (
  5686. SOURCE_TYPE="PostgreSQL",
  5687. LOCATION="protocol://host:port/",
  5688. AUTH_METHOD="AWS",
  5689. AWS_SECRET_ACCESS_KEY_SECRET_NAME="secret_key_name",
  5690. AWS_ACCESS_KEY_ID_SECRET_NAME="secred_id_name"
  5691. );
  5692. )sql" , "<main>:8:51: Error: AWS_REGION requires key\n");
  5693. }
  5694. Y_UNIT_TEST(DropExternalDataSourceWithTablePrefix) {
  5695. NYql::TAstParseResult res = SqlToYql(R"sql(
  5696. USE plato;
  5697. DROP EXTERNAL DATA SOURCE MyDataSource;
  5698. )sql");
  5699. UNIT_ASSERT(res.Root);
  5700. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  5701. if (word == "Write") {
  5702. UNIT_ASSERT_VALUES_EQUAL(TString::npos, line.find("'features"));
  5703. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("dropObject"));
  5704. }
  5705. };
  5706. TWordCountHive elementStat = { {TString("Write"), 0}};
  5707. VerifyProgram(res, elementStat, verifyLine);
  5708. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  5709. }
  5710. Y_UNIT_TEST(DropExternalDataSource) {
  5711. NYql::TAstParseResult res = SqlToYql(R"sql(
  5712. USE plato;
  5713. pragma TablePathPrefix='/aba';
  5714. DROP EXTERNAL DATA SOURCE MyDataSource;
  5715. )sql");
  5716. UNIT_ASSERT(res.Root);
  5717. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  5718. if (word == "Write") {
  5719. UNIT_ASSERT_STRING_CONTAINS(line, "/aba/MyDataSource");
  5720. UNIT_ASSERT_VALUES_EQUAL(TString::npos, line.find("'features"));
  5721. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("dropObject"));
  5722. }
  5723. };
  5724. TWordCountHive elementStat = { {TString("Write"), 0}};
  5725. VerifyProgram(res, elementStat, verifyLine);
  5726. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  5727. }
  5728. Y_UNIT_TEST(DropExternalDataSourceIfExists) {
  5729. NYql::TAstParseResult res = SqlToYql(R"sql(
  5730. USE plato;
  5731. DROP EXTERNAL DATA SOURCE IF EXISTS MyDataSource;
  5732. )sql");
  5733. UNIT_ASSERT(res.Root);
  5734. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  5735. if (word == "Write") {
  5736. UNIT_ASSERT_STRING_CONTAINS(line, "MyDataSource");
  5737. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("dropObjectIfExists"));
  5738. }
  5739. };
  5740. TWordCountHive elementStat = { {TString("Write"), 0}};
  5741. VerifyProgram(res, elementStat, verifyLine);
  5742. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  5743. }
  5744. }
  5745. Y_UNIT_TEST_SUITE(ExternalTable) {
  5746. Y_UNIT_TEST(CreateExternalTable) {
  5747. NYql::TAstParseResult res = SqlToYql(R"sql(
  5748. USE plato;
  5749. CREATE EXTERNAL TABLE mytable (
  5750. a int
  5751. ) WITH (
  5752. DATA_SOURCE="/Root/mydatasource",
  5753. LOCATION="/folder1/*"
  5754. );
  5755. )sql");
  5756. UNIT_ASSERT_C(res.Root, res.Issues.ToOneLineString());
  5757. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  5758. if (word == "Write") {
  5759. UNIT_ASSERT_STRING_CONTAINS(line, R"#('('('data_source_path (String '"/Root/mydatasource")) '('location (String '"/folder1/*")))) '('tableType 'externalTable)))))#");
  5760. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("tablescheme"));
  5761. }
  5762. };
  5763. TWordCountHive elementStat = { {TString("Write"), 0} };
  5764. VerifyProgram(res, elementStat, verifyLine);
  5765. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  5766. }
  5767. Y_UNIT_TEST(CreateExternalTableWithTablePrefix) {
  5768. NYql::TAstParseResult res = SqlToYql(R"sql(
  5769. USE plato;
  5770. pragma TablePathPrefix='/aba';
  5771. CREATE EXTERNAL TABLE mytable (
  5772. a int
  5773. ) WITH (
  5774. DATA_SOURCE="mydatasource",
  5775. LOCATION="/folder1/*"
  5776. );
  5777. )sql");
  5778. UNIT_ASSERT_C(res.Root, res.Issues.ToOneLineString());
  5779. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  5780. if (word == "Write") {
  5781. UNIT_ASSERT_STRING_CONTAINS(line, "/aba/mydatasource");
  5782. UNIT_ASSERT_STRING_CONTAINS(line, "/aba/mytable");
  5783. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("tablescheme"));
  5784. }
  5785. };
  5786. TWordCountHive elementStat = { {TString("Write"), 0} };
  5787. VerifyProgram(res, elementStat, verifyLine);
  5788. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  5789. }
  5790. Y_UNIT_TEST(CreateExternalTableObjectStorage) {
  5791. auto res = SqlToYql(R"sql(
  5792. USE plato;
  5793. CREATE EXTERNAL TABLE mytable (
  5794. a int,
  5795. year Int
  5796. ) WITH (
  5797. DATA_SOURCE="/Root/mydatasource",
  5798. LOCATION="/folder1/*",
  5799. FORMAT="json_as_string",
  5800. `projection.enabled`="true",
  5801. `projection.year.type`="integer",
  5802. `projection.year.min`="2010",
  5803. `projection.year.max`="2022",
  5804. `projection.year.interval`="1",
  5805. `projection.month.type`="integer",
  5806. `projection.month.min`="1",
  5807. `projection.month.max`="12",
  5808. `projection.month.interval`="1",
  5809. `projection.month.digits`="2",
  5810. `storage.location.template`="${year}/${month}",
  5811. PARTITONED_BY = "[year, month]"
  5812. );
  5813. )sql");
  5814. UNIT_ASSERT_C(res.IsOk(), res.Issues.ToString());
  5815. }
  5816. Y_UNIT_TEST(CreateExternalTableIfNotExists) {
  5817. NYql::TAstParseResult res = SqlToYql(R"sql(
  5818. USE plato;
  5819. CREATE EXTERNAL TABLE IF NOT EXISTS mytable (
  5820. a int
  5821. ) WITH (
  5822. DATA_SOURCE="/Root/mydatasource",
  5823. LOCATION="/folder1/*"
  5824. );
  5825. )sql");
  5826. UNIT_ASSERT_C(res.Root, res.Issues.ToOneLineString());
  5827. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  5828. if (word == "Write") {
  5829. UNIT_ASSERT_STRING_CONTAINS(line, R"#('('('data_source_path (String '"/Root/mydatasource")) '('location (String '"/folder1/*")))) '('tableType 'externalTable)))))#");
  5830. UNIT_ASSERT_STRING_CONTAINS(line, "create_if_not_exists");
  5831. }
  5832. };
  5833. TWordCountHive elementStat = { {TString("Write"), 0} };
  5834. VerifyProgram(res, elementStat, verifyLine);
  5835. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  5836. }
  5837. Y_UNIT_TEST(CreateExternalTableOrReplace) {
  5838. NYql::TAstParseResult res = SqlToYql(R"(
  5839. USE plato;
  5840. CREATE OR REPLACE EXTERNAL TABLE mytable (
  5841. a int
  5842. ) WITH (
  5843. DATA_SOURCE="/Root/mydatasource",
  5844. LOCATION="/folder1/*"
  5845. );
  5846. )");
  5847. UNIT_ASSERT_C(res.Root, res.Issues.ToOneLineString());
  5848. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  5849. if (word == "Write") {
  5850. UNIT_ASSERT_STRING_CONTAINS(line, R"#('('('data_source_path (String '"/Root/mydatasource")) '('location (String '"/folder1/*")))) '('tableType 'externalTable)))))#");
  5851. UNIT_ASSERT_STRING_CONTAINS(line, "create_or_replace");
  5852. }
  5853. };
  5854. TWordCountHive elementStat = { {TString("Write"), 0} };
  5855. VerifyProgram(res, elementStat, verifyLine);
  5856. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  5857. }
  5858. Y_UNIT_TEST(AlterExternalTableAddColumn) {
  5859. NYql::TAstParseResult res = SqlToYql(R"sql(
  5860. USE plato;
  5861. ALTER EXTERNAL TABLE mytable
  5862. ADD COLUMN my_column int32,
  5863. RESET (LOCATION);
  5864. )sql");
  5865. UNIT_ASSERT_C(res.Root, res.Issues.ToOneLineString());
  5866. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  5867. if (word == "Write") {
  5868. UNIT_ASSERT_STRING_CONTAINS(line, R"#('actions '('('addColumns '('('"my_column" (AsOptionalType (DataType 'Int32))#");
  5869. UNIT_ASSERT_STRING_CONTAINS(line, R"#(('setTableSettings '('('location)))#");
  5870. UNIT_ASSERT_STRING_CONTAINS(line, R"#(('tableType 'externalTable))#");
  5871. UNIT_ASSERT_STRING_CONTAINS(line, R"#(('mode 'alter))#");
  5872. }
  5873. };
  5874. TWordCountHive elementStat = { {TString("Write"), 0} };
  5875. VerifyProgram(res, elementStat, verifyLine);
  5876. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  5877. }
  5878. Y_UNIT_TEST(AlterExternalTableDropColumn) {
  5879. NYql::TAstParseResult res = SqlToYql(R"sql(
  5880. USE plato;
  5881. ALTER EXTERNAL TABLE mytable
  5882. DROP COLUMN my_column,
  5883. SET (Location = "abc", Other_Prop = "42"),
  5884. SET x 'y';
  5885. )sql");
  5886. UNIT_ASSERT_C(res.Root, res.Issues.ToOneLineString());
  5887. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  5888. if (word == "Write") {
  5889. UNIT_ASSERT_STRING_CONTAINS(line, R"#('actions '('('dropColumns '('"my_column")#");
  5890. UNIT_ASSERT_STRING_CONTAINS(line, R"#(('setTableSettings '('('location (String '"abc")) '('Other_Prop (String '"42")) '('x (String '"y")))))#");
  5891. UNIT_ASSERT_STRING_CONTAINS(line, R"#(('tableType 'externalTable))#");
  5892. UNIT_ASSERT_STRING_CONTAINS(line, R"#(('mode 'alter))#");
  5893. }
  5894. };
  5895. TWordCountHive elementStat = { {TString("Write"), 0} };
  5896. VerifyProgram(res, elementStat, verifyLine);
  5897. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  5898. }
  5899. Y_UNIT_TEST(CreateExternalTableWithBadArguments) {
  5900. ExpectFailWithError(R"sql(
  5901. USE plato;
  5902. CREATE EXTERNAL TABLE mytable;
  5903. )sql" , "<main>:3:45: Error: Unexpected token ';' : syntax error...\n\n");
  5904. ExpectFailWithError(R"sql(
  5905. USE plato;
  5906. CREATE EXTERNAL TABLE mytable (
  5907. a int
  5908. );
  5909. )sql" , "<main>:4:23: Error: DATA_SOURCE requires key\n");
  5910. ExpectFailWithError(R"sql(
  5911. USE plato;
  5912. CREATE EXTERNAL TABLE mytable (
  5913. a int
  5914. ) WITH (
  5915. DATA_SOURCE="/Root/mydatasource"
  5916. );
  5917. )sql" , "<main>:6:33: Error: LOCATION requires key\n");
  5918. ExpectFailWithError(R"sql(
  5919. USE plato;
  5920. CREATE EXTERNAL TABLE mytable (
  5921. a int
  5922. ) WITH (
  5923. LOCATION="/folder1/*"
  5924. );
  5925. )sql" , "<main>:6:30: Error: DATA_SOURCE requires key\n");
  5926. ExpectFailWithError(R"sql(
  5927. USE plato;
  5928. CREATE EXTERNAL TABLE mytable (
  5929. a int,
  5930. PRIMARY KEY(a)
  5931. ) WITH (
  5932. DATA_SOURCE="/Root/mydatasource",
  5933. LOCATION="/folder1/*"
  5934. );
  5935. )sql" , "<main>:8:30: Error: PRIMARY KEY is not supported for external table\n");
  5936. }
  5937. Y_UNIT_TEST(DropExternalTable) {
  5938. NYql::TAstParseResult res = SqlToYql(R"sql(
  5939. USE plato;
  5940. DROP EXTERNAL TABLE MyExternalTable;
  5941. )sql");
  5942. UNIT_ASSERT(res.Root);
  5943. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  5944. if (word == "Write") {
  5945. UNIT_ASSERT_VALUES_EQUAL(TString::npos, line.find("tablescheme"));
  5946. }
  5947. };
  5948. TWordCountHive elementStat = { {TString("Write"), 0}};
  5949. VerifyProgram(res, elementStat, verifyLine);
  5950. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  5951. }
  5952. Y_UNIT_TEST(DropExternalTableWithTablePrefix) {
  5953. NYql::TAstParseResult res = SqlToYql(R"sql(
  5954. USE plato;
  5955. pragma TablePathPrefix='/aba';
  5956. DROP EXTERNAL TABLE MyExternalTable;
  5957. )sql");
  5958. UNIT_ASSERT(res.Root);
  5959. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  5960. if (word == "Write") {
  5961. UNIT_ASSERT_STRING_CONTAINS(line, "/aba/MyExternalTable");
  5962. UNIT_ASSERT_VALUES_EQUAL(TString::npos, line.find("'tablescheme"));
  5963. }
  5964. };
  5965. TWordCountHive elementStat = { {TString("Write"), 0}};
  5966. VerifyProgram(res, elementStat, verifyLine);
  5967. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  5968. }
  5969. Y_UNIT_TEST(DropExternalTableIfExists) {
  5970. NYql::TAstParseResult res = SqlToYql(R"sql(
  5971. USE plato;
  5972. DROP EXTERNAL TABLE IF EXISTS MyExternalTable;
  5973. )sql");
  5974. UNIT_ASSERT(res.Root);
  5975. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  5976. if (word == "Write") {
  5977. UNIT_ASSERT_VALUES_EQUAL(TString::npos, line.find("tablescheme"));
  5978. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("drop_if_exists"));
  5979. }
  5980. };
  5981. TWordCountHive elementStat = { {TString("Write"), 0}};
  5982. VerifyProgram(res, elementStat, verifyLine);
  5983. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  5984. }
  5985. }
  5986. Y_UNIT_TEST_SUITE(TopicsDDL) {
  5987. void TestQuery(const TString& query, bool expectOk = true) {
  5988. TStringBuilder finalQuery;
  5989. finalQuery << "use plato;" << Endl << query;
  5990. auto res = SqlToYql(finalQuery, 10, "kikimr");
  5991. if (expectOk) {
  5992. UNIT_ASSERT_C(res.IsOk(), res.Issues.ToString());
  5993. } else {
  5994. UNIT_ASSERT(!res.IsOk());
  5995. }
  5996. }
  5997. Y_UNIT_TEST(CreateTopicSimple) {
  5998. TestQuery(R"(
  5999. CREATE TOPIC topic1;
  6000. )");
  6001. TestQuery(R"(
  6002. CREATE TOPIC `cluster1.topic1`;
  6003. )");
  6004. TestQuery(R"(
  6005. CREATE TOPIC topic1 WITH (metering_mode = "str_value", partition_count_limit = 123, retention_period = Interval('PT1H'));
  6006. )");
  6007. }
  6008. Y_UNIT_TEST(CreateTopicConsumer) {
  6009. TestQuery(R"(
  6010. CREATE TOPIC topic1 (CONSUMER cons1);
  6011. )");
  6012. TestQuery(R"(
  6013. CREATE TOPIC topic1 (CONSUMER cons1, CONSUMER cons2 WITH (important = false));
  6014. )");
  6015. TestQuery(R"(
  6016. CREATE TOPIC topic1 (CONSUMER cons1, CONSUMER cons2 WITH (important = false)) WITH (supported_codecs = "1,2,3");
  6017. )");
  6018. }
  6019. Y_UNIT_TEST(AlterTopicSimple) {
  6020. TestQuery(R"(
  6021. ALTER TOPIC topic1 SET (retention_period = Interval('PT1H'));
  6022. )");
  6023. TestQuery(R"(
  6024. ALTER TOPIC topic1 SET (retention_storage_mb = 3, partition_count_limit = 50);
  6025. )");
  6026. TestQuery(R"(
  6027. ALTER TOPIC topic1 RESET (supported_codecs, retention_period);
  6028. )");
  6029. TestQuery(R"(
  6030. ALTER TOPIC topic1 RESET (partition_write_speed_bytes_per_second),
  6031. SET (partition_write_burst_bytes = 11111, min_active_partitions = 1);
  6032. )");
  6033. }
  6034. Y_UNIT_TEST(AlterTopicConsumer) {
  6035. TestQuery(R"(
  6036. ALTER TOPIC topic1 ADD CONSUMER consumer1,
  6037. ADD CONSUMER consumer2 WITH (important = false, supported_codecs = "RAW"),
  6038. ALTER CONSUMER consumer3 SET (important = false, read_from = 1),
  6039. ALTER CONSUMER consumer3 RESET (supported_codecs),
  6040. DROP CONSUMER consumer4,
  6041. SET (partition_count_limit = 11, retention_period = Interval('PT1H')),
  6042. RESET(metering_mode)
  6043. )");
  6044. }
  6045. Y_UNIT_TEST(DropTopic) {
  6046. TestQuery(R"(
  6047. DROP TOPIC topic1;
  6048. )");
  6049. }
  6050. Y_UNIT_TEST(TopicBadRequests) {
  6051. TestQuery(R"(
  6052. CREATE TOPIC topic1();
  6053. )", false);
  6054. TestQuery(R"(
  6055. CREATE TOPIC topic1 SET setting1 = value1;
  6056. )", false);
  6057. TestQuery(R"(
  6058. ALTER TOPIC topic1 SET setting1 value1;
  6059. )", false);
  6060. TestQuery(R"(
  6061. ALTER TOPIC topic1 RESET setting1;
  6062. )", false);
  6063. TestQuery(R"(
  6064. ALTER TOPIC topic1 DROP CONSUMER consumer4 WITH (k1 = v1);
  6065. )", false);
  6066. TestQuery(R"(
  6067. CREATE TOPIC topic1 WITH (retention_period = 123);
  6068. )", false);
  6069. TestQuery(R"(
  6070. CREATE TOPIC topic1 (CONSUMER cons1, CONSUMER cons1 WITH (important = false));
  6071. )", false);
  6072. TestQuery(R"(
  6073. CREATE TOPIC topic1 (CONSUMER cons1 WITH (bad_option = false));
  6074. )", false);
  6075. TestQuery(R"(
  6076. ALTER TOPIC topic1 ADD CONSUMER cons1, ALTER CONSUMER cons1 RESET (important);
  6077. )", false);
  6078. TestQuery(R"(
  6079. ALTER TOPIC topic1 ADD CONSUMER consumer1,
  6080. ALTER CONSUMER consumer3 SET (supported_codecs = "RAW", read_from = 1),
  6081. ALTER CONSUMER consumer3 RESET (supported_codecs);
  6082. )", false);
  6083. TestQuery(R"(
  6084. ALTER TOPIC topic1 ADD CONSUMER consumer1,
  6085. ALTER CONSUMER consumer3 SET (supported_codecs = "RAW", read_from = 1),
  6086. ALTER CONSUMER consumer3 SET (read_from = 2);
  6087. )", false);
  6088. }
  6089. Y_UNIT_TEST(TopicWithPrefix) {
  6090. NYql::TAstParseResult res = SqlToYql(R"(
  6091. USE plato;
  6092. PRAGMA TablePathPrefix = '/database/path/to/tables';
  6093. ALTER TOPIC `my_table/my_feed` ADD CONSUMER `my_consumer`;
  6094. )");
  6095. UNIT_ASSERT(res.Root);
  6096. TWordCountHive elementStat = {{TString("/database/path/to/tables/my_table/my_feed"), 0}, {"topic", 0}};
  6097. VerifyProgram(res, elementStat);
  6098. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["topic"]);
  6099. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["/database/path/to/tables/my_table/my_feed"]);
  6100. }
  6101. }
  6102. Y_UNIT_TEST_SUITE(BlockEnginePragma) {
  6103. Y_UNIT_TEST(Basic) {
  6104. const TVector<TString> values = {"auto", "force", "disable"};
  6105. for (const auto& value : values) {
  6106. const auto query = TStringBuilder() << "pragma Blockengine='" << value << "'; select 1;";
  6107. NYql::TAstParseResult res = SqlToYql(query);
  6108. UNIT_ASSERT(res.Root);
  6109. TVerifyLineFunc verifyLine = [&](const TString& word, const TString& line) {
  6110. Y_UNUSED(word);
  6111. UNIT_ASSERT_STRING_CONTAINS(line, TStringBuilder() << R"(Configure! world (DataSource '"config") '"BlockEngine" '")" << value << "\"");
  6112. };
  6113. TWordCountHive elementStat({"BlockEngine"});
  6114. VerifyProgram(res, elementStat, verifyLine);
  6115. UNIT_ASSERT(elementStat["BlockEngine"] == ((value == "disable") ? 0 : 1));
  6116. }
  6117. }
  6118. Y_UNIT_TEST(UnknownSetting) {
  6119. ExpectFailWithError("use plato; pragma BlockEngine='foo';",
  6120. "<main>:1:31: Error: Expected `disable|auto|force' argument for: BlockEngine\n");
  6121. }
  6122. }
  6123. Y_UNIT_TEST_SUITE(TViewSyntaxTest) {
  6124. Y_UNIT_TEST(CreateViewSimple) {
  6125. NYql::TAstParseResult res = SqlToYql(R"(
  6126. USE plato;
  6127. CREATE VIEW TheView WITH (security_invoker = TRUE) AS SELECT 1;
  6128. )"
  6129. );
  6130. UNIT_ASSERT_C(res.Root, res.Issues.ToString());
  6131. }
  6132. Y_UNIT_TEST(CreateViewIfNotExists) {
  6133. constexpr const char* name = "TheView";
  6134. NYql::TAstParseResult res = SqlToYql(std::format(R"(
  6135. USE plato;
  6136. CREATE VIEW IF NOT EXISTS {} WITH (security_invoker = TRUE) AS SELECT 1;
  6137. )", name
  6138. ));
  6139. UNIT_ASSERT_C(res.Root, res.Issues.ToString());
  6140. TVerifyLineFunc verifyLine = [&](const TString& word, const TString& line) {
  6141. if (word == "Write!") {
  6142. UNIT_ASSERT_STRING_CONTAINS(line, name);
  6143. UNIT_ASSERT_STRING_CONTAINS(line, "createObjectIfNotExists");
  6144. }
  6145. };
  6146. TWordCountHive elementStat = { {"Write!"} };
  6147. VerifyProgram(res, elementStat, verifyLine);
  6148. UNIT_ASSERT_VALUES_EQUAL(elementStat["Write!"], 1);
  6149. }
  6150. Y_UNIT_TEST(CreateViewFromTable) {
  6151. constexpr const char* path = "/PathPrefix/TheView";
  6152. constexpr const char* query = R"(
  6153. SELECT * FROM SomeTable
  6154. )";
  6155. NYql::TAstParseResult res = SqlToYql(std::format(R"(
  6156. USE plato;
  6157. CREATE VIEW `{}` WITH (security_invoker = TRUE) AS {};
  6158. )",
  6159. path,
  6160. query
  6161. )
  6162. );
  6163. UNIT_ASSERT_C(res.Root, res.Issues.ToString());
  6164. TVerifyLineFunc verifyLine = [&](const TString& word, const TString& line) {
  6165. if (word == "Write!") {
  6166. UNIT_ASSERT_STRING_CONTAINS(line, path);
  6167. UNIT_ASSERT_STRING_CONTAINS(line, "createObject");
  6168. }
  6169. };
  6170. TWordCountHive elementStat = { {"Write!"} };
  6171. VerifyProgram(res, elementStat, verifyLine);
  6172. UNIT_ASSERT_VALUES_EQUAL(elementStat["Write!"], 1);
  6173. }
  6174. Y_UNIT_TEST(CheckReconstructedQuery) {
  6175. constexpr const char* path = "/PathPrefix/TheView";
  6176. constexpr const char* query = R"(
  6177. SELECT * FROM FirstTable JOIN SecondTable ON FirstTable.key == SecondTable.key
  6178. )";
  6179. NYql::TAstParseResult res = SqlToYql(std::format(R"(
  6180. USE plato;
  6181. CREATE VIEW `{}` WITH (security_invoker = TRUE) AS {};
  6182. )",
  6183. path,
  6184. query
  6185. )
  6186. );
  6187. UNIT_ASSERT_C(res.Root, res.Issues.ToString());
  6188. TString reconstructedQuery = ToString(Tokenize(query));
  6189. TVerifyLineFunc verifyLine = [&](const TString& word, const TString& line) {
  6190. if (word == "query_text") {
  6191. UNIT_ASSERT_STRING_CONTAINS(line, reconstructedQuery);
  6192. }
  6193. };
  6194. TWordCountHive elementStat = { {"Write!"} };
  6195. VerifyProgram(res, elementStat, verifyLine);
  6196. UNIT_ASSERT_VALUES_EQUAL(elementStat["Write!"], 1);
  6197. }
  6198. Y_UNIT_TEST(DropView) {
  6199. constexpr const char* path = "/PathPrefix/TheView";
  6200. NYql::TAstParseResult res = SqlToYql(std::format(R"(
  6201. USE plato;
  6202. DROP VIEW `{}`;
  6203. )",
  6204. path
  6205. )
  6206. );
  6207. UNIT_ASSERT_C(res.Root, res.Issues.ToString());
  6208. TVerifyLineFunc verifyLine = [&](const TString& word, const TString& line) {
  6209. if (word == "Write!") {
  6210. UNIT_ASSERT_STRING_CONTAINS(line, path);
  6211. UNIT_ASSERT_STRING_CONTAINS(line, "dropObject");
  6212. }
  6213. };
  6214. TWordCountHive elementStat = { {"Write!"} };
  6215. VerifyProgram(res, elementStat, verifyLine);
  6216. UNIT_ASSERT_VALUES_EQUAL(elementStat["Write!"], 1);
  6217. }
  6218. Y_UNIT_TEST(DropViewIfExists) {
  6219. constexpr const char* name = "TheView";
  6220. NYql::TAstParseResult res = SqlToYql(std::format(R"(
  6221. USE plato;
  6222. DROP VIEW IF EXISTS {};
  6223. )", name
  6224. ));
  6225. UNIT_ASSERT_C(res.Root, res.Issues.ToString());
  6226. TVerifyLineFunc verifyLine = [&](const TString& word, const TString& line) {
  6227. if (word == "Write!") {
  6228. UNIT_ASSERT_STRING_CONTAINS(line, name);
  6229. UNIT_ASSERT_STRING_CONTAINS(line, "dropObjectIfExists");
  6230. }
  6231. };
  6232. TWordCountHive elementStat = { {"Write!"} };
  6233. VerifyProgram(res, elementStat, verifyLine);
  6234. UNIT_ASSERT_VALUES_EQUAL(elementStat["Write!"], 1);
  6235. }
  6236. Y_UNIT_TEST(CreateViewWithTablePrefix) {
  6237. NYql::TAstParseResult res = SqlToYql(R"(
  6238. USE plato;
  6239. PRAGMA TablePathPrefix='/PathPrefix';
  6240. CREATE VIEW TheView WITH (security_invoker = TRUE) AS SELECT 1;
  6241. )"
  6242. );
  6243. UNIT_ASSERT_C(res.Root, res.Issues.ToString());
  6244. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  6245. if (word == "Write!") {
  6246. UNIT_ASSERT_STRING_CONTAINS(line, "/PathPrefix/TheView");
  6247. UNIT_ASSERT_STRING_CONTAINS(line, "createObject");
  6248. }
  6249. };
  6250. TWordCountHive elementStat = { {"Write!"} };
  6251. VerifyProgram(res, elementStat, verifyLine);
  6252. UNIT_ASSERT_VALUES_EQUAL(elementStat["Write!"], 1);
  6253. }
  6254. Y_UNIT_TEST(DropViewWithTablePrefix) {
  6255. NYql::TAstParseResult res = SqlToYql(R"(
  6256. USE plato;
  6257. PRAGMA TablePathPrefix='/PathPrefix';
  6258. DROP VIEW TheView;
  6259. )"
  6260. );
  6261. UNIT_ASSERT_C(res.Root, res.Issues.ToString());
  6262. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  6263. if (word == "Write") {
  6264. UNIT_ASSERT_STRING_CONTAINS(line, "/PathPrefix/TheView");
  6265. UNIT_ASSERT_STRING_CONTAINS(line, "dropObject");
  6266. }
  6267. };
  6268. TWordCountHive elementStat = { {"Write!"} };
  6269. VerifyProgram(res, elementStat, verifyLine);
  6270. UNIT_ASSERT_VALUES_EQUAL(elementStat["Write!"], 1);
  6271. }
  6272. Y_UNIT_TEST(YtAlternativeSchemaSyntax) {
  6273. NYql::TAstParseResult res = SqlToYql(R"(
  6274. SELECT * FROM plato.Input WITH schema(y Int32, x String not null);
  6275. )");
  6276. UNIT_ASSERT_C(res.Root, res.Issues.ToString());
  6277. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  6278. if (word == "userschema") {
  6279. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos,
  6280. line.find(R"__('('('"userschema" (StructType '('"y" (AsOptionalType (DataType 'Int32))) '('"x" (DataType 'String))))))__"));
  6281. }
  6282. };
  6283. TWordCountHive elementStat = {{TString("userschema"), 0}};
  6284. VerifyProgram(res, elementStat, verifyLine);
  6285. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["userschema"]);
  6286. }
  6287. Y_UNIT_TEST(UseViewAndFullColumnId) {
  6288. NYql::TAstParseResult res = SqlToYql("USE plato; SELECT Input.x FROM Input VIEW uitzicht;");
  6289. UNIT_ASSERT(res.Root);
  6290. TWordCountHive elementStat = {{TString("SqlAccess"), 0}, {"SqlProjectItem", 0}, {"Read!", 0}};
  6291. VerifyProgram(res, elementStat);
  6292. UNIT_ASSERT_VALUES_EQUAL(0, elementStat["SqlAccess"]);
  6293. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["SqlProjectItem"]);
  6294. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Read!"]);
  6295. }
  6296. }
  6297. Y_UNIT_TEST_SUITE(CompactNamedExprs) {
  6298. Y_UNIT_TEST(SourceCallablesInWrongContext) {
  6299. TString query = R"(
  6300. pragma CompactNamedExprs;
  6301. $foo = %s();
  6302. select $foo from plato.Input;
  6303. )";
  6304. THashMap<TString, TString> errs = {
  6305. {"TableRow", "<main>:3:20: Error: TableRow requires data source\n"},
  6306. {"JoinTableRow", "<main>:3:20: Error: JoinTableRow requires data source\n"},
  6307. {"TableRecordIndex", "<main>:3:20: Error: Unable to use function: TableRecord without source\n"},
  6308. {"TablePath", "<main>:3:20: Error: Unable to use function: TablePath without source\n"},
  6309. {"SystemMetadata", "<main>:3:20: Error: Unable to use function: SystemMetadata without source\n"},
  6310. };
  6311. for (TString callable : { "TableRow", "JoinTableRow", "TableRecordIndex", "TablePath", "SystemMetadata"}) {
  6312. auto req = Sprintf(query.c_str(), callable.c_str());
  6313. ExpectFailWithError(req, errs[callable]);
  6314. }
  6315. }
  6316. Y_UNIT_TEST(ValidateUnusedExprs) {
  6317. TString query = R"(
  6318. pragma warning("disable", "4527");
  6319. pragma CompactNamedExprs;
  6320. pragma ValidateUnusedExprs;
  6321. $foo = count(1);
  6322. select 1;
  6323. )";
  6324. ExpectFailWithError(query, "<main>:6:20: Error: Aggregation is not allowed in this context\n");
  6325. query = R"(
  6326. pragma warning("disable", "4527");
  6327. pragma CompactNamedExprs;
  6328. pragma ValidateUnusedExprs;
  6329. define subquery $x() as
  6330. select count(1, 2);
  6331. end define;
  6332. select 1;
  6333. )";
  6334. ExpectFailWithError(query, "<main>:7:24: Error: Aggregation function Count requires exactly 1 argument(s), given: 2\n");
  6335. }
  6336. Y_UNIT_TEST(DisableValidateUnusedExprs) {
  6337. TString query = R"(
  6338. pragma warning("disable", "4527");
  6339. pragma CompactNamedExprs;
  6340. pragma DisableValidateUnusedExprs;
  6341. $foo = count(1);
  6342. select 1;
  6343. )";
  6344. SqlToYql(query).IsOk();
  6345. query = R"(
  6346. pragma warning("disable", "4527");
  6347. pragma CompactNamedExprs;
  6348. pragma DisableValidateUnusedExprs;
  6349. define subquery $x() as
  6350. select count(1, 2);
  6351. end define;
  6352. select 1;
  6353. )";
  6354. SqlToYql(query).IsOk();
  6355. }
  6356. }
  6357. Y_UNIT_TEST_SUITE(ResourcePool) {
  6358. Y_UNIT_TEST(CreateResourcePool) {
  6359. NYql::TAstParseResult res = SqlToYql(R"sql(
  6360. USE plato;
  6361. CREATE RESOURCE POOL MyResourcePool WITH (
  6362. CONCURRENT_QUERY_LIMIT=20,
  6363. QUERY_CANCEL_AFTER_SECONDS=86400,
  6364. QUEUE_TYPE="FIFO"
  6365. );
  6366. )sql");
  6367. UNIT_ASSERT_C(res.Root, res.Issues.ToString());
  6368. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  6369. if (word == "Write") {
  6370. UNIT_ASSERT_STRING_CONTAINS(line, R"#('('('"concurrent_query_limit" (Int32 '"20")) '('"query_cancel_after_seconds" (Int32 '"86400")) '('"queue_type" '"FIFO"))#");
  6371. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("createObject"));
  6372. }
  6373. };
  6374. TWordCountHive elementStat = { {TString("Write"), 0} };
  6375. VerifyProgram(res, elementStat, verifyLine);
  6376. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  6377. }
  6378. Y_UNIT_TEST(CreateResourcePoolWithBadArguments) {
  6379. ExpectFailWithError(R"sql(
  6380. USE plato;
  6381. CREATE RESOURCE POOL MyResourcePool;
  6382. )sql" , "<main>:3:51: Error: Unexpected token ';' : syntax error...\n\n");
  6383. ExpectFailWithError(R"sql(
  6384. USE plato;
  6385. CREATE RESOURCE POOL MyResourcePool WITH (
  6386. DUPLICATE_SETTING="first_value",
  6387. DUPLICATE_SETTING="second_value"
  6388. );
  6389. )sql" , "<main>:5:21: Error: DUPLICATE_SETTING duplicate keys\n");
  6390. }
  6391. Y_UNIT_TEST(AlterResourcePool) {
  6392. NYql::TAstParseResult res = SqlToYql(R"sql(
  6393. USE plato;
  6394. ALTER RESOURCE POOL MyResourcePool
  6395. SET (CONCURRENT_QUERY_LIMIT = 30, Weight = 5, QUEUE_TYPE = "UNORDERED"),
  6396. RESET (Query_Cancel_After_Seconds, Query_Count_Limit);
  6397. )sql");
  6398. UNIT_ASSERT_C(res.Root, res.Issues.ToString());
  6399. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  6400. if (word == "Write") {
  6401. UNIT_ASSERT_STRING_CONTAINS(line, R"#(('mode 'alterObject))#");
  6402. UNIT_ASSERT_STRING_CONTAINS(line, R"#('('features '('('"concurrent_query_limit" (Int32 '"30")) '('"queue_type" '"UNORDERED") '('"weight" (Int32 '"5")))))#");
  6403. UNIT_ASSERT_STRING_CONTAINS(line, R"#('('resetFeatures '('"query_cancel_after_seconds" '"query_count_limit")))#");
  6404. }
  6405. };
  6406. TWordCountHive elementStat = { {TString("Write"), 0} };
  6407. VerifyProgram(res, elementStat, verifyLine);
  6408. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  6409. }
  6410. Y_UNIT_TEST(DropResourcePool) {
  6411. NYql::TAstParseResult res = SqlToYql(R"sql(
  6412. USE plato;
  6413. DROP RESOURCE POOL MyResourcePool;
  6414. )sql");
  6415. UNIT_ASSERT(res.Root);
  6416. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  6417. if (word == "Write") {
  6418. UNIT_ASSERT_VALUES_EQUAL(TString::npos, line.find("'features"));
  6419. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("dropObject"));
  6420. }
  6421. };
  6422. TWordCountHive elementStat = { {TString("Write"), 0}};
  6423. VerifyProgram(res, elementStat, verifyLine);
  6424. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  6425. }
  6426. }
  6427. Y_UNIT_TEST_SUITE(BackupCollection) {
  6428. Y_UNIT_TEST(CreateBackupCollection) {
  6429. NYql::TAstParseResult res = SqlToYql(R"sql(
  6430. USE plato;
  6431. CREATE BACKUP COLLECTION TestCollection WITH (
  6432. STORAGE="local",
  6433. TAG="test" -- for testing purposes, not a real thing
  6434. );
  6435. )sql");
  6436. UNIT_ASSERT_C(res.Root, res.Issues.ToString());
  6437. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  6438. if (word == "Write") {
  6439. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find(R"#('"TestCollection")#"));
  6440. UNIT_ASSERT_STRING_CONTAINS(line, R"#(('"storage" (String '"local")))#");
  6441. UNIT_ASSERT_STRING_CONTAINS(line, R"#(('"tag" (String '"test"))))#");
  6442. UNIT_ASSERT_STRING_CONTAINS(line, R"#(('entries '()))#");
  6443. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("'create"));
  6444. }
  6445. };
  6446. TWordCountHive elementStat = { {TString("Write"), 0} };
  6447. VerifyProgram(res, elementStat, verifyLine);
  6448. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  6449. }
  6450. Y_UNIT_TEST(CreateBackupCollectionWithDatabase) {
  6451. NYql::TAstParseResult res = SqlToYql(R"sql(
  6452. USE plato;
  6453. CREATE BACKUP COLLECTION TestCollection DATABASE WITH (
  6454. STORAGE="local",
  6455. TAG="test" -- for testing purposes, not a real thing
  6456. );
  6457. )sql");
  6458. UNIT_ASSERT_C(res.Root, res.Issues.ToString());
  6459. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  6460. if (word == "Write") {
  6461. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find(R"#('"TestCollection")#"));
  6462. UNIT_ASSERT_STRING_CONTAINS(line, R"#(('"storage" (String '"local")))#");
  6463. UNIT_ASSERT_STRING_CONTAINS(line, R"#(('"tag" (String '"test"))))#");
  6464. UNIT_ASSERT_STRING_CONTAINS(line, R"#(('entries '('('('type 'database)))))#");
  6465. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("'create"));
  6466. }
  6467. };
  6468. TWordCountHive elementStat = { {TString("Write"), 0} };
  6469. VerifyProgram(res, elementStat, verifyLine);
  6470. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  6471. }
  6472. Y_UNIT_TEST(CreateBackupCollectionWithTables) {
  6473. NYql::TAstParseResult res = SqlToYql(R"sql(
  6474. USE plato;
  6475. CREATE BACKUP COLLECTION TestCollection (
  6476. TABLE someTable,
  6477. TABLE `prefix/anotherTable`
  6478. ) WITH (
  6479. STORAGE="local",
  6480. TAG="test" -- for testing purposes, not a real thing
  6481. );
  6482. )sql");
  6483. UNIT_ASSERT_C(res.Root, res.Issues.ToString());
  6484. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  6485. if (word == "Write") {
  6486. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find(R"#('"TestCollection")#"));
  6487. UNIT_ASSERT_STRING_CONTAINS(line, R"#(('"storage" (String '"local")))#");
  6488. UNIT_ASSERT_STRING_CONTAINS(line, R"#(('"tag" (String '"test"))))#");
  6489. UNIT_ASSERT_STRING_CONTAINS(line, R"#(('entries '('('('type 'table) '('path '"someTable")) '('('type 'table) '('path '"prefix/anotherTable")))))#");
  6490. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("'create"));
  6491. }
  6492. };
  6493. TWordCountHive elementStat = { {TString("Write"), 0} };
  6494. VerifyProgram(res, elementStat, verifyLine);
  6495. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  6496. }
  6497. Y_UNIT_TEST(CreateBackupCollectionWithBadArguments) {
  6498. ExpectFailWithError(R"sql(
  6499. USE plato;
  6500. CREATE BACKUP COLLECTION TestCollection;
  6501. )sql" , "<main>:3:55: Error: Unexpected token ';' : syntax error...\n\n");
  6502. ExpectFailWithError(R"sql(
  6503. USE plato;
  6504. CREATE BACKUP COLLECTION TABLE TestCollection;
  6505. )sql" , "<main>:3:47: Error: Unexpected token 'TestCollection' : syntax error...\n\n");
  6506. ExpectFailWithError(R"sql(
  6507. USE plato;
  6508. CREATE BACKUP COLLECTION DATABASE `test` TestCollection;
  6509. )sql" , "<main>:3:50: Error: Unexpected token '`test`' : syntax error...\n\n");
  6510. ExpectFailWithError(R"sql(
  6511. USE plato;
  6512. CREATE BACKUP COLLECTION TestCollection WITH (
  6513. DUPLICATE_SETTING="first_value",
  6514. DUPLICATE_SETTING="second_value"
  6515. );
  6516. )sql" , "<main>:5:21: Error: DUPLICATE_SETTING duplicate keys\n");
  6517. ExpectFailWithError(R"sql(
  6518. USE plato;
  6519. CREATE BACKUP COLLECTION TestCollection WITH (
  6520. INT_SETTING=1
  6521. );
  6522. )sql" , "<main>:4:21: Error: INT_SETTING value should be a string literal\n");
  6523. }
  6524. Y_UNIT_TEST(AlterBackupCollection) {
  6525. NYql::TAstParseResult res = SqlToYql(R"sql(
  6526. USE plato;
  6527. ALTER BACKUP COLLECTION TestCollection
  6528. SET (STORAGE="remote"), -- also just for test
  6529. SET (TAG1 = "123"),
  6530. RESET (TAG2, TAG3);
  6531. )sql");
  6532. UNIT_ASSERT_C(res.Root, res.Issues.ToString());
  6533. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  6534. if (word == "Write") {
  6535. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find(R"#('"TestCollection")#"));
  6536. UNIT_ASSERT_STRING_CONTAINS(line, R"#(('mode 'alter))#");
  6537. UNIT_ASSERT_STRING_CONTAINS(line, R"#(('"storage" (String '"remote")))#");
  6538. UNIT_ASSERT_STRING_CONTAINS(line, R"#(('"tag1" (String '"123"))))#");
  6539. UNIT_ASSERT_STRING_CONTAINS(line, R"#('('resetSettings '('"tag2" '"tag3")))#");
  6540. }
  6541. };
  6542. TWordCountHive elementStat = { {TString("Write"), 0} };
  6543. VerifyProgram(res, elementStat, verifyLine);
  6544. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  6545. }
  6546. Y_UNIT_TEST(AlterBackupCollectionEntries) {
  6547. NYql::TAstParseResult res = SqlToYql(R"sql(
  6548. USE plato;
  6549. ALTER BACKUP COLLECTION TestCollection
  6550. DROP TABLE `test`,
  6551. ADD DATABASE;
  6552. )sql");
  6553. UNIT_ASSERT_C(res.Root, res.Issues.ToString());
  6554. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  6555. if (word == "Write") {
  6556. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find(R"#('"TestCollection")#"));
  6557. UNIT_ASSERT_STRING_CONTAINS(line, R"#(('mode 'alter))#");
  6558. UNIT_ASSERT_STRING_CONTAINS(line, R"#('alterEntries)#");
  6559. UNIT_ASSERT_STRING_CONTAINS(line, R"#('('('type 'table) '('path '"test") '('action 'drop)))#");
  6560. UNIT_ASSERT_STRING_CONTAINS(line, R"#('('('type 'database) '('action 'add)))#");
  6561. }
  6562. };
  6563. TWordCountHive elementStat = { {TString("Write"), 0} };
  6564. VerifyProgram(res, elementStat, verifyLine);
  6565. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  6566. }
  6567. Y_UNIT_TEST(DropBackupCollection) {
  6568. NYql::TAstParseResult res = SqlToYql(R"sql(
  6569. USE plato;
  6570. DROP BACKUP COLLECTION TestCollection;
  6571. )sql");
  6572. UNIT_ASSERT(res.Root);
  6573. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  6574. if (word == "Write") {
  6575. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find(R"#('"TestCollection")#"));
  6576. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("drop"));
  6577. }
  6578. };
  6579. TWordCountHive elementStat = { {TString("Write"), 0}};
  6580. VerifyProgram(res, elementStat, verifyLine);
  6581. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  6582. }
  6583. }
  6584. Y_UNIT_TEST_SUITE(ResourcePoolClassifier) {
  6585. Y_UNIT_TEST(CreateResourcePoolClassifier) {
  6586. NYql::TAstParseResult res = SqlToYql(R"sql(
  6587. USE plato;
  6588. CREATE RESOURCE POOL CLASSIFIER MyResourcePoolClassifier WITH (
  6589. RANK=20,
  6590. RESOURCE_POOL='wgUserQueries',
  6591. MEMBER_NAME='yandex_query@abc'
  6592. );
  6593. )sql");
  6594. UNIT_ASSERT_C(res.Root, res.Issues.ToString());
  6595. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  6596. if (word == "Write") {
  6597. UNIT_ASSERT_STRING_CONTAINS(line, R"#('('('"member_name" '"yandex_query@abc") '('"rank" (Int32 '"20")) '('"resource_pool" '"wgUserQueries"))#");
  6598. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("createObject"));
  6599. }
  6600. };
  6601. TWordCountHive elementStat = { {TString("Write"), 0} };
  6602. VerifyProgram(res, elementStat, verifyLine);
  6603. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  6604. }
  6605. Y_UNIT_TEST(CreateResourcePoolClassifierWithBadArguments) {
  6606. ExpectFailWithError(R"sql(
  6607. USE plato;
  6608. CREATE RESOURCE POOL CLASSIFIER MyResourcePoolClassifier;
  6609. )sql" , "<main>:3:72: Error: Unexpected token ';' : syntax error...\n\n");
  6610. ExpectFailWithError(R"sql(
  6611. USE plato;
  6612. CREATE RESOURCE POOL CLASSIFIER MyResourcePoolClassifier WITH (
  6613. DUPLICATE_SETTING="first_value",
  6614. DUPLICATE_SETTING="second_value"
  6615. );
  6616. )sql" , "<main>:5:21: Error: DUPLICATE_SETTING duplicate keys\n");
  6617. }
  6618. Y_UNIT_TEST(AlterResourcePoolClassifier) {
  6619. NYql::TAstParseResult res = SqlToYql(R"sql(
  6620. USE plato;
  6621. ALTER RESOURCE POOL CLASSIFIER MyResourcePoolClassifier
  6622. SET (RANK = 30, Weight = 5, MEMBER_NAME = "test@user"),
  6623. RESET (Resource_Pool);
  6624. )sql");
  6625. UNIT_ASSERT_C(res.Root, res.Issues.ToString());
  6626. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  6627. if (word == "Write") {
  6628. UNIT_ASSERT_STRING_CONTAINS(line, R"#(('mode 'alterObject))#");
  6629. UNIT_ASSERT_STRING_CONTAINS(line, R"#('('features '('('"member_name" '"test@user") '('"rank" (Int32 '"30")) '('"weight" (Int32 '"5")))))#");
  6630. UNIT_ASSERT_STRING_CONTAINS(line, R"#('('resetFeatures '('"resource_pool")))#");
  6631. }
  6632. };
  6633. TWordCountHive elementStat = { {TString("Write"), 0} };
  6634. VerifyProgram(res, elementStat, verifyLine);
  6635. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  6636. }
  6637. Y_UNIT_TEST(DropResourcePoolClassifier) {
  6638. NYql::TAstParseResult res = SqlToYql(R"sql(
  6639. USE plato;
  6640. DROP RESOURCE POOL CLASSIFIER MyResourcePoolClassifier;
  6641. )sql");
  6642. UNIT_ASSERT(res.Root);
  6643. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  6644. if (word == "Write") {
  6645. UNIT_ASSERT_VALUES_EQUAL(TString::npos, line.find("'features"));
  6646. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("dropObject"));
  6647. }
  6648. };
  6649. TWordCountHive elementStat = { {TString("Write"), 0}};
  6650. VerifyProgram(res, elementStat, verifyLine);
  6651. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  6652. }
  6653. Y_UNIT_TEST(BacktickMatching) {
  6654. auto req = "select\n"
  6655. " 1 as `Schema has \\`RealCost\\``\n"
  6656. " -- foo`bar";
  6657. auto res = SqlToYql(req);
  6658. UNIT_ASSERT(res.Root);
  6659. UNIT_ASSERT(res.IsOk());
  6660. UNIT_ASSERT(res.Issues.Size() == 0);
  6661. res = SqlToYqlWithAnsiLexer(req);
  6662. UNIT_ASSERT(res.Root);
  6663. UNIT_ASSERT(res.IsOk());
  6664. UNIT_ASSERT(res.Issues.Size() == 0);
  6665. req = "select 1 as `a``b`, 2 as ````, 3 as `\\x60a\\x60`, 4 as ```b```, 5 as `\\`c\\``";
  6666. res = SqlToYql(req);
  6667. UNIT_ASSERT(res.Root);
  6668. UNIT_ASSERT(res.IsOk());
  6669. UNIT_ASSERT(res.Issues.Size() == 0);
  6670. res = SqlToYqlWithAnsiLexer(req);
  6671. UNIT_ASSERT(res.Root);
  6672. UNIT_ASSERT(res.IsOk());
  6673. UNIT_ASSERT(res.Issues.Size() == 0);
  6674. }
  6675. }
  6676. Y_UNIT_TEST_SUITE(OlapPartitionCount) {
  6677. Y_UNIT_TEST(CorrectUsage) {
  6678. NYql::TAstParseResult res = SqlToYql(R"sql(
  6679. USE plato;
  6680. CREATE TABLE `mytable` (id Uint32, PRIMARY KEY (id))
  6681. PARTITION BY HASH(id)
  6682. WITH (STORE = COLUMN, PARTITION_COUNT = 8);
  6683. )sql");
  6684. UNIT_ASSERT_C(res.IsOk(), res.Issues.ToString());
  6685. }
  6686. Y_UNIT_TEST(UseWithoutColumnStore) {
  6687. NYql::TAstParseResult res = SqlToYql(R"sql(
  6688. USE plato;
  6689. CREATE TABLE `mytable` (id Uint32, PRIMARY KEY (id))
  6690. WITH (PARTITION_COUNT = 8);
  6691. )sql");
  6692. UNIT_ASSERT(!res.IsOk());
  6693. UNIT_ASSERT(res.Issues.Size() == 1);
  6694. UNIT_ASSERT_STRING_CONTAINS(res.Issues.ToString(), "PARTITION_COUNT can be used only with STORE=COLUMN");
  6695. }
  6696. }
  6697. Y_UNIT_TEST_SUITE(Backup) {
  6698. Y_UNIT_TEST(Simple) {
  6699. NYql::TAstParseResult res = SqlToYql(R"sql(
  6700. USE plato;
  6701. BACKUP TestCollection;
  6702. )sql");
  6703. UNIT_ASSERT_C(res.Root, res.Issues.ToString());
  6704. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  6705. if (word == "Write") {
  6706. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find(R"#('"TestCollection")#"));
  6707. UNIT_ASSERT_VALUES_EQUAL(TString::npos, line.find("'Incremental"));
  6708. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("'backup"));
  6709. }
  6710. };
  6711. TWordCountHive elementStat = { {TString("Write"), 0} };
  6712. VerifyProgram(res, elementStat, verifyLine);
  6713. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  6714. }
  6715. Y_UNIT_TEST(Incremental) {
  6716. NYql::TAstParseResult res = SqlToYql(R"sql(
  6717. USE plato;
  6718. BACKUP TestCollection INCREMENTAL;
  6719. )sql");
  6720. UNIT_ASSERT_C(res.Root, res.Issues.ToString());
  6721. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  6722. if (word == "Write") {
  6723. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find(R"#('"TestCollection")#"));
  6724. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("'backupIncremental"));
  6725. }
  6726. };
  6727. TWordCountHive elementStat = { {TString("Write"), 0} };
  6728. VerifyProgram(res, elementStat, verifyLine);
  6729. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  6730. }
  6731. }
  6732. Y_UNIT_TEST_SUITE(Restore) {
  6733. Y_UNIT_TEST(Simple) {
  6734. NYql::TAstParseResult res = SqlToYql(R"sql(
  6735. USE plato;
  6736. RESTORE TestCollection;
  6737. )sql");
  6738. UNIT_ASSERT_C(res.Root, res.Issues.ToString());
  6739. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  6740. if (word == "Write") {
  6741. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find(R"#('"TestCollection")#"));
  6742. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("'restore"));
  6743. }
  6744. };
  6745. TWordCountHive elementStat = { {TString("Write"), 0} };
  6746. VerifyProgram(res, elementStat, verifyLine);
  6747. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  6748. }
  6749. Y_UNIT_TEST(AtPoint) {
  6750. NYql::TAstParseResult res = SqlToYql(R"sql(
  6751. USE plato;
  6752. RESTORE TestCollection AT '2024-06-16_20-14-02';
  6753. )sql");
  6754. UNIT_ASSERT_C(res.Root, res.Issues.ToString());
  6755. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  6756. if (word == "Write") {
  6757. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find(R"#('"TestCollection")#"));
  6758. UNIT_ASSERT_STRING_CONTAINS(line, R"#('at '"2024-06-16_20-14-02")#");
  6759. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("'restore"));
  6760. }
  6761. };
  6762. TWordCountHive elementStat = { {TString("Write"), 0} };
  6763. VerifyProgram(res, elementStat, verifyLine);
  6764. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  6765. }
  6766. }
  6767. Y_UNIT_TEST_SUITE(ColumnFamily) {
  6768. Y_UNIT_TEST(CompressionLevelCorrectUsage) {
  6769. NYql::TAstParseResult res = SqlToYql(R"( use plato;
  6770. CREATE TABLE tableName (
  6771. Key Uint32 FAMILY default,
  6772. Value String FAMILY family1,
  6773. PRIMARY KEY (Key),
  6774. FAMILY default (
  6775. DATA = "test",
  6776. COMPRESSION = "lz4",
  6777. COMPRESSION_LEVEL = 5
  6778. ),
  6779. FAMILY family1 (
  6780. DATA = "test",
  6781. COMPRESSION = "lz4",
  6782. COMPRESSION_LEVEL = 3
  6783. )
  6784. );
  6785. )");
  6786. UNIT_ASSERT(res.IsOk());
  6787. UNIT_ASSERT(res.Issues.Size() == 0);
  6788. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  6789. if (word == "Write") {
  6790. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("compression_level"));
  6791. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("5"));
  6792. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("3"));
  6793. }
  6794. };
  6795. TWordCountHive elementStat = { { TString("Write"), 0 }, { TString("compression_level"), 0 } };
  6796. VerifyProgram(res, elementStat, verifyLine);
  6797. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  6798. UNIT_ASSERT_VALUES_EQUAL(2, elementStat["compression_level"]);
  6799. }
  6800. Y_UNIT_TEST(FieldDataIsNotString) {
  6801. NYql::TAstParseResult res = SqlToYql(R"( use plato;
  6802. CREATE TABLE tableName (
  6803. Key Uint32 FAMILY default,
  6804. PRIMARY KEY (Key),
  6805. FAMILY default (
  6806. DATA = 1,
  6807. COMPRESSION = "lz4",
  6808. COMPRESSION_LEVEL = 5
  6809. )
  6810. );
  6811. )");
  6812. UNIT_ASSERT(!res.IsOk());
  6813. UNIT_ASSERT(res.Issues.Size() == 1);
  6814. UNIT_ASSERT_STRING_CONTAINS(res.Issues.ToString(), "DATA value should be a string literal");
  6815. }
  6816. Y_UNIT_TEST(FieldCompressionIsNotString) {
  6817. NYql::TAstParseResult res = SqlToYql(R"( use plato;
  6818. CREATE TABLE tableName (
  6819. Key Uint32 FAMILY default,
  6820. PRIMARY KEY (Key),
  6821. FAMILY default (
  6822. DATA = "test",
  6823. COMPRESSION = 2,
  6824. COMPRESSION_LEVEL = 5
  6825. ),
  6826. );
  6827. )");
  6828. UNIT_ASSERT(!res.IsOk());
  6829. UNIT_ASSERT(res.Issues.Size() == 1);
  6830. UNIT_ASSERT_STRING_CONTAINS(res.Issues.ToString(), "COMPRESSION value should be a string literal");
  6831. }
  6832. Y_UNIT_TEST(FieldCompressionLevelIsNotInteger) {
  6833. NYql::TAstParseResult res = SqlToYql(R"( use plato;
  6834. CREATE TABLE tableName (
  6835. Key Uint32 FAMILY default,
  6836. PRIMARY KEY (Key),
  6837. FAMILY default (
  6838. DATA = "test",
  6839. COMPRESSION = "lz4",
  6840. COMPRESSION_LEVEL = "5"
  6841. )
  6842. );
  6843. )");
  6844. UNIT_ASSERT(!res.IsOk());
  6845. UNIT_ASSERT(res.Issues.Size() == 1);
  6846. UNIT_ASSERT_STRING_CONTAINS(res.Issues.ToString(), "COMPRESSION_LEVEL value should be an integer");
  6847. }
  6848. Y_UNIT_TEST(AlterCompressionCorrectUsage) {
  6849. NYql::TAstParseResult res = SqlToYql(R"( use plato;
  6850. ALTER TABLE tableName ALTER FAMILY default SET COMPRESSION "lz4";
  6851. )");
  6852. UNIT_ASSERT(res.IsOk());
  6853. UNIT_ASSERT(res.Issues.Size() == 0);
  6854. }
  6855. Y_UNIT_TEST(AlterCompressionFieldIsNotString) {
  6856. NYql::TAstParseResult res = SqlToYql(R"( use plato;
  6857. ALTER TABLE tableName ALTER FAMILY default SET COMPRESSION lz4;
  6858. )");
  6859. UNIT_ASSERT(!res.IsOk());
  6860. UNIT_ASSERT(res.Issues.Size() == 1);
  6861. UNIT_ASSERT_STRING_CONTAINS(res.Issues.ToString(), "Unexpected token 'lz4' : cannot match to any predicted input");
  6862. }
  6863. Y_UNIT_TEST(AlterCompressionLevelCorrectUsage) {
  6864. NYql::TAstParseResult res = SqlToYql(R"( use plato;
  6865. ALTER TABLE tableName ALTER FAMILY default SET COMPRESSION_LEVEL 5;
  6866. )");
  6867. UNIT_ASSERT(res.IsOk());
  6868. UNIT_ASSERT(res.Issues.Size() == 0);
  6869. }
  6870. Y_UNIT_TEST(AlterCompressionLevelFieldIsNotInteger) {
  6871. NYql::TAstParseResult res = SqlToYql(R"( use plato;
  6872. ALTER TABLE tableName ALTER FAMILY default SET COMPRESSION_LEVEL "5";
  6873. )");
  6874. UNIT_ASSERT(!res.IsOk());
  6875. UNIT_ASSERT(res.Issues.Size() == 1);
  6876. UNIT_ASSERT_STRING_CONTAINS(res.Issues.ToString(), "COMPRESSION_LEVEL value should be an integer");
  6877. }
  6878. }
  6879. Y_UNIT_TEST_SUITE(QuerySplit) {
  6880. Y_UNIT_TEST(Simple) {
  6881. TString query = R"(
  6882. ;
  6883. -- Comment 1
  6884. SELECT * From Input; -- Comment 2
  6885. -- Comment 3
  6886. $a = "a";
  6887. -- Comment 9
  6888. ;
  6889. -- Comment 10
  6890. -- Comment 8
  6891. $b = ($x) -> {
  6892. -- comment 4
  6893. return /* Comment 5 */ $x;
  6894. -- Comment 6
  6895. };
  6896. // Comment 7
  6897. )";
  6898. google::protobuf::Arena Arena;
  6899. NSQLTranslation::TTranslationSettings settings;
  6900. settings.AnsiLexer = false;
  6901. settings.Antlr4Parser = true;
  6902. settings.Arena = &Arena;
  6903. TVector<TString> statements;
  6904. NYql::TIssues issues;
  6905. UNIT_ASSERT(NSQLTranslationV1::SplitQueryToStatements(query, statements, issues, settings));
  6906. UNIT_ASSERT_VALUES_EQUAL(statements.size(), 3);
  6907. UNIT_ASSERT_VALUES_EQUAL(statements[0], "-- Comment 1\n SELECT * From Input; -- Comment 2\n");
  6908. UNIT_ASSERT_VALUES_EQUAL(statements[1], R"(-- Comment 3
  6909. $a = "a";)");
  6910. UNIT_ASSERT_VALUES_EQUAL(statements[2], R"(-- Comment 10
  6911. -- Comment 8
  6912. $b = ($x) -> {
  6913. -- comment 4
  6914. return /* Comment 5 */ $x;
  6915. -- Comment 6
  6916. };)");
  6917. }
  6918. }
  6919. Y_UNIT_TEST_SUITE(MatchRecognizeMeasuresAggregation) {
  6920. Y_UNIT_TEST(InsideSelect) {
  6921. ExpectFailWithError(R"sql(
  6922. SELECT FIRST(0), LAST(1);
  6923. )sql",
  6924. "<main>:2:20: Error: Cannot use FIRST and LAST outside the MATCH_RECOGNIZE context\n"
  6925. "<main>:2:30: Error: Cannot use FIRST and LAST outside the MATCH_RECOGNIZE context\n"
  6926. );
  6927. }
  6928. Y_UNIT_TEST(OutsideSelect) {
  6929. ExpectFailWithError(R"sql(
  6930. $lambda = ($x) -> (FIRST($x) + LAST($x));
  6931. SELECT $lambda(x) FROM plato.Input;
  6932. )sql",
  6933. "<main>:2:32: Error: Cannot use FIRST and LAST outside the MATCH_RECOGNIZE context\n"
  6934. "<main>:2:44: Error: Cannot use FIRST and LAST outside the MATCH_RECOGNIZE context\n"
  6935. );
  6936. }
  6937. Y_UNIT_TEST(AsAggregateFunction) {
  6938. ExpectFailWithError(R"sql(
  6939. SELECT FIRST(x), LAST(x) FROM plato.Input;
  6940. )sql",
  6941. "<main>:2:20: Error: Cannot use FIRST and LAST outside the MATCH_RECOGNIZE context\n"
  6942. "<main>:2:30: Error: Cannot use FIRST and LAST outside the MATCH_RECOGNIZE context\n"
  6943. );
  6944. }
  6945. Y_UNIT_TEST(AsWindowFunction) {
  6946. ExpectFailWithError(R"sql(
  6947. SELECT FIRST(x) OVER(), LAST(x) OVER() FROM plato.Input;
  6948. )sql",
  6949. "<main>:2:20: Error: Cannot use FIRST and LAST outside the MATCH_RECOGNIZE context\n"
  6950. "<main>:2:37: Error: Cannot use FIRST and LAST outside the MATCH_RECOGNIZE context\n"
  6951. );
  6952. }
  6953. }