parser_test.go 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243
  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: "error getting 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. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  1352. for i, tt := range tests {
  1353. //fmt.Printf("Parsing SQL %q.\n", tt.s)
  1354. stmt, err := NewParser(strings.NewReader(tt.s)).Parse()
  1355. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  1356. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.s, tt.err, err)
  1357. } else if tt.err == "" && !reflect.DeepEqual(tt.stmt, stmt) {
  1358. t.Errorf("%d. %q\n\nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.s, tt.stmt, stmt)
  1359. }
  1360. }
  1361. }
  1362. func TestParser_ParseWindowsExpr(t *testing.T) {
  1363. var tests = []struct {
  1364. s string
  1365. stmt *ast.SelectStatement
  1366. err string
  1367. }{
  1368. {
  1369. s: `SELECT f1 FROM tbl GROUP BY TUMBLINGWINDOW(ss, 10)`,
  1370. stmt: &ast.SelectStatement{
  1371. Fields: []ast.Field{
  1372. {
  1373. Expr: &ast.FieldRef{Name: "f1", StreamName: ast.DefaultStream},
  1374. Name: "f1",
  1375. AName: ""},
  1376. },
  1377. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1378. Dimensions: ast.Dimensions{
  1379. ast.Dimension{
  1380. Expr: &ast.Window{
  1381. WindowType: ast.TUMBLING_WINDOW,
  1382. Length: &ast.IntegerLiteral{Val: 10000},
  1383. Interval: &ast.IntegerLiteral{Val: 0},
  1384. },
  1385. },
  1386. },
  1387. },
  1388. },
  1389. {
  1390. s: `SELECT f1 FROM tbl GROUP BY HOPPINGWINDOW(mi, 5, 1)`,
  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.HOPPING_WINDOW,
  1403. Length: &ast.IntegerLiteral{Val: 3e5},
  1404. Interval: &ast.IntegerLiteral{Val: 6e4},
  1405. },
  1406. },
  1407. },
  1408. },
  1409. },
  1410. {
  1411. s: `SELECT f1 FROM tbl GROUP BY SESSIONWINDOW(hh, 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.SESSION_WINDOW,
  1424. Length: &ast.IntegerLiteral{Val: 1.8e7},
  1425. Interval: &ast.IntegerLiteral{Val: 3.6e6},
  1426. },
  1427. },
  1428. },
  1429. },
  1430. },
  1431. {
  1432. s: `SELECT f1 FROM tbl GROUP BY SLIDINGWINDOW(ms, 5)`,
  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.SLIDING_WINDOW,
  1445. Length: &ast.IntegerLiteral{Val: 5},
  1446. Interval: &ast.IntegerLiteral{Val: 0},
  1447. },
  1448. },
  1449. },
  1450. },
  1451. },
  1452. {
  1453. s: `SELECT f1 FROM tbl GROUP BY SLIDINGWINDOW(mi, 5, 1)`,
  1454. stmt: nil,
  1455. err: "The arguments for slidingwindow should be 2.\n",
  1456. },
  1457. {
  1458. s: `SELECT f1 FROM tbl GROUP BY SLIDINGWINDOW("mi", 5)`,
  1459. stmt: nil,
  1460. err: "The 1st argument for slidingwindow is expecting timer literal expression. One value of [dd|hh|mi|ss|ms].\n",
  1461. },
  1462. {
  1463. s: `SELECT f1 FROM tbl GROUP BY COUNTWINDOW(10)`,
  1464. stmt: &ast.SelectStatement{
  1465. Fields: []ast.Field{
  1466. {
  1467. Expr: &ast.FieldRef{Name: "f1", StreamName: ast.DefaultStream},
  1468. Name: "f1",
  1469. AName: ""},
  1470. },
  1471. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1472. Dimensions: ast.Dimensions{
  1473. ast.Dimension{
  1474. Expr: &ast.Window{
  1475. WindowType: ast.COUNT_WINDOW,
  1476. Length: &ast.IntegerLiteral{Val: 10},
  1477. },
  1478. },
  1479. },
  1480. },
  1481. },
  1482. {
  1483. s: `SELECT f1 FROM tbl GROUP BY COUNTWINDOW(10, 5)`,
  1484. stmt: &ast.SelectStatement{
  1485. Fields: []ast.Field{
  1486. {
  1487. Expr: &ast.FieldRef{Name: "f1", StreamName: ast.DefaultStream},
  1488. Name: "f1",
  1489. AName: ""},
  1490. },
  1491. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1492. Dimensions: ast.Dimensions{
  1493. ast.Dimension{
  1494. Expr: &ast.Window{
  1495. WindowType: ast.COUNT_WINDOW,
  1496. Length: &ast.IntegerLiteral{Val: 10},
  1497. Interval: &ast.IntegerLiteral{Val: 5},
  1498. },
  1499. },
  1500. },
  1501. },
  1502. },
  1503. {
  1504. s: `SELECT f1 FROM tbl GROUP BY COUNTWINDOW(3, 5)`,
  1505. stmt: nil,
  1506. err: "The second parameter value 5 should be less than the first parameter 3.",
  1507. },
  1508. {
  1509. s: `SELECT * FROM demo GROUP BY COUNTWINDOW(3,1) FILTER( where revenue > 100 )`,
  1510. stmt: &ast.SelectStatement{
  1511. Fields: []ast.Field{
  1512. {
  1513. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  1514. Name: "",
  1515. AName: ""},
  1516. },
  1517. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  1518. Dimensions: ast.Dimensions{
  1519. ast.Dimension{
  1520. Expr: &ast.Window{
  1521. WindowType: ast.COUNT_WINDOW,
  1522. Length: &ast.IntegerLiteral{Val: 3},
  1523. Interval: &ast.IntegerLiteral{Val: 1},
  1524. Filter: &ast.BinaryExpr{
  1525. LHS: &ast.FieldRef{Name: "revenue", StreamName: ast.DefaultStream},
  1526. OP: ast.GT,
  1527. RHS: &ast.IntegerLiteral{Val: 100},
  1528. },
  1529. },
  1530. },
  1531. },
  1532. },
  1533. },
  1534. {
  1535. s: `SELECT * FROM demo GROUP BY department, COUNTWINDOW(3,1) FILTER( where revenue > 100 ), year`,
  1536. stmt: &ast.SelectStatement{
  1537. Fields: []ast.Field{
  1538. {
  1539. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  1540. Name: "",
  1541. AName: ""},
  1542. },
  1543. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  1544. Dimensions: ast.Dimensions{
  1545. ast.Dimension{Expr: &ast.FieldRef{Name: "department", StreamName: ast.DefaultStream}},
  1546. ast.Dimension{
  1547. Expr: &ast.Window{
  1548. WindowType: ast.COUNT_WINDOW,
  1549. Length: &ast.IntegerLiteral{Val: 3},
  1550. Interval: &ast.IntegerLiteral{Val: 1},
  1551. Filter: &ast.BinaryExpr{
  1552. LHS: &ast.FieldRef{Name: "revenue", StreamName: ast.DefaultStream},
  1553. OP: ast.GT,
  1554. RHS: &ast.IntegerLiteral{Val: 100},
  1555. },
  1556. },
  1557. },
  1558. ast.Dimension{Expr: &ast.FieldRef{Name: "year", StreamName: ast.DefaultStream}},
  1559. },
  1560. },
  1561. },
  1562. //to be supported
  1563. {
  1564. s: `SELECT sum(f1) FILTER( where revenue > 100 ) FROM tbl GROUP BY year`,
  1565. stmt: nil,
  1566. err: "found \"FILTER\", expected FROM.",
  1567. },
  1568. {
  1569. s: `SELECT * FROM demo GROUP BY COUNTWINDOW(3,1) FILTER where revenue > 100`,
  1570. stmt: nil,
  1571. err: "Found \"WHERE\" after FILTER, expect parentheses.",
  1572. },
  1573. {
  1574. s: `SELECT * FROM demo GROUP BY COUNTWINDOW(3,1) where revenue > 100`,
  1575. stmt: nil,
  1576. err: "found \"WHERE\", expected EOF.",
  1577. },
  1578. }
  1579. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  1580. for i, tt := range tests {
  1581. //fmt.Printf("Parsing SQL %q.\n", tt.s)
  1582. stmt, err := NewParser(strings.NewReader(tt.s)).Parse()
  1583. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  1584. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.s, tt.err, err)
  1585. } else if tt.err == "" && !reflect.DeepEqual(tt.stmt, stmt) {
  1586. t.Errorf("%d. %q\n\nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.s, tt.stmt, stmt)
  1587. }
  1588. }
  1589. }
  1590. func TestParser_ParseJsonExpr(t *testing.T) {
  1591. var tests = []struct {
  1592. s string
  1593. stmt *ast.SelectStatement
  1594. err string
  1595. }{
  1596. {
  1597. s: `SELECT children[0] FROM demo`,
  1598. stmt: &ast.SelectStatement{
  1599. Fields: []ast.Field{
  1600. {
  1601. Expr: &ast.BinaryExpr{
  1602. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  1603. OP: ast.SUBSET,
  1604. RHS: &ast.IndexExpr{Index: 0},
  1605. },
  1606. Name: "",
  1607. AName: ""},
  1608. },
  1609. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  1610. },
  1611. },
  1612. {
  1613. s: `SELECT children[0]->first FROM demo`,
  1614. stmt: &ast.SelectStatement{
  1615. Fields: []ast.Field{
  1616. {
  1617. Expr: &ast.BinaryExpr{
  1618. LHS: &ast.BinaryExpr{
  1619. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  1620. OP: ast.SUBSET,
  1621. RHS: &ast.IndexExpr{Index: 0},
  1622. },
  1623. OP: ast.ARROW,
  1624. RHS: &ast.FieldRef{Name: "first"},
  1625. },
  1626. Name: "",
  1627. AName: ""},
  1628. },
  1629. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  1630. },
  1631. },
  1632. {
  1633. s: `SELECT children->first[2] FROM demo`,
  1634. stmt: &ast.SelectStatement{
  1635. Fields: []ast.Field{
  1636. {
  1637. Expr: &ast.BinaryExpr{
  1638. LHS: &ast.BinaryExpr{
  1639. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  1640. OP: ast.ARROW,
  1641. RHS: &ast.FieldRef{Name: "first"},
  1642. },
  1643. OP: ast.SUBSET,
  1644. RHS: &ast.IndexExpr{Index: 2},
  1645. },
  1646. Name: "",
  1647. AName: ""},
  1648. },
  1649. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  1650. },
  1651. },
  1652. {
  1653. s: `SELECT children->first[2]->test FROM demo`,
  1654. stmt: &ast.SelectStatement{
  1655. Fields: []ast.Field{
  1656. {
  1657. Expr: &ast.BinaryExpr{
  1658. LHS: &ast.BinaryExpr{
  1659. LHS: &ast.BinaryExpr{
  1660. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  1661. OP: ast.ARROW,
  1662. RHS: &ast.FieldRef{Name: "first"},
  1663. },
  1664. OP: ast.SUBSET,
  1665. RHS: &ast.IndexExpr{Index: 2},
  1666. },
  1667. OP: ast.ARROW,
  1668. RHS: &ast.FieldRef{Name: "test"},
  1669. },
  1670. Name: "",
  1671. AName: ""},
  1672. },
  1673. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  1674. },
  1675. },
  1676. {
  1677. s: `SELECT children[0:1] FROM demo`,
  1678. stmt: &ast.SelectStatement{
  1679. Fields: []ast.Field{
  1680. {
  1681. Expr: &ast.BinaryExpr{
  1682. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  1683. OP: ast.SUBSET,
  1684. RHS: &ast.ColonExpr{Start: 0, End: 1},
  1685. },
  1686. Name: "",
  1687. AName: ""},
  1688. },
  1689. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  1690. },
  1691. },
  1692. {
  1693. s: `SELECT children[:1] FROM demo`,
  1694. stmt: &ast.SelectStatement{
  1695. Fields: []ast.Field{
  1696. {
  1697. Expr: &ast.BinaryExpr{
  1698. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  1699. OP: ast.SUBSET,
  1700. RHS: &ast.ColonExpr{Start: 0, End: 1},
  1701. },
  1702. Name: "",
  1703. AName: ""},
  1704. },
  1705. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  1706. },
  1707. },
  1708. {
  1709. s: `SELECT children[:] FROM demo`,
  1710. stmt: &ast.SelectStatement{
  1711. Fields: []ast.Field{
  1712. {
  1713. Expr: &ast.BinaryExpr{
  1714. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  1715. OP: ast.SUBSET,
  1716. RHS: &ast.ColonExpr{Start: 0, End: math.MinInt32},
  1717. },
  1718. Name: "",
  1719. AName: ""},
  1720. },
  1721. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  1722. },
  1723. },
  1724. {
  1725. s: `SELECT children[2:] AS c FROM demo`,
  1726. stmt: &ast.SelectStatement{
  1727. Fields: []ast.Field{
  1728. {
  1729. Expr: &ast.BinaryExpr{
  1730. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  1731. OP: ast.SUBSET,
  1732. RHS: &ast.ColonExpr{Start: 2, End: math.MinInt32},
  1733. },
  1734. Name: "",
  1735. AName: "c"},
  1736. },
  1737. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  1738. },
  1739. },
  1740. {
  1741. s: `SELECT children[2:]->first AS c FROM demo`,
  1742. stmt: &ast.SelectStatement{
  1743. Fields: []ast.Field{
  1744. {
  1745. Expr: &ast.BinaryExpr{
  1746. LHS: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream}, OP: ast.SUBSET, RHS: &ast.ColonExpr{Start: 2, End: math.MinInt32}},
  1747. OP: ast.ARROW,
  1748. RHS: &ast.FieldRef{Name: "first"},
  1749. },
  1750. Name: "",
  1751. AName: "c"},
  1752. },
  1753. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  1754. },
  1755. },
  1756. {
  1757. s: `SELECT demo.* FROM demo`,
  1758. stmt: &ast.SelectStatement{
  1759. Fields: []ast.Field{
  1760. {
  1761. Expr: &ast.FieldRef{StreamName: ast.StreamName("demo"), Name: "*"},
  1762. Name: "*",
  1763. AName: ""},
  1764. },
  1765. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  1766. },
  1767. },
  1768. {
  1769. s: `SELECT demo.children[2:]->first AS c FROM demo`,
  1770. stmt: &ast.SelectStatement{
  1771. Fields: []ast.Field{
  1772. {
  1773. Expr: &ast.BinaryExpr{
  1774. LHS: &ast.BinaryExpr{LHS: &ast.FieldRef{StreamName: ast.StreamName("demo"), Name: "children"}, OP: ast.SUBSET, RHS: &ast.ColonExpr{Start: 2, End: math.MinInt32}},
  1775. OP: ast.ARROW,
  1776. RHS: &ast.FieldRef{Name: "first"},
  1777. },
  1778. Name: "",
  1779. AName: "c"},
  1780. },
  1781. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  1782. },
  1783. },
  1784. {
  1785. s: `SELECT lower(demo.children[2:]->first) AS c FROM demo`,
  1786. stmt: &ast.SelectStatement{
  1787. Fields: []ast.Field{
  1788. {
  1789. Expr: &ast.Call{
  1790. Name: "lower",
  1791. Args: []ast.Expr{
  1792. &ast.BinaryExpr{
  1793. LHS: &ast.BinaryExpr{LHS: &ast.FieldRef{StreamName: ast.StreamName("demo"), Name: "children"}, OP: ast.SUBSET, RHS: &ast.ColonExpr{Start: 2, End: math.MinInt32}},
  1794. OP: ast.ARROW,
  1795. RHS: &ast.FieldRef{Name: "first"},
  1796. },
  1797. },
  1798. },
  1799. Name: "lower",
  1800. AName: "c"},
  1801. },
  1802. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  1803. },
  1804. },
  1805. {
  1806. s: `SELECT children[:1] FROM demo WHERE abc[0] > 12`,
  1807. stmt: &ast.SelectStatement{
  1808. Fields: []ast.Field{
  1809. {
  1810. Expr: &ast.BinaryExpr{
  1811. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  1812. OP: ast.SUBSET,
  1813. RHS: &ast.ColonExpr{Start: 0, End: 1},
  1814. },
  1815. Name: "",
  1816. AName: ""},
  1817. },
  1818. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  1819. Condition: &ast.BinaryExpr{
  1820. LHS: &ast.BinaryExpr{
  1821. LHS: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream},
  1822. OP: ast.SUBSET,
  1823. RHS: &ast.IndexExpr{Index: 0},
  1824. },
  1825. OP: ast.GT,
  1826. RHS: &ast.IntegerLiteral{Val: 12},
  1827. },
  1828. },
  1829. },
  1830. {
  1831. s: `SELECT demo.children.first AS c FROM demo`,
  1832. stmt: nil,
  1833. err: "Too many field names. Please use -> to reference keys in struct.\n",
  1834. },
  1835. }
  1836. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  1837. for i, tt := range tests {
  1838. stmt, err := NewParser(strings.NewReader(tt.s)).Parse()
  1839. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  1840. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.s, tt.err, err)
  1841. } else if tt.err == "" && !reflect.DeepEqual(tt.stmt, stmt) {
  1842. t.Errorf("%d. %q\n\nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.s, tt.stmt, stmt)
  1843. }
  1844. }
  1845. }
  1846. func TestParser_ParseJoins(t *testing.T) {
  1847. var tests = []struct {
  1848. s string
  1849. stmt *ast.SelectStatement
  1850. err string
  1851. }{
  1852. {
  1853. s: `SELECT * FROM topic/sensor1 LEFT JOIN topic1 ON f=k`,
  1854. stmt: &ast.SelectStatement{
  1855. Fields: []ast.Field{
  1856. {
  1857. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  1858. Name: "",
  1859. AName: ""},
  1860. },
  1861. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  1862. Joins: []ast.Join{
  1863. {
  1864. Name: "topic1", Alias: "", JoinType: ast.LEFT_JOIN, Expr: &ast.BinaryExpr{
  1865. LHS: &ast.FieldRef{Name: "f", StreamName: ast.DefaultStream},
  1866. OP: ast.EQ,
  1867. RHS: &ast.FieldRef{Name: "k", StreamName: ast.DefaultStream},
  1868. },
  1869. },
  1870. },
  1871. },
  1872. },
  1873. {
  1874. s: `SELECT * FROM topic/sensor1 AS t1 INNER JOIN topic1 AS t2 ON f=k`,
  1875. stmt: &ast.SelectStatement{
  1876. Fields: []ast.Field{
  1877. {
  1878. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  1879. Name: "",
  1880. AName: ""},
  1881. },
  1882. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
  1883. Joins: []ast.Join{
  1884. {
  1885. Name: "topic1", Alias: "t2", JoinType: ast.INNER_JOIN, Expr: &ast.BinaryExpr{
  1886. LHS: &ast.FieldRef{Name: "f", StreamName: ast.DefaultStream},
  1887. OP: ast.EQ,
  1888. RHS: &ast.FieldRef{Name: "k", StreamName: ast.DefaultStream},
  1889. },
  1890. },
  1891. },
  1892. },
  1893. },
  1894. {
  1895. s: `SELECT * FROM topic/sensor1 AS t1 LEFT JOIN topic1/sensor2 AS t2 ON f=k`,
  1896. stmt: &ast.SelectStatement{
  1897. Fields: []ast.Field{
  1898. {
  1899. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  1900. Name: "",
  1901. AName: ""},
  1902. },
  1903. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
  1904. Joins: []ast.Join{
  1905. {
  1906. Name: "topic1/sensor2", Alias: "t2", JoinType: ast.LEFT_JOIN, Expr: &ast.BinaryExpr{
  1907. LHS: &ast.FieldRef{Name: "f", StreamName: ast.DefaultStream},
  1908. OP: ast.EQ,
  1909. RHS: &ast.FieldRef{Name: "k", StreamName: ast.DefaultStream},
  1910. },
  1911. },
  1912. },
  1913. },
  1914. },
  1915. {
  1916. s: `SELECT t1.name FROM topic/sensor1 AS t1 LEFT JOIN topic1/sensor2 AS t2 ON f=k`,
  1917. stmt: &ast.SelectStatement{
  1918. Fields: []ast.Field{
  1919. {
  1920. Expr: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "name"},
  1921. Name: "name",
  1922. AName: ""},
  1923. },
  1924. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
  1925. Joins: []ast.Join{
  1926. {
  1927. Name: "topic1/sensor2", Alias: "t2", JoinType: ast.LEFT_JOIN, Expr: &ast.BinaryExpr{
  1928. LHS: &ast.FieldRef{Name: "f", StreamName: ast.DefaultStream},
  1929. OP: ast.EQ,
  1930. RHS: &ast.FieldRef{Name: "k", StreamName: ast.DefaultStream},
  1931. },
  1932. },
  1933. },
  1934. },
  1935. },
  1936. {
  1937. s: `SELECT t1.name FROM topic/sensor1 AS t1 LEFT JOIN topic1/sensor2 AS t2 ON t1.f=t2.k`,
  1938. stmt: &ast.SelectStatement{
  1939. Fields: []ast.Field{
  1940. {
  1941. Expr: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "name"},
  1942. Name: "name",
  1943. AName: ""},
  1944. },
  1945. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
  1946. Joins: []ast.Join{
  1947. {
  1948. Name: "topic1/sensor2", Alias: "t2", JoinType: ast.LEFT_JOIN, Expr: &ast.BinaryExpr{
  1949. LHS: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "f"},
  1950. OP: ast.EQ,
  1951. RHS: &ast.FieldRef{StreamName: ast.StreamName("t2"), Name: "k"},
  1952. },
  1953. },
  1954. },
  1955. },
  1956. },
  1957. {
  1958. s: `SELECT t1.name FROM topic/sensor1 AS t1 RIGHT JOIN topic1/sensor2 AS t2 ON t1.f=t2.k`,
  1959. stmt: &ast.SelectStatement{
  1960. Fields: []ast.Field{
  1961. {
  1962. Expr: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "name"},
  1963. Name: "name",
  1964. AName: ""},
  1965. },
  1966. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
  1967. Joins: []ast.Join{
  1968. {
  1969. Name: "topic1/sensor2", Alias: "t2", JoinType: ast.RIGHT_JOIN, Expr: &ast.BinaryExpr{
  1970. LHS: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "f"},
  1971. OP: ast.EQ,
  1972. RHS: &ast.FieldRef{StreamName: ast.StreamName("t2"), Name: "k"},
  1973. },
  1974. },
  1975. },
  1976. },
  1977. },
  1978. {
  1979. s: `SELECT t1.name FROM topic/sensor1 AS t1 FULL JOIN topic1/sensor2 AS t2 ON t1.f=t2.k`,
  1980. stmt: &ast.SelectStatement{
  1981. Fields: []ast.Field{
  1982. {
  1983. Expr: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "name"},
  1984. Name: "name",
  1985. AName: ""},
  1986. },
  1987. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
  1988. Joins: []ast.Join{
  1989. {
  1990. Name: "topic1/sensor2", Alias: "t2", JoinType: ast.FULL_JOIN, Expr: &ast.BinaryExpr{
  1991. LHS: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "f"},
  1992. OP: ast.EQ,
  1993. RHS: &ast.FieldRef{StreamName: ast.StreamName("t2"), Name: "k"},
  1994. },
  1995. },
  1996. },
  1997. },
  1998. },
  1999. {
  2000. s: `SELECT t1.name FROM topic/sensor1 AS t1 CROSS JOIN topic1/sensor2 AS t2 ON t1.f=t2.k`,
  2001. stmt: nil,
  2002. err: "On expression is not required for cross join type.\n",
  2003. },
  2004. {
  2005. s: `SELECT t1.name FROM topic/sensor1 AS t1 CROSS JOIN topic1/sensor2 AS t2`,
  2006. stmt: &ast.SelectStatement{
  2007. Fields: []ast.Field{
  2008. {
  2009. Expr: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "name"},
  2010. Name: "name",
  2011. AName: ""},
  2012. },
  2013. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
  2014. Joins: []ast.Join{
  2015. {
  2016. Name: "topic1/sensor2", Alias: "t2", JoinType: ast.CROSS_JOIN, Expr: nil,
  2017. },
  2018. },
  2019. },
  2020. },
  2021. {
  2022. s: `SELECT demo.*, demo2.* FROM demo LEFT JOIN demo2 on demo.f1 = demo2.f2`,
  2023. stmt: &ast.SelectStatement{
  2024. Fields: []ast.Field{
  2025. {
  2026. Expr: &ast.FieldRef{StreamName: ast.StreamName("demo"), Name: "*"},
  2027. Name: "*",
  2028. AName: ""},
  2029. {
  2030. Expr: &ast.FieldRef{StreamName: ast.StreamName("demo2"), Name: "*"},
  2031. Name: "*",
  2032. AName: ""},
  2033. },
  2034. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2035. Joins: []ast.Join{
  2036. {
  2037. Name: "demo2", Alias: "", JoinType: ast.LEFT_JOIN, Expr: &ast.BinaryExpr{
  2038. LHS: &ast.FieldRef{StreamName: ast.StreamName("demo"), Name: "f1"},
  2039. OP: ast.EQ,
  2040. RHS: &ast.FieldRef{StreamName: ast.StreamName("demo2"), Name: "f2"},
  2041. },
  2042. },
  2043. },
  2044. },
  2045. },
  2046. }
  2047. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  2048. for i, tt := range tests {
  2049. stmt, err := NewParser(strings.NewReader(tt.s)).Parse()
  2050. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  2051. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.s, tt.err, err)
  2052. } else if tt.err == "" && !reflect.DeepEqual(tt.stmt, stmt) {
  2053. t.Errorf("%d. %q\n\nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.s, tt.stmt, stmt)
  2054. }
  2055. }
  2056. }
  2057. func TestParser_ParseStatements(t *testing.T) {
  2058. var tests = []struct {
  2059. s string
  2060. stmts []ast.SelectStatement
  2061. err string
  2062. }{
  2063. {s: "SELECT name FROM tbl;\nSELECT name FROM topic/sensor1\n",
  2064. stmts: []ast.SelectStatement{
  2065. {
  2066. Fields: []ast.Field{
  2067. {
  2068. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  2069. Name: "name",
  2070. AName: ""},
  2071. },
  2072. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  2073. },
  2074. {
  2075. Fields: []ast.Field{
  2076. {
  2077. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  2078. Name: "name",
  2079. AName: ""},
  2080. },
  2081. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  2082. },
  2083. },
  2084. },
  2085. {s: "SELECT name FROM tbl;\nSELECT name FROM topic/sensor1;\n--SELECT comment FROM topic/comment",
  2086. stmts: []ast.SelectStatement{
  2087. {
  2088. Fields: []ast.Field{
  2089. {
  2090. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  2091. Name: "name",
  2092. AName: ""},
  2093. },
  2094. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  2095. },
  2096. {
  2097. Fields: []ast.Field{
  2098. {
  2099. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  2100. Name: "name",
  2101. AName: ""},
  2102. },
  2103. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  2104. },
  2105. },
  2106. },
  2107. }
  2108. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  2109. for i, tt := range tests {
  2110. stmts, err := NewParser(strings.NewReader(tt.s)).ParseQueries()
  2111. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  2112. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.s, tt.err, err)
  2113. } else if tt.err == "" && !reflect.DeepEqual(tt.stmts, stmts) {
  2114. t.Errorf("%d. %q\n\nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.s, tt.stmts, stmts)
  2115. }
  2116. }
  2117. }