sql_ut.cpp 349 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220
  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 '('('mode 'alterDatabase) '('dbPath '/Root/test) '('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(OptionalAliases) {
  2465. UNIT_ASSERT(SqlToYql("USE plato; SELECT foo FROM (SELECT key foo FROM Input);").IsOk());
  2466. UNIT_ASSERT(SqlToYql("USE plato; SELECT a.x FROM Input1 a JOIN Input2 b ON a.key = b.key;").IsOk());
  2467. 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());
  2468. }
  2469. Y_UNIT_TEST(TableNameConstness) {
  2470. UNIT_ASSERT(SqlToYql("USE plato; $path = 'foo'; SELECT TableName($path), count(*) FROM Input;").IsOk());
  2471. UNIT_ASSERT(SqlToYql("$path = 'foo'; SELECT TableName($path, 'yt'), count(*) FROM plato.Input;").IsOk());
  2472. ExpectFailWithError("USE plato; SELECT TableName(), count(*) FROM plato.Input;",
  2473. "<main>:1:19: Error: Expression has to be an aggregation function or key column, because aggregation is used elsewhere in this subquery\n");
  2474. }
  2475. Y_UNIT_TEST(UseShouldWorkAsColumnName) {
  2476. UNIT_ASSERT(SqlToYql("select use from (select 1 as use);").IsOk());
  2477. }
  2478. Y_UNIT_TEST(TrueFalseWorkAfterDollar) {
  2479. UNIT_ASSERT(SqlToYql("$ true = false; SELECT $ true or false;").IsOk());
  2480. UNIT_ASSERT(SqlToYql("$False = 0; SELECT $False;").IsOk());
  2481. }
  2482. Y_UNIT_TEST(WithSchemaEquals) {
  2483. UNIT_ASSERT(SqlToYql("select * from plato.T with schema Struct<a:Int32, b:String>;").IsOk());
  2484. UNIT_ASSERT(SqlToYql("select * from plato.T with columns = Struct<a:Int32, b:String>;").IsOk());
  2485. }
  2486. Y_UNIT_TEST(WithNonStructSchemaS3) {
  2487. NSQLTranslation::TTranslationSettings settings;
  2488. settings.ClusterMapping["s3bucket"] = NYql::S3ProviderName;
  2489. UNIT_ASSERT(SqlToYql("select * from s3bucket.`foo` with schema (col1 Int32, String as col2, Int64 as col3);", settings).IsOk());
  2490. }
  2491. Y_UNIT_TEST(AllowNestedTuplesInGroupBy) {
  2492. NYql::TAstParseResult res = SqlToYql("select count(*) from plato.Input group by 1 + (x, y, z);");
  2493. UNIT_ASSERT(res.Root);
  2494. TVerifyLineFunc verifyLine = [&](const TString& word, const TString& line) {
  2495. Y_UNUSED(word);
  2496. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("(Aggregate core '('\"group0\")"));
  2497. };
  2498. TWordCountHive elementStat({"Aggregate"});
  2499. VerifyProgram(res, elementStat, verifyLine);
  2500. UNIT_ASSERT(elementStat["Aggregate"] == 1);
  2501. }
  2502. Y_UNIT_TEST(AllowGroupByWithParens) {
  2503. NYql::TAstParseResult res = SqlToYql("select count(*) from plato.Input group by (x, y as alias1, z);");
  2504. UNIT_ASSERT(res.Root);
  2505. TVerifyLineFunc verifyLine = [&](const TString& word, const TString& line) {
  2506. Y_UNUSED(word);
  2507. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("(Aggregate core '('\"x\" '\"alias1\" '\"z\")"));
  2508. };
  2509. TWordCountHive elementStat({"Aggregate"});
  2510. VerifyProgram(res, elementStat, verifyLine);
  2511. UNIT_ASSERT(elementStat["Aggregate"] == 1);
  2512. }
  2513. Y_UNIT_TEST(CreateAsyncReplicationParseCorrect) {
  2514. auto req = R"(
  2515. USE plato;
  2516. CREATE ASYNC REPLICATION MyReplication
  2517. FOR table1 AS table2, table3 AS table4
  2518. WITH (
  2519. CONNECTION_STRING = "grpc://localhost:2135/?database=/MyDatabase",
  2520. ENDPOINT = "localhost:2135",
  2521. DATABASE = "/MyDatabase"
  2522. );
  2523. )";
  2524. auto res = SqlToYql(req);
  2525. UNIT_ASSERT(res.Root);
  2526. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  2527. if (word == "Write") {
  2528. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("MyReplication"));
  2529. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("create"));
  2530. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("table1"));
  2531. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("table2"));
  2532. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("table3"));
  2533. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("table4"));
  2534. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("connection_string"));
  2535. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("grpc://localhost:2135/?database=/MyDatabase"));
  2536. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("endpoint"));
  2537. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("localhost:2135"));
  2538. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("database"));
  2539. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("/MyDatabase"));
  2540. }
  2541. };
  2542. TWordCountHive elementStat = { {TString("Write"), 0}};
  2543. VerifyProgram(res, elementStat, verifyLine);
  2544. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  2545. }
  2546. Y_UNIT_TEST(CreateAsyncReplicationUnsupportedSettings) {
  2547. auto reqTpl = R"(
  2548. USE plato;
  2549. CREATE ASYNC REPLICATION MyReplication
  2550. FOR table1 AS table2, table3 AS table4
  2551. WITH (
  2552. %s = "%s"
  2553. )
  2554. )";
  2555. auto settings = THashMap<TString, TString>{
  2556. {"STATE", "DONE"},
  2557. {"FAILOVER_MODE", "FORCE"},
  2558. };
  2559. for (const auto& [k, v] : settings) {
  2560. auto req = Sprintf(reqTpl, k.c_str(), v.c_str());
  2561. auto res = SqlToYql(req);
  2562. UNIT_ASSERT(!res.Root);
  2563. UNIT_ASSERT_NO_DIFF(Err2Str(res), Sprintf("<main>:6:%zu: Error: %s is not supported in CREATE\n", 20 + k.size(), k.c_str()));
  2564. }
  2565. }
  2566. Y_UNIT_TEST(AsyncReplicationInvalidCommitInterval) {
  2567. auto req = R"(
  2568. USE plato;
  2569. CREATE ASYNC REPLICATION MyReplication
  2570. FOR table1 AS table2, table3 AS table4
  2571. WITH (
  2572. COMMIT_INTERVAL = "FOO"
  2573. );
  2574. )";
  2575. auto res = SqlToYql(req);
  2576. UNIT_ASSERT(!res.Root);
  2577. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:6:35: Error: Literal of Interval type is expected for COMMIT_INTERVAL\n");
  2578. }
  2579. Y_UNIT_TEST(AlterAsyncReplicationParseCorrect) {
  2580. auto req = R"(
  2581. USE plato;
  2582. ALTER ASYNC REPLICATION MyReplication
  2583. SET (
  2584. STATE = "DONE",
  2585. FAILOVER_MODE = "FORCE"
  2586. );
  2587. )";
  2588. auto res = SqlToYql(req);
  2589. UNIT_ASSERT(res.Root);
  2590. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  2591. if (word == "Write") {
  2592. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("MyReplication"));
  2593. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("alter"));
  2594. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("state"));
  2595. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("DONE"));
  2596. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("failover_mode"));
  2597. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("FORCE"));
  2598. }
  2599. };
  2600. TWordCountHive elementStat = { {TString("Write"), 0}};
  2601. VerifyProgram(res, elementStat, verifyLine);
  2602. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  2603. }
  2604. Y_UNIT_TEST(AlterAsyncReplicationSettings) {
  2605. auto reqTpl = R"(
  2606. USE plato;
  2607. ALTER ASYNC REPLICATION MyReplication
  2608. SET (
  2609. %s = "%s"
  2610. )
  2611. )";
  2612. auto settings = THashMap<TString, TString>{
  2613. {"connection_string", "grpc://localhost:2135/?database=/MyDatabase"},
  2614. {"endpoint", "localhost:2135"},
  2615. {"database", "/MyDatabase"},
  2616. {"token", "foo"},
  2617. {"token_secret_name", "foo_secret_name"},
  2618. {"user", "user"},
  2619. {"password", "bar"},
  2620. {"password_secret_name", "bar_secret_name"},
  2621. };
  2622. for (const auto& [k, v] : settings) {
  2623. auto req = Sprintf(reqTpl, k.c_str(), v.c_str());
  2624. auto res = SqlToYql(req);
  2625. UNIT_ASSERT(res.Root);
  2626. TVerifyLineFunc verifyLine = [&k, &v](const TString& word, const TString& line) {
  2627. if (word == "Write") {
  2628. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("MyReplication"));
  2629. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("alter"));
  2630. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find(k));
  2631. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find(v));
  2632. }
  2633. };
  2634. TWordCountHive elementStat = { {TString("Write"), 0}};
  2635. VerifyProgram(res, elementStat, verifyLine);
  2636. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  2637. }
  2638. }
  2639. Y_UNIT_TEST(AlterAsyncReplicationUnsupportedSettings) {
  2640. {
  2641. auto req = R"(
  2642. USE plato;
  2643. ALTER ASYNC REPLICATION MyReplication SET (CONSISTENCY_LEVEL = "GLOBAL");
  2644. )";
  2645. auto res = SqlToYql(req);
  2646. UNIT_ASSERT(!res.Root);
  2647. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:3:80: Error: CONSISTENCY_LEVEL is not supported in ALTER\n");
  2648. }
  2649. {
  2650. auto req = R"(
  2651. USE plato;
  2652. ALTER ASYNC REPLICATION MyReplication SET (COMMIT_INTERVAL = Interval("PT10S"));
  2653. )";
  2654. auto res = SqlToYql(req);
  2655. UNIT_ASSERT(!res.Root);
  2656. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:3:87: Error: COMMIT_INTERVAL is not supported in ALTER\n");
  2657. }
  2658. }
  2659. Y_UNIT_TEST(AsyncReplicationInvalidSettings) {
  2660. auto req = R"(
  2661. USE plato;
  2662. ALTER ASYNC REPLICATION MyReplication SET (FOO = "BAR");
  2663. )";
  2664. auto res = SqlToYql(req);
  2665. UNIT_ASSERT(!res.Root);
  2666. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:3:62: Error: Unknown replication setting: FOO\n");
  2667. }
  2668. Y_UNIT_TEST(DropAsyncReplicationParseCorrect) {
  2669. auto req = R"(
  2670. USE plato;
  2671. DROP ASYNC REPLICATION MyReplication;
  2672. )";
  2673. auto res = SqlToYql(req);
  2674. UNIT_ASSERT(res.Root);
  2675. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  2676. if (word == "Write") {
  2677. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("MyReplication"));
  2678. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("drop"));
  2679. }
  2680. };
  2681. TWordCountHive elementStat = { {TString("Write"), 0}};
  2682. VerifyProgram(res, elementStat, verifyLine);
  2683. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  2684. }
  2685. Y_UNIT_TEST(DropAsyncReplicationCascade) {
  2686. auto req = R"(
  2687. USE plato;
  2688. DROP ASYNC REPLICATION MyReplication CASCADE;
  2689. )";
  2690. auto res = SqlToYql(req);
  2691. UNIT_ASSERT(res.Root);
  2692. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  2693. if (word == "Write") {
  2694. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("dropCascade"));
  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(PragmaCompactGroupBy) {
  2702. auto req = "PRAGMA CompactGroupBy; SELECT key, COUNT(*) FROM plato.Input GROUP BY key;";
  2703. auto res = SqlToYql(req);
  2704. UNIT_ASSERT(res.Root);
  2705. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  2706. if (word == "Aggregate") {
  2707. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("'('compact)"));
  2708. }
  2709. };
  2710. TWordCountHive elementStat = { {TString("Aggregate"), 0}};
  2711. VerifyProgram(res, elementStat, verifyLine);
  2712. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Aggregate"]);
  2713. }
  2714. Y_UNIT_TEST(PragmaDisableCompactGroupBy) {
  2715. auto req = "PRAGMA DisableCompactGroupBy; SELECT key, COUNT(*) FROM plato.Input GROUP /*+ compact() */ BY key;";
  2716. auto res = SqlToYql(req);
  2717. UNIT_ASSERT(res.Root);
  2718. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  2719. if (word == "Aggregate") {
  2720. UNIT_ASSERT_VALUES_EQUAL(TString::npos, line.find("'('compact)"));
  2721. }
  2722. };
  2723. TWordCountHive elementStat = { {TString("Aggregate"), 0}};
  2724. VerifyProgram(res, elementStat, verifyLine);
  2725. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Aggregate"]);
  2726. }
  2727. Y_UNIT_TEST(AutoSampleWorksWithNamedSubquery) {
  2728. UNIT_ASSERT(SqlToYql("$src = select * from plato.Input; select * from $src sample 0.2").IsOk());
  2729. }
  2730. Y_UNIT_TEST(AutoSampleWorksWithSubquery) {
  2731. UNIT_ASSERT(SqlToYql("select * from (select * from plato.Input) sample 0.2").IsOk());
  2732. }
  2733. Y_UNIT_TEST(CreateTableTrailingComma) {
  2734. UNIT_ASSERT(SqlToYql("USE plato; CREATE TABLE tableName (Key Uint32, PRIMARY KEY (Key),);").IsOk());
  2735. UNIT_ASSERT(SqlToYql("USE plato; CREATE TABLE tableName (Key Uint32,);").IsOk());
  2736. }
  2737. Y_UNIT_TEST(BetweenSymmetric) {
  2738. UNIT_ASSERT(SqlToYql("select 3 between symmetric 5 and 4;").IsOk());
  2739. UNIT_ASSERT(SqlToYql("select 3 between asymmetric 5 and 4;").IsOk());
  2740. UNIT_ASSERT(SqlToYql("use plato; select key between symmetric and and and from Input;").IsOk());
  2741. UNIT_ASSERT(SqlToYql("use plato; select key between and and and from Input;").IsOk());
  2742. }
  2743. }
  2744. Y_UNIT_TEST_SUITE(ExternalFunction) {
  2745. Y_UNIT_TEST(ValidUseFunctions) {
  2746. UNIT_ASSERT(SqlToYql(
  2747. "PROCESS plato.Input"
  2748. " USING EXTERNAL FUNCTION('YANDEX-CLOUD', 'foo', <|a: 123, b: a + 641|>)"
  2749. " WITH INPUT_TYPE=Struct<a:Int32>, OUTPUT_TYPE=Struct<b:Int32>,"
  2750. " CONCURRENCY=3, OPTIMIZE_FOR='CALLS'").IsOk());
  2751. // use CALLS without quotes, as keyword
  2752. UNIT_ASSERT(SqlToYql(
  2753. "PROCESS plato.Input"
  2754. " USING EXTERNAL FUNCTION('YANDEX-CLOUD', 'foo')"
  2755. " WITH INPUT_TYPE=Struct<a:Int32>, OUTPUT_TYPE=Struct<b:Int32>,"
  2756. " OPTIMIZE_FOR=CALLS").IsOk());
  2757. UNIT_ASSERT(SqlToYql(
  2758. "PROCESS plato.Input"
  2759. " USING EXTERNAL FUNCTION('YANDEX-CLOUD', 'foo', TableRow())"
  2760. " WITH INPUT_TYPE=Struct<a:Int32>, OUTPUT_TYPE=Struct<b:Int32>,"
  2761. " CONCURRENCY=3").IsOk());
  2762. UNIT_ASSERT(SqlToYql(
  2763. "PROCESS plato.Input"
  2764. " USING EXTERNAL FUNCTION('YANDEX-CLOUD', 'foo')"
  2765. " WITH INPUT_TYPE=Struct<a:Int32>, OUTPUT_TYPE=Struct<b:Int32>,"
  2766. " CONCURRENCY=3, BATCH_SIZE=1000000, CONNECTION='yc-folder34fse-con',"
  2767. " INIT=[0, 900]").IsOk());
  2768. UNIT_ASSERT(SqlToYql(
  2769. "PROCESS plato.Input"
  2770. " USING EXTERNAL FUNCTION('YANDEX-CLOUD', 'bar', TableRow())"
  2771. " WITH UNKNOWN_PARAM_1='837747712', UNKNOWN_PARAM_2=Tuple<Uint16, Utf8>,"
  2772. " INPUT_TYPE=Struct<a:Int32>, OUTPUT_TYPE=Struct<b:Int32>").IsOk());
  2773. }
  2774. Y_UNIT_TEST(InValidUseFunctions) {
  2775. ExpectFailWithError("PROCESS plato.Input USING some::udf(*) WITH INPUT_TYPE=Struct<a:Int32>",
  2776. "<main>:1:33: Error: PROCESS without USING EXTERNAL FUNCTION doesn't allow WITH block\n");
  2777. ExpectFailWithError("PROCESS plato.Input USING EXTERNAL FUNCTION('YANDEX-CLOUD', 'jhhjfh88134d')"
  2778. " WITH INPUT_TYPE=Struct<a:Int32>, OUTPUT_TYPE=Struct<b:Int32>"
  2779. " ASSUME ORDER BY key",
  2780. "<main>:1:129: Error: PROCESS with USING EXTERNAL FUNCTION doesn't allow ASSUME block\n");
  2781. ExpectFailWithError("PROCESS plato.Input USING EXTERNAL FUNCTION('YANDEX-CLOUD', 'foo', 'bar', 'baz')",
  2782. "<main>:1:15: Error: EXTERNAL FUNCTION requires from 2 to 3 arguments, but got: 4\n");
  2783. ExpectFailWithError("PROCESS plato.Input\n"
  2784. " USING EXTERNAL FUNCTION('YANDEX-CLOUD', 'foo', <|field_1: a1, field_b: b1|>)\n"
  2785. " WITH INPUT_TYPE=Struct<a:Int32>, OUTPUT_TYPE=Struct<b:Int32>,\n"
  2786. " CONCURRENCY=3, BATCH_SIZE=1000000, CONNECTION='yc-folder34fse-con',\n"
  2787. " CONCURRENCY=5, INPUT_TYPE=Struct<b:Bool>,\n"
  2788. " INIT=[0, 900]\n",
  2789. "<main>:5:2: Error: WITH \"CONCURRENCY\" clause should be specified only once\n"
  2790. "<main>:5:17: Error: WITH \"INPUT_TYPE\" clause should be specified only once\n");
  2791. }
  2792. }
  2793. Y_UNIT_TEST_SUITE(SqlToYQLErrors) {
  2794. Y_UNIT_TEST(UdfSyntaxSugarMissingCall) {
  2795. auto req = "SELECT Udf(DateTime::FromString, \"foo\" as RunConfig);";
  2796. auto res = SqlToYql(req);
  2797. TString a1 = Err2Str(res);
  2798. TString a2("<main>:1:8: Error: Abstract Udf Node can't be used as a part of expression.\n");
  2799. UNIT_ASSERT_NO_DIFF(a1, a2);
  2800. }
  2801. Y_UNIT_TEST(UdfSyntaxSugarIsNotCallable) {
  2802. auto req = "SELECT Udf(123, \"foo\" as RunConfig);";
  2803. auto res = SqlToYql(req);
  2804. TString a1 = Err2Str(res);
  2805. TString a2("<main>:1:8: Error: Udf: first argument must be a callable, like Foo::Bar\n");
  2806. UNIT_ASSERT_NO_DIFF(a1, a2);
  2807. }
  2808. Y_UNIT_TEST(UdfSyntaxSugarNoArgs) {
  2809. auto req = "SELECT Udf()();";
  2810. auto res = SqlToYql(req);
  2811. TString a1 = Err2Str(res);
  2812. TString a2("<main>:1:8: Error: Udf: expected at least one argument\n");
  2813. UNIT_ASSERT_NO_DIFF(a1, a2);
  2814. }
  2815. Y_UNIT_TEST(StrayUTF8) {
  2816. /// 'c' in plato is russian here
  2817. NYql::TAstParseResult res = SqlToYql("select * from сedar.Input");
  2818. UNIT_ASSERT(!res.Root);
  2819. TString a1 = Err2Str(res);
  2820. TString a2(R"foo(<main>:1:14: Error: Unexpected character 'с' (Unicode character <1089>) : cannot match to any predicted input...
  2821. <main>:1:15: Error: Unexpected character : cannot match to any predicted input...
  2822. )foo");
  2823. UNIT_ASSERT_NO_DIFF(a1, a2);
  2824. }
  2825. Y_UNIT_TEST(IvalidStringLiteralWithEscapedBackslash) {
  2826. NYql::TAstParseResult res1 = SqlToYql(R"foo($bar = 'a\\'b';)foo");
  2827. NYql::TAstParseResult res2 = SqlToYql(R"foo($bar = "a\\"b";)foo");
  2828. UNIT_ASSERT(!res1.Root);
  2829. UNIT_ASSERT(!res2.Root);
  2830. UNIT_ASSERT_NO_DIFF(Err2Str(res1), "<main>:1:15: Error: Unexpected character : syntax error...\n\n");
  2831. UNIT_ASSERT_NO_DIFF(Err2Str(res2), "<main>:1:15: Error: Unexpected character : syntax error...\n\n");
  2832. }
  2833. Y_UNIT_TEST(InvalidHexInStringLiteral) {
  2834. NYql::TAstParseResult res = SqlToYql("select \"foo\\x1\\xfe\"");
  2835. UNIT_ASSERT(!res.Root);
  2836. TString a1 = Err2Str(res);
  2837. TString a2 = "<main>:1:15: Error: Failed to parse string literal: Invalid hexadecimal value\n";
  2838. UNIT_ASSERT_NO_DIFF(a1, a2);
  2839. }
  2840. Y_UNIT_TEST(InvalidOctalInMultilineStringLiteral) {
  2841. NYql::TAstParseResult res = SqlToYql("select \"foo\n"
  2842. "bar\n"
  2843. "\\01\"");
  2844. UNIT_ASSERT(!res.Root);
  2845. TString a1 = Err2Str(res);
  2846. TString a2 = "<main>:3:4: Error: Failed to parse string literal: Invalid octal value\n";
  2847. UNIT_ASSERT_NO_DIFF(a1, a2);
  2848. }
  2849. Y_UNIT_TEST(InvalidDoubleAtString) {
  2850. NYql::TAstParseResult res = SqlToYql("select @@@@@@");
  2851. UNIT_ASSERT(!res.Root);
  2852. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:13: Error: Unexpected character : syntax error...\n\n");
  2853. }
  2854. Y_UNIT_TEST(InvalidDoubleAtStringWhichWasAcceptedEarlier) {
  2855. NYql::TAstParseResult res = SqlToYql("SELECT @@foo@@ @ @@bar@@");
  2856. UNIT_ASSERT(!res.Root);
  2857. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:7: Error: Unexpected token '@@foo@@' : cannot match to any predicted input...\n\n");
  2858. }
  2859. Y_UNIT_TEST(InvalidStringFromTable) {
  2860. NYql::TAstParseResult res = SqlToYql("select \"FOO\"\"BAR from plato.foo");
  2861. UNIT_ASSERT(!res.Root);
  2862. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:31: Error: Unexpected character : syntax error...\n\n");
  2863. }
  2864. Y_UNIT_TEST(InvalidDoubleAtStringFromTable) {
  2865. NYql::TAstParseResult res = SqlToYql("select @@@@@@ from plato.foo");
  2866. UNIT_ASSERT(!res.Root);
  2867. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:28: Error: Unexpected character : syntax error...\n\n");
  2868. }
  2869. Y_UNIT_TEST(SelectInvalidSyntax) {
  2870. NYql::TAstParseResult res = SqlToYql("select 1 form Wat");
  2871. UNIT_ASSERT(!res.Root);
  2872. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:14: Error: Unexpected token 'Wat' : cannot match to any predicted input...\n\n");
  2873. }
  2874. Y_UNIT_TEST(SelectNoCluster) {
  2875. NYql::TAstParseResult res = SqlToYql("select foo from bar");
  2876. UNIT_ASSERT(!res.Root);
  2877. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:1: Error: No cluster name given and no default cluster is selected\n");
  2878. }
  2879. Y_UNIT_TEST(SelectDuplicateColumns) {
  2880. NYql::TAstParseResult res = SqlToYql("select a, a from plato.Input");
  2881. UNIT_ASSERT(!res.Root);
  2882. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:11: Error: Unable to use duplicate column names. Collision in name: a\n");
  2883. }
  2884. Y_UNIT_TEST(SelectDuplicateLabels) {
  2885. NYql::TAstParseResult res = SqlToYql("select a as foo, b as foo from plato.Input");
  2886. UNIT_ASSERT(!res.Root);
  2887. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:18: Error: Unable to use duplicate column names. Collision in name: foo\n");
  2888. }
  2889. Y_UNIT_TEST(SelectCaseWithoutThen) {
  2890. NYql::TAstParseResult res = SqlToYql("select case when true 1;");
  2891. UNIT_ASSERT(!res.Root);
  2892. UNIT_ASSERT_NO_DIFF(Err2Str(res),
  2893. "<main>:1:22: Error: Unexpected token absence : Missing THEN \n\n"
  2894. "<main>:1:23: Error: Unexpected token absence : Missing END \n\n"
  2895. );
  2896. }
  2897. Y_UNIT_TEST(SelectComplexCaseWithoutThen) {
  2898. NYql::TAstParseResult res = SqlToYql(
  2899. "SELECT *\n"
  2900. "FROM plato.Input AS a\n"
  2901. "WHERE CASE WHEN a.key = \"foo\" a.subkey ELSE a.value END\n"
  2902. );
  2903. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:3:30: Error: Unexpected token absence : Missing THEN \n\n");
  2904. }
  2905. Y_UNIT_TEST(SelectCaseWithoutEnd) {
  2906. NYql::TAstParseResult res = SqlToYql("select case a when b then c end from plato.Input");
  2907. UNIT_ASSERT(!res.Root);
  2908. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:8: Error: ELSE is required\n");
  2909. }
  2910. Y_UNIT_TEST(SelectWithBadAggregationNoInput) {
  2911. NYql::TAstParseResult res = SqlToYql("select a, Min(b), c");
  2912. UNIT_ASSERT(!res.Root);
  2913. UNIT_ASSERT_NO_DIFF(Err2Str(res),
  2914. "<main>:1:1: Error: Column references are not allowed without FROM\n"
  2915. "<main>:1:8: Error: Column reference 'a'\n"
  2916. "<main>:1:1: Error: Column references are not allowed without FROM\n"
  2917. "<main>:1:15: Error: Column reference 'b'\n"
  2918. "<main>:1:1: Error: Column references are not allowed without FROM\n"
  2919. "<main>:1:19: Error: Column reference 'c'\n"
  2920. );
  2921. }
  2922. Y_UNIT_TEST(SelectWithBadAggregation) {
  2923. ExpectFailWithError("select count(*), 1 + key from plato.Input",
  2924. "<main>:1:22: Error: Column `key` must either be a key column in GROUP BY or it should be used in aggregation function\n");
  2925. }
  2926. Y_UNIT_TEST(SelectWithBadAggregatedTerms) {
  2927. ExpectFailWithError("select key, 2 * subkey from plato.Input group by key",
  2928. "<main>:1:17: Error: Column `subkey` must either be a key column in GROUP BY or it should be used in aggregation function\n");
  2929. }
  2930. Y_UNIT_TEST(SelectDistinctWithBadAggregation) {
  2931. ExpectFailWithError("select distinct count(*), 1 + key from plato.Input",
  2932. "<main>:1:31: Error: Column `key` must either be a key column in GROUP BY or it should be used in aggregation function\n");
  2933. ExpectFailWithError("select distinct key, 2 * subkey from plato.Input group by key",
  2934. "<main>:1:26: Error: Column `subkey` must either be a key column in GROUP BY or it should be used in aggregation function\n");
  2935. }
  2936. Y_UNIT_TEST(SelectWithBadAggregationInHaving) {
  2937. ExpectFailWithError("select key from plato.Input group by key\n"
  2938. "having \"f\" || value == \"foo\"",
  2939. "<main>:2:15: Error: Column `value` must either be a key column in GROUP BY or it should be used in aggregation function\n");
  2940. }
  2941. Y_UNIT_TEST(JoinWithNonAggregatedColumnInProjection) {
  2942. ExpectFailWithError("select a.key, 1 + b.subkey\n"
  2943. "from plato.Input1 as a join plato.Input2 as b using(key)\n"
  2944. "group by a.key;",
  2945. "<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");
  2946. ExpectFailWithError("select a.key, 1 + b.subkey.x\n"
  2947. "from plato.Input1 as a join plato.Input2 as b using(key)\n"
  2948. "group by a.key;",
  2949. "<main>:1:19: Error: Column must either be a key column in GROUP BY or it should be used in aggregation function\n");
  2950. }
  2951. Y_UNIT_TEST(SelectWithBadAggregatedTermsWithSources) {
  2952. ExpectFailWithError("select key, 1 + a.subkey\n"
  2953. "from plato.Input1 as a\n"
  2954. "group by a.key;",
  2955. "<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");
  2956. ExpectFailWithError("select key, 1 + a.subkey.x\n"
  2957. "from plato.Input1 as a\n"
  2958. "group by a.key;",
  2959. "<main>:1:17: Error: Column must either be a key column in GROUP BY or it should be used in aggregation function\n");
  2960. }
  2961. Y_UNIT_TEST(WarnForAggregationBySelectAlias) {
  2962. NYql::TAstParseResult res = SqlToYql("select c + 1 as c from plato.Input\n"
  2963. "group by c");
  2964. UNIT_ASSERT(res.Root);
  2965. UNIT_ASSERT_NO_DIFF(Err2Str(res),
  2966. "<main>:2:11: Warning: GROUP BY will aggregate by column `c` instead of aggregating by SELECT expression with same alias, code: 4532\n"
  2967. "<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");
  2968. res = SqlToYql("select c + 1 as c from plato.Input\n"
  2969. "group by Math::Floor(c + 2) as c;");
  2970. UNIT_ASSERT(res.Root);
  2971. UNIT_ASSERT_NO_DIFF(Err2Str(res),
  2972. "<main>:2:22: Warning: GROUP BY will aggregate by column `c` instead of aggregating by SELECT expression with same alias, code: 4532\n"
  2973. "<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");
  2974. }
  2975. Y_UNIT_TEST(NoWarnForAggregationBySelectAliasWhenAggrFunctionsAreUsedInAlias) {
  2976. NYql::TAstParseResult res = SqlToYql("select\n"
  2977. " cast(avg(val) as int) as value,\n"
  2978. " value as key\n"
  2979. "from\n"
  2980. " plato.Input\n"
  2981. "group by value");
  2982. UNIT_ASSERT(res.Root);
  2983. UNIT_ASSERT(res.Issues.Size() == 0);
  2984. res = SqlToYql("select\n"
  2985. " cast(avg(val) over w as int) as value,\n"
  2986. " value as key\n"
  2987. "from\n"
  2988. " plato.Input\n"
  2989. "group by value\n"
  2990. "window w as ()");
  2991. UNIT_ASSERT(res.Root);
  2992. UNIT_ASSERT(res.Issues.Size() == 0);
  2993. }
  2994. Y_UNIT_TEST(NoWarnForAggregationBySelectAliasWhenQualifiedNameIsUsed) {
  2995. NYql::TAstParseResult res = SqlToYql("select\n"
  2996. " Unwrap(a.key) as key\n"
  2997. "from plato.Input as a\n"
  2998. "join plato.Input2 as b using(k)\n"
  2999. "group by a.key;");
  3000. UNIT_ASSERT(res.Root);
  3001. UNIT_ASSERT(res.Issues.Size() == 0);
  3002. res = SqlToYql("select Unwrap(a.key) as key\n"
  3003. "from plato.Input as a\n"
  3004. "group by a.key;");
  3005. UNIT_ASSERT(res.Root);
  3006. UNIT_ASSERT(res.Issues.Size() == 0);
  3007. }
  3008. Y_UNIT_TEST(NoWarnForAggregationBySelectAliasWhenTrivialRenamingIsUsed) {
  3009. NYql::TAstParseResult res = SqlToYql("select a.key as key\n"
  3010. "from plato.Input as a\n"
  3011. "group by key;");
  3012. UNIT_ASSERT(res.Root);
  3013. UNIT_ASSERT(res.Issues.Size() == 0);
  3014. res = SqlToYql("select key as key\n"
  3015. "from plato.Input\n"
  3016. "group by key;");
  3017. UNIT_ASSERT(res.Root);
  3018. UNIT_ASSERT(res.Issues.Size() == 0);
  3019. }
  3020. Y_UNIT_TEST(ErrorByAggregatingByExpressionWithSameExpressionInSelect) {
  3021. ExpectFailWithError("select k * 2 from plato.Input group by k * 2",
  3022. "<main>:1:8: Error: Column `k` must either be a key column in GROUP BY or it should be used in aggregation function\n");
  3023. }
  3024. Y_UNIT_TEST(ErrorForAggregationBySelectAlias) {
  3025. ExpectFailWithError("select key, Math::Floor(1.1 + a.subkey) as foo\n"
  3026. "from plato.Input as a\n"
  3027. "group by a.key, foo;",
  3028. "<main>:3:17: Warning: GROUP BY will aggregate by column `foo` instead of aggregating by SELECT expression with same alias, code: 4532\n"
  3029. "<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"
  3030. "<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");
  3031. ExpectFailWithError("select c + 1 as c from plato.Input\n"
  3032. "group by Math::Floor(c + 2);",
  3033. "<main>:2:22: Warning: GROUP BY will aggregate by column `c` instead of aggregating by SELECT expression with same alias, code: 4532\n"
  3034. "<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"
  3035. "<main>:1:8: Error: Column `c` must either be a key column in GROUP BY or it should be used in aggregation function\n");
  3036. }
  3037. Y_UNIT_TEST(ExplainQueryPlan) {
  3038. NYql::TAstParseResult res = SqlToYql("EXPLAIN Q U E R Y PLAN SELECT 1;");
  3039. UNIT_ASSERT(!res.Root);
  3040. UNIT_ASSERT_STRING_CONTAINS(Err2Str(res), "<main>:1:8: Error: Unexpected token 'Q' : cannot match to any predicted input");
  3041. }
  3042. Y_UNIT_TEST(SelectWithDuplicateGroupingColumns) {
  3043. NYql::TAstParseResult res = SqlToYql("select c from plato.Input group by c, c");
  3044. UNIT_ASSERT(!res.Root);
  3045. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:8: Error: Duplicate grouping column: c\n");
  3046. }
  3047. Y_UNIT_TEST(SelectWithBadAggregationInGrouping) {
  3048. NYql::TAstParseResult res = SqlToYql("select a, Min(b), c group by c");
  3049. UNIT_ASSERT(!res.Root);
  3050. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:1: Error: Column references are not allowed without FROM\n"
  3051. "<main>:1:30: Error: Column reference 'c'\n");
  3052. }
  3053. Y_UNIT_TEST(SelectWithOpOnBadAggregation) {
  3054. ExpectFailWithError("select 1 + a + Min(b) from plato.Input",
  3055. "<main>:1:12: Error: Column `a` must either be a key column in GROUP BY or it should be used in aggregation function\n");
  3056. }
  3057. Y_UNIT_TEST(SelectOrderByConstantNum) {
  3058. NYql::TAstParseResult res = SqlToYql("select a from plato.Input order by 1");
  3059. UNIT_ASSERT(!res.Root);
  3060. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:36: Error: Unable to ORDER BY constant expression\n");
  3061. }
  3062. Y_UNIT_TEST(SelectOrderByConstantExpr) {
  3063. NYql::TAstParseResult res = SqlToYql("select a from plato.Input order by 1 * 42");
  3064. UNIT_ASSERT(!res.Root);
  3065. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:38: Error: Unable to ORDER BY constant expression\n");
  3066. }
  3067. Y_UNIT_TEST(SelectOrderByConstantString) {
  3068. NYql::TAstParseResult res = SqlToYql("select a from plato.Input order by \"nest\"");
  3069. UNIT_ASSERT(!res.Root);
  3070. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:36: Error: Unable to ORDER BY constant expression\n");
  3071. }
  3072. Y_UNIT_TEST(SelectOrderByAggregated) {
  3073. NYql::TAstParseResult res = SqlToYql("select a from plato.Input order by min(a)");
  3074. UNIT_ASSERT(!res.Root);
  3075. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:36: Error: Unable to ORDER BY aggregated values\n");
  3076. }
  3077. Y_UNIT_TEST(ErrorInOrderByExpresison) {
  3078. NYql::TAstParseResult res = SqlToYql("select key, value from plato.Input order by (key as zey)");
  3079. UNIT_ASSERT(!res.Root);
  3080. 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");
  3081. }
  3082. Y_UNIT_TEST(ErrorsInOrderByWhenColumnIsMissingInProjection) {
  3083. ExpectFailWithError("select subkey from (select 1 as subkey) order by key", "<main>:1:50: Error: Column key is not in source column set\n");
  3084. ExpectFailWithError("select subkey from plato.Input as a order by x.key", "<main>:1:46: Error: Unknown correlation name: x\n");
  3085. 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");
  3086. 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");
  3087. 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");
  3088. UNIT_ASSERT(SqlToYql("select a, b from plato.Input order by c").IsOk());
  3089. }
  3090. Y_UNIT_TEST(SelectAggregatedWhere) {
  3091. NYql::TAstParseResult res = SqlToYql("select * from plato.Input where count(key)");
  3092. UNIT_ASSERT(!res.Root);
  3093. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:33: Error: Can not use aggregated values in filtering\n");
  3094. }
  3095. Y_UNIT_TEST(DoubleFrom) {
  3096. NYql::TAstParseResult res = SqlToYql("from plato.Input select * from plato.Input");
  3097. UNIT_ASSERT(!res.Root);
  3098. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:27: Error: Only one FROM clause is allowed\n");
  3099. }
  3100. Y_UNIT_TEST(SelectJoinMissingCorrName) {
  3101. NYql::TAstParseResult res = SqlToYql("select * from plato.Input1 as a join plato.Input2 as b on a.key == key");
  3102. UNIT_ASSERT(!res.Root);
  3103. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:65: Error: JOIN: column requires correlation name\n");
  3104. }
  3105. Y_UNIT_TEST(SelectJoinMissingCorrName1) {
  3106. NYql::TAstParseResult res = SqlToYql(
  3107. "use plato;\n"
  3108. "$foo = select * from Input1;\n"
  3109. "select * from Input2 join $foo USING(key);\n"
  3110. );
  3111. UNIT_ASSERT(!res.Root);
  3112. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:3:27: Error: JOIN: missing correlation name for source\n");
  3113. }
  3114. Y_UNIT_TEST(SelectJoinMissingCorrName2) {
  3115. NYql::TAstParseResult res = SqlToYql(
  3116. "use plato;\n"
  3117. "$foo = select * from Input1;\n"
  3118. "select * from Input2 cross join $foo;\n"
  3119. );
  3120. UNIT_ASSERT(!res.Root);
  3121. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:3:33: Error: JOIN: missing correlation name for source\n");
  3122. }
  3123. Y_UNIT_TEST(SelectJoinEmptyCorrNames) {
  3124. NYql::TAstParseResult res = SqlToYql(
  3125. "$left = (SELECT * FROM plato.Input1 LIMIT 2);\n"
  3126. "$right = (SELECT * FROM plato.Input2 LIMIT 2);\n"
  3127. "SELECT * FROM $left FULL JOIN $right USING (key);\n"
  3128. );
  3129. UNIT_ASSERT(!res.Root);
  3130. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:3:45: Error: At least one correlation name is required in join\n");
  3131. }
  3132. Y_UNIT_TEST(SelectJoinSameCorrNames) {
  3133. NYql::TAstParseResult res = SqlToYql("SELECT Input.key FROM plato.Input JOIN plato.Input1 ON Input.key == Input.subkey\n");
  3134. UNIT_ASSERT(!res.Root);
  3135. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:66: Error: JOIN: different correlation names are required for joined tables\n");
  3136. }
  3137. Y_UNIT_TEST(SelectJoinConstPredicateArg) {
  3138. 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");
  3139. UNIT_ASSERT(!res.Root);
  3140. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:87: Error: JOIN: each equality predicate argument must depend on exactly one JOIN input\n");
  3141. }
  3142. Y_UNIT_TEST(SelectJoinNonEqualityPredicate) {
  3143. 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");
  3144. UNIT_ASSERT(!res.Root);
  3145. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:87: Error: JOIN ON expression must be a conjunction of equality predicates\n");
  3146. }
  3147. Y_UNIT_TEST(SelectEquiJoinCorrNameOutOfScope) {
  3148. NYql::TAstParseResult res = SqlToYql(
  3149. "PRAGMA equijoin;\n"
  3150. "SELECT * FROM plato.A JOIN plato.B ON A.key == C.key JOIN plato.C ON A.subkey == C.subkey;\n"
  3151. );
  3152. UNIT_ASSERT(!res.Root);
  3153. 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");
  3154. }
  3155. Y_UNIT_TEST(SelectEquiJoinNoRightSource) {
  3156. NYql::TAstParseResult res = SqlToYql(
  3157. "PRAGMA equijoin;\n"
  3158. "SELECT * FROM plato.A JOIN plato.B ON A.key == B.key JOIN plato.C ON A.subkey == B.subkey;\n"
  3159. );
  3160. UNIT_ASSERT(!res.Root);
  3161. 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");
  3162. }
  3163. Y_UNIT_TEST(SelectEquiJoinOuterWithoutType) {
  3164. NYql::TAstParseResult res = SqlToYql(
  3165. "SELECT * FROM plato.A Outer JOIN plato.B ON A.key == B.key;\n"
  3166. );
  3167. UNIT_ASSERT(!res.Root);
  3168. 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");
  3169. }
  3170. Y_UNIT_TEST(SelectEquiJoinOuterWithWrongType) {
  3171. NYql::TAstParseResult res = SqlToYql(
  3172. "SELECT * FROM plato.A LEFT semi OUTER JOIN plato.B ON A.key == B.key;\n"
  3173. );
  3174. UNIT_ASSERT(!res.Root);
  3175. 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");
  3176. }
  3177. Y_UNIT_TEST(InsertNoCluster) {
  3178. NYql::TAstParseResult res = SqlToYql("insert into Output (foo) values (1)");
  3179. UNIT_ASSERT(!res.Root);
  3180. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:1: Error: No cluster name given and no default cluster is selected\n");
  3181. }
  3182. Y_UNIT_TEST(InsertValuesNoLabels) {
  3183. NYql::TAstParseResult res = SqlToYql("insert into plato.Output values (1)");
  3184. UNIT_ASSERT(!res.Root);
  3185. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:19: Error: INSERT INTO ... VALUES requires specification of table columns\n");
  3186. }
  3187. Y_UNIT_TEST(UpsertValuesNoLabelsKikimr) {
  3188. NYql::TAstParseResult res = SqlToYql("upsert into plato.Output values (1)", 10, TString(NYql::KikimrProviderName));
  3189. UNIT_ASSERT(!res.Root);
  3190. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:19: Error: UPSERT INTO ... VALUES requires specification of table columns\n");
  3191. }
  3192. Y_UNIT_TEST(ReplaceValuesNoLabelsKikimr) {
  3193. NYql::TAstParseResult res = SqlToYql("replace into plato.Output values (1)", 10, TString(NYql::KikimrProviderName));
  3194. UNIT_ASSERT(!res.Root);
  3195. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:20: Error: REPLACE INTO ... VALUES requires specification of table columns\n");
  3196. }
  3197. Y_UNIT_TEST(InsertValuesInvalidLabels) {
  3198. NYql::TAstParseResult res = SqlToYql("insert into plato.Output (foo) values (1, 2)");
  3199. UNIT_ASSERT(!res.Root);
  3200. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:27: Error: VALUES have 2 columns, INSERT INTO expects: 1\n");
  3201. }
  3202. Y_UNIT_TEST(BuiltinFileOpNoArgs) {
  3203. NYql::TAstParseResult res = SqlToYql("select FilePath()");
  3204. UNIT_ASSERT(!res.Root);
  3205. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:8: Error: FilePath() requires exactly 1 arguments, given: 0\n");
  3206. }
  3207. Y_UNIT_TEST(ProcessWithHaving) {
  3208. NYql::TAstParseResult res = SqlToYql("process plato.Input using some::udf(value) having value == 1");
  3209. UNIT_ASSERT(!res.Root);
  3210. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:15: Error: PROCESS does not allow HAVING yet! You may request it on yql@ maillist.\n");
  3211. }
  3212. Y_UNIT_TEST(ReduceNoBy) {
  3213. NYql::TAstParseResult res = SqlToYql("reduce plato.Input using some::udf(value)");
  3214. UNIT_ASSERT(!res.Root);
  3215. 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");
  3216. }
  3217. Y_UNIT_TEST(ReduceDistinct) {
  3218. NYql::TAstParseResult res = SqlToYql("reduce plato.Input on key using some::udf(distinct value)");
  3219. UNIT_ASSERT(!res.Root);
  3220. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:43: Error: DISTINCT can not be used in PROCESS/REDUCE\n");
  3221. }
  3222. Y_UNIT_TEST(CreateTableWithView) {
  3223. NYql::TAstParseResult res = SqlToYql("CREATE TABLE plato.foo:bar (key INT);");
  3224. UNIT_ASSERT(!res.Root);
  3225. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:22: Error: Unexpected token ':' : syntax error...\n\n");
  3226. }
  3227. Y_UNIT_TEST(AsteriskWithSomethingAfter) {
  3228. NYql::TAstParseResult res = SqlToYql("select *, LENGTH(value) from plato.Input;");
  3229. UNIT_ASSERT(!res.Root);
  3230. 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");
  3231. }
  3232. Y_UNIT_TEST(AsteriskWithSomethingBefore) {
  3233. NYql::TAstParseResult res = SqlToYql("select LENGTH(value), * from plato.Input;");
  3234. UNIT_ASSERT(!res.Root);
  3235. 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");
  3236. }
  3237. Y_UNIT_TEST(DuplicatedQualifiedAsterisk) {
  3238. NYql::TAstParseResult res = SqlToYql("select in.*, key, in.* from plato.Input as in;");
  3239. UNIT_ASSERT(!res.Root);
  3240. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:19: Error: Unable to use twice same quialified asterisk. Invalid source: in\n");
  3241. }
  3242. Y_UNIT_TEST(BrokenLabel) {
  3243. NYql::TAstParseResult res = SqlToYql("select in.*, key as `funny.label` from plato.Input as in;");
  3244. UNIT_ASSERT(!res.Root);
  3245. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:14: Error: Unable to use '.' in column name. Invalid column name: funny.label\n");
  3246. }
  3247. Y_UNIT_TEST(KeyConflictDetect0) {
  3248. NYql::TAstParseResult res = SqlToYql("select key, in.key as key from plato.Input as in;");
  3249. UNIT_ASSERT(!res.Root);
  3250. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:13: Error: Unable to use duplicate column names. Collision in name: key\n");
  3251. }
  3252. Y_UNIT_TEST(KeyConflictDetect1) {
  3253. NYql::TAstParseResult res = SqlToYql("select length(key) as key, key from plato.Input;");
  3254. UNIT_ASSERT(!res.Root);
  3255. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:28: Error: Unable to use duplicate column names. Collision in name: key\n");
  3256. }
  3257. Y_UNIT_TEST(KeyConflictDetect2) {
  3258. NYql::TAstParseResult res = SqlToYql("select key, in.key from plato.Input as in;");
  3259. UNIT_ASSERT(!res.Root);
  3260. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:1: Error: Duplicate column: key\n");
  3261. }
  3262. Y_UNIT_TEST(AutogenerationAliasWithCollisionConflict1) {
  3263. UNIT_ASSERT(SqlToYql("select LENGTH(Value), key as column0 from plato.Input;").IsOk());
  3264. }
  3265. Y_UNIT_TEST(AutogenerationAliasWithCollisionConflict2) {
  3266. UNIT_ASSERT(SqlToYql("select key as column1, LENGTH(Value) from plato.Input;").IsOk());
  3267. }
  3268. Y_UNIT_TEST(MissedSourceTableForQualifiedAsteriskOnSimpleSelect) {
  3269. NYql::TAstParseResult res = SqlToYql("use plato; select Intop.*, Input.key from plato.Input;");
  3270. UNIT_ASSERT(!res.Root);
  3271. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:19: Error: Unknown correlation name: Intop\n");
  3272. }
  3273. Y_UNIT_TEST(MissedSourceTableForQualifiedAsteriskOnJoin) {
  3274. 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;");
  3275. UNIT_ASSERT(!res.Root);
  3276. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:19: Error: Unknown correlation name for asterisk: tmissed\n");
  3277. }
  3278. Y_UNIT_TEST(UnableToReferenceOnNotExistSubcolumn) {
  3279. NYql::TAstParseResult res = SqlToYql("select b.subkey from (select key from plato.Input as a) as b;");
  3280. UNIT_ASSERT(!res.Root);
  3281. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:8: Error: Column subkey is not in source column set\n");
  3282. }
  3283. Y_UNIT_TEST(ConflictOnSameNameWithQualify0) {
  3284. NYql::TAstParseResult res = SqlToYql("select in.key, in.key as key from plato.Input as in;");
  3285. UNIT_ASSERT(!res.Root);
  3286. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:1: Error: Duplicate column: key\n");
  3287. }
  3288. Y_UNIT_TEST(ConflictOnSameNameWithQualify1) {
  3289. NYql::TAstParseResult res = SqlToYql("select in.key, length(key) as key from plato.Input as in;");
  3290. UNIT_ASSERT(!res.Root);
  3291. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:1: Error: Duplicate column: key\n");
  3292. }
  3293. Y_UNIT_TEST(ConflictOnSameNameWithQualify2) {
  3294. NYql::TAstParseResult res = SqlToYql("select key, in.key from plato.Input as in;");
  3295. UNIT_ASSERT(!res.Root);
  3296. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:1: Error: Duplicate column: key\n");
  3297. }
  3298. Y_UNIT_TEST(ConflictOnSameNameWithQualify3) {
  3299. NYql::TAstParseResult res = SqlToYql("select in.key, subkey as key from plato.Input as in;");
  3300. UNIT_ASSERT(!res.Root);
  3301. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:1: Error: Duplicate column: key\n");
  3302. }
  3303. Y_UNIT_TEST(SelectFlattenBySameColumns) {
  3304. NYql::TAstParseResult res = SqlToYql("select key from plato.Input flatten by (key, key as kk)");
  3305. UNIT_ASSERT(!res.Root);
  3306. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:46: Error: Duplicate column name found: key in FlattenBy section\n");
  3307. }
  3308. Y_UNIT_TEST(SelectFlattenBySameAliases) {
  3309. NYql::TAstParseResult res = SqlToYql("select key from plato.Input flatten by (key as kk, subkey as kk);");
  3310. UNIT_ASSERT(!res.Root);
  3311. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:52: Error: Duplicate alias found: kk in FlattenBy section\n");
  3312. }
  3313. Y_UNIT_TEST(SelectFlattenByExprSameAliases) {
  3314. NYql::TAstParseResult res = SqlToYql("select key from plato.Input flatten by (key as kk, ListSkip(subkey,1) as kk);");
  3315. UNIT_ASSERT(!res.Root);
  3316. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:52: Error: Collision between alias and column name: kk in FlattenBy section\n");
  3317. }
  3318. Y_UNIT_TEST(SelectFlattenByConflictNameAndAlias0) {
  3319. NYql::TAstParseResult res = SqlToYql("select key from plato.Input flatten by (key, subkey as key);");
  3320. UNIT_ASSERT(!res.Root);
  3321. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:46: Error: Collision between alias and column name: key in FlattenBy section\n");
  3322. }
  3323. Y_UNIT_TEST(SelectFlattenByConflictNameAndAlias1) {
  3324. NYql::TAstParseResult res = SqlToYql("select key from plato.Input flatten by (key as kk, subkey as key);");
  3325. UNIT_ASSERT(!res.Root);
  3326. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:52: Error: Collision between alias and column name: key in FlattenBy section\n");
  3327. }
  3328. Y_UNIT_TEST(SelectFlattenByExprConflictNameAndAlias1) {
  3329. NYql::TAstParseResult res = SqlToYql("select key from plato.Input flatten by (key as kk, ListSkip(subkey,1) as key);");
  3330. UNIT_ASSERT(!res.Root);
  3331. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:52: Error: Duplicate column name found: key in FlattenBy section\n");
  3332. }
  3333. Y_UNIT_TEST(SelectFlattenByUnnamedExpr) {
  3334. NYql::TAstParseResult res = SqlToYql("select key from plato.Input flatten by (key, ListSkip(key, 1))");
  3335. UNIT_ASSERT(!res.Root);
  3336. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:46: Error: Unnamed expression after FLATTEN BY is not allowed\n");
  3337. }
  3338. Y_UNIT_TEST(UseInOnStrings) {
  3339. NYql::TAstParseResult res = SqlToYql("select * from plato.Input where \"foo\" in \"foovalue\";");
  3340. UNIT_ASSERT(!res.Root);
  3341. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:42: Error: Unable to use IN predicate with string argument, it won't search substring - "
  3342. "expecting tuple, list, dict or single column table source\n");
  3343. }
  3344. Y_UNIT_TEST(UseSubqueryInScalarContextInsideIn) {
  3345. NYql::TAstParseResult res = SqlToYql("$q = (select key from plato.Input); select * from plato.Input where subkey in ($q);");
  3346. UNIT_ASSERT(res.Root);
  3347. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:79: Warning: Using subrequest in scalar context after IN, "
  3348. "perhaps you should remove parenthesis here, code: 4501\n");
  3349. }
  3350. Y_UNIT_TEST(InHintsWithKeywordClash) {
  3351. NYql::TAstParseResult res = SqlToYql("SELECT COMPACT FROM plato.Input WHERE COMPACT IN COMPACT `COMPACT`(1,2,3)");
  3352. UNIT_ASSERT(!res.Root);
  3353. // should try to parse last compact as call expression
  3354. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:58: Error: Unknown builtin: COMPACT\n");
  3355. }
  3356. Y_UNIT_TEST(ErrorColumnPosition) {
  3357. NYql::TAstParseResult res = SqlToYql(
  3358. "USE plato;\n"
  3359. "SELECT \n"
  3360. "value FROM (\n"
  3361. "select key from Input\n"
  3362. ");\n"
  3363. );
  3364. UNIT_ASSERT(!res.Root);
  3365. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:3:1: Error: Column value is not in source column set\n");
  3366. }
  3367. Y_UNIT_TEST(PrimaryViewAbortMapReduce) {
  3368. NYql::TAstParseResult res = SqlToYql("SELECT key FROM plato.Input VIEW PRIMARY KEY");
  3369. UNIT_ASSERT(!res.Root);
  3370. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:17: Error: primary view is not supported for yt tables\n");
  3371. }
  3372. Y_UNIT_TEST(InsertAbortMapReduce) {
  3373. NYql::TAstParseResult res = SqlToYql("INSERT OR ABORT INTO plato.Output SELECT key FROM plato.Input");
  3374. UNIT_ASSERT(!res.Root);
  3375. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:0: Error: INSERT OR ABORT INTO is not supported for yt tables\n");
  3376. }
  3377. Y_UNIT_TEST(ReplaceIntoMapReduce) {
  3378. NYql::TAstParseResult res = SqlToYql("REPLACE INTO plato.Output SELECT key FROM plato.Input");
  3379. UNIT_ASSERT(!res.Root);
  3380. 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");
  3381. }
  3382. Y_UNIT_TEST(UpsertIntoMapReduce) {
  3383. NYql::TAstParseResult res = SqlToYql("UPSERT INTO plato.Output SELECT key FROM plato.Input");
  3384. UNIT_ASSERT(!res.Root);
  3385. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:0: Error: UPSERT INTO is not supported for yt tables\n");
  3386. }
  3387. Y_UNIT_TEST(UpdateMapReduce) {
  3388. NYql::TAstParseResult res = SqlToYql("UPDATE plato.Output SET value = value + 1 WHERE key < 1");
  3389. UNIT_ASSERT(!res.Root);
  3390. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:0: Error: UPDATE is unsupported for yt\n");
  3391. }
  3392. Y_UNIT_TEST(DeleteMapReduce) {
  3393. NYql::TAstParseResult res = SqlToYql("DELETE FROM plato.Output WHERE key < 1");
  3394. UNIT_ASSERT(!res.Root);
  3395. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:0: Error: DELETE is unsupported for yt\n");
  3396. }
  3397. Y_UNIT_TEST(ReplaceIntoWithTruncate) {
  3398. NYql::TAstParseResult res = SqlToYql("REPLACE INTO plato.Output WITH TRUNCATE SELECT key FROM plato.Input");
  3399. UNIT_ASSERT(!res.Root);
  3400. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:32: Error: Unable REPLACE INTO with truncate mode\n");
  3401. }
  3402. Y_UNIT_TEST(UpsertIntoWithTruncate) {
  3403. NYql::TAstParseResult res = SqlToYql("UPSERT INTO plato.Output WITH TRUNCATE SELECT key FROM plato.Input");
  3404. UNIT_ASSERT(!res.Root);
  3405. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:31: Error: Unable UPSERT INTO with truncate mode\n");
  3406. }
  3407. Y_UNIT_TEST(InsertIntoWithTruncateKikimr) {
  3408. NYql::TAstParseResult res = SqlToYql("INSERT INTO plato.Output WITH TRUNCATE SELECT key FROM plato.Input", 10, TString(NYql::KikimrProviderName));
  3409. UNIT_ASSERT(!res.Root);
  3410. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:0: Error: INSERT INTO WITH TRUNCATE is not supported for kikimr tables\n");
  3411. }
  3412. Y_UNIT_TEST(InsertIntoWithWrongArgumentCount) {
  3413. NYql::TAstParseResult res = SqlToYql("insert into plato.Output with truncate (key, value, subkey) values (5, '1', '2', '3');");
  3414. UNIT_ASSERT(!res.Root);
  3415. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:53: Error: VALUES have 4 columns, INSERT INTO ... WITH TRUNCATE expects: 3\n");
  3416. }
  3417. Y_UNIT_TEST(UpsertWithWrongArgumentCount) {
  3418. NYql::TAstParseResult res = SqlToYql("upsert into plato.Output (key, value, subkey) values (2, '3');", 10, TString(NYql::KikimrProviderName));
  3419. UNIT_ASSERT(!res.Root);
  3420. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:39: Error: VALUES have 2 columns, UPSERT INTO expects: 3\n");
  3421. }
  3422. Y_UNIT_TEST(GroupingSetByExprWithoutAlias) {
  3423. NYql::TAstParseResult res = SqlToYql("SELECT key FROM plato.Input GROUP BY GROUPING SETS (cast(key as uint32), subkey);");
  3424. UNIT_ASSERT(!res.Root);
  3425. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:53: Error: Unnamed expressions are not supported in GROUPING SETS. Please use '<expr> AS <name>'.\n");
  3426. }
  3427. Y_UNIT_TEST(GroupingSetByExprWithoutAlias2) {
  3428. NYql::TAstParseResult res = SqlToYql("SELECT key FROM plato.Input GROUP BY subkey || subkey, GROUPING SETS (\n"
  3429. "cast(key as uint32), subkey);");
  3430. UNIT_ASSERT(!res.Root);
  3431. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:2:1: Error: Unnamed expressions are not supported in GROUPING SETS. Please use '<expr> AS <name>'.\n");
  3432. }
  3433. Y_UNIT_TEST(CubeByExprWithoutAlias) {
  3434. NYql::TAstParseResult res = SqlToYql("SELECT key FROM plato.Input GROUP BY CUBE (key, subkey / key);");
  3435. UNIT_ASSERT(!res.Root);
  3436. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:56: Error: Unnamed expressions are not supported in CUBE. Please use '<expr> AS <name>'.\n");
  3437. }
  3438. Y_UNIT_TEST(RollupByExprWithoutAlias) {
  3439. NYql::TAstParseResult res = SqlToYql("SELECT key FROM plato.Input GROUP BY ROLLUP (subkey / key);");
  3440. UNIT_ASSERT(!res.Root);
  3441. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:53: Error: Unnamed expressions are not supported in ROLLUP. Please use '<expr> AS <name>'.\n");
  3442. }
  3443. Y_UNIT_TEST(GroupByHugeCubeDeniedNoPragma) {
  3444. 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);");
  3445. UNIT_ASSERT(!res.Root);
  3446. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:119: Error: GROUP BY CUBE is allowed only for 5 columns, but you use 6\n");
  3447. }
  3448. Y_UNIT_TEST(GroupByInvalidPragma) {
  3449. NYql::TAstParseResult res = SqlToYql("PRAGMA GroupByCubeLimit = '-4';");
  3450. UNIT_ASSERT(!res.Root);
  3451. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:27: Error: Expected unsigned integer literal as a single argument for: GroupByCubeLimit\n");
  3452. }
  3453. Y_UNIT_TEST(GroupByHugeCubeDeniedPragme) {
  3454. 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);");
  3455. UNIT_ASSERT(!res.Root);
  3456. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:132: Error: GROUP BY CUBE is allowed only for 4 columns, but you use 5\n");
  3457. }
  3458. Y_UNIT_TEST(GroupByFewBigCubes) {
  3459. 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);");
  3460. UNIT_ASSERT(!res.Root);
  3461. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:1: Error: Unable to GROUP BY more than 64 groups, you try use 80 groups\n");
  3462. }
  3463. Y_UNIT_TEST(GroupByFewBigCubesWithPragmaLimit) {
  3464. 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);");
  3465. UNIT_ASSERT(!res.Root);
  3466. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:29: Error: Unable to GROUP BY more than 16 groups, you try use 18 groups\n");
  3467. }
  3468. Y_UNIT_TEST(NoGroupingColumn0) {
  3469. NYql::TAstParseResult res = SqlToYql(
  3470. "select count(1), key_first, val_first, grouping(key_first, val_first, nomind) as group\n"
  3471. "from plato.Input group by grouping sets (cast(key as uint32) /100 as key_first, Substring(value, 1, 1) as val_first);");
  3472. UNIT_ASSERT(!res.Root);
  3473. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:71: Error: Column 'nomind' is not a grouping column\n");
  3474. }
  3475. Y_UNIT_TEST(NoGroupingColumn1) {
  3476. NYql::TAstParseResult res = SqlToYql("select count(1), grouping(key, value) as group_duo from plato.Input group by cube (key, subkey);");
  3477. UNIT_ASSERT(!res.Root);
  3478. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:32: Error: Column 'value' is not a grouping column\n");
  3479. }
  3480. Y_UNIT_TEST(EmptyAccess0) {
  3481. NYql::TAstParseResult res = SqlToYql("insert into plato.Output (list0, list1) values (AsList(0, 1, 2), AsList(``));");
  3482. UNIT_ASSERT(!res.Root);
  3483. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:73: Error: Column reference \"\" is not allowed in current scope\n");
  3484. }
  3485. Y_UNIT_TEST(EmptyAccess1) {
  3486. NYql::TAstParseResult res = SqlToYql("insert into plato.Output (list0, list1) values (AsList(0, 1, 2), ``);");
  3487. UNIT_ASSERT(!res.Root);
  3488. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:66: Error: Column reference \"\" is not allowed in current scope\n");
  3489. }
  3490. Y_UNIT_TEST(UseUnknownColumnInInsert) {
  3491. NYql::TAstParseResult res = SqlToYql("insert into plato.Output (list0, list1) values (AsList(0, 1, 2), AsList(`test`));");
  3492. UNIT_ASSERT(!res.Root);
  3493. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:73: Error: Column reference \"test\" is not allowed in current scope\n");
  3494. }
  3495. Y_UNIT_TEST(GroupByEmptyColumn) {
  3496. NYql::TAstParseResult res = SqlToYql("select count(1) from plato.Input group by ``;");
  3497. UNIT_ASSERT(!res.Root);
  3498. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:43: Error: Column name can not be empty\n");
  3499. }
  3500. Y_UNIT_TEST(ConvertNumberOutOfBase) {
  3501. NYql::TAstParseResult res = SqlToYql("select 0o80l;");
  3502. UNIT_ASSERT(!res.Root);
  3503. 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");
  3504. }
  3505. Y_UNIT_TEST(ConvertNumberOutOfRangeForInt64ButFitsInUint64) {
  3506. NYql::TAstParseResult res = SqlToYql("select 0xc000000000000000l;");
  3507. UNIT_ASSERT(!res.Root);
  3508. 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");
  3509. }
  3510. Y_UNIT_TEST(ConvertNumberOutOfRangeUint64) {
  3511. NYql::TAstParseResult res = SqlToYql("select 0xc0000000000000000l;");
  3512. UNIT_ASSERT(!res.Root);
  3513. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:8: Error: Failed to parse number from string: 0xc0000000000000000l, number limit overflow\n");
  3514. res = SqlToYql("select 1234234543563435151456;\n");
  3515. UNIT_ASSERT(!res.Root);
  3516. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:8: Error: Failed to parse number from string: 1234234543563435151456, number limit overflow\n");
  3517. }
  3518. Y_UNIT_TEST(ConvertNumberNegativeOutOfRange) {
  3519. NYql::TAstParseResult res = SqlToYql("select -9223372036854775808;\n"
  3520. "select -9223372036854775809;");
  3521. UNIT_ASSERT(!res.Root);
  3522. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:2:8: Error: Failed to parse negative integer: -9223372036854775809, number limit overflow\n");
  3523. }
  3524. Y_UNIT_TEST(InvaildUsageReal0) {
  3525. NYql::TAstParseResult res = SqlToYql("select .0;");
  3526. UNIT_ASSERT(!res.Root);
  3527. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:7: Error: Unexpected token '.' : cannot match to any predicted input...\n\n");
  3528. }
  3529. Y_UNIT_TEST(InvaildUsageReal1) {
  3530. NYql::TAstParseResult res = SqlToYql("select .0f;");
  3531. UNIT_ASSERT(!res.Root);
  3532. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:7: Error: Unexpected token '.' : cannot match to any predicted input...\n\n");
  3533. }
  3534. Y_UNIT_TEST(InvaildUsageWinFunctionWithoutWindow) {
  3535. NYql::TAstParseResult res = SqlToYql("select lead(key, 2) from plato.Input;");
  3536. UNIT_ASSERT(!res.Root);
  3537. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:8: Error: Failed to use window function Lead without window specification\n");
  3538. }
  3539. Y_UNIT_TEST(DropTableWithIfExists) {
  3540. NYql::TAstParseResult res = SqlToYql("DROP TABLE IF EXISTS plato.foo;");
  3541. UNIT_ASSERT(res.Root);
  3542. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  3543. if (word == "Write") {
  3544. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("drop_if_exists"));
  3545. }
  3546. };
  3547. TWordCountHive elementStat = { {TString("Write"), 0}};
  3548. VerifyProgram(res, elementStat, verifyLine);
  3549. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  3550. }
  3551. Y_UNIT_TEST(TooManyErrors) {
  3552. const char* q = R"(
  3553. USE plato;
  3554. select A, B, C, D, E, F, G, H, I, J, K, L, M, N from (select b from `abc`);
  3555. )";
  3556. NYql::TAstParseResult res = SqlToYql(q, 10);
  3557. UNIT_ASSERT(!res.Root);
  3558. UNIT_ASSERT_NO_DIFF(Err2Str(res),
  3559. R"(<main>:3:16: Error: Column A is not in source column set. Did you mean b?
  3560. <main>:3:19: Error: Column B is not in source column set. Did you mean b?
  3561. <main>:3:22: Error: Column C is not in source column set. Did you mean b?
  3562. <main>:3:25: Error: Column D is not in source column set. Did you mean b?
  3563. <main>:3:28: Error: Column E is not in source column set. Did you mean b?
  3564. <main>:3:31: Error: Column F is not in source column set. Did you mean b?
  3565. <main>:3:34: Error: Column G is not in source column set. Did you mean b?
  3566. <main>:3:37: Error: Column H is not in source column set. Did you mean b?
  3567. <main>:3:40: Error: Column I is not in source column set. Did you mean b?
  3568. <main>: Error: Too many issues, code: 1
  3569. )");
  3570. };
  3571. Y_UNIT_TEST(ShouldCloneBindingForNamedParameter) {
  3572. NYql::TAstParseResult res = SqlToYql(R"($f = () -> {
  3573. $value_type = TypeOf(1);
  3574. $pair_type = StructType(
  3575. TypeOf("2") AS key,
  3576. $value_type AS value
  3577. );
  3578. RETURN TupleType(
  3579. ListType($value_type),
  3580. $pair_type);
  3581. };
  3582. select FormatType($f());
  3583. )");
  3584. UNIT_ASSERT(res.Root);
  3585. }
  3586. Y_UNIT_TEST(BlockedInvalidFrameBounds) {
  3587. auto check = [](const TString& frame, const TString& err) {
  3588. const TString prefix = "SELECT SUM(x) OVER w FROM plato.Input WINDOW w AS (PARTITION BY key ORDER BY subkey\n";
  3589. NYql::TAstParseResult res = SqlToYql(prefix + frame + ")");
  3590. UNIT_ASSERT(!res.Root);
  3591. UNIT_ASSERT_NO_DIFF(Err2Str(res), err);
  3592. };
  3593. check("ROWS UNBOUNDED FOLLOWING", "<main>:2:5: Error: Frame cannot start from UNBOUNDED FOLLOWING\n");
  3594. check("ROWS BETWEEN 5 PRECEDING AND UNBOUNDED PRECEDING", "<main>:2:29: Error: Frame cannot end with UNBOUNDED PRECEDING\n");
  3595. check("ROWS BETWEEN CURRENT ROW AND 5 PRECEDING", "<main>:2:13: Error: Frame cannot start from CURRENT ROW and end with PRECEDING\n");
  3596. check("ROWS BETWEEN 5 FOLLOWING AND CURRENT ROW", "<main>:2:14: Error: Frame cannot start from FOLLOWING and end with CURRENT ROW\n");
  3597. check("ROWS BETWEEN 5 FOLLOWING AND 5 PRECEDING", "<main>:2:14: Error: Frame cannot start from FOLLOWING and end with PRECEDING\n");
  3598. }
  3599. Y_UNIT_TEST(BlockedRangeValueWithoutSingleOrderBy) {
  3600. UNIT_ASSERT(SqlToYql("SELECT COUNT(*) OVER (RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) FROM plato.Input").IsOk());
  3601. UNIT_ASSERT(SqlToYql("SELECT COUNT(*) OVER (RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM plato.Input").IsOk());
  3602. auto res = SqlToYql("SELECT COUNT(*) OVER (RANGE 5 PRECEDING) FROM plato.Input");
  3603. UNIT_ASSERT(!res.Root);
  3604. 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");
  3605. res = SqlToYql("SELECT COUNT(*) OVER (ORDER BY key, value RANGE 5 PRECEDING) FROM plato.Input");
  3606. UNIT_ASSERT(!res.Root);
  3607. 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");
  3608. }
  3609. Y_UNIT_TEST(NoColumnsInFrameBounds) {
  3610. NYql::TAstParseResult res = SqlToYql(
  3611. "SELECT SUM(x) OVER w FROM plato.Input WINDOW w AS (ROWS BETWEEN\n"
  3612. " 1 + key PRECEDING AND 2 + key FOLLOWING);");
  3613. UNIT_ASSERT(!res.Root);
  3614. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:2:6: Error: Column reference \"key\" is not allowed in current scope\n");
  3615. }
  3616. Y_UNIT_TEST(WarnOnEmptyFrameBounds) {
  3617. NYql::TAstParseResult res = SqlToYql(
  3618. "SELECT SUM(x) OVER w FROM plato.Input WINDOW w AS (PARTITION BY key ORDER BY subkey\n"
  3619. "ROWS BETWEEN 10 FOLLOWING AND 5 FOLLOWING)");
  3620. UNIT_ASSERT(res.Root);
  3621. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:2:14: Warning: Used frame specification implies empty window frame, code: 4520\n");
  3622. }
  3623. Y_UNIT_TEST(WarnOnRankWithUnorderedWindow) {
  3624. NYql::TAstParseResult res = SqlToYql("SELECT RANK() OVER w FROM plato.Input WINDOW w AS ()");
  3625. UNIT_ASSERT(res.Root);
  3626. 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");
  3627. }
  3628. Y_UNIT_TEST(WarnOnRankExprWithUnorderedWindow) {
  3629. NYql::TAstParseResult res = SqlToYql("SELECT RANK(key) OVER w FROM plato.Input WINDOW w AS ()");
  3630. UNIT_ASSERT(res.Root);
  3631. 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");
  3632. }
  3633. Y_UNIT_TEST(AnyAsTableName) {
  3634. NYql::TAstParseResult res = SqlToYql("use plato; select * from any;");
  3635. UNIT_ASSERT(!res.Root);
  3636. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:28: Error: Unexpected token ';' : syntax error...\n\n");
  3637. }
  3638. Y_UNIT_TEST(IncorrectOrderOfLambdaOptionalArgs) {
  3639. NYql::TAstParseResult res = SqlToYql("$f = ($x?, $y)->($x + $y); select $f(1);");
  3640. UNIT_ASSERT(!res.Root);
  3641. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:12: Error: Non-optional argument can not follow optional one\n");
  3642. }
  3643. Y_UNIT_TEST(IncorrectOrderOfActionOptionalArgs) {
  3644. NYql::TAstParseResult res = SqlToYql("define action $f($x?, $y) as select $x,$y; end define; do $f(1);");
  3645. UNIT_ASSERT(!res.Root);
  3646. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:23: Error: Non-optional argument can not follow optional one\n");
  3647. }
  3648. Y_UNIT_TEST(NotAllowedQuestionOnNamedNode) {
  3649. NYql::TAstParseResult res = SqlToYql("$f = 1; select $f?;");
  3650. UNIT_ASSERT(!res.Root);
  3651. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:18: Error: Unexpected token '?' at the end of expression\n");
  3652. }
  3653. Y_UNIT_TEST(AnyAndCrossJoin) {
  3654. NYql::TAstParseResult res = SqlToYql("use plato; select * from any Input1 cross join Input2");
  3655. UNIT_ASSERT(!res.Root);
  3656. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:26: Error: ANY should not be used with Cross JOIN\n");
  3657. res = SqlToYql("use plato; select * from Input1 cross join any Input2");
  3658. UNIT_ASSERT(!res.Root);
  3659. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:44: Error: ANY should not be used with Cross JOIN\n");
  3660. }
  3661. Y_UNIT_TEST(AnyWithCartesianProduct) {
  3662. NYql::TAstParseResult res = SqlToYql("pragma AnsiImplicitCrossJoin; use plato; select * from any Input1, Input2");
  3663. UNIT_ASSERT(!res.Root);
  3664. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:56: Error: ANY should not be used with Cross JOIN\n");
  3665. res = SqlToYql("pragma AnsiImplicitCrossJoin; use plato; select * from Input1, any Input2");
  3666. UNIT_ASSERT(!res.Root);
  3667. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:64: Error: ANY should not be used with Cross JOIN\n");
  3668. }
  3669. Y_UNIT_TEST(ErrorPlainEndAsInlineActionTerminator) {
  3670. NYql::TAstParseResult res = SqlToYql(
  3671. "do begin\n"
  3672. " select 1\n"
  3673. "; end\n");
  3674. UNIT_ASSERT(!res.Root);
  3675. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:4:0: Error: Unexpected token absence : Missing DO \n\n");
  3676. }
  3677. Y_UNIT_TEST(ErrorMultiWayJoinWithUsing) {
  3678. NYql::TAstParseResult res = SqlToYql(
  3679. "USE plato;\n"
  3680. "PRAGMA DisableSimpleColumns;\n"
  3681. "SELECT *\n"
  3682. "FROM Input1 AS a\n"
  3683. "JOIN Input2 AS b USING(key)\n"
  3684. "JOIN Input3 AS c ON a.key = c.key;\n"
  3685. );
  3686. UNIT_ASSERT(!res.Root);
  3687. UNIT_ASSERT_NO_DIFF(Err2Str(res),
  3688. "<main>:5:24: Error: Multi-way JOINs should be connected with ON clause instead of USING clause\n"
  3689. );
  3690. }
  3691. Y_UNIT_TEST(RequireLabelInFlattenByWithDot) {
  3692. NYql::TAstParseResult res = SqlToYql("select * from plato.Input flatten by x.y");
  3693. UNIT_ASSERT(!res.Root);
  3694. UNIT_ASSERT_NO_DIFF(Err2Str(res),
  3695. "<main>:1:40: Error: Unnamed expression after FLATTEN BY is not allowed\n"
  3696. );
  3697. }
  3698. Y_UNIT_TEST(WarnUnnamedColumns) {
  3699. NYql::TAstParseResult res = SqlToYql(
  3700. "PRAGMA WarnUnnamedColumns;\n"
  3701. "\n"
  3702. "SELECT key, subkey, key || subkey FROM plato.Input ORDER BY subkey;\n");
  3703. UNIT_ASSERT(res.Root);
  3704. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:3:28: Warning: Autogenerated column name column2 will be used for expression, code: 4516\n");
  3705. }
  3706. Y_UNIT_TEST(WarnSourceColumnMismatch) {
  3707. NYql::TAstParseResult res = SqlToYql(
  3708. "insert into plato.Output (key, subkey, new_value, one_more_value) select key as Key, subkey, value, \"x\" from plato.Input;");
  3709. UNIT_ASSERT(res.Root);
  3710. 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");
  3711. }
  3712. Y_UNIT_TEST(YtCaseInsensitive) {
  3713. NYql::TAstParseResult res = SqlToYql("select * from PlatO.foo;");
  3714. UNIT_ASSERT(res.Root);
  3715. res = SqlToYql("use PlatO; select * from foo;");
  3716. UNIT_ASSERT(res.Root);
  3717. }
  3718. Y_UNIT_TEST(KikimrCaseSensitive) {
  3719. NYql::TAstParseResult res = SqlToYql("select * from PlatO.foo;", 10, "kikimr");
  3720. UNIT_ASSERT(!res.Root);
  3721. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:15: Error: Unknown cluster: PlatO\n");
  3722. res = SqlToYql("use PlatO; select * from foo;", 10, "kikimr");
  3723. UNIT_ASSERT(!res.Root);
  3724. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:5: Error: Unknown cluster: PlatO\n");
  3725. }
  3726. Y_UNIT_TEST(DiscoveryModeForbidden) {
  3727. NYql::TAstParseResult res = SqlToYqlWithMode("insert into plato.Output select * from plato.range(\"\", Input1, Input4)", NSQLTranslation::ESqlMode::DISCOVERY);
  3728. UNIT_ASSERT(!res.Root);
  3729. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:40: Error: range is not allowed in Discovery mode, code: 4600\n");
  3730. res = SqlToYqlWithMode("insert into plato.Output select * from plato.like(\"\", \"Input%\")", NSQLTranslation::ESqlMode::DISCOVERY);
  3731. UNIT_ASSERT(!res.Root);
  3732. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:40: Error: like is not allowed in Discovery mode, code: 4600\n");
  3733. res = SqlToYqlWithMode("insert into plato.Output select * from plato.regexp(\"\", \"Input.\")", NSQLTranslation::ESqlMode::DISCOVERY);
  3734. UNIT_ASSERT(!res.Root);
  3735. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:40: Error: regexp is not allowed in Discovery mode, code: 4600\n");
  3736. res = SqlToYqlWithMode("insert into plato.Output select * from plato.filter(\"\", ($name) -> { return find($name, \"Input\") is not null; })", NSQLTranslation::ESqlMode::DISCOVERY);
  3737. UNIT_ASSERT(!res.Root);
  3738. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:40: Error: filter is not allowed in Discovery mode, code: 4600\n");
  3739. res = SqlToYqlWithMode("select Path from plato.folder(\"\") where Type == \"table\"", NSQLTranslation::ESqlMode::DISCOVERY);
  3740. UNIT_ASSERT(!res.Root);
  3741. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:18: Error: folder is not allowed in Discovery mode, code: 4600\n");
  3742. }
  3743. Y_UNIT_TEST(YsonFuncWithoutArgs) {
  3744. UNIT_ASSERT(SqlToYql("SELECT Yson::SerializeText(Yson::From());").IsOk());
  3745. }
  3746. Y_UNIT_TEST(CanNotUseOrderByInNonLastSelectInUnionAllChain) {
  3747. auto req = "pragma AnsiOrderByLimitInUnionAll;\n"
  3748. "use plato;\n"
  3749. "\n"
  3750. "select * from Input order by key\n"
  3751. "union all\n"
  3752. "select * from Input order by key limit 1;";
  3753. auto res = SqlToYql(req);
  3754. UNIT_ASSERT(!res.Root);
  3755. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:4:21: Error: ORDER BY within UNION ALL is only allowed after last subquery\n");
  3756. }
  3757. Y_UNIT_TEST(CanNotUseLimitInNonLastSelectInUnionAllChain) {
  3758. auto req = "pragma AnsiOrderByLimitInUnionAll;\n"
  3759. "use plato;\n"
  3760. "\n"
  3761. "select * from Input limit 1\n"
  3762. "union all\n"
  3763. "select * from Input order by key limit 1;";
  3764. auto res = SqlToYql(req);
  3765. UNIT_ASSERT(!res.Root);
  3766. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:4:21: Error: LIMIT within UNION ALL is only allowed after last subquery\n");
  3767. }
  3768. Y_UNIT_TEST(CanNotUseDiscardInNonFirstSelectInUnionAllChain) {
  3769. auto req = "pragma AnsiOrderByLimitInUnionAll;\n"
  3770. "use plato;\n"
  3771. "\n"
  3772. "select * from Input\n"
  3773. "union all\n"
  3774. "discard select * from Input;";
  3775. auto res = SqlToYql(req);
  3776. UNIT_ASSERT(!res.Root);
  3777. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:6:1: Error: DISCARD within UNION ALL is only allowed before first subquery\n");
  3778. }
  3779. Y_UNIT_TEST(CanNotUseIntoResultInNonLastSelectInUnionAllChain) {
  3780. auto req = "use plato;\n"
  3781. "pragma AnsiOrderByLimitInUnionAll;\n"
  3782. "\n"
  3783. "select * from Input\n"
  3784. "union all\n"
  3785. "discard select * from Input;";
  3786. auto res = SqlToYql(req);
  3787. UNIT_ASSERT(!res.Root);
  3788. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:6:1: Error: DISCARD within UNION ALL is only allowed before first subquery\n");
  3789. }
  3790. Y_UNIT_TEST(YsonStrictInvalidPragma) {
  3791. auto res = SqlToYql("pragma yson.Strict = \"wrong\";");
  3792. UNIT_ASSERT(!res.Root);
  3793. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:22: Error: Expected 'true', 'false' or no parameter for: Strict\n");
  3794. }
  3795. Y_UNIT_TEST(WarnTableNameInSomeContexts) {
  3796. UNIT_ASSERT(SqlToYql("use plato; select TableName() from Input;").IsOk());
  3797. UNIT_ASSERT(SqlToYql("use plato; select TableName(\"aaaa\");").IsOk());
  3798. UNIT_ASSERT(SqlToYql("select TableName(\"aaaa\", \"yt\");").IsOk());
  3799. auto res = SqlToYql("select TableName() from plato.Input;");
  3800. UNIT_ASSERT(!res.Root);
  3801. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:8: Error: TableName requires either service name as second argument or current cluster name\n");
  3802. res = SqlToYql("use plato;\n"
  3803. "select TableName() from Input1 as a join Input2 as b using(key);");
  3804. UNIT_ASSERT(res.Root);
  3805. 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");
  3806. res = SqlToYql("use plato;\n"
  3807. "select SOME(TableName()), key from Input group by key;");
  3808. UNIT_ASSERT(res.Root);
  3809. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:2:13: Warning: TableName() will produce empty result when used with aggregation.\n"
  3810. "Please consult documentation for possible workaround, code: 4525\n");
  3811. }
  3812. Y_UNIT_TEST(WarnOnDistincWithHavingWithoutAggregations) {
  3813. auto res = SqlToYql("select distinct key from plato.Input having key != '0';");
  3814. UNIT_ASSERT(res.Root);
  3815. 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");
  3816. }
  3817. Y_UNIT_TEST(FlattenByExprWithNestedNull) {
  3818. auto res = SqlToYql("USE plato;\n"
  3819. "\n"
  3820. "SELECT * FROM (SELECT 1 AS region_id)\n"
  3821. "FLATTEN BY (\n"
  3822. " CAST($unknown(region_id) AS List<String>) AS region\n"
  3823. ")");
  3824. UNIT_ASSERT(!res.Root);
  3825. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:5:10: Error: Unknown name: $unknown\n");
  3826. }
  3827. Y_UNIT_TEST(EmptySymbolNameIsForbidden) {
  3828. auto req = " $`` = 1; select $``;";
  3829. auto res = SqlToYql(req);
  3830. UNIT_ASSERT(!res.Root);
  3831. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:5: Error: Empty symbol name is not allowed\n");
  3832. }
  3833. Y_UNIT_TEST(WarnOnBinaryOpWithNullArg) {
  3834. auto req = "select * from plato.Input where cast(key as Int32) != NULL";
  3835. auto res = SqlToYql(req);
  3836. UNIT_ASSERT(res.Root);
  3837. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:52: Warning: Binary operation != will return NULL here, code: 4529\n");
  3838. req = "select 1 or null";
  3839. res = SqlToYql(req);
  3840. UNIT_ASSERT(res.Root);
  3841. UNIT_ASSERT_NO_DIFF(Err2Str(res), "");
  3842. }
  3843. Y_UNIT_TEST(ErrorIfTableSampleArgUsesColumns) {
  3844. auto req = "SELECT key FROM plato.Input TABLESAMPLE BERNOULLI(MIN_OF(100.0, CAST(subkey as Int32)));";
  3845. auto res = SqlToYql(req);
  3846. UNIT_ASSERT(!res.Root);
  3847. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:70: Error: Column reference \"subkey\" is not allowed in current scope\n");
  3848. }
  3849. Y_UNIT_TEST(DerivedColumnListForSelectIsNotSupportedYet) {
  3850. auto req = "SELECT a,b,c FROM plato.Input as t(x,y,z);";
  3851. auto res = SqlToYql(req);
  3852. UNIT_ASSERT(!res.Root);
  3853. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:35: Error: Derived column list is only supported for VALUES\n");
  3854. }
  3855. Y_UNIT_TEST(ErrorIfValuesHasDifferentCountOfColumns) {
  3856. auto req = "VALUES (1,2,3), (4,5);";
  3857. auto res = SqlToYql(req);
  3858. UNIT_ASSERT(!res.Root);
  3859. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:17: Error: All VALUES items should have same size: expecting 3, got 2\n");
  3860. }
  3861. Y_UNIT_TEST(ErrorIfDerivedColumnSizeExceedValuesColumnCount) {
  3862. auto req = "SELECT * FROM(VALUES (1,2), (3,4)) as t(x,y,z);";
  3863. auto res = SqlToYql(req);
  3864. UNIT_ASSERT(!res.Root);
  3865. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:40: Error: Derived column list size exceeds column count in VALUES\n");
  3866. }
  3867. Y_UNIT_TEST(WarnoOnAutogeneratedNamesForValues) {
  3868. auto req = "PRAGMA WarnUnnamedColumns;\n"
  3869. "SELECT * FROM (VALUES (1,2,3,4), (5,6,7,8)) as t(x,y);";
  3870. auto res = SqlToYql(req);
  3871. UNIT_ASSERT(res.Root);
  3872. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:2:16: Warning: Autogenerated column names column2...column3 will be used here, code: 4516\n");
  3873. }
  3874. Y_UNIT_TEST(ErrUnionAllWithOrderByWithoutExplicitLegacyMode) {
  3875. auto req = "use plato;\n"
  3876. "\n"
  3877. "select * from Input order by key\n"
  3878. "union all\n"
  3879. "select * from Input order by key;";
  3880. auto res = SqlToYql(req);
  3881. UNIT_ASSERT(!res.Root);
  3882. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:3:21: Error: ORDER BY within UNION ALL is only allowed after last subquery\n");
  3883. }
  3884. Y_UNIT_TEST(ErrUnionAllWithLimitWithoutExplicitLegacyMode) {
  3885. auto req = "use plato;\n"
  3886. "\n"
  3887. "select * from Input limit 10\n"
  3888. "union all\n"
  3889. "select * from Input limit 1;";
  3890. auto res = SqlToYql(req);
  3891. UNIT_ASSERT(!res.Root);
  3892. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:3:21: Error: LIMIT within UNION ALL is only allowed after last subquery\n");
  3893. }
  3894. Y_UNIT_TEST(ErrUnionAllWithIntoResultWithoutExplicitLegacyMode) {
  3895. auto req = "use plato;\n"
  3896. "\n"
  3897. "select * from Input into result aaa\n"
  3898. "union all\n"
  3899. "select * from Input;";
  3900. auto res = SqlToYql(req);
  3901. UNIT_ASSERT(!res.Root);
  3902. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:3:21: Error: INTO RESULT within UNION ALL is only allowed after last subquery\n");
  3903. }
  3904. Y_UNIT_TEST(ErrUnionAllWithDiscardWithoutExplicitLegacyMode) {
  3905. auto req = "use plato;\n"
  3906. "\n"
  3907. "select * from Input\n"
  3908. "union all\n"
  3909. "discard select * from Input;";
  3910. auto res = SqlToYql(req);
  3911. UNIT_ASSERT(!res.Root);
  3912. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:5:1: Error: DISCARD within UNION ALL is only allowed before first subquery\n");
  3913. }
  3914. Y_UNIT_TEST(ErrUnionAllKeepsIgnoredOrderByWarning) {
  3915. auto req = "use plato;\n"
  3916. "\n"
  3917. "SELECT * FROM (\n"
  3918. " SELECT * FROM Input\n"
  3919. " UNION ALL\n"
  3920. " SELECT t.* FROM Input AS t ORDER BY t.key\n"
  3921. ");";
  3922. auto res = SqlToYql(req);
  3923. UNIT_ASSERT(!res.Root);
  3924. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:4:3: Warning: ORDER BY without LIMIT in subquery will be ignored, code: 4504\n"
  3925. "<main>:6:39: Error: Unknown correlation name: t\n");
  3926. }
  3927. Y_UNIT_TEST(ErrOrderByIgnoredButCheckedForMissingColumns) {
  3928. auto req = "$src = SELECT key FROM (SELECT 1 as key, 2 as subkey) ORDER BY x; SELECT * FROM $src;";
  3929. ExpectFailWithError(req, "<main>:1:8: Warning: ORDER BY without LIMIT in subquery will be ignored, code: 4504\n"
  3930. "<main>:1:64: Error: Column x is not in source column set\n");
  3931. req = "$src = SELECT key FROM plato.Input ORDER BY x; SELECT * FROM $src;";
  3932. auto res = SqlToYql(req);
  3933. UNIT_ASSERT(res.Root);
  3934. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:8: Warning: ORDER BY without LIMIT in subquery will be ignored, code: 4504\n");
  3935. }
  3936. Y_UNIT_TEST(InvalidTtlInterval) {
  3937. auto req = R"(
  3938. USE plato;
  3939. CREATE TABLE tableName (Key Uint32, CreatedAt Timestamp, PRIMARY KEY (Key))
  3940. WITH (TTL = 1 On CreatedAt);
  3941. )";
  3942. auto res = SqlToYql(req);
  3943. UNIT_ASSERT(!res.Root);
  3944. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:4:25: Error: Literal of Interval type is expected for TTL\n"
  3945. "<main>:4:25: Error: Invalid TTL settings\n");
  3946. }
  3947. Y_UNIT_TEST(InvalidTtlUnit) {
  3948. auto req = R"(
  3949. USE plato;
  3950. CREATE TABLE tableName (Key Uint32, CreatedAt Uint32, PRIMARY KEY (Key))
  3951. WITH (TTL = Interval("P1D") On CreatedAt AS PICOSECONDS);
  3952. )";
  3953. auto res = SqlToYql(req);
  3954. UNIT_ASSERT(!res.Root);
  3955. UNIT_ASSERT_STRING_CONTAINS(Err2Str(res), "<main>:4:56: Error: Unexpected token 'PICOSECONDS'");
  3956. }
  3957. Y_UNIT_TEST(InvalidChangefeedSink) {
  3958. auto req = R"(
  3959. USE plato;
  3960. CREATE TABLE tableName (
  3961. Key Uint32, PRIMARY KEY (Key),
  3962. CHANGEFEED feedName WITH (SINK_TYPE = "S3", MODE = "KEYS_ONLY", FORMAT = "json")
  3963. );
  3964. )";
  3965. auto res = SqlToYql(req);
  3966. UNIT_ASSERT(!res.Root);
  3967. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:5:55: Error: Unknown changefeed sink type: S3\n");
  3968. }
  3969. Y_UNIT_TEST(InvalidChangefeedSettings) {
  3970. auto req = R"(
  3971. USE plato;
  3972. CREATE TABLE tableName (
  3973. Key Uint32, PRIMARY KEY (Key),
  3974. CHANGEFEED feedName WITH (SINK_TYPE = "local", FOO = "bar")
  3975. );
  3976. )";
  3977. auto res = SqlToYql(req);
  3978. UNIT_ASSERT(!res.Root);
  3979. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:5:64: Error: Unknown changefeed setting: FOO\n");
  3980. }
  3981. Y_UNIT_TEST(InvalidChangefeedInitialScan) {
  3982. auto req = R"(
  3983. USE plato;
  3984. CREATE TABLE tableName (
  3985. Key Uint32, PRIMARY KEY (Key),
  3986. CHANGEFEED feedName WITH (MODE = "KEYS_ONLY", FORMAT = "json", INITIAL_SCAN = "foo")
  3987. );
  3988. )";
  3989. auto res = SqlToYql(req);
  3990. UNIT_ASSERT(!res.Root);
  3991. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:5:95: Error: Literal of Bool type is expected for INITIAL_SCAN\n");
  3992. }
  3993. Y_UNIT_TEST(InvalidChangefeedVirtualTimestamps) {
  3994. auto req = R"(
  3995. USE plato;
  3996. CREATE TABLE tableName (
  3997. Key Uint32, PRIMARY KEY (Key),
  3998. CHANGEFEED feedName WITH (MODE = "KEYS_ONLY", FORMAT = "json", VIRTUAL_TIMESTAMPS = "foo")
  3999. );
  4000. )";
  4001. auto res = SqlToYql(req);
  4002. UNIT_ASSERT(!res.Root);
  4003. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:5:101: Error: Literal of Bool type is expected for VIRTUAL_TIMESTAMPS\n");
  4004. }
  4005. Y_UNIT_TEST(InvalidChangefeedResolvedTimestamps) {
  4006. auto req = R"(
  4007. USE plato;
  4008. CREATE TABLE tableName (
  4009. Key Uint32, PRIMARY KEY (Key),
  4010. CHANGEFEED feedName WITH (MODE = "KEYS_ONLY", FORMAT = "json", BARRIERS_INTERVAL = "foo")
  4011. );
  4012. )";
  4013. auto res = SqlToYql(req);
  4014. UNIT_ASSERT(!res.Root);
  4015. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:5:100: Error: Literal of Interval type is expected for BARRIERS_INTERVAL\n");
  4016. }
  4017. Y_UNIT_TEST(InvalidChangefeedRetentionPeriod) {
  4018. auto req = R"(
  4019. USE plato;
  4020. CREATE TABLE tableName (
  4021. Key Uint32, PRIMARY KEY (Key),
  4022. CHANGEFEED feedName WITH (MODE = "KEYS_ONLY", FORMAT = "json", RETENTION_PERIOD = "foo")
  4023. );
  4024. )";
  4025. auto res = SqlToYql(req);
  4026. UNIT_ASSERT(!res.Root);
  4027. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:5:99: Error: Literal of Interval type is expected for RETENTION_PERIOD\n");
  4028. }
  4029. Y_UNIT_TEST(InvalidChangefeedTopicPartitions) {
  4030. auto req = R"(
  4031. USE plato;
  4032. CREATE TABLE tableName (
  4033. Key Uint32, PRIMARY KEY (Key),
  4034. CHANGEFEED feedName WITH (MODE = "KEYS_ONLY", FORMAT = "json", TOPIC_MIN_ACTIVE_PARTITIONS = "foo")
  4035. );
  4036. )";
  4037. auto res = SqlToYql(req);
  4038. UNIT_ASSERT(!res.Root);
  4039. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:5:110: Error: Literal of integer type is expected for TOPIC_MIN_ACTIVE_PARTITIONS\n");
  4040. }
  4041. Y_UNIT_TEST(InvalidChangefeedAwsRegion) {
  4042. auto req = R"(
  4043. USE plato;
  4044. CREATE TABLE tableName (
  4045. Key Uint32, PRIMARY KEY (Key),
  4046. CHANGEFEED feedName WITH (MODE = "KEYS_ONLY", FORMAT = "json", AWS_REGION = true)
  4047. );
  4048. )";
  4049. auto res = SqlToYql(req);
  4050. UNIT_ASSERT(!res.Root);
  4051. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:5:93: Error: Literal of String type is expected for AWS_REGION\n");
  4052. }
  4053. Y_UNIT_TEST(ErrJoinWithGroupingSetsWithoutCorrelationName) {
  4054. auto req = "USE plato;\n"
  4055. "\n"
  4056. "SELECT k1, k2, subkey\n"
  4057. "FROM T1 AS a JOIN T2 AS b USING (key)\n"
  4058. "GROUP BY GROUPING SETS(\n"
  4059. " (a.key as k1, b.subkey as k2),\n"
  4060. " (k1),\n"
  4061. " (subkey)\n"
  4062. ");";
  4063. auto res = SqlToYql(req);
  4064. UNIT_ASSERT(!res.Root);
  4065. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:8:4: Error: Columns in grouping sets should have correlation name, error in key: subkey\n");
  4066. }
  4067. Y_UNIT_TEST(ErrJoinWithGroupByWithoutCorrelationName) {
  4068. auto req = "USE plato;\n"
  4069. "\n"
  4070. "SELECT k1, k2,\n"
  4071. " value\n"
  4072. "FROM T1 AS a JOIN T2 AS b USING (key)\n"
  4073. "GROUP BY a.key as k1, b.subkey as k2,\n"
  4074. " value;";
  4075. ExpectFailWithError(req,
  4076. "<main>:7:5: Error: Columns in GROUP BY should have correlation name, error in key: value\n");
  4077. }
  4078. Y_UNIT_TEST(ErrWithMissingFrom) {
  4079. auto req = "select 1 as key where 1 > 1;";
  4080. auto res = SqlToYql(req);
  4081. UNIT_ASSERT(!res.Root);
  4082. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:25: Error: Filtering is not allowed without FROM\n");
  4083. req = "select 1 + count(*);";
  4084. res = SqlToYql(req);
  4085. UNIT_ASSERT(!res.Root);
  4086. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:12: Error: Aggregation is not allowed without FROM\n");
  4087. req = "select 1 as key, subkey + value;";
  4088. res = SqlToYql(req);
  4089. UNIT_ASSERT(!res.Root);
  4090. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:1: Error: Column references are not allowed without FROM\n"
  4091. "<main>:1:18: Error: Column reference 'subkey'\n"
  4092. "<main>:1:1: Error: Column references are not allowed without FROM\n"
  4093. "<main>:1:27: Error: Column reference 'value'\n");
  4094. req = "select count(1) group by key;";
  4095. res = SqlToYql(req);
  4096. UNIT_ASSERT(!res.Root);
  4097. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:1: Error: Column references are not allowed without FROM\n"
  4098. "<main>:1:26: Error: Column reference 'key'\n");
  4099. }
  4100. Y_UNIT_TEST(ErrWithMissingFromForWindow) {
  4101. auto req = "$c = () -> (1 + count(1) over w);\n"
  4102. "select $c();";
  4103. ExpectFailWithError(req,
  4104. "<main>:1:9: Error: Window and aggregation functions are not allowed in this context\n"
  4105. "<main>:1:17: Error: Failed to use aggregation function Count without window specification or in wrong place\n");
  4106. req = "$c = () -> (1 + lead(1) over w);\n"
  4107. "select $c();";
  4108. ExpectFailWithError(req,
  4109. "<main>:1:17: Error: Window functions are not allowed in this context\n"
  4110. "<main>:1:17: Error: Failed to use window function Lead without window specification or in wrong place\n");
  4111. req = "select 1 + count(1) over w window w as ();";
  4112. ExpectFailWithError(req,
  4113. "<main>:1:1: Error: Window and aggregation functions are not allowed without FROM\n"
  4114. "<main>:1:12: Error: Failed to use aggregation function Count without window specification or in wrong place\n");
  4115. req = "select 1 + lead(1) over w window w as ();";
  4116. ExpectFailWithError(req,
  4117. "<main>:1:12: Error: Window functions are not allowed without FROM\n"
  4118. "<main>:1:12: Error: Failed to use window function Lead without window specification or in wrong place\n");
  4119. }
  4120. Y_UNIT_TEST(ErrWithMissingFromForInplaceWindow) {
  4121. auto req = "$c = () -> (1 + count(1) over ());\n"
  4122. "select $c();";
  4123. ExpectFailWithError(req,
  4124. "<main>:1:26: Error: Window and aggregation functions are not allowed in this context\n");
  4125. req = "$c = () -> (1 + lead(1) over (rows between unbounded preceding and current row));\n"
  4126. "select $c();";
  4127. ExpectFailWithError(req,
  4128. "<main>:1:25: Error: Window and aggregation functions are not allowed in this context\n");
  4129. req = "select 1 + count(1) over ();";
  4130. ExpectFailWithError(req,
  4131. "<main>:1:1: Error: Window and aggregation functions are not allowed without FROM\n"
  4132. "<main>:1:12: Error: Failed to use aggregation function Count without window specification or in wrong place\n");
  4133. req = "select 1 + lead(1) over (rows between current row and unbounded following);";
  4134. ExpectFailWithError(req,
  4135. "<main>:1:12: Error: Window functions are not allowed without FROM\n"
  4136. "<main>:1:12: Error: Failed to use window function Lead without window specification or in wrong place\n");
  4137. }
  4138. Y_UNIT_TEST(ErrDistinctInWrongPlace) {
  4139. auto req = "select Some::Udf(distinct key) from plato.Input;";
  4140. ExpectFailWithError(req,
  4141. "<main>:1:18: Error: DISTINCT can only be used in aggregation functions\n");
  4142. req = "select sum(key)(distinct foo) from plato.Input;";
  4143. ExpectFailWithError(req,
  4144. "<main>:1:17: Error: DISTINCT can only be used in aggregation functions\n");
  4145. req = "select len(distinct foo) from plato.Input;";
  4146. ExpectFailWithError(req,
  4147. "<main>:1:8: Error: DISTINCT can only be used in aggregation functions\n");
  4148. req = "$foo = ($x) -> ($x); select $foo(distinct key) from plato.Input;";
  4149. ExpectFailWithError(req,
  4150. "<main>:1:34: Error: DISTINCT can only be used in aggregation functions\n");
  4151. }
  4152. Y_UNIT_TEST(ErrForNotSingleChildInInlineAST) {
  4153. ExpectFailWithError("select YQL::\"\"",
  4154. "<main>:1:8: Error: Failed to parse YQL: expecting AST root node with single child, but got 0\n");
  4155. ExpectFailWithError("select YQL::@@ \t@@",
  4156. "<main>:1:8: Error: Failed to parse YQL: expecting AST root node with single child, but got 0\n");
  4157. auto req = "$lambda = YQL::@@(lambda '(x)(+ x x)) (lambda '(y)(+ y y))@@;\n"
  4158. "select ListMap([1, 2, 3], $lambda);";
  4159. ExpectFailWithError(req,
  4160. "<main>:1:11: Error: Failed to parse YQL: expecting AST root node with single child, but got 2\n");
  4161. }
  4162. Y_UNIT_TEST(ErrEmptyColumnName) {
  4163. ExpectFailWithError("select * without \"\" from plato.Input",
  4164. "<main>:1:18: Error: String literal can not be used here\n");
  4165. ExpectFailWithError("select * without `` from plato.Input;",
  4166. "<main>:1:18: Error: Empty column name is not allowed\n");
  4167. ExpectFailWithErrorForAnsiLexer("select * without \"\" from plato.Input",
  4168. "<main>:1:18: Error: Empty column name is not allowed\n");
  4169. ExpectFailWithErrorForAnsiLexer("select * without `` from plato.Input;",
  4170. "<main>:1:18: Error: Empty column name is not allowed\n");
  4171. }
  4172. Y_UNIT_TEST(ErrOnNonZeroArgumentsForTableRows) {
  4173. ExpectFailWithError("$udf=\"\";process plato.Input using $udf(TableRows(k))",
  4174. "<main>:1:40: Error: TableRows requires exactly 0 arguments\n");
  4175. }
  4176. Y_UNIT_TEST(ErrGroupByWithAggregationFunctionAndDistinctExpr) {
  4177. ExpectFailWithError("select * from plato.Input group by count(distinct key|key)",
  4178. "<main>:1:36: Error: Unable to GROUP BY aggregated values\n");
  4179. }
  4180. // FIXME: check if we can get old behaviour
  4181. #if 0
  4182. Y_UNIT_TEST(ErrWithSchemaWithColumnsWithoutType) {
  4183. ExpectFailWithError("select * from plato.Input with COLUMNs",
  4184. "<main>:1:32: Error: Expected type after COLUMNS\n"
  4185. "<main>:1:32: Error: Failed to parse table hints\n");
  4186. ExpectFailWithError("select * from plato.Input with scheMa",
  4187. "<main>:1:32: Error: Expected type after SCHEMA\n"
  4188. "<main>:1:32: Error: Failed to parse table hints\n");
  4189. }
  4190. #endif
  4191. Y_UNIT_TEST(ErrCollectPreaggregatedInListLiteralWithoutFrom) {
  4192. ExpectFailWithError("SELECT([VARIANCE(DISTINCT[])])",
  4193. "<main>:1:1: Error: Column references are not allowed without FROM\n"
  4194. "<main>:1:9: Error: Column reference '_yql_preagg_Variance0'\n");
  4195. }
  4196. Y_UNIT_TEST(ErrGroupBySmartParenAsTuple) {
  4197. ExpectFailWithError("SELECT * FROM plato.Input GROUP BY (k, v,)",
  4198. "<main>:1:41: Error: Unexpected trailing comma in grouping elements list\n");
  4199. }
  4200. Y_UNIT_TEST(HandleNestedSmartParensInGroupBy) {
  4201. ExpectFailWithError("SELECT * FROM plato.Input GROUP BY (+() as k)",
  4202. "<main>:1:37: Error: Unable to GROUP BY constant expression\n");
  4203. }
  4204. Y_UNIT_TEST(ErrRenameWithAddColumn) {
  4205. ExpectFailWithError("USE plato; ALTER TABLE table RENAME TO moved, ADD COLUMN addc uint64",
  4206. "<main>:1:40: Error: RENAME TO can not be used together with another table action\n");
  4207. }
  4208. Y_UNIT_TEST(ErrAddColumnAndRename) {
  4209. // FIXME: fix positions in ALTER TABLE
  4210. ExpectFailWithError("USE plato; ALTER TABLE table ADD COLUMN addc uint64, RENAME TO moved",
  4211. "<main>:1:46: Error: RENAME TO can not be used together with another table action\n");
  4212. }
  4213. Y_UNIT_TEST(InvalidUuidValue) {
  4214. ExpectFailWithError("SELECT Uuid('123e4567ae89ba12d3aa456a426614174ab0')",
  4215. "<main>:1:8: Error: Invalid value \"123e4567ae89ba12d3aa456a426614174ab0\" for type Uuid\n");
  4216. ExpectFailWithError("SELECT Uuid('123e4567ae89b-12d3-a456-426614174000')",
  4217. "<main>:1:8: Error: Invalid value \"123e4567ae89b-12d3-a456-426614174000\" for type Uuid\n");
  4218. }
  4219. Y_UNIT_TEST(WindowFunctionWithoutOver) {
  4220. ExpectFailWithError("SELECT LAST_VALUE(foo) FROM plato.Input",
  4221. "<main>:1:8: Error: Can't use window function LastValue without window specification (OVER keyword is missing)\n");
  4222. ExpectFailWithError("SELECT LAST_VALUE(foo) FROM plato.Input GROUP BY key",
  4223. "<main>:1:8: Error: Can't use window function LastValue without window specification (OVER keyword is missing)\n");
  4224. }
  4225. Y_UNIT_TEST(CreateAlterUserWithLoginNoLogin) {
  4226. auto reqCreateUser = SqlToYql(R"(
  4227. USE plato;
  4228. CREATE USER user1;
  4229. )");
  4230. UNIT_ASSERT(reqCreateUser.IsOk());
  4231. auto reqAlterUser = SqlToYql(R"(
  4232. USE plato;
  4233. ALTER USER user1;
  4234. )");
  4235. UNIT_ASSERT(!reqAlterUser.IsOk());
  4236. UNIT_ASSERT_STRING_CONTAINS(reqAlterUser.Issues.ToString(), "Error: Unexpected token ';' : cannot match to any predicted input...");
  4237. auto reqPasswordAndLogin = SqlToYql(R"(
  4238. USE plato;
  4239. CREATE USER user1 PASSWORD '123' LOGIN;
  4240. )");
  4241. UNIT_ASSERT(reqPasswordAndLogin.IsOk());
  4242. auto reqPasswordAndNoLogin = SqlToYql(R"(
  4243. USE plato;
  4244. CREATE USER user1 PASSWORD '123' NOLOGIN;
  4245. )");
  4246. UNIT_ASSERT(reqPasswordAndNoLogin.IsOk());
  4247. auto reqLogin = SqlToYql(R"(
  4248. USE plato;
  4249. CREATE USER user1 LOGIN;
  4250. )");
  4251. UNIT_ASSERT(reqLogin.IsOk());
  4252. auto reqNoLogin = SqlToYql(R"(
  4253. USE plato;
  4254. CREATE USER user1 NOLOGIN;
  4255. )");
  4256. UNIT_ASSERT(reqNoLogin.IsOk());
  4257. auto reqLoginNoLogin = SqlToYql(R"(
  4258. USE plato;
  4259. CREATE USER user1 LOGIN NOLOGIN;
  4260. )");
  4261. UNIT_ASSERT(!reqLoginNoLogin.IsOk());
  4262. UNIT_ASSERT_STRING_CONTAINS(reqLoginNoLogin.Issues.ToString(), "Error: Conflicting or redundant options");
  4263. auto reqAlterLoginNoLogin = SqlToYql(R"(
  4264. USE plato;
  4265. CREATE USER user1 LOGIN;
  4266. ALTER USER user1 NOLOGIN;
  4267. )");
  4268. UNIT_ASSERT(reqAlterLoginNoLogin.IsOk());
  4269. auto reqAlterLoginNoLoginWithPassword = SqlToYql(R"(
  4270. USE plato;
  4271. CREATE USER user1 LOGIN;
  4272. ALTER USER user1 PASSWORD '321' NOLOGIN;
  4273. )");
  4274. UNIT_ASSERT(reqAlterLoginNoLoginWithPassword.IsOk());
  4275. }
  4276. Y_UNIT_TEST(CreateUserWithHash) {
  4277. auto reqCreateUser = SqlToYql(R"(
  4278. USE plato;
  4279. CREATE USER user1 HASH '{
  4280. "hash": "p4ffeMugohqyBwyckYCK1TjJfz3LIHbKiGL+t+oEhzw=",
  4281. "salt": "U+tzBtgo06EBQCjlARA6Jg==",
  4282. "type": "argon2id"
  4283. }';
  4284. )");
  4285. UNIT_ASSERT(reqCreateUser.IsOk());
  4286. auto reqCreateUserWithNoLogin = SqlToYql(R"(
  4287. USE plato;
  4288. CREATE USER user1 HASH '{
  4289. "hash": "p4ffeMugohqyBwyckYCK1TjJfz3LIHbKiGL+t+oEhzw=",
  4290. "salt": "U+tzBtgo06EBQCjlARA6Jg==",
  4291. "type": "argon2id"
  4292. }'
  4293. NOLOGIN;
  4294. )");
  4295. UNIT_ASSERT(reqCreateUserWithNoLogin.IsOk());
  4296. auto reqCreateUserWithPassword = SqlToYql(R"(
  4297. USE plato;
  4298. CREATE USER user1 HASH '{
  4299. "hash": "p4ffeMugohqyBwyckYCK1TjJfz3LIHbKiGL+t+oEhzw=",
  4300. "salt": "U+tzBtgo06EBQCjlARA6Jg==",
  4301. "type": "argon2id"
  4302. }'
  4303. PASSWORD '123';
  4304. )");
  4305. UNIT_ASSERT(!reqCreateUserWithPassword.IsOk());
  4306. UNIT_ASSERT_STRING_CONTAINS(reqCreateUserWithPassword.Issues.ToString(), "Error: Conflicting or redundant options");
  4307. auto reqAlterUser = SqlToYql(R"(
  4308. USE plato;
  4309. CREATE USER user1;
  4310. ALTER USER user1 HASH '{
  4311. "hash": "p4ffeMugohqyBwyckYCK1TjJfz3LIHbKiGL+t+oEhzw=",
  4312. "salt": "U+tzBtgo06EBQCjlARA6Jg==",
  4313. "type": "argon2id"
  4314. }';
  4315. )");
  4316. UNIT_ASSERT(reqAlterUser.IsOk());
  4317. }
  4318. Y_UNIT_TEST(CreateAlterUserWithoutCluster) {
  4319. ExpectFailWithError("\n CREATE USER user ENCRYPTED PASSWORD 'foobar';", "<main>:2:2: Error: USE statement is missing - no default cluster is selected\n");
  4320. ExpectFailWithError("ALTER USER CURRENT_USER RENAME TO $foo;", "<main>:1:1: Error: USE statement is missing - no default cluster is selected\n");
  4321. }
  4322. Y_UNIT_TEST(ModifyPermissionsWithoutCluster) {
  4323. ExpectFailWithError("\n GRANT CONNECT ON `/Root` TO user;", "<main>:2:2: Error: USE statement is missing - no default cluster is selected\n");
  4324. ExpectFailWithError("\n REVOKE MANAGE ON `/Root` FROM user;", "<main>:2:2: Error: USE statement is missing - no default cluster is selected\n");
  4325. }
  4326. Y_UNIT_TEST(ReservedRoleNames) {
  4327. ExpectFailWithError("USE plato; CREATE USER current_User;", "<main>:1:24: Error: System role CURRENT_USER can not be used here\n");
  4328. 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");
  4329. UNIT_ASSERT(SqlToYql("USE plato; DROP GROUP IF EXISTS a, b, c, current_User;").IsOk());
  4330. }
  4331. Y_UNIT_TEST(DisableClassicDivisionWithError) {
  4332. ExpectFailWithError("pragma ClassicDivision = 'false'; select $foo / 30;", "<main>:1:42: Error: Unknown name: $foo\n");
  4333. }
  4334. Y_UNIT_TEST(AggregationOfAgrregatedDistinctExpr) {
  4335. ExpectFailWithError("select sum(sum(distinct x + 1)) from plato.Input", "<main>:1:12: Error: Aggregation of aggregated values is forbidden\n");
  4336. }
  4337. Y_UNIT_TEST(WarnForUnusedSqlHint) {
  4338. NYql::TAstParseResult res = SqlToYql("select * from plato.Input1 as a join /*+ merge() */ plato.Input2 as b using(key);\n"
  4339. "select --+ foo(bar)\n"
  4340. " 1;");
  4341. UNIT_ASSERT(res.Root);
  4342. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:2:23: Warning: Hint foo will not be used, code: 4534\n");
  4343. }
  4344. Y_UNIT_TEST(WarnForDeprecatedSchema) {
  4345. NSQLTranslation::TTranslationSettings settings;
  4346. settings.ClusterMapping["s3bucket"] = NYql::S3ProviderName;
  4347. NYql::TAstParseResult res = SqlToYql("select * from s3bucket.`foo` with schema (col1 Int32, String as col2, Int64 as col3);", settings);
  4348. UNIT_ASSERT(res.Root);
  4349. 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");
  4350. }
  4351. Y_UNIT_TEST(ErrorOnColumnNameInMaxByLimit) {
  4352. ExpectFailWithError(
  4353. "SELECT AGGREGATE_BY(AsTuple(value, key), AggregationFactory(\"MAX_BY\", subkey)) FROM plato.Input;",
  4354. "<main>:1:42: Error: Source does not allow column references\n"
  4355. "<main>:1:71: Error: Column reference 'subkey'\n");
  4356. }
  4357. Y_UNIT_TEST(ErrorInLibraryWithTopLevelNamedSubquery) {
  4358. TString withUnusedSubq = "$unused = select max(key) from plato.Input;\n"
  4359. "\n"
  4360. "define subquery $foo() as\n"
  4361. " $count = select count(*) from plato.Input;\n"
  4362. " select * from plato.Input limit $count / 2;\n"
  4363. "end define;\n"
  4364. "export $foo;\n";
  4365. UNIT_ASSERT(SqlToYqlWithMode(withUnusedSubq, NSQLTranslation::ESqlMode::LIBRARY).IsOk());
  4366. TString withTopLevelSubq = "$count = select count(*) from plato.Input;\n"
  4367. "\n"
  4368. "define subquery $foo() as\n"
  4369. " select * from plato.Input limit $count / 2;\n"
  4370. "end define;\n"
  4371. "export $foo;\n";
  4372. auto res = SqlToYqlWithMode(withTopLevelSubq, NSQLTranslation::ESqlMode::LIBRARY);
  4373. UNIT_ASSERT(!res.Root);
  4374. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:17: Error: Named subquery can not be used as a top level statement in libraries\n");
  4375. }
  4376. Y_UNIT_TEST(SessionStartAndSessionStateShouldSurviveSessionWindowArgsError){
  4377. TString query = R"(
  4378. $init = ($_row) -> (min(1, 2)); -- error: aggregation func min() can not be used here
  4379. $calculate = ($_row, $_state) -> (1);
  4380. $update = ($_row, $_state) -> (2);
  4381. SELECT
  4382. SessionStart() over w as session_start,
  4383. SessionState() over w as session_state,
  4384. FROM plato.Input as t
  4385. WINDOW w AS (
  4386. PARTITION BY user, SessionWindow(ts + 1, $init, $update, $calculate)
  4387. )
  4388. )";
  4389. ExpectFailWithError(query, "<main>:2:33: Error: Aggregation function Min requires exactly 1 argument(s), given: 2\n");
  4390. }
  4391. Y_UNIT_TEST(ScalarContextUsage1) {
  4392. TString query = R"(
  4393. $a = (select 1 as x, 2 as y);
  4394. select 1 + $a;
  4395. )";
  4396. ExpectFailWithError(query, "<main>:2:39: Error: Source used in expression should contain one concrete column\n"
  4397. "<main>:3:24: Error: Source is used here\n");
  4398. }
  4399. Y_UNIT_TEST(ScalarContextUsage2) {
  4400. TString query = R"(
  4401. use plato;
  4402. $a = (select 1 as x, 2 as y);
  4403. select * from concat($a);
  4404. )";
  4405. ExpectFailWithError(query, "<main>:3:39: Error: Source used in expression should contain one concrete column\n"
  4406. "<main>:4:34: Error: Source is used here\n");
  4407. }
  4408. Y_UNIT_TEST(ScalarContextUsage3) {
  4409. TString query = R"(
  4410. use plato;
  4411. $a = (select 1 as x, 2 as y);
  4412. select * from range($a);
  4413. )";
  4414. ExpectFailWithError(query, "<main>:3:39: Error: Source used in expression should contain one concrete column\n"
  4415. "<main>:4:33: Error: Source is used here\n");
  4416. }
  4417. Y_UNIT_TEST(ScalarContextUsage4) {
  4418. TString query = R"(
  4419. use plato;
  4420. $a = (select 1 as x, 2 as y);
  4421. insert into $a select 1;
  4422. )";
  4423. ExpectFailWithError(query, "<main>:3:39: Error: Source used in expression should contain one concrete column\n"
  4424. "<main>:4:25: Error: Source is used here\n");
  4425. }
  4426. }
  4427. void CheckUnused(const TString& req, const TString& symbol, unsigned row, unsigned col) {
  4428. auto res = SqlToYql(req);
  4429. UNIT_ASSERT(res.Root);
  4430. UNIT_ASSERT_NO_DIFF(Err2Str(res), TStringBuilder() << "<main>:" << row << ":" << col << ": Warning: Symbol " << symbol << " is not used, code: 4527\n");
  4431. }
  4432. Y_UNIT_TEST_SUITE(WarnUnused) {
  4433. Y_UNIT_TEST(ActionOrSubquery) {
  4434. TString req = " $a()\n"
  4435. "as select 1;\n"
  4436. "end define;\n"
  4437. "\n"
  4438. "select 1;";
  4439. CheckUnused("define action\n" + req, "$a", 2, 3);
  4440. CheckUnused("define subquery\n" + req, "$a", 2, 3);
  4441. }
  4442. Y_UNIT_TEST(Import) {
  4443. TString req = "import lib1 symbols\n"
  4444. " $sqr;\n"
  4445. "select 1;";
  4446. CheckUnused(req, "$sqr", 2, 3);
  4447. req = "import lib1 symbols\n"
  4448. " $sqr as\n"
  4449. " $sq;\n"
  4450. "select 1;";
  4451. CheckUnused(req, "$sq", 3, 5);
  4452. }
  4453. Y_UNIT_TEST(NamedNodeStatement) {
  4454. TString req = " $a, $a = AsTuple(1, 2);\n"
  4455. "select $a;";
  4456. CheckUnused(req, "$a", 1, 2);
  4457. req = "$a, $b = AsTuple(1, 2);\n"
  4458. "select $a;";
  4459. CheckUnused(req, "$b", 1, 6);
  4460. CheckUnused(" $a = 1; $a = 2; select $a;", "$a", 1, 2);
  4461. }
  4462. Y_UNIT_TEST(Declare) {
  4463. CheckUnused("declare $a as String;select 1;", "$a", 1, 9);
  4464. }
  4465. Y_UNIT_TEST(ActionParams) {
  4466. TString req = "define action $a($x, $y) as\n"
  4467. " select $x;\n"
  4468. "end define;\n"
  4469. "\n"
  4470. "do $a(1,2);";
  4471. CheckUnused(req, "$y", 1, 22);
  4472. }
  4473. Y_UNIT_TEST(SubqueryParams) {
  4474. TString req = "use plato;\n"
  4475. "define subquery $q($name, $x) as\n"
  4476. " select * from $name;\n"
  4477. "end define;\n"
  4478. "\n"
  4479. "select * from $q(\"Input\", 1);";
  4480. CheckUnused(req, "$x", 2, 27);
  4481. }
  4482. Y_UNIT_TEST(For) {
  4483. TString req = "define action $a() as\n"
  4484. " select 1;\n"
  4485. "end define;\n"
  4486. "\n"
  4487. "for $i in ListFromRange(1, 10)\n"
  4488. "do $a();";
  4489. CheckUnused(req, "$i", 5, 5);
  4490. }
  4491. Y_UNIT_TEST(LambdaParams) {
  4492. TString req = "$lambda = ($x, $y) -> ($x);\n"
  4493. "select $lambda(1, 2);";
  4494. CheckUnused(req, "$y", 1, 16);
  4495. }
  4496. Y_UNIT_TEST(InsideLambdaBody) {
  4497. TString req = "$lambda = () -> {\n"
  4498. " $x = 1; return 1;\n"
  4499. "};\n"
  4500. "select $lambda();";
  4501. CheckUnused(req, "$x", 2, 3);
  4502. req = "$lambda = () -> {\n"
  4503. " $x = 1; $x = 2; return $x;\n"
  4504. "};\n"
  4505. "select $lambda();";
  4506. CheckUnused(req, "$x", 2, 3);
  4507. }
  4508. Y_UNIT_TEST(InsideAction) {
  4509. TString req = "define action $a() as\n"
  4510. " $x = 1; select 1;\n"
  4511. "end define;\n"
  4512. "\n"
  4513. "do $a();";
  4514. CheckUnused(req, "$x", 2, 3);
  4515. req = "define action $a() as\n"
  4516. " $x = 1; $x = 2; select $x;\n"
  4517. "end define;\n"
  4518. "\n"
  4519. "do $a();";
  4520. CheckUnused(req, "$x", 2, 3);
  4521. }
  4522. Y_UNIT_TEST(NoWarnOnNestedActions) {
  4523. auto req = "pragma warning(\"error\", \"4527\");\n"
  4524. "define action $action($b) as\n"
  4525. " define action $aaa() as\n"
  4526. " select $b;\n"
  4527. " end define;\n"
  4528. " do $aaa();\n"
  4529. "end define;\n"
  4530. "\n"
  4531. "do $action(1);";
  4532. UNIT_ASSERT(SqlToYql(req).IsOk());
  4533. }
  4534. Y_UNIT_TEST(NoWarnForUsageAfterSubquery) {
  4535. auto req = "use plato;\n"
  4536. "pragma warning(\"error\", \"4527\");\n"
  4537. "\n"
  4538. "$a = 1;\n"
  4539. "\n"
  4540. "define subquery $q($table) as\n"
  4541. " select * from $table;\n"
  4542. "end define;\n"
  4543. "\n"
  4544. "select * from $q(\"Input\");\n"
  4545. "select $a;";
  4546. UNIT_ASSERT(SqlToYql(req).IsOk());
  4547. }
  4548. }
  4549. Y_UNIT_TEST_SUITE(AnonymousNames) {
  4550. Y_UNIT_TEST(ReferenceAnonymousVariableIsForbidden) {
  4551. auto req = "$_ = 1; select $_;";
  4552. auto res = SqlToYql(req);
  4553. UNIT_ASSERT(!res.Root);
  4554. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:16: Error: Unable to reference anonymous name $_\n");
  4555. req = "$`_` = 1; select $`_`;";
  4556. res = SqlToYql(req);
  4557. UNIT_ASSERT(!res.Root);
  4558. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:18: Error: Unable to reference anonymous name $_\n");
  4559. }
  4560. Y_UNIT_TEST(Declare) {
  4561. auto req = "declare $_ as String;";
  4562. auto res = SqlToYql(req);
  4563. UNIT_ASSERT(!res.Root);
  4564. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:9: Error: Can not use anonymous name '$_' in DECLARE statement\n");
  4565. }
  4566. Y_UNIT_TEST(ActionSubquery) {
  4567. auto req = "define action $_() as select 1; end define;";
  4568. auto res = SqlToYql(req);
  4569. UNIT_ASSERT(!res.Root);
  4570. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:15: Error: Can not use anonymous name '$_' as ACTION name\n");
  4571. req = "define subquery $_() as select 1; end define;";
  4572. res = SqlToYql(req);
  4573. UNIT_ASSERT(!res.Root);
  4574. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:17: Error: Can not use anonymous name '$_' as SUBQUERY name\n");
  4575. }
  4576. Y_UNIT_TEST(Import) {
  4577. auto req = "import lib symbols $sqr as $_;";
  4578. auto res = SqlToYql(req);
  4579. UNIT_ASSERT(!res.Root);
  4580. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:28: Error: Can not import anonymous name $_\n");
  4581. }
  4582. Y_UNIT_TEST(Export) {
  4583. auto req = "export $_;";
  4584. auto res = SqlToYqlWithMode(req, NSQLTranslation::ESqlMode::LIBRARY);
  4585. UNIT_ASSERT(!res.Root);
  4586. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:8: Error: Can not export anonymous name $_\n");
  4587. }
  4588. Y_UNIT_TEST(AnonymousInActionArgs) {
  4589. auto req = "pragma warning(\"error\", \"4527\");\n"
  4590. "define action $a($_, $y, $_) as\n"
  4591. " select $y;\n"
  4592. "end define;\n"
  4593. "\n"
  4594. "do $a(1,2,3);";
  4595. UNIT_ASSERT(SqlToYql(req).IsOk());
  4596. }
  4597. Y_UNIT_TEST(AnonymousInSubqueryArgs) {
  4598. auto req = "use plato;\n"
  4599. "pragma warning(\"error\", \"4527\");\n"
  4600. "define subquery $q($_, $y, $_) as\n"
  4601. " select * from $y;\n"
  4602. "end define;\n"
  4603. "\n"
  4604. "select * from $q(1,\"Input\",3);";
  4605. UNIT_ASSERT(SqlToYql(req).IsOk());
  4606. }
  4607. Y_UNIT_TEST(AnonymousInLambdaArgs) {
  4608. auto req = "pragma warning(\"error\", \"4527\");\n"
  4609. "$lambda = ($_, $x, $_) -> ($x);\n"
  4610. "select $lambda(1,2,3);";
  4611. UNIT_ASSERT(SqlToYql(req).IsOk());
  4612. }
  4613. Y_UNIT_TEST(AnonymousInFor) {
  4614. auto req = "pragma warning(\"error\", \"4527\");\n"
  4615. "evaluate for $_ in ListFromRange(1, 10) do begin select 1; end do;";
  4616. UNIT_ASSERT(SqlToYql(req).IsOk());
  4617. }
  4618. Y_UNIT_TEST(Assignment) {
  4619. auto req = "pragma warning(\"error\", \"4527\");\n"
  4620. "$_ = 1;\n"
  4621. "$_, $x, $_ = AsTuple(1,2,3);\n"
  4622. "select $x;";
  4623. UNIT_ASSERT(SqlToYql(req).IsOk());
  4624. }
  4625. }
  4626. Y_UNIT_TEST_SUITE(JsonValue) {
  4627. Y_UNIT_TEST(JsonValueArgumentCount) {
  4628. NYql::TAstParseResult res = SqlToYql("select JSON_VALUE(CAST(@@{\"key\": 1238}@@ as Json));");
  4629. UNIT_ASSERT(!res.Root);
  4630. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:49: Error: Unexpected token ')' : syntax error...\n\n");
  4631. }
  4632. Y_UNIT_TEST(JsonValueJsonPathMustBeLiteralString) {
  4633. NYql::TAstParseResult res = SqlToYql("$jsonPath = \"strict $.key\"; select JSON_VALUE(CAST(@@{\"key\": 1238}@@ as Json), $jsonPath);");
  4634. UNIT_ASSERT(!res.Root);
  4635. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:79: Error: Unexpected token absence : Missing STRING_VALUE \n\n");
  4636. }
  4637. Y_UNIT_TEST(JsonValueTranslation) {
  4638. NYql::TAstParseResult res = SqlToYql("select JSON_VALUE(CAST(@@{\"key\": 1238}@@ as Json), \"strict $.key\");");
  4639. UNIT_ASSERT(res.Root);
  4640. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  4641. Y_UNUSED(word);
  4642. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("'\"strict $.key\""));
  4643. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("SafeCast"));
  4644. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("DataType 'Json"));
  4645. };
  4646. TWordCountHive elementStat({"JsonValue"});
  4647. VerifyProgram(res, elementStat, verifyLine);
  4648. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["JsonValue"]);
  4649. }
  4650. Y_UNIT_TEST(JsonValueReturningSection) {
  4651. for (const auto& typeName : {"Bool", "Int64", "Double", "String"}) {
  4652. NYql::TAstParseResult res = SqlToYql(
  4653. TStringBuilder() << "select JSON_VALUE(CAST(@@{\"key\": 1238}@@ as Json), \"strict $.key\" RETURNING " << typeName << ");"
  4654. );
  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. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find(TStringBuilder() << "DataType '" << typeName));
  4662. };
  4663. TWordCountHive elementStat({typeName});
  4664. VerifyProgram(res, elementStat, verifyLine);
  4665. UNIT_ASSERT(elementStat[typeName] > 0);
  4666. }
  4667. }
  4668. Y_UNIT_TEST(JsonValueInvalidReturningType) {
  4669. NYql::TAstParseResult res = SqlToYql("select JSON_VALUE(CAST(@@{'key': 1238}@@ as Json), 'strict $.key' RETURNING invalid);");
  4670. UNIT_ASSERT(!res.Root);
  4671. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:77: Error: Unknown simple type 'invalid'\n");
  4672. }
  4673. Y_UNIT_TEST(JsonValueAndReturningInExpressions) {
  4674. NYql::TAstParseResult res = SqlToYql(
  4675. "USE plato\n;"
  4676. "$json_value = \"some string\";\n"
  4677. "SELECT $json_value;\n"
  4678. "SELECT 1 as json_value;\n"
  4679. "SELECT $json_value as json_value;\n"
  4680. "$returning = \"another string\";\n"
  4681. "SELECT $returning;\n"
  4682. "SELECT 1 as returning;\n"
  4683. "SELECT $returning as returning;\n"
  4684. );
  4685. UNIT_ASSERT(res.Root);
  4686. }
  4687. Y_UNIT_TEST(JsonValueValidCaseHandlers) {
  4688. const TVector<std::pair<TString, TString>> testCases = {
  4689. {"", "'DefaultValue (Null)"},
  4690. {"NULL", "'DefaultValue (Null)"},
  4691. {"ERROR", "'Error (Null)"},
  4692. {"DEFAULT 123", "'DefaultValue (Int32 '\"123\")"},
  4693. };
  4694. for (const auto& onEmpty : testCases) {
  4695. for (const auto& onError : testCases) {
  4696. TStringBuilder query;
  4697. query << "$json = CAST(@@{\"key\": 1238}@@ as Json);\n"
  4698. << "SELECT JSON_VALUE($json, \"strict $.key\"";
  4699. if (!onEmpty.first.empty()) {
  4700. query << " " << onEmpty.first << " ON EMPTY";
  4701. }
  4702. if (!onError.first.empty()) {
  4703. query << " " << onError.first << " ON ERROR";
  4704. }
  4705. query << ");\n";
  4706. NYql::TAstParseResult res = SqlToYql(query);
  4707. UNIT_ASSERT(res.Root);
  4708. TVerifyLineFunc verifyLine = [&](const TString& word, const TString& line) {
  4709. Y_UNUSED(word);
  4710. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find(onEmpty.second + " " + onError.second));
  4711. };
  4712. TWordCountHive elementStat({"JsonValue"});
  4713. VerifyProgram(res, elementStat, verifyLine);
  4714. UNIT_ASSERT(elementStat["JsonValue"] > 0);
  4715. }
  4716. }
  4717. }
  4718. Y_UNIT_TEST(JsonValueTooManyCaseHandlers) {
  4719. NYql::TAstParseResult res = SqlToYql(
  4720. "select JSON_VALUE(CAST(@@{\"key\": 1238}@@ as Json), \"strict $.key\" NULL ON EMPTY NULL ON ERROR NULL ON EMPTY);\n"
  4721. );
  4722. UNIT_ASSERT(!res.Root);
  4723. UNIT_ASSERT_NO_DIFF(
  4724. Err2Str(res),
  4725. "<main>:1:52: Error: Only 1 ON EMPTY and/or 1 ON ERROR clause is expected\n"
  4726. );
  4727. }
  4728. Y_UNIT_TEST(JsonValueTooManyOnEmpty) {
  4729. NYql::TAstParseResult res = SqlToYql(
  4730. "select JSON_VALUE(CAST(@@{\"key\": 1238}@@ as Json), \"strict $.key\" NULL ON EMPTY NULL ON EMPTY);\n"
  4731. );
  4732. UNIT_ASSERT(!res.Root);
  4733. UNIT_ASSERT_NO_DIFF(
  4734. Err2Str(res),
  4735. "<main>:1:52: Error: Only 1 ON EMPTY clause is expected\n"
  4736. );
  4737. }
  4738. Y_UNIT_TEST(JsonValueTooManyOnError) {
  4739. NYql::TAstParseResult res = SqlToYql(
  4740. "select JSON_VALUE(CAST(@@{\"key\": 1238}@@ as Json), \"strict $.key\" NULL ON ERROR NULL ON ERROR);\n"
  4741. );
  4742. UNIT_ASSERT(!res.Root);
  4743. UNIT_ASSERT_NO_DIFF(
  4744. Err2Str(res),
  4745. "<main>:1:52: Error: Only 1 ON ERROR clause is expected\n"
  4746. );
  4747. }
  4748. Y_UNIT_TEST(JsonValueOnEmptyAfterOnError) {
  4749. NYql::TAstParseResult res = SqlToYql(
  4750. "select JSON_VALUE(CAST(@@{\"key\": 1238}@@ as Json), \"strict $.key\" NULL ON ERROR NULL ON EMPTY);\n"
  4751. );
  4752. UNIT_ASSERT(!res.Root);
  4753. UNIT_ASSERT_NO_DIFF(
  4754. Err2Str(res),
  4755. "<main>:1:52: Error: ON EMPTY clause must be before ON ERROR clause\n"
  4756. );
  4757. }
  4758. Y_UNIT_TEST(JsonValueNullInput) {
  4759. NYql::TAstParseResult res = SqlToYql(R"(SELECT JSON_VALUE(NULL, "strict $.key");)");
  4760. UNIT_ASSERT(res.Root);
  4761. TVerifyLineFunc verifyLine = [&](const TString& word, const TString& line) {
  4762. Y_UNUSED(word);
  4763. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("(Nothing (OptionalType (DataType 'Json)))"));
  4764. };
  4765. TWordCountHive elementStat({"JsonValue"});
  4766. VerifyProgram(res, elementStat, verifyLine);
  4767. UNIT_ASSERT(elementStat["JsonValue"] > 0);
  4768. }
  4769. }
  4770. Y_UNIT_TEST_SUITE(JsonExists) {
  4771. Y_UNIT_TEST(JsonExistsValidHandlers) {
  4772. const TVector<std::pair<TString, TString>> testCases = {
  4773. {"", "(Just (Bool '\"false\"))"},
  4774. {"TRUE ON ERROR", "(Just (Bool '\"true\"))"},
  4775. {"FALSE ON ERROR", "(Just (Bool '\"false\"))"},
  4776. {"UNKNOWN ON ERROR", "(Nothing (OptionalType (DataType 'Bool)))"},
  4777. // NOTE: in this case we expect arguments of JsonExists callable to end immediately
  4778. // after variables. This parenthesis at the end of the expression is left on purpose
  4779. {"ERROR ON ERROR", "(Utf8 '\"strict $.key\") (JsonVariables))"},
  4780. };
  4781. for (const auto& item : testCases) {
  4782. NYql::TAstParseResult res = SqlToYql(
  4783. TStringBuilder() << R"(
  4784. $json = CAST(@@{"key": 1238}@@ as Json);
  4785. SELECT JSON_EXISTS($json, "strict $.key" )" << item.first << ");\n"
  4786. );
  4787. UNIT_ASSERT(res.Root);
  4788. TVerifyLineFunc verifyLine = [&](const TString& word, const TString& line) {
  4789. Y_UNUSED(word);
  4790. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find(item.second));
  4791. };
  4792. TWordCountHive elementStat({"JsonExists"});
  4793. VerifyProgram(res, elementStat, verifyLine);
  4794. UNIT_ASSERT(elementStat["JsonExists"] > 0);
  4795. }
  4796. }
  4797. Y_UNIT_TEST(JsonExistsInvalidHandler) {
  4798. NYql::TAstParseResult res = SqlToYql(R"(
  4799. $json = CAST(@@{"key": 1238}@@ as Json);
  4800. $default = false;
  4801. SELECT JSON_EXISTS($json, "strict $.key" $default ON ERROR);
  4802. )");
  4803. UNIT_ASSERT(!res.Root);
  4804. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:4:53: Error: Unexpected token absence : Missing RPAREN \n\n");
  4805. }
  4806. Y_UNIT_TEST(JsonExistsNullInput) {
  4807. NYql::TAstParseResult res = SqlToYql(R"(SELECT JSON_EXISTS(NULL, "strict $.key");)");
  4808. UNIT_ASSERT(res.Root);
  4809. TVerifyLineFunc verifyLine = [&](const TString& word, const TString& line) {
  4810. Y_UNUSED(word);
  4811. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("(Nothing (OptionalType (DataType 'Json)))"));
  4812. };
  4813. TWordCountHive elementStat({"JsonExists"});
  4814. VerifyProgram(res, elementStat, verifyLine);
  4815. UNIT_ASSERT(elementStat["JsonExists"] > 0);
  4816. }
  4817. }
  4818. Y_UNIT_TEST_SUITE(JsonQuery) {
  4819. Y_UNIT_TEST(JsonQueryValidHandlers) {
  4820. using TTestSuite = const TVector<std::pair<TString, TString>>;
  4821. TTestSuite wrapCases = {
  4822. {"", "'NoWrap"},
  4823. {"WITHOUT WRAPPER", "'NoWrap"},
  4824. {"WITHOUT ARRAY WRAPPER", "'NoWrap"},
  4825. {"WITH WRAPPER", "'Wrap"},
  4826. {"WITH ARRAY WRAPPER", "'Wrap"},
  4827. {"WITH UNCONDITIONAL WRAPPER", "'Wrap"},
  4828. {"WITH UNCONDITIONAL ARRAY WRAPPER", "'Wrap"},
  4829. {"WITH CONDITIONAL WRAPPER", "'ConditionalWrap"},
  4830. {"WITH CONDITIONAL ARRAY WRAPPER", "'ConditionalWrap"},
  4831. };
  4832. TTestSuite handlerCases = {
  4833. {"", "'Null"},
  4834. {"ERROR", "'Error"},
  4835. {"NULL", "'Null"},
  4836. {"EMPTY ARRAY", "'EmptyArray"},
  4837. {"EMPTY OBJECT", "'EmptyObject"},
  4838. };
  4839. for (const auto& wrap : wrapCases) {
  4840. for (const auto& onError : handlerCases) {
  4841. for (const auto& onEmpty : handlerCases) {
  4842. TStringBuilder query;
  4843. query << R"($json = CAST(@@{"key": [123]}@@ as Json);
  4844. SELECT JSON_QUERY($json, "strict $.key" )" << wrap.first;
  4845. if (!onEmpty.first.empty()) {
  4846. if (wrap.first.StartsWith("WITH ")) {
  4847. continue;
  4848. }
  4849. query << " " << onEmpty.first << " ON EMPTY";
  4850. }
  4851. if (!onError.first.empty()) {
  4852. query << " " << onError.first << " ON ERROR";
  4853. }
  4854. query << ");\n";
  4855. NYql::TAstParseResult res = SqlToYql(query);
  4856. UNIT_ASSERT(res.Root);
  4857. TVerifyLineFunc verifyLine = [&](const TString& word, const TString& line) {
  4858. Y_UNUSED(word);
  4859. const TString args = TStringBuilder() << wrap.second << " " << onEmpty.second << " " << onError.second;
  4860. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find(args));
  4861. };
  4862. Cout << wrap.first << " " << onEmpty.first << " " << onError.first << Endl;
  4863. TWordCountHive elementStat({"JsonQuery"});
  4864. VerifyProgram(res, elementStat, verifyLine);
  4865. UNIT_ASSERT(elementStat["JsonQuery"] > 0);
  4866. }
  4867. }
  4868. }
  4869. }
  4870. Y_UNIT_TEST(JsonQueryOnEmptyWithWrapper) {
  4871. NYql::TAstParseResult res = SqlToYql(R"(
  4872. $json = CAST(@@{"key": 1238}@@ as Json);
  4873. SELECT JSON_QUERY($json, "strict $" WITH ARRAY WRAPPER EMPTY ARRAY ON EMPTY);
  4874. )");
  4875. UNIT_ASSERT(!res.Root);
  4876. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:3:38: Error: ON EMPTY is prohibited because WRAPPER clause is specified\n");
  4877. }
  4878. Y_UNIT_TEST(JsonQueryNullInput) {
  4879. NYql::TAstParseResult res = SqlToYql(R"(SELECT JSON_QUERY(NULL, "strict $.key");)");
  4880. UNIT_ASSERT(res.Root);
  4881. TVerifyLineFunc verifyLine = [&](const TString& word, const TString& line) {
  4882. Y_UNUSED(word);
  4883. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("(Nothing (OptionalType (DataType 'Json)))"));
  4884. };
  4885. TWordCountHive elementStat({"JsonQuery"});
  4886. VerifyProgram(res, elementStat, verifyLine);
  4887. UNIT_ASSERT(elementStat["JsonQuery"] > 0);
  4888. }
  4889. }
  4890. Y_UNIT_TEST_SUITE(JsonPassing) {
  4891. Y_UNIT_TEST(SupportedVariableTypes) {
  4892. const TVector<TString> functions = {"JSON_EXISTS", "JSON_VALUE", "JSON_QUERY"};
  4893. for (const auto& function : functions) {
  4894. const auto query = Sprintf(R"(
  4895. pragma CompactNamedExprs;
  4896. $json = CAST(@@{"key": 1238}@@ as Json);
  4897. SELECT %s(
  4898. $json,
  4899. "strict $.key"
  4900. PASSING
  4901. "string" as var1,
  4902. 1.234 as var2,
  4903. CAST(1 as Int64) as var3,
  4904. true as var4,
  4905. $json as var5
  4906. ))",
  4907. function.data()
  4908. );
  4909. NYql::TAstParseResult res = SqlToYql(query);
  4910. UNIT_ASSERT(res.Root);
  4911. TVerifyLineFunc verifyLine = [&](const TString& word, const TString& line) {
  4912. Y_UNUSED(word);
  4913. UNIT_ASSERT_VALUES_UNEQUAL_C(TString::npos, line.find(R"('('"var1" (String '"string")))"), "Cannot find `var1`");
  4914. UNIT_ASSERT_VALUES_UNEQUAL_C(TString::npos, line.find(R"('('"var2" (Double '"1.234")))"), "Cannot find `var2`");
  4915. UNIT_ASSERT_VALUES_UNEQUAL_C(TString::npos, line.find(R"('('"var3" (SafeCast (Int32 '"1") (DataType 'Int64))))"), "Cannot find `var3`");
  4916. UNIT_ASSERT_VALUES_UNEQUAL_C(TString::npos, line.find(R"('('"var4" (Bool '"true")))"), "Cannot find `var4`");
  4917. UNIT_ASSERT_VALUES_UNEQUAL_C(TString::npos, line.find(R"('('"var5" namedexprnode0))"), "Cannot find `var5`");
  4918. };
  4919. TWordCountHive elementStat({"JsonVariables"});
  4920. VerifyProgram(res, elementStat, verifyLine);
  4921. UNIT_ASSERT(elementStat["JsonVariables"] > 0);
  4922. }
  4923. }
  4924. Y_UNIT_TEST(ValidVariableNames) {
  4925. const TVector<TString> functions = {"JSON_EXISTS", "JSON_VALUE", "JSON_QUERY"};
  4926. for (const auto& function : functions) {
  4927. const auto query = Sprintf(R"(
  4928. $json = CAST(@@{"key": 1238}@@ as Json);
  4929. SELECT %s(
  4930. $json,
  4931. "strict $.key"
  4932. PASSING
  4933. "one" as var1,
  4934. "two" as "VaR2",
  4935. "three" as `var3`,
  4936. "four" as VaR4
  4937. ))",
  4938. function.data()
  4939. );
  4940. NYql::TAstParseResult res = SqlToYql(query);
  4941. UNIT_ASSERT(res.Root);
  4942. TVerifyLineFunc verifyLine = [&](const TString& word, const TString& line) {
  4943. Y_UNUSED(word);
  4944. UNIT_ASSERT_VALUES_UNEQUAL_C(TString::npos, line.find(R"('('"var1" (String '"one")))"), "Cannot find `var1`");
  4945. UNIT_ASSERT_VALUES_UNEQUAL_C(TString::npos, line.find(R"('('"VaR2" (String '"two")))"), "Cannot find `VaR2`");
  4946. UNIT_ASSERT_VALUES_UNEQUAL_C(TString::npos, line.find(R"('('"var3" (String '"three")))"), "Cannot find `var3`");
  4947. UNIT_ASSERT_VALUES_UNEQUAL_C(TString::npos, line.find(R"('('"VaR4" (String '"four")))"), "Cannot find `VaR4`");
  4948. };
  4949. TWordCountHive elementStat({"JsonVariables"});
  4950. VerifyProgram(res, elementStat, verifyLine);
  4951. UNIT_ASSERT(elementStat["JsonVariables"] > 0);
  4952. }
  4953. }
  4954. }
  4955. Y_UNIT_TEST_SUITE(MigrationToJsonApi) {
  4956. Y_UNIT_TEST(WarningOnDeprecatedJsonUdf) {
  4957. NYql::TAstParseResult res = SqlToYql(R"(
  4958. $json = CAST(@@{"key": 1234}@@ as Json);
  4959. SELECT Json::Parse($json);
  4960. )");
  4961. UNIT_ASSERT(res.Root);
  4962. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:3:26: Warning: Json UDF is deprecated. Please use JSON API instead, code: 4506\n");
  4963. }
  4964. }
  4965. Y_UNIT_TEST_SUITE(AnsiIdentsNegative) {
  4966. Y_UNIT_TEST(EnableAnsiLexerFromRequestSpecialComments) {
  4967. auto req = "\n"
  4968. "\t --!ansi_lexer \n"
  4969. "-- Some comment\n"
  4970. "-- another comment\n"
  4971. "pragma SimpleColumns;\n"
  4972. "\n"
  4973. "select 1, '''' as empty;";
  4974. auto res = SqlToYql(req);
  4975. UNIT_ASSERT(res.IsOk());
  4976. UNIT_ASSERT(res.Issues.Size() == 0);
  4977. }
  4978. Y_UNIT_TEST(AnsiLexerShouldNotBeEnabledHere) {
  4979. auto req = "$str = '\n"
  4980. "--!ansi_lexer\n"
  4981. "--!syntax_v1\n"
  4982. "';\n"
  4983. "\n"
  4984. "select 1, $str, \"\" as empty;";
  4985. auto res = SqlToYql(req);
  4986. UNIT_ASSERT(res.IsOk());
  4987. UNIT_ASSERT(res.Issues.Size() == 0);
  4988. }
  4989. Y_UNIT_TEST(DoubleQuotesInDictsTuplesOrLists) {
  4990. auto req = "$d = { 'a': 1, \"b\": 2, 'c': 3,};";
  4991. auto res = SqlToYqlWithAnsiLexer(req);
  4992. UNIT_ASSERT(!res.Root);
  4993. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:16: Error: Column reference \"b\" is not allowed in current scope\n");
  4994. req = "$t = (1, 2, \"a\");";
  4995. res = SqlToYqlWithAnsiLexer(req);
  4996. UNIT_ASSERT(!res.Root);
  4997. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:13: Error: Column reference \"a\" is not allowed in current scope\n");
  4998. req = "$l = ['a', 'b', \"c\"];";
  4999. res = SqlToYqlWithAnsiLexer(req);
  5000. UNIT_ASSERT(!res.Root);
  5001. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:17: Error: Column reference \"c\" is not allowed in current scope\n");
  5002. }
  5003. Y_UNIT_TEST(MultilineComments) {
  5004. auto req = "/*/**/ select 1;";
  5005. auto res = SqlToYql(req);
  5006. UNIT_ASSERT(res.Root);
  5007. res = SqlToYqlWithAnsiLexer(req);
  5008. UNIT_ASSERT(!res.Root);
  5009. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:16: Error: Unexpected character : syntax error...\n\n");
  5010. req = "/*\n"
  5011. "--/*\n"
  5012. "*/ select 1;";
  5013. res = SqlToYql(req);
  5014. UNIT_ASSERT(res.Root);
  5015. res = SqlToYqlWithAnsiLexer(req);
  5016. UNIT_ASSERT(!res.Root);
  5017. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:3:12: Error: Unexpected character : syntax error...\n\n");
  5018. req = "/*\n"
  5019. "/*\n"
  5020. "--*/\n"
  5021. "*/ select 1;";
  5022. res = SqlToYql(req);
  5023. UNIT_ASSERT(!res.Root);
  5024. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:4:0: Error: Unexpected token '*' : cannot match to any predicted input...\n\n");
  5025. res = SqlToYqlWithAnsiLexer(req);
  5026. UNIT_ASSERT(res.Root);
  5027. }
  5028. }
  5029. Y_UNIT_TEST_SUITE(AnsiOptionalAs) {
  5030. Y_UNIT_TEST(OptionalAsInProjection) {
  5031. UNIT_ASSERT(SqlToYql("PRAGMA AnsiOptionalAs; SELECT a b, c FROM plato.Input;").IsOk());
  5032. ExpectFailWithError("PRAGMA DisableAnsiOptionalAs;\n"
  5033. "SELECT a b, c FROM plato.Input;",
  5034. "<main>:2:10: Error: Expecting mandatory AS here. Did you miss comma? Please add PRAGMA AnsiOptionalAs; for ANSI compatibility\n");
  5035. }
  5036. Y_UNIT_TEST(OptionalAsWithKeywords) {
  5037. UNIT_ASSERT(SqlToYql("PRAGMA AnsiOptionalAs; SELECT a type, b data, c source FROM plato.Input;").IsOk());
  5038. }
  5039. }
  5040. Y_UNIT_TEST_SUITE(SessionWindowNegative) {
  5041. Y_UNIT_TEST(SessionWindowWithoutSource) {
  5042. ExpectFailWithError("SELECT 1 + SessionWindow(ts, 32);",
  5043. "<main>:1:12: Error: SessionWindow requires data source\n");
  5044. }
  5045. Y_UNIT_TEST(SessionWindowInProjection) {
  5046. ExpectFailWithError("SELECT 1 + SessionWindow(ts, 32) from plato.Input;",
  5047. "<main>:1:12: Error: SessionWindow can only be used as a top-level GROUP BY / PARTITION BY expression\n");
  5048. }
  5049. Y_UNIT_TEST(SessionWindowWithNonConstSecondArg) {
  5050. ExpectFailWithError(
  5051. "SELECT key, session_start FROM plato.Input\n"
  5052. "GROUP BY SessionWindow(ts, 32 + subkey) as session_start, key;",
  5053. "<main>:2:10: Error: Source does not allow column references\n"
  5054. "<main>:2:33: Error: Column reference 'subkey'\n");
  5055. }
  5056. Y_UNIT_TEST(SessionWindowWithWrongNumberOfArgs) {
  5057. ExpectFailWithError("SELECT * FROM plato.Input GROUP BY SessionWindow()",
  5058. "<main>:1:36: Error: SessionWindow requires either two or four arguments\n");
  5059. ExpectFailWithError("SELECT * FROM plato.Input GROUP BY SessionWindow(key, subkey, 100)",
  5060. "<main>:1:36: Error: SessionWindow requires either two or four arguments\n");
  5061. }
  5062. Y_UNIT_TEST(DuplicateSessionWindow) {
  5063. ExpectFailWithError(
  5064. "SELECT\n"
  5065. " *\n"
  5066. "FROM plato.Input\n"
  5067. "GROUP BY\n"
  5068. " SessionWindow(ts, 10),\n"
  5069. " user,\n"
  5070. " SessionWindow(ts, 20)\n"
  5071. ";",
  5072. "<main>:7:5: Error: Duplicate session window specification:\n"
  5073. "<main>:5:5: Error: Previous session window is declared here\n");
  5074. ExpectFailWithError(
  5075. "SELECT\n"
  5076. " MIN(key) over w\n"
  5077. "FROM plato.Input\n"
  5078. "WINDOW w AS (\n"
  5079. " PARTITION BY SessionWindow(ts, 10), user,\n"
  5080. " SessionWindow(ts, 20)\n"
  5081. ");",
  5082. "<main>:6:5: Error: Duplicate session window specification:\n"
  5083. "<main>:5:18: Error: Previous session window is declared here\n");
  5084. }
  5085. Y_UNIT_TEST(SessionStartStateWithoutSource) {
  5086. ExpectFailWithError("SELECT 1 + SessionStart();",
  5087. "<main>:1:12: Error: SessionStart requires data source\n");
  5088. ExpectFailWithError("SELECT 1 + SessionState();",
  5089. "<main>:1:12: Error: SessionState requires data source\n");
  5090. }
  5091. Y_UNIT_TEST(SessionStartStateWithoutGroupByOrWindow) {
  5092. ExpectFailWithError("SELECT 1 + SessionStart() from plato.Input;",
  5093. "<main>:1:12: Error: SessionStart can not be used without aggregation by SessionWindow\n");
  5094. ExpectFailWithError("SELECT 1 + SessionState() from plato.Input;",
  5095. "<main>:1:12: Error: SessionState can not be used without aggregation by SessionWindow\n");
  5096. }
  5097. Y_UNIT_TEST(SessionStartStateWithGroupByWithoutSession) {
  5098. ExpectFailWithError("SELECT 1 + SessionStart() from plato.Input group by user;",
  5099. "<main>:1:12: Error: SessionStart can not be used here: SessionWindow specification is missing in GROUP BY\n");
  5100. ExpectFailWithError("SELECT 1 + SessionState() from plato.Input group by user;",
  5101. "<main>:1:12: Error: SessionState can not be used here: SessionWindow specification is missing in GROUP BY\n");
  5102. }
  5103. Y_UNIT_TEST(SessionStartStateWithoutOverWithWindowWithoutSession) {
  5104. ExpectFailWithError("SELECT 1 + SessionStart(), MIN(key) over w from plato.Input window w as ()",
  5105. "<main>:1:12: Error: SessionStart can not be used without aggregation by SessionWindow. Maybe you forgot to add OVER `window_name`?\n");
  5106. ExpectFailWithError("SELECT 1 + SessionState(), MIN(key) over w from plato.Input window w as ()",
  5107. "<main>:1:12: Error: SessionState can not be used without aggregation by SessionWindow. Maybe you forgot to add OVER `window_name`?\n");
  5108. }
  5109. Y_UNIT_TEST(SessionStartStateWithWindowWithoutSession) {
  5110. ExpectFailWithError("SELECT 1 + SessionStart() over w, MIN(key) over w from plato.Input window w as ()",
  5111. "<main>:1:12: Error: SessionStart can not be used with window w: SessionWindow specification is missing in PARTITION BY\n");
  5112. ExpectFailWithError("SELECT 1 + SessionState() over w, MIN(key) over w from plato.Input window w as ()",
  5113. "<main>:1:12: Error: SessionState can not be used with window w: SessionWindow specification is missing in PARTITION BY\n");
  5114. }
  5115. Y_UNIT_TEST(SessionStartStateWithSessionedWindow) {
  5116. ExpectFailWithError("SELECT 1 + SessionStart(), MIN(key) over w from plato.Input group by key window w as (partition by SessionWindow(ts, 1)) ",
  5117. "<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");
  5118. ExpectFailWithError("SELECT 1 + SessionState(), MIN(key) over w from plato.Input group by key window w as (partition by SessionWindow(ts, 1)) ",
  5119. "<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");
  5120. }
  5121. Y_UNIT_TEST(AggregationBySessionStateIsNotSupportedYet) {
  5122. ExpectFailWithError("SELECT SOME(1 + SessionState()), key from plato.Input group by key, SessionWindow(ts, 1);",
  5123. "<main>:1:17: Error: SessionState with GROUP BY is not supported yet\n");
  5124. }
  5125. Y_UNIT_TEST(SessionWindowInRtmr) {
  5126. NYql::TAstParseResult res = SqlToYql(
  5127. "SELECT * FROM plato.Input GROUP BY SessionWindow(ts, 10);",
  5128. 10, TString(NYql::RtmrProviderName));
  5129. UNIT_ASSERT(!res.Root);
  5130. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:54: Error: Streaming group by query must have a hopping window specification.\n");
  5131. res = SqlToYql(R"(
  5132. SELECT key, SUM(value) AS value FROM plato.Input
  5133. GROUP BY key, HOP(subkey, "PT10S", "PT30S", "PT20S"), SessionWindow(ts, 10);
  5134. )", 10, TString(NYql::RtmrProviderName));
  5135. UNIT_ASSERT(!res.Root);
  5136. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:2:13: Error: SessionWindow is unsupported for streaming sources\n");
  5137. }
  5138. }
  5139. Y_UNIT_TEST_SUITE(LibraSqlSugar) {
  5140. auto makeResult = [](TStringBuf settings) {
  5141. return SqlToYql(
  5142. TStringBuilder()
  5143. << settings
  5144. << "\n$udf1 = MyLibra::MakeLibraPreprocessor($settings);"
  5145. << "\n$udf2 = CustomLibra::MakeLibraPreprocessor($settings);"
  5146. << "\nPROCESS plato.Input USING $udf1(TableRow())"
  5147. << "\nUNION ALL"
  5148. << "\nPROCESS plato.Input USING $udf2(TableRow());"
  5149. );
  5150. };
  5151. Y_UNIT_TEST(EmptySettings) {
  5152. auto res = makeResult(R"(
  5153. $settings = AsStruct();
  5154. )");
  5155. UNIT_ASSERT(res.IsOk());
  5156. }
  5157. Y_UNIT_TEST(OnlyEntities) {
  5158. auto res = makeResult(R"(
  5159. $settings = AsStruct(
  5160. AsList("A", "B", "C") AS Entities
  5161. );
  5162. )");
  5163. UNIT_ASSERT(res.IsOk());
  5164. }
  5165. Y_UNIT_TEST(EntitiesWithStrategy) {
  5166. auto res = makeResult(R"(
  5167. $settings = AsStruct(
  5168. AsList("A", "B", "C") AS Entities,
  5169. "blacklist" AS EntitiesStrategy
  5170. );
  5171. )");
  5172. UNIT_ASSERT(res.IsOk());
  5173. }
  5174. Y_UNIT_TEST(AllSettings) {
  5175. auto res = makeResult(R"(
  5176. $settings = AsStruct(
  5177. AsList("A", "B", "C") AS Entities,
  5178. "whitelist" AS EntitiesStrategy,
  5179. "path" AS BlockstatDict,
  5180. false AS ParseWithFat,
  5181. "map" AS Mode
  5182. );
  5183. )");
  5184. UNIT_ASSERT(res.IsOk());
  5185. }
  5186. Y_UNIT_TEST(BadStrategy) {
  5187. auto res = makeResult(R"(
  5188. $settings = AsStruct("bad" AS EntitiesStrategy);
  5189. )");
  5190. UNIT_ASSERT_STRING_CONTAINS(
  5191. Err2Str(res),
  5192. "Error: MakeLibraPreprocessor got invalid entities strategy: expected 'whitelist' or 'blacklist'"
  5193. );
  5194. }
  5195. Y_UNIT_TEST(BadEntities) {
  5196. auto res = makeResult(R"(
  5197. $settings = AsStruct(AsList("A", 1) AS Entities);
  5198. )");
  5199. UNIT_ASSERT_STRING_CONTAINS(Err2Str(res), "Error: MakeLibraPreprocessor entity must be string literal");
  5200. }
  5201. }
  5202. Y_UNIT_TEST_SUITE(TrailingQuestionsNegative) {
  5203. Y_UNIT_TEST(Basic) {
  5204. ExpectFailWithError("SELECT 1?;", "<main>:1:9: Error: Unexpected token '?' at the end of expression\n");
  5205. ExpectFailWithError("SELECT 1? + 1;", "<main>:1:10: Error: Unexpected token '+' : cannot match to any predicted input...\n\n");
  5206. ExpectFailWithError("SELECT 1 + 1??? < 2", "<main>:1:13: Error: Unexpected token '?' at the end of expression\n");
  5207. ExpectFailWithError("SELECT 1? > 2? > 3?",
  5208. "<main>:1:11: Error: Unexpected token '?' at the end of expression\n"
  5209. "<main>:1:16: Error: Unexpected token '?' at the end of expression\n"
  5210. "<main>:1:21: Error: Unexpected token '?' at the end of expression\n");
  5211. }
  5212. Y_UNIT_TEST(SmartParen) {
  5213. ExpectFailWithError("$x = 1; SELECT (Int32?, $x?)", "<main>:1:27: Error: Unexpected token '?' at the end of expression\n");
  5214. ExpectFailWithError("SELECT (Int32, foo?)", "<main>:1:19: Error: Unexpected token '?' at the end of expression\n");
  5215. }
  5216. Y_UNIT_TEST(LambdaOptArgs) {
  5217. 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");
  5218. }
  5219. }
  5220. Y_UNIT_TEST_SUITE(FlexibleTypes) {
  5221. Y_UNIT_TEST(AssumeOrderByType) {
  5222. UNIT_ASSERT(SqlToYql("PRAGMA FlexibleTypes; SELECT 1 AS int32 ASSUME ORDER BY int32").IsOk());
  5223. }
  5224. Y_UNIT_TEST(GroupingSets) {
  5225. UNIT_ASSERT(SqlToYql("PRAGMA FlexibleTypes; SELECT COUNT(*) AS cnt, text, uuid FROM plato.Input GROUP BY GROUPING SETS((uuid), (uuid, text));").IsOk());
  5226. }
  5227. Y_UNIT_TEST(WeakField) {
  5228. UNIT_ASSERT(SqlToYql("PRAGMA FlexibleTypes; SELECT WeakField(text, string) as text FROM plato.Input").IsOk());
  5229. }
  5230. Y_UNIT_TEST(Aggregation1) {
  5231. TString q =
  5232. "PRAGMA FlexibleTypes;\n"
  5233. "$foo = ($x, $const, $type) -> ($x || $const || FormatType($type));\n"
  5234. "SELECT $foo(SOME(x), 'aaa', String) FROM plato.Input GROUP BY y;";
  5235. UNIT_ASSERT(SqlToYql(q).IsOk());
  5236. }
  5237. Y_UNIT_TEST(Aggregation2) {
  5238. TString q =
  5239. "PRAGMA FlexibleTypes;\n"
  5240. "SELECT 1 + String + MAX(key) FROM plato.Input;";
  5241. UNIT_ASSERT(SqlToYql(q).IsOk());
  5242. }
  5243. }
  5244. Y_UNIT_TEST_SUITE(ExternalDeclares) {
  5245. Y_UNIT_TEST(BasicUsage) {
  5246. NSQLTranslation::TTranslationSettings settings;
  5247. settings.DeclaredNamedExprs["foo"] = "String";
  5248. auto res = SqlToYqlWithSettings("select $foo;", settings);
  5249. UNIT_ASSERT(res.IsOk());
  5250. UNIT_ASSERT(res.Issues.Size() == 0);
  5251. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  5252. if (word == "declare") {
  5253. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find(R"__((declare "$foo" (DataType 'String)))__"));
  5254. }
  5255. };
  5256. TWordCountHive elementStat = {{TString("declare"), 0}};
  5257. VerifyProgram(res, elementStat, verifyLine);
  5258. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["declare"]);
  5259. }
  5260. Y_UNIT_TEST(DeclareOverrides) {
  5261. NSQLTranslation::TTranslationSettings settings;
  5262. settings.DeclaredNamedExprs["foo"] = "String";
  5263. auto res = SqlToYqlWithSettings("declare $foo as Int32; select $foo;", settings);
  5264. UNIT_ASSERT(res.IsOk());
  5265. UNIT_ASSERT(res.Issues.Size() == 0);
  5266. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  5267. if (word == "declare") {
  5268. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find(R"__((declare "$foo" (DataType 'Int32)))__"));
  5269. }
  5270. };
  5271. TWordCountHive elementStat = {{TString("declare"), 0}};
  5272. VerifyProgram(res, elementStat, verifyLine);
  5273. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["declare"]);
  5274. }
  5275. Y_UNIT_TEST(UnusedDeclareDoesNotProduceWarning) {
  5276. NSQLTranslation::TTranslationSettings settings;
  5277. settings.DeclaredNamedExprs["foo"] = "String";
  5278. auto res = SqlToYqlWithSettings("select 1;", settings);
  5279. UNIT_ASSERT(res.IsOk());
  5280. UNIT_ASSERT(res.Issues.Size() == 0);
  5281. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  5282. if (word == "declare") {
  5283. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find(R"__((declare "$foo" (DataType 'String)))__"));
  5284. }
  5285. };
  5286. TWordCountHive elementStat = {{TString("declare"), 0}};
  5287. VerifyProgram(res, elementStat, verifyLine);
  5288. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["declare"]);
  5289. }
  5290. Y_UNIT_TEST(DeclaresWithInvalidTypesFails) {
  5291. NSQLTranslation::TTranslationSettings settings;
  5292. settings.DeclaredNamedExprs["foo"] = "List<BadType>";
  5293. auto res = SqlToYqlWithSettings("select 1;", settings);
  5294. UNIT_ASSERT(!res.Root);
  5295. UNIT_ASSERT_NO_DIFF(Err2Str(res),
  5296. "<main>:0:5: Error: Unknown type: 'BadType'\n"
  5297. "<main>: Error: Failed to parse type for externally declared name 'foo'\n");
  5298. }
  5299. }
  5300. Y_UNIT_TEST_SUITE(ExternalDataSource) {
  5301. Y_UNIT_TEST(CreateExternalDataSourceWithAuthNone) {
  5302. NYql::TAstParseResult res = SqlToYql(R"sql(
  5303. USE plato;
  5304. CREATE EXTERNAL DATA SOURCE MyDataSource WITH (
  5305. SOURCE_TYPE="ObjectStorage",
  5306. LOCATION="my-bucket",
  5307. AUTH_METHOD="NONE"
  5308. );
  5309. )sql");
  5310. UNIT_ASSERT(res.Root);
  5311. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  5312. if (word == "Write") {
  5313. UNIT_ASSERT_STRING_CONTAINS(line, R"#('('('"auth_method" '"NONE") '('"location" '"my-bucket") '('"source_type" '"ObjectStorage"))#");
  5314. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("createObject"));
  5315. }
  5316. };
  5317. TWordCountHive elementStat = { {TString("Write"), 0} };
  5318. VerifyProgram(res, elementStat, verifyLine);
  5319. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  5320. }
  5321. Y_UNIT_TEST(CreateExternalDataSourceWithAuthServiceAccount) {
  5322. NYql::TAstParseResult res = SqlToYql(R"sql(
  5323. USE plato;
  5324. CREATE EXTERNAL DATA SOURCE MyDataSource WITH (
  5325. SOURCE_TYPE="ObjectStorage",
  5326. LOCATION="my-bucket",
  5327. AUTH_METHOD="SERVICE_ACCOUNT",
  5328. SERVICE_ACCOUNT_ID="sa",
  5329. SERVICE_ACCOUNT_SECRET_NAME="sa_secret_name"
  5330. );
  5331. )sql");
  5332. UNIT_ASSERT(res.Root);
  5333. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  5334. if (word == "Write") {
  5335. 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"))#");
  5336. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("createObject"));
  5337. }
  5338. };
  5339. TWordCountHive elementStat = { {TString("Write"), 0} };
  5340. VerifyProgram(res, elementStat, verifyLine);
  5341. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  5342. }
  5343. Y_UNIT_TEST(CreateExternalDataSourceWithBasic) {
  5344. NYql::TAstParseResult res = SqlToYql(R"sql(
  5345. USE plato;
  5346. CREATE EXTERNAL DATA SOURCE MyDataSource WITH (
  5347. SOURCE_TYPE="PostgreSQL",
  5348. LOCATION="protocol://host:port/",
  5349. AUTH_METHOD="BASIC",
  5350. LOGIN="admin",
  5351. PASSWORD_SECRET_NAME="secret_name"
  5352. );
  5353. )sql");
  5354. UNIT_ASSERT(res.Root);
  5355. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  5356. if (word == "Write") {
  5357. UNIT_ASSERT_STRING_CONTAINS(line, R"#('('('"auth_method" '"BASIC") '('"location" '"protocol://host:port/") '('"login" '"admin") '('"password_secret_name" '"secret_name") '('"source_type" '"PostgreSQL"))#");
  5358. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("createObject"));
  5359. }
  5360. };
  5361. TWordCountHive elementStat = { {TString("Write"), 0} };
  5362. VerifyProgram(res, elementStat, verifyLine);
  5363. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  5364. }
  5365. Y_UNIT_TEST(CreateExternalDataSourceWithMdbBasic) {
  5366. NYql::TAstParseResult res = SqlToYql(R"sql(
  5367. USE plato;
  5368. CREATE EXTERNAL DATA SOURCE MyDataSource WITH (
  5369. SOURCE_TYPE="PostgreSQL",
  5370. LOCATION="protocol://host:port/",
  5371. AUTH_METHOD="MDB_BASIC",
  5372. SERVICE_ACCOUNT_ID="sa",
  5373. SERVICE_ACCOUNT_SECRET_NAME="sa_secret_name",
  5374. LOGIN="admin",
  5375. PASSWORD_SECRET_NAME="secret_name"
  5376. );
  5377. )sql");
  5378. UNIT_ASSERT(res.Root);
  5379. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  5380. if (word == "Write") {
  5381. 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"))#");
  5382. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("createObject"));
  5383. }
  5384. };
  5385. TWordCountHive elementStat = { {TString("Write"), 0} };
  5386. VerifyProgram(res, elementStat, verifyLine);
  5387. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  5388. }
  5389. Y_UNIT_TEST(CreateExternalDataSourceWithAws) {
  5390. NYql::TAstParseResult res = SqlToYql(R"sql(
  5391. USE plato;
  5392. CREATE EXTERNAL DATA SOURCE MyDataSource WITH (
  5393. SOURCE_TYPE="PostgreSQL",
  5394. LOCATION="protocol://host:port/",
  5395. AUTH_METHOD="AWS",
  5396. AWS_ACCESS_KEY_ID_SECRET_NAME="secred_id_name",
  5397. AWS_SECRET_ACCESS_KEY_SECRET_NAME="secret_key_name",
  5398. AWS_REGION="ru-central-1"
  5399. );
  5400. )sql");
  5401. UNIT_ASSERT(res.Root);
  5402. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  5403. if (word == "Write") {
  5404. 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"))#");
  5405. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("createObject"));
  5406. }
  5407. };
  5408. TWordCountHive elementStat = { {TString("Write"), 0} };
  5409. VerifyProgram(res, elementStat, verifyLine);
  5410. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  5411. }
  5412. Y_UNIT_TEST(CreateExternalDataSourceWithToken) {
  5413. NYql::TAstParseResult res = SqlToYql(R"sql(
  5414. USE plato;
  5415. CREATE EXTERNAL DATA SOURCE MyDataSource WITH (
  5416. SOURCE_TYPE="YT",
  5417. LOCATION="protocol://host:port/",
  5418. AUTH_METHOD="TOKEN",
  5419. TOKEN_SECRET_NAME="token_name"
  5420. );
  5421. )sql");
  5422. UNIT_ASSERT(res.Root);
  5423. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  5424. if (word == "Write") {
  5425. UNIT_ASSERT_STRING_CONTAINS(line, R"#('('('"auth_method" '"TOKEN") '('"location" '"protocol://host:port/") '('"source_type" '"YT") '('"token_secret_name" '"token_name"))#");
  5426. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("createObject"));
  5427. }
  5428. };
  5429. TWordCountHive elementStat = { {TString("Write"), 0} };
  5430. VerifyProgram(res, elementStat, verifyLine);
  5431. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  5432. }
  5433. Y_UNIT_TEST(CreateExternalDataSourceWithTablePrefix) {
  5434. NYql::TAstParseResult res = SqlToYql(R"sql(
  5435. USE plato;
  5436. pragma TablePathPrefix='/aba';
  5437. CREATE EXTERNAL DATA SOURCE MyDataSource WITH (
  5438. SOURCE_TYPE="ObjectStorage",
  5439. LOCATION="my-bucket",
  5440. AUTH_METHOD="NONE"
  5441. );
  5442. )sql");
  5443. UNIT_ASSERT(res.Root);
  5444. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  5445. if (word == "Write") {
  5446. UNIT_ASSERT_STRING_CONTAINS(line, "/aba/MyDataSource");
  5447. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("createObject"));
  5448. }
  5449. };
  5450. TWordCountHive elementStat = { {TString("Write"), 0} };
  5451. VerifyProgram(res, elementStat, verifyLine);
  5452. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  5453. }
  5454. Y_UNIT_TEST(CreateExternalDataSourceIfNotExists) {
  5455. NYql::TAstParseResult res = SqlToYql(R"sql(
  5456. USE plato;
  5457. CREATE EXTERNAL DATA SOURCE IF NOT EXISTS MyDataSource WITH (
  5458. SOURCE_TYPE="ObjectStorage",
  5459. LOCATION="my-bucket",
  5460. AUTH_METHOD="NONE"
  5461. );
  5462. )sql");
  5463. UNIT_ASSERT(res.Root);
  5464. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  5465. if (word == "Write") {
  5466. UNIT_ASSERT_STRING_CONTAINS(line, R"#('('('"auth_method" '"NONE") '('"location" '"my-bucket") '('"source_type" '"ObjectStorage"))#");
  5467. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("createObjectIfNotExists"));
  5468. }
  5469. };
  5470. TWordCountHive elementStat = { {TString("Write"), 0} };
  5471. VerifyProgram(res, elementStat, verifyLine);
  5472. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  5473. }
  5474. Y_UNIT_TEST(AlterExternalDataSource) {
  5475. NYql::TAstParseResult res = SqlToYql(R"sql(
  5476. USE plato;
  5477. ALTER EXTERNAL DATA SOURCE MyDataSource
  5478. SET (SOURCE_TYPE = "ObjectStorage", Login = "Admin"),
  5479. SET Location "bucket",
  5480. RESET (Auth_Method, Service_Account_Id, Service_Account_Secret_Name);
  5481. )sql");
  5482. UNIT_ASSERT_C(res.Root, res.Issues.ToString());
  5483. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  5484. if (word == "Write") {
  5485. UNIT_ASSERT_STRING_CONTAINS(line, R"#(('mode 'alterObject))#");
  5486. UNIT_ASSERT_STRING_CONTAINS(line, R"#('('features '('('"location" '"bucket") '('"login" '"Admin") '('"source_type" '"ObjectStorage"))))#");
  5487. UNIT_ASSERT_STRING_CONTAINS(line, R"#('('resetFeatures '('"auth_method" '"service_account_id" '"service_account_secret_name")))#");
  5488. }
  5489. };
  5490. TWordCountHive elementStat = { {TString("Write"), 0} };
  5491. VerifyProgram(res, elementStat, verifyLine);
  5492. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  5493. }
  5494. Y_UNIT_TEST(CreateExternalDataSourceOrReplace) {
  5495. NYql::TAstParseResult res = SqlToYql(R"(
  5496. USE plato;
  5497. CREATE OR REPLACE EXTERNAL DATA SOURCE MyDataSource WITH (
  5498. SOURCE_TYPE="ObjectStorage",
  5499. LOCATION="my-bucket",
  5500. AUTH_METHOD="NONE"
  5501. );
  5502. )");
  5503. UNIT_ASSERT(res.Root);
  5504. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  5505. if (word == "Write") {
  5506. UNIT_ASSERT_STRING_CONTAINS(line, R"#('('('"auth_method" '"NONE") '('"location" '"my-bucket") '('"source_type" '"ObjectStorage"))#");
  5507. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("createObjectOrReplace"));
  5508. }
  5509. };
  5510. TWordCountHive elementStat = { {TString("Write"), 0} };
  5511. VerifyProgram(res, elementStat, verifyLine);
  5512. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  5513. }
  5514. Y_UNIT_TEST(CreateOrReplaceForUnsupportedTableTypesShouldFail) {
  5515. ExpectFailWithError(R"sql(
  5516. USE plato;
  5517. CREATE OR REPLACE TABLE t (a int32 not null, primary key(a, a));
  5518. )sql" , "<main>:3:23: Error: OR REPLACE feature is supported only for EXTERNAL DATA SOURCE and EXTERNAL TABLE\n");
  5519. ExpectFailWithError(R"sql(
  5520. USE plato;
  5521. CREATE OR REPLACE TABLE t (
  5522. Key Uint64,
  5523. Value1 String,
  5524. PRIMARY KEY (Key)
  5525. )
  5526. WITH (
  5527. STORE = COLUMN,
  5528. AUTO_PARTITIONING_MIN_PARTITIONS_COUNT = 10
  5529. );
  5530. )sql" , "<main>:3:23: Error: OR REPLACE feature is supported only for EXTERNAL DATA SOURCE and EXTERNAL TABLE\n");
  5531. }
  5532. Y_UNIT_TEST(CreateExternalDataSourceWithBadArguments) {
  5533. ExpectFailWithError(R"sql(
  5534. USE plato;
  5535. CREATE EXTERNAL DATA SOURCE MyDataSource;
  5536. )sql" , "<main>:3:56: Error: Unexpected token ';' : syntax error...\n\n");
  5537. ExpectFailWithError(R"sql(
  5538. USE plato;
  5539. CREATE EXTERNAL DATA SOURCE MyDataSource WITH (
  5540. LOCATION="my-bucket",
  5541. AUTH_METHOD="NONE"
  5542. );
  5543. )sql" , "<main>:5:33: Error: SOURCE_TYPE requires key\n");
  5544. ExpectFailWithError(R"sql(
  5545. USE plato;
  5546. CREATE EXTERNAL DATA SOURCE MyDataSource WITH (
  5547. SOURCE_TYPE="ObjectStorage",
  5548. LOCATION="my-bucket"
  5549. );
  5550. )sql" , "<main>:5:30: Error: AUTH_METHOD requires key\n");
  5551. ExpectFailWithError(R"sql(
  5552. USE plato;
  5553. CREATE EXTERNAL DATA SOURCE MyDataSource WITH (
  5554. SOURCE_TYPE="ObjectStorage",
  5555. LOCATION="my-bucket",
  5556. AUTH_METHOD="NONE1"
  5557. );
  5558. )sql" , "<main>:6:33: Error: Unknown AUTH_METHOD = NONE1\n");
  5559. ExpectFailWithError(R"sql(
  5560. USE plato;
  5561. CREATE EXTERNAL DATA SOURCE MyDataSource WITH (
  5562. SOURCE_TYPE="ObjectStorage",
  5563. LOCATION="my-bucket",
  5564. AUTH_METHOD="SERVICE_ACCOUNT"
  5565. );
  5566. )sql" , "<main>:6:33: Error: SERVICE_ACCOUNT_ID 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="SERVICE_ACCOUNT",
  5573. SERVICE_ACCOUNT_ID="s1"
  5574. );
  5575. )sql" , "<main>:7:40: Error: SERVICE_ACCOUNT_SECRET_NAME requires key\n");
  5576. ExpectFailWithError(R"sql(
  5577. USE plato;
  5578. CREATE EXTERNAL DATA SOURCE MyDataSource WITH (
  5579. SOURCE_TYPE="ObjectStorage",
  5580. LOCATION="my-bucket",
  5581. AUTH_METHOD="SERVICE_ACCOUNT",
  5582. SERVICE_ACCOUNT_SECRET_NAME="s1"
  5583. );
  5584. )sql" , "<main>:7:49: Error: SERVICE_ACCOUNT_ID requires key\n");
  5585. ExpectFailWithError(R"sql(
  5586. USE plato;
  5587. CREATE EXTERNAL DATA SOURCE MyDataSource WITH (
  5588. SOURCE_TYPE="PostgreSQL",
  5589. LOCATION="protocol://host:port/",
  5590. AUTH_METHOD="BASIC",
  5591. LOGIN="admin"
  5592. );
  5593. )sql" , "<main>:7:27: Error: PASSWORD_SECRET_NAME requires key\n");
  5594. ExpectFailWithError(R"sql(
  5595. USE plato;
  5596. CREATE EXTERNAL DATA SOURCE MyDataSource WITH (
  5597. SOURCE_TYPE="PostgreSQL",
  5598. LOCATION="protocol://host:port/",
  5599. AUTH_METHOD="BASIC",
  5600. PASSWORD_SECRET_NAME="secret_name"
  5601. );
  5602. )sql" , "<main>:7:42: Error: LOGIN requires key\n");
  5603. ExpectFailWithError(R"sql(
  5604. USE plato;
  5605. CREATE EXTERNAL DATA SOURCE MyDataSource WITH (
  5606. SOURCE_TYPE="PostgreSQL",
  5607. LOCATION="protocol://host:port/",
  5608. AUTH_METHOD="MDB_BASIC",
  5609. SERVICE_ACCOUNT_SECRET_NAME="sa_secret_name",
  5610. LOGIN="admin",
  5611. PASSWORD_SECRET_NAME="secret_name"
  5612. );
  5613. )sql" , "<main>:9:42: Error: SERVICE_ACCOUNT_ID requires key\n");
  5614. ExpectFailWithError(R"sql(
  5615. USE plato;
  5616. CREATE EXTERNAL DATA SOURCE MyDataSource WITH (
  5617. SOURCE_TYPE="PostgreSQL",
  5618. LOCATION="protocol://host:port/",
  5619. AUTH_METHOD="MDB_BASIC",
  5620. SERVICE_ACCOUNT_ID="sa",
  5621. LOGIN="admin",
  5622. PASSWORD_SECRET_NAME="secret_name"
  5623. );
  5624. )sql" , "<main>:9:42: Error: SERVICE_ACCOUNT_SECRET_NAME requires key\n");
  5625. ExpectFailWithError(R"sql(
  5626. USE plato;
  5627. CREATE EXTERNAL DATA SOURCE MyDataSource WITH (
  5628. SOURCE_TYPE="PostgreSQL",
  5629. LOCATION="protocol://host:port/",
  5630. AUTH_METHOD="MDB_BASIC",
  5631. SERVICE_ACCOUNT_ID="sa",
  5632. SERVICE_ACCOUNT_SECRET_NAME="sa_secret_name",
  5633. PASSWORD_SECRET_NAME="secret_name"
  5634. );
  5635. )sql" , "<main>:9:42: Error: LOGIN requires key\n");
  5636. ExpectFailWithError(R"sql(
  5637. USE plato;
  5638. CREATE EXTERNAL DATA SOURCE MyDataSource WITH (
  5639. SOURCE_TYPE="PostgreSQL",
  5640. LOCATION="protocol://host:port/",
  5641. AUTH_METHOD="MDB_BASIC",
  5642. SERVICE_ACCOUNT_ID="sa",
  5643. SERVICE_ACCOUNT_SECRET_NAME="sa_secret_name",
  5644. LOGIN="admin"
  5645. );
  5646. )sql" , "<main>:9:27: Error: PASSWORD_SECRET_NAME requires key\n");
  5647. ExpectFailWithError(R"sql(
  5648. USE plato;
  5649. CREATE EXTERNAL DATA SOURCE MyDataSource WITH (
  5650. SOURCE_TYPE="PostgreSQL",
  5651. LOCATION="protocol://host:port/",
  5652. AUTH_METHOD="AWS",
  5653. AWS_SECRET_ACCESS_KEY_SECRET_NAME="secret_key_name",
  5654. AWS_REGION="ru-central-1"
  5655. );
  5656. )sql" , "<main>:8:32: Error: AWS_ACCESS_KEY_ID_SECRET_NAME requires key\n");
  5657. ExpectFailWithError(R"sql(
  5658. USE plato;
  5659. CREATE EXTERNAL DATA SOURCE MyDataSource WITH (
  5660. SOURCE_TYPE="PostgreSQL",
  5661. LOCATION="protocol://host:port/",
  5662. AUTH_METHOD="AWS",
  5663. AWS_ACCESS_KEY_ID_SECRET_NAME="secred_id_name",
  5664. AWS_REGION="ru-central-1"
  5665. );
  5666. )sql" , "<main>:8:32: Error: AWS_SECRET_ACCESS_KEY_SECRET_NAME requires key\n");
  5667. ExpectFailWithError(R"sql(
  5668. USE plato;
  5669. CREATE EXTERNAL DATA SOURCE MyDataSource WITH (
  5670. SOURCE_TYPE="PostgreSQL",
  5671. LOCATION="protocol://host:port/",
  5672. AUTH_METHOD="AWS",
  5673. AWS_SECRET_ACCESS_KEY_SECRET_NAME="secret_key_name",
  5674. AWS_ACCESS_KEY_ID_SECRET_NAME="secred_id_name"
  5675. );
  5676. )sql" , "<main>:8:51: Error: AWS_REGION requires key\n");
  5677. }
  5678. Y_UNIT_TEST(DropExternalDataSourceWithTablePrefix) {
  5679. NYql::TAstParseResult res = SqlToYql(R"sql(
  5680. USE plato;
  5681. DROP EXTERNAL DATA SOURCE MyDataSource;
  5682. )sql");
  5683. UNIT_ASSERT(res.Root);
  5684. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  5685. if (word == "Write") {
  5686. UNIT_ASSERT_VALUES_EQUAL(TString::npos, line.find("'features"));
  5687. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("dropObject"));
  5688. }
  5689. };
  5690. TWordCountHive elementStat = { {TString("Write"), 0}};
  5691. VerifyProgram(res, elementStat, verifyLine);
  5692. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  5693. }
  5694. Y_UNIT_TEST(DropExternalDataSource) {
  5695. NYql::TAstParseResult res = SqlToYql(R"sql(
  5696. USE plato;
  5697. pragma TablePathPrefix='/aba';
  5698. DROP EXTERNAL DATA SOURCE MyDataSource;
  5699. )sql");
  5700. UNIT_ASSERT(res.Root);
  5701. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  5702. if (word == "Write") {
  5703. UNIT_ASSERT_STRING_CONTAINS(line, "/aba/MyDataSource");
  5704. UNIT_ASSERT_VALUES_EQUAL(TString::npos, line.find("'features"));
  5705. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("dropObject"));
  5706. }
  5707. };
  5708. TWordCountHive elementStat = { {TString("Write"), 0}};
  5709. VerifyProgram(res, elementStat, verifyLine);
  5710. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  5711. }
  5712. Y_UNIT_TEST(DropExternalDataSourceIfExists) {
  5713. NYql::TAstParseResult res = SqlToYql(R"sql(
  5714. USE plato;
  5715. DROP EXTERNAL DATA SOURCE IF EXISTS MyDataSource;
  5716. )sql");
  5717. UNIT_ASSERT(res.Root);
  5718. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  5719. if (word == "Write") {
  5720. UNIT_ASSERT_STRING_CONTAINS(line, "MyDataSource");
  5721. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("dropObjectIfExists"));
  5722. }
  5723. };
  5724. TWordCountHive elementStat = { {TString("Write"), 0}};
  5725. VerifyProgram(res, elementStat, verifyLine);
  5726. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  5727. }
  5728. }
  5729. Y_UNIT_TEST_SUITE(ExternalTable) {
  5730. Y_UNIT_TEST(CreateExternalTable) {
  5731. NYql::TAstParseResult res = SqlToYql(R"sql(
  5732. USE plato;
  5733. CREATE EXTERNAL TABLE mytable (
  5734. a int
  5735. ) WITH (
  5736. DATA_SOURCE="/Root/mydatasource",
  5737. LOCATION="/folder1/*"
  5738. );
  5739. )sql");
  5740. UNIT_ASSERT_C(res.Root, res.Issues.ToOneLineString());
  5741. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  5742. if (word == "Write") {
  5743. UNIT_ASSERT_STRING_CONTAINS(line, R"#('('('data_source_path (String '"/Root/mydatasource")) '('location (String '"/folder1/*")))) '('tableType 'externalTable)))))#");
  5744. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("tablescheme"));
  5745. }
  5746. };
  5747. TWordCountHive elementStat = { {TString("Write"), 0} };
  5748. VerifyProgram(res, elementStat, verifyLine);
  5749. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  5750. }
  5751. Y_UNIT_TEST(CreateExternalTableWithTablePrefix) {
  5752. NYql::TAstParseResult res = SqlToYql(R"sql(
  5753. USE plato;
  5754. pragma TablePathPrefix='/aba';
  5755. CREATE EXTERNAL TABLE mytable (
  5756. a int
  5757. ) WITH (
  5758. DATA_SOURCE="mydatasource",
  5759. LOCATION="/folder1/*"
  5760. );
  5761. )sql");
  5762. UNIT_ASSERT_C(res.Root, res.Issues.ToOneLineString());
  5763. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  5764. if (word == "Write") {
  5765. UNIT_ASSERT_STRING_CONTAINS(line, "/aba/mydatasource");
  5766. UNIT_ASSERT_STRING_CONTAINS(line, "/aba/mytable");
  5767. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("tablescheme"));
  5768. }
  5769. };
  5770. TWordCountHive elementStat = { {TString("Write"), 0} };
  5771. VerifyProgram(res, elementStat, verifyLine);
  5772. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  5773. }
  5774. Y_UNIT_TEST(CreateExternalTableObjectStorage) {
  5775. auto res = SqlToYql(R"sql(
  5776. USE plato;
  5777. CREATE EXTERNAL TABLE mytable (
  5778. a int,
  5779. year Int
  5780. ) WITH (
  5781. DATA_SOURCE="/Root/mydatasource",
  5782. LOCATION="/folder1/*",
  5783. FORMAT="json_as_string",
  5784. `projection.enabled`="true",
  5785. `projection.year.type`="integer",
  5786. `projection.year.min`="2010",
  5787. `projection.year.max`="2022",
  5788. `projection.year.interval`="1",
  5789. `projection.month.type`="integer",
  5790. `projection.month.min`="1",
  5791. `projection.month.max`="12",
  5792. `projection.month.interval`="1",
  5793. `projection.month.digits`="2",
  5794. `storage.location.template`="${year}/${month}",
  5795. PARTITONED_BY = "[year, month]"
  5796. );
  5797. )sql");
  5798. UNIT_ASSERT_C(res.IsOk(), res.Issues.ToString());
  5799. }
  5800. Y_UNIT_TEST(CreateExternalTableIfNotExists) {
  5801. NYql::TAstParseResult res = SqlToYql(R"sql(
  5802. USE plato;
  5803. CREATE EXTERNAL TABLE IF NOT EXISTS mytable (
  5804. a int
  5805. ) WITH (
  5806. DATA_SOURCE="/Root/mydatasource",
  5807. LOCATION="/folder1/*"
  5808. );
  5809. )sql");
  5810. UNIT_ASSERT_C(res.Root, res.Issues.ToOneLineString());
  5811. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  5812. if (word == "Write") {
  5813. UNIT_ASSERT_STRING_CONTAINS(line, R"#('('('data_source_path (String '"/Root/mydatasource")) '('location (String '"/folder1/*")))) '('tableType 'externalTable)))))#");
  5814. UNIT_ASSERT_STRING_CONTAINS(line, "create_if_not_exists");
  5815. }
  5816. };
  5817. TWordCountHive elementStat = { {TString("Write"), 0} };
  5818. VerifyProgram(res, elementStat, verifyLine);
  5819. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  5820. }
  5821. Y_UNIT_TEST(CreateExternalTableOrReplace) {
  5822. NYql::TAstParseResult res = SqlToYql(R"(
  5823. USE plato;
  5824. CREATE OR REPLACE EXTERNAL TABLE mytable (
  5825. a int
  5826. ) WITH (
  5827. DATA_SOURCE="/Root/mydatasource",
  5828. LOCATION="/folder1/*"
  5829. );
  5830. )");
  5831. UNIT_ASSERT_C(res.Root, res.Issues.ToOneLineString());
  5832. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  5833. if (word == "Write") {
  5834. UNIT_ASSERT_STRING_CONTAINS(line, R"#('('('data_source_path (String '"/Root/mydatasource")) '('location (String '"/folder1/*")))) '('tableType 'externalTable)))))#");
  5835. UNIT_ASSERT_STRING_CONTAINS(line, "create_or_replace");
  5836. }
  5837. };
  5838. TWordCountHive elementStat = { {TString("Write"), 0} };
  5839. VerifyProgram(res, elementStat, verifyLine);
  5840. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  5841. }
  5842. Y_UNIT_TEST(AlterExternalTableAddColumn) {
  5843. NYql::TAstParseResult res = SqlToYql(R"sql(
  5844. USE plato;
  5845. ALTER EXTERNAL TABLE mytable
  5846. ADD COLUMN my_column int32,
  5847. RESET (LOCATION);
  5848. )sql");
  5849. UNIT_ASSERT_C(res.Root, res.Issues.ToOneLineString());
  5850. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  5851. if (word == "Write") {
  5852. UNIT_ASSERT_STRING_CONTAINS(line, R"#('actions '('('addColumns '('('"my_column" (AsOptionalType (DataType 'Int32))#");
  5853. UNIT_ASSERT_STRING_CONTAINS(line, R"#(('setTableSettings '('('location)))#");
  5854. UNIT_ASSERT_STRING_CONTAINS(line, R"#(('tableType 'externalTable))#");
  5855. UNIT_ASSERT_STRING_CONTAINS(line, R"#(('mode 'alter))#");
  5856. }
  5857. };
  5858. TWordCountHive elementStat = { {TString("Write"), 0} };
  5859. VerifyProgram(res, elementStat, verifyLine);
  5860. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  5861. }
  5862. Y_UNIT_TEST(AlterExternalTableDropColumn) {
  5863. NYql::TAstParseResult res = SqlToYql(R"sql(
  5864. USE plato;
  5865. ALTER EXTERNAL TABLE mytable
  5866. DROP COLUMN my_column,
  5867. SET (Location = "abc", Other_Prop = "42"),
  5868. SET x 'y';
  5869. )sql");
  5870. UNIT_ASSERT_C(res.Root, res.Issues.ToOneLineString());
  5871. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  5872. if (word == "Write") {
  5873. UNIT_ASSERT_STRING_CONTAINS(line, R"#('actions '('('dropColumns '('"my_column")#");
  5874. UNIT_ASSERT_STRING_CONTAINS(line, R"#(('setTableSettings '('('location (String '"abc")) '('Other_Prop (String '"42")) '('x (String '"y")))))#");
  5875. UNIT_ASSERT_STRING_CONTAINS(line, R"#(('tableType 'externalTable))#");
  5876. UNIT_ASSERT_STRING_CONTAINS(line, R"#(('mode 'alter))#");
  5877. }
  5878. };
  5879. TWordCountHive elementStat = { {TString("Write"), 0} };
  5880. VerifyProgram(res, elementStat, verifyLine);
  5881. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  5882. }
  5883. Y_UNIT_TEST(CreateExternalTableWithBadArguments) {
  5884. ExpectFailWithError(R"sql(
  5885. USE plato;
  5886. CREATE EXTERNAL TABLE mytable;
  5887. )sql" , "<main>:3:45: Error: Unexpected token ';' : syntax error...\n\n");
  5888. ExpectFailWithError(R"sql(
  5889. USE plato;
  5890. CREATE EXTERNAL TABLE mytable (
  5891. a int
  5892. );
  5893. )sql" , "<main>:4:23: Error: DATA_SOURCE requires key\n");
  5894. ExpectFailWithError(R"sql(
  5895. USE plato;
  5896. CREATE EXTERNAL TABLE mytable (
  5897. a int
  5898. ) WITH (
  5899. DATA_SOURCE="/Root/mydatasource"
  5900. );
  5901. )sql" , "<main>:6:33: Error: LOCATION requires key\n");
  5902. ExpectFailWithError(R"sql(
  5903. USE plato;
  5904. CREATE EXTERNAL TABLE mytable (
  5905. a int
  5906. ) WITH (
  5907. LOCATION="/folder1/*"
  5908. );
  5909. )sql" , "<main>:6:30: Error: DATA_SOURCE requires key\n");
  5910. ExpectFailWithError(R"sql(
  5911. USE plato;
  5912. CREATE EXTERNAL TABLE mytable (
  5913. a int,
  5914. PRIMARY KEY(a)
  5915. ) WITH (
  5916. DATA_SOURCE="/Root/mydatasource",
  5917. LOCATION="/folder1/*"
  5918. );
  5919. )sql" , "<main>:8:30: Error: PRIMARY KEY is not supported for external table\n");
  5920. }
  5921. Y_UNIT_TEST(DropExternalTable) {
  5922. NYql::TAstParseResult res = SqlToYql(R"sql(
  5923. USE plato;
  5924. DROP EXTERNAL TABLE MyExternalTable;
  5925. )sql");
  5926. UNIT_ASSERT(res.Root);
  5927. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  5928. if (word == "Write") {
  5929. UNIT_ASSERT_VALUES_EQUAL(TString::npos, line.find("tablescheme"));
  5930. }
  5931. };
  5932. TWordCountHive elementStat = { {TString("Write"), 0}};
  5933. VerifyProgram(res, elementStat, verifyLine);
  5934. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  5935. }
  5936. Y_UNIT_TEST(DropExternalTableWithTablePrefix) {
  5937. NYql::TAstParseResult res = SqlToYql(R"sql(
  5938. USE plato;
  5939. pragma TablePathPrefix='/aba';
  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_STRING_CONTAINS(line, "/aba/MyExternalTable");
  5946. UNIT_ASSERT_VALUES_EQUAL(TString::npos, line.find("'tablescheme"));
  5947. }
  5948. };
  5949. TWordCountHive elementStat = { {TString("Write"), 0}};
  5950. VerifyProgram(res, elementStat, verifyLine);
  5951. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  5952. }
  5953. Y_UNIT_TEST(DropExternalTableIfExists) {
  5954. NYql::TAstParseResult res = SqlToYql(R"sql(
  5955. USE plato;
  5956. DROP EXTERNAL TABLE IF EXISTS MyExternalTable;
  5957. )sql");
  5958. UNIT_ASSERT(res.Root);
  5959. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  5960. if (word == "Write") {
  5961. UNIT_ASSERT_VALUES_EQUAL(TString::npos, line.find("tablescheme"));
  5962. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("drop_if_exists"));
  5963. }
  5964. };
  5965. TWordCountHive elementStat = { {TString("Write"), 0}};
  5966. VerifyProgram(res, elementStat, verifyLine);
  5967. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  5968. }
  5969. }
  5970. Y_UNIT_TEST_SUITE(TopicsDDL) {
  5971. void TestQuery(const TString& query, bool expectOk = true) {
  5972. TStringBuilder finalQuery;
  5973. finalQuery << "use plato;" << Endl << query;
  5974. auto res = SqlToYql(finalQuery, 10, "kikimr");
  5975. if (expectOk) {
  5976. UNIT_ASSERT_C(res.IsOk(), res.Issues.ToString());
  5977. } else {
  5978. UNIT_ASSERT(!res.IsOk());
  5979. }
  5980. }
  5981. Y_UNIT_TEST(CreateTopicSimple) {
  5982. TestQuery(R"(
  5983. CREATE TOPIC topic1;
  5984. )");
  5985. TestQuery(R"(
  5986. CREATE TOPIC `cluster1.topic1`;
  5987. )");
  5988. TestQuery(R"(
  5989. CREATE TOPIC topic1 WITH (metering_mode = "str_value", partition_count_limit = 123, retention_period = Interval('PT1H'));
  5990. )");
  5991. }
  5992. Y_UNIT_TEST(CreateTopicConsumer) {
  5993. TestQuery(R"(
  5994. CREATE TOPIC topic1 (CONSUMER cons1);
  5995. )");
  5996. TestQuery(R"(
  5997. CREATE TOPIC topic1 (CONSUMER cons1, CONSUMER cons2 WITH (important = false));
  5998. )");
  5999. TestQuery(R"(
  6000. CREATE TOPIC topic1 (CONSUMER cons1, CONSUMER cons2 WITH (important = false)) WITH (supported_codecs = "1,2,3");
  6001. )");
  6002. }
  6003. Y_UNIT_TEST(AlterTopicSimple) {
  6004. TestQuery(R"(
  6005. ALTER TOPIC topic1 SET (retention_period = Interval('PT1H'));
  6006. )");
  6007. TestQuery(R"(
  6008. ALTER TOPIC topic1 SET (retention_storage_mb = 3, partition_count_limit = 50);
  6009. )");
  6010. TestQuery(R"(
  6011. ALTER TOPIC topic1 RESET (supported_codecs, retention_period);
  6012. )");
  6013. TestQuery(R"(
  6014. ALTER TOPIC topic1 RESET (partition_write_speed_bytes_per_second),
  6015. SET (partition_write_burst_bytes = 11111, min_active_partitions = 1);
  6016. )");
  6017. }
  6018. Y_UNIT_TEST(AlterTopicConsumer) {
  6019. TestQuery(R"(
  6020. ALTER TOPIC topic1 ADD CONSUMER consumer1,
  6021. ADD CONSUMER consumer2 WITH (important = false, supported_codecs = "RAW"),
  6022. ALTER CONSUMER consumer3 SET (important = false, read_from = 1),
  6023. ALTER CONSUMER consumer3 RESET (supported_codecs),
  6024. DROP CONSUMER consumer4,
  6025. SET (partition_count_limit = 11, retention_period = Interval('PT1H')),
  6026. RESET(metering_mode)
  6027. )");
  6028. }
  6029. Y_UNIT_TEST(DropTopic) {
  6030. TestQuery(R"(
  6031. DROP TOPIC topic1;
  6032. )");
  6033. }
  6034. Y_UNIT_TEST(TopicBadRequests) {
  6035. TestQuery(R"(
  6036. CREATE TOPIC topic1();
  6037. )", false);
  6038. TestQuery(R"(
  6039. CREATE TOPIC topic1 SET setting1 = value1;
  6040. )", false);
  6041. TestQuery(R"(
  6042. ALTER TOPIC topic1 SET setting1 value1;
  6043. )", false);
  6044. TestQuery(R"(
  6045. ALTER TOPIC topic1 RESET setting1;
  6046. )", false);
  6047. TestQuery(R"(
  6048. ALTER TOPIC topic1 DROP CONSUMER consumer4 WITH (k1 = v1);
  6049. )", false);
  6050. TestQuery(R"(
  6051. CREATE TOPIC topic1 WITH (retention_period = 123);
  6052. )", false);
  6053. TestQuery(R"(
  6054. CREATE TOPIC topic1 (CONSUMER cons1, CONSUMER cons1 WITH (important = false));
  6055. )", false);
  6056. TestQuery(R"(
  6057. CREATE TOPIC topic1 (CONSUMER cons1 WITH (bad_option = false));
  6058. )", false);
  6059. TestQuery(R"(
  6060. ALTER TOPIC topic1 ADD CONSUMER cons1, ALTER CONSUMER cons1 RESET (important);
  6061. )", false);
  6062. TestQuery(R"(
  6063. ALTER TOPIC topic1 ADD CONSUMER consumer1,
  6064. ALTER CONSUMER consumer3 SET (supported_codecs = "RAW", read_from = 1),
  6065. ALTER CONSUMER consumer3 RESET (supported_codecs);
  6066. )", false);
  6067. TestQuery(R"(
  6068. ALTER TOPIC topic1 ADD CONSUMER consumer1,
  6069. ALTER CONSUMER consumer3 SET (supported_codecs = "RAW", read_from = 1),
  6070. ALTER CONSUMER consumer3 SET (read_from = 2);
  6071. )", false);
  6072. }
  6073. Y_UNIT_TEST(TopicWithPrefix) {
  6074. NYql::TAstParseResult res = SqlToYql(R"(
  6075. USE plato;
  6076. PRAGMA TablePathPrefix = '/database/path/to/tables';
  6077. ALTER TOPIC `my_table/my_feed` ADD CONSUMER `my_consumer`;
  6078. )");
  6079. UNIT_ASSERT(res.Root);
  6080. TWordCountHive elementStat = {{TString("/database/path/to/tables/my_table/my_feed"), 0}, {"topic", 0}};
  6081. VerifyProgram(res, elementStat);
  6082. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["topic"]);
  6083. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["/database/path/to/tables/my_table/my_feed"]);
  6084. }
  6085. }
  6086. Y_UNIT_TEST_SUITE(BlockEnginePragma) {
  6087. Y_UNIT_TEST(Basic) {
  6088. const TVector<TString> values = {"auto", "force", "disable"};
  6089. for (const auto& value : values) {
  6090. const auto query = TStringBuilder() << "pragma Blockengine='" << value << "'; select 1;";
  6091. NYql::TAstParseResult res = SqlToYql(query);
  6092. UNIT_ASSERT(res.Root);
  6093. TVerifyLineFunc verifyLine = [&](const TString& word, const TString& line) {
  6094. Y_UNUSED(word);
  6095. UNIT_ASSERT_STRING_CONTAINS(line, TStringBuilder() << R"(Configure! world (DataSource '"config") '"BlockEngine" '")" << value << "\"");
  6096. };
  6097. TWordCountHive elementStat({"BlockEngine"});
  6098. VerifyProgram(res, elementStat, verifyLine);
  6099. UNIT_ASSERT(elementStat["BlockEngine"] == ((value == "disable") ? 0 : 1));
  6100. }
  6101. }
  6102. Y_UNIT_TEST(UnknownSetting) {
  6103. ExpectFailWithError("use plato; pragma BlockEngine='foo';",
  6104. "<main>:1:31: Error: Expected `disable|auto|force' argument for: BlockEngine\n");
  6105. }
  6106. }
  6107. Y_UNIT_TEST_SUITE(TViewSyntaxTest) {
  6108. Y_UNIT_TEST(CreateViewSimple) {
  6109. NYql::TAstParseResult res = SqlToYql(R"(
  6110. USE plato;
  6111. CREATE VIEW TheView WITH (security_invoker = TRUE) AS SELECT 1;
  6112. )"
  6113. );
  6114. UNIT_ASSERT_C(res.Root, res.Issues.ToString());
  6115. }
  6116. Y_UNIT_TEST(CreateViewIfNotExists) {
  6117. constexpr const char* name = "TheView";
  6118. NYql::TAstParseResult res = SqlToYql(std::format(R"(
  6119. USE plato;
  6120. CREATE VIEW IF NOT EXISTS {} WITH (security_invoker = TRUE) AS SELECT 1;
  6121. )", name
  6122. ));
  6123. UNIT_ASSERT_C(res.Root, res.Issues.ToString());
  6124. TVerifyLineFunc verifyLine = [&](const TString& word, const TString& line) {
  6125. if (word == "Write!") {
  6126. UNIT_ASSERT_STRING_CONTAINS(line, name);
  6127. UNIT_ASSERT_STRING_CONTAINS(line, "createObjectIfNotExists");
  6128. }
  6129. };
  6130. TWordCountHive elementStat = { {"Write!"} };
  6131. VerifyProgram(res, elementStat, verifyLine);
  6132. UNIT_ASSERT_VALUES_EQUAL(elementStat["Write!"], 1);
  6133. }
  6134. Y_UNIT_TEST(CreateViewFromTable) {
  6135. constexpr const char* path = "/PathPrefix/TheView";
  6136. constexpr const char* query = R"(
  6137. SELECT * FROM SomeTable
  6138. )";
  6139. NYql::TAstParseResult res = SqlToYql(std::format(R"(
  6140. USE plato;
  6141. CREATE VIEW `{}` WITH (security_invoker = TRUE) AS {};
  6142. )",
  6143. path,
  6144. query
  6145. )
  6146. );
  6147. UNIT_ASSERT_C(res.Root, res.Issues.ToString());
  6148. TVerifyLineFunc verifyLine = [&](const TString& word, const TString& line) {
  6149. if (word == "Write!") {
  6150. UNIT_ASSERT_STRING_CONTAINS(line, path);
  6151. UNIT_ASSERT_STRING_CONTAINS(line, "createObject");
  6152. }
  6153. };
  6154. TWordCountHive elementStat = { {"Write!"} };
  6155. VerifyProgram(res, elementStat, verifyLine);
  6156. UNIT_ASSERT_VALUES_EQUAL(elementStat["Write!"], 1);
  6157. }
  6158. Y_UNIT_TEST(CheckReconstructedQuery) {
  6159. constexpr const char* path = "/PathPrefix/TheView";
  6160. constexpr const char* query = R"(
  6161. SELECT * FROM FirstTable JOIN SecondTable ON FirstTable.key == SecondTable.key
  6162. )";
  6163. NYql::TAstParseResult res = SqlToYql(std::format(R"(
  6164. USE plato;
  6165. CREATE VIEW `{}` WITH (security_invoker = TRUE) AS {};
  6166. )",
  6167. path,
  6168. query
  6169. )
  6170. );
  6171. UNIT_ASSERT_C(res.Root, res.Issues.ToString());
  6172. TString reconstructedQuery = ToString(Tokenize(query));
  6173. TVerifyLineFunc verifyLine = [&](const TString& word, const TString& line) {
  6174. if (word == "query_text") {
  6175. UNIT_ASSERT_STRING_CONTAINS(line, reconstructedQuery);
  6176. }
  6177. };
  6178. TWordCountHive elementStat = { {"Write!"} };
  6179. VerifyProgram(res, elementStat, verifyLine);
  6180. UNIT_ASSERT_VALUES_EQUAL(elementStat["Write!"], 1);
  6181. }
  6182. Y_UNIT_TEST(DropView) {
  6183. constexpr const char* path = "/PathPrefix/TheView";
  6184. NYql::TAstParseResult res = SqlToYql(std::format(R"(
  6185. USE plato;
  6186. DROP VIEW `{}`;
  6187. )",
  6188. path
  6189. )
  6190. );
  6191. UNIT_ASSERT_C(res.Root, res.Issues.ToString());
  6192. TVerifyLineFunc verifyLine = [&](const TString& word, const TString& line) {
  6193. if (word == "Write!") {
  6194. UNIT_ASSERT_STRING_CONTAINS(line, path);
  6195. UNIT_ASSERT_STRING_CONTAINS(line, "dropObject");
  6196. }
  6197. };
  6198. TWordCountHive elementStat = { {"Write!"} };
  6199. VerifyProgram(res, elementStat, verifyLine);
  6200. UNIT_ASSERT_VALUES_EQUAL(elementStat["Write!"], 1);
  6201. }
  6202. Y_UNIT_TEST(DropViewIfExists) {
  6203. constexpr const char* name = "TheView";
  6204. NYql::TAstParseResult res = SqlToYql(std::format(R"(
  6205. USE plato;
  6206. DROP VIEW IF EXISTS {};
  6207. )", name
  6208. ));
  6209. UNIT_ASSERT_C(res.Root, res.Issues.ToString());
  6210. TVerifyLineFunc verifyLine = [&](const TString& word, const TString& line) {
  6211. if (word == "Write!") {
  6212. UNIT_ASSERT_STRING_CONTAINS(line, name);
  6213. UNIT_ASSERT_STRING_CONTAINS(line, "dropObjectIfExists");
  6214. }
  6215. };
  6216. TWordCountHive elementStat = { {"Write!"} };
  6217. VerifyProgram(res, elementStat, verifyLine);
  6218. UNIT_ASSERT_VALUES_EQUAL(elementStat["Write!"], 1);
  6219. }
  6220. Y_UNIT_TEST(CreateViewWithTablePrefix) {
  6221. NYql::TAstParseResult res = SqlToYql(R"(
  6222. USE plato;
  6223. PRAGMA TablePathPrefix='/PathPrefix';
  6224. CREATE VIEW TheView WITH (security_invoker = TRUE) AS SELECT 1;
  6225. )"
  6226. );
  6227. UNIT_ASSERT_C(res.Root, res.Issues.ToString());
  6228. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  6229. if (word == "Write!") {
  6230. UNIT_ASSERT_STRING_CONTAINS(line, "/PathPrefix/TheView");
  6231. UNIT_ASSERT_STRING_CONTAINS(line, "createObject");
  6232. }
  6233. };
  6234. TWordCountHive elementStat = { {"Write!"} };
  6235. VerifyProgram(res, elementStat, verifyLine);
  6236. UNIT_ASSERT_VALUES_EQUAL(elementStat["Write!"], 1);
  6237. }
  6238. Y_UNIT_TEST(DropViewWithTablePrefix) {
  6239. NYql::TAstParseResult res = SqlToYql(R"(
  6240. USE plato;
  6241. PRAGMA TablePathPrefix='/PathPrefix';
  6242. DROP VIEW TheView;
  6243. )"
  6244. );
  6245. UNIT_ASSERT_C(res.Root, res.Issues.ToString());
  6246. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  6247. if (word == "Write") {
  6248. UNIT_ASSERT_STRING_CONTAINS(line, "/PathPrefix/TheView");
  6249. UNIT_ASSERT_STRING_CONTAINS(line, "dropObject");
  6250. }
  6251. };
  6252. TWordCountHive elementStat = { {"Write!"} };
  6253. VerifyProgram(res, elementStat, verifyLine);
  6254. UNIT_ASSERT_VALUES_EQUAL(elementStat["Write!"], 1);
  6255. }
  6256. Y_UNIT_TEST(YtAlternativeSchemaSyntax) {
  6257. NYql::TAstParseResult res = SqlToYql(R"(
  6258. SELECT * FROM plato.Input WITH schema(y Int32, x String not null);
  6259. )");
  6260. UNIT_ASSERT_C(res.Root, res.Issues.ToString());
  6261. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  6262. if (word == "userschema") {
  6263. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos,
  6264. line.find(R"__('('('"userschema" (StructType '('"y" (AsOptionalType (DataType 'Int32))) '('"x" (DataType 'String))))))__"));
  6265. }
  6266. };
  6267. TWordCountHive elementStat = {{TString("userschema"), 0}};
  6268. VerifyProgram(res, elementStat, verifyLine);
  6269. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["userschema"]);
  6270. }
  6271. Y_UNIT_TEST(UseViewAndFullColumnId) {
  6272. NYql::TAstParseResult res = SqlToYql("USE plato; SELECT Input.x FROM Input VIEW uitzicht;");
  6273. UNIT_ASSERT(res.Root);
  6274. TWordCountHive elementStat = {{TString("SqlAccess"), 0}, {"SqlProjectItem", 0}, {"Read!", 0}};
  6275. VerifyProgram(res, elementStat);
  6276. UNIT_ASSERT_VALUES_EQUAL(0, elementStat["SqlAccess"]);
  6277. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["SqlProjectItem"]);
  6278. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Read!"]);
  6279. }
  6280. }
  6281. Y_UNIT_TEST_SUITE(CompactNamedExprs) {
  6282. Y_UNIT_TEST(SourceCallablesInWrongContext) {
  6283. TString query = R"(
  6284. pragma CompactNamedExprs;
  6285. $foo = %s();
  6286. select $foo from plato.Input;
  6287. )";
  6288. THashMap<TString, TString> errs = {
  6289. {"TableRow", "<main>:3:20: Error: TableRow requires data source\n"},
  6290. {"JoinTableRow", "<main>:3:20: Error: JoinTableRow requires data source\n"},
  6291. {"TableRecordIndex", "<main>:3:20: Error: Unable to use function: TableRecord without source\n"},
  6292. {"TablePath", "<main>:3:20: Error: Unable to use function: TablePath without source\n"},
  6293. {"SystemMetadata", "<main>:3:20: Error: Unable to use function: SystemMetadata without source\n"},
  6294. };
  6295. for (TString callable : { "TableRow", "JoinTableRow", "TableRecordIndex", "TablePath", "SystemMetadata"}) {
  6296. auto req = Sprintf(query.c_str(), callable.c_str());
  6297. ExpectFailWithError(req, errs[callable]);
  6298. }
  6299. }
  6300. Y_UNIT_TEST(ValidateUnusedExprs) {
  6301. TString query = R"(
  6302. pragma warning("disable", "4527");
  6303. pragma CompactNamedExprs;
  6304. pragma ValidateUnusedExprs;
  6305. $foo = count(1);
  6306. select 1;
  6307. )";
  6308. ExpectFailWithError(query, "<main>:6:20: Error: Aggregation is not allowed in this context\n");
  6309. query = R"(
  6310. pragma warning("disable", "4527");
  6311. pragma CompactNamedExprs;
  6312. pragma ValidateUnusedExprs;
  6313. define subquery $x() as
  6314. select count(1, 2);
  6315. end define;
  6316. select 1;
  6317. )";
  6318. ExpectFailWithError(query, "<main>:7:24: Error: Aggregation function Count requires exactly 1 argument(s), given: 2\n");
  6319. }
  6320. Y_UNIT_TEST(DisableValidateUnusedExprs) {
  6321. TString query = R"(
  6322. pragma warning("disable", "4527");
  6323. pragma CompactNamedExprs;
  6324. pragma DisableValidateUnusedExprs;
  6325. $foo = count(1);
  6326. select 1;
  6327. )";
  6328. SqlToYql(query).IsOk();
  6329. query = R"(
  6330. pragma warning("disable", "4527");
  6331. pragma CompactNamedExprs;
  6332. pragma DisableValidateUnusedExprs;
  6333. define subquery $x() as
  6334. select count(1, 2);
  6335. end define;
  6336. select 1;
  6337. )";
  6338. SqlToYql(query).IsOk();
  6339. }
  6340. }
  6341. Y_UNIT_TEST_SUITE(ResourcePool) {
  6342. Y_UNIT_TEST(CreateResourcePool) {
  6343. NYql::TAstParseResult res = SqlToYql(R"sql(
  6344. USE plato;
  6345. CREATE RESOURCE POOL MyResourcePool WITH (
  6346. CONCURRENT_QUERY_LIMIT=20,
  6347. QUERY_CANCEL_AFTER_SECONDS=86400,
  6348. QUEUE_TYPE="FIFO"
  6349. );
  6350. )sql");
  6351. UNIT_ASSERT_C(res.Root, res.Issues.ToString());
  6352. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  6353. if (word == "Write") {
  6354. UNIT_ASSERT_STRING_CONTAINS(line, R"#('('('"concurrent_query_limit" (Int32 '"20")) '('"query_cancel_after_seconds" (Int32 '"86400")) '('"queue_type" '"FIFO"))#");
  6355. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("createObject"));
  6356. }
  6357. };
  6358. TWordCountHive elementStat = { {TString("Write"), 0} };
  6359. VerifyProgram(res, elementStat, verifyLine);
  6360. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  6361. }
  6362. Y_UNIT_TEST(CreateResourcePoolWithBadArguments) {
  6363. ExpectFailWithError(R"sql(
  6364. USE plato;
  6365. CREATE RESOURCE POOL MyResourcePool;
  6366. )sql" , "<main>:3:51: Error: Unexpected token ';' : syntax error...\n\n");
  6367. ExpectFailWithError(R"sql(
  6368. USE plato;
  6369. CREATE RESOURCE POOL MyResourcePool WITH (
  6370. DUPLICATE_SETTING="first_value",
  6371. DUPLICATE_SETTING="second_value"
  6372. );
  6373. )sql" , "<main>:5:21: Error: DUPLICATE_SETTING duplicate keys\n");
  6374. }
  6375. Y_UNIT_TEST(AlterResourcePool) {
  6376. NYql::TAstParseResult res = SqlToYql(R"sql(
  6377. USE plato;
  6378. ALTER RESOURCE POOL MyResourcePool
  6379. SET (CONCURRENT_QUERY_LIMIT = 30, Weight = 5, QUEUE_TYPE = "UNORDERED"),
  6380. RESET (Query_Cancel_After_Seconds, Query_Count_Limit);
  6381. )sql");
  6382. UNIT_ASSERT_C(res.Root, res.Issues.ToString());
  6383. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  6384. if (word == "Write") {
  6385. UNIT_ASSERT_STRING_CONTAINS(line, R"#(('mode 'alterObject))#");
  6386. UNIT_ASSERT_STRING_CONTAINS(line, R"#('('features '('('"concurrent_query_limit" (Int32 '"30")) '('"queue_type" '"UNORDERED") '('"weight" (Int32 '"5")))))#");
  6387. UNIT_ASSERT_STRING_CONTAINS(line, R"#('('resetFeatures '('"query_cancel_after_seconds" '"query_count_limit")))#");
  6388. }
  6389. };
  6390. TWordCountHive elementStat = { {TString("Write"), 0} };
  6391. VerifyProgram(res, elementStat, verifyLine);
  6392. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  6393. }
  6394. Y_UNIT_TEST(DropResourcePool) {
  6395. NYql::TAstParseResult res = SqlToYql(R"sql(
  6396. USE plato;
  6397. DROP RESOURCE POOL MyResourcePool;
  6398. )sql");
  6399. UNIT_ASSERT(res.Root);
  6400. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  6401. if (word == "Write") {
  6402. UNIT_ASSERT_VALUES_EQUAL(TString::npos, line.find("'features"));
  6403. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("dropObject"));
  6404. }
  6405. };
  6406. TWordCountHive elementStat = { {TString("Write"), 0}};
  6407. VerifyProgram(res, elementStat, verifyLine);
  6408. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  6409. }
  6410. }
  6411. Y_UNIT_TEST_SUITE(BackupCollection) {
  6412. Y_UNIT_TEST(CreateBackupCollection) {
  6413. NYql::TAstParseResult res = SqlToYql(R"sql(
  6414. USE plato;
  6415. CREATE BACKUP COLLECTION TestCollection WITH (
  6416. STORAGE="local",
  6417. TAG="test" -- for testing purposes, not a real thing
  6418. );
  6419. )sql");
  6420. UNIT_ASSERT_C(res.Root, res.Issues.ToString());
  6421. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  6422. if (word == "Write") {
  6423. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find(R"#('"TestCollection")#"));
  6424. UNIT_ASSERT_STRING_CONTAINS(line, R"#(('"storage" (String '"local")))#");
  6425. UNIT_ASSERT_STRING_CONTAINS(line, R"#(('"tag" (String '"test"))))#");
  6426. UNIT_ASSERT_STRING_CONTAINS(line, R"#(('entries '()))#");
  6427. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("'create"));
  6428. }
  6429. };
  6430. TWordCountHive elementStat = { {TString("Write"), 0} };
  6431. VerifyProgram(res, elementStat, verifyLine);
  6432. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  6433. }
  6434. Y_UNIT_TEST(CreateBackupCollectionWithDatabase) {
  6435. NYql::TAstParseResult res = SqlToYql(R"sql(
  6436. USE plato;
  6437. CREATE BACKUP COLLECTION TestCollection DATABASE WITH (
  6438. STORAGE="local",
  6439. TAG="test" -- for testing purposes, not a real thing
  6440. );
  6441. )sql");
  6442. UNIT_ASSERT_C(res.Root, res.Issues.ToString());
  6443. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  6444. if (word == "Write") {
  6445. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find(R"#('"TestCollection")#"));
  6446. UNIT_ASSERT_STRING_CONTAINS(line, R"#(('"storage" (String '"local")))#");
  6447. UNIT_ASSERT_STRING_CONTAINS(line, R"#(('"tag" (String '"test"))))#");
  6448. UNIT_ASSERT_STRING_CONTAINS(line, R"#(('entries '('('('type 'database)))))#");
  6449. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("'create"));
  6450. }
  6451. };
  6452. TWordCountHive elementStat = { {TString("Write"), 0} };
  6453. VerifyProgram(res, elementStat, verifyLine);
  6454. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  6455. }
  6456. Y_UNIT_TEST(CreateBackupCollectionWithTables) {
  6457. NYql::TAstParseResult res = SqlToYql(R"sql(
  6458. USE plato;
  6459. CREATE BACKUP COLLECTION TestCollection (
  6460. TABLE someTable,
  6461. TABLE `prefix/anotherTable`
  6462. ) WITH (
  6463. STORAGE="local",
  6464. TAG="test" -- for testing purposes, not a real thing
  6465. );
  6466. )sql");
  6467. UNIT_ASSERT_C(res.Root, res.Issues.ToString());
  6468. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  6469. if (word == "Write") {
  6470. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find(R"#('"TestCollection")#"));
  6471. UNIT_ASSERT_STRING_CONTAINS(line, R"#(('"storage" (String '"local")))#");
  6472. UNIT_ASSERT_STRING_CONTAINS(line, R"#(('"tag" (String '"test"))))#");
  6473. UNIT_ASSERT_STRING_CONTAINS(line, R"#(('entries '('('('type 'table) '('path '"someTable")) '('('type 'table) '('path '"prefix/anotherTable")))))#");
  6474. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("'create"));
  6475. }
  6476. };
  6477. TWordCountHive elementStat = { {TString("Write"), 0} };
  6478. VerifyProgram(res, elementStat, verifyLine);
  6479. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  6480. }
  6481. Y_UNIT_TEST(CreateBackupCollectionWithBadArguments) {
  6482. ExpectFailWithError(R"sql(
  6483. USE plato;
  6484. CREATE BACKUP COLLECTION TestCollection;
  6485. )sql" , "<main>:3:55: Error: Unexpected token ';' : syntax error...\n\n");
  6486. ExpectFailWithError(R"sql(
  6487. USE plato;
  6488. CREATE BACKUP COLLECTION TABLE TestCollection;
  6489. )sql" , "<main>:3:47: Error: Unexpected token 'TestCollection' : syntax error...\n\n");
  6490. ExpectFailWithError(R"sql(
  6491. USE plato;
  6492. CREATE BACKUP COLLECTION DATABASE `test` TestCollection;
  6493. )sql" , "<main>:3:50: Error: Unexpected token '`test`' : syntax error...\n\n");
  6494. ExpectFailWithError(R"sql(
  6495. USE plato;
  6496. CREATE BACKUP COLLECTION TestCollection WITH (
  6497. DUPLICATE_SETTING="first_value",
  6498. DUPLICATE_SETTING="second_value"
  6499. );
  6500. )sql" , "<main>:5:21: Error: DUPLICATE_SETTING duplicate keys\n");
  6501. ExpectFailWithError(R"sql(
  6502. USE plato;
  6503. CREATE BACKUP COLLECTION TestCollection WITH (
  6504. INT_SETTING=1
  6505. );
  6506. )sql" , "<main>:4:21: Error: INT_SETTING value should be a string literal\n");
  6507. }
  6508. Y_UNIT_TEST(AlterBackupCollection) {
  6509. NYql::TAstParseResult res = SqlToYql(R"sql(
  6510. USE plato;
  6511. ALTER BACKUP COLLECTION TestCollection
  6512. SET (STORAGE="remote"), -- also just for test
  6513. SET (TAG1 = "123"),
  6514. RESET (TAG2, TAG3);
  6515. )sql");
  6516. UNIT_ASSERT_C(res.Root, res.Issues.ToString());
  6517. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  6518. if (word == "Write") {
  6519. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find(R"#('"TestCollection")#"));
  6520. UNIT_ASSERT_STRING_CONTAINS(line, R"#(('mode 'alter))#");
  6521. UNIT_ASSERT_STRING_CONTAINS(line, R"#(('"storage" (String '"remote")))#");
  6522. UNIT_ASSERT_STRING_CONTAINS(line, R"#(('"tag1" (String '"123"))))#");
  6523. UNIT_ASSERT_STRING_CONTAINS(line, R"#('('resetSettings '('"tag2" '"tag3")))#");
  6524. }
  6525. };
  6526. TWordCountHive elementStat = { {TString("Write"), 0} };
  6527. VerifyProgram(res, elementStat, verifyLine);
  6528. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  6529. }
  6530. Y_UNIT_TEST(AlterBackupCollectionEntries) {
  6531. NYql::TAstParseResult res = SqlToYql(R"sql(
  6532. USE plato;
  6533. ALTER BACKUP COLLECTION TestCollection
  6534. DROP TABLE `test`,
  6535. ADD DATABASE;
  6536. )sql");
  6537. UNIT_ASSERT_C(res.Root, res.Issues.ToString());
  6538. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  6539. if (word == "Write") {
  6540. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find(R"#('"TestCollection")#"));
  6541. UNIT_ASSERT_STRING_CONTAINS(line, R"#(('mode 'alter))#");
  6542. UNIT_ASSERT_STRING_CONTAINS(line, R"#('alterEntries)#");
  6543. UNIT_ASSERT_STRING_CONTAINS(line, R"#('('('type 'table) '('path '"test") '('action 'drop)))#");
  6544. UNIT_ASSERT_STRING_CONTAINS(line, R"#('('('type 'database) '('action 'add)))#");
  6545. }
  6546. };
  6547. TWordCountHive elementStat = { {TString("Write"), 0} };
  6548. VerifyProgram(res, elementStat, verifyLine);
  6549. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  6550. }
  6551. Y_UNIT_TEST(DropBackupCollection) {
  6552. NYql::TAstParseResult res = SqlToYql(R"sql(
  6553. USE plato;
  6554. DROP BACKUP COLLECTION TestCollection;
  6555. )sql");
  6556. UNIT_ASSERT(res.Root);
  6557. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  6558. if (word == "Write") {
  6559. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find(R"#('"TestCollection")#"));
  6560. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("drop"));
  6561. }
  6562. };
  6563. TWordCountHive elementStat = { {TString("Write"), 0}};
  6564. VerifyProgram(res, elementStat, verifyLine);
  6565. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  6566. }
  6567. }
  6568. Y_UNIT_TEST_SUITE(ResourcePoolClassifier) {
  6569. Y_UNIT_TEST(CreateResourcePoolClassifier) {
  6570. NYql::TAstParseResult res = SqlToYql(R"sql(
  6571. USE plato;
  6572. CREATE RESOURCE POOL CLASSIFIER MyResourcePoolClassifier WITH (
  6573. RANK=20,
  6574. RESOURCE_POOL='wgUserQueries',
  6575. MEMBER_NAME='yandex_query@abc'
  6576. );
  6577. )sql");
  6578. UNIT_ASSERT_C(res.Root, res.Issues.ToString());
  6579. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  6580. if (word == "Write") {
  6581. UNIT_ASSERT_STRING_CONTAINS(line, R"#('('('"member_name" '"yandex_query@abc") '('"rank" (Int32 '"20")) '('"resource_pool" '"wgUserQueries"))#");
  6582. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("createObject"));
  6583. }
  6584. };
  6585. TWordCountHive elementStat = { {TString("Write"), 0} };
  6586. VerifyProgram(res, elementStat, verifyLine);
  6587. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  6588. }
  6589. Y_UNIT_TEST(CreateResourcePoolClassifierWithBadArguments) {
  6590. ExpectFailWithError(R"sql(
  6591. USE plato;
  6592. CREATE RESOURCE POOL CLASSIFIER MyResourcePoolClassifier;
  6593. )sql" , "<main>:3:72: Error: Unexpected token ';' : syntax error...\n\n");
  6594. ExpectFailWithError(R"sql(
  6595. USE plato;
  6596. CREATE RESOURCE POOL CLASSIFIER MyResourcePoolClassifier WITH (
  6597. DUPLICATE_SETTING="first_value",
  6598. DUPLICATE_SETTING="second_value"
  6599. );
  6600. )sql" , "<main>:5:21: Error: DUPLICATE_SETTING duplicate keys\n");
  6601. }
  6602. Y_UNIT_TEST(AlterResourcePoolClassifier) {
  6603. NYql::TAstParseResult res = SqlToYql(R"sql(
  6604. USE plato;
  6605. ALTER RESOURCE POOL CLASSIFIER MyResourcePoolClassifier
  6606. SET (RANK = 30, Weight = 5, MEMBER_NAME = "test@user"),
  6607. RESET (Resource_Pool);
  6608. )sql");
  6609. UNIT_ASSERT_C(res.Root, res.Issues.ToString());
  6610. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  6611. if (word == "Write") {
  6612. UNIT_ASSERT_STRING_CONTAINS(line, R"#(('mode 'alterObject))#");
  6613. UNIT_ASSERT_STRING_CONTAINS(line, R"#('('features '('('"member_name" '"test@user") '('"rank" (Int32 '"30")) '('"weight" (Int32 '"5")))))#");
  6614. UNIT_ASSERT_STRING_CONTAINS(line, R"#('('resetFeatures '('"resource_pool")))#");
  6615. }
  6616. };
  6617. TWordCountHive elementStat = { {TString("Write"), 0} };
  6618. VerifyProgram(res, elementStat, verifyLine);
  6619. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  6620. }
  6621. Y_UNIT_TEST(DropResourcePoolClassifier) {
  6622. NYql::TAstParseResult res = SqlToYql(R"sql(
  6623. USE plato;
  6624. DROP RESOURCE POOL CLASSIFIER MyResourcePoolClassifier;
  6625. )sql");
  6626. UNIT_ASSERT(res.Root);
  6627. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  6628. if (word == "Write") {
  6629. UNIT_ASSERT_VALUES_EQUAL(TString::npos, line.find("'features"));
  6630. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("dropObject"));
  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(BacktickMatching) {
  6638. auto req = "select\n"
  6639. " 1 as `Schema has \\`RealCost\\``\n"
  6640. " -- foo`bar";
  6641. auto res = SqlToYql(req);
  6642. UNIT_ASSERT(res.Root);
  6643. UNIT_ASSERT(res.IsOk());
  6644. UNIT_ASSERT(res.Issues.Size() == 0);
  6645. res = SqlToYqlWithAnsiLexer(req);
  6646. UNIT_ASSERT(res.Root);
  6647. UNIT_ASSERT(res.IsOk());
  6648. UNIT_ASSERT(res.Issues.Size() == 0);
  6649. req = "select 1 as `a``b`, 2 as ````, 3 as `\\x60a\\x60`, 4 as ```b```, 5 as `\\`c\\``";
  6650. res = SqlToYql(req);
  6651. UNIT_ASSERT(res.Root);
  6652. UNIT_ASSERT(res.IsOk());
  6653. UNIT_ASSERT(res.Issues.Size() == 0);
  6654. res = SqlToYqlWithAnsiLexer(req);
  6655. UNIT_ASSERT(res.Root);
  6656. UNIT_ASSERT(res.IsOk());
  6657. UNIT_ASSERT(res.Issues.Size() == 0);
  6658. }
  6659. }
  6660. Y_UNIT_TEST_SUITE(OlapPartitionCount) {
  6661. Y_UNIT_TEST(CorrectUsage) {
  6662. NYql::TAstParseResult res = SqlToYql(R"sql(
  6663. USE plato;
  6664. CREATE TABLE `mytable` (id Uint32, PRIMARY KEY (id))
  6665. PARTITION BY HASH(id)
  6666. WITH (STORE = COLUMN, PARTITION_COUNT = 8);
  6667. )sql");
  6668. UNIT_ASSERT_C(res.IsOk(), res.Issues.ToString());
  6669. }
  6670. Y_UNIT_TEST(UseWithoutColumnStore) {
  6671. NYql::TAstParseResult res = SqlToYql(R"sql(
  6672. USE plato;
  6673. CREATE TABLE `mytable` (id Uint32, PRIMARY KEY (id))
  6674. WITH (PARTITION_COUNT = 8);
  6675. )sql");
  6676. UNIT_ASSERT(!res.IsOk());
  6677. UNIT_ASSERT(res.Issues.Size() == 1);
  6678. UNIT_ASSERT_STRING_CONTAINS(res.Issues.ToString(), "PARTITION_COUNT can be used only with STORE=COLUMN");
  6679. }
  6680. }
  6681. Y_UNIT_TEST_SUITE(Backup) {
  6682. Y_UNIT_TEST(Simple) {
  6683. NYql::TAstParseResult res = SqlToYql(R"sql(
  6684. USE plato;
  6685. BACKUP TestCollection;
  6686. )sql");
  6687. UNIT_ASSERT_C(res.Root, res.Issues.ToString());
  6688. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  6689. if (word == "Write") {
  6690. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find(R"#('"TestCollection")#"));
  6691. UNIT_ASSERT_VALUES_EQUAL(TString::npos, line.find("'Incremental"));
  6692. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("'backup"));
  6693. }
  6694. };
  6695. TWordCountHive elementStat = { {TString("Write"), 0} };
  6696. VerifyProgram(res, elementStat, verifyLine);
  6697. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  6698. }
  6699. Y_UNIT_TEST(Incremental) {
  6700. NYql::TAstParseResult res = SqlToYql(R"sql(
  6701. USE plato;
  6702. BACKUP TestCollection INCREMENTAL;
  6703. )sql");
  6704. UNIT_ASSERT_C(res.Root, res.Issues.ToString());
  6705. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  6706. if (word == "Write") {
  6707. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find(R"#('"TestCollection")#"));
  6708. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("'backupIncremental"));
  6709. }
  6710. };
  6711. TWordCountHive elementStat = { {TString("Write"), 0} };
  6712. VerifyProgram(res, elementStat, verifyLine);
  6713. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  6714. }
  6715. }
  6716. Y_UNIT_TEST_SUITE(Restore) {
  6717. Y_UNIT_TEST(Simple) {
  6718. NYql::TAstParseResult res = SqlToYql(R"sql(
  6719. USE plato;
  6720. RESTORE TestCollection;
  6721. )sql");
  6722. UNIT_ASSERT_C(res.Root, res.Issues.ToString());
  6723. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  6724. if (word == "Write") {
  6725. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find(R"#('"TestCollection")#"));
  6726. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("'restore"));
  6727. }
  6728. };
  6729. TWordCountHive elementStat = { {TString("Write"), 0} };
  6730. VerifyProgram(res, elementStat, verifyLine);
  6731. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  6732. }
  6733. Y_UNIT_TEST(AtPoint) {
  6734. NYql::TAstParseResult res = SqlToYql(R"sql(
  6735. USE plato;
  6736. RESTORE TestCollection AT '2024-06-16_20-14-02';
  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_STRING_CONTAINS(line, R"#('at '"2024-06-16_20-14-02")#");
  6743. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("'restore"));
  6744. }
  6745. };
  6746. TWordCountHive elementStat = { {TString("Write"), 0} };
  6747. VerifyProgram(res, elementStat, verifyLine);
  6748. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  6749. }
  6750. }
  6751. Y_UNIT_TEST_SUITE(ColumnFamily) {
  6752. Y_UNIT_TEST(CompressionLevelCorrectUsage) {
  6753. NYql::TAstParseResult res = SqlToYql(R"( use plato;
  6754. CREATE TABLE tableName (
  6755. Key Uint32 FAMILY default,
  6756. Value String FAMILY family1,
  6757. PRIMARY KEY (Key),
  6758. FAMILY default (
  6759. DATA = "test",
  6760. COMPRESSION = "lz4",
  6761. COMPRESSION_LEVEL = 5
  6762. ),
  6763. FAMILY family1 (
  6764. DATA = "test",
  6765. COMPRESSION = "lz4",
  6766. COMPRESSION_LEVEL = 3
  6767. )
  6768. );
  6769. )");
  6770. UNIT_ASSERT(res.IsOk());
  6771. UNIT_ASSERT(res.Issues.Size() == 0);
  6772. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  6773. if (word == "Write") {
  6774. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("compression_level"));
  6775. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("5"));
  6776. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("3"));
  6777. }
  6778. };
  6779. TWordCountHive elementStat = { { TString("Write"), 0 }, { TString("compression_level"), 0 } };
  6780. VerifyProgram(res, elementStat, verifyLine);
  6781. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  6782. UNIT_ASSERT_VALUES_EQUAL(2, elementStat["compression_level"]);
  6783. }
  6784. Y_UNIT_TEST(FieldDataIsNotString) {
  6785. NYql::TAstParseResult res = SqlToYql(R"( use plato;
  6786. CREATE TABLE tableName (
  6787. Key Uint32 FAMILY default,
  6788. PRIMARY KEY (Key),
  6789. FAMILY default (
  6790. DATA = 1,
  6791. COMPRESSION = "lz4",
  6792. COMPRESSION_LEVEL = 5
  6793. )
  6794. );
  6795. )");
  6796. UNIT_ASSERT(!res.IsOk());
  6797. UNIT_ASSERT(res.Issues.Size() == 1);
  6798. UNIT_ASSERT_STRING_CONTAINS(res.Issues.ToString(), "DATA value should be a string literal");
  6799. }
  6800. Y_UNIT_TEST(FieldCompressionIsNotString) {
  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 = "test",
  6807. COMPRESSION = 2,
  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(), "COMPRESSION value should be a string literal");
  6815. }
  6816. Y_UNIT_TEST(FieldCompressionLevelIsNotInteger) {
  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 = "lz4",
  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_LEVEL value should be an integer");
  6831. }
  6832. Y_UNIT_TEST(AlterCompressionCorrectUsage) {
  6833. NYql::TAstParseResult res = SqlToYql(R"( use plato;
  6834. ALTER TABLE tableName ALTER FAMILY default SET COMPRESSION "lz4";
  6835. )");
  6836. UNIT_ASSERT(res.IsOk());
  6837. UNIT_ASSERT(res.Issues.Size() == 0);
  6838. }
  6839. Y_UNIT_TEST(AlterCompressionFieldIsNotString) {
  6840. NYql::TAstParseResult res = SqlToYql(R"( use plato;
  6841. ALTER TABLE tableName ALTER FAMILY default SET COMPRESSION lz4;
  6842. )");
  6843. UNIT_ASSERT(!res.IsOk());
  6844. UNIT_ASSERT(res.Issues.Size() == 1);
  6845. UNIT_ASSERT_STRING_CONTAINS(res.Issues.ToString(), "Unexpected token 'lz4' : cannot match to any predicted input");
  6846. }
  6847. Y_UNIT_TEST(AlterCompressionLevelCorrectUsage) {
  6848. NYql::TAstParseResult res = SqlToYql(R"( use plato;
  6849. ALTER TABLE tableName ALTER FAMILY default SET COMPRESSION_LEVEL 5;
  6850. )");
  6851. UNIT_ASSERT(res.IsOk());
  6852. UNIT_ASSERT(res.Issues.Size() == 0);
  6853. }
  6854. Y_UNIT_TEST(AlterCompressionLevelFieldIsNotInteger) {
  6855. NYql::TAstParseResult res = SqlToYql(R"( use plato;
  6856. ALTER TABLE tableName ALTER FAMILY default SET COMPRESSION_LEVEL "5";
  6857. )");
  6858. UNIT_ASSERT(!res.IsOk());
  6859. UNIT_ASSERT(res.Issues.Size() == 1);
  6860. UNIT_ASSERT_STRING_CONTAINS(res.Issues.ToString(), "COMPRESSION_LEVEL value should be an integer");
  6861. }
  6862. }
  6863. Y_UNIT_TEST_SUITE(QuerySplit) {
  6864. Y_UNIT_TEST(Simple) {
  6865. TString query = R"(
  6866. ;
  6867. -- Comment 1
  6868. SELECT * From Input; -- Comment 2
  6869. -- Comment 3
  6870. $a = "a";
  6871. -- Comment 9
  6872. ;
  6873. -- Comment 10
  6874. -- Comment 8
  6875. $b = ($x) -> {
  6876. -- comment 4
  6877. return /* Comment 5 */ $x;
  6878. -- Comment 6
  6879. };
  6880. // Comment 7
  6881. )";
  6882. google::protobuf::Arena Arena;
  6883. NSQLTranslation::TTranslationSettings settings;
  6884. settings.AnsiLexer = false;
  6885. settings.Antlr4Parser = true;
  6886. settings.Arena = &Arena;
  6887. TVector<TString> statements;
  6888. NYql::TIssues issues;
  6889. UNIT_ASSERT(NSQLTranslationV1::SplitQueryToStatements(query, statements, issues, settings));
  6890. UNIT_ASSERT_VALUES_EQUAL(statements.size(), 3);
  6891. UNIT_ASSERT_VALUES_EQUAL(statements[0], "-- Comment 1\n SELECT * From Input; -- Comment 2\n");
  6892. UNIT_ASSERT_VALUES_EQUAL(statements[1], R"(-- Comment 3
  6893. $a = "a";)");
  6894. UNIT_ASSERT_VALUES_EQUAL(statements[2], R"(-- Comment 10
  6895. -- Comment 8
  6896. $b = ($x) -> {
  6897. -- comment 4
  6898. return /* Comment 5 */ $x;
  6899. -- Comment 6
  6900. };)");
  6901. }
  6902. }
  6903. Y_UNIT_TEST_SUITE(MatchRecognizeMeasuresAggregation) {
  6904. Y_UNIT_TEST(InsideSelect) {
  6905. ExpectFailWithError(R"sql(
  6906. SELECT FIRST(0), LAST(1);
  6907. )sql",
  6908. "<main>:2:20: Error: Cannot use FIRST and LAST outside the MATCH_RECOGNIZE context\n"
  6909. "<main>:2:30: Error: Cannot use FIRST and LAST outside the MATCH_RECOGNIZE context\n"
  6910. );
  6911. }
  6912. Y_UNIT_TEST(OutsideSelect) {
  6913. ExpectFailWithError(R"sql(
  6914. $lambda = ($x) -> (FIRST($x) + LAST($x));
  6915. SELECT $lambda(x) FROM plato.Input;
  6916. )sql",
  6917. "<main>:2:32: Error: Cannot use FIRST and LAST outside the MATCH_RECOGNIZE context\n"
  6918. "<main>:2:44: Error: Cannot use FIRST and LAST outside the MATCH_RECOGNIZE context\n"
  6919. );
  6920. }
  6921. Y_UNIT_TEST(AsAggregateFunction) {
  6922. ExpectFailWithError(R"sql(
  6923. SELECT FIRST(x), LAST(x) FROM plato.Input;
  6924. )sql",
  6925. "<main>:2:20: Error: Cannot use FIRST and LAST outside the MATCH_RECOGNIZE context\n"
  6926. "<main>:2:30: Error: Cannot use FIRST and LAST outside the MATCH_RECOGNIZE context\n"
  6927. );
  6928. }
  6929. Y_UNIT_TEST(AsWindowFunction) {
  6930. ExpectFailWithError(R"sql(
  6931. SELECT FIRST(x) OVER(), LAST(x) OVER() FROM plato.Input;
  6932. )sql",
  6933. "<main>:2:20: Error: Cannot use FIRST and LAST outside the MATCH_RECOGNIZE context\n"
  6934. "<main>:2:37: Error: Cannot use FIRST and LAST outside the MATCH_RECOGNIZE context\n"
  6935. );
  6936. }
  6937. }