parser_test.go 77 KB

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