parser_test.go 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280
  1. // Copyright 2021 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. "strings"
  22. "testing"
  23. )
  24. // Ensure the parser can parse strings into Statement ASTs.
  25. func TestParser_ParseStatement(t *testing.T) {
  26. var tests = []struct {
  27. s string
  28. stmt *ast.SelectStatement
  29. err string
  30. }{
  31. {
  32. s: `SELECT name FROM tbl`,
  33. stmt: &ast.SelectStatement{
  34. Fields: []ast.Field{
  35. {
  36. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  37. Name: "name",
  38. AName: ""},
  39. },
  40. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  41. },
  42. },
  43. {
  44. s: "SELECT `select` FROM tbl",
  45. stmt: &ast.SelectStatement{
  46. Fields: []ast.Field{
  47. {
  48. Expr: &ast.FieldRef{Name: "select", StreamName: ast.DefaultStream},
  49. Name: "select",
  50. AName: ""},
  51. },
  52. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  53. },
  54. },
  55. {
  56. s: `SELECT name FROM topic/sensor1`,
  57. stmt: &ast.SelectStatement{
  58. Fields: []ast.Field{
  59. {
  60. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  61. Name: "name",
  62. AName: ""},
  63. },
  64. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  65. },
  66. },
  67. {
  68. s: `SELECT t1.name FROM topic/sensor1 AS t1`,
  69. stmt: &ast.SelectStatement{
  70. Fields: []ast.Field{
  71. {
  72. Expr: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "name"},
  73. Name: "name",
  74. AName: ""},
  75. },
  76. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
  77. },
  78. },
  79. {
  80. s: "SELECT t1.name FROM topic/sensor1 AS `join`",
  81. stmt: &ast.SelectStatement{
  82. Fields: []ast.Field{
  83. {
  84. Expr: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "name"},
  85. Name: "name",
  86. AName: ""},
  87. },
  88. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "join"}},
  89. },
  90. },
  91. {
  92. s: `SELECT name FROM topic/sensor1 AS t1`,
  93. stmt: &ast.SelectStatement{
  94. Fields: []ast.Field{
  95. {
  96. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  97. Name: "name",
  98. AName: ""},
  99. },
  100. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
  101. },
  102. },
  103. {
  104. s: `SELECT name FROM topic/sensor1/#`,
  105. stmt: &ast.SelectStatement{
  106. Fields: []ast.Field{
  107. {
  108. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  109. Name: "name",
  110. AName: ""},
  111. },
  112. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1/#"}},
  113. },
  114. },
  115. {
  116. s: `SELECT name FROM topic/sensor1/# AS t2 `,
  117. stmt: &ast.SelectStatement{
  118. Fields: []ast.Field{
  119. {
  120. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  121. Name: "name",
  122. AName: ""},
  123. },
  124. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1/#", Alias: "t2"}},
  125. },
  126. },
  127. {
  128. s: `SELECT name FROM /topic/sensor1/#`,
  129. stmt: &ast.SelectStatement{
  130. Fields: []ast.Field{
  131. {
  132. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  133. Name: "name",
  134. AName: ""},
  135. },
  136. Sources: []ast.Source{&ast.Table{Name: "/topic/sensor1/#"}},
  137. },
  138. },
  139. {
  140. s: `SELECT name FROM /topic/sensor1/#/`,
  141. stmt: &ast.SelectStatement{
  142. Fields: []ast.Field{
  143. {
  144. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  145. Name: "name",
  146. AName: ""},
  147. },
  148. Sources: []ast.Source{&ast.Table{Name: "/topic/sensor1/#/"}},
  149. },
  150. },
  151. {
  152. s: `SELECT name FROM /topic/sensor1/+/temp1/`,
  153. stmt: &ast.SelectStatement{
  154. Fields: []ast.Field{
  155. {
  156. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  157. Name: "name",
  158. AName: ""},
  159. },
  160. Sources: []ast.Source{&ast.Table{Name: "/topic/sensor1/+/temp1/"}},
  161. },
  162. },
  163. {
  164. s: `SELECT name FROM topic/sensor1/+/temp`,
  165. stmt: &ast.SelectStatement{
  166. Fields: []ast.Field{
  167. {
  168. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  169. Name: "name",
  170. AName: ""},
  171. },
  172. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1/+/temp"}},
  173. },
  174. },
  175. {
  176. s: `SELECT * FROM tbl`,
  177. stmt: &ast.SelectStatement{
  178. Fields: []ast.Field{
  179. {
  180. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  181. Name: "",
  182. AName: ""},
  183. },
  184. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  185. },
  186. },
  187. {
  188. s: `SELECT a,b FROM tbl`,
  189. stmt: &ast.SelectStatement{
  190. Fields: []ast.Field{
  191. {Expr: &ast.FieldRef{Name: "a", StreamName: ast.DefaultStream}, Name: "a", AName: ""},
  192. {Expr: &ast.FieldRef{Name: "b", StreamName: ast.DefaultStream}, Name: "b", AName: ""},
  193. },
  194. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  195. },
  196. },
  197. {
  198. s: `SELECT a, b,c FROM tbl`,
  199. stmt: &ast.SelectStatement{
  200. Fields: []ast.Field{
  201. {Expr: &ast.FieldRef{Name: "a", StreamName: ast.DefaultStream}, Name: "a", AName: ""},
  202. {Expr: &ast.FieldRef{Name: "b", StreamName: ast.DefaultStream}, Name: "b", AName: ""},
  203. {Expr: &ast.FieldRef{Name: "c", StreamName: ast.DefaultStream}, Name: "c", AName: ""},
  204. },
  205. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  206. },
  207. },
  208. {
  209. s: `SELECT a AS alias FROM tbl`,
  210. stmt: &ast.SelectStatement{
  211. Fields: []ast.Field{{Expr: &ast.FieldRef{Name: "a", StreamName: ast.DefaultStream}, Name: "a", AName: "alias"}},
  212. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  213. },
  214. },
  215. {
  216. s: `SELECT a AS alias1, b as Alias2 FROM tbl`,
  217. stmt: &ast.SelectStatement{
  218. Fields: []ast.Field{
  219. {Expr: &ast.FieldRef{Name: "a", StreamName: ast.DefaultStream}, Name: "a", AName: "alias1"},
  220. {Expr: &ast.FieldRef{Name: "b", StreamName: ast.DefaultStream}, Name: "b", AName: "Alias2"},
  221. },
  222. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  223. },
  224. },
  225. {
  226. s: `SELECT length("test") FROM tbl`,
  227. stmt: &ast.SelectStatement{
  228. Fields: []ast.Field{
  229. {
  230. AName: "",
  231. Name: "length",
  232. Expr: &ast.Call{
  233. Name: "length",
  234. Args: []ast.Expr{&ast.StringLiteral{Val: "test"}},
  235. },
  236. },
  237. },
  238. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  239. },
  240. },
  241. {
  242. s: `SELECT length(test) FROM tbl`,
  243. stmt: &ast.SelectStatement{
  244. Fields: []ast.Field{
  245. {
  246. AName: "",
  247. Name: "length",
  248. Expr: &ast.Call{
  249. Name: "length",
  250. Args: []ast.Expr{&ast.FieldRef{Name: "test", StreamName: ast.DefaultStream}},
  251. },
  252. },
  253. },
  254. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  255. },
  256. },
  257. {
  258. s: `SELECT sin(123) FROM tbl`,
  259. stmt: &ast.SelectStatement{
  260. Fields: []ast.Field{
  261. {
  262. AName: "",
  263. Name: "sin",
  264. Expr: &ast.Call{
  265. Name: "sin",
  266. Args: []ast.Expr{&ast.IntegerLiteral{Val: 123}},
  267. },
  268. },
  269. },
  270. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  271. },
  272. },
  273. {
  274. s: `SELECT lpad("abc", 123) FROM tbl`,
  275. stmt: &ast.SelectStatement{
  276. Fields: []ast.Field{
  277. {
  278. AName: "",
  279. Name: "lpad",
  280. Expr: &ast.Call{
  281. Name: "lpad",
  282. Args: []ast.Expr{&ast.StringLiteral{Val: "abc"}, &ast.IntegerLiteral{Val: 123}},
  283. },
  284. },
  285. },
  286. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  287. },
  288. },
  289. {
  290. s: `SELECT newuuid() FROM tbl`,
  291. stmt: &ast.SelectStatement{
  292. Fields: []ast.Field{
  293. {
  294. AName: "",
  295. Name: "newuuid",
  296. Expr: &ast.Call{
  297. Name: "newuuid",
  298. Args: nil,
  299. },
  300. },
  301. },
  302. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  303. },
  304. },
  305. {
  306. s: `SELECT indexof("abc", field1) FROM tbl`,
  307. stmt: &ast.SelectStatement{
  308. Fields: []ast.Field{
  309. {
  310. AName: "",
  311. Name: "indexof",
  312. Expr: &ast.Call{
  313. Name: "indexof",
  314. Args: []ast.Expr{
  315. &ast.StringLiteral{Val: "abc"},
  316. &ast.FieldRef{Name: "field1", StreamName: ast.DefaultStream},
  317. },
  318. },
  319. },
  320. },
  321. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  322. },
  323. },
  324. {
  325. s: `SELECT lpad(lower(test),1) FROM tbl`,
  326. stmt: &ast.SelectStatement{
  327. Fields: []ast.Field{
  328. {
  329. AName: "",
  330. Name: "lpad",
  331. Expr: &ast.Call{
  332. Name: "lpad",
  333. Args: []ast.Expr{
  334. &ast.Call{
  335. Name: "lower",
  336. Args: []ast.Expr{
  337. &ast.FieldRef{Name: "test", StreamName: ast.DefaultStream},
  338. },
  339. },
  340. &ast.IntegerLiteral{Val: 1},
  341. },
  342. },
  343. },
  344. },
  345. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  346. },
  347. },
  348. {
  349. s: `SELECT lpad(lower(test),1) AS field1 FROM tbl`,
  350. stmt: &ast.SelectStatement{
  351. Fields: []ast.Field{
  352. {
  353. AName: "field1",
  354. Name: "lpad",
  355. Expr: &ast.Call{
  356. Name: "lpad",
  357. Args: []ast.Expr{
  358. &ast.Call{
  359. Name: "lower",
  360. Args: []ast.Expr{
  361. &ast.FieldRef{Name: "test", StreamName: ast.DefaultStream},
  362. },
  363. },
  364. &ast.IntegerLiteral{Val: 1},
  365. },
  366. },
  367. },
  368. },
  369. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  370. },
  371. },
  372. {
  373. s: `SELECT length(lower("test")) FROM tbl`,
  374. stmt: &ast.SelectStatement{
  375. Fields: []ast.Field{
  376. {
  377. AName: "",
  378. Name: "length",
  379. Expr: &ast.Call{
  380. Name: "length",
  381. Args: []ast.Expr{
  382. &ast.Call{
  383. Name: "lower",
  384. Args: []ast.Expr{
  385. &ast.StringLiteral{Val: "test"},
  386. },
  387. },
  388. },
  389. },
  390. },
  391. },
  392. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  393. },
  394. },
  395. {
  396. s: `SELECT count(*) FROM tbl`,
  397. stmt: &ast.SelectStatement{
  398. Fields: []ast.Field{
  399. {
  400. AName: "",
  401. Name: "count",
  402. Expr: &ast.Call{
  403. Name: "count",
  404. Args: []ast.Expr{&ast.Wildcard{Token: ast.ASTERISK}},
  405. },
  406. },
  407. },
  408. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  409. },
  410. },
  411. {
  412. s: `SELECT count(*, f1) FROM tbl`,
  413. stmt: nil,
  414. err: `found ",", expected right paren.`,
  415. },
  416. {
  417. s: `SELECT deduplicate(temperature, false) FROM tbl`,
  418. stmt: &ast.SelectStatement{
  419. Fields: []ast.Field{
  420. {
  421. AName: "",
  422. Name: "deduplicate",
  423. Expr: &ast.Call{
  424. Name: "deduplicate",
  425. Args: []ast.Expr{&ast.Wildcard{Token: ast.ASTERISK}, &ast.FieldRef{Name: "temperature", StreamName: ast.DefaultStream}, &ast.BooleanLiteral{Val: false}},
  426. },
  427. },
  428. },
  429. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  430. },
  431. },
  432. {
  433. s: `SELECT "abc" FROM tbl`,
  434. stmt: &ast.SelectStatement{
  435. Fields: []ast.Field{{AName: "", Name: "", Expr: &ast.StringLiteral{Val: "abc"}}},
  436. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  437. },
  438. },
  439. {
  440. s: `SELECT "abc" AS field1 FROM tbl`,
  441. stmt: &ast.SelectStatement{
  442. Fields: []ast.Field{{AName: "field1", Name: "", Expr: &ast.StringLiteral{Val: "abc"}}},
  443. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  444. },
  445. },
  446. {
  447. s: `SELECT field0, "abc" AS field1, field2 FROM tbl`,
  448. stmt: &ast.SelectStatement{
  449. Fields: []ast.Field{
  450. {AName: "", Name: "field0", Expr: &ast.FieldRef{Name: "field0", StreamName: ast.DefaultStream}},
  451. {AName: "field1", Name: "", Expr: &ast.StringLiteral{Val: "abc"}},
  452. {AName: "", Name: "field2", Expr: &ast.FieldRef{Name: "field2", StreamName: ast.DefaultStream}}},
  453. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  454. },
  455. },
  456. {
  457. s: `SELECT * AS alias FROM tbl`,
  458. stmt: nil,
  459. err: `found "AS", expected FROM.`,
  460. },
  461. {
  462. s: `SELECT *, FROM tbl`,
  463. stmt: nil,
  464. err: `found ",", expected FROM.`,
  465. },
  466. {
  467. s: `SELECTname FROM tbl`,
  468. stmt: nil,
  469. err: `Found "SELECTname", Expected SELECT.` + "\n",
  470. },
  471. {
  472. s: `SELECT abc FROM tbl WHERE abc > 12 `,
  473. stmt: &ast.SelectStatement{
  474. Fields: []ast.Field{{AName: "", Name: "abc", Expr: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream}}},
  475. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  476. Condition: &ast.BinaryExpr{
  477. LHS: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream},
  478. OP: ast.GT,
  479. RHS: &ast.IntegerLiteral{Val: 12},
  480. },
  481. },
  482. },
  483. {
  484. s: `SELECT abc FROM tbl WHERE abc = "hello" `,
  485. stmt: &ast.SelectStatement{
  486. Fields: []ast.Field{{AName: "", Name: "abc", Expr: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream}}},
  487. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  488. Condition: &ast.BinaryExpr{
  489. LHS: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream},
  490. OP: ast.EQ,
  491. RHS: &ast.StringLiteral{Val: "hello"},
  492. },
  493. },
  494. },
  495. {
  496. s: `SELECT t1.abc FROM tbl AS t1 WHERE t1.abc = "hello" `,
  497. stmt: &ast.SelectStatement{
  498. Fields: []ast.Field{{AName: "", Name: "abc", Expr: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "abc"}}},
  499. Sources: []ast.Source{&ast.Table{Name: "tbl", Alias: "t1"}},
  500. Condition: &ast.BinaryExpr{
  501. LHS: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "abc"},
  502. OP: ast.EQ,
  503. RHS: &ast.StringLiteral{Val: "hello"},
  504. },
  505. },
  506. },
  507. {
  508. s: `SELECT abc, "fff" AS fa FROM tbl WHERE fa >= 5 `,
  509. stmt: &ast.SelectStatement{
  510. Fields: []ast.Field{{AName: "", Name: "abc", Expr: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream}}, {AName: "fa", Name: "", Expr: &ast.StringLiteral{Val: "fff"}}},
  511. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  512. Condition: &ast.BinaryExpr{
  513. LHS: &ast.FieldRef{Name: "fa", StreamName: ast.DefaultStream},
  514. OP: ast.GTE,
  515. RHS: &ast.IntegerLiteral{Val: 5},
  516. },
  517. },
  518. },
  519. {
  520. s: `SELECT field2 FROM tbl WHERE field2 != 5 `,
  521. stmt: &ast.SelectStatement{
  522. Fields: []ast.Field{{AName: "", Name: "field2", Expr: &ast.FieldRef{Name: "field2", StreamName: ast.DefaultStream}}},
  523. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  524. Condition: &ast.BinaryExpr{
  525. LHS: &ast.FieldRef{Name: "field2", StreamName: ast.DefaultStream},
  526. OP: ast.NEQ,
  527. RHS: &ast.IntegerLiteral{Val: 5},
  528. },
  529. },
  530. },
  531. {
  532. s: `SELECT field2 FROM tbl WHERE field2 ! = 5 `, //Add space char in expression
  533. stmt: &ast.SelectStatement{
  534. Fields: []ast.Field{{AName: "", Name: "field2", Expr: &ast.FieldRef{Name: "field2", StreamName: ast.DefaultStream}}},
  535. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  536. Condition: &ast.BinaryExpr{
  537. LHS: &ast.FieldRef{Name: "field2", StreamName: ast.DefaultStream},
  538. OP: ast.NEQ,
  539. RHS: &ast.IntegerLiteral{Val: 5},
  540. },
  541. },
  542. },
  543. {
  544. s: `SELECT *f FROM tbl`,
  545. stmt: nil,
  546. err: `found "f", expected FROM.`,
  547. },
  548. ////TODO
  549. //{
  550. // s: `SELECT *from FROM tbl`,
  551. // stmt: nil,
  552. // err: `found "f", expected FROM.`,
  553. //},
  554. {
  555. s: `SELECT abc+2 FROM tbl`,
  556. stmt: &ast.SelectStatement{
  557. Fields: []ast.Field{
  558. {
  559. AName: "",
  560. Name: "",
  561. Expr: &ast.BinaryExpr{
  562. LHS: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream},
  563. OP: ast.ADD,
  564. RHS: &ast.IntegerLiteral{Val: 2},
  565. },
  566. },
  567. },
  568. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  569. },
  570. },
  571. {
  572. s: `SELECT t1.abc+2 FROM tbl AS t1`,
  573. stmt: &ast.SelectStatement{
  574. Fields: []ast.Field{
  575. {
  576. AName: "",
  577. Name: "",
  578. Expr: &ast.BinaryExpr{
  579. LHS: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "abc"},
  580. OP: ast.ADD,
  581. RHS: &ast.IntegerLiteral{Val: 2},
  582. },
  583. },
  584. },
  585. Sources: []ast.Source{&ast.Table{Name: "tbl", Alias: "t1"}},
  586. },
  587. },
  588. {
  589. s: `SELECT abc + "hello" FROM tbl`,
  590. stmt: &ast.SelectStatement{
  591. Fields: []ast.Field{
  592. {
  593. AName: "",
  594. Name: "",
  595. Expr: &ast.BinaryExpr{
  596. LHS: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream},
  597. OP: ast.ADD,
  598. RHS: &ast.StringLiteral{Val: "hello"},
  599. },
  600. },
  601. },
  602. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  603. },
  604. },
  605. {
  606. s: `SELECT abc*2 + 3 FROM tbl`,
  607. stmt: &ast.SelectStatement{
  608. Fields: []ast.Field{
  609. {
  610. AName: "",
  611. Name: "",
  612. Expr: &ast.BinaryExpr{
  613. LHS: &ast.BinaryExpr{
  614. LHS: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream},
  615. OP: ast.MUL,
  616. RHS: &ast.IntegerLiteral{Val: 2},
  617. },
  618. OP: ast.ADD,
  619. RHS: &ast.IntegerLiteral{Val: 3},
  620. },
  621. },
  622. },
  623. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  624. },
  625. },
  626. {
  627. s: `SELECT ln(abc*2 + 3) FROM tbl`,
  628. stmt: &ast.SelectStatement{
  629. Fields: []ast.Field{
  630. {
  631. AName: "",
  632. Name: "ln",
  633. Expr: &ast.Call{
  634. Name: "ln",
  635. Args: []ast.Expr{
  636. &ast.BinaryExpr{
  637. LHS: &ast.BinaryExpr{
  638. LHS: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream},
  639. OP: ast.MUL,
  640. RHS: &ast.IntegerLiteral{Val: 2},
  641. },
  642. OP: ast.ADD,
  643. RHS: &ast.IntegerLiteral{Val: 3},
  644. },
  645. },
  646. },
  647. },
  648. },
  649. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  650. },
  651. },
  652. {
  653. s: `SELECT ln(t1.abc*2 + 3) FROM tbl AS t1`,
  654. stmt: &ast.SelectStatement{
  655. Fields: []ast.Field{
  656. {
  657. AName: "",
  658. Name: "ln",
  659. Expr: &ast.Call{
  660. Name: "ln",
  661. Args: []ast.Expr{
  662. &ast.BinaryExpr{
  663. LHS: &ast.BinaryExpr{
  664. LHS: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "abc"},
  665. OP: ast.MUL,
  666. RHS: &ast.IntegerLiteral{Val: 2},
  667. },
  668. OP: ast.ADD,
  669. RHS: &ast.IntegerLiteral{Val: 3},
  670. },
  671. },
  672. },
  673. },
  674. },
  675. Sources: []ast.Source{&ast.Table{Name: "tbl", Alias: "t1"}},
  676. },
  677. },
  678. {
  679. s: `SELECT lpad("param2", abc*2 + 3) FROM tbl`,
  680. stmt: &ast.SelectStatement{
  681. Fields: []ast.Field{
  682. {
  683. AName: "",
  684. Name: "lpad",
  685. Expr: &ast.Call{
  686. Name: "lpad",
  687. Args: []ast.Expr{
  688. &ast.StringLiteral{Val: "param2"},
  689. &ast.BinaryExpr{
  690. LHS: &ast.BinaryExpr{
  691. LHS: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream},
  692. OP: ast.MUL,
  693. RHS: &ast.IntegerLiteral{Val: 2},
  694. },
  695. OP: ast.ADD,
  696. RHS: &ast.IntegerLiteral{Val: 3},
  697. },
  698. },
  699. },
  700. },
  701. },
  702. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  703. },
  704. },
  705. {
  706. s: `SELECT 0.2 FROM tbl`,
  707. stmt: &ast.SelectStatement{
  708. Fields: []ast.Field{
  709. {
  710. AName: "",
  711. Name: "",
  712. Expr: &ast.NumberLiteral{Val: 0.2},
  713. },
  714. },
  715. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  716. },
  717. },
  718. {
  719. s: `SELECT .2 FROM tbl`,
  720. stmt: &ast.SelectStatement{
  721. Fields: []ast.Field{
  722. {
  723. AName: "",
  724. Name: "",
  725. Expr: &ast.NumberLiteral{Val: 0.2},
  726. },
  727. },
  728. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  729. },
  730. },
  731. {
  732. s: `SELECT sin(.2) FROM tbl`,
  733. stmt: &ast.SelectStatement{
  734. Fields: []ast.Field{
  735. {
  736. AName: "",
  737. Name: "sin",
  738. Expr: &ast.Call{
  739. Name: "sin",
  740. Args: []ast.Expr{&ast.NumberLiteral{Val: 0.2}},
  741. },
  742. },
  743. },
  744. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  745. },
  746. },
  747. {
  748. s: `SELECT power(.2, 4) FROM tbl`,
  749. stmt: &ast.SelectStatement{
  750. Fields: []ast.Field{
  751. {
  752. AName: "",
  753. Name: "power",
  754. Expr: &ast.Call{
  755. Name: "power",
  756. Args: []ast.Expr{&ast.NumberLiteral{Val: 0.2}, &ast.IntegerLiteral{Val: 4}},
  757. },
  758. },
  759. },
  760. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  761. },
  762. },
  763. {
  764. s: `SELECT power(.2, 4) AS f1 FROM tbl WHERE f1 > 2.2`,
  765. stmt: &ast.SelectStatement{
  766. Fields: []ast.Field{
  767. {
  768. AName: "f1",
  769. Name: "power",
  770. Expr: &ast.Call{
  771. Name: "power",
  772. Args: []ast.Expr{&ast.NumberLiteral{Val: 0.2}, &ast.IntegerLiteral{Val: 4}},
  773. },
  774. },
  775. },
  776. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  777. Condition: &ast.BinaryExpr{
  778. LHS: &ast.FieldRef{Name: "f1", StreamName: ast.DefaultStream},
  779. OP: ast.GT,
  780. RHS: &ast.NumberLiteral{Val: 2.2},
  781. },
  782. },
  783. },
  784. {
  785. s: `SELECT deviceId, name FROM topic/sensor1 WHERE deviceId=1 AND name = "dname"`,
  786. stmt: &ast.SelectStatement{
  787. Fields: []ast.Field{
  788. {Expr: &ast.FieldRef{Name: "deviceId", StreamName: ast.DefaultStream}, Name: "deviceId", AName: ""},
  789. {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
  790. },
  791. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  792. Condition: &ast.BinaryExpr{
  793. LHS: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "deviceId", StreamName: ast.DefaultStream}, OP: ast.EQ, RHS: &ast.IntegerLiteral{Val: 1}},
  794. OP: ast.AND,
  795. RHS: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, OP: ast.EQ, RHS: &ast.StringLiteral{Val: "dname"}},
  796. },
  797. },
  798. },
  799. {
  800. s: `SELECT deviceId, name FROM topic/sensor1 AS t1 WHERE t1.deviceId=1 AND t1.name = "dname"`,
  801. stmt: &ast.SelectStatement{
  802. Fields: []ast.Field{
  803. {Expr: &ast.FieldRef{Name: "deviceId", StreamName: ast.DefaultStream}, Name: "deviceId", AName: ""},
  804. {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
  805. },
  806. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
  807. Condition: &ast.BinaryExpr{
  808. LHS: &ast.BinaryExpr{LHS: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "deviceId"}, OP: ast.EQ, RHS: &ast.IntegerLiteral{Val: 1}},
  809. OP: ast.AND,
  810. RHS: &ast.BinaryExpr{LHS: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "name"}, OP: ast.EQ, RHS: &ast.StringLiteral{Val: "dname"}},
  811. },
  812. },
  813. },
  814. {
  815. s: `SELECT temp AS t, name FROM topic/sensor1 WHERE t> = 20.5 OR name = "dname"`,
  816. stmt: &ast.SelectStatement{
  817. Fields: []ast.Field{
  818. {Expr: &ast.FieldRef{Name: "temp", StreamName: ast.DefaultStream}, Name: "temp", AName: "t"},
  819. {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
  820. },
  821. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  822. Condition: &ast.BinaryExpr{
  823. LHS: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "t", StreamName: ast.DefaultStream}, OP: ast.GTE, RHS: &ast.NumberLiteral{Val: 20.5}},
  824. OP: ast.OR,
  825. RHS: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, OP: ast.EQ, RHS: &ast.StringLiteral{Val: "dname"}},
  826. },
  827. },
  828. },
  829. {
  830. s: `SELECT temp AS t, name FROM topic/sensor1 WHERE name = "dname" GROUP BY name`,
  831. stmt: &ast.SelectStatement{
  832. Fields: []ast.Field{
  833. {Expr: &ast.FieldRef{Name: "temp", StreamName: ast.DefaultStream}, Name: "temp", AName: "t"},
  834. {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
  835. },
  836. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  837. Condition: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, OP: ast.EQ, RHS: &ast.StringLiteral{Val: "dname"}},
  838. Dimensions: ast.Dimensions{ast.Dimension{Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}}},
  839. },
  840. },
  841. {
  842. s: `SELECT temp AS t, name FROM topic/sensor1 WHERE name = "dname" GROUP BY name HAVING count(name) > 3`,
  843. stmt: &ast.SelectStatement{
  844. Fields: []ast.Field{
  845. {Expr: &ast.FieldRef{Name: "temp", StreamName: ast.DefaultStream}, Name: "temp", AName: "t"},
  846. {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
  847. },
  848. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  849. Condition: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, OP: ast.EQ, RHS: &ast.StringLiteral{Val: "dname"}},
  850. Dimensions: ast.Dimensions{ast.Dimension{Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}}},
  851. 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}},
  852. },
  853. },
  854. {
  855. s: `SELECT temp AS t, name FROM topic/sensor1 WHERE name = "dname" HAVING count(name) > 3`,
  856. stmt: &ast.SelectStatement{
  857. Fields: []ast.Field{
  858. {Expr: &ast.FieldRef{Name: "temp", StreamName: ast.DefaultStream}, Name: "temp", AName: "t"},
  859. {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
  860. },
  861. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  862. Condition: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, OP: ast.EQ, RHS: &ast.StringLiteral{Val: "dname"}},
  863. 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}},
  864. },
  865. },
  866. {
  867. s: `SELECT id,AVG(data) FROM t GROUP BY SUM(data)>10`,
  868. stmt: nil,
  869. err: "Not allowed to call aggregate functions in GROUP BY clause.",
  870. },
  871. {
  872. s: `SELECT temp AS t, name FROM topic/sensor1 WHERE count(name) = 3`,
  873. stmt: nil,
  874. err: "Not allowed to call aggregate functions in WHERE clause.",
  875. },
  876. {
  877. s: `SELECT s1.temp AS t, name FROM topic/sensor1 AS s1 WHERE t = "dname" GROUP BY s1.temp`,
  878. stmt: &ast.SelectStatement{
  879. Fields: []ast.Field{
  880. {Expr: &ast.FieldRef{StreamName: "s1", Name: "temp"}, Name: "temp", AName: "t"},
  881. {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
  882. },
  883. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "s1"}},
  884. Condition: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "t", StreamName: ast.DefaultStream}, OP: ast.EQ, RHS: &ast.StringLiteral{Val: "dname"}},
  885. Dimensions: ast.Dimensions{ast.Dimension{Expr: &ast.FieldRef{StreamName: "s1", Name: "temp"}}},
  886. },
  887. },
  888. {
  889. s: `SELECT temp AS t, name FROM topic/sensor1 WHERE name = "dname" GROUP BY lpad(name,1)`,
  890. stmt: &ast.SelectStatement{
  891. Fields: []ast.Field{
  892. {Expr: &ast.FieldRef{Name: "temp", StreamName: ast.DefaultStream}, Name: "temp", AName: "t"},
  893. {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
  894. },
  895. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  896. Condition: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, OP: ast.EQ, RHS: &ast.StringLiteral{Val: "dname"}},
  897. Dimensions: ast.Dimensions{ast.Dimension{
  898. Expr: &ast.Call{Name: "lpad", Args: []ast.Expr{&ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, &ast.IntegerLiteral{Val: 1}}},
  899. },
  900. },
  901. },
  902. },
  903. {
  904. s: `SELECT temp AS t, name FROM topic/sensor1 AS s1 WHERE name = "dname" GROUP BY lpad(s1.name,1)`,
  905. stmt: &ast.SelectStatement{
  906. Fields: []ast.Field{
  907. {Expr: &ast.FieldRef{Name: "temp", StreamName: ast.DefaultStream}, Name: "temp", AName: "t"},
  908. {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
  909. },
  910. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "s1"}},
  911. Condition: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, OP: ast.EQ, RHS: &ast.StringLiteral{Val: "dname"}},
  912. Dimensions: ast.Dimensions{ast.Dimension{
  913. Expr: &ast.Call{Name: "lpad", Args: []ast.Expr{&ast.FieldRef{StreamName: ast.StreamName("s1"), Name: "name"}, &ast.IntegerLiteral{Val: 1}}},
  914. },
  915. },
  916. },
  917. },
  918. {
  919. s: `SELECT temp AS t, name FROM topic/sensor1 WHERE name = "dname" GROUP BY lpad(name,1) ORDER BY name`,
  920. stmt: &ast.SelectStatement{
  921. Fields: []ast.Field{
  922. {Expr: &ast.FieldRef{Name: "temp", StreamName: ast.DefaultStream}, Name: "temp", AName: "t"},
  923. {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
  924. },
  925. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  926. Condition: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, OP: ast.EQ, RHS: &ast.StringLiteral{Val: "dname"}},
  927. Dimensions: ast.Dimensions{
  928. ast.Dimension{
  929. Expr: &ast.Call{Name: "lpad", Args: []ast.Expr{
  930. &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  931. &ast.IntegerLiteral{Val: 1}},
  932. },
  933. },
  934. },
  935. SortFields: []ast.SortField{{Name: "name", Ascending: true}},
  936. },
  937. },
  938. {
  939. 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`,
  940. stmt: &ast.SelectStatement{
  941. Fields: []ast.Field{
  942. {Expr: &ast.FieldRef{Name: "temp", StreamName: ast.DefaultStream}, Name: "temp", AName: "t"},
  943. {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
  944. },
  945. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "s1"}},
  946. Condition: &ast.BinaryExpr{LHS: &ast.FieldRef{StreamName: ast.StreamName("s1"), Name: "name"}, OP: ast.EQ, RHS: &ast.StringLiteral{Val: "dname"}},
  947. Dimensions: ast.Dimensions{
  948. ast.Dimension{
  949. Expr: &ast.Call{Name: "lpad", Args: []ast.Expr{
  950. &ast.FieldRef{StreamName: ast.StreamName("s1"), Name: "name"},
  951. &ast.IntegerLiteral{Val: 1}},
  952. },
  953. },
  954. },
  955. SortFields: []ast.SortField{{Name: "s1\007name", Ascending: true}},
  956. },
  957. },
  958. {
  959. s: `SELECT temp AS t, name FROM topic/sensor1 WHERE name = "dname" GROUP BY lpad(name,1) ORDER BY name DESC`,
  960. stmt: &ast.SelectStatement{
  961. Fields: []ast.Field{
  962. {Expr: &ast.FieldRef{Name: "temp", StreamName: ast.DefaultStream}, Name: "temp", AName: "t"},
  963. {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
  964. },
  965. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  966. Condition: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, OP: ast.EQ, RHS: &ast.StringLiteral{Val: "dname"}},
  967. Dimensions: ast.Dimensions{
  968. ast.Dimension{
  969. Expr: &ast.Call{Name: "lpad", Args: []ast.Expr{
  970. &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  971. &ast.IntegerLiteral{Val: 1}},
  972. },
  973. },
  974. },
  975. SortFields: []ast.SortField{{Name: "name", Ascending: false}},
  976. },
  977. },
  978. {
  979. s: `SELECT * FROM topic/sensor1 ORDER BY name DESC`,
  980. stmt: &ast.SelectStatement{
  981. Fields: []ast.Field{
  982. {
  983. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  984. Name: "",
  985. AName: ""},
  986. },
  987. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  988. SortFields: []ast.SortField{{Name: "name", Ascending: false}},
  989. },
  990. },
  991. {
  992. s: `SELECT * FROM topic/sensor1 ORDER BY name DESC, name2 ASC`,
  993. stmt: &ast.SelectStatement{
  994. Fields: []ast.Field{
  995. {
  996. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  997. Name: "",
  998. AName: ""},
  999. },
  1000. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  1001. SortFields: []ast.SortField{{Name: "name", Ascending: false}, {Name: "name2", Ascending: true}},
  1002. },
  1003. },
  1004. {
  1005. s: `SELECT * FROM topic/sensor1 GROUP BY name, name2,power(name3,1.8) ORDER BY name DESC, name2 ASC`,
  1006. stmt: &ast.SelectStatement{
  1007. Fields: []ast.Field{
  1008. {
  1009. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  1010. Name: "",
  1011. AName: ""},
  1012. },
  1013. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  1014. Dimensions: ast.Dimensions{
  1015. ast.Dimension{Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}},
  1016. ast.Dimension{Expr: &ast.FieldRef{Name: "name2", StreamName: ast.DefaultStream}},
  1017. ast.Dimension{
  1018. Expr: &ast.Call{Name: "power", Args: []ast.Expr{
  1019. &ast.FieldRef{Name: "name3", StreamName: ast.DefaultStream},
  1020. &ast.NumberLiteral{Val: 1.8}},
  1021. },
  1022. },
  1023. },
  1024. SortFields: []ast.SortField{{Name: "name", Ascending: false}, {Name: "name2", Ascending: true}},
  1025. },
  1026. },
  1027. //{
  1028. // s: `SELECT .2sd FROM tbl`,
  1029. // stmt: &SelectStatement{
  1030. // Fields: []Field{
  1031. // Field{
  1032. // AName:"",
  1033. // Expr: &NumberLiteral{Val: 0.2},
  1034. // },
  1035. // },
  1036. // TableName: "tbl",
  1037. // },
  1038. //},
  1039. {
  1040. s: `SELECT name FROM tbl/*SELECT comment FROM testComments*/`,
  1041. stmt: &ast.SelectStatement{
  1042. Fields: []ast.Field{
  1043. {
  1044. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  1045. Name: "name",
  1046. AName: ""},
  1047. },
  1048. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1049. },
  1050. },
  1051. {
  1052. s: `/*SELECT comment FROM testComments*/SELECT name FROM tbl`,
  1053. stmt: &ast.SelectStatement{
  1054. Fields: []ast.Field{
  1055. {
  1056. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  1057. Name: "name",
  1058. AName: ""},
  1059. },
  1060. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1061. },
  1062. },
  1063. {
  1064. s: `SELECT name /*SELECT comment FROM testComments*/ FROM tbl`,
  1065. stmt: &ast.SelectStatement{
  1066. Fields: []ast.Field{
  1067. {
  1068. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  1069. Name: "name",
  1070. AName: ""},
  1071. },
  1072. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1073. },
  1074. },
  1075. {
  1076. s: `SELECT true AS f1, FALSE as f2 FROM tbl`,
  1077. stmt: &ast.SelectStatement{
  1078. Fields: []ast.Field{
  1079. {AName: "f1", Name: "", Expr: &ast.BooleanLiteral{Val: true}},
  1080. {AName: "f2", Name: "", Expr: &ast.BooleanLiteral{Val: false}},
  1081. },
  1082. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1083. },
  1084. },
  1085. {
  1086. s: `SELECT true AS f1 FROM tbl WHERE f2 = true`,
  1087. stmt: &ast.SelectStatement{
  1088. Fields: []ast.Field{
  1089. {AName: "f1", Name: "", Expr: &ast.BooleanLiteral{Val: true}},
  1090. },
  1091. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1092. Condition: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "f2", StreamName: ast.DefaultStream}, OP: ast.EQ, RHS: &ast.BooleanLiteral{Val: true}},
  1093. },
  1094. },
  1095. {
  1096. s: `SELECT indexof(field1, "abc") FROM tbl`,
  1097. stmt: &ast.SelectStatement{
  1098. Fields: []ast.Field{
  1099. {
  1100. AName: "",
  1101. Name: "indexof",
  1102. Expr: &ast.Call{
  1103. Name: "indexof",
  1104. Args: []ast.Expr{&ast.FieldRef{Name: "field1", StreamName: ast.DefaultStream}, &ast.StringLiteral{Val: "abc"}},
  1105. },
  1106. },
  1107. },
  1108. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1109. },
  1110. },
  1111. //The negative value expression support.
  1112. {
  1113. s: `SELECT -3 AS t1 FROM tbl`,
  1114. stmt: &ast.SelectStatement{
  1115. Fields: []ast.Field{
  1116. {
  1117. Expr: &ast.IntegerLiteral{Val: -3},
  1118. Name: "",
  1119. AName: "t1"},
  1120. },
  1121. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1122. },
  1123. },
  1124. {
  1125. s: `SELECT - 3 AS t1 FROM tbl`,
  1126. stmt: &ast.SelectStatement{
  1127. Fields: []ast.Field{
  1128. {
  1129. Expr: &ast.IntegerLiteral{Val: -3},
  1130. Name: "",
  1131. AName: "t1"},
  1132. },
  1133. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1134. },
  1135. },
  1136. {
  1137. s: `SELECT -. 3 AS t1 FROM tbl`,
  1138. stmt: &ast.SelectStatement{
  1139. Fields: []ast.Field{
  1140. {
  1141. Expr: &ast.NumberLiteral{Val: -.3},
  1142. Name: "",
  1143. AName: "t1"},
  1144. },
  1145. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1146. },
  1147. },
  1148. {
  1149. s: `SELECT -3.3 AS t1 FROM tbl`,
  1150. stmt: &ast.SelectStatement{
  1151. Fields: []ast.Field{
  1152. {
  1153. Expr: &ast.NumberLiteral{Val: -3.3},
  1154. Name: "",
  1155. AName: "t1"},
  1156. },
  1157. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1158. },
  1159. },
  1160. {
  1161. s: `SELECT sample(-.3,) FROM tbl`,
  1162. stmt: nil,
  1163. err: "function sample not found",
  1164. },
  1165. {
  1166. s: `select timestamp() as tp from demo`,
  1167. stmt: nil,
  1168. err: "found \"TIMESTAMP\", expected expression.",
  1169. },
  1170. {
  1171. s: `select tstamp() as tp from demo`,
  1172. stmt: &ast.SelectStatement{
  1173. Fields: []ast.Field{
  1174. {
  1175. Expr: &ast.Call{
  1176. Name: "tstamp",
  1177. Args: nil,
  1178. },
  1179. Name: "tstamp",
  1180. AName: "tp"},
  1181. },
  1182. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  1183. },
  1184. err: "",
  1185. },
  1186. {
  1187. s: "SELECT `half FROM tb",
  1188. stmt: nil,
  1189. err: `found "EOF", expected FROM.`,
  1190. },
  1191. {
  1192. s: "SELECT `space var` FROM tbl",
  1193. stmt: &ast.SelectStatement{
  1194. Fields: []ast.Field{
  1195. {
  1196. Expr: &ast.FieldRef{Name: "space var", StreamName: ast.DefaultStream},
  1197. Name: "space var",
  1198. AName: ""},
  1199. },
  1200. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1201. },
  1202. },
  1203. {
  1204. s: "SELECT `中文 Chinese` FROM tbl",
  1205. stmt: &ast.SelectStatement{
  1206. Fields: []ast.Field{
  1207. {
  1208. Expr: &ast.FieldRef{Name: "中文 Chinese", StreamName: ast.DefaultStream},
  1209. Name: "中文 Chinese",
  1210. AName: ""},
  1211. },
  1212. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1213. },
  1214. }, {
  1215. s: "SELECT CASE temperature WHEN 25 THEN \"bingo\" WHEN 30 THEN \"high\" ELSE \"low\" END as label, humidity FROM tbl",
  1216. stmt: &ast.SelectStatement{
  1217. Fields: []ast.Field{
  1218. {
  1219. Expr: &ast.CaseExpr{
  1220. Value: &ast.FieldRef{Name: "temperature", StreamName: ast.DefaultStream},
  1221. WhenClauses: []*ast.WhenClause{
  1222. {
  1223. Expr: &ast.IntegerLiteral{Val: 25},
  1224. Result: &ast.StringLiteral{Val: "bingo"},
  1225. }, {
  1226. Expr: &ast.IntegerLiteral{Val: 30},
  1227. Result: &ast.StringLiteral{Val: "high"},
  1228. },
  1229. },
  1230. ElseClause: &ast.StringLiteral{Val: "low"},
  1231. },
  1232. Name: "",
  1233. AName: "label",
  1234. }, {
  1235. Expr: &ast.FieldRef{Name: "humidity", StreamName: ast.DefaultStream},
  1236. Name: "humidity",
  1237. AName: "",
  1238. },
  1239. },
  1240. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1241. },
  1242. }, {
  1243. s: "SELECT CASE temperature WHEN 25 THEN \"bingo\" WHEN 30 THEN \"high\" END as label, humidity FROM tbl",
  1244. stmt: &ast.SelectStatement{
  1245. Fields: []ast.Field{
  1246. {
  1247. Expr: &ast.CaseExpr{
  1248. Value: &ast.FieldRef{Name: "temperature", StreamName: ast.DefaultStream},
  1249. WhenClauses: []*ast.WhenClause{
  1250. {
  1251. Expr: &ast.IntegerLiteral{Val: 25},
  1252. Result: &ast.StringLiteral{Val: "bingo"},
  1253. }, {
  1254. Expr: &ast.IntegerLiteral{Val: 30},
  1255. Result: &ast.StringLiteral{Val: "high"},
  1256. },
  1257. },
  1258. ElseClause: nil,
  1259. },
  1260. Name: "",
  1261. AName: "label",
  1262. }, {
  1263. Expr: &ast.FieldRef{Name: "humidity", StreamName: ast.DefaultStream},
  1264. Name: "humidity",
  1265. AName: "",
  1266. },
  1267. },
  1268. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1269. },
  1270. }, {
  1271. s: "SELECT CASE temperature ELSE \"low\" END as label, humidity FROM tbl",
  1272. stmt: nil,
  1273. err: "invalid CASE expression, WHEN expected before ELSE",
  1274. }, {
  1275. s: "SELECT CASE WHEN temperature > 30 THEN \"high\" ELSE \"low\" END as label, humidity FROM tbl",
  1276. stmt: &ast.SelectStatement{
  1277. Fields: []ast.Field{
  1278. {
  1279. Expr: &ast.CaseExpr{
  1280. Value: nil,
  1281. WhenClauses: []*ast.WhenClause{
  1282. {
  1283. Expr: &ast.BinaryExpr{
  1284. OP: ast.GT,
  1285. LHS: &ast.FieldRef{Name: "temperature", StreamName: ast.DefaultStream},
  1286. RHS: &ast.IntegerLiteral{Val: 30},
  1287. },
  1288. Result: &ast.StringLiteral{Val: "high"},
  1289. },
  1290. },
  1291. ElseClause: &ast.StringLiteral{Val: "low"},
  1292. },
  1293. Name: "",
  1294. AName: "label",
  1295. }, {
  1296. Expr: &ast.FieldRef{Name: "humidity", StreamName: ast.DefaultStream},
  1297. Name: "humidity",
  1298. AName: "",
  1299. },
  1300. },
  1301. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1302. },
  1303. }, {
  1304. s: "SELECT CASE WHEN 30 THEN \"high\" ELSE \"low\" END as label, humidity FROM tbl",
  1305. stmt: nil,
  1306. err: "invalid CASE expression, WHEN expression must be a bool condition",
  1307. }, {
  1308. s: `SELECT echo(*) FROM tbl`,
  1309. stmt: &ast.SelectStatement{
  1310. Fields: []ast.Field{
  1311. {
  1312. AName: "",
  1313. Name: "echo",
  1314. Expr: &ast.Call{
  1315. Name: "echo",
  1316. Args: []ast.Expr{&ast.Wildcard{Token: ast.ASTERISK}},
  1317. },
  1318. },
  1319. },
  1320. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1321. },
  1322. },
  1323. {
  1324. s: `SELECT count(*)-10 FROM demo`,
  1325. stmt: &ast.SelectStatement{
  1326. Fields: []ast.Field{
  1327. {
  1328. AName: "",
  1329. Name: "",
  1330. Expr: &ast.BinaryExpr{
  1331. OP: ast.SUB,
  1332. LHS: &ast.Call{
  1333. Name: "count",
  1334. Args: []ast.Expr{
  1335. &ast.Wildcard{Token: ast.ASTERISK},
  1336. },
  1337. },
  1338. RHS: &ast.IntegerLiteral{Val: 10},
  1339. },
  1340. },
  1341. },
  1342. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  1343. },
  1344. },
  1345. {
  1346. s: `SELECT -abc FROM demo`,
  1347. stmt: nil,
  1348. err: "found \"-\", expected expression.",
  1349. },
  1350. {
  1351. s: `SELECT meta(*) FROM tbl`,
  1352. stmt: &ast.SelectStatement{
  1353. Fields: []ast.Field{
  1354. {
  1355. AName: "",
  1356. Name: "meta",
  1357. Expr: &ast.Call{
  1358. Name: "meta",
  1359. Args: []ast.Expr{
  1360. &ast.MetaRef{
  1361. Name: "*",
  1362. StreamName: ast.DefaultStream,
  1363. },
  1364. },
  1365. },
  1366. },
  1367. },
  1368. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1369. },
  1370. },
  1371. }
  1372. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  1373. for i, tt := range tests {
  1374. //fmt.Printf("Parsing SQL %q.\n", tt.s)
  1375. stmt, err := NewParser(strings.NewReader(tt.s)).Parse()
  1376. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  1377. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.s, tt.err, err)
  1378. } else if tt.err == "" && !reflect.DeepEqual(tt.stmt, stmt) {
  1379. t.Errorf("%d. %q\n\nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.s, tt.stmt, stmt)
  1380. }
  1381. }
  1382. }
  1383. func TestParser_ParseWindowsExpr(t *testing.T) {
  1384. var tests = []struct {
  1385. s string
  1386. stmt *ast.SelectStatement
  1387. err string
  1388. }{
  1389. {
  1390. s: `SELECT f1 FROM tbl GROUP BY TUMBLINGWINDOW(ss, 10)`,
  1391. stmt: &ast.SelectStatement{
  1392. Fields: []ast.Field{
  1393. {
  1394. Expr: &ast.FieldRef{Name: "f1", StreamName: ast.DefaultStream},
  1395. Name: "f1",
  1396. AName: ""},
  1397. },
  1398. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1399. Dimensions: ast.Dimensions{
  1400. ast.Dimension{
  1401. Expr: &ast.Window{
  1402. WindowType: ast.TUMBLING_WINDOW,
  1403. Length: &ast.IntegerLiteral{Val: 10000},
  1404. Interval: &ast.IntegerLiteral{Val: 0},
  1405. },
  1406. },
  1407. },
  1408. },
  1409. },
  1410. {
  1411. s: `SELECT f1 FROM tbl GROUP BY HOPPINGWINDOW(mi, 5, 1)`,
  1412. stmt: &ast.SelectStatement{
  1413. Fields: []ast.Field{
  1414. {
  1415. Expr: &ast.FieldRef{Name: "f1", StreamName: ast.DefaultStream},
  1416. Name: "f1",
  1417. AName: ""},
  1418. },
  1419. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1420. Dimensions: ast.Dimensions{
  1421. ast.Dimension{
  1422. Expr: &ast.Window{
  1423. WindowType: ast.HOPPING_WINDOW,
  1424. Length: &ast.IntegerLiteral{Val: 3e5},
  1425. Interval: &ast.IntegerLiteral{Val: 6e4},
  1426. },
  1427. },
  1428. },
  1429. },
  1430. },
  1431. {
  1432. s: `SELECT f1 FROM tbl GROUP BY SESSIONWINDOW(hh, 5, 1)`,
  1433. stmt: &ast.SelectStatement{
  1434. Fields: []ast.Field{
  1435. {
  1436. Expr: &ast.FieldRef{Name: "f1", StreamName: ast.DefaultStream},
  1437. Name: "f1",
  1438. AName: ""},
  1439. },
  1440. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1441. Dimensions: ast.Dimensions{
  1442. ast.Dimension{
  1443. Expr: &ast.Window{
  1444. WindowType: ast.SESSION_WINDOW,
  1445. Length: &ast.IntegerLiteral{Val: 1.8e7},
  1446. Interval: &ast.IntegerLiteral{Val: 3.6e6},
  1447. },
  1448. },
  1449. },
  1450. },
  1451. },
  1452. {
  1453. s: `SELECT f1 FROM tbl GROUP BY SLIDINGWINDOW(ms, 5)`,
  1454. stmt: &ast.SelectStatement{
  1455. Fields: []ast.Field{
  1456. {
  1457. Expr: &ast.FieldRef{Name: "f1", StreamName: ast.DefaultStream},
  1458. Name: "f1",
  1459. AName: ""},
  1460. },
  1461. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1462. Dimensions: ast.Dimensions{
  1463. ast.Dimension{
  1464. Expr: &ast.Window{
  1465. WindowType: ast.SLIDING_WINDOW,
  1466. Length: &ast.IntegerLiteral{Val: 5},
  1467. Interval: &ast.IntegerLiteral{Val: 0},
  1468. },
  1469. },
  1470. },
  1471. },
  1472. },
  1473. {
  1474. s: `SELECT f1 FROM tbl GROUP BY SLIDINGWINDOW(mi, 5, 1)`,
  1475. stmt: nil,
  1476. err: "The arguments for slidingwindow should be 2.\n",
  1477. },
  1478. {
  1479. s: `SELECT f1 FROM tbl GROUP BY SLIDINGWINDOW("mi", 5)`,
  1480. stmt: nil,
  1481. err: "The 1st argument for slidingwindow is expecting timer literal expression. One value of [dd|hh|mi|ss|ms].\n",
  1482. },
  1483. {
  1484. s: `SELECT f1 FROM tbl GROUP BY COUNTWINDOW(10)`,
  1485. stmt: &ast.SelectStatement{
  1486. Fields: []ast.Field{
  1487. {
  1488. Expr: &ast.FieldRef{Name: "f1", StreamName: ast.DefaultStream},
  1489. Name: "f1",
  1490. AName: ""},
  1491. },
  1492. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1493. Dimensions: ast.Dimensions{
  1494. ast.Dimension{
  1495. Expr: &ast.Window{
  1496. WindowType: ast.COUNT_WINDOW,
  1497. Length: &ast.IntegerLiteral{Val: 10},
  1498. },
  1499. },
  1500. },
  1501. },
  1502. },
  1503. {
  1504. s: `SELECT f1 FROM tbl GROUP BY COUNTWINDOW(10, 5)`,
  1505. stmt: &ast.SelectStatement{
  1506. Fields: []ast.Field{
  1507. {
  1508. Expr: &ast.FieldRef{Name: "f1", StreamName: ast.DefaultStream},
  1509. Name: "f1",
  1510. AName: ""},
  1511. },
  1512. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1513. Dimensions: ast.Dimensions{
  1514. ast.Dimension{
  1515. Expr: &ast.Window{
  1516. WindowType: ast.COUNT_WINDOW,
  1517. Length: &ast.IntegerLiteral{Val: 10},
  1518. Interval: &ast.IntegerLiteral{Val: 5},
  1519. },
  1520. },
  1521. },
  1522. },
  1523. },
  1524. {
  1525. s: `SELECT f1 FROM tbl GROUP BY COUNTWINDOW(3, 5)`,
  1526. stmt: nil,
  1527. err: "The second parameter value 5 should be less than the first parameter 3.",
  1528. },
  1529. {
  1530. s: `SELECT * FROM demo GROUP BY COUNTWINDOW(3,1) FILTER( where revenue > 100 )`,
  1531. stmt: &ast.SelectStatement{
  1532. Fields: []ast.Field{
  1533. {
  1534. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  1535. Name: "",
  1536. AName: ""},
  1537. },
  1538. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  1539. Dimensions: ast.Dimensions{
  1540. ast.Dimension{
  1541. Expr: &ast.Window{
  1542. WindowType: ast.COUNT_WINDOW,
  1543. Length: &ast.IntegerLiteral{Val: 3},
  1544. Interval: &ast.IntegerLiteral{Val: 1},
  1545. Filter: &ast.BinaryExpr{
  1546. LHS: &ast.FieldRef{Name: "revenue", StreamName: ast.DefaultStream},
  1547. OP: ast.GT,
  1548. RHS: &ast.IntegerLiteral{Val: 100},
  1549. },
  1550. },
  1551. },
  1552. },
  1553. },
  1554. },
  1555. {
  1556. s: `SELECT * FROM demo GROUP BY department, COUNTWINDOW(3,1) FILTER( where revenue > 100 ), year`,
  1557. stmt: &ast.SelectStatement{
  1558. Fields: []ast.Field{
  1559. {
  1560. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  1561. Name: "",
  1562. AName: ""},
  1563. },
  1564. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  1565. Dimensions: ast.Dimensions{
  1566. ast.Dimension{Expr: &ast.FieldRef{Name: "department", StreamName: ast.DefaultStream}},
  1567. ast.Dimension{
  1568. Expr: &ast.Window{
  1569. WindowType: ast.COUNT_WINDOW,
  1570. Length: &ast.IntegerLiteral{Val: 3},
  1571. Interval: &ast.IntegerLiteral{Val: 1},
  1572. Filter: &ast.BinaryExpr{
  1573. LHS: &ast.FieldRef{Name: "revenue", StreamName: ast.DefaultStream},
  1574. OP: ast.GT,
  1575. RHS: &ast.IntegerLiteral{Val: 100},
  1576. },
  1577. },
  1578. },
  1579. ast.Dimension{Expr: &ast.FieldRef{Name: "year", StreamName: ast.DefaultStream}},
  1580. },
  1581. },
  1582. },
  1583. //to be supported
  1584. {
  1585. s: `SELECT sum(f1) FILTER( where revenue > 100 ) FROM tbl GROUP BY year`,
  1586. stmt: nil,
  1587. err: "found \"FILTER\", expected FROM.",
  1588. },
  1589. {
  1590. s: `SELECT * FROM demo GROUP BY COUNTWINDOW(3,1) FILTER where revenue > 100`,
  1591. stmt: nil,
  1592. err: "Found \"WHERE\" after FILTER, expect parentheses.",
  1593. },
  1594. {
  1595. s: `SELECT * FROM demo GROUP BY COUNTWINDOW(3,1) where revenue > 100`,
  1596. stmt: nil,
  1597. err: "found \"WHERE\", expected EOF.",
  1598. },
  1599. }
  1600. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  1601. for i, tt := range tests {
  1602. //fmt.Printf("Parsing SQL %q.\n", tt.s)
  1603. stmt, err := NewParser(strings.NewReader(tt.s)).Parse()
  1604. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  1605. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.s, tt.err, err)
  1606. } else if tt.err == "" && !reflect.DeepEqual(tt.stmt, stmt) {
  1607. t.Errorf("%d. %q\n\nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.s, tt.stmt, stmt)
  1608. }
  1609. }
  1610. }
  1611. func TestParser_ParseJsonExpr(t *testing.T) {
  1612. var tests = []struct {
  1613. s string
  1614. stmt *ast.SelectStatement
  1615. err string
  1616. }{
  1617. {
  1618. s: `SELECT children[0] FROM demo`,
  1619. stmt: &ast.SelectStatement{
  1620. Fields: []ast.Field{
  1621. {
  1622. Expr: &ast.BinaryExpr{
  1623. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  1624. OP: ast.SUBSET,
  1625. RHS: &ast.IndexExpr{Index: &ast.IntegerLiteral{Val: 0}},
  1626. },
  1627. Name: "",
  1628. AName: ""},
  1629. },
  1630. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  1631. },
  1632. },
  1633. {
  1634. s: `SELECT children[0]->first FROM demo`,
  1635. stmt: &ast.SelectStatement{
  1636. Fields: []ast.Field{
  1637. {
  1638. Expr: &ast.BinaryExpr{
  1639. LHS: &ast.BinaryExpr{
  1640. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  1641. OP: ast.SUBSET,
  1642. RHS: &ast.IndexExpr{Index: &ast.IntegerLiteral{Val: 0}},
  1643. },
  1644. OP: ast.ARROW,
  1645. RHS: &ast.JsonFieldRef{Name: "first"},
  1646. },
  1647. Name: "",
  1648. AName: ""},
  1649. },
  1650. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  1651. },
  1652. },
  1653. {
  1654. s: `SELECT children->first[2] FROM demo`,
  1655. stmt: &ast.SelectStatement{
  1656. Fields: []ast.Field{
  1657. {
  1658. Expr: &ast.BinaryExpr{
  1659. LHS: &ast.BinaryExpr{
  1660. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  1661. OP: ast.ARROW,
  1662. RHS: &ast.JsonFieldRef{Name: "first"},
  1663. },
  1664. OP: ast.SUBSET,
  1665. RHS: &ast.IndexExpr{Index: &ast.IntegerLiteral{Val: 2}},
  1666. },
  1667. Name: "",
  1668. AName: ""},
  1669. },
  1670. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  1671. },
  1672. },
  1673. {
  1674. s: `SELECT children->first[2]->test FROM demo`,
  1675. stmt: &ast.SelectStatement{
  1676. Fields: []ast.Field{
  1677. {
  1678. Expr: &ast.BinaryExpr{
  1679. LHS: &ast.BinaryExpr{
  1680. LHS: &ast.BinaryExpr{
  1681. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  1682. OP: ast.ARROW,
  1683. RHS: &ast.JsonFieldRef{Name: "first"},
  1684. },
  1685. OP: ast.SUBSET,
  1686. RHS: &ast.IndexExpr{Index: &ast.IntegerLiteral{Val: 2}},
  1687. },
  1688. OP: ast.ARROW,
  1689. RHS: &ast.JsonFieldRef{Name: "test"},
  1690. },
  1691. Name: "",
  1692. AName: ""},
  1693. },
  1694. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  1695. },
  1696. },
  1697. {
  1698. s: `SELECT children[0:1] FROM demo`,
  1699. stmt: &ast.SelectStatement{
  1700. Fields: []ast.Field{
  1701. {
  1702. Expr: &ast.BinaryExpr{
  1703. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  1704. OP: ast.SUBSET,
  1705. RHS: &ast.ColonExpr{Start: &ast.IntegerLiteral{Val: 0}, End: &ast.IntegerLiteral{Val: 1}},
  1706. },
  1707. Name: "",
  1708. AName: ""},
  1709. },
  1710. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  1711. },
  1712. },
  1713. {
  1714. s: `SELECT children[:1] FROM demo`,
  1715. stmt: &ast.SelectStatement{
  1716. Fields: []ast.Field{
  1717. {
  1718. Expr: &ast.BinaryExpr{
  1719. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  1720. OP: ast.SUBSET,
  1721. RHS: &ast.ColonExpr{Start: &ast.IntegerLiteral{Val: 0}, End: &ast.IntegerLiteral{Val: 1}},
  1722. },
  1723. Name: "",
  1724. AName: ""},
  1725. },
  1726. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  1727. },
  1728. },
  1729. {
  1730. s: `SELECT children[:] FROM demo`,
  1731. stmt: &ast.SelectStatement{
  1732. Fields: []ast.Field{
  1733. {
  1734. Expr: &ast.BinaryExpr{
  1735. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  1736. OP: ast.SUBSET,
  1737. RHS: &ast.ColonExpr{Start: &ast.IntegerLiteral{Val: 0}, End: &ast.IntegerLiteral{Val: math.MinInt32}},
  1738. },
  1739. Name: "",
  1740. AName: ""},
  1741. },
  1742. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  1743. },
  1744. },
  1745. {
  1746. s: `SELECT children[2:] AS c FROM demo`,
  1747. stmt: &ast.SelectStatement{
  1748. Fields: []ast.Field{
  1749. {
  1750. Expr: &ast.BinaryExpr{
  1751. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  1752. OP: ast.SUBSET,
  1753. RHS: &ast.ColonExpr{Start: &ast.IntegerLiteral{Val: 2}, End: &ast.IntegerLiteral{Val: math.MinInt32}},
  1754. },
  1755. Name: "",
  1756. AName: "c"},
  1757. },
  1758. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  1759. },
  1760. },
  1761. {
  1762. s: `SELECT children[2:]->first AS c FROM demo`,
  1763. stmt: &ast.SelectStatement{
  1764. Fields: []ast.Field{
  1765. {
  1766. Expr: &ast.BinaryExpr{
  1767. 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}}},
  1768. OP: ast.ARROW,
  1769. RHS: &ast.JsonFieldRef{Name: "first"},
  1770. },
  1771. Name: "",
  1772. AName: "c"},
  1773. },
  1774. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  1775. },
  1776. },
  1777. {
  1778. s: `SELECT demo.* FROM demo`,
  1779. stmt: &ast.SelectStatement{
  1780. Fields: []ast.Field{
  1781. {
  1782. Expr: &ast.FieldRef{StreamName: ast.StreamName("demo"), Name: "*"},
  1783. Name: "*",
  1784. AName: ""},
  1785. },
  1786. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  1787. },
  1788. },
  1789. {
  1790. s: `SELECT demo.children[2:]->first AS c FROM demo`,
  1791. stmt: &ast.SelectStatement{
  1792. Fields: []ast.Field{
  1793. {
  1794. Expr: &ast.BinaryExpr{
  1795. 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}}},
  1796. OP: ast.ARROW,
  1797. RHS: &ast.JsonFieldRef{Name: "first"},
  1798. },
  1799. Name: "",
  1800. AName: "c"},
  1801. },
  1802. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  1803. },
  1804. },
  1805. {
  1806. s: `SELECT lower(demo.children[2:]->first) AS c FROM demo`,
  1807. stmt: &ast.SelectStatement{
  1808. Fields: []ast.Field{
  1809. {
  1810. Expr: &ast.Call{
  1811. Name: "lower",
  1812. Args: []ast.Expr{
  1813. &ast.BinaryExpr{
  1814. 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}}},
  1815. OP: ast.ARROW,
  1816. RHS: &ast.JsonFieldRef{Name: "first"},
  1817. },
  1818. },
  1819. },
  1820. Name: "lower",
  1821. AName: "c"},
  1822. },
  1823. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  1824. },
  1825. },
  1826. {
  1827. s: `SELECT children[:1] FROM demo WHERE abc[0] > 12`,
  1828. stmt: &ast.SelectStatement{
  1829. Fields: []ast.Field{
  1830. {
  1831. Expr: &ast.BinaryExpr{
  1832. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  1833. OP: ast.SUBSET,
  1834. RHS: &ast.ColonExpr{Start: &ast.IntegerLiteral{Val: 0}, End: &ast.IntegerLiteral{Val: 1}},
  1835. },
  1836. Name: "",
  1837. AName: ""},
  1838. },
  1839. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  1840. Condition: &ast.BinaryExpr{
  1841. LHS: &ast.BinaryExpr{
  1842. LHS: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream},
  1843. OP: ast.SUBSET,
  1844. RHS: &ast.IndexExpr{Index: &ast.IntegerLiteral{Val: 0}},
  1845. },
  1846. OP: ast.GT,
  1847. RHS: &ast.IntegerLiteral{Val: 12},
  1848. },
  1849. },
  1850. },
  1851. {
  1852. s: `SELECT demo.children.first AS c FROM demo`,
  1853. stmt: nil,
  1854. err: "Too many field names. Please use -> to reference keys in struct.\n",
  1855. },
  1856. {
  1857. s: `SELECT children[index] FROM demo`,
  1858. stmt: &ast.SelectStatement{
  1859. Fields: []ast.Field{
  1860. {
  1861. Expr: &ast.BinaryExpr{
  1862. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  1863. OP: ast.SUBSET,
  1864. RHS: &ast.IndexExpr{Index: &ast.FieldRef{Name: "index", StreamName: ast.DefaultStream}},
  1865. },
  1866. Name: "",
  1867. AName: ""},
  1868. },
  1869. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  1870. },
  1871. },
  1872. }
  1873. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  1874. for i, tt := range tests {
  1875. stmt, err := NewParser(strings.NewReader(tt.s)).Parse()
  1876. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  1877. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.s, tt.err, err)
  1878. } else if tt.err == "" && !reflect.DeepEqual(tt.stmt, stmt) {
  1879. t.Errorf("%d. %q\n\nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.s, tt.stmt, stmt)
  1880. }
  1881. }
  1882. }
  1883. func TestParser_ParseJoins(t *testing.T) {
  1884. var tests = []struct {
  1885. s string
  1886. stmt *ast.SelectStatement
  1887. err string
  1888. }{
  1889. {
  1890. s: `SELECT * FROM topic/sensor1 LEFT JOIN topic1 ON f=k`,
  1891. stmt: &ast.SelectStatement{
  1892. Fields: []ast.Field{
  1893. {
  1894. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  1895. Name: "",
  1896. AName: ""},
  1897. },
  1898. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  1899. Joins: []ast.Join{
  1900. {
  1901. Name: "topic1", Alias: "", JoinType: ast.LEFT_JOIN, Expr: &ast.BinaryExpr{
  1902. LHS: &ast.FieldRef{Name: "f", StreamName: ast.DefaultStream},
  1903. OP: ast.EQ,
  1904. RHS: &ast.FieldRef{Name: "k", StreamName: ast.DefaultStream},
  1905. },
  1906. },
  1907. },
  1908. },
  1909. },
  1910. {
  1911. s: `SELECT * FROM topic/sensor1 AS t1 INNER JOIN topic1 AS t2 ON f=k`,
  1912. stmt: &ast.SelectStatement{
  1913. Fields: []ast.Field{
  1914. {
  1915. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  1916. Name: "",
  1917. AName: ""},
  1918. },
  1919. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
  1920. Joins: []ast.Join{
  1921. {
  1922. Name: "topic1", Alias: "t2", JoinType: ast.INNER_JOIN, Expr: &ast.BinaryExpr{
  1923. LHS: &ast.FieldRef{Name: "f", StreamName: ast.DefaultStream},
  1924. OP: ast.EQ,
  1925. RHS: &ast.FieldRef{Name: "k", StreamName: ast.DefaultStream},
  1926. },
  1927. },
  1928. },
  1929. },
  1930. },
  1931. {
  1932. s: `SELECT * FROM topic/sensor1 AS t1 LEFT JOIN topic1/sensor2 AS t2 ON f=k`,
  1933. stmt: &ast.SelectStatement{
  1934. Fields: []ast.Field{
  1935. {
  1936. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  1937. Name: "",
  1938. AName: ""},
  1939. },
  1940. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
  1941. Joins: []ast.Join{
  1942. {
  1943. Name: "topic1/sensor2", Alias: "t2", JoinType: ast.LEFT_JOIN, Expr: &ast.BinaryExpr{
  1944. LHS: &ast.FieldRef{Name: "f", StreamName: ast.DefaultStream},
  1945. OP: ast.EQ,
  1946. RHS: &ast.FieldRef{Name: "k", StreamName: ast.DefaultStream},
  1947. },
  1948. },
  1949. },
  1950. },
  1951. },
  1952. {
  1953. s: `SELECT t1.name FROM topic/sensor1 AS t1 LEFT JOIN topic1/sensor2 AS t2 ON f=k`,
  1954. stmt: &ast.SelectStatement{
  1955. Fields: []ast.Field{
  1956. {
  1957. Expr: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "name"},
  1958. Name: "name",
  1959. AName: ""},
  1960. },
  1961. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
  1962. Joins: []ast.Join{
  1963. {
  1964. Name: "topic1/sensor2", Alias: "t2", JoinType: ast.LEFT_JOIN, Expr: &ast.BinaryExpr{
  1965. LHS: &ast.FieldRef{Name: "f", StreamName: ast.DefaultStream},
  1966. OP: ast.EQ,
  1967. RHS: &ast.FieldRef{Name: "k", StreamName: ast.DefaultStream},
  1968. },
  1969. },
  1970. },
  1971. },
  1972. },
  1973. {
  1974. s: `SELECT t1.name FROM topic/sensor1 AS t1 LEFT JOIN topic1/sensor2 AS t2 ON t1.f=t2.k`,
  1975. stmt: &ast.SelectStatement{
  1976. Fields: []ast.Field{
  1977. {
  1978. Expr: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "name"},
  1979. Name: "name",
  1980. AName: ""},
  1981. },
  1982. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
  1983. Joins: []ast.Join{
  1984. {
  1985. Name: "topic1/sensor2", Alias: "t2", JoinType: ast.LEFT_JOIN, Expr: &ast.BinaryExpr{
  1986. LHS: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "f"},
  1987. OP: ast.EQ,
  1988. RHS: &ast.FieldRef{StreamName: ast.StreamName("t2"), Name: "k"},
  1989. },
  1990. },
  1991. },
  1992. },
  1993. },
  1994. {
  1995. s: `SELECT t1.name FROM topic/sensor1 AS t1 RIGHT JOIN topic1/sensor2 AS t2 ON t1.f=t2.k`,
  1996. stmt: &ast.SelectStatement{
  1997. Fields: []ast.Field{
  1998. {
  1999. Expr: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "name"},
  2000. Name: "name",
  2001. AName: ""},
  2002. },
  2003. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
  2004. Joins: []ast.Join{
  2005. {
  2006. Name: "topic1/sensor2", Alias: "t2", JoinType: ast.RIGHT_JOIN, Expr: &ast.BinaryExpr{
  2007. LHS: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "f"},
  2008. OP: ast.EQ,
  2009. RHS: &ast.FieldRef{StreamName: ast.StreamName("t2"), Name: "k"},
  2010. },
  2011. },
  2012. },
  2013. },
  2014. },
  2015. {
  2016. s: `SELECT t1.name FROM topic/sensor1 AS t1 FULL JOIN topic1/sensor2 AS t2 ON t1.f=t2.k`,
  2017. stmt: &ast.SelectStatement{
  2018. Fields: []ast.Field{
  2019. {
  2020. Expr: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "name"},
  2021. Name: "name",
  2022. AName: ""},
  2023. },
  2024. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
  2025. Joins: []ast.Join{
  2026. {
  2027. Name: "topic1/sensor2", Alias: "t2", JoinType: ast.FULL_JOIN, Expr: &ast.BinaryExpr{
  2028. LHS: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "f"},
  2029. OP: ast.EQ,
  2030. RHS: &ast.FieldRef{StreamName: ast.StreamName("t2"), Name: "k"},
  2031. },
  2032. },
  2033. },
  2034. },
  2035. },
  2036. {
  2037. s: `SELECT t1.name FROM topic/sensor1 AS t1 CROSS JOIN topic1/sensor2 AS t2 ON t1.f=t2.k`,
  2038. stmt: nil,
  2039. err: "On expression is not required for cross join type.\n",
  2040. },
  2041. {
  2042. s: `SELECT t1.name FROM topic/sensor1 AS t1 CROSS JOIN topic1/sensor2 AS t2`,
  2043. stmt: &ast.SelectStatement{
  2044. Fields: []ast.Field{
  2045. {
  2046. Expr: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "name"},
  2047. Name: "name",
  2048. AName: ""},
  2049. },
  2050. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
  2051. Joins: []ast.Join{
  2052. {
  2053. Name: "topic1/sensor2", Alias: "t2", JoinType: ast.CROSS_JOIN, Expr: nil,
  2054. },
  2055. },
  2056. },
  2057. },
  2058. {
  2059. s: `SELECT demo.*, demo2.* FROM demo LEFT JOIN demo2 on demo.f1 = demo2.f2`,
  2060. stmt: &ast.SelectStatement{
  2061. Fields: []ast.Field{
  2062. {
  2063. Expr: &ast.FieldRef{StreamName: ast.StreamName("demo"), Name: "*"},
  2064. Name: "*",
  2065. AName: ""},
  2066. {
  2067. Expr: &ast.FieldRef{StreamName: ast.StreamName("demo2"), Name: "*"},
  2068. Name: "*",
  2069. AName: ""},
  2070. },
  2071. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2072. Joins: []ast.Join{
  2073. {
  2074. Name: "demo2", Alias: "", JoinType: ast.LEFT_JOIN, Expr: &ast.BinaryExpr{
  2075. LHS: &ast.FieldRef{StreamName: ast.StreamName("demo"), Name: "f1"},
  2076. OP: ast.EQ,
  2077. RHS: &ast.FieldRef{StreamName: ast.StreamName("demo2"), Name: "f2"},
  2078. },
  2079. },
  2080. },
  2081. },
  2082. },
  2083. }
  2084. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  2085. for i, tt := range tests {
  2086. stmt, err := NewParser(strings.NewReader(tt.s)).Parse()
  2087. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  2088. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.s, tt.err, err)
  2089. } else if tt.err == "" && !reflect.DeepEqual(tt.stmt, stmt) {
  2090. t.Errorf("%d. %q\n\nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.s, tt.stmt, stmt)
  2091. }
  2092. }
  2093. }
  2094. func TestParser_ParseStatements(t *testing.T) {
  2095. var tests = []struct {
  2096. s string
  2097. stmts []ast.SelectStatement
  2098. err string
  2099. }{
  2100. {s: "SELECT name FROM tbl;\nSELECT name FROM topic/sensor1\n",
  2101. stmts: []ast.SelectStatement{
  2102. {
  2103. Fields: []ast.Field{
  2104. {
  2105. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  2106. Name: "name",
  2107. AName: ""},
  2108. },
  2109. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  2110. },
  2111. {
  2112. Fields: []ast.Field{
  2113. {
  2114. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  2115. Name: "name",
  2116. AName: ""},
  2117. },
  2118. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  2119. },
  2120. },
  2121. },
  2122. {s: "SELECT name FROM tbl;\nSELECT name FROM topic/sensor1;\n--SELECT comment FROM topic/comment",
  2123. stmts: []ast.SelectStatement{
  2124. {
  2125. Fields: []ast.Field{
  2126. {
  2127. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  2128. Name: "name",
  2129. AName: ""},
  2130. },
  2131. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  2132. },
  2133. {
  2134. Fields: []ast.Field{
  2135. {
  2136. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  2137. Name: "name",
  2138. AName: ""},
  2139. },
  2140. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  2141. },
  2142. },
  2143. },
  2144. }
  2145. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  2146. for i, tt := range tests {
  2147. stmts, err := NewParser(strings.NewReader(tt.s)).ParseQueries()
  2148. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  2149. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.s, tt.err, err)
  2150. } else if tt.err == "" && !reflect.DeepEqual(tt.stmts, stmts) {
  2151. t.Errorf("%d. %q\n\nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.s, tt.stmts, stmts)
  2152. }
  2153. }
  2154. }