sql_format.cpp 114 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253
  1. #include "sql_format.h"
  2. #include <yql/essentials/parser/lexer_common/lexer.h>
  3. #include <yql/essentials/core/sql_types/simple_types.h>
  4. #include <yql/essentials/sql/v1/lexer/lexer.h>
  5. #include <yql/essentials/sql/v1/proto_parser/proto_parser.h>
  6. #include <yql/essentials/parser/proto_ast/gen/v1_proto_split/SQLv1Parser.pb.main.h>
  7. #include <library/cpp/protobuf/util/simple_reflection.h>
  8. #include <library/cpp/resource/resource.h>
  9. #include <util/string/builder.h>
  10. #include <util/string/split.h>
  11. #include <util/string/strip.h>
  12. #include <util/string/subst.h>
  13. #include <util/generic/hash_set.h>
  14. namespace NSQLFormat {
  15. namespace {
  16. using namespace NSQLv1Generated;
  17. using NSQLTranslation::TParsedToken;
  18. using NSQLTranslation::TParsedTokenList;
  19. using NSQLTranslationV1::IsProbablyKeyword;
  20. using TTokenIterator = TParsedTokenList::const_iterator;
  21. TTokenIterator SkipWS(TTokenIterator curr, TTokenIterator end) {
  22. while (curr != end && curr->Name == "WS") {
  23. ++curr;
  24. }
  25. return curr;
  26. }
  27. TTokenIterator SkipWSOrComment(TTokenIterator curr, TTokenIterator end) {
  28. while (curr != end && (curr->Name == "WS" || curr->Name == "COMMENT")) {
  29. ++curr;
  30. }
  31. return curr;
  32. }
  33. TTokenIterator SkipWSOrCommentBackward(TTokenIterator curr, TTokenIterator begin) {
  34. while (curr != begin && (curr->Name == "WS" || curr->Name == "COMMENT")) {
  35. --curr;
  36. }
  37. return curr;
  38. }
  39. void SkipForValidate(
  40. TTokenIterator& in,
  41. TTokenIterator& out,
  42. const TParsedTokenList& query,
  43. const TParsedTokenList& formattedQuery,
  44. i32& parenthesesBalance
  45. ) {
  46. in = SkipWS(in, query.end());
  47. out = SkipWS(out, formattedQuery.end());
  48. auto inSkippedComments = SkipWSOrComment(in, query.end());
  49. auto skipAddedToken = [&](const TString& addedToken, const TVector<TString>& beforeTokens, const TVector<TString>& afterTokens) {
  50. if (
  51. out != formattedQuery.end() && out->Name == addedToken &&
  52. (in == query.end() || in->Name != addedToken) &&
  53. (beforeTokens.empty() || (inSkippedComments != query.end() && IsIn(beforeTokens, inSkippedComments->Name))) &&
  54. (afterTokens.empty() || (in != query.begin() && IsIn(afterTokens, SkipWSOrCommentBackward(in - 1, query.begin())->Name)))
  55. ) {
  56. out = SkipWS(++out, formattedQuery.end());
  57. if (addedToken == "LPAREN") {
  58. ++parenthesesBalance;
  59. } else if (addedToken == "RPAREN") {
  60. --parenthesesBalance;
  61. }
  62. }
  63. };
  64. skipAddedToken("LPAREN", {}, {"EQUALS"});
  65. skipAddedToken("RPAREN", {"END", "EOF", "SEMICOLON"}, {});
  66. skipAddedToken("SEMICOLON", {"END", "RBRACE_CURLY"}, {});
  67. auto skipDeletedToken = [&](const TString& deletedToken, const TVector<TString>& afterTokens) {
  68. if (
  69. in != query.end() && in->Name == deletedToken &&
  70. (out == formattedQuery.end() || out->Name != deletedToken) &&
  71. in != query.begin() && IsIn(afterTokens, SkipWSOrCommentBackward(in - 1, query.begin())->Name)
  72. ) {
  73. in = SkipWS(++in, query.end());
  74. return true;
  75. }
  76. return false;
  77. };
  78. while (skipDeletedToken("SEMICOLON", {"AS", "BEGIN", "LBRACE_CURLY", "SEMICOLON"})) {}
  79. }
  80. TParsedToken TransformTokenForValidate(TParsedToken token) {
  81. if (token.Name == "EQUALS2") {
  82. token.Name = "EQUALS";
  83. token.Content = "=";
  84. } else if (token.Name == "NOT_EQUALS2") {
  85. token.Name = "NOT_EQUALS";
  86. token.Content = "!=";
  87. }
  88. return token;
  89. }
  90. TStringBuf SkipQuotes(const TString& content) {
  91. TStringBuf str = content;
  92. str.SkipPrefix("\"");
  93. str.ChopSuffix("\"");
  94. str.SkipPrefix("'");
  95. str.ChopSuffix("'");
  96. return str;
  97. }
  98. TStringBuf SkipNewline(const TString& content) {
  99. TStringBuf str = content;
  100. str.ChopSuffix("\n");
  101. return str;
  102. }
  103. bool Validate(const TParsedTokenList& query, const TParsedTokenList& formattedQuery) {
  104. auto in = query.begin();
  105. auto out = formattedQuery.begin();
  106. auto inEnd = query.end();
  107. auto outEnd = formattedQuery.end();
  108. i32 parenthesesBalance = 0;
  109. while (in != inEnd && out != outEnd) {
  110. SkipForValidate(in, out, query, formattedQuery, parenthesesBalance);
  111. if (in != inEnd && out != outEnd) {
  112. auto inToken = TransformTokenForValidate(*in);
  113. auto outToken = TransformTokenForValidate(*out);
  114. if (inToken.Name != outToken.Name) {
  115. return false;
  116. }
  117. if (IsProbablyKeyword(inToken)) {
  118. if (!AsciiEqualsIgnoreCase(inToken.Content, outToken.Content)) {
  119. return false;
  120. }
  121. } else if (inToken.Name == "STRING_VALUE") {
  122. if (SkipQuotes(inToken.Content) != SkipQuotes(outToken.Content)) {
  123. return false;
  124. }
  125. } else if (inToken.Name == "COMMENT") {
  126. if (SkipNewline(inToken.Content) != SkipNewline(outToken.Content)) {
  127. return false;
  128. }
  129. } else {
  130. if (inToken.Content != outToken.Content) {
  131. return false;
  132. }
  133. }
  134. ++in;
  135. ++out;
  136. }
  137. }
  138. SkipForValidate(in, out, query, formattedQuery, parenthesesBalance);
  139. return in == inEnd && out == outEnd && parenthesesBalance == 0;
  140. }
  141. enum class EScope {
  142. Default,
  143. TypeName,
  144. Identifier,
  145. DoubleQuestion
  146. };
  147. class TPrettyVisitor;
  148. using TPrettyFunctor = std::function<void(TPrettyVisitor&, const NProtoBuf::Message& msg)>;
  149. class TObfuscatingVisitor;
  150. using TObfuscatingFunctor = std::function<void(TObfuscatingVisitor&, const NProtoBuf::Message& msg)>;
  151. struct TStaticData {
  152. TStaticData();
  153. static const TStaticData& GetInstance() {
  154. return *Singleton<TStaticData>();
  155. }
  156. THashSet<TString> Keywords;
  157. THashMap<const NProtoBuf::Descriptor*, EScope> ScopeDispatch;
  158. THashMap<const NProtoBuf::Descriptor*, TPrettyFunctor> PrettyVisitDispatch;
  159. THashMap<const NProtoBuf::Descriptor*, TObfuscatingFunctor> ObfuscatingVisitDispatch;
  160. };
  161. template <typename T, void (T::*Func)(const NProtoBuf::Message&)>
  162. void VisitAllFieldsImpl(T* obj, const NProtoBuf::Descriptor* descr, const NProtoBuf::Message& msg) {
  163. for (int i = 0; i < descr->field_count(); ++i) {
  164. const NProtoBuf::FieldDescriptor* fd = descr->field(i);
  165. NProtoBuf::TConstField field(msg, fd);
  166. if (field.IsMessage()) {
  167. for (size_t j = 0; j < field.Size(); ++j) {
  168. (obj->*Func)(*field.template Get<NProtoBuf::Message>(j));
  169. }
  170. }
  171. }
  172. }
  173. class TObfuscatingVisitor {
  174. friend struct TStaticData;
  175. public:
  176. TObfuscatingVisitor()
  177. : StaticData(TStaticData::GetInstance())
  178. {}
  179. TString Process(const NProtoBuf::Message& msg) {
  180. Scopes.push_back(EScope::Default);
  181. Visit(msg);
  182. return SB;
  183. }
  184. private:
  185. void VisitToken(const TToken& token) {
  186. auto str = token.GetValue();
  187. if (str == "<EOF>") {
  188. return;
  189. }
  190. if (!First) {
  191. SB << ' ';
  192. } else {
  193. First = false;
  194. }
  195. if (str == "$" && FuncCall) {
  196. FuncCall = false;
  197. }
  198. if (Scopes.back() == EScope::Identifier && !FuncCall) {
  199. if (str != "$" && !NYql::LookupSimpleTypeBySqlAlias(str, true)) {
  200. SB << "id";
  201. } else {
  202. SB << str;
  203. }
  204. } else if (NextToken) {
  205. SB << *NextToken;
  206. NextToken = Nothing();
  207. } else {
  208. SB << str;
  209. }
  210. }
  211. void VisitPragmaValue(const TRule_pragma_value& msg) {
  212. switch (msg.Alt_case()) {
  213. case TRule_pragma_value::kAltPragmaValue1: {
  214. NextToken = "0";
  215. break;
  216. }
  217. case TRule_pragma_value::kAltPragmaValue3: {
  218. NextToken = "'str'";
  219. break;
  220. }
  221. case TRule_pragma_value::kAltPragmaValue4: {
  222. NextToken = "false";
  223. break;
  224. }
  225. default:;
  226. }
  227. VisitAllFields(TRule_pragma_value::GetDescriptor(), msg);
  228. }
  229. void VisitLiteralValue(const TRule_literal_value& msg) {
  230. switch (msg.Alt_case()) {
  231. case TRule_literal_value::kAltLiteralValue1: {
  232. NextToken = "0";
  233. break;
  234. }
  235. case TRule_literal_value::kAltLiteralValue2: {
  236. NextToken = "0.0";
  237. break;
  238. }
  239. case TRule_literal_value::kAltLiteralValue3: {
  240. NextToken = "'str'";
  241. break;
  242. }
  243. case TRule_literal_value::kAltLiteralValue9: {
  244. NextToken = "false";
  245. break;
  246. }
  247. default:;
  248. }
  249. VisitAllFields(TRule_literal_value::GetDescriptor(), msg);
  250. }
  251. void VisitAtomExpr(const TRule_atom_expr& msg) {
  252. switch (msg.Alt_case()) {
  253. case TRule_atom_expr::kAltAtomExpr7: {
  254. FuncCall = true;
  255. break;
  256. }
  257. default:;
  258. }
  259. VisitAllFields(TRule_atom_expr::GetDescriptor(), msg);
  260. FuncCall = false;
  261. }
  262. void VisitInAtomExpr(const TRule_in_atom_expr& msg) {
  263. switch (msg.Alt_case()) {
  264. case TRule_in_atom_expr::kAltInAtomExpr6: {
  265. FuncCall = true;
  266. break;
  267. }
  268. default:;
  269. }
  270. VisitAllFields(TRule_in_atom_expr::GetDescriptor(), msg);
  271. FuncCall = false;
  272. }
  273. void VisitUnaryCasualSubexpr(const TRule_unary_casual_subexpr& msg) {
  274. bool invoke = false;
  275. for (auto& b : msg.GetRule_unary_subexpr_suffix2().GetBlock1()) {
  276. switch (b.GetBlock1().Alt_case()) {
  277. case TRule_unary_subexpr_suffix::TBlock1::TBlock1::kAlt2: {
  278. invoke = true;
  279. break;
  280. }
  281. default:;
  282. }
  283. break;
  284. }
  285. if (invoke) {
  286. FuncCall = true;
  287. }
  288. Visit(msg.GetBlock1());
  289. if (invoke) {
  290. FuncCall = false;
  291. }
  292. Visit(msg.GetRule_unary_subexpr_suffix2());
  293. }
  294. void VisitInUnaryCasualSubexpr(const TRule_in_unary_casual_subexpr& msg) {
  295. bool invoke = false;
  296. for (auto& b : msg.GetRule_unary_subexpr_suffix2().GetBlock1()) {
  297. switch (b.GetBlock1().Alt_case()) {
  298. case TRule_unary_subexpr_suffix::TBlock1::TBlock1::kAlt2: {
  299. invoke = true;
  300. break;
  301. }
  302. default:;
  303. }
  304. break;
  305. }
  306. if (invoke) {
  307. FuncCall = true;
  308. }
  309. Visit(msg.GetBlock1());
  310. if (invoke) {
  311. FuncCall = false;
  312. }
  313. Visit(msg.GetRule_unary_subexpr_suffix2());
  314. }
  315. void Visit(const NProtoBuf::Message& msg) {
  316. const NProtoBuf::Descriptor* descr = msg.GetDescriptor();
  317. auto scopePtr = StaticData.ScopeDispatch.FindPtr(descr);
  318. if (scopePtr) {
  319. Scopes.push_back(*scopePtr);
  320. }
  321. auto funcPtr = StaticData.ObfuscatingVisitDispatch.FindPtr(descr);
  322. if (funcPtr) {
  323. (*funcPtr)(*this, msg);
  324. } else {
  325. VisitAllFields(descr, msg);
  326. }
  327. if (scopePtr) {
  328. Scopes.pop_back();
  329. }
  330. }
  331. void VisitAllFields(const NProtoBuf::Descriptor* descr, const NProtoBuf::Message& msg) {
  332. VisitAllFieldsImpl<TObfuscatingVisitor, &TObfuscatingVisitor::Visit>(this, descr, msg);
  333. }
  334. const TStaticData& StaticData;
  335. TStringBuilder SB;
  336. bool First = true;
  337. TMaybe<TString> NextToken;
  338. TVector<EScope> Scopes;
  339. bool FuncCall = false;
  340. };
  341. class TPrettyVisitor {
  342. friend struct TStaticData;
  343. public:
  344. TPrettyVisitor(const TParsedTokenList& parsedTokens, const TParsedTokenList& comments, bool ansiLexer)
  345. : StaticData(TStaticData::GetInstance())
  346. , ParsedTokens(parsedTokens)
  347. , Comments(comments)
  348. , AnsiLexer(ansiLexer)
  349. {
  350. }
  351. TString Process(const NProtoBuf::Message& msg, bool& addLineBefore, bool& addLineAfter, TMaybe<ui32>& stmtCoreAltCase) {
  352. Scopes.push_back(EScope::Default);
  353. MarkedTokens.reserve(ParsedTokens.size());
  354. MarkTokens(msg);
  355. Y_ENSURE(MarkTokenStack.empty());
  356. Y_ENSURE(TokenIndex == ParsedTokens.size());
  357. TokenIndex = 0;
  358. Visit(msg);
  359. Y_ENSURE(TokenIndex == ParsedTokens.size());
  360. Y_ENSURE(MarkTokenStack.empty());
  361. for (; LastComment < Comments.size(); ++LastComment) {
  362. const auto text = Comments[LastComment].Content;
  363. AddComment(text);
  364. }
  365. ui32 lines = OutLine - (OutColumn == 0 ? 1 : 0);
  366. addLineBefore = AddLine.GetOrElse(true) || lines > 1;
  367. addLineAfter = AddLine.GetOrElse(true) || lines - CommentLines > 1;
  368. stmtCoreAltCase = StmtCoreAltCase;
  369. return SB;
  370. }
  371. private:
  372. struct TTokenInfo {
  373. bool OpeningBracket = false;
  374. bool ClosingBracket = false;
  375. bool BracketForcedExpansion = false;
  376. ui32 ClosingBracketIndex = 0;
  377. };
  378. using TMarkTokenStack = TVector<ui32>;
  379. void Out(TStringBuf s) {
  380. for (ui32 i = 0; i < s.size(); ++i) {
  381. Out(s[i], i == 0);
  382. }
  383. }
  384. void Out(char c, bool useIndent = true) {
  385. if (c == '\n' || c == '\r') {
  386. SB << c;
  387. if (!(c == '\n' && !SB.empty() && SB.back() == '\r')) {
  388. // do not increase OutLine if \n is preceded by \r
  389. // this way we handle \r, \n, or \r\n as single new line
  390. ++OutLine;
  391. }
  392. OutColumn = 0;
  393. } else {
  394. if (!OutColumn && useIndent) {
  395. ui32 indent = (CurrentIndent >= 0) ? CurrentIndent : 0;
  396. for (ui32 i = 0; i < indent; ++i) {
  397. SB << ' ';
  398. }
  399. }
  400. SB << c;
  401. ++OutColumn;
  402. }
  403. }
  404. void NewLine() {
  405. if (TokenIndex >= ParsedTokens.size() || ParsedTokens[TokenIndex].Line > LastLine) {
  406. WriteComments(true);
  407. }
  408. if (OutColumn) {
  409. Out('\n');
  410. }
  411. }
  412. void AddComment(TStringBuf text) {
  413. if (!AfterComment && OutLine > BlockFirstLine && OutColumn == 0) {
  414. Out('\n');
  415. }
  416. AfterComment = true;
  417. if (!SB.empty() && SB.back() != ' ' && SB.back() != '\n') {
  418. Out(' ');
  419. }
  420. if (OutColumn == 0) {
  421. ++CommentLines;
  422. }
  423. if (!text.StartsWith("--")) {
  424. CommentLines += CountIf(text, [](auto c) { return c == '\n'; });
  425. }
  426. Out(text);
  427. if (text.StartsWith("--") && !text.EndsWith("\n")) {
  428. Out('\n');
  429. }
  430. if (!text.StartsWith("--") &&
  431. TokenIndex < ParsedTokens.size() &&
  432. Comments[LastComment].Line < ParsedTokens[TokenIndex].Line &&
  433. (LastComment + 1 >= Comments.size() || Comments[LastComment].Line < Comments[LastComment + 1].Line)
  434. ) {
  435. Out('\n');
  436. }
  437. }
  438. void MarkTokens(const NProtoBuf::Message& msg) {
  439. const NProtoBuf::Descriptor* descr = msg.GetDescriptor();
  440. auto scopePtr = StaticData.ScopeDispatch.FindPtr(descr);
  441. if (scopePtr) {
  442. if (*scopePtr == EScope::TypeName) {
  443. ++InsideType;
  444. }
  445. Scopes.push_back(*scopePtr);
  446. }
  447. bool suppressExpr = false;
  448. if (descr == TToken::GetDescriptor()) {
  449. const auto& token = dynamic_cast<const TToken&>(msg);
  450. MarkToken(token);
  451. } else if (descr == TRule_sql_stmt_core::GetDescriptor()) {
  452. if (AddLine.Empty()) {
  453. const auto& rule = dynamic_cast<const TRule_sql_stmt_core&>(msg);
  454. AddLine = !IsSimpleStatement(rule).GetOrElse(false);
  455. StmtCoreAltCase = rule.Alt_case();
  456. }
  457. } else if (descr == TRule_lambda_body::GetDescriptor()) {
  458. Y_ENSURE(TokenIndex >= 1);
  459. auto prevIndex = TokenIndex - 1;
  460. Y_ENSURE(prevIndex < ParsedTokens.size());
  461. Y_ENSURE(ParsedTokens[prevIndex].Content == "{");
  462. MarkedTokens[prevIndex].OpeningBracket = false;
  463. ForceExpandedColumn = ParsedTokens[prevIndex].LinePos;
  464. ForceExpandedLine = ParsedTokens[prevIndex].Line;
  465. } else if (descr == TRule_in_atom_expr::GetDescriptor()) {
  466. const auto& value = dynamic_cast<const TRule_in_atom_expr&>(msg);
  467. if (value.Alt_case() == TRule_in_atom_expr::kAltInAtomExpr7) {
  468. suppressExpr = true;
  469. }
  470. } else if (descr == TRule_select_kind_parenthesis::GetDescriptor()) {
  471. const auto& value = dynamic_cast<const TRule_select_kind_parenthesis&>(msg);
  472. if (value.Alt_case() == TRule_select_kind_parenthesis::kAltSelectKindParenthesis2) {
  473. suppressExpr = true;
  474. }
  475. } else if (descr == TRule_window_specification::GetDescriptor()) {
  476. const auto& value = dynamic_cast<const TRule_window_specification&>(msg);
  477. const auto& details = value.GetRule_window_specification_details2();
  478. const bool needsNewline = details.HasBlock1() || details.HasBlock2() ||
  479. details.HasBlock3() || details.HasBlock4();
  480. if (needsNewline) {
  481. auto& paren = value.GetToken1();
  482. ForceExpandedColumn = paren.GetColumn();
  483. ForceExpandedLine = paren.GetLine();
  484. }
  485. suppressExpr = true;
  486. } else if (descr == TRule_exists_expr::GetDescriptor()) {
  487. const auto& value = dynamic_cast<const TRule_exists_expr&>(msg);
  488. auto& paren = value.GetToken2();
  489. ForceExpandedColumn = paren.GetColumn();
  490. ForceExpandedLine = paren.GetLine();
  491. suppressExpr = true;
  492. } else if (descr == TRule_case_expr::GetDescriptor()) {
  493. const auto& value = dynamic_cast<const TRule_case_expr&>(msg);
  494. auto& token = value.GetToken1();
  495. ForceExpandedColumn = token.GetColumn();
  496. ForceExpandedLine = token.GetLine();
  497. }
  498. const bool expr = (descr == TRule_expr::GetDescriptor() || descr == TRule_in_expr::GetDescriptor() || descr == TRule_type_name_composite::GetDescriptor());
  499. if (expr) {
  500. ++InsideExpr;
  501. }
  502. ui64 prevInsideExpr = InsideExpr;
  503. if (suppressExpr) {
  504. InsideExpr = 0;
  505. }
  506. VisitAllFieldsImpl<TPrettyVisitor, &TPrettyVisitor::MarkTokens>(this, descr, msg);
  507. if (suppressExpr) {
  508. InsideExpr = prevInsideExpr;
  509. }
  510. if (scopePtr) {
  511. if (*scopePtr == EScope::TypeName) {
  512. --InsideType;
  513. }
  514. Scopes.pop_back();
  515. }
  516. if (expr) {
  517. --InsideExpr;
  518. }
  519. }
  520. void MarkToken(const TToken& token) {
  521. auto str = token.GetValue();
  522. if (str == "<EOF>") {
  523. return;
  524. }
  525. MarkedTokens.emplace_back();
  526. if (str == "(" || str == "[" || str == "{" || str == "<|" || (InsideType && str == "<")) {
  527. MarkTokenStack.push_back(TokenIndex);
  528. auto& info = MarkedTokens[TokenIndex];
  529. info.OpeningBracket = (InsideExpr > 0);
  530. } else if (str == ")") {
  531. PopBracket("(");
  532. } else if (str == "]") {
  533. PopBracket("[");
  534. } else if (str == "}") {
  535. PopBracket("{");
  536. } else if (str == "|>") {
  537. PopBracket("<|");
  538. } else if (InsideType && str == ">") {
  539. PopBracket("<");
  540. }
  541. TokenIndex++;
  542. }
  543. void PopBracket(const TString& expected) {
  544. Y_ENSURE(!MarkTokenStack.empty());
  545. Y_ENSURE(MarkTokenStack.back() < ParsedTokens.size());
  546. auto& openToken = ParsedTokens[MarkTokenStack.back()];
  547. Y_ENSURE(openToken.Content == expected);
  548. auto& openInfo = MarkedTokens[MarkTokenStack.back()];
  549. auto& closeInfo = MarkedTokens[TokenIndex];
  550. const bool forcedExpansion = openToken.Line == ForceExpandedLine && openToken.LinePos <= ForceExpandedColumn;
  551. if (openInfo.OpeningBracket) {
  552. openInfo.ClosingBracketIndex = TokenIndex;
  553. openInfo.BracketForcedExpansion = forcedExpansion;
  554. closeInfo.BracketForcedExpansion = forcedExpansion;
  555. closeInfo.ClosingBracket = true;
  556. }
  557. MarkTokenStack.pop_back();
  558. }
  559. void Visit(const NProtoBuf::Message& msg) {
  560. const NProtoBuf::Descriptor* descr = msg.GetDescriptor();
  561. //Cerr << descr->name() << "\n";
  562. auto scopePtr = StaticData.ScopeDispatch.FindPtr(descr);
  563. if (descr == TRule_invoke_expr::GetDescriptor()) {
  564. AfterInvokeExpr = true;
  565. }
  566. if (descr == TRule_unary_op::GetDescriptor()) {
  567. AfterUnaryOp = true;
  568. }
  569. if (scopePtr) {
  570. if (*scopePtr == EScope::TypeName) {
  571. ++InsideType;
  572. }
  573. Scopes.push_back(*scopePtr);
  574. }
  575. auto funcPtr = StaticData.PrettyVisitDispatch.FindPtr(descr);
  576. if (funcPtr) {
  577. (*funcPtr)(*this, msg);
  578. } else {
  579. VisitAllFields(descr, msg);
  580. }
  581. if (scopePtr) {
  582. if (*scopePtr == EScope::TypeName) {
  583. --InsideType;
  584. }
  585. Scopes.pop_back();
  586. }
  587. }
  588. TMaybe<bool> IsSimpleStatement(const TRule_sql_stmt_core& msg) {
  589. switch (msg.Alt_case()) {
  590. case TRule_sql_stmt_core::kAltSqlStmtCore1: // pragma
  591. case TRule_sql_stmt_core::kAltSqlStmtCore5: // drop table
  592. case TRule_sql_stmt_core::kAltSqlStmtCore6: // use
  593. case TRule_sql_stmt_core::kAltSqlStmtCore8: // commit
  594. case TRule_sql_stmt_core::kAltSqlStmtCore11: // rollback
  595. case TRule_sql_stmt_core::kAltSqlStmtCore12: // declare
  596. case TRule_sql_stmt_core::kAltSqlStmtCore13: // import
  597. case TRule_sql_stmt_core::kAltSqlStmtCore14: // export
  598. case TRule_sql_stmt_core::kAltSqlStmtCore32: // drop external data source
  599. case TRule_sql_stmt_core::kAltSqlStmtCore34: // drop replication
  600. case TRule_sql_stmt_core::kAltSqlStmtCore60: // drop transfer
  601. return true;
  602. case TRule_sql_stmt_core::kAltSqlStmtCore3: { // named nodes
  603. const auto& stmt = msg.GetAlt_sql_stmt_core3().GetRule_named_nodes_stmt1();
  604. if (stmt.GetBlock3().HasAlt1()) {
  605. return true;
  606. }
  607. break;
  608. }
  609. case TRule_sql_stmt_core::kAltSqlStmtCore17: { // do
  610. const auto& stmt = msg.GetAlt_sql_stmt_core17().GetRule_do_stmt1();
  611. if (stmt.GetBlock2().HasAlt1()) {
  612. return true;
  613. }
  614. break;
  615. }
  616. case TRule_sql_stmt_core::kAltSqlStmtCore19: // if
  617. case TRule_sql_stmt_core::kAltSqlStmtCore20: // for
  618. return false;
  619. default:
  620. break;
  621. }
  622. return {};
  623. }
  624. template <typename T>
  625. void VisitRepeated(const ::google::protobuf::RepeatedPtrField<T>& field) {
  626. for (const auto& m : field) {
  627. Visit(m);
  628. }
  629. }
  630. template <typename T>
  631. void SkipSemicolons(const ::google::protobuf::RepeatedPtrField<T>& field, bool printOne = false) {
  632. for (const auto& m : field) {
  633. if (printOne) {
  634. Visit(m);
  635. printOne = false;
  636. } else {
  637. ++TokenIndex;
  638. }
  639. }
  640. if (printOne) {
  641. Out(';');
  642. }
  643. }
  644. void VisitDefineActionOrSubqueryBody(const TRule_define_action_or_subquery_body& msg) {
  645. SkipSemicolons(msg.GetBlock1());
  646. if (msg.HasBlock2()) {
  647. const auto& b = msg.GetBlock2();
  648. Visit(b.GetRule_sql_stmt_core1());
  649. for (auto block : b.GetBlock2()) {
  650. SkipSemicolons(block.GetBlock1(), /* printOne = */ true);
  651. if (!IsSimpleStatement(block.GetRule_sql_stmt_core2()).GetOrElse(false)) {
  652. Out('\n');
  653. }
  654. Visit(block.GetRule_sql_stmt_core2());
  655. }
  656. SkipSemicolons(b.GetBlock3(), /* printOne = */ true);
  657. }
  658. }
  659. void VisitPragma(const TRule_pragma_stmt& msg) {
  660. NewLine();
  661. VisitKeyword(msg.GetToken1());
  662. auto prefix = msg.GetRule_opt_id_prefix_or_type2();
  663. if (prefix.HasBlock1()) {
  664. Visit(prefix.GetBlock1().GetRule_an_id_or_type1());
  665. VisitKeyword(prefix.GetBlock1().GetToken2());
  666. AfterDot = true;
  667. }
  668. Visit(msg.GetRule_an_id3());
  669. if (msg.GetBlock4().HasAlt2()) {
  670. AfterInvokeExpr = true;
  671. const auto& alt2 = msg.GetBlock4().GetAlt2();
  672. VisitKeyword(alt2.GetToken1());
  673. Visit(alt2.GetRule_pragma_value2());
  674. VisitRepeated(alt2.GetBlock3());
  675. VisitKeyword(alt2.GetToken4());
  676. } else {
  677. Visit(msg.GetBlock4());
  678. }
  679. }
  680. void VisitSelect(const TRule_select_stmt& msg) {
  681. NewLine();
  682. Visit(msg.GetRule_select_kind_parenthesis1());
  683. for (const auto& block : msg.GetBlock2()) {
  684. NewLine();
  685. Visit(block.GetRule_select_op1());
  686. NewLine();
  687. Visit(block.GetRule_select_kind_parenthesis2());
  688. }
  689. }
  690. void VisitSelectUnparenthesized(const TRule_select_unparenthesized_stmt& msg) {
  691. NewLine();
  692. Visit(msg.GetRule_select_kind_partial1());
  693. for (const auto& block : msg.GetBlock2()) {
  694. NewLine();
  695. Visit(block.GetRule_select_op1());
  696. NewLine();
  697. Visit(block.GetRule_select_kind_parenthesis2());
  698. }
  699. }
  700. void VisitNamedNodes(const TRule_named_nodes_stmt& msg) {
  701. NewLine();
  702. Visit(msg.GetRule_bind_parameter_list1());
  703. Visit(msg.GetToken2());
  704. ExprLineIndent = CurrentIndent;
  705. switch (msg.GetBlock3().Alt_case()) {
  706. case TRule_named_nodes_stmt::TBlock3::kAlt1: {
  707. const auto& alt = msg.GetBlock3().GetAlt1();
  708. Visit(alt);
  709. break;
  710. }
  711. case TRule_named_nodes_stmt::TBlock3::kAlt2: {
  712. const auto& alt = msg.GetBlock3().GetAlt2();
  713. const auto& subselect = alt.GetRule_subselect_stmt1();
  714. switch (subselect.GetBlock1().Alt_case()) {
  715. case TRule_subselect_stmt::TBlock1::kAlt1: {
  716. const auto& alt = subselect.GetBlock1().GetAlt1();
  717. Visit(alt.GetToken1());
  718. NewLine();
  719. PushCurrentIndent();
  720. Visit(alt.GetRule_select_stmt2());
  721. PopCurrentIndent();
  722. NewLine();
  723. Visit(alt.GetToken3());
  724. break;
  725. }
  726. case TRule_subselect_stmt::TBlock1::kAlt2: {
  727. const auto& alt = subselect.GetBlock1().GetAlt2();
  728. Out(" (");
  729. NewLine();
  730. PushCurrentIndent();
  731. Visit(alt);
  732. PopCurrentIndent();
  733. NewLine();
  734. Out(')');
  735. break;
  736. }
  737. default:
  738. ythrow yexception() << "Alt is not supported";
  739. }
  740. break;
  741. }
  742. default:
  743. ythrow yexception() << "Alt is not supported";
  744. }
  745. ExprLineIndent = 0;
  746. }
  747. void VisitAlterDatabase(const TRule_alter_database_stmt& msg) {
  748. NewLine();
  749. VisitAllFields(TRule_alter_database_stmt::GetDescriptor(), msg);
  750. }
  751. void VisitCreateTable(const TRule_create_table_stmt& msg) {
  752. NewLine();
  753. Visit(msg.GetToken1());
  754. Visit(msg.GetBlock2());
  755. Visit(msg.GetBlock3());
  756. Visit(msg.GetBlock4());
  757. Visit(msg.GetRule_simple_table_ref5());
  758. Visit(msg.GetToken6());
  759. PushCurrentIndent();
  760. NewLine();
  761. Visit(msg.GetRule_create_table_entry7());
  762. for (const auto& b : msg.GetBlock8()) {
  763. Visit(b.GetToken1());
  764. NewLine();
  765. Visit(b.GetRule_create_table_entry2());
  766. }
  767. if (msg.HasBlock9()) {
  768. Visit(msg.GetBlock9());
  769. }
  770. PopCurrentIndent();
  771. NewLine();
  772. Visit(msg.GetToken10());
  773. if (msg.HasBlock11()) {
  774. NewLine();
  775. Visit(msg.GetBlock11());
  776. }
  777. if (msg.HasBlock12()) {
  778. NewLine();
  779. Visit(msg.GetBlock12());
  780. }
  781. if (msg.HasBlock13()) {
  782. NewLine();
  783. Visit(msg.GetBlock13());
  784. }
  785. if (msg.HasBlock14()) {
  786. NewLine();
  787. Visit(msg.GetBlock14());
  788. }
  789. if (msg.HasBlock15()) {
  790. NewLine();
  791. Visit(msg.GetBlock15());
  792. }
  793. }
  794. void VisitDropTable(const TRule_drop_table_stmt& msg) {
  795. NewLine();
  796. VisitAllFields(TRule_drop_table_stmt::GetDescriptor(), msg);
  797. }
  798. void VisitAnalyze(const TRule_analyze_stmt& msg) {
  799. NewLine();
  800. VisitAllFields(TRule_analyze_stmt::GetDescriptor(), msg);
  801. }
  802. void VisitBackup(const TRule_backup_stmt& msg) {
  803. NewLine();
  804. VisitAllFields(TRule_backup_stmt::GetDescriptor(), msg);
  805. }
  806. void VisitRestore(const TRule_restore_stmt& msg) {
  807. NewLine();
  808. VisitAllFields(TRule_restore_stmt::GetDescriptor(), msg);
  809. }
  810. void VisitUse(const TRule_use_stmt& msg) {
  811. NewLine();
  812. VisitAllFields(TRule_use_stmt::GetDescriptor(), msg);
  813. }
  814. void VisitAlterSequence(const TRule_alter_sequence_stmt& msg) {
  815. PosFromToken(msg.GetToken1());
  816. NewLine();
  817. VisitAllFields(TRule_alter_sequence_stmt::GetDescriptor(), msg);
  818. }
  819. void VisitShowCreateTable(const TRule_show_create_table_stmt& msg) {
  820. NewLine();
  821. VisitAllFields(TRule_show_create_table_stmt::GetDescriptor(), msg);
  822. }
  823. void VisitIntoTable(const TRule_into_table_stmt& msg) {
  824. NewLine();
  825. VisitAllFields(TRule_into_table_stmt::GetDescriptor(), msg);
  826. }
  827. void VisitCommit(const TRule_commit_stmt& msg) {
  828. NewLine();
  829. VisitAllFields(TRule_commit_stmt::GetDescriptor(), msg);
  830. }
  831. void VisitUpdate(const TRule_update_stmt& msg) {
  832. NewLine();
  833. Visit(msg.GetToken2());
  834. Visit(msg.GetRule_simple_table_ref3());
  835. switch (msg.GetBlock4().Alt_case()) {
  836. case TRule_update_stmt_TBlock4::kAlt1: {
  837. const auto& alt = msg.GetBlock4().GetAlt1();
  838. NewLine();
  839. Visit(alt.GetToken1());
  840. const auto& choice = alt.GetRule_set_clause_choice2();
  841. NewLine();
  842. switch (choice.Alt_case()) {
  843. case TRule_set_clause_choice::kAltSetClauseChoice1: {
  844. const auto& clauses = choice.GetAlt_set_clause_choice1().GetRule_set_clause_list1();
  845. NewLine();
  846. PushCurrentIndent();
  847. Visit(clauses.GetRule_set_clause1());
  848. for (auto& block : clauses.GetBlock2()) {
  849. Visit(block.GetToken1());
  850. NewLine();
  851. Visit(block.GetRule_set_clause2());
  852. }
  853. PopCurrentIndent();
  854. NewLine();
  855. break;
  856. }
  857. case TRule_set_clause_choice::kAltSetClauseChoice2: {
  858. const auto& multiColumn = choice.GetAlt_set_clause_choice2().GetRule_multiple_column_assignment1();
  859. const auto& targets = multiColumn.GetRule_set_target_list1();
  860. Visit(targets.GetToken1());
  861. NewLine();
  862. PushCurrentIndent();
  863. Visit(targets.GetRule_set_target2());
  864. for (auto& block : targets.GetBlock3()) {
  865. Visit(block.GetToken1());
  866. NewLine();
  867. Visit(block.GetRule_set_target2());
  868. }
  869. NewLine();
  870. PopCurrentIndent();
  871. Visit(targets.GetToken4());
  872. Visit(multiColumn.GetToken2());
  873. Visit(multiColumn.GetToken3());
  874. NewLine();
  875. const auto& simpleValues = multiColumn.GetRule_simple_values_source4();
  876. switch (simpleValues.Alt_case()) {
  877. case TRule_simple_values_source::kAltSimpleValuesSource1: {
  878. const auto& exprs = simpleValues.GetAlt_simple_values_source1().GetRule_expr_list1();
  879. NewLine();
  880. PushCurrentIndent();
  881. Visit(exprs.GetRule_expr1());
  882. for (const auto& block : exprs.GetBlock2()) {
  883. Visit(block.GetToken1());
  884. NewLine();
  885. Visit(block.GetRule_expr2());
  886. }
  887. PopCurrentIndent();
  888. NewLine();
  889. break;
  890. }
  891. case TRule_simple_values_source::kAltSimpleValuesSource2: {
  892. NewLine();
  893. PushCurrentIndent();
  894. Visit(simpleValues.GetAlt_simple_values_source2());
  895. PopCurrentIndent();
  896. NewLine();
  897. break;
  898. }
  899. default:
  900. ythrow yexception() << "Alt is not supported";
  901. }
  902. NewLine();
  903. Visit(multiColumn.GetToken5());
  904. break;
  905. }
  906. default:
  907. ythrow yexception() << "Alt is not supported";
  908. }
  909. PopCurrentIndent();
  910. if (alt.HasBlock3()) {
  911. NewLine();
  912. Visit(alt.GetBlock3());
  913. }
  914. PopCurrentIndent();
  915. break;
  916. }
  917. case TRule_update_stmt_TBlock4::kAlt2: {
  918. const auto& alt = msg.GetBlock4().GetAlt2();
  919. NewLine();
  920. Visit(alt.GetToken1());
  921. Visit(alt.GetRule_into_values_source2());
  922. break;
  923. }
  924. default:
  925. ythrow yexception() << "Alt is not supported";
  926. }
  927. }
  928. void VisitDelete(const TRule_delete_stmt& msg) {
  929. NewLine();
  930. Visit(msg.GetToken2());
  931. Visit(msg.GetToken3());
  932. Visit(msg.GetRule_simple_table_ref4());
  933. if (msg.HasBlock5()) {
  934. switch (msg.GetBlock5().Alt_case()) {
  935. case TRule_delete_stmt_TBlock5::kAlt1: {
  936. const auto& alt = msg.GetBlock5().GetAlt1();
  937. NewLine();
  938. Visit(alt);
  939. break;
  940. }
  941. case TRule_delete_stmt_TBlock5::kAlt2: {
  942. const auto& alt = msg.GetBlock5().GetAlt2();
  943. NewLine();
  944. Visit(alt);
  945. break;
  946. }
  947. default:
  948. ythrow yexception() << "Alt is not supported";
  949. }
  950. }
  951. }
  952. void VisitRollback(const TRule_rollback_stmt& msg) {
  953. NewLine();
  954. VisitAllFields(TRule_rollback_stmt::GetDescriptor(), msg);
  955. }
  956. void VisitDeclare(const TRule_declare_stmt& msg) {
  957. NewLine();
  958. VisitAllFields(TRule_declare_stmt::GetDescriptor(), msg);
  959. }
  960. void VisitImport(const TRule_import_stmt& msg) {
  961. NewLine();
  962. VisitAllFields(TRule_import_stmt::GetDescriptor(), msg);
  963. }
  964. void VisitExport(const TRule_export_stmt& msg) {
  965. NewLine();
  966. VisitKeyword(msg.GetToken1());
  967. NewLine();
  968. PushCurrentIndent();
  969. const auto& list = msg.GetRule_bind_parameter_list2();
  970. Visit(list.GetRule_bind_parameter1());
  971. for (auto& b : list.GetBlock2()) {
  972. Visit(b.GetToken1());
  973. NewLine();
  974. Visit(b.GetRule_bind_parameter2());
  975. }
  976. PopCurrentIndent();
  977. NewLine();
  978. }
  979. void VisitAlterTable(const TRule_alter_table_stmt& msg) {
  980. NewLine();
  981. VisitKeyword(msg.GetToken1());
  982. VisitKeyword(msg.GetToken2());
  983. Visit(msg.GetRule_simple_table_ref3());
  984. NewLine();
  985. PushCurrentIndent();
  986. Visit(msg.GetRule_alter_table_action4());
  987. for (auto& b : msg.GetBlock5()) {
  988. Visit(b.GetToken1());
  989. NewLine();
  990. Visit(b.GetRule_alter_table_action2());
  991. }
  992. PopCurrentIndent();
  993. NewLine();
  994. }
  995. void VisitAlterTableStore(const TRule_alter_table_store_stmt& msg) {
  996. NewLine();
  997. VisitAllFields(TRule_alter_table_store_stmt::GetDescriptor(), msg);
  998. }
  999. void VisitAlterExternalTable(const TRule_alter_external_table_stmt& msg) {
  1000. NewLine();
  1001. VisitKeyword(msg.GetToken1());
  1002. VisitKeyword(msg.GetToken2());
  1003. VisitKeyword(msg.GetToken3());
  1004. Visit(msg.GetRule_simple_table_ref4());
  1005. NewLine();
  1006. PushCurrentIndent();
  1007. Visit(msg.GetRule_alter_external_table_action5());
  1008. for (auto& b : msg.GetBlock6()) {
  1009. Visit(b.GetToken1());
  1010. NewLine();
  1011. Visit(b.GetRule_alter_external_table_action2());
  1012. }
  1013. PopCurrentIndent();
  1014. NewLine();
  1015. }
  1016. void VisitDo(const TRule_do_stmt& msg) {
  1017. VisitKeyword(msg.GetToken1());
  1018. switch (msg.GetBlock2().Alt_case()) {
  1019. case TRule_do_stmt_TBlock2::kAlt1: { // CALL
  1020. PushCurrentIndent();
  1021. NewLine();
  1022. const auto& alt = msg.GetBlock2().GetAlt1().GetRule_call_action1();
  1023. Visit(alt.GetBlock1());
  1024. AfterInvokeExpr = true;
  1025. Visit(alt.GetToken2());
  1026. if (alt.HasBlock3()) {
  1027. Visit(alt.GetBlock3());
  1028. }
  1029. Visit(alt.GetToken4());
  1030. PopCurrentIndent();
  1031. NewLine();
  1032. break;
  1033. }
  1034. case TRule_do_stmt_TBlock2::kAlt2: { // INLINE
  1035. const auto& alt = msg.GetBlock2().GetAlt2().GetRule_inline_action1();
  1036. VisitKeyword(alt.GetToken1());
  1037. PushCurrentIndent();
  1038. NewLine();
  1039. Visit(alt.GetRule_define_action_or_subquery_body2());
  1040. PopCurrentIndent();
  1041. NewLine();
  1042. VisitKeyword(alt.GetToken3());
  1043. VisitKeyword(alt.GetToken4());
  1044. break;
  1045. }
  1046. default:
  1047. ythrow yexception() << "Alt is not supported";
  1048. }
  1049. }
  1050. void VisitAction(const TRule_define_action_or_subquery_stmt& msg) {
  1051. NewLine();
  1052. VisitKeyword(msg.GetToken1());
  1053. VisitKeyword(msg.GetToken2());
  1054. Visit(msg.GetRule_bind_parameter3());
  1055. AfterInvokeExpr = true;
  1056. Visit(msg.GetToken4());
  1057. if (msg.HasBlock5()) {
  1058. Visit(msg.GetBlock5());
  1059. }
  1060. Visit(msg.GetToken6());
  1061. VisitKeyword(msg.GetToken7()); // AS
  1062. NewLine();
  1063. PushCurrentIndent();
  1064. Visit(msg.GetRule_define_action_or_subquery_body8());
  1065. PopCurrentIndent();
  1066. NewLine();
  1067. VisitKeyword(msg.GetToken9());
  1068. VisitKeyword(msg.GetToken10());
  1069. }
  1070. void VisitIf(const TRule_if_stmt& msg) {
  1071. NewLine();
  1072. VisitAllFields(TRule_if_stmt::GetDescriptor(), msg);
  1073. }
  1074. void VisitFor(const TRule_for_stmt& msg) {
  1075. NewLine();
  1076. VisitAllFields(TRule_for_stmt::GetDescriptor(), msg);
  1077. }
  1078. void VisitValues(const TRule_values_stmt& msg) {
  1079. NewLine();
  1080. VisitKeyword(msg.GetToken1());
  1081. const auto& rowList = msg.GetRule_values_source_row_list2();
  1082. PushCurrentIndent();
  1083. NewLine();
  1084. Visit(rowList.GetRule_values_source_row1());
  1085. for (const auto& b : rowList.GetBlock2()) {
  1086. Visit(b.GetToken1());
  1087. NewLine();
  1088. Visit(b.GetRule_values_source_row2());
  1089. }
  1090. PopCurrentIndent();
  1091. NewLine();
  1092. }
  1093. void VisitGrantPermissions(const TRule_grant_permissions_stmt& msg) {
  1094. NewLine();
  1095. VisitAllFields(TRule_grant_permissions_stmt::GetDescriptor(), msg);
  1096. }
  1097. void VisitRevokePermissions(const TRule_revoke_permissions_stmt& msg) {
  1098. NewLine();
  1099. VisitAllFields(TRule_revoke_permissions_stmt::GetDescriptor(), msg);
  1100. }
  1101. void VisitCreateUser(const TRule_create_user_stmt& msg) {
  1102. NewLine();
  1103. VisitAllFields(TRule_create_user_stmt::GetDescriptor(), msg);
  1104. }
  1105. void VisitAlterUser(const TRule_alter_user_stmt& msg) {
  1106. NewLine();
  1107. VisitAllFields(TRule_alter_user_stmt::GetDescriptor(), msg);
  1108. }
  1109. void VisitCreateGroup(const TRule_create_group_stmt& msg) {
  1110. NewLine();
  1111. VisitAllFields(TRule_create_group_stmt::GetDescriptor(), msg);
  1112. }
  1113. void VisitAlterGroup(const TRule_alter_group_stmt& msg) {
  1114. NewLine();
  1115. VisitAllFields(TRule_alter_group_stmt::GetDescriptor(), msg);
  1116. }
  1117. void VisitDropRole(const TRule_drop_role_stmt& msg) {
  1118. NewLine();
  1119. VisitAllFields(TRule_drop_role_stmt::GetDescriptor(), msg);
  1120. }
  1121. void VisitUpsertObject(const TRule_upsert_object_stmt& msg) {
  1122. NewLine();
  1123. VisitAllFields(TRule_upsert_object_stmt::GetDescriptor(), msg);
  1124. }
  1125. void VisitCreateObject(const TRule_create_object_stmt& msg) {
  1126. NewLine();
  1127. VisitAllFields(TRule_create_object_stmt::GetDescriptor(), msg);
  1128. }
  1129. void VisitAlterObject(const TRule_alter_object_stmt& msg) {
  1130. NewLine();
  1131. VisitAllFields(TRule_alter_object_stmt::GetDescriptor(), msg);
  1132. }
  1133. void VisitDropObject(const TRule_drop_object_stmt& msg) {
  1134. NewLine();
  1135. VisitAllFields(TRule_drop_object_stmt::GetDescriptor(), msg);
  1136. }
  1137. void VisitCreateTopic(const TRule_create_topic_stmt& msg) {
  1138. NewLine();
  1139. VisitKeyword(msg.GetToken1());
  1140. VisitKeyword(msg.GetToken2());
  1141. Visit(msg.GetBlock3());
  1142. Visit(msg.GetRule_topic_ref4());
  1143. if (msg.HasBlock5()) {
  1144. auto& b = msg.GetBlock5().GetRule_create_topic_entries1();
  1145. Visit(b.GetToken1());
  1146. NewLine();
  1147. PushCurrentIndent();
  1148. Visit(b.GetRule_create_topic_entry2());
  1149. for (auto& subEntry : b.GetBlock3()) {
  1150. Visit(subEntry.GetToken1());
  1151. NewLine();
  1152. Visit(subEntry.GetRule_create_topic_entry2());
  1153. }
  1154. NewLine();
  1155. PopCurrentIndent();
  1156. Visit(b.GetToken4());
  1157. }
  1158. if (msg.HasBlock6()) {
  1159. auto& b = msg.GetBlock6().GetRule_with_topic_settings1();
  1160. VisitKeyword(b.GetToken1());
  1161. VisitKeyword(b.GetToken2());
  1162. PushCurrentIndent();
  1163. NewLine();
  1164. Visit(b.GetRule_topic_settings3());
  1165. PopCurrentIndent();
  1166. NewLine();
  1167. VisitKeyword(b.GetToken4());
  1168. }
  1169. }
  1170. void VisitAlterTopic(const TRule_alter_topic_stmt& msg) {
  1171. NewLine();
  1172. VisitKeyword(msg.GetToken1());
  1173. VisitKeyword(msg.GetToken2());
  1174. Visit(msg.GetBlock3());
  1175. Visit(msg.GetRule_topic_ref4());
  1176. NewLine();
  1177. PushCurrentIndent();
  1178. Visit(msg.GetRule_alter_topic_action5());
  1179. for (auto& b : msg.GetBlock6()) {
  1180. Visit(b.GetToken1());
  1181. NewLine();
  1182. Visit(b.GetRule_alter_topic_action2());
  1183. }
  1184. PopCurrentIndent();
  1185. NewLine();
  1186. }
  1187. void VisitDropTopic(const TRule_drop_topic_stmt& msg) {
  1188. NewLine();
  1189. VisitAllFields(TRule_drop_topic_stmt::GetDescriptor(), msg);
  1190. }
  1191. void VisitCreateExternalDataSource(const TRule_create_external_data_source_stmt& msg) {
  1192. NewLine();
  1193. VisitAllFields(TRule_create_external_data_source_stmt::GetDescriptor(), msg);
  1194. }
  1195. void VisitAlterExternalDataSource(const TRule_alter_external_data_source_stmt& msg) {
  1196. NewLine();
  1197. VisitToken(msg.GetToken1());
  1198. VisitToken(msg.GetToken2());
  1199. VisitToken(msg.GetToken3());
  1200. VisitToken(msg.GetToken4());
  1201. Visit(msg.GetRule_object_ref5());
  1202. NewLine();
  1203. PushCurrentIndent();
  1204. Visit(msg.GetRule_alter_external_data_source_action6());
  1205. for (const auto& action : msg.GetBlock7()) {
  1206. Visit(action.GetToken1()); // comma
  1207. NewLine();
  1208. Visit(action.GetRule_alter_external_data_source_action2());
  1209. }
  1210. PopCurrentIndent();
  1211. NewLine();
  1212. }
  1213. void VisitDropExternalDataSource(const TRule_drop_external_data_source_stmt& msg) {
  1214. NewLine();
  1215. VisitAllFields(TRule_drop_external_data_source_stmt::GetDescriptor(), msg);
  1216. }
  1217. void VisitCreateView(const TRule_create_view_stmt& msg) {
  1218. NewLine();
  1219. VisitAllFields(TRule_create_view_stmt::GetDescriptor(), msg);
  1220. }
  1221. void VisitDropView(const TRule_drop_view_stmt& msg) {
  1222. NewLine();
  1223. VisitAllFields(TRule_drop_view_stmt::GetDescriptor(), msg);
  1224. }
  1225. void VisitCreateAsyncReplication(const TRule_create_replication_stmt& msg) {
  1226. NewLine();
  1227. VisitAllFields(TRule_create_replication_stmt::GetDescriptor(), msg);
  1228. }
  1229. void VisitAlterAsyncReplication(const TRule_alter_replication_stmt& msg) {
  1230. NewLine();
  1231. VisitAllFields(TRule_alter_replication_stmt::GetDescriptor(), msg);
  1232. }
  1233. void VisitDropAsyncReplication(const TRule_drop_replication_stmt& msg) {
  1234. NewLine();
  1235. VisitAllFields(TRule_drop_replication_stmt::GetDescriptor(), msg);
  1236. }
  1237. void VisitCreateTransfer(const TRule_create_transfer_stmt& msg) {
  1238. NewLine();
  1239. VisitAllFields(TRule_create_transfer_stmt::GetDescriptor(), msg);
  1240. }
  1241. void VisitAlterTransfer(const TRule_alter_transfer_stmt& msg) {
  1242. NewLine();
  1243. VisitAllFields(TRule_alter_transfer_stmt::GetDescriptor(), msg);
  1244. }
  1245. void VisitDropTransfer(const TRule_drop_transfer_stmt& msg) {
  1246. NewLine();
  1247. VisitAllFields(TRule_drop_transfer_stmt::GetDescriptor(), msg);
  1248. }
  1249. void VisitCreateResourcePool(const TRule_create_resource_pool_stmt& msg) {
  1250. NewLine();
  1251. VisitAllFields(TRule_create_resource_pool_stmt::GetDescriptor(), msg);
  1252. }
  1253. void VisitAlterResourcePool(const TRule_alter_resource_pool_stmt& msg) {
  1254. NewLine();
  1255. VisitToken(msg.GetToken1());
  1256. VisitToken(msg.GetToken2());
  1257. VisitToken(msg.GetToken3());
  1258. Visit(msg.GetRule_object_ref4());
  1259. NewLine();
  1260. PushCurrentIndent();
  1261. Visit(msg.GetRule_alter_resource_pool_action5());
  1262. for (const auto& action : msg.GetBlock6()) {
  1263. Visit(action.GetToken1()); // comma
  1264. NewLine();
  1265. Visit(action.GetRule_alter_resource_pool_action2());
  1266. }
  1267. PopCurrentIndent();
  1268. NewLine();
  1269. }
  1270. void VisitDropResourcePool(const TRule_drop_resource_pool_stmt& msg) {
  1271. NewLine();
  1272. VisitAllFields(TRule_drop_resource_pool_stmt::GetDescriptor(), msg);
  1273. }
  1274. void VisitCreateBackupCollection(const TRule_create_backup_collection_stmt& msg) {
  1275. NewLine();
  1276. VisitAllFields(TRule_create_backup_collection_stmt::GetDescriptor(), msg);
  1277. }
  1278. void VisitAlterBackupCollection(const TRule_alter_backup_collection_stmt& msg) {
  1279. NewLine();
  1280. VisitToken(msg.GetToken1());
  1281. Visit(msg.GetRule_backup_collection2());
  1282. NewLine();
  1283. PushCurrentIndent();
  1284. switch (msg.GetBlock3().Alt_case()) {
  1285. case TRule_alter_backup_collection_stmt_TBlock3::kAlt1: {
  1286. Visit(msg.GetBlock3().GetAlt1().GetRule_alter_backup_collection_actions1().GetRule_alter_backup_collection_action1());
  1287. for (const auto& action : msg.GetBlock3().GetAlt1().GetRule_alter_backup_collection_actions1().GetBlock2()) {
  1288. Visit(action.GetToken1()); // comma
  1289. NewLine();
  1290. Visit(action.GetRule_alter_backup_collection_action2());
  1291. }
  1292. break;
  1293. }
  1294. case TRule_alter_backup_collection_stmt_TBlock3::kAlt2: {
  1295. Visit(msg.GetBlock3().GetAlt2().GetRule_alter_backup_collection_entries1().GetRule_alter_backup_collection_entry1());
  1296. for (const auto& entry : msg.GetBlock3().GetAlt2().GetRule_alter_backup_collection_entries1().GetBlock2()) {
  1297. Visit(entry.GetToken1()); // comma
  1298. NewLine();
  1299. Visit(entry.GetRule_alter_backup_collection_entry2());
  1300. }
  1301. break;
  1302. }
  1303. default:
  1304. ythrow yexception() << "Alt is not supported";
  1305. }
  1306. PopCurrentIndent();
  1307. NewLine();
  1308. }
  1309. void VisitDropBackupCollection(const TRule_drop_backup_collection_stmt& msg) {
  1310. NewLine();
  1311. VisitAllFields(TRule_drop_backup_collection_stmt::GetDescriptor(), msg);
  1312. }
  1313. void VisitCreateResourcePoolClassifier(const TRule_create_resource_pool_classifier_stmt& msg) {
  1314. NewLine();
  1315. VisitAllFields(TRule_create_resource_pool_classifier_stmt::GetDescriptor(), msg);
  1316. }
  1317. void VisitAlterResourcePoolClassifier(const TRule_alter_resource_pool_classifier_stmt& msg) {
  1318. NewLine();
  1319. VisitToken(msg.GetToken1());
  1320. VisitToken(msg.GetToken2());
  1321. VisitToken(msg.GetToken3());
  1322. VisitToken(msg.GetToken4());
  1323. Visit(msg.GetRule_object_ref5());
  1324. NewLine();
  1325. PushCurrentIndent();
  1326. Visit(msg.GetRule_alter_resource_pool_classifier_action6());
  1327. for (const auto& action : msg.GetBlock7()) {
  1328. Visit(action.GetToken1()); // comma
  1329. NewLine();
  1330. Visit(action.GetRule_alter_resource_pool_classifier_action2());
  1331. }
  1332. PopCurrentIndent();
  1333. NewLine();
  1334. }
  1335. void VisitDropResourcePoolClassifier(const TRule_drop_resource_pool_classifier_stmt& msg) {
  1336. NewLine();
  1337. VisitAllFields(TRule_drop_resource_pool_classifier_stmt::GetDescriptor(), msg);
  1338. }
  1339. void VisitAllFields(const NProtoBuf::Descriptor* descr, const NProtoBuf::Message& msg) {
  1340. VisitAllFieldsImpl<TPrettyVisitor, &TPrettyVisitor::Visit>(this, descr, msg);
  1341. }
  1342. void WriteComments(bool completeLine) {
  1343. while (LastComment < Comments.size()) {
  1344. const auto& c = Comments[LastComment];
  1345. if (c.Line > LastLine || !completeLine && c.Line == LastLine && c.LinePos > LastColumn) {
  1346. break;
  1347. }
  1348. AddComment(c.Content);
  1349. ++LastComment;
  1350. }
  1351. }
  1352. void PosFromToken(const TToken& token) {
  1353. LastLine = token.GetLine();
  1354. LastColumn = token.GetColumn();
  1355. WriteComments(false);
  1356. }
  1357. void VisitToken(const TToken& token) {
  1358. VisitTokenImpl(token, false);
  1359. }
  1360. void VisitKeyword(const TToken& token) {
  1361. VisitTokenImpl(token, true);
  1362. }
  1363. void VisitTokenImpl(const TToken& token, bool forceKeyword) {
  1364. PosFromToken(token);
  1365. auto str = token.GetValue();
  1366. if (str == "<EOF>") {
  1367. return;
  1368. }
  1369. //Cerr << str << "\n";
  1370. auto currentScope = Scopes.back();
  1371. if (!SkipSpaceAfterUnaryOp && !InMultiTokenOp) {
  1372. if (AfterLess && str == ">") {
  1373. Out(' ');
  1374. } else if (AfterDigits && str == ".") {
  1375. Out(' ');
  1376. } else if (OutColumn && (currentScope == EScope::DoubleQuestion || str != "?")
  1377. && str != ":" && str != "." && str != "," && str != ";" && str != ")" && str != "]"
  1378. && str != "}" && str != "|>" && str != "::" && !AfterNamespace && !AfterBracket
  1379. && !AfterInvokeExpr && !AfterDollarOrAt && !AfterDot && (!AfterQuestion || str != "?")
  1380. && (!InsideType || (str != "<" && str != ">" && str != "<>"))
  1381. && (!InsideType || !AfterLess)
  1382. && (!AfterKeyExpr || str != "[")
  1383. ) {
  1384. Out(' ');
  1385. }
  1386. }
  1387. SkipSpaceAfterUnaryOp = false;
  1388. if (AfterUnaryOp) {
  1389. if (str == "+" || str == "-" || str == "~") {
  1390. SkipSpaceAfterUnaryOp = true;
  1391. }
  1392. AfterUnaryOp = false;
  1393. }
  1394. AfterInvokeExpr = false;
  1395. AfterNamespace = (str == "::");
  1396. AfterBracket = (str == "(" || str == "[" || str == "{" || str == "<|");
  1397. AfterDot = (str == ".");
  1398. AfterDigits = !str.empty() && AllOf(str, [](char c) { return c >= '0' && c <= '9'; });
  1399. AfterQuestion = (str == "?");
  1400. AfterLess = (str == "<");
  1401. AfterKeyExpr = false;
  1402. AfterComment = false;
  1403. if (forceKeyword) {
  1404. str = to_upper(str);
  1405. } else if (currentScope == EScope::Default) {
  1406. if (auto p = StaticData.Keywords.find(to_upper(str)); p != StaticData.Keywords.end()) {
  1407. str = *p;
  1408. }
  1409. }
  1410. AfterDollarOrAt = (str == "$" || str == "@");
  1411. const auto& markedInfo = MarkedTokens[TokenIndex];
  1412. if (markedInfo.ClosingBracket) {
  1413. Y_ENSURE(!MarkTokenStack.empty());
  1414. auto beginTokenIndex = MarkTokenStack.back();
  1415. if (markedInfo.BracketForcedExpansion || ParsedTokens[beginTokenIndex].Line != ParsedTokens[TokenIndex].Line) {
  1416. // multiline
  1417. PopCurrentIndent();
  1418. NewLine();
  1419. }
  1420. MarkTokenStack.pop_back();
  1421. }
  1422. if (InCondExpr) {
  1423. if (str == "=") {
  1424. str = "==";
  1425. } else if (str == "<>") {
  1426. str = "!=";
  1427. }
  1428. }
  1429. if (!AnsiLexer && ParsedTokens[TokenIndex].Name == "STRING_VALUE") {
  1430. TStringBuf checkStr = str;
  1431. if (checkStr.SkipPrefix("\"") && checkStr.ChopSuffix("\"") && !checkStr.Contains("'")) {
  1432. str = TStringBuilder() << '\'' << checkStr << '\'';
  1433. }
  1434. }
  1435. Out(str);
  1436. if (TokenIndex + 1 >= ParsedTokens.size() || ParsedTokens[TokenIndex + 1].Line > LastLine) {
  1437. WriteComments(true);
  1438. }
  1439. if (str == ";") {
  1440. NewLine();
  1441. }
  1442. if (markedInfo.OpeningBracket) {
  1443. MarkTokenStack.push_back(TokenIndex);
  1444. if (markedInfo.BracketForcedExpansion || ParsedTokens[TokenIndex].Line != ParsedTokens[markedInfo.ClosingBracketIndex].Line) {
  1445. // multiline
  1446. PushCurrentIndent();
  1447. NewLine();
  1448. }
  1449. }
  1450. if (str == "," && !MarkTokenStack.empty()) {
  1451. const bool addNewline =
  1452. (TokenIndex + 1 < ParsedTokens.size() && ParsedTokens[TokenIndex].Line != ParsedTokens[TokenIndex + 1].Line)
  1453. || (TokenIndex > 0 && ParsedTokens[TokenIndex - 1].Line != ParsedTokens[TokenIndex].Line);
  1454. // add line for trailing comma
  1455. if (addNewline) {
  1456. NewLine();
  1457. }
  1458. }
  1459. TokenIndex++;
  1460. }
  1461. void VisitIntoValuesSource(const TRule_into_values_source& msg) {
  1462. switch (msg.Alt_case()) {
  1463. case TRule_into_values_source::kAltIntoValuesSource1: {
  1464. const auto& alt = msg.GetAlt_into_values_source1();
  1465. if (alt.HasBlock1()) {
  1466. const auto& columns = alt.GetBlock1().GetRule_pure_column_list1();
  1467. Visit(columns.GetToken1());
  1468. NewLine();
  1469. PushCurrentIndent();
  1470. Visit(columns.GetRule_an_id2());
  1471. for (const auto& block : columns.GetBlock3()) {
  1472. Visit(block.GetToken1());
  1473. NewLine();
  1474. Visit(block.GetRule_an_id2());
  1475. }
  1476. PopCurrentIndent();
  1477. NewLine();
  1478. Visit(columns.GetToken4());
  1479. NewLine();
  1480. }
  1481. Visit(alt.GetRule_values_source2());
  1482. break;
  1483. }
  1484. case TRule_into_values_source::kAltIntoValuesSource2: {
  1485. VisitAllFields(TRule_into_values_source::GetDescriptor(), msg);
  1486. break;
  1487. }
  1488. default:
  1489. ythrow yexception() << "Alt is not supported";
  1490. }
  1491. }
  1492. void VisitSelectKind(const TRule_select_kind& msg) {
  1493. if (msg.HasBlock1()) {
  1494. Visit(msg.GetBlock1());
  1495. }
  1496. Visit(msg.GetBlock2());
  1497. if (msg.HasBlock3()) {
  1498. NewLine();
  1499. Visit(msg.GetBlock3());
  1500. }
  1501. }
  1502. void VisitProcessCore(const TRule_process_core& msg) {
  1503. Visit(msg.GetToken1());
  1504. if (msg.HasBlock2()) {
  1505. Visit(msg.GetBlock2());
  1506. }
  1507. Visit(msg.GetRule_named_single_source3());
  1508. VisitRepeated(msg.GetBlock4());
  1509. if (msg.HasBlock5()) {
  1510. NewLine();
  1511. const auto& block5 = msg.GetBlock5();
  1512. Visit(block5.GetToken1());
  1513. Visit(block5.GetRule_using_call_expr2());
  1514. if (block5.HasBlock3()) {
  1515. Visit(block5.GetBlock3());
  1516. }
  1517. if (block5.HasBlock4()) {
  1518. NewLine();
  1519. Visit(block5.GetBlock4());
  1520. }
  1521. if (block5.HasBlock5()) {
  1522. NewLine();
  1523. const auto& whereBlock = block5.GetBlock5();
  1524. Visit(whereBlock.GetToken1());
  1525. NewLine();
  1526. PushCurrentIndent();
  1527. Visit(whereBlock.GetRule_expr2());
  1528. PopCurrentIndent();
  1529. NewLine();
  1530. }
  1531. if (block5.HasBlock6()) {
  1532. NewLine();
  1533. const auto& havingBlock = block5.GetBlock6();
  1534. Visit(havingBlock.GetToken1());
  1535. NewLine();
  1536. PushCurrentIndent();
  1537. Visit(havingBlock.GetRule_expr2());
  1538. PopCurrentIndent();
  1539. NewLine();
  1540. }
  1541. if (block5.HasBlock7()) {
  1542. NewLine();
  1543. Visit(block5.GetBlock7());
  1544. }
  1545. }
  1546. }
  1547. void VisitReduceCore(const TRule_reduce_core& msg) {
  1548. Visit(msg.GetToken1());
  1549. Visit(msg.GetRule_named_single_source2());
  1550. VisitRepeated(msg.GetBlock3());
  1551. if (msg.HasBlock4()) {
  1552. NewLine();
  1553. Visit(msg.GetBlock4());
  1554. }
  1555. NewLine();
  1556. Visit(msg.GetToken5());
  1557. const auto& columns = msg.GetRule_column_list6();
  1558. NewLine();
  1559. PushCurrentIndent();
  1560. Visit(columns.GetRule_column_name1());
  1561. for (const auto& block : columns.GetBlock2()) {
  1562. Visit(block.GetToken1());
  1563. NewLine();
  1564. Visit(block.GetRule_column_name2());
  1565. }
  1566. if (columns.HasBlock3()) {
  1567. Visit(columns.GetBlock3());
  1568. }
  1569. PopCurrentIndent();
  1570. NewLine();
  1571. Visit(msg.GetToken7());
  1572. if (msg.HasBlock8()) {
  1573. Visit(msg.GetBlock8());
  1574. }
  1575. Visit(msg.GetRule_using_call_expr9());
  1576. if (msg.HasBlock10()) {
  1577. Visit(msg.GetBlock10());
  1578. }
  1579. if (msg.HasBlock11()) {
  1580. NewLine();
  1581. const auto& whereBlock = msg.GetBlock11();
  1582. Visit(whereBlock.GetToken1());
  1583. NewLine();
  1584. PushCurrentIndent();
  1585. Visit(whereBlock.GetRule_expr2());
  1586. PopCurrentIndent();
  1587. NewLine();
  1588. }
  1589. if (msg.HasBlock12()) {
  1590. NewLine();
  1591. const auto& havingBlock = msg.GetBlock12();
  1592. Visit(havingBlock.GetToken1());
  1593. NewLine();
  1594. PushCurrentIndent();
  1595. Visit(havingBlock.GetRule_expr2());
  1596. PopCurrentIndent();
  1597. NewLine();
  1598. }
  1599. if (msg.HasBlock13()) {
  1600. NewLine();
  1601. Visit(msg.GetBlock13());
  1602. }
  1603. }
  1604. void VisitSortSpecificationList(const TRule_sort_specification_list& msg) {
  1605. NewLine();
  1606. PushCurrentIndent();
  1607. Visit(msg.GetRule_sort_specification1());
  1608. for (const auto& block : msg.GetBlock2()) {
  1609. Visit(block.GetToken1());
  1610. NewLine();
  1611. Visit(block.GetRule_sort_specification2());
  1612. }
  1613. PopCurrentIndent();
  1614. NewLine();
  1615. }
  1616. void VisitSelectCore(const TRule_select_core& msg) {
  1617. if (msg.HasBlock1()) {
  1618. Visit(msg.GetBlock1());
  1619. NewLine();
  1620. }
  1621. Visit(msg.GetToken2());
  1622. if (msg.HasBlock3()) {
  1623. Visit(msg.GetBlock3());
  1624. }
  1625. Visit(msg.GetRule_opt_set_quantifier4());
  1626. NewLine();
  1627. PushCurrentIndent();
  1628. Visit(msg.GetRule_result_column5());
  1629. for (const auto& block : msg.GetBlock6()) {
  1630. Visit(block.GetToken1());
  1631. NewLine();
  1632. Visit(block.GetRule_result_column2());
  1633. }
  1634. if (msg.HasBlock7()) {
  1635. Visit(msg.GetBlock7());
  1636. }
  1637. PopCurrentIndent();
  1638. NewLine();
  1639. if (msg.HasBlock8()) {
  1640. NewLine();
  1641. Visit(msg.GetBlock8());
  1642. }
  1643. if (msg.HasBlock9()) {
  1644. NewLine();
  1645. Visit(msg.GetBlock9());
  1646. }
  1647. if (msg.HasBlock10()) {
  1648. NewLine();
  1649. const auto& whereBlock = msg.GetBlock10();
  1650. Visit(whereBlock.GetToken1());
  1651. NewLine();
  1652. PushCurrentIndent();
  1653. Visit(whereBlock.GetRule_expr2());
  1654. PopCurrentIndent();
  1655. NewLine();
  1656. }
  1657. if (msg.HasBlock11()) {
  1658. NewLine();
  1659. Visit(msg.GetBlock11());
  1660. }
  1661. if (msg.HasBlock12()) {
  1662. NewLine();
  1663. const auto& havingBlock = msg.GetBlock12();
  1664. Visit(havingBlock.GetToken1());
  1665. NewLine();
  1666. PushCurrentIndent();
  1667. Visit(havingBlock.GetRule_expr2());
  1668. PopCurrentIndent();
  1669. NewLine();
  1670. }
  1671. if (msg.HasBlock13()) {
  1672. NewLine();
  1673. Visit(msg.GetBlock13());
  1674. }
  1675. if (msg.HasBlock14()) {
  1676. NewLine();
  1677. Visit(msg.GetBlock14());
  1678. }
  1679. }
  1680. void VisitRowPatternRecognitionClause(const TRule_row_pattern_recognition_clause& msg) {
  1681. VisitToken(msg.GetToken1());
  1682. VisitToken(msg.GetToken2());
  1683. NewLine();
  1684. PushCurrentIndent();
  1685. if (msg.HasBlock3()) {
  1686. Visit(msg.GetBlock3());
  1687. NewLine();
  1688. }
  1689. if (msg.HasBlock4()) {
  1690. Visit(msg.GetBlock4());
  1691. NewLine();
  1692. }
  1693. if (msg.HasBlock5()) {
  1694. const auto& block = msg.GetBlock5().GetRule_row_pattern_measures1();
  1695. VisitToken(block.GetToken1());
  1696. NewLine();
  1697. PushCurrentIndent();
  1698. const auto& measureList = block.GetRule_row_pattern_measure_list2();
  1699. Visit(measureList.GetRule_row_pattern_measure_definition1());
  1700. for (const auto& measureDefinitionBlock : measureList.GetBlock2()) {
  1701. VisitToken(measureDefinitionBlock.GetToken1());
  1702. NewLine();
  1703. Visit(measureDefinitionBlock.GetRule_row_pattern_measure_definition2());
  1704. }
  1705. PopCurrentIndent();
  1706. NewLine();
  1707. }
  1708. if (msg.HasBlock6()) {
  1709. Visit(msg.GetBlock6());
  1710. NewLine();
  1711. }
  1712. const auto& common = msg.GetRule_row_pattern_common_syntax7();
  1713. if (common.HasBlock1()) {
  1714. Visit(common.GetBlock1());
  1715. NewLine();
  1716. }
  1717. if (common.HasBlock2()) {
  1718. Visit(common.GetBlock2());
  1719. }
  1720. VisitToken(common.GetToken3());
  1721. VisitToken(common.GetToken4());
  1722. Visit(common.GetRule_row_pattern5());
  1723. VisitToken(common.GetToken6());
  1724. NewLine();
  1725. if (common.HasBlock7()) {
  1726. const auto& block = common.GetBlock7().GetRule_row_pattern_subset_clause1();
  1727. VisitToken(block.GetToken1());
  1728. NewLine();
  1729. PushCurrentIndent();
  1730. const auto& subsetList = block.GetRule_row_pattern_subset_list2();
  1731. Visit(subsetList.GetRule_row_pattern_subset_item1());
  1732. for (const auto& subsetItemBlock : subsetList.GetBlock2()) {
  1733. VisitToken(subsetItemBlock.GetToken1());
  1734. NewLine();
  1735. Visit(subsetItemBlock.GetRule_row_pattern_subset_item2());
  1736. }
  1737. PopCurrentIndent();
  1738. NewLine();
  1739. }
  1740. VisitToken(common.GetToken8());
  1741. NewLine();
  1742. PushCurrentIndent();
  1743. const auto& definitionList = common.GetRule_row_pattern_definition_list9();
  1744. Visit(definitionList.GetRule_row_pattern_definition1());
  1745. for (const auto& definitionBlock : definitionList.GetBlock2()) {
  1746. VisitToken(definitionBlock.GetToken1());
  1747. NewLine();
  1748. Visit(definitionBlock.GetRule_row_pattern_definition2());
  1749. }
  1750. PopCurrentIndent();
  1751. NewLine();
  1752. PopCurrentIndent();
  1753. NewLine();
  1754. VisitToken(msg.GetToken8());
  1755. }
  1756. void VisitJoinSource(const TRule_join_source& msg) {
  1757. if (msg.HasBlock1()) {
  1758. Visit(msg.GetBlock1());
  1759. }
  1760. Visit(msg.GetRule_flatten_source2());
  1761. for (const auto& block : msg.GetBlock3()) {
  1762. NewLine();
  1763. Visit(block.GetRule_join_op1());
  1764. if (block.HasBlock2()) {
  1765. Visit(block.GetBlock2());
  1766. }
  1767. Visit(block.GetRule_flatten_source3());
  1768. if (block.HasBlock4()) {
  1769. NewLine();
  1770. Visit(block.GetBlock4());
  1771. }
  1772. }
  1773. }
  1774. void VisitJoinConstraint(const TRule_join_constraint& msg) {
  1775. if (msg.Alt_case() == TRule_join_constraint::kAltJoinConstraint1) {
  1776. const auto& alt = msg.GetAlt_join_constraint1();
  1777. Visit(alt.GetToken1());
  1778. NewLine();
  1779. PushCurrentIndent();
  1780. Visit(alt.GetRule_expr2());
  1781. PopCurrentIndent();
  1782. NewLine();
  1783. } else {
  1784. VisitAllFields(TRule_join_constraint::GetDescriptor(), msg);
  1785. }
  1786. }
  1787. void VisitSingleSource(const TRule_single_source& msg) {
  1788. switch (msg.Alt_case()) {
  1789. case TRule_single_source::kAltSingleSource1: {
  1790. const auto& alt = msg.GetAlt_single_source1();
  1791. Visit(alt);
  1792. break;
  1793. }
  1794. case TRule_single_source::kAltSingleSource2: {
  1795. const auto& alt = msg.GetAlt_single_source2();
  1796. Visit(alt.GetToken1());
  1797. NewLine();
  1798. PushCurrentIndent();
  1799. Visit(alt.GetRule_select_stmt2());
  1800. PopCurrentIndent();
  1801. NewLine();
  1802. Visit(alt.GetToken3());
  1803. break;
  1804. }
  1805. case TRule_single_source::kAltSingleSource3: {
  1806. const auto& alt = msg.GetAlt_single_source3();
  1807. Visit(alt.GetToken1());
  1808. NewLine();
  1809. PushCurrentIndent();
  1810. Visit(alt.GetRule_values_stmt2());
  1811. PopCurrentIndent();
  1812. NewLine();
  1813. Visit(alt.GetToken3());
  1814. break;
  1815. }
  1816. default:
  1817. ythrow yexception() << "Alt is not supported";
  1818. }
  1819. }
  1820. void VisitFlattenSource(const TRule_flatten_source& msg) {
  1821. const auto& namedSingleSource = msg.GetRule_named_single_source1();
  1822. bool indentBeforeSource = namedSingleSource.GetRule_single_source1().Alt_case() == TRule_single_source::kAltSingleSource1;
  1823. if (indentBeforeSource) {
  1824. NewLine();
  1825. PushCurrentIndent();
  1826. }
  1827. Visit(namedSingleSource);
  1828. if (indentBeforeSource) {
  1829. PopCurrentIndent();
  1830. NewLine();
  1831. }
  1832. if (msg.HasBlock2()) {
  1833. NewLine();
  1834. PushCurrentIndent();
  1835. Visit(msg.GetBlock2());
  1836. PopCurrentIndent();
  1837. NewLine();
  1838. }
  1839. }
  1840. void VisitNamedSingleSource(const TRule_named_single_source& msg) {
  1841. Visit(msg.GetRule_single_source1());
  1842. if (msg.HasBlock2()) {
  1843. Visit(msg.GetBlock2());
  1844. }
  1845. if (msg.HasBlock3()) {
  1846. const auto& block3 = msg.GetBlock3();
  1847. Visit(block3.GetBlock1());
  1848. if (block3.HasBlock2()) {
  1849. const auto& columns = block3.GetBlock2().GetRule_pure_column_list1();
  1850. Visit(columns.GetToken1());
  1851. NewLine();
  1852. PushCurrentIndent();
  1853. Visit(columns.GetRule_an_id2());
  1854. for (const auto& block : columns.GetBlock3()) {
  1855. Visit(block.GetToken1());
  1856. NewLine();
  1857. Visit(block.GetRule_an_id2());
  1858. }
  1859. NewLine();
  1860. PopCurrentIndent();
  1861. Visit(columns.GetToken4());
  1862. }
  1863. }
  1864. if (msg.HasBlock4()) {
  1865. NewLine();
  1866. Visit(msg.GetBlock4());
  1867. }
  1868. }
  1869. void VisitTableHints(const TRule_table_hints& msg) {
  1870. Visit(msg.GetToken1());
  1871. const auto& block2 = msg.GetBlock2();
  1872. if (block2.Alt_case() == TRule_table_hints::TBlock2::kAlt2) {
  1873. const auto& alt = block2.GetAlt2();
  1874. Visit(alt.GetToken1());
  1875. NewLine();
  1876. PushCurrentIndent();
  1877. Visit(alt.GetRule_table_hint2());
  1878. for (const auto& block : alt.GetBlock3()) {
  1879. Visit(block.GetToken1());
  1880. NewLine();
  1881. Visit(block.GetRule_table_hint2());
  1882. }
  1883. NewLine();
  1884. PopCurrentIndent();
  1885. Visit(alt.GetToken4());
  1886. } else {
  1887. Visit(block2);
  1888. }
  1889. }
  1890. void VisitSimpleTableRef(const TRule_simple_table_ref& msg) {
  1891. Visit(msg.GetRule_simple_table_ref_core1());
  1892. if (msg.HasBlock2()) {
  1893. Visit(msg.GetBlock2());
  1894. }
  1895. }
  1896. void VisitIntoSimpleTableRef(const TRule_into_simple_table_ref& msg) {
  1897. Visit(msg.GetRule_simple_table_ref1());
  1898. if (msg.HasBlock2()) {
  1899. const auto& block2 = msg.GetBlock2();
  1900. NewLine();
  1901. PushCurrentIndent();
  1902. Visit(block2.GetToken1());
  1903. Visit(block2.GetToken2());
  1904. const auto& columns = block2.GetRule_pure_column_list3();
  1905. Visit(columns.GetToken1());
  1906. NewLine();
  1907. PushCurrentIndent();
  1908. Visit(columns.GetRule_an_id2());
  1909. for (const auto& block : columns.GetBlock3()) {
  1910. Visit(block.GetToken1());
  1911. NewLine();
  1912. Visit(block.GetRule_an_id2());
  1913. }
  1914. PopCurrentIndent();
  1915. NewLine();
  1916. Visit(columns.GetToken4());
  1917. PopCurrentIndent();
  1918. }
  1919. }
  1920. void VisitSelectKindPartial(const TRule_select_kind_partial& msg) {
  1921. Visit(msg.GetRule_select_kind1());
  1922. if (msg.HasBlock2()) {
  1923. NewLine();
  1924. Visit(msg.GetBlock2());
  1925. }
  1926. }
  1927. void VisitFlattenByArg(const TRule_flatten_by_arg& msg) {
  1928. switch (msg.Alt_case()) {
  1929. case TRule_flatten_by_arg::kAltFlattenByArg1: {
  1930. const auto& alt = msg.GetAlt_flatten_by_arg1();
  1931. Visit(alt);
  1932. break;
  1933. }
  1934. case TRule_flatten_by_arg::kAltFlattenByArg2: {
  1935. const auto& alt = msg.GetAlt_flatten_by_arg2();
  1936. Visit(alt.GetToken1());
  1937. NewLine();
  1938. PushCurrentIndent();
  1939. const auto& exprs = alt.GetRule_named_expr_list2();
  1940. Visit(exprs.GetRule_named_expr1());
  1941. for (const auto& block : exprs.GetBlock2()) {
  1942. Visit(block.GetToken1());
  1943. NewLine();
  1944. Visit(block.GetRule_named_expr2());
  1945. }
  1946. if (alt.HasBlock3()) {
  1947. Visit(alt.GetBlock3());
  1948. }
  1949. NewLine();
  1950. PopCurrentIndent();
  1951. Visit(alt.GetToken4());
  1952. break;
  1953. }
  1954. default:
  1955. ythrow yexception() << "Alt is not supported";
  1956. }
  1957. }
  1958. void VisitWithoutColumnList(const TRule_without_column_list& msg) {
  1959. NewLine();
  1960. PushCurrentIndent();
  1961. Visit(msg.GetRule_without_column_name1());
  1962. for (const auto& block : msg.GetBlock2()) {
  1963. Visit(block.GetToken1());
  1964. NewLine();
  1965. Visit(block.GetRule_without_column_name2());
  1966. }
  1967. if (msg.HasBlock3()) {
  1968. Visit(msg.GetBlock3());
  1969. }
  1970. PopCurrentIndent();
  1971. NewLine();
  1972. }
  1973. void VisitTableRef(const TRule_table_ref& msg) {
  1974. if (msg.HasBlock1()) {
  1975. Visit(msg.GetBlock1());
  1976. }
  1977. if (msg.HasBlock2()) {
  1978. Visit(msg.GetBlock2());
  1979. }
  1980. const auto& block3 = msg.GetBlock3();
  1981. switch (block3.Alt_case()) {
  1982. case TRule_table_ref::TBlock3::kAlt1: {
  1983. const auto& alt = block3.GetAlt1();
  1984. const auto& key = alt.GetRule_table_key1();
  1985. Visit(key.GetRule_id_table_or_type1());
  1986. Visit(key.GetBlock2());
  1987. break;
  1988. }
  1989. case TRule_table_ref::TBlock3::kAlt2: {
  1990. const auto& alt = block3.GetAlt2();
  1991. Visit(alt.GetRule_an_id_expr1());
  1992. AfterInvokeExpr = true;
  1993. Visit(alt.GetToken2());
  1994. if (alt.HasBlock3()) {
  1995. Visit(alt.GetBlock3());
  1996. }
  1997. Visit(alt.GetToken4());
  1998. break;
  1999. }
  2000. case TRule_table_ref::TBlock3::kAlt3: {
  2001. const auto& alt = block3.GetAlt3();
  2002. Visit(alt.GetRule_bind_parameter1());
  2003. if (alt.HasBlock2()) {
  2004. AfterInvokeExpr = true;
  2005. Visit(alt.GetBlock2());
  2006. }
  2007. if (alt.HasBlock3()) {
  2008. Visit(alt.GetBlock3());
  2009. }
  2010. break;
  2011. }
  2012. default:
  2013. ythrow yexception() << "Alt is not supported";
  2014. }
  2015. if (msg.HasBlock4()) {
  2016. Visit(msg.GetBlock4());
  2017. }
  2018. }
  2019. void VisitGroupingElementList(const TRule_grouping_element_list& msg) {
  2020. NewLine();
  2021. PushCurrentIndent();
  2022. Visit(msg.GetRule_grouping_element1());
  2023. for (const auto& block : msg.GetBlock2()) {
  2024. Visit(block.GetToken1());
  2025. NewLine();
  2026. Visit(block.GetRule_grouping_element2());
  2027. }
  2028. PopCurrentIndent();
  2029. NewLine();
  2030. }
  2031. void VisitGroupByClause(const TRule_group_by_clause& msg) {
  2032. Visit(msg.GetToken1());
  2033. if (msg.HasBlock2()) {
  2034. Visit(msg.GetBlock2());
  2035. }
  2036. Visit(msg.GetToken3());
  2037. Visit(msg.GetRule_opt_set_quantifier4());
  2038. Visit(msg.GetRule_grouping_element_list5());
  2039. if (msg.HasBlock6()) {
  2040. NewLine();
  2041. PushCurrentIndent();
  2042. Visit(msg.GetBlock6());
  2043. PopCurrentIndent();
  2044. NewLine();
  2045. }
  2046. }
  2047. void VisitWindowDefinitionList(const TRule_window_definition_list& msg) {
  2048. NewLine();
  2049. PushCurrentIndent();
  2050. Visit(msg.GetRule_window_definition1());
  2051. for (const auto& block : msg.GetBlock2()) {
  2052. Visit(block.GetToken1());
  2053. NewLine();
  2054. Visit(block.GetRule_window_definition2());
  2055. }
  2056. PopCurrentIndent();
  2057. NewLine();
  2058. }
  2059. void VisitWindowSpecification(const TRule_window_specification& msg) {
  2060. Visit(msg.GetToken1());
  2061. const auto& details = msg.GetRule_window_specification_details2();
  2062. const bool needsNewline = details.HasBlock1() || details.HasBlock2() ||
  2063. details.HasBlock3() || details.HasBlock4();
  2064. if (needsNewline) {
  2065. NewLine();
  2066. PushCurrentIndent();
  2067. }
  2068. if (details.HasBlock1()) {
  2069. NewLine();
  2070. Visit(details.GetBlock1());
  2071. }
  2072. if (details.HasBlock2()) {
  2073. NewLine();
  2074. Visit(details.GetBlock2());
  2075. }
  2076. if (details.HasBlock3()) {
  2077. NewLine();
  2078. Visit(details.GetBlock3());
  2079. }
  2080. if (details.HasBlock4()) {
  2081. NewLine();
  2082. Visit(details.GetBlock4());
  2083. }
  2084. if (needsNewline) {
  2085. NewLine();
  2086. PopCurrentIndent();
  2087. }
  2088. Visit(msg.GetToken3());
  2089. }
  2090. void VisitWindowParitionClause(const TRule_window_partition_clause& msg) {
  2091. Visit(msg.GetToken1());
  2092. if (msg.HasBlock2()) {
  2093. Visit(msg.GetBlock2());
  2094. }
  2095. Visit(msg.GetToken3());
  2096. const auto& exprs = msg.GetRule_named_expr_list4();
  2097. PushCurrentIndent();
  2098. NewLine();
  2099. Visit(exprs.GetRule_named_expr1());
  2100. for (const auto& block : exprs.GetBlock2()) {
  2101. Visit(block.GetToken1());
  2102. NewLine();
  2103. Visit(block.GetRule_named_expr2());
  2104. }
  2105. PopCurrentIndent();
  2106. NewLine();
  2107. }
  2108. void VisitLambdaBody(const TRule_lambda_body& msg) {
  2109. PushCurrentIndent();
  2110. NewLine();
  2111. SkipSemicolons(msg.GetBlock1());
  2112. for (const auto& block : msg.GetBlock2()) {
  2113. Visit(block.GetRule_lambda_stmt1());
  2114. SkipSemicolons(block.GetBlock2(), /* printOne = */ true);
  2115. NewLine();
  2116. }
  2117. Visit(msg.GetToken3());
  2118. ExprLineIndent = CurrentIndent;
  2119. Visit(msg.GetRule_expr4());
  2120. SkipSemicolons(msg.GetBlock5(), /* printOne = */ true);
  2121. ExprLineIndent = 0;
  2122. PopCurrentIndent();
  2123. NewLine();
  2124. }
  2125. void VisitInAtomExpr(const TRule_in_atom_expr& msg) {
  2126. if (msg.Alt_case() == TRule_in_atom_expr::kAltInAtomExpr7) {
  2127. const auto& alt = msg.GetAlt_in_atom_expr7();
  2128. Visit(alt.GetToken1());
  2129. NewLine();
  2130. PushCurrentIndent();
  2131. Visit(alt.GetRule_select_stmt2());
  2132. NewLine();
  2133. PopCurrentIndent();
  2134. Visit(alt.GetToken3());
  2135. } else {
  2136. VisitAllFields(TRule_in_atom_expr::GetDescriptor(), msg);
  2137. }
  2138. }
  2139. void VisitSelectKindParenthesis(const TRule_select_kind_parenthesis& msg) {
  2140. if (msg.Alt_case() == TRule_select_kind_parenthesis::kAltSelectKindParenthesis2) {
  2141. const auto& alt = msg.GetAlt_select_kind_parenthesis2();
  2142. Visit(alt.GetToken1());
  2143. NewLine();
  2144. PushCurrentIndent();
  2145. Visit(alt.GetRule_select_kind_partial2());
  2146. PopCurrentIndent();
  2147. NewLine();
  2148. Visit(alt.GetToken3());
  2149. } else {
  2150. VisitAllFields(TRule_select_kind_parenthesis::GetDescriptor(), msg);
  2151. }
  2152. }
  2153. void VisitCastExpr(const TRule_cast_expr& msg) {
  2154. Visit(msg.GetToken1());
  2155. AfterInvokeExpr = true;
  2156. Visit(msg.GetToken2());
  2157. Visit(msg.GetRule_expr3());
  2158. Visit(msg.GetToken4());
  2159. Visit(msg.GetRule_type_name_or_bind5());
  2160. Visit(msg.GetToken6());
  2161. }
  2162. void VisitBitCastExpr(const TRule_bitcast_expr& msg) {
  2163. Visit(msg.GetToken1());
  2164. AfterInvokeExpr = true;
  2165. Visit(msg.GetToken2());
  2166. Visit(msg.GetRule_expr3());
  2167. Visit(msg.GetToken4());
  2168. Visit(msg.GetRule_type_name_simple5());
  2169. Visit(msg.GetToken6());
  2170. }
  2171. void VisitExtOrderByClause(const TRule_ext_order_by_clause& msg) {
  2172. if (msg.HasBlock1()) {
  2173. Visit(msg.GetBlock1());
  2174. }
  2175. Visit(msg.GetRule_order_by_clause2());
  2176. }
  2177. void VisitKeyExpr(const TRule_key_expr& msg) {
  2178. AfterKeyExpr = true;
  2179. VisitAllFields(TRule_key_expr::GetDescriptor(), msg);
  2180. }
  2181. void VisitExistsExpr(const TRule_exists_expr& msg) {
  2182. VisitKeyword(msg.GetToken1());
  2183. VisitToken(msg.GetToken2());
  2184. NewLine();
  2185. PushCurrentIndent();
  2186. Visit(msg.GetBlock3());
  2187. PopCurrentIndent();
  2188. NewLine();
  2189. VisitToken(msg.GetToken4());
  2190. }
  2191. void VisitCaseExpr(const TRule_case_expr& msg) {
  2192. VisitKeyword(msg.GetToken1());
  2193. if (msg.HasBlock2()) {
  2194. Visit(msg.GetBlock2());
  2195. }
  2196. NewLine();
  2197. PushCurrentIndent();
  2198. for (const auto& block : msg.GetBlock3()) {
  2199. Visit(block);
  2200. NewLine();
  2201. }
  2202. if (msg.HasBlock4()) {
  2203. const auto& block = msg.GetBlock4();
  2204. VisitKeyword(block.GetToken1());
  2205. Visit(block.GetRule_expr2());
  2206. }
  2207. PopCurrentIndent();
  2208. NewLine();
  2209. Visit(msg.GetToken5());
  2210. }
  2211. void VisitWithTableSettingsExpr(const TRule_with_table_settings& msg) {
  2212. VisitKeyword(msg.GetToken1());
  2213. Visit(msg.GetToken2());
  2214. const bool needIndent = msg.Block4Size() > 0; // more then one setting
  2215. if (needIndent) {
  2216. NewLine();
  2217. PushCurrentIndent();
  2218. Visit(msg.GetRule_table_settings_entry3()); // first setting
  2219. for (const auto& entry : msg.GetBlock4()) {
  2220. Visit(entry.GetToken1()); // comma
  2221. NewLine();
  2222. Visit(entry.GetRule_table_settings_entry2()); // other settings
  2223. }
  2224. PopCurrentIndent();
  2225. NewLine();
  2226. } else {
  2227. Visit(msg.GetRule_table_settings_entry3());
  2228. }
  2229. Visit(msg.GetToken5());
  2230. }
  2231. void VisitTableSettingValue(const TRule_table_setting_value& msg) {
  2232. switch (msg.GetAltCase()) {
  2233. case TRule_table_setting_value::kAltTableSettingValue5: {
  2234. // | ttl_tier_list ON an_id (AS (SECONDS | MILLISECONDS | MICROSECONDS | NANOSECONDS))?
  2235. const auto& ttlSettings = msg.GetAlt_table_setting_value5();
  2236. const auto& tierList = ttlSettings.GetRule_ttl_tier_list1();
  2237. const bool needIndent = tierList.HasBlock2() && tierList.GetBlock2().Block2Size() > 0; // more then one tier
  2238. if (needIndent) {
  2239. NewLine();
  2240. PushCurrentIndent();
  2241. Visit(tierList.GetRule_expr1());
  2242. VisitTtlTierAction(tierList.GetBlock2().GetRule_ttl_tier_action1());
  2243. for (const auto& tierEntry : tierList.GetBlock2().GetBlock2()) {
  2244. Visit(tierEntry.GetToken1()); // comma
  2245. NewLine();
  2246. Visit(tierEntry.GetRule_expr2());
  2247. VisitTtlTierAction(tierEntry.GetRule_ttl_tier_action3());
  2248. }
  2249. PopCurrentIndent();
  2250. NewLine();
  2251. } else {
  2252. Visit(tierList.GetRule_expr1());
  2253. if (tierList.HasBlock2()) {
  2254. VisitTtlTierAction(tierList.GetBlock2().GetRule_ttl_tier_action1());
  2255. }
  2256. }
  2257. VisitKeyword(ttlSettings.GetToken2());
  2258. Visit(ttlSettings.GetRule_an_id3());
  2259. if (ttlSettings.HasBlock4()) {
  2260. VisitKeyword(ttlSettings.GetBlock4().GetToken1());
  2261. VisitKeyword(ttlSettings.GetBlock4().GetToken2());
  2262. }
  2263. } break;
  2264. default:
  2265. VisitAllFields(TRule_table_setting_value::GetDescriptor(), msg);
  2266. }
  2267. }
  2268. void VisitTtlTierAction(const TRule_ttl_tier_action& msg) {
  2269. switch (msg.GetAltCase()) {
  2270. case TRule_ttl_tier_action::kAltTtlTierAction1:
  2271. // | TO EXTERNAL DATA SOURCE an_id
  2272. VisitKeyword(msg.GetAlt_ttl_tier_action1().GetToken1());
  2273. VisitKeyword(msg.GetAlt_ttl_tier_action1().GetToken2());
  2274. VisitKeyword(msg.GetAlt_ttl_tier_action1().GetToken3());
  2275. VisitKeyword(msg.GetAlt_ttl_tier_action1().GetToken4());
  2276. Visit(msg.GetAlt_ttl_tier_action1().GetRule_an_id5());
  2277. break;
  2278. case TRule_ttl_tier_action::kAltTtlTierAction2:
  2279. // | DELETE
  2280. VisitKeyword(msg.GetAlt_ttl_tier_action2().GetToken1());
  2281. break;
  2282. case TRule_ttl_tier_action::ALT_NOT_SET:
  2283. break;
  2284. }
  2285. }
  2286. void VisitExpr(const TRule_expr& msg) {
  2287. if (msg.HasAlt_expr2()) {
  2288. Visit(msg.GetAlt_expr2());
  2289. return;
  2290. }
  2291. const auto& orExpr = msg.GetAlt_expr1();
  2292. auto getExpr = [](const TRule_expr::TAlt1::TBlock2& b) -> const TRule_or_subexpr& { return b.GetRule_or_subexpr2(); };
  2293. auto getOp = [](const TRule_expr::TAlt1::TBlock2& b) -> const TToken& { return b.GetToken1(); };
  2294. VisitBinaryOp(orExpr.GetRule_or_subexpr1(), getOp, getExpr, orExpr.GetBlock2().begin(), orExpr.GetBlock2().end());
  2295. }
  2296. void VisitCondExpr(const TRule_cond_expr& msg) {
  2297. if (msg.Alt_case() == TRule_cond_expr::kAltCondExpr5) {
  2298. for (const auto& block : msg.GetAlt_cond_expr5().GetBlock1()) {
  2299. InCondExpr = true;
  2300. Visit(block.GetBlock1());
  2301. InCondExpr = false;
  2302. Visit(block.GetRule_eq_subexpr2());
  2303. }
  2304. } else {
  2305. VisitAllFields(TRule_cond_expr::GetDescriptor(), msg);
  2306. }
  2307. }
  2308. void VisitOrSubexpr(const TRule_or_subexpr& msg) {
  2309. auto getExpr = [](const TRule_or_subexpr::TBlock2& b) -> const TRule_and_subexpr& { return b.GetRule_and_subexpr2(); };
  2310. auto getOp = [](const TRule_or_subexpr::TBlock2& b) -> const TToken& { return b.GetToken1(); };
  2311. VisitBinaryOp(msg.GetRule_and_subexpr1(), getOp, getExpr, msg.GetBlock2().begin(), msg.GetBlock2().end());
  2312. }
  2313. void VisitAndSubexpr(const TRule_and_subexpr& msg) {
  2314. auto getExpr = [](const TRule_and_subexpr::TBlock2& b) -> const TRule_xor_subexpr& { return b.GetRule_xor_subexpr2(); };
  2315. auto getOp = [](const TRule_and_subexpr::TBlock2& b) -> const TToken& { return b.GetToken1(); };
  2316. VisitBinaryOp(msg.GetRule_xor_subexpr1(), getOp, getExpr, msg.GetBlock2().begin(), msg.GetBlock2().end());
  2317. }
  2318. void VisitEqSubexpr(const TRule_eq_subexpr& msg) {
  2319. auto getExpr = [](const TRule_eq_subexpr::TBlock2& b) -> const TRule_neq_subexpr& { return b.GetRule_neq_subexpr2(); };
  2320. auto getOp = [](const TRule_eq_subexpr::TBlock2& b) -> const TToken& { return b.GetToken1(); };
  2321. VisitBinaryOp(msg.GetRule_neq_subexpr1(), getOp, getExpr, msg.GetBlock2().begin(), msg.GetBlock2().end());
  2322. }
  2323. void VisitNeqSubexpr(const TRule_neq_subexpr& msg) {
  2324. bool pushedIndent = false;
  2325. VisitNeqSubexprImpl(msg, pushedIndent, true);
  2326. }
  2327. void VisitNeqSubexprImpl(const TRule_neq_subexpr& msg, bool& pushedIndent, bool top) {
  2328. auto getExpr = [](const TRule_neq_subexpr::TBlock2& b) -> const TRule_bit_subexpr& { return b.GetRule_bit_subexpr2(); };
  2329. auto getOp = [](const TRule_neq_subexpr::TBlock2& b) -> const TRule_neq_subexpr::TBlock2::TBlock1& { return b.GetBlock1(); };
  2330. VisitBinaryOp(msg.GetRule_bit_subexpr1(), getOp, getExpr, msg.GetBlock2().begin(), msg.GetBlock2().end());
  2331. if (msg.HasBlock3()) {
  2332. const auto& b = msg.GetBlock3();
  2333. switch (b.Alt_case()) {
  2334. case TRule_neq_subexpr_TBlock3::kAlt1: {
  2335. const auto& alt = b.GetAlt1();
  2336. const bool hasFirstNewline = LastLine != ParsedTokens[TokenIndex].Line;
  2337. // 2 is `??` size in tokens
  2338. const bool hasSecondNewline = ParsedTokens[TokenIndex].Line != ParsedTokens[TokenIndex + 2].Line;
  2339. const ui32 currentOutLine = OutLine;
  2340. if (currentOutLine != OutLine || (hasFirstNewline && hasSecondNewline)) {
  2341. NewLine();
  2342. if (!pushedIndent) {
  2343. PushCurrentIndent();
  2344. pushedIndent = true;
  2345. }
  2346. }
  2347. Visit(alt.GetRule_double_question1());
  2348. if (hasFirstNewline || hasSecondNewline) {
  2349. NewLine();
  2350. if (!pushedIndent) {
  2351. PushCurrentIndent();
  2352. pushedIndent = true;
  2353. }
  2354. }
  2355. VisitNeqSubexprImpl(alt.GetRule_neq_subexpr2(), pushedIndent, false);
  2356. if (pushedIndent && top) {
  2357. PopCurrentIndent();
  2358. pushedIndent = false;
  2359. }
  2360. break;
  2361. }
  2362. case TRule_neq_subexpr_TBlock3::kAlt2:
  2363. Visit(b.GetAlt2());
  2364. break;
  2365. default:
  2366. ythrow yexception() << "Alt is not supported";
  2367. }
  2368. }
  2369. }
  2370. void VisitBitSubexpr(const TRule_bit_subexpr& msg) {
  2371. auto getExpr = [](const TRule_bit_subexpr::TBlock2& b) -> const TRule_add_subexpr& { return b.GetRule_add_subexpr2(); };
  2372. auto getOp = [](const TRule_bit_subexpr::TBlock2& b) -> const TToken& { return b.GetToken1(); };
  2373. VisitBinaryOp(msg.GetRule_add_subexpr1(), getOp, getExpr, msg.GetBlock2().begin(), msg.GetBlock2().end());
  2374. }
  2375. void VisitAddSubexpr(const TRule_add_subexpr& msg) {
  2376. auto getExpr = [](const TRule_add_subexpr::TBlock2& b) -> const TRule_mul_subexpr& { return b.GetRule_mul_subexpr2(); };
  2377. auto getOp = [](const TRule_add_subexpr::TBlock2& b) -> const TToken& { return b.GetToken1(); };
  2378. VisitBinaryOp(msg.GetRule_mul_subexpr1(), getOp, getExpr, msg.GetBlock2().begin(), msg.GetBlock2().end());
  2379. }
  2380. void VisitMulSubexpr(const TRule_mul_subexpr& msg) {
  2381. auto getExpr = [](const TRule_mul_subexpr::TBlock2& b) -> const TRule_con_subexpr& { return b.GetRule_con_subexpr2(); };
  2382. auto getOp = [](const TRule_mul_subexpr::TBlock2& b) -> const TToken& { return b.GetToken1(); };
  2383. VisitBinaryOp(msg.GetRule_con_subexpr1(), getOp, getExpr, msg.GetBlock2().begin(), msg.GetBlock2().end());
  2384. }
  2385. ui32 BinaryOpTokenSize(const TToken&) {
  2386. return 1;
  2387. }
  2388. ui32 BinaryOpTokenSize(const TRule_neq_subexpr::TBlock2::TBlock1& block) {
  2389. switch (block.Alt_case()) {
  2390. case TRule_neq_subexpr::TBlock2::TBlock1::kAlt1:
  2391. case TRule_neq_subexpr::TBlock2::TBlock1::kAlt3:
  2392. case TRule_neq_subexpr::TBlock2::TBlock1::kAlt5:
  2393. case TRule_neq_subexpr::TBlock2::TBlock1::kAlt6:
  2394. case TRule_neq_subexpr::TBlock2::TBlock1::kAlt7:
  2395. return 1;
  2396. case TRule_neq_subexpr::TBlock2::TBlock1::kAlt2:
  2397. return 2;
  2398. case TRule_neq_subexpr::TBlock2::TBlock1::kAlt4:
  2399. return 3;
  2400. default:
  2401. ythrow yexception() << "Alt is not supported";
  2402. }
  2403. }
  2404. void VisitShiftRight(const TRule_shift_right& msg) {
  2405. VisitToken(msg.GetToken1());
  2406. InMultiTokenOp = true;
  2407. VisitToken(msg.GetToken2());
  2408. InMultiTokenOp = false;
  2409. }
  2410. void VisitRotRight(const TRule_rot_right& msg) {
  2411. VisitToken(msg.GetToken1());
  2412. InMultiTokenOp = true;
  2413. VisitToken(msg.GetToken2());
  2414. VisitToken(msg.GetToken3());
  2415. InMultiTokenOp = false;
  2416. }
  2417. template <typename TExpr, typename TGetOp, typename TGetExpr, typename TIter>
  2418. void VisitBinaryOp(const TExpr& expr, TGetOp getOp, TGetExpr getExpr, TIter begin, TIter end) {
  2419. Visit(expr);
  2420. bool pushedIndent = false;
  2421. for (; begin != end; ++begin) {
  2422. const auto op = getOp(*begin);
  2423. const auto opSize = BinaryOpTokenSize(op);
  2424. const bool hasFirstNewline = LastLine != ParsedTokens[TokenIndex].Line;
  2425. const bool hasSecondNewline = ParsedTokens[TokenIndex].Line != ParsedTokens[TokenIndex + opSize].Line;
  2426. const ui32 currentOutLine = OutLine;
  2427. if (currentOutLine != OutLine || hasFirstNewline || hasSecondNewline) {
  2428. NewLine();
  2429. if (!pushedIndent && CurrentIndent == ExprLineIndent) {
  2430. PushCurrentIndent();
  2431. pushedIndent = true;
  2432. }
  2433. }
  2434. Visit(op);
  2435. if (hasFirstNewline && hasSecondNewline) {
  2436. NewLine();
  2437. }
  2438. Visit(getExpr(*begin));
  2439. }
  2440. if (pushedIndent) {
  2441. PopCurrentIndent();
  2442. }
  2443. }
  2444. void PushCurrentIndent() {
  2445. CurrentIndent += OneIndent;
  2446. BlockFirstLine = OutLine;
  2447. if (OutColumn > 0) {
  2448. ++BlockFirstLine;
  2449. }
  2450. }
  2451. void PopCurrentIndent() {
  2452. CurrentIndent -= OneIndent;
  2453. }
  2454. private:
  2455. const TStaticData& StaticData;
  2456. const TParsedTokenList& ParsedTokens;
  2457. const TParsedTokenList& Comments;
  2458. const bool AnsiLexer;
  2459. TStringBuilder SB;
  2460. ui32 OutColumn = 0;
  2461. ui32 OutLine = 1;
  2462. ui32 LastLine = 0;
  2463. ui32 LastColumn = 0;
  2464. ui32 LastComment = 0;
  2465. ui32 CommentLines = 0;
  2466. i32 CurrentIndent = 0;
  2467. TVector<EScope> Scopes;
  2468. TMaybe<bool> AddLine;
  2469. TMaybe<ui32> StmtCoreAltCase;
  2470. ui64 InsideType = 0;
  2471. bool AfterNamespace = false;
  2472. bool AfterBracket = false;
  2473. bool AfterInvokeExpr = false;
  2474. bool AfterUnaryOp = false;
  2475. bool SkipSpaceAfterUnaryOp = false;
  2476. bool AfterDollarOrAt = false;
  2477. bool AfterDot = false;
  2478. bool AfterDigits = false;
  2479. bool AfterQuestion = false;
  2480. bool AfterLess = false;
  2481. bool AfterKeyExpr = false;
  2482. bool AfterComment = false;
  2483. bool InMultiTokenOp = false;
  2484. bool InCondExpr = false;
  2485. ui32 ForceExpandedLine = 0;
  2486. ui32 ForceExpandedColumn = 0;
  2487. ui32 BlockFirstLine = 1;
  2488. i32 ExprLineIndent = 0;
  2489. ui32 TokenIndex = 0;
  2490. TMarkTokenStack MarkTokenStack;
  2491. TVector<TTokenInfo> MarkedTokens;
  2492. ui64 InsideExpr = 0;
  2493. };
  2494. template <typename T>
  2495. TPrettyFunctor MakePrettyFunctor(void (TPrettyVisitor::*memberPtr)(const T& msg)) {
  2496. return [memberPtr](TPrettyVisitor& visitor, const NProtoBuf::Message& rawMsg) {
  2497. (visitor.*memberPtr)(dynamic_cast<const T&>(rawMsg));
  2498. };
  2499. }
  2500. template <typename T>
  2501. TObfuscatingFunctor MakeObfuscatingFunctor(void (TObfuscatingVisitor::*memberPtr)(const T& msg)) {
  2502. return [memberPtr](TObfuscatingVisitor& visitor, const NProtoBuf::Message& rawMsg) {
  2503. (visitor.*memberPtr)(dynamic_cast<const T&>(rawMsg));
  2504. };
  2505. }
  2506. TStaticData::TStaticData()
  2507. : Keywords(GetKeywords())
  2508. , ScopeDispatch({
  2509. {TRule_type_name::GetDescriptor(), EScope::TypeName},
  2510. {TRule_type_name_composite::GetDescriptor(), EScope::TypeName},
  2511. {TRule_double_question::GetDescriptor(), EScope::DoubleQuestion},
  2512. {TRule_id::GetDescriptor(), EScope::Identifier},
  2513. {TRule_id_or_type::GetDescriptor(), EScope::Identifier},
  2514. {TRule_id_schema::GetDescriptor(), EScope::Identifier},
  2515. {TRule_id_expr::GetDescriptor(), EScope::Identifier},
  2516. {TRule_id_expr_in::GetDescriptor(), EScope::Identifier},
  2517. {TRule_id_window::GetDescriptor(), EScope::Identifier},
  2518. {TRule_id_table::GetDescriptor(), EScope::Identifier},
  2519. {TRule_id_without::GetDescriptor(), EScope::Identifier},
  2520. {TRule_id_hint::GetDescriptor(), EScope::Identifier},
  2521. {TRule_identifier::GetDescriptor(), EScope::Identifier},
  2522. {TRule_id_table_or_type::GetDescriptor(), EScope::Identifier},
  2523. {TRule_bind_parameter::GetDescriptor(), EScope::Identifier},
  2524. {TRule_an_id_as_compat::GetDescriptor(), EScope::Identifier},
  2525. })
  2526. , PrettyVisitDispatch({
  2527. {TToken::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitToken)},
  2528. {TRule_into_values_source::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitIntoValuesSource)},
  2529. {TRule_select_kind::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitSelectKind)},
  2530. {TRule_process_core::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitProcessCore)},
  2531. {TRule_reduce_core::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitReduceCore)},
  2532. {TRule_sort_specification_list::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitSortSpecificationList)},
  2533. {TRule_select_core::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitSelectCore)},
  2534. {TRule_row_pattern_recognition_clause::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitRowPatternRecognitionClause)},
  2535. {TRule_join_source::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitJoinSource)},
  2536. {TRule_join_constraint::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitJoinConstraint)},
  2537. {TRule_single_source::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitSingleSource)},
  2538. {TRule_flatten_source::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitFlattenSource)},
  2539. {TRule_named_single_source::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitNamedSingleSource)},
  2540. {TRule_table_hints::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitTableHints)},
  2541. {TRule_simple_table_ref::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitSimpleTableRef)},
  2542. {TRule_into_simple_table_ref::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitIntoSimpleTableRef)},
  2543. {TRule_select_kind_partial::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitSelectKindPartial)},
  2544. {TRule_flatten_by_arg::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitFlattenByArg)},
  2545. {TRule_without_column_list::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitWithoutColumnList)},
  2546. {TRule_table_ref::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitTableRef)},
  2547. {TRule_grouping_element_list::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitGroupingElementList)},
  2548. {TRule_group_by_clause::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitGroupByClause)},
  2549. {TRule_window_definition_list::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitWindowDefinitionList)},
  2550. {TRule_window_specification::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitWindowSpecification)},
  2551. {TRule_window_partition_clause::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitWindowParitionClause)},
  2552. {TRule_lambda_body::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitLambdaBody)},
  2553. {TRule_in_atom_expr::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitInAtomExpr)},
  2554. {TRule_select_kind_parenthesis::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitSelectKindParenthesis)},
  2555. {TRule_cast_expr::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitCastExpr)},
  2556. {TRule_bitcast_expr::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitBitCastExpr)},
  2557. {TRule_ext_order_by_clause::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitExtOrderByClause)},
  2558. {TRule_key_expr::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitKeyExpr)},
  2559. {TRule_define_action_or_subquery_body::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitDefineActionOrSubqueryBody)},
  2560. {TRule_exists_expr::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitExistsExpr)},
  2561. {TRule_case_expr::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitCaseExpr)},
  2562. {TRule_with_table_settings::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitWithTableSettingsExpr)},
  2563. {TRule_table_setting_value::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitTableSettingValue)},
  2564. {TRule_ttl_tier_action::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitTtlTierAction)},
  2565. {TRule_expr::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitExpr)},
  2566. {TRule_cond_expr::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitCondExpr)},
  2567. {TRule_or_subexpr::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitOrSubexpr)},
  2568. {TRule_and_subexpr::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitAndSubexpr)},
  2569. {TRule_eq_subexpr::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitEqSubexpr)},
  2570. {TRule_neq_subexpr::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitNeqSubexpr)},
  2571. {TRule_bit_subexpr::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitBitSubexpr)},
  2572. {TRule_add_subexpr::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitAddSubexpr)},
  2573. {TRule_mul_subexpr::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitMulSubexpr)},
  2574. {TRule_rot_right::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitRotRight)},
  2575. {TRule_shift_right::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitShiftRight)},
  2576. {TRule_pragma_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitPragma)},
  2577. {TRule_select_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitSelect)},
  2578. {TRule_select_unparenthesized_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitSelectUnparenthesized)},
  2579. {TRule_named_nodes_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitNamedNodes)},
  2580. {TRule_create_table_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitCreateTable)},
  2581. {TRule_drop_table_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitDropTable)},
  2582. {TRule_use_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitUse)},
  2583. {TRule_into_table_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitIntoTable)},
  2584. {TRule_commit_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitCommit)},
  2585. {TRule_update_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitUpdate)},
  2586. {TRule_delete_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitDelete)},
  2587. {TRule_rollback_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitRollback)},
  2588. {TRule_declare_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitDeclare)},
  2589. {TRule_import_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitImport)},
  2590. {TRule_export_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitExport)},
  2591. {TRule_alter_table_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitAlterTable)},
  2592. {TRule_alter_external_table_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitAlterExternalTable)},
  2593. {TRule_do_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitDo)},
  2594. {TRule_define_action_or_subquery_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitAction)},
  2595. {TRule_if_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitIf)},
  2596. {TRule_for_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitFor)},
  2597. {TRule_values_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitValues)},
  2598. {TRule_create_user_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitCreateUser)},
  2599. {TRule_alter_user_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitAlterUser)},
  2600. {TRule_create_group_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitCreateGroup)},
  2601. {TRule_alter_group_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitAlterGroup)},
  2602. {TRule_drop_role_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitDropRole)},
  2603. {TRule_upsert_object_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitUpsertObject)},
  2604. {TRule_create_object_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitCreateObject)},
  2605. {TRule_alter_object_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitAlterObject)},
  2606. {TRule_drop_object_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitDropObject)},
  2607. {TRule_create_external_data_source_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitCreateExternalDataSource)},
  2608. {TRule_alter_external_data_source_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitAlterExternalDataSource)},
  2609. {TRule_drop_external_data_source_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitDropExternalDataSource)},
  2610. {TRule_create_replication_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitCreateAsyncReplication)},
  2611. {TRule_alter_replication_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitAlterAsyncReplication)},
  2612. {TRule_drop_replication_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitDropAsyncReplication)},
  2613. {TRule_create_transfer_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitCreateTransfer)},
  2614. {TRule_alter_transfer_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitAlterTransfer)},
  2615. {TRule_drop_transfer_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitDropTransfer)},
  2616. {TRule_create_topic_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitCreateTopic)},
  2617. {TRule_alter_topic_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitAlterTopic)},
  2618. {TRule_drop_topic_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitDropTopic)},
  2619. {TRule_grant_permissions_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitGrantPermissions)},
  2620. {TRule_revoke_permissions_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitRevokePermissions)},
  2621. {TRule_alter_table_store_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitAlterTableStore)},
  2622. {TRule_create_view_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitCreateView)},
  2623. {TRule_drop_view_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitDropView)},
  2624. {TRule_create_resource_pool_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitCreateResourcePool)},
  2625. {TRule_alter_resource_pool_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitAlterResourcePool)},
  2626. {TRule_drop_resource_pool_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitDropResourcePool)},
  2627. {TRule_create_backup_collection_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitCreateBackupCollection)},
  2628. {TRule_alter_backup_collection_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitAlterBackupCollection)},
  2629. {TRule_drop_backup_collection_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitDropBackupCollection)},
  2630. {TRule_analyze_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitAnalyze)},
  2631. {TRule_create_resource_pool_classifier_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitCreateResourcePoolClassifier)},
  2632. {TRule_alter_resource_pool_classifier_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitAlterResourcePoolClassifier)},
  2633. {TRule_drop_resource_pool_classifier_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitDropResourcePoolClassifier)},
  2634. {TRule_backup_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitBackup)},
  2635. {TRule_restore_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitRestore)},
  2636. {TRule_alter_sequence_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitAlterSequence)},
  2637. {TRule_alter_database_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitAlterDatabase)},
  2638. {TRule_show_create_table_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitShowCreateTable)},
  2639. })
  2640. , ObfuscatingVisitDispatch({
  2641. {TToken::GetDescriptor(), MakeObfuscatingFunctor(&TObfuscatingVisitor::VisitToken)},
  2642. {TRule_literal_value::GetDescriptor(), MakeObfuscatingFunctor(&TObfuscatingVisitor::VisitLiteralValue)},
  2643. {TRule_pragma_value::GetDescriptor(), MakeObfuscatingFunctor(&TObfuscatingVisitor::VisitPragmaValue)},
  2644. {TRule_atom_expr::GetDescriptor(), MakeObfuscatingFunctor(&TObfuscatingVisitor::VisitAtomExpr)},
  2645. {TRule_in_atom_expr::GetDescriptor(), MakeObfuscatingFunctor(&TObfuscatingVisitor::VisitInAtomExpr)},
  2646. {TRule_unary_casual_subexpr::GetDescriptor(), MakeObfuscatingFunctor(&TObfuscatingVisitor::VisitUnaryCasualSubexpr)},
  2647. {TRule_in_unary_casual_subexpr::GetDescriptor(), MakeObfuscatingFunctor(&TObfuscatingVisitor::VisitInUnaryCasualSubexpr)},
  2648. })
  2649. {
  2650. // ensure that all statements have a visitor
  2651. auto coreDescr = TRule_sql_stmt_core::GetDescriptor();
  2652. for (int i = 0; i < coreDescr->field_count(); ++i) {
  2653. const NProtoBuf::FieldDescriptor* fd = coreDescr->field(i);
  2654. if (fd->cpp_type() != NProtoBuf::FieldDescriptor::CPPTYPE_MESSAGE) {
  2655. continue;
  2656. }
  2657. auto altDescr = fd->message_type();
  2658. for (int j = 0; j < altDescr->field_count(); ++j) {
  2659. auto fd2 = altDescr->field(j);
  2660. if (fd2->cpp_type() != NProtoBuf::FieldDescriptor::CPPTYPE_MESSAGE) {
  2661. continue;
  2662. }
  2663. auto stmtMessage = fd2->message_type();
  2664. Y_ENSURE(PrettyVisitDispatch.contains(stmtMessage), TStringBuilder() << "Missing visitor for " << stmtMessage->name());
  2665. }
  2666. }
  2667. }
  2668. class TSqlFormatter : public NSQLFormat::ISqlFormatter {
  2669. public:
  2670. TSqlFormatter(const NSQLTranslation::TTranslationSettings& settings)
  2671. : Settings(settings)
  2672. {}
  2673. bool Format(const TString& query, TString& formattedQuery, NYql::TIssues& issues, EFormatMode mode) override {
  2674. formattedQuery = (mode == EFormatMode::Obfuscate) ? "" : query;
  2675. auto parsedSettings = Settings;
  2676. if (!NSQLTranslation::ParseTranslationSettings(query, parsedSettings, issues)) {
  2677. return false;
  2678. }
  2679. if (parsedSettings.PgParser) {
  2680. return mode != EFormatMode::Obfuscate;
  2681. }
  2682. if (mode == EFormatMode::Obfuscate) {
  2683. auto message = NSQLTranslationV1::SqlAST(query, parsedSettings.File, issues, NSQLTranslation::SQL_MAX_PARSER_ERRORS, parsedSettings.AnsiLexer, parsedSettings.Antlr4Parser, parsedSettings.TestAntlr4, parsedSettings.Arena);
  2684. if (!message) {
  2685. return false;
  2686. }
  2687. TObfuscatingVisitor visitor;
  2688. return Format(visitor.Process(*message), formattedQuery, issues, EFormatMode::Pretty);
  2689. }
  2690. auto lexer = NSQLTranslationV1::MakeLexer(parsedSettings.AnsiLexer, parsedSettings.Antlr4Parser);
  2691. TVector<TString> statements;
  2692. if (!NSQLTranslationV1::SplitQueryToStatements(query, lexer, statements, issues, parsedSettings.File)) {
  2693. return false;
  2694. }
  2695. TStringBuilder finalFormattedQuery;
  2696. bool prevAddLine = false;
  2697. TMaybe<ui32> prevStmtCoreAltCase;
  2698. for (const TString& currentQuery : statements) {
  2699. TVector<NSQLTranslation::TParsedToken> comments;
  2700. TParsedTokenList parsedTokens, stmtTokens;
  2701. auto onNextRawToken = [&](NSQLTranslation::TParsedToken&& token) {
  2702. stmtTokens.push_back(token);
  2703. if (token.Name == "COMMENT") {
  2704. comments.emplace_back(std::move(token));
  2705. } else if (token.Name != "WS" && token.Name != "EOF") {
  2706. parsedTokens.emplace_back(std::move(token));
  2707. }
  2708. };
  2709. if (!lexer->Tokenize(currentQuery, parsedSettings.File, onNextRawToken, issues, NSQLTranslation::SQL_MAX_PARSER_ERRORS)) {
  2710. return false;
  2711. }
  2712. NYql::TIssues parserIssues;
  2713. auto message = NSQLTranslationV1::SqlAST(currentQuery, parsedSettings.File, parserIssues, NSQLTranslation::SQL_MAX_PARSER_ERRORS, parsedSettings.AnsiLexer, parsedSettings.Antlr4Parser, parsedSettings.TestAntlr4, parsedSettings.Arena);
  2714. if (!message) {
  2715. finalFormattedQuery << currentQuery;
  2716. if (!currentQuery.EndsWith("\n")) {
  2717. finalFormattedQuery << "\n";
  2718. }
  2719. continue;
  2720. }
  2721. TPrettyVisitor visitor(parsedTokens, comments, parsedSettings.AnsiLexer);
  2722. bool addLineBefore = false;
  2723. bool addLineAfter = false;
  2724. TMaybe<ui32> stmtCoreAltCase;
  2725. auto currentFormattedQuery = visitor.Process(*message, addLineBefore, addLineAfter, stmtCoreAltCase);
  2726. TParsedTokenList stmtFormattedTokens;
  2727. auto onNextFormattedToken = [&](NSQLTranslation::TParsedToken&& token) {
  2728. stmtFormattedTokens.push_back(token);
  2729. };
  2730. if (!lexer->Tokenize(currentFormattedQuery, parsedSettings.File, onNextFormattedToken, issues, NSQLTranslation::SQL_MAX_PARSER_ERRORS)) {
  2731. return false;
  2732. }
  2733. if (!Validate(stmtTokens, stmtFormattedTokens)) {
  2734. issues.AddIssue(NYql::TIssue({}, TStringBuilder() << "Validation failed: " << currentQuery.Quote() << " != " << currentFormattedQuery.Quote()));
  2735. return false;
  2736. }
  2737. const bool differentStmtAltCase = prevStmtCoreAltCase.Defined() && stmtCoreAltCase != prevStmtCoreAltCase;
  2738. if ((addLineBefore || prevAddLine || differentStmtAltCase) && !finalFormattedQuery.empty()) {
  2739. finalFormattedQuery << "\n";
  2740. }
  2741. prevAddLine = addLineAfter;
  2742. prevStmtCoreAltCase = stmtCoreAltCase;
  2743. finalFormattedQuery << currentFormattedQuery;
  2744. if (parsedTokens.back().Name != "SEMICOLON") {
  2745. finalFormattedQuery << ";\n";
  2746. }
  2747. }
  2748. formattedQuery = finalFormattedQuery;
  2749. return true;
  2750. }
  2751. private:
  2752. const NSQLTranslation::TTranslationSettings Settings;
  2753. };
  2754. }
  2755. ISqlFormatter::TPtr MakeSqlFormatter(const NSQLTranslation::TTranslationSettings& settings) {
  2756. return ISqlFormatter::TPtr(new TSqlFormatter(settings));
  2757. }
  2758. TString MutateQuery(const TString& query, const NSQLTranslation::TTranslationSettings& settings) {
  2759. auto parsedSettings = settings;
  2760. NYql::TIssues issues;
  2761. if (!NSQLTranslation::ParseTranslationSettings(query, parsedSettings, issues)) {
  2762. throw yexception() << issues.ToString();
  2763. }
  2764. auto lexer = NSQLTranslationV1::MakeLexer(parsedSettings.AnsiLexer, parsedSettings.Antlr4Parser);
  2765. TVector<NSQLTranslation::TParsedToken> allTokens;
  2766. auto onNextToken = [&](NSQLTranslation::TParsedToken&& token) {
  2767. if (token.Name != "EOF") {
  2768. allTokens.push_back(token);
  2769. }
  2770. };
  2771. if (!lexer->Tokenize(query, parsedSettings.File, onNextToken, issues, NSQLTranslation::SQL_MAX_PARSER_ERRORS)) {
  2772. throw yexception() << issues.ToString();
  2773. }
  2774. TStringBuilder newQueryBuilder;
  2775. ui32 index = 0;
  2776. for (const auto& x : allTokens) {
  2777. newQueryBuilder << " /*" << index++ << "*/ ";
  2778. newQueryBuilder << x.Content;
  2779. }
  2780. newQueryBuilder << " /*" << index++ << "*/ ";
  2781. return newQueryBuilder;
  2782. }
  2783. bool SqlFormatSimple(const TString& query, TString& formattedQuery, TString& error) {
  2784. try {
  2785. google::protobuf::Arena arena;
  2786. NSQLTranslation::TTranslationSettings settings;
  2787. settings.Arena = &arena;
  2788. auto formatter = MakeSqlFormatter(settings);
  2789. NYql::TIssues issues;
  2790. const bool result = formatter->Format(query, formattedQuery, issues);
  2791. if (!result) {
  2792. error = issues.ToString();
  2793. }
  2794. return result;
  2795. } catch (const std::exception& e) {
  2796. error = e.what();
  2797. return false;
  2798. }
  2799. }
  2800. THashSet<TString> GetKeywords() {
  2801. TString grammar;
  2802. // ANTLR4-MIGRATION: just change SQLv1 to SQLv1Antlr4
  2803. Y_ENSURE(NResource::FindExact("SQLv1.g.in", &grammar));
  2804. THashSet<TString> res;
  2805. TVector<TString> lines;
  2806. Split(grammar, "\n", lines);
  2807. for (auto s : lines) {
  2808. s = StripString(s);
  2809. if (s.StartsWith("//")) {
  2810. continue;
  2811. }
  2812. auto pos1 = s.find(':');
  2813. auto pos2 = s.find(';');
  2814. if (pos1 == TString::npos || pos2 == TString::npos || pos2 < pos1 + 2) {
  2815. continue;
  2816. }
  2817. auto before = s.substr(0, pos1);
  2818. auto after = s.substr(pos1 + 1, pos2 - pos1 - 1);
  2819. SubstGlobal(after, " ", "");
  2820. SubstGlobal(after, "'", "");
  2821. if (after == before) {
  2822. //Cerr << before << "\n";
  2823. res.insert(before);
  2824. }
  2825. }
  2826. return res;
  2827. }
  2828. } // namespace NSQLFormat