parser_test.go 83 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931
  1. // Copyright 2021-2022 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package xsql
  15. import (
  16. "fmt"
  17. "github.com/lf-edge/ekuiper/internal/testx"
  18. "github.com/lf-edge/ekuiper/pkg/ast"
  19. "math"
  20. "reflect"
  21. "regexp"
  22. "strings"
  23. "testing"
  24. )
  25. // Ensure the parser can parse strings into Statement ASTs.
  26. func TestParser_ParseStatement(t *testing.T) {
  27. re1, _ := regexp.Compile("^foo$")
  28. re2, _ := regexp.Compile("^fo.o.*$")
  29. re3, _ := regexp.Compile("^foo\\\\%$")
  30. var tests = []struct {
  31. s string
  32. stmt *ast.SelectStatement
  33. err string
  34. }{
  35. {
  36. s: `SELECT name FROM tbl`,
  37. stmt: &ast.SelectStatement{
  38. Fields: []ast.Field{
  39. {
  40. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  41. Name: "name",
  42. AName: ""},
  43. },
  44. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  45. },
  46. },
  47. {
  48. s: "SELECT `select` FROM tbl",
  49. stmt: &ast.SelectStatement{
  50. Fields: []ast.Field{
  51. {
  52. Expr: &ast.FieldRef{Name: "select", StreamName: ast.DefaultStream},
  53. Name: "select",
  54. AName: ""},
  55. },
  56. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  57. },
  58. },
  59. {
  60. s: `SELECT name FROM topic/sensor1`,
  61. stmt: &ast.SelectStatement{
  62. Fields: []ast.Field{
  63. {
  64. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  65. Name: "name",
  66. AName: ""},
  67. },
  68. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  69. },
  70. },
  71. {
  72. s: `SELECT t1.name FROM topic/sensor1 AS t1`,
  73. stmt: &ast.SelectStatement{
  74. Fields: []ast.Field{
  75. {
  76. Expr: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "name"},
  77. Name: "name",
  78. AName: ""},
  79. },
  80. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
  81. },
  82. },
  83. {
  84. s: "SELECT t1.name FROM topic/sensor1 AS `join`",
  85. stmt: &ast.SelectStatement{
  86. Fields: []ast.Field{
  87. {
  88. Expr: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "name"},
  89. Name: "name",
  90. AName: ""},
  91. },
  92. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "join"}},
  93. },
  94. },
  95. {
  96. s: `SELECT name FROM topic/sensor1 AS t1`,
  97. stmt: &ast.SelectStatement{
  98. Fields: []ast.Field{
  99. {
  100. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  101. Name: "name",
  102. AName: ""},
  103. },
  104. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
  105. },
  106. },
  107. {
  108. s: `SELECT name FROM topic/sensor1/#`,
  109. stmt: &ast.SelectStatement{
  110. Fields: []ast.Field{
  111. {
  112. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  113. Name: "name",
  114. AName: ""},
  115. },
  116. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1/#"}},
  117. },
  118. },
  119. {
  120. s: `SELECT name FROM topic/sensor1/# AS t2 `,
  121. stmt: &ast.SelectStatement{
  122. Fields: []ast.Field{
  123. {
  124. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  125. Name: "name",
  126. AName: ""},
  127. },
  128. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1/#", Alias: "t2"}},
  129. },
  130. },
  131. {
  132. s: `SELECT name FROM /topic/sensor1/#`,
  133. stmt: &ast.SelectStatement{
  134. Fields: []ast.Field{
  135. {
  136. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  137. Name: "name",
  138. AName: ""},
  139. },
  140. Sources: []ast.Source{&ast.Table{Name: "/topic/sensor1/#"}},
  141. },
  142. },
  143. {
  144. s: `SELECT name FROM /topic/sensor1/#/`,
  145. stmt: &ast.SelectStatement{
  146. Fields: []ast.Field{
  147. {
  148. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  149. Name: "name",
  150. AName: ""},
  151. },
  152. Sources: []ast.Source{&ast.Table{Name: "/topic/sensor1/#/"}},
  153. },
  154. },
  155. {
  156. s: `SELECT name FROM /topic/sensor1/+/temp1/`,
  157. stmt: &ast.SelectStatement{
  158. Fields: []ast.Field{
  159. {
  160. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  161. Name: "name",
  162. AName: ""},
  163. },
  164. Sources: []ast.Source{&ast.Table{Name: "/topic/sensor1/+/temp1/"}},
  165. },
  166. },
  167. {
  168. s: `SELECT name FROM topic/sensor1/+/temp`,
  169. stmt: &ast.SelectStatement{
  170. Fields: []ast.Field{
  171. {
  172. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  173. Name: "name",
  174. AName: ""},
  175. },
  176. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1/+/temp"}},
  177. },
  178. },
  179. {
  180. s: `SELECT * FROM tbl`,
  181. stmt: &ast.SelectStatement{
  182. Fields: []ast.Field{
  183. {
  184. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  185. Name: "*",
  186. AName: ""},
  187. },
  188. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  189. },
  190. },
  191. {
  192. s: `SELECT a,b FROM tbl`,
  193. stmt: &ast.SelectStatement{
  194. Fields: []ast.Field{
  195. {Expr: &ast.FieldRef{Name: "a", StreamName: ast.DefaultStream}, Name: "a", AName: ""},
  196. {Expr: &ast.FieldRef{Name: "b", StreamName: ast.DefaultStream}, Name: "b", AName: ""},
  197. },
  198. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  199. },
  200. },
  201. {
  202. s: `SELECT a, b,c FROM tbl`,
  203. stmt: &ast.SelectStatement{
  204. Fields: []ast.Field{
  205. {Expr: &ast.FieldRef{Name: "a", StreamName: ast.DefaultStream}, Name: "a", AName: ""},
  206. {Expr: &ast.FieldRef{Name: "b", StreamName: ast.DefaultStream}, Name: "b", AName: ""},
  207. {Expr: &ast.FieldRef{Name: "c", StreamName: ast.DefaultStream}, Name: "c", AName: ""},
  208. },
  209. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  210. },
  211. },
  212. {
  213. s: `SELECT a AS alias FROM tbl`,
  214. stmt: &ast.SelectStatement{
  215. Fields: []ast.Field{{Expr: &ast.FieldRef{Name: "a", StreamName: ast.DefaultStream}, Name: "a", AName: "alias"}},
  216. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  217. },
  218. },
  219. {
  220. s: `SELECT a AS alias1, b as Alias2 FROM tbl`,
  221. stmt: &ast.SelectStatement{
  222. Fields: []ast.Field{
  223. {Expr: &ast.FieldRef{Name: "a", StreamName: ast.DefaultStream}, Name: "a", AName: "alias1"},
  224. {Expr: &ast.FieldRef{Name: "b", StreamName: ast.DefaultStream}, Name: "b", AName: "Alias2"},
  225. },
  226. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  227. },
  228. },
  229. {
  230. s: `SELECT LenGth("test") FROM tbl`,
  231. stmt: &ast.SelectStatement{
  232. Fields: []ast.Field{
  233. {
  234. AName: "",
  235. Name: "length",
  236. Expr: &ast.Call{
  237. Name: "length",
  238. Args: []ast.Expr{&ast.StringLiteral{Val: "test"}},
  239. },
  240. },
  241. },
  242. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  243. },
  244. },
  245. {
  246. s: `SELECT length(test) FROM tbl`,
  247. stmt: &ast.SelectStatement{
  248. Fields: []ast.Field{
  249. {
  250. AName: "",
  251. Name: "length",
  252. Expr: &ast.Call{
  253. Name: "length",
  254. Args: []ast.Expr{&ast.FieldRef{Name: "test", StreamName: ast.DefaultStream}},
  255. },
  256. },
  257. },
  258. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  259. },
  260. },
  261. {
  262. s: `SELECT sin(123) FROM tbl`,
  263. stmt: &ast.SelectStatement{
  264. Fields: []ast.Field{
  265. {
  266. AName: "",
  267. Name: "sin",
  268. Expr: &ast.Call{
  269. Name: "sin",
  270. Args: []ast.Expr{&ast.IntegerLiteral{Val: 123}},
  271. },
  272. },
  273. },
  274. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  275. },
  276. },
  277. {
  278. s: `SELECT lpad("abc", 123) FROM tbl`,
  279. stmt: &ast.SelectStatement{
  280. Fields: []ast.Field{
  281. {
  282. AName: "",
  283. Name: "lpad",
  284. Expr: &ast.Call{
  285. Name: "lpad",
  286. Args: []ast.Expr{&ast.StringLiteral{Val: "abc"}, &ast.IntegerLiteral{Val: 123}},
  287. },
  288. },
  289. },
  290. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  291. },
  292. },
  293. {
  294. s: `SELECT newuuid() FROM tbl`,
  295. stmt: &ast.SelectStatement{
  296. Fields: []ast.Field{
  297. {
  298. AName: "",
  299. Name: "newuuid",
  300. Expr: &ast.Call{
  301. Name: "newuuid",
  302. Args: nil,
  303. },
  304. },
  305. },
  306. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  307. },
  308. },
  309. {
  310. s: `SELECT indexof("abc", field1) FROM tbl`,
  311. stmt: &ast.SelectStatement{
  312. Fields: []ast.Field{
  313. {
  314. AName: "",
  315. Name: "indexof",
  316. Expr: &ast.Call{
  317. Name: "indexof",
  318. Args: []ast.Expr{
  319. &ast.StringLiteral{Val: "abc"},
  320. &ast.FieldRef{Name: "field1", StreamName: ast.DefaultStream},
  321. },
  322. },
  323. },
  324. },
  325. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  326. },
  327. },
  328. {
  329. s: `SELECT lpad(lower(test),1) FROM tbl`,
  330. stmt: &ast.SelectStatement{
  331. Fields: []ast.Field{
  332. {
  333. AName: "",
  334. Name: "lpad",
  335. Expr: &ast.Call{
  336. Name: "lpad",
  337. FuncId: 1,
  338. Args: []ast.Expr{
  339. &ast.Call{
  340. Name: "lower",
  341. Args: []ast.Expr{
  342. &ast.FieldRef{Name: "test", StreamName: ast.DefaultStream},
  343. },
  344. },
  345. &ast.IntegerLiteral{Val: 1},
  346. },
  347. },
  348. },
  349. },
  350. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  351. },
  352. },
  353. {
  354. s: `SELECT lpad(lower(test),1) AS field1 FROM tbl`,
  355. stmt: &ast.SelectStatement{
  356. Fields: []ast.Field{
  357. {
  358. AName: "field1",
  359. Name: "lpad",
  360. Expr: &ast.Call{
  361. Name: "lpad",
  362. FuncId: 1,
  363. Args: []ast.Expr{
  364. &ast.Call{
  365. Name: "lower",
  366. Args: []ast.Expr{
  367. &ast.FieldRef{Name: "test", StreamName: ast.DefaultStream},
  368. },
  369. },
  370. &ast.IntegerLiteral{Val: 1},
  371. },
  372. },
  373. },
  374. },
  375. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  376. },
  377. },
  378. {
  379. s: `SELECT length(lower("test")) FROM tbl`,
  380. stmt: &ast.SelectStatement{
  381. Fields: []ast.Field{
  382. {
  383. AName: "",
  384. Name: "length",
  385. Expr: &ast.Call{
  386. Name: "length",
  387. FuncId: 1,
  388. Args: []ast.Expr{
  389. &ast.Call{
  390. Name: "lower",
  391. Args: []ast.Expr{
  392. &ast.StringLiteral{Val: "test"},
  393. },
  394. },
  395. },
  396. },
  397. },
  398. },
  399. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  400. },
  401. },
  402. {
  403. s: `SELECT count(*) FROM tbl`,
  404. stmt: &ast.SelectStatement{
  405. Fields: []ast.Field{
  406. {
  407. AName: "",
  408. Name: "count",
  409. Expr: &ast.Call{
  410. Name: "count",
  411. Args: []ast.Expr{&ast.Wildcard{Token: ast.ASTERISK}},
  412. FuncType: ast.FuncTypeAgg,
  413. },
  414. },
  415. },
  416. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  417. },
  418. },
  419. {
  420. s: `SELECT count(*, f1) FROM tbl`,
  421. stmt: nil,
  422. err: `Expect 1 arguments but found 2.`,
  423. },
  424. {
  425. s: `SELECT lag() FROM tbl`,
  426. stmt: nil,
  427. err: `expect one two or three args but got 0`,
  428. },
  429. {
  430. s: `SELECT lag(a, b, "default value") FROM tbl`,
  431. stmt: nil,
  432. err: `Expect int type for parameter 2`,
  433. },
  434. {
  435. s: `SELECT lag(a, 2, 20) FROM tbl`,
  436. stmt: &ast.SelectStatement{
  437. Fields: []ast.Field{
  438. {
  439. AName: "",
  440. Name: "lag",
  441. Expr: &ast.Call{
  442. Name: "lag",
  443. Args: []ast.Expr{&ast.FieldRef{Name: "a", StreamName: ast.DefaultStream}, &ast.IntegerLiteral{Val: 2}, &ast.IntegerLiteral{Val: 20}},
  444. },
  445. },
  446. },
  447. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  448. },
  449. },
  450. {
  451. s: `SELECT deduplicate(temperature, false) FROM tbl`,
  452. stmt: &ast.SelectStatement{
  453. Fields: []ast.Field{
  454. {
  455. AName: "",
  456. Name: "deduplicate",
  457. Expr: &ast.Call{
  458. Name: "deduplicate",
  459. Args: []ast.Expr{&ast.Wildcard{Token: ast.ASTERISK}, &ast.FieldRef{Name: "temperature", StreamName: ast.DefaultStream}, &ast.BooleanLiteral{Val: false}},
  460. FuncType: ast.FuncTypeAgg,
  461. },
  462. },
  463. },
  464. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  465. },
  466. },
  467. {
  468. s: `SELECT "abc" FROM tbl`,
  469. stmt: &ast.SelectStatement{
  470. Fields: []ast.Field{{AName: "", Name: "kuiper_field_0", Expr: &ast.StringLiteral{Val: "abc"}}},
  471. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  472. },
  473. },
  474. {
  475. s: `SELECT "abc" AS field1 FROM tbl`,
  476. stmt: &ast.SelectStatement{
  477. Fields: []ast.Field{{AName: "field1", Name: "", Expr: &ast.StringLiteral{Val: "abc"}}},
  478. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  479. },
  480. },
  481. {
  482. s: `SELECT field0, "abc" AS field1, field2 FROM tbl`,
  483. stmt: &ast.SelectStatement{
  484. Fields: []ast.Field{
  485. {AName: "", Name: "field0", Expr: &ast.FieldRef{Name: "field0", StreamName: ast.DefaultStream}},
  486. {AName: "field1", Name: "", Expr: &ast.StringLiteral{Val: "abc"}},
  487. {AName: "", Name: "field2", Expr: &ast.FieldRef{Name: "field2", StreamName: ast.DefaultStream}}},
  488. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  489. },
  490. },
  491. {
  492. s: `SELECT * AS alias FROM tbl`,
  493. stmt: nil,
  494. err: `alias is not supported for *`,
  495. },
  496. {
  497. s: `SELECT *, FROM tbl`,
  498. stmt: nil,
  499. err: `found "FROM", expected expression.`,
  500. },
  501. {
  502. s: `SELECTname FROM tbl`,
  503. stmt: nil,
  504. err: `Found "SELECTname", Expected SELECT.` + "\n",
  505. },
  506. {
  507. s: `SELECT abc FROM tbl WHERE abc > 12 `,
  508. stmt: &ast.SelectStatement{
  509. Fields: []ast.Field{{AName: "", Name: "abc", Expr: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream}}},
  510. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  511. Condition: &ast.BinaryExpr{
  512. LHS: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream},
  513. OP: ast.GT,
  514. RHS: &ast.IntegerLiteral{Val: 12},
  515. },
  516. },
  517. },
  518. {
  519. s: `SELECT abc FROM tbl WHERE abc = "hello" `,
  520. stmt: &ast.SelectStatement{
  521. Fields: []ast.Field{{AName: "", Name: "abc", Expr: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream}}},
  522. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  523. Condition: &ast.BinaryExpr{
  524. LHS: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream},
  525. OP: ast.EQ,
  526. RHS: &ast.StringLiteral{Val: "hello"},
  527. },
  528. },
  529. },
  530. {
  531. s: `SELECT t1.abc FROM tbl AS t1 WHERE t1.abc = "hello" `,
  532. stmt: &ast.SelectStatement{
  533. Fields: []ast.Field{{AName: "", Name: "abc", Expr: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "abc"}}},
  534. Sources: []ast.Source{&ast.Table{Name: "tbl", Alias: "t1"}},
  535. Condition: &ast.BinaryExpr{
  536. LHS: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "abc"},
  537. OP: ast.EQ,
  538. RHS: &ast.StringLiteral{Val: "hello"},
  539. },
  540. },
  541. },
  542. {
  543. s: `SELECT abc, "fff" AS fa FROM tbl WHERE fa >= 5 `,
  544. stmt: &ast.SelectStatement{
  545. Fields: []ast.Field{{AName: "", Name: "abc", Expr: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream}}, {AName: "fa", Name: "", Expr: &ast.StringLiteral{Val: "fff"}}},
  546. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  547. Condition: &ast.BinaryExpr{
  548. LHS: &ast.FieldRef{Name: "fa", StreamName: ast.DefaultStream},
  549. OP: ast.GTE,
  550. RHS: &ast.IntegerLiteral{Val: 5},
  551. },
  552. },
  553. },
  554. {
  555. s: `SELECT field2 FROM tbl WHERE field2 != 5 `,
  556. stmt: &ast.SelectStatement{
  557. Fields: []ast.Field{{AName: "", Name: "field2", Expr: &ast.FieldRef{Name: "field2", StreamName: ast.DefaultStream}}},
  558. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  559. Condition: &ast.BinaryExpr{
  560. LHS: &ast.FieldRef{Name: "field2", StreamName: ast.DefaultStream},
  561. OP: ast.NEQ,
  562. RHS: &ast.IntegerLiteral{Val: 5},
  563. },
  564. },
  565. },
  566. {
  567. s: `SELECT field2 FROM tbl WHERE field2 ! = 5 `, //Add space char in expression
  568. stmt: &ast.SelectStatement{
  569. Fields: []ast.Field{{AName: "", Name: "field2", Expr: &ast.FieldRef{Name: "field2", StreamName: ast.DefaultStream}}},
  570. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  571. Condition: &ast.BinaryExpr{
  572. LHS: &ast.FieldRef{Name: "field2", StreamName: ast.DefaultStream},
  573. OP: ast.NEQ,
  574. RHS: &ast.IntegerLiteral{Val: 5},
  575. },
  576. },
  577. },
  578. {
  579. s: `SELECT *f FROM tbl`,
  580. stmt: nil,
  581. err: `found "f", expected FROM.`,
  582. },
  583. ////TODO
  584. //{
  585. // s: `SELECT *from FROM tbl`,
  586. // stmt: nil,
  587. // err: `found "f", expected FROM.`,
  588. //},
  589. {
  590. s: `SELECT abc+2 FROM tbl`,
  591. stmt: &ast.SelectStatement{
  592. Fields: []ast.Field{
  593. {
  594. AName: "",
  595. Name: "kuiper_field_0",
  596. Expr: &ast.BinaryExpr{
  597. LHS: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream},
  598. OP: ast.ADD,
  599. RHS: &ast.IntegerLiteral{Val: 2},
  600. },
  601. },
  602. },
  603. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  604. },
  605. },
  606. {
  607. s: `SELECT t1.abc+2 FROM tbl AS t1`,
  608. stmt: &ast.SelectStatement{
  609. Fields: []ast.Field{
  610. {
  611. AName: "",
  612. Name: "kuiper_field_0",
  613. Expr: &ast.BinaryExpr{
  614. LHS: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "abc"},
  615. OP: ast.ADD,
  616. RHS: &ast.IntegerLiteral{Val: 2},
  617. },
  618. },
  619. },
  620. Sources: []ast.Source{&ast.Table{Name: "tbl", Alias: "t1"}},
  621. },
  622. },
  623. {
  624. s: `SELECT abc + "hello" FROM tbl`,
  625. stmt: &ast.SelectStatement{
  626. Fields: []ast.Field{
  627. {
  628. AName: "",
  629. Name: "kuiper_field_0",
  630. Expr: &ast.BinaryExpr{
  631. LHS: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream},
  632. OP: ast.ADD,
  633. RHS: &ast.StringLiteral{Val: "hello"},
  634. },
  635. },
  636. },
  637. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  638. },
  639. },
  640. {
  641. s: `SELECT abc*2 + 3 FROM tbl`,
  642. stmt: &ast.SelectStatement{
  643. Fields: []ast.Field{
  644. {
  645. AName: "",
  646. Name: "kuiper_field_0",
  647. Expr: &ast.BinaryExpr{
  648. LHS: &ast.BinaryExpr{
  649. LHS: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream},
  650. OP: ast.MUL,
  651. RHS: &ast.IntegerLiteral{Val: 2},
  652. },
  653. OP: ast.ADD,
  654. RHS: &ast.IntegerLiteral{Val: 3},
  655. },
  656. },
  657. },
  658. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  659. },
  660. },
  661. {
  662. s: `SELECT ln(abc*2 + 3) FROM tbl`,
  663. stmt: &ast.SelectStatement{
  664. Fields: []ast.Field{
  665. {
  666. AName: "",
  667. Name: "ln",
  668. Expr: &ast.Call{
  669. Name: "ln",
  670. Args: []ast.Expr{
  671. &ast.BinaryExpr{
  672. LHS: &ast.BinaryExpr{
  673. LHS: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream},
  674. OP: ast.MUL,
  675. RHS: &ast.IntegerLiteral{Val: 2},
  676. },
  677. OP: ast.ADD,
  678. RHS: &ast.IntegerLiteral{Val: 3},
  679. },
  680. },
  681. },
  682. },
  683. },
  684. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  685. },
  686. },
  687. {
  688. s: `SELECT ln(t1.abc*2 + 3) FROM tbl AS t1`,
  689. stmt: &ast.SelectStatement{
  690. Fields: []ast.Field{
  691. {
  692. AName: "",
  693. Name: "ln",
  694. Expr: &ast.Call{
  695. Name: "ln",
  696. Args: []ast.Expr{
  697. &ast.BinaryExpr{
  698. LHS: &ast.BinaryExpr{
  699. LHS: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "abc"},
  700. OP: ast.MUL,
  701. RHS: &ast.IntegerLiteral{Val: 2},
  702. },
  703. OP: ast.ADD,
  704. RHS: &ast.IntegerLiteral{Val: 3},
  705. },
  706. },
  707. },
  708. },
  709. },
  710. Sources: []ast.Source{&ast.Table{Name: "tbl", Alias: "t1"}},
  711. },
  712. },
  713. {
  714. s: `SELECT lpad("param2", abc*2 + 3) FROM tbl`,
  715. stmt: &ast.SelectStatement{
  716. Fields: []ast.Field{
  717. {
  718. AName: "",
  719. Name: "lpad",
  720. Expr: &ast.Call{
  721. Name: "lpad",
  722. Args: []ast.Expr{
  723. &ast.StringLiteral{Val: "param2"},
  724. &ast.BinaryExpr{
  725. LHS: &ast.BinaryExpr{
  726. LHS: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream},
  727. OP: ast.MUL,
  728. RHS: &ast.IntegerLiteral{Val: 2},
  729. },
  730. OP: ast.ADD,
  731. RHS: &ast.IntegerLiteral{Val: 3},
  732. },
  733. },
  734. },
  735. },
  736. },
  737. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  738. },
  739. },
  740. {
  741. s: `SELECT 0.2 FROM tbl`,
  742. stmt: &ast.SelectStatement{
  743. Fields: []ast.Field{
  744. {
  745. AName: "",
  746. Name: "kuiper_field_0",
  747. Expr: &ast.NumberLiteral{Val: 0.2},
  748. },
  749. },
  750. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  751. },
  752. },
  753. {
  754. s: `SELECT .2 FROM tbl`,
  755. stmt: &ast.SelectStatement{
  756. Fields: []ast.Field{
  757. {
  758. AName: "",
  759. Name: "kuiper_field_0",
  760. Expr: &ast.NumberLiteral{Val: 0.2},
  761. },
  762. },
  763. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  764. },
  765. },
  766. {
  767. s: `SELECT sin(.2) FROM tbl`,
  768. stmt: &ast.SelectStatement{
  769. Fields: []ast.Field{
  770. {
  771. AName: "",
  772. Name: "sin",
  773. Expr: &ast.Call{
  774. Name: "sin",
  775. Args: []ast.Expr{&ast.NumberLiteral{Val: 0.2}},
  776. },
  777. },
  778. },
  779. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  780. },
  781. },
  782. {
  783. s: `SELECT power(.2, 4) FROM tbl`,
  784. stmt: &ast.SelectStatement{
  785. Fields: []ast.Field{
  786. {
  787. AName: "",
  788. Name: "power",
  789. Expr: &ast.Call{
  790. Name: "power",
  791. Args: []ast.Expr{&ast.NumberLiteral{Val: 0.2}, &ast.IntegerLiteral{Val: 4}},
  792. },
  793. },
  794. },
  795. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  796. },
  797. },
  798. {
  799. s: `SELECT power(.2, 4) AS f1 FROM tbl WHERE f1 > 2.2`,
  800. stmt: &ast.SelectStatement{
  801. Fields: []ast.Field{
  802. {
  803. AName: "f1",
  804. Name: "power",
  805. Expr: &ast.Call{
  806. Name: "power",
  807. Args: []ast.Expr{&ast.NumberLiteral{Val: 0.2}, &ast.IntegerLiteral{Val: 4}},
  808. },
  809. },
  810. },
  811. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  812. Condition: &ast.BinaryExpr{
  813. LHS: &ast.FieldRef{Name: "f1", StreamName: ast.DefaultStream},
  814. OP: ast.GT,
  815. RHS: &ast.NumberLiteral{Val: 2.2},
  816. },
  817. },
  818. },
  819. {
  820. s: `SELECT power(.2, 4) AS f1 FROM tbl WHERE f1 BETWEEN 1 AND 2`,
  821. stmt: &ast.SelectStatement{
  822. Fields: []ast.Field{
  823. {
  824. AName: "f1",
  825. Name: "power",
  826. Expr: &ast.Call{
  827. Name: "power",
  828. Args: []ast.Expr{&ast.NumberLiteral{Val: 0.2}, &ast.IntegerLiteral{Val: 4}},
  829. },
  830. },
  831. },
  832. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  833. Condition: &ast.BinaryExpr{
  834. LHS: &ast.FieldRef{Name: "f1", StreamName: ast.DefaultStream},
  835. OP: ast.BETWEEN,
  836. RHS: &ast.BetweenExpr{
  837. Lower: &ast.IntegerLiteral{Val: 1},
  838. Higher: &ast.IntegerLiteral{Val: 2},
  839. },
  840. },
  841. },
  842. },
  843. {
  844. s: `SELECT a FROM tbl WHERE f1 > 4 AND f2 BETWEEN 1 AND 2`,
  845. stmt: &ast.SelectStatement{
  846. Fields: []ast.Field{
  847. {
  848. AName: "",
  849. Name: "a",
  850. Expr: &ast.FieldRef{Name: "a", StreamName: ast.DefaultStream},
  851. },
  852. },
  853. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  854. Condition: &ast.BinaryExpr{
  855. OP: ast.AND,
  856. LHS: &ast.BinaryExpr{
  857. LHS: &ast.FieldRef{Name: "f1", StreamName: ast.DefaultStream},
  858. OP: ast.GT,
  859. RHS: &ast.IntegerLiteral{Val: 4},
  860. },
  861. RHS: &ast.BinaryExpr{
  862. LHS: &ast.FieldRef{Name: "f2", StreamName: ast.DefaultStream},
  863. OP: ast.BETWEEN,
  864. RHS: &ast.BetweenExpr{
  865. Lower: &ast.IntegerLiteral{Val: 1},
  866. Higher: &ast.IntegerLiteral{Val: 2},
  867. },
  868. },
  869. },
  870. },
  871. },
  872. {
  873. s: `SELECT a FROM tbl WHERE f1 NOT BETWEEN b AND c AND f2 BETWEEN 1 AND 2 AND f3 > 4`,
  874. stmt: &ast.SelectStatement{
  875. Fields: []ast.Field{
  876. {
  877. AName: "",
  878. Name: "a",
  879. Expr: &ast.FieldRef{Name: "a", StreamName: ast.DefaultStream},
  880. },
  881. },
  882. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  883. Condition: &ast.BinaryExpr{
  884. OP: ast.AND,
  885. LHS: &ast.BinaryExpr{
  886. OP: ast.AND,
  887. LHS: &ast.BinaryExpr{
  888. LHS: &ast.FieldRef{Name: "f1", StreamName: ast.DefaultStream},
  889. OP: ast.NOTBETWEEN,
  890. RHS: &ast.BetweenExpr{
  891. Lower: &ast.FieldRef{Name: "b", StreamName: ast.DefaultStream},
  892. Higher: &ast.FieldRef{Name: "c", StreamName: ast.DefaultStream},
  893. },
  894. },
  895. RHS: &ast.BinaryExpr{
  896. LHS: &ast.FieldRef{Name: "f2", StreamName: ast.DefaultStream},
  897. OP: ast.BETWEEN,
  898. RHS: &ast.BetweenExpr{
  899. Lower: &ast.IntegerLiteral{Val: 1},
  900. Higher: &ast.IntegerLiteral{Val: 2},
  901. },
  902. },
  903. },
  904. RHS: &ast.BinaryExpr{
  905. OP: ast.GT,
  906. LHS: &ast.FieldRef{Name: "f3", StreamName: ast.DefaultStream},
  907. RHS: &ast.IntegerLiteral{Val: 4},
  908. },
  909. },
  910. },
  911. },
  912. {
  913. s: `SELECT a FROM tbl WHERE f1 NOT BETWEEN b`,
  914. err: "expect AND expression after between but found EOF",
  915. },
  916. {
  917. s: `SELECT a FROM tbl WHERE f1 NOT BETWEEN 1 OR 2`,
  918. err: "expect AND expression after between but found OR",
  919. },
  920. {
  921. s: `SELECT a FROM tbl WHERE a LIKE "foo"`,
  922. stmt: &ast.SelectStatement{
  923. Fields: []ast.Field{
  924. {
  925. AName: "",
  926. Name: "a",
  927. Expr: &ast.FieldRef{Name: "a", StreamName: ast.DefaultStream},
  928. },
  929. },
  930. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  931. Condition: &ast.BinaryExpr{
  932. LHS: &ast.FieldRef{Name: "a", StreamName: ast.DefaultStream},
  933. OP: ast.LIKE,
  934. RHS: &ast.LikePattern{Expr: &ast.StringLiteral{Val: "foo"}, Pattern: re1},
  935. },
  936. },
  937. },
  938. {
  939. s: `SELECT a FROM tbl WHERE a NOT LIKE "fo_o%"`,
  940. stmt: &ast.SelectStatement{
  941. Fields: []ast.Field{
  942. {
  943. AName: "",
  944. Name: "a",
  945. Expr: &ast.FieldRef{Name: "a", StreamName: ast.DefaultStream},
  946. },
  947. },
  948. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  949. Condition: &ast.BinaryExpr{
  950. LHS: &ast.FieldRef{Name: "a", StreamName: ast.DefaultStream},
  951. OP: ast.NOTLIKE,
  952. RHS: &ast.LikePattern{Expr: &ast.StringLiteral{Val: "fo_o%"}, Pattern: re2},
  953. },
  954. },
  955. },
  956. {
  957. s: `SELECT a FROM tbl WHERE a LIKE "foo\\%"`,
  958. stmt: &ast.SelectStatement{
  959. Fields: []ast.Field{
  960. {
  961. AName: "",
  962. Name: "a",
  963. Expr: &ast.FieldRef{Name: "a", StreamName: ast.DefaultStream},
  964. },
  965. },
  966. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  967. Condition: &ast.BinaryExpr{
  968. LHS: &ast.FieldRef{Name: "a", StreamName: ast.DefaultStream},
  969. OP: ast.LIKE,
  970. RHS: &ast.LikePattern{Expr: &ast.StringLiteral{Val: "foo\\%"}, Pattern: re3},
  971. },
  972. },
  973. },
  974. {
  975. s: `SELECT deviceId, name FROM topic/sensor1 WHERE deviceId=1 AND name = "dname"`,
  976. stmt: &ast.SelectStatement{
  977. Fields: []ast.Field{
  978. {Expr: &ast.FieldRef{Name: "deviceId", StreamName: ast.DefaultStream}, Name: "deviceId", AName: ""},
  979. {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
  980. },
  981. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  982. Condition: &ast.BinaryExpr{
  983. LHS: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "deviceId", StreamName: ast.DefaultStream}, OP: ast.EQ, RHS: &ast.IntegerLiteral{Val: 1}},
  984. OP: ast.AND,
  985. RHS: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, OP: ast.EQ, RHS: &ast.StringLiteral{Val: "dname"}},
  986. },
  987. },
  988. },
  989. {
  990. s: `SELECT deviceId, name FROM topic/sensor1 AS t1 WHERE t1.deviceId=1 AND t1.name = "dname"`,
  991. stmt: &ast.SelectStatement{
  992. Fields: []ast.Field{
  993. {Expr: &ast.FieldRef{Name: "deviceId", StreamName: ast.DefaultStream}, Name: "deviceId", AName: ""},
  994. {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
  995. },
  996. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
  997. Condition: &ast.BinaryExpr{
  998. LHS: &ast.BinaryExpr{LHS: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "deviceId"}, OP: ast.EQ, RHS: &ast.IntegerLiteral{Val: 1}},
  999. OP: ast.AND,
  1000. RHS: &ast.BinaryExpr{LHS: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "name"}, OP: ast.EQ, RHS: &ast.StringLiteral{Val: "dname"}},
  1001. },
  1002. },
  1003. },
  1004. {
  1005. s: `SELECT temp AS t, name FROM topic/sensor1 WHERE t> = 20.5 OR name = "dname"`,
  1006. stmt: &ast.SelectStatement{
  1007. Fields: []ast.Field{
  1008. {Expr: &ast.FieldRef{Name: "temp", StreamName: ast.DefaultStream}, Name: "temp", AName: "t"},
  1009. {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
  1010. },
  1011. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  1012. Condition: &ast.BinaryExpr{
  1013. LHS: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "t", StreamName: ast.DefaultStream}, OP: ast.GTE, RHS: &ast.NumberLiteral{Val: 20.5}},
  1014. OP: ast.OR,
  1015. RHS: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, OP: ast.EQ, RHS: &ast.StringLiteral{Val: "dname"}},
  1016. },
  1017. },
  1018. },
  1019. {
  1020. s: `SELECT temp AS t, name FROM topic/sensor1 WHERE t IN arraySet OR name IN arraySet`,
  1021. stmt: &ast.SelectStatement{
  1022. Fields: []ast.Field{
  1023. {Expr: &ast.FieldRef{Name: "temp", StreamName: ast.DefaultStream}, Name: "temp", AName: "t"},
  1024. {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
  1025. },
  1026. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  1027. Condition: &ast.BinaryExpr{
  1028. LHS: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "t", StreamName: ast.DefaultStream}, OP: ast.IN, RHS: &ast.FieldRef{Name: "arraySet", StreamName: ast.DefaultStream}},
  1029. OP: ast.OR,
  1030. RHS: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, OP: ast.IN, RHS: &ast.FieldRef{Name: "arraySet", StreamName: ast.DefaultStream}},
  1031. },
  1032. },
  1033. },
  1034. {
  1035. s: `SELECT temp AS t, name FROM topic/sensor1 WHERE t NOT IN arraySet OR name NOT IN arraySet`,
  1036. stmt: &ast.SelectStatement{
  1037. Fields: []ast.Field{
  1038. {Expr: &ast.FieldRef{Name: "temp", StreamName: ast.DefaultStream}, Name: "temp", AName: "t"},
  1039. {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
  1040. },
  1041. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  1042. Condition: &ast.BinaryExpr{
  1043. LHS: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "t", StreamName: ast.DefaultStream}, OP: ast.NOTIN, RHS: &ast.FieldRef{Name: "arraySet", StreamName: ast.DefaultStream}},
  1044. OP: ast.OR,
  1045. RHS: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, OP: ast.NOTIN, RHS: &ast.FieldRef{Name: "arraySet", StreamName: ast.DefaultStream}},
  1046. },
  1047. },
  1048. },
  1049. {
  1050. s: `SELECT temp AS t, name FROM topic/sensor1 WHERE t IN (20.5, 20.4) OR name IN ("dname", "ename")`,
  1051. stmt: &ast.SelectStatement{
  1052. Fields: []ast.Field{
  1053. {Expr: &ast.FieldRef{Name: "temp", StreamName: ast.DefaultStream}, Name: "temp", AName: "t"},
  1054. {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
  1055. },
  1056. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  1057. Condition: &ast.BinaryExpr{
  1058. LHS: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "t", StreamName: ast.DefaultStream}, OP: ast.IN, RHS: &ast.ValueSetExpr{LiteralExprs: []ast.Expr{&ast.NumberLiteral{Val: 20.5}, &ast.NumberLiteral{Val: 20.4}}}},
  1059. OP: ast.OR,
  1060. RHS: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, OP: ast.IN, RHS: &ast.ValueSetExpr{LiteralExprs: []ast.Expr{&ast.StringLiteral{Val: "dname"}, &ast.StringLiteral{Val: "ename"}}}},
  1061. },
  1062. },
  1063. },
  1064. {
  1065. s: `SELECT temp AS t, name FROM topic/sensor1 WHERE t NOT IN (20.5, 20.4) OR name IN ("dname", "ename")`,
  1066. stmt: &ast.SelectStatement{
  1067. Fields: []ast.Field{
  1068. {Expr: &ast.FieldRef{Name: "temp", StreamName: ast.DefaultStream}, Name: "temp", AName: "t"},
  1069. {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
  1070. },
  1071. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  1072. Condition: &ast.BinaryExpr{
  1073. LHS: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "t", StreamName: ast.DefaultStream}, OP: ast.NOTIN, RHS: &ast.ValueSetExpr{LiteralExprs: []ast.Expr{&ast.NumberLiteral{Val: 20.5}, &ast.NumberLiteral{Val: 20.4}}}},
  1074. OP: ast.OR,
  1075. RHS: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, OP: ast.IN, RHS: &ast.ValueSetExpr{LiteralExprs: []ast.Expr{&ast.StringLiteral{Val: "dname"}, &ast.StringLiteral{Val: "ename"}}}},
  1076. },
  1077. },
  1078. },
  1079. {
  1080. s: `SELECT temp AS t, name FROM topic/sensor1 WHERE name = "dname" GROUP BY name`,
  1081. stmt: &ast.SelectStatement{
  1082. Fields: []ast.Field{
  1083. {Expr: &ast.FieldRef{Name: "temp", StreamName: ast.DefaultStream}, Name: "temp", AName: "t"},
  1084. {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
  1085. },
  1086. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  1087. Condition: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, OP: ast.EQ, RHS: &ast.StringLiteral{Val: "dname"}},
  1088. Dimensions: ast.Dimensions{ast.Dimension{Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}}},
  1089. },
  1090. },
  1091. {
  1092. s: `SELECT temp AS t, name FROM topic/sensor1 WHERE name = "dname" GROUP BY name HAVING count(name) > 3`,
  1093. stmt: &ast.SelectStatement{
  1094. Fields: []ast.Field{
  1095. {Expr: &ast.FieldRef{Name: "temp", StreamName: ast.DefaultStream}, Name: "temp", AName: "t"},
  1096. {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
  1097. },
  1098. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  1099. Condition: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, OP: ast.EQ, RHS: &ast.StringLiteral{Val: "dname"}},
  1100. Dimensions: ast.Dimensions{ast.Dimension{Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}}},
  1101. Having: &ast.BinaryExpr{LHS: &ast.Call{Name: "count", Args: []ast.Expr{&ast.FieldRef{StreamName: ast.DefaultStream, Name: "name"}}, FuncType: ast.FuncTypeAgg}, OP: ast.GT, RHS: &ast.IntegerLiteral{Val: 3}},
  1102. },
  1103. },
  1104. {
  1105. s: `SELECT temp AS t, name FROM topic/sensor1 WHERE name = "dname" HAVING count(name) > 3`,
  1106. stmt: &ast.SelectStatement{
  1107. Fields: []ast.Field{
  1108. {Expr: &ast.FieldRef{Name: "temp", StreamName: ast.DefaultStream}, Name: "temp", AName: "t"},
  1109. {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
  1110. },
  1111. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  1112. Condition: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, OP: ast.EQ, RHS: &ast.StringLiteral{Val: "dname"}},
  1113. Having: &ast.BinaryExpr{LHS: &ast.Call{Name: "count", Args: []ast.Expr{&ast.FieldRef{StreamName: ast.DefaultStream, Name: "name"}}, FuncType: ast.FuncTypeAgg}, OP: ast.GT, RHS: &ast.IntegerLiteral{Val: 3}},
  1114. },
  1115. },
  1116. {
  1117. s: `SELECT id,AVG(data) FROM t GROUP BY SUM(data)>10`,
  1118. stmt: nil,
  1119. err: "Not allowed to call aggregate functions in GROUP BY clause.",
  1120. },
  1121. {
  1122. s: `SELECT temp AS t, name FROM topic/sensor1 WHERE count(name) = 3`,
  1123. stmt: nil,
  1124. err: "Not allowed to call aggregate functions in WHERE clause.",
  1125. },
  1126. {
  1127. s: `SELECT s1.temp AS t, name FROM topic/sensor1 AS s1 WHERE t = "dname" GROUP BY s1.temp`,
  1128. stmt: &ast.SelectStatement{
  1129. Fields: []ast.Field{
  1130. {Expr: &ast.FieldRef{StreamName: "s1", Name: "temp"}, Name: "temp", AName: "t"},
  1131. {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
  1132. },
  1133. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "s1"}},
  1134. Condition: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "t", StreamName: ast.DefaultStream}, OP: ast.EQ, RHS: &ast.StringLiteral{Val: "dname"}},
  1135. Dimensions: ast.Dimensions{ast.Dimension{Expr: &ast.FieldRef{StreamName: "s1", Name: "temp"}}},
  1136. },
  1137. },
  1138. {
  1139. s: `SELECT temp AS t, name FROM topic/sensor1 WHERE name = "dname" GROUP BY lpad(name,1)`,
  1140. stmt: &ast.SelectStatement{
  1141. Fields: []ast.Field{
  1142. {Expr: &ast.FieldRef{Name: "temp", StreamName: ast.DefaultStream}, Name: "temp", AName: "t"},
  1143. {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
  1144. },
  1145. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  1146. Condition: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, OP: ast.EQ, RHS: &ast.StringLiteral{Val: "dname"}},
  1147. Dimensions: ast.Dimensions{ast.Dimension{
  1148. Expr: &ast.Call{Name: "lpad", Args: []ast.Expr{&ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, &ast.IntegerLiteral{Val: 1}}},
  1149. },
  1150. },
  1151. },
  1152. },
  1153. {
  1154. s: `SELECT temp AS t, name FROM topic/sensor1 AS s1 WHERE name = "dname" GROUP BY lpad(s1.name,1)`,
  1155. stmt: &ast.SelectStatement{
  1156. Fields: []ast.Field{
  1157. {Expr: &ast.FieldRef{Name: "temp", StreamName: ast.DefaultStream}, Name: "temp", AName: "t"},
  1158. {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
  1159. },
  1160. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "s1"}},
  1161. Condition: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, OP: ast.EQ, RHS: &ast.StringLiteral{Val: "dname"}},
  1162. Dimensions: ast.Dimensions{ast.Dimension{
  1163. Expr: &ast.Call{Name: "lpad", Args: []ast.Expr{&ast.FieldRef{StreamName: ast.StreamName("s1"), Name: "name"}, &ast.IntegerLiteral{Val: 1}}},
  1164. },
  1165. },
  1166. },
  1167. },
  1168. {
  1169. s: `SELECT temp AS t, name FROM topic/sensor1 WHERE name = "dname" GROUP BY lpad(name,1) ORDER BY name`,
  1170. stmt: &ast.SelectStatement{
  1171. Fields: []ast.Field{
  1172. {Expr: &ast.FieldRef{Name: "temp", StreamName: ast.DefaultStream}, Name: "temp", AName: "t"},
  1173. {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
  1174. },
  1175. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  1176. Condition: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, OP: ast.EQ, RHS: &ast.StringLiteral{Val: "dname"}},
  1177. Dimensions: ast.Dimensions{
  1178. ast.Dimension{
  1179. Expr: &ast.Call{Name: "lpad", Args: []ast.Expr{
  1180. &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  1181. &ast.IntegerLiteral{Val: 1}},
  1182. },
  1183. },
  1184. },
  1185. SortFields: []ast.SortField{{Uname: "name", Name: "name", Ascending: true, FieldExpr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}}},
  1186. },
  1187. },
  1188. {
  1189. s: `SELECT temp AS t, name FROM topic/sensor1 AS s1 WHERE s1.name = "dname" GROUP BY lpad(s1.name,1) ORDER BY s1.name`,
  1190. stmt: &ast.SelectStatement{
  1191. Fields: []ast.Field{
  1192. {Expr: &ast.FieldRef{Name: "temp", StreamName: ast.DefaultStream}, Name: "temp", AName: "t"},
  1193. {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
  1194. },
  1195. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "s1"}},
  1196. Condition: &ast.BinaryExpr{LHS: &ast.FieldRef{StreamName: ast.StreamName("s1"), Name: "name"}, OP: ast.EQ, RHS: &ast.StringLiteral{Val: "dname"}},
  1197. Dimensions: ast.Dimensions{
  1198. ast.Dimension{
  1199. Expr: &ast.Call{Name: "lpad", Args: []ast.Expr{
  1200. &ast.FieldRef{StreamName: ast.StreamName("s1"), Name: "name"},
  1201. &ast.IntegerLiteral{Val: 1}},
  1202. },
  1203. },
  1204. },
  1205. SortFields: []ast.SortField{{Uname: "s1\007name", Name: "name", StreamName: ast.StreamName("s1"), Ascending: true, FieldExpr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}}},
  1206. },
  1207. },
  1208. {
  1209. s: `SELECT temp AS t, name FROM topic/sensor1 WHERE name = "dname" GROUP BY lpad(name,1) ORDER BY name DESC`,
  1210. stmt: &ast.SelectStatement{
  1211. Fields: []ast.Field{
  1212. {Expr: &ast.FieldRef{Name: "temp", StreamName: ast.DefaultStream}, Name: "temp", AName: "t"},
  1213. {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
  1214. },
  1215. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  1216. Condition: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, OP: ast.EQ, RHS: &ast.StringLiteral{Val: "dname"}},
  1217. Dimensions: ast.Dimensions{
  1218. ast.Dimension{
  1219. Expr: &ast.Call{Name: "lpad", Args: []ast.Expr{
  1220. &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  1221. &ast.IntegerLiteral{Val: 1}},
  1222. },
  1223. },
  1224. },
  1225. SortFields: []ast.SortField{{Uname: "name", Name: "name", Ascending: false, FieldExpr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}}},
  1226. },
  1227. },
  1228. {
  1229. s: `SELECT * FROM topic/sensor1 ORDER BY name DESC`,
  1230. stmt: &ast.SelectStatement{
  1231. Fields: []ast.Field{
  1232. {
  1233. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  1234. Name: "*",
  1235. AName: ""},
  1236. },
  1237. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  1238. SortFields: []ast.SortField{{Uname: "name", Name: "name", Ascending: false, FieldExpr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}}},
  1239. },
  1240. },
  1241. {
  1242. s: `SELECT * FROM topic/sensor1 ORDER BY name DESC, name2 ASC`,
  1243. stmt: &ast.SelectStatement{
  1244. Fields: []ast.Field{
  1245. {
  1246. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  1247. Name: "*",
  1248. AName: ""},
  1249. },
  1250. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  1251. SortFields: []ast.SortField{{Uname: "name", Name: "name", Ascending: false, FieldExpr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}}, {Uname: "name2", Name: "name2", Ascending: true, FieldExpr: &ast.FieldRef{Name: "name2", StreamName: ast.DefaultStream}}},
  1252. },
  1253. },
  1254. {
  1255. s: `SELECT * FROM topic/sensor1 GROUP BY name, name2,power(name3,1.8) ORDER BY name DESC, name2 ASC`,
  1256. stmt: &ast.SelectStatement{
  1257. Fields: []ast.Field{
  1258. {
  1259. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  1260. Name: "*",
  1261. AName: ""},
  1262. },
  1263. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  1264. Dimensions: ast.Dimensions{
  1265. ast.Dimension{Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}},
  1266. ast.Dimension{Expr: &ast.FieldRef{Name: "name2", StreamName: ast.DefaultStream}},
  1267. ast.Dimension{
  1268. Expr: &ast.Call{Name: "power", Args: []ast.Expr{
  1269. &ast.FieldRef{Name: "name3", StreamName: ast.DefaultStream},
  1270. &ast.NumberLiteral{Val: 1.8}},
  1271. },
  1272. },
  1273. },
  1274. SortFields: []ast.SortField{{Uname: "name", Name: "name", Ascending: false, FieldExpr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}}, {Uname: "name2", Name: "name2", Ascending: true, FieldExpr: &ast.FieldRef{Name: "name2", StreamName: ast.DefaultStream}}},
  1275. },
  1276. },
  1277. //{
  1278. // s: `SELECT .2sd FROM tbl`,
  1279. // stmt: &SelectStatement{
  1280. // Fields: []Field{
  1281. // Field{
  1282. // AName:"",
  1283. // Expr: &NumberLiteral{Val: 0.2},
  1284. // },
  1285. // },
  1286. // TableName: "tbl",
  1287. // },
  1288. //},
  1289. {
  1290. s: `SELECT name FROM tbl/*SELECT comment FROM testComments*/`,
  1291. stmt: &ast.SelectStatement{
  1292. Fields: []ast.Field{
  1293. {
  1294. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  1295. Name: "name",
  1296. AName: ""},
  1297. },
  1298. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1299. },
  1300. },
  1301. {
  1302. s: `/*SELECT comment FROM testComments*/SELECT name FROM tbl`,
  1303. stmt: &ast.SelectStatement{
  1304. Fields: []ast.Field{
  1305. {
  1306. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  1307. Name: "name",
  1308. AName: ""},
  1309. },
  1310. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1311. },
  1312. },
  1313. {
  1314. s: `SELECT name /*SELECT comment FROM testComments*/ FROM tbl`,
  1315. stmt: &ast.SelectStatement{
  1316. Fields: []ast.Field{
  1317. {
  1318. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  1319. Name: "name",
  1320. AName: ""},
  1321. },
  1322. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1323. },
  1324. },
  1325. {
  1326. s: `SELECT true AS f1, FALSE as f2 FROM tbl`,
  1327. stmt: &ast.SelectStatement{
  1328. Fields: []ast.Field{
  1329. {AName: "f1", Name: "", Expr: &ast.BooleanLiteral{Val: true}},
  1330. {AName: "f2", Name: "", Expr: &ast.BooleanLiteral{Val: false}},
  1331. },
  1332. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1333. },
  1334. },
  1335. {
  1336. s: `SELECT true AS f1 FROM tbl WHERE f2 = true`,
  1337. stmt: &ast.SelectStatement{
  1338. Fields: []ast.Field{
  1339. {AName: "f1", Name: "", Expr: &ast.BooleanLiteral{Val: true}},
  1340. },
  1341. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1342. Condition: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "f2", StreamName: ast.DefaultStream}, OP: ast.EQ, RHS: &ast.BooleanLiteral{Val: true}},
  1343. },
  1344. },
  1345. {
  1346. s: `SELECT indexof(field1, "abc") FROM tbl`,
  1347. stmt: &ast.SelectStatement{
  1348. Fields: []ast.Field{
  1349. {
  1350. AName: "",
  1351. Name: "indexof",
  1352. Expr: &ast.Call{
  1353. Name: "indexof",
  1354. Args: []ast.Expr{&ast.FieldRef{Name: "field1", StreamName: ast.DefaultStream}, &ast.StringLiteral{Val: "abc"}},
  1355. },
  1356. },
  1357. },
  1358. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1359. },
  1360. },
  1361. //The negative value expression support.
  1362. {
  1363. s: `SELECT -3 AS t1 FROM tbl`,
  1364. stmt: &ast.SelectStatement{
  1365. Fields: []ast.Field{
  1366. {
  1367. Expr: &ast.IntegerLiteral{Val: -3},
  1368. Name: "",
  1369. AName: "t1"},
  1370. },
  1371. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1372. },
  1373. },
  1374. {
  1375. s: `SELECT - 3 AS t1 FROM tbl`,
  1376. stmt: &ast.SelectStatement{
  1377. Fields: []ast.Field{
  1378. {
  1379. Expr: &ast.IntegerLiteral{Val: -3},
  1380. Name: "",
  1381. AName: "t1"},
  1382. },
  1383. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1384. },
  1385. },
  1386. {
  1387. s: `SELECT -. 3 AS t1 FROM tbl`,
  1388. stmt: &ast.SelectStatement{
  1389. Fields: []ast.Field{
  1390. {
  1391. Expr: &ast.NumberLiteral{Val: -.3},
  1392. Name: "",
  1393. AName: "t1"},
  1394. },
  1395. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1396. },
  1397. },
  1398. {
  1399. s: `SELECT -3.3 AS t1 FROM tbl`,
  1400. stmt: &ast.SelectStatement{
  1401. Fields: []ast.Field{
  1402. {
  1403. Expr: &ast.NumberLiteral{Val: -3.3},
  1404. Name: "",
  1405. AName: "t1"},
  1406. },
  1407. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1408. },
  1409. },
  1410. {
  1411. s: `SELECT sample(-.3,) FROM tbl`,
  1412. stmt: nil,
  1413. err: "function sample not found",
  1414. },
  1415. {
  1416. s: `select timestamp() as tp from demo`,
  1417. stmt: nil,
  1418. err: "function timestamp not found",
  1419. },
  1420. {
  1421. s: `select tstamp() as tp from demo`,
  1422. stmt: &ast.SelectStatement{
  1423. Fields: []ast.Field{
  1424. {
  1425. Expr: &ast.Call{
  1426. Name: "tstamp",
  1427. Args: nil,
  1428. },
  1429. Name: "tstamp",
  1430. AName: "tp"},
  1431. },
  1432. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  1433. },
  1434. err: "",
  1435. },
  1436. {
  1437. s: "SELECT `half FROM tb",
  1438. stmt: nil,
  1439. err: `found "EOF", expected FROM.`,
  1440. },
  1441. {
  1442. s: "SELECT `space var` FROM tbl",
  1443. stmt: &ast.SelectStatement{
  1444. Fields: []ast.Field{
  1445. {
  1446. Expr: &ast.FieldRef{Name: "space var", StreamName: ast.DefaultStream},
  1447. Name: "space var",
  1448. AName: ""},
  1449. },
  1450. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1451. },
  1452. },
  1453. {
  1454. s: "SELECT `中文 Chinese` FROM tbl",
  1455. stmt: &ast.SelectStatement{
  1456. Fields: []ast.Field{
  1457. {
  1458. Expr: &ast.FieldRef{Name: "中文 Chinese", StreamName: ast.DefaultStream},
  1459. Name: "中文 Chinese",
  1460. AName: ""},
  1461. },
  1462. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1463. },
  1464. }, {
  1465. s: "SELECT CASE temperature WHEN 25 THEN \"bingo\" WHEN 30 THEN \"high\" ELSE \"low\" END as label, humidity FROM tbl",
  1466. stmt: &ast.SelectStatement{
  1467. Fields: []ast.Field{
  1468. {
  1469. Expr: &ast.CaseExpr{
  1470. Value: &ast.FieldRef{Name: "temperature", StreamName: ast.DefaultStream},
  1471. WhenClauses: []*ast.WhenClause{
  1472. {
  1473. Expr: &ast.IntegerLiteral{Val: 25},
  1474. Result: &ast.StringLiteral{Val: "bingo"},
  1475. }, {
  1476. Expr: &ast.IntegerLiteral{Val: 30},
  1477. Result: &ast.StringLiteral{Val: "high"},
  1478. },
  1479. },
  1480. ElseClause: &ast.StringLiteral{Val: "low"},
  1481. },
  1482. Name: "",
  1483. AName: "label",
  1484. }, {
  1485. Expr: &ast.FieldRef{Name: "humidity", StreamName: ast.DefaultStream},
  1486. Name: "humidity",
  1487. AName: "",
  1488. },
  1489. },
  1490. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1491. },
  1492. }, {
  1493. s: "SELECT CASE temperature WHEN 25 THEN \"bingo\" WHEN 30 THEN \"high\" END as label, humidity FROM tbl",
  1494. stmt: &ast.SelectStatement{
  1495. Fields: []ast.Field{
  1496. {
  1497. Expr: &ast.CaseExpr{
  1498. Value: &ast.FieldRef{Name: "temperature", StreamName: ast.DefaultStream},
  1499. WhenClauses: []*ast.WhenClause{
  1500. {
  1501. Expr: &ast.IntegerLiteral{Val: 25},
  1502. Result: &ast.StringLiteral{Val: "bingo"},
  1503. }, {
  1504. Expr: &ast.IntegerLiteral{Val: 30},
  1505. Result: &ast.StringLiteral{Val: "high"},
  1506. },
  1507. },
  1508. ElseClause: nil,
  1509. },
  1510. Name: "",
  1511. AName: "label",
  1512. }, {
  1513. Expr: &ast.FieldRef{Name: "humidity", StreamName: ast.DefaultStream},
  1514. Name: "humidity",
  1515. AName: "",
  1516. },
  1517. },
  1518. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1519. },
  1520. }, {
  1521. s: "SELECT CASE temperature ELSE \"low\" END as label, humidity FROM tbl",
  1522. stmt: nil,
  1523. err: "invalid CASE expression, WHEN expected before ELSE",
  1524. }, {
  1525. s: "SELECT CASE WHEN temperature > 30 THEN \"high\" ELSE \"low\" END as label, humidity FROM tbl",
  1526. stmt: &ast.SelectStatement{
  1527. Fields: []ast.Field{
  1528. {
  1529. Expr: &ast.CaseExpr{
  1530. Value: nil,
  1531. WhenClauses: []*ast.WhenClause{
  1532. {
  1533. Expr: &ast.BinaryExpr{
  1534. OP: ast.GT,
  1535. LHS: &ast.FieldRef{Name: "temperature", StreamName: ast.DefaultStream},
  1536. RHS: &ast.IntegerLiteral{Val: 30},
  1537. },
  1538. Result: &ast.StringLiteral{Val: "high"},
  1539. },
  1540. },
  1541. ElseClause: &ast.StringLiteral{Val: "low"},
  1542. },
  1543. Name: "",
  1544. AName: "label",
  1545. }, {
  1546. Expr: &ast.FieldRef{Name: "humidity", StreamName: ast.DefaultStream},
  1547. Name: "humidity",
  1548. AName: "",
  1549. },
  1550. },
  1551. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1552. },
  1553. }, {
  1554. s: "SELECT CASE WHEN 30 THEN \"high\" ELSE \"low\" END as label, humidity FROM tbl",
  1555. stmt: nil,
  1556. err: "invalid CASE expression, WHEN expression must be a bool condition",
  1557. }, {
  1558. s: `SELECT count(*)-10 FROM demo`,
  1559. stmt: &ast.SelectStatement{
  1560. Fields: []ast.Field{
  1561. {
  1562. AName: "",
  1563. Name: "kuiper_field_0",
  1564. Expr: &ast.BinaryExpr{
  1565. OP: ast.SUB,
  1566. LHS: &ast.Call{
  1567. Name: "count",
  1568. Args: []ast.Expr{
  1569. &ast.Wildcard{Token: ast.ASTERISK},
  1570. },
  1571. FuncType: ast.FuncTypeAgg,
  1572. },
  1573. RHS: &ast.IntegerLiteral{Val: 10},
  1574. },
  1575. },
  1576. },
  1577. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  1578. },
  1579. },
  1580. {
  1581. s: `SELECT -abc FROM demo`,
  1582. stmt: nil,
  1583. err: "found \"-\", expected expression.",
  1584. },
  1585. {
  1586. s: `SELECT meta(*) FROM tbl`,
  1587. stmt: &ast.SelectStatement{
  1588. Fields: []ast.Field{
  1589. {
  1590. AName: "",
  1591. Name: "meta",
  1592. Expr: &ast.Call{
  1593. Name: "meta",
  1594. Args: []ast.Expr{
  1595. &ast.MetaRef{
  1596. Name: "*",
  1597. StreamName: ast.DefaultStream,
  1598. },
  1599. },
  1600. },
  1601. },
  1602. },
  1603. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1604. },
  1605. }, {
  1606. s: `SELECT changed_cols("",true,a,b,c) FROM tbl`,
  1607. stmt: &ast.SelectStatement{
  1608. Fields: []ast.Field{
  1609. {
  1610. AName: "",
  1611. Name: "changed_cols",
  1612. Expr: &ast.Call{
  1613. Name: "changed_cols",
  1614. Args: []ast.Expr{
  1615. &ast.ColFuncField{
  1616. Name: "",
  1617. Expr: &ast.StringLiteral{Val: ""},
  1618. },
  1619. &ast.ColFuncField{
  1620. Name: "",
  1621. Expr: &ast.BooleanLiteral{Val: true},
  1622. },
  1623. &ast.ColFuncField{Name: "a", Expr: &ast.FieldRef{
  1624. StreamName: ast.DefaultStream,
  1625. Name: "a",
  1626. }},
  1627. &ast.ColFuncField{Name: "b", Expr: &ast.FieldRef{
  1628. StreamName: ast.DefaultStream,
  1629. Name: "b",
  1630. }},
  1631. &ast.ColFuncField{Name: "c", Expr: &ast.FieldRef{
  1632. StreamName: ast.DefaultStream,
  1633. Name: "c",
  1634. }},
  1635. },
  1636. FuncType: ast.FuncTypeCols,
  1637. },
  1638. },
  1639. },
  1640. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1641. },
  1642. }, {
  1643. s: `SELECT changed_cols("",true,a,*,c) FROM tbl`,
  1644. stmt: &ast.SelectStatement{
  1645. Fields: []ast.Field{
  1646. {
  1647. AName: "",
  1648. Name: "changed_cols",
  1649. Expr: &ast.Call{
  1650. Name: "changed_cols",
  1651. Args: []ast.Expr{
  1652. &ast.ColFuncField{
  1653. Name: "",
  1654. Expr: &ast.StringLiteral{Val: ""},
  1655. },
  1656. &ast.ColFuncField{
  1657. Name: "",
  1658. Expr: &ast.BooleanLiteral{Val: true},
  1659. },
  1660. &ast.ColFuncField{Name: "a", Expr: &ast.FieldRef{
  1661. StreamName: ast.DefaultStream,
  1662. Name: "a",
  1663. }},
  1664. &ast.ColFuncField{Name: "*", Expr: &ast.Wildcard{
  1665. Token: ast.ASTERISK,
  1666. }},
  1667. &ast.ColFuncField{Name: "c", Expr: &ast.FieldRef{
  1668. StreamName: ast.DefaultStream,
  1669. Name: "c",
  1670. }},
  1671. },
  1672. FuncType: ast.FuncTypeCols,
  1673. },
  1674. },
  1675. },
  1676. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1677. },
  1678. }, {
  1679. s: `SELECT a FROM tbl WHERE changed_cols("",true,a,b,c) > 3`,
  1680. err: "function changed_cols can only be used inside the select clause",
  1681. },
  1682. {
  1683. s: `SELECT ".*(/)(?!.*\1)" FROM topic/sensor1 AS t1`,
  1684. err: `found "invalid string: \".*(/)(?!.*\\1)\"", expected expression.`,
  1685. },
  1686. {
  1687. s: `SELECT name FROM tbl WHERE name IN ("A", "B","C")`,
  1688. stmt: &ast.SelectStatement{
  1689. Fields: []ast.Field{
  1690. {
  1691. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  1692. Name: "name",
  1693. AName: ""},
  1694. },
  1695. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1696. Condition: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, OP: ast.IN, RHS: &ast.ValueSetExpr{LiteralExprs: []ast.Expr{&ast.StringLiteral{Val: "A"}, &ast.StringLiteral{Val: "B"}, &ast.StringLiteral{Val: "C"}}}},
  1697. },
  1698. },
  1699. {
  1700. s: `SELECT name FROM tbl WHERE name IN ()`,
  1701. err: `expect elements for IN expression, but found ")", expected expression.`,
  1702. },
  1703. {
  1704. s: `SELECT name FROM tbl WHERE name IN (abc,def OR name in (abc)`,
  1705. err: `expect ) for IN expression, but got "EOF"`,
  1706. },
  1707. {
  1708. s: `SELECT lag(name) OVER (PARTITION BY device) FROM tbl`,
  1709. stmt: &ast.SelectStatement{
  1710. Fields: []ast.Field{
  1711. {
  1712. Expr: &ast.Call{
  1713. Name: "lag",
  1714. FuncId: 0,
  1715. Args: []ast.Expr{
  1716. &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  1717. },
  1718. Partition: &ast.PartitionExpr{
  1719. Exprs: []ast.Expr{
  1720. &ast.FieldRef{Name: "device", StreamName: ast.DefaultStream},
  1721. },
  1722. },
  1723. },
  1724. Name: "lag",
  1725. AName: ""},
  1726. },
  1727. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1728. },
  1729. },
  1730. {
  1731. s: `SELECT name OVER (PARTITION BY device) FROM tbl`,
  1732. err: `found "OVER", expected FROM.`,
  1733. },
  1734. {
  1735. s: `SELECT avg(name) OVER (PARTITION BY device) FROM tbl`,
  1736. err: `Found OVER after non analytic function avg`,
  1737. },
  1738. {
  1739. s: `SELECT name FROM tbl WHERE lag(name) OVER (PARTITION BY device, groupName) > 3`,
  1740. stmt: &ast.SelectStatement{
  1741. Fields: []ast.Field{
  1742. {
  1743. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  1744. Name: "name",
  1745. AName: ""},
  1746. },
  1747. Condition: &ast.BinaryExpr{
  1748. LHS: &ast.Call{
  1749. Name: "lag",
  1750. FuncId: 0,
  1751. Args: []ast.Expr{
  1752. &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  1753. },
  1754. Partition: &ast.PartitionExpr{
  1755. Exprs: []ast.Expr{
  1756. &ast.FieldRef{Name: "device", StreamName: ast.DefaultStream},
  1757. &ast.FieldRef{Name: "groupName", StreamName: ast.DefaultStream},
  1758. },
  1759. },
  1760. },
  1761. OP: ast.GT,
  1762. RHS: &ast.IntegerLiteral{Val: 3},
  1763. },
  1764. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1765. },
  1766. },
  1767. {
  1768. s: `SELECT lag(name) OVER (PARTITION BY device) as ll FROM tbl`,
  1769. stmt: &ast.SelectStatement{
  1770. Fields: []ast.Field{
  1771. {
  1772. Expr: &ast.Call{
  1773. Name: "lag",
  1774. FuncId: 0,
  1775. Args: []ast.Expr{
  1776. &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  1777. },
  1778. Partition: &ast.PartitionExpr{
  1779. Exprs: []ast.Expr{
  1780. &ast.FieldRef{Name: "device", StreamName: ast.DefaultStream},
  1781. },
  1782. },
  1783. },
  1784. Name: "lag",
  1785. AName: "ll"},
  1786. },
  1787. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1788. },
  1789. },
  1790. {
  1791. s: `SELECT lag(name) OVER (PARTITION BY device WHEN abc > 12) as ll FROM tbl`,
  1792. stmt: &ast.SelectStatement{
  1793. Fields: []ast.Field{
  1794. {
  1795. Expr: &ast.Call{
  1796. Name: "lag",
  1797. FuncId: 0,
  1798. Args: []ast.Expr{
  1799. &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  1800. },
  1801. Partition: &ast.PartitionExpr{
  1802. Exprs: []ast.Expr{
  1803. &ast.FieldRef{Name: "device", StreamName: ast.DefaultStream},
  1804. },
  1805. },
  1806. WhenExpr: &ast.BinaryExpr{
  1807. LHS: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream},
  1808. OP: ast.GT,
  1809. RHS: &ast.IntegerLiteral{Val: 12},
  1810. },
  1811. },
  1812. Name: "lag",
  1813. AName: "ll"},
  1814. },
  1815. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1816. },
  1817. },
  1818. {
  1819. s: `SELECT lag(name) OVER (PARTITION BY device WHEN had_changed(true, StatusCode)) as ll FROM tbl`,
  1820. stmt: &ast.SelectStatement{
  1821. Fields: []ast.Field{
  1822. {
  1823. Expr: &ast.Call{
  1824. Name: "lag",
  1825. FuncId: 0,
  1826. Args: []ast.Expr{
  1827. &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  1828. },
  1829. Partition: &ast.PartitionExpr{
  1830. Exprs: []ast.Expr{
  1831. &ast.FieldRef{Name: "device", StreamName: ast.DefaultStream},
  1832. },
  1833. },
  1834. WhenExpr: &ast.Call{
  1835. Name: "had_changed",
  1836. FuncId: 1,
  1837. Args: []ast.Expr{&ast.BooleanLiteral{Val: true}, &ast.FieldRef{Name: "StatusCode", StreamName: ast.DefaultStream}},
  1838. },
  1839. },
  1840. Name: "lag",
  1841. AName: "ll"},
  1842. },
  1843. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1844. },
  1845. },
  1846. {
  1847. s: `SELECT lag(name) OVER (WHEN had_changed(true, StatusCode)) as ll FROM tbl`,
  1848. stmt: &ast.SelectStatement{
  1849. Fields: []ast.Field{
  1850. {
  1851. Expr: &ast.Call{
  1852. Name: "lag",
  1853. FuncId: 0,
  1854. Args: []ast.Expr{
  1855. &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  1856. },
  1857. WhenExpr: &ast.Call{
  1858. Name: "had_changed",
  1859. FuncId: 1,
  1860. Args: []ast.Expr{&ast.BooleanLiteral{Val: true}, &ast.FieldRef{Name: "StatusCode", StreamName: ast.DefaultStream}},
  1861. },
  1862. },
  1863. Name: "lag",
  1864. AName: "ll"},
  1865. },
  1866. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1867. },
  1868. },
  1869. {
  1870. s: `SELECT name OVER (WHEN a > b) FROM tbl`,
  1871. err: `found "OVER", expected FROM.`,
  1872. },
  1873. {
  1874. s: `SELECT avg(name) OVER (WHEN a > b) FROM tbl`,
  1875. err: `Found OVER after non analytic function avg`,
  1876. },
  1877. {
  1878. s: `SELECT *, name, lower(name) as ln FROM tbl`,
  1879. stmt: &ast.SelectStatement{
  1880. Fields: []ast.Field{
  1881. {
  1882. Expr: &ast.Wildcard{
  1883. Token: ast.ASTERISK,
  1884. },
  1885. Name: "*",
  1886. },
  1887. {
  1888. Expr: &ast.FieldRef{
  1889. Name: "name",
  1890. StreamName: ast.DefaultStream,
  1891. },
  1892. Name: "name",
  1893. AName: "",
  1894. },
  1895. {
  1896. Expr: &ast.Call{
  1897. Name: "lower",
  1898. FuncId: 0,
  1899. Args: []ast.Expr{
  1900. &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  1901. },
  1902. },
  1903. Name: "lower",
  1904. AName: "ln",
  1905. },
  1906. },
  1907. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1908. },
  1909. },
  1910. {
  1911. s: `SELECT name, * FROM tbl`,
  1912. stmt: &ast.SelectStatement{
  1913. Fields: []ast.Field{
  1914. {
  1915. Expr: &ast.FieldRef{
  1916. Name: "name",
  1917. StreamName: ast.DefaultStream,
  1918. },
  1919. Name: "name",
  1920. AName: "",
  1921. },
  1922. {
  1923. Expr: &ast.Wildcard{
  1924. Token: ast.ASTERISK,
  1925. },
  1926. Name: "*",
  1927. },
  1928. },
  1929. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1930. },
  1931. },
  1932. {
  1933. s: `SELECT name, * FROM tbl`,
  1934. stmt: &ast.SelectStatement{
  1935. Fields: []ast.Field{
  1936. {
  1937. Expr: &ast.FieldRef{
  1938. Name: "name",
  1939. StreamName: ast.DefaultStream,
  1940. },
  1941. Name: "name",
  1942. AName: "",
  1943. },
  1944. {
  1945. Expr: &ast.Wildcard{
  1946. Token: ast.ASTERISK,
  1947. },
  1948. Name: "*",
  1949. },
  1950. },
  1951. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1952. },
  1953. },
  1954. }
  1955. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  1956. for i, tt := range tests {
  1957. //fmt.Printf("Parsing SQL %q.\n", tt.s)
  1958. stmt, err := NewParser(strings.NewReader(tt.s)).Parse()
  1959. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  1960. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.s, tt.err, err)
  1961. } else if tt.err == "" && !reflect.DeepEqual(tt.stmt, stmt) {
  1962. t.Errorf("%d. %q\n\nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.s, tt.stmt, stmt)
  1963. }
  1964. }
  1965. }
  1966. func TestParser_ParseWindowsExpr(t *testing.T) {
  1967. var tests = []struct {
  1968. s string
  1969. stmt *ast.SelectStatement
  1970. err string
  1971. }{
  1972. {
  1973. s: `SELECT f1 FROM tbl GROUP BY TUMBLINGWINDOW(ss, 10)`,
  1974. stmt: &ast.SelectStatement{
  1975. Fields: []ast.Field{
  1976. {
  1977. Expr: &ast.FieldRef{Name: "f1", StreamName: ast.DefaultStream},
  1978. Name: "f1",
  1979. AName: ""},
  1980. },
  1981. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1982. Dimensions: ast.Dimensions{
  1983. ast.Dimension{
  1984. Expr: &ast.Window{
  1985. WindowType: ast.TUMBLING_WINDOW,
  1986. Length: &ast.IntegerLiteral{Val: 10000},
  1987. Interval: &ast.IntegerLiteral{Val: 0},
  1988. },
  1989. },
  1990. },
  1991. },
  1992. },
  1993. {
  1994. s: `SELECT f1 FROM tbl GROUP BY HOPPINGWINDOW(mi, 5, 1)`,
  1995. stmt: &ast.SelectStatement{
  1996. Fields: []ast.Field{
  1997. {
  1998. Expr: &ast.FieldRef{Name: "f1", StreamName: ast.DefaultStream},
  1999. Name: "f1",
  2000. AName: ""},
  2001. },
  2002. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  2003. Dimensions: ast.Dimensions{
  2004. ast.Dimension{
  2005. Expr: &ast.Window{
  2006. WindowType: ast.HOPPING_WINDOW,
  2007. Length: &ast.IntegerLiteral{Val: 3e5},
  2008. Interval: &ast.IntegerLiteral{Val: 6e4},
  2009. },
  2010. },
  2011. },
  2012. },
  2013. },
  2014. {
  2015. s: `SELECT f1 FROM tbl GROUP BY SESSIONWINDOW(hh, 5, 1)`,
  2016. stmt: &ast.SelectStatement{
  2017. Fields: []ast.Field{
  2018. {
  2019. Expr: &ast.FieldRef{Name: "f1", StreamName: ast.DefaultStream},
  2020. Name: "f1",
  2021. AName: ""},
  2022. },
  2023. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  2024. Dimensions: ast.Dimensions{
  2025. ast.Dimension{
  2026. Expr: &ast.Window{
  2027. WindowType: ast.SESSION_WINDOW,
  2028. Length: &ast.IntegerLiteral{Val: 1.8e7},
  2029. Interval: &ast.IntegerLiteral{Val: 3.6e6},
  2030. },
  2031. },
  2032. },
  2033. },
  2034. },
  2035. {
  2036. s: `SELECT f1 FROM tbl GROUP BY SLIDINGWINDOW(ms, 5)`,
  2037. stmt: &ast.SelectStatement{
  2038. Fields: []ast.Field{
  2039. {
  2040. Expr: &ast.FieldRef{Name: "f1", StreamName: ast.DefaultStream},
  2041. Name: "f1",
  2042. AName: ""},
  2043. },
  2044. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  2045. Dimensions: ast.Dimensions{
  2046. ast.Dimension{
  2047. Expr: &ast.Window{
  2048. WindowType: ast.SLIDING_WINDOW,
  2049. Length: &ast.IntegerLiteral{Val: 5},
  2050. Interval: &ast.IntegerLiteral{Val: 0},
  2051. },
  2052. },
  2053. },
  2054. },
  2055. },
  2056. {
  2057. s: `SELECT f1 FROM tbl GROUP BY SLIDINGWINDOW(mi, 5, 1)`,
  2058. stmt: nil,
  2059. err: "The arguments for slidingwindow should be 2.\n",
  2060. },
  2061. {
  2062. s: `SELECT f1 FROM tbl GROUP BY SLIDINGWINDOW("mi", 5)`,
  2063. stmt: nil,
  2064. err: "The 1st argument for slidingwindow is expecting timer literal expression. One value of [dd|hh|mi|ss|ms].\n",
  2065. },
  2066. {
  2067. s: `SELECT f1 FROM tbl GROUP BY COUNTWINDOW(10)`,
  2068. stmt: &ast.SelectStatement{
  2069. Fields: []ast.Field{
  2070. {
  2071. Expr: &ast.FieldRef{Name: "f1", StreamName: ast.DefaultStream},
  2072. Name: "f1",
  2073. AName: ""},
  2074. },
  2075. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  2076. Dimensions: ast.Dimensions{
  2077. ast.Dimension{
  2078. Expr: &ast.Window{
  2079. WindowType: ast.COUNT_WINDOW,
  2080. Length: &ast.IntegerLiteral{Val: 10},
  2081. },
  2082. },
  2083. },
  2084. },
  2085. },
  2086. {
  2087. s: `SELECT f1 FROM tbl GROUP BY COUNTWINDOW(10, 5)`,
  2088. stmt: &ast.SelectStatement{
  2089. Fields: []ast.Field{
  2090. {
  2091. Expr: &ast.FieldRef{Name: "f1", StreamName: ast.DefaultStream},
  2092. Name: "f1",
  2093. AName: ""},
  2094. },
  2095. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  2096. Dimensions: ast.Dimensions{
  2097. ast.Dimension{
  2098. Expr: &ast.Window{
  2099. WindowType: ast.COUNT_WINDOW,
  2100. Length: &ast.IntegerLiteral{Val: 10},
  2101. Interval: &ast.IntegerLiteral{Val: 5},
  2102. },
  2103. },
  2104. },
  2105. },
  2106. },
  2107. {
  2108. s: `SELECT f1 FROM tbl GROUP BY COUNTWINDOW(3, 5)`,
  2109. stmt: nil,
  2110. err: "The second parameter value 5 should be less than the first parameter 3.",
  2111. },
  2112. {
  2113. s: `SELECT * FROM demo GROUP BY COUNTWINDOW(3,1) FILTER( where revenue > 100 )`,
  2114. stmt: &ast.SelectStatement{
  2115. Fields: []ast.Field{
  2116. {
  2117. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  2118. Name: "*",
  2119. AName: ""},
  2120. },
  2121. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2122. Dimensions: ast.Dimensions{
  2123. ast.Dimension{
  2124. Expr: &ast.Window{
  2125. WindowType: ast.COUNT_WINDOW,
  2126. Length: &ast.IntegerLiteral{Val: 3},
  2127. Interval: &ast.IntegerLiteral{Val: 1},
  2128. Filter: &ast.BinaryExpr{
  2129. LHS: &ast.FieldRef{Name: "revenue", StreamName: ast.DefaultStream},
  2130. OP: ast.GT,
  2131. RHS: &ast.IntegerLiteral{Val: 100},
  2132. },
  2133. },
  2134. },
  2135. },
  2136. },
  2137. },
  2138. {
  2139. s: `SELECT * FROM demo GROUP BY department, COUNTWINDOW(3,1) FILTER( where revenue > 100 ), year`,
  2140. stmt: &ast.SelectStatement{
  2141. Fields: []ast.Field{
  2142. {
  2143. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  2144. Name: "*",
  2145. AName: ""},
  2146. },
  2147. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2148. Dimensions: ast.Dimensions{
  2149. ast.Dimension{Expr: &ast.FieldRef{Name: "department", StreamName: ast.DefaultStream}},
  2150. ast.Dimension{
  2151. Expr: &ast.Window{
  2152. WindowType: ast.COUNT_WINDOW,
  2153. Length: &ast.IntegerLiteral{Val: 3},
  2154. Interval: &ast.IntegerLiteral{Val: 1},
  2155. Filter: &ast.BinaryExpr{
  2156. LHS: &ast.FieldRef{Name: "revenue", StreamName: ast.DefaultStream},
  2157. OP: ast.GT,
  2158. RHS: &ast.IntegerLiteral{Val: 100},
  2159. },
  2160. },
  2161. },
  2162. ast.Dimension{Expr: &ast.FieldRef{Name: "year", StreamName: ast.DefaultStream}},
  2163. },
  2164. },
  2165. },
  2166. {
  2167. s: `SELECT * FROM demo GROUP BY department, COUNTWINDOW(3,1) FILTER( where revenue IN (100, 200)), year`,
  2168. stmt: &ast.SelectStatement{
  2169. Fields: []ast.Field{
  2170. {
  2171. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  2172. Name: "*",
  2173. AName: ""},
  2174. },
  2175. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2176. Dimensions: ast.Dimensions{
  2177. ast.Dimension{Expr: &ast.FieldRef{Name: "department", StreamName: ast.DefaultStream}},
  2178. ast.Dimension{
  2179. Expr: &ast.Window{
  2180. WindowType: ast.COUNT_WINDOW,
  2181. Length: &ast.IntegerLiteral{Val: 3},
  2182. Interval: &ast.IntegerLiteral{Val: 1},
  2183. Filter: &ast.BinaryExpr{
  2184. LHS: &ast.FieldRef{Name: "revenue", StreamName: ast.DefaultStream},
  2185. OP: ast.IN,
  2186. RHS: &ast.ValueSetExpr{
  2187. LiteralExprs: []ast.Expr{&ast.IntegerLiteral{Val: 100}, &ast.IntegerLiteral{Val: 200}},
  2188. },
  2189. },
  2190. },
  2191. },
  2192. ast.Dimension{Expr: &ast.FieldRef{Name: "year", StreamName: ast.DefaultStream}},
  2193. },
  2194. },
  2195. },
  2196. //to be supported
  2197. {
  2198. s: `SELECT sum(f1) FILTER( where revenue > 100 ) FROM tbl GROUP BY year`,
  2199. stmt: nil,
  2200. err: "found \"FILTER\", expected FROM.",
  2201. },
  2202. {
  2203. s: `SELECT * FROM demo GROUP BY COUNTWINDOW(3,1) FILTER where revenue > 100`,
  2204. stmt: nil,
  2205. err: "Found \"WHERE\" after FILTER, expect parentheses.",
  2206. },
  2207. {
  2208. s: `SELECT * FROM demo GROUP BY COUNTWINDOW(3,1) where revenue > 100`,
  2209. stmt: nil,
  2210. err: "found \"WHERE\", expected EOF.",
  2211. },
  2212. }
  2213. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  2214. for i, tt := range tests {
  2215. //fmt.Printf("Parsing SQL %q.\n", tt.s)
  2216. stmt, err := NewParser(strings.NewReader(tt.s)).Parse()
  2217. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  2218. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.s, tt.err, err)
  2219. } else if tt.err == "" && !reflect.DeepEqual(tt.stmt, stmt) {
  2220. t.Errorf("%d. %q\n\nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.s, tt.stmt, stmt)
  2221. }
  2222. }
  2223. }
  2224. func TestParser_ParseJsonExpr(t *testing.T) {
  2225. var tests = []struct {
  2226. s string
  2227. stmt *ast.SelectStatement
  2228. err string
  2229. }{
  2230. {
  2231. s: `SELECT children[0] FROM demo`,
  2232. stmt: &ast.SelectStatement{
  2233. Fields: []ast.Field{
  2234. {
  2235. Expr: &ast.BinaryExpr{
  2236. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  2237. OP: ast.SUBSET,
  2238. RHS: &ast.IndexExpr{Index: &ast.IntegerLiteral{Val: 0}},
  2239. },
  2240. Name: "kuiper_field_0",
  2241. AName: ""},
  2242. },
  2243. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2244. },
  2245. },
  2246. {
  2247. s: `SELECT children[0]->first FROM demo`,
  2248. stmt: &ast.SelectStatement{
  2249. Fields: []ast.Field{
  2250. {
  2251. Expr: &ast.BinaryExpr{
  2252. LHS: &ast.BinaryExpr{
  2253. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  2254. OP: ast.SUBSET,
  2255. RHS: &ast.IndexExpr{Index: &ast.IntegerLiteral{Val: 0}},
  2256. },
  2257. OP: ast.ARROW,
  2258. RHS: &ast.JsonFieldRef{Name: "first"},
  2259. },
  2260. Name: "kuiper_field_0",
  2261. AName: ""},
  2262. },
  2263. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2264. },
  2265. },
  2266. {
  2267. s: `SELECT children->first[2] FROM demo`,
  2268. stmt: &ast.SelectStatement{
  2269. Fields: []ast.Field{
  2270. {
  2271. Expr: &ast.BinaryExpr{
  2272. LHS: &ast.BinaryExpr{
  2273. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  2274. OP: ast.ARROW,
  2275. RHS: &ast.JsonFieldRef{Name: "first"},
  2276. },
  2277. OP: ast.SUBSET,
  2278. RHS: &ast.IndexExpr{Index: &ast.IntegerLiteral{Val: 2}},
  2279. },
  2280. Name: "kuiper_field_0",
  2281. AName: ""},
  2282. },
  2283. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2284. },
  2285. },
  2286. {
  2287. s: `SELECT children->first[2]->test FROM demo`,
  2288. stmt: &ast.SelectStatement{
  2289. Fields: []ast.Field{
  2290. {
  2291. Expr: &ast.BinaryExpr{
  2292. LHS: &ast.BinaryExpr{
  2293. LHS: &ast.BinaryExpr{
  2294. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  2295. OP: ast.ARROW,
  2296. RHS: &ast.JsonFieldRef{Name: "first"},
  2297. },
  2298. OP: ast.SUBSET,
  2299. RHS: &ast.IndexExpr{Index: &ast.IntegerLiteral{Val: 2}},
  2300. },
  2301. OP: ast.ARROW,
  2302. RHS: &ast.JsonFieldRef{Name: "test"},
  2303. },
  2304. Name: "kuiper_field_0",
  2305. AName: ""},
  2306. },
  2307. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2308. },
  2309. },
  2310. {
  2311. s: `SELECT children[0:1] FROM demo`,
  2312. stmt: &ast.SelectStatement{
  2313. Fields: []ast.Field{
  2314. {
  2315. Expr: &ast.BinaryExpr{
  2316. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  2317. OP: ast.SUBSET,
  2318. RHS: &ast.ColonExpr{Start: &ast.IntegerLiteral{Val: 0}, End: &ast.IntegerLiteral{Val: 1}},
  2319. },
  2320. Name: "kuiper_field_0",
  2321. AName: ""},
  2322. },
  2323. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2324. },
  2325. },
  2326. {
  2327. s: `SELECT children[:1] FROM demo`,
  2328. stmt: &ast.SelectStatement{
  2329. Fields: []ast.Field{
  2330. {
  2331. Expr: &ast.BinaryExpr{
  2332. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  2333. OP: ast.SUBSET,
  2334. RHS: &ast.ColonExpr{Start: &ast.IntegerLiteral{Val: 0}, End: &ast.IntegerLiteral{Val: 1}},
  2335. },
  2336. Name: "kuiper_field_0",
  2337. AName: ""},
  2338. },
  2339. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2340. },
  2341. },
  2342. {
  2343. s: `SELECT children[:] FROM demo`,
  2344. stmt: &ast.SelectStatement{
  2345. Fields: []ast.Field{
  2346. {
  2347. Expr: &ast.BinaryExpr{
  2348. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  2349. OP: ast.SUBSET,
  2350. RHS: &ast.ColonExpr{Start: &ast.IntegerLiteral{Val: 0}, End: &ast.IntegerLiteral{Val: math.MinInt32}},
  2351. },
  2352. Name: "kuiper_field_0",
  2353. AName: ""},
  2354. },
  2355. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2356. },
  2357. },
  2358. {
  2359. s: `SELECT children[2:] AS c FROM demo`,
  2360. stmt: &ast.SelectStatement{
  2361. Fields: []ast.Field{
  2362. {
  2363. Expr: &ast.BinaryExpr{
  2364. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  2365. OP: ast.SUBSET,
  2366. RHS: &ast.ColonExpr{Start: &ast.IntegerLiteral{Val: 2}, End: &ast.IntegerLiteral{Val: math.MinInt32}},
  2367. },
  2368. Name: "",
  2369. AName: "c"},
  2370. },
  2371. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2372. },
  2373. },
  2374. {
  2375. s: `SELECT children[2:]->first AS c FROM demo`,
  2376. stmt: &ast.SelectStatement{
  2377. Fields: []ast.Field{
  2378. {
  2379. Expr: &ast.BinaryExpr{
  2380. LHS: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream}, OP: ast.SUBSET, RHS: &ast.ColonExpr{Start: &ast.IntegerLiteral{Val: 2}, End: &ast.IntegerLiteral{Val: math.MinInt32}}},
  2381. OP: ast.ARROW,
  2382. RHS: &ast.JsonFieldRef{Name: "first"},
  2383. },
  2384. Name: "",
  2385. AName: "c"},
  2386. },
  2387. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2388. },
  2389. },
  2390. {
  2391. s: `SELECT demo.* FROM demo`,
  2392. stmt: &ast.SelectStatement{
  2393. Fields: []ast.Field{
  2394. {
  2395. Expr: &ast.FieldRef{StreamName: ast.StreamName("demo"), Name: "*"},
  2396. Name: "*",
  2397. AName: ""},
  2398. },
  2399. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2400. },
  2401. },
  2402. {
  2403. s: `SELECT demo.children[2:]->first AS c FROM demo`,
  2404. stmt: &ast.SelectStatement{
  2405. Fields: []ast.Field{
  2406. {
  2407. Expr: &ast.BinaryExpr{
  2408. LHS: &ast.BinaryExpr{LHS: &ast.FieldRef{StreamName: ast.StreamName("demo"), Name: "children"}, OP: ast.SUBSET, RHS: &ast.ColonExpr{Start: &ast.IntegerLiteral{Val: 2}, End: &ast.IntegerLiteral{Val: math.MinInt32}}},
  2409. OP: ast.ARROW,
  2410. RHS: &ast.JsonFieldRef{Name: "first"},
  2411. },
  2412. Name: "",
  2413. AName: "c"},
  2414. },
  2415. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2416. },
  2417. },
  2418. {
  2419. s: `SELECT lower(demo.children[2:]->first) AS c FROM demo`,
  2420. stmt: &ast.SelectStatement{
  2421. Fields: []ast.Field{
  2422. {
  2423. Expr: &ast.Call{
  2424. Name: "lower",
  2425. Args: []ast.Expr{
  2426. &ast.BinaryExpr{
  2427. LHS: &ast.BinaryExpr{LHS: &ast.FieldRef{StreamName: ast.StreamName("demo"), Name: "children"}, OP: ast.SUBSET, RHS: &ast.ColonExpr{Start: &ast.IntegerLiteral{Val: 2}, End: &ast.IntegerLiteral{Val: math.MinInt32}}},
  2428. OP: ast.ARROW,
  2429. RHS: &ast.JsonFieldRef{Name: "first"},
  2430. },
  2431. },
  2432. },
  2433. Name: "lower",
  2434. AName: "c"},
  2435. },
  2436. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2437. },
  2438. },
  2439. {
  2440. s: `SELECT children[:1] FROM demo WHERE abc[0] > 12`,
  2441. stmt: &ast.SelectStatement{
  2442. Fields: []ast.Field{
  2443. {
  2444. Expr: &ast.BinaryExpr{
  2445. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  2446. OP: ast.SUBSET,
  2447. RHS: &ast.ColonExpr{Start: &ast.IntegerLiteral{Val: 0}, End: &ast.IntegerLiteral{Val: 1}},
  2448. },
  2449. Name: "kuiper_field_0",
  2450. AName: ""},
  2451. },
  2452. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2453. Condition: &ast.BinaryExpr{
  2454. LHS: &ast.BinaryExpr{
  2455. LHS: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream},
  2456. OP: ast.SUBSET,
  2457. RHS: &ast.IndexExpr{Index: &ast.IntegerLiteral{Val: 0}},
  2458. },
  2459. OP: ast.GT,
  2460. RHS: &ast.IntegerLiteral{Val: 12},
  2461. },
  2462. },
  2463. },
  2464. {
  2465. s: `SELECT children[:1] FROM demo WHERE abc[0] IN demo.children[2:]->first`,
  2466. stmt: &ast.SelectStatement{
  2467. Fields: []ast.Field{
  2468. {
  2469. Expr: &ast.BinaryExpr{
  2470. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  2471. OP: ast.SUBSET,
  2472. RHS: &ast.ColonExpr{Start: &ast.IntegerLiteral{Val: 0}, End: &ast.IntegerLiteral{Val: 1}},
  2473. },
  2474. Name: "kuiper_field_0",
  2475. AName: ""},
  2476. },
  2477. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2478. Condition: &ast.BinaryExpr{
  2479. LHS: &ast.BinaryExpr{
  2480. LHS: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream},
  2481. OP: ast.SUBSET,
  2482. RHS: &ast.IndexExpr{Index: &ast.IntegerLiteral{Val: 0}},
  2483. },
  2484. OP: ast.IN,
  2485. RHS: &ast.BinaryExpr{
  2486. LHS: &ast.BinaryExpr{LHS: &ast.FieldRef{StreamName: ast.StreamName("demo"), Name: "children"}, OP: ast.SUBSET, RHS: &ast.ColonExpr{Start: &ast.IntegerLiteral{Val: 2}, End: &ast.IntegerLiteral{Val: math.MinInt32}}},
  2487. OP: ast.ARROW,
  2488. RHS: &ast.JsonFieldRef{Name: "first"},
  2489. },
  2490. },
  2491. },
  2492. },
  2493. {
  2494. s: `SELECT demo.children.first AS c FROM demo`,
  2495. stmt: nil,
  2496. err: "Too many field names. Please use -> to reference keys in struct.\n",
  2497. },
  2498. {
  2499. s: `SELECT children[index] FROM demo`,
  2500. stmt: &ast.SelectStatement{
  2501. Fields: []ast.Field{
  2502. {
  2503. Expr: &ast.BinaryExpr{
  2504. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  2505. OP: ast.SUBSET,
  2506. RHS: &ast.IndexExpr{Index: &ast.FieldRef{Name: "index", StreamName: ast.DefaultStream}},
  2507. },
  2508. Name: "kuiper_field_0",
  2509. AName: ""},
  2510. },
  2511. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2512. },
  2513. },
  2514. }
  2515. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  2516. for i, tt := range tests {
  2517. stmt, err := NewParser(strings.NewReader(tt.s)).Parse()
  2518. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  2519. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.s, tt.err, err)
  2520. } else if tt.err == "" && !reflect.DeepEqual(tt.stmt, stmt) {
  2521. t.Errorf("%d. %q\n\nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.s, tt.stmt, stmt)
  2522. }
  2523. }
  2524. }
  2525. func TestParser_ParseJoins(t *testing.T) {
  2526. var tests = []struct {
  2527. s string
  2528. stmt *ast.SelectStatement
  2529. err string
  2530. }{
  2531. {
  2532. s: `SELECT * FROM topic/sensor1 LEFT JOIN topic1 ON f=k`,
  2533. stmt: &ast.SelectStatement{
  2534. Fields: []ast.Field{
  2535. {
  2536. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  2537. Name: "*",
  2538. AName: ""},
  2539. },
  2540. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  2541. Joins: []ast.Join{
  2542. {
  2543. Name: "topic1", Alias: "", JoinType: ast.LEFT_JOIN, Expr: &ast.BinaryExpr{
  2544. LHS: &ast.FieldRef{Name: "f", StreamName: ast.DefaultStream},
  2545. OP: ast.EQ,
  2546. RHS: &ast.FieldRef{Name: "k", StreamName: ast.DefaultStream},
  2547. },
  2548. },
  2549. },
  2550. },
  2551. },
  2552. {
  2553. s: `SELECT * FROM topic/sensor1 AS t1 INNER JOIN topic1 AS t2 ON f=k`,
  2554. stmt: &ast.SelectStatement{
  2555. Fields: []ast.Field{
  2556. {
  2557. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  2558. Name: "*",
  2559. AName: ""},
  2560. },
  2561. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
  2562. Joins: []ast.Join{
  2563. {
  2564. Name: "topic1", Alias: "t2", JoinType: ast.INNER_JOIN, Expr: &ast.BinaryExpr{
  2565. LHS: &ast.FieldRef{Name: "f", StreamName: ast.DefaultStream},
  2566. OP: ast.EQ,
  2567. RHS: &ast.FieldRef{Name: "k", StreamName: ast.DefaultStream},
  2568. },
  2569. },
  2570. },
  2571. },
  2572. },
  2573. {
  2574. s: `SELECT * FROM topic/sensor1 AS t1 LEFT JOIN topic1/sensor2 AS t2 ON f=k`,
  2575. stmt: &ast.SelectStatement{
  2576. Fields: []ast.Field{
  2577. {
  2578. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  2579. Name: "*",
  2580. AName: ""},
  2581. },
  2582. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
  2583. Joins: []ast.Join{
  2584. {
  2585. Name: "topic1/sensor2", Alias: "t2", JoinType: ast.LEFT_JOIN, Expr: &ast.BinaryExpr{
  2586. LHS: &ast.FieldRef{Name: "f", StreamName: ast.DefaultStream},
  2587. OP: ast.EQ,
  2588. RHS: &ast.FieldRef{Name: "k", StreamName: ast.DefaultStream},
  2589. },
  2590. },
  2591. },
  2592. },
  2593. },
  2594. {
  2595. s: `SELECT t1.name FROM topic/sensor1 AS t1 LEFT JOIN topic1/sensor2 AS t2 ON f=k`,
  2596. stmt: &ast.SelectStatement{
  2597. Fields: []ast.Field{
  2598. {
  2599. Expr: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "name"},
  2600. Name: "name",
  2601. AName: ""},
  2602. },
  2603. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
  2604. Joins: []ast.Join{
  2605. {
  2606. Name: "topic1/sensor2", Alias: "t2", JoinType: ast.LEFT_JOIN, Expr: &ast.BinaryExpr{
  2607. LHS: &ast.FieldRef{Name: "f", StreamName: ast.DefaultStream},
  2608. OP: ast.EQ,
  2609. RHS: &ast.FieldRef{Name: "k", StreamName: ast.DefaultStream},
  2610. },
  2611. },
  2612. },
  2613. },
  2614. },
  2615. {
  2616. s: `SELECT t1.name FROM topic/sensor1 AS t1 LEFT JOIN topic1/sensor2 AS t2 ON t1.f=t2.k`,
  2617. stmt: &ast.SelectStatement{
  2618. Fields: []ast.Field{
  2619. {
  2620. Expr: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "name"},
  2621. Name: "name",
  2622. AName: ""},
  2623. },
  2624. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
  2625. Joins: []ast.Join{
  2626. {
  2627. Name: "topic1/sensor2", Alias: "t2", JoinType: ast.LEFT_JOIN, Expr: &ast.BinaryExpr{
  2628. LHS: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "f"},
  2629. OP: ast.EQ,
  2630. RHS: &ast.FieldRef{StreamName: ast.StreamName("t2"), Name: "k"},
  2631. },
  2632. },
  2633. },
  2634. },
  2635. },
  2636. {
  2637. s: `SELECT t1.name FROM topic/sensor1 AS t1 RIGHT JOIN topic1/sensor2 AS t2 ON t1.f=t2.k`,
  2638. stmt: &ast.SelectStatement{
  2639. Fields: []ast.Field{
  2640. {
  2641. Expr: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "name"},
  2642. Name: "name",
  2643. AName: ""},
  2644. },
  2645. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
  2646. Joins: []ast.Join{
  2647. {
  2648. Name: "topic1/sensor2", Alias: "t2", JoinType: ast.RIGHT_JOIN, Expr: &ast.BinaryExpr{
  2649. LHS: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "f"},
  2650. OP: ast.EQ,
  2651. RHS: &ast.FieldRef{StreamName: ast.StreamName("t2"), Name: "k"},
  2652. },
  2653. },
  2654. },
  2655. },
  2656. },
  2657. {
  2658. s: `SELECT t1.name FROM topic/sensor1 AS t1 FULL JOIN topic1/sensor2 AS t2 ON t1.f=t2.k`,
  2659. stmt: &ast.SelectStatement{
  2660. Fields: []ast.Field{
  2661. {
  2662. Expr: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "name"},
  2663. Name: "name",
  2664. AName: ""},
  2665. },
  2666. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
  2667. Joins: []ast.Join{
  2668. {
  2669. Name: "topic1/sensor2", Alias: "t2", JoinType: ast.FULL_JOIN, Expr: &ast.BinaryExpr{
  2670. LHS: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "f"},
  2671. OP: ast.EQ,
  2672. RHS: &ast.FieldRef{StreamName: ast.StreamName("t2"), Name: "k"},
  2673. },
  2674. },
  2675. },
  2676. },
  2677. },
  2678. {
  2679. s: `SELECT t1.name FROM topic/sensor1 AS t1 CROSS JOIN topic1/sensor2 AS t2 ON t1.f=t2.k`,
  2680. stmt: nil,
  2681. err: "On expression is not required for cross join type.\n",
  2682. },
  2683. {
  2684. s: `SELECT t1.name FROM topic/sensor1 AS t1 CROSS JOIN topic1/sensor2 AS t2`,
  2685. stmt: &ast.SelectStatement{
  2686. Fields: []ast.Field{
  2687. {
  2688. Expr: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "name"},
  2689. Name: "name",
  2690. AName: ""},
  2691. },
  2692. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
  2693. Joins: []ast.Join{
  2694. {
  2695. Name: "topic1/sensor2", Alias: "t2", JoinType: ast.CROSS_JOIN, Expr: nil,
  2696. },
  2697. },
  2698. },
  2699. },
  2700. {
  2701. s: `SELECT demo.*, demo2.* FROM demo LEFT JOIN demo2 on demo.f1 = demo2.f2`,
  2702. stmt: &ast.SelectStatement{
  2703. Fields: []ast.Field{
  2704. {
  2705. Expr: &ast.FieldRef{StreamName: ast.StreamName("demo"), Name: "*"},
  2706. Name: "*",
  2707. AName: ""},
  2708. {
  2709. Expr: &ast.FieldRef{StreamName: ast.StreamName("demo2"), Name: "*"},
  2710. Name: "*",
  2711. AName: ""},
  2712. },
  2713. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2714. Joins: []ast.Join{
  2715. {
  2716. Name: "demo2", Alias: "", JoinType: ast.LEFT_JOIN, Expr: &ast.BinaryExpr{
  2717. LHS: &ast.FieldRef{StreamName: ast.StreamName("demo"), Name: "f1"},
  2718. OP: ast.EQ,
  2719. RHS: &ast.FieldRef{StreamName: ast.StreamName("demo2"), Name: "f2"},
  2720. },
  2721. },
  2722. },
  2723. },
  2724. },
  2725. }
  2726. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  2727. for i, tt := range tests {
  2728. stmt, err := NewParser(strings.NewReader(tt.s)).Parse()
  2729. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  2730. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.s, tt.err, err)
  2731. } else if tt.err == "" && !reflect.DeepEqual(tt.stmt, stmt) {
  2732. t.Errorf("%d. %q\n\nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.s, tt.stmt, stmt)
  2733. }
  2734. }
  2735. }
  2736. func TestParser_ParseStatements(t *testing.T) {
  2737. var tests = []struct {
  2738. s string
  2739. stmts []ast.SelectStatement
  2740. err string
  2741. }{
  2742. {s: "SELECT name FROM tbl;\nSELECT name FROM topic/sensor1\n",
  2743. stmts: []ast.SelectStatement{
  2744. {
  2745. Fields: []ast.Field{
  2746. {
  2747. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  2748. Name: "name",
  2749. AName: ""},
  2750. },
  2751. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  2752. },
  2753. {
  2754. Fields: []ast.Field{
  2755. {
  2756. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  2757. Name: "name",
  2758. AName: ""},
  2759. },
  2760. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  2761. },
  2762. },
  2763. },
  2764. {s: "SELECT name FROM tbl;\nSELECT name FROM topic/sensor1;\n--SELECT comment FROM topic/comment",
  2765. stmts: []ast.SelectStatement{
  2766. {
  2767. Fields: []ast.Field{
  2768. {
  2769. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  2770. Name: "name",
  2771. AName: ""},
  2772. },
  2773. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  2774. },
  2775. {
  2776. Fields: []ast.Field{
  2777. {
  2778. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  2779. Name: "name",
  2780. AName: ""},
  2781. },
  2782. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  2783. },
  2784. },
  2785. },
  2786. }
  2787. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  2788. for i, tt := range tests {
  2789. stmts, err := NewParser(strings.NewReader(tt.s)).ParseQueries()
  2790. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  2791. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.s, tt.err, err)
  2792. } else if tt.err == "" && !reflect.DeepEqual(tt.stmts, stmts) {
  2793. t.Errorf("%d. %q\n\nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.s, tt.stmts, stmts)
  2794. }
  2795. }
  2796. }