parser_test.go 65 KB

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