sql_format.cpp 113 KB

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