parser_test.go 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264
  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 count(*)-10 FROM demo`,
  1309. stmt: &ast.SelectStatement{
  1310. Fields: []ast.Field{
  1311. {
  1312. AName: "",
  1313. Name: "",
  1314. Expr: &ast.BinaryExpr{
  1315. OP: ast.SUB,
  1316. LHS: &ast.Call{
  1317. Name: "count",
  1318. Args: []ast.Expr{
  1319. &ast.Wildcard{Token: ast.ASTERISK},
  1320. },
  1321. },
  1322. RHS: &ast.IntegerLiteral{Val: 10},
  1323. },
  1324. },
  1325. },
  1326. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  1327. },
  1328. },
  1329. {
  1330. s: `SELECT -abc FROM demo`,
  1331. stmt: nil,
  1332. err: "found \"-\", expected expression.",
  1333. },
  1334. {
  1335. s: `SELECT meta(*) FROM tbl`,
  1336. stmt: &ast.SelectStatement{
  1337. Fields: []ast.Field{
  1338. {
  1339. AName: "",
  1340. Name: "meta",
  1341. Expr: &ast.Call{
  1342. Name: "meta",
  1343. Args: []ast.Expr{
  1344. &ast.MetaRef{
  1345. Name: "*",
  1346. StreamName: ast.DefaultStream,
  1347. },
  1348. },
  1349. },
  1350. },
  1351. },
  1352. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1353. },
  1354. },
  1355. }
  1356. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  1357. for i, tt := range tests {
  1358. //fmt.Printf("Parsing SQL %q.\n", tt.s)
  1359. stmt, err := NewParser(strings.NewReader(tt.s)).Parse()
  1360. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  1361. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.s, tt.err, err)
  1362. } else if tt.err == "" && !reflect.DeepEqual(tt.stmt, stmt) {
  1363. t.Errorf("%d. %q\n\nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.s, tt.stmt, stmt)
  1364. }
  1365. }
  1366. }
  1367. func TestParser_ParseWindowsExpr(t *testing.T) {
  1368. var tests = []struct {
  1369. s string
  1370. stmt *ast.SelectStatement
  1371. err string
  1372. }{
  1373. {
  1374. s: `SELECT f1 FROM tbl GROUP BY TUMBLINGWINDOW(ss, 10)`,
  1375. stmt: &ast.SelectStatement{
  1376. Fields: []ast.Field{
  1377. {
  1378. Expr: &ast.FieldRef{Name: "f1", StreamName: ast.DefaultStream},
  1379. Name: "f1",
  1380. AName: ""},
  1381. },
  1382. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1383. Dimensions: ast.Dimensions{
  1384. ast.Dimension{
  1385. Expr: &ast.Window{
  1386. WindowType: ast.TUMBLING_WINDOW,
  1387. Length: &ast.IntegerLiteral{Val: 10000},
  1388. Interval: &ast.IntegerLiteral{Val: 0},
  1389. },
  1390. },
  1391. },
  1392. },
  1393. },
  1394. {
  1395. s: `SELECT f1 FROM tbl GROUP BY HOPPINGWINDOW(mi, 5, 1)`,
  1396. stmt: &ast.SelectStatement{
  1397. Fields: []ast.Field{
  1398. {
  1399. Expr: &ast.FieldRef{Name: "f1", StreamName: ast.DefaultStream},
  1400. Name: "f1",
  1401. AName: ""},
  1402. },
  1403. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1404. Dimensions: ast.Dimensions{
  1405. ast.Dimension{
  1406. Expr: &ast.Window{
  1407. WindowType: ast.HOPPING_WINDOW,
  1408. Length: &ast.IntegerLiteral{Val: 3e5},
  1409. Interval: &ast.IntegerLiteral{Val: 6e4},
  1410. },
  1411. },
  1412. },
  1413. },
  1414. },
  1415. {
  1416. s: `SELECT f1 FROM tbl GROUP BY SESSIONWINDOW(hh, 5, 1)`,
  1417. stmt: &ast.SelectStatement{
  1418. Fields: []ast.Field{
  1419. {
  1420. Expr: &ast.FieldRef{Name: "f1", StreamName: ast.DefaultStream},
  1421. Name: "f1",
  1422. AName: ""},
  1423. },
  1424. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1425. Dimensions: ast.Dimensions{
  1426. ast.Dimension{
  1427. Expr: &ast.Window{
  1428. WindowType: ast.SESSION_WINDOW,
  1429. Length: &ast.IntegerLiteral{Val: 1.8e7},
  1430. Interval: &ast.IntegerLiteral{Val: 3.6e6},
  1431. },
  1432. },
  1433. },
  1434. },
  1435. },
  1436. {
  1437. s: `SELECT f1 FROM tbl GROUP BY SLIDINGWINDOW(ms, 5)`,
  1438. stmt: &ast.SelectStatement{
  1439. Fields: []ast.Field{
  1440. {
  1441. Expr: &ast.FieldRef{Name: "f1", StreamName: ast.DefaultStream},
  1442. Name: "f1",
  1443. AName: ""},
  1444. },
  1445. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1446. Dimensions: ast.Dimensions{
  1447. ast.Dimension{
  1448. Expr: &ast.Window{
  1449. WindowType: ast.SLIDING_WINDOW,
  1450. Length: &ast.IntegerLiteral{Val: 5},
  1451. Interval: &ast.IntegerLiteral{Val: 0},
  1452. },
  1453. },
  1454. },
  1455. },
  1456. },
  1457. {
  1458. s: `SELECT f1 FROM tbl GROUP BY SLIDINGWINDOW(mi, 5, 1)`,
  1459. stmt: nil,
  1460. err: "The arguments for slidingwindow should be 2.\n",
  1461. },
  1462. {
  1463. s: `SELECT f1 FROM tbl GROUP BY SLIDINGWINDOW("mi", 5)`,
  1464. stmt: nil,
  1465. err: "The 1st argument for slidingwindow is expecting timer literal expression. One value of [dd|hh|mi|ss|ms].\n",
  1466. },
  1467. {
  1468. s: `SELECT f1 FROM tbl GROUP BY COUNTWINDOW(10)`,
  1469. stmt: &ast.SelectStatement{
  1470. Fields: []ast.Field{
  1471. {
  1472. Expr: &ast.FieldRef{Name: "f1", StreamName: ast.DefaultStream},
  1473. Name: "f1",
  1474. AName: ""},
  1475. },
  1476. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1477. Dimensions: ast.Dimensions{
  1478. ast.Dimension{
  1479. Expr: &ast.Window{
  1480. WindowType: ast.COUNT_WINDOW,
  1481. Length: &ast.IntegerLiteral{Val: 10},
  1482. },
  1483. },
  1484. },
  1485. },
  1486. },
  1487. {
  1488. s: `SELECT f1 FROM tbl GROUP BY COUNTWINDOW(10, 5)`,
  1489. stmt: &ast.SelectStatement{
  1490. Fields: []ast.Field{
  1491. {
  1492. Expr: &ast.FieldRef{Name: "f1", StreamName: ast.DefaultStream},
  1493. Name: "f1",
  1494. AName: ""},
  1495. },
  1496. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1497. Dimensions: ast.Dimensions{
  1498. ast.Dimension{
  1499. Expr: &ast.Window{
  1500. WindowType: ast.COUNT_WINDOW,
  1501. Length: &ast.IntegerLiteral{Val: 10},
  1502. Interval: &ast.IntegerLiteral{Val: 5},
  1503. },
  1504. },
  1505. },
  1506. },
  1507. },
  1508. {
  1509. s: `SELECT f1 FROM tbl GROUP BY COUNTWINDOW(3, 5)`,
  1510. stmt: nil,
  1511. err: "The second parameter value 5 should be less than the first parameter 3.",
  1512. },
  1513. {
  1514. s: `SELECT * FROM demo GROUP BY COUNTWINDOW(3,1) FILTER( where revenue > 100 )`,
  1515. stmt: &ast.SelectStatement{
  1516. Fields: []ast.Field{
  1517. {
  1518. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  1519. Name: "",
  1520. AName: ""},
  1521. },
  1522. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  1523. Dimensions: ast.Dimensions{
  1524. ast.Dimension{
  1525. Expr: &ast.Window{
  1526. WindowType: ast.COUNT_WINDOW,
  1527. Length: &ast.IntegerLiteral{Val: 3},
  1528. Interval: &ast.IntegerLiteral{Val: 1},
  1529. Filter: &ast.BinaryExpr{
  1530. LHS: &ast.FieldRef{Name: "revenue", StreamName: ast.DefaultStream},
  1531. OP: ast.GT,
  1532. RHS: &ast.IntegerLiteral{Val: 100},
  1533. },
  1534. },
  1535. },
  1536. },
  1537. },
  1538. },
  1539. {
  1540. s: `SELECT * FROM demo GROUP BY department, COUNTWINDOW(3,1) FILTER( where revenue > 100 ), year`,
  1541. stmt: &ast.SelectStatement{
  1542. Fields: []ast.Field{
  1543. {
  1544. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  1545. Name: "",
  1546. AName: ""},
  1547. },
  1548. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  1549. Dimensions: ast.Dimensions{
  1550. ast.Dimension{Expr: &ast.FieldRef{Name: "department", StreamName: ast.DefaultStream}},
  1551. ast.Dimension{
  1552. Expr: &ast.Window{
  1553. WindowType: ast.COUNT_WINDOW,
  1554. Length: &ast.IntegerLiteral{Val: 3},
  1555. Interval: &ast.IntegerLiteral{Val: 1},
  1556. Filter: &ast.BinaryExpr{
  1557. LHS: &ast.FieldRef{Name: "revenue", StreamName: ast.DefaultStream},
  1558. OP: ast.GT,
  1559. RHS: &ast.IntegerLiteral{Val: 100},
  1560. },
  1561. },
  1562. },
  1563. ast.Dimension{Expr: &ast.FieldRef{Name: "year", StreamName: ast.DefaultStream}},
  1564. },
  1565. },
  1566. },
  1567. //to be supported
  1568. {
  1569. s: `SELECT sum(f1) FILTER( where revenue > 100 ) FROM tbl GROUP BY year`,
  1570. stmt: nil,
  1571. err: "found \"FILTER\", expected FROM.",
  1572. },
  1573. {
  1574. s: `SELECT * FROM demo GROUP BY COUNTWINDOW(3,1) FILTER where revenue > 100`,
  1575. stmt: nil,
  1576. err: "Found \"WHERE\" after FILTER, expect parentheses.",
  1577. },
  1578. {
  1579. s: `SELECT * FROM demo GROUP BY COUNTWINDOW(3,1) where revenue > 100`,
  1580. stmt: nil,
  1581. err: "found \"WHERE\", expected EOF.",
  1582. },
  1583. }
  1584. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  1585. for i, tt := range tests {
  1586. //fmt.Printf("Parsing SQL %q.\n", tt.s)
  1587. stmt, err := NewParser(strings.NewReader(tt.s)).Parse()
  1588. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  1589. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.s, tt.err, err)
  1590. } else if tt.err == "" && !reflect.DeepEqual(tt.stmt, stmt) {
  1591. t.Errorf("%d. %q\n\nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.s, tt.stmt, stmt)
  1592. }
  1593. }
  1594. }
  1595. func TestParser_ParseJsonExpr(t *testing.T) {
  1596. var tests = []struct {
  1597. s string
  1598. stmt *ast.SelectStatement
  1599. err string
  1600. }{
  1601. {
  1602. s: `SELECT children[0] FROM demo`,
  1603. stmt: &ast.SelectStatement{
  1604. Fields: []ast.Field{
  1605. {
  1606. Expr: &ast.BinaryExpr{
  1607. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  1608. OP: ast.SUBSET,
  1609. RHS: &ast.IndexExpr{Index: &ast.IntegerLiteral{Val: 0}},
  1610. },
  1611. Name: "",
  1612. AName: ""},
  1613. },
  1614. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  1615. },
  1616. },
  1617. {
  1618. s: `SELECT children[0]->first FROM demo`,
  1619. stmt: &ast.SelectStatement{
  1620. Fields: []ast.Field{
  1621. {
  1622. Expr: &ast.BinaryExpr{
  1623. LHS: &ast.BinaryExpr{
  1624. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  1625. OP: ast.SUBSET,
  1626. RHS: &ast.IndexExpr{Index: &ast.IntegerLiteral{Val: 0}},
  1627. },
  1628. OP: ast.ARROW,
  1629. RHS: &ast.JsonFieldRef{Name: "first"},
  1630. },
  1631. Name: "",
  1632. AName: ""},
  1633. },
  1634. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  1635. },
  1636. },
  1637. {
  1638. s: `SELECT children->first[2] FROM demo`,
  1639. stmt: &ast.SelectStatement{
  1640. Fields: []ast.Field{
  1641. {
  1642. Expr: &ast.BinaryExpr{
  1643. LHS: &ast.BinaryExpr{
  1644. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  1645. OP: ast.ARROW,
  1646. RHS: &ast.JsonFieldRef{Name: "first"},
  1647. },
  1648. OP: ast.SUBSET,
  1649. RHS: &ast.IndexExpr{Index: &ast.IntegerLiteral{Val: 2}},
  1650. },
  1651. Name: "",
  1652. AName: ""},
  1653. },
  1654. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  1655. },
  1656. },
  1657. {
  1658. s: `SELECT children->first[2]->test FROM demo`,
  1659. stmt: &ast.SelectStatement{
  1660. Fields: []ast.Field{
  1661. {
  1662. Expr: &ast.BinaryExpr{
  1663. LHS: &ast.BinaryExpr{
  1664. LHS: &ast.BinaryExpr{
  1665. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  1666. OP: ast.ARROW,
  1667. RHS: &ast.JsonFieldRef{Name: "first"},
  1668. },
  1669. OP: ast.SUBSET,
  1670. RHS: &ast.IndexExpr{Index: &ast.IntegerLiteral{Val: 2}},
  1671. },
  1672. OP: ast.ARROW,
  1673. RHS: &ast.JsonFieldRef{Name: "test"},
  1674. },
  1675. Name: "",
  1676. AName: ""},
  1677. },
  1678. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  1679. },
  1680. },
  1681. {
  1682. s: `SELECT children[0:1] FROM demo`,
  1683. stmt: &ast.SelectStatement{
  1684. Fields: []ast.Field{
  1685. {
  1686. Expr: &ast.BinaryExpr{
  1687. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  1688. OP: ast.SUBSET,
  1689. RHS: &ast.ColonExpr{Start: &ast.IntegerLiteral{Val: 0}, End: &ast.IntegerLiteral{Val: 1}},
  1690. },
  1691. Name: "",
  1692. AName: ""},
  1693. },
  1694. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  1695. },
  1696. },
  1697. {
  1698. s: `SELECT children[: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[:] 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: math.MinInt32}},
  1722. },
  1723. Name: "",
  1724. AName: ""},
  1725. },
  1726. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  1727. },
  1728. },
  1729. {
  1730. s: `SELECT children[2:] AS c 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: 2}, End: &ast.IntegerLiteral{Val: math.MinInt32}},
  1738. },
  1739. Name: "",
  1740. AName: "c"},
  1741. },
  1742. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  1743. },
  1744. },
  1745. {
  1746. s: `SELECT children[2:]->first AS c FROM demo`,
  1747. stmt: &ast.SelectStatement{
  1748. Fields: []ast.Field{
  1749. {
  1750. Expr: &ast.BinaryExpr{
  1751. 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}}},
  1752. OP: ast.ARROW,
  1753. RHS: &ast.JsonFieldRef{Name: "first"},
  1754. },
  1755. Name: "",
  1756. AName: "c"},
  1757. },
  1758. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  1759. },
  1760. },
  1761. {
  1762. s: `SELECT demo.* FROM demo`,
  1763. stmt: &ast.SelectStatement{
  1764. Fields: []ast.Field{
  1765. {
  1766. Expr: &ast.FieldRef{StreamName: ast.StreamName("demo"), Name: "*"},
  1767. Name: "*",
  1768. AName: ""},
  1769. },
  1770. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  1771. },
  1772. },
  1773. {
  1774. s: `SELECT demo.children[2:]->first AS c FROM demo`,
  1775. stmt: &ast.SelectStatement{
  1776. Fields: []ast.Field{
  1777. {
  1778. Expr: &ast.BinaryExpr{
  1779. 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}}},
  1780. OP: ast.ARROW,
  1781. RHS: &ast.JsonFieldRef{Name: "first"},
  1782. },
  1783. Name: "",
  1784. AName: "c"},
  1785. },
  1786. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  1787. },
  1788. },
  1789. {
  1790. s: `SELECT lower(demo.children[2:]->first) AS c FROM demo`,
  1791. stmt: &ast.SelectStatement{
  1792. Fields: []ast.Field{
  1793. {
  1794. Expr: &ast.Call{
  1795. Name: "lower",
  1796. Args: []ast.Expr{
  1797. &ast.BinaryExpr{
  1798. 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}}},
  1799. OP: ast.ARROW,
  1800. RHS: &ast.JsonFieldRef{Name: "first"},
  1801. },
  1802. },
  1803. },
  1804. Name: "lower",
  1805. AName: "c"},
  1806. },
  1807. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  1808. },
  1809. },
  1810. {
  1811. s: `SELECT children[:1] FROM demo WHERE abc[0] > 12`,
  1812. stmt: &ast.SelectStatement{
  1813. Fields: []ast.Field{
  1814. {
  1815. Expr: &ast.BinaryExpr{
  1816. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  1817. OP: ast.SUBSET,
  1818. RHS: &ast.ColonExpr{Start: &ast.IntegerLiteral{Val: 0}, End: &ast.IntegerLiteral{Val: 1}},
  1819. },
  1820. Name: "",
  1821. AName: ""},
  1822. },
  1823. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  1824. Condition: &ast.BinaryExpr{
  1825. LHS: &ast.BinaryExpr{
  1826. LHS: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream},
  1827. OP: ast.SUBSET,
  1828. RHS: &ast.IndexExpr{Index: &ast.IntegerLiteral{Val: 0}},
  1829. },
  1830. OP: ast.GT,
  1831. RHS: &ast.IntegerLiteral{Val: 12},
  1832. },
  1833. },
  1834. },
  1835. {
  1836. s: `SELECT demo.children.first AS c FROM demo`,
  1837. stmt: nil,
  1838. err: "Too many field names. Please use -> to reference keys in struct.\n",
  1839. },
  1840. {
  1841. s: `SELECT children[index] FROM demo`,
  1842. stmt: &ast.SelectStatement{
  1843. Fields: []ast.Field{
  1844. {
  1845. Expr: &ast.BinaryExpr{
  1846. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  1847. OP: ast.SUBSET,
  1848. RHS: &ast.IndexExpr{Index: &ast.FieldRef{Name: "index", StreamName: ast.DefaultStream}},
  1849. },
  1850. Name: "",
  1851. AName: ""},
  1852. },
  1853. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  1854. },
  1855. },
  1856. }
  1857. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  1858. for i, tt := range tests {
  1859. stmt, err := NewParser(strings.NewReader(tt.s)).Parse()
  1860. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  1861. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.s, tt.err, err)
  1862. } else if tt.err == "" && !reflect.DeepEqual(tt.stmt, stmt) {
  1863. t.Errorf("%d. %q\n\nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.s, tt.stmt, stmt)
  1864. }
  1865. }
  1866. }
  1867. func TestParser_ParseJoins(t *testing.T) {
  1868. var tests = []struct {
  1869. s string
  1870. stmt *ast.SelectStatement
  1871. err string
  1872. }{
  1873. {
  1874. s: `SELECT * FROM topic/sensor1 LEFT JOIN topic1 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"}},
  1883. Joins: []ast.Join{
  1884. {
  1885. Name: "topic1", Alias: "", JoinType: ast.LEFT_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 INNER JOIN topic1 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", Alias: "t2", JoinType: ast.INNER_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 * 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.Wildcard{Token: ast.ASTERISK},
  1921. 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 f=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{Name: "f", StreamName: ast.DefaultStream},
  1950. OP: ast.EQ,
  1951. RHS: &ast.FieldRef{Name: "k", StreamName: ast.DefaultStream},
  1952. },
  1953. },
  1954. },
  1955. },
  1956. },
  1957. {
  1958. s: `SELECT t1.name FROM topic/sensor1 AS t1 LEFT 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.LEFT_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 RIGHT 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.RIGHT_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 FULL JOIN topic1/sensor2 AS t2 ON t1.f=t2.k`,
  2001. stmt: &ast.SelectStatement{
  2002. Fields: []ast.Field{
  2003. {
  2004. Expr: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "name"},
  2005. Name: "name",
  2006. AName: ""},
  2007. },
  2008. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
  2009. Joins: []ast.Join{
  2010. {
  2011. Name: "topic1/sensor2", Alias: "t2", JoinType: ast.FULL_JOIN, Expr: &ast.BinaryExpr{
  2012. LHS: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "f"},
  2013. OP: ast.EQ,
  2014. RHS: &ast.FieldRef{StreamName: ast.StreamName("t2"), Name: "k"},
  2015. },
  2016. },
  2017. },
  2018. },
  2019. },
  2020. {
  2021. s: `SELECT t1.name FROM topic/sensor1 AS t1 CROSS JOIN topic1/sensor2 AS t2 ON t1.f=t2.k`,
  2022. stmt: nil,
  2023. err: "On expression is not required for cross join type.\n",
  2024. },
  2025. {
  2026. s: `SELECT t1.name FROM topic/sensor1 AS t1 CROSS JOIN topic1/sensor2 AS t2`,
  2027. stmt: &ast.SelectStatement{
  2028. Fields: []ast.Field{
  2029. {
  2030. Expr: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "name"},
  2031. Name: "name",
  2032. AName: ""},
  2033. },
  2034. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
  2035. Joins: []ast.Join{
  2036. {
  2037. Name: "topic1/sensor2", Alias: "t2", JoinType: ast.CROSS_JOIN, Expr: nil,
  2038. },
  2039. },
  2040. },
  2041. },
  2042. {
  2043. s: `SELECT demo.*, demo2.* FROM demo LEFT JOIN demo2 on demo.f1 = demo2.f2`,
  2044. stmt: &ast.SelectStatement{
  2045. Fields: []ast.Field{
  2046. {
  2047. Expr: &ast.FieldRef{StreamName: ast.StreamName("demo"), Name: "*"},
  2048. Name: "*",
  2049. AName: ""},
  2050. {
  2051. Expr: &ast.FieldRef{StreamName: ast.StreamName("demo2"), Name: "*"},
  2052. Name: "*",
  2053. AName: ""},
  2054. },
  2055. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2056. Joins: []ast.Join{
  2057. {
  2058. Name: "demo2", Alias: "", JoinType: ast.LEFT_JOIN, Expr: &ast.BinaryExpr{
  2059. LHS: &ast.FieldRef{StreamName: ast.StreamName("demo"), Name: "f1"},
  2060. OP: ast.EQ,
  2061. RHS: &ast.FieldRef{StreamName: ast.StreamName("demo2"), Name: "f2"},
  2062. },
  2063. },
  2064. },
  2065. },
  2066. },
  2067. }
  2068. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  2069. for i, tt := range tests {
  2070. stmt, err := NewParser(strings.NewReader(tt.s)).Parse()
  2071. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  2072. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.s, tt.err, err)
  2073. } else if tt.err == "" && !reflect.DeepEqual(tt.stmt, stmt) {
  2074. t.Errorf("%d. %q\n\nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.s, tt.stmt, stmt)
  2075. }
  2076. }
  2077. }
  2078. func TestParser_ParseStatements(t *testing.T) {
  2079. var tests = []struct {
  2080. s string
  2081. stmts []ast.SelectStatement
  2082. err string
  2083. }{
  2084. {s: "SELECT name FROM tbl;\nSELECT name FROM topic/sensor1\n",
  2085. stmts: []ast.SelectStatement{
  2086. {
  2087. Fields: []ast.Field{
  2088. {
  2089. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  2090. Name: "name",
  2091. AName: ""},
  2092. },
  2093. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  2094. },
  2095. {
  2096. Fields: []ast.Field{
  2097. {
  2098. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  2099. Name: "name",
  2100. AName: ""},
  2101. },
  2102. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  2103. },
  2104. },
  2105. },
  2106. {s: "SELECT name FROM tbl;\nSELECT name FROM topic/sensor1;\n--SELECT comment FROM topic/comment",
  2107. stmts: []ast.SelectStatement{
  2108. {
  2109. Fields: []ast.Field{
  2110. {
  2111. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  2112. Name: "name",
  2113. AName: ""},
  2114. },
  2115. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  2116. },
  2117. {
  2118. Fields: []ast.Field{
  2119. {
  2120. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  2121. Name: "name",
  2122. AName: ""},
  2123. },
  2124. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  2125. },
  2126. },
  2127. },
  2128. }
  2129. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  2130. for i, tt := range tests {
  2131. stmts, err := NewParser(strings.NewReader(tt.s)).ParseQueries()
  2132. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  2133. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.s, tt.err, err)
  2134. } else if tt.err == "" && !reflect.DeepEqual(tt.stmts, stmts) {
  2135. t.Errorf("%d. %q\n\nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.s, tt.stmts, stmts)
  2136. }
  2137. }
  2138. }