parser_test.go 83 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946
  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 rule_id() as rule_id from demo`,
  1438. stmt: &ast.SelectStatement{
  1439. Fields: []ast.Field{
  1440. {
  1441. Expr: &ast.Call{
  1442. Name: "rule_id",
  1443. Args: nil,
  1444. },
  1445. Name: "rule_id",
  1446. AName: "rule_id"},
  1447. },
  1448. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  1449. },
  1450. err: "",
  1451. },
  1452. {
  1453. s: "SELECT `half FROM tb",
  1454. stmt: nil,
  1455. err: `found "EOF", expected FROM.`,
  1456. },
  1457. {
  1458. s: "SELECT `space var` FROM tbl",
  1459. stmt: &ast.SelectStatement{
  1460. Fields: []ast.Field{
  1461. {
  1462. Expr: &ast.FieldRef{Name: "space var", StreamName: ast.DefaultStream},
  1463. Name: "space var",
  1464. AName: ""},
  1465. },
  1466. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1467. },
  1468. },
  1469. {
  1470. s: "SELECT `中文 Chinese` FROM tbl",
  1471. stmt: &ast.SelectStatement{
  1472. Fields: []ast.Field{
  1473. {
  1474. Expr: &ast.FieldRef{Name: "中文 Chinese", StreamName: ast.DefaultStream},
  1475. Name: "中文 Chinese",
  1476. AName: ""},
  1477. },
  1478. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1479. },
  1480. }, {
  1481. s: "SELECT CASE temperature WHEN 25 THEN \"bingo\" WHEN 30 THEN \"high\" ELSE \"low\" END as label, humidity FROM tbl",
  1482. stmt: &ast.SelectStatement{
  1483. Fields: []ast.Field{
  1484. {
  1485. Expr: &ast.CaseExpr{
  1486. Value: &ast.FieldRef{Name: "temperature", StreamName: ast.DefaultStream},
  1487. WhenClauses: []*ast.WhenClause{
  1488. {
  1489. Expr: &ast.IntegerLiteral{Val: 25},
  1490. Result: &ast.StringLiteral{Val: "bingo"},
  1491. }, {
  1492. Expr: &ast.IntegerLiteral{Val: 30},
  1493. Result: &ast.StringLiteral{Val: "high"},
  1494. },
  1495. },
  1496. ElseClause: &ast.StringLiteral{Val: "low"},
  1497. },
  1498. Name: "",
  1499. AName: "label",
  1500. }, {
  1501. Expr: &ast.FieldRef{Name: "humidity", StreamName: ast.DefaultStream},
  1502. Name: "humidity",
  1503. AName: "",
  1504. },
  1505. },
  1506. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1507. },
  1508. }, {
  1509. s: "SELECT CASE temperature WHEN 25 THEN \"bingo\" WHEN 30 THEN \"high\" END as label, humidity FROM tbl",
  1510. stmt: &ast.SelectStatement{
  1511. Fields: []ast.Field{
  1512. {
  1513. Expr: &ast.CaseExpr{
  1514. Value: &ast.FieldRef{Name: "temperature", StreamName: ast.DefaultStream},
  1515. WhenClauses: []*ast.WhenClause{
  1516. {
  1517. Expr: &ast.IntegerLiteral{Val: 25},
  1518. Result: &ast.StringLiteral{Val: "bingo"},
  1519. }, {
  1520. Expr: &ast.IntegerLiteral{Val: 30},
  1521. Result: &ast.StringLiteral{Val: "high"},
  1522. },
  1523. },
  1524. ElseClause: nil,
  1525. },
  1526. Name: "",
  1527. AName: "label",
  1528. }, {
  1529. Expr: &ast.FieldRef{Name: "humidity", StreamName: ast.DefaultStream},
  1530. Name: "humidity",
  1531. AName: "",
  1532. },
  1533. },
  1534. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1535. },
  1536. }, {
  1537. s: "SELECT CASE temperature ELSE \"low\" END as label, humidity FROM tbl",
  1538. stmt: nil,
  1539. err: "invalid CASE expression, WHEN expected before ELSE",
  1540. }, {
  1541. s: "SELECT CASE WHEN temperature > 30 THEN \"high\" ELSE \"low\" END as label, humidity FROM tbl",
  1542. stmt: &ast.SelectStatement{
  1543. Fields: []ast.Field{
  1544. {
  1545. Expr: &ast.CaseExpr{
  1546. Value: nil,
  1547. WhenClauses: []*ast.WhenClause{
  1548. {
  1549. Expr: &ast.BinaryExpr{
  1550. OP: ast.GT,
  1551. LHS: &ast.FieldRef{Name: "temperature", StreamName: ast.DefaultStream},
  1552. RHS: &ast.IntegerLiteral{Val: 30},
  1553. },
  1554. Result: &ast.StringLiteral{Val: "high"},
  1555. },
  1556. },
  1557. ElseClause: &ast.StringLiteral{Val: "low"},
  1558. },
  1559. Name: "",
  1560. AName: "label",
  1561. }, {
  1562. Expr: &ast.FieldRef{Name: "humidity", StreamName: ast.DefaultStream},
  1563. Name: "humidity",
  1564. AName: "",
  1565. },
  1566. },
  1567. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1568. },
  1569. }, {
  1570. s: "SELECT CASE WHEN 30 THEN \"high\" ELSE \"low\" END as label, humidity FROM tbl",
  1571. stmt: nil,
  1572. err: "invalid CASE expression, WHEN expression must be a bool condition",
  1573. }, {
  1574. s: `SELECT count(*)-10 FROM demo`,
  1575. stmt: &ast.SelectStatement{
  1576. Fields: []ast.Field{
  1577. {
  1578. AName: "",
  1579. Name: "kuiper_field_0",
  1580. Expr: &ast.BinaryExpr{
  1581. OP: ast.SUB,
  1582. LHS: &ast.Call{
  1583. Name: "count",
  1584. Args: []ast.Expr{
  1585. &ast.Wildcard{Token: ast.ASTERISK},
  1586. },
  1587. FuncType: ast.FuncTypeAgg,
  1588. },
  1589. RHS: &ast.IntegerLiteral{Val: 10},
  1590. },
  1591. },
  1592. },
  1593. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  1594. },
  1595. },
  1596. {
  1597. s: `SELECT -abc FROM demo`,
  1598. stmt: nil,
  1599. err: "found \"-\", expected expression.",
  1600. },
  1601. {
  1602. s: `SELECT meta(*) FROM tbl`,
  1603. stmt: &ast.SelectStatement{
  1604. Fields: []ast.Field{
  1605. {
  1606. AName: "",
  1607. Name: "meta",
  1608. Expr: &ast.Call{
  1609. Name: "meta",
  1610. Args: []ast.Expr{
  1611. &ast.MetaRef{
  1612. Name: "*",
  1613. StreamName: ast.DefaultStream,
  1614. },
  1615. },
  1616. },
  1617. },
  1618. },
  1619. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1620. },
  1621. }, {
  1622. s: `SELECT changed_cols("",true,a,b,c) FROM tbl`,
  1623. stmt: &ast.SelectStatement{
  1624. Fields: []ast.Field{
  1625. {
  1626. AName: "",
  1627. Name: "changed_cols",
  1628. Expr: &ast.Call{
  1629. Name: "changed_cols",
  1630. Args: []ast.Expr{
  1631. &ast.ColFuncField{
  1632. Name: "",
  1633. Expr: &ast.StringLiteral{Val: ""},
  1634. },
  1635. &ast.ColFuncField{
  1636. Name: "",
  1637. Expr: &ast.BooleanLiteral{Val: true},
  1638. },
  1639. &ast.ColFuncField{Name: "a", Expr: &ast.FieldRef{
  1640. StreamName: ast.DefaultStream,
  1641. Name: "a",
  1642. }},
  1643. &ast.ColFuncField{Name: "b", Expr: &ast.FieldRef{
  1644. StreamName: ast.DefaultStream,
  1645. Name: "b",
  1646. }},
  1647. &ast.ColFuncField{Name: "c", Expr: &ast.FieldRef{
  1648. StreamName: ast.DefaultStream,
  1649. Name: "c",
  1650. }},
  1651. },
  1652. FuncType: ast.FuncTypeCols,
  1653. },
  1654. },
  1655. },
  1656. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1657. },
  1658. }, {
  1659. s: `SELECT changed_cols("",true,a,*,c) FROM tbl`,
  1660. stmt: &ast.SelectStatement{
  1661. Fields: []ast.Field{
  1662. {
  1663. AName: "",
  1664. Name: "changed_cols",
  1665. Expr: &ast.Call{
  1666. Name: "changed_cols",
  1667. Args: []ast.Expr{
  1668. &ast.ColFuncField{
  1669. Name: "",
  1670. Expr: &ast.StringLiteral{Val: ""},
  1671. },
  1672. &ast.ColFuncField{
  1673. Name: "",
  1674. Expr: &ast.BooleanLiteral{Val: true},
  1675. },
  1676. &ast.ColFuncField{Name: "a", Expr: &ast.FieldRef{
  1677. StreamName: ast.DefaultStream,
  1678. Name: "a",
  1679. }},
  1680. &ast.ColFuncField{Name: "*", Expr: &ast.Wildcard{
  1681. Token: ast.ASTERISK,
  1682. }},
  1683. &ast.ColFuncField{Name: "c", Expr: &ast.FieldRef{
  1684. StreamName: ast.DefaultStream,
  1685. Name: "c",
  1686. }},
  1687. },
  1688. FuncType: ast.FuncTypeCols,
  1689. },
  1690. },
  1691. },
  1692. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1693. },
  1694. }, {
  1695. s: `SELECT a FROM tbl WHERE changed_cols("",true,a,b,c) > 3`,
  1696. err: "function changed_cols can only be used inside the select clause",
  1697. },
  1698. {
  1699. s: `SELECT ".*(/)(?!.*\1)" FROM topic/sensor1 AS t1`,
  1700. err: `found "invalid string: \".*(/)(?!.*\\1)\"", expected expression.`,
  1701. },
  1702. {
  1703. s: `SELECT name FROM tbl WHERE name IN ("A", "B","C")`,
  1704. stmt: &ast.SelectStatement{
  1705. Fields: []ast.Field{
  1706. {
  1707. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  1708. Name: "name",
  1709. AName: ""},
  1710. },
  1711. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1712. 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"}}}},
  1713. },
  1714. },
  1715. {
  1716. s: `SELECT name FROM tbl WHERE name IN ()`,
  1717. err: `expect elements for IN expression, but found ")", expected expression.`,
  1718. },
  1719. {
  1720. s: `SELECT name FROM tbl WHERE name IN (abc,def OR name in (abc)`,
  1721. err: `expect ) for IN expression, but got "EOF"`,
  1722. },
  1723. {
  1724. s: `SELECT lag(name) OVER (PARTITION BY device) FROM tbl`,
  1725. stmt: &ast.SelectStatement{
  1726. Fields: []ast.Field{
  1727. {
  1728. Expr: &ast.Call{
  1729. Name: "lag",
  1730. FuncId: 0,
  1731. Args: []ast.Expr{
  1732. &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  1733. },
  1734. Partition: &ast.PartitionExpr{
  1735. Exprs: []ast.Expr{
  1736. &ast.FieldRef{Name: "device", StreamName: ast.DefaultStream},
  1737. },
  1738. },
  1739. },
  1740. Name: "lag",
  1741. AName: ""},
  1742. },
  1743. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1744. },
  1745. },
  1746. {
  1747. s: `SELECT name OVER (PARTITION BY device) FROM tbl`,
  1748. err: `found "OVER", expected FROM.`,
  1749. },
  1750. {
  1751. s: `SELECT avg(name) OVER (PARTITION BY device) FROM tbl`,
  1752. err: `Found OVER after non analytic function avg`,
  1753. },
  1754. {
  1755. s: `SELECT name FROM tbl WHERE lag(name) OVER (PARTITION BY device, groupName) > 3`,
  1756. stmt: &ast.SelectStatement{
  1757. Fields: []ast.Field{
  1758. {
  1759. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  1760. Name: "name",
  1761. AName: ""},
  1762. },
  1763. Condition: &ast.BinaryExpr{
  1764. LHS: &ast.Call{
  1765. Name: "lag",
  1766. FuncId: 0,
  1767. Args: []ast.Expr{
  1768. &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  1769. },
  1770. Partition: &ast.PartitionExpr{
  1771. Exprs: []ast.Expr{
  1772. &ast.FieldRef{Name: "device", StreamName: ast.DefaultStream},
  1773. &ast.FieldRef{Name: "groupName", StreamName: ast.DefaultStream},
  1774. },
  1775. },
  1776. },
  1777. OP: ast.GT,
  1778. RHS: &ast.IntegerLiteral{Val: 3},
  1779. },
  1780. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1781. },
  1782. },
  1783. {
  1784. s: `SELECT lag(name) OVER (PARTITION BY device) as ll FROM tbl`,
  1785. stmt: &ast.SelectStatement{
  1786. Fields: []ast.Field{
  1787. {
  1788. Expr: &ast.Call{
  1789. Name: "lag",
  1790. FuncId: 0,
  1791. Args: []ast.Expr{
  1792. &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  1793. },
  1794. Partition: &ast.PartitionExpr{
  1795. Exprs: []ast.Expr{
  1796. &ast.FieldRef{Name: "device", StreamName: ast.DefaultStream},
  1797. },
  1798. },
  1799. },
  1800. Name: "lag",
  1801. AName: "ll"},
  1802. },
  1803. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1804. },
  1805. },
  1806. {
  1807. s: `SELECT lag(name) OVER (PARTITION BY device WHEN abc > 12) as ll FROM tbl`,
  1808. stmt: &ast.SelectStatement{
  1809. Fields: []ast.Field{
  1810. {
  1811. Expr: &ast.Call{
  1812. Name: "lag",
  1813. FuncId: 0,
  1814. Args: []ast.Expr{
  1815. &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  1816. },
  1817. Partition: &ast.PartitionExpr{
  1818. Exprs: []ast.Expr{
  1819. &ast.FieldRef{Name: "device", StreamName: ast.DefaultStream},
  1820. },
  1821. },
  1822. WhenExpr: &ast.BinaryExpr{
  1823. LHS: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream},
  1824. OP: ast.GT,
  1825. RHS: &ast.IntegerLiteral{Val: 12},
  1826. },
  1827. },
  1828. Name: "lag",
  1829. AName: "ll"},
  1830. },
  1831. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1832. },
  1833. },
  1834. {
  1835. s: `SELECT lag(name) OVER (PARTITION BY device WHEN had_changed(true, StatusCode)) as ll FROM tbl`,
  1836. stmt: &ast.SelectStatement{
  1837. Fields: []ast.Field{
  1838. {
  1839. Expr: &ast.Call{
  1840. Name: "lag",
  1841. FuncId: 0,
  1842. Args: []ast.Expr{
  1843. &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  1844. },
  1845. Partition: &ast.PartitionExpr{
  1846. Exprs: []ast.Expr{
  1847. &ast.FieldRef{Name: "device", StreamName: ast.DefaultStream},
  1848. },
  1849. },
  1850. WhenExpr: &ast.Call{
  1851. Name: "had_changed",
  1852. FuncId: 1,
  1853. Args: []ast.Expr{&ast.BooleanLiteral{Val: true}, &ast.FieldRef{Name: "StatusCode", StreamName: ast.DefaultStream}},
  1854. },
  1855. },
  1856. Name: "lag",
  1857. AName: "ll"},
  1858. },
  1859. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1860. },
  1861. },
  1862. {
  1863. s: `SELECT lag(name) OVER (WHEN had_changed(true, StatusCode)) as ll FROM tbl`,
  1864. stmt: &ast.SelectStatement{
  1865. Fields: []ast.Field{
  1866. {
  1867. Expr: &ast.Call{
  1868. Name: "lag",
  1869. FuncId: 0,
  1870. Args: []ast.Expr{
  1871. &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  1872. },
  1873. WhenExpr: &ast.Call{
  1874. Name: "had_changed",
  1875. FuncId: 1,
  1876. Args: []ast.Expr{&ast.BooleanLiteral{Val: true}, &ast.FieldRef{Name: "StatusCode", StreamName: ast.DefaultStream}},
  1877. },
  1878. },
  1879. Name: "lag",
  1880. AName: "ll"},
  1881. },
  1882. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1883. },
  1884. },
  1885. {
  1886. s: `SELECT name OVER (WHEN a > b) FROM tbl`,
  1887. err: `found "OVER", expected FROM.`,
  1888. },
  1889. {
  1890. s: `SELECT avg(name) OVER (WHEN a > b) FROM tbl`,
  1891. err: `Found OVER after non analytic function avg`,
  1892. },
  1893. {
  1894. s: `SELECT *, name, lower(name) as ln FROM tbl`,
  1895. stmt: &ast.SelectStatement{
  1896. Fields: []ast.Field{
  1897. {
  1898. Expr: &ast.Wildcard{
  1899. Token: ast.ASTERISK,
  1900. },
  1901. Name: "*",
  1902. },
  1903. {
  1904. Expr: &ast.FieldRef{
  1905. Name: "name",
  1906. StreamName: ast.DefaultStream,
  1907. },
  1908. Name: "name",
  1909. AName: "",
  1910. },
  1911. {
  1912. Expr: &ast.Call{
  1913. Name: "lower",
  1914. FuncId: 0,
  1915. Args: []ast.Expr{
  1916. &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  1917. },
  1918. },
  1919. Name: "lower",
  1920. AName: "ln",
  1921. },
  1922. },
  1923. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1924. },
  1925. },
  1926. {
  1927. s: `SELECT name, * FROM tbl`,
  1928. stmt: &ast.SelectStatement{
  1929. Fields: []ast.Field{
  1930. {
  1931. Expr: &ast.FieldRef{
  1932. Name: "name",
  1933. StreamName: ast.DefaultStream,
  1934. },
  1935. Name: "name",
  1936. AName: "",
  1937. },
  1938. {
  1939. Expr: &ast.Wildcard{
  1940. Token: ast.ASTERISK,
  1941. },
  1942. Name: "*",
  1943. },
  1944. },
  1945. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1946. },
  1947. },
  1948. {
  1949. s: `SELECT name, * FROM tbl`,
  1950. stmt: &ast.SelectStatement{
  1951. Fields: []ast.Field{
  1952. {
  1953. Expr: &ast.FieldRef{
  1954. Name: "name",
  1955. StreamName: ast.DefaultStream,
  1956. },
  1957. Name: "name",
  1958. AName: "",
  1959. },
  1960. {
  1961. Expr: &ast.Wildcard{
  1962. Token: ast.ASTERISK,
  1963. },
  1964. Name: "*",
  1965. },
  1966. },
  1967. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1968. },
  1969. },
  1970. }
  1971. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  1972. for i, tt := range tests {
  1973. //fmt.Printf("Parsing SQL %q.\n", tt.s)
  1974. stmt, err := NewParser(strings.NewReader(tt.s)).Parse()
  1975. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  1976. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.s, tt.err, err)
  1977. } else if tt.err == "" && !reflect.DeepEqual(tt.stmt, stmt) {
  1978. t.Errorf("%d. %q\n\nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.s, tt.stmt, stmt)
  1979. }
  1980. }
  1981. }
  1982. func TestParser_ParseWindowsExpr(t *testing.T) {
  1983. var tests = []struct {
  1984. s string
  1985. stmt *ast.SelectStatement
  1986. err string
  1987. }{
  1988. {
  1989. s: `SELECT f1 FROM tbl GROUP BY TUMBLINGWINDOW(ss, 10)`,
  1990. stmt: &ast.SelectStatement{
  1991. Fields: []ast.Field{
  1992. {
  1993. Expr: &ast.FieldRef{Name: "f1", StreamName: ast.DefaultStream},
  1994. Name: "f1",
  1995. AName: ""},
  1996. },
  1997. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1998. Dimensions: ast.Dimensions{
  1999. ast.Dimension{
  2000. Expr: &ast.Window{
  2001. WindowType: ast.TUMBLING_WINDOW,
  2002. Length: &ast.IntegerLiteral{Val: 10000},
  2003. Interval: &ast.IntegerLiteral{Val: 0},
  2004. },
  2005. },
  2006. },
  2007. },
  2008. },
  2009. {
  2010. s: `SELECT f1 FROM tbl GROUP BY HOPPINGWINDOW(mi, 5, 1)`,
  2011. stmt: &ast.SelectStatement{
  2012. Fields: []ast.Field{
  2013. {
  2014. Expr: &ast.FieldRef{Name: "f1", StreamName: ast.DefaultStream},
  2015. Name: "f1",
  2016. AName: ""},
  2017. },
  2018. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  2019. Dimensions: ast.Dimensions{
  2020. ast.Dimension{
  2021. Expr: &ast.Window{
  2022. WindowType: ast.HOPPING_WINDOW,
  2023. Length: &ast.IntegerLiteral{Val: 3e5},
  2024. Interval: &ast.IntegerLiteral{Val: 6e4},
  2025. },
  2026. },
  2027. },
  2028. },
  2029. },
  2030. {
  2031. s: `SELECT f1 FROM tbl GROUP BY SESSIONWINDOW(hh, 5, 1)`,
  2032. stmt: &ast.SelectStatement{
  2033. Fields: []ast.Field{
  2034. {
  2035. Expr: &ast.FieldRef{Name: "f1", StreamName: ast.DefaultStream},
  2036. Name: "f1",
  2037. AName: ""},
  2038. },
  2039. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  2040. Dimensions: ast.Dimensions{
  2041. ast.Dimension{
  2042. Expr: &ast.Window{
  2043. WindowType: ast.SESSION_WINDOW,
  2044. Length: &ast.IntegerLiteral{Val: 1.8e7},
  2045. Interval: &ast.IntegerLiteral{Val: 3.6e6},
  2046. },
  2047. },
  2048. },
  2049. },
  2050. },
  2051. {
  2052. s: `SELECT f1 FROM tbl GROUP BY SLIDINGWINDOW(ms, 5)`,
  2053. stmt: &ast.SelectStatement{
  2054. Fields: []ast.Field{
  2055. {
  2056. Expr: &ast.FieldRef{Name: "f1", StreamName: ast.DefaultStream},
  2057. Name: "f1",
  2058. AName: ""},
  2059. },
  2060. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  2061. Dimensions: ast.Dimensions{
  2062. ast.Dimension{
  2063. Expr: &ast.Window{
  2064. WindowType: ast.SLIDING_WINDOW,
  2065. Length: &ast.IntegerLiteral{Val: 5},
  2066. Interval: &ast.IntegerLiteral{Val: 0},
  2067. },
  2068. },
  2069. },
  2070. },
  2071. },
  2072. {
  2073. s: `SELECT f1 FROM tbl GROUP BY SLIDINGWINDOW(mi, 5, 1)`,
  2074. stmt: nil,
  2075. err: "The arguments for slidingwindow should be 2.\n",
  2076. },
  2077. {
  2078. s: `SELECT f1 FROM tbl GROUP BY SLIDINGWINDOW("mi", 5)`,
  2079. stmt: nil,
  2080. err: "The 1st argument for slidingwindow is expecting timer literal expression. One value of [dd|hh|mi|ss|ms].\n",
  2081. },
  2082. {
  2083. s: `SELECT f1 FROM tbl GROUP BY COUNTWINDOW(10)`,
  2084. stmt: &ast.SelectStatement{
  2085. Fields: []ast.Field{
  2086. {
  2087. Expr: &ast.FieldRef{Name: "f1", StreamName: ast.DefaultStream},
  2088. Name: "f1",
  2089. AName: ""},
  2090. },
  2091. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  2092. Dimensions: ast.Dimensions{
  2093. ast.Dimension{
  2094. Expr: &ast.Window{
  2095. WindowType: ast.COUNT_WINDOW,
  2096. Length: &ast.IntegerLiteral{Val: 10},
  2097. },
  2098. },
  2099. },
  2100. },
  2101. },
  2102. {
  2103. s: `SELECT f1 FROM tbl GROUP BY COUNTWINDOW(10, 5)`,
  2104. stmt: &ast.SelectStatement{
  2105. Fields: []ast.Field{
  2106. {
  2107. Expr: &ast.FieldRef{Name: "f1", StreamName: ast.DefaultStream},
  2108. Name: "f1",
  2109. AName: ""},
  2110. },
  2111. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  2112. Dimensions: ast.Dimensions{
  2113. ast.Dimension{
  2114. Expr: &ast.Window{
  2115. WindowType: ast.COUNT_WINDOW,
  2116. Length: &ast.IntegerLiteral{Val: 10},
  2117. Interval: &ast.IntegerLiteral{Val: 5},
  2118. },
  2119. },
  2120. },
  2121. },
  2122. },
  2123. {
  2124. s: `SELECT f1 FROM tbl GROUP BY COUNTWINDOW(3, 5)`,
  2125. stmt: nil,
  2126. err: "The second parameter value 5 should be less than the first parameter 3.",
  2127. },
  2128. {
  2129. s: `SELECT * FROM demo GROUP BY COUNTWINDOW(3,1) FILTER( where revenue > 100 )`,
  2130. stmt: &ast.SelectStatement{
  2131. Fields: []ast.Field{
  2132. {
  2133. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  2134. Name: "*",
  2135. AName: ""},
  2136. },
  2137. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2138. Dimensions: ast.Dimensions{
  2139. ast.Dimension{
  2140. Expr: &ast.Window{
  2141. WindowType: ast.COUNT_WINDOW,
  2142. Length: &ast.IntegerLiteral{Val: 3},
  2143. Interval: &ast.IntegerLiteral{Val: 1},
  2144. Filter: &ast.BinaryExpr{
  2145. LHS: &ast.FieldRef{Name: "revenue", StreamName: ast.DefaultStream},
  2146. OP: ast.GT,
  2147. RHS: &ast.IntegerLiteral{Val: 100},
  2148. },
  2149. },
  2150. },
  2151. },
  2152. },
  2153. },
  2154. {
  2155. s: `SELECT * FROM demo GROUP BY department, COUNTWINDOW(3,1) FILTER( where revenue > 100 ), year`,
  2156. stmt: &ast.SelectStatement{
  2157. Fields: []ast.Field{
  2158. {
  2159. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  2160. Name: "*",
  2161. AName: ""},
  2162. },
  2163. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2164. Dimensions: ast.Dimensions{
  2165. ast.Dimension{Expr: &ast.FieldRef{Name: "department", StreamName: ast.DefaultStream}},
  2166. ast.Dimension{
  2167. Expr: &ast.Window{
  2168. WindowType: ast.COUNT_WINDOW,
  2169. Length: &ast.IntegerLiteral{Val: 3},
  2170. Interval: &ast.IntegerLiteral{Val: 1},
  2171. Filter: &ast.BinaryExpr{
  2172. LHS: &ast.FieldRef{Name: "revenue", StreamName: ast.DefaultStream},
  2173. OP: ast.GT,
  2174. RHS: &ast.IntegerLiteral{Val: 100},
  2175. },
  2176. },
  2177. },
  2178. ast.Dimension{Expr: &ast.FieldRef{Name: "year", StreamName: ast.DefaultStream}},
  2179. },
  2180. },
  2181. },
  2182. {
  2183. s: `SELECT * FROM demo GROUP BY department, COUNTWINDOW(3,1) FILTER( where revenue IN (100, 200)), year`,
  2184. stmt: &ast.SelectStatement{
  2185. Fields: []ast.Field{
  2186. {
  2187. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  2188. Name: "*",
  2189. AName: ""},
  2190. },
  2191. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2192. Dimensions: ast.Dimensions{
  2193. ast.Dimension{Expr: &ast.FieldRef{Name: "department", StreamName: ast.DefaultStream}},
  2194. ast.Dimension{
  2195. Expr: &ast.Window{
  2196. WindowType: ast.COUNT_WINDOW,
  2197. Length: &ast.IntegerLiteral{Val: 3},
  2198. Interval: &ast.IntegerLiteral{Val: 1},
  2199. Filter: &ast.BinaryExpr{
  2200. LHS: &ast.FieldRef{Name: "revenue", StreamName: ast.DefaultStream},
  2201. OP: ast.IN,
  2202. RHS: &ast.ValueSetExpr{
  2203. LiteralExprs: []ast.Expr{&ast.IntegerLiteral{Val: 100}, &ast.IntegerLiteral{Val: 200}},
  2204. },
  2205. },
  2206. },
  2207. },
  2208. ast.Dimension{Expr: &ast.FieldRef{Name: "year", StreamName: ast.DefaultStream}},
  2209. },
  2210. },
  2211. },
  2212. //to be supported
  2213. {
  2214. s: `SELECT sum(f1) FILTER( where revenue > 100 ) FROM tbl GROUP BY year`,
  2215. stmt: nil,
  2216. err: "found \"FILTER\", expected FROM.",
  2217. },
  2218. {
  2219. s: `SELECT * FROM demo GROUP BY COUNTWINDOW(3,1) FILTER where revenue > 100`,
  2220. stmt: nil,
  2221. err: "Found \"WHERE\" after FILTER, expect parentheses.",
  2222. },
  2223. {
  2224. s: `SELECT * FROM demo GROUP BY COUNTWINDOW(3,1) where revenue > 100`,
  2225. stmt: nil,
  2226. err: "found \"WHERE\", expected EOF.",
  2227. },
  2228. }
  2229. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  2230. for i, tt := range tests {
  2231. //fmt.Printf("Parsing SQL %q.\n", tt.s)
  2232. stmt, err := NewParser(strings.NewReader(tt.s)).Parse()
  2233. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  2234. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.s, tt.err, err)
  2235. } else if tt.err == "" && !reflect.DeepEqual(tt.stmt, stmt) {
  2236. t.Errorf("%d. %q\n\nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.s, tt.stmt, stmt)
  2237. }
  2238. }
  2239. }
  2240. func TestParser_ParseJsonExpr(t *testing.T) {
  2241. var tests = []struct {
  2242. s string
  2243. stmt *ast.SelectStatement
  2244. err string
  2245. }{
  2246. {
  2247. s: `SELECT children[0] FROM demo`,
  2248. stmt: &ast.SelectStatement{
  2249. Fields: []ast.Field{
  2250. {
  2251. Expr: &ast.BinaryExpr{
  2252. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  2253. OP: ast.SUBSET,
  2254. RHS: &ast.IndexExpr{Index: &ast.IntegerLiteral{Val: 0}},
  2255. },
  2256. Name: "kuiper_field_0",
  2257. AName: ""},
  2258. },
  2259. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2260. },
  2261. },
  2262. {
  2263. s: `SELECT children[0]->first FROM demo`,
  2264. stmt: &ast.SelectStatement{
  2265. Fields: []ast.Field{
  2266. {
  2267. Expr: &ast.BinaryExpr{
  2268. LHS: &ast.BinaryExpr{
  2269. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  2270. OP: ast.SUBSET,
  2271. RHS: &ast.IndexExpr{Index: &ast.IntegerLiteral{Val: 0}},
  2272. },
  2273. OP: ast.ARROW,
  2274. RHS: &ast.JsonFieldRef{Name: "first"},
  2275. },
  2276. Name: "kuiper_field_0",
  2277. AName: ""},
  2278. },
  2279. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2280. },
  2281. },
  2282. {
  2283. s: `SELECT children->first[2] FROM demo`,
  2284. stmt: &ast.SelectStatement{
  2285. Fields: []ast.Field{
  2286. {
  2287. Expr: &ast.BinaryExpr{
  2288. LHS: &ast.BinaryExpr{
  2289. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  2290. OP: ast.ARROW,
  2291. RHS: &ast.JsonFieldRef{Name: "first"},
  2292. },
  2293. OP: ast.SUBSET,
  2294. RHS: &ast.IndexExpr{Index: &ast.IntegerLiteral{Val: 2}},
  2295. },
  2296. Name: "kuiper_field_0",
  2297. AName: ""},
  2298. },
  2299. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2300. },
  2301. },
  2302. {
  2303. s: `SELECT children->first[2]->test FROM demo`,
  2304. stmt: &ast.SelectStatement{
  2305. Fields: []ast.Field{
  2306. {
  2307. Expr: &ast.BinaryExpr{
  2308. LHS: &ast.BinaryExpr{
  2309. LHS: &ast.BinaryExpr{
  2310. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  2311. OP: ast.ARROW,
  2312. RHS: &ast.JsonFieldRef{Name: "first"},
  2313. },
  2314. OP: ast.SUBSET,
  2315. RHS: &ast.IndexExpr{Index: &ast.IntegerLiteral{Val: 2}},
  2316. },
  2317. OP: ast.ARROW,
  2318. RHS: &ast.JsonFieldRef{Name: "test"},
  2319. },
  2320. Name: "kuiper_field_0",
  2321. AName: ""},
  2322. },
  2323. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2324. },
  2325. },
  2326. {
  2327. s: `SELECT children[0: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[:1] 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: 1}},
  2351. },
  2352. Name: "kuiper_field_0",
  2353. AName: ""},
  2354. },
  2355. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2356. },
  2357. },
  2358. {
  2359. s: `SELECT children[:] 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: 0}, End: &ast.IntegerLiteral{Val: math.MinInt32}},
  2367. },
  2368. Name: "kuiper_field_0",
  2369. AName: ""},
  2370. },
  2371. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2372. },
  2373. },
  2374. {
  2375. s: `SELECT children[2:] AS c FROM demo`,
  2376. stmt: &ast.SelectStatement{
  2377. Fields: []ast.Field{
  2378. {
  2379. Expr: &ast.BinaryExpr{
  2380. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  2381. OP: ast.SUBSET,
  2382. RHS: &ast.ColonExpr{Start: &ast.IntegerLiteral{Val: 2}, End: &ast.IntegerLiteral{Val: math.MinInt32}},
  2383. },
  2384. Name: "",
  2385. AName: "c"},
  2386. },
  2387. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2388. },
  2389. },
  2390. {
  2391. s: `SELECT children[2:]->first AS c FROM demo`,
  2392. stmt: &ast.SelectStatement{
  2393. Fields: []ast.Field{
  2394. {
  2395. Expr: &ast.BinaryExpr{
  2396. 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}}},
  2397. OP: ast.ARROW,
  2398. RHS: &ast.JsonFieldRef{Name: "first"},
  2399. },
  2400. Name: "",
  2401. AName: "c"},
  2402. },
  2403. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2404. },
  2405. },
  2406. {
  2407. s: `SELECT demo.* FROM demo`,
  2408. stmt: &ast.SelectStatement{
  2409. Fields: []ast.Field{
  2410. {
  2411. Expr: &ast.FieldRef{StreamName: ast.StreamName("demo"), Name: "*"},
  2412. Name: "*",
  2413. AName: ""},
  2414. },
  2415. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2416. },
  2417. },
  2418. {
  2419. s: `SELECT demo.children[2:]->first AS c FROM demo`,
  2420. stmt: &ast.SelectStatement{
  2421. Fields: []ast.Field{
  2422. {
  2423. Expr: &ast.BinaryExpr{
  2424. 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}}},
  2425. OP: ast.ARROW,
  2426. RHS: &ast.JsonFieldRef{Name: "first"},
  2427. },
  2428. Name: "",
  2429. AName: "c"},
  2430. },
  2431. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2432. },
  2433. },
  2434. {
  2435. s: `SELECT lower(demo.children[2:]->first) AS c FROM demo`,
  2436. stmt: &ast.SelectStatement{
  2437. Fields: []ast.Field{
  2438. {
  2439. Expr: &ast.Call{
  2440. Name: "lower",
  2441. Args: []ast.Expr{
  2442. &ast.BinaryExpr{
  2443. 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}}},
  2444. OP: ast.ARROW,
  2445. RHS: &ast.JsonFieldRef{Name: "first"},
  2446. },
  2447. },
  2448. },
  2449. Name: "lower",
  2450. AName: "c"},
  2451. },
  2452. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2453. },
  2454. },
  2455. {
  2456. s: `SELECT children[:1] FROM demo WHERE abc[0] > 12`,
  2457. stmt: &ast.SelectStatement{
  2458. Fields: []ast.Field{
  2459. {
  2460. Expr: &ast.BinaryExpr{
  2461. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  2462. OP: ast.SUBSET,
  2463. RHS: &ast.ColonExpr{Start: &ast.IntegerLiteral{Val: 0}, End: &ast.IntegerLiteral{Val: 1}},
  2464. },
  2465. Name: "kuiper_field_0",
  2466. AName: ""},
  2467. },
  2468. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2469. Condition: &ast.BinaryExpr{
  2470. LHS: &ast.BinaryExpr{
  2471. LHS: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream},
  2472. OP: ast.SUBSET,
  2473. RHS: &ast.IndexExpr{Index: &ast.IntegerLiteral{Val: 0}},
  2474. },
  2475. OP: ast.GT,
  2476. RHS: &ast.IntegerLiteral{Val: 12},
  2477. },
  2478. },
  2479. },
  2480. {
  2481. s: `SELECT children[:1] FROM demo WHERE abc[0] IN demo.children[2:]->first`,
  2482. stmt: &ast.SelectStatement{
  2483. Fields: []ast.Field{
  2484. {
  2485. Expr: &ast.BinaryExpr{
  2486. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  2487. OP: ast.SUBSET,
  2488. RHS: &ast.ColonExpr{Start: &ast.IntegerLiteral{Val: 0}, End: &ast.IntegerLiteral{Val: 1}},
  2489. },
  2490. Name: "kuiper_field_0",
  2491. AName: ""},
  2492. },
  2493. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2494. Condition: &ast.BinaryExpr{
  2495. LHS: &ast.BinaryExpr{
  2496. LHS: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream},
  2497. OP: ast.SUBSET,
  2498. RHS: &ast.IndexExpr{Index: &ast.IntegerLiteral{Val: 0}},
  2499. },
  2500. OP: ast.IN,
  2501. RHS: &ast.BinaryExpr{
  2502. 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}}},
  2503. OP: ast.ARROW,
  2504. RHS: &ast.JsonFieldRef{Name: "first"},
  2505. },
  2506. },
  2507. },
  2508. },
  2509. {
  2510. s: `SELECT demo.children.first AS c FROM demo`,
  2511. stmt: nil,
  2512. err: "Too many field names. Please use -> to reference keys in struct.\n",
  2513. },
  2514. {
  2515. s: `SELECT children[index] FROM demo`,
  2516. stmt: &ast.SelectStatement{
  2517. Fields: []ast.Field{
  2518. {
  2519. Expr: &ast.BinaryExpr{
  2520. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  2521. OP: ast.SUBSET,
  2522. RHS: &ast.IndexExpr{Index: &ast.FieldRef{Name: "index", StreamName: ast.DefaultStream}},
  2523. },
  2524. Name: "kuiper_field_0",
  2525. AName: ""},
  2526. },
  2527. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2528. },
  2529. },
  2530. }
  2531. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  2532. for i, tt := range tests {
  2533. stmt, err := NewParser(strings.NewReader(tt.s)).Parse()
  2534. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  2535. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.s, tt.err, err)
  2536. } else if tt.err == "" && !reflect.DeepEqual(tt.stmt, stmt) {
  2537. t.Errorf("%d. %q\n\nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.s, tt.stmt, stmt)
  2538. }
  2539. }
  2540. }
  2541. func TestParser_ParseJoins(t *testing.T) {
  2542. var tests = []struct {
  2543. s string
  2544. stmt *ast.SelectStatement
  2545. err string
  2546. }{
  2547. {
  2548. s: `SELECT * FROM topic/sensor1 LEFT JOIN topic1 ON f=k`,
  2549. stmt: &ast.SelectStatement{
  2550. Fields: []ast.Field{
  2551. {
  2552. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  2553. Name: "*",
  2554. AName: ""},
  2555. },
  2556. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  2557. Joins: []ast.Join{
  2558. {
  2559. Name: "topic1", Alias: "", JoinType: ast.LEFT_JOIN, Expr: &ast.BinaryExpr{
  2560. LHS: &ast.FieldRef{Name: "f", StreamName: ast.DefaultStream},
  2561. OP: ast.EQ,
  2562. RHS: &ast.FieldRef{Name: "k", StreamName: ast.DefaultStream},
  2563. },
  2564. },
  2565. },
  2566. },
  2567. },
  2568. {
  2569. s: `SELECT * FROM topic/sensor1 AS t1 INNER JOIN topic1 AS t2 ON f=k`,
  2570. stmt: &ast.SelectStatement{
  2571. Fields: []ast.Field{
  2572. {
  2573. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  2574. Name: "*",
  2575. AName: ""},
  2576. },
  2577. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
  2578. Joins: []ast.Join{
  2579. {
  2580. Name: "topic1", Alias: "t2", JoinType: ast.INNER_JOIN, Expr: &ast.BinaryExpr{
  2581. LHS: &ast.FieldRef{Name: "f", StreamName: ast.DefaultStream},
  2582. OP: ast.EQ,
  2583. RHS: &ast.FieldRef{Name: "k", StreamName: ast.DefaultStream},
  2584. },
  2585. },
  2586. },
  2587. },
  2588. },
  2589. {
  2590. s: `SELECT * FROM topic/sensor1 AS t1 LEFT JOIN topic1/sensor2 AS t2 ON f=k`,
  2591. stmt: &ast.SelectStatement{
  2592. Fields: []ast.Field{
  2593. {
  2594. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  2595. Name: "*",
  2596. AName: ""},
  2597. },
  2598. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
  2599. Joins: []ast.Join{
  2600. {
  2601. Name: "topic1/sensor2", Alias: "t2", JoinType: ast.LEFT_JOIN, Expr: &ast.BinaryExpr{
  2602. LHS: &ast.FieldRef{Name: "f", StreamName: ast.DefaultStream},
  2603. OP: ast.EQ,
  2604. RHS: &ast.FieldRef{Name: "k", StreamName: ast.DefaultStream},
  2605. },
  2606. },
  2607. },
  2608. },
  2609. },
  2610. {
  2611. s: `SELECT t1.name FROM topic/sensor1 AS t1 LEFT JOIN topic1/sensor2 AS t2 ON f=k`,
  2612. stmt: &ast.SelectStatement{
  2613. Fields: []ast.Field{
  2614. {
  2615. Expr: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "name"},
  2616. Name: "name",
  2617. AName: ""},
  2618. },
  2619. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
  2620. Joins: []ast.Join{
  2621. {
  2622. Name: "topic1/sensor2", Alias: "t2", JoinType: ast.LEFT_JOIN, Expr: &ast.BinaryExpr{
  2623. LHS: &ast.FieldRef{Name: "f", StreamName: ast.DefaultStream},
  2624. OP: ast.EQ,
  2625. RHS: &ast.FieldRef{Name: "k", StreamName: ast.DefaultStream},
  2626. },
  2627. },
  2628. },
  2629. },
  2630. },
  2631. {
  2632. s: `SELECT t1.name FROM topic/sensor1 AS t1 LEFT JOIN topic1/sensor2 AS t2 ON t1.f=t2.k`,
  2633. stmt: &ast.SelectStatement{
  2634. Fields: []ast.Field{
  2635. {
  2636. Expr: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "name"},
  2637. Name: "name",
  2638. AName: ""},
  2639. },
  2640. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
  2641. Joins: []ast.Join{
  2642. {
  2643. Name: "topic1/sensor2", Alias: "t2", JoinType: ast.LEFT_JOIN, Expr: &ast.BinaryExpr{
  2644. LHS: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "f"},
  2645. OP: ast.EQ,
  2646. RHS: &ast.FieldRef{StreamName: ast.StreamName("t2"), Name: "k"},
  2647. },
  2648. },
  2649. },
  2650. },
  2651. },
  2652. {
  2653. s: `SELECT t1.name FROM topic/sensor1 AS t1 RIGHT JOIN topic1/sensor2 AS t2 ON t1.f=t2.k`,
  2654. stmt: &ast.SelectStatement{
  2655. Fields: []ast.Field{
  2656. {
  2657. Expr: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "name"},
  2658. Name: "name",
  2659. AName: ""},
  2660. },
  2661. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
  2662. Joins: []ast.Join{
  2663. {
  2664. Name: "topic1/sensor2", Alias: "t2", JoinType: ast.RIGHT_JOIN, Expr: &ast.BinaryExpr{
  2665. LHS: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "f"},
  2666. OP: ast.EQ,
  2667. RHS: &ast.FieldRef{StreamName: ast.StreamName("t2"), Name: "k"},
  2668. },
  2669. },
  2670. },
  2671. },
  2672. },
  2673. {
  2674. s: `SELECT t1.name FROM topic/sensor1 AS t1 FULL JOIN topic1/sensor2 AS t2 ON t1.f=t2.k`,
  2675. stmt: &ast.SelectStatement{
  2676. Fields: []ast.Field{
  2677. {
  2678. Expr: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "name"},
  2679. Name: "name",
  2680. AName: ""},
  2681. },
  2682. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
  2683. Joins: []ast.Join{
  2684. {
  2685. Name: "topic1/sensor2", Alias: "t2", JoinType: ast.FULL_JOIN, Expr: &ast.BinaryExpr{
  2686. LHS: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "f"},
  2687. OP: ast.EQ,
  2688. RHS: &ast.FieldRef{StreamName: ast.StreamName("t2"), Name: "k"},
  2689. },
  2690. },
  2691. },
  2692. },
  2693. },
  2694. {
  2695. s: `SELECT t1.name FROM topic/sensor1 AS t1 CROSS JOIN topic1/sensor2 AS t2 ON t1.f=t2.k`,
  2696. stmt: nil,
  2697. err: "On expression is not required for cross join type.\n",
  2698. },
  2699. {
  2700. s: `SELECT t1.name FROM topic/sensor1 AS t1 CROSS JOIN topic1/sensor2 AS t2`,
  2701. stmt: &ast.SelectStatement{
  2702. Fields: []ast.Field{
  2703. {
  2704. Expr: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "name"},
  2705. Name: "name",
  2706. AName: ""},
  2707. },
  2708. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
  2709. Joins: []ast.Join{
  2710. {
  2711. Name: "topic1/sensor2", Alias: "t2", JoinType: ast.CROSS_JOIN, Expr: nil,
  2712. },
  2713. },
  2714. },
  2715. },
  2716. {
  2717. s: `SELECT demo.*, demo2.* FROM demo LEFT JOIN demo2 on demo.f1 = demo2.f2`,
  2718. stmt: &ast.SelectStatement{
  2719. Fields: []ast.Field{
  2720. {
  2721. Expr: &ast.FieldRef{StreamName: ast.StreamName("demo"), Name: "*"},
  2722. Name: "*",
  2723. AName: ""},
  2724. {
  2725. Expr: &ast.FieldRef{StreamName: ast.StreamName("demo2"), Name: "*"},
  2726. Name: "*",
  2727. AName: ""},
  2728. },
  2729. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2730. Joins: []ast.Join{
  2731. {
  2732. Name: "demo2", Alias: "", JoinType: ast.LEFT_JOIN, Expr: &ast.BinaryExpr{
  2733. LHS: &ast.FieldRef{StreamName: ast.StreamName("demo"), Name: "f1"},
  2734. OP: ast.EQ,
  2735. RHS: &ast.FieldRef{StreamName: ast.StreamName("demo2"), Name: "f2"},
  2736. },
  2737. },
  2738. },
  2739. },
  2740. },
  2741. }
  2742. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  2743. for i, tt := range tests {
  2744. stmt, err := NewParser(strings.NewReader(tt.s)).Parse()
  2745. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  2746. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.s, tt.err, err)
  2747. } else if tt.err == "" && !reflect.DeepEqual(tt.stmt, stmt) {
  2748. t.Errorf("%d. %q\n\nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.s, tt.stmt, stmt)
  2749. }
  2750. }
  2751. }
  2752. func TestParser_ParseStatements(t *testing.T) {
  2753. var tests = []struct {
  2754. s string
  2755. stmts []ast.SelectStatement
  2756. err string
  2757. }{
  2758. {s: "SELECT name FROM tbl;\nSELECT name FROM topic/sensor1\n",
  2759. stmts: []ast.SelectStatement{
  2760. {
  2761. Fields: []ast.Field{
  2762. {
  2763. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  2764. Name: "name",
  2765. AName: ""},
  2766. },
  2767. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  2768. },
  2769. {
  2770. Fields: []ast.Field{
  2771. {
  2772. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  2773. Name: "name",
  2774. AName: ""},
  2775. },
  2776. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  2777. },
  2778. },
  2779. },
  2780. {s: "SELECT name FROM tbl;\nSELECT name FROM topic/sensor1;\n--SELECT comment FROM topic/comment",
  2781. stmts: []ast.SelectStatement{
  2782. {
  2783. Fields: []ast.Field{
  2784. {
  2785. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  2786. Name: "name",
  2787. AName: ""},
  2788. },
  2789. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  2790. },
  2791. {
  2792. Fields: []ast.Field{
  2793. {
  2794. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  2795. Name: "name",
  2796. AName: ""},
  2797. },
  2798. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  2799. },
  2800. },
  2801. },
  2802. }
  2803. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  2804. for i, tt := range tests {
  2805. stmts, err := NewParser(strings.NewReader(tt.s)).ParseQueries()
  2806. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  2807. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.s, tt.err, err)
  2808. } else if tt.err == "" && !reflect.DeepEqual(tt.stmts, stmts) {
  2809. t.Errorf("%d. %q\n\nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.s, tt.stmts, stmts)
  2810. }
  2811. }
  2812. }