parser_test.go 65 KB

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