node.cpp 100 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584
  1. #include "node.h"
  2. #include "source.h"
  3. #include "context.h"
  4. #include <yql/essentials/ast/yql_ast_escaping.h>
  5. #include <yql/essentials/ast/yql_expr.h>
  6. #include <yql/essentials/core/sql_types/simple_types.h>
  7. #include <yql/essentials/minikql/mkql_type_ops.h>
  8. #include <yql/essentials/parser/pg_catalog/catalog.h>
  9. #include <yql/essentials/utils/yql_panic.h>
  10. #include <library/cpp/containers/stack_vector/stack_vec.h>
  11. #include <library/cpp/charset/ci_string.h>
  12. #include <util/generic/hash_set.h>
  13. #include <util/stream/str.h>
  14. #include <util/string/cast.h>
  15. #include <util/string/escape.h>
  16. #include <util/string/subst.h>
  17. using namespace NYql;
  18. namespace NSQLTranslationV1 {
  19. TString ErrorDistinctWithoutCorrelation(const TString& column) {
  20. return TStringBuilder() << "DISTINCT columns for JOIN in SELECT should have table aliases (correlation name),"
  21. " add it if necessary to FROM section over 'AS <alias>' keyword and put it like '<alias>." << column << "'";
  22. }
  23. TString ErrorDistinctByGroupKey(const TString& column) {
  24. return TStringBuilder() << "Unable to use DISTINCT by grouping column: " << column << ". You should leave one of them.";
  25. }
  26. TTopicRef::TTopicRef(const TString& refName, const TDeferredAtom& cluster, TNodePtr keys)
  27. : RefName(refName)
  28. , Cluster(cluster)
  29. , Keys(keys)
  30. {
  31. }
  32. TColumnConstraints::TColumnConstraints(TNodePtr defaultExpr, bool nullable)
  33. : DefaultExpr(defaultExpr)
  34. , Nullable(nullable)
  35. {
  36. }
  37. TColumnSchema::TColumnSchema(TPosition pos, const TString& name, const TNodePtr& type, bool nullable,
  38. TVector<TIdentifier> families, bool serial, TNodePtr defaultExpr, ETypeOfChange typeOfChange)
  39. : Pos(pos)
  40. , Name(name)
  41. , Type(type)
  42. , Nullable(nullable)
  43. , Families(families)
  44. , Serial(serial)
  45. , DefaultExpr(defaultExpr)
  46. , TypeOfChange(typeOfChange)
  47. {
  48. }
  49. INode::INode(TPosition pos)
  50. : Pos(pos)
  51. {
  52. }
  53. INode::~INode()
  54. {
  55. }
  56. TPosition INode::GetPos() const {
  57. return Pos;
  58. }
  59. const TString& INode::GetLabel() const {
  60. return Label;
  61. }
  62. TMaybe<TPosition> INode::GetLabelPos() const {
  63. return LabelPos;
  64. }
  65. void INode::SetLabel(const TString& label, TMaybe<TPosition> pos) {
  66. Label = label;
  67. LabelPos = pos;
  68. }
  69. bool INode::IsImplicitLabel() const {
  70. return ImplicitLabel;
  71. }
  72. void INode::MarkImplicitLabel(bool isImplicitLabel) {
  73. ImplicitLabel = isImplicitLabel;
  74. }
  75. void INode::SetRefPos(TPosition pos) {
  76. RefPos = pos;
  77. }
  78. TMaybe<TPosition> INode::GetRefPos() const {
  79. return RefPos;
  80. }
  81. void INode::SetCountHint(bool isCount) {
  82. State.Set(ENodeState::CountHint, isCount);
  83. }
  84. bool INode::GetCountHint() const {
  85. return State.Test(ENodeState::CountHint);
  86. }
  87. bool INode::IsConstant() const {
  88. return HasState(ENodeState::Const);
  89. }
  90. bool INode::MaybeConstant() const {
  91. return HasState(ENodeState::MaybeConst);
  92. }
  93. bool INode::IsAggregated() const {
  94. return HasState(ENodeState::Aggregated);
  95. }
  96. bool INode::IsAggregationKey() const {
  97. return HasState(ENodeState::AggregationKey);
  98. }
  99. bool INode::IsOverWindow() const {
  100. return HasState(ENodeState::OverWindow);
  101. }
  102. bool INode::IsOverWindowDistinct() const {
  103. return HasState(ENodeState::OverWindowDistinct);
  104. }
  105. bool INode::IsNull() const {
  106. return false;
  107. }
  108. bool INode::IsLiteral() const {
  109. return false;
  110. }
  111. TString INode::GetLiteralType() const {
  112. return "";
  113. }
  114. TString INode::GetLiteralValue() const {
  115. return "";
  116. }
  117. bool INode::IsIntegerLiteral() const {
  118. return false;
  119. }
  120. INode::TPtr INode::ApplyUnaryOp(TContext& ctx, TPosition pos, const TString& opName) const {
  121. Y_UNUSED(ctx);
  122. if (IsNull()) {
  123. return BuildLiteralNull(pos);
  124. }
  125. return new TCallNodeImpl(pos, opName, { Clone() });
  126. }
  127. bool INode::IsAsterisk() const {
  128. return false;
  129. }
  130. const TString* INode::SubqueryAlias() const {
  131. return nullptr;
  132. }
  133. TString INode::GetOpName() const {
  134. return TString();
  135. }
  136. const TString* INode::GetLiteral(const TString& type) const {
  137. Y_UNUSED(type);
  138. return nullptr;
  139. }
  140. const TString* INode::GetColumnName() const {
  141. return nullptr;
  142. }
  143. bool INode::IsPlainColumn() const {
  144. return GetColumnName() != nullptr;
  145. }
  146. bool INode::IsTableRow() const {
  147. return false;
  148. }
  149. void INode::AssumeColumn() {
  150. }
  151. const TString* INode::GetSourceName() const {
  152. return nullptr;
  153. }
  154. const TString* INode::GetAtomContent() const {
  155. return nullptr;
  156. }
  157. bool INode::IsOptionalArg() const {
  158. return false;
  159. }
  160. size_t INode::GetTupleSize() const {
  161. return 0;
  162. }
  163. INode::TPtr INode::GetTupleElement(size_t index) const {
  164. Y_UNUSED(index);
  165. return nullptr;
  166. }
  167. ITableKeys* INode::GetTableKeys() {
  168. return nullptr;
  169. }
  170. ISource* INode::GetSource() {
  171. return nullptr;
  172. }
  173. TVector<TNodePtr>* INode::ContentListPtr() {
  174. return nullptr;
  175. }
  176. bool INode::Init(TContext& ctx, ISource* src) {
  177. if (State.Test(ENodeState::Failed)) {
  178. return false;
  179. }
  180. if (!State.Test(ENodeState::Initialized)) {
  181. if (!DoInit(ctx, src)) {
  182. State.Set(ENodeState::Failed);
  183. return false;
  184. }
  185. State.Set(ENodeState::Initialized);
  186. }
  187. return true;
  188. }
  189. bool INode::InitReference(TContext& ctx) {
  190. Y_UNUSED(ctx);
  191. return true;
  192. }
  193. bool INode::DoInit(TContext& ctx, ISource* src) {
  194. Y_UNUSED(ctx);
  195. Y_UNUSED(src);
  196. return true;
  197. }
  198. TNodePtr INode::AstNode() const {
  199. return new TAstListNodeImpl(Pos);
  200. }
  201. TNodePtr INode::AstNode(TNodePtr node) const {
  202. return node;
  203. }
  204. TNodePtr INode::AstNode(const TString& str) const {
  205. return new TAstAtomNodeImpl(Pos, str, TNodeFlags::Default);
  206. }
  207. TNodePtr INode::AstNode(TAstNode* node) const {
  208. return new TAstDirectNode(node);
  209. }
  210. TNodePtr INode::Clone() const {
  211. TNodePtr clone = DoClone();
  212. if (!clone) {
  213. clone = const_cast<INode*>(this);
  214. } else {
  215. YQL_ENSURE(!State.Test(ENodeState::Initialized), "Clone should be for uninitialized or persistent node");
  216. clone->SetLabel(Label, LabelPos);
  217. clone->MarkImplicitLabel(ImplicitLabel);
  218. }
  219. return clone;
  220. }
  221. TAggregationPtr INode::GetAggregation() const {
  222. return {};
  223. }
  224. void INode::CollectPreaggregateExprs(TContext& ctx, ISource& src, TVector<INode::TPtr>& exprs) {
  225. Y_UNUSED(ctx);
  226. Y_UNUSED(src);
  227. Y_UNUSED(exprs);
  228. }
  229. INode::TPtr INode::WindowSpecFunc(const TPtr& type) const {
  230. Y_UNUSED(type);
  231. return {};
  232. }
  233. bool INode::SetViewName(TContext& ctx, TPosition pos, const TString& view) {
  234. Y_UNUSED(pos);
  235. Y_UNUSED(view);
  236. ctx.Error() << "Node not support views";
  237. return false;
  238. }
  239. bool INode::SetPrimaryView(TContext& ctx, TPosition pos) {
  240. Y_UNUSED(pos);
  241. ctx.Error() << "Node not support primary views";
  242. return false;
  243. }
  244. void INode::UseAsInner() {
  245. AsInner = true;
  246. }
  247. void INode::DisableSort() {
  248. DisableSort_ = true;
  249. }
  250. bool INode::UsedSubquery() const {
  251. return false;
  252. }
  253. bool INode::IsSelect() const {
  254. return false;
  255. }
  256. bool INode::HasSelectResult() const {
  257. return false;
  258. }
  259. const TString* INode::FuncName() const {
  260. return nullptr;
  261. }
  262. const TString* INode::ModuleName() const {
  263. return nullptr;
  264. }
  265. bool INode::IsScript() const {
  266. return false;
  267. }
  268. bool INode::HasSkip() const {
  269. return false;
  270. }
  271. TColumnNode* INode::GetColumnNode() {
  272. return nullptr;
  273. }
  274. const TColumnNode* INode::GetColumnNode() const {
  275. return nullptr;
  276. }
  277. TTupleNode* INode::GetTupleNode() {
  278. return nullptr;
  279. }
  280. const TTupleNode* INode::GetTupleNode() const {
  281. return nullptr;
  282. }
  283. TCallNode* INode::GetCallNode() {
  284. return nullptr;
  285. }
  286. const TCallNode* INode::GetCallNode() const {
  287. return nullptr;
  288. }
  289. TStructNode* INode::GetStructNode() {
  290. return nullptr;
  291. }
  292. const TStructNode* INode::GetStructNode() const {
  293. return nullptr;
  294. }
  295. TAccessNode* INode::GetAccessNode() {
  296. return nullptr;
  297. }
  298. const TAccessNode* INode::GetAccessNode() const {
  299. return nullptr;
  300. }
  301. TLambdaNode* INode::GetLambdaNode() {
  302. return nullptr;
  303. }
  304. const TLambdaNode* INode::GetLambdaNode() const {
  305. return nullptr;
  306. }
  307. TUdfNode* INode::GetUdfNode() {
  308. return nullptr;
  309. }
  310. const TUdfNode* INode::GetUdfNode() const {
  311. return nullptr;
  312. }
  313. void INode::VisitTree(const TVisitFunc& func) const {
  314. TVisitNodeSet visited;
  315. VisitTree(func, visited);
  316. }
  317. void INode::VisitTree(const TVisitFunc& func, TVisitNodeSet& visited) const {
  318. if (visited.emplace(this).second && HasState(ENodeState::Initialized) && func(*this)) {
  319. DoVisitChildren(func, visited);
  320. }
  321. }
  322. TNodePtr INode::ShallowCopy() const {
  323. Y_DEBUG_ABORT_UNLESS(false, "Node is not copyable");
  324. return nullptr;
  325. }
  326. void INode::DoUpdateState() const {
  327. }
  328. void INode::PrecacheState() const {
  329. if (State.Test(ENodeState::Failed)) {
  330. return;
  331. }
  332. /// Not work right now! It's better use Init at first, because some kind of update depend on it
  333. /// \todo turn on and remove all issues
  334. //Y_DEBUG_ABORT_UNLESS(State.Test(ENodeState::Initialized));
  335. if (State.Test(ENodeState::Precached)) {
  336. return;
  337. }
  338. DoUpdateState();
  339. State.Set(ENodeState::Precached);
  340. }
  341. void INode::DoVisitChildren(const TVisitFunc& func, TVisitNodeSet& visited) const {
  342. Y_UNUSED(func);
  343. Y_UNUSED(visited);
  344. }
  345. void INode::DoAdd(TNodePtr node) {
  346. Y_UNUSED(node);
  347. Y_DEBUG_ABORT_UNLESS(false, "Node is not expandable");
  348. }
  349. bool IProxyNode::IsNull() const {
  350. return Inner->IsNull();
  351. }
  352. bool IProxyNode::IsLiteral() const {
  353. return Inner->IsNull();
  354. }
  355. TString IProxyNode::GetLiteralType() const {
  356. return Inner->GetLiteralType();
  357. }
  358. TString IProxyNode::GetLiteralValue() const {
  359. return Inner->GetLiteralValue();
  360. }
  361. bool IProxyNode::IsIntegerLiteral() const {
  362. return Inner->IsIntegerLiteral();
  363. }
  364. INode::TPtr IProxyNode::ApplyUnaryOp(TContext& ctx, TPosition pos, const TString& opName) const {
  365. return Inner->ApplyUnaryOp(ctx, pos, opName);
  366. }
  367. bool IProxyNode::IsAsterisk() const {
  368. return Inner->IsAsterisk();
  369. }
  370. const TString* IProxyNode::SubqueryAlias() const {
  371. return Inner->SubqueryAlias();
  372. }
  373. TString IProxyNode::GetOpName() const {
  374. return Inner->GetOpName();
  375. }
  376. const TString* IProxyNode::GetLiteral(const TString& type) const {
  377. return Inner->GetLiteral(type);
  378. }
  379. const TString* IProxyNode::GetColumnName() const {
  380. return Inner->GetColumnName();
  381. }
  382. bool IProxyNode::IsPlainColumn() const {
  383. return Inner->IsPlainColumn();
  384. }
  385. bool IProxyNode::IsTableRow() const {
  386. return Inner->IsTableRow();
  387. }
  388. void IProxyNode::AssumeColumn() {
  389. Inner->AssumeColumn();
  390. }
  391. const TString* IProxyNode::GetSourceName() const {
  392. return Inner->GetSourceName();
  393. }
  394. const TString* IProxyNode::GetAtomContent() const {
  395. return Inner->GetAtomContent();
  396. }
  397. bool IProxyNode::IsOptionalArg() const {
  398. return Inner->IsOptionalArg();
  399. }
  400. size_t IProxyNode::GetTupleSize() const {
  401. return Inner->GetTupleSize();
  402. }
  403. INode::TPtr IProxyNode::GetTupleElement(size_t index) const {
  404. return Inner->GetTupleElement(index);
  405. }
  406. ITableKeys* IProxyNode::GetTableKeys() {
  407. return Inner->GetTableKeys();
  408. }
  409. ISource* IProxyNode::GetSource() {
  410. return Inner->GetSource();
  411. }
  412. TVector<INode::TPtr>* IProxyNode::ContentListPtr() {
  413. return Inner->ContentListPtr();
  414. }
  415. TAggregationPtr IProxyNode::GetAggregation() const {
  416. return Inner->GetAggregation();
  417. }
  418. void IProxyNode::CollectPreaggregateExprs(TContext& ctx, ISource& src, TVector<INode::TPtr>& exprs) {
  419. Inner->CollectPreaggregateExprs(ctx, src, exprs);
  420. }
  421. INode::TPtr IProxyNode::WindowSpecFunc(const TPtr& type) const {
  422. return Inner->WindowSpecFunc(type);
  423. }
  424. bool IProxyNode::SetViewName(TContext& ctx, TPosition pos, const TString& view) {
  425. return Inner->SetViewName(ctx, pos, view);
  426. }
  427. bool IProxyNode::SetPrimaryView(TContext& ctx, TPosition pos) {
  428. return Inner->SetPrimaryView(ctx, pos);
  429. }
  430. bool IProxyNode::UsedSubquery() const {
  431. return Inner->UsedSubquery();
  432. }
  433. bool IProxyNode::IsSelect() const {
  434. return Inner->IsSelect();
  435. }
  436. bool IProxyNode::HasSelectResult() const {
  437. return Inner->HasSelectResult();
  438. }
  439. const TString* IProxyNode::FuncName() const {
  440. return Inner->FuncName();
  441. }
  442. const TString* IProxyNode::ModuleName() const {
  443. return Inner->ModuleName();
  444. }
  445. bool IProxyNode::IsScript() const {
  446. return Inner->IsScript();
  447. }
  448. bool IProxyNode::HasSkip() const {
  449. return Inner->HasSkip();
  450. }
  451. TColumnNode* IProxyNode::GetColumnNode() {
  452. return Inner->GetColumnNode();
  453. }
  454. const TColumnNode* IProxyNode::GetColumnNode() const {
  455. return static_cast<const INode*>(Inner.Get())->GetColumnNode();
  456. }
  457. TTupleNode* IProxyNode::GetTupleNode() {
  458. return Inner->GetTupleNode();
  459. }
  460. const TTupleNode* IProxyNode::GetTupleNode() const {
  461. return static_cast<const INode*>(Inner.Get())->GetTupleNode();
  462. }
  463. TCallNode* IProxyNode::GetCallNode() {
  464. return Inner->GetCallNode();
  465. }
  466. const TCallNode* IProxyNode::GetCallNode() const {
  467. return static_cast<const INode*>(Inner.Get())->GetCallNode();
  468. }
  469. TStructNode* IProxyNode::GetStructNode() {
  470. return Inner->GetStructNode();
  471. }
  472. const TStructNode* IProxyNode::GetStructNode() const {
  473. return static_cast<const INode*>(Inner.Get())->GetStructNode();
  474. }
  475. TAccessNode* IProxyNode::GetAccessNode() {
  476. return Inner->GetAccessNode();
  477. }
  478. const TAccessNode* IProxyNode::GetAccessNode() const {
  479. return static_cast<const INode*>(Inner.Get())->GetAccessNode();
  480. }
  481. TLambdaNode* IProxyNode::GetLambdaNode() {
  482. return Inner->GetLambdaNode();
  483. }
  484. const TLambdaNode* IProxyNode::GetLambdaNode() const {
  485. return static_cast<const INode*>(Inner.Get())->GetLambdaNode();
  486. }
  487. TUdfNode* IProxyNode::GetUdfNode() {
  488. return Inner->GetUdfNode();
  489. }
  490. const TUdfNode* IProxyNode::GetUdfNode() const {
  491. return static_cast<const INode*>(Inner.Get())->GetUdfNode();
  492. }
  493. void IProxyNode::DoUpdateState() const {
  494. static_assert(static_cast<int>(ENodeState::End) == 10, "Need to support new states here");
  495. State.Set(ENodeState::CountHint, Inner->GetCountHint());
  496. State.Set(ENodeState::Const, Inner->IsConstant());
  497. State.Set(ENodeState::MaybeConst, Inner->MaybeConstant());
  498. State.Set(ENodeState::Aggregated, Inner->IsAggregated());
  499. State.Set(ENodeState::AggregationKey, Inner->IsAggregationKey());
  500. State.Set(ENodeState::OverWindow, Inner->IsOverWindow());
  501. State.Set(ENodeState::OverWindowDistinct, Inner->IsOverWindowDistinct());
  502. }
  503. void IProxyNode::DoVisitChildren(const TVisitFunc& func, TVisitNodeSet& visited) const {
  504. Inner->VisitTree(func, visited);
  505. }
  506. bool IProxyNode::InitReference(TContext& ctx) {
  507. return Inner->InitReference(ctx);
  508. }
  509. bool IProxyNode::DoInit(TContext& ctx, ISource* src) {
  510. return Inner->Init(ctx, src);
  511. }
  512. void IProxyNode::DoAdd(TPtr node) {
  513. Inner->Add(node);
  514. }
  515. void MergeHints(TTableHints& base, const TTableHints& overrides) {
  516. for (auto& i : overrides) {
  517. base[i.first] = i.second;
  518. }
  519. }
  520. TTableHints CloneContainer(const TTableHints& hints) {
  521. TTableHints result;
  522. for (auto& [name, nodes] : hints) {
  523. result.emplace(std::make_pair(name, CloneContainer(nodes)));
  524. }
  525. return result;
  526. }
  527. TAstAtomNode::TAstAtomNode(TPosition pos, const TString& content, ui32 flags, bool isOptionalArg)
  528. : INode(pos)
  529. , Content(content)
  530. , Flags(flags)
  531. , IsOptionalArg_(isOptionalArg)
  532. {
  533. }
  534. TAstAtomNode::~TAstAtomNode()
  535. {
  536. }
  537. void TAstAtomNode::DoUpdateState() const {
  538. State.Set(ENodeState::Const);
  539. }
  540. TAstNode* TAstAtomNode::Translate(TContext& ctx) const {
  541. return TAstNode::NewAtom(Pos, Content, *ctx.Pool, Flags);
  542. }
  543. const TString* TAstAtomNode::GetAtomContent() const {
  544. return &Content;
  545. }
  546. bool TAstAtomNode::IsOptionalArg() const {
  547. return IsOptionalArg_;
  548. }
  549. TAstDirectNode::TAstDirectNode(TAstNode* node)
  550. : INode(node->GetPosition())
  551. , Node(node)
  552. {
  553. }
  554. TAstNode* TAstDirectNode::Translate(TContext& ctx) const {
  555. Y_UNUSED(ctx);
  556. return Node;
  557. }
  558. TNodePtr BuildList(TPosition pos, TVector<TNodePtr> nodes) {
  559. return new TAstListNodeImpl(pos, std::move(nodes));
  560. }
  561. TNodePtr BuildQuote(TPosition pos, TNodePtr expr) {
  562. return BuildList(pos, {BuildAtom(pos, "quote", TNodeFlags::Default), expr});
  563. }
  564. TNodePtr BuildAtom(TPosition pos, const TString& content, ui32 flags, bool isOptionalArg) {
  565. return new TAstAtomNodeImpl(pos, content, flags, isOptionalArg);
  566. }
  567. TAstListNode::TAstListNode(TPosition pos)
  568. : INode(pos)
  569. {
  570. }
  571. TAstListNode::~TAstListNode()
  572. {
  573. }
  574. bool TAstListNode::DoInit(TContext& ctx, ISource* src) {
  575. for (auto& node: Nodes) {
  576. if (!node->Init(ctx, src)) {
  577. return false;
  578. }
  579. }
  580. return true;
  581. }
  582. TAstNode* TAstListNode::Translate(TContext& ctx) const {
  583. TSmallVec<TAstNode*> children;
  584. children.reserve(Nodes.size());
  585. auto listPos = Pos;
  586. for (auto& node: Nodes) {
  587. if (node) {
  588. auto astNode = node->Translate(ctx);
  589. if (!astNode) {
  590. return nullptr;
  591. }
  592. children.push_back(astNode);
  593. } else {
  594. ctx.Error(Pos) << "Translation error: encountered empty TNodePtr";
  595. return nullptr;
  596. }
  597. }
  598. return TAstNode::NewList(listPos, children.data(), children.size(), *ctx.Pool);
  599. }
  600. void TAstListNode::UpdateStateByListNodes(const TVector<TNodePtr>& nodes) const {
  601. bool isConst = true;
  602. struct TAttributesFlags {
  603. bool has = false;
  604. bool all = true;
  605. };
  606. std::array<ENodeState, 3> checkStates = {{ENodeState::Aggregated, ENodeState::AggregationKey, ENodeState::OverWindow}};
  607. std::map<ENodeState, TAttributesFlags> flags;
  608. for (auto& node: nodes) {
  609. const bool isNodeConst = node->IsConstant();
  610. const bool isNodeMaybeConst = node->MaybeConstant();
  611. for (auto state: checkStates) {
  612. if (node->HasState(state)) {
  613. flags[state].has = true;
  614. } else if (!isNodeConst && !isNodeMaybeConst) {
  615. flags[state].all = false;
  616. }
  617. if (!isNodeConst) {
  618. isConst = false;
  619. }
  620. }
  621. }
  622. State.Set(ENodeState::Const, isConst);
  623. for (auto& flag: flags) {
  624. State.Set(flag.first, flag.second.has && flag.second.all);
  625. }
  626. State.Set(ENodeState::MaybeConst, !isConst && AllOf(nodes, [](const auto& node) { return node->IsConstant() || node->MaybeConstant(); }));
  627. }
  628. void TAstListNode::DoUpdateState() const {
  629. UpdateStateByListNodes(Nodes);
  630. }
  631. void TAstListNode::DoVisitChildren(const TVisitFunc& func, TVisitNodeSet& visited) const {
  632. for (auto& node : Nodes) {
  633. node->VisitTree(func, visited);
  634. }
  635. }
  636. TAstListNode::TAstListNode(const TAstListNode& node)
  637. : INode(node.Pos)
  638. , Nodes(node.Nodes)
  639. {
  640. Label = node.Label;
  641. State = node.State;
  642. }
  643. TAstListNode::TAstListNode(TPosition pos, TVector<TNodePtr>&& nodes)
  644. : INode(pos)
  645. , Nodes(std::move(nodes))
  646. {
  647. for (const auto& node: Nodes) {
  648. YQL_ENSURE(node, "Null ptr passed as list element");
  649. }
  650. }
  651. TNodePtr TAstListNode::ShallowCopy() const {
  652. return new TAstListNodeImpl(Pos, Nodes);
  653. }
  654. void TAstListNode::DoAdd(TNodePtr node) {
  655. Y_DEBUG_ABORT_UNLESS(node);
  656. Y_DEBUG_ABORT_UNLESS(node.Get() != this);
  657. Nodes.push_back(node);
  658. }
  659. TAstListNodeImpl::TAstListNodeImpl(TPosition pos)
  660. : TAstListNode(pos)
  661. {}
  662. TAstListNodeImpl::TAstListNodeImpl(TPosition pos, TVector<TNodePtr> nodes)
  663. : TAstListNode(pos)
  664. {
  665. for (const auto& node: nodes) {
  666. YQL_ENSURE(node, "Null ptr passed as list element");
  667. }
  668. Nodes.swap(nodes);
  669. }
  670. void TAstListNodeImpl::CollectPreaggregateExprs(TContext& ctx, ISource& src, TVector<INode::TPtr>& exprs) {
  671. for (auto& node : Nodes) {
  672. node->CollectPreaggregateExprs(ctx, src, exprs);
  673. }
  674. }
  675. TNodePtr TAstListNodeImpl::DoClone() const {
  676. return new TAstListNodeImpl(Pos, CloneContainer(Nodes));
  677. }
  678. TCallNode::TCallNode(TPosition pos, const TString& opName, i32 minArgs, i32 maxArgs, const TVector<TNodePtr>& args)
  679. : TAstListNode(pos)
  680. , OpName(opName)
  681. , MinArgs(minArgs)
  682. , MaxArgs(maxArgs)
  683. , Args(args)
  684. {
  685. for (const auto& arg: Args) {
  686. YQL_ENSURE(arg, "Null ptr passed as call argument");
  687. }
  688. }
  689. TString TCallNode::GetOpName() const {
  690. return OpName;
  691. }
  692. const TString* DeriveCommonSourceName(const TVector<TNodePtr> &nodes) {
  693. const TString* name = nullptr;
  694. for (auto& node: nodes) {
  695. auto n = node->GetSourceName();
  696. if (!n) {
  697. continue;
  698. }
  699. if (name && *n != *name) {
  700. return nullptr;
  701. }
  702. name = n;
  703. }
  704. return name;
  705. }
  706. const TString* TCallNode::GetSourceName() const {
  707. return DeriveCommonSourceName(Args);
  708. }
  709. const TVector<TNodePtr>& TCallNode::GetArgs() const {
  710. return Args;
  711. }
  712. void TCallNode::DoUpdateState() const {
  713. UpdateStateByListNodes(Args);
  714. }
  715. TString TCallNode::GetCallExplain() const {
  716. auto derivedName = GetOpName();
  717. TStringBuilder sb;
  718. sb << derivedName << "()";
  719. if (derivedName != OpName) {
  720. sb << ", converted to " << OpName << "()";
  721. }
  722. return std::move(sb);
  723. }
  724. void TCallNode::CollectPreaggregateExprs(TContext& ctx, ISource& src, TVector<INode::TPtr>& exprs) {
  725. for (auto& arg : Args) {
  726. arg->CollectPreaggregateExprs(ctx, src, exprs);
  727. }
  728. }
  729. bool TCallNode::ValidateArguments(TContext& ctx) const {
  730. const auto argsCount = static_cast<i32>(Args.size());
  731. if (MinArgs >= 0 && MaxArgs == MinArgs && argsCount != MinArgs) {
  732. ctx.Error(Pos) << GetCallExplain() << " requires exactly " << MinArgs << " arguments, given: " << Args.size();
  733. return false;
  734. }
  735. if (MinArgs >= 0 && argsCount < MinArgs) {
  736. ctx.Error(Pos) << GetCallExplain() << " requires at least " << MinArgs << " arguments, given: " << Args.size();
  737. return false;
  738. }
  739. if (MaxArgs >= 0 && argsCount > MaxArgs) {
  740. ctx.Error(Pos) << GetCallExplain() << " requires at most " << MaxArgs << " arguments, given: " << Args.size();
  741. return false;
  742. }
  743. return true;
  744. }
  745. bool TCallNode::DoInit(TContext& ctx, ISource* src) {
  746. if (!ValidateArguments(ctx)) {
  747. return false;
  748. }
  749. bool hasError = false;
  750. for (auto& arg: Args) {
  751. if (!arg->Init(ctx, src)) {
  752. hasError = true;
  753. continue;
  754. }
  755. }
  756. if (hasError) {
  757. return false;
  758. }
  759. Nodes.push_back(BuildAtom(Pos, OpName,
  760. OpName.cend() == std::find_if_not(OpName.cbegin(), OpName.cend(), [](char c) { return bool(std::isalnum(c)); }) ? TNodeFlags::Default : TNodeFlags::ArbitraryContent));
  761. Nodes.insert(Nodes.end(), Args.begin(), Args.end());
  762. return true;
  763. }
  764. TCallNode* TCallNode::GetCallNode() {
  765. return this;
  766. }
  767. const TCallNode* TCallNode::GetCallNode() const {
  768. return this;
  769. }
  770. TCallNodeImpl::TCallNodeImpl(TPosition pos, const TString& opName, i32 minArgs, i32 maxArgs, const TVector<TNodePtr>& args)
  771. : TCallNode(pos, opName, minArgs, maxArgs, args)
  772. {}
  773. TCallNodeImpl::TCallNodeImpl(TPosition pos, const TString& opName, const TVector<TNodePtr>& args)
  774. : TCallNode(pos, opName, args.size(), args.size(), args)
  775. {}
  776. TCallNode::TPtr TCallNodeImpl::DoClone() const {
  777. return new TCallNodeImpl(GetPos(), OpName, MinArgs, MaxArgs, CloneContainer(Args));
  778. }
  779. TFuncNodeImpl::TFuncNodeImpl(TPosition pos, const TString& opName)
  780. : TCallNode(pos, opName, 0, 0, {})
  781. {}
  782. TCallNode::TPtr TFuncNodeImpl::DoClone() const {
  783. return new TFuncNodeImpl(GetPos(), OpName);
  784. }
  785. const TString* TFuncNodeImpl::FuncName() const {
  786. return &OpName;
  787. }
  788. TCallNodeDepArgs::TCallNodeDepArgs(ui32 reqArgsCount, TPosition pos, const TString& opName, i32 minArgs, i32 maxArgs, const TVector<TNodePtr>& args)
  789. : TCallNode(pos, opName, minArgs, maxArgs, args)
  790. , ReqArgsCount(reqArgsCount)
  791. {}
  792. TCallNodeDepArgs::TCallNodeDepArgs(ui32 reqArgsCount, TPosition pos, const TString& opName, const TVector<TNodePtr>& args)
  793. : TCallNode(pos, opName, args.size(), args.size(), args)
  794. , ReqArgsCount(reqArgsCount)
  795. {}
  796. TCallNode::TPtr TCallNodeDepArgs::DoClone() const {
  797. return new TCallNodeDepArgs(ReqArgsCount, GetPos(), OpName, MinArgs, MaxArgs, CloneContainer(Args));
  798. }
  799. bool TCallNodeDepArgs::DoInit(TContext& ctx, ISource* src) {
  800. if (!TCallNode::DoInit(ctx, src)) {
  801. return false;
  802. }
  803. for (ui32 i = 1 + ReqArgsCount; i < Nodes.size(); ++i) {
  804. Nodes[i] = Y("DependsOn", Nodes[i]);
  805. }
  806. return true;
  807. }
  808. TCallDirectRow::TPtr TCallDirectRow::DoClone() const {
  809. return new TCallDirectRow(Pos, OpName, CloneContainer(Args));
  810. }
  811. TCallDirectRow::TCallDirectRow(TPosition pos, const TString& opName, i32 minArgs, i32 maxArgs, const TVector<TNodePtr>& args)
  812. : TCallNode(pos, opName, minArgs, maxArgs, args)
  813. {}
  814. TCallDirectRow::TCallDirectRow(TPosition pos, const TString& opName, const TVector<TNodePtr>& args)
  815. : TCallNode(pos, opName, 0, 0, args)
  816. {}
  817. bool TCallDirectRow::DoInit(TContext& ctx, ISource* src) {
  818. if (!src || (ctx.CompactNamedExprs && src->IsFake())) {
  819. ctx.Error(Pos) << "Unable to use function: " << OpName << " without source";
  820. return false;
  821. }
  822. if (src->IsCompositeSource() || src->GetJoin() || src->HasAggregations() || src->IsFlattenByColumns() || src->IsOverWindowSource()) {
  823. ctx.Error(Pos) << "Failed to use function: " << OpName << " with aggregation, join, flatten by or window functions";
  824. return false;
  825. }
  826. if (!TCallNode::DoInit(ctx, src)) {
  827. return false;
  828. }
  829. Nodes.push_back(Y("DependsOn", "row"));
  830. return true;
  831. }
  832. void TCallDirectRow::DoUpdateState() const {
  833. State.Set(ENodeState::Const, false);
  834. }
  835. void TWinAggrEmulation::DoUpdateState() const {
  836. State.Set(ENodeState::OverWindow, true);
  837. }
  838. bool TWinAggrEmulation::DoInit(TContext& ctx, ISource* src) {
  839. if (!src) {
  840. ctx.Error(Pos) << "Unable to use window function " << OpName << " without source";
  841. return false;
  842. }
  843. if (!src->IsOverWindowSource()) {
  844. ctx.Error(Pos) << "Failed to use window function " << OpName << " without window specification";
  845. return false;
  846. }
  847. if (!src->AddFuncOverWindow(ctx, this)) {
  848. ctx.Error(Pos) << "Failed to use window function " << OpName << " without window specification or in wrong place";
  849. return false;
  850. }
  851. FuncAlias = "_yql_" + src->MakeLocalName(OpName);
  852. src->AddTmpWindowColumn(FuncAlias);
  853. if (!TCallNode::DoInit(ctx, src)) {
  854. return false;
  855. }
  856. Nodes.clear();
  857. Add("Member", "row", Q(FuncAlias));
  858. return true;
  859. }
  860. INode::TPtr TWinAggrEmulation::WindowSpecFunc(const TPtr& type) const {
  861. auto result = Y(OpName, type);
  862. for (const auto& arg: Args) {
  863. result = L(result, arg);
  864. }
  865. return Q(Y(Q(FuncAlias), result));
  866. }
  867. TWinAggrEmulation::TWinAggrEmulation(TPosition pos, const TString& opName, i32 minArgs, i32 maxArgs, const TVector<TNodePtr>& args)
  868. : TCallNode(pos, opName, minArgs, maxArgs, args)
  869. , FuncAlias(opName)
  870. {}
  871. TWinRowNumber::TWinRowNumber(TPosition pos, const TString& opName, i32 minArgs, i32 maxArgs, const TVector<TNodePtr>& args)
  872. : TWinAggrEmulation(pos, opName, minArgs, maxArgs, args)
  873. {}
  874. TWinCumeDist::TWinCumeDist(TPosition pos, const TString& opName, i32 minArgs, i32 maxArgs, const TVector<TNodePtr>& args)
  875. : TWinAggrEmulation(pos, opName, minArgs, maxArgs, args)
  876. {}
  877. bool TWinCumeDist::DoInit(TContext& ctx, ISource* src) {
  878. if (!ValidateArguments(ctx)) {
  879. return false;
  880. }
  881. YQL_ENSURE(Args.size() == 0);
  882. TVector<TNodePtr> optionsElements;
  883. if (ctx.AnsiCurrentRow) {
  884. optionsElements.push_back(BuildTuple(Pos, { BuildQuotedAtom(Pos, "ansi", NYql::TNodeFlags::Default) }));
  885. }
  886. Args.push_back(BuildTuple(Pos, optionsElements));
  887. MinArgs = MaxArgs = 1;
  888. if (!TWinAggrEmulation::DoInit(ctx, src)) {
  889. return false;
  890. }
  891. YQL_ENSURE(Args.size() == 1);
  892. return true;
  893. }
  894. TWinNTile::TWinNTile(TPosition pos, const TString& opName, i32 minArgs, i32 maxArgs, const TVector<TNodePtr>& args)
  895. : TWinAggrEmulation(pos, opName, minArgs, maxArgs, args)
  896. {
  897. FakeSource = BuildFakeSource(pos);
  898. }
  899. bool TWinNTile::DoInit(TContext& ctx, ISource* src) {
  900. if (Args.size() >= 1 && !Args[0]->Init(ctx, FakeSource.Get())) {
  901. return false;
  902. }
  903. if (!TWinAggrEmulation::DoInit(ctx, src)) {
  904. return false;
  905. }
  906. return true;
  907. }
  908. TWinLeadLag::TWinLeadLag(TPosition pos, const TString& opName, i32 minArgs, i32 maxArgs, const TVector<TNodePtr>& args)
  909. : TWinAggrEmulation(pos, opName, minArgs, maxArgs, args)
  910. {}
  911. bool TWinLeadLag::DoInit(TContext& ctx, ISource* src) {
  912. if (Args.size() >= 2) {
  913. if (!Args[1]->IsIntegerLiteral()) {
  914. ctx.Error(Args[1]->GetPos()) << "Expected integer literal as second parameter of " << OpName << "( ) function";
  915. return false;
  916. }
  917. }
  918. if (!TWinAggrEmulation::DoInit(ctx, src)) {
  919. return false;
  920. }
  921. if (Args.size() >= 1) {
  922. Args[0] = BuildLambda(Pos, Y("row"), Args[0]);
  923. }
  924. return true;
  925. }
  926. TWinRank::TWinRank(TPosition pos, const TString& opName, i32 minArgs, i32 maxArgs, const TVector<TNodePtr>& args)
  927. : TWinAggrEmulation(pos, opName, minArgs, maxArgs, args)
  928. {
  929. }
  930. bool TExternalFunctionConfig::DoInit(TContext& ctx, ISource* src) {
  931. for (auto& param: Config) {
  932. auto paramName = Y(BuildQuotedAtom(Pos, param.first));
  933. if (!param.second->Init(ctx, src)) {
  934. return false;
  935. }
  936. Nodes.push_back(Q(L(paramName, param.second)));
  937. }
  938. return true;
  939. }
  940. INode::TPtr TExternalFunctionConfig::DoClone() const {
  941. TFunctionConfig cloned;
  942. for (auto& [name, node] : Config) {
  943. cloned[name] = SafeClone(node);
  944. }
  945. return new TExternalFunctionConfig(GetPos(), cloned);
  946. }
  947. bool TWinRank::DoInit(TContext& ctx, ISource* src) {
  948. if (!ValidateArguments(ctx)) {
  949. return false;
  950. }
  951. if (!src) {
  952. ctx.Error(Pos) << "Unable to use window function: " << OpName << " without source";
  953. return false;
  954. }
  955. auto winNamePtr = src->GetWindowName();
  956. if (!winNamePtr) {
  957. ctx.Error(Pos) << "Failed to use window function: " << OpName << " without window";
  958. return false;
  959. }
  960. auto winSpecPtr = src->FindWindowSpecification(ctx, *winNamePtr);
  961. if (!winSpecPtr) {
  962. return false;
  963. }
  964. const auto& orderSpec = winSpecPtr->OrderBy;
  965. if (orderSpec.empty()) {
  966. if (Args.empty()) {
  967. ctx.Warning(GetPos(), TIssuesIds::YQL_RANK_WITHOUT_ORDER_BY) <<
  968. OpName << "() is used with unordered window - all rows will be considered equal to each other";
  969. } else {
  970. ctx.Warning(GetPos(), TIssuesIds::YQL_RANK_WITHOUT_ORDER_BY) <<
  971. OpName << "(<expression>) is used with unordered window - the result is likely to be undefined";
  972. }
  973. }
  974. if (Args.empty()) {
  975. for (const auto& spec: orderSpec) {
  976. Args.push_back(spec->Clone()->OrderExpr);
  977. }
  978. if (Args.size() != 1) {
  979. Args = {BuildTuple(GetPos(), Args)};
  980. }
  981. }
  982. YQL_ENSURE(Args.size() == 1);
  983. TVector<TNodePtr> optionsElements;
  984. if (!ctx.AnsiRankForNullableKeys.Defined()) {
  985. optionsElements.push_back(BuildTuple(Pos, { BuildQuotedAtom(Pos, "warnNoAnsi", NYql::TNodeFlags::Default) }));
  986. } else if (*ctx.AnsiRankForNullableKeys) {
  987. optionsElements.push_back(BuildTuple(Pos, { BuildQuotedAtom(Pos, "ansi", NYql::TNodeFlags::Default) }));
  988. }
  989. Args.push_back(BuildTuple(Pos, optionsElements));
  990. MinArgs = MaxArgs = 2;
  991. if (!TWinAggrEmulation::DoInit(ctx, src)) {
  992. return false;
  993. }
  994. YQL_ENSURE(Args.size() == 2);
  995. Args[0] = BuildLambda(Pos, Y("row"), Args[0]);
  996. return true;
  997. }
  998. class TQuotedAtomNode: public TAstListNode {
  999. public:
  1000. TQuotedAtomNode(TPosition pos, const TString& content, ui32 flags)
  1001. : TAstListNode(pos)
  1002. {
  1003. Add("quote", BuildAtom(pos, content, flags));
  1004. }
  1005. protected:
  1006. TQuotedAtomNode(const TQuotedAtomNode& other)
  1007. : TAstListNode(other.Pos)
  1008. {
  1009. Nodes = CloneContainer(other.Nodes);
  1010. }
  1011. TPtr DoClone() const final {
  1012. return new TQuotedAtomNode(*this);
  1013. }
  1014. };
  1015. TNodePtr BuildQuotedAtom(TPosition pos, const TString& content, ui32 flags) {
  1016. return new TQuotedAtomNode(pos, content, flags);
  1017. }
  1018. TNodePtr ITableKeys::AddView(TNodePtr key, const TViewDescription& view) {
  1019. if (view.PrimaryFlag) {
  1020. return L(key, Q(Y(Q("primary_view"))));
  1021. } else if (!view.empty()) {
  1022. return L(key, Q(Y(Q("view"), Y("String", BuildQuotedAtom(Pos, view.ViewName)))));
  1023. } else {
  1024. return key;
  1025. }
  1026. }
  1027. TString TColumns::AddUnnamed() {
  1028. TString desiredResult = TStringBuilder() << "column" << List.size();
  1029. if (!All) {
  1030. HasUnnamed = true;
  1031. List.emplace_back();
  1032. NamedColumns.push_back(false);
  1033. }
  1034. return desiredResult;
  1035. }
  1036. bool TColumns::Add(const TString* column, bool countHint, bool isArtificial, bool isReliable) {
  1037. if (!column || *column == "*") {
  1038. if (!countHint) {
  1039. SetAll();
  1040. }
  1041. } else if (!All) {
  1042. if (column->EndsWith('*')) {
  1043. QualifiedAll = true;
  1044. }
  1045. bool inserted = false;
  1046. if (isArtificial) {
  1047. inserted = Artificial.insert(*column).second;
  1048. } else {
  1049. inserted = Real.insert(*column).second;
  1050. }
  1051. if (!isReliable) {
  1052. HasUnreliable = true;
  1053. }
  1054. if (std::find(List.begin(), List.end(), *column) == List.end()) {
  1055. List.push_back(*column);
  1056. NamedColumns.push_back(true);
  1057. }
  1058. return inserted;
  1059. }
  1060. return All;
  1061. }
  1062. void TColumns::Merge(const TColumns& columns) {
  1063. if (columns.All) {
  1064. SetAll();
  1065. } else {
  1066. YQL_ENSURE(columns.List.size() == columns.NamedColumns.size());
  1067. size_t myUnnamed = NamedColumns.size() - std::accumulate(NamedColumns.begin(), NamedColumns.end(), 0);
  1068. size_t otherUnnamed = 0;
  1069. for (size_t i = 0; i < columns.List.size(); ++i) {
  1070. auto& c = columns.List[i];
  1071. if (!columns.NamedColumns[i]) {
  1072. if (++otherUnnamed > myUnnamed) {
  1073. AddUnnamed();
  1074. ++myUnnamed;
  1075. }
  1076. continue;
  1077. }
  1078. if (columns.Real.contains(c)) {
  1079. Add(&c, false, false);
  1080. }
  1081. if (columns.Artificial.contains(c)) {
  1082. Add(&c, false, true);
  1083. }
  1084. }
  1085. HasUnreliable |= columns.HasUnreliable;
  1086. HasUnnamed |= columns.HasUnnamed;
  1087. }
  1088. }
  1089. void TColumns::SetPrefix(const TString& prefix) {
  1090. Y_DEBUG_ABORT_UNLESS(!prefix.empty());
  1091. auto addPrefixFunc = [&prefix](const TString& str) {
  1092. return prefix + "." + str;
  1093. };
  1094. TSet<TString> newReal;
  1095. TSet<TString> newArtificial;
  1096. TVector<TString> newList;
  1097. std::transform(Real.begin(), Real.end(), std::inserter(newReal, newReal.begin()), addPrefixFunc);
  1098. std::transform(Artificial.begin(), Artificial.end(), std::inserter(newArtificial, newArtificial.begin()), addPrefixFunc);
  1099. std::transform(List.begin(), List.end(), std::back_inserter(newList), addPrefixFunc);
  1100. newReal.swap(Real);
  1101. newArtificial.swap(Artificial);
  1102. newList.swap(List);
  1103. }
  1104. void TColumns::SetAll() {
  1105. All = true;
  1106. QualifiedAll = false;
  1107. Real.clear();
  1108. List.clear();
  1109. Artificial.clear();
  1110. NamedColumns.clear();
  1111. HasUnnamed = HasUnreliable = false;
  1112. }
  1113. namespace {
  1114. bool MaybeAutogenerated(const TString& name) {
  1115. TStringBuf prefix = "column";
  1116. if (!name.StartsWith(prefix)) {
  1117. return false;
  1118. }
  1119. TString suffix = name.substr(prefix.size());
  1120. return !suffix.empty() && AllOf(suffix, [](const auto c) { return std::isdigit(c); });
  1121. }
  1122. bool MatchDotSuffix(const TSet<TString>& columns, const TString& column) {
  1123. for (const auto& col: columns) {
  1124. const auto pos = col.find_first_of(".");
  1125. if (pos == TString::npos) {
  1126. continue;
  1127. }
  1128. if (column == col.substr(pos + 1)) {
  1129. return true;
  1130. }
  1131. }
  1132. return false;
  1133. }
  1134. }
  1135. bool TColumns::IsColumnPossible(TContext& ctx, const TString& name) const {
  1136. if (All || Real.contains(name) || Artificial.contains(name)) {
  1137. return true;
  1138. }
  1139. if (ctx.SimpleColumns && !name.Contains('.') && (MatchDotSuffix(Real, name) || MatchDotSuffix(Artificial, name))) {
  1140. return true;
  1141. }
  1142. if (QualifiedAll) {
  1143. if (ctx.SimpleColumns) {
  1144. return true;
  1145. }
  1146. if (HasUnnamed) {
  1147. const auto dotPos = name.find_first_of(".");
  1148. TString suffix = (dotPos == TString::npos) ? name : name.substr(dotPos + 1);
  1149. if (MaybeAutogenerated(suffix)) {
  1150. return true;
  1151. }
  1152. }
  1153. for (const auto& real: Real) {
  1154. const auto pos = real.find_first_of("*");
  1155. if (pos == TString::npos) {
  1156. continue;
  1157. }
  1158. if (name.StartsWith(real.substr(0, pos))) {
  1159. return true;
  1160. }
  1161. }
  1162. } else if (HasUnnamed && MaybeAutogenerated(name)) {
  1163. return true;
  1164. }
  1165. return false;
  1166. }
  1167. TSortSpecification::TSortSpecification(const TNodePtr& orderExpr, bool ascending)
  1168. : OrderExpr(orderExpr->Clone())
  1169. , Ascending(ascending)
  1170. , CleanOrderExpr(orderExpr->Clone())
  1171. {
  1172. }
  1173. TSortSpecificationPtr TSortSpecification::Clone() const {
  1174. return MakeIntrusive<TSortSpecification>(CleanOrderExpr, Ascending);
  1175. }
  1176. TFrameBoundPtr TFrameBound::Clone() const {
  1177. auto res = MakeIntrusive<TFrameBound>();
  1178. res->Pos = Pos;
  1179. res->Bound = SafeClone(Bound);
  1180. res->Settings = Settings;
  1181. return res;
  1182. }
  1183. TFrameSpecificationPtr TFrameSpecification::Clone() const {
  1184. YQL_ENSURE(FrameBegin);
  1185. YQL_ENSURE(FrameEnd);
  1186. auto res = MakeIntrusive<TFrameSpecification>();
  1187. res->FrameType = FrameType;
  1188. res->FrameBegin = FrameBegin->Clone();
  1189. res->FrameEnd = FrameEnd->Clone();
  1190. res->FrameExclusion = FrameExclusion;
  1191. return res;
  1192. }
  1193. TWindowSpecificationPtr TWindowSpecification::Clone() const {
  1194. YQL_ENSURE(Frame);
  1195. auto res = MakeIntrusive<TWindowSpecification>();
  1196. res->ExistingWindowName = ExistingWindowName;
  1197. res->Partitions = CloneContainer(Partitions);
  1198. res->IsCompact = IsCompact;
  1199. res->OrderBy = CloneContainer(OrderBy);
  1200. res->Session = SafeClone(Session);
  1201. res->Frame = Frame->Clone();
  1202. return res;
  1203. }
  1204. TWinSpecs CloneContainer(const TWinSpecs& specs) {
  1205. TWinSpecs newSpecs;
  1206. for (auto cur: specs) {
  1207. newSpecs.emplace(cur.first, cur.second->Clone());
  1208. }
  1209. return newSpecs;
  1210. }
  1211. TLegacyHoppingWindowSpecPtr TLegacyHoppingWindowSpec::Clone() const {
  1212. auto res = MakeIntrusive<TLegacyHoppingWindowSpec>();
  1213. res->TimeExtractor = TimeExtractor->Clone();
  1214. res->Hop = Hop->Clone();
  1215. res->Interval = Interval->Clone();
  1216. res->Delay = Delay->Clone();
  1217. res->DataWatermarks = DataWatermarks;
  1218. return res;
  1219. }
  1220. TColumnNode::TColumnNode(TPosition pos, const TString& column, const TString& source, bool maybeType)
  1221. : INode(pos)
  1222. , ColumnName(column)
  1223. , Source(source)
  1224. , MaybeType(maybeType)
  1225. {
  1226. }
  1227. TColumnNode::TColumnNode(TPosition pos, const TNodePtr& column, const TString& source)
  1228. : INode(pos)
  1229. , ColumnExpr(column)
  1230. , Source(source)
  1231. {
  1232. }
  1233. TColumnNode::~TColumnNode()
  1234. {
  1235. }
  1236. bool TColumnNode::IsAsterisk() const {
  1237. return ColumnName == "*";
  1238. }
  1239. bool TColumnNode::IsArtificial() const {
  1240. return Artificial;
  1241. }
  1242. const TString* TColumnNode::GetColumnName() const {
  1243. return UseSourceAsColumn ? &Source : (ColumnExpr ? nullptr : &ColumnName);
  1244. }
  1245. const TString* TColumnNode::GetSourceName() const {
  1246. return UseSourceAsColumn ? &Empty : &Source;
  1247. }
  1248. TColumnNode* TColumnNode::GetColumnNode() {
  1249. return this;
  1250. }
  1251. const TColumnNode* TColumnNode::GetColumnNode () const {
  1252. return this;
  1253. }
  1254. bool TColumnNode::DoInit(TContext& ctx, ISource* src) {
  1255. if (src) {
  1256. YQL_ENSURE(!State.Test(ENodeState::Initialized)); /// should be not initialized or Aggregated already invalid
  1257. if (src->ShouldUseSourceAsColumn(*GetSourceName())) {
  1258. if (!IsAsterisk() && IsReliable()) {
  1259. SetUseSourceAsColumn();
  1260. }
  1261. }
  1262. if (GetColumnName()) {
  1263. auto fullName = Source ? DotJoin(Source, *GetColumnName()) : *GetColumnName();
  1264. auto alias = src->GetGroupByColumnAlias(fullName);
  1265. if (alias) {
  1266. ResetColumn(alias, {});
  1267. }
  1268. Artificial = !Source && src->IsExprAlias(*GetColumnName());
  1269. }
  1270. if (!src->AddColumn(ctx, *this)) {
  1271. return false;
  1272. }
  1273. if (GetColumnName()) {
  1274. if (src->GetJoin() && Source) {
  1275. GroupKey = src->IsGroupByColumn(DotJoin(Source, *GetColumnName()));
  1276. } else {
  1277. GroupKey = src->IsGroupByColumn(*GetColumnName()) || src->IsAlias(EExprSeat::GroupBy, *GetColumnName());
  1278. }
  1279. }
  1280. }
  1281. if (IsAsterisk()) {
  1282. Node = AstNode("row");
  1283. } else {
  1284. TString callable;
  1285. if (MaybeType) {
  1286. callable = Reliable && !UseSource ? "SqlPlainColumnOrType" : "SqlColumnOrType";
  1287. } else {
  1288. // TODO: consider replacing Member -> SqlPlainColumn
  1289. callable = Reliable && !UseSource ? "Member" : "SqlColumn";
  1290. }
  1291. Node = Y(callable, "row", ColumnExpr ? Y("EvaluateAtom", ColumnExpr) : BuildQuotedAtom(Pos, *GetColumnName()));
  1292. if (UseSource) {
  1293. YQL_ENSURE(Source);
  1294. Node = L(Node, BuildQuotedAtom(Pos, Source));
  1295. }
  1296. }
  1297. return Node->Init(ctx, src);
  1298. }
  1299. void TColumnNode::SetUseSourceAsColumn() {
  1300. YQL_ENSURE(!State.Test(ENodeState::Initialized)); /// should be not initialized or Aggregated already invalid
  1301. YQL_ENSURE(!IsAsterisk());
  1302. UseSourceAsColumn = true;
  1303. }
  1304. void TColumnNode::ResetAsReliable() {
  1305. Reliable = true;
  1306. }
  1307. void TColumnNode::SetAsNotReliable() {
  1308. Reliable = false;
  1309. }
  1310. void TColumnNode::SetUseSource() {
  1311. UseSource = true;
  1312. }
  1313. bool TColumnNode::IsUseSourceAsColumn() const {
  1314. return UseSourceAsColumn;
  1315. }
  1316. bool TColumnNode::IsUseSource() const {
  1317. return UseSource;
  1318. }
  1319. bool TColumnNode::IsReliable() const {
  1320. return Reliable;
  1321. }
  1322. bool TColumnNode::CanBeType() const {
  1323. return MaybeType;
  1324. }
  1325. TNodePtr TColumnNode::DoClone() const {
  1326. YQL_ENSURE(!Node, "TColumnNode::Clone: Node should not be initialized");
  1327. auto copy = ColumnExpr ? new TColumnNode(Pos, ColumnExpr, Source) : new TColumnNode(Pos, ColumnName, Source, MaybeType);
  1328. copy->GroupKey = GroupKey;
  1329. copy->Artificial = Artificial;
  1330. copy->Reliable = Reliable;
  1331. copy->UseSource = UseSource;
  1332. copy->UseSourceAsColumn = UseSourceAsColumn;
  1333. return copy;
  1334. }
  1335. void TColumnNode::DoUpdateState() const {
  1336. State.Set(ENodeState::Const, false);
  1337. State.Set(ENodeState::MaybeConst, MaybeType);
  1338. State.Set(ENodeState::Aggregated, GroupKey);
  1339. State.Set(ENodeState::AggregationKey, GroupKey);
  1340. }
  1341. TAstNode* TColumnNode::Translate(TContext& ctx) const {
  1342. return Node->Translate(ctx);
  1343. }
  1344. void TColumnNode::ResetColumn(const TString& column, const TString& source) {
  1345. YQL_ENSURE(!State.Test(ENodeState::Initialized)); /// should be not initialized
  1346. Reliable = true;
  1347. UseSource = false;
  1348. UseSourceAsColumn = false;
  1349. ColumnName = column;
  1350. ColumnExpr = nullptr;
  1351. Source = source;
  1352. }
  1353. void TColumnNode::ResetColumn(const TNodePtr& column, const TString& source) {
  1354. YQL_ENSURE(!State.Test(ENodeState::Initialized)); /// should be not initialized
  1355. Reliable = true;
  1356. UseSource = false;
  1357. UseSourceAsColumn = false;
  1358. ColumnName = "";
  1359. ColumnExpr = column;
  1360. Source = source;
  1361. }
  1362. const TString TColumnNode::Empty;
  1363. TNodePtr BuildColumn(TPosition pos, const TString& column, const TString& source) {
  1364. bool maybeType = false;
  1365. return new TColumnNode(pos, column, source, maybeType);
  1366. }
  1367. TNodePtr BuildColumn(TPosition pos, const TNodePtr& column, const TString& source) {
  1368. return new TColumnNode(pos, column, source);
  1369. }
  1370. TNodePtr BuildColumn(TPosition pos, const TDeferredAtom& column, const TString& source) {
  1371. return column.GetLiteral() ? BuildColumn(pos, *column.GetLiteral(), source) : BuildColumn(pos, column.Build(), source);
  1372. }
  1373. TNodePtr BuildColumnOrType(TPosition pos, const TString& column) {
  1374. TString source = "";
  1375. bool maybeType = true;
  1376. return new TColumnNode(pos, column, source, maybeType);
  1377. }
  1378. ITableKeys::ITableKeys(TPosition pos)
  1379. : INode(pos)
  1380. {
  1381. }
  1382. const TString* ITableKeys::GetTableName() const {
  1383. return nullptr;
  1384. }
  1385. ITableKeys* ITableKeys::GetTableKeys() {
  1386. return this;
  1387. }
  1388. TAstNode* ITableKeys::Translate(TContext& ctx) const {
  1389. Y_DEBUG_ABORT_UNLESS(false);
  1390. Y_UNUSED(ctx);
  1391. return nullptr;
  1392. }
  1393. bool IAggregation::IsDistinct() const {
  1394. return !DistinctKey.empty();
  1395. }
  1396. void IAggregation::DoUpdateState() const {
  1397. State.Set(ENodeState::Aggregated, AggMode == EAggregateMode::Normal);
  1398. State.Set(ENodeState::OverWindow, AggMode == EAggregateMode::OverWindow);
  1399. State.Set(ENodeState::OverWindowDistinct, AggMode == EAggregateMode::OverWindowDistinct);
  1400. }
  1401. const TString* IAggregation::GetGenericKey() const {
  1402. return nullptr;
  1403. }
  1404. void IAggregation::Join(IAggregation*) {
  1405. YQL_ENSURE(false, "Should not be called");
  1406. }
  1407. const TString& IAggregation::GetName() const {
  1408. return Name;
  1409. }
  1410. EAggregateMode IAggregation::GetAggregationMode() const {
  1411. return AggMode;
  1412. }
  1413. void IAggregation::MarkKeyColumnAsGenerated() {
  1414. IsGeneratedKeyColumn = true;
  1415. }
  1416. IAggregation::IAggregation(TPosition pos, const TString& name, const TString& func, EAggregateMode aggMode)
  1417. : INode(pos), Name(name), Func(func), AggMode(aggMode)
  1418. {}
  1419. TAstNode* IAggregation::Translate(TContext& ctx) const {
  1420. Y_DEBUG_ABORT_UNLESS(false);
  1421. Y_UNUSED(ctx);
  1422. return nullptr;
  1423. }
  1424. std::pair<TNodePtr, bool> IAggregation::AggregationTraits(const TNodePtr& type, bool overState, bool many, bool allowAggApply, TContext& ctx) const {
  1425. const bool distinct = AggMode == EAggregateMode::Distinct;
  1426. const auto listType = distinct ? Y("ListType", Y("StructMemberType", Y("ListItemType", type), BuildQuotedAtom(Pos, DistinctKey))) : type;
  1427. auto apply = GetApply(listType, many, allowAggApply, ctx);
  1428. if (!apply) {
  1429. return { nullptr, false };
  1430. }
  1431. auto wrapped = WrapIfOverState(apply, overState, many, ctx);
  1432. if (!wrapped) {
  1433. return { nullptr, false };
  1434. }
  1435. return { distinct ?
  1436. Q(Y(Q(Name), wrapped, BuildQuotedAtom(Pos, DistinctKey))) :
  1437. Q(Y(Q(Name), wrapped)), true };
  1438. }
  1439. TNodePtr IAggregation::WrapIfOverState(const TNodePtr& input, bool overState, bool many, TContext& ctx) const {
  1440. if (!overState) {
  1441. return input;
  1442. }
  1443. auto extractor = GetExtractor(many, ctx);
  1444. if (!extractor) {
  1445. return nullptr;
  1446. }
  1447. return Y(ToString("AggOverState"), extractor, BuildLambda(Pos, Y(), input));
  1448. }
  1449. void IAggregation::AddFactoryArguments(TNodePtr& apply) const {
  1450. Y_UNUSED(apply);
  1451. }
  1452. std::vector<ui32> IAggregation::GetFactoryColumnIndices() const {
  1453. return {0u};
  1454. }
  1455. TNodePtr IAggregation::WindowTraits(const TNodePtr& type, TContext& ctx) const {
  1456. YQL_ENSURE(AggMode == EAggregateMode::OverWindow || AggMode == EAggregateMode::OverWindowDistinct, "Windows traits is unavailable");
  1457. const bool distinct = AggMode == EAggregateMode::OverWindowDistinct;
  1458. const auto listType = distinct ? Y("ListType", Y("StructMemberType", Y("ListItemType", type), BuildQuotedAtom(Pos, DistinctKey))) : type;
  1459. auto traits = Y(Q(Name), GetApply(listType, false, false, ctx));
  1460. if (AggMode == EAggregateMode::OverWindowDistinct) {
  1461. traits->Add(BuildQuotedAtom(Pos, DistinctKey));
  1462. }
  1463. return Q(traits);
  1464. }
  1465. namespace {
  1466. bool UnescapeQuoted(const TString& str, TPosition& pos, char quoteChar, TString& result, TString& error, bool utf8Aware) {
  1467. result = error = {};
  1468. size_t readBytes = 0;
  1469. TStringBuf atom(str);
  1470. TStringOutput sout(result);
  1471. atom.Skip(1);
  1472. result.reserve(str.size());
  1473. auto unescapeResult = UnescapeArbitraryAtom(atom, quoteChar, &sout, &readBytes);
  1474. if (unescapeResult != EUnescapeResult::OK) {
  1475. TTextWalker walker(pos, utf8Aware);
  1476. walker.Advance(atom.Trunc(readBytes));
  1477. error = UnescapeResultToString(unescapeResult);
  1478. return false;
  1479. }
  1480. return true;
  1481. }
  1482. TString UnescapeAnsiQuoted(const TString& str) {
  1483. YQL_ENSURE(str.length() >= 2);
  1484. YQL_ENSURE(str[0] == str[str.length() - 1]);
  1485. YQL_ENSURE(str[0] == '\'' || str[0] == '"');
  1486. TString quote(1, str[0]);
  1487. TString replace(2, str[0]);
  1488. TString result = str.substr(1, str.length() - 2);
  1489. SubstGlobal(result, replace, quote);
  1490. return result;
  1491. }
  1492. enum class EStringContentMode : int {
  1493. Default = 0,
  1494. AnsiIdent,
  1495. TypedStringLiteral,
  1496. };
  1497. TMaybe<TStringContent>
  1498. StringContentInternal(TContext& ctx, TPosition pos, const TString& input, EStringContentMode mode) {
  1499. TStringContent result;
  1500. if (mode == EStringContentMode::AnsiIdent) {
  1501. if (!(input.size() >= 2 && input.StartsWith('"') && input.EndsWith('"'))) {
  1502. ctx.Error(pos) << "Expected double quoted identifier, got string literal";
  1503. return {};
  1504. }
  1505. result.Flags = NYql::TNodeFlags::ArbitraryContent;
  1506. result.Content = UnescapeAnsiQuoted(input);
  1507. return result;
  1508. }
  1509. TString str = input;
  1510. if (mode == EStringContentMode::TypedStringLiteral) {
  1511. auto lower = to_lower(str);
  1512. if (lower.EndsWith("y")) {
  1513. str = str.substr(0, str.size() - 1);
  1514. result.Type = NKikimr::NUdf::EDataSlot::Yson;
  1515. } else if (lower.EndsWith("j")) {
  1516. str = str.substr(0, str.size() - 1);
  1517. result.Type = NKikimr::NUdf::EDataSlot::Json;
  1518. } else if (lower.EndsWith("p")) {
  1519. str = str.substr(0, str.size() - 1);
  1520. result.PgType = "PgText";
  1521. } else if (lower.EndsWith("pt")) {
  1522. str = str.substr(0, str.size() - 2);
  1523. result.PgType = "PgText";
  1524. } else if (lower.EndsWith("pb")) {
  1525. str = str.substr(0, str.size() - 2);
  1526. result.PgType = "PgBytea";
  1527. } else if (lower.EndsWith("pv")) {
  1528. str = str.substr(0, str.size() - 2);
  1529. result.PgType = "PgVarchar";
  1530. } else if (lower.EndsWith("s")) {
  1531. str = str.substr(0, str.size() - 1);
  1532. result.Type = NKikimr::NUdf::EDataSlot::String;
  1533. } else if (lower.EndsWith("u")) {
  1534. str = str.substr(0, str.size() - 1);
  1535. result.Type = NKikimr::NUdf::EDataSlot::Utf8;
  1536. } else {
  1537. if (ctx.Scoped->WarnUntypedStringLiterals) {
  1538. ctx.Warning(pos, TIssuesIds::YQL_UNTYPED_STRING_LITERALS)
  1539. << "Please add suffix u for Utf8 strings or s for arbitrary binary strings";
  1540. }
  1541. if (ctx.Scoped->UnicodeLiterals) {
  1542. result.Type = NKikimr::NUdf::EDataSlot::Utf8;
  1543. }
  1544. }
  1545. }
  1546. if (mode == EStringContentMode::Default && (result.Type != NKikimr::NUdf::EDataSlot::String || result.PgType)) {
  1547. ctx.Error(pos) << "Type suffix is not allowed here";
  1548. return {};
  1549. }
  1550. bool doubleQuoted = (str.StartsWith('"') && str.EndsWith('"'));
  1551. bool singleQuoted = !doubleQuoted && (str.StartsWith('\'') && str.EndsWith('\''));
  1552. if (str.size() >= 2 && (doubleQuoted || singleQuoted)) {
  1553. result.Flags = NYql::TNodeFlags::ArbitraryContent;
  1554. if (ctx.Settings.AnsiLexer) {
  1555. YQL_ENSURE(singleQuoted);
  1556. result.Content = UnescapeAnsiQuoted(str);
  1557. } else {
  1558. TString error;
  1559. if (!UnescapeQuoted(str, pos, str[0], result.Content, error, ctx.Settings.Antlr4Parser)) {
  1560. ctx.Error(pos) << "Failed to parse string literal: " << error;
  1561. return {};
  1562. }
  1563. }
  1564. } else if (str.size() >= 4 && str.StartsWith("@@") && str.EndsWith("@@")) {
  1565. result.Flags = TNodeFlags::MultilineContent;
  1566. TString s = str.substr(2, str.length() - 4);
  1567. SubstGlobal(s, "@@@@", "@@");
  1568. result.Content.swap(s);
  1569. } else {
  1570. ctx.Error(pos) << "Invalid string literal: " << EscapeC(str);
  1571. return {};
  1572. }
  1573. if (!result.PgType.Defined() && !NKikimr::NMiniKQL::IsValidStringValue(result.Type, result.Content)) {
  1574. ctx.Error() << "Invalid value " << result.Content.Quote() << " for type " << result.Type;
  1575. return {};
  1576. }
  1577. return result;
  1578. }
  1579. } // namespace
  1580. TMaybe<TStringContent> StringContent(TContext& ctx, TPosition pos, const TString& input) {
  1581. if (ctx.AnsiQuotedIdentifiers && input.StartsWith('"')) {
  1582. ctx.Error() << "Expected string literal, got quoted identifier";
  1583. return {};
  1584. }
  1585. return StringContentInternal(ctx, pos, input, EStringContentMode::Default);
  1586. }
  1587. TMaybe<TStringContent> StringContentOrIdContent(TContext& ctx, TPosition pos, const TString& input) {
  1588. return StringContentInternal(ctx, pos, input,
  1589. (ctx.AnsiQuotedIdentifiers && input.StartsWith('"'))? EStringContentMode::AnsiIdent : EStringContentMode::Default);
  1590. }
  1591. TTtlSettings::TTierSettings::TTierSettings(const TNodePtr& evictionDelay, const std::optional<TIdentifier>& storageName)
  1592. : EvictionDelay(evictionDelay)
  1593. , StorageName(storageName) {
  1594. }
  1595. TTtlSettings::TTtlSettings(const TIdentifier& columnName, const std::vector<TTierSettings>& tiers, const TMaybe<EUnit>& columnUnit)
  1596. : ColumnName(columnName)
  1597. , Tiers(tiers)
  1598. , ColumnUnit(columnUnit)
  1599. {
  1600. }
  1601. TString IdContent(TContext& ctx, const TString& s) {
  1602. YQL_ENSURE(!s.empty(), "Empty identifier not expected");
  1603. if (!s.StartsWith('`')) {
  1604. return s;
  1605. }
  1606. auto endSym = '`';
  1607. if (s.size() < 2 || !s.EndsWith(endSym)) {
  1608. ctx.Error() << "The identifier that starts with: '" << s[0] << "' should ends with: '" << endSym << "'";
  1609. return {};
  1610. }
  1611. size_t skipSymbols = 1;
  1612. TStringBuf atom(s.data() + skipSymbols, s.size() - 2 * skipSymbols + 1);
  1613. TString unescapedStr;
  1614. TStringOutput sout(unescapedStr);
  1615. unescapedStr.reserve(s.size());
  1616. size_t readBytes = 0;
  1617. TPosition pos = ctx.Pos();
  1618. pos.Column += skipSymbols - 1;
  1619. auto unescapeResult = UnescapeArbitraryAtom(atom, endSym, &sout, &readBytes);
  1620. if (unescapeResult != EUnescapeResult::OK) {
  1621. TTextWalker walker(pos, ctx.Settings.Antlr4Parser);
  1622. walker.Advance(atom.Trunc(readBytes));
  1623. ctx.Error(pos) << "Cannot parse broken identifier: " << UnescapeResultToString(unescapeResult);
  1624. return {};
  1625. }
  1626. if (readBytes != atom.size()) {
  1627. ctx.Error() << "The identifier not parsed completely";
  1628. return {};
  1629. }
  1630. return unescapedStr;
  1631. }
  1632. TString IdContentFromString(TContext& ctx, const TString& str) {
  1633. if (!ctx.AnsiQuotedIdentifiers) {
  1634. ctx.Error() << "String literal can not be used here";
  1635. return {};
  1636. }
  1637. auto parsed = StringContentInternal(ctx, ctx.Pos(), str, EStringContentMode::AnsiIdent);
  1638. if (!parsed) {
  1639. return {};
  1640. }
  1641. return parsed->Content;
  1642. }
  1643. namespace {
  1644. class TInvalidLiteralNode final: public INode {
  1645. public:
  1646. TInvalidLiteralNode(TPosition pos)
  1647. : INode(pos)
  1648. {
  1649. }
  1650. bool DoInit(TContext& ctx, ISource* source) override {
  1651. Y_UNUSED(ctx);
  1652. Y_UNUSED(source);
  1653. return false;
  1654. }
  1655. TAstNode* Translate(TContext& ctx) const override {
  1656. Y_UNUSED(ctx);
  1657. return nullptr;
  1658. }
  1659. TPtr DoClone() const override {
  1660. return new TInvalidLiteralNode(GetPos());
  1661. }
  1662. };
  1663. }
  1664. TLiteralNode::TLiteralNode(TPosition pos, bool isNull)
  1665. : TAstListNode(pos)
  1666. , Null(isNull)
  1667. , Void(!isNull)
  1668. {
  1669. Add(isNull ? "Null" : "Void");
  1670. }
  1671. TLiteralNode::TLiteralNode(TPosition pos, const TString& type, const TString& value)
  1672. : TAstListNode(pos)
  1673. , Null(false)
  1674. , Void(false)
  1675. , Type(type)
  1676. , Value(value)
  1677. {
  1678. if (Type.StartsWith("Pg")) {
  1679. Add("PgConst", BuildQuotedAtom(Pos, Value), Y("PgType", Q(to_lower(Type.substr(2)))));
  1680. } else {
  1681. Add(Type, BuildQuotedAtom(Pos, Value));
  1682. }
  1683. }
  1684. TLiteralNode::TLiteralNode(TPosition pos, const TString& value, ui32 nodeFlags)
  1685. : TAstListNode(pos)
  1686. , Null(false)
  1687. , Void(false)
  1688. , Type("String")
  1689. , Value(value)
  1690. {
  1691. Add(Type, BuildQuotedAtom(pos, Value, nodeFlags));
  1692. }
  1693. TLiteralNode::TLiteralNode(TPosition pos, const TString& value, ui32 nodeFlags, const TString& type)
  1694. : TAstListNode(pos)
  1695. , Null(false)
  1696. , Void(false)
  1697. , Type(type)
  1698. , Value(value)
  1699. {
  1700. if (Type.StartsWith("Pg")) {
  1701. Add("PgConst", BuildQuotedAtom(Pos, Value, nodeFlags), Y("PgType", Q(to_lower(Type.substr(2)))));
  1702. } else {
  1703. Add(Type, BuildQuotedAtom(pos, Value, nodeFlags));
  1704. }
  1705. }
  1706. bool TLiteralNode::IsNull() const {
  1707. return Null;
  1708. }
  1709. const TString* TLiteralNode::GetLiteral(const TString& type) const {
  1710. return type == Type ? &Value : nullptr;
  1711. }
  1712. bool TLiteralNode::IsLiteral() const {
  1713. return true;
  1714. }
  1715. TString TLiteralNode::GetLiteralType() const {
  1716. return Type;
  1717. }
  1718. TString TLiteralNode::GetLiteralValue() const {
  1719. return Value;
  1720. }
  1721. void TLiteralNode::DoUpdateState() const {
  1722. State.Set(ENodeState::Const);
  1723. }
  1724. TNodePtr TLiteralNode::DoClone() const {
  1725. auto res = (Null || Void) ? MakeIntrusive<TLiteralNode>(Pos, Null) : MakeIntrusive<TLiteralNode>(Pos, Type, Value);
  1726. res->Nodes = Nodes;
  1727. return res;
  1728. }
  1729. template<typename T>
  1730. TLiteralNumberNode<T>::TLiteralNumberNode(TPosition pos, const TString& type, const TString& value, bool implicitType)
  1731. : TLiteralNode(pos, type, value)
  1732. , ImplicitType(implicitType)
  1733. {}
  1734. template<typename T>
  1735. TNodePtr TLiteralNumberNode<T>::DoClone() const {
  1736. return new TLiteralNumberNode<T>(Pos, Type, Value, ImplicitType);
  1737. }
  1738. template<typename T>
  1739. bool TLiteralNumberNode<T>::DoInit(TContext& ctx, ISource* src) {
  1740. Y_UNUSED(src);
  1741. T val;
  1742. if (!TryFromString(Value, val)) {
  1743. ctx.Error(Pos) << "Failed to parse " << Value << " as integer literal of " << Type << " type: value out of range for " << Type;
  1744. return false;
  1745. }
  1746. return true;
  1747. }
  1748. template<typename T>
  1749. bool TLiteralNumberNode<T>::IsIntegerLiteral() const {
  1750. return std::numeric_limits<T>::is_integer;
  1751. }
  1752. template<typename T>
  1753. TNodePtr TLiteralNumberNode<T>::ApplyUnaryOp(TContext& ctx, TPosition pos, const TString& opName) const {
  1754. YQL_ENSURE(!Value.empty());
  1755. if (opName == "Minus" && IsIntegerLiteral() && Value[0] != '-') {
  1756. if (ImplicitType) {
  1757. ui64 val = FromString<ui64>(Value);
  1758. TString negated = "-" + Value;
  1759. if (val <= ui64(std::numeric_limits<i32>::max()) + 1) {
  1760. // negated value fits in Int32
  1761. i32 v;
  1762. YQL_ENSURE(TryFromString(negated, v));
  1763. return new TLiteralNumberNode<i32>(pos, Type.StartsWith("Pg") ? "PgInt4" : "Int32", negated);
  1764. }
  1765. if (val <= ui64(std::numeric_limits<i64>::max()) + 1) {
  1766. // negated value fits in Int64
  1767. i64 v;
  1768. YQL_ENSURE(TryFromString(negated, v));
  1769. return new TLiteralNumberNode<i64>(pos, Type.StartsWith("Pg") ? "PgInt8" : "Int64", negated);
  1770. }
  1771. ctx.Error(pos) << "Failed to parse negative integer: " << negated << ", number limit overflow";
  1772. return {};
  1773. }
  1774. if (std::numeric_limits<T>::is_signed) {
  1775. return new TLiteralNumberNode<T>(pos, Type, "-" + Value);
  1776. }
  1777. }
  1778. return INode::ApplyUnaryOp(ctx, pos, opName);
  1779. }
  1780. template class TLiteralNumberNode<i32>;
  1781. template class TLiteralNumberNode<i64>;
  1782. template class TLiteralNumberNode<ui32>;
  1783. template class TLiteralNumberNode<ui64>;
  1784. template class TLiteralNumberNode<float>;
  1785. template class TLiteralNumberNode<double>;
  1786. template class TLiteralNumberNode<ui8>;
  1787. template class TLiteralNumberNode<i8>;
  1788. template class TLiteralNumberNode<ui16>;
  1789. template class TLiteralNumberNode<i16>;
  1790. TNodePtr BuildLiteralNull(TPosition pos) {
  1791. return new TLiteralNode(pos, true);
  1792. }
  1793. TNodePtr BuildLiteralVoid(TPosition pos) {
  1794. return new TLiteralNode(pos, false);
  1795. }
  1796. TNodePtr BuildLiteralSmartString(TContext& ctx, const TString& value) {
  1797. auto unescaped = StringContent(ctx, ctx.Pos(), value);
  1798. if (!unescaped) {
  1799. return new TInvalidLiteralNode(ctx.Pos());
  1800. }
  1801. YQL_ENSURE(unescaped->Type == NKikimr::NUdf::EDataSlot::String);
  1802. return new TLiteralNode(ctx.Pos(), unescaped->Content, unescaped->Flags, "String");
  1803. }
  1804. TMaybe<TExprOrIdent> BuildLiteralTypedSmartStringOrId(TContext& ctx, const TString& value) {
  1805. TExprOrIdent result;
  1806. if (ctx.AnsiQuotedIdentifiers && value.StartsWith('"')) {
  1807. auto unescaped = StringContentInternal(ctx, ctx.Pos(), value, EStringContentMode::AnsiIdent);
  1808. if (!unescaped) {
  1809. return {};
  1810. }
  1811. result.Ident = unescaped->Content;
  1812. return result;
  1813. }
  1814. auto unescaped = StringContentInternal(ctx, ctx.Pos(), value, EStringContentMode::TypedStringLiteral);
  1815. if (!unescaped) {
  1816. return {};
  1817. }
  1818. TString type = unescaped->PgType ? *unescaped->PgType : ToString(unescaped->Type);
  1819. result.Expr = new TLiteralNode(ctx.Pos(), unescaped->Content, unescaped->Flags, type);
  1820. return result;
  1821. }
  1822. TNodePtr BuildLiteralRawString(TPosition pos, const TString& value, bool isUtf8) {
  1823. return new TLiteralNode(pos, isUtf8 ? "Utf8" : "String", value);
  1824. }
  1825. TNodePtr BuildLiteralBool(TPosition pos, bool value) {
  1826. return new TLiteralNode(pos, "Bool", value ? "true" : "false");
  1827. }
  1828. TAsteriskNode::TAsteriskNode(TPosition pos)
  1829. : INode(pos)
  1830. {}
  1831. bool TAsteriskNode::IsAsterisk() const {
  1832. return true;
  1833. };
  1834. TNodePtr TAsteriskNode::DoClone() const {
  1835. return new TAsteriskNode(Pos);
  1836. }
  1837. TAstNode* TAsteriskNode::Translate(TContext& ctx) const {
  1838. ctx.Error(Pos) << "* is not allowed here";
  1839. return nullptr;
  1840. }
  1841. TNodePtr BuildEmptyAction(TPosition pos) {
  1842. TNodePtr params = new TAstListNodeImpl(pos);
  1843. TNodePtr arg = new TAstAtomNodeImpl(pos, "x", TNodeFlags::Default);
  1844. params->Add(arg);
  1845. return BuildLambda(pos, params, arg);
  1846. }
  1847. TDeferredAtom::TDeferredAtom()
  1848. {}
  1849. TDeferredAtom::TDeferredAtom(TPosition pos, const TString& str)
  1850. {
  1851. Node = BuildQuotedAtom(pos, str);
  1852. Explicit = str;
  1853. Repr = str;
  1854. }
  1855. TDeferredAtom::TDeferredAtom(TNodePtr node, TContext& ctx)
  1856. {
  1857. Node = node;
  1858. Repr = ctx.MakeName("DeferredAtom");
  1859. }
  1860. const TString* TDeferredAtom::GetLiteral() const {
  1861. return Explicit.Get();
  1862. }
  1863. bool TDeferredAtom::GetLiteral(TString& value, TContext& ctx) const {
  1864. if (Explicit) {
  1865. value = *Explicit;
  1866. return true;
  1867. }
  1868. ctx.Error(Node ? Node->GetPos() : ctx.Pos()) << "Expected literal value";
  1869. return false;
  1870. }
  1871. TNodePtr TDeferredAtom::Build() const {
  1872. return Node;
  1873. }
  1874. TString TDeferredAtom::GetRepr() const {
  1875. return Repr;
  1876. }
  1877. bool TDeferredAtom::Empty() const {
  1878. return !Node || Repr.empty();
  1879. }
  1880. bool TDeferredAtom::HasNode() const {
  1881. return !!Node;
  1882. }
  1883. TTupleNode::TTupleNode(TPosition pos, const TVector<TNodePtr>& exprs)
  1884. : TAstListNode(pos)
  1885. , Exprs(exprs)
  1886. {}
  1887. bool TTupleNode::IsEmpty() const {
  1888. return Exprs.empty();
  1889. }
  1890. const TVector<TNodePtr>& TTupleNode::Elements() const {
  1891. return Exprs;
  1892. }
  1893. TTupleNode* TTupleNode::GetTupleNode() {
  1894. return this;
  1895. }
  1896. const TTupleNode* TTupleNode::GetTupleNode() const {
  1897. return this;
  1898. }
  1899. bool TTupleNode::DoInit(TContext& ctx, ISource* src) {
  1900. auto node(Y());
  1901. for (auto& expr: Exprs) {
  1902. if (expr->GetLabel()) {
  1903. ctx.Error(expr->GetPos()) << "Tuple does not allow named members";
  1904. return false;
  1905. }
  1906. node = L(node, expr);
  1907. }
  1908. Add("quote", node);
  1909. return TAstListNode::DoInit(ctx, src);
  1910. }
  1911. size_t TTupleNode::GetTupleSize() const {
  1912. return Exprs.size();
  1913. }
  1914. TNodePtr TTupleNode::GetTupleElement(size_t index) const {
  1915. return Exprs[index];
  1916. }
  1917. TNodePtr TTupleNode::DoClone() const {
  1918. return new TTupleNode(Pos, CloneContainer(Exprs));
  1919. }
  1920. void TTupleNode::CollectPreaggregateExprs(TContext& ctx, ISource& src, TVector<INode::TPtr>& exprs) {
  1921. for (auto& expr : Exprs) {
  1922. expr->CollectPreaggregateExprs(ctx, src, exprs);
  1923. }
  1924. }
  1925. const TString* TTupleNode::GetSourceName() const {
  1926. return DeriveCommonSourceName(Exprs);
  1927. }
  1928. TNodePtr BuildTuple(TPosition pos, const TVector<TNodePtr>& exprs) {
  1929. return new TTupleNode(pos, exprs);
  1930. }
  1931. TStructNode::TStructNode(TPosition pos, const TVector<TNodePtr>& exprs, const TVector<TNodePtr>& labels, bool ordered)
  1932. : TAstListNode(pos)
  1933. , Exprs(exprs)
  1934. , Labels(labels)
  1935. , Ordered(ordered)
  1936. {
  1937. YQL_ENSURE(Labels.empty() || Labels.size() == Exprs.size());
  1938. }
  1939. bool TStructNode::DoInit(TContext& ctx, ISource* src) {
  1940. Nodes.push_back(BuildAtom(Pos, (Ordered || Exprs.size() < 2) ? "AsStruct" : "AsStructUnordered", TNodeFlags::Default));
  1941. size_t i = 0;
  1942. for (const auto& expr : Exprs) {
  1943. TNodePtr label;
  1944. if (Labels.empty()) {
  1945. if (!expr->GetLabel()) {
  1946. ctx.Error(expr->GetPos()) << "Structure does not allow anonymous members";
  1947. return false;
  1948. }
  1949. label = BuildQuotedAtom(expr->GetPos(), expr->GetLabel());
  1950. } else {
  1951. label = Labels[i++];
  1952. }
  1953. Nodes.push_back(Q(Y(label, expr)));
  1954. }
  1955. return TAstListNode::DoInit(ctx, src);
  1956. }
  1957. TNodePtr TStructNode::DoClone() const {
  1958. return new TStructNode(Pos, CloneContainer(Exprs), CloneContainer(Labels), Ordered);
  1959. }
  1960. TStructNode* TStructNode::GetStructNode() {
  1961. return this;
  1962. }
  1963. const TStructNode* TStructNode::GetStructNode() const {
  1964. return this;
  1965. }
  1966. void TStructNode::CollectPreaggregateExprs(TContext& ctx, ISource& src, TVector<INode::TPtr>& exprs) {
  1967. for (auto& expr : Exprs) {
  1968. expr->CollectPreaggregateExprs(ctx, src, exprs);
  1969. }
  1970. }
  1971. const TString* TStructNode::GetSourceName() const {
  1972. return DeriveCommonSourceName(Exprs);
  1973. }
  1974. TNodePtr BuildStructure(TPosition pos, const TVector<TNodePtr>& exprs) {
  1975. bool ordered = false;
  1976. return new TStructNode(pos, exprs, {}, ordered);
  1977. }
  1978. TNodePtr BuildStructure(TPosition pos, const TVector<TNodePtr>& exprsUnlabeled, const TVector<TNodePtr>& labels) {
  1979. bool ordered = false;
  1980. return new TStructNode(pos, exprsUnlabeled, labels, ordered);
  1981. }
  1982. TNodePtr BuildOrderedStructure(TPosition pos, const TVector<TNodePtr>& exprsUnlabeled, const TVector<TNodePtr>& labels) {
  1983. bool ordered = true;
  1984. return new TStructNode(pos, exprsUnlabeled, labels, ordered);
  1985. }
  1986. TListOfNamedNodes::TListOfNamedNodes(TPosition pos, TVector<TNodePtr>&& exprs)
  1987. : INode(pos)
  1988. , Exprs(std::move(exprs))
  1989. {}
  1990. TVector<TNodePtr>* TListOfNamedNodes::ContentListPtr() {
  1991. return &Exprs;
  1992. }
  1993. TAstNode* TListOfNamedNodes::Translate(TContext& ctx) const {
  1994. YQL_ENSURE(!"Unexpected usage");
  1995. Y_UNUSED(ctx);
  1996. return nullptr;
  1997. }
  1998. TNodePtr TListOfNamedNodes::DoClone() const {
  1999. return new TListOfNamedNodes(GetPos(), CloneContainer(Exprs));
  2000. }
  2001. void TListOfNamedNodes::DoVisitChildren(const TVisitFunc& func, TVisitNodeSet& visited) const {
  2002. for (auto& expr : Exprs) {
  2003. expr->VisitTree(func, visited);
  2004. }
  2005. }
  2006. TNodePtr BuildListOfNamedNodes(TPosition pos, TVector<TNodePtr>&& exprs) {
  2007. return new TListOfNamedNodes(pos, std::move(exprs));
  2008. }
  2009. TArgPlaceholderNode::TArgPlaceholderNode(TPosition pos, const TString &name) :
  2010. INode(pos),
  2011. Name(name)
  2012. {
  2013. }
  2014. bool TArgPlaceholderNode::DoInit(TContext& ctx, ISource* src) {
  2015. Y_UNUSED(src);
  2016. ctx.Error(Pos) << Name << " can't be used as a part of expression.";
  2017. return false;
  2018. }
  2019. TAstNode* TArgPlaceholderNode::Translate(TContext& ctx) const {
  2020. Y_UNUSED(ctx);
  2021. return nullptr;
  2022. }
  2023. TString TArgPlaceholderNode::GetName() const {
  2024. return Name;
  2025. }
  2026. TNodePtr TArgPlaceholderNode::DoClone() const {
  2027. return new TArgPlaceholderNode(GetPos(), Name);
  2028. }
  2029. TNodePtr BuildArgPlaceholder(TPosition pos, const TString& name) {
  2030. return new TArgPlaceholderNode(pos, name);
  2031. }
  2032. class TAccessNode: public INode {
  2033. public:
  2034. TAccessNode(TPosition pos, const TVector<TIdPart>& ids, bool isLookup)
  2035. : INode(pos)
  2036. , Ids(ids)
  2037. , IsLookup(isLookup)
  2038. , ColumnOnly(false)
  2039. , IsColumnRequired(false)
  2040. , AccessOpName("AccessNode")
  2041. {
  2042. Y_DEBUG_ABORT_UNLESS(Ids.size() > 1);
  2043. Y_DEBUG_ABORT_UNLESS(Ids[0].Expr);
  2044. auto column = Ids[0].Expr->GetColumnNode();
  2045. if (column) {
  2046. ui32 idx = 1;
  2047. TString source;
  2048. if (Ids.size() > 2) {
  2049. source = Ids[idx].Name;
  2050. ++idx;
  2051. }
  2052. ColumnOnly = !IsLookup && Ids.size() < 4;
  2053. if (ColumnOnly && Ids[idx].Expr) {
  2054. column->ResetColumn(Ids[idx].Expr, source);
  2055. } else {
  2056. column->ResetColumn(Ids[idx].Name, source);
  2057. }
  2058. }
  2059. }
  2060. void AssumeColumn() override {
  2061. IsColumnRequired = true;
  2062. }
  2063. TMaybe<TString> TryMakeTable() {
  2064. if (!ColumnOnly) {
  2065. return Nothing();
  2066. }
  2067. ui32 idx = 1;
  2068. if (Ids.size() > 2) {
  2069. return Nothing();
  2070. }
  2071. return Ids[idx].Name;
  2072. }
  2073. const TString* GetColumnName() const override {
  2074. return ColumnOnly ? Ids[0].Expr->GetColumnName() : nullptr;
  2075. }
  2076. bool IsPlainColumn() const override {
  2077. if (GetColumnName()) {
  2078. return true;
  2079. }
  2080. if (Ids[0].Expr->IsTableRow()) {
  2081. return true;
  2082. }
  2083. return false;
  2084. }
  2085. const TString* GetSourceName() const override {
  2086. return Ids[0].Expr->GetSourceName();
  2087. }
  2088. TAccessNode* GetAccessNode() override {
  2089. return this;
  2090. }
  2091. const TAccessNode* GetAccessNode() const override {
  2092. return this;
  2093. }
  2094. bool DoInit(TContext& ctx, ISource* src) override {
  2095. auto expr = Ids[0].Expr;
  2096. const TPosition pos(expr->GetPos());
  2097. if (expr->IsAsterisk()) {
  2098. ctx.Error(pos) << "Asterisk column does not allow any access";
  2099. return false;
  2100. }
  2101. if (!expr->Init(ctx, src)) {
  2102. return false;
  2103. }
  2104. for (auto& id: Ids) {
  2105. if (id.Expr && !id.Expr->Init(ctx, src)) {
  2106. return false;
  2107. }
  2108. }
  2109. ui32 idx = 1;
  2110. auto column = expr->GetColumnNode();
  2111. if (column) {
  2112. const bool useSourceAsColumn = column->IsUseSourceAsColumn();
  2113. ColumnOnly &= !useSourceAsColumn;
  2114. if (IsColumnRequired && !ColumnOnly) {
  2115. ctx.Error(pos) << "Please use a full form (corellation.struct.field) or an alias (struct.field as alias) to access struct's field in the GROUP BY";
  2116. return false;
  2117. }
  2118. if (Ids.size() > 2) {
  2119. if (!CheckColumnId(pos, ctx, Ids[idx], ColumnOnly ? "Correlation" : "Column", true)) {
  2120. return false;
  2121. }
  2122. ++idx;
  2123. }
  2124. if (!useSourceAsColumn) {
  2125. if (!IsLookup && !CheckColumnId(pos, ctx, Ids[idx], ColumnOnly ? "Column" : "Member", false)) {
  2126. return false;
  2127. }
  2128. ++idx;
  2129. }
  2130. }
  2131. for (; idx < Ids.size(); ++idx) {
  2132. const auto& id = Ids[idx];
  2133. if (!id.Name.empty()) {
  2134. expr = Y("SqlAccess", Q("struct"), expr, id.Expr ? Y("EvaluateAtom", id.Expr) : BuildQuotedAtom(Pos, id.Name));
  2135. AccessOpName = "AccessStructMember";
  2136. } else if (id.Expr) {
  2137. expr = Y("SqlAccess", Q("dict"), expr, id.Expr);
  2138. AccessOpName = "AccessDictMember";
  2139. } else {
  2140. continue;
  2141. }
  2142. if (ctx.PragmaYsonAutoConvert || ctx.PragmaYsonStrict || ctx.PragmaYsonFast) {
  2143. auto ysonOptions = Y();
  2144. if (ctx.PragmaYsonAutoConvert) {
  2145. ysonOptions->Add(BuildQuotedAtom(Pos, "yson_auto_convert"));
  2146. }
  2147. if (ctx.PragmaYsonStrict) {
  2148. ysonOptions->Add(BuildQuotedAtom(Pos, "yson_strict"));
  2149. }
  2150. if (ctx.PragmaYsonFast) {
  2151. ysonOptions->Add(BuildQuotedAtom(Pos, "yson_fast"));
  2152. }
  2153. expr->Add(Q(ysonOptions));
  2154. }
  2155. }
  2156. Node = expr;
  2157. return true;
  2158. }
  2159. TAstNode* Translate(TContext& ctx) const override {
  2160. Y_DEBUG_ABORT_UNLESS(Node);
  2161. return Node->Translate(ctx);
  2162. }
  2163. TPtr DoClone() const override {
  2164. YQL_ENSURE(!Node, "TAccessNode::Clone: Node should not be initialized");
  2165. TVector<TIdPart> cloneIds;
  2166. cloneIds.reserve(Ids.size());
  2167. for (const auto& id: Ids) {
  2168. cloneIds.emplace_back(id.Clone());
  2169. }
  2170. auto copy = new TAccessNode(Pos, cloneIds, IsLookup);
  2171. copy->ColumnOnly = ColumnOnly;
  2172. return copy;
  2173. }
  2174. const TVector<TIdPart>& GetParts() const {
  2175. return Ids;
  2176. }
  2177. protected:
  2178. void DoUpdateState() const override {
  2179. YQL_ENSURE(Node);
  2180. State.Set(ENodeState::Const, Node->IsConstant());
  2181. State.Set(ENodeState::MaybeConst, Node->MaybeConstant());
  2182. State.Set(ENodeState::Aggregated, Node->IsAggregated());
  2183. State.Set(ENodeState::AggregationKey, Node->HasState(ENodeState::AggregationKey));
  2184. State.Set(ENodeState::OverWindow, Node->IsOverWindow());
  2185. }
  2186. void DoVisitChildren(const TVisitFunc& func, TVisitNodeSet& visited) const final {
  2187. Y_DEBUG_ABORT_UNLESS(Node);
  2188. Node->VisitTree(func, visited);
  2189. }
  2190. bool CheckColumnId(TPosition pos, TContext& ctx, const TIdPart& id, const TString& where, bool checkLookup) {
  2191. if (id.Name.empty()) {
  2192. ctx.Error(pos) << where << " name can not be empty";
  2193. return false;
  2194. }
  2195. if (checkLookup && id.Expr) {
  2196. ctx.Error(pos) << where << " name does not allow dict lookup";
  2197. return false;
  2198. }
  2199. return true;
  2200. }
  2201. TString GetOpName() const override {
  2202. return AccessOpName;
  2203. }
  2204. void CollectPreaggregateExprs(TContext& ctx, ISource& src, TVector<INode::TPtr>& exprs) override {
  2205. for (auto& id : Ids) {
  2206. if (id.Expr) {
  2207. id.Expr->CollectPreaggregateExprs(ctx, src, exprs);
  2208. }
  2209. }
  2210. }
  2211. private:
  2212. TNodePtr Node;
  2213. TVector<TIdPart> Ids;
  2214. bool IsLookup;
  2215. bool ColumnOnly;
  2216. bool IsColumnRequired;
  2217. TString AccessOpName;
  2218. };
  2219. TNodePtr BuildAccess(TPosition pos, const TVector<INode::TIdPart>& ids, bool isLookup) {
  2220. return new TAccessNode(pos, ids, isLookup);
  2221. }
  2222. void WarnIfAliasFromSelectIsUsedInGroupBy(TContext& ctx, const TVector<TNodePtr>& selectTerms, const TVector<TNodePtr>& groupByTerms,
  2223. const TVector<TNodePtr>& groupByExprTerms)
  2224. {
  2225. THashMap<TString, TNodePtr> termsByLabel;
  2226. for (auto& term : selectTerms) {
  2227. auto label = term->GetLabel();
  2228. if (!label || term->IsOverWindow()) {
  2229. continue;
  2230. }
  2231. auto column = term->GetColumnName();
  2232. // do not warn for trivial renaming such as '[X.]foo AS foo'
  2233. if (column && *column == label) {
  2234. continue;
  2235. }
  2236. // skip terms with aggregation functions inside
  2237. bool hasAggregationFunction = false;
  2238. auto visitor = [&](const INode& current) {
  2239. hasAggregationFunction = hasAggregationFunction || current.GetAggregation();
  2240. return !hasAggregationFunction;
  2241. };
  2242. term->VisitTree(visitor);
  2243. if (!hasAggregationFunction) {
  2244. termsByLabel[label] = term;
  2245. }
  2246. }
  2247. if (termsByLabel.empty()) {
  2248. return;
  2249. }
  2250. bool found = false;
  2251. auto visitor = [&](const INode& current) {
  2252. if (found) {
  2253. return false;
  2254. }
  2255. if (auto columnName = current.GetColumnName()) {
  2256. // do not warn if source name is set
  2257. auto src = current.GetSourceName();
  2258. if (src && *src) {
  2259. return true;
  2260. }
  2261. auto it = termsByLabel.find(*columnName);
  2262. if (it != termsByLabel.end()) {
  2263. found = true;
  2264. ctx.Warning(current.GetPos(), TIssuesIds::YQL_PROJECTION_ALIAS_IS_REFERENCED_IN_GROUP_BY)
  2265. << "GROUP BY will aggregate by column `" << *columnName << "` instead of aggregating by SELECT expression with same alias";
  2266. ctx.Warning(it->second->GetPos(), TIssuesIds::YQL_PROJECTION_ALIAS_IS_REFERENCED_IN_GROUP_BY)
  2267. << "You should probably use alias in GROUP BY instead of using it here. Please consult documentation for more details";
  2268. return false;
  2269. }
  2270. }
  2271. return true;
  2272. };
  2273. TVector<TNodePtr> originalGroupBy;
  2274. {
  2275. THashSet<TString> groupByExprLabels;
  2276. for (auto& expr : groupByExprTerms) {
  2277. auto label = expr->GetLabel();
  2278. YQL_ENSURE(label);
  2279. groupByExprLabels.insert(label);
  2280. }
  2281. originalGroupBy = groupByTerms;
  2282. EraseIf(originalGroupBy, [&](const TNodePtr& node) {
  2283. auto column = node->GetColumnName();
  2284. auto src = node->GetSourceName();
  2285. return (!src || src->empty()) && column && groupByExprLabels.contains(*column);
  2286. });
  2287. originalGroupBy.insert(originalGroupBy.end(), groupByExprTerms.begin(), groupByExprTerms.end());
  2288. }
  2289. for (auto& groupByTerm : originalGroupBy) {
  2290. groupByTerm->VisitTree(visitor);
  2291. if (found) {
  2292. return;
  2293. }
  2294. }
  2295. }
  2296. bool ValidateAllNodesForAggregation(TContext& ctx, const TVector<TNodePtr>& nodes) {
  2297. for (auto& node: nodes) {
  2298. if (!node->HasState(ENodeState::Initialized) || node->IsConstant() || node->MaybeConstant()) {
  2299. continue;
  2300. }
  2301. // TODO: "!node->IsOverWindow()" doesn't look right here
  2302. if (!node->IsAggregated() && !node->IsOverWindow() && !node->IsOverWindowDistinct()) {
  2303. // locate column which is not a key column and not aggregated
  2304. const INode* found = nullptr;
  2305. auto visitor = [&found](const INode& current) {
  2306. if (found || current.IsAggregated() || current.IsOverWindow() || current.IsOverWindowDistinct()) {
  2307. return false;
  2308. }
  2309. if (current.GetColumnNode() || current.GetAccessNode()) {
  2310. found = &current;
  2311. return false;
  2312. }
  2313. return true;
  2314. };
  2315. node->VisitTree(visitor);
  2316. if (found) {
  2317. TString columnName;
  2318. if (auto col = found->GetColumnName(); col && *col) {
  2319. columnName = "`";
  2320. if (auto src = found->GetSourceName(); src && *src) {
  2321. columnName += DotJoin(*src, *col);
  2322. } else {
  2323. columnName += *col;
  2324. }
  2325. columnName += "` ";
  2326. }
  2327. ctx.Error(found->GetPos()) << "Column " << columnName << "must either be a key column in GROUP BY or it should be used in aggregation function";
  2328. } else {
  2329. ctx.Error(node->GetPos()) << "Expression has to be an aggregation function or key column, because aggregation is used elsewhere in this subquery";
  2330. }
  2331. return false;
  2332. }
  2333. }
  2334. return true;
  2335. }
  2336. class TBindNode: public TAstListNode {
  2337. public:
  2338. TBindNode(TPosition pos, const TString& module, const TString& alias)
  2339. : TAstListNode(pos)
  2340. {
  2341. Add("bind", AstNode(module), BuildQuotedAtom(pos, alias));
  2342. }
  2343. private:
  2344. TBindNode(const TBindNode& other)
  2345. : TAstListNode(other.GetPos())
  2346. {
  2347. Nodes = CloneContainer(other.Nodes);
  2348. }
  2349. TPtr DoClone() const final {
  2350. return new TBindNode(*this);
  2351. }
  2352. };
  2353. TNodePtr BuildBind(TPosition pos, const TString& module, const TString& alias) {
  2354. return new TBindNode(pos, module, alias);
  2355. }
  2356. class TLambdaNode: public TAstListNode {
  2357. public:
  2358. TLambdaNode(TPosition pos, TNodePtr params, TNodePtr body, const TString& resName)
  2359. : TAstListNode(pos)
  2360. {
  2361. if (!resName.empty()) {
  2362. body = Y("block", Q(L(body, Y("return", resName))));
  2363. }
  2364. Add("lambda", Q(params), body);
  2365. }
  2366. TLambdaNode(TPosition pos, TNodePtr params, TVector<TNodePtr> bodies)
  2367. : TAstListNode(pos)
  2368. {
  2369. Add("lambda", Q(params));
  2370. for (const auto& b : bodies) {
  2371. Add(b);
  2372. }
  2373. }
  2374. TLambdaNode* GetLambdaNode() override {
  2375. return this;
  2376. }
  2377. const TLambdaNode* GetLambdaNode() const override {
  2378. return this;
  2379. }
  2380. private:
  2381. TLambdaNode(const TLambdaNode& other)
  2382. : TAstListNode(other.GetPos())
  2383. {
  2384. Nodes = CloneContainer(other.Nodes);
  2385. }
  2386. TPtr DoClone() const final {
  2387. return new TLambdaNode(*this);
  2388. }
  2389. void DoUpdateState() const final {
  2390. State.Set(ENodeState::Const);
  2391. }
  2392. };
  2393. TNodePtr BuildLambda(TPosition pos, TNodePtr params, TNodePtr body, const TString& resName) {
  2394. return new TLambdaNode(pos, params, body, resName);
  2395. }
  2396. TNodePtr BuildLambda(TPosition pos, TNodePtr params, const TVector<TNodePtr>& bodies) {
  2397. return new TLambdaNode(pos, params, bodies);
  2398. }
  2399. TNodePtr BuildDataType(TPosition pos, const TString& typeName) {
  2400. return new TCallNodeImpl(pos, "DataType", {BuildQuotedAtom(pos, typeName, TNodeFlags::Default)});
  2401. }
  2402. TMaybe<TString> LookupSimpleType(const TStringBuf& alias, bool flexibleTypes, bool isPgType) {
  2403. TString normalized = to_lower(TString(alias));
  2404. if (isPgType) {
  2405. // expecting original pg type (like _int4 or varchar) with optional pg suffix (i.e. _pgint4, pgvarchar)
  2406. if (normalized.StartsWith("pg")) {
  2407. normalized = normalized.substr(2);
  2408. } else if (normalized.StartsWith("_pg")) {
  2409. normalized = "_" + normalized.substr(3);
  2410. }
  2411. if (!NPg::HasType(normalized)) {
  2412. return {};
  2413. }
  2414. if (normalized.StartsWith("_")) {
  2415. return "_pg" + normalized.substr(1);
  2416. }
  2417. return "pg" + normalized;
  2418. }
  2419. if (auto sqlAlias = LookupSimpleTypeBySqlAlias(alias, flexibleTypes)) {
  2420. return TString(*sqlAlias);
  2421. }
  2422. TString pgType;
  2423. if (normalized.StartsWith("_pg")) {
  2424. pgType = normalized.substr(3);
  2425. } else if (normalized.StartsWith("pg")) {
  2426. pgType = normalized.substr(2);
  2427. } else {
  2428. return {};
  2429. }
  2430. if (NPg::HasType(pgType)) {
  2431. return normalized;
  2432. }
  2433. return {};
  2434. }
  2435. TNodePtr BuildSimpleType(TContext& ctx, TPosition pos, const TString& typeName, bool dataOnly) {
  2436. bool explicitPgType = ctx.GetColumnReferenceState() == EColumnRefState::AsPgType;
  2437. auto found = LookupSimpleType(typeName, ctx.FlexibleTypes, explicitPgType);
  2438. if (!found) {
  2439. ctx.Error(pos) << "Unknown " << (explicitPgType ? "pg" : "simple") << " type '" << typeName << "'";
  2440. return {};
  2441. }
  2442. auto type = *found;
  2443. if (type == "Void" || type == "Unit" || type == "Generic" || type == "EmptyList" || type == "EmptyDict") {
  2444. if (dataOnly) {
  2445. ctx.Error(pos) << "Only data types are allowed here, but got: '" << typeName << "'";
  2446. return {};
  2447. }
  2448. type += "Type";
  2449. return new TCallNodeImpl(pos, type, {});
  2450. }
  2451. if (type.StartsWith("_pg") || type.StartsWith("pg")) {
  2452. TString pgType;
  2453. if (type.StartsWith("_pg")) {
  2454. pgType = "_" + type.substr(3);
  2455. } else {
  2456. pgType = type.substr(2);
  2457. }
  2458. return new TCallNodeImpl(pos, "PgType", { BuildQuotedAtom(pos, pgType, TNodeFlags::Default) });
  2459. }
  2460. return new TCallNodeImpl(pos, "DataType", { BuildQuotedAtom(pos, type, TNodeFlags::Default) });
  2461. }
  2462. TString TypeByAlias(const TString& alias, bool normalize) {
  2463. TString type(alias);
  2464. TCiString typeAlias(alias);
  2465. if (typeAlias.StartsWith("varchar")) {
  2466. type = "String";
  2467. } else if (typeAlias == "tinyint") {
  2468. type = "Int8";
  2469. } else if (typeAlias == "byte") {
  2470. type = "Uint8";
  2471. } else if (typeAlias == "smallint") {
  2472. type = "Int16";
  2473. } else if (typeAlias == "int" || typeAlias == "integer") {
  2474. type = "Int32";
  2475. } else if (typeAlias == "bigint") {
  2476. type = "Int64";
  2477. }
  2478. return normalize ? NormalizeTypeString(type) : type;
  2479. }
  2480. TNodePtr BuildIsNullOp(TPosition pos, TNodePtr a) {
  2481. if (!a) {
  2482. return nullptr;
  2483. }
  2484. if (a->IsNull()) {
  2485. return BuildLiteralBool(pos, true);
  2486. }
  2487. return new TCallNodeImpl(pos, "Not", {new TCallNodeImpl(pos, "Exists", {a})});
  2488. }
  2489. TUdfNode::TUdfNode(TPosition pos, const TVector<TNodePtr>& args)
  2490. : INode(pos)
  2491. , Args(args)
  2492. {
  2493. if (Args.size()) {
  2494. // If there aren't any named args, args are passed as vector of positional args,
  2495. // else Args has length 2: tuple for positional args and struct for named args,
  2496. // so let's construct tuple of args there. Other type checks will within DoInit call.
  2497. if (!Args[0]->GetTupleNode()) {
  2498. Args = {BuildTuple(pos, args)};
  2499. }
  2500. }
  2501. }
  2502. bool TUdfNode::DoInit(TContext& ctx, ISource* src) {
  2503. Y_UNUSED(src);
  2504. if (Args.size() < 1) {
  2505. ctx.Error(Pos) << "Udf: expected at least one argument";
  2506. return false;
  2507. }
  2508. TTupleNode* as_tuple = Args[0]->GetTupleNode();
  2509. if (!as_tuple || as_tuple->GetTupleSize() < 1) {
  2510. ctx.Error(Pos) << "Udf: first argument must be a callable, like Foo::Bar";
  2511. return false;
  2512. }
  2513. TNodePtr function = as_tuple->GetTupleElement(0);
  2514. if (!function || !function->FuncName()) {
  2515. ctx.Error(Pos) << "Udf: first argument must be a callable, like Foo::Bar";
  2516. return false;
  2517. }
  2518. FunctionName = function->FuncName();
  2519. ModuleName = function->ModuleName();
  2520. ScriptUdf = function->IsScript();
  2521. if (ScriptUdf && as_tuple->GetTupleSize() > 1) {
  2522. ctx.Error(Pos) << "Udf: user type is not supported for script udfs";
  2523. return false;
  2524. }
  2525. if (ScriptUdf) {
  2526. for (size_t i = 0; i < function->GetTupleSize(); ++i) {
  2527. ScriptArgs.push_back(function->GetTupleElement(i));
  2528. }
  2529. }
  2530. TVector<TNodePtr> external;
  2531. external.reserve(as_tuple->GetTupleSize() - 1);
  2532. for (size_t i = 1; i < as_tuple->GetTupleSize(); ++i) {
  2533. // TODO(): support named args in GetFunctionArgColumnStatus
  2534. TNodePtr current = as_tuple->GetTupleElement(i);
  2535. if (TAccessNode* as_access = current->GetAccessNode(); as_access) {
  2536. external.push_back(Y("DataType", Q(as_access->GetParts()[1].Name)));
  2537. continue;
  2538. }
  2539. external.push_back(current);
  2540. }
  2541. ExternalTypesTuple = new TCallNodeImpl(Pos, "TupleType", external);
  2542. if (Args.size() == 1) {
  2543. return true;
  2544. }
  2545. if (TStructNode* named_args = Args[1]->GetStructNode(); named_args) {
  2546. for (const auto &arg: named_args->GetExprs()) {
  2547. if (arg->GetLabel() == "TypeConfig") {
  2548. if (function->IsScript()) {
  2549. ctx.Error() << "Udf: TypeConfig is not supported for script udfs";
  2550. return false;
  2551. }
  2552. TypeConfig = MakeAtomFromExpression(Pos, ctx, arg);
  2553. } else if (arg->GetLabel() == "RunConfig") {
  2554. if (function->IsScript()) {
  2555. ctx.Error() << "Udf: RunConfig is not supported for script udfs";
  2556. return false;
  2557. }
  2558. RunConfig = arg;
  2559. } else if (arg->GetLabel() == "Cpu") {
  2560. Cpu = MakeAtomFromExpression(Pos, ctx, arg);
  2561. } else if (arg->GetLabel() == "ExtraMem") {
  2562. ExtraMem = MakeAtomFromExpression(Pos, ctx, arg);
  2563. } else {
  2564. ctx.Error() << "Udf: unexpected named argument: " << arg->GetLabel();
  2565. return false;
  2566. }
  2567. }
  2568. }
  2569. return true;
  2570. }
  2571. const TNodePtr TUdfNode::GetExternalTypes() const {
  2572. return ExternalTypesTuple;
  2573. }
  2574. const TString& TUdfNode::GetFunction() const {
  2575. return *FunctionName;
  2576. }
  2577. const TString& TUdfNode::GetModule() const {
  2578. return *ModuleName;
  2579. }
  2580. TNodePtr TUdfNode::GetRunConfig() const {
  2581. return RunConfig;
  2582. }
  2583. const TDeferredAtom& TUdfNode::GetTypeConfig() const {
  2584. return TypeConfig;
  2585. }
  2586. TNodePtr TUdfNode::BuildOptions() const {
  2587. if (Cpu.Empty() && ExtraMem.Empty()) {
  2588. return nullptr;
  2589. }
  2590. auto options = Y();
  2591. if (!Cpu.Empty()) {
  2592. options = L(options, Q(Y(Q("cpu"), Cpu.Build())));
  2593. }
  2594. if (!ExtraMem.Empty()) {
  2595. options = L(options, Q(Y(Q("extraMem"), ExtraMem.Build())));
  2596. }
  2597. return Q(options);
  2598. }
  2599. bool TUdfNode::IsScript() const {
  2600. return ScriptUdf;
  2601. }
  2602. const TVector<TNodePtr>& TUdfNode::GetScriptArgs() const {
  2603. return ScriptArgs;
  2604. }
  2605. TUdfNode* TUdfNode::GetUdfNode() {
  2606. return this;
  2607. }
  2608. const TUdfNode* TUdfNode::GetUdfNode() const {
  2609. return this;
  2610. }
  2611. TAstNode* TUdfNode::Translate(TContext& ctx) const {
  2612. ctx.Error(Pos) << "Abstract Udf Node can't be used as a part of expression.";
  2613. return nullptr;
  2614. }
  2615. TNodePtr TUdfNode::DoClone() const {
  2616. return new TUdfNode(Pos, CloneContainer(Args));
  2617. }
  2618. class TBinaryOpNode final: public TCallNode {
  2619. public:
  2620. TBinaryOpNode(TPosition pos, const TString& opName, TNodePtr a, TNodePtr b);
  2621. TNodePtr DoClone() const final {
  2622. YQL_ENSURE(Args.size() == 2);
  2623. return new TBinaryOpNode(Pos, OpName, Args[0]->Clone(), Args[1]->Clone());
  2624. }
  2625. };
  2626. TBinaryOpNode::TBinaryOpNode(TPosition pos, const TString& opName, TNodePtr a, TNodePtr b)
  2627. : TCallNode(pos, opName, 2, 2, { a, b })
  2628. {
  2629. }
  2630. TNodePtr BuildBinaryOp(TContext& ctx, TPosition pos, const TString& opName, TNodePtr a, TNodePtr b) {
  2631. if (!a || !b) {
  2632. return nullptr;
  2633. }
  2634. static const THashSet<TStringBuf> nullSafeOps = {"IsDistinctFrom", "IsNotDistinctFrom"};
  2635. if (!nullSafeOps.contains(opName)) {
  2636. const bool bothArgNull = a->IsNull() && b->IsNull();
  2637. const bool oneArgNull = a->IsNull() || b->IsNull();
  2638. if (bothArgNull || (oneArgNull && opName != "Or" && opName != "And")) {
  2639. ctx.Warning(pos, TIssuesIds::YQL_OPERATION_WILL_RETURN_NULL) << "Binary operation "
  2640. << opName.substr(0, opName.size() - 7 * opName.EndsWith("MayWarn"))
  2641. << " will return NULL here";
  2642. }
  2643. }
  2644. return new TBinaryOpNode(pos, opName, a, b);
  2645. }
  2646. TNodePtr BuildBinaryOpRaw(TPosition pos, const TString& opName, TNodePtr a, TNodePtr b) {
  2647. if (!a || !b) {
  2648. return nullptr;
  2649. }
  2650. return new TBinaryOpNode(pos, opName, a, b);
  2651. }
  2652. class TCalcOverWindow final: public INode {
  2653. public:
  2654. TCalcOverWindow(TPosition pos, const TString& windowName, TNodePtr node)
  2655. : INode(pos)
  2656. , WindowName(windowName)
  2657. , FuncNode(node)
  2658. {}
  2659. TAstNode* Translate(TContext& ctx) const override {
  2660. return FuncNode->Translate(ctx);
  2661. }
  2662. bool DoInit(TContext& ctx, ISource* src) override {
  2663. YQL_ENSURE(src);
  2664. TSourcePtr overWindowSource = BuildOverWindowSource(ctx.Pos(), WindowName, src);
  2665. if (!FuncNode->Init(ctx, overWindowSource.Get())) {
  2666. return false;
  2667. }
  2668. return true;
  2669. }
  2670. TPtr DoClone() const final {
  2671. return new TCalcOverWindow(Pos, WindowName, SafeClone(FuncNode));
  2672. }
  2673. void DoUpdateState() const override {
  2674. State.Set(ENodeState::Const, FuncNode->IsConstant());
  2675. State.Set(ENodeState::MaybeConst, FuncNode->MaybeConstant());
  2676. State.Set(ENodeState::Aggregated, FuncNode->IsAggregated());
  2677. State.Set(ENodeState::OverWindow, true);
  2678. }
  2679. void DoVisitChildren(const TVisitFunc& func, TVisitNodeSet& visited) const final {
  2680. Y_DEBUG_ABORT_UNLESS(FuncNode);
  2681. FuncNode->VisitTree(func, visited);
  2682. }
  2683. void CollectPreaggregateExprs(TContext& ctx, ISource& src, TVector<INode::TPtr>& exprs) override {
  2684. if (ctx.DistinctOverWindow) {
  2685. FuncNode->CollectPreaggregateExprs(ctx, src, exprs);
  2686. } else {
  2687. INode::CollectPreaggregateExprs(ctx, src, exprs);
  2688. }
  2689. }
  2690. protected:
  2691. const TString WindowName;
  2692. TNodePtr FuncNode;
  2693. };
  2694. TNodePtr BuildCalcOverWindow(TPosition pos, const TString& windowName, TNodePtr call) {
  2695. return new TCalcOverWindow(pos, windowName, call);
  2696. }
  2697. template<bool Fast>
  2698. class TYsonOptionsNode final: public INode {
  2699. public:
  2700. TYsonOptionsNode(TPosition pos, bool autoConvert, bool strict)
  2701. : INode(pos)
  2702. , AutoConvert(autoConvert)
  2703. , Strict(strict)
  2704. {
  2705. auto udf = Y("Udf", Q(Fast ? "Yson2.Options" : "Yson.Options"));
  2706. auto autoConvertNode = BuildLiteralBool(pos, autoConvert);
  2707. autoConvertNode->SetLabel("AutoConvert");
  2708. auto strictNode = BuildLiteralBool(pos, strict);
  2709. strictNode->SetLabel("Strict");
  2710. Node = Y("NamedApply", udf, Q(Y()), BuildStructure(pos, { autoConvertNode, strictNode }));
  2711. }
  2712. TAstNode* Translate(TContext& ctx) const override {
  2713. return Node->Translate(ctx);
  2714. }
  2715. bool DoInit(TContext& ctx, ISource* src) override {
  2716. if (!Node->Init(ctx, src)) {
  2717. return false;
  2718. }
  2719. return true;
  2720. }
  2721. TPtr DoClone() const final {
  2722. return new TYsonOptionsNode(Pos, AutoConvert, Strict);
  2723. }
  2724. void DoUpdateState() const override {
  2725. State.Set(ENodeState::Const, true);
  2726. }
  2727. protected:
  2728. TNodePtr Node;
  2729. const bool AutoConvert;
  2730. const bool Strict;
  2731. };
  2732. TNodePtr BuildYsonOptionsNode(TPosition pos, bool autoConvert, bool strict, bool fastYson) {
  2733. if (fastYson)
  2734. return new TYsonOptionsNode<true>(pos, autoConvert, strict);
  2735. else
  2736. return new TYsonOptionsNode<false>(pos, autoConvert, strict);
  2737. }
  2738. class TDoCall final : public INode {
  2739. public:
  2740. TDoCall(TPosition pos, const TNodePtr& node)
  2741. : INode(pos)
  2742. , Node(node)
  2743. {
  2744. FakeSource = BuildFakeSource(pos);
  2745. }
  2746. ISource* GetSource() final {
  2747. return FakeSource.Get();
  2748. }
  2749. bool DoInit(TContext& ctx, ISource* src) final {
  2750. Y_UNUSED(src);
  2751. if (!Node->Init(ctx, FakeSource.Get())) {
  2752. return false;
  2753. }
  2754. return true;
  2755. }
  2756. TAstNode* Translate(TContext& ctx) const final {
  2757. return Node->Translate(ctx);
  2758. }
  2759. TPtr DoClone() const final {
  2760. return new TDoCall(Pos, Node->Clone());
  2761. }
  2762. void DoVisitChildren(const TVisitFunc& func, TVisitNodeSet& visited) const final {
  2763. Y_DEBUG_ABORT_UNLESS(Node);
  2764. Node->VisitTree(func, visited);
  2765. }
  2766. private:
  2767. TNodePtr Node;
  2768. TSourcePtr FakeSource;
  2769. };
  2770. TNodePtr BuildDoCall(TPosition pos, const TNodePtr& node) {
  2771. return new TDoCall(pos, node);
  2772. }
  2773. bool Parseui32(TNodePtr from, ui32& to) {
  2774. const TString* val;
  2775. if (!(val = from->GetLiteral("Int32"))) {
  2776. if (!(val = from->GetLiteral("Uint32"))) {
  2777. return false;
  2778. }
  2779. }
  2780. return TryFromString(*val, to);
  2781. }
  2782. TNodePtr GroundWithExpr(const TNodePtr& ground, const TNodePtr& expr) {
  2783. return ground ? expr->Y("block", expr->Q(expr->L(ground, expr->Y("return", expr)))) : expr;
  2784. }
  2785. TSourcePtr TryMakeSourceFromExpression(TPosition pos, TContext& ctx, const TString& currService, const TDeferredAtom& currCluster,
  2786. TNodePtr node, const TString& view) {
  2787. if (currCluster.Empty()) {
  2788. ctx.Error() << "No cluster name given and no default cluster is selected";
  2789. return nullptr;
  2790. }
  2791. if (auto literal = node->GetLiteral("String")) {
  2792. TNodePtr tableKey = BuildTableKey(node->GetPos(), currService, currCluster, TDeferredAtom(node->GetPos(), *literal), {view});
  2793. TTableRef table(ctx.MakeName("table"), currService, currCluster, tableKey);
  2794. table.Options = BuildInputOptions(node->GetPos(), GetContextHints(ctx));
  2795. return BuildTableSource(node->GetPos(), table);
  2796. }
  2797. if (node->GetLambdaNode()) {
  2798. ctx.Error() << "Lambda is not allowed to be used as source. Did you forget to call a subquery template?";
  2799. return nullptr;
  2800. }
  2801. auto wrappedNode = new TAstListNodeImpl(pos, {
  2802. new TAstAtomNodeImpl(pos, "EvaluateAtom", TNodeFlags::Default),
  2803. node
  2804. });
  2805. TNodePtr tableKey = BuildTableKey(node->GetPos(), currService, currCluster, TDeferredAtom(wrappedNode, ctx), {view});
  2806. TTableRef table(ctx.MakeName("table"), currService, currCluster, tableKey);
  2807. table.Options = BuildInputOptions(node->GetPos(), GetContextHints(ctx));
  2808. return BuildTableSource(node->GetPos(), table);
  2809. }
  2810. void MakeTableFromExpression(TPosition pos, TContext& ctx, TNodePtr node, TDeferredAtom& table, const TString& prefix) {
  2811. if (auto literal = node->GetLiteral("String")) {
  2812. table = TDeferredAtom(node->GetPos(), prefix + *literal);
  2813. return;
  2814. }
  2815. if (auto access = node->GetAccessNode()) {
  2816. auto ret = access->TryMakeTable();
  2817. if (ret) {
  2818. table = TDeferredAtom(node->GetPos(), prefix + *ret);
  2819. return;
  2820. }
  2821. }
  2822. if (!prefix.empty()) {
  2823. node = node->Y("Concat", node->Y("String", node->Q(prefix)), node);
  2824. }
  2825. auto wrappedNode = new TAstListNodeImpl(pos, {
  2826. new TAstAtomNodeImpl(pos, "EvaluateAtom", TNodeFlags::Default),
  2827. node
  2828. });
  2829. table = TDeferredAtom(wrappedNode, ctx);
  2830. }
  2831. TDeferredAtom MakeAtomFromExpression(TPosition pos, TContext& ctx, TNodePtr node, const TString& prefix) {
  2832. if (auto literal = node->GetLiteral("String")) {
  2833. return TDeferredAtom(node->GetPos(), prefix + *literal);
  2834. }
  2835. if (!prefix.empty()) {
  2836. node = node->Y("Concat", node->Y("String", node->Q(prefix)), node);
  2837. }
  2838. auto wrappedNode = new TAstListNodeImpl(pos, {
  2839. new TAstAtomNodeImpl(pos, "EvaluateAtom", TNodeFlags::Default),
  2840. node
  2841. });
  2842. return TDeferredAtom(wrappedNode, ctx);
  2843. }
  2844. class TTupleResultNode: public INode {
  2845. public:
  2846. TTupleResultNode(TNodePtr&& tuple, size_t ensureTupleSize)
  2847. : INode(tuple->GetPos())
  2848. , Node(std::move(tuple))
  2849. , EnsureTupleSize(ensureTupleSize)
  2850. {
  2851. }
  2852. bool DoInit(TContext& ctx, ISource* src) override {
  2853. if (!Node->Init(ctx, src)) {
  2854. return false;
  2855. }
  2856. Node = Y("EnsureTupleSize", Node, Q(ToString(EnsureTupleSize)));
  2857. return true;
  2858. }
  2859. TAstNode* Translate(TContext& ctx) const override {
  2860. return Node->Translate(ctx);
  2861. }
  2862. TPtr DoClone() const final {
  2863. return new TTupleResultNode(Node->Clone(), EnsureTupleSize);
  2864. }
  2865. void DoVisitChildren(const TVisitFunc& func, TVisitNodeSet& visited) const final {
  2866. Y_DEBUG_ABORT_UNLESS(Node);
  2867. Node->VisitTree(func, visited);
  2868. }
  2869. protected:
  2870. TNodePtr Node;
  2871. const size_t EnsureTupleSize;
  2872. };
  2873. TNodePtr BuildTupleResult(TNodePtr tuple, size_t ensureTupleSize) {
  2874. return new TTupleResultNode(std::move(tuple), ensureTupleSize);
  2875. }
  2876. class TNamedExprReferenceNode: public IProxyNode {
  2877. public:
  2878. TNamedExprReferenceNode(TNodePtr parent, const TString& name, TMaybe<size_t> tupleIndex)
  2879. : IProxyNode(parent->GetPos(), parent)
  2880. , Name(name)
  2881. , TupleIndex(tupleIndex)
  2882. {
  2883. }
  2884. bool DoInit(TContext& ctx, ISource* src) final {
  2885. Y_UNUSED(src);
  2886. if (!IProxyNode::DoInit(ctx, nullptr) || !IProxyNode::InitReference(ctx)) {
  2887. return false;
  2888. }
  2889. Node = BuildAtom(GetPos(), Name, TNodeFlags::Default);
  2890. if (TupleIndex.Defined()) {
  2891. Node = Y("Nth", Node, Q(ToString(*TupleIndex)));
  2892. }
  2893. return true;
  2894. }
  2895. TAstNode* Translate(TContext& ctx) const override {
  2896. YQL_ENSURE(Node, "Init() should be done before Translate()");
  2897. return Node->Translate(ctx);
  2898. }
  2899. TPtr DoClone() const final {
  2900. // do not clone Inner here
  2901. return new TNamedExprReferenceNode(Inner, Name, TupleIndex);
  2902. }
  2903. private:
  2904. const TString Name;
  2905. const TMaybe<size_t> TupleIndex;
  2906. TNodePtr Node;
  2907. };
  2908. TNodePtr BuildNamedExprReference(TNodePtr parent, const TString& name, TMaybe<size_t> tupleIndex) {
  2909. YQL_ENSURE(parent);
  2910. return new TNamedExprReferenceNode(parent, name, tupleIndex);
  2911. }
  2912. class TNamedExprNode: public IProxyNode {
  2913. public:
  2914. TNamedExprNode(TNodePtr parent)
  2915. : IProxyNode(parent->GetPos(), parent)
  2916. , FakeSource(BuildFakeSource(parent->GetPos()))
  2917. , Referenced(false)
  2918. {
  2919. }
  2920. bool DoInit(TContext& ctx, ISource* src) final {
  2921. YQL_ENSURE(!Referenced, "Refrence is initialized before named expr itself");
  2922. Y_UNUSED(src);
  2923. if (ctx.ValidateUnusedExprs) {
  2924. return IProxyNode::DoInit(ctx, FakeSource.Get());
  2925. }
  2926. // do actual init in InitReference()
  2927. return true;
  2928. }
  2929. bool InitReference(TContext& ctx) final {
  2930. Referenced = true;
  2931. return IProxyNode::DoInit(ctx, FakeSource.Get());
  2932. }
  2933. TAstNode* Translate(TContext& ctx) const override {
  2934. if (ctx.ValidateUnusedExprs || Referenced) {
  2935. return Inner->Translate(ctx);
  2936. }
  2937. auto unused = BuildQuotedAtom(GetPos(), "unused", TNodeFlags::Default);
  2938. return unused->Translate(ctx);
  2939. }
  2940. TPtr DoClone() const final {
  2941. return new TNamedExprNode(Inner->Clone());
  2942. }
  2943. private:
  2944. const TSourcePtr FakeSource;
  2945. bool Referenced;
  2946. };
  2947. TNodePtr BuildNamedExpr(TNodePtr parent) {
  2948. YQL_ENSURE(parent);
  2949. return new TNamedExprNode(parent);
  2950. }
  2951. bool TVectorIndexSettings::Validate(TContext& ctx) const {
  2952. if (!Distance && !Similarity) {
  2953. ctx.Error() << "either distance or similarity should be set";
  2954. return false;
  2955. }
  2956. if (!VectorType) {
  2957. ctx.Error() << "vector_type should be set";
  2958. return false;
  2959. }
  2960. if (!VectorDimension) {
  2961. ctx.Error() << "vector_dimension should be set";
  2962. return false;
  2963. }
  2964. return true;
  2965. }
  2966. } // namespace NSQLTranslationV1