12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447 |
- #include "sql_translation.h"
- #include "sql_expression.h"
- #include "sql_call_expr.h"
- #include "sql_query.h"
- #include "sql_values.h"
- #include "sql_select.h"
- #include "source.h"
- #include <yql/essentials/parser/proto_ast/gen/v1/SQLv1Lexer.h>
- #include <yql/essentials/parser/proto_ast/gen/v1_antlr4/SQLv1Antlr4Lexer.h>
- #include <yql/essentials/sql/settings/partitioning.h>
- #include <yql/essentials/sql/v1/proto_parser/proto_parser.h>
- #include <util/generic/scope.h>
- #include <util/string/join.h>
- #include <library/cpp/protobuf/util/simple_reflection.h>
- namespace {
- using namespace NSQLTranslationV1;
- template <typename Callback>
- void VisitAllFields(const NProtoBuf::Message& msg, Callback& callback) {
- const auto* descr = msg.GetDescriptor();
- for (int i = 0; i < descr->field_count(); ++i) {
- const auto* fd = descr->field(i);
- NProtoBuf::TConstField field(msg, fd);
- if (field.IsMessage()) {
- for (size_t j = 0; j < field.Size(); ++j) {
- const auto& message = *field.Get<NProtoBuf::Message>(j);
- callback(message);
- VisitAllFields(message, callback);
- }
- }
- }
- }
- struct TTokenCollector {
- void operator()(const NProtoBuf::Message& message) {
- if (const auto* token = dynamic_cast<const NSQLv1Generated::TToken*>(&message)) {
- if (!Tokens.empty()) {
- Tokens << ' ';
- }
- Tokens << token->GetValue();
- }
- }
- TStringBuilder Tokens;
- };
- TString CollectTokens(const TRule_select_stmt& selectStatement) {
- TTokenCollector tokenCollector;
- VisitAllFields(selectStatement, tokenCollector);
- return tokenCollector.Tokens;
- }
- bool RecreateContext(
- TContext& ctx, const NSQLTranslation::TTranslationSettings& settings, const TString& recreationQuery
- ) {
- if (!recreationQuery) {
- return true;
- }
- const TString queryName = "context recreation query";
- const auto* ast = NSQLTranslationV1::SqlAST(
- recreationQuery, queryName, ctx.Issues,
- settings.MaxErrors, settings.AnsiLexer, settings.Antlr4Parser, settings.TestAntlr4, settings.Arena
- );
- if (!ast) {
- return false;
- }
- TSqlQuery queryTranslator(ctx, ctx.Settings.Mode, true);
- auto node = queryTranslator.Build(static_cast<const TSQLv1ParserAST&>(*ast));
- return node && node->Init(ctx, nullptr) && node->Translate(ctx);
- }
- TNodePtr BuildViewSelect(
- const TRule_select_stmt& selectStatement,
- TContext& parentContext,
- const TString& contextRecreationQuery
- ) {
- TIssues issues;
- TContext context(parentContext.Settings, {}, issues, parentContext.Query);
- if (!RecreateContext(context, context.Settings, contextRecreationQuery)) {
- parentContext.Issues.AddIssues(issues);
- return nullptr;
- }
- issues.Clear();
- // Holds (among other things) subquery references.
- // These references need to be passed to the parent context
- // to be able to compile view queries with subqueries.
- context.PushCurrentBlocks(&parentContext.GetCurrentBlocks());
- context.Settings.Mode = NSQLTranslation::ESqlMode::LIMITED_VIEW;
- TSqlSelect selectTranslator(context, context.Settings.Mode);
- TPosition pos = parentContext.Pos();
- auto source = selectTranslator.Build(selectStatement, pos);
- if (!source) {
- parentContext.Issues.AddIssues(issues);
- return nullptr;
- }
- auto node = BuildSelectResult(
- pos,
- std::move(source),
- false,
- false,
- context.Scoped
- );
- if (!node) {
- parentContext.Issues.AddIssues(issues);
- return nullptr;
- }
- return node;
- }
- }
- namespace NSQLTranslationV1 {
- using NALPDefault::SQLv1LexerTokens;
- using NALPDefaultAntlr4::SQLv1Antlr4Lexer;
- using namespace NSQLv1Generated;
- TIdentifier GetKeywordId(TTranslation& ctx, const TRule_keyword& node) {
- // keyword:
- // keyword_compat
- // | keyword_expr_uncompat
- // | keyword_table_uncompat
- // | keyword_select_uncompat
- // | keyword_alter_uncompat
- // | keyword_in_uncompat
- // | keyword_window_uncompat
- // | keyword_hint_uncompat
- //;
- switch (node.Alt_case()) {
- case TRule_keyword::kAltKeyword1:
- return GetIdentifier(ctx, node.GetAlt_keyword1().GetRule_keyword_compat1());
- case TRule_keyword::kAltKeyword2:
- return GetIdentifier(ctx, node.GetAlt_keyword2().GetRule_keyword_expr_uncompat1());
- case TRule_keyword::kAltKeyword3:
- return GetIdentifier(ctx, node.GetAlt_keyword3().GetRule_keyword_table_uncompat1());
- case TRule_keyword::kAltKeyword4:
- return GetIdentifier(ctx, node.GetAlt_keyword4().GetRule_keyword_select_uncompat1());
- case TRule_keyword::kAltKeyword5:
- return GetIdentifier(ctx, node.GetAlt_keyword5().GetRule_keyword_alter_uncompat1());
- case TRule_keyword::kAltKeyword6:
- return GetIdentifier(ctx, node.GetAlt_keyword6().GetRule_keyword_in_uncompat1());
- case TRule_keyword::kAltKeyword7:
- return GetIdentifier(ctx, node.GetAlt_keyword7().GetRule_keyword_window_uncompat1());
- case TRule_keyword::kAltKeyword8:
- return GetIdentifier(ctx, node.GetAlt_keyword8().GetRule_keyword_hint_uncompat1());
- case TRule_keyword::ALT_NOT_SET:
- Y_ABORT("You should change implementation according to grammar changes");
- }
- }
- TString Id(const TRule_id& node, TTranslation& ctx) {
- // id: identifier | keyword;
- switch (node.Alt_case()) {
- case TRule_id::kAltId1:
- return Id(node.GetAlt_id1().GetRule_identifier1(), ctx);
- case TRule_id::kAltId2:
- return GetKeyword(ctx, node.GetAlt_id2().GetRule_keyword1());
- case TRule_id::ALT_NOT_SET:
- Y_ABORT("You should change implementation according to grammar changes");
- }
- }
- TString Id(const TRule_id_or_type& node, TTranslation& ctx) {
- switch (node.Alt_case()) {
- case TRule_id_or_type::kAltIdOrType1:
- return Id(node.GetAlt_id_or_type1().GetRule_id1(), ctx);
- case TRule_id_or_type::kAltIdOrType2:
- return ctx.Identifier(node.GetAlt_id_or_type2().GetRule_type_id1().GetToken1());
- case TRule_id_or_type::ALT_NOT_SET:
- Y_ABORT("You should change implementation according to grammar changes");
- }
- }
- TString Id(const TRule_id_as_compat& node, TTranslation& ctx) {
- switch (node.Alt_case()) {
- case TRule_id_as_compat::kAltIdAsCompat1:
- return Id(node.GetAlt_id_as_compat1().GetRule_identifier1(), ctx);
- case TRule_id_as_compat::kAltIdAsCompat2:
- return ctx.Token(node.GetAlt_id_as_compat2().GetRule_keyword_as_compat1().GetToken1());
- case TRule_id_as_compat::ALT_NOT_SET:
- Y_ABORT("You should change implementation according to grammar changes");
- }
- }
- TString Id(const TRule_an_id_as_compat& node, TTranslation& ctx) {
- switch (node.Alt_case()) {
- case TRule_an_id_as_compat::kAltAnIdAsCompat1:
- return Id(node.GetAlt_an_id_as_compat1().GetRule_id_as_compat1(), ctx);
- case TRule_an_id_as_compat::kAltAnIdAsCompat2:
- return IdContentFromString(ctx.Context(), ctx.Token(node.GetAlt_an_id_as_compat2().GetToken1()));
- case TRule_an_id_as_compat::ALT_NOT_SET:
- Y_ABORT("You should change implementation according to grammar changes");
- }
- }
- TString Id(const TRule_id_schema& node, TTranslation& ctx) {
- //id_schema:
- // identifier
- // | keyword_compat
- // | keyword_expr_uncompat
- // // | keyword_table_uncompat
- // | keyword_select_uncompat
- // // | keyword_alter_uncompat
- // | keyword_in_uncompat
- // | keyword_window_uncompat
- // | keyword_hint_uncompat
- //;
- switch (node.Alt_case()) {
- case TRule_id_schema::kAltIdSchema1:
- return Id(node.GetAlt_id_schema1().GetRule_identifier1(), ctx);
- case TRule_id_schema::kAltIdSchema2:
- return GetKeyword(ctx, node.GetAlt_id_schema2().GetRule_keyword_compat1());
- case TRule_id_schema::kAltIdSchema3:
- return GetKeyword(ctx, node.GetAlt_id_schema3().GetRule_keyword_expr_uncompat1());
- case TRule_id_schema::kAltIdSchema4:
- return GetKeyword(ctx, node.GetAlt_id_schema4().GetRule_keyword_select_uncompat1());
- case TRule_id_schema::kAltIdSchema5:
- return GetKeyword(ctx, node.GetAlt_id_schema5().GetRule_keyword_in_uncompat1());
- case TRule_id_schema::kAltIdSchema6:
- return GetKeyword(ctx, node.GetAlt_id_schema6().GetRule_keyword_window_uncompat1());
- case TRule_id_schema::kAltIdSchema7:
- return GetKeyword(ctx, node.GetAlt_id_schema7().GetRule_keyword_hint_uncompat1());
- case TRule_id_schema::ALT_NOT_SET:
- Y_ABORT("You should change implementation according to grammar changes");
- }
- }
- TString Id(const TRule_an_id_or_type& node, TTranslation& ctx) {
- // an_id_or_type: id_or_type | STRING_VALUE;
- switch (node.Alt_case()) {
- case TRule_an_id_or_type::kAltAnIdOrType1:
- return Id(node.GetAlt_an_id_or_type1().GetRule_id_or_type1(), ctx);
- case TRule_an_id_or_type::kAltAnIdOrType2:
- return IdContentFromString(ctx.Context(), ctx.Token(node.GetAlt_an_id_or_type2().GetToken1()));
- case TRule_an_id_or_type::ALT_NOT_SET:
- Y_ABORT("You should change implementation according to grammar changes");
- }
- }
- std::pair<bool, TString> Id(const TRule_id_or_at& node, TTranslation& ctx) {
- bool hasAt = node.HasBlock1();
- return std::make_pair(hasAt, Id(node.GetRule_an_id_or_type2(), ctx) );
- }
- TString Id(const TRule_id_table& node, TTranslation& ctx) {
- //id_table:
- // identifier
- // | keyword_compat
- // | keyword_expr_uncompat
- // // | keyword_table_uncompat
- // | keyword_select_uncompat
- // // | keyword_alter_uncompat
- // | keyword_in_uncompat
- // | keyword_window_uncompat
- // | keyword_hint_uncompat
- //;
- switch (node.Alt_case()) {
- case TRule_id_table::kAltIdTable1:
- return Id(node.GetAlt_id_table1().GetRule_identifier1(), ctx);
- case TRule_id_table::kAltIdTable2:
- return GetKeyword(ctx, node.GetAlt_id_table2().GetRule_keyword_compat1());
- case TRule_id_table::kAltIdTable3:
- return GetKeyword(ctx, node.GetAlt_id_table3().GetRule_keyword_expr_uncompat1());
- case TRule_id_table::kAltIdTable4:
- return GetKeyword(ctx, node.GetAlt_id_table4().GetRule_keyword_select_uncompat1());
- case TRule_id_table::kAltIdTable5:
- return GetKeyword(ctx, node.GetAlt_id_table5().GetRule_keyword_in_uncompat1());
- case TRule_id_table::kAltIdTable6:
- return GetKeyword(ctx, node.GetAlt_id_table6().GetRule_keyword_window_uncompat1());
- case TRule_id_table::kAltIdTable7:
- return GetKeyword(ctx, node.GetAlt_id_table7().GetRule_keyword_hint_uncompat1());
- case TRule_id_table::ALT_NOT_SET:
- Y_ABORT("You should change implementation according to grammar changes");
- }
- }
- TString Id(const TRule_an_id_table& node, TTranslation& ctx) {
- // an_id_table: id_table | STRING_VALUE;
- switch (node.Alt_case()) {
- case TRule_an_id_table::kAltAnIdTable1:
- return Id(node.GetAlt_an_id_table1().GetRule_id_table1(), ctx);
- case TRule_an_id_table::kAltAnIdTable2:
- return IdContentFromString(ctx.Context(), ctx.Token(node.GetAlt_an_id_table2().GetToken1()));
- case TRule_an_id_table::ALT_NOT_SET:
- Y_ABORT("You should change implementation according to grammar changes");
- }
- }
- TString Id(const TRule_id_table_or_type& node, TTranslation& ctx) {
- switch (node.Alt_case()) {
- case TRule_id_table_or_type::kAltIdTableOrType1:
- return Id(node.GetAlt_id_table_or_type1().GetRule_an_id_table1(), ctx);
- case TRule_id_table_or_type::kAltIdTableOrType2:
- return ctx.Identifier(node.GetAlt_id_table_or_type2().GetRule_type_id1().GetToken1());
- case TRule_id_table_or_type::ALT_NOT_SET:
- Y_ABORT("You should change implementation according to grammar changes");
- }
- }
- TString Id(const TRule_id_expr& node, TTranslation& ctx) {
- //id_expr:
- // identifier
- // | keyword_compat
- // // | keyword_expr_uncompat
- // // | keyword_table_uncompat
- // // | keyword_select_uncompat
- // | keyword_alter_uncompat
- // | keyword_in_uncompat
- // | keyword_window_uncompat
- // | keyword_hint_uncompat
- //;
- switch (node.Alt_case()) {
- case TRule_id_expr::kAltIdExpr1:
- return Id(node.GetAlt_id_expr1().GetRule_identifier1(), ctx);
- case TRule_id_expr::kAltIdExpr2:
- return GetKeyword(ctx, node.GetAlt_id_expr2().GetRule_keyword_compat1());
- case TRule_id_expr::kAltIdExpr3:
- return GetKeyword(ctx, node.GetAlt_id_expr3().GetRule_keyword_alter_uncompat1());
- case TRule_id_expr::kAltIdExpr4:
- return GetKeyword(ctx, node.GetAlt_id_expr4().GetRule_keyword_in_uncompat1());
- case TRule_id_expr::kAltIdExpr5:
- return GetKeyword(ctx, node.GetAlt_id_expr5().GetRule_keyword_window_uncompat1());
- case TRule_id_expr::kAltIdExpr6:
- return GetKeyword(ctx, node.GetAlt_id_expr6().GetRule_keyword_hint_uncompat1());
- case TRule_id_expr::ALT_NOT_SET:
- Y_ABORT("You should change implementation according to grammar changes");
- }
- }
- bool IsQuotedId(const TRule_id_expr& node, TTranslation& ctx) {
- if (node.Alt_case() != TRule_id_expr::kAltIdExpr1) {
- return false;
- }
- const auto& id = ctx.Token(node.GetAlt_id_expr1().GetRule_identifier1().GetToken1());
- // identifier: ID_PLAIN | ID_QUOTED;
- return id.StartsWith('`');
- }
- TString Id(const TRule_id_expr_in& node, TTranslation& ctx) {
- //id_expr_in:
- // identifier
- // | keyword_compat
- // // | keyword_expr_uncompat
- // // | keyword_table_uncompat
- // // | keyword_select_uncompat
- // | keyword_alter_uncompat
- // // | keyword_in_uncompat
- // | keyword_window_uncompat
- // | keyword_hint_uncompat
- //;
- switch (node.Alt_case()) {
- case TRule_id_expr_in::kAltIdExprIn1:
- return Id(node.GetAlt_id_expr_in1().GetRule_identifier1(), ctx);
- case TRule_id_expr_in::kAltIdExprIn2:
- return GetKeyword(ctx, node.GetAlt_id_expr_in2().GetRule_keyword_compat1());
- case TRule_id_expr_in::kAltIdExprIn3:
- return GetKeyword(ctx, node.GetAlt_id_expr_in3().GetRule_keyword_alter_uncompat1());
- case TRule_id_expr_in::kAltIdExprIn4:
- return GetKeyword(ctx, node.GetAlt_id_expr_in4().GetRule_keyword_window_uncompat1());
- case TRule_id_expr_in::kAltIdExprIn5:
- return GetKeyword(ctx, node.GetAlt_id_expr_in5().GetRule_keyword_hint_uncompat1());
- case TRule_id_expr_in::ALT_NOT_SET:
- Y_ABORT("You should change implementation according to grammar changes");
- }
- }
- TString Id(const TRule_id_window& node, TTranslation& ctx) {
- //id_window:
- // identifier
- // | keyword_compat
- // | keyword_expr_uncompat
- // | keyword_table_uncompat
- // | keyword_select_uncompat
- // | keyword_alter_uncompat
- // | keyword_in_uncompat
- // // | keyword_window_uncompat
- // | keyword_hint_uncompat
- //;
- switch (node.Alt_case()) {
- case TRule_id_window::kAltIdWindow1:
- return Id(node.GetAlt_id_window1().GetRule_identifier1(), ctx);
- case TRule_id_window::kAltIdWindow2:
- return GetKeyword(ctx, node.GetAlt_id_window2().GetRule_keyword_compat1());
- case TRule_id_window::kAltIdWindow3:
- return GetKeyword(ctx, node.GetAlt_id_window3().GetRule_keyword_expr_uncompat1());
- case TRule_id_window::kAltIdWindow4:
- return GetKeyword(ctx, node.GetAlt_id_window4().GetRule_keyword_table_uncompat1());
- case TRule_id_window::kAltIdWindow5:
- return GetKeyword(ctx, node.GetAlt_id_window5().GetRule_keyword_select_uncompat1());
- case TRule_id_window::kAltIdWindow6:
- return GetKeyword(ctx, node.GetAlt_id_window6().GetRule_keyword_alter_uncompat1());
- case TRule_id_window::kAltIdWindow7:
- return GetKeyword(ctx, node.GetAlt_id_window7().GetRule_keyword_in_uncompat1());
- case TRule_id_window::kAltIdWindow8:
- return GetKeyword(ctx, node.GetAlt_id_window8().GetRule_keyword_hint_uncompat1());
- case TRule_id_window::ALT_NOT_SET:
- Y_ABORT("You should change implementation according to grammar changes");
- }
- }
- TString Id(const TRule_id_without& node, TTranslation& ctx) {
- //id_without:
- // identifier
- // | keyword_compat
- // // | keyword_expr_uncompat
- // | keyword_table_uncompat
- // // | keyword_select_uncompat
- // | keyword_alter_uncompat
- // | keyword_in_uncompat
- // | keyword_window_uncompat
- // | keyword_hint_uncompat
- //;
- switch (node.Alt_case()) {
- case TRule_id_without::kAltIdWithout1:
- return Id(node.GetAlt_id_without1().GetRule_identifier1(), ctx);
- case TRule_id_without::kAltIdWithout2:
- return GetKeyword(ctx, node.GetAlt_id_without2().GetRule_keyword_compat1());
- case TRule_id_without::kAltIdWithout3:
- return GetKeyword(ctx, node.GetAlt_id_without3().GetRule_keyword_table_uncompat1());
- case TRule_id_without::kAltIdWithout4:
- return GetKeyword(ctx, node.GetAlt_id_without4().GetRule_keyword_alter_uncompat1());
- case TRule_id_without::kAltIdWithout5:
- return GetKeyword(ctx, node.GetAlt_id_without5().GetRule_keyword_in_uncompat1());
- case TRule_id_without::kAltIdWithout6:
- return GetKeyword(ctx, node.GetAlt_id_without6().GetRule_keyword_window_uncompat1());
- case TRule_id_without::kAltIdWithout7:
- return GetKeyword(ctx, node.GetAlt_id_without7().GetRule_keyword_hint_uncompat1());
- case TRule_id_without::ALT_NOT_SET:
- Y_ABORT("You should change implementation according to grammar changes");
- }
- }
- TString Id(const TRule_id_hint& node, TTranslation& ctx) {
- //id_hint:
- // identifier
- // | keyword_compat
- // | keyword_expr_uncompat
- // | keyword_table_uncompat
- // | keyword_select_uncompat
- // | keyword_alter_uncompat
- // | keyword_in_uncompat
- // | keyword_window_uncompat
- // // | keyword_hint_uncompat
- //;
- switch (node.Alt_case()) {
- case TRule_id_hint::kAltIdHint1:
- return Id(node.GetAlt_id_hint1().GetRule_identifier1(), ctx);
- case TRule_id_hint::kAltIdHint2:
- return GetKeyword(ctx, node.GetAlt_id_hint2().GetRule_keyword_compat1());
- case TRule_id_hint::kAltIdHint3:
- return GetKeyword(ctx, node.GetAlt_id_hint3().GetRule_keyword_expr_uncompat1());
- case TRule_id_hint::kAltIdHint4:
- return GetKeyword(ctx, node.GetAlt_id_hint4().GetRule_keyword_table_uncompat1());
- case TRule_id_hint::kAltIdHint5:
- return GetKeyword(ctx, node.GetAlt_id_hint5().GetRule_keyword_select_uncompat1());
- case TRule_id_hint::kAltIdHint6:
- return GetKeyword(ctx, node.GetAlt_id_hint6().GetRule_keyword_alter_uncompat1());
- case TRule_id_hint::kAltIdHint7:
- return GetKeyword(ctx, node.GetAlt_id_hint7().GetRule_keyword_in_uncompat1());
- case TRule_id_hint::kAltIdHint8:
- return GetKeyword(ctx, node.GetAlt_id_hint8().GetRule_keyword_window_uncompat1());
- case TRule_id_hint::ALT_NOT_SET:
- Y_ABORT("You should change implementation according to grammar changes");
- }
- }
- TString Id(const TRule_an_id& node, TTranslation& ctx) {
- // an_id: id | STRING_VALUE;
- switch (node.Alt_case()) {
- case TRule_an_id::kAltAnId1:
- return Id(node.GetAlt_an_id1().GetRule_id1(), ctx);
- case TRule_an_id::kAltAnId2:
- return IdContentFromString(ctx.Context(), ctx.Token(node.GetAlt_an_id2().GetToken1()));
- case TRule_an_id::ALT_NOT_SET:
- Y_ABORT("You should change implementation according to grammar changes");
- }
- }
- TString Id(const TRule_an_id_schema& node, TTranslation& ctx) {
- // an_id_schema: id_schema | STRING_VALUE;
- switch (node.Alt_case()) {
- case TRule_an_id_schema::kAltAnIdSchema1:
- return Id(node.GetAlt_an_id_schema1().GetRule_id_schema1(), ctx);
- case TRule_an_id_schema::kAltAnIdSchema2:
- return IdContentFromString(ctx.Context(), ctx.Token(node.GetAlt_an_id_schema2().GetToken1()));
- case TRule_an_id_schema::ALT_NOT_SET:
- Y_ABORT("You should change implementation according to grammar changes");
- }
- }
- TString Id(const TRule_an_id_expr& node, TTranslation& ctx) {
- // an_id_expr: id_expr | STRING_VALUE;
- switch (node.Alt_case()) {
- case TRule_an_id_expr::kAltAnIdExpr1:
- return Id(node.GetAlt_an_id_expr1().GetRule_id_expr1(), ctx);
- case TRule_an_id_expr::kAltAnIdExpr2:
- return IdContentFromString(ctx.Context(), ctx.Token(node.GetAlt_an_id_expr2().GetToken1()));
- case TRule_an_id_expr::ALT_NOT_SET:
- Y_ABORT("You should change implementation according to grammar changes");
- }
- }
- TString Id(const TRule_an_id_window& node, TTranslation& ctx) {
- // an_id_window: id_window | STRING_VALUE;
- switch (node.Alt_case()) {
- case TRule_an_id_window::kAltAnIdWindow1:
- return Id(node.GetAlt_an_id_window1().GetRule_id_window1(), ctx);
- case TRule_an_id_window::kAltAnIdWindow2:
- return IdContentFromString(ctx.Context(), ctx.Token(node.GetAlt_an_id_window2().GetToken1()));
- case TRule_an_id_window::ALT_NOT_SET:
- Y_ABORT("You should change implementation according to grammar changes");
- }
- }
- TString Id(const TRule_an_id_without& node, TTranslation& ctx) {
- // an_id_without: id_without | STRING_VALUE;
- switch (node.Alt_case()) {
- case TRule_an_id_without::kAltAnIdWithout1:
- return Id(node.GetAlt_an_id_without1().GetRule_id_without1(), ctx);
- case TRule_an_id_without::kAltAnIdWithout2:
- return IdContentFromString(ctx.Context(), ctx.Token(node.GetAlt_an_id_without2().GetToken1()));
- case TRule_an_id_without::ALT_NOT_SET:
- Y_ABORT("You should change implementation according to grammar changes");
- }
- }
- TString Id(const TRule_an_id_hint& node, TTranslation& ctx) {
- // an_id_hint: id_hint | STRING_VALUE;
- switch (node.Alt_case()) {
- case TRule_an_id_hint::kAltAnIdHint1:
- return Id(node.GetAlt_an_id_hint1().GetRule_id_hint1(), ctx);
- case TRule_an_id_hint::kAltAnIdHint2:
- return IdContentFromString(ctx.Context(), ctx.Token(node.GetAlt_an_id_hint2().GetToken1()));
- case TRule_an_id_hint::ALT_NOT_SET:
- Y_ABORT("You should change implementation according to grammar changes");
- }
- }
- TString Id(const TRule_an_id_pure& node, TTranslation& ctx) {
- // an_id_pure: identifier | STRING_VALUE;
- switch (node.Alt_case()) {
- case TRule_an_id_pure::kAltAnIdPure1:
- return Id(node.GetAlt_an_id_pure1().GetRule_identifier1(), ctx);
- case TRule_an_id_pure::kAltAnIdPure2:
- return IdContentFromString(ctx.Context(), ctx.Token(node.GetAlt_an_id_pure2().GetToken1()));
- case TRule_an_id_pure::ALT_NOT_SET:
- Y_ABORT("You should change implementation according to grammar changes");
- }
- }
- TViewDescription Id(const TRule_view_name& node, TTranslation& ctx) {
- switch (node.Alt_case()) {
- case TRule_view_name::kAltViewName1:
- return {Id(node.GetAlt_view_name1().GetRule_an_id1(), ctx)};
- case TRule_view_name::kAltViewName2:
- return {"", true};
- case TRule_view_name::ALT_NOT_SET:
- Y_ABORT("You should change implementation according to grammar changes");
- }
- }
- bool NamedNodeImpl(const TRule_bind_parameter& node, TString& name, TTranslation& ctx) {
- // bind_parameter: DOLLAR (an_id_or_type | TRUE | FALSE);
- TString id;
- switch (node.GetBlock2().Alt_case()) {
- case TRule_bind_parameter::TBlock2::kAlt1:
- id = Id(node.GetBlock2().GetAlt1().GetRule_an_id_or_type1(), ctx);
- break;
- case TRule_bind_parameter::TBlock2::kAlt2:
- id = ctx.Token(node.GetBlock2().GetAlt2().GetToken1());
- break;
- case TRule_bind_parameter::TBlock2::kAlt3:
- id = ctx.Token(node.GetBlock2().GetAlt3().GetToken1());
- break;
- case TRule_bind_parameter::TBlock2::ALT_NOT_SET:
- Y_ABORT("You should change implementation according to grammar changes");
- }
- auto dollar = ctx.Token(node.GetToken1());
- if (id.empty()) {
- ctx.Error() << "Empty symbol name is not allowed";
- return false;
- }
- name = dollar + id;
- return true;
- }
- TString OptIdPrefixAsStr(const TRule_opt_id_prefix& node, TTranslation& ctx, const TString& defaultStr) {
- if (!node.HasBlock1()) {
- return defaultStr;
- }
- return Id(node.GetBlock1().GetRule_an_id1(), ctx);
- }
- TString OptIdPrefixAsStr(const TRule_opt_id_prefix_or_type& node, TTranslation& ctx, const TString& defaultStr) {
- if (!node.HasBlock1()) {
- return defaultStr;
- }
- return Id(node.GetBlock1().GetRule_an_id_or_type1(), ctx);
- }
- void PureColumnListStr(const TRule_pure_column_list& node, TTranslation& ctx, TVector<TString>& outList) {
- outList.push_back(Id(node.GetRule_an_id2(), ctx));
- for (auto& block: node.GetBlock3()) {
- outList.push_back(Id(block.GetRule_an_id2(), ctx));
- }
- }
- bool NamedNodeImpl(const TRule_opt_bind_parameter& node, TString& name, bool& isOptional, TTranslation& ctx) {
- // opt_bind_parameter: bind_parameter QUESTION?;
- isOptional = false;
- if (!NamedNodeImpl(node.GetRule_bind_parameter1(), name, ctx)) {
- return false;
- }
- isOptional = node.HasBlock2();
- return true;
- }
- TDeferredAtom PureColumnOrNamed(const TRule_pure_column_or_named& node, TTranslation& ctx) {
- switch (node.Alt_case()) {
- case TRule_pure_column_or_named::kAltPureColumnOrNamed1: {
- TString named;
- if (!NamedNodeImpl(node.GetAlt_pure_column_or_named1().GetRule_bind_parameter1(), named, ctx)) {
- return {};
- }
- auto namedNode = ctx.GetNamedNode(named);
- if (!namedNode) {
- return {};
- }
- return TDeferredAtom(namedNode, ctx.Context());
- }
- case TRule_pure_column_or_named::kAltPureColumnOrNamed2:
- return TDeferredAtom(ctx.Context().Pos(), Id(node.GetAlt_pure_column_or_named2().GetRule_an_id1(), ctx));
- case TRule_pure_column_or_named::ALT_NOT_SET:
- Y_ABORT("You should change implementation according to grammar changes");
- }
- }
- bool PureColumnOrNamedListStr(const TRule_pure_column_or_named_list& node, TTranslation& ctx, TVector<TDeferredAtom>& outList) {
- outList.push_back(PureColumnOrNamed(node.GetRule_pure_column_or_named2(), ctx));
- if (outList.back().Empty()) {
- return false;
- }
- for (auto& block : node.GetBlock3()) {
- outList.push_back(PureColumnOrNamed(block.GetRule_pure_column_or_named2(), ctx));
- if (outList.back().Empty()) {
- return false;
- }
- }
- return true;
- }
- bool TSqlTranslation::CreateTableIndex(const TRule_table_index& node, TVector<TIndexDescription>& indexes) {
- indexes.emplace_back(IdEx(node.GetRule_an_id2(), *this));
- const auto& indexType = node.GetRule_table_index_type3().GetBlock1();
- switch (indexType.Alt_case()) {
- // "GLOBAL"
- case TRule_table_index_type_TBlock1::kAlt1: {
- auto globalIndex = indexType.GetAlt1().GetRule_global_index1();
- bool uniqIndex = false;
- if (globalIndex.HasBlock2()) {
- uniqIndex = true;
- }
- if (globalIndex.HasBlock3()) {
- const TString token = to_lower(Ctx.Token(globalIndex.GetBlock3().GetToken1()));
- if (token == "sync") {
- if (uniqIndex) {
- indexes.back().Type = TIndexDescription::EType::GlobalSyncUnique;
- } else {
- indexes.back().Type = TIndexDescription::EType::GlobalSync;
- }
- } else if (token == "async") {
- if (uniqIndex) {
- AltNotImplemented("unique", indexType);
- return false;
- }
- indexes.back().Type = TIndexDescription::EType::GlobalAsync;
- } else {
- Y_ABORT("You should change implementation according to grammar changes");
- }
- }
- }
- break;
- // "LOCAL"
- case TRule_table_index_type_TBlock1::kAlt2:
- AltNotImplemented("local", indexType);
- return false;
- case TRule_table_index_type_TBlock1::ALT_NOT_SET:
- Y_ABORT("You should change implementation according to grammar changes");
- }
- if (node.GetRule_table_index_type3().HasBlock2()) {
- const TString subType = to_upper(IdEx(node.GetRule_table_index_type3().GetBlock2().GetRule_index_subtype2().GetRule_an_id1(), *this).Name) ;
- if (subType == "VECTOR_KMEANS_TREE") {
- if (indexes.back().Type != TIndexDescription::EType::GlobalSync) {
- Ctx.Error() << subType << " index can only be GLOBAL [SYNC]";
- return false;
- }
- indexes.back().Type = TIndexDescription::EType::GlobalVectorKmeansTree;
- } else {
- Ctx.Error() << subType << " index subtype is not supported";
- return false;
- }
- }
- // WITH
- if (node.HasBlock10()) {
- //const auto& with = node.GetBlock4();
- auto& index = indexes.back();
- if (index.Type == TIndexDescription::EType::GlobalVectorKmeansTree) {
- auto& vectorSettings = index.IndexSettings.emplace<TVectorIndexSettings>();
- if (!CreateIndexSettings(node.GetBlock10().GetRule_with_index_settings1(), index.Type, index.IndexSettings)) {
- return false;
- }
- if (!vectorSettings.Validate(Ctx)) {
- return false;
- }
- } else {
- AltNotImplemented("with", indexType);
- return false;
- }
- }
- indexes.back().IndexColumns.emplace_back(IdEx(node.GetRule_an_id_schema6(), *this));
- for (const auto& block : node.GetBlock7()) {
- indexes.back().IndexColumns.emplace_back(IdEx(block.GetRule_an_id_schema2(), *this));
- }
- if (node.HasBlock9()) {
- const auto& block = node.GetBlock9();
- indexes.back().DataColumns.emplace_back(IdEx(block.GetRule_an_id_schema3(), *this));
- for (const auto& inner : block.GetBlock4()) {
- indexes.back().DataColumns.emplace_back(IdEx(inner.GetRule_an_id_schema2(), *this));
- }
- }
- return true;
- }
- bool TSqlTranslation::CreateIndexSettings(const TRule_with_index_settings& settingsNode,
- TIndexDescription::EType indexType,
- TIndexDescription::TIndexSettings& indexSettings) {
- const auto& firstEntry = settingsNode.GetRule_index_setting_entry3();
- if (!CreateIndexSettingEntry(IdEx(firstEntry.GetRule_an_id1(), *this), firstEntry.GetRule_index_setting_value3(), indexType, indexSettings)) {
- return false;
- }
- for (auto& block : settingsNode.GetBlock4()) {
- const auto& entry = block.GetRule_index_setting_entry2();
- if (!CreateIndexSettingEntry(IdEx(entry.GetRule_an_id1(), *this), entry.GetRule_index_setting_value3(), indexType, indexSettings)) {
- return false;
- }
- }
- return true;
- }
- template<typename T>
- std::tuple<bool, T, TString> TSqlTranslation::GetIndexSettingValue(const TRule_index_setting_value& node) {
- T value{};
- // id_or_type
- if (node.HasAlt_index_setting_value1()) {
- const TString stringValue = to_lower(IdEx(node.GetAlt_index_setting_value1().GetRule_id_or_type1(), *this).Name);
- if (!TryFromString<T>(stringValue, value)) {
- return {false, value, stringValue};
- }
- return {true, value, stringValue};
- }
- // STRING_VALUE
- else if (node.HasAlt_index_setting_value2()) {
- const TString stringValue = to_lower(Token(node.GetAlt_index_setting_value2().GetToken1()));
- const auto unescaped = StringContent(Ctx, Ctx.Pos(), stringValue);
- if (!unescaped) {
- return {false, value, stringValue};
- }
- if (!TryFromString<T>(unescaped->Content, value)) {
- return {false, value, stringValue};
- }
- return {true, value, unescaped->Content};
- } else {
- Y_ABORT("You should change implementation according to grammar changes");
- }
- }
- template<>
- std::tuple<bool, ui64, TString> TSqlTranslation::GetIndexSettingValue(const TRule_index_setting_value& node) {
- const auto& intNode = node.GetAlt_index_setting_value3().GetRule_integer1();
- const TString stringValue = Token(intNode.GetToken1());
- ui64 value = 0;
- TString suffix;
- if (!ParseNumbers(Ctx, stringValue, value, suffix)) {
- return {false, value, stringValue};
- }
- return {true, value, stringValue};
- }
- template<>
- std::tuple<bool, bool, TString> TSqlTranslation::GetIndexSettingValue(const TRule_index_setting_value& node) {
- bool value = false;
- const TString stringValue = to_lower(Token(node.GetAlt_index_setting_value4().GetRule_bool_value1().GetToken1()));;
- if (!TryFromString<bool>(stringValue, value)) {
- return {false, value, stringValue};
- }
- return {true, value, stringValue};
- }
- bool TSqlTranslation::CreateIndexSettingEntry(const TIdentifier &id,
- const TRule_index_setting_value& node,
- TIndexDescription::EType indexType,
- TIndexDescription::TIndexSettings& indexSettings) {
- if (indexType == TIndexDescription::EType::GlobalVectorKmeansTree) {
- TVectorIndexSettings &vectorIndexSettings = std::get<TVectorIndexSettings>(indexSettings);
- if (to_lower(id.Name) == "distance") {
- const auto [success, value, stringValue] = GetIndexSettingValue<TVectorIndexSettings::EDistance>(node);
- if (!success) {
- Ctx.Error() << "Invalid distance: " << stringValue;
- return false;
- }
- vectorIndexSettings.Distance = value;
- } else if (to_lower(id.Name) == "similarity") {
- const auto [success, value, stringValue] = GetIndexSettingValue<TVectorIndexSettings::ESimilarity>(node);
- if (!success) {
- Ctx.Error() << "Invalid similarity: " << stringValue;
- return false;
- }
- vectorIndexSettings.Similarity = value;
- } else if (to_lower(id.Name) == "vector_type") {
- const auto [success, value, stringValue] = GetIndexSettingValue<TVectorIndexSettings::EVectorType>(node);
- if (!success) {
- Ctx.Error() << "Invalid vector_type: " << stringValue;
- return false;
- }
- vectorIndexSettings.VectorType = value;
- } else if (to_lower(id.Name) == "vector_dimension") {
- const auto [success, value, stringValue] = GetIndexSettingValue<ui64>(node);
- if (!success || value > Max<ui32>()) {
- Ctx.Error() << "Invalid vector_dimension: " << stringValue;
- return false;
- }
- vectorIndexSettings.VectorDimension = value;
- } else if (to_lower(id.Name) == "clusters") {
- const auto [success, value, stringValue] = GetIndexSettingValue<ui64>(node);
- if (!success || value > Max<ui32>()) {
- Ctx.Error() << "Invalid clusters: " << stringValue;
- return false;
- }
- vectorIndexSettings.Clusters = value;
- } else if (to_lower(id.Name) == "levels") {
- const auto [success, value, stringValue] = GetIndexSettingValue<ui64>(node);
- if (!success || value > Max<ui32>()) {
- Ctx.Error() << "Invalid levels: " << stringValue;
- return false;
- }
- vectorIndexSettings.Levels = value;
- } else {
- Ctx.Error() << "Unknown index setting: " << id.Name;
- return false;
- }
- } else {
- Ctx.Error() << "Unknown index setting: " << id.Name;
- return false;
- }
- return true;
- }
- std::pair<TString, TViewDescription> TableKeyImpl(const std::pair<bool, TString>& nameWithAt, TViewDescription view, TTranslation& ctx) {
- if (nameWithAt.first) {
- view = {"@"};
- ctx.Context().IncrementMonCounter("sql_features", "AnonymousTable");
- }
- return std::make_pair(nameWithAt.second, view);
- }
- std::pair<TString, TViewDescription> TableKeyImpl(const TRule_table_key& node, TTranslation& ctx, bool hasAt) {
- auto name(Id(node.GetRule_id_table_or_type1(), ctx));
- TViewDescription view;
- if (node.HasBlock2()) {
- view = Id(node.GetBlock2().GetRule_view_name2(), ctx);
- ctx.Context().IncrementMonCounter("sql_features", "View");
- }
- return TableKeyImpl(std::make_pair(hasAt, name), view, ctx);
- }
- /// \return optional prefix
- TString ColumnNameAsStr(TTranslation& ctx, const TRule_column_name& node, TString& id) {
- id = Id(node.GetRule_an_id2(), ctx);
- return OptIdPrefixAsStr(node.GetRule_opt_id_prefix1(), ctx);
- }
- TString ColumnNameAsSingleStr(TTranslation& ctx, const TRule_column_name& node) {
- TString body;
- const TString prefix = ColumnNameAsStr(ctx, node, body);
- return prefix ? prefix + '.' + body : body;
- }
- TTableHints GetContextHints(TContext& ctx) {
- TTableHints hints;
- if (ctx.PragmaInferSchema) {
- hints["infer_schema"] = {};
- }
- if (ctx.PragmaDirectRead) {
- hints["direct_read"] = {};
- }
- return hints;
- }
- TTableHints GetTableFuncHints(TStringBuf funcName) {
- TCiString func(funcName);
- TTableHints res;
- if (func.StartsWith("range") || func.StartsWith("like") || func.StartsWith("regexp") || func.StartsWith("filter")) {
- res.emplace("ignore_non_existing", TVector<TNodePtr>{});
- } else if (func.StartsWith("each")) {
- res.emplace("ignore_non_existing", TVector<TNodePtr>{});
- res.emplace("warn_non_existing", TVector<TNodePtr>{});
- }
- return res;
- }
- TNodePtr TSqlTranslation::NamedExpr(const TRule_named_expr& node, EExpr exprMode) {
- TSqlExpression expr(Ctx, Mode);
- if (exprMode == EExpr::GroupBy) {
- expr.SetSmartParenthesisMode(TSqlExpression::ESmartParenthesis::GroupBy);
- } else if (exprMode == EExpr::SqlLambdaParams) {
- expr.SetSmartParenthesisMode(TSqlExpression::ESmartParenthesis::SqlLambdaParams);
- }
- if (node.HasBlock2()) {
- expr.MarkAsNamed();
- }
- TNodePtr exprNode(expr.Build(node.GetRule_expr1()));
- if (!exprNode) {
- Ctx.IncrementMonCounter("sql_errors", "NamedExprInvalid");
- return nullptr;
- }
- if (node.HasBlock2()) {
- exprNode = SafeClone(exprNode);
- exprNode->SetLabel(Id(node.GetBlock2().GetRule_an_id_or_type2(), *this));
- }
- return exprNode;
- }
- bool TSqlTranslation::NamedExprList(const TRule_named_expr_list& node, TVector<TNodePtr>& exprs, EExpr exprMode) {
- exprs.emplace_back(NamedExpr(node.GetRule_named_expr1(), exprMode));
- if (!exprs.back()) {
- return false;
- }
- for (auto& b: node.GetBlock2()) {
- exprs.emplace_back(NamedExpr(b.GetRule_named_expr2(), exprMode));
- if (!exprs.back()) {
- return false;
- }
- }
- return true;
- }
- bool TSqlTranslation::BindList(const TRule_bind_parameter_list& node, TVector<TSymbolNameWithPos>& bindNames) {
- bindNames.clear();
- TString name;
- if (!NamedNodeImpl(node.GetRule_bind_parameter1(), name, *this)) {
- return false;
- }
- bindNames.emplace_back(TSymbolNameWithPos{name, Ctx.Pos()});
- for (auto& b: node.GetBlock2()) {
- if (!NamedNodeImpl(b.GetRule_bind_parameter2(), name, *this)) {
- return false;
- }
- bindNames.emplace_back(TSymbolNameWithPos{name, Ctx.Pos()});
- }
- return true;
- }
- bool TSqlTranslation::ActionOrSubqueryArgs(const TRule_action_or_subquery_args& node, TVector<TSymbolNameWithPos>& bindNames, ui32& optionalArgsCount) {
- bindNames.clear();
- optionalArgsCount = 0;
- TString name;
- bool isOptional = false;
- if (!NamedNodeImpl(node.GetRule_opt_bind_parameter1(), name, isOptional, *this)) {
- return false;
- }
- if (isOptional) {
- optionalArgsCount++;
- }
- bindNames.emplace_back(TSymbolNameWithPos{name, Ctx.Pos()});
- for (auto& b: node.GetBlock2()) {
- if (!NamedNodeImpl(b.GetRule_opt_bind_parameter2(), name, isOptional, *this)) {
- return false;
- }
- if (isOptional) {
- optionalArgsCount++;
- } else if (optionalArgsCount > 0) {
- Context().Error() << "Non-optional argument can not follow optional one";
- return false;
- }
- bindNames.emplace_back(TSymbolNameWithPos{name, Ctx.Pos()});
- }
- return true;
- }
- bool TSqlTranslation::ModulePath(const TRule_module_path& node, TVector<TString>& path) {
- if (node.HasBlock1()) {
- path.emplace_back(TString());
- }
- path.emplace_back(Id(node.GetRule_an_id2(), *this));
- for (auto& b: node.GetBlock3()) {
- path.emplace_back(Id(b.GetRule_an_id2(), *this));
- }
- return true;
- }
- bool TSqlTranslation::NamedBindList(const TRule_named_bind_parameter_list& node, TVector<TSymbolNameWithPos>& names,
- TVector<TSymbolNameWithPos>& aliases)
- {
- names.clear();
- aliases.clear();
- TSymbolNameWithPos name;
- TSymbolNameWithPos alias;
- if (!NamedBindParam(node.GetRule_named_bind_parameter1(), name, alias)) {
- return false;
- }
- names.push_back(name);
- aliases.push_back(alias);
- for (auto& b: node.GetBlock2()) {
- if (!NamedBindParam(b.GetRule_named_bind_parameter2(), name, alias)) {
- return false;
- }
- names.push_back(name);
- aliases.push_back(alias);
- }
- return true;
- }
- bool TSqlTranslation::NamedBindParam(const TRule_named_bind_parameter& node, TSymbolNameWithPos& name, TSymbolNameWithPos& alias) {
- name = alias = {};
- if (!NamedNodeImpl(node.GetRule_bind_parameter1(), name.Name, *this)) {
- return false;
- }
- name.Pos = Ctx.Pos();
- if (node.HasBlock2()) {
- if (!NamedNodeImpl(node.GetBlock2().GetRule_bind_parameter2(), alias.Name, *this)) {
- return false;
- }
- alias.Pos = Ctx.Pos();
- }
- return true;
- }
- TMaybe<TTableArg> TSqlTranslation::TableArgImpl(const TRule_table_arg& node) {
- TTableArg ret;
- ret.HasAt = node.HasBlock1();
- TColumnRefScope scope(Ctx, EColumnRefState::AsStringLiteral);
- ret.Expr = NamedExpr(node.GetRule_named_expr2());
- if (!ret.Expr) {
- return Nothing();
- }
- if (node.HasBlock3()) {
- ret.View = Id(node.GetBlock3().GetRule_view_name2(), *this);
- Context().IncrementMonCounter("sql_features", "View");
- }
- return ret;
- }
- bool TSqlTranslation::ClusterExpr(const TRule_cluster_expr& node, bool allowWildcard, TString& service, TDeferredAtom& cluster) {
- bool allowBinding = false;
- bool isBinding;
- return ClusterExpr(node, allowWildcard, allowBinding, service, cluster, isBinding);
- }
- bool TSqlTranslation::ClusterExprOrBinding(const TRule_cluster_expr& node, TString& service, TDeferredAtom& cluster, bool& isBinding) {
- bool allowWildcard = false;
- bool allowBinding = true;
- return ClusterExpr(node, allowWildcard, allowBinding, service, cluster, isBinding);
- }
- bool TSqlTranslation::ClusterExpr(const TRule_cluster_expr& node, bool allowWildcard, bool allowBinding, TString& service,
- TDeferredAtom& cluster, bool& isBinding)
- {
- service = "";
- cluster = TDeferredAtom();
- isBinding = false;
- if (node.HasBlock1()) {
- service = to_lower(Id(node.GetBlock1().GetRule_an_id1(), *this));
- allowBinding = false;
- if (service != YtProviderName &&
- service != KikimrProviderName &&
- service != RtmrProviderName && service != StatProviderName) {
- Ctx.Error() << "Unknown service: " << service;
- return false;
- }
- }
- switch (node.GetBlock2().Alt_case()) {
- case TRule_cluster_expr::TBlock2::kAlt1: {
- auto value = PureColumnOrNamed(node.GetBlock2().GetAlt1().GetRule_pure_column_or_named1(), *this);
- if (value.Empty()) {
- return false;
- }
- if (value.GetLiteral()) {
- TString clusterName = *value.GetLiteral();
- if (allowBinding && to_lower(clusterName) == "bindings") {
- switch (Ctx.Settings.BindingsMode) {
- case NSQLTranslation::EBindingsMode::DISABLED:
- Ctx.Error(Ctx.Pos(), TIssuesIds::YQL_DISABLED_BINDINGS) << "Please remove 'bindings.' from your query, the support for this syntax has ended";
- Ctx.IncrementMonCounter("sql_errors", "DisabledBinding");
- return false;
- case NSQLTranslation::EBindingsMode::ENABLED:
- isBinding = true;
- break;
- case NSQLTranslation::EBindingsMode::DROP_WITH_WARNING:
- Ctx.Warning(Ctx.Pos(), TIssuesIds::YQL_DEPRECATED_BINDINGS) << "Please remove 'bindings.' from your query, the support for this syntax will be dropped soon";
- Ctx.IncrementMonCounter("sql_errors", "DeprecatedBinding");
- [[fallthrough]];
- case NSQLTranslation::EBindingsMode::DROP:
- service = Context().Scoped->CurrService;
- cluster = Context().Scoped->CurrCluster;
- break;
- }
- return true;
- }
- TString normalizedClusterName;
- auto foundProvider = Ctx.GetClusterProvider(clusterName, normalizedClusterName);
- if (!foundProvider) {
- Ctx.Error() << "Unknown cluster: " << clusterName;
- return false;
- }
- if (service && *foundProvider != service) {
- Ctx.Error() << "Mismatch of cluster " << clusterName << " service, expected: "
- << *foundProvider << ", got: " << service;
- return false;
- }
- if (!service) {
- service = *foundProvider;
- }
- value = TDeferredAtom(Ctx.Pos(), normalizedClusterName);
- } else {
- if (!service) {
- Ctx.Error() << "Cluster service is not set";
- return false;
- }
- }
- cluster = value;
- return true;
- }
- case TRule_cluster_expr::TBlock2::kAlt2: {
- if (!allowWildcard) {
- Ctx.Error() << "Cluster wildcards allowed only in USE statement";
- return false;
- }
- return true;
- }
- case TRule_cluster_expr::TBlock2::ALT_NOT_SET:
- Y_ABORT("You should change implementation according to grammar changes");
- }
- }
- bool TSqlTranslation::ApplyTableBinding(const TString& binding, TTableRef& tr, TTableHints& hints) {
- NSQLTranslation::TBindingInfo bindingInfo;
- if (const auto& error = ExtractBindingInfo(Context().Settings, binding, bindingInfo)) {
- Ctx.Error() << error;
- return false;
- }
- if (bindingInfo.Schema) {
- TNodePtr schema = BuildQuotedAtom(Ctx.Pos(), bindingInfo.Schema);
- TNodePtr type = new TCallNodeImpl(Ctx.Pos(), "SqlTypeFromYson", { schema });
- TNodePtr columns = new TCallNodeImpl(Ctx.Pos(), "SqlColumnOrderFromYson", { schema });
- hints["user_schema"] = { type, columns };
- }
- for (auto& [key, values] : bindingInfo.Attributes) {
- TVector<TNodePtr> hintValue;
- for (auto& column : values) {
- hintValue.push_back(BuildQuotedAtom(Ctx.Pos(), column));
- }
- hints[key] = std::move(hintValue);
- }
- tr.Service = bindingInfo.ClusterType;
- tr.Cluster = TDeferredAtom(Ctx.Pos(), bindingInfo.Cluster);
- const TString view = "";
- tr.Keys = BuildTableKey(Ctx.Pos(), tr.Service, tr.Cluster, TDeferredAtom(Ctx.Pos(), bindingInfo.Path), {view});
- return true;
- }
- bool TSqlTranslation::TableRefImpl(const TRule_table_ref& node, TTableRef& result, bool unorderedSubquery) {
- // table_ref:
- // (cluster_expr DOT)? AT?
- // (table_key | an_id_expr LPAREN (table_arg (COMMA table_arg)*)? RPAREN |
- // bind_parameter (LPAREN expr_list? RPAREN)? (VIEW an_id)?)
- // table_hints?;
- if (Mode == NSQLTranslation::ESqlMode::LIMITED_VIEW && node.HasBlock1()) {
- Ctx.Error() << "Cluster should not be used in limited view";
- return false;
- }
- auto service = Context().Scoped->CurrService;
- auto cluster = Context().Scoped->CurrCluster;
- const bool hasAt = node.HasBlock2();
- bool isBinding = false;
- if (node.HasBlock1()) {
- const auto& clusterExpr = node.GetBlock1().GetRule_cluster_expr1();
- bool result = !hasAt ?
- ClusterExprOrBinding(clusterExpr, service, cluster, isBinding) : ClusterExpr(clusterExpr, false, service, cluster);
- if (!result) {
- return false;
- }
- }
- TTableRef tr(Context().MakeName("table"), service, cluster, nullptr);
- TPosition pos(Context().Pos());
- TTableHints hints = GetContextHints(Ctx);
- TTableHints tableHints;
- TMaybe<TString> keyFunc;
- auto& block = node.GetBlock3();
- switch (block.Alt_case()) {
- case TRule_table_ref::TBlock3::kAlt1: {
- if (!isBinding && cluster.Empty()) {
- Ctx.Error() << "No cluster name given and no default cluster is selected";
- return false;
- }
- auto pair = TableKeyImpl(block.GetAlt1().GetRule_table_key1(), *this, hasAt);
- if (isBinding) {
- TString binding = pair.first;
- auto view = pair.second;
- if (!view.ViewName.empty()) {
- YQL_ENSURE(view != TViewDescription{"@"});
- Ctx.Error() << "VIEW is not supported for table bindings";
- return false;
- }
- if (!ApplyTableBinding(binding, tr, tableHints)) {
- return false;
- }
- } else {
- tr.Keys = BuildTableKey(pos, service, cluster, TDeferredAtom(pos, pair.first), pair.second);
- }
- break;
- }
- case TRule_table_ref::TBlock3::kAlt2: {
- if (cluster.Empty()) {
- Ctx.Error() << "No cluster name given and no default cluster is selected";
- return false;
- }
- auto& alt = block.GetAlt2();
- keyFunc = Id(alt.GetRule_an_id_expr1(), *this);
- TVector<TTableArg> args;
- if (alt.HasBlock3()) {
- auto& argsBlock = alt.GetBlock3();
- auto arg = TableArgImpl(argsBlock.GetRule_table_arg1());
- if (!arg) {
- return false;
- }
- args.push_back(std::move(*arg));
- for (auto& b : argsBlock.GetBlock2()) {
- arg = TableArgImpl(b.GetRule_table_arg2());
- if (!arg) {
- return false;
- }
- args.push_back(std::move(*arg));
- }
- }
- tableHints = GetTableFuncHints(*keyFunc);
- tr.Keys = BuildTableKeys(pos, service, cluster, *keyFunc, args);
- break;
- }
- case TRule_table_ref::TBlock3::kAlt3: {
- auto& alt = block.GetAlt3();
- Ctx.IncrementMonCounter("sql_features", "NamedNodeUseSource");
- TString named;
- if (!NamedNodeImpl(alt.GetRule_bind_parameter1(), named, *this)) {
- return false;
- }
- if (hasAt) {
- if (alt.HasBlock2()) {
- Ctx.Error() << "Subquery must not be used as anonymous table name";
- return false;
- }
- if (alt.HasBlock3()) {
- Ctx.Error() << "View is not supported for anonymous tables";
- return false;
- }
- if (node.HasBlock4()) {
- Ctx.Error() << "Hints are not supported for anonymous tables";
- return false;
- }
- auto namedNode = GetNamedNode(named);
- if (!namedNode) {
- return false;
- }
- auto source = TryMakeSourceFromExpression(Ctx.Pos(), Ctx, service, cluster, namedNode, "@");
- if (!source) {
- Ctx.Error() << "Cannot infer cluster and table name";
- return false;
- }
- result.Source = source;
- return true;
- }
- auto nodePtr = GetNamedNode(named);
- if (!nodePtr) {
- Ctx.IncrementMonCounter("sql_errors", "NamedNodeSourceError");
- return false;
- }
- if (alt.HasBlock2()) {
- if (alt.HasBlock3()) {
- Ctx.Error() << "View is not supported for subqueries";
- return false;
- }
- if (node.HasBlock4()) {
- Ctx.Error() << "Hints are not supported for subqueries";
- return false;
- }
- TVector<TNodePtr> values;
- values.push_back(new TAstAtomNodeImpl(Ctx.Pos(), "Apply", TNodeFlags::Default));
- values.push_back(nodePtr);
- values.push_back(new TAstAtomNodeImpl(Ctx.Pos(), "world", TNodeFlags::Default));
- TSqlExpression sqlExpr(Ctx, Mode);
- if (alt.GetBlock2().HasBlock2() && !ExprList(sqlExpr, values, alt.GetBlock2().GetBlock2().GetRule_expr_list1())) {
- return false;
- }
- TNodePtr apply = new TAstListNodeImpl(Ctx.Pos(), std::move(values));
- if (unorderedSubquery && Ctx.UnorderedSubqueries) {
- apply = new TCallNodeImpl(Ctx.Pos(), "UnorderedSubquery", { apply });
- }
- result.Source = BuildNodeSource(Ctx.Pos(), apply);
- return true;
- }
- TTableHints hints;
- TTableHints contextHints = GetContextHints(Ctx);
- auto ret = BuildInnerSource(Ctx.Pos(), nodePtr, service, cluster);
- if (alt.HasBlock3()) {
- auto view = Id(alt.GetBlock3().GetRule_view_name2(), *this);
- Ctx.IncrementMonCounter("sql_features", "View");
- bool result = view.PrimaryFlag
- ? ret->SetPrimaryView(Ctx, Ctx.Pos())
- : ret->SetViewName(Ctx, Ctx.Pos(), view.ViewName);
- if (!result) {
- return false;
- }
- }
- if (node.HasBlock4()) {
- auto tmp = TableHintsImpl(node.GetBlock4().GetRule_table_hints1(), service, keyFunc.GetOrElse(""));
- if (!tmp) {
- return false;
- }
- hints = *tmp;
- }
- if (hints || contextHints) {
- if (!ret->SetTableHints(Ctx, Ctx.Pos(), hints, contextHints)) {
- return false;
- }
- }
- result.Source = ret;
- return true;
- }
- case TRule_table_ref::TBlock3::ALT_NOT_SET:
- Y_ABORT("You should change implementation according to grammar changes");
- }
- MergeHints(hints, tableHints);
- if (node.HasBlock4()) {
- auto tmp = TableHintsImpl(node.GetBlock4().GetRule_table_hints1(), service, keyFunc.GetOrElse(""));
- if (!tmp) {
- Ctx.Error() << "Failed to parse table hints";
- return false;
- }
- MergeHints(hints, *tmp);
- }
- if (!hints.empty()) {
- tr.Options = BuildInputOptions(pos, hints);
- }
- if (!tr.Keys) {
- return false;
- }
- result = tr;
- return true;
- }
- TMaybe<TSourcePtr> TSqlTranslation::AsTableImpl(const TRule_table_ref& node) {
- const auto& block = node.GetBlock3();
- if (block.Alt_case() == TRule_table_ref::TBlock3::kAlt2) {
- auto& alt = block.GetAlt2();
- TCiString func(Id(alt.GetRule_an_id_expr1(), *this));
- if (func == "as_table") {
- if (node.HasBlock1()) {
- Ctx.Error() << "Cluster shouldn't be specified for AS_TABLE source";
- return TMaybe<TSourcePtr>(nullptr);
- }
- if (!alt.HasBlock3() || !alt.GetBlock3().GetBlock2().empty()) {
- Ctx.Error() << "Expected single argument for AS_TABLE source";
- return TMaybe<TSourcePtr>(nullptr);
- }
- if (node.HasBlock4()) {
- Ctx.Error() << "No hints expected for AS_TABLE source";
- return TMaybe<TSourcePtr>(nullptr);
- }
- auto arg = TableArgImpl(alt.GetBlock3().GetRule_table_arg1());
- if (!arg) {
- return TMaybe<TSourcePtr>(nullptr);
- }
- if (arg->Expr->GetSource()) {
- Ctx.Error() << "AS_TABLE shouldn't be used for table sources";
- return TMaybe<TSourcePtr>(nullptr);
- }
- return BuildNodeSource(Ctx.Pos(), arg->Expr, true, Ctx.EmitTableSource);
- }
- }
- return Nothing();
- }
- TMaybe<TColumnConstraints> ColumnConstraints(const TRule_column_schema& node, TTranslation& ctx) {
- TNodePtr defaultExpr = nullptr;
- bool nullable = true;
- auto constraintsNode = node.GetRule_opt_column_constraints4();
- if (constraintsNode.HasBlock1()) {
- nullable = !constraintsNode.GetBlock1().HasBlock1();
- }
- if (constraintsNode.HasBlock2()) {
- TSqlExpression expr(ctx.Context(), ctx.Context().Settings.Mode);
- defaultExpr = expr.Build(constraintsNode.GetBlock2().GetRule_expr2());
- if (!defaultExpr) {
- return {};
- }
- }
- return TColumnConstraints(defaultExpr, nullable);
- }
- TMaybe<TColumnSchema> TSqlTranslation::ColumnSchemaImpl(const TRule_column_schema& node) {
- const TString name(Id(node.GetRule_an_id_schema1(), *this));
- const TPosition pos(Context().Pos());
- TNodePtr type = SerialTypeNode(node.GetRule_type_name_or_bind2());
- const bool serial = (type != nullptr);
- const auto constraints = ColumnConstraints(node, *this);
- if (!constraints){
- return {};
- }
- if (!type) {
- type = TypeNodeOrBind(node.GetRule_type_name_or_bind2());
- }
- if (!type) {
- return {};
- }
- TVector<TIdentifier> families;
- if (node.HasBlock3()) {
- const auto& familyRelation = node.GetBlock3().GetRule_family_relation1();
- families.push_back(IdEx(familyRelation.GetRule_an_id2(), *this));
- }
- return TColumnSchema(pos, name, type, constraints->Nullable, families, serial, constraints->DefaultExpr);
- }
- TNodePtr TSqlTranslation::SerialTypeNode(const TRule_type_name_or_bind& node) {
- if (node.Alt_case() != TRule_type_name_or_bind::kAltTypeNameOrBind1) {
- return nullptr;
- }
- TPosition pos = Ctx.Pos();
- auto typeNameNode = node.GetAlt_type_name_or_bind1().GetRule_type_name1();
- if (typeNameNode.Alt_case() != TRule_type_name::kAltTypeName2) {
- return nullptr;
- }
- auto alt = typeNameNode.GetAlt_type_name2();
- auto& block = alt.GetBlock1();
- if (block.Alt_case() != TRule_type_name::TAlt2::TBlock1::kAlt2) {
- return nullptr;
- }
- auto alt2 = block.GetAlt2().GetRule_type_name_simple1();
- const TString name = Id(alt2.GetRule_an_id_pure1(), *this);
- if (name.empty()) {
- return nullptr;
- }
- const auto res = to_lower(name);
- if (res == "bigserial" || res == "serial8") {
- return new TCallNodeImpl(pos, "DataType", { BuildQuotedAtom(pos, "Int64", TNodeFlags::Default) });
- } else if (res == "serial" || res == "serial4") {
- return new TCallNodeImpl(pos, "DataType", { BuildQuotedAtom(pos, "Int32", TNodeFlags::Default) });
- } else if (res == "smallserial" || res == "serial2") {
- return new TCallNodeImpl(pos, "DataType", { BuildQuotedAtom(pos, "Int16", TNodeFlags::Default) });
- }
- return nullptr;
- }
- bool StoreString(const TRule_family_setting_value& from, TNodePtr& to, TContext& ctx) {
- switch (from.Alt_case()) {
- case TRule_family_setting_value::kAltFamilySettingValue1: {
- // STRING_VALUE
- const TString stringValue(ctx.Token(from.GetAlt_family_setting_value1().GetToken1()));
- TNodePtr literal = BuildLiteralSmartString(ctx, stringValue);
- if (!literal) {
- return false;
- }
- to = literal;
- break;
- }
- default:
- return false;
- }
- return true;
- }
- bool StoreInt(const TRule_family_setting_value& from, TNodePtr& to, TContext& ctx) {
- switch (from.Alt_case()) {
- case TRule_family_setting_value::kAltFamilySettingValue2: {
- // integer
- TNodePtr literal = LiteralNumber(ctx, from.GetAlt_family_setting_value2().GetRule_integer1());
- if (!literal) {
- return false;
- }
- to = literal;
- break;
- }
- default:
- return false;
- }
- return true;
- }
- bool TSqlTranslation::FillFamilySettingsEntry(const TRule_family_settings_entry& settingNode, TFamilyEntry& family) {
- TIdentifier id = IdEx(settingNode.GetRule_an_id1(), *this);
- const TRule_family_setting_value& value = settingNode.GetRule_family_setting_value3();
- if (to_lower(id.Name) == "data") {
- if (!StoreString(value, family.Data, Ctx)) {
- Ctx.Error() << to_upper(id.Name) << " value should be a string literal";
- return false;
- }
- } else if (to_lower(id.Name) == "compression") {
- if (!StoreString(value, family.Compression, Ctx)) {
- Ctx.Error() << to_upper(id.Name) << " value should be a string literal";
- return false;
- }
- } else if (to_lower(id.Name) == "compression_level") {
- if (!StoreInt(value, family.CompressionLevel, Ctx)) {
- Ctx.Error() << to_upper(id.Name) << " value should be an integer";
- return false;
- }
- } else {
- Ctx.Error() << "Unknown table setting: " << id.Name;
- return false;
- }
- return true;
- }
- bool TSqlTranslation::FillFamilySettings(const TRule_family_settings& settingsNode, TFamilyEntry& family) {
- // family_settings: LPAREN (family_settings_entry (COMMA family_settings_entry)*)? RPAREN;
- if (settingsNode.HasBlock2()) {
- auto& settings = settingsNode.GetBlock2();
- if (!FillFamilySettingsEntry(settings.GetRule_family_settings_entry1(), family)) {
- return false;
- }
- for (auto& block : settings.GetBlock2()) {
- if (!FillFamilySettingsEntry(block.GetRule_family_settings_entry2(), family)) {
- return false;
- }
- }
- }
- return true;
- }
- bool TSqlTranslation::CreateTableEntry(const TRule_create_table_entry& node, TCreateTableParameters& params, const bool isCreateTableAs)
- {
- switch (node.Alt_case()) {
- case TRule_create_table_entry::kAltCreateTableEntry1:
- {
- if (isCreateTableAs) {
- Ctx.Error() << "Column types are not supported for CREATE TABLE AS";
- return false;
- }
- // column_schema
- auto columnSchema = ColumnSchemaImpl(node.GetAlt_create_table_entry1().GetRule_column_schema1());
- if (!columnSchema) {
- return false;
- }
- if (columnSchema->Families.size() > 1) {
- Ctx.Error() << "Several column families for a single column are not yet supported";
- return false;
- }
- params.Columns.push_back(*columnSchema);
- break;
- }
- case TRule_create_table_entry::kAltCreateTableEntry2:
- {
- // table_constraint
- auto& constraint = node.GetAlt_create_table_entry2().GetRule_table_constraint1();
- switch (constraint.Alt_case()) {
- case TRule_table_constraint::kAltTableConstraint1: {
- if (!params.PkColumns.empty()) {
- Ctx.Error() << "PRIMARY KEY statement must be specified only once";
- return false;
- }
- auto& pkConstraint = constraint.GetAlt_table_constraint1();
- params.PkColumns.push_back(IdEx(pkConstraint.GetRule_an_id4(), *this));
- for (auto& block : pkConstraint.GetBlock5()) {
- params.PkColumns.push_back(IdEx(block.GetRule_an_id2(), *this));
- }
- break;
- }
- case TRule_table_constraint::kAltTableConstraint2: {
- if (!params.PartitionByColumns.empty()) {
- Ctx.Error() << "PARTITION BY statement must be specified only once";
- return false;
- }
- auto& pbConstraint = constraint.GetAlt_table_constraint2();
- params.PartitionByColumns.push_back(IdEx(pbConstraint.GetRule_an_id4(), *this));
- for (auto& block : pbConstraint.GetBlock5()) {
- params.PartitionByColumns.push_back(IdEx(block.GetRule_an_id2(), *this));
- }
- break;
- }
- case TRule_table_constraint::kAltTableConstraint3: {
- if (!params.OrderByColumns.empty()) {
- Ctx.Error() << "ORDER BY statement must be specified only once";
- return false;
- }
- auto& obConstraint = constraint.GetAlt_table_constraint3();
- auto extractDirection = [this] (const TRule_column_order_by_specification& spec, bool& desc) {
- desc = false;
- if (!spec.HasBlock2()) {
- return true;
- }
- auto& token = spec.GetBlock2().GetToken1();
- auto tokenId = token.GetId();
- if (IS_TOKEN(tokenId, ASC)) {
- return true;
- } else if (IS_TOKEN(tokenId, DESC)) {
- desc = true;
- return true;
- } else {
- Ctx.Error() << "Unsupported direction token: " << token.GetId();
- return false;
- }
- };
- bool desc = false;
- auto& obSpec = obConstraint.GetRule_column_order_by_specification4();
- if (!extractDirection(obSpec, desc)) {
- return false;
- }
- params.OrderByColumns.push_back(std::make_pair(IdEx(obSpec.GetRule_an_id1(), *this), desc));
- for (auto& block : obConstraint.GetBlock5()) {
- auto& obSpec = block.GetRule_column_order_by_specification2();
- if (!extractDirection(obSpec, desc)) {
- return false;
- }
- params.OrderByColumns.push_back(std::make_pair(IdEx(obSpec.GetRule_an_id1(), *this), desc));
- }
- break;
- }
- default:
- AltNotImplemented("table_constraint", constraint);
- return false;
- }
- break;
- }
- case TRule_create_table_entry::kAltCreateTableEntry3:
- {
- // table_index
- auto& table_index = node.GetAlt_create_table_entry3().GetRule_table_index1();
- if (!CreateTableIndex(table_index, params.Indexes)) {
- return false;
- }
- break;
- }
- case TRule_create_table_entry::kAltCreateTableEntry4:
- {
- if (isCreateTableAs) {
- Ctx.Error() << "Column families are not supported for CREATE TABLE AS";
- return false;
- }
- // family_entry
- auto& family_entry = node.GetAlt_create_table_entry4().GetRule_family_entry1();
- TFamilyEntry family(IdEx(family_entry.GetRule_an_id2(), *this));
- if (!FillFamilySettings(family_entry.GetRule_family_settings3(), family)) {
- return false;
- }
- params.ColumnFamilies.push_back(family);
- break;
- }
- case TRule_create_table_entry::kAltCreateTableEntry5:
- {
- // changefeed
- auto& changefeed = node.GetAlt_create_table_entry5().GetRule_changefeed1();
- TSqlExpression expr(Ctx, Mode);
- if (!CreateChangefeed(changefeed, expr, params.Changefeeds)) {
- return false;
- }
- break;
- }
- case TRule_create_table_entry::kAltCreateTableEntry6:
- {
- if (!isCreateTableAs) {
- Ctx.Error() << "Column requires a type";
- return false;
- }
- // an_id_schema
- const TString name(Id(node.GetAlt_create_table_entry6().GetRule_an_id_schema1(), *this));
- const TPosition pos(Context().Pos());
- params.Columns.push_back(TColumnSchema(pos, name, nullptr, true, {}, false, nullptr));
- break;
- }
- default:
- AltNotImplemented("create_table_entry", node);
- return false;
- }
- return true;
- }
- namespace {
- bool StoreId(const TRule_table_setting_value& from, TMaybe<TIdentifier>& to, TTranslation& ctx) {
- switch (from.Alt_case()) {
- case TRule_table_setting_value::kAltTableSettingValue1: {
- // id
- to = IdEx(from.GetAlt_table_setting_value1().GetRule_id1(), ctx);
- break;
- }
- default:
- return false;
- }
- return true;
- }
- bool StoreString(const TRule_table_setting_value& from, TNodePtr& to, TContext& ctx) {
- switch (from.Alt_case()) {
- case TRule_table_setting_value::kAltTableSettingValue2: {
- // STRING_VALUE
- const TString stringValue(ctx.Token(from.GetAlt_table_setting_value2().GetToken1()));
- to = BuildLiteralSmartString(ctx, stringValue);
- break;
- }
- default:
- return false;
- }
- return true;
- }
- bool StoreString(const TRule_table_setting_value& from, TDeferredAtom& to, TContext& ctx, const TString& errorPrefix = {}) {
- switch (from.Alt_case()) {
- case TRule_table_setting_value::kAltTableSettingValue2: {
- // STRING_VALUE
- const TString stringValue(ctx.Token(from.GetAlt_table_setting_value2().GetToken1()));
- auto unescaped = StringContent(ctx, ctx.Pos(), stringValue);
- if (!unescaped) {
- ctx.Error() << errorPrefix << " value cannot be unescaped";
- return false;
- }
- to = TDeferredAtom(ctx.Pos(), unescaped->Content);
- break;
- }
- default:
- ctx.Error() << errorPrefix << " value should be a string literal";
- return false;
- }
- return true;
- }
- bool StoreInt(const TRule_table_setting_value& from, TNodePtr& to, TContext& ctx) {
- switch (from.Alt_case()) {
- case TRule_table_setting_value::kAltTableSettingValue3: {
- // integer
- to = LiteralNumber(ctx, from.GetAlt_table_setting_value3().GetRule_integer1());
- break;
- }
- default:
- return false;
- }
- return true;
- }
- bool StoreInt(const TRule_table_setting_value& from, TDeferredAtom& to, TContext& ctx, const TString& errorPrefix = {}) {
- switch (from.Alt_case()) {
- case TRule_table_setting_value::kAltTableSettingValue3: {
- // integer
- to = TDeferredAtom(LiteralNumber(ctx, from.GetAlt_table_setting_value3().GetRule_integer1()), ctx);
- break;
- }
- default:
- ctx.Error() << errorPrefix << " value should be an integer";
- return false;
- }
- return true;
- }
- bool StoreSplitBoundary(const TRule_literal_value_list& boundary, TVector<TVector<TNodePtr>>& to,
- TSqlExpression& expr, TContext& ctx) {
- TVector<TNodePtr> boundaryKeys;
- auto first_key = expr.LiteralExpr(boundary.GetRule_literal_value2());
- if (!first_key) {
- ctx.Error() << "Empty key in partition at keys";
- return false;
- }
- if (!first_key->Expr) {
- ctx.Error() << "Identifier is not expected in partition at keys";
- return false;
- }
- boundaryKeys.emplace_back(first_key->Expr);
- for (auto& key : boundary.GetBlock3()) {
- auto keyExprOrIdent = expr.LiteralExpr(key.GetRule_literal_value2());
- if (!keyExprOrIdent) {
- ctx.Error() << "Empty key in partition at keys";
- return false;
- }
- if (!keyExprOrIdent->Expr) {
- ctx.Error() << "Identifier is not expected in partition at keys";
- return false;
- }
- boundaryKeys.emplace_back(keyExprOrIdent->Expr);
- }
- to.push_back(boundaryKeys);
- return true;
- }
- bool StoreSplitBoundaries(const TRule_table_setting_value& from, TVector<TVector<TNodePtr>>& to,
- TSqlExpression& expr, TContext& ctx) {
- switch (from.Alt_case()) {
- case TRule_table_setting_value::kAltTableSettingValue4: {
- // split_boundaries
- const auto& boundariesNode = from.GetAlt_table_setting_value4().GetRule_split_boundaries1();
- switch (boundariesNode.Alt_case()) {
- case TRule_split_boundaries::kAltSplitBoundaries1: {
- // literal_value_list (COMMA literal_value_list)*
- auto& complexBoundaries = boundariesNode.GetAlt_split_boundaries1();
- auto& first_boundary = complexBoundaries.GetRule_literal_value_list2();
- if (!StoreSplitBoundary(first_boundary, to, expr, ctx)) {
- return false;
- }
- for (auto& boundary : complexBoundaries.GetBlock3()) {
- if (!StoreSplitBoundary(boundary.GetRule_literal_value_list2(), to, expr, ctx)) {
- return false;
- }
- }
- break;
- }
- case TRule_split_boundaries::kAltSplitBoundaries2: {
- // literal_value_list
- auto& simpleBoundaries = boundariesNode.GetAlt_split_boundaries2().GetRule_literal_value_list1();
- auto first_key = expr.LiteralExpr(simpleBoundaries.GetRule_literal_value2());
- if (!first_key) {
- ctx.Error() << "Empty key in partition at keys";
- return false;
- }
- if (!first_key->Expr) {
- ctx.Error() << "Identifier is not expected in partition at keys";
- return false;
- }
- to.push_back(TVector<TNodePtr>(1, first_key->Expr));
- for (auto& key : simpleBoundaries.GetBlock3()) {
- auto keyExprOrIdent = expr.LiteralExpr(key.GetRule_literal_value2());
- if (!keyExprOrIdent) {
- ctx.Error() << "Empty key in partition at keys";
- return false;
- }
- if (!first_key->Expr) {
- ctx.Error() << "Identifier is not expected in partition at keys";
- return false;
- }
- to.push_back(
- TVector<TNodePtr>(1, keyExprOrIdent->Expr)
- );
- }
- break;
- }
- default:
- return false;
- }
- break;
- }
- default:
- return false;
- }
- return true;
- }
- bool FillTieringInterval(const TRule_expr& from, TNodePtr& tieringInterval, TSqlExpression& expr, TContext& ctx) {
- auto exprNode = expr.Build(from);
- if (!exprNode) {
- return false;
- }
- if (exprNode->GetOpName() != "Interval") {
- ctx.Error() << "Literal of Interval type is expected for TTL";
- return false;
- }
- tieringInterval = exprNode;
- return true;
- }
- bool FillTierAction(const TRule_ttl_tier_action& from, std::optional<TIdentifier>& storageName, TTranslation& txc) {
- switch (from.GetAltCase()) {
- case TRule_ttl_tier_action::kAltTtlTierAction1:
- storageName = IdEx(from.GetAlt_ttl_tier_action1().GetRule_an_id5(), txc);
- break;
- case TRule_ttl_tier_action::kAltTtlTierAction2:
- storageName.reset();
- break;
- case TRule_ttl_tier_action::ALT_NOT_SET:
- Y_ABORT("You should change implementation according to grammar changes");
- }
- return true;
- }
- bool StoreTtlSettings(const TRule_table_setting_value& from, TResetableSetting<TTtlSettings, void>& to, TSqlExpression& expr, TContext& ctx,
- TTranslation& txc) {
- switch (from.Alt_case()) {
- case TRule_table_setting_value::kAltTableSettingValue5: {
- auto columnName = IdEx(from.GetAlt_table_setting_value5().GetRule_an_id3(), txc);
- auto tiersLiteral = from.GetAlt_table_setting_value5().GetRule_ttl_tier_list1();
- TNodePtr firstInterval;
- if (!FillTieringInterval(tiersLiteral.GetRule_expr1(), firstInterval, expr, ctx)) {
- return false;
- }
- std::vector<TTtlSettings::TTierSettings> tiers;
- if (!tiersLiteral.HasBlock2()) {
- tiers.emplace_back(firstInterval);
- } else {
- std::optional<TIdentifier> firstStorageName;
- if (!FillTierAction(tiersLiteral.GetBlock2().GetRule_ttl_tier_action1(), firstStorageName, txc)) {
- return false;
- }
- tiers.emplace_back(firstInterval, firstStorageName);
- for (const auto& tierLiteral : tiersLiteral.GetBlock2().GetBlock2()) {
- TNodePtr intervalExpr;
- if (!FillTieringInterval(tierLiteral.GetRule_expr2(), intervalExpr, expr, ctx)) {
- return false;
- }
- std::optional<TIdentifier> storageName;
- if (!FillTierAction(tierLiteral.GetRule_ttl_tier_action3(), storageName, txc)) {
- return false;
- }
- tiers.emplace_back(intervalExpr, storageName);
- }
- }
- TMaybe<TTtlSettings::EUnit> columnUnit;
- if (from.GetAlt_table_setting_value5().HasBlock4()) {
- const TString unit = to_lower(ctx.Token(from.GetAlt_table_setting_value5().GetBlock4().GetToken2()));
- columnUnit.ConstructInPlace();
- if (!TryFromString<TTtlSettings::EUnit>(unit, *columnUnit)) {
- ctx.Error() << "Invalid unit: " << unit;
- return false;
- }
- }
- to.Set(TTtlSettings(columnName, tiers, columnUnit));
- break;
- }
- default:
- return false;
- }
- return true;
- }
- template<typename TChar>
- struct TPatternComponent {
- TBasicString<TChar> Prefix;
- TBasicString<TChar> Suffix;
- bool IsSimple = true;
- void AppendPlain(TChar c) {
- if (IsSimple) {
- Prefix.push_back(c);
- }
- Suffix.push_back(c);
- }
- void AppendAnyChar() {
- IsSimple = false;
- Suffix.clear();
- }
- };
- template<typename TChar>
- TVector<TPatternComponent<TChar>> SplitPattern(const TBasicString<TChar>& pattern, TMaybe<char> escape, bool& inEscape) {
- inEscape = false;
- TVector<TPatternComponent<TChar>> result;
- TPatternComponent<TChar> current;
- bool prevIsPercentChar = false;
- for (const TChar c : pattern) {
- if (inEscape) {
- current.AppendPlain(c);
- inEscape = false;
- prevIsPercentChar = false;
- } else if (escape && c == static_cast<TChar>(*escape)) {
- inEscape = true;
- } else if (c == '%') {
- if (!prevIsPercentChar) {
- result.push_back(std::move(current));
- }
- current = {};
- prevIsPercentChar = true;
- } else if (c == '_') {
- current.AppendAnyChar();
- prevIsPercentChar = false;
- } else {
- current.AppendPlain(c);
- prevIsPercentChar = false;
- }
- }
- result.push_back(std::move(current));
- return result;
- }
- }
- bool TSqlTranslation::StoreTableSettingsEntry(const TIdentifier& id, const TRule_table_setting_value* value,
- TTableSettings& settings, ETableType tableType, bool alter, bool reset) {
- switch (tableType) {
- case ETableType::ExternalTable:
- return StoreExternalTableSettingsEntry(id, value, settings, alter, reset);
- case ETableType::Table:
- case ETableType::TableStore:
- return StoreTableSettingsEntry(id, value, settings, alter, reset);
- }
- }
- bool TSqlTranslation::StoreExternalTableSettingsEntry(const TIdentifier& id, const TRule_table_setting_value* value,
- TTableSettings& settings, bool alter, bool reset) {
- YQL_ENSURE(value || reset);
- YQL_ENSURE(!reset || reset && alter);
- if (to_lower(id.Name) == "data_source") {
- if (reset) {
- Ctx.Error() << to_upper(id.Name) << " reset is not supported";
- return false;
- }
- TDeferredAtom dataSource;
- if (!StoreString(*value, dataSource, Ctx, to_upper(id.Name))) {
- return false;
- }
- TString service = Context().Scoped->CurrService;
- TDeferredAtom cluster = Context().Scoped->CurrCluster;
- TNodePtr root = new TAstListNodeImpl(Ctx.Pos());
- root->Add("String", Ctx.GetPrefixedPath(service, cluster, dataSource));
- settings.DataSourcePath = root;
- } else if (to_lower(id.Name) == "location") {
- if (reset) {
- settings.Location.Reset();
- } else {
- TNodePtr location;
- if (!StoreString(*value, location, Ctx)) {
- Ctx.Error() << to_upper(id.Name) << " value should be a string literal";
- return false;
- }
- settings.Location.Set(location);
- }
- } else {
- auto& setting = settings.ExternalSourceParameters.emplace_back();
- if (reset) {
- setting.Reset(id);
- } else {
- TNodePtr node;
- if (!StoreString(*value, node, Ctx)) {
- Ctx.Error() << to_upper(id.Name) << " value should be a string literal";
- return false;
- }
- setting.Set(std::pair<TIdentifier, TNodePtr>{id, std::move(node)});
- }
- }
- return true;
- }
- bool TSqlTranslation::ValidateTableSettings(const TTableSettings& settings) {
- if (settings.PartitionCount) {
- if (!settings.StoreType || to_lower(settings.StoreType->Name) != "column") {
- Ctx.Error() << " PARTITION_COUNT can be used only with STORE=COLUMN";
- return false;
- }
- }
- return true;
- }
- bool TSqlTranslation::StoreTableSettingsEntry(const TIdentifier& id, const TRule_table_setting_value* value,
- TTableSettings& settings, bool alter, bool reset) {
- YQL_ENSURE(value || reset);
- YQL_ENSURE(!reset || reset && alter);
- if (to_lower(id.Name) == "compaction_policy") {
- if (reset) {
- Ctx.Error() << to_upper(id.Name) << " reset is not supported";
- return false;
- }
- if (!StoreString(*value, settings.CompactionPolicy, Ctx)) {
- Ctx.Error() << to_upper(id.Name) << " value should be a string literal";
- return false;
- }
- } else if (to_lower(id.Name) == "auto_partitioning_by_size") {
- if (reset) {
- Ctx.Error() << to_upper(id.Name) << " reset is not supported";
- return false;
- }
- if (!StoreId(*value, settings.AutoPartitioningBySize, *this)) {
- Ctx.Error() << to_upper(id.Name) << " value should be an identifier";
- return false;
- }
- } else if (to_lower(id.Name) == "auto_partitioning_partition_size_mb") {
- if (reset) {
- Ctx.Error() << to_upper(id.Name) << " reset is not supported";
- return false;
- }
- if (!StoreInt(*value, settings.PartitionSizeMb, Ctx)) {
- Ctx.Error() << to_upper(id.Name) << " value should be an integer";
- return false;
- }
- } else if (to_lower(id.Name) == "auto_partitioning_by_load") {
- if (reset) {
- Ctx.Error() << to_upper(id.Name) << " reset is not supported";
- return false;
- }
- if (!StoreId(*value, settings.AutoPartitioningByLoad, *this)) {
- Ctx.Error() << to_upper(id.Name) << " value should be an identifier";
- return false;
- }
- } else if (to_lower(id.Name) == "auto_partitioning_min_partitions_count") {
- if (reset) {
- Ctx.Error() << to_upper(id.Name) << " reset is not supported";
- return false;
- }
- if (!StoreInt(*value, settings.MinPartitions, Ctx)) {
- Ctx.Error() << to_upper(id.Name) << " value should be an integer";
- return false;
- }
- } else if (to_lower(id.Name) == "auto_partitioning_max_partitions_count") {
- if (reset) {
- Ctx.Error() << to_upper(id.Name) << " reset is not supported";
- return false;
- }
- if (!StoreInt(*value, settings.MaxPartitions, Ctx)) {
- Ctx.Error() << to_upper(id.Name) << " value should be an integer";
- return false;
- }
- } else if (to_lower(id.Name) == "partition_count") {
- if (reset) {
- Ctx.Error() << to_upper(id.Name) << " reset is not supported";
- return false;
- }
- if (!StoreInt(*value, settings.PartitionCount, Ctx)) {
- Ctx.Error() << to_upper(id.Name) << " value should be an integer";
- return false;
- }
- } else if (to_lower(id.Name) == "uniform_partitions") {
- if (alter) {
- Ctx.Error() << to_upper(id.Name) << " alter is not supported";
- return false;
- }
- if (!StoreInt(*value, settings.UniformPartitions, Ctx)) {
- Ctx.Error() << to_upper(id.Name) << " value should be an integer";
- return false;
- }
- } else if (to_lower(id.Name) == "partition_at_keys") {
- if (alter) {
- Ctx.Error() << to_upper(id.Name) << " alter is not supported";
- return false;
- }
- TSqlExpression expr(Ctx, Mode);
- if (!StoreSplitBoundaries(*value, settings.PartitionAtKeys, expr, Ctx)) {
- Ctx.Error() << to_upper(id.Name) << " value should be a list of keys. "
- << "Example1: (10, 1000) Example2: ((10), (1000, \"abc\"))";
- return false;
- }
- } else if (to_lower(id.Name) == "key_bloom_filter") {
- if (reset) {
- Ctx.Error() << to_upper(id.Name) << " reset is not supported";
- return false;
- }
- if (!StoreId(*value, settings.KeyBloomFilter, *this)) {
- Ctx.Error() << to_upper(id.Name) << " value should be an identifier";
- return false;
- }
- } else if (to_lower(id.Name) == "read_replicas_settings") {
- if (reset) {
- Ctx.Error() << to_upper(id.Name) << " reset is not supported";
- return false;
- }
- if (!StoreString(*value, settings.ReadReplicasSettings, Ctx)) {
- Ctx.Error() << to_upper(id.Name) << " value should be a string literal";
- return false;
- }
- } else if (to_lower(id.Name) == "ttl") {
- if (!reset) {
- TSqlExpression expr(Ctx, Mode);
- if (!StoreTtlSettings(*value, settings.TtlSettings, expr, Ctx, *this)) {
- Ctx.Error() << "Invalid TTL settings";
- return false;
- }
- } else {
- settings.TtlSettings.Reset();
- }
- } else if (to_lower(id.Name) == "tiering") {
- if (!reset) {
- TNodePtr tieringNode;
- if (!StoreString(*value, tieringNode, Ctx)) {
- Ctx.Error() << to_upper(id.Name) << " value should be a string literal";
- return false;
- }
- settings.Tiering.Set(tieringNode);
- } else {
- settings.Tiering.Reset();
- }
- } else if (to_lower(id.Name) == "store") {
- if (reset) {
- Ctx.Error() << to_upper(id.Name) << " reset is not supported";
- return false;
- }
- if (!StoreId(*value, settings.StoreType, *this)) {
- Ctx.Error() << to_upper(id.Name) << " value should be an identifier";
- return false;
- }
- } else if (to_lower(id.Name) == "partition_by_hash_function") {
- if (reset) {
- Ctx.Error() << to_upper(id.Name) << " reset is not supported";
- return false;
- }
- if (!StoreString(*value, settings.PartitionByHashFunction, Ctx)) {
- Ctx.Error() << to_upper(id.Name) << " value should be a string literal";
- return false;
- }
- } else if (to_lower(id.Name) == "store_external_blobs") {
- if (reset) {
- Ctx.Error() << to_upper(id.Name) << " reset is not supported";
- return false;
- }
- if (!StoreId(*value, settings.StoreExternalBlobs, *this)) {
- Ctx.Error() << to_upper(id.Name) << " value should be an identifier";
- return false;
- }
- } else {
- Ctx.Error() << "Unknown table setting: " << id.Name;
- return false;
- }
- return ValidateTableSettings(settings);
- }
- bool TSqlTranslation::StoreTableSettingsEntry(const TIdentifier& id, const TRule_table_setting_value& value,
- TTableSettings& settings, ETableType tableType, bool alter) {
- return StoreTableSettingsEntry(id, &value, settings, tableType, alter, false);
- }
- bool TSqlTranslation::ResetTableSettingsEntry(const TIdentifier& id, TTableSettings& settings, ETableType tableType) {
- return StoreTableSettingsEntry(id, nullptr, settings, tableType, true, true);
- }
- bool TSqlTranslation::CreateTableSettings(const TRule_with_table_settings& settingsNode, TCreateTableParameters& params) {
- const auto& firstEntry = settingsNode.GetRule_table_settings_entry3();
- if (!StoreTableSettingsEntry(IdEx(firstEntry.GetRule_an_id1(), *this), firstEntry.GetRule_table_setting_value3(),
- params.TableSettings, params.TableType)) {
- return false;
- }
- for (auto& block : settingsNode.GetBlock4()) {
- const auto& entry = block.GetRule_table_settings_entry2();
- if (!StoreTableSettingsEntry(IdEx(entry.GetRule_an_id1(), *this), entry.GetRule_table_setting_value3(), params.TableSettings, params.TableType)) {
- return false;
- }
- }
- return true;
- }
- bool StoreConsumerSettingsEntry(
- const TIdentifier& id, const TRule_topic_consumer_setting_value* value, TSqlExpression& ctx,
- TTopicConsumerSettings& settings,
- bool reset
- ) {
- YQL_ENSURE(value || reset);
- TNodePtr valueExprNode;
- if (value) {
- valueExprNode = ctx.Build(value->GetRule_expr1());
- if (!valueExprNode) {
- ctx.Error() << "invalid value for setting: " << id.Name;
- return false;
- }
- }
- if (to_lower(id.Name) == "important") {
- if (settings.Important) {
- ctx.Error() << to_upper(id.Name) << " specified multiple times in ALTER CONSUMER statements for single consumer";
- return false;
- }
- if (reset) {
- ctx.Error() << to_upper(id.Name) << " reset is not supported";
- return false;
- }
- if (!valueExprNode->IsLiteral() || valueExprNode->GetLiteralType() != "Bool") {
- ctx.Error() << to_upper(id.Name) << " value should be boolean";
- return false;
- }
- settings.Important = valueExprNode;
- } else if (to_lower(id.Name) == "read_from") {
- if (settings.ReadFromTs) {
- ctx.Error() << to_upper(id.Name) << " specified multiple times in ALTER CONSUMER statements for single consumer";
- return false;
- }
- if (reset) {
- settings.ReadFromTs.Reset();
- } else {
- //ToDo: !! validate
- settings.ReadFromTs.Set(valueExprNode);
- }
- } else if (to_lower(id.Name) == "supported_codecs") {
- if (settings.SupportedCodecs) {
- ctx.Error() << to_upper(id.Name) << " specified multiple times in ALTER CONSUMER statements for single consumer";
- return false;
- }
- if (reset) {
- settings.SupportedCodecs.Reset();
- } else {
- if (!valueExprNode->IsLiteral() || valueExprNode->GetLiteralType() != "String") {
- ctx.Error() << to_upper(id.Name) << " value should be a string literal";
- return false;
- }
- settings.SupportedCodecs.Set(valueExprNode);
- }
- } else {
- ctx.Error() << to_upper(id.Name) << ": unknown option for consumer";
- return false;
- }
- return true;
- }
- TIdentifier TSqlTranslation::GetTopicConsumerId(const TRule_topic_consumer_ref& node) {
- return IdEx(node.GetRule_an_id_pure1(), *this);
- }
- bool TSqlTranslation::CreateConsumerSettings(
- const TRule_topic_consumer_settings& node, TTopicConsumerSettings& settings
- ) {
- const auto& firstEntry = node.GetRule_topic_consumer_settings_entry1();
- TSqlExpression expr(Ctx, Mode);
- if (!StoreConsumerSettingsEntry(
- IdEx(firstEntry.GetRule_an_id1(), *this),
- &firstEntry.GetRule_topic_consumer_setting_value3(),
- expr, settings, false
- )) {
- return false;
- }
- for (auto& block : node.GetBlock2()) {
- const auto& entry = block.GetRule_topic_consumer_settings_entry2();
- if (!StoreConsumerSettingsEntry(
- IdEx(entry.GetRule_an_id1(), *this),
- &entry.GetRule_topic_consumer_setting_value3(),
- expr, settings, false
- )) {
- return false;
- }
- }
- return true;
- }
- bool TSqlTranslation::CreateTopicConsumer(
- const TRule_topic_create_consumer_entry& node,
- TVector<TTopicConsumerDescription>& consumers
- ) {
- consumers.emplace_back(IdEx(node.GetRule_an_id2(), *this));
- if (node.HasBlock3()) {
- auto& settings = node.GetBlock3().GetRule_topic_consumer_with_settings1().GetRule_topic_consumer_settings3();
- if (!CreateConsumerSettings(settings, consumers.back().Settings)) {
- return false;
- }
- }
- return true;
- }
- bool TSqlTranslation::AlterTopicConsumerEntry(
- const TRule_alter_topic_alter_consumer_entry& node, TTopicConsumerDescription& alterConsumer
- ) {
- switch (node.Alt_case()) {
- case TRule_alter_topic_alter_consumer_entry::kAltAlterTopicAlterConsumerEntry1:
- return CreateConsumerSettings(
- node.GetAlt_alter_topic_alter_consumer_entry1().GetRule_topic_alter_consumer_set1()
- .GetRule_topic_consumer_settings3(),
- alterConsumer.Settings
- );
- //case TRule_alter_topic_alter_consumer_entry::ALT_NOT_SET:
- case TRule_alter_topic_alter_consumer_entry::kAltAlterTopicAlterConsumerEntry2: {
- auto& resetNode = node.GetAlt_alter_topic_alter_consumer_entry2().GetRule_topic_alter_consumer_reset1();
- TSqlExpression expr(Ctx, Mode);
- if (!StoreConsumerSettingsEntry(
- IdEx(resetNode.GetRule_an_id3(), *this),
- nullptr,
- expr, alterConsumer.Settings, true
- )) {
- return false;
- }
- for (auto& resetItem: resetNode.GetBlock4()) {
- if (!StoreConsumerSettingsEntry(
- IdEx(resetItem.GetRule_an_id2(), *this),
- nullptr,
- expr, alterConsumer.Settings, true
- )) {
- return false;
- }
- }
- return true;
- }
- default:
- Ctx.Error() << "unknown alter consumer action";
- return false;
- }
- return true;
- }
- bool TSqlTranslation::AlterTopicConsumer(
- const TRule_alter_topic_alter_consumer& node,
- THashMap<TString, TTopicConsumerDescription>& alterConsumers
- ) {
- auto consumerId = GetTopicConsumerId(node.GetRule_topic_consumer_ref3());
- TString name = to_lower(consumerId.Name);
- auto iter = alterConsumers.insert(std::make_pair(
- name, TTopicConsumerDescription(std::move(consumerId))
- )).first;
- if (!AlterTopicConsumerEntry(node.GetRule_alter_topic_alter_consumer_entry4(), iter->second)) {
- return false;
- }
- return true;
- }
- bool TSqlTranslation::CreateTopicEntry(const TRule_create_topic_entry& node, TCreateTopicParameters& params) {
- // Will need a switch() here if (ever) create_topic_entry gets more than 1 type of statement
- auto& consumer = node.GetRule_topic_create_consumer_entry1();
- if (!CreateTopicConsumer(consumer, params.Consumers)) {
- return false;
- }
- return true;
- }
- static bool StoreTopicSettingsEntry(
- const TIdentifier& id, const TRule_topic_setting_value* value, TSqlExpression& ctx,
- TTopicSettings& settings, bool reset
- ) {
- YQL_ENSURE(value || reset);
- TNodePtr valueExprNode;
- if (value) {
- valueExprNode = ctx.Build(value->GetRule_expr1());
- if (!valueExprNode) {
- ctx.Error() << "invalid value for setting: " << id.Name;
- return false;
- }
- }
- if (to_lower(id.Name) == "min_active_partitions") {
- if (reset) {
- settings.MinPartitions.Reset();
- } else {
- if (!valueExprNode->IsIntegerLiteral()) {
- ctx.Error() << to_upper(id.Name) << " value should be an integer";
- return false;
- }
- settings.MinPartitions.Set(valueExprNode);
- }
- } else if (to_lower(id.Name) == "partition_count_limit" || to_lower(id.Name) == "max_active_partitions") {
- if (reset) {
- settings.MaxPartitions.Reset();
- } else {
- if (!valueExprNode->IsIntegerLiteral()) {
- ctx.Error() << to_upper(id.Name) << " value should be an integer";
- return false;
- }
- settings.MaxPartitions.Set(valueExprNode);
- }
- } else if (to_lower(id.Name) == "retention_period") {
- if (reset) {
- settings.RetentionPeriod.Reset();
- } else {
- if (valueExprNode->GetOpName() != "Interval") {
- ctx.Error() << "Literal of Interval type is expected for retention";
- return false;
- }
- settings.RetentionPeriod.Set(valueExprNode);
- }
- } else if (to_lower(id.Name) == "retention_storage_mb") {
- if (reset) {
- settings.RetentionStorage.Reset();
- } else {
- if (!valueExprNode->IsIntegerLiteral()) {
- ctx.Error() << to_upper(id.Name) << " value should be an integer";
- return false;
- }
- settings.RetentionStorage.Set(valueExprNode);
- }
- } else if (to_lower(id.Name) == "partition_write_speed_bytes_per_second") {
- if (reset) {
- settings.PartitionWriteSpeed.Reset();
- } else {
- if (!valueExprNode->IsIntegerLiteral()) {
- ctx.Error() << to_upper(id.Name) << " value should be an integer";
- return false;
- }
- settings.PartitionWriteSpeed.Set(valueExprNode);
- }
- } else if (to_lower(id.Name) == "partition_write_burst_bytes") {
- if (reset) {
- settings.PartitionWriteBurstSpeed.Reset();
- } else {
- if (!valueExprNode->IsIntegerLiteral()) {
- ctx.Error() << to_upper(id.Name) << " value should be an integer";
- return false;
- }
- settings.PartitionWriteBurstSpeed.Set(valueExprNode);
- }
- } else if (to_lower(id.Name) == "metering_mode") {
- if (reset) {
- settings.MeteringMode.Reset();
- } else {
- if (!valueExprNode->IsLiteral() || valueExprNode->GetLiteralType() != "String") {
- ctx.Error() << to_upper(id.Name) << " value should be string";
- return false;
- }
- settings.MeteringMode.Set(valueExprNode);
- }
- } else if (to_lower(id.Name) == "supported_codecs") {
- if (reset) {
- settings.SupportedCodecs.Reset();
- } else {
- if (!valueExprNode->IsLiteral() || valueExprNode->GetLiteralType() != "String") {
- ctx.Error() << to_upper(id.Name) << " value should be string";
- return false;
- }
- settings.SupportedCodecs.Set(valueExprNode);
- }
- } else if (to_lower(id.Name) == "auto_partitioning_stabilization_window") {
- if (reset) {
- settings.AutoPartitioningStabilizationWindow.Reset();
- } else {
- if (valueExprNode->GetOpName() != "Interval") {
- ctx.Error() << "Literal of Interval type is expected for retention";
- return false;
- }
- settings.AutoPartitioningStabilizationWindow.Set(valueExprNode);
- }
- } else if (to_lower(id.Name) == "auto_partitioning_up_utilization_percent") {
- if (reset) {
- settings.AutoPartitioningUpUtilizationPercent.Reset();
- } else {
- if (!valueExprNode->IsIntegerLiteral()) {
- ctx.Error() << to_upper(id.Name) << " value should be an integer";
- return false;
- }
- settings.AutoPartitioningUpUtilizationPercent.Set(valueExprNode);
- }
- } else if (to_lower(id.Name) == "auto_partitioning_down_utilization_percent") {
- if (reset) {
- settings.AutoPartitioningDownUtilizationPercent.Reset();
- } else {
- if (!valueExprNode->IsIntegerLiteral()) {
- ctx.Error() << to_upper(id.Name) << " value should be an integer";
- return false;
- }
- settings.AutoPartitioningDownUtilizationPercent.Set(valueExprNode);
- }
- } else if (to_lower(id.Name) == "auto_partitioning_strategy") {
- if (reset) {
- settings.AutoPartitioningStrategy.Reset();
- } else {
- if (!valueExprNode->IsLiteral() || valueExprNode->GetLiteralType() != "String") {
- ctx.Error() << to_upper(id.Name) << " value should be string";
- return false;
- }
- settings.AutoPartitioningStrategy.Set(valueExprNode);
- }
- } else {
- ctx.Error() << "unknown topic setting: " << id.Name;
- return false;
- }
- return true;
- }
- bool TSqlTranslation::AlterTopicAction(const TRule_alter_topic_action& node, TAlterTopicParameters& params) {
- // alter_topic_action:
- // alter_topic_add_consumer
- // | alter_topic_alter_consumer
- // | alter_topic_drop_consumer
- // | alter_topic_set_settings
- // | alter_topic_reset_settings
- switch (node.Alt_case()) {
- case TRule_alter_topic_action::kAltAlterTopicAction1: // alter_topic_add_consumer
- return CreateTopicConsumer(
- node.GetAlt_alter_topic_action1().GetRule_alter_topic_add_consumer1()
- .GetRule_topic_create_consumer_entry2(),
- params.AddConsumers
- );
- case TRule_alter_topic_action::kAltAlterTopicAction2: // alter_topic_alter_consumer
- return AlterTopicConsumer(
- node.GetAlt_alter_topic_action2().GetRule_alter_topic_alter_consumer1(),
- params.AlterConsumers
- );
- case TRule_alter_topic_action::kAltAlterTopicAction3: // drop_consumer
- params.DropConsumers.emplace_back(GetTopicConsumerId(
- node.GetAlt_alter_topic_action3().GetRule_alter_topic_drop_consumer1()
- .GetRule_topic_consumer_ref3()
- ));
- return true;
- case TRule_alter_topic_action::kAltAlterTopicAction4: // set_settings
- return CreateTopicSettings(
- node.GetAlt_alter_topic_action4().GetRule_alter_topic_set_settings1()
- .GetRule_topic_settings3(),
- params.TopicSettings
- );
- case TRule_alter_topic_action::kAltAlterTopicAction5: { // reset_settings
- auto& resetNode = node.GetAlt_alter_topic_action5().GetRule_alter_topic_reset_settings1();
- TSqlExpression expr(Ctx, Mode);
- if (!StoreTopicSettingsEntry(
- IdEx(resetNode.GetRule_an_id3(), *this),
- nullptr, expr,
- params.TopicSettings, true
- )) {
- return false;
- }
- for (auto& resetItem: resetNode.GetBlock4()) {
- if (!StoreTopicSettingsEntry(
- IdEx(resetItem.GetRule_an_id_pure2(), *this),
- nullptr, expr,
- params.TopicSettings, true
- )) {
- return false;
- }
- }
- return true;
- }
- default:
- Ctx.Error() << "unknown alter topic action";
- return false;
- }
- return true;
- }
- bool TSqlTranslation::CreateTopicSettings(const TRule_topic_settings& node, TTopicSettings& settings) {
- const auto& firstEntry = node.GetRule_topic_settings_entry1();
- TSqlExpression expr(Ctx, Mode);
- if (!StoreTopicSettingsEntry(
- IdEx(firstEntry.GetRule_an_id1(), *this),
- &firstEntry.GetRule_topic_setting_value3(),
- expr, settings, false
- )) {
- return false;
- }
- for (auto& block : node.GetBlock2()) {
- const auto& entry = block.GetRule_topic_settings_entry2();
- if (!StoreTopicSettingsEntry(
- IdEx(entry.GetRule_an_id1(), *this),
- &entry.GetRule_topic_setting_value3(),
- expr, settings, false
- )) {
- return false;
- }
- }
- return true;
- }
- TNodePtr TSqlTranslation::IntegerOrBind(const TRule_integer_or_bind& node) {
- switch (node.Alt_case()) {
- case TRule_integer_or_bind::kAltIntegerOrBind1: {
- const TString intString = Ctx.Token(node.GetAlt_integer_or_bind1().GetRule_integer1().GetToken1());
- ui64 value;
- TString suffix;
- if (!ParseNumbers(Ctx, intString, value, suffix)) {
- return {};
- }
- return BuildQuotedAtom(Ctx.Pos(), ToString(value), TNodeFlags::ArbitraryContent);
- }
- case TRule_integer_or_bind::kAltIntegerOrBind2: {
- TString bindName;
- if (!NamedNodeImpl(node.GetAlt_integer_or_bind2().GetRule_bind_parameter1(), bindName, *this)) {
- return {};
- }
- auto namedNode = GetNamedNode(bindName);
- if (!namedNode) {
- return {};
- }
- auto atom = MakeAtomFromExpression(Ctx.Pos(), Ctx, namedNode);
- return atom.Build();
- }
- case TRule_integer_or_bind::ALT_NOT_SET:
- Y_ABORT("You should change implementation according to grammar changes");
- }
- }
- TNodePtr TSqlTranslation::TypeNameTag(const TRule_type_name_tag& node) {
- switch (node.Alt_case()) {
- case TRule_type_name_tag::kAltTypeNameTag1: {
- auto content = Id(node.GetAlt_type_name_tag1().GetRule_id1(), *this);
- auto atom = TDeferredAtom(Ctx.Pos(), content);
- return atom.Build();
- }
- case TRule_type_name_tag::kAltTypeNameTag2: {
- auto value = Token(node.GetAlt_type_name_tag2().GetToken1());
- auto parsed = StringContentOrIdContent(Ctx, Ctx.Pos(), value);
- if (!parsed) {
- return {};
- }
- auto atom = TDeferredAtom(Ctx.Pos(), parsed->Content);
- return atom.Build();
- }
- case TRule_type_name_tag::kAltTypeNameTag3: {
- TString bindName;
- if (!NamedNodeImpl(node.GetAlt_type_name_tag3().GetRule_bind_parameter1(), bindName, *this)) {
- return {};
- }
- auto namedNode = GetNamedNode(bindName);
- if (!namedNode) {
- return {};
- }
- TDeferredAtom atom;
- MakeTableFromExpression(Ctx.Pos(), Ctx, namedNode, atom);
- return atom.Build();
- }
- case TRule_type_name_tag::ALT_NOT_SET:
- Y_ABORT("You should change implementation according to grammar changes");
- }
- }
- TNodePtr TSqlTranslation::TypeSimple(const TRule_type_name_simple& node, bool onlyDataAllowed) {
- const TString origName = Id(node.GetRule_an_id_pure1(), *this);
- if (origName.empty()) {
- return {};
- }
- return BuildSimpleType(Ctx, Ctx.Pos(), origName, onlyDataAllowed);
- }
- TNodePtr TSqlTranslation::TypeDecimal(const TRule_type_name_decimal& node) {
- auto pos = Ctx.Pos();
- auto flags = TNodeFlags::Default;
- auto paramOne = IntegerOrBind(node.GetRule_integer_or_bind3());
- if (!paramOne) {
- return {};
- }
- auto paramTwo = IntegerOrBind(node.GetRule_integer_or_bind5());
- if (!paramTwo) {
- return {};
- }
- return new TCallNodeImpl(pos, "DataType", { BuildQuotedAtom(pos, "Decimal", flags), paramOne, paramTwo });
- }
- TNodePtr TSqlTranslation::AddOptionals(const TNodePtr& node, size_t optionalCount) {
- TNodePtr result = node;
- if (node) {
- TPosition pos = node->GetPos();
- for (size_t i = 0; i < optionalCount; ++i) {
- result = new TCallNodeImpl(pos, "OptionalType", { result });
- }
- }
- return result;
- }
- TMaybe<std::pair<TVector<TNodePtr>, bool>> TSqlTranslation::CallableArgList(const TRule_callable_arg_list& argList, bool namedArgsStarted) {
- auto pos = Ctx.Pos();
- auto flags = TNodeFlags::Default;
- auto& arg1 = argList.GetRule_callable_arg1();
- auto& varArg = arg1.GetRule_variant_arg1();
- TVector<TNodePtr> result;
- TVector<TNodePtr> items;
- auto typeNode = TypeNodeOrBind(varArg.GetRule_type_name_or_bind2());
- if (!typeNode) {
- return {};
- }
- items.push_back(typeNode);
- if (varArg.HasBlock1()) {
- namedArgsStarted = true;
- auto tag = TypeNameTag(varArg.GetBlock1().GetRule_type_name_tag1());
- if (!tag) {
- return {};
- }
- items.push_back(tag);
- }
- if (arg1.HasBlock2()) {
- if (!varArg.HasBlock1()) {
- items.push_back(BuildQuotedAtom(pos, "", flags));
- }
- items.push_back(BuildQuotedAtom(pos, "1", flags));
- }
- result.push_back(new TAstListNodeImpl(pos, items));
- for (auto& arg : argList.GetBlock2()) {
- auto& varArg = arg.GetRule_callable_arg2().GetRule_variant_arg1();
- TVector<TNodePtr> items;
- auto typeNode = TypeNodeOrBind(varArg.GetRule_type_name_or_bind2());
- if (!typeNode) {
- return {};
- }
- items.push_back(typeNode);
- if (varArg.HasBlock1()) {
- auto tag = TypeNameTag(varArg.GetBlock1().GetRule_type_name_tag1());
- if (!tag) {
- return {};
- }
- items.push_back(tag);
- } else {
- if (namedArgsStarted) {
- Ctx.Error() << "Expected named argument, previous argument was named";
- return {};
- }
- items.push_back(BuildQuotedAtom(pos, "", flags));
- }
- if (arg.GetRule_callable_arg2().HasBlock2()) {
- if (!varArg.HasBlock1()) {
- items.push_back(BuildQuotedAtom(pos, "", flags));
- }
- items.push_back(BuildQuotedAtom(pos, "1", flags));
- }
- result.push_back(new TAstListNodeImpl(pos, items));
- }
- return std::make_pair(result, namedArgsStarted);
- }
- TNodePtr TSqlTranslation::TypeNodeOrBind(const TRule_type_name_or_bind& node) {
- switch (node.Alt_case()) {
- case TRule_type_name_or_bind::kAltTypeNameOrBind1: {
- return TypeNode(node.GetAlt_type_name_or_bind1().GetRule_type_name1());
- }
- case TRule_type_name_or_bind::kAltTypeNameOrBind2: {
- TString bindName;
- if (!NamedNodeImpl(node.GetAlt_type_name_or_bind2().GetRule_bind_parameter1(), bindName, *this)) {
- return {};
- }
- return GetNamedNode(bindName);
- }
- case TRule_type_name_or_bind::ALT_NOT_SET:
- Y_ABORT("You should change implementation according to grammar changes");
- }
- }
- TNodePtr TSqlTranslation::TypeNode(const TRule_type_name& node) {
- //type_name:
- // type_name_composite
- // | (type_name_decimal | type_name_simple) QUESTION*;
- if (node.Alt_case() == TRule_type_name::kAltTypeName1) {
- return TypeNode(node.GetAlt_type_name1().GetRule_type_name_composite1());
- }
- TNodePtr result;
- TPosition pos = Ctx.Pos();
- auto& alt = node.GetAlt_type_name2();
- auto& block = alt.GetBlock1();
- switch (block.Alt_case()) {
- case TRule_type_name::TAlt2::TBlock1::kAlt1: {
- auto& decimalType = block.GetAlt1().GetRule_type_name_decimal1();
- result = TypeDecimal(decimalType);
- break;
- }
- case TRule_type_name::TAlt2::TBlock1::kAlt2: {
- auto& simpleType = block.GetAlt2().GetRule_type_name_simple1();
- result = TypeSimple(simpleType, false);
- break;
- }
- case TRule_type_name::TAlt2::TBlock1::ALT_NOT_SET:
- Y_ABORT("You should change implementation according to grammar changes");
- }
- return AddOptionals(result, alt.GetBlock2().size());
- }
- TNodePtr TSqlTranslation::TypeNode(const TRule_type_name_composite& node) {
- //type_name_composite:
- // ( type_name_optional
- // | type_name_tuple
- // | type_name_struct
- // | type_name_variant
- // | type_name_list
- // | type_name_stream
- // | type_name_flow
- // | type_name_dict
- // | type_name_set
- // | type_name_enum
- // | type_name_resource
- // | type_name_tagged
- // | type_name_callable
- // ) QUESTION*;
- TNodePtr result;
- TPosition pos = Ctx.Pos();
- auto flags = TNodeFlags::Default;
- auto wrapOneParamType = [&] (const TRule_type_name_or_bind& param, const char* type) -> TNodePtr {
- auto node = TypeNodeOrBind(param);
- return node ? new TAstListNodeImpl(pos, { BuildAtom(pos, type, flags), node }) : nullptr;
- };
- auto makeVoid = [&] () -> TNodePtr {
- return new TAstListNodeImpl(pos, { BuildAtom(pos, "VoidType", flags) });
- };
- auto makeQuote = [&] (const TNodePtr& node) -> TNodePtr {
- return new TAstListNodeImpl(pos, { new TAstAtomNodeImpl(pos, "quote", 0), node });
- };
- auto& block = node.GetBlock1();
- switch (block.Alt_case()) {
- case TRule_type_name_composite_TBlock1::kAlt1: {
- auto& optionalType = block.GetAlt1().GetRule_type_name_optional1();
- result = wrapOneParamType(optionalType.GetRule_type_name_or_bind3(), "OptionalType");
- break;
- }
- case TRule_type_name_composite_TBlock1::kAlt2: {
- auto& tupleType = block.GetAlt2().GetRule_type_name_tuple1();
- TVector<TNodePtr> items;
- items.push_back(BuildAtom(pos, "TupleType", flags));
- switch (tupleType.GetBlock2().Alt_case()) {
- case TRule_type_name_tuple::TBlock2::kAlt1: {
- if (tupleType.GetBlock2().GetAlt1().HasBlock2()) {
- auto typeNode = TypeNodeOrBind(tupleType.GetBlock2().GetAlt1().GetBlock2().GetRule_type_name_or_bind1());
- if (!typeNode) {
- return {};
- }
- items.push_back(typeNode);
- for (auto& arg : tupleType.GetBlock2().GetAlt1().GetBlock2().GetBlock2()) {
- auto typeNode = TypeNodeOrBind(arg.GetRule_type_name_or_bind2());
- if (!typeNode) {
- return {};
- }
- items.push_back(typeNode);
- }
- }
- [[fallthrough]]; // AUTOGENERATED_FALLTHROUGH_FIXME
- }
- case TRule_type_name_tuple::TBlock2::kAlt2:
- break;
- case TRule_type_name_tuple::TBlock2::ALT_NOT_SET:
- Y_ABORT("You should change implementation according to grammar changes");
- }
- result = new TAstListNodeImpl(pos, items);
- break;
- }
- case TRule_type_name_composite_TBlock1::kAlt3: {
- auto& structType = block.GetAlt3().GetRule_type_name_struct1();
- TVector<TNodePtr> items;
- items.push_back(BuildAtom(pos, "StructType", flags));
- switch (structType.GetBlock2().Alt_case()) {
- case TRule_type_name_struct::TBlock2::kAlt1: {
- if (structType.GetBlock2().GetAlt1().HasBlock2()) {
- auto& structArg = structType.GetBlock2().GetAlt1().GetBlock2().GetRule_struct_arg1();
- auto typeNode = TypeNodeOrBind(structArg.GetRule_type_name_or_bind3());
- if (!typeNode) {
- return {};
- }
- auto tag = TypeNameTag(structArg.GetRule_type_name_tag1());
- if (!tag) {
- return {};
- }
- items.push_back(makeQuote(new TAstListNodeImpl(pos, { tag, typeNode })));
- for (auto& arg : structType.GetBlock2().GetAlt1().GetBlock2().GetBlock2()) {
- auto typeNode = TypeNodeOrBind(arg.GetRule_struct_arg2().GetRule_type_name_or_bind3());
- if (!typeNode) {
- return {};
- }
- auto tag = TypeNameTag(arg.GetRule_struct_arg2().GetRule_type_name_tag1());
- if (!tag) {
- return {};
- }
- items.push_back(makeQuote(new TAstListNodeImpl(pos, { tag, typeNode })));
- }
- }
- [[fallthrough]]; // AUTOGENERATED_FALLTHROUGH_FIXME
- }
- case TRule_type_name_struct::TBlock2::kAlt2:
- break;
- case TRule_type_name_struct::TBlock2::ALT_NOT_SET:
- Y_ABORT("You should change implementation according to grammar changes");
- }
- result = new TAstListNodeImpl(pos, items);
- break;
- }
- case TRule_type_name_composite_TBlock1::kAlt4: {
- auto& variantType = block.GetAlt4().GetRule_type_name_variant1();
- TVector<TNodePtr> items;
- bool overStruct = false;
- auto& variantArg = variantType.GetRule_variant_arg3();
- auto typeNode = TypeNodeOrBind(variantArg.GetRule_type_name_or_bind2());
- if (!typeNode) {
- return {};
- }
- if (variantArg.HasBlock1()) {
- items.push_back(BuildAtom(pos, "StructType", flags));
- overStruct = true;
- auto tag = TypeNameTag(variantArg.GetBlock1().GetRule_type_name_tag1());
- if (!tag) {
- return {};
- }
- items.push_back(makeQuote(new TAstListNodeImpl(pos, { tag, typeNode })));
- } else {
- items.push_back(BuildAtom(pos, "TupleType", flags));
- items.push_back(typeNode);
- }
- for (auto& arg : variantType.GetBlock4()) {
- auto typeNode = TypeNodeOrBind(arg.GetRule_variant_arg2().GetRule_type_name_or_bind2());
- if (!typeNode) {
- return {};
- }
- if (overStruct) {
- if (!arg.GetRule_variant_arg2().HasBlock1()) {
- Ctx.Error() << "Variant over struct and tuple mixture";
- return {};
- }
- auto tag = TypeNameTag(arg.GetRule_variant_arg2().GetBlock1().GetRule_type_name_tag1());
- if (!tag) {
- return {};
- }
- items.push_back(makeQuote(new TAstListNodeImpl(pos, { tag, typeNode })));
- } else {
- if (arg.GetRule_variant_arg2().HasBlock1()) {
- Ctx.Error() << "Variant over struct and tuple mixture";
- return {};
- }
- items.push_back(typeNode);
- }
- }
- typeNode = new TAstListNodeImpl(pos, items);
- result = new TAstListNodeImpl(pos, { BuildAtom(pos, "VariantType", flags), typeNode });
- break;
- }
- case TRule_type_name_composite_TBlock1::kAlt5: {
- auto& listType = block.GetAlt5().GetRule_type_name_list1();
- result = wrapOneParamType(listType.GetRule_type_name_or_bind3(), "ListType");
- break;
- }
- case TRule_type_name_composite_TBlock1::kAlt6: {
- auto& streamType = block.GetAlt6().GetRule_type_name_stream1();
- result = wrapOneParamType(streamType.GetRule_type_name_or_bind3(), "StreamType");
- break;
- }
- case TRule_type_name_composite_TBlock1::kAlt7: {
- auto& flowType = block.GetAlt7().GetRule_type_name_flow1();
- result = wrapOneParamType(flowType.GetRule_type_name_or_bind3(), "FlowType");
- break;
- }
- case TRule_type_name_composite_TBlock1::kAlt8: {
- auto& dictType = block.GetAlt8().GetRule_type_name_dict1();
- TVector<TNodePtr> items;
- items.push_back(BuildAtom(pos, "DictType", flags));
- auto typeNode = TypeNodeOrBind(dictType.GetRule_type_name_or_bind3());
- if (!typeNode) {
- return {};
- }
- items.push_back(typeNode);
- typeNode = TypeNodeOrBind(dictType.GetRule_type_name_or_bind5());
- if (!typeNode) {
- return {};
- }
- items.push_back(typeNode);
- result = new TAstListNodeImpl(pos, items);
- break;
- }
- case TRule_type_name_composite_TBlock1::kAlt9: {
- auto& setType = block.GetAlt9().GetRule_type_name_set1();
- auto typeNode = TypeNodeOrBind(setType.GetRule_type_name_or_bind3());
- if (!typeNode) {
- return {};
- }
- result = new TAstListNodeImpl(pos, { BuildAtom(pos, "DictType", flags), typeNode, makeVoid() });
- break;
- }
- case TRule_type_name_composite_TBlock1::kAlt10: {
- auto& enumType = block.GetAlt10().GetRule_type_name_enum1();
- TVector<TNodePtr> items;
- items.push_back(BuildAtom(pos, "StructType", flags));
- auto tag = TypeNameTag(enumType.GetRule_type_name_tag3());
- if (!tag) {
- return {};
- }
- items.push_back(makeQuote(new TAstListNodeImpl(pos, { tag, makeVoid() })));
- for (auto& arg : enumType.GetBlock4()) {
- auto tag = TypeNameTag(arg.GetRule_type_name_tag2());
- if (!tag) {
- return {};
- }
- items.push_back(makeQuote(new TAstListNodeImpl(pos, { tag, makeVoid() })));
- }
- auto typeNode = new TAstListNodeImpl(pos, items);
- result = new TAstListNodeImpl(pos, { BuildAtom(pos, "VariantType", flags), typeNode });
- break;
- }
- case TRule_type_name_composite_TBlock1::kAlt11: {
- auto& resourceType = block.GetAlt11().GetRule_type_name_resource1();
- auto tag = TypeNameTag(resourceType.GetRule_type_name_tag3());
- if (!tag) {
- return {};
- }
- result = new TAstListNodeImpl(pos, { BuildAtom(pos, "ResourceType", flags), tag });
- break;
- }
- case TRule_type_name_composite_TBlock1::kAlt12: {
- auto& taggedType = block.GetAlt12().GetRule_type_name_tagged1();
- auto typeNode = TypeNodeOrBind(taggedType.GetRule_type_name_or_bind3());
- if (!typeNode) {
- return {};
- }
- auto tag = TypeNameTag(taggedType.GetRule_type_name_tag5());
- if (!tag) {
- return {};
- }
- result = new TAstListNodeImpl(pos, { BuildAtom(pos, "TaggedType", flags), typeNode, tag });
- break;
- }
- case TRule_type_name_composite_TBlock1::kAlt13: {
- auto& callableType = block.GetAlt13().GetRule_type_name_callable1();
- TMaybe<std::pair<TVector<TNodePtr>, bool>> requiredArgs, optionalArgs;
- bool namedArgsStarted = false;
- size_t optionalArgsCount = 0;
- if (callableType.HasBlock4()) {
- auto& argList = callableType.GetBlock4().GetRule_callable_arg_list1();
- requiredArgs = CallableArgList(argList, namedArgsStarted);
- if (!requiredArgs) {
- return {};
- }
- namedArgsStarted = requiredArgs->second;
- }
- if (callableType.HasBlock6()) {
- auto& argList = callableType.GetBlock6().GetRule_callable_arg_list2();
- optionalArgs = CallableArgList(argList, namedArgsStarted);
- if (!optionalArgs) {
- return {};
- }
- optionalArgsCount = optionalArgs->first.size();
- }
- auto returnType = TypeNodeOrBind(callableType.GetRule_type_name_or_bind9());
- if (!returnType) {
- return {};
- }
- TVector<TNodePtr> items;
- items.push_back(BuildAtom(pos, "CallableType", flags));
- if (optionalArgsCount) {
- items.push_back(makeQuote(new TAstListNodeImpl(pos,
- { BuildQuotedAtom(pos, ToString(optionalArgsCount), flags) })));
- } else {
- items.push_back(makeQuote(new TAstListNodeImpl(pos, {})));
- }
- items.push_back(makeQuote(new TAstListNodeImpl(pos, { returnType })));
- if (requiredArgs) {
- for (auto& arg: requiredArgs->first) {
- items.push_back(makeQuote(arg));
- }
- }
- if (optionalArgs) {
- for (auto& arg: optionalArgs->first) {
- items.push_back(makeQuote(arg));
- }
- }
- result = new TAstListNodeImpl(pos, items);
- break;
- }
- case TRule_type_name_composite_TBlock1::ALT_NOT_SET:
- Y_ABORT("You should change implementation according to grammar changes");
- }
- return AddOptionals(result, node.GetBlock2().size());
- }
- TNodePtr TSqlTranslation::ValueConstructorLiteral(const TRule_value_constructor_literal& node) {
- return BuildLiteralSmartString(Ctx, Token(node.GetToken1()));
- }
- TNodePtr TSqlTranslation::ValueConstructor(const TRule_value_constructor& node) {
- TSqlCallExpr call(Ctx, Mode);
- if (!call.Init(node)) {
- return {};
- }
- return call.BuildCall();
- }
- TNodePtr TSqlTranslation::ListLiteral(const TRule_list_literal& node) {
- TVector<TNodePtr> values;
- values.push_back(new TAstAtomNodeImpl(Ctx.Pos(), "AsListMayWarn", TNodeFlags::Default));
- TSqlExpression sqlExpr(Ctx, Mode);
- if (node.HasBlock2() && !ExprList(sqlExpr, values, node.GetBlock2().GetRule_expr_list1())) {
- return nullptr;
- }
- return new TAstListNodeImpl(Ctx.Pos(), std::move(values));
- }
- TNodePtr TSqlTranslation::DictLiteral(const TRule_dict_literal& node) {
- TVector<TNodePtr> values;
- if (node.HasBlock2()) {
- const auto& list = node.GetBlock2().GetRule_expr_dict_list1();
- const bool isSet = !list.HasBlock2();
- values.push_back(new TAstAtomNodeImpl(Ctx.Pos(), isSet ? "AsSet" : "AsDict", TNodeFlags::Default));
- TSqlExpression sqlExpr(Ctx, Mode);
- if (isSet) {
- if (!Expr(sqlExpr, values, list.GetRule_expr1())) {
- return nullptr;
- }
- } else {
- TVector<TNodePtr> tupleItems;
- if (!Expr(sqlExpr, tupleItems, list.GetRule_expr1())) {
- return nullptr;
- }
- if (!Expr(sqlExpr, tupleItems, list.GetBlock2().GetRule_expr2())) {
- return nullptr;
- }
- values.push_back(new TTupleNode(Ctx.Pos(), std::move(tupleItems)));
- }
- for (auto& b : list.GetBlock3()) {
- sqlExpr.Token(b.GetToken1());
- const bool isSetCurr = !b.HasBlock3();
- if (isSetCurr != isSet) {
- Error() << "Expected keys/values pair or keys, but got mix of them";
- return nullptr;
- }
- if (isSet) {
- if (!Expr(sqlExpr, values, b.GetRule_expr2())) {
- return nullptr;
- }
- } else {
- TVector<TNodePtr> tupleItems;
- if (!Expr(sqlExpr, tupleItems, b.GetRule_expr2())) {
- return nullptr;
- }
- if (!Expr(sqlExpr, tupleItems, b.GetBlock3().GetRule_expr2())) {
- return nullptr;
- }
- values.push_back(new TTupleNode(Ctx.Pos(), std::move(tupleItems)));
- }
- }
- } else {
- values.push_back(new TAstAtomNodeImpl(Ctx.Pos(), "AsDict", TNodeFlags::Default));
- }
- return new TAstListNodeImpl(Ctx.Pos(), std::move(values));
- }
- bool TSqlTranslation::StructLiteralItem(TVector<TNodePtr>& labels, const TRule_expr& label, TVector<TNodePtr>& values, const TRule_expr& value) {
- // label expr
- {
- TColumnRefScope scope(Ctx, EColumnRefState::AsStringLiteral, /* topLevel */ false);
- TSqlExpression sqlExpr(Ctx, Mode);
- if (!Expr(sqlExpr, labels, label)) {
- return false;
- }
- TDeferredAtom atom;
- MakeTableFromExpression(Ctx.Pos(), Ctx, labels.back(), atom);
- labels.back() = atom.Build();
- if (!labels.back()) {
- return false;
- }
- }
- // value expr
- {
- TSqlExpression sqlExpr(Ctx, Mode);
- if (!Expr(sqlExpr, values, value)) {
- return false;
- }
- }
- return true;
- }
- TNodePtr TSqlTranslation::StructLiteral(const TRule_struct_literal& node) {
- TVector<TNodePtr> labels;
- TVector<TNodePtr> values;
- TPosition pos = Ctx.TokenPosition(node.GetToken1());
- if (node.HasBlock2()) {
- const auto& list = node.GetBlock2().GetRule_expr_struct_list1();
- if (!StructLiteralItem(labels, list.GetRule_expr1(), values, list.GetRule_expr3())) {
- return {};
- }
- for (auto& b : list.GetBlock4()) {
- if (!StructLiteralItem(labels, b.GetRule_expr2(), values, b.GetRule_expr4())) {
- return {};
- }
- }
- }
- return BuildStructure(pos, values, labels);
- }
- bool TSqlTranslation::TableHintImpl(const TRule_table_hint& rule, TTableHints& hints, const TString& provider, const TString& keyFunc) {
- // table_hint:
- // an_id_hint (EQUALS (type_name_tag | LPAREN type_name_tag (COMMA type_name_tag)* COMMA? RPAREN))?
- // | (SCHEMA | COLUMNS) EQUALS? type_name_or_bind
- // | SCHEMA EQUALS? LPAREN (struct_arg_positional (COMMA struct_arg_positional)*)? COMMA? RPAREN
- switch (rule.Alt_case()) {
- case TRule_table_hint::kAltTableHint1: {
- const auto& alt = rule.GetAlt_table_hint1();
- const TString id = Id(alt.GetRule_an_id_hint1(), *this);
- const auto idLower = to_lower(id);
- if (idLower == "schema" || idLower == "columns") {
- Error() << "Expected type after " << to_upper(id);
- return false;
- }
- TVector<TNodePtr> hint_val;
- if (alt.HasBlock2()) {
- auto& tags = alt.GetBlock2().GetBlock2();
- switch (tags.Alt_case()) {
- case TRule_table_hint_TAlt1_TBlock2_TBlock2::kAlt1:
- hint_val.push_back(TypeNameTag(tags.GetAlt1().GetRule_type_name_tag1()));
- break;
- case TRule_table_hint_TAlt1_TBlock2_TBlock2::kAlt2: {
- hint_val.push_back(TypeNameTag(tags.GetAlt2().GetRule_type_name_tag2()));
- for (auto& tag : tags.GetAlt2().GetBlock3()) {
- hint_val.push_back(TypeNameTag(tag.GetRule_type_name_tag2()));
- }
- break;
- }
- case TRule_table_hint_TAlt1_TBlock2_TBlock2::ALT_NOT_SET:
- Y_ABORT("You should change implementation according to grammar changes");
- }
- }
- hints[id] = hint_val;
- break;
- }
- case TRule_table_hint::kAltTableHint2: {
- const auto& alt2 = rule.GetAlt_table_hint2();
- auto node = TypeNodeOrBind(alt2.GetRule_type_name_or_bind3());
- if (!node) {
- return false;
- }
- hints["user_" + to_lower(alt2.GetToken1().GetValue())] = { node };
- break;
- }
- case TRule_table_hint::kAltTableHint3: {
- const auto& alt = rule.GetAlt_table_hint3();
- TVector<TNodePtr> labels;
- TVector<TNodePtr> structTypeItems;
- if (alt.HasBlock4()) {
- bool warn = false;
- auto processItem = [&](const TRule_struct_arg_positional& arg) {
- // struct_arg_positional:
- // type_name_tag type_name_or_bind (NOT? NULL)?
- // | type_name_or_bind AS type_name_tag; //deprecated
- const bool altCurrent = arg.Alt_case() == TRule_struct_arg_positional::kAltStructArgPositional1;
- auto& typeNameOrBind = altCurrent ?
- arg.GetAlt_struct_arg_positional1().GetRule_type_name_or_bind2() :
- arg.GetAlt_struct_arg_positional2().GetRule_type_name_or_bind1();
- auto typeNode = TypeNodeOrBind(typeNameOrBind);
- if (!typeNode) {
- return false;
- }
- auto pos = Ctx.Pos();
- if (!altCurrent && !warn) {
- Ctx.Warning(pos, TIssuesIds::YQL_DEPRECATED_POSITIONAL_SCHEMA)
- << "Deprecated syntax for positional schema: please use 'column type' instead of 'type AS column'";
- warn = true;
- }
- if (altCurrent) {
- bool notNull = arg.GetAlt_struct_arg_positional1().HasBlock3() && arg.GetAlt_struct_arg_positional1().GetBlock3().HasBlock1();
- if (!notNull) {
- typeNode = new TCallNodeImpl(pos, "AsOptionalType", { typeNode });
- }
- }
- auto& typeNameTag = altCurrent ?
- arg.GetAlt_struct_arg_positional1().GetRule_type_name_tag1() :
- arg.GetAlt_struct_arg_positional2().GetRule_type_name_tag3();
- auto tag = TypeNameTag(typeNameTag);
- if (!tag) {
- return false;
- }
- labels.push_back(tag);
- structTypeItems.push_back(BuildTuple(pos, { tag, typeNode }));
- return true;
- };
- if (!processItem(alt.GetBlock4().GetRule_struct_arg_positional1())) {
- return false;
- }
- for (auto& entry : alt.GetBlock4().GetBlock2()) {
- if (!processItem(entry.GetRule_struct_arg_positional2())) {
- return false;
- }
- }
- }
- TPosition pos = Ctx.TokenPosition(alt.GetToken1());
- TNodePtr structType = new TCallNodeImpl(pos, "StructType", structTypeItems);
- bool shouldEmitLabel = provider != YtProviderName || TCiString(keyFunc) == "object";
- if (shouldEmitLabel) {
- auto labelsTuple = BuildTuple(pos, labels);
- hints["user_" + to_lower(alt.GetToken1().GetValue())] = { structType, labelsTuple };
- break;
- } else {
- hints["user_" + to_lower(alt.GetToken1().GetValue())] = { structType };
- break;
- }
- }
- case TRule_table_hint::ALT_NOT_SET:
- Y_ABORT("You should change implementation according to grammar changes");
- }
- return true;
- }
- TMaybe<TTableHints> TSqlTranslation::TableHintsImpl(const TRule_table_hints& node, const TString& provider, const TString& keyFunc) {
- TTableHints hints;
- auto& block = node.GetBlock2();
- bool hasErrors = false;
- switch (block.Alt_case()) {
- case TRule_table_hints::TBlock2::kAlt1: {
- hasErrors = !TableHintImpl(block.GetAlt1().GetRule_table_hint1(), hints, provider, keyFunc);
- break;
- }
- case TRule_table_hints::TBlock2::kAlt2: {
- hasErrors = !TableHintImpl(block.GetAlt2().GetRule_table_hint2(), hints, provider, keyFunc);
- for (const auto& x : block.GetAlt2().GetBlock3()) {
- hasErrors = hasErrors || !TableHintImpl(x.GetRule_table_hint2(), hints, provider, keyFunc);
- }
- break;
- }
- case TRule_table_hints::TBlock2::ALT_NOT_SET:
- Y_ABORT("You should change implementation according to grammar changes");
- }
- if (hasErrors) {
- return Nothing();
- }
- return hints;
- }
- bool TSqlTranslation::SimpleTableRefImpl(const TRule_simple_table_ref& node, TTableRef& result) {
- // simple_table_ref: simple_table_ref_core table_hints?;
- if (!SimpleTableRefCoreImpl(node.GetRule_simple_table_ref_core1(), result)) {
- return false;
- }
- TTableHints hints = GetContextHints(Context());
- if (node.HasBlock2()) {
- const TString& service = Context().Scoped->CurrService;
- auto tmp = TableHintsImpl(node.GetBlock2().GetRule_table_hints1(), service);
- if (!tmp) {
- Error() << "Failed to parse table hints";
- return false;
- }
- hints = *tmp;
- }
- if (!hints.empty()) {
- result.Options = BuildInputOptions(Context().Pos(), hints);
- }
- return true;
- }
- bool TSqlTranslation::SimpleTableRefCoreImpl(const TRule_simple_table_ref_core& node, TTableRef& result) {
- // simple_table_ref_core: ((cluster_expr DOT)? id_or_at) | AT? bind_parameter;
- TString service = Context().Scoped->CurrService;
- TDeferredAtom cluster = Context().Scoped->CurrCluster;
- switch (node.Alt_case()) {
- case TRule_simple_table_ref_core::AltCase::kAltSimpleTableRefCore1: {
- if (node.GetAlt_simple_table_ref_core1().GetRule_object_ref1().HasBlock1()) {
- if (Mode == NSQLTranslation::ESqlMode::LIMITED_VIEW) {
- Error() << "Cluster should not be used in limited view";
- return false;
- }
- if (!ClusterExpr(node.GetAlt_simple_table_ref_core1().GetRule_object_ref1().GetBlock1().GetRule_cluster_expr1(), false, service, cluster)) {
- return false;
- }
- }
- if (cluster.Empty()) {
- Error() << "No cluster name given and no default cluster is selected";
- return false;
- }
- result = TTableRef(Context().MakeName("table"), service, cluster, nullptr);
- auto tableOrAt = Id(node.GetAlt_simple_table_ref_core1().GetRule_object_ref1().GetRule_id_or_at2(), *this);
- auto tableAndView = TableKeyImpl(tableOrAt, {}, *this);
- result.Keys = BuildTableKey(Context().Pos(), result.Service, result.Cluster,
- TDeferredAtom(Context().Pos(), tableAndView.first), tableAndView.second);
- break;
- }
- case TRule_simple_table_ref_core::AltCase::kAltSimpleTableRefCore2: {
- if (cluster.Empty()) {
- Error() << "No cluster name given and no default cluster is selected";
- return false;
- }
- auto at = node.GetAlt_simple_table_ref_core2().HasBlock1();
- TString bindName;
- if (!NamedNodeImpl(node.GetAlt_simple_table_ref_core2().GetRule_bind_parameter2(), bindName, *this)) {
- return false;
- }
- auto named = GetNamedNode(bindName);
- if (!named) {
- return false;
- }
- TDeferredAtom table;
- MakeTableFromExpression(Context().Pos(), Context(), named, table);
- result = TTableRef(Context().MakeName("table"), service, cluster, nullptr);
- result.Keys = BuildTableKey(Context().Pos(), result.Service, result.Cluster, table, {at ? "@" : ""});
- break;
- }
- case TRule_simple_table_ref_core::AltCase::ALT_NOT_SET:
- Y_ABORT("You should change implementation according to grammar changes");
- }
- return result.Keys != nullptr;
- }
- bool TSqlTranslation::TopicRefImpl(const TRule_topic_ref& node, TTopicRef& result) {
- TString service = Context().Scoped->CurrService;
- TDeferredAtom cluster = Context().Scoped->CurrCluster;
- if (node.HasBlock1()) {
- if (Mode == NSQLTranslation::ESqlMode::LIMITED_VIEW) {
- Error() << "Cluster should not be used in limited view";
- return false;
- }
- if (!ClusterExpr(node.GetBlock1().GetRule_cluster_expr1(), false, service, cluster)) {
- return false;
- }
- }
- if (cluster.Empty()) {
- Error() << "No cluster name given and no default cluster is selected";
- return false;
- }
- result = TTopicRef(Context().MakeName("topic"), cluster, nullptr);
- auto topic = Id(node.GetRule_an_id2(), *this);
- result.Keys = BuildTopicKey(Context().Pos(), result.Cluster, TDeferredAtom(Context().Pos(), topic));
- return true;
- }
- TNodePtr TSqlTranslation::NamedNode(const TRule_named_nodes_stmt& rule, TVector<TSymbolNameWithPos>& names) {
- // named_nodes_stmt: bind_parameter_list EQUALS (expr | subselect_stmt);
- // subselect_stmt: (LPAREN select_stmt RPAREN | select_unparenthesized_stmt);
- if (!BindList(rule.GetRule_bind_parameter_list1(), names)) {
- return {};
- }
- TNodePtr nodeExpr = nullptr;
- switch (rule.GetBlock3().Alt_case()) {
- case TRule_named_nodes_stmt::TBlock3::kAlt1: {
- TSqlExpression expr(Ctx, Mode);
- auto result = expr.Build(rule.GetBlock3().GetAlt1().GetRule_expr1());
- return result;
- }
- case TRule_named_nodes_stmt::TBlock3::kAlt2:{
- const auto& subselect_rule = rule.GetBlock3().GetAlt2().GetRule_subselect_stmt1();
- TSqlSelect expr(Ctx, Mode);
- TPosition pos;
- TSourcePtr source = nullptr;
- switch (subselect_rule.GetBlock1().Alt_case()) {
- case TRule_subselect_stmt::TBlock1::kAlt1:
- source = expr.Build(subselect_rule.GetBlock1().GetAlt1().GetRule_select_stmt2(), pos);
- break;
- case TRule_subselect_stmt::TBlock1::kAlt2:
- source = expr.Build(subselect_rule.GetBlock1().GetAlt2().GetRule_select_unparenthesized_stmt1(), pos);
- break;
- case TRule_subselect_stmt::TBlock1::ALT_NOT_SET:
- AltNotImplemented("subselect_stmt", subselect_rule.GetBlock1());
- Ctx.IncrementMonCounter("sql_errors", "UnknownNamedNode");
- return nullptr;
- }
- if (!source) {
- return {};
- }
- return BuildSourceNode(pos, std::move(source));
- }
- case TRule_named_nodes_stmt::TBlock3::ALT_NOT_SET:
- AltNotImplemented("named_node", rule.GetBlock3());
- Ctx.IncrementMonCounter("sql_errors", "UnknownNamedNode");
- return nullptr;
- }
- }
- bool TSqlTranslation::ImportStatement(const TRule_import_stmt& stmt, TVector<TString>* namesPtr) {
- TVector<TString> modulePath;
- if (!ModulePath(stmt.GetRule_module_path2(), modulePath)) {
- return false;
- }
- TVector<TSymbolNameWithPos> names;
- TVector<TSymbolNameWithPos> aliases;
- if (!NamedBindList(stmt.GetRule_named_bind_parameter_list4(), names, aliases)) {
- return false;
- }
- YQL_ENSURE(names.size() == aliases.size());
- const TString moduleAlias = Ctx.AddImport(std::move(modulePath));
- if (!moduleAlias) {
- return false;
- }
- for (size_t i = 0; i < names.size(); ++i) {
- auto& name = names[i];
- auto& alias = aliases[i];
- auto& var = alias.Name ? alias : name;
- if (IsAnonymousName(var.Name)) {
- Ctx.Error(var.Pos) << "Can not import anonymous name " << var.Name;
- return false;
- }
- auto builder = [&](const TString& realName) {
- YQL_ENSURE(realName == var.Name);
- auto atom = BuildQuotedAtom(name.Pos, name.Name);
- return atom->Y("bind", moduleAlias, atom);
- };
- var.Name = PushNamedNode(var.Pos, var.Name, builder);
- if (namesPtr) {
- namesPtr->push_back(var.Name);
- }
- }
- return true;
- }
- bool TSqlTranslation::SortSpecification(const TRule_sort_specification& node, TVector<TSortSpecificationPtr>& sortSpecs) {
- bool asc = true;
- TSqlExpression expr(Ctx, Mode);
- TColumnRefScope scope(Ctx, EColumnRefState::Allow);
- TNodePtr exprNode = expr.Build(node.GetRule_expr1());
- if (!exprNode) {
- return false;
- }
- if (node.HasBlock2()) {
- const auto& token = node.GetBlock2().GetToken1();
- Token(token);
- auto tokenId = token.GetId();
- if (IS_TOKEN(tokenId, ASC)) {
- Ctx.IncrementMonCounter("sql_features", "OrderByAsc");
- } else if (IS_TOKEN(tokenId, DESC)) {
- asc = false;
- Ctx.IncrementMonCounter("sql_features", "OrderByDesc");
- } else {
- Ctx.IncrementMonCounter("sql_errors", "UnknownOrderBy");
- Error() << "Unsupported direction token: " << token.GetId();
- return false;
- }
- } else {
- Ctx.IncrementMonCounter("sql_features", "OrderByDefault");
- }
- sortSpecs.emplace_back(MakeIntrusive<TSortSpecification>(exprNode, asc));
- return true;
- }
- bool TSqlTranslation::SortSpecificationList(const TRule_sort_specification_list& node, TVector<TSortSpecificationPtr>& sortSpecs) {
- if (!SortSpecification(node.GetRule_sort_specification1(), sortSpecs)) {
- return false;
- }
- for (auto sortSpec: node.GetBlock2()) {
- Token(sortSpec.GetToken1());
- if (!SortSpecification(sortSpec.GetRule_sort_specification2(), sortSpecs)) {
- return false;
- }
- }
- return true;
- }
- bool TSqlTranslation::IsDistinctOptSet(const TRule_opt_set_quantifier& node) const {
- TPosition pos;
- return node.HasBlock1() && IS_TOKEN(node.GetBlock1().GetToken1().GetId(), DISTINCT);
- }
- bool TSqlTranslation::IsDistinctOptSet(const TRule_opt_set_quantifier& node, TPosition& distinctPos) const {
- if (node.HasBlock1() && IS_TOKEN(node.GetBlock1().GetToken1().GetId(), DISTINCT)) {
- distinctPos = Ctx.TokenPosition(node.GetBlock1().GetToken1());
- return true;
- }
- return false;
- }
- bool TSqlTranslation::RoleNameClause(const TRule_role_name& node, TDeferredAtom& result, bool allowSystemRoles) {
- // role_name: an_id_or_type | bind_parameter;
- switch (node.Alt_case()) {
- case TRule_role_name::kAltRoleName1:
- {
- TString name = Id(node.GetAlt_role_name1().GetRule_an_id_or_type1(), *this);
- result = TDeferredAtom(Ctx.Pos(), name);
- break;
- }
- case TRule_role_name::kAltRoleName2:
- {
- if (!BindParameterClause(node.GetAlt_role_name2().GetRule_bind_parameter1(), result)) {
- return false;
- }
- break;
- }
- case TRule_role_name::ALT_NOT_SET:
- Y_ABORT("You should change implementation according to grammar changes");
- }
- if (auto literalName = result.GetLiteral(); literalName && !allowSystemRoles) {
- static const THashSet<TStringBuf> systemRoles = { "current_role", "current_user", "session_user" };
- if (systemRoles.contains(to_lower(*literalName))) {
- Ctx.Error() << "System role " << to_upper(*literalName) << " can not be used here";
- return false;
- }
- }
- return true;
- }
- bool TSqlTranslation::PasswordParameter(const TRule_password_option& passwordOption, TUserParameters& result) {
- // password_option: ENCRYPTED? PASSWORD expr;
- TSqlExpression expr(Ctx, Mode);
- TNodePtr password = expr.Build(passwordOption.GetRule_expr3());
- if (!password) {
- Error() << "Couldn't parse the password";
- return false;
- }
- result.IsPasswordEncrypted = passwordOption.HasBlock1();
- if (!password->IsNull()) {
- result.Password = MakeAtomFromExpression(Ctx.Pos(), Ctx, password);
- }
- return true;
- }
- bool TSqlTranslation::HashParameter(const TRule_hash_option& hashOption, TUserParameters& result) {
- // hash_option: HASH expr;
- TSqlExpression expr(Ctx, Mode);
- TNodePtr hash = expr.Build(hashOption.GetRule_expr2());
- if (!hash) {
- Error() << "Couldn't parse the hash of password";
- return false;
- }
- if (!hash->IsNull()) {
- result.Hash = MakeAtomFromExpression(Ctx.Pos(), Ctx, hash);
- }
- return true;
- }
- void TSqlTranslation::LoginParameter(const TRule_login_option& loginOption, std::optional<bool>& canLogin) {
- // login_option: LOGIN | NOLOGIN;
- auto token = loginOption.GetToken1().GetId();
- if (IS_TOKEN(token, LOGIN)) {
- canLogin = true;
- } else if (IS_TOKEN(token, NOLOGIN)) {
- canLogin = false;
- } else {
- Y_ABORT("You should change implementation according to grammar changes");
- }
- }
- bool TSqlTranslation::UserParameters(const std::vector<TRule_user_option>& optionsList, TUserParameters& result, bool isCreateUser) {
- enum class EUserOption {
- Login,
- Authentication
- };
- std::set<EUserOption> used;
- auto ParseUserOption = [&used, this](const TRule_user_option& option, TUserParameters& result) -> bool {
- // user_option: authentication_option | login_option;
- // authentication_option: password_option | hash_option;
- switch (option.Alt_case()) {
- case TRule_user_option::kAltUserOption1:
- {
- if (used.contains(EUserOption::Authentication)) {
- Error() << "Conflicting or redundant options";
- return false;
- }
- used.insert(EUserOption::Authentication);
- const auto& authenticationOption = option.GetAlt_user_option1().GetRule_authentication_option1();
- switch (authenticationOption.Alt_case()) {
- case TRule_authentication_option::kAltAuthenticationOption1: {
- if (!PasswordParameter(authenticationOption.GetAlt_authentication_option1().GetRule_password_option1(), result)){
- return false;
- }
- break;
- }
- case TRule_authentication_option::kAltAuthenticationOption2: {
- if (!HashParameter(authenticationOption.GetAlt_authentication_option2().GetRule_hash_option1(), result)){
- return false;
- }
- break;
- }
- case TRule_authentication_option::ALT_NOT_SET: {
- Y_ABORT("You should change implementation according to grammar changes");
- }
- }
- break;
- }
- case TRule_user_option::kAltUserOption2:
- {
- if (used.contains(EUserOption::Login)) {
- Error() << "Conflicting or redundant options";
- return false;
- }
- used.insert(EUserOption::Login);
- LoginParameter(option.GetAlt_user_option2().GetRule_login_option1(), result.CanLogin);
- break;
- }
- case TRule_user_option::ALT_NOT_SET:
- {
- Y_ABORT("You should change implementation according to grammar changes");
- }
- }
- return true;
- };
- if (isCreateUser) {
- result.CanLogin = true;
- }
- for (const auto& option : optionsList) {
- if (!ParseUserOption(option, result)) {
- return false;
- }
- }
- return true;
- }
- bool TSqlTranslation::PermissionNameClause(const TRule_permission_id& node, TDeferredAtom& result) {
- // permission_id:
- // CONNECT
- // | LIST
- // | INSERT
- // | MANAGE
- // | DROP
- // | GRANT
- // | MODIFY (TABLES | ATTRIBUTES)
- // | (UPDATE | ERASE) ROW
- // | (REMOVE | DESCRIBE | ALTER) SCHEMA
- // | SELECT (TABLES | ATTRIBUTES | ROW)?
- // | (USE | FULL) LEGACY?
- // | CREATE (DIRECTORY | TABLE | QUEUE)?
- auto handleOneIdentifier = [&result, this] (const auto& permissionNameKeyword) {
- result = TDeferredAtom(Ctx.Pos(), GetIdentifier(*this, permissionNameKeyword).Name);
- };
- auto handleTwoIdentifiers = [&result, this] (const auto& permissionNameKeyword) {
- const auto& token1 = permissionNameKeyword.GetToken1();
- const auto& token2 = permissionNameKeyword.GetToken2();
- TString identifierName = TIdentifier(TPosition(token1.GetColumn(), token1.GetLine()), Identifier(token1)).Name +
- "_" +
- TIdentifier(TPosition(token2.GetColumn(), token2.GetLine()), Identifier(token2)).Name;
- result = TDeferredAtom(Ctx.Pos(), identifierName);
- };
- auto handleOneOrTwoIdentifiers = [&result, this] (const auto& permissionNameKeyword) {
- TString identifierName = GetIdentifier(*this, permissionNameKeyword).Name;
- if (permissionNameKeyword.HasBlock2()) {
- identifierName += "_" + GetIdentifier(*this, permissionNameKeyword.GetBlock2()).Name;
- }
- result = TDeferredAtom(Ctx.Pos(), identifierName);
- };
- switch (node.GetAltCase()) {
- case TRule_permission_id::kAltPermissionId1:
- {
- // CONNECT
- handleOneIdentifier(node.GetAlt_permission_id1());
- break;
- }
- case TRule_permission_id::kAltPermissionId2:
- {
- // LIST
- handleOneIdentifier(node.GetAlt_permission_id2());
- break;
- }
- case TRule_permission_id::kAltPermissionId3:
- {
- // INSERT
- handleOneIdentifier(node.GetAlt_permission_id3());
- break;
- }
- case TRule_permission_id::kAltPermissionId4:
- {
- // MANAGE
- handleOneIdentifier(node.GetAlt_permission_id4());
- break;
- }
- case TRule_permission_id::kAltPermissionId5:
- {
- // DROP
- handleOneIdentifier(node.GetAlt_permission_id5());
- break;
- }
- case TRule_permission_id::kAltPermissionId6:
- {
- // GRANT
- handleOneIdentifier(node.GetAlt_permission_id6());
- break;
- }
- case TRule_permission_id::kAltPermissionId7:
- {
- // MODIFY (TABLES | ATTRIBUTES)
- handleTwoIdentifiers(node.GetAlt_permission_id7());
- break;
- }
- case TRule_permission_id::kAltPermissionId8:
- {
- // (UPDATE | ERASE) ROW
- handleTwoIdentifiers(node.GetAlt_permission_id8());
- break;
- }
- case TRule_permission_id::kAltPermissionId9:
- {
- // (REMOVE | DESCRIBE | ALTER) SCHEMA
- handleTwoIdentifiers(node.GetAlt_permission_id9());
- break;
- }
- case TRule_permission_id::kAltPermissionId10:
- {
- // SELECT (TABLES | ATTRIBUTES | ROW)?
- handleOneOrTwoIdentifiers(node.GetAlt_permission_id10());
- break;
- }
- case TRule_permission_id::kAltPermissionId11:
- {
- // (USE | FULL) LEGACY?
- handleOneOrTwoIdentifiers(node.GetAlt_permission_id11());
- break;
- }
- case TRule_permission_id::kAltPermissionId12:
- {
- // CREATE (DIRECTORY | TABLE | QUEUE)?
- handleOneOrTwoIdentifiers(node.GetAlt_permission_id12());
- break;
- }
- case TRule_permission_id::ALT_NOT_SET:
- Y_ABORT("You should change implementation according to grammar changes");
- }
- return true;
- }
- bool TSqlTranslation::PermissionNameClause(const TRule_permission_name& node, TDeferredAtom& result) {
- // permission_name: permission_id | STRING_VALUE;
- switch (node.Alt_case()) {
- case TRule_permission_name::kAltPermissionName1:
- {
- return PermissionNameClause(node.GetAlt_permission_name1().GetRule_permission_id1(), result);
- break;
- }
- case TRule_permission_name::kAltPermissionName2:
- {
- const TString stringValue(Ctx.Token(node.GetAlt_permission_name2().GetToken1()));
- auto unescaped = StringContent(Ctx, Ctx.Pos(), stringValue);
- if (!unescaped) {
- return false;
- }
- result = TDeferredAtom(Ctx.Pos(), unescaped->Content);
- break;
- }
- case TRule_permission_name::ALT_NOT_SET:
- Y_ABORT("You should change implementation according to grammar changes");
- }
- return true;
- }
- bool TSqlTranslation::PermissionNameClause(const TRule_permission_name_target& node, TVector<TDeferredAtom>& result, bool withGrantOption) {
- // permission_name_target: permission_name (COMMA permission_name)* COMMA? | ALL PRIVILEGES?;
- switch (node.Alt_case()) {
- case TRule_permission_name_target::kAltPermissionNameTarget1:
- {
- const auto& permissionNameRule = node.GetAlt_permission_name_target1();
- result.emplace_back();
- if (!PermissionNameClause(permissionNameRule.GetRule_permission_name1(), result.back())) {
- return false;
- }
- for (const auto& item : permissionNameRule.GetBlock2()) {
- result.emplace_back();
- if (!PermissionNameClause(item.GetRule_permission_name2(), result.back())) {
- return false;
- }
- }
- break;
- }
- case TRule_permission_name_target::kAltPermissionNameTarget2:
- {
- result.emplace_back(Ctx.Pos(), "all_privileges");
- break;
- }
- case TRule_permission_name_target::ALT_NOT_SET:
- Y_ABORT("You should change implementation according to grammar changes");
- }
- if (withGrantOption) {
- result.emplace_back(Ctx.Pos(), "grant");
- }
- return true;
- }
- bool TSqlTranslation::StoreStringSettingsEntry(const TIdentifier& id, const TRule_table_setting_value* value, std::map<TString, TDeferredAtom>& result) {
- YQL_ENSURE(value);
- const TString key = to_lower(id.Name);
- if (result.find(key) != result.end()) {
- Ctx.Error() << to_upper(key) << " duplicate keys";
- return false;
- }
- switch (value->Alt_case()) {
- case TRule_table_setting_value::kAltTableSettingValue2:
- return StoreString(*value, result[key], Ctx, to_upper(key));
- default:
- Ctx.Error() << to_upper(key) << " value should be a string literal";
- return false;
- }
- return true;
- }
- bool TSqlTranslation::StoreStringSettingsEntry(const TRule_alter_table_setting_entry& entry, std::map<TString, TDeferredAtom>& result) {
- const TIdentifier id = IdEx(entry.GetRule_an_id1(), *this);
- return StoreStringSettingsEntry(id, &entry.GetRule_table_setting_value3(), result);
- }
- bool TSqlTranslation::ParseBackupCollectionSettings(std::map<TString, TDeferredAtom>& result, const TRule_backup_collection_settings& settings) {
- const auto& firstEntry = settings.GetRule_backup_collection_settings_entry1();
- if (!StoreStringSettingsEntry(IdEx(firstEntry.GetRule_an_id1(), *this), &firstEntry.GetRule_table_setting_value3(), result)) {
- return false;
- }
- for (const auto& block : settings.GetBlock2()) {
- const auto& entry = block.GetRule_backup_collection_settings_entry2();
- if (!StoreStringSettingsEntry(IdEx(entry.GetRule_an_id1(), *this), &entry.GetRule_table_setting_value3(), result)) {
- return false;
- }
- }
- return true;
- }
- bool TSqlTranslation::ParseBackupCollectionSettings(std::map<TString, TDeferredAtom>& result, std::set<TString>& toReset, const TRule_alter_backup_collection_actions& actions) {
- auto parseAction = [&](auto& actionVariant) {
- switch (actionVariant.Alt_case()) {
- case TRule_alter_backup_collection_action::kAltAlterBackupCollectionAction1: {
- const auto& action = actionVariant.GetAlt_alter_backup_collection_action1().GetRule_alter_table_set_table_setting_compat1();
- if (!StoreStringSettingsEntry(action.GetRule_alter_table_setting_entry3(), result)) {
- return false;
- }
- for (const auto& entry : action.GetBlock4()) {
- if (!StoreStringSettingsEntry(entry.GetRule_alter_table_setting_entry2(), result)) {
- return false;
- }
- }
- return true;
- }
- case TRule_alter_backup_collection_action::kAltAlterBackupCollectionAction2: {
- const auto& action = actionVariant.GetAlt_alter_backup_collection_action2().GetRule_alter_table_reset_table_setting1();
- const TString firstKey = to_lower(IdEx(action.GetRule_an_id3(), *this).Name);
- toReset.insert(firstKey);
- for (const auto& key : action.GetBlock4()) {
- toReset.insert(to_lower(IdEx(key.GetRule_an_id2(), *this).Name));
- }
- return true;
- }
- case TRule_alter_backup_collection_action::ALT_NOT_SET:
- Y_ABORT("You should change implementation according to grammar changes");
- }
- };
- const auto& firstAction = actions.GetRule_alter_backup_collection_action1();
- if (!parseAction(firstAction)) {
- return false;
- }
- for (const auto& action : actions.GetBlock2()) {
- if (!parseAction(action.GetRule_alter_backup_collection_action2())) {
- return false;
- }
- }
- return true;
- }
- bool TSqlTranslation::ParseBackupCollectionTables(TVector<TDeferredAtom>& result, const TRule_table_list& tables) {
- const auto& firstEntry = tables.GetRule_an_id_table2();
- result.push_back(TDeferredAtom(Ctx.Pos(), Id(firstEntry, *this)));
- for (const auto& block : tables.GetBlock3()) {
- const auto& entry = block.GetRule_an_id_table3();
- result.push_back(TDeferredAtom(Ctx.Pos(), Id(entry, *this)));
- }
- return true;
- }
- bool TSqlTranslation::ParseBackupCollectionEntry(
- bool& addDatabase,
- bool& removeDatabase,
- TVector<TDeferredAtom>& addTables,
- TVector<TDeferredAtom>& removeTables,
- const TRule_alter_backup_collection_entry& entry)
- {
- switch (entry.Alt_case()) {
- case TRule_alter_backup_collection_entry::kAltAlterBackupCollectionEntry1: {
- addDatabase = true;
- return true;
- }
- case TRule_alter_backup_collection_entry::kAltAlterBackupCollectionEntry2: {
- removeDatabase = true;
- return true;
- }
- case TRule_alter_backup_collection_entry::kAltAlterBackupCollectionEntry3: {
- auto table = entry.GetAlt_alter_backup_collection_entry3().GetRule_an_id_table3();
- addTables.push_back(TDeferredAtom(Ctx.Pos(), Id(table, *this)));
- return true;
- }
- case TRule_alter_backup_collection_entry::kAltAlterBackupCollectionEntry4: {
- auto table = entry.GetAlt_alter_backup_collection_entry4().GetRule_an_id_table3();
- removeTables.push_back(TDeferredAtom(Ctx.Pos(), Id(table, *this)));
- return true;
- }
- case TRule_alter_backup_collection_entry::ALT_NOT_SET:
- Y_ABORT("You should change implementation according to grammar changes");
- }
- return true;
- }
- bool TSqlTranslation::ParseBackupCollectionEntries(
- bool& addDatabase,
- bool& removeDatabase,
- TVector<TDeferredAtom>& addTables,
- TVector<TDeferredAtom>& removeTables,
- const TRule_alter_backup_collection_entries& entries)
- {
- const auto& firstEntry = entries.GetRule_alter_backup_collection_entry1();
- if (!ParseBackupCollectionEntry(addDatabase, removeDatabase, addTables, removeTables, firstEntry)) {
- return false;
- }
- for (const auto& block : entries.GetBlock2()) {
- const auto& entry = block.GetRule_alter_backup_collection_entry2();
- if (!ParseBackupCollectionEntry(addDatabase, removeDatabase, addTables, removeTables, entry)) {
- return false;
- }
- }
- return true;
- }
- TString TSqlTranslation::FrameSettingsToString(EFrameSettings settings, bool isUnbounded) {
- TString result;
- switch (settings) {
- case FramePreceding:
- result = "PRECEDING"; break;
- case FrameCurrentRow:
- YQL_ENSURE(!isUnbounded);
- result = "CURRENT ROW"; break;
- case FrameFollowing:
- result = "FOLLOWING"; break;
- default:
- Y_ABORT("Unexpected frame settings");
- }
- return (isUnbounded ? "UNBOUNDED " : "") + result;
- }
- bool CheckFrameBoundLiteral(TContext& ctx, const TFrameBound& bound, TMaybe<i32>& boundValue) {
- boundValue = {};
- auto node = bound.Bound;
- if (node && node->IsLiteral()) {
- auto type = node->GetLiteralType();
- if (type != "Int32") {
- ctx.Error(node->GetPos()) << "Expecting Int32 as frame bound value, but got " << type << " literal";
- return false;
- }
- i32 value = FromString<i32>(node->GetLiteralValue());
- if (value < 0) {
- ctx.Error(node->GetPos()) << "Expecting non-negative value for frame bound, but got " << value;
- return false;
- }
- boundValue = value;
- }
- return true;
- }
- bool TSqlTranslation::IsValidFrameSettings(TContext& ctx, const TFrameSpecification& frameSpec, size_t sortSpecSize) {
- const TFrameBound& begin = *frameSpec.FrameBegin;
- const TFrameBound& end = *frameSpec.FrameEnd;
- YQL_ENSURE(begin.Settings != FrameUndefined);
- YQL_ENSURE(end.Settings != FrameUndefined);
- const bool beginUnbounded = !begin.Bound && begin.Settings != FrameCurrentRow;
- const bool endUnbounded = !end.Bound && end.Settings != FrameCurrentRow;
- if (beginUnbounded && begin.Settings == FrameFollowing) {
- ctx.Error(begin.Pos) << "Frame cannot start from " << FrameSettingsToString(begin.Settings, beginUnbounded);
- return false;
- }
- if (endUnbounded && end.Settings == FramePreceding) {
- ctx.Error(end.Pos) << "Frame cannot end with " << FrameSettingsToString(end.Settings, endUnbounded);
- return false;
- }
- if (begin.Settings > end.Settings) {
- ctx.Error(begin.Pos) << "Frame cannot start from " << FrameSettingsToString(begin.Settings, beginUnbounded)
- << " and end with " << FrameSettingsToString(end.Settings, endUnbounded);
- return false;
- }
- if (frameSpec.FrameType == FrameByRange && sortSpecSize != 1) {
- TStringBuf msg = "RANGE with <offset> PRECEDING/FOLLOWING requires exactly one expression in ORDER BY partition clause";
- if (begin.Bound) {
- ctx.Error(begin.Bound->GetPos()) << msg;
- return false;
- }
- if (end.Bound) {
- ctx.Error(end.Bound->GetPos()) << msg;
- return false;
- }
- }
- TMaybe<i32> beginValue;
- TMaybe<i32> endValue;
- if (frameSpec.FrameType != EFrameType::FrameByRange) {
- if (!CheckFrameBoundLiteral(ctx, begin, beginValue) || !CheckFrameBoundLiteral(ctx, end, endValue)) {
- return false;
- }
- }
- if (beginValue.Defined() && endValue.Defined()) {
- if (begin.Settings == FramePreceding) {
- beginValue = 0 - *beginValue;
- }
- if (end.Settings == FramePreceding) {
- endValue = 0 - *endValue;
- }
- if (*beginValue > *endValue) {
- YQL_ENSURE(begin.Bound);
- ctx.Warning(begin.Bound->GetPos(), TIssuesIds::YQL_EMPTY_WINDOW_FRAME) << "Used frame specification implies empty window frame";
- }
- }
- return true;
- }
- bool TSqlTranslation::FrameBound(const TRule_window_frame_bound& rule, TFrameBoundPtr& bound) {
- // window_frame_bound:
- // CURRENT ROW
- // | (expr | UNBOUNDED) (PRECEDING | FOLLOWING)
- // ;
- bound = new TFrameBound;
- switch (rule.Alt_case()) {
- case TRule_window_frame_bound::kAltWindowFrameBound1:
- bound->Pos = GetPos(rule.GetAlt_window_frame_bound1().GetToken1());
- bound->Settings = FrameCurrentRow;
- break;
- case TRule_window_frame_bound::kAltWindowFrameBound2: {
- auto block = rule.GetAlt_window_frame_bound2().GetBlock1();
- switch (block.Alt_case()) {
- case TRule_window_frame_bound_TAlt2_TBlock1::kAlt1: {
- TSqlExpression boundExpr(Ctx, Mode);
- bound->Bound = boundExpr.Build(block.GetAlt1().GetRule_expr1());
- if (!bound->Bound) {
- return false;
- }
- bound->Pos = bound->Bound->GetPos();
- break;
- }
- case TRule_window_frame_bound_TAlt2_TBlock1::kAlt2:
- bound->Pos = GetPos(block.GetAlt2().GetToken1());
- break;
- case TRule_window_frame_bound_TAlt2_TBlock1::ALT_NOT_SET:
- Y_ABORT("You should change implementation according to grammar changes");
- }
- const TString settingToken = to_lower(Token(rule.GetAlt_window_frame_bound2().GetToken2()));
- if (settingToken == "preceding") {
- bound->Settings = FramePreceding;
- } else if (settingToken == "following") {
- bound->Settings = FrameFollowing;
- } else {
- Y_ABORT("You should change implementation according to grammar changes");
- }
- break;
- }
- case TRule_window_frame_bound::ALT_NOT_SET:
- Y_ABORT("FrameClause: frame bound not corresond to grammar changes");
- }
- return true;
- }
- bool TSqlTranslation::FrameClause(const TRule_window_frame_clause& rule, TFrameSpecificationPtr& frameSpec, size_t sortSpecSize) {
- // window_frame_clause: window_frame_units window_frame_extent window_frame_exclusion?;
- frameSpec = new TFrameSpecification;
- const TString frameUnitStr = to_lower(Token(rule.GetRule_window_frame_units1().GetToken1()));
- if (frameUnitStr == "rows") {
- frameSpec->FrameType = EFrameType::FrameByRows;
- } else if (frameUnitStr == "range") {
- frameSpec->FrameType = EFrameType::FrameByRange;
- } else {
- YQL_ENSURE(frameUnitStr == "groups");
- frameSpec->FrameType = EFrameType::FrameByGroups;
- }
- auto frameExtent = rule.GetRule_window_frame_extent2();
- // window_frame_extent: window_frame_bound | window_frame_between;
- switch (frameExtent.Alt_case()) {
- case TRule_window_frame_extent::kAltWindowFrameExtent1: {
- auto start = frameExtent.GetAlt_window_frame_extent1().GetRule_window_frame_bound1();
- if (!FrameBound(start, frameSpec->FrameBegin)) {
- return false;
- }
- // frame end is CURRENT ROW
- frameSpec->FrameEnd = new TFrameBound;
- frameSpec->FrameEnd->Pos = frameSpec->FrameBegin->Pos;
- frameSpec->FrameEnd->Settings = FrameCurrentRow;
- break;
- }
- case TRule_window_frame_extent::kAltWindowFrameExtent2: {
- // window_frame_between: BETWEEN window_frame_bound AND window_frame_bound;
- auto between = frameExtent.GetAlt_window_frame_extent2().GetRule_window_frame_between1();
- if (!FrameBound(between.GetRule_window_frame_bound2(), frameSpec->FrameBegin)) {
- return false;
- }
- if (!FrameBound(between.GetRule_window_frame_bound4(), frameSpec->FrameEnd)) {
- return false;
- }
- break;
- }
- case TRule_window_frame_extent::ALT_NOT_SET:
- Y_ABORT("FrameClause: frame extent not correspond to grammar changes");
- }
- YQL_ENSURE(frameSpec->FrameBegin);
- YQL_ENSURE(frameSpec->FrameEnd);
- if (!IsValidFrameSettings(Ctx, *frameSpec, sortSpecSize)) {
- return false;
- }
- if (rule.HasBlock3()) {
- // window_frame_exclusion: EXCLUDE CURRENT ROW | EXCLUDE GROUP | EXCLUDE TIES | EXCLUDE NO OTHERS;
- switch (rule.GetBlock3().GetRule_window_frame_exclusion1().Alt_case()) {
- case TRule_window_frame_exclusion::kAltWindowFrameExclusion1:
- frameSpec->FrameExclusion = FrameExclCurRow;
- break;
- case TRule_window_frame_exclusion::kAltWindowFrameExclusion2:
- frameSpec->FrameExclusion = FrameExclGroup;
- break;
- case TRule_window_frame_exclusion::kAltWindowFrameExclusion3:
- frameSpec->FrameExclusion = FrameExclTies;
- break;
- case TRule_window_frame_exclusion::kAltWindowFrameExclusion4:
- frameSpec->FrameExclusion = FrameExclNone;
- break;
- case TRule_window_frame_exclusion::ALT_NOT_SET:
- Y_ABORT("FrameClause: frame exclusion not correspond to grammar changes");
- }
- }
- if (frameSpec->FrameExclusion != FrameExclNone) {
- Ctx.Error() << "Frame exclusion is not supported yet";
- return false;
- }
- return true;
- }
- TWindowSpecificationPtr TSqlTranslation::WindowSpecification(const TRule_window_specification_details& rule) {
- /*
- window_specification_details:
- existing_window_name?
- window_partition_clause?
- window_order_clause?
- window_frame_clause?
- */
- TWindowSpecificationPtr winSpecPtr = new TWindowSpecification;
- if (rule.HasBlock1()) {
- Ctx.Error() << "Existing window name is not supported in window specification yet!";
- return {};
- }
- if (rule.HasBlock2()) {
- /*
- window_partition_clause: PARTITION COMPACT? BY named_expr_list;
- */
- auto& partitionClause = rule.GetBlock2().GetRule_window_partition_clause1();
- winSpecPtr->IsCompact = partitionClause.HasBlock2();
- if (!winSpecPtr->IsCompact) {
- auto hints = Ctx.PullHintForToken(Ctx.TokenPosition(partitionClause.GetToken1()));
- winSpecPtr->IsCompact = AnyOf(hints, [](const NSQLTranslation::TSQLHint& hint) { return to_lower(hint.Name) == "compact"; });
- }
- TColumnRefScope scope(Ctx, EColumnRefState::Allow);
- if (!NamedExprList(partitionClause.GetRule_named_expr_list4(), winSpecPtr->Partitions)) {
- return {};
- }
- // ignore empty unnamed tuples:
- // "PARTITION BY (), foo(x) as y, (), (z)" is allowed and will work exactly the same as
- // "PARTITION BY foo(x) as y, z"
- auto removed = std::remove_if(winSpecPtr->Partitions.begin(), winSpecPtr->Partitions.end(),
- [](const TNodePtr& partitionNode) {
- return !partitionNode->GetLabel() && !partitionNode->GetColumnName() &&
- partitionNode->GetTupleNode() != nullptr &&
- partitionNode->GetTupleSize() == 0;
- });
- winSpecPtr->Partitions.erase(removed, winSpecPtr->Partitions.end());
- }
- if (rule.HasBlock3()) {
- if (!OrderByClause(rule.GetBlock3().GetRule_window_order_clause1().GetRule_order_by_clause1(), winSpecPtr->OrderBy)) {
- return {};
- }
- }
- const bool ordered = !winSpecPtr->OrderBy.empty();
- if (rule.HasBlock4()) {
- if (!FrameClause(rule.GetBlock4().GetRule_window_frame_clause1(), winSpecPtr->Frame, winSpecPtr->OrderBy.size())) {
- return {};
- }
- } else {
- winSpecPtr->Frame = new TFrameSpecification;
- winSpecPtr->Frame->FrameBegin = new TFrameBound;
- winSpecPtr->Frame->FrameEnd = new TFrameBound;
- winSpecPtr->Frame->FrameBegin->Pos = winSpecPtr->Frame->FrameEnd->Pos = Ctx.Pos();
- winSpecPtr->Frame->FrameExclusion = EFrameExclusions::FrameExclNone;
- winSpecPtr->Frame->FrameBegin->Settings = EFrameSettings::FramePreceding;
- if (Ctx.AnsiCurrentRow) {
- // RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
- winSpecPtr->Frame->FrameType = EFrameType::FrameByRange;
- winSpecPtr->Frame->FrameEnd->Settings = EFrameSettings::FrameCurrentRow;
- } else if (ordered) {
- // legacy behavior
- // ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
- winSpecPtr->Frame->FrameType = EFrameType::FrameByRows;
- winSpecPtr->Frame->FrameEnd->Settings = EFrameSettings::FrameCurrentRow;
- } else {
- // ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
- winSpecPtr->Frame->FrameType = EFrameType::FrameByRows;
- winSpecPtr->Frame->FrameEnd->Settings = EFrameSettings::FrameFollowing;
- }
- }
- // Normalize and simplify
- auto replaceCurrentWith = [](TFrameBound& frame, bool preceding, TNodePtr value ) {
- frame.Settings = preceding ? EFrameSettings::FramePreceding : EFrameSettings::FrameFollowing;
- frame.Bound = value;
- };
- const auto frameSpec = winSpecPtr->Frame;
- if (!ordered && frameSpec->FrameType != EFrameType::FrameByRows) {
- // CURRENT ROW -> UNBOUNDED
- if (frameSpec->FrameBegin->Settings == EFrameSettings::FrameCurrentRow) {
- replaceCurrentWith(*frameSpec->FrameBegin, true, nullptr);
- }
- if (frameSpec->FrameEnd->Settings == EFrameSettings::FrameCurrentRow) {
- replaceCurrentWith(*frameSpec->FrameBegin, false, nullptr);
- }
- }
- // RANGE/GROUPS UNBOUNDED -> ROWS UNBOUNDED
- if (frameSpec->FrameBegin->Settings == EFrameSettings::FramePreceding && !frameSpec->FrameBegin->Bound &&
- frameSpec->FrameEnd->Settings == EFrameSettings::FrameFollowing && !frameSpec->FrameEnd->Bound)
- {
- frameSpec->FrameType = EFrameType::FrameByRows;
- }
- if (frameSpec->FrameType != EFrameType::FrameByRange) {
- // replace FrameCurrentRow for ROWS/GROUPS with 0 preceding/following
- // FrameCurrentRow has special meaning ( = first/last peer row)
- if (frameSpec->FrameBegin->Settings == EFrameSettings::FrameCurrentRow) {
- TNodePtr zero = new TLiteralNumberNode<i32>(winSpecPtr->Frame->FrameBegin->Pos, "Int32", "0");
- replaceCurrentWith(*frameSpec->FrameBegin, true, zero);
- }
- if (frameSpec->FrameEnd->Settings == EFrameSettings::FrameCurrentRow) {
- TNodePtr zero = new TLiteralNumberNode<i32>(winSpecPtr->Frame->FrameEnd->Pos, "Int32", "0");
- replaceCurrentWith(*frameSpec->FrameEnd, false, zero);
- }
- }
- return winSpecPtr;
- }
- TNodePtr TSqlTranslation::DoStatement(const TRule_do_stmt& stmt, bool makeLambda, const TVector<TString>& args) {
- switch (stmt.GetBlock2().Alt_case()) {
- case TRule_do_stmt_TBlock2::kAlt1: {
- const auto& callAction = stmt.GetBlock2().GetAlt1().GetRule_call_action1();
- TNodePtr action;
- switch (callAction.GetBlock1().GetAltCase()) {
- case TRule_call_action_TBlock1::kAlt1: {
- TString bindName;
- if (!NamedNodeImpl(callAction.GetBlock1().GetAlt1().GetRule_bind_parameter1(), bindName, *this)) {
- return nullptr;
- }
- action = GetNamedNode(bindName);
- if (!action) {
- return nullptr;
- }
- break;
- }
- case TRule_call_action_TBlock1::kAlt2:
- action = BuildEmptyAction(Ctx.Pos());
- break;
- case TRule_call_action_TBlock1::ALT_NOT_SET:
- Ctx.IncrementMonCounter("sql_errors", "UnknownDoStmt");
- AltNotImplemented("do_stmt", callAction.GetBlock1());
- return nullptr;
- }
- TVector<TNodePtr> values;
- values.push_back(new TAstAtomNodeImpl(Ctx.Pos(), "Apply", TNodeFlags::Default));
- values.push_back(action);
- values.push_back(new TAstAtomNodeImpl(Ctx.Pos(), "world", TNodeFlags::Default));
- TSqlExpression sqlExpr(Ctx, Mode);
- if (callAction.HasBlock3() && !ExprList(sqlExpr, values, callAction.GetBlock3().GetRule_expr_list1())) {
- return nullptr;
- }
- TNodePtr apply = new TAstListNodeImpl(Ctx.Pos(), std::move(values));
- if (!makeLambda) {
- return BuildDoCall(Ctx.Pos(), apply);
- }
- TNodePtr params = new TAstListNodeImpl(Ctx.Pos());
- params->Add("world");
- for (const auto& arg : args) {
- params->Add(new TAstAtomNodeImpl(Ctx.Pos(), arg, TNodeFlags::ArbitraryContent));
- }
- return BuildDoCall(Ctx.Pos(), BuildLambda(Ctx.Pos(), params, apply));
- }
- case TRule_do_stmt_TBlock2::kAlt2: {
- const auto& inlineAction = stmt.GetBlock2().GetAlt2().GetRule_inline_action1();
- const auto& body = inlineAction.GetRule_define_action_or_subquery_body2();
- auto saveScoped = Ctx.Scoped;
- Ctx.Scoped = MakeIntrusive<TScopedState>();
- Ctx.AllScopes.push_back(Ctx.Scoped);
- *Ctx.Scoped = *saveScoped;
- Ctx.Scoped->Local = TScopedState::TLocal{};
- Ctx.ScopeLevel++;
- TSqlQuery query(Ctx, Ctx.Settings.Mode, false);
- TBlocks innerBlocks;
- const bool hasValidBody = DefineActionOrSubqueryBody(query, innerBlocks, body);
- auto ret = hasValidBody ? BuildQuery(Ctx.Pos(), innerBlocks, false, Ctx.Scoped, Ctx.SeqMode) : nullptr;
- WarnUnusedNodes();
- Ctx.ScopeLevel--;
- Ctx.Scoped = saveScoped;
- if (!ret) {
- return {};
- }
- TNodePtr blockNode = new TAstListNodeImpl(Ctx.Pos());
- blockNode->Add("block");
- blockNode->Add(blockNode->Q(ret));
- if (!makeLambda) {
- return blockNode;
- }
- TNodePtr params = new TAstListNodeImpl(Ctx.Pos());
- params->Add("world");
- for (const auto& arg : args) {
- params->Add(new TAstAtomNodeImpl(Ctx.Pos(), arg, TNodeFlags::ArbitraryContent));
- }
- return BuildLambda(Ctx.Pos(), params, blockNode);
- }
- case TRule_do_stmt_TBlock2::ALT_NOT_SET:
- Y_ABORT("You should change implementation according to grammar changes");
- }
- }
- bool TSqlTranslation::DefineActionOrSubqueryBody(TSqlQuery& query, TBlocks& blocks, const TRule_define_action_or_subquery_body& body) {
- if (body.HasBlock2()) {
- Ctx.PushCurrentBlocks(&blocks);
- Y_DEFER {
- Ctx.PopCurrentBlocks();
- };
- size_t statementNumber = 0;
- if (!query.Statement(blocks, body.GetBlock2().GetRule_sql_stmt_core1(), statementNumber++)) {
- return false;
- }
- for (const auto& nestedStmtItem : body.GetBlock2().GetBlock2()) {
- const auto& nestedStmt = nestedStmtItem.GetRule_sql_stmt_core2();
- if (!query.Statement(blocks, nestedStmt, statementNumber++)) {
- return false;
- }
- }
- }
- return true;
- }
- bool TSqlTranslation::DefineActionOrSubqueryStatement(const TRule_define_action_or_subquery_stmt& stmt, TSymbolNameWithPos& nameAndPos, TNodePtr& lambda) {
- auto kind = Ctx.Token(stmt.GetToken2());
- const bool isSubquery = to_lower(kind) == "subquery";
- if (!isSubquery && Mode == NSQLTranslation::ESqlMode::SUBQUERY) {
- Error() << "Definition of actions is not allowed in the subquery";
- return false;
- }
- TString actionName;
- if (!NamedNodeImpl(stmt.GetRule_bind_parameter3(), actionName, *this)) {
- return false;
- }
- if (IsAnonymousName(actionName)) {
- Error() << "Can not use anonymous name '" << actionName << "' as " << to_upper(kind) << " name";
- return false;
- }
- TPosition actionNamePos = Ctx.Pos();
- TVector<TSymbolNameWithPos> argNames;
- ui32 optionalArgumentsCount = 0;
- if (stmt.HasBlock5() && !ActionOrSubqueryArgs(stmt.GetBlock5().GetRule_action_or_subquery_args1(), argNames, optionalArgumentsCount)) {
- return false;
- }
- auto saveScoped = Ctx.Scoped;
- Ctx.Scoped = MakeIntrusive<TScopedState>();
- Ctx.AllScopes.push_back(Ctx.Scoped);
- *Ctx.Scoped = *saveScoped;
- Ctx.Scoped->Local = TScopedState::TLocal{};
- Ctx.ScopeLevel++;
- for (auto& arg : argNames) {
- arg.Name = PushNamedAtom(arg.Pos, arg.Name);
- }
- auto saveMode = Ctx.Settings.Mode;
- if (isSubquery) {
- Ctx.Settings.Mode = NSQLTranslation::ESqlMode::SUBQUERY;
- }
- TSqlQuery query(Ctx, Ctx.Settings.Mode, false);
- TBlocks innerBlocks;
- const bool hasValidBody = DefineActionOrSubqueryBody(query, innerBlocks, stmt.GetRule_define_action_or_subquery_body8());
- ui32 topLevelSelects = 0;
- bool hasTailOps = false;
- for (auto& block : innerBlocks) {
- if (block->SubqueryAlias()) {
- continue;
- }
- if (block->HasSelectResult()) {
- ++topLevelSelects;
- } else if (topLevelSelects) {
- hasTailOps = true;
- }
- }
- if (isSubquery && (topLevelSelects != 1 || hasTailOps)) {
- Error() << "Strictly one select/process/reduce statement is expected at the end of subquery";
- return false;
- }
- auto ret = hasValidBody ? BuildQuery(Ctx.Pos(), innerBlocks, false, Ctx.Scoped, Ctx.SeqMode) : nullptr;
- WarnUnusedNodes();
- Ctx.Scoped = saveScoped;
- Ctx.ScopeLevel--;
- Ctx.Settings.Mode = saveMode;
- if (!ret) {
- return false;
- }
- TNodePtr blockNode = new TAstListNodeImpl(Ctx.Pos());
- blockNode->Add("block");
- blockNode->Add(blockNode->Q(ret));
- TNodePtr params = new TAstListNodeImpl(Ctx.Pos());
- params->Add("world");
- for (const auto& arg : argNames) {
- params->Add(BuildAtom(arg.Pos, arg.Name));
- }
- lambda = BuildLambda(Ctx.Pos(), params, blockNode);
- if (optionalArgumentsCount > 0) {
- lambda = new TCallNodeImpl(Ctx.Pos(), "WithOptionalArgs", {
- lambda,
- BuildQuotedAtom(Ctx.Pos(), ToString(optionalArgumentsCount), TNodeFlags::Default)
- });
- }
- nameAndPos.Name = actionName;
- nameAndPos.Pos = actionNamePos;
- return true;
- }
- TNodePtr TSqlTranslation::IfStatement(const TRule_if_stmt& stmt) {
- bool isEvaluate = stmt.HasBlock1();
- TSqlExpression expr(Ctx, Mode);
- auto exprNode = expr.Build(stmt.GetRule_expr3());
- if (!exprNode) {
- return {};
- }
- auto thenNode = DoStatement(stmt.GetRule_do_stmt4(), isEvaluate);
- if (!thenNode) {
- return {};
- }
- TNodePtr elseNode;
- if (stmt.HasBlock5()) {
- elseNode = DoStatement(stmt.GetBlock5().GetRule_do_stmt2(), isEvaluate);
- if (!elseNode) {
- return {};
- }
- }
- return BuildWorldIfNode(Ctx.Pos(), exprNode, thenNode, elseNode, isEvaluate);
- }
- TNodePtr TSqlTranslation::ForStatement(const TRule_for_stmt& stmt) {
- bool isEvaluate = stmt.HasBlock1();
- bool isParallel = stmt.HasBlock2();
- TSqlExpression expr(Ctx, Mode);
- TString itemArgName;
- if (!NamedNodeImpl(stmt.GetRule_bind_parameter4(), itemArgName, *this)) {
- return {};
- }
- TPosition itemArgNamePos = Ctx.Pos();
- auto exprNode = expr.Build(stmt.GetRule_expr6());
- if (!exprNode) {
- return{};
- }
- itemArgName = PushNamedAtom(itemArgNamePos, itemArgName);
- if (isParallel) {
- ++Ctx.ParallelModeCount;
- }
- auto bodyNode = DoStatement(stmt.GetRule_do_stmt7(), true, { itemArgName });
- if (isParallel) {
- --Ctx.ParallelModeCount;
- }
- PopNamedNode(itemArgName);
- if (!bodyNode) {
- return{};
- }
- TNodePtr elseNode;
- if (stmt.HasBlock8()) {
- elseNode = DoStatement(stmt.GetBlock8().GetRule_do_stmt2(), true);
- if (!elseNode) {
- return{};
- }
- }
- return BuildWorldForNode(Ctx.Pos(), exprNode, bodyNode, elseNode, isEvaluate, isParallel);
- }
- bool TSqlTranslation::BindParameterClause(const TRule_bind_parameter& node, TDeferredAtom& result) {
- TString paramName;
- if (!NamedNodeImpl(node, paramName, *this)) {
- return false;
- }
- auto named = GetNamedNode(paramName);
- if (!named) {
- return false;
- }
- result = MakeAtomFromExpression(Ctx.Pos(), Ctx, named);
- return true;
- }
- bool TSqlTranslation::ObjectFeatureValueClause(const TRule_object_feature_value& node, TDeferredAtom& result) {
- // object_feature_value: id_or_type | bind_parameter | STRING_VALUE | bool_value;
- switch (node.Alt_case()) {
- case TRule_object_feature_value::kAltObjectFeatureValue1:
- {
- TString name = Id(node.GetAlt_object_feature_value1().GetRule_id_or_type1(), *this);
- result = TDeferredAtom(Ctx.Pos(), name);
- break;
- }
- case TRule_object_feature_value::kAltObjectFeatureValue2:
- {
- if (!BindParameterClause(node.GetAlt_object_feature_value2().GetRule_bind_parameter1(), result)) {
- return false;
- }
- break;
- }
- case TRule_object_feature_value::kAltObjectFeatureValue3:
- {
- auto strValue = StringContent(Ctx, Ctx.Pos(), Ctx.Token(node.GetAlt_object_feature_value3().GetToken1()));
- if (!strValue) {
- Error() << "Cannot parse string correctly: " << Ctx.Token(node.GetAlt_object_feature_value3().GetToken1());
- return false;
- }
- result = TDeferredAtom(Ctx.Pos(), strValue->Content);
- break;
- }
- case TRule_object_feature_value::kAltObjectFeatureValue4:
- {
- TString value = Ctx.Token(node.GetAlt_object_feature_value4().GetRule_bool_value1().GetToken1());
- result = TDeferredAtom(BuildLiteralBool(Ctx.Pos(), FromString<bool>(value)), Ctx);
- break;
- }
- case TRule_object_feature_value::ALT_NOT_SET:
- Y_ABORT("You should change implementation according to grammar changes");
- }
- return true;
- }
- bool TSqlTranslation::AddObjectFeature(std::map<TString, TDeferredAtom>& result, const TRule_object_feature& feature) {
- if (feature.has_alt_object_feature1()) {
- auto& kv = feature.GetAlt_object_feature1().GetRule_object_feature_kv1();
- const TString& key = Id(kv.GetRule_an_id_or_type1(), *this);
- auto& ruleValue = kv.GetRule_object_feature_value3();
- TDeferredAtom value;
- if (!ObjectFeatureValueClause(ruleValue, value)) {
- return false;
- }
- result[key] = value;
- } else if (feature.has_alt_object_feature2()) {
- result[Id(feature.GetAlt_object_feature2().GetRule_object_feature_flag1().GetRule_an_id_or_type1(), *this)] = TDeferredAtom();
- }
- return true;
- }
- bool TSqlTranslation::ParseObjectFeatures(std::map<TString, TDeferredAtom>& result, const TRule_object_features& features) {
- if (features.has_alt_object_features1()) {
- if (!AddObjectFeature(result, features.alt_object_features1().GetRule_object_feature1())) {
- return false;
- }
- } else if (features.has_alt_object_features2()) {
- if (!AddObjectFeature(result, features.alt_object_features2().GetRule_object_feature2())) {
- return false;
- }
- for (auto&& i : features.alt_object_features2().GetBlock3()) {
- if (!AddObjectFeature(result, i.GetRule_object_feature2())) {
- return false;
- }
- }
- } else {
- return false;
- }
- return true;
- }
- bool TSqlTranslation::StoreDataSourceSettingsEntry(const TIdentifier& id, const TRule_table_setting_value* value, std::map<TString, TDeferredAtom>& result) {
- YQL_ENSURE(value);
- const TString key = to_lower(id.Name);
- if (result.find(key) != result.end()) {
- Ctx.Error() << to_upper(key) << " duplicate keys";
- return false;
- }
- if (!StoreString(*value, result[key], Ctx, to_upper(key))) {
- return false;
- }
- return true;
- }
- bool TSqlTranslation::StoreDataSourceSettingsEntry(const TRule_alter_table_setting_entry& entry, std::map<TString, TDeferredAtom>& result) {
- const TIdentifier id = IdEx(entry.GetRule_an_id1(), *this);
- return StoreDataSourceSettingsEntry(id, &entry.GetRule_table_setting_value3(), result);
- }
- bool TSqlTranslation::ParseExternalDataSourceSettings(std::map<TString, TDeferredAtom>& result, const TRule_with_table_settings& settingsNode) {
- const auto& firstEntry = settingsNode.GetRule_table_settings_entry3();
- if (!StoreDataSourceSettingsEntry(IdEx(firstEntry.GetRule_an_id1(), *this), &firstEntry.GetRule_table_setting_value3(),
- result)) {
- return false;
- }
- for (auto& block : settingsNode.GetBlock4()) {
- const auto& entry = block.GetRule_table_settings_entry2();
- if (!StoreDataSourceSettingsEntry(IdEx(entry.GetRule_an_id1(), *this), &entry.GetRule_table_setting_value3(), result)) {
- return false;
- }
- }
- if (result.find("source_type") == result.end()) {
- Ctx.Error() << "SOURCE_TYPE requires key";
- return false;
- }
- if (!ValidateAuthMethod(result)) {
- return false;
- }
- return true;
- }
- bool TSqlTranslation::ParseExternalDataSourceSettings(std::map<TString, TDeferredAtom>& result, std::set<TString>& toReset, const TRule_alter_external_data_source_action& alterAction) {
- switch (alterAction.Alt_case()) {
- case TRule_alter_external_data_source_action::kAltAlterExternalDataSourceAction1: {
- const auto& action = alterAction.GetAlt_alter_external_data_source_action1().GetRule_alter_table_set_table_setting_uncompat1();
- if (!StoreDataSourceSettingsEntry(IdEx(action.GetRule_an_id2(), *this), &action.GetRule_table_setting_value3(), result)) {
- return false;
- }
- return true;
- }
- case TRule_alter_external_data_source_action::kAltAlterExternalDataSourceAction2: {
- const auto& action = alterAction.GetAlt_alter_external_data_source_action2().GetRule_alter_table_set_table_setting_compat1();
- if (!StoreDataSourceSettingsEntry(action.GetRule_alter_table_setting_entry3(), result)) {
- return false;
- }
- for (const auto& entry : action.GetBlock4()) {
- if (!StoreDataSourceSettingsEntry(entry.GetRule_alter_table_setting_entry2(), result)) {
- return false;
- }
- }
- return true;
- }
- case TRule_alter_external_data_source_action::kAltAlterExternalDataSourceAction3: {
- const auto& action = alterAction.GetAlt_alter_external_data_source_action3().GetRule_alter_table_reset_table_setting1();
- const TString key = to_lower(IdEx(action.GetRule_an_id3(), *this).Name);
- toReset.insert(key);
- for (const auto& keys : action.GetBlock4()) {
- const TString key = to_lower(IdEx(keys.GetRule_an_id2(), *this).Name);
- toReset.insert(key);
- }
- return true;
- }
- case TRule_alter_external_data_source_action::ALT_NOT_SET:
- Y_ABORT("You should change implementation according to grammar changes");
- }
- }
- bool TSqlTranslation::ValidateAuthMethod(const std::map<TString, TDeferredAtom>& result) {
- const static TSet<TStringBuf> allAuthFields{
- "service_account_id",
- "service_account_secret_name",
- "login",
- "password_secret_name",
- "aws_access_key_id_secret_name",
- "aws_secret_access_key_secret_name",
- "aws_region",
- "token_secret_name"
- };
- const static TMap<TStringBuf, TSet<TStringBuf>> authMethodFields{
- {"NONE", {}},
- {"SERVICE_ACCOUNT", {"service_account_id", "service_account_secret_name"}},
- {"BASIC", {"login", "password_secret_name"}},
- {"AWS", {"aws_access_key_id_secret_name", "aws_secret_access_key_secret_name", "aws_region"}},
- {"MDB_BASIC", {"service_account_id", "service_account_secret_name", "login", "password_secret_name"}},
- {"TOKEN", {"token_secret_name"}}
- };
- auto authMethodIt = result.find("auth_method");
- if (authMethodIt == result.end() || authMethodIt->second.GetLiteral() == nullptr) {
- Ctx.Error() << "AUTH_METHOD requires key";
- return false;
- }
- const auto& authMethod = *authMethodIt->second.GetLiteral();
- auto it = authMethodFields.find(authMethod);
- if (it == authMethodFields.end()) {
- Ctx.Error() << "Unknown AUTH_METHOD = " << authMethod;
- return false;
- }
- const auto& currentAuthFields = it->second;
- for (const auto& authField: allAuthFields) {
- if (currentAuthFields.contains(authField) && !result.contains(TString{authField})) {
- Ctx.Error() << to_upper(TString{authField}) << " requires key";
- return false;
- }
- if (!currentAuthFields.contains(authField) && result.contains(TString{authField})) {
- Ctx.Error() << to_upper(TString{authField}) << " key is not supported for AUTH_METHOD = " << authMethod;
- return false;
- }
- }
- return true;
- }
- bool TSqlTranslation::ValidateExternalTable(const TCreateTableParameters& params) {
- if (params.TableType != ETableType::ExternalTable) {
- return true;
- }
- if (!params.TableSettings.DataSourcePath) {
- Ctx.Error() << "DATA_SOURCE requires key";
- return false;
- }
- if (!params.TableSettings.Location) {
- Ctx.Error() << "LOCATION requires key";
- return false;
- }
- if (params.PkColumns) {
- Ctx.Error() << "PRIMARY KEY is not supported for external table";
- return false;
- }
- return true;
- }
- bool TSqlTranslation::ParseViewQuery(
- std::map<TString, TDeferredAtom>& features,
- const TRule_select_stmt& query
- ) {
- TString queryText = CollectTokens(query);
- TString contextRecreationQuery;
- {
- const auto& service = Ctx.Scoped->CurrService;
- const auto& cluster = Ctx.Scoped->CurrCluster;
- const auto effectivePathPrefix = Ctx.GetPrefixPath(service, cluster);
- // TO DO: capture all runtime pragmas in a similar fashion.
- if (effectivePathPrefix != Ctx.Settings.PathPrefix) {
- contextRecreationQuery = TStringBuilder() << "PRAGMA TablePathPrefix = \"" << effectivePathPrefix << "\";\n";
- }
- // TO DO: capture other compilation-affecting statements except USE.
- if (cluster.GetLiteral() && *cluster.GetLiteral() != Ctx.Settings.DefaultCluster) {
- contextRecreationQuery = TStringBuilder() << "USE " << *cluster.GetLiteral() << ";\n";
- }
- }
- features["query_text"] = { Ctx.Pos(), contextRecreationQuery + queryText };
- // AST is needed for ready-made validation of CREATE VIEW statement.
- // Query is stored as plain text, not AST.
- const auto viewSelect = BuildViewSelect(query, Ctx, contextRecreationQuery);
- if (!viewSelect) {
- return false;
- }
- features["query_ast"] = {viewSelect, Ctx};
- return true;
- }
- namespace {
- static std::string::size_type GetQueryPosition(const TString& query, const NSQLv1Generated::TToken& token, bool antlr4) {
- if (1 == token.GetLine() && 0 == token.GetColumn()) {
- return 0;
- }
- TPosition pos = {0, 1};
- TTextWalker walker(pos, antlr4);
- std::string::size_type position = 0;
- for (char c : query) {
- walker.Advance(c);
- ++position;
- if (pos.Row == token.GetLine() && pos.Column == token.GetColumn()) {
- return position;
- }
- }
- return std::string::npos;
- }
- static TString GetLambdaText(TTranslation& ctx, TContext& Ctx, const TRule_lambda_or_parameter& lambdaOrParameter) {
- static const TString statementSeparator = ";\n";
- TVector<TString> statements;
- NYql::TIssues issues;
- if (!SplitQueryToStatements(Ctx.Query, statements, issues, Ctx.Settings)) {
- return {};
- }
- TStringBuilder result;
- for (const auto id : Ctx.ForAllStatementsParts) {
- result << statements[id] << "\n";
- }
- switch (lambdaOrParameter.Alt_case()) {
- case NSQLv1Generated::TRule_lambda_or_parameter::kAltLambdaOrParameter1: {
- const auto& lambda = lambdaOrParameter.GetAlt_lambda_or_parameter1().GetRule_lambda1();
- auto& beginToken = lambda.GetRule_smart_parenthesis1().GetToken1();
- const NSQLv1Generated::TToken* endToken = nullptr;
- switch (lambda.GetBlock2().GetBlock2().GetAltCase()) {
- case TRule_lambda_TBlock2_TBlock2::AltCase::kAlt1:
- endToken = &lambda.GetBlock2().GetBlock2().GetAlt1().GetToken3();
- break;
- case TRule_lambda_TBlock2_TBlock2::AltCase::kAlt2:
- endToken = &lambda.GetBlock2().GetBlock2().GetAlt2().GetToken3();
- break;
- case TRule_lambda_TBlock2_TBlock2::AltCase::ALT_NOT_SET:
- Y_ABORT("You should change implementation according to grammar changes");
- }
- auto begin = GetQueryPosition(Ctx.Query, beginToken, Ctx.Settings.Antlr4Parser);
- auto end = GetQueryPosition(Ctx.Query, *endToken, Ctx.Settings.Antlr4Parser);
- if (begin == std::string::npos || end == std::string::npos) {
- return {};
- }
- result << "$__ydb_transfer_lambda = " << Ctx.Query.substr(begin, end - begin + endToken->value().size()) << statementSeparator;
- return result;
- }
- case NSQLv1Generated::TRule_lambda_or_parameter::kAltLambdaOrParameter2: {
- const auto& valueBlock = lambdaOrParameter.GetAlt_lambda_or_parameter2().GetRule_bind_parameter1().GetBlock2();
- const auto id = Id(valueBlock.GetAlt1().GetRule_an_id_or_type1(), ctx);
- result << "$__ydb_transfer_lambda = $" << id << statementSeparator;
- return result;
- }
- case NSQLv1Generated::TRule_lambda_or_parameter::ALT_NOT_SET:
- Y_ABORT("You should change implementation according to grammar changes");
- }
- }
- }
- bool TSqlTranslation::ParseTransferLambda(
- TString& lambdaText,
- const TRule_lambda_or_parameter& lambdaOrParameter) {
- TSqlExpression expr(Ctx, Ctx.Settings.Mode);
- auto result = expr.Build(lambdaOrParameter);
- if (!result) {
- return false;
- }
- lambdaText = GetLambdaText(*this, Ctx, lambdaOrParameter);
- if (lambdaText.empty()) {
- Ctx.Error() << "Cannot parse lambda correctly";
- }
- return !lambdaText.empty();
- }
- class TReturningListColumns : public INode {
- public:
- TReturningListColumns(TPosition pos)
- : INode(pos)
- {
- }
- void SetStar() {
- ColumnNames.clear();
- Star = true;
- }
- void AddColumn(const NSQLv1Generated::TRule_an_id & rule, TTranslation& ctx) {
- ColumnNames.push_back(NSQLTranslationV1::Id(rule, ctx));
- }
- bool DoInit(TContext& ctx, ISource* source) override {
- Node = Y();
- if (Star) {
- Node->Add(Y("ReturningStar"));
- } else {
- for (auto&& column : ColumnNames) {
- Node->Add(Y("ReturningListItem", Q(column)));
- }
- }
- Node = Q(Y(Q("returning"), Q(Node)));
- return Node->Init(ctx, source);
- }
- TNodePtr DoClone() const override {
- return new TReturningListColumns(GetPos());
- }
- TAstNode* Translate(TContext& ctx) const override {
- return Node->Translate(ctx);
- }
- private:
- TNodePtr Node;
- TVector<TString> ColumnNames;
- bool Star = false;
- };
- TNodePtr TSqlTranslation::ReturningList(const ::NSQLv1Generated::TRule_returning_columns_list& columns) {
- auto result = MakeHolder<TReturningListColumns>(Ctx.Pos());
- if (columns.GetBlock2().Alt_case() == TRule_returning_columns_list_TBlock2::AltCase::kAlt1) {
- result->SetStar();
- } else if (columns.GetBlock2().Alt_case() == TRule_returning_columns_list_TBlock2::AltCase::kAlt2) {
- result->AddColumn(columns.GetBlock2().alt2().GetRule_an_id1(), *this);
- for (auto& block : columns.GetBlock2().alt2().GetBlock2()) {
- result->AddColumn(block.GetRule_an_id2(), *this);
- }
- }
- return result.Release();
- }
- bool TSqlTranslation::StoreResourcePoolSettingsEntry(const TIdentifier& id, const TRule_table_setting_value* value, std::map<TString, TDeferredAtom>& result) {
- YQL_ENSURE(value);
- const TString key = to_lower(id.Name);
- if (result.find(key) != result.end()) {
- Ctx.Error() << to_upper(key) << " duplicate keys";
- return false;
- }
- switch (value->Alt_case()) {
- case TRule_table_setting_value::kAltTableSettingValue2:
- return StoreString(*value, result[key], Ctx, to_upper(key));
- case TRule_table_setting_value::kAltTableSettingValue3:
- return StoreInt(*value, result[key], Ctx, to_upper(key));
- default:
- Ctx.Error() << to_upper(key) << " value should be a string literal or integer";
- return false;
- }
- return true;
- }
- bool TSqlTranslation::StoreResourcePoolSettingsEntry(const TRule_alter_table_setting_entry& entry, std::map<TString, TDeferredAtom>& result) {
- const TIdentifier id = IdEx(entry.GetRule_an_id1(), *this);
- return StoreResourcePoolSettingsEntry(id, &entry.GetRule_table_setting_value3(), result);
- }
- bool TSqlTranslation::ParseResourcePoolSettings(std::map<TString, TDeferredAtom>& result, const TRule_with_table_settings& settingsNode) {
- const auto& firstEntry = settingsNode.GetRule_table_settings_entry3();
- if (!StoreResourcePoolSettingsEntry(IdEx(firstEntry.GetRule_an_id1(), *this), &firstEntry.GetRule_table_setting_value3(), result)) {
- return false;
- }
- for (const auto& block : settingsNode.GetBlock4()) {
- const auto& entry = block.GetRule_table_settings_entry2();
- if (!StoreResourcePoolSettingsEntry(IdEx(entry.GetRule_an_id1(), *this), &entry.GetRule_table_setting_value3(), result)) {
- return false;
- }
- }
- return true;
- }
- bool TSqlTranslation::ParseResourcePoolSettings(std::map<TString, TDeferredAtom>& result, std::set<TString>& toReset, const TRule_alter_resource_pool_action& alterAction) {
- switch (alterAction.Alt_case()) {
- case TRule_alter_resource_pool_action::kAltAlterResourcePoolAction1: {
- const auto& action = alterAction.GetAlt_alter_resource_pool_action1().GetRule_alter_table_set_table_setting_compat1();
- if (!StoreResourcePoolSettingsEntry(action.GetRule_alter_table_setting_entry3(), result)) {
- return false;
- }
- for (const auto& entry : action.GetBlock4()) {
- if (!StoreResourcePoolSettingsEntry(entry.GetRule_alter_table_setting_entry2(), result)) {
- return false;
- }
- }
- return true;
- }
- case TRule_alter_resource_pool_action::kAltAlterResourcePoolAction2: {
- const auto& action = alterAction.GetAlt_alter_resource_pool_action2().GetRule_alter_table_reset_table_setting1();
- const TString firstKey = to_lower(IdEx(action.GetRule_an_id3(), *this).Name);
- toReset.insert(firstKey);
- for (const auto& key : action.GetBlock4()) {
- toReset.insert(to_lower(IdEx(key.GetRule_an_id2(), *this).Name));
- }
- return true;
- }
- case TRule_alter_resource_pool_action::ALT_NOT_SET:
- Y_ABORT("You should change implementation according to grammar changes");
- }
- }
- bool TSqlTranslation::StoreResourcePoolClassifierSettingsEntry(const TIdentifier& id, const TRule_table_setting_value* value, std::map<TString, TDeferredAtom>& result) {
- YQL_ENSURE(value);
- const TString key = to_lower(id.Name);
- if (result.find(key) != result.end()) {
- Ctx.Error() << to_upper(key) << " duplicate keys";
- return false;
- }
- switch (value->Alt_case()) {
- case TRule_table_setting_value::kAltTableSettingValue2:
- return StoreString(*value, result[key], Ctx, to_upper(key));
- case TRule_table_setting_value::kAltTableSettingValue3:
- return StoreInt(*value, result[key], Ctx, to_upper(key));
- default:
- Ctx.Error() << to_upper(key) << " value should be a string literal or integer";
- return false;
- }
- return true;
- }
- bool TSqlTranslation::StoreResourcePoolClassifierSettingsEntry(const TRule_alter_table_setting_entry& entry, std::map<TString, TDeferredAtom>& result) {
- const TIdentifier id = IdEx(entry.GetRule_an_id1(), *this);
- return StoreResourcePoolClassifierSettingsEntry(id, &entry.GetRule_table_setting_value3(), result);
- }
- bool TSqlTranslation::ParseResourcePoolClassifierSettings(std::map<TString, TDeferredAtom>& result, const TRule_with_table_settings& settingsNode) {
- const auto& firstEntry = settingsNode.GetRule_table_settings_entry3();
- if (!StoreResourcePoolClassifierSettingsEntry(IdEx(firstEntry.GetRule_an_id1(), *this), &firstEntry.GetRule_table_setting_value3(), result)) {
- return false;
- }
- for (const auto& block : settingsNode.GetBlock4()) {
- const auto& entry = block.GetRule_table_settings_entry2();
- if (!StoreResourcePoolClassifierSettingsEntry(IdEx(entry.GetRule_an_id1(), *this), &entry.GetRule_table_setting_value3(), result)) {
- return false;
- }
- }
- return true;
- }
- bool TSqlTranslation::ParseResourcePoolClassifierSettings(std::map<TString, TDeferredAtom>& result, std::set<TString>& toReset, const TRule_alter_resource_pool_classifier_action& alterAction) {
- switch (alterAction.Alt_case()) {
- case TRule_alter_resource_pool_classifier_action::kAltAlterResourcePoolClassifierAction1: {
- const auto& action = alterAction.GetAlt_alter_resource_pool_classifier_action1().GetRule_alter_table_set_table_setting_compat1();
- if (!StoreResourcePoolClassifierSettingsEntry(action.GetRule_alter_table_setting_entry3(), result)) {
- return false;
- }
- for (const auto& entry : action.GetBlock4()) {
- if (!StoreResourcePoolClassifierSettingsEntry(entry.GetRule_alter_table_setting_entry2(), result)) {
- return false;
- }
- }
- return true;
- }
- case TRule_alter_resource_pool_classifier_action::kAltAlterResourcePoolClassifierAction2: {
- const auto& action = alterAction.GetAlt_alter_resource_pool_classifier_action2().GetRule_alter_table_reset_table_setting1();
- const TString firstKey = to_lower(IdEx(action.GetRule_an_id3(), *this).Name);
- toReset.insert(firstKey);
- for (const auto& key : action.GetBlock4()) {
- toReset.insert(to_lower(IdEx(key.GetRule_an_id2(), *this).Name));
- }
- return true;
- }
- case TRule_alter_resource_pool_classifier_action::ALT_NOT_SET:
- Y_ABORT("You should change implementation according to grammar changes");
- }
- }
- } // namespace NSQLTranslationV1
|