sql_ut.cpp 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744
  1. #include "sql.h"
  2. #include <yql/essentials/providers/common/provider/yql_provider_names.h>
  3. #include <util/generic/map.h>
  4. #include <library/cpp/testing/unittest/registar.h>
  5. #include <util/string/split.h>
  6. using namespace NSQLTranslationV0;
  7. enum class EDebugOutput {
  8. None,
  9. ToCerr,
  10. };
  11. TString Err2Str(NYql::TAstParseResult& res, EDebugOutput debug = EDebugOutput::None) {
  12. TStringStream s;
  13. res.Issues.PrintTo(s);
  14. if (debug == EDebugOutput::ToCerr) {
  15. Cerr << s.Str() << Endl;
  16. }
  17. return s.Str();
  18. }
  19. NYql::TAstParseResult SqlToYqlWithMode(const TString& query, NSQLTranslation::ESqlMode mode = NSQLTranslation::ESqlMode::QUERY, size_t maxErrors = 10, const TString& provider = {}, EDebugOutput debug = EDebugOutput::None) {
  20. google::protobuf::Arena arena;
  21. const auto service = provider ? provider : TString(NYql::YtProviderName);
  22. const TString cluster = "plato";
  23. NSQLTranslation::TTranslationSettings settings;
  24. settings.ClusterMapping[cluster] = service;
  25. settings.MaxErrors = maxErrors;
  26. settings.Mode = mode;
  27. settings.Arena = &arena;
  28. settings.V0Behavior = NSQLTranslation::EV0Behavior::Report;
  29. settings.WarnOnV0 = false;
  30. auto res = SqlToYql(query, settings);
  31. if (debug == EDebugOutput::ToCerr) {
  32. Err2Str(res, debug);
  33. }
  34. return res;
  35. }
  36. NYql::TAstParseResult SqlToYql(const TString& query, size_t maxErrors = 10, const TString& provider = {}, EDebugOutput debug = EDebugOutput::None) {
  37. return SqlToYqlWithMode(query, NSQLTranslation::ESqlMode::QUERY, maxErrors, provider, debug);
  38. }
  39. TString GetPrettyPrint(const NYql::TAstParseResult& res) {
  40. TStringStream yqlProgram;
  41. res.Root->PrettyPrintTo(yqlProgram, NYql::TAstPrintFlags::PerLine | NYql::TAstPrintFlags::ShortQuote);
  42. return yqlProgram.Str();
  43. }
  44. TString Quote(const char* str) {
  45. return TStringBuilder() << "'\"" << str << "\"";
  46. }
  47. class TWordCountHive: public TMap<TString, unsigned> {
  48. public:
  49. TWordCountHive(std::initializer_list<TString> strings) {
  50. for (auto& str: strings) {
  51. emplace(str, 0);
  52. }
  53. }
  54. TWordCountHive(std::initializer_list<std::pair<const TString, unsigned>> list)
  55. : TMap(list)
  56. {
  57. }
  58. };
  59. typedef std::function<void (const TString& word, const TString& line)> TVerifyLineFunc;
  60. TString VerifyProgram(const NYql::TAstParseResult& res, TWordCountHive& wordCounter, TVerifyLineFunc verifyLine = TVerifyLineFunc()) {
  61. const auto programm = GetPrettyPrint(res);
  62. TVector<TString> yqlProgram;
  63. Split(programm, "\n", yqlProgram);
  64. for (const auto& line: yqlProgram) {
  65. for (auto& counterIter: wordCounter) {
  66. const auto& word = counterIter.first;
  67. if (line.find(word) != TString::npos) {
  68. ++counterIter.second;
  69. if (verifyLine) {
  70. verifyLine(word, line);
  71. }
  72. }
  73. }
  74. }
  75. return programm;
  76. }
  77. Y_UNIT_TEST_SUITE(SqlParsingOnly) {
  78. Y_UNIT_TEST(V0) {
  79. NYql::TAstParseResult res = SqlToYql("select null;");
  80. UNIT_ASSERT(res.Root);
  81. const auto programm = GetPrettyPrint(res);
  82. UNIT_ASSERT(TString::npos != programm.find("(Configure! world (DataSource '\"config\") '\"SQL\" '\"0\"))"));
  83. }
  84. Y_UNIT_TEST(TableHints) {
  85. UNIT_ASSERT(SqlToYql("SELECT * FROM plato.Input WITH INFER_SCHEME").IsOk());
  86. UNIT_ASSERT(SqlToYql("SELECT * FROM plato.Input WITH (INFER_SCHEME)").IsOk());
  87. }
  88. Y_UNIT_TEST(InNoHints) {
  89. NYql::TAstParseResult res = SqlToYql("SELECT * FROM plato.Input WHERE key IN (1,2,3)");
  90. UNIT_ASSERT(res.Root);
  91. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  92. Y_UNUSED(word);
  93. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("'()"));
  94. };
  95. TWordCountHive elementStat = {{TString("SqlIn"), 0}};
  96. VerifyProgram(res, elementStat, verifyLine);
  97. }
  98. Y_UNIT_TEST(InHintCompact) {
  99. // should parse COMPACT as hint
  100. NYql::TAstParseResult res = SqlToYql("SELECT * FROM plato.Input WHERE key IN COMPACT(1, 2, 3)");
  101. UNIT_ASSERT(res.Root);
  102. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  103. Y_UNUSED(word);
  104. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("'('('isCompact))"));
  105. };
  106. TWordCountHive elementStat = {{TString("SqlIn"), 0}};
  107. VerifyProgram(res, elementStat, verifyLine);
  108. }
  109. Y_UNIT_TEST(InHintTableSource) {
  110. // should add tableSource as a hint
  111. NYql::TAstParseResult res = SqlToYql("$subq = (SELECT key FROM plato.Input); SELECT * FROM plato.Input WHERE key IN $subq");
  112. UNIT_ASSERT(res.Root);
  113. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  114. Y_UNUSED(word);
  115. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("'('('tableSource))"));
  116. };
  117. TWordCountHive elementStat = {{TString("SqlIn"), 0}};
  118. VerifyProgram(res, elementStat, verifyLine);
  119. }
  120. Y_UNIT_TEST(InHintCompactAndTableSource) {
  121. NYql::TAstParseResult res = SqlToYql("$subq = (SELECT key FROM plato.Input); SELECT * FROM plato.Input WHERE key IN COMPACT $subq");
  122. UNIT_ASSERT(res.Root);
  123. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  124. Y_UNUSED(word);
  125. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("'('('isCompact) '('tableSource))"));
  126. };
  127. TWordCountHive elementStat = {{TString("SqlIn"), 0}};
  128. VerifyProgram(res, elementStat, verifyLine);
  129. }
  130. Y_UNIT_TEST(CompactKeywordNotReservedForNames) {
  131. UNIT_ASSERT(SqlToYql("SELECT COMPACT FROM plato.Input WHERE COMPACT IN COMPACT(1, 2, 3)").IsOk());
  132. UNIT_ASSERT(SqlToYql("USE plato; SELECT * FROM COMPACT").IsOk());
  133. }
  134. Y_UNIT_TEST(Jubilee) {
  135. NYql::TAstParseResult res = SqlToYql("USE plato; INSERT INTO Arcadia (r2000000) VALUES (\"2M GET!!!\");");
  136. UNIT_ASSERT(res.Root);
  137. }
  138. Y_UNIT_TEST(QualifiedAsteriskBefore) {
  139. NYql::TAstParseResult res = SqlToYql("select interested_table.*, LENGTH(value) AS megahelpful_len from plato.Input as interested_table;");
  140. UNIT_ASSERT(res.Root);
  141. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  142. if (word == "FlattenMembers") {
  143. static ui32 count1 = 0;
  144. if (++count1 == 1) {
  145. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("interested_table."));
  146. }
  147. } else if (word == "AsStruct") {
  148. static ui32 count2 = 0;
  149. if (++count2 == 2) {
  150. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("megahelpful_len"));
  151. }
  152. }
  153. };
  154. TWordCountHive elementStat = {{TString("FlattenMembers"), 0}, {TString("AsStruct"), 0}};
  155. VerifyProgram(res, elementStat, verifyLine);
  156. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["FlattenMembers"]);
  157. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["AsStruct"]);
  158. }
  159. Y_UNIT_TEST(QualifiedAsteriskAfter) {
  160. NYql::TAstParseResult res = SqlToYql("select LENGTH(value) AS megahelpful_len, interested_table.* from plato.Input as interested_table;");
  161. UNIT_ASSERT(res.Root);
  162. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  163. if (word == "FlattenMembers") {
  164. static ui32 count1 = 0;
  165. if (++count1 == 1) {
  166. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("interested_table."));
  167. }
  168. } else if (word == "AsStruct") {
  169. static ui32 count2 = 0;
  170. if (++count2 == 2) {
  171. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("megahelpful_len"));
  172. }
  173. }
  174. };
  175. TWordCountHive elementStat = {{TString("FlattenMembers"), 0}, {TString("AsStruct"), 0}};
  176. VerifyProgram(res, elementStat, verifyLine);
  177. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["FlattenMembers"]);
  178. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["AsStruct"]);
  179. }
  180. Y_UNIT_TEST(QualifiedMembers) {
  181. NYql::TAstParseResult res = SqlToYql("select interested_table.key, interested_table.value from plato.Input as interested_table;");
  182. UNIT_ASSERT(res.Root);
  183. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  184. const bool fieldKey = TString::npos != line.find(Quote("key"));
  185. const bool fieldValue = TString::npos != line.find(Quote("value"));
  186. const bool refOnTable = TString::npos != line.find("interested_table.");
  187. if (word == "AsStruct") {
  188. UNIT_ASSERT(fieldKey || fieldValue);
  189. UNIT_ASSERT(!refOnTable);
  190. } else if (word == "Write!") {
  191. UNIT_ASSERT(fieldKey && fieldValue && !refOnTable);
  192. }
  193. };
  194. TWordCountHive elementStat = {{TString("FlattenMembers"), 0}, {TString("AsStruct"), 0}, {TString("Write!"), 0}};
  195. VerifyProgram(res, elementStat, verifyLine);
  196. UNIT_ASSERT_VALUES_EQUAL(0, elementStat["FlattenMembers"]);
  197. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["AsStruct"]);
  198. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write!"]);
  199. }
  200. Y_UNIT_TEST(JoinParseCorrect) {
  201. NYql::TAstParseResult res = SqlToYql(
  202. " SELECT table_bb.*, table_aa.key as megakey"
  203. " FROM plato.Input AS table_aa"
  204. " JOIN plato.Input AS table_bb"
  205. " ON table_aa.value == table_bb.value;"
  206. );
  207. UNIT_ASSERT(res.Root);
  208. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  209. if (word == "SelectMembers") {
  210. UNIT_ASSERT_VALUES_EQUAL(TString::npos, line.find("table_aa."));
  211. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("table_bb."));
  212. } else if (word == "AsStruct") {
  213. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("SqlColumn"));
  214. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find(Quote("table_aa")));
  215. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find(Quote("key")));
  216. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find(Quote("megakey")));
  217. }
  218. };
  219. TWordCountHive elementStat = {{TString("FlattenMembers"), 0}, {TString("SelectMembers"), 0}, {TString("AsStruct"), 0}};
  220. VerifyProgram(res, elementStat, verifyLine);
  221. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["FlattenMembers"]);
  222. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["SelectMembers"]);
  223. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["AsStruct"]);
  224. }
  225. Y_UNIT_TEST(Join3Table) {
  226. NYql::TAstParseResult res = SqlToYql(
  227. " SELECT table_bb.*, table_aa.key as gigakey, table_cc.* "
  228. " FROM plato.Input AS table_aa"
  229. " JOIN plato.Input AS table_bb ON table_aa.key == table_bb.key"
  230. " JOIN plato.Input AS table_cc ON table_aa.subkey == table_cc.subkey;"
  231. );
  232. Err2Str(res);
  233. UNIT_ASSERT(res.Root);
  234. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  235. if (word == "SelectMembers") {
  236. UNIT_ASSERT_VALUES_EQUAL(TString::npos, line.find("table_aa."));
  237. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("table_bb."));
  238. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("table_cc."));
  239. } else if (word == "AsStruct") {
  240. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("SqlColumn"));
  241. const auto posTableAA = line.find(Quote("table_aa"));
  242. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, posTableAA);
  243. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find(Quote("key")));
  244. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find(Quote("gigakey")));
  245. UNIT_ASSERT_VALUES_EQUAL(TString::npos, line.find("table_aa", posTableAA + 3));
  246. }
  247. };
  248. TWordCountHive elementStat = {{TString("FlattenMembers"), 0}, {TString("SelectMembers"), 0}, {TString("AsStruct"), 0}};
  249. VerifyProgram(res, elementStat, verifyLine);
  250. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["FlattenMembers"]);
  251. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["SelectMembers"]);
  252. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["AsStruct"]);
  253. }
  254. Y_UNIT_TEST(JoinWithoutConcreteColumns) {
  255. NYql::TAstParseResult res = SqlToYql(
  256. " use plato;"
  257. " SELECT a.v, b.value"
  258. " FROM [Input1]:[ksv] AS a"
  259. " JOIN [Input2] AS b"
  260. " ON a.k == b.key;"
  261. );
  262. UNIT_ASSERT(res.Root);
  263. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  264. if (word == "AsStruct") {
  265. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("SqlColumn"));
  266. const auto posTableA = line.find(Quote("a"));
  267. const auto posTableB = line.find(Quote("b"));
  268. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, posTableA);
  269. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, posTableB);
  270. UNIT_ASSERT_VALUES_EQUAL(TString::npos, line.find(Quote("a"), posTableA + 1));
  271. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find(Quote("v")));
  272. UNIT_ASSERT_VALUES_EQUAL(TString::npos, line.find(Quote("b"), posTableB + 1));
  273. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find(Quote("value")));
  274. }
  275. };
  276. TWordCountHive elementStat = {{TString("FlattenMembers"), 0}, {TString("AsStruct"), 0}};
  277. VerifyProgram(res, elementStat, verifyLine);
  278. UNIT_ASSERT_VALUES_EQUAL(0, elementStat["FlattenMembers"]);
  279. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["AsStruct"]);
  280. }
  281. Y_UNIT_TEST(JoinWithSameValues) {
  282. NYql::TAstParseResult res = SqlToYql("SELECT a.value, b.value FROM plato.Input AS a JOIN plato.Input as b ON a.key == b.key;");
  283. UNIT_ASSERT(res.Root);
  284. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  285. if (word == "AsStruct") {
  286. const bool isValueFromA = TString::npos != line.find(Quote("a.value"));
  287. const bool isValueFromB = TString::npos != line.find(Quote("b.value"));
  288. UNIT_ASSERT(isValueFromA || isValueFromB);
  289. } if (word == "Write!") {
  290. const bool noDuplicateSourceInA = TString::npos == line.find("a.a.");
  291. const bool noDuplicateSourceInB = TString::npos == line.find("b.b.");
  292. UNIT_ASSERT(noDuplicateSourceInA || noDuplicateSourceInB);
  293. }
  294. };
  295. TWordCountHive elementStat = {{TString("FlattenMembers"), 0}, {TString("AsStruct"), 0}, {"Write!", 0}};
  296. VerifyProgram(res, elementStat, verifyLine);
  297. UNIT_ASSERT_VALUES_EQUAL(0, elementStat["FlattenMembers"]);
  298. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["AsStruct"]);
  299. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write!"]);
  300. }
  301. Y_UNIT_TEST(SameColumnsForDifferentTables) {
  302. NYql::TAstParseResult res = SqlToYql("SELECT a.key, b.key FROM plato.Input as a JOIN plato.Input as b on a.key==b.key;");
  303. UNIT_ASSERT(res.Root);
  304. }
  305. Y_UNIT_TEST(SameColumnsForDifferentTablesFullJoin) {
  306. NYql::TAstParseResult res = SqlToYql("SELECT a.key, b.key, a.value, b.value FROM plato.Input AS a FULL JOIN plato.Input AS b USING(key);");
  307. UNIT_ASSERT(res.Root);
  308. }
  309. Y_UNIT_TEST(ReverseLabels) {
  310. NYql::TAstParseResult res = SqlToYql("select in.key as subkey, subkey as key from plato.Input as in;");
  311. UNIT_ASSERT(res.Root);
  312. }
  313. Y_UNIT_TEST(AutogenerationAliasWithoutCollisionConflict1) {
  314. NYql::TAstParseResult res = SqlToYql("select LENGTH(Value), key as column1 from plato.Input;");
  315. UNIT_ASSERT(res.Root);
  316. }
  317. Y_UNIT_TEST(AutogenerationAliasWithoutCollision2Conflict2) {
  318. NYql::TAstParseResult res = SqlToYql("select key as column0, LENGTH(Value) from plato.Input;");
  319. UNIT_ASSERT(res.Root);
  320. }
  321. Y_UNIT_TEST(InputAliasForQualifiedAsterisk) {
  322. NYql::TAstParseResult res = SqlToYql("use plato; select zyuzya.*, key from plato.Input as zyuzya;");
  323. UNIT_ASSERT(res.Root);
  324. }
  325. Y_UNIT_TEST(SelectOrderByLabeledColumn) {
  326. NYql::TAstParseResult res = SqlToYql("select key as goal from plato.Input order by goal");
  327. UNIT_ASSERT(res.Root);
  328. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  329. if (word == "DataSource" && TString::npos == line.find("SQL")) {
  330. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("plato"));
  331. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("Input"));
  332. UNIT_ASSERT_VALUES_EQUAL(TString::npos, line.find("goal"));
  333. } else if (word == "Sort") {
  334. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("goal"));
  335. UNIT_ASSERT_VALUES_EQUAL(TString::npos, line.find("key"));
  336. }
  337. };
  338. TWordCountHive elementStat = {{TString("DataSource"), 0}, {TString("Sort"), 0}};
  339. VerifyProgram(res, elementStat, verifyLine);
  340. UNIT_ASSERT_VALUES_EQUAL(2, elementStat["DataSource"]);
  341. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Sort"]);
  342. }
  343. Y_UNIT_TEST(SelectOrderBySimpleExpr) {
  344. NYql::TAstParseResult res = SqlToYql("select a from plato.Input order by a + a");
  345. UNIT_ASSERT(res.Root);
  346. }
  347. Y_UNIT_TEST(SelectOrderByDuplicateLabels) {
  348. NYql::TAstParseResult res = SqlToYql("select a from plato.Input order by a, a");
  349. UNIT_ASSERT(res.Root);
  350. }
  351. Y_UNIT_TEST(SelectOrderByExpression) {
  352. NYql::TAstParseResult res = SqlToYql("select * from plato.Input as i order by cast(key as uint32) + cast(subkey as uint32)");
  353. UNIT_ASSERT(res.Root);
  354. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  355. if (word == "Sort") {
  356. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("\"+\""));
  357. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("key"));
  358. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("subkey"));
  359. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("(Bool 'true)"));
  360. UNIT_ASSERT_VALUES_EQUAL(TString::npos, line.find("i.key"));
  361. UNIT_ASSERT_VALUES_EQUAL(TString::npos, line.find("i.subkey"));
  362. }
  363. };
  364. TWordCountHive elementStat = {{TString("Sort"), 0}};
  365. VerifyProgram(res, elementStat, verifyLine);
  366. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Sort"]);
  367. }
  368. Y_UNIT_TEST(SelectOrderByExpressionDesc) {
  369. NYql::TAstParseResult res = SqlToYql("select i.*, key, subkey from plato.Input as i order by cast(i.key as uint32) - cast(i.subkey as uint32) desc");
  370. UNIT_ASSERT(res.Root);
  371. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  372. if (word == "Sort") {
  373. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("\"-\""));
  374. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("\"key\""));
  375. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("\"subkey\""));
  376. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("(Bool 'false)"));
  377. } else if (word == "Write!") {
  378. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("'columns"));
  379. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("\"key\""));
  380. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("\"subkey\""));
  381. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("prefix"));
  382. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("\"i.\""));
  383. }
  384. };
  385. TWordCountHive elementStat = {{TString("Sort"), 0}, {TString("Write!"), 0}};
  386. VerifyProgram(res, elementStat, verifyLine);
  387. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Sort"]);
  388. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write!"]);
  389. }
  390. Y_UNIT_TEST(SelectOrderByExpressionAsc) {
  391. NYql::TAstParseResult res = SqlToYql("select i.key, i.subkey from plato.Input as i order by cast(key as uint32) % cast(i.subkey as uint32) asc");
  392. UNIT_ASSERT(res.Root);
  393. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  394. if (word == "Sort") {
  395. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("\"%\""));
  396. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("\"key\""));
  397. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("\"subkey\""));
  398. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("(Bool 'true)"));
  399. } else if (word == "Write!") {
  400. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("'columns"));
  401. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("\"key\""));
  402. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("\"subkey\""));
  403. UNIT_ASSERT_VALUES_EQUAL(TString::npos, line.find("i."));
  404. }
  405. };
  406. TWordCountHive elementStat = {{TString("Sort"), 0}, {TString("Write!"), 0}};
  407. VerifyProgram(res, elementStat, verifyLine);
  408. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Sort"]);
  409. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write!"]);
  410. }
  411. Y_UNIT_TEST(ReferenceToKeyInSubselect) {
  412. NYql::TAstParseResult res = SqlToYql("select b.key from (select a.key from plato.Input as a) as b;");
  413. UNIT_ASSERT(res.Root);
  414. }
  415. Y_UNIT_TEST(OrderByCastValue) {
  416. NYql::TAstParseResult res = SqlToYql("select i.key, i.subkey from plato.Input as i order by cast(key as uint32) desc;");
  417. UNIT_ASSERT(res.Root);
  418. }
  419. Y_UNIT_TEST(GroupByCastValue) {
  420. NYql::TAstParseResult res = SqlToYql("select count(1) from plato.Input as i group by cast(key as uint8);");
  421. UNIT_ASSERT(res.Root);
  422. }
  423. Y_UNIT_TEST(KeywordInSelectColumns) {
  424. NYql::TAstParseResult res = SqlToYql("select in, s.check from (select 1 as in, \"test\" as check) as s;");
  425. UNIT_ASSERT(res.Root);
  426. }
  427. Y_UNIT_TEST(SelectAllGroupBy) {
  428. NYql::TAstParseResult res = SqlToYql("select * from plato.Input group by subkey;");
  429. UNIT_ASSERT(res.Root);
  430. }
  431. Y_UNIT_TEST(PrimaryKeyParseCorrect) {
  432. NYql::TAstParseResult res = SqlToYql("USE plato; CREATE TABLE tableName (Key Uint32, Subkey Int64, Value String, PRIMARY KEY (Key, Subkey));");
  433. UNIT_ASSERT(res.Root);
  434. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  435. if (word == "Write") {
  436. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("\"Key\""));
  437. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("\"Subkey\""));
  438. }
  439. };
  440. TWordCountHive elementStat = {{TString("Write"), 0}, {TString("primarykey"), 0}};
  441. VerifyProgram(res, elementStat, verifyLine);
  442. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  443. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["primarykey"]);
  444. }
  445. Y_UNIT_TEST(DeleteFromTableByKey) {
  446. NYql::TAstParseResult res = SqlToYql("delete from plato.Input where key = 200;", 10, "kikimr");
  447. UNIT_ASSERT(res.Root);
  448. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  449. if (word == "Write") {
  450. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("('mode 'delete)"));
  451. }
  452. };
  453. TWordCountHive elementStat = {{TString("Write"), 0}};
  454. VerifyProgram(res, elementStat, verifyLine);
  455. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  456. }
  457. Y_UNIT_TEST(DeleteFromTable) {
  458. NYql::TAstParseResult res = SqlToYql("delete from plato.Input;", 10, "kikimr");
  459. UNIT_ASSERT(res.Root);
  460. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  461. if (word == "Write") {
  462. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("('mode 'delete)"));
  463. }
  464. };
  465. TWordCountHive elementStat = {{TString("Write"), 0}};
  466. VerifyProgram(res, elementStat, verifyLine);
  467. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  468. }
  469. Y_UNIT_TEST(DeleteFromTableOnValues) {
  470. NYql::TAstParseResult res = SqlToYql("delete from plato.Input on (key) values (1);",
  471. 10, "kikimr");
  472. UNIT_ASSERT(res.Root);
  473. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  474. if (word == "Write") {
  475. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("('mode 'delete_on)"));
  476. }
  477. };
  478. TWordCountHive elementStat = {{TString("Write"), 0}};
  479. VerifyProgram(res, elementStat, verifyLine);
  480. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  481. }
  482. Y_UNIT_TEST(DeleteFromTableOnSelect) {
  483. NYql::TAstParseResult res = SqlToYql(
  484. "delete from plato.Input on select key from plato.Input where value > 0;", 10, "kikimr");
  485. UNIT_ASSERT(res.Root);
  486. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  487. if (word == "Write") {
  488. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("('mode 'delete_on)"));
  489. }
  490. };
  491. TWordCountHive elementStat = {{TString("Write"), 0}};
  492. VerifyProgram(res, elementStat, verifyLine);
  493. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  494. }
  495. Y_UNIT_TEST(UpdateByValues) {
  496. NYql::TAstParseResult res = SqlToYql("update plato.Input set key = 777, value = 'cool' where key = 200;", 10, "kikimr");
  497. UNIT_ASSERT(res.Root);
  498. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  499. if (word == "Write") {
  500. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("('mode 'update)"));
  501. } else if (word == "AsStruct") {
  502. const bool isKey = line.find("key") != TString::npos;
  503. const bool isValue = line.find("value") != TString::npos;
  504. UNIT_ASSERT(isKey || isValue);
  505. if (isKey) {
  506. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find(Quote("777")));
  507. } else if (isValue) {
  508. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find(Quote("cool")));
  509. }
  510. }
  511. };
  512. TWordCountHive elementStat = {{TString("Write"), 0}, {TString("AsStruct"), 0}};
  513. VerifyProgram(res, elementStat, verifyLine);
  514. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  515. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["AsStruct"]);
  516. }
  517. Y_UNIT_TEST(UpdateByMultiValues) {
  518. NYql::TAstParseResult res = SqlToYql("update plato.Input set (key, value, subkey) = ('2','ddd',':') where key = 200;", 10, "kikimr");
  519. UNIT_ASSERT(res.Root);
  520. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  521. if (word == "Write") {
  522. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("('mode 'update)"));
  523. } else if (word == "AsStruct") {
  524. const bool isKey = line.find("key") != TString::npos;
  525. const bool isSubkey = line.find("subkey") != TString::npos;
  526. const bool isValue = line.find("value") != TString::npos;
  527. UNIT_ASSERT(isKey || isSubkey || isValue);
  528. if (isKey && !isSubkey) {
  529. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find(Quote("2")));
  530. } else if (isSubkey) {
  531. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find(Quote(":")));
  532. } else if (isValue) {
  533. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find(Quote("ddd")));
  534. }
  535. }
  536. };
  537. TWordCountHive elementStat = {{TString("Write"), 0}, {TString("AsStruct"), 0}};
  538. VerifyProgram(res, elementStat, verifyLine);
  539. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  540. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["AsStruct"]);
  541. }
  542. Y_UNIT_TEST(UpdateBySelect) {
  543. NYql::TAstParseResult res = SqlToYql("update plato.Input set (key, value, subkey) = (select key, value, subkey from plato.Input where key = 911) where key = 200;", 10, "kikimr");
  544. UNIT_ASSERT(res.Root);
  545. int lineIndex = 0;
  546. int writeLineIndex = -1;
  547. bool found = false;
  548. TVerifyLineFunc verifyLine = [&lineIndex, &writeLineIndex, &found](const TString& word, const TString& line) {
  549. if (word == "Write") {
  550. writeLineIndex = lineIndex;
  551. found = line.find("('mode 'update)") != TString::npos;
  552. } else if (word == "mode") {
  553. found |= lineIndex == writeLineIndex + 1 && line.find("('mode 'update)") != TString::npos;
  554. UNIT_ASSERT(found);
  555. }
  556. ++lineIndex;
  557. };
  558. TWordCountHive elementStat = {{TString("Write"), 0}, {TString("mode"), 0}};
  559. VerifyProgram(res, elementStat, verifyLine);
  560. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  561. }
  562. Y_UNIT_TEST(UpdateSelfModifyAll) {
  563. NYql::TAstParseResult res = SqlToYql("update plato.Input set subkey = subkey + 's';", 10, "kikimr");
  564. UNIT_ASSERT(res.Root);
  565. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  566. if (word == "Write") {
  567. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("('mode 'update)"));
  568. } else if (word == "AsStruct") {
  569. const bool isSubkey = line.find("subkey") != TString::npos;
  570. UNIT_ASSERT(isSubkey);
  571. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find(Quote("subkey")));
  572. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find(Quote("s")));
  573. }
  574. };
  575. TWordCountHive elementStat = {{TString("Write"), 0}, {TString("AsStruct"), 0}};
  576. VerifyProgram(res, elementStat, verifyLine);
  577. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  578. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["AsStruct"]);
  579. }
  580. Y_UNIT_TEST(UpdateOnValues) {
  581. NYql::TAstParseResult res = SqlToYql("update plato.Input on (key, value) values (5, 'cool')", 10, "kikimr");
  582. UNIT_ASSERT(res.Root);
  583. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  584. if (word == "Write") {
  585. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("('mode 'update_on)"));
  586. }
  587. };
  588. TWordCountHive elementStat = {{TString("Write"), 0}};
  589. VerifyProgram(res, elementStat, verifyLine);
  590. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  591. }
  592. Y_UNIT_TEST(UpdateOnSelect) {
  593. NYql::TAstParseResult res = SqlToYql(
  594. "update plato.Input on select key, value + 1 as value from plato.Input", 10, "kikimr");
  595. UNIT_ASSERT(res.Root);
  596. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  597. if (word == "Write") {
  598. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("('mode 'update_on)"));
  599. }
  600. };
  601. TWordCountHive elementStat = {{TString("Write"), 0}};
  602. VerifyProgram(res, elementStat, verifyLine);
  603. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Write"]);
  604. }
  605. Y_UNIT_TEST(UnionAllTest) {
  606. NYql::TAstParseResult res = SqlToYql("SELECT key FROM plato.Input UNION ALL select subkey FROM plato.Input;");
  607. UNIT_ASSERT(res.Root);
  608. TWordCountHive elementStat = {{TString("UnionAll"), 0}};
  609. VerifyProgram(res, elementStat, {});
  610. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["UnionAll"]);
  611. }
  612. Y_UNIT_TEST(DeclareDecimalParameter) {
  613. NYql::TAstParseResult res = SqlToYql("declare $value as Decimal(22,9); select $value as cnt;");
  614. UNIT_ASSERT(res.Root);
  615. }
  616. Y_UNIT_TEST(SimpleGroupBy) {
  617. NYql::TAstParseResult res = SqlToYql("select count(1),z from plato.Input group by key as z order by z;");
  618. UNIT_ASSERT(res.Root);
  619. }
  620. Y_UNIT_TEST(EmptyColumnName0) {
  621. /// Now it's parsed well and error occur on validate step like "4:31:Empty struct member name is not allowed" in "4:31:Function: AddMember"
  622. NYql::TAstParseResult res = SqlToYql("insert into plato.Output ([], list1) values (0, AsList(0, 1, 2));");
  623. /// Verify that parsed well without crash
  624. UNIT_ASSERT(res.Root);
  625. }
  626. Y_UNIT_TEST(KikimrRollback) {
  627. NYql::TAstParseResult res = SqlToYql("use plato; rollback;", 10, "kikimr");
  628. UNIT_ASSERT(res.Root);
  629. TWordCountHive elementStat = {{TString("rollback"), 0}};
  630. VerifyProgram(res, elementStat);
  631. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["rollback"]);
  632. }
  633. Y_UNIT_TEST(PragmaFile) {
  634. NYql::TAstParseResult res = SqlToYql(R"(pragma file("HW", "sbr:181041334");)");
  635. UNIT_ASSERT(res.Root);
  636. TWordCountHive elementStat = {{TString(R"((let world (Configure! world (DataSource '"config") '"AddFileByUrl" '"HW" '"sbr:181041334")))"), 0}};
  637. VerifyProgram(res, elementStat);
  638. UNIT_ASSERT_VALUES_EQUAL(1, elementStat.cbegin()->second);
  639. }
  640. Y_UNIT_TEST(DoNotCrashOnNamedInFilter) {
  641. NYql::TAstParseResult res = SqlToYql("USE plato; $all = YQL::@@(lambda '(table_name) (Bool 'true))@@; SELECT * FROM FILTER(Input, $all)");
  642. UNIT_ASSERT(res.Root);
  643. }
  644. Y_UNIT_TEST(PragmasFileAndUdfOrder) {
  645. NYql::TAstParseResult res = SqlToYql(R"(
  646. PRAGMA file("libvideoplayers_udf.so", "https://proxy.sandbox.yandex-team.ru/235185290");
  647. PRAGMA udf("libvideoplayers_udf.so");
  648. )");
  649. UNIT_ASSERT(res.Root);
  650. const auto programm = GetPrettyPrint(res);
  651. const auto file = programm.find("AddFileByUrl");
  652. const auto udfs = programm.find("ImportUdfs");
  653. UNIT_ASSERT(file < udfs);
  654. }
  655. Y_UNIT_TEST(ProcessUserType) {
  656. NYql::TAstParseResult res = SqlToYql("process plato.Input using Kikimr::PushData($ROWS);", 1, TString(NYql::KikimrProviderName));
  657. UNIT_ASSERT(res.Root);
  658. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  659. if (word == "Kikimr.PushData") {
  660. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("TupleType"));
  661. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("TypeOf"));
  662. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("inputRowsList"));
  663. }
  664. };
  665. TWordCountHive elementStat = {{TString("Kikimr.PushData"), 0}};
  666. VerifyProgram(res, elementStat, verifyLine);
  667. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["Kikimr.PushData"]);
  668. }
  669. Y_UNIT_TEST(ProcessUserTypeAuth) {
  670. NYql::TAstParseResult res = SqlToYql("process plato.Input using YDB::PushData($ROWS, AsTuple('oauth', SecureParam('api:oauth')));", 1, TString(NYql::KikimrProviderName));
  671. UNIT_ASSERT(res.Root);
  672. TVerifyLineFunc verifyLine = [](const TString& word, const TString& line) {
  673. if (word == "YDB.PushData") {
  674. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("TupleType"));
  675. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("TypeOf"));
  676. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("inputRowsList"));
  677. UNIT_ASSERT_VALUES_UNEQUAL(TString::npos, line.find("api:oauth"));
  678. }
  679. };
  680. TWordCountHive elementStat = {{TString("YDB.PushData"), 0}};
  681. VerifyProgram(res, elementStat, verifyLine);
  682. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["YDB.PushData"]);
  683. }
  684. Y_UNIT_TEST(SelectStreamRtmr) {
  685. NYql::TAstParseResult res = SqlToYql(
  686. "USE plato; INSERT INTO Output SELECT STREAM key FROM Input;",
  687. 10, TString(NYql::RtmrProviderName));
  688. UNIT_ASSERT(res.Root);
  689. }
  690. Y_UNIT_TEST(GroupByHopRtmr) {
  691. NYql::TAstParseResult res = SqlToYql(R"(
  692. USE plato; INSERT INTO Output SELECT STREAM key, SUM(value) AS value FROM Input
  693. GROUP BY key, HOP(subkey, "PT10S", "PT30S", "PT20S");
  694. )", 10, TString(NYql::RtmrProviderName));
  695. UNIT_ASSERT(res.Root);
  696. }
  697. Y_UNIT_TEST(KikimrInserts) {
  698. NYql::TAstParseResult res = SqlToYql(R"(
  699. USE plato;
  700. INSERT INTO Output SELECT key, value FROM Input;
  701. INSERT OR ABORT INTO Output SELECT key, value FROM Input;
  702. INSERT OR IGNORE INTO Output SELECT key, value FROM Input;
  703. INSERT OR REVERT INTO Output SELECT key, value FROM Input;
  704. )", 10, TString(NYql::KikimrProviderName));
  705. UNIT_ASSERT(res.Root);
  706. }
  707. Y_UNIT_TEST(WarnMissingIsBeforeNotNull) {
  708. NYql::TAstParseResult res = SqlToYql("select 1 NOT NULL");
  709. UNIT_ASSERT(res.Root);
  710. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:8: Warning: Missing IS keyword before NOT NULL, code: 4507\n");
  711. }
  712. Y_UNIT_TEST(WarnLegacySuffixForTinyIntLiteral) {
  713. NYql::TAstParseResult res = SqlToYql("select 1b, 0ub, 0b0b;");
  714. UNIT_ASSERT(res.Root);
  715. UNIT_ASSERT_NO_DIFF(Err2Str(res),
  716. "<main>:1:8: Warning: Deprecated suffix 'b' - please use 't' suffix for 8-bit integers, code: 4508\n"
  717. "<main>:1:12: Warning: Deprecated suffix 'b' - please use 't' suffix for 8-bit integers, code: 4508\n"
  718. "<main>:1:17: Warning: Deprecated suffix 'b' - please use 't' suffix for 8-bit integers, code: 4508\n"
  719. );
  720. }
  721. Y_UNIT_TEST(WarnMultiWayJoinWithUsing) {
  722. NYql::TAstParseResult res = SqlToYql(
  723. "USE plato;\n"
  724. "PRAGMA DisableSimpleColumns;\n"
  725. "SELECT *\n"
  726. "FROM Input1 AS a\n"
  727. "JOIN Input2 AS b USING(key)\n"
  728. "JOIN Input3 AS c ON a.key = c.key;\n"
  729. );
  730. UNIT_ASSERT(res.Root);
  731. UNIT_ASSERT_NO_DIFF(Err2Str(res),
  732. "<main>:5:24: Warning: Multi-way JOINs should be connected with ON clause instead of USING clause, code: 4514\n"
  733. );
  734. }
  735. Y_UNIT_TEST(StringLiteralWithEscapedBackslash) {
  736. NYql::TAstParseResult res1 = SqlToYql(R"foo(SELECT 'a\\';)foo");
  737. NYql::TAstParseResult res2 = SqlToYql(R"foo(SELECT "a\\";)foo");
  738. UNIT_ASSERT(res1.Root);
  739. UNIT_ASSERT(res2.Root);
  740. TWordCountHive elementStat = {{TString("a\\"), 0}};
  741. VerifyProgram(res1, elementStat);
  742. UNIT_ASSERT_VALUES_EQUAL(1, elementStat["a\\"]);
  743. VerifyProgram(res2, elementStat);
  744. UNIT_ASSERT_VALUES_EQUAL(2, elementStat["a\\"]);
  745. }
  746. Y_UNIT_TEST(StringMultiLineLiteralWithEscapes) {
  747. UNIT_ASSERT(SqlToYql("SELECT @@@foo@@@@bar@@@").IsOk());
  748. UNIT_ASSERT(SqlToYql("SELECT @@@@@@@@@").IsOk());
  749. }
  750. Y_UNIT_TEST(StringMultiLineLiteralConsequitiveAt) {
  751. UNIT_ASSERT(!SqlToYql("SELECT @").IsOk());
  752. UNIT_ASSERT(!SqlToYql("SELECT @@").IsOk());
  753. UNIT_ASSERT(!SqlToYql("SELECT @@@").IsOk());
  754. UNIT_ASSERT( SqlToYql("SELECT @@@@").IsOk());
  755. UNIT_ASSERT( SqlToYql("SELECT @@@@@").IsOk());
  756. UNIT_ASSERT(!SqlToYql("SELECT @@@@@@").IsOk());
  757. UNIT_ASSERT(!SqlToYql("SELECT @@@@@@@").IsOk());
  758. UNIT_ASSERT( SqlToYql("SELECT @@@@@@@@").IsOk());
  759. UNIT_ASSERT( SqlToYql("SELECT @@@@@@@@@").IsOk());
  760. UNIT_ASSERT(!SqlToYql("SELECT @@@@@@@@@@").IsOk());
  761. }
  762. }
  763. Y_UNIT_TEST_SUITE(SqlToYQLErrors) {
  764. Y_UNIT_TEST(StrayUTF8) {
  765. /// 'c' in plato is russian here
  766. NYql::TAstParseResult res = SqlToYql("select * from сedar.Input");
  767. UNIT_ASSERT(!res.Root);
  768. TString a1 = Err2Str(res);
  769. TString a2(R"foo(<main>:1:14: Error: Unexpected character 'с' (Unicode character <1089>) : cannot match to any predicted input...
  770. <main>:1:15: Error: Unexpected character : cannot match to any predicted input...
  771. )foo");
  772. UNIT_ASSERT_NO_DIFF(a1, a2);
  773. }
  774. Y_UNIT_TEST(IvalidStringLiteralWithEscapedBackslash) {
  775. NYql::TAstParseResult res1 = SqlToYql(R"foo($bar = 'a\\'b';)foo");
  776. NYql::TAstParseResult res2 = SqlToYql(R"foo($bar = "a\\"b";)foo");
  777. UNIT_ASSERT(!res1.Root);
  778. UNIT_ASSERT(!res2.Root);
  779. TString header = "<main>:1:15: Error: Unexpected character : syntax error...\n\n";
  780. TString err1 = "<main>:1:7: Error: Unexpected token '\'a\\\\\'' : cannot match to any predicted input...\n\n";
  781. TString err2 = "<main>:1:7: Error: Unexpected token '\"a\\\\\"' : cannot match to any predicted input...\n\n";
  782. UNIT_ASSERT_NO_DIFF(Err2Str(res1), header + err1);
  783. UNIT_ASSERT_NO_DIFF(Err2Str(res2), header + err2);
  784. }
  785. Y_UNIT_TEST(InvalidHexInStringLiteral) {
  786. NYql::TAstParseResult res = SqlToYql("select \"foo\\x1\\xfe\"");
  787. UNIT_ASSERT(!res.Root);
  788. TString a1 = Err2Str(res);
  789. TString a2 = "<main>:1:15: Error: Failed to parse string literal: Invalid hexadecimal value\n";
  790. UNIT_ASSERT_NO_DIFF(a1, a2);
  791. }
  792. Y_UNIT_TEST(InvalidOctalInMultilineStringLiteral) {
  793. NYql::TAstParseResult res = SqlToYql("select \"foo\n"
  794. "bar\n"
  795. "\\01\"");
  796. UNIT_ASSERT(!res.Root);
  797. TString a1 = Err2Str(res);
  798. TString a2 = "<main>:3:4: Error: Failed to parse string literal: Invalid octal value\n";
  799. UNIT_ASSERT_NO_DIFF(a1, a2);
  800. }
  801. Y_UNIT_TEST(InvalidOctalInMultilineStringIdentifier) {
  802. NYql::TAstParseResult res = SqlToYql("use plato; select * from `In\\0123\n\n"
  803. "\\`\\01put`");
  804. UNIT_ASSERT(!res.Root);
  805. TString a1 = Err2Str(res);
  806. TString a2 = "<main>:3:6: Error: Cannot parse broken identifier: Invalid octal value\n";
  807. UNIT_ASSERT_NO_DIFF(a1, a2);
  808. }
  809. Y_UNIT_TEST(WrongTokenTrivial) {
  810. NYql::TAstParseResult res = SqlToYql("foo");
  811. UNIT_ASSERT(!res.Root);
  812. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:0: Error: Unexpected token 'foo' : cannot match to any predicted input...\n\n");
  813. }
  814. Y_UNIT_TEST(InvalidDoubleAtString) {
  815. NYql::TAstParseResult res = SqlToYql("select @@@@@@");
  816. UNIT_ASSERT(!res.Root);
  817. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:13: Error: Unexpected character : syntax error...\n\n"
  818. "<main>:1:0: Error: Unexpected token absence : cannot match to any predicted input...\n\n" );
  819. }
  820. Y_UNIT_TEST(InvalidDoubleAtStringWhichWasAcceptedEarlier) {
  821. NYql::TAstParseResult res = SqlToYql("SELECT @@foo@@ @ @@bar@@");
  822. UNIT_ASSERT(!res.Root);
  823. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:7: Error: Unexpected token '@@foo@@' : cannot match to any predicted input...\n\n");
  824. }
  825. Y_UNIT_TEST(InvalidStringFromTable) {
  826. NYql::TAstParseResult res = SqlToYql("select \"FOO\"\"BAR from plato.foo");
  827. UNIT_ASSERT(!res.Root);
  828. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:31: Error: Unexpected character : syntax error...\n\n");
  829. }
  830. Y_UNIT_TEST(InvalidDoubleAtStringFromTable) {
  831. NYql::TAstParseResult res = SqlToYql("select @@@@@@ from plato.foo");
  832. UNIT_ASSERT(!res.Root);
  833. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:28: Error: Unexpected character : syntax error...\n\n"
  834. "<main>:1:0: Error: Unexpected token absence : cannot match to any predicted input...\n\n");
  835. }
  836. Y_UNIT_TEST(SelectInvalidSyntax) {
  837. NYql::TAstParseResult res = SqlToYql("select 1 form Wat");
  838. UNIT_ASSERT(!res.Root);
  839. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:9: Error: Unexpected token 'form' : syntax error...\n\n");
  840. }
  841. Y_UNIT_TEST(SelectNoCluster) {
  842. NYql::TAstParseResult res = SqlToYql("select foo from bar");
  843. UNIT_ASSERT(!res.Root);
  844. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:1: Error: No cluster name given and no default cluster is selected\n");
  845. }
  846. Y_UNIT_TEST(SelectDuplicateColumns) {
  847. NYql::TAstParseResult res = SqlToYql("select a, a from plato.Input");
  848. UNIT_ASSERT(!res.Root);
  849. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:11: Error: Unable to use duplicate column names. Collision in name: a\n");
  850. }
  851. Y_UNIT_TEST(SelectDuplicateLabels) {
  852. NYql::TAstParseResult res = SqlToYql("select a as foo, b as foo from plato.Input");
  853. UNIT_ASSERT(!res.Root);
  854. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:18: Error: Unable to use duplicate column names. Collision in name: foo\n");
  855. }
  856. Y_UNIT_TEST(SelectCaseWithoutThen) {
  857. NYql::TAstParseResult res = SqlToYql("select case when true 1;");
  858. UNIT_ASSERT(!res.Root);
  859. UNIT_ASSERT_NO_DIFF(Err2Str(res),
  860. "<main>:1:22: Error: Unexpected token absence : Missing THEN \n\n"
  861. "<main>:1:23: Error: Unexpected token absence : Missing END \n\n"
  862. );
  863. }
  864. Y_UNIT_TEST(SelectComplexCaseWithoutThen) {
  865. NYql::TAstParseResult res = SqlToYql(
  866. "SELECT *\n"
  867. "FROM plato.Input AS a\n"
  868. "WHERE CASE WHEN a.key = \"foo\" a.subkey ELSE a.value END\n"
  869. );
  870. UNIT_ASSERT_NO_DIFF(Err2Str(res),
  871. "<main>:3:24: Error: Unexpected token '\"foo\"' : cannot match to any predicted input...\n\n"
  872. "<main>:3:39: Error: Unexpected token absence : Missing THEN \n\n"
  873. );
  874. }
  875. Y_UNIT_TEST(SelectCaseWithoutEnd) {
  876. NYql::TAstParseResult res = SqlToYql("select case a when b then c end from plato.Input");
  877. UNIT_ASSERT(!res.Root);
  878. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:8: Error: ELSE is required\n");
  879. }
  880. Y_UNIT_TEST(SelectWithBadAggregationNoInput) {
  881. NYql::TAstParseResult res = SqlToYql("select a, Min(b), c");
  882. UNIT_ASSERT(!res.Root);
  883. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:1: Error: Source does not allow column references\n"
  884. "<main>:1:1: Error: Source does not allow column references\n"
  885. "<main>:1:1: Error: Source does not allow column references\n");
  886. }
  887. Y_UNIT_TEST(SelectWithBadAggregation) {
  888. NYql::TAstParseResult res = SqlToYql("select count(*), key from plato.Input");
  889. UNIT_ASSERT(!res.Root);
  890. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:18: Error: Expression has to be an aggregation function or key column, because aggregation is used elsewhere in this subquery\n");
  891. }
  892. Y_UNIT_TEST(SelectWithBadAggregatedTerms) {
  893. NYql::TAstParseResult res = SqlToYql("select key, subkey from plato.Input group by key");
  894. UNIT_ASSERT(!res.Root);
  895. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:13: Error: Expression has to be an aggregation function or key column, because aggregation is used elsewhere in this subquery\n");
  896. }
  897. Y_UNIT_TEST(SelectWithBadAggregationInHaving) {
  898. NYql::TAstParseResult res = SqlToYql("select key from plato.Input group by key having value == \"foo\"");
  899. UNIT_ASSERT(!res.Root);
  900. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:55: Error: Expression has to be an aggregation function or key column, because aggregation is used elsewhere in this subquery\n");
  901. }
  902. Y_UNIT_TEST(SelectWithDuplicateGroupingColumns) {
  903. NYql::TAstParseResult res = SqlToYql("select c from plato.Input group by c, c");
  904. UNIT_ASSERT(!res.Root);
  905. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:8: Error: Duplicate grouping column: c\n");
  906. }
  907. Y_UNIT_TEST(SelectWithBadAggregationInGrouping) {
  908. NYql::TAstParseResult res = SqlToYql("select a, Min(b), c group by c");
  909. UNIT_ASSERT(!res.Root);
  910. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:1: Error: Source does not allow column references\n"
  911. "<main>:1:1: Error: Source does not allow column references\n"
  912. "<main>:1:1: Error: Source does not allow column references\n"
  913. "<main>:1:1: Error: Source does not allow column references\n"
  914. "<main>:1:1: Error: No aggregations were specified\n");
  915. }
  916. Y_UNIT_TEST(SelectWithOpOnBadAggregation) {
  917. NYql::TAstParseResult res = SqlToYql("select a + Min(b) from plato.Input");
  918. UNIT_ASSERT(!res.Root);
  919. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:10: Error: Expression has to be an aggregation function or key column, because aggregation is used elsewhere in this subquery\n");
  920. }
  921. Y_UNIT_TEST(SelectOrderByConstantNum) {
  922. NYql::TAstParseResult res = SqlToYql("select a from plato.Input order by 1");
  923. UNIT_ASSERT(!res.Root);
  924. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:36: Error: Unable to ORDER BY constant expression\n");
  925. }
  926. Y_UNIT_TEST(SelectOrderByConstantExpr) {
  927. NYql::TAstParseResult res = SqlToYql("select a from plato.Input order by 1 * 42");
  928. UNIT_ASSERT(!res.Root);
  929. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:38: Error: Unable to ORDER BY constant expression\n");
  930. }
  931. Y_UNIT_TEST(SelectOrderByConstantString) {
  932. NYql::TAstParseResult res = SqlToYql("select a from plato.Input order by \"nest\"");
  933. UNIT_ASSERT(!res.Root);
  934. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:36: Error: Unable to ORDER BY constant expression\n");
  935. }
  936. Y_UNIT_TEST(SelectOrderByAggregated) {
  937. NYql::TAstParseResult res = SqlToYql("select a from plato.Input order by min(a)");
  938. UNIT_ASSERT(!res.Root);
  939. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:36: Error: Unable to ORDER BY aggregated values\n");
  940. }
  941. Y_UNIT_TEST(ErrorInOrderByExpresison) {
  942. NYql::TAstParseResult res = SqlToYql("select key, value from plato.Input order by (key as zey)");
  943. UNIT_ASSERT(!res.Root);
  944. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:45: Error: You should use in ORDER BY column name, qualified field, callable function or expression\n");
  945. }
  946. Y_UNIT_TEST(SelectAggregatedWhere) {
  947. NYql::TAstParseResult res = SqlToYql("select * from plato.Input where count(key)");
  948. UNIT_ASSERT(!res.Root);
  949. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:33: Error: Can not use aggregated values in filtering\n");
  950. }
  951. Y_UNIT_TEST(DoubleFrom) {
  952. NYql::TAstParseResult res = SqlToYql("from plato.Input select * from plato.Input");
  953. UNIT_ASSERT(!res.Root);
  954. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:27: Error: Only one FROM clause is allowed\n");
  955. }
  956. Y_UNIT_TEST(SelectJoinMissingCorrName) {
  957. NYql::TAstParseResult res = SqlToYql("select * from plato.Input1 as a join plato.Input2 as b on a.key == key");
  958. UNIT_ASSERT(!res.Root);
  959. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:65: Error: JOIN: column requires correlation name\n");
  960. }
  961. Y_UNIT_TEST(SelectJoinEmptyCorrNames) {
  962. NYql::TAstParseResult res = SqlToYql(
  963. "$left = (SELECT * FROM plato.Input1 LIMIT 2);\n"
  964. "$right = (SELECT * FROM plato.Input2 LIMIT 2);\n"
  965. "SELECT * FROM $left FULL JOIN $right USING (key);\n"
  966. );
  967. UNIT_ASSERT(!res.Root);
  968. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:3:45: Error: At least one correlation name is required in join\n");
  969. }
  970. Y_UNIT_TEST(SelectJoinSameCorrNames) {
  971. NYql::TAstParseResult res = SqlToYql("SELECT Input.key FROM plato.Input JOIN plato.Input1 ON Input.key == Input.subkey\n");
  972. UNIT_ASSERT(!res.Root);
  973. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:66: Error: JOIN: different correlation names are required for joined tables\n");
  974. }
  975. Y_UNIT_TEST(SelectJoinConstPredicateArg) {
  976. NYql::TAstParseResult res = SqlToYql("SELECT * FROM plato.Input1 as A JOIN plato.Input2 as B ON A.key == B.key AND A.subkey == \"wtf\"\n");
  977. UNIT_ASSERT(!res.Root);
  978. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:87: Error: JOIN: equality predicate arguments must not be constant\n");
  979. }
  980. Y_UNIT_TEST(SelectJoinNonEqualityPredicate) {
  981. NYql::TAstParseResult res = SqlToYql("SELECT * FROM plato.Input1 as A JOIN plato.Input2 as B ON A.key == B.key AND A.subkey > B.subkey\n");
  982. UNIT_ASSERT(!res.Root);
  983. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:87: Error: JOIN ON expression must be a conjunction of equality predicates\n");
  984. }
  985. Y_UNIT_TEST(SelectEquiJoinCorrNameOutOfScope) {
  986. NYql::TAstParseResult res = SqlToYql(
  987. "PRAGMA equijoin;\n"
  988. "SELECT * FROM plato.A JOIN plato.B ON A.key == C.key JOIN plato.C ON A.subkey == C.subkey;\n"
  989. );
  990. UNIT_ASSERT(!res.Root);
  991. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:2:45: Error: JOIN: can not use source: C in equality predicate, it is out of current join scope\n");
  992. }
  993. Y_UNIT_TEST(SelectEquiJoinNoRightSource) {
  994. NYql::TAstParseResult res = SqlToYql(
  995. "PRAGMA equijoin;\n"
  996. "SELECT * FROM plato.A JOIN plato.B ON A.key == B.key JOIN plato.C ON A.subkey == B.subkey;\n"
  997. );
  998. UNIT_ASSERT(!res.Root);
  999. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:2:79: Error: JOIN ON equality predicate must have one of its arguments from the rightmost source\n");
  1000. }
  1001. Y_UNIT_TEST(InsertNoCluster) {
  1002. NYql::TAstParseResult res = SqlToYql("insert into Output (foo) values (1)");
  1003. UNIT_ASSERT(!res.Root);
  1004. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:34: Error: No cluster name given and no default cluster is selected\n");
  1005. }
  1006. Y_UNIT_TEST(InsertValuesNoLabels) {
  1007. NYql::TAstParseResult res = SqlToYql("insert into plato.Output values (1)");
  1008. UNIT_ASSERT(!res.Root);
  1009. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:19: Error: INSERT INTO ... VALUES requires specification of table columns\n");
  1010. }
  1011. Y_UNIT_TEST(UpsertValuesNoLabelsKikimr) {
  1012. NYql::TAstParseResult res = SqlToYql("upsert into plato.Output values (1)", 10, TString(NYql::KikimrProviderName));
  1013. UNIT_ASSERT(!res.Root);
  1014. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:19: Error: UPSERT INTO ... VALUES requires specification of table columns\n");
  1015. }
  1016. Y_UNIT_TEST(ReplaceValuesNoLabelsKikimr) {
  1017. NYql::TAstParseResult res = SqlToYql("replace into plato.Output values (1)", 10, TString(NYql::KikimrProviderName));
  1018. UNIT_ASSERT(!res.Root);
  1019. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:20: Error: REPLACE INTO ... VALUES requires specification of table columns\n");
  1020. }
  1021. Y_UNIT_TEST(InsertValuesInvalidLabels) {
  1022. NYql::TAstParseResult res = SqlToYql("insert into plato.Output (foo) values (1, 2)");
  1023. UNIT_ASSERT(!res.Root);
  1024. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:27: Error: VALUES have 2 columns, INSERT INTO expects: 1\n");
  1025. }
  1026. Y_UNIT_TEST(BuiltinFileOpNoArgs) {
  1027. NYql::TAstParseResult res = SqlToYql("select FilePath()");
  1028. UNIT_ASSERT(!res.Root);
  1029. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:8: Error: FilePath() requires exactly 1 arguments, given: 0\n");
  1030. }
  1031. Y_UNIT_TEST(ProcessWithHaving) {
  1032. NYql::TAstParseResult res = SqlToYql("process plato.Input using some::udf(value) having value == 1");
  1033. UNIT_ASSERT(!res.Root);
  1034. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:15: Error: PROCESS does not allow HAVING yet! You may request it on yql@ maillist.\n");
  1035. }
  1036. Y_UNIT_TEST(ReduceNoBy) {
  1037. NYql::TAstParseResult res = SqlToYql("reduce plato.Input using some::udf(value)");
  1038. UNIT_ASSERT(!res.Root);
  1039. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:19: Error: Unexpected token absence : Missing ON \n\n<main>:1:25: Error: Unexpected token absence : Missing USING \n\n");
  1040. }
  1041. Y_UNIT_TEST(ReduceDistinct) {
  1042. NYql::TAstParseResult res = SqlToYql("reduce plato.Input on key using some::udf(distinct value)");
  1043. UNIT_ASSERT(!res.Root);
  1044. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:52: Error: REDUCE does not allow DISTINCT arguments\n");
  1045. }
  1046. Y_UNIT_TEST(ProcessMultipleRowsPlaceholders) {
  1047. NYql::TAstParseResult res = SqlToYql("process plato.Input using some::udf($ROWS, $ROWS)");
  1048. UNIT_ASSERT(!res.Root);
  1049. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:15: Error: Only single instance of $ROWS is allowed.\n");
  1050. }
  1051. Y_UNIT_TEST(ProcessRowInExpression) {
  1052. NYql::TAstParseResult res = SqlToYql("process plato.Input using some::udf($ROW + 1)");
  1053. UNIT_ASSERT(!res.Root);
  1054. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:15: Error: $ROW can't be used as a part of expression.\n");
  1055. }
  1056. Y_UNIT_TEST(ProcessRowsInExpression) {
  1057. NYql::TAstParseResult res = SqlToYql("process plato.Input using some::udf($ROWS + 1)");
  1058. UNIT_ASSERT(!res.Root);
  1059. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:15: Error: $ROWS can't be used as a part of expression.\n");
  1060. }
  1061. Y_UNIT_TEST(ProcessRowsWithColumnAccess) {
  1062. NYql::TAstParseResult res = SqlToYql("process plato.Input using some::udf(key, $ROWS)");
  1063. UNIT_ASSERT(!res.Root);
  1064. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:15: Error: Source does not allow column references\n");
  1065. }
  1066. Y_UNIT_TEST(ProcessRowsWithRowAccess) {
  1067. NYql::TAstParseResult res = SqlToYql("process plato.Input using some::udf($ROWS, $ROW)");
  1068. UNIT_ASSERT(!res.Root);
  1069. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:15: Error: Source does not allow column references\n");
  1070. }
  1071. Y_UNIT_TEST(ProcessRowsPlaceholderOutOfScope) {
  1072. NYql::TAstParseResult res = SqlToYql(
  1073. "$data = (process plato.Input using some::udf($ROWS));\n"
  1074. "SELECT * FROM $ROWS;\n"
  1075. );
  1076. UNIT_ASSERT(!res.Root);
  1077. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:2:15: Error: Unknown name: $ROWS\n");
  1078. }
  1079. Y_UNIT_TEST(ProcessWithInvalidSource) {
  1080. NYql::TAstParseResult res = SqlToYql("PROCESS $input USING YQL::AsList($ROWS);");
  1081. UNIT_ASSERT(!res.Root);
  1082. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:9: Error: Unknown name: $input\n");
  1083. }
  1084. Y_UNIT_TEST(SelectWithUnknownBind) {
  1085. NYql::TAstParseResult res = SqlToYql("select * from plato.Input where $value like 'Hell!';");
  1086. UNIT_ASSERT(!res.Root);
  1087. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:33: Error: Unknown name: $value\n");
  1088. }
  1089. Y_UNIT_TEST(CreateTableWithView) {
  1090. NYql::TAstParseResult res = SqlToYql("CREATE TABLE plato.foo:bar (key INT);");
  1091. UNIT_ASSERT(!res.Root);
  1092. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:22: Error: Unexpected token ':' : syntax error...\n\n");
  1093. }
  1094. Y_UNIT_TEST(CreateTableWithOptColumn) {
  1095. NYql::TAstParseResult res = SqlToYql("CREATE TABLE plato.foo (key \"Int32?\");");
  1096. UNIT_ASSERT(!res.Root);
  1097. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:25: Error: CREATE TABLE clause requires non-optional column types in scheme\n");
  1098. }
  1099. Y_UNIT_TEST(AsteriskWithSomethingAfter) {
  1100. NYql::TAstParseResult res = SqlToYql("select *, LENGTH(value) from plato.Input;");
  1101. UNIT_ASSERT(!res.Root);
  1102. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:8: Error: Unable to use general '*' with other columns, either specify concrete table like '<table>.*', either specify concrete columns.\n");
  1103. }
  1104. Y_UNIT_TEST(AsteriskWithSomethingBefore) {
  1105. NYql::TAstParseResult res = SqlToYql("select LENGTH(value), * from plato.Input;");
  1106. UNIT_ASSERT(!res.Root);
  1107. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:23: Error: Unable to use general '*' with other columns, either specify concrete table like '<table>.*', either specify concrete columns.\n");
  1108. }
  1109. Y_UNIT_TEST(DuplicatedQualifiedAsterisk) {
  1110. NYql::TAstParseResult res = SqlToYql("select in.*, key, in.* from plato.Input as in;");
  1111. UNIT_ASSERT(!res.Root);
  1112. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:19: Error: Unable to use twice same quialified asterisk. Invalid source: in\n");
  1113. }
  1114. Y_UNIT_TEST(BrokenLabel) {
  1115. NYql::TAstParseResult res = SqlToYql("select in.*, key as [funny.label] from plato.Input as in;");
  1116. UNIT_ASSERT(!res.Root);
  1117. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:14: Error: Unable to use '.' in column name. Invalid column name: funny.label\n");
  1118. }
  1119. Y_UNIT_TEST(KeyConflictDetect0) {
  1120. NYql::TAstParseResult res = SqlToYql("select key, in.key as key from plato.Input as in;");
  1121. UNIT_ASSERT(!res.Root);
  1122. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:13: Error: Unable to use duplicate column names. Collision in name: key\n");
  1123. }
  1124. Y_UNIT_TEST(KeyConflictDetect1) {
  1125. NYql::TAstParseResult res = SqlToYql("select length(key) as key, key from plato.Input;");
  1126. UNIT_ASSERT(!res.Root);
  1127. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:28: Error: Unable to use duplicate column names. Collision in name: key\n");
  1128. }
  1129. Y_UNIT_TEST(KeyConflictDetect2) {
  1130. NYql::TAstParseResult res = SqlToYql("select key, in.key from plato.Input as in;");
  1131. UNIT_ASSERT(!res.Root);
  1132. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:1: Error: Duplicate column: key\n");
  1133. }
  1134. Y_UNIT_TEST(AutogenerationAliasWithCollisionConflict1) {
  1135. NYql::TAstParseResult res = SqlToYql("select LENGTH(Value), key as column0 from plato.Input;");
  1136. UNIT_ASSERT(!res.Root);
  1137. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:1: Error: Duplicate column: column0\n");
  1138. }
  1139. Y_UNIT_TEST(AutogenerationAliasWithCollisionConflict2) {
  1140. NYql::TAstParseResult res = SqlToYql("select key as column1, LENGTH(Value) from plato.Input;");
  1141. UNIT_ASSERT(!res.Root);
  1142. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:1: Error: Duplicate column: column1\n");
  1143. }
  1144. Y_UNIT_TEST(MissedSourceTableForQualifiedAsteriskOnSimpleSelect) {
  1145. NYql::TAstParseResult res = SqlToYql("use plato; select Intop.*, Input.key from plato.Input;");
  1146. UNIT_ASSERT(!res.Root);
  1147. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:19: Error: Unknown correlation name: Intop\n");
  1148. }
  1149. Y_UNIT_TEST(MissedSourceTableForQualifiedAsteriskOnJoin) {
  1150. NYql::TAstParseResult res = SqlToYql("use plato; select tmissed.*, t2.*, t1.key from plato.Input as t1 join plato.Input as t2 on t1.key==t2.key;");
  1151. UNIT_ASSERT(!res.Root);
  1152. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:19: Error: Unknown correlation name for asterisk: tmissed\n");
  1153. }
  1154. Y_UNIT_TEST(UnableToReferenceOnNotExistSubcolumn) {
  1155. NYql::TAstParseResult res = SqlToYql("select b.subkey from (select key from plato.Input as a) as b;");
  1156. UNIT_ASSERT(!res.Root);
  1157. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:8: Error: Column subkey is not in source column set\n");
  1158. }
  1159. Y_UNIT_TEST(ConflictOnSameNameWithQualify0) {
  1160. NYql::TAstParseResult res = SqlToYql("select in.key, in.key as key from plato.Input as in;");
  1161. UNIT_ASSERT(!res.Root);
  1162. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:1: Error: Duplicate column: key\n");
  1163. }
  1164. Y_UNIT_TEST(ConflictOnSameNameWithQualify1) {
  1165. NYql::TAstParseResult res = SqlToYql("select in.key, length(key) as key from plato.Input as in;");
  1166. UNIT_ASSERT(!res.Root);
  1167. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:1: Error: Duplicate column: key\n");
  1168. }
  1169. Y_UNIT_TEST(ConflictOnSameNameWithQualify2) {
  1170. NYql::TAstParseResult res = SqlToYql("select key, in.key from plato.Input as in;");
  1171. UNIT_ASSERT(!res.Root);
  1172. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:1: Error: Duplicate column: key\n");
  1173. }
  1174. Y_UNIT_TEST(ConflictOnSameNameWithQualify3) {
  1175. NYql::TAstParseResult res = SqlToYql("select in.key, subkey as key from plato.Input as in;");
  1176. UNIT_ASSERT(!res.Root);
  1177. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:1: Error: Duplicate column: key\n");
  1178. }
  1179. Y_UNIT_TEST(SelectOrderByUnknownLabel) {
  1180. NYql::TAstParseResult res = SqlToYql("select a from plato.Input order by b");
  1181. UNIT_ASSERT(!res.Root);
  1182. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:36: Error: Column b is not in source column set. Did you mean a?\n");
  1183. }
  1184. Y_UNIT_TEST(SelectFlattenBySameColumns) {
  1185. NYql::TAstParseResult res = SqlToYql("select key from plato.Input flatten by (key, key as kk)");
  1186. UNIT_ASSERT(!res.Root);
  1187. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:46: Error: Duplicate column name found: key in FlattenBy section\n");
  1188. }
  1189. Y_UNIT_TEST(SelectFlattenBySameAliases) {
  1190. NYql::TAstParseResult res = SqlToYql("select key from plato.Input flatten by (key as kk, subkey as kk);");
  1191. UNIT_ASSERT(!res.Root);
  1192. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:52: Error: Duplicate alias found: kk in FlattenBy section\n");
  1193. }
  1194. Y_UNIT_TEST(SelectFlattenByConflictNameAndAlias0) {
  1195. NYql::TAstParseResult res = SqlToYql("select key from plato.Input flatten by (key, subkey as key);");
  1196. UNIT_ASSERT(!res.Root);
  1197. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:46: Error: Collision between alias and column name: key in FlattenBy section\n");
  1198. }
  1199. Y_UNIT_TEST(SelectFlattenByConflictNameAndAlias1) {
  1200. NYql::TAstParseResult res = SqlToYql("select key from plato.Input flatten by (key as kk, subkey as key);");
  1201. UNIT_ASSERT(!res.Root);
  1202. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:52: Error: Collision between alias and column name: key in FlattenBy section\n");
  1203. }
  1204. Y_UNIT_TEST(UseInOnStrings) {
  1205. NYql::TAstParseResult res = SqlToYql("select * from plato.Input where \"foo\" in \"foovalue\";");
  1206. UNIT_ASSERT(!res.Root);
  1207. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:42: Error: Unable to use IN predicate with string argument, it won't search substring - "
  1208. "expecting tuple, list, dict or single column table source\n");
  1209. }
  1210. Y_UNIT_TEST(UseSubqueryInScalarContextInsideIn) {
  1211. NYql::TAstParseResult res = SqlToYql("$q = (select key from plato.Input); select * from plato.Input where subkey in ($q);");
  1212. UNIT_ASSERT(res.Root);
  1213. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:79: Warning: Using subrequest in scalar context after IN, perhaps you should remove parenthesis here, code: 4501\n");
  1214. }
  1215. Y_UNIT_TEST(InHintsWithKeywordClash) {
  1216. NYql::TAstParseResult res = SqlToYql("SELECT COMPACT FROM plato.Input WHERE COMPACT IN COMPACT [COMPACT](1,2,3)");
  1217. UNIT_ASSERT(!res.Root);
  1218. // should try to parse last compact as call expression
  1219. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:58: Error: Unknown builtin: COMPACT, to use YQL functions, try YQL::COMPACT\n");
  1220. }
  1221. Y_UNIT_TEST(ErrorColumnPosition) {
  1222. NYql::TAstParseResult res = SqlToYql(
  1223. "USE plato;\n"
  1224. "SELECT \n"
  1225. "value FROM (\n"
  1226. "select key from Input\n"
  1227. ");\n"
  1228. );
  1229. UNIT_ASSERT(!res.Root);
  1230. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:3:1: Error: Column value is not in source column set\n");
  1231. }
  1232. Y_UNIT_TEST(InsertAbortMapReduce) {
  1233. NYql::TAstParseResult res = SqlToYql("INSERT OR ABORT INTO plato.Output SELECT key FROM plato.Input");
  1234. UNIT_ASSERT(!res.Root);
  1235. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:0: Error: INSERT OR ABORT INTO is not supported for yt tables\n");
  1236. }
  1237. Y_UNIT_TEST(ReplaceIntoMapReduce) {
  1238. NYql::TAstParseResult res = SqlToYql("REPLACE INTO plato.Output SELECT key FROM plato.Input");
  1239. UNIT_ASSERT(!res.Root);
  1240. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:0: Error: Meaning of REPLACE INTO has been changed, now you should use INSERT INTO <table> WITH TRUNCATE ... for yt\n");
  1241. }
  1242. Y_UNIT_TEST(UpsertIntoMapReduce) {
  1243. NYql::TAstParseResult res = SqlToYql("UPSERT INTO plato.Output SELECT key FROM plato.Input");
  1244. UNIT_ASSERT(!res.Root);
  1245. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:0: Error: UPSERT INTO is not supported for yt tables\n");
  1246. }
  1247. Y_UNIT_TEST(UpdateMapReduce) {
  1248. NYql::TAstParseResult res = SqlToYql("UPDATE plato.Output SET value = value + 1 WHERE key < 1");
  1249. UNIT_ASSERT(!res.Root);
  1250. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:0: Error: UPDATE is unsupported for yt\n");
  1251. }
  1252. Y_UNIT_TEST(DeleteMapReduce) {
  1253. NYql::TAstParseResult res = SqlToYql("DELETE FROM plato.Output WHERE key < 1");
  1254. UNIT_ASSERT(!res.Root);
  1255. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:0: Error: DELETE is unsupported for yt\n");
  1256. }
  1257. Y_UNIT_TEST(ReplaceIntoWithTruncate) {
  1258. NYql::TAstParseResult res = SqlToYql("REPLACE INTO plato.Output WITH TRUNCATE SELECT key FROM plato.Input");
  1259. UNIT_ASSERT(!res.Root);
  1260. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:32: Error: Unable REPLACE INTO with truncate mode\n");
  1261. }
  1262. Y_UNIT_TEST(UpsertIntoWithTruncate) {
  1263. NYql::TAstParseResult res = SqlToYql("UPSERT INTO plato.Output WITH TRUNCATE SELECT key FROM plato.Input");
  1264. UNIT_ASSERT(!res.Root);
  1265. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:31: Error: Unable UPSERT INTO with truncate mode\n");
  1266. }
  1267. Y_UNIT_TEST(InsertIntoWithTruncateKikimr) {
  1268. NYql::TAstParseResult res = SqlToYql("INSERT INTO plato.Output WITH TRUNCATE SELECT key FROM plato.Input", 10, TString(NYql::KikimrProviderName));
  1269. UNIT_ASSERT(!res.Root);
  1270. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:0: Error: INSERT INTO WITH TRUNCATE is not supported for kikimr tables\n");
  1271. }
  1272. Y_UNIT_TEST(InsertIntoWithWrongArgumentCount) {
  1273. NYql::TAstParseResult res = SqlToYql("insert into plato.Output with truncate (key, value, subkey) values (5, '1', '2', '3');");
  1274. UNIT_ASSERT(!res.Root);
  1275. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:53: Error: VALUES have 4 columns, INSERT INTO ... WITH TRUNCATE expects: 3\n");
  1276. }
  1277. Y_UNIT_TEST(UpsertWithWrongArgumentCount) {
  1278. NYql::TAstParseResult res = SqlToYql("upsert into plato.Output (key, value, subkey) values (2, '3');", 10, TString(NYql::KikimrProviderName));
  1279. UNIT_ASSERT(!res.Root);
  1280. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:39: Error: VALUES have 2 columns, UPSERT INTO expects: 3\n");
  1281. }
  1282. Y_UNIT_TEST(UnionNotSupported) {
  1283. NYql::TAstParseResult res = SqlToYql("SELECT key FROM plato.Input UNION select subkey FROM plato.Input;");
  1284. UNIT_ASSERT(!res.Root);
  1285. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:29: Error: UNION without quantifier ALL is not supported yet. Did you mean UNION ALL?\n");
  1286. }
  1287. Y_UNIT_TEST(GroupingSetByExprWithoutAlias) {
  1288. NYql::TAstParseResult res = SqlToYql("SELECT key FROM plato.Input GROUP BY GROUPING SETS (cast(key as uint32), subkey);");
  1289. UNIT_ASSERT(!res.Root);
  1290. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:74: Error: You should use in GROUPING SETS either expression with required alias either column name or used alias.\n");
  1291. }
  1292. Y_UNIT_TEST(CubeByExprWithoutAlias) {
  1293. NYql::TAstParseResult res = SqlToYql("SELECT key FROM plato.Input GROUP BY CUBE (key, subkey / key);");
  1294. UNIT_ASSERT(!res.Root);
  1295. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:58: Error: You should use in CUBE either expression with required alias either column name or used alias.\n");
  1296. }
  1297. Y_UNIT_TEST(RollupByExprWithoutAlias) {
  1298. NYql::TAstParseResult res = SqlToYql("SELECT key FROM plato.Input GROUP BY ROLLUP (subkey / key);");
  1299. UNIT_ASSERT(!res.Root);
  1300. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:55: Error: You should use in ROLLUP either expression with required alias either column name or used alias.\n");
  1301. }
  1302. Y_UNIT_TEST(GroupByHugeCubeDeniedNoPragma) {
  1303. NYql::TAstParseResult res = SqlToYql("SELECT key FROM plato.Input GROUP BY CUBE (key, subkey, value, key + subkey as sum, key - subkey as sub, key + val as keyval);");
  1304. UNIT_ASSERT(!res.Root);
  1305. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:119: Error: GROUP BY CUBE is allowed only for 5 columns, but you use 6\n");
  1306. }
  1307. Y_UNIT_TEST(GroupByInvalidPragma) {
  1308. NYql::TAstParseResult res = SqlToYql("PRAGMA GroupByCubeLimit = '-4';");
  1309. UNIT_ASSERT(!res.Root);
  1310. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:8: Error: Expected single unsigned integer argument for: GroupByCubeLimit\n");
  1311. }
  1312. Y_UNIT_TEST(GroupByHugeCubeDeniedPragme) {
  1313. NYql::TAstParseResult res = SqlToYql("PRAGMA GroupByCubeLimit = '4'; SELECT key FROM plato.Input GROUP BY CUBE (key, subkey, value, key + subkey as sum, key - subkey as sub);");
  1314. UNIT_ASSERT(!res.Root);
  1315. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:132: Error: GROUP BY CUBE is allowed only for 4 columns, but you use 5\n");
  1316. }
  1317. Y_UNIT_TEST(GroupByFewBigCubes) {
  1318. NYql::TAstParseResult res = SqlToYql("SELECT key FROM plato.Input GROUP BY CUBE(key, subkey, key + subkey as sum), CUBE(value, value + key + subkey as total);");
  1319. UNIT_ASSERT(!res.Root);
  1320. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:1: Error: Unable to GROUP BY more than 64 groups, you try use 80 groups\n");
  1321. }
  1322. Y_UNIT_TEST(GroupByFewBigCubesWithPragmaLimit) {
  1323. NYql::TAstParseResult res = SqlToYql("PRAGMA GroupByLimit = '16'; SELECT key FROM plato.Input GROUP BY GROUPING SETS(key, subkey, key + subkey as sum), ROLLUP(value, value + key + subkey as total);");
  1324. UNIT_ASSERT(!res.Root);
  1325. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:29: Error: Unable to GROUP BY more than 16 groups, you try use 18 groups\n");
  1326. }
  1327. Y_UNIT_TEST(NoGroupingColumn0) {
  1328. NYql::TAstParseResult res = SqlToYql(
  1329. "select count(1), key_first, val_first, grouping(key_first, val_first, nomind) as group\n"
  1330. "from plato.Input group by grouping sets (cast(key as uint32) /100 as key_first, Substring(value, 1, 1) as val_first);");
  1331. UNIT_ASSERT(!res.Root);
  1332. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:71: Error: Column 'nomind' not used as grouping column\n");
  1333. }
  1334. Y_UNIT_TEST(NoGroupingColumn1) {
  1335. NYql::TAstParseResult res = SqlToYql("select count(1), grouping(key, value) as group_duo from plato.Input group by cube (key, subkey);");
  1336. UNIT_ASSERT(!res.Root);
  1337. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:32: Error: Column 'value' not used as grouping column\n");
  1338. }
  1339. Y_UNIT_TEST(EmptyAccess0) {
  1340. NYql::TAstParseResult res = SqlToYql("insert into plato.Output (list0, list1) values (AsList(0, 1, 2), AsList([]));");
  1341. UNIT_ASSERT(!res.Root);
  1342. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:34: Error: Source does not allow column references\n");
  1343. }
  1344. Y_UNIT_TEST(EmptyAccess1) {
  1345. NYql::TAstParseResult res = SqlToYql("insert into plato.Output (list0, list1) values (AsList(0, 1, 2), []);");
  1346. UNIT_ASSERT(!res.Root);
  1347. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:34: Error: Source does not allow column references\n");
  1348. }
  1349. Y_UNIT_TEST(UseUnknownColumnInInsert) {
  1350. NYql::TAstParseResult res = SqlToYql("insert into plato.Output (list0, list1) values (AsList(0, 1, 2), AsList([test]));");
  1351. UNIT_ASSERT(!res.Root);
  1352. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:34: Error: Source does not allow column references\n");
  1353. }
  1354. Y_UNIT_TEST(GroupByEmptyColumn) {
  1355. NYql::TAstParseResult res = SqlToYql("select count(1) from plato.Input group by [];");
  1356. UNIT_ASSERT(!res.Root);
  1357. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:43: Error: Column name can not be empty\n");
  1358. }
  1359. Y_UNIT_TEST(RollbackUnsupported) {
  1360. NYql::TAstParseResult res = SqlToYql("insert into plato.Output (v0, v1) values (0, 1); rollback;");
  1361. UNIT_ASSERT(!res.Root);
  1362. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:50: Error: ROLLBACK isn't supported for provider: yt\n");
  1363. }
  1364. Y_UNIT_TEST(ShouldFailAsTruncatedBinaryNotSucceedAsLegacyTinyZeroInt) {
  1365. NYql::TAstParseResult res = SqlToYql("select 0b;");
  1366. UNIT_ASSERT(!res.Root);
  1367. UNIT_ASSERT_NO_DIFF(Err2Str(res),
  1368. "<main>:1:9: Error: Unexpected character ';' (Unicode character <59>) : missing elements...\n\n"
  1369. "<main>:1:0: Error: Unexpected token absence : cannot match to any predicted input...\n\n"
  1370. );
  1371. }
  1372. Y_UNIT_TEST(ConvertNumberFailed0) {
  1373. NYql::TAstParseResult res = SqlToYql("select 0o80l;");
  1374. UNIT_ASSERT(!res.Root);
  1375. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:8: Error: Failed to parse number from string: 0o80l, char: '8' is out of base: 8\n");
  1376. }
  1377. Y_UNIT_TEST(ConvertNumberFailed1) {
  1378. NYql::TAstParseResult res = SqlToYql("select 0xc000000000000000l;");
  1379. UNIT_ASSERT(!res.Root);
  1380. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:8: Error: Failed to convert string: 13835058055282163712 to Int64 value\n");
  1381. }
  1382. Y_UNIT_TEST(ConvertNumberFailed2) {
  1383. NYql::TAstParseResult res = SqlToYql("select 0xc0000000000000000l;");
  1384. UNIT_ASSERT(!res.Root);
  1385. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:8: Error: Failed to parse number from string: 0xc0000000000000000l, number limit overflow\n");
  1386. }
  1387. Y_UNIT_TEST(InvaildUsageReal0) {
  1388. NYql::TAstParseResult res = SqlToYql("select .0;");
  1389. UNIT_ASSERT(!res.Root);
  1390. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:7: Error: Unexpected token '.' : cannot match to any predicted input...\n\n");
  1391. }
  1392. Y_UNIT_TEST(InvaildUsageReal1) {
  1393. NYql::TAstParseResult res = SqlToYql("select .0f;");
  1394. UNIT_ASSERT(!res.Root);
  1395. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:7: Error: Unexpected token '.' : cannot match to any predicted input...\n\n");
  1396. }
  1397. Y_UNIT_TEST(InvaildUsageWinFunctionWithoutWindow) {
  1398. NYql::TAstParseResult res = SqlToYql("select lead(key, 2) from plato.Input;");
  1399. UNIT_ASSERT(!res.Root);
  1400. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:8: Error: Failed to use window function: Lead without window specification\n");
  1401. }
  1402. Y_UNIT_TEST(DropTableWithIfExists) {
  1403. NYql::TAstParseResult res = SqlToYql("DROP TABLE IF EXISTS plato.foo;");
  1404. UNIT_ASSERT(!res.Root);
  1405. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:0: Error: IF EXISTS in DROP TABLE is not supported.\n");
  1406. }
  1407. Y_UNIT_TEST(TooManyErrors) {
  1408. const char* q = R"(
  1409. USE plato;
  1410. $a1 = A;
  1411. $a2 = DateTime::ToDate($a1, $a1);
  1412. $a3 = DateTime::ToDate($a2, $a2);
  1413. $a4 = DateTime::ToDate($a3, $a3);
  1414. $s = (select b from plato.abc);
  1415. select $a4 from $s;
  1416. )";
  1417. NYql::TAstParseResult res = SqlToYql(q, 10);
  1418. UNIT_ASSERT(!res.Root);
  1419. UNIT_ASSERT_NO_DIFF(Err2Str(res),
  1420. R"(<main>:3:15: Error: Column A is not in source column set. Did you mean b?
  1421. <main>:3:15: Error: Column A is not in source column set. Did you mean b?
  1422. <main>:3:15: Error: Column A is not in source column set. Did you mean b?
  1423. <main>:3:15: Error: Column A is not in source column set. Did you mean b?
  1424. <main>:3:15: Error: Column A is not in source column set. Did you mean b?
  1425. <main>:3:15: Error: Column A is not in source column set. Did you mean b?
  1426. <main>:3:15: Error: Column A is not in source column set. Did you mean b?
  1427. <main>:3:15: Error: Column A is not in source column set. Did you mean b?
  1428. <main>:3:15: Error: Column A is not in source column set. Did you mean b?
  1429. <main>: Error: Too many issues, code: 1
  1430. )");
  1431. }
  1432. Y_UNIT_TEST(ShouldCloneBindingForNamedParameter) {
  1433. NYql::TAstParseResult res = SqlToYql(R"(
  1434. $f = () -> {
  1435. $value_type = TypeOf(1);
  1436. $pair_type = StructType(
  1437. TypeOf("2") AS key,
  1438. $value_type AS value
  1439. );
  1440. RETURN TupleType(
  1441. ListType($value_type),
  1442. $pair_type);
  1443. };
  1444. select FormatType($f());
  1445. )");
  1446. UNIT_ASSERT(res.Root);
  1447. }
  1448. Y_UNIT_TEST(WarnSourceColumnMismatch) {
  1449. NYql::TAstParseResult res = SqlToYql(
  1450. "insert into plato.Output (key, subkey, new_value, one_more_value) select key as Key, subkey, value, \"x\" from plato.Input;");
  1451. UNIT_ASSERT(res.Root);
  1452. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:51: Warning: Column names in SELECT don't match column specification in parenthesis. \"key\" doesn't match \"Key\". \"new_value\" doesn't match \"value\", code: 4517\n");
  1453. }
  1454. Y_UNIT_TEST(YtCaseInsensitive) {
  1455. NYql::TAstParseResult res = SqlToYql("select * from PlatO.foo;");
  1456. UNIT_ASSERT(res.Root);
  1457. res = SqlToYql("use PlatO; select * from foo;");
  1458. UNIT_ASSERT(res.Root);
  1459. }
  1460. Y_UNIT_TEST(KikimrCaseSensitive) {
  1461. NYql::TAstParseResult res = SqlToYql("select * from PlatO.foo;", 10, "kikimr");
  1462. UNIT_ASSERT(!res.Root);
  1463. UNIT_ASSERT_NO_DIFF(Err2Str(res), R"(<main>:1:15: Error: Unknown cluster: PlatO
  1464. <main>:1:15: Error: No cluster name given and no default cluster is selected
  1465. )");
  1466. res = SqlToYql("use PlatO; select * from foo;", 10, "kikimr");
  1467. UNIT_ASSERT(!res.Root);
  1468. UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:5: Error: Unknown cluster: PlatO\n");
  1469. }
  1470. }