parser_test.go 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679
  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. },
  413. },
  414. },
  415. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  416. },
  417. },
  418. {
  419. s: `SELECT count(*, f1) FROM tbl`,
  420. stmt: nil,
  421. err: `Expect 1 arguments but found 2.`,
  422. },
  423. {
  424. s: `SELECT lag() FROM tbl`,
  425. stmt: nil,
  426. err: `expect one two or three args but got 0`,
  427. },
  428. {
  429. s: `SELECT lag(a, b, "default value") FROM tbl`,
  430. stmt: nil,
  431. err: `Expect int type for parameter 2`,
  432. },
  433. {
  434. s: `SELECT lag(a, 2, 20) FROM tbl`,
  435. stmt: &ast.SelectStatement{
  436. Fields: []ast.Field{
  437. {
  438. AName: "",
  439. Name: "lag",
  440. Expr: &ast.Call{
  441. Name: "lag",
  442. Args: []ast.Expr{&ast.FieldRef{Name: "a", StreamName: ast.DefaultStream}, &ast.IntegerLiteral{Val: 2}, &ast.IntegerLiteral{Val: 20}},
  443. },
  444. },
  445. },
  446. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  447. },
  448. },
  449. {
  450. s: `SELECT deduplicate(temperature, false) FROM tbl`,
  451. stmt: &ast.SelectStatement{
  452. Fields: []ast.Field{
  453. {
  454. AName: "",
  455. Name: "deduplicate",
  456. Expr: &ast.Call{
  457. Name: "deduplicate",
  458. Args: []ast.Expr{&ast.Wildcard{Token: ast.ASTERISK}, &ast.FieldRef{Name: "temperature", StreamName: ast.DefaultStream}, &ast.BooleanLiteral{Val: false}},
  459. },
  460. },
  461. },
  462. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  463. },
  464. },
  465. {
  466. s: `SELECT "abc" FROM tbl`,
  467. stmt: &ast.SelectStatement{
  468. Fields: []ast.Field{{AName: "", Name: "kuiper_field_0", Expr: &ast.StringLiteral{Val: "abc"}}},
  469. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  470. },
  471. },
  472. {
  473. s: `SELECT "abc" AS field1 FROM tbl`,
  474. stmt: &ast.SelectStatement{
  475. Fields: []ast.Field{{AName: "field1", Name: "", Expr: &ast.StringLiteral{Val: "abc"}}},
  476. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  477. },
  478. },
  479. {
  480. s: `SELECT field0, "abc" AS field1, field2 FROM tbl`,
  481. stmt: &ast.SelectStatement{
  482. Fields: []ast.Field{
  483. {AName: "", Name: "field0", Expr: &ast.FieldRef{Name: "field0", StreamName: ast.DefaultStream}},
  484. {AName: "field1", Name: "", Expr: &ast.StringLiteral{Val: "abc"}},
  485. {AName: "", Name: "field2", Expr: &ast.FieldRef{Name: "field2", StreamName: ast.DefaultStream}}},
  486. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  487. },
  488. },
  489. {
  490. s: `SELECT * AS alias FROM tbl`,
  491. stmt: nil,
  492. err: `found "AS", expected FROM.`,
  493. },
  494. {
  495. s: `SELECT *, FROM tbl`,
  496. stmt: nil,
  497. err: `found ",", expected FROM.`,
  498. },
  499. {
  500. s: `SELECTname FROM tbl`,
  501. stmt: nil,
  502. err: `Found "SELECTname", Expected SELECT.` + "\n",
  503. },
  504. {
  505. s: `SELECT abc FROM tbl WHERE abc > 12 `,
  506. stmt: &ast.SelectStatement{
  507. Fields: []ast.Field{{AName: "", Name: "abc", Expr: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream}}},
  508. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  509. Condition: &ast.BinaryExpr{
  510. LHS: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream},
  511. OP: ast.GT,
  512. RHS: &ast.IntegerLiteral{Val: 12},
  513. },
  514. },
  515. },
  516. {
  517. s: `SELECT abc FROM tbl WHERE abc = "hello" `,
  518. stmt: &ast.SelectStatement{
  519. Fields: []ast.Field{{AName: "", Name: "abc", Expr: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream}}},
  520. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  521. Condition: &ast.BinaryExpr{
  522. LHS: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream},
  523. OP: ast.EQ,
  524. RHS: &ast.StringLiteral{Val: "hello"},
  525. },
  526. },
  527. },
  528. {
  529. s: `SELECT t1.abc FROM tbl AS t1 WHERE t1.abc = "hello" `,
  530. stmt: &ast.SelectStatement{
  531. Fields: []ast.Field{{AName: "", Name: "abc", Expr: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "abc"}}},
  532. Sources: []ast.Source{&ast.Table{Name: "tbl", Alias: "t1"}},
  533. Condition: &ast.BinaryExpr{
  534. LHS: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "abc"},
  535. OP: ast.EQ,
  536. RHS: &ast.StringLiteral{Val: "hello"},
  537. },
  538. },
  539. },
  540. {
  541. s: `SELECT abc, "fff" AS fa FROM tbl WHERE fa >= 5 `,
  542. stmt: &ast.SelectStatement{
  543. Fields: []ast.Field{{AName: "", Name: "abc", Expr: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream}}, {AName: "fa", Name: "", Expr: &ast.StringLiteral{Val: "fff"}}},
  544. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  545. Condition: &ast.BinaryExpr{
  546. LHS: &ast.FieldRef{Name: "fa", StreamName: ast.DefaultStream},
  547. OP: ast.GTE,
  548. RHS: &ast.IntegerLiteral{Val: 5},
  549. },
  550. },
  551. },
  552. {
  553. s: `SELECT field2 FROM tbl WHERE field2 != 5 `,
  554. stmt: &ast.SelectStatement{
  555. Fields: []ast.Field{{AName: "", Name: "field2", Expr: &ast.FieldRef{Name: "field2", StreamName: ast.DefaultStream}}},
  556. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  557. Condition: &ast.BinaryExpr{
  558. LHS: &ast.FieldRef{Name: "field2", StreamName: ast.DefaultStream},
  559. OP: ast.NEQ,
  560. RHS: &ast.IntegerLiteral{Val: 5},
  561. },
  562. },
  563. },
  564. {
  565. s: `SELECT field2 FROM tbl WHERE field2 ! = 5 `, //Add space char in expression
  566. stmt: &ast.SelectStatement{
  567. Fields: []ast.Field{{AName: "", Name: "field2", Expr: &ast.FieldRef{Name: "field2", StreamName: ast.DefaultStream}}},
  568. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  569. Condition: &ast.BinaryExpr{
  570. LHS: &ast.FieldRef{Name: "field2", StreamName: ast.DefaultStream},
  571. OP: ast.NEQ,
  572. RHS: &ast.IntegerLiteral{Val: 5},
  573. },
  574. },
  575. },
  576. {
  577. s: `SELECT *f FROM tbl`,
  578. stmt: nil,
  579. err: `found "f", expected FROM.`,
  580. },
  581. ////TODO
  582. //{
  583. // s: `SELECT *from FROM tbl`,
  584. // stmt: nil,
  585. // err: `found "f", expected FROM.`,
  586. //},
  587. {
  588. s: `SELECT abc+2 FROM tbl`,
  589. stmt: &ast.SelectStatement{
  590. Fields: []ast.Field{
  591. {
  592. AName: "",
  593. Name: "kuiper_field_0",
  594. Expr: &ast.BinaryExpr{
  595. LHS: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream},
  596. OP: ast.ADD,
  597. RHS: &ast.IntegerLiteral{Val: 2},
  598. },
  599. },
  600. },
  601. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  602. },
  603. },
  604. {
  605. s: `SELECT t1.abc+2 FROM tbl AS t1`,
  606. stmt: &ast.SelectStatement{
  607. Fields: []ast.Field{
  608. {
  609. AName: "",
  610. Name: "kuiper_field_0",
  611. Expr: &ast.BinaryExpr{
  612. LHS: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "abc"},
  613. OP: ast.ADD,
  614. RHS: &ast.IntegerLiteral{Val: 2},
  615. },
  616. },
  617. },
  618. Sources: []ast.Source{&ast.Table{Name: "tbl", Alias: "t1"}},
  619. },
  620. },
  621. {
  622. s: `SELECT abc + "hello" FROM tbl`,
  623. stmt: &ast.SelectStatement{
  624. Fields: []ast.Field{
  625. {
  626. AName: "",
  627. Name: "kuiper_field_0",
  628. Expr: &ast.BinaryExpr{
  629. LHS: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream},
  630. OP: ast.ADD,
  631. RHS: &ast.StringLiteral{Val: "hello"},
  632. },
  633. },
  634. },
  635. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  636. },
  637. },
  638. {
  639. s: `SELECT abc*2 + 3 FROM tbl`,
  640. stmt: &ast.SelectStatement{
  641. Fields: []ast.Field{
  642. {
  643. AName: "",
  644. Name: "kuiper_field_0",
  645. Expr: &ast.BinaryExpr{
  646. LHS: &ast.BinaryExpr{
  647. LHS: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream},
  648. OP: ast.MUL,
  649. RHS: &ast.IntegerLiteral{Val: 2},
  650. },
  651. OP: ast.ADD,
  652. RHS: &ast.IntegerLiteral{Val: 3},
  653. },
  654. },
  655. },
  656. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  657. },
  658. },
  659. {
  660. s: `SELECT ln(abc*2 + 3) FROM tbl`,
  661. stmt: &ast.SelectStatement{
  662. Fields: []ast.Field{
  663. {
  664. AName: "",
  665. Name: "ln",
  666. Expr: &ast.Call{
  667. Name: "ln",
  668. Args: []ast.Expr{
  669. &ast.BinaryExpr{
  670. LHS: &ast.BinaryExpr{
  671. LHS: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream},
  672. OP: ast.MUL,
  673. RHS: &ast.IntegerLiteral{Val: 2},
  674. },
  675. OP: ast.ADD,
  676. RHS: &ast.IntegerLiteral{Val: 3},
  677. },
  678. },
  679. },
  680. },
  681. },
  682. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  683. },
  684. },
  685. {
  686. s: `SELECT ln(t1.abc*2 + 3) FROM tbl AS t1`,
  687. stmt: &ast.SelectStatement{
  688. Fields: []ast.Field{
  689. {
  690. AName: "",
  691. Name: "ln",
  692. Expr: &ast.Call{
  693. Name: "ln",
  694. Args: []ast.Expr{
  695. &ast.BinaryExpr{
  696. LHS: &ast.BinaryExpr{
  697. LHS: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "abc"},
  698. OP: ast.MUL,
  699. RHS: &ast.IntegerLiteral{Val: 2},
  700. },
  701. OP: ast.ADD,
  702. RHS: &ast.IntegerLiteral{Val: 3},
  703. },
  704. },
  705. },
  706. },
  707. },
  708. Sources: []ast.Source{&ast.Table{Name: "tbl", Alias: "t1"}},
  709. },
  710. },
  711. {
  712. s: `SELECT lpad("param2", abc*2 + 3) FROM tbl`,
  713. stmt: &ast.SelectStatement{
  714. Fields: []ast.Field{
  715. {
  716. AName: "",
  717. Name: "lpad",
  718. Expr: &ast.Call{
  719. Name: "lpad",
  720. Args: []ast.Expr{
  721. &ast.StringLiteral{Val: "param2"},
  722. &ast.BinaryExpr{
  723. LHS: &ast.BinaryExpr{
  724. LHS: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream},
  725. OP: ast.MUL,
  726. RHS: &ast.IntegerLiteral{Val: 2},
  727. },
  728. OP: ast.ADD,
  729. RHS: &ast.IntegerLiteral{Val: 3},
  730. },
  731. },
  732. },
  733. },
  734. },
  735. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  736. },
  737. },
  738. {
  739. s: `SELECT 0.2 FROM tbl`,
  740. stmt: &ast.SelectStatement{
  741. Fields: []ast.Field{
  742. {
  743. AName: "",
  744. Name: "kuiper_field_0",
  745. Expr: &ast.NumberLiteral{Val: 0.2},
  746. },
  747. },
  748. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  749. },
  750. },
  751. {
  752. s: `SELECT .2 FROM tbl`,
  753. stmt: &ast.SelectStatement{
  754. Fields: []ast.Field{
  755. {
  756. AName: "",
  757. Name: "kuiper_field_0",
  758. Expr: &ast.NumberLiteral{Val: 0.2},
  759. },
  760. },
  761. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  762. },
  763. },
  764. {
  765. s: `SELECT sin(.2) FROM tbl`,
  766. stmt: &ast.SelectStatement{
  767. Fields: []ast.Field{
  768. {
  769. AName: "",
  770. Name: "sin",
  771. Expr: &ast.Call{
  772. Name: "sin",
  773. Args: []ast.Expr{&ast.NumberLiteral{Val: 0.2}},
  774. },
  775. },
  776. },
  777. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  778. },
  779. },
  780. {
  781. s: `SELECT power(.2, 4) FROM tbl`,
  782. stmt: &ast.SelectStatement{
  783. Fields: []ast.Field{
  784. {
  785. AName: "",
  786. Name: "power",
  787. Expr: &ast.Call{
  788. Name: "power",
  789. Args: []ast.Expr{&ast.NumberLiteral{Val: 0.2}, &ast.IntegerLiteral{Val: 4}},
  790. },
  791. },
  792. },
  793. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  794. },
  795. },
  796. {
  797. s: `SELECT power(.2, 4) AS f1 FROM tbl WHERE f1 > 2.2`,
  798. stmt: &ast.SelectStatement{
  799. Fields: []ast.Field{
  800. {
  801. AName: "f1",
  802. Name: "power",
  803. Expr: &ast.Call{
  804. Name: "power",
  805. Args: []ast.Expr{&ast.NumberLiteral{Val: 0.2}, &ast.IntegerLiteral{Val: 4}},
  806. },
  807. },
  808. },
  809. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  810. Condition: &ast.BinaryExpr{
  811. LHS: &ast.FieldRef{Name: "f1", StreamName: ast.DefaultStream},
  812. OP: ast.GT,
  813. RHS: &ast.NumberLiteral{Val: 2.2},
  814. },
  815. },
  816. },
  817. {
  818. s: `SELECT power(.2, 4) AS f1 FROM tbl WHERE f1 BETWEEN 1 AND 2`,
  819. stmt: &ast.SelectStatement{
  820. Fields: []ast.Field{
  821. {
  822. AName: "f1",
  823. Name: "power",
  824. Expr: &ast.Call{
  825. Name: "power",
  826. Args: []ast.Expr{&ast.NumberLiteral{Val: 0.2}, &ast.IntegerLiteral{Val: 4}},
  827. },
  828. },
  829. },
  830. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  831. Condition: &ast.BinaryExpr{
  832. LHS: &ast.FieldRef{Name: "f1", StreamName: ast.DefaultStream},
  833. OP: ast.BETWEEN,
  834. RHS: &ast.BetweenExpr{
  835. Lower: &ast.IntegerLiteral{Val: 1},
  836. Higher: &ast.IntegerLiteral{Val: 2},
  837. },
  838. },
  839. },
  840. },
  841. {
  842. s: `SELECT a FROM tbl WHERE f1 > 4 AND f2 BETWEEN 1 AND 2`,
  843. stmt: &ast.SelectStatement{
  844. Fields: []ast.Field{
  845. {
  846. AName: "",
  847. Name: "a",
  848. Expr: &ast.FieldRef{Name: "a", StreamName: ast.DefaultStream},
  849. },
  850. },
  851. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  852. Condition: &ast.BinaryExpr{
  853. OP: ast.AND,
  854. LHS: &ast.BinaryExpr{
  855. LHS: &ast.FieldRef{Name: "f1", StreamName: ast.DefaultStream},
  856. OP: ast.GT,
  857. RHS: &ast.IntegerLiteral{Val: 4},
  858. },
  859. RHS: &ast.BinaryExpr{
  860. LHS: &ast.FieldRef{Name: "f2", StreamName: ast.DefaultStream},
  861. OP: ast.BETWEEN,
  862. RHS: &ast.BetweenExpr{
  863. Lower: &ast.IntegerLiteral{Val: 1},
  864. Higher: &ast.IntegerLiteral{Val: 2},
  865. },
  866. },
  867. },
  868. },
  869. },
  870. {
  871. s: `SELECT a FROM tbl WHERE f1 NOT BETWEEN b AND c AND f2 BETWEEN 1 AND 2 AND f3 > 4`,
  872. stmt: &ast.SelectStatement{
  873. Fields: []ast.Field{
  874. {
  875. AName: "",
  876. Name: "a",
  877. Expr: &ast.FieldRef{Name: "a", StreamName: ast.DefaultStream},
  878. },
  879. },
  880. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  881. Condition: &ast.BinaryExpr{
  882. OP: ast.AND,
  883. LHS: &ast.BinaryExpr{
  884. OP: ast.AND,
  885. LHS: &ast.BinaryExpr{
  886. LHS: &ast.FieldRef{Name: "f1", StreamName: ast.DefaultStream},
  887. OP: ast.NOTBETWEEN,
  888. RHS: &ast.BetweenExpr{
  889. Lower: &ast.FieldRef{Name: "b", StreamName: ast.DefaultStream},
  890. Higher: &ast.FieldRef{Name: "c", StreamName: ast.DefaultStream},
  891. },
  892. },
  893. RHS: &ast.BinaryExpr{
  894. LHS: &ast.FieldRef{Name: "f2", StreamName: ast.DefaultStream},
  895. OP: ast.BETWEEN,
  896. RHS: &ast.BetweenExpr{
  897. Lower: &ast.IntegerLiteral{Val: 1},
  898. Higher: &ast.IntegerLiteral{Val: 2},
  899. },
  900. },
  901. },
  902. RHS: &ast.BinaryExpr{
  903. OP: ast.GT,
  904. LHS: &ast.FieldRef{Name: "f3", StreamName: ast.DefaultStream},
  905. RHS: &ast.IntegerLiteral{Val: 4},
  906. },
  907. },
  908. },
  909. },
  910. {
  911. s: `SELECT a FROM tbl WHERE f1 NOT BETWEEN b`,
  912. err: "expect AND expression after between but found EOF",
  913. },
  914. {
  915. s: `SELECT a FROM tbl WHERE f1 NOT BETWEEN 1 OR 2`,
  916. err: "expect AND expression after between but found OR",
  917. },
  918. {
  919. s: `SELECT a FROM tbl WHERE a LIKE "foo"`,
  920. stmt: &ast.SelectStatement{
  921. Fields: []ast.Field{
  922. {
  923. AName: "",
  924. Name: "a",
  925. Expr: &ast.FieldRef{Name: "a", StreamName: ast.DefaultStream},
  926. },
  927. },
  928. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  929. Condition: &ast.BinaryExpr{
  930. LHS: &ast.FieldRef{Name: "a", StreamName: ast.DefaultStream},
  931. OP: ast.LIKE,
  932. RHS: &ast.LikePattern{Expr: &ast.StringLiteral{Val: "foo"}, Pattern: re1},
  933. },
  934. },
  935. },
  936. {
  937. s: `SELECT a FROM tbl WHERE a NOT LIKE "fo_o%"`,
  938. stmt: &ast.SelectStatement{
  939. Fields: []ast.Field{
  940. {
  941. AName: "",
  942. Name: "a",
  943. Expr: &ast.FieldRef{Name: "a", StreamName: ast.DefaultStream},
  944. },
  945. },
  946. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  947. Condition: &ast.BinaryExpr{
  948. LHS: &ast.FieldRef{Name: "a", StreamName: ast.DefaultStream},
  949. OP: ast.NOTLIKE,
  950. RHS: &ast.LikePattern{Expr: &ast.StringLiteral{Val: "fo_o%"}, Pattern: re2},
  951. },
  952. },
  953. },
  954. {
  955. s: `SELECT a FROM tbl WHERE a LIKE "foo\\%"`,
  956. stmt: &ast.SelectStatement{
  957. Fields: []ast.Field{
  958. {
  959. AName: "",
  960. Name: "a",
  961. Expr: &ast.FieldRef{Name: "a", StreamName: ast.DefaultStream},
  962. },
  963. },
  964. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  965. Condition: &ast.BinaryExpr{
  966. LHS: &ast.FieldRef{Name: "a", StreamName: ast.DefaultStream},
  967. OP: ast.LIKE,
  968. RHS: &ast.LikePattern{Expr: &ast.StringLiteral{Val: "foo\\%"}, Pattern: re3},
  969. },
  970. },
  971. },
  972. {
  973. s: `SELECT deviceId, name FROM topic/sensor1 WHERE deviceId=1 AND name = "dname"`,
  974. stmt: &ast.SelectStatement{
  975. Fields: []ast.Field{
  976. {Expr: &ast.FieldRef{Name: "deviceId", StreamName: ast.DefaultStream}, Name: "deviceId", AName: ""},
  977. {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
  978. },
  979. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  980. Condition: &ast.BinaryExpr{
  981. LHS: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "deviceId", StreamName: ast.DefaultStream}, OP: ast.EQ, RHS: &ast.IntegerLiteral{Val: 1}},
  982. OP: ast.AND,
  983. RHS: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, OP: ast.EQ, RHS: &ast.StringLiteral{Val: "dname"}},
  984. },
  985. },
  986. },
  987. {
  988. s: `SELECT deviceId, name FROM topic/sensor1 AS t1 WHERE t1.deviceId=1 AND t1.name = "dname"`,
  989. stmt: &ast.SelectStatement{
  990. Fields: []ast.Field{
  991. {Expr: &ast.FieldRef{Name: "deviceId", StreamName: ast.DefaultStream}, Name: "deviceId", AName: ""},
  992. {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
  993. },
  994. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
  995. Condition: &ast.BinaryExpr{
  996. LHS: &ast.BinaryExpr{LHS: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "deviceId"}, OP: ast.EQ, RHS: &ast.IntegerLiteral{Val: 1}},
  997. OP: ast.AND,
  998. RHS: &ast.BinaryExpr{LHS: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "name"}, OP: ast.EQ, RHS: &ast.StringLiteral{Val: "dname"}},
  999. },
  1000. },
  1001. },
  1002. {
  1003. s: `SELECT temp AS t, name FROM topic/sensor1 WHERE t> = 20.5 OR name = "dname"`,
  1004. stmt: &ast.SelectStatement{
  1005. Fields: []ast.Field{
  1006. {Expr: &ast.FieldRef{Name: "temp", StreamName: ast.DefaultStream}, Name: "temp", AName: "t"},
  1007. {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
  1008. },
  1009. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  1010. Condition: &ast.BinaryExpr{
  1011. LHS: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "t", StreamName: ast.DefaultStream}, OP: ast.GTE, RHS: &ast.NumberLiteral{Val: 20.5}},
  1012. OP: ast.OR,
  1013. RHS: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, OP: ast.EQ, RHS: &ast.StringLiteral{Val: "dname"}},
  1014. },
  1015. },
  1016. },
  1017. {
  1018. s: `SELECT temp AS t, name FROM topic/sensor1 WHERE t IN arraySet OR name IN arraySet`,
  1019. stmt: &ast.SelectStatement{
  1020. Fields: []ast.Field{
  1021. {Expr: &ast.FieldRef{Name: "temp", StreamName: ast.DefaultStream}, Name: "temp", AName: "t"},
  1022. {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
  1023. },
  1024. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  1025. Condition: &ast.BinaryExpr{
  1026. LHS: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "t", StreamName: ast.DefaultStream}, OP: ast.IN, RHS: &ast.FieldRef{Name: "arraySet", StreamName: ast.DefaultStream}},
  1027. OP: ast.OR,
  1028. RHS: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, OP: ast.IN, RHS: &ast.FieldRef{Name: "arraySet", StreamName: ast.DefaultStream}},
  1029. },
  1030. },
  1031. },
  1032. {
  1033. s: `SELECT temp AS t, name FROM topic/sensor1 WHERE t NOT IN arraySet OR name NOT IN arraySet`,
  1034. stmt: &ast.SelectStatement{
  1035. Fields: []ast.Field{
  1036. {Expr: &ast.FieldRef{Name: "temp", StreamName: ast.DefaultStream}, Name: "temp", AName: "t"},
  1037. {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
  1038. },
  1039. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  1040. Condition: &ast.BinaryExpr{
  1041. LHS: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "t", StreamName: ast.DefaultStream}, OP: ast.NOTIN, RHS: &ast.FieldRef{Name: "arraySet", StreamName: ast.DefaultStream}},
  1042. OP: ast.OR,
  1043. RHS: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, OP: ast.NOTIN, RHS: &ast.FieldRef{Name: "arraySet", StreamName: ast.DefaultStream}},
  1044. },
  1045. },
  1046. },
  1047. {
  1048. s: `SELECT temp AS t, name FROM topic/sensor1 WHERE t IN (20.5, 20.4) OR name IN ("dname", "ename")`,
  1049. stmt: &ast.SelectStatement{
  1050. Fields: []ast.Field{
  1051. {Expr: &ast.FieldRef{Name: "temp", StreamName: ast.DefaultStream}, Name: "temp", AName: "t"},
  1052. {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
  1053. },
  1054. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  1055. Condition: &ast.BinaryExpr{
  1056. 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}}}},
  1057. OP: ast.OR,
  1058. 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"}}}},
  1059. },
  1060. },
  1061. },
  1062. {
  1063. s: `SELECT temp AS t, name FROM topic/sensor1 WHERE t NOT IN (20.5, 20.4) OR name IN ("dname", "ename")`,
  1064. stmt: &ast.SelectStatement{
  1065. Fields: []ast.Field{
  1066. {Expr: &ast.FieldRef{Name: "temp", StreamName: ast.DefaultStream}, Name: "temp", AName: "t"},
  1067. {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
  1068. },
  1069. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  1070. Condition: &ast.BinaryExpr{
  1071. 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}}}},
  1072. OP: ast.OR,
  1073. 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"}}}},
  1074. },
  1075. },
  1076. },
  1077. {
  1078. s: `SELECT temp AS t, name FROM topic/sensor1 WHERE name = "dname" GROUP BY name`,
  1079. stmt: &ast.SelectStatement{
  1080. Fields: []ast.Field{
  1081. {Expr: &ast.FieldRef{Name: "temp", StreamName: ast.DefaultStream}, Name: "temp", AName: "t"},
  1082. {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
  1083. },
  1084. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  1085. Condition: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, OP: ast.EQ, RHS: &ast.StringLiteral{Val: "dname"}},
  1086. Dimensions: ast.Dimensions{ast.Dimension{Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}}},
  1087. },
  1088. },
  1089. {
  1090. s: `SELECT temp AS t, name FROM topic/sensor1 WHERE name = "dname" GROUP BY name HAVING count(name) > 3`,
  1091. stmt: &ast.SelectStatement{
  1092. Fields: []ast.Field{
  1093. {Expr: &ast.FieldRef{Name: "temp", StreamName: ast.DefaultStream}, Name: "temp", AName: "t"},
  1094. {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
  1095. },
  1096. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  1097. Condition: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, OP: ast.EQ, RHS: &ast.StringLiteral{Val: "dname"}},
  1098. Dimensions: ast.Dimensions{ast.Dimension{Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}}},
  1099. Having: &ast.BinaryExpr{LHS: &ast.Call{Name: "count", Args: []ast.Expr{&ast.FieldRef{StreamName: ast.DefaultStream, Name: "name"}}}, OP: ast.GT, RHS: &ast.IntegerLiteral{Val: 3}},
  1100. },
  1101. },
  1102. {
  1103. s: `SELECT temp AS t, name FROM topic/sensor1 WHERE name = "dname" HAVING count(name) > 3`,
  1104. stmt: &ast.SelectStatement{
  1105. Fields: []ast.Field{
  1106. {Expr: &ast.FieldRef{Name: "temp", StreamName: ast.DefaultStream}, Name: "temp", AName: "t"},
  1107. {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
  1108. },
  1109. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  1110. Condition: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, OP: ast.EQ, RHS: &ast.StringLiteral{Val: "dname"}},
  1111. Having: &ast.BinaryExpr{LHS: &ast.Call{Name: "count", Args: []ast.Expr{&ast.FieldRef{StreamName: ast.DefaultStream, Name: "name"}}}, OP: ast.GT, RHS: &ast.IntegerLiteral{Val: 3}},
  1112. },
  1113. },
  1114. {
  1115. s: `SELECT id,AVG(data) FROM t GROUP BY SUM(data)>10`,
  1116. stmt: nil,
  1117. err: "Not allowed to call aggregate functions in GROUP BY clause.",
  1118. },
  1119. {
  1120. s: `SELECT temp AS t, name FROM topic/sensor1 WHERE count(name) = 3`,
  1121. stmt: nil,
  1122. err: "Not allowed to call aggregate functions in WHERE clause.",
  1123. },
  1124. {
  1125. s: `SELECT s1.temp AS t, name FROM topic/sensor1 AS s1 WHERE t = "dname" GROUP BY s1.temp`,
  1126. stmt: &ast.SelectStatement{
  1127. Fields: []ast.Field{
  1128. {Expr: &ast.FieldRef{StreamName: "s1", Name: "temp"}, Name: "temp", AName: "t"},
  1129. {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
  1130. },
  1131. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "s1"}},
  1132. Condition: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "t", StreamName: ast.DefaultStream}, OP: ast.EQ, RHS: &ast.StringLiteral{Val: "dname"}},
  1133. Dimensions: ast.Dimensions{ast.Dimension{Expr: &ast.FieldRef{StreamName: "s1", Name: "temp"}}},
  1134. },
  1135. },
  1136. {
  1137. s: `SELECT temp AS t, name FROM topic/sensor1 WHERE name = "dname" GROUP BY lpad(name,1)`,
  1138. stmt: &ast.SelectStatement{
  1139. Fields: []ast.Field{
  1140. {Expr: &ast.FieldRef{Name: "temp", StreamName: ast.DefaultStream}, Name: "temp", AName: "t"},
  1141. {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
  1142. },
  1143. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  1144. Condition: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, OP: ast.EQ, RHS: &ast.StringLiteral{Val: "dname"}},
  1145. Dimensions: ast.Dimensions{ast.Dimension{
  1146. Expr: &ast.Call{Name: "lpad", Args: []ast.Expr{&ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, &ast.IntegerLiteral{Val: 1}}},
  1147. },
  1148. },
  1149. },
  1150. },
  1151. {
  1152. s: `SELECT temp AS t, name FROM topic/sensor1 AS s1 WHERE name = "dname" GROUP BY lpad(s1.name,1)`,
  1153. stmt: &ast.SelectStatement{
  1154. Fields: []ast.Field{
  1155. {Expr: &ast.FieldRef{Name: "temp", StreamName: ast.DefaultStream}, Name: "temp", AName: "t"},
  1156. {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
  1157. },
  1158. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "s1"}},
  1159. Condition: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, OP: ast.EQ, RHS: &ast.StringLiteral{Val: "dname"}},
  1160. Dimensions: ast.Dimensions{ast.Dimension{
  1161. Expr: &ast.Call{Name: "lpad", Args: []ast.Expr{&ast.FieldRef{StreamName: ast.StreamName("s1"), Name: "name"}, &ast.IntegerLiteral{Val: 1}}},
  1162. },
  1163. },
  1164. },
  1165. },
  1166. {
  1167. s: `SELECT temp AS t, name FROM topic/sensor1 WHERE name = "dname" GROUP BY lpad(name,1) ORDER BY name`,
  1168. stmt: &ast.SelectStatement{
  1169. Fields: []ast.Field{
  1170. {Expr: &ast.FieldRef{Name: "temp", StreamName: ast.DefaultStream}, Name: "temp", AName: "t"},
  1171. {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
  1172. },
  1173. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  1174. Condition: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, OP: ast.EQ, RHS: &ast.StringLiteral{Val: "dname"}},
  1175. Dimensions: ast.Dimensions{
  1176. ast.Dimension{
  1177. Expr: &ast.Call{Name: "lpad", Args: []ast.Expr{
  1178. &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  1179. &ast.IntegerLiteral{Val: 1}},
  1180. },
  1181. },
  1182. },
  1183. SortFields: []ast.SortField{{Uname: "name", Name: "name", Ascending: true, FieldExpr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}}},
  1184. },
  1185. },
  1186. {
  1187. 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`,
  1188. stmt: &ast.SelectStatement{
  1189. Fields: []ast.Field{
  1190. {Expr: &ast.FieldRef{Name: "temp", StreamName: ast.DefaultStream}, Name: "temp", AName: "t"},
  1191. {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
  1192. },
  1193. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "s1"}},
  1194. Condition: &ast.BinaryExpr{LHS: &ast.FieldRef{StreamName: ast.StreamName("s1"), Name: "name"}, OP: ast.EQ, RHS: &ast.StringLiteral{Val: "dname"}},
  1195. Dimensions: ast.Dimensions{
  1196. ast.Dimension{
  1197. Expr: &ast.Call{Name: "lpad", Args: []ast.Expr{
  1198. &ast.FieldRef{StreamName: ast.StreamName("s1"), Name: "name"},
  1199. &ast.IntegerLiteral{Val: 1}},
  1200. },
  1201. },
  1202. },
  1203. SortFields: []ast.SortField{{Uname: "s1\007name", Name: "name", StreamName: ast.StreamName("s1"), Ascending: true, FieldExpr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}}},
  1204. },
  1205. },
  1206. {
  1207. s: `SELECT temp AS t, name FROM topic/sensor1 WHERE name = "dname" GROUP BY lpad(name,1) ORDER BY name DESC`,
  1208. stmt: &ast.SelectStatement{
  1209. Fields: []ast.Field{
  1210. {Expr: &ast.FieldRef{Name: "temp", StreamName: ast.DefaultStream}, Name: "temp", AName: "t"},
  1211. {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
  1212. },
  1213. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  1214. Condition: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, OP: ast.EQ, RHS: &ast.StringLiteral{Val: "dname"}},
  1215. Dimensions: ast.Dimensions{
  1216. ast.Dimension{
  1217. Expr: &ast.Call{Name: "lpad", Args: []ast.Expr{
  1218. &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  1219. &ast.IntegerLiteral{Val: 1}},
  1220. },
  1221. },
  1222. },
  1223. SortFields: []ast.SortField{{Uname: "name", Name: "name", Ascending: false, FieldExpr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}}},
  1224. },
  1225. },
  1226. {
  1227. s: `SELECT * FROM topic/sensor1 ORDER BY name DESC`,
  1228. stmt: &ast.SelectStatement{
  1229. Fields: []ast.Field{
  1230. {
  1231. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  1232. Name: "",
  1233. AName: ""},
  1234. },
  1235. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  1236. SortFields: []ast.SortField{{Uname: "name", Name: "name", Ascending: false, FieldExpr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}}},
  1237. },
  1238. },
  1239. {
  1240. s: `SELECT * FROM topic/sensor1 ORDER BY name DESC, name2 ASC`,
  1241. stmt: &ast.SelectStatement{
  1242. Fields: []ast.Field{
  1243. {
  1244. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  1245. Name: "",
  1246. AName: ""},
  1247. },
  1248. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  1249. 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}}},
  1250. },
  1251. },
  1252. {
  1253. s: `SELECT * FROM topic/sensor1 GROUP BY name, name2,power(name3,1.8) ORDER BY name DESC, name2 ASC`,
  1254. stmt: &ast.SelectStatement{
  1255. Fields: []ast.Field{
  1256. {
  1257. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  1258. Name: "",
  1259. AName: ""},
  1260. },
  1261. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  1262. Dimensions: ast.Dimensions{
  1263. ast.Dimension{Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}},
  1264. ast.Dimension{Expr: &ast.FieldRef{Name: "name2", StreamName: ast.DefaultStream}},
  1265. ast.Dimension{
  1266. Expr: &ast.Call{Name: "power", Args: []ast.Expr{
  1267. &ast.FieldRef{Name: "name3", StreamName: ast.DefaultStream},
  1268. &ast.NumberLiteral{Val: 1.8}},
  1269. },
  1270. },
  1271. },
  1272. 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}}},
  1273. },
  1274. },
  1275. //{
  1276. // s: `SELECT .2sd FROM tbl`,
  1277. // stmt: &SelectStatement{
  1278. // Fields: []Field{
  1279. // Field{
  1280. // AName:"",
  1281. // Expr: &NumberLiteral{Val: 0.2},
  1282. // },
  1283. // },
  1284. // TableName: "tbl",
  1285. // },
  1286. //},
  1287. {
  1288. s: `SELECT name FROM tbl/*SELECT comment FROM testComments*/`,
  1289. stmt: &ast.SelectStatement{
  1290. Fields: []ast.Field{
  1291. {
  1292. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  1293. Name: "name",
  1294. AName: ""},
  1295. },
  1296. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1297. },
  1298. },
  1299. {
  1300. s: `/*SELECT comment FROM testComments*/SELECT name FROM tbl`,
  1301. stmt: &ast.SelectStatement{
  1302. Fields: []ast.Field{
  1303. {
  1304. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  1305. Name: "name",
  1306. AName: ""},
  1307. },
  1308. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1309. },
  1310. },
  1311. {
  1312. s: `SELECT name /*SELECT comment FROM testComments*/ FROM tbl`,
  1313. stmt: &ast.SelectStatement{
  1314. Fields: []ast.Field{
  1315. {
  1316. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  1317. Name: "name",
  1318. AName: ""},
  1319. },
  1320. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1321. },
  1322. },
  1323. {
  1324. s: `SELECT true AS f1, FALSE as f2 FROM tbl`,
  1325. stmt: &ast.SelectStatement{
  1326. Fields: []ast.Field{
  1327. {AName: "f1", Name: "", Expr: &ast.BooleanLiteral{Val: true}},
  1328. {AName: "f2", Name: "", Expr: &ast.BooleanLiteral{Val: false}},
  1329. },
  1330. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1331. },
  1332. },
  1333. {
  1334. s: `SELECT true AS f1 FROM tbl WHERE f2 = true`,
  1335. stmt: &ast.SelectStatement{
  1336. Fields: []ast.Field{
  1337. {AName: "f1", Name: "", Expr: &ast.BooleanLiteral{Val: true}},
  1338. },
  1339. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1340. Condition: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "f2", StreamName: ast.DefaultStream}, OP: ast.EQ, RHS: &ast.BooleanLiteral{Val: true}},
  1341. },
  1342. },
  1343. {
  1344. s: `SELECT indexof(field1, "abc") FROM tbl`,
  1345. stmt: &ast.SelectStatement{
  1346. Fields: []ast.Field{
  1347. {
  1348. AName: "",
  1349. Name: "indexof",
  1350. Expr: &ast.Call{
  1351. Name: "indexof",
  1352. Args: []ast.Expr{&ast.FieldRef{Name: "field1", StreamName: ast.DefaultStream}, &ast.StringLiteral{Val: "abc"}},
  1353. },
  1354. },
  1355. },
  1356. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1357. },
  1358. },
  1359. //The negative value expression support.
  1360. {
  1361. s: `SELECT -3 AS t1 FROM tbl`,
  1362. stmt: &ast.SelectStatement{
  1363. Fields: []ast.Field{
  1364. {
  1365. Expr: &ast.IntegerLiteral{Val: -3},
  1366. Name: "",
  1367. AName: "t1"},
  1368. },
  1369. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1370. },
  1371. },
  1372. {
  1373. s: `SELECT - 3 AS t1 FROM tbl`,
  1374. stmt: &ast.SelectStatement{
  1375. Fields: []ast.Field{
  1376. {
  1377. Expr: &ast.IntegerLiteral{Val: -3},
  1378. Name: "",
  1379. AName: "t1"},
  1380. },
  1381. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1382. },
  1383. },
  1384. {
  1385. s: `SELECT -. 3 AS t1 FROM tbl`,
  1386. stmt: &ast.SelectStatement{
  1387. Fields: []ast.Field{
  1388. {
  1389. Expr: &ast.NumberLiteral{Val: -.3},
  1390. Name: "",
  1391. AName: "t1"},
  1392. },
  1393. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1394. },
  1395. },
  1396. {
  1397. s: `SELECT -3.3 AS t1 FROM tbl`,
  1398. stmt: &ast.SelectStatement{
  1399. Fields: []ast.Field{
  1400. {
  1401. Expr: &ast.NumberLiteral{Val: -3.3},
  1402. Name: "",
  1403. AName: "t1"},
  1404. },
  1405. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1406. },
  1407. },
  1408. {
  1409. s: `SELECT sample(-.3,) FROM tbl`,
  1410. stmt: nil,
  1411. err: "function sample not found",
  1412. },
  1413. {
  1414. s: `select timestamp() as tp from demo`,
  1415. stmt: nil,
  1416. err: "found \"TIMESTAMP\", expected expression.",
  1417. },
  1418. {
  1419. s: `select tstamp() as tp from demo`,
  1420. stmt: &ast.SelectStatement{
  1421. Fields: []ast.Field{
  1422. {
  1423. Expr: &ast.Call{
  1424. Name: "tstamp",
  1425. Args: nil,
  1426. },
  1427. Name: "tstamp",
  1428. AName: "tp"},
  1429. },
  1430. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  1431. },
  1432. err: "",
  1433. },
  1434. {
  1435. s: "SELECT `half FROM tb",
  1436. stmt: nil,
  1437. err: `found "EOF", expected FROM.`,
  1438. },
  1439. {
  1440. s: "SELECT `space var` FROM tbl",
  1441. stmt: &ast.SelectStatement{
  1442. Fields: []ast.Field{
  1443. {
  1444. Expr: &ast.FieldRef{Name: "space var", StreamName: ast.DefaultStream},
  1445. Name: "space var",
  1446. AName: ""},
  1447. },
  1448. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1449. },
  1450. },
  1451. {
  1452. s: "SELECT `中文 Chinese` FROM tbl",
  1453. stmt: &ast.SelectStatement{
  1454. Fields: []ast.Field{
  1455. {
  1456. Expr: &ast.FieldRef{Name: "中文 Chinese", StreamName: ast.DefaultStream},
  1457. Name: "中文 Chinese",
  1458. AName: ""},
  1459. },
  1460. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1461. },
  1462. }, {
  1463. s: "SELECT CASE temperature WHEN 25 THEN \"bingo\" WHEN 30 THEN \"high\" ELSE \"low\" END as label, humidity FROM tbl",
  1464. stmt: &ast.SelectStatement{
  1465. Fields: []ast.Field{
  1466. {
  1467. Expr: &ast.CaseExpr{
  1468. Value: &ast.FieldRef{Name: "temperature", StreamName: ast.DefaultStream},
  1469. WhenClauses: []*ast.WhenClause{
  1470. {
  1471. Expr: &ast.IntegerLiteral{Val: 25},
  1472. Result: &ast.StringLiteral{Val: "bingo"},
  1473. }, {
  1474. Expr: &ast.IntegerLiteral{Val: 30},
  1475. Result: &ast.StringLiteral{Val: "high"},
  1476. },
  1477. },
  1478. ElseClause: &ast.StringLiteral{Val: "low"},
  1479. },
  1480. Name: "",
  1481. AName: "label",
  1482. }, {
  1483. Expr: &ast.FieldRef{Name: "humidity", StreamName: ast.DefaultStream},
  1484. Name: "humidity",
  1485. AName: "",
  1486. },
  1487. },
  1488. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1489. },
  1490. }, {
  1491. s: "SELECT CASE temperature WHEN 25 THEN \"bingo\" WHEN 30 THEN \"high\" END as label, humidity FROM tbl",
  1492. stmt: &ast.SelectStatement{
  1493. Fields: []ast.Field{
  1494. {
  1495. Expr: &ast.CaseExpr{
  1496. Value: &ast.FieldRef{Name: "temperature", StreamName: ast.DefaultStream},
  1497. WhenClauses: []*ast.WhenClause{
  1498. {
  1499. Expr: &ast.IntegerLiteral{Val: 25},
  1500. Result: &ast.StringLiteral{Val: "bingo"},
  1501. }, {
  1502. Expr: &ast.IntegerLiteral{Val: 30},
  1503. Result: &ast.StringLiteral{Val: "high"},
  1504. },
  1505. },
  1506. ElseClause: nil,
  1507. },
  1508. Name: "",
  1509. AName: "label",
  1510. }, {
  1511. Expr: &ast.FieldRef{Name: "humidity", StreamName: ast.DefaultStream},
  1512. Name: "humidity",
  1513. AName: "",
  1514. },
  1515. },
  1516. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1517. },
  1518. }, {
  1519. s: "SELECT CASE temperature ELSE \"low\" END as label, humidity FROM tbl",
  1520. stmt: nil,
  1521. err: "invalid CASE expression, WHEN expected before ELSE",
  1522. }, {
  1523. s: "SELECT CASE WHEN temperature > 30 THEN \"high\" ELSE \"low\" END as label, humidity FROM tbl",
  1524. stmt: &ast.SelectStatement{
  1525. Fields: []ast.Field{
  1526. {
  1527. Expr: &ast.CaseExpr{
  1528. Value: nil,
  1529. WhenClauses: []*ast.WhenClause{
  1530. {
  1531. Expr: &ast.BinaryExpr{
  1532. OP: ast.GT,
  1533. LHS: &ast.FieldRef{Name: "temperature", StreamName: ast.DefaultStream},
  1534. RHS: &ast.IntegerLiteral{Val: 30},
  1535. },
  1536. Result: &ast.StringLiteral{Val: "high"},
  1537. },
  1538. },
  1539. ElseClause: &ast.StringLiteral{Val: "low"},
  1540. },
  1541. Name: "",
  1542. AName: "label",
  1543. }, {
  1544. Expr: &ast.FieldRef{Name: "humidity", StreamName: ast.DefaultStream},
  1545. Name: "humidity",
  1546. AName: "",
  1547. },
  1548. },
  1549. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1550. },
  1551. }, {
  1552. s: "SELECT CASE WHEN 30 THEN \"high\" ELSE \"low\" END as label, humidity FROM tbl",
  1553. stmt: nil,
  1554. err: "invalid CASE expression, WHEN expression must be a bool condition",
  1555. }, {
  1556. s: `SELECT count(*)-10 FROM demo`,
  1557. stmt: &ast.SelectStatement{
  1558. Fields: []ast.Field{
  1559. {
  1560. AName: "",
  1561. Name: "kuiper_field_0",
  1562. Expr: &ast.BinaryExpr{
  1563. OP: ast.SUB,
  1564. LHS: &ast.Call{
  1565. Name: "count",
  1566. Args: []ast.Expr{
  1567. &ast.Wildcard{Token: ast.ASTERISK},
  1568. },
  1569. },
  1570. RHS: &ast.IntegerLiteral{Val: 10},
  1571. },
  1572. },
  1573. },
  1574. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  1575. },
  1576. },
  1577. {
  1578. s: `SELECT -abc FROM demo`,
  1579. stmt: nil,
  1580. err: "found \"-\", expected expression.",
  1581. },
  1582. {
  1583. s: `SELECT meta(*) FROM tbl`,
  1584. stmt: &ast.SelectStatement{
  1585. Fields: []ast.Field{
  1586. {
  1587. AName: "",
  1588. Name: "meta",
  1589. Expr: &ast.Call{
  1590. Name: "meta",
  1591. Args: []ast.Expr{
  1592. &ast.MetaRef{
  1593. Name: "*",
  1594. StreamName: ast.DefaultStream,
  1595. },
  1596. },
  1597. },
  1598. },
  1599. },
  1600. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1601. },
  1602. }, {
  1603. s: `SELECT changed_cols("",true,a,b,c) FROM tbl`,
  1604. stmt: &ast.SelectStatement{
  1605. Fields: []ast.Field{
  1606. {
  1607. AName: "",
  1608. Name: "changed_cols",
  1609. Expr: &ast.Call{
  1610. Name: "changed_cols",
  1611. Args: []ast.Expr{
  1612. &ast.ColFuncField{
  1613. Name: "",
  1614. Expr: &ast.StringLiteral{Val: ""},
  1615. },
  1616. &ast.ColFuncField{
  1617. Name: "",
  1618. Expr: &ast.BooleanLiteral{Val: true},
  1619. },
  1620. &ast.ColFuncField{Name: "a", Expr: &ast.FieldRef{
  1621. StreamName: ast.DefaultStream,
  1622. Name: "a",
  1623. }},
  1624. &ast.ColFuncField{Name: "b", Expr: &ast.FieldRef{
  1625. StreamName: ast.DefaultStream,
  1626. Name: "b",
  1627. }},
  1628. &ast.ColFuncField{Name: "c", Expr: &ast.FieldRef{
  1629. StreamName: ast.DefaultStream,
  1630. Name: "c",
  1631. }},
  1632. },
  1633. },
  1634. },
  1635. },
  1636. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1637. },
  1638. }, {
  1639. s: `SELECT changed_cols("",true,a,*,c) FROM tbl`,
  1640. stmt: &ast.SelectStatement{
  1641. Fields: []ast.Field{
  1642. {
  1643. AName: "",
  1644. Name: "changed_cols",
  1645. Expr: &ast.Call{
  1646. Name: "changed_cols",
  1647. Args: []ast.Expr{
  1648. &ast.ColFuncField{
  1649. Name: "",
  1650. Expr: &ast.StringLiteral{Val: ""},
  1651. },
  1652. &ast.ColFuncField{
  1653. Name: "",
  1654. Expr: &ast.BooleanLiteral{Val: true},
  1655. },
  1656. &ast.ColFuncField{Name: "a", Expr: &ast.FieldRef{
  1657. StreamName: ast.DefaultStream,
  1658. Name: "a",
  1659. }},
  1660. &ast.ColFuncField{Name: "", Expr: &ast.Wildcard{
  1661. Token: ast.ASTERISK,
  1662. }},
  1663. &ast.ColFuncField{Name: "c", Expr: &ast.FieldRef{
  1664. StreamName: ast.DefaultStream,
  1665. Name: "c",
  1666. }},
  1667. },
  1668. },
  1669. },
  1670. },
  1671. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1672. },
  1673. }, {
  1674. s: `SELECT a FROM tbl WHERE changed_cols("",true,a,b,c) > 3`,
  1675. err: "function changed_cols can only be used inside the select clause",
  1676. },
  1677. {
  1678. s: `SELECT ".*(/)(?!.*\1)" FROM topic/sensor1 AS t1`,
  1679. err: `found "invalid string: \".*(/)(?!.*\\1)\"", expected expression.`,
  1680. },
  1681. {
  1682. s: `SELECT name FROM tbl WHERE name IN ("A", "B","C")`,
  1683. stmt: &ast.SelectStatement{
  1684. Fields: []ast.Field{
  1685. {
  1686. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  1687. Name: "name",
  1688. AName: ""},
  1689. },
  1690. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1691. 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"}}}},
  1692. },
  1693. },
  1694. {
  1695. s: `SELECT name FROM tbl WHERE name IN ()`,
  1696. err: `expect elements for IN expression, but found ")", expected expression.`,
  1697. },
  1698. {
  1699. s: `SELECT name FROM tbl WHERE name IN (abc,def OR name in (abc)`,
  1700. err: `expect ) for IN expression, but got "EOF"`,
  1701. },
  1702. }
  1703. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  1704. for i, tt := range tests {
  1705. //fmt.Printf("Parsing SQL %q.\n", tt.s)
  1706. stmt, err := NewParser(strings.NewReader(tt.s)).Parse()
  1707. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  1708. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.s, tt.err, err)
  1709. } else if tt.err == "" && !reflect.DeepEqual(tt.stmt, stmt) {
  1710. t.Errorf("%d. %q\n\nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.s, tt.stmt, stmt)
  1711. }
  1712. }
  1713. }
  1714. func TestParser_ParseWindowsExpr(t *testing.T) {
  1715. var tests = []struct {
  1716. s string
  1717. stmt *ast.SelectStatement
  1718. err string
  1719. }{
  1720. {
  1721. s: `SELECT f1 FROM tbl GROUP BY TUMBLINGWINDOW(ss, 10)`,
  1722. stmt: &ast.SelectStatement{
  1723. Fields: []ast.Field{
  1724. {
  1725. Expr: &ast.FieldRef{Name: "f1", StreamName: ast.DefaultStream},
  1726. Name: "f1",
  1727. AName: ""},
  1728. },
  1729. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1730. Dimensions: ast.Dimensions{
  1731. ast.Dimension{
  1732. Expr: &ast.Window{
  1733. WindowType: ast.TUMBLING_WINDOW,
  1734. Length: &ast.IntegerLiteral{Val: 10000},
  1735. Interval: &ast.IntegerLiteral{Val: 0},
  1736. },
  1737. },
  1738. },
  1739. },
  1740. },
  1741. {
  1742. s: `SELECT f1 FROM tbl GROUP BY HOPPINGWINDOW(mi, 5, 1)`,
  1743. stmt: &ast.SelectStatement{
  1744. Fields: []ast.Field{
  1745. {
  1746. Expr: &ast.FieldRef{Name: "f1", StreamName: ast.DefaultStream},
  1747. Name: "f1",
  1748. AName: ""},
  1749. },
  1750. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1751. Dimensions: ast.Dimensions{
  1752. ast.Dimension{
  1753. Expr: &ast.Window{
  1754. WindowType: ast.HOPPING_WINDOW,
  1755. Length: &ast.IntegerLiteral{Val: 3e5},
  1756. Interval: &ast.IntegerLiteral{Val: 6e4},
  1757. },
  1758. },
  1759. },
  1760. },
  1761. },
  1762. {
  1763. s: `SELECT f1 FROM tbl GROUP BY SESSIONWINDOW(hh, 5, 1)`,
  1764. stmt: &ast.SelectStatement{
  1765. Fields: []ast.Field{
  1766. {
  1767. Expr: &ast.FieldRef{Name: "f1", StreamName: ast.DefaultStream},
  1768. Name: "f1",
  1769. AName: ""},
  1770. },
  1771. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1772. Dimensions: ast.Dimensions{
  1773. ast.Dimension{
  1774. Expr: &ast.Window{
  1775. WindowType: ast.SESSION_WINDOW,
  1776. Length: &ast.IntegerLiteral{Val: 1.8e7},
  1777. Interval: &ast.IntegerLiteral{Val: 3.6e6},
  1778. },
  1779. },
  1780. },
  1781. },
  1782. },
  1783. {
  1784. s: `SELECT f1 FROM tbl GROUP BY SLIDINGWINDOW(ms, 5)`,
  1785. stmt: &ast.SelectStatement{
  1786. Fields: []ast.Field{
  1787. {
  1788. Expr: &ast.FieldRef{Name: "f1", StreamName: ast.DefaultStream},
  1789. Name: "f1",
  1790. AName: ""},
  1791. },
  1792. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1793. Dimensions: ast.Dimensions{
  1794. ast.Dimension{
  1795. Expr: &ast.Window{
  1796. WindowType: ast.SLIDING_WINDOW,
  1797. Length: &ast.IntegerLiteral{Val: 5},
  1798. Interval: &ast.IntegerLiteral{Val: 0},
  1799. },
  1800. },
  1801. },
  1802. },
  1803. },
  1804. {
  1805. s: `SELECT f1 FROM tbl GROUP BY SLIDINGWINDOW(mi, 5, 1)`,
  1806. stmt: nil,
  1807. err: "The arguments for slidingwindow should be 2.\n",
  1808. },
  1809. {
  1810. s: `SELECT f1 FROM tbl GROUP BY SLIDINGWINDOW("mi", 5)`,
  1811. stmt: nil,
  1812. err: "The 1st argument for slidingwindow is expecting timer literal expression. One value of [dd|hh|mi|ss|ms].\n",
  1813. },
  1814. {
  1815. s: `SELECT f1 FROM tbl GROUP BY COUNTWINDOW(10)`,
  1816. stmt: &ast.SelectStatement{
  1817. Fields: []ast.Field{
  1818. {
  1819. Expr: &ast.FieldRef{Name: "f1", StreamName: ast.DefaultStream},
  1820. Name: "f1",
  1821. AName: ""},
  1822. },
  1823. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1824. Dimensions: ast.Dimensions{
  1825. ast.Dimension{
  1826. Expr: &ast.Window{
  1827. WindowType: ast.COUNT_WINDOW,
  1828. Length: &ast.IntegerLiteral{Val: 10},
  1829. },
  1830. },
  1831. },
  1832. },
  1833. },
  1834. {
  1835. s: `SELECT f1 FROM tbl GROUP BY COUNTWINDOW(10, 5)`,
  1836. stmt: &ast.SelectStatement{
  1837. Fields: []ast.Field{
  1838. {
  1839. Expr: &ast.FieldRef{Name: "f1", StreamName: ast.DefaultStream},
  1840. Name: "f1",
  1841. AName: ""},
  1842. },
  1843. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1844. Dimensions: ast.Dimensions{
  1845. ast.Dimension{
  1846. Expr: &ast.Window{
  1847. WindowType: ast.COUNT_WINDOW,
  1848. Length: &ast.IntegerLiteral{Val: 10},
  1849. Interval: &ast.IntegerLiteral{Val: 5},
  1850. },
  1851. },
  1852. },
  1853. },
  1854. },
  1855. {
  1856. s: `SELECT f1 FROM tbl GROUP BY COUNTWINDOW(3, 5)`,
  1857. stmt: nil,
  1858. err: "The second parameter value 5 should be less than the first parameter 3.",
  1859. },
  1860. {
  1861. s: `SELECT * FROM demo GROUP BY COUNTWINDOW(3,1) FILTER( where revenue > 100 )`,
  1862. stmt: &ast.SelectStatement{
  1863. Fields: []ast.Field{
  1864. {
  1865. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  1866. Name: "",
  1867. AName: ""},
  1868. },
  1869. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  1870. Dimensions: ast.Dimensions{
  1871. ast.Dimension{
  1872. Expr: &ast.Window{
  1873. WindowType: ast.COUNT_WINDOW,
  1874. Length: &ast.IntegerLiteral{Val: 3},
  1875. Interval: &ast.IntegerLiteral{Val: 1},
  1876. Filter: &ast.BinaryExpr{
  1877. LHS: &ast.FieldRef{Name: "revenue", StreamName: ast.DefaultStream},
  1878. OP: ast.GT,
  1879. RHS: &ast.IntegerLiteral{Val: 100},
  1880. },
  1881. },
  1882. },
  1883. },
  1884. },
  1885. },
  1886. {
  1887. s: `SELECT * FROM demo GROUP BY department, COUNTWINDOW(3,1) FILTER( where revenue > 100 ), year`,
  1888. stmt: &ast.SelectStatement{
  1889. Fields: []ast.Field{
  1890. {
  1891. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  1892. Name: "",
  1893. AName: ""},
  1894. },
  1895. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  1896. Dimensions: ast.Dimensions{
  1897. ast.Dimension{Expr: &ast.FieldRef{Name: "department", StreamName: ast.DefaultStream}},
  1898. ast.Dimension{
  1899. Expr: &ast.Window{
  1900. WindowType: ast.COUNT_WINDOW,
  1901. Length: &ast.IntegerLiteral{Val: 3},
  1902. Interval: &ast.IntegerLiteral{Val: 1},
  1903. Filter: &ast.BinaryExpr{
  1904. LHS: &ast.FieldRef{Name: "revenue", StreamName: ast.DefaultStream},
  1905. OP: ast.GT,
  1906. RHS: &ast.IntegerLiteral{Val: 100},
  1907. },
  1908. },
  1909. },
  1910. ast.Dimension{Expr: &ast.FieldRef{Name: "year", StreamName: ast.DefaultStream}},
  1911. },
  1912. },
  1913. },
  1914. {
  1915. s: `SELECT * FROM demo GROUP BY department, COUNTWINDOW(3,1) FILTER( where revenue IN (100, 200)), year`,
  1916. stmt: &ast.SelectStatement{
  1917. Fields: []ast.Field{
  1918. {
  1919. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  1920. Name: "",
  1921. AName: ""},
  1922. },
  1923. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  1924. Dimensions: ast.Dimensions{
  1925. ast.Dimension{Expr: &ast.FieldRef{Name: "department", StreamName: ast.DefaultStream}},
  1926. ast.Dimension{
  1927. Expr: &ast.Window{
  1928. WindowType: ast.COUNT_WINDOW,
  1929. Length: &ast.IntegerLiteral{Val: 3},
  1930. Interval: &ast.IntegerLiteral{Val: 1},
  1931. Filter: &ast.BinaryExpr{
  1932. LHS: &ast.FieldRef{Name: "revenue", StreamName: ast.DefaultStream},
  1933. OP: ast.IN,
  1934. RHS: &ast.ValueSetExpr{
  1935. LiteralExprs: []ast.Expr{&ast.IntegerLiteral{Val: 100}, &ast.IntegerLiteral{Val: 200}},
  1936. },
  1937. },
  1938. },
  1939. },
  1940. ast.Dimension{Expr: &ast.FieldRef{Name: "year", StreamName: ast.DefaultStream}},
  1941. },
  1942. },
  1943. },
  1944. //to be supported
  1945. {
  1946. s: `SELECT sum(f1) FILTER( where revenue > 100 ) FROM tbl GROUP BY year`,
  1947. stmt: nil,
  1948. err: "found \"FILTER\", expected FROM.",
  1949. },
  1950. {
  1951. s: `SELECT * FROM demo GROUP BY COUNTWINDOW(3,1) FILTER where revenue > 100`,
  1952. stmt: nil,
  1953. err: "Found \"WHERE\" after FILTER, expect parentheses.",
  1954. },
  1955. {
  1956. s: `SELECT * FROM demo GROUP BY COUNTWINDOW(3,1) where revenue > 100`,
  1957. stmt: nil,
  1958. err: "found \"WHERE\", expected EOF.",
  1959. },
  1960. }
  1961. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  1962. for i, tt := range tests {
  1963. //fmt.Printf("Parsing SQL %q.\n", tt.s)
  1964. stmt, err := NewParser(strings.NewReader(tt.s)).Parse()
  1965. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  1966. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.s, tt.err, err)
  1967. } else if tt.err == "" && !reflect.DeepEqual(tt.stmt, stmt) {
  1968. t.Errorf("%d. %q\n\nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.s, tt.stmt, stmt)
  1969. }
  1970. }
  1971. }
  1972. func TestParser_ParseJsonExpr(t *testing.T) {
  1973. var tests = []struct {
  1974. s string
  1975. stmt *ast.SelectStatement
  1976. err string
  1977. }{
  1978. {
  1979. s: `SELECT children[0] FROM demo`,
  1980. stmt: &ast.SelectStatement{
  1981. Fields: []ast.Field{
  1982. {
  1983. Expr: &ast.BinaryExpr{
  1984. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  1985. OP: ast.SUBSET,
  1986. RHS: &ast.IndexExpr{Index: &ast.IntegerLiteral{Val: 0}},
  1987. },
  1988. Name: "kuiper_field_0",
  1989. AName: ""},
  1990. },
  1991. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  1992. },
  1993. },
  1994. {
  1995. s: `SELECT children[0]->first FROM demo`,
  1996. stmt: &ast.SelectStatement{
  1997. Fields: []ast.Field{
  1998. {
  1999. Expr: &ast.BinaryExpr{
  2000. LHS: &ast.BinaryExpr{
  2001. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  2002. OP: ast.SUBSET,
  2003. RHS: &ast.IndexExpr{Index: &ast.IntegerLiteral{Val: 0}},
  2004. },
  2005. OP: ast.ARROW,
  2006. RHS: &ast.JsonFieldRef{Name: "first"},
  2007. },
  2008. Name: "kuiper_field_0",
  2009. AName: ""},
  2010. },
  2011. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2012. },
  2013. },
  2014. {
  2015. s: `SELECT children->first[2] FROM demo`,
  2016. stmt: &ast.SelectStatement{
  2017. Fields: []ast.Field{
  2018. {
  2019. Expr: &ast.BinaryExpr{
  2020. LHS: &ast.BinaryExpr{
  2021. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  2022. OP: ast.ARROW,
  2023. RHS: &ast.JsonFieldRef{Name: "first"},
  2024. },
  2025. OP: ast.SUBSET,
  2026. RHS: &ast.IndexExpr{Index: &ast.IntegerLiteral{Val: 2}},
  2027. },
  2028. Name: "kuiper_field_0",
  2029. AName: ""},
  2030. },
  2031. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2032. },
  2033. },
  2034. {
  2035. s: `SELECT children->first[2]->test FROM demo`,
  2036. stmt: &ast.SelectStatement{
  2037. Fields: []ast.Field{
  2038. {
  2039. Expr: &ast.BinaryExpr{
  2040. LHS: &ast.BinaryExpr{
  2041. LHS: &ast.BinaryExpr{
  2042. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  2043. OP: ast.ARROW,
  2044. RHS: &ast.JsonFieldRef{Name: "first"},
  2045. },
  2046. OP: ast.SUBSET,
  2047. RHS: &ast.IndexExpr{Index: &ast.IntegerLiteral{Val: 2}},
  2048. },
  2049. OP: ast.ARROW,
  2050. RHS: &ast.JsonFieldRef{Name: "test"},
  2051. },
  2052. Name: "kuiper_field_0",
  2053. AName: ""},
  2054. },
  2055. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2056. },
  2057. },
  2058. {
  2059. s: `SELECT children[0:1] FROM demo`,
  2060. stmt: &ast.SelectStatement{
  2061. Fields: []ast.Field{
  2062. {
  2063. Expr: &ast.BinaryExpr{
  2064. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  2065. OP: ast.SUBSET,
  2066. RHS: &ast.ColonExpr{Start: &ast.IntegerLiteral{Val: 0}, End: &ast.IntegerLiteral{Val: 1}},
  2067. },
  2068. Name: "kuiper_field_0",
  2069. AName: ""},
  2070. },
  2071. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2072. },
  2073. },
  2074. {
  2075. s: `SELECT children[:1] FROM demo`,
  2076. stmt: &ast.SelectStatement{
  2077. Fields: []ast.Field{
  2078. {
  2079. Expr: &ast.BinaryExpr{
  2080. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  2081. OP: ast.SUBSET,
  2082. RHS: &ast.ColonExpr{Start: &ast.IntegerLiteral{Val: 0}, End: &ast.IntegerLiteral{Val: 1}},
  2083. },
  2084. Name: "kuiper_field_0",
  2085. AName: ""},
  2086. },
  2087. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2088. },
  2089. },
  2090. {
  2091. s: `SELECT children[:] FROM demo`,
  2092. stmt: &ast.SelectStatement{
  2093. Fields: []ast.Field{
  2094. {
  2095. Expr: &ast.BinaryExpr{
  2096. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  2097. OP: ast.SUBSET,
  2098. RHS: &ast.ColonExpr{Start: &ast.IntegerLiteral{Val: 0}, End: &ast.IntegerLiteral{Val: math.MinInt32}},
  2099. },
  2100. Name: "kuiper_field_0",
  2101. AName: ""},
  2102. },
  2103. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2104. },
  2105. },
  2106. {
  2107. s: `SELECT children[2:] AS c FROM demo`,
  2108. stmt: &ast.SelectStatement{
  2109. Fields: []ast.Field{
  2110. {
  2111. Expr: &ast.BinaryExpr{
  2112. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  2113. OP: ast.SUBSET,
  2114. RHS: &ast.ColonExpr{Start: &ast.IntegerLiteral{Val: 2}, End: &ast.IntegerLiteral{Val: math.MinInt32}},
  2115. },
  2116. Name: "",
  2117. AName: "c"},
  2118. },
  2119. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2120. },
  2121. },
  2122. {
  2123. s: `SELECT children[2:]->first AS c FROM demo`,
  2124. stmt: &ast.SelectStatement{
  2125. Fields: []ast.Field{
  2126. {
  2127. Expr: &ast.BinaryExpr{
  2128. 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}}},
  2129. OP: ast.ARROW,
  2130. RHS: &ast.JsonFieldRef{Name: "first"},
  2131. },
  2132. Name: "",
  2133. AName: "c"},
  2134. },
  2135. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2136. },
  2137. },
  2138. {
  2139. s: `SELECT demo.* FROM demo`,
  2140. stmt: &ast.SelectStatement{
  2141. Fields: []ast.Field{
  2142. {
  2143. Expr: &ast.FieldRef{StreamName: ast.StreamName("demo"), Name: "*"},
  2144. Name: "*",
  2145. AName: ""},
  2146. },
  2147. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2148. },
  2149. },
  2150. {
  2151. s: `SELECT demo.children[2:]->first AS c FROM demo`,
  2152. stmt: &ast.SelectStatement{
  2153. Fields: []ast.Field{
  2154. {
  2155. Expr: &ast.BinaryExpr{
  2156. 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}}},
  2157. OP: ast.ARROW,
  2158. RHS: &ast.JsonFieldRef{Name: "first"},
  2159. },
  2160. Name: "",
  2161. AName: "c"},
  2162. },
  2163. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2164. },
  2165. },
  2166. {
  2167. s: `SELECT lower(demo.children[2:]->first) AS c FROM demo`,
  2168. stmt: &ast.SelectStatement{
  2169. Fields: []ast.Field{
  2170. {
  2171. Expr: &ast.Call{
  2172. Name: "lower",
  2173. Args: []ast.Expr{
  2174. &ast.BinaryExpr{
  2175. 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}}},
  2176. OP: ast.ARROW,
  2177. RHS: &ast.JsonFieldRef{Name: "first"},
  2178. },
  2179. },
  2180. },
  2181. Name: "lower",
  2182. AName: "c"},
  2183. },
  2184. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2185. },
  2186. },
  2187. {
  2188. s: `SELECT children[:1] FROM demo WHERE abc[0] > 12`,
  2189. stmt: &ast.SelectStatement{
  2190. Fields: []ast.Field{
  2191. {
  2192. Expr: &ast.BinaryExpr{
  2193. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  2194. OP: ast.SUBSET,
  2195. RHS: &ast.ColonExpr{Start: &ast.IntegerLiteral{Val: 0}, End: &ast.IntegerLiteral{Val: 1}},
  2196. },
  2197. Name: "kuiper_field_0",
  2198. AName: ""},
  2199. },
  2200. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2201. Condition: &ast.BinaryExpr{
  2202. LHS: &ast.BinaryExpr{
  2203. LHS: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream},
  2204. OP: ast.SUBSET,
  2205. RHS: &ast.IndexExpr{Index: &ast.IntegerLiteral{Val: 0}},
  2206. },
  2207. OP: ast.GT,
  2208. RHS: &ast.IntegerLiteral{Val: 12},
  2209. },
  2210. },
  2211. },
  2212. {
  2213. s: `SELECT children[:1] FROM demo WHERE abc[0] IN demo.children[2:]->first`,
  2214. stmt: &ast.SelectStatement{
  2215. Fields: []ast.Field{
  2216. {
  2217. Expr: &ast.BinaryExpr{
  2218. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  2219. OP: ast.SUBSET,
  2220. RHS: &ast.ColonExpr{Start: &ast.IntegerLiteral{Val: 0}, End: &ast.IntegerLiteral{Val: 1}},
  2221. },
  2222. Name: "kuiper_field_0",
  2223. AName: ""},
  2224. },
  2225. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2226. Condition: &ast.BinaryExpr{
  2227. LHS: &ast.BinaryExpr{
  2228. LHS: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream},
  2229. OP: ast.SUBSET,
  2230. RHS: &ast.IndexExpr{Index: &ast.IntegerLiteral{Val: 0}},
  2231. },
  2232. OP: ast.IN,
  2233. RHS: &ast.BinaryExpr{
  2234. 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}}},
  2235. OP: ast.ARROW,
  2236. RHS: &ast.JsonFieldRef{Name: "first"},
  2237. },
  2238. },
  2239. },
  2240. },
  2241. {
  2242. s: `SELECT demo.children.first AS c FROM demo`,
  2243. stmt: nil,
  2244. err: "Too many field names. Please use -> to reference keys in struct.\n",
  2245. },
  2246. {
  2247. s: `SELECT children[index] 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.FieldRef{Name: "index", StreamName: ast.DefaultStream}},
  2255. },
  2256. Name: "kuiper_field_0",
  2257. AName: ""},
  2258. },
  2259. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2260. },
  2261. },
  2262. }
  2263. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  2264. for i, tt := range tests {
  2265. stmt, err := NewParser(strings.NewReader(tt.s)).Parse()
  2266. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  2267. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.s, tt.err, err)
  2268. } else if tt.err == "" && !reflect.DeepEqual(tt.stmt, stmt) {
  2269. t.Errorf("%d. %q\n\nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.s, tt.stmt, stmt)
  2270. }
  2271. }
  2272. }
  2273. func TestParser_ParseJoins(t *testing.T) {
  2274. var tests = []struct {
  2275. s string
  2276. stmt *ast.SelectStatement
  2277. err string
  2278. }{
  2279. {
  2280. s: `SELECT * FROM topic/sensor1 LEFT JOIN topic1 ON f=k`,
  2281. stmt: &ast.SelectStatement{
  2282. Fields: []ast.Field{
  2283. {
  2284. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  2285. Name: "",
  2286. AName: ""},
  2287. },
  2288. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  2289. Joins: []ast.Join{
  2290. {
  2291. Name: "topic1", Alias: "", JoinType: ast.LEFT_JOIN, Expr: &ast.BinaryExpr{
  2292. LHS: &ast.FieldRef{Name: "f", StreamName: ast.DefaultStream},
  2293. OP: ast.EQ,
  2294. RHS: &ast.FieldRef{Name: "k", StreamName: ast.DefaultStream},
  2295. },
  2296. },
  2297. },
  2298. },
  2299. },
  2300. {
  2301. s: `SELECT * FROM topic/sensor1 AS t1 INNER JOIN topic1 AS t2 ON f=k`,
  2302. stmt: &ast.SelectStatement{
  2303. Fields: []ast.Field{
  2304. {
  2305. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  2306. Name: "",
  2307. AName: ""},
  2308. },
  2309. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
  2310. Joins: []ast.Join{
  2311. {
  2312. Name: "topic1", Alias: "t2", JoinType: ast.INNER_JOIN, Expr: &ast.BinaryExpr{
  2313. LHS: &ast.FieldRef{Name: "f", StreamName: ast.DefaultStream},
  2314. OP: ast.EQ,
  2315. RHS: &ast.FieldRef{Name: "k", StreamName: ast.DefaultStream},
  2316. },
  2317. },
  2318. },
  2319. },
  2320. },
  2321. {
  2322. s: `SELECT * FROM topic/sensor1 AS t1 LEFT JOIN topic1/sensor2 AS t2 ON f=k`,
  2323. stmt: &ast.SelectStatement{
  2324. Fields: []ast.Field{
  2325. {
  2326. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  2327. Name: "",
  2328. AName: ""},
  2329. },
  2330. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
  2331. Joins: []ast.Join{
  2332. {
  2333. Name: "topic1/sensor2", Alias: "t2", JoinType: ast.LEFT_JOIN, Expr: &ast.BinaryExpr{
  2334. LHS: &ast.FieldRef{Name: "f", StreamName: ast.DefaultStream},
  2335. OP: ast.EQ,
  2336. RHS: &ast.FieldRef{Name: "k", StreamName: ast.DefaultStream},
  2337. },
  2338. },
  2339. },
  2340. },
  2341. },
  2342. {
  2343. s: `SELECT t1.name FROM topic/sensor1 AS t1 LEFT JOIN topic1/sensor2 AS t2 ON f=k`,
  2344. stmt: &ast.SelectStatement{
  2345. Fields: []ast.Field{
  2346. {
  2347. Expr: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "name"},
  2348. Name: "name",
  2349. AName: ""},
  2350. },
  2351. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
  2352. Joins: []ast.Join{
  2353. {
  2354. Name: "topic1/sensor2", Alias: "t2", JoinType: ast.LEFT_JOIN, Expr: &ast.BinaryExpr{
  2355. LHS: &ast.FieldRef{Name: "f", StreamName: ast.DefaultStream},
  2356. OP: ast.EQ,
  2357. RHS: &ast.FieldRef{Name: "k", StreamName: ast.DefaultStream},
  2358. },
  2359. },
  2360. },
  2361. },
  2362. },
  2363. {
  2364. s: `SELECT t1.name FROM topic/sensor1 AS t1 LEFT JOIN topic1/sensor2 AS t2 ON t1.f=t2.k`,
  2365. stmt: &ast.SelectStatement{
  2366. Fields: []ast.Field{
  2367. {
  2368. Expr: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "name"},
  2369. Name: "name",
  2370. AName: ""},
  2371. },
  2372. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
  2373. Joins: []ast.Join{
  2374. {
  2375. Name: "topic1/sensor2", Alias: "t2", JoinType: ast.LEFT_JOIN, Expr: &ast.BinaryExpr{
  2376. LHS: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "f"},
  2377. OP: ast.EQ,
  2378. RHS: &ast.FieldRef{StreamName: ast.StreamName("t2"), Name: "k"},
  2379. },
  2380. },
  2381. },
  2382. },
  2383. },
  2384. {
  2385. s: `SELECT t1.name FROM topic/sensor1 AS t1 RIGHT JOIN topic1/sensor2 AS t2 ON t1.f=t2.k`,
  2386. stmt: &ast.SelectStatement{
  2387. Fields: []ast.Field{
  2388. {
  2389. Expr: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "name"},
  2390. Name: "name",
  2391. AName: ""},
  2392. },
  2393. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
  2394. Joins: []ast.Join{
  2395. {
  2396. Name: "topic1/sensor2", Alias: "t2", JoinType: ast.RIGHT_JOIN, Expr: &ast.BinaryExpr{
  2397. LHS: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "f"},
  2398. OP: ast.EQ,
  2399. RHS: &ast.FieldRef{StreamName: ast.StreamName("t2"), Name: "k"},
  2400. },
  2401. },
  2402. },
  2403. },
  2404. },
  2405. {
  2406. s: `SELECT t1.name FROM topic/sensor1 AS t1 FULL JOIN topic1/sensor2 AS t2 ON t1.f=t2.k`,
  2407. stmt: &ast.SelectStatement{
  2408. Fields: []ast.Field{
  2409. {
  2410. Expr: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "name"},
  2411. Name: "name",
  2412. AName: ""},
  2413. },
  2414. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
  2415. Joins: []ast.Join{
  2416. {
  2417. Name: "topic1/sensor2", Alias: "t2", JoinType: ast.FULL_JOIN, Expr: &ast.BinaryExpr{
  2418. LHS: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "f"},
  2419. OP: ast.EQ,
  2420. RHS: &ast.FieldRef{StreamName: ast.StreamName("t2"), Name: "k"},
  2421. },
  2422. },
  2423. },
  2424. },
  2425. },
  2426. {
  2427. s: `SELECT t1.name FROM topic/sensor1 AS t1 CROSS JOIN topic1/sensor2 AS t2 ON t1.f=t2.k`,
  2428. stmt: nil,
  2429. err: "On expression is not required for cross join type.\n",
  2430. },
  2431. {
  2432. s: `SELECT t1.name FROM topic/sensor1 AS t1 CROSS JOIN topic1/sensor2 AS t2`,
  2433. stmt: &ast.SelectStatement{
  2434. Fields: []ast.Field{
  2435. {
  2436. Expr: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "name"},
  2437. Name: "name",
  2438. AName: ""},
  2439. },
  2440. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
  2441. Joins: []ast.Join{
  2442. {
  2443. Name: "topic1/sensor2", Alias: "t2", JoinType: ast.CROSS_JOIN, Expr: nil,
  2444. },
  2445. },
  2446. },
  2447. },
  2448. {
  2449. s: `SELECT demo.*, demo2.* FROM demo LEFT JOIN demo2 on demo.f1 = demo2.f2`,
  2450. stmt: &ast.SelectStatement{
  2451. Fields: []ast.Field{
  2452. {
  2453. Expr: &ast.FieldRef{StreamName: ast.StreamName("demo"), Name: "*"},
  2454. Name: "*",
  2455. AName: ""},
  2456. {
  2457. Expr: &ast.FieldRef{StreamName: ast.StreamName("demo2"), Name: "*"},
  2458. Name: "*",
  2459. AName: ""},
  2460. },
  2461. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2462. Joins: []ast.Join{
  2463. {
  2464. Name: "demo2", Alias: "", JoinType: ast.LEFT_JOIN, Expr: &ast.BinaryExpr{
  2465. LHS: &ast.FieldRef{StreamName: ast.StreamName("demo"), Name: "f1"},
  2466. OP: ast.EQ,
  2467. RHS: &ast.FieldRef{StreamName: ast.StreamName("demo2"), Name: "f2"},
  2468. },
  2469. },
  2470. },
  2471. },
  2472. },
  2473. }
  2474. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  2475. for i, tt := range tests {
  2476. stmt, err := NewParser(strings.NewReader(tt.s)).Parse()
  2477. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  2478. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.s, tt.err, err)
  2479. } else if tt.err == "" && !reflect.DeepEqual(tt.stmt, stmt) {
  2480. t.Errorf("%d. %q\n\nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.s, tt.stmt, stmt)
  2481. }
  2482. }
  2483. }
  2484. func TestParser_ParseStatements(t *testing.T) {
  2485. var tests = []struct {
  2486. s string
  2487. stmts []ast.SelectStatement
  2488. err string
  2489. }{
  2490. {s: "SELECT name FROM tbl;\nSELECT name FROM topic/sensor1\n",
  2491. stmts: []ast.SelectStatement{
  2492. {
  2493. Fields: []ast.Field{
  2494. {
  2495. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  2496. Name: "name",
  2497. AName: ""},
  2498. },
  2499. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  2500. },
  2501. {
  2502. Fields: []ast.Field{
  2503. {
  2504. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  2505. Name: "name",
  2506. AName: ""},
  2507. },
  2508. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  2509. },
  2510. },
  2511. },
  2512. {s: "SELECT name FROM tbl;\nSELECT name FROM topic/sensor1;\n--SELECT comment FROM topic/comment",
  2513. stmts: []ast.SelectStatement{
  2514. {
  2515. Fields: []ast.Field{
  2516. {
  2517. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  2518. Name: "name",
  2519. AName: ""},
  2520. },
  2521. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  2522. },
  2523. {
  2524. Fields: []ast.Field{
  2525. {
  2526. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  2527. Name: "name",
  2528. AName: ""},
  2529. },
  2530. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  2531. },
  2532. },
  2533. },
  2534. }
  2535. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  2536. for i, tt := range tests {
  2537. stmts, err := NewParser(strings.NewReader(tt.s)).ParseQueries()
  2538. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  2539. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.s, tt.err, err)
  2540. } else if tt.err == "" && !reflect.DeepEqual(tt.stmts, stmts) {
  2541. t.Errorf("%d. %q\n\nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.s, tt.stmts, stmts)
  2542. }
  2543. }
  2544. }