parser_test.go 91 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332
  1. // Copyright 2021-2023 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. "math"
  18. "reflect"
  19. "regexp"
  20. "strings"
  21. "testing"
  22. "github.com/lf-edge/ekuiper/internal/testx"
  23. "github.com/lf-edge/ekuiper/pkg/ast"
  24. )
  25. // Ensure the parser can parse strings into Statement ASTs.
  26. func TestParser_ParseStatement(t *testing.T) {
  27. re1, _ := regexp.Compile("^foo$")
  28. re2, _ := regexp.Compile("^fo.o.*$")
  29. re3, _ := regexp.Compile("^foo\\\\%$")
  30. tests := []struct {
  31. s string
  32. stmt *ast.SelectStatement
  33. err string
  34. }{
  35. {
  36. s: "SELECT arr[x:4] FROM tbl",
  37. stmt: &ast.SelectStatement{
  38. Fields: []ast.Field{
  39. {
  40. Expr: &ast.BinaryExpr{
  41. OP: ast.SUBSET,
  42. LHS: &ast.FieldRef{
  43. Name: "arr",
  44. StreamName: ast.DefaultStream,
  45. },
  46. RHS: &ast.ColonExpr{
  47. Start: &ast.FieldRef{
  48. StreamName: ast.DefaultStream,
  49. Name: "x",
  50. },
  51. End: &ast.IntegerLiteral{
  52. Val: 4,
  53. },
  54. },
  55. },
  56. Name: "kuiper_field_0",
  57. AName: "",
  58. },
  59. },
  60. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  61. },
  62. },
  63. {
  64. s: "SELECT arr[1:x] FROM tbl",
  65. stmt: &ast.SelectStatement{
  66. Fields: []ast.Field{
  67. {
  68. Expr: &ast.BinaryExpr{
  69. OP: ast.SUBSET,
  70. LHS: &ast.FieldRef{
  71. Name: "arr",
  72. StreamName: ast.DefaultStream,
  73. },
  74. RHS: &ast.ColonExpr{
  75. Start: &ast.IntegerLiteral{
  76. Val: 1,
  77. },
  78. End: &ast.FieldRef{
  79. StreamName: ast.DefaultStream,
  80. Name: "x",
  81. },
  82. },
  83. },
  84. Name: "kuiper_field_0",
  85. AName: "",
  86. },
  87. },
  88. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  89. },
  90. },
  91. {
  92. s: "SELECT arr[x] FROM tbl",
  93. stmt: &ast.SelectStatement{
  94. Fields: []ast.Field{
  95. {
  96. Expr: &ast.BinaryExpr{
  97. OP: ast.SUBSET,
  98. LHS: &ast.FieldRef{
  99. Name: "arr",
  100. StreamName: ast.DefaultStream,
  101. },
  102. RHS: &ast.IndexExpr{
  103. Index: &ast.FieldRef{
  104. Name: "x",
  105. StreamName: ast.DefaultStream,
  106. },
  107. },
  108. },
  109. Name: "kuiper_field_0",
  110. AName: "",
  111. },
  112. },
  113. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  114. },
  115. },
  116. {
  117. s: "SELECT arr[x+1:y] FROM tbl",
  118. stmt: &ast.SelectStatement{
  119. Fields: []ast.Field{
  120. {
  121. Expr: &ast.BinaryExpr{
  122. OP: ast.SUBSET,
  123. LHS: &ast.FieldRef{
  124. Name: "arr",
  125. StreamName: ast.DefaultStream,
  126. },
  127. RHS: &ast.ColonExpr{
  128. Start: &ast.BinaryExpr{
  129. OP: ast.ADD,
  130. LHS: &ast.FieldRef{
  131. StreamName: ast.DefaultStream,
  132. Name: "x",
  133. },
  134. RHS: &ast.IntegerLiteral{
  135. Val: 1,
  136. },
  137. },
  138. End: &ast.FieldRef{
  139. StreamName: ast.DefaultStream,
  140. Name: "y",
  141. },
  142. },
  143. },
  144. Name: "kuiper_field_0",
  145. AName: "",
  146. },
  147. },
  148. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  149. },
  150. },
  151. {
  152. s: `SELECT name FROM tbl`,
  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. },
  161. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  162. },
  163. },
  164. {
  165. s: "SELECT `select` FROM tbl",
  166. stmt: &ast.SelectStatement{
  167. Fields: []ast.Field{
  168. {
  169. Expr: &ast.FieldRef{Name: "select", StreamName: ast.DefaultStream},
  170. Name: "select",
  171. AName: "",
  172. },
  173. },
  174. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  175. },
  176. },
  177. {
  178. s: `SELECT name FROM topic/sensor1`,
  179. stmt: &ast.SelectStatement{
  180. Fields: []ast.Field{
  181. {
  182. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  183. Name: "name",
  184. AName: "",
  185. },
  186. },
  187. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  188. },
  189. },
  190. {
  191. s: `SELECT t1.name FROM topic/sensor1 AS t1`,
  192. stmt: &ast.SelectStatement{
  193. Fields: []ast.Field{
  194. {
  195. Expr: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "name"},
  196. Name: "name",
  197. AName: "",
  198. },
  199. },
  200. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
  201. },
  202. },
  203. {
  204. s: "SELECT t1.name FROM topic/sensor1 AS `join`",
  205. stmt: &ast.SelectStatement{
  206. Fields: []ast.Field{
  207. {
  208. Expr: &ast.BinaryExpr{
  209. LHS: &ast.FieldRef{
  210. Name: "t1",
  211. StreamName: ast.DefaultStream,
  212. },
  213. OP: ast.ARROW,
  214. RHS: &ast.JsonFieldRef{
  215. Name: "name",
  216. },
  217. },
  218. Name: "name",
  219. AName: "",
  220. },
  221. },
  222. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "join"}},
  223. },
  224. },
  225. {
  226. s: `SELECT name FROM topic/sensor1 AS t1`,
  227. stmt: &ast.SelectStatement{
  228. Fields: []ast.Field{
  229. {
  230. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  231. Name: "name",
  232. AName: "",
  233. },
  234. },
  235. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
  236. },
  237. },
  238. {
  239. s: `SELECT name FROM topic/sensor1/#`,
  240. stmt: &ast.SelectStatement{
  241. Fields: []ast.Field{
  242. {
  243. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  244. Name: "name",
  245. AName: "",
  246. },
  247. },
  248. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1/#"}},
  249. },
  250. },
  251. {
  252. s: `SELECT name FROM topic/sensor1/# AS t2 `,
  253. stmt: &ast.SelectStatement{
  254. Fields: []ast.Field{
  255. {
  256. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  257. Name: "name",
  258. AName: "",
  259. },
  260. },
  261. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1/#", Alias: "t2"}},
  262. },
  263. },
  264. {
  265. s: `SELECT name FROM /topic/sensor1/#`,
  266. stmt: &ast.SelectStatement{
  267. Fields: []ast.Field{
  268. {
  269. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  270. Name: "name",
  271. AName: "",
  272. },
  273. },
  274. Sources: []ast.Source{&ast.Table{Name: "/topic/sensor1/#"}},
  275. },
  276. },
  277. {
  278. s: `SELECT name FROM /topic/sensor1/#/`,
  279. stmt: &ast.SelectStatement{
  280. Fields: []ast.Field{
  281. {
  282. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  283. Name: "name",
  284. AName: "",
  285. },
  286. },
  287. Sources: []ast.Source{&ast.Table{Name: "/topic/sensor1/#/"}},
  288. },
  289. },
  290. {
  291. s: `SELECT name FROM /topic/sensor1/+/temp1/`,
  292. stmt: &ast.SelectStatement{
  293. Fields: []ast.Field{
  294. {
  295. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  296. Name: "name",
  297. AName: "",
  298. },
  299. },
  300. Sources: []ast.Source{&ast.Table{Name: "/topic/sensor1/+/temp1/"}},
  301. },
  302. },
  303. {
  304. s: `SELECT name FROM topic/sensor1/+/temp`,
  305. stmt: &ast.SelectStatement{
  306. Fields: []ast.Field{
  307. {
  308. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  309. Name: "name",
  310. AName: "",
  311. },
  312. },
  313. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1/+/temp"}},
  314. },
  315. },
  316. {
  317. s: `SELECT * FROM tbl`,
  318. stmt: &ast.SelectStatement{
  319. Fields: []ast.Field{
  320. {
  321. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  322. Name: "*",
  323. AName: "",
  324. },
  325. },
  326. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  327. },
  328. },
  329. {
  330. s: `SELECT a,b FROM tbl`,
  331. stmt: &ast.SelectStatement{
  332. Fields: []ast.Field{
  333. {Expr: &ast.FieldRef{Name: "a", StreamName: ast.DefaultStream}, Name: "a", AName: ""},
  334. {Expr: &ast.FieldRef{Name: "b", StreamName: ast.DefaultStream}, Name: "b", AName: ""},
  335. },
  336. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  337. },
  338. },
  339. {
  340. s: `SELECT a, b,c FROM tbl`,
  341. stmt: &ast.SelectStatement{
  342. Fields: []ast.Field{
  343. {Expr: &ast.FieldRef{Name: "a", StreamName: ast.DefaultStream}, Name: "a", AName: ""},
  344. {Expr: &ast.FieldRef{Name: "b", StreamName: ast.DefaultStream}, Name: "b", AName: ""},
  345. {Expr: &ast.FieldRef{Name: "c", StreamName: ast.DefaultStream}, Name: "c", AName: ""},
  346. },
  347. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  348. },
  349. },
  350. {
  351. s: `SELECT a AS alias FROM tbl`,
  352. stmt: &ast.SelectStatement{
  353. Fields: []ast.Field{{Expr: &ast.FieldRef{Name: "a", StreamName: ast.DefaultStream}, Name: "a", AName: "alias"}},
  354. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  355. },
  356. },
  357. {
  358. s: `SELECT a AS alias1, b as Alias2 FROM tbl`,
  359. stmt: &ast.SelectStatement{
  360. Fields: []ast.Field{
  361. {Expr: &ast.FieldRef{Name: "a", StreamName: ast.DefaultStream}, Name: "a", AName: "alias1"},
  362. {Expr: &ast.FieldRef{Name: "b", StreamName: ast.DefaultStream}, Name: "b", AName: "Alias2"},
  363. },
  364. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  365. },
  366. },
  367. {
  368. s: `SELECT LenGth("test") FROM tbl`,
  369. stmt: &ast.SelectStatement{
  370. Fields: []ast.Field{
  371. {
  372. AName: "",
  373. Name: "length",
  374. Expr: &ast.Call{
  375. Name: "length",
  376. Args: []ast.Expr{&ast.StringLiteral{Val: "test"}},
  377. },
  378. },
  379. },
  380. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  381. },
  382. },
  383. {
  384. s: `SELECT length(test) FROM tbl`,
  385. stmt: &ast.SelectStatement{
  386. Fields: []ast.Field{
  387. {
  388. AName: "",
  389. Name: "length",
  390. Expr: &ast.Call{
  391. Name: "length",
  392. Args: []ast.Expr{&ast.FieldRef{Name: "test", StreamName: ast.DefaultStream}},
  393. },
  394. },
  395. },
  396. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  397. },
  398. },
  399. {
  400. s: `SELECT sin(123) FROM tbl`,
  401. stmt: &ast.SelectStatement{
  402. Fields: []ast.Field{
  403. {
  404. AName: "",
  405. Name: "sin",
  406. Expr: &ast.Call{
  407. Name: "sin",
  408. Args: []ast.Expr{&ast.IntegerLiteral{Val: 123}},
  409. },
  410. },
  411. },
  412. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  413. },
  414. },
  415. {
  416. s: `SELECT lpad("abc", 123) FROM tbl`,
  417. stmt: &ast.SelectStatement{
  418. Fields: []ast.Field{
  419. {
  420. AName: "",
  421. Name: "lpad",
  422. Expr: &ast.Call{
  423. Name: "lpad",
  424. Args: []ast.Expr{&ast.StringLiteral{Val: "abc"}, &ast.IntegerLiteral{Val: 123}},
  425. },
  426. },
  427. },
  428. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  429. },
  430. },
  431. {
  432. s: `SELECT newuuid() FROM tbl`,
  433. stmt: &ast.SelectStatement{
  434. Fields: []ast.Field{
  435. {
  436. AName: "",
  437. Name: "newuuid",
  438. Expr: &ast.Call{
  439. Name: "newuuid",
  440. Args: nil,
  441. },
  442. },
  443. },
  444. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  445. },
  446. },
  447. {
  448. s: `SELECT indexof("abc", field1) FROM tbl`,
  449. stmt: &ast.SelectStatement{
  450. Fields: []ast.Field{
  451. {
  452. AName: "",
  453. Name: "indexof",
  454. Expr: &ast.Call{
  455. Name: "indexof",
  456. Args: []ast.Expr{
  457. &ast.StringLiteral{Val: "abc"},
  458. &ast.FieldRef{Name: "field1", StreamName: ast.DefaultStream},
  459. },
  460. },
  461. },
  462. },
  463. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  464. },
  465. },
  466. {
  467. s: `SELECT lpad(lower(test),1) FROM tbl`,
  468. stmt: &ast.SelectStatement{
  469. Fields: []ast.Field{
  470. {
  471. AName: "",
  472. Name: "lpad",
  473. Expr: &ast.Call{
  474. Name: "lpad",
  475. FuncId: 1,
  476. Args: []ast.Expr{
  477. &ast.Call{
  478. Name: "lower",
  479. Args: []ast.Expr{
  480. &ast.FieldRef{Name: "test", StreamName: ast.DefaultStream},
  481. },
  482. },
  483. &ast.IntegerLiteral{Val: 1},
  484. },
  485. },
  486. },
  487. },
  488. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  489. },
  490. },
  491. {
  492. s: `SELECT lpad(lower(test),1) AS field1 FROM tbl`,
  493. stmt: &ast.SelectStatement{
  494. Fields: []ast.Field{
  495. {
  496. AName: "field1",
  497. Name: "lpad",
  498. Expr: &ast.Call{
  499. Name: "lpad",
  500. FuncId: 1,
  501. Args: []ast.Expr{
  502. &ast.Call{
  503. Name: "lower",
  504. Args: []ast.Expr{
  505. &ast.FieldRef{Name: "test", StreamName: ast.DefaultStream},
  506. },
  507. },
  508. &ast.IntegerLiteral{Val: 1},
  509. },
  510. },
  511. },
  512. },
  513. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  514. },
  515. },
  516. {
  517. s: `SELECT length(lower("test")) FROM tbl`,
  518. stmt: &ast.SelectStatement{
  519. Fields: []ast.Field{
  520. {
  521. AName: "",
  522. Name: "length",
  523. Expr: &ast.Call{
  524. Name: "length",
  525. FuncId: 1,
  526. Args: []ast.Expr{
  527. &ast.Call{
  528. Name: "lower",
  529. Args: []ast.Expr{
  530. &ast.StringLiteral{Val: "test"},
  531. },
  532. },
  533. },
  534. },
  535. },
  536. },
  537. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  538. },
  539. },
  540. {
  541. s: `SELECT count(*) FROM tbl`,
  542. stmt: &ast.SelectStatement{
  543. Fields: []ast.Field{
  544. {
  545. AName: "",
  546. Name: "count",
  547. Expr: &ast.Call{
  548. Name: "count",
  549. Args: []ast.Expr{&ast.Wildcard{Token: ast.ASTERISK}},
  550. FuncType: ast.FuncTypeAgg,
  551. },
  552. },
  553. },
  554. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  555. },
  556. },
  557. {
  558. s: `SELECT count(*, f1) FROM tbl`,
  559. stmt: nil,
  560. err: `Expect 1 arguments but found 2.`,
  561. },
  562. {
  563. s: `SELECT lag() FROM tbl`,
  564. stmt: nil,
  565. err: `expect one two or three args but got 0`,
  566. },
  567. {
  568. s: `SELECT lag(a, b, "default value") FROM tbl`,
  569. stmt: nil,
  570. err: `Expect int type for parameter 2`,
  571. },
  572. {
  573. s: `SELECT lag(a, 2, 20) FROM tbl`,
  574. stmt: &ast.SelectStatement{
  575. Fields: []ast.Field{
  576. {
  577. AName: "",
  578. Name: "lag",
  579. Expr: &ast.Call{
  580. Name: "lag",
  581. Args: []ast.Expr{&ast.FieldRef{Name: "a", StreamName: ast.DefaultStream}, &ast.IntegerLiteral{Val: 2}, &ast.IntegerLiteral{Val: 20}},
  582. },
  583. },
  584. },
  585. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  586. },
  587. },
  588. {
  589. s: `SELECT deduplicate(temperature, false) FROM tbl`,
  590. stmt: &ast.SelectStatement{
  591. Fields: []ast.Field{
  592. {
  593. AName: "",
  594. Name: "deduplicate",
  595. Expr: &ast.Call{
  596. Name: "deduplicate",
  597. Args: []ast.Expr{&ast.Wildcard{Token: ast.ASTERISK}, &ast.FieldRef{Name: "temperature", StreamName: ast.DefaultStream}, &ast.BooleanLiteral{Val: false}},
  598. FuncType: ast.FuncTypeAgg,
  599. },
  600. },
  601. },
  602. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  603. },
  604. },
  605. {
  606. s: `SELECT "abc" FROM tbl`,
  607. stmt: &ast.SelectStatement{
  608. Fields: []ast.Field{{AName: "", Name: "kuiper_field_0", Expr: &ast.StringLiteral{Val: "abc"}}},
  609. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  610. },
  611. },
  612. {
  613. s: `SELECT "abc" AS field1 FROM tbl`,
  614. stmt: &ast.SelectStatement{
  615. Fields: []ast.Field{{AName: "field1", Name: "", Expr: &ast.StringLiteral{Val: "abc"}}},
  616. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  617. },
  618. },
  619. {
  620. s: `SELECT field0, "abc" AS field1, field2 FROM tbl`,
  621. stmt: &ast.SelectStatement{
  622. Fields: []ast.Field{
  623. {AName: "", Name: "field0", Expr: &ast.FieldRef{Name: "field0", StreamName: ast.DefaultStream}},
  624. {AName: "field1", Name: "", Expr: &ast.StringLiteral{Val: "abc"}},
  625. {AName: "", Name: "field2", Expr: &ast.FieldRef{Name: "field2", StreamName: ast.DefaultStream}},
  626. },
  627. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  628. },
  629. },
  630. {
  631. s: `SELECT * AS alias FROM tbl`,
  632. stmt: nil,
  633. err: `alias is not supported for *`,
  634. },
  635. {
  636. s: `SELECT *, FROM tbl`,
  637. stmt: nil,
  638. err: `found "FROM", expected expression.`,
  639. },
  640. {
  641. s: `SELECTname FROM tbl`,
  642. stmt: nil,
  643. err: `Found "SELECTname", Expected SELECT.` + "\n",
  644. },
  645. {
  646. s: `SELECT abc FROM tbl WHERE abc > 12 `,
  647. stmt: &ast.SelectStatement{
  648. Fields: []ast.Field{{AName: "", Name: "abc", Expr: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream}}},
  649. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  650. Condition: &ast.BinaryExpr{
  651. LHS: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream},
  652. OP: ast.GT,
  653. RHS: &ast.IntegerLiteral{Val: 12},
  654. },
  655. },
  656. },
  657. {
  658. s: `SELECT abc FROM tbl WHERE abc = "hello" `,
  659. stmt: &ast.SelectStatement{
  660. Fields: []ast.Field{{AName: "", Name: "abc", Expr: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream}}},
  661. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  662. Condition: &ast.BinaryExpr{
  663. LHS: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream},
  664. OP: ast.EQ,
  665. RHS: &ast.StringLiteral{Val: "hello"},
  666. },
  667. },
  668. },
  669. {
  670. s: `SELECT t1.abc FROM tbl AS t1 WHERE t1.abc = "hello" `,
  671. stmt: &ast.SelectStatement{
  672. Fields: []ast.Field{{AName: "", Name: "abc", Expr: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "abc"}}},
  673. Sources: []ast.Source{&ast.Table{Name: "tbl", Alias: "t1"}},
  674. Condition: &ast.BinaryExpr{
  675. LHS: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "abc"},
  676. OP: ast.EQ,
  677. RHS: &ast.StringLiteral{Val: "hello"},
  678. },
  679. },
  680. },
  681. {
  682. s: `SELECT abc, "fff" AS fa FROM tbl WHERE fa >= 5 `,
  683. stmt: &ast.SelectStatement{
  684. Fields: []ast.Field{{AName: "", Name: "abc", Expr: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream}}, {AName: "fa", Name: "", Expr: &ast.StringLiteral{Val: "fff"}}},
  685. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  686. Condition: &ast.BinaryExpr{
  687. LHS: &ast.FieldRef{Name: "fa", StreamName: ast.DefaultStream},
  688. OP: ast.GTE,
  689. RHS: &ast.IntegerLiteral{Val: 5},
  690. },
  691. },
  692. },
  693. {
  694. s: `SELECT field2 FROM tbl WHERE field2 != 5 `,
  695. stmt: &ast.SelectStatement{
  696. Fields: []ast.Field{{AName: "", Name: "field2", Expr: &ast.FieldRef{Name: "field2", StreamName: ast.DefaultStream}}},
  697. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  698. Condition: &ast.BinaryExpr{
  699. LHS: &ast.FieldRef{Name: "field2", StreamName: ast.DefaultStream},
  700. OP: ast.NEQ,
  701. RHS: &ast.IntegerLiteral{Val: 5},
  702. },
  703. },
  704. },
  705. {
  706. s: `SELECT field2 FROM tbl WHERE field2 ! = 5 `, // Add space char in expression
  707. stmt: &ast.SelectStatement{
  708. Fields: []ast.Field{{AName: "", Name: "field2", Expr: &ast.FieldRef{Name: "field2", StreamName: ast.DefaultStream}}},
  709. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  710. Condition: &ast.BinaryExpr{
  711. LHS: &ast.FieldRef{Name: "field2", StreamName: ast.DefaultStream},
  712. OP: ast.NEQ,
  713. RHS: &ast.IntegerLiteral{Val: 5},
  714. },
  715. },
  716. },
  717. {
  718. s: `SELECT *f FROM tbl`,
  719. stmt: nil,
  720. err: `found "f", expected FROM.`,
  721. },
  722. ////TODO
  723. //{
  724. // s: `SELECT *from FROM tbl`,
  725. // stmt: nil,
  726. // err: `found "f", expected FROM.`,
  727. //},
  728. {
  729. s: `SELECT abc+2 FROM tbl`,
  730. stmt: &ast.SelectStatement{
  731. Fields: []ast.Field{
  732. {
  733. AName: "",
  734. Name: "kuiper_field_0",
  735. Expr: &ast.BinaryExpr{
  736. LHS: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream},
  737. OP: ast.ADD,
  738. RHS: &ast.IntegerLiteral{Val: 2},
  739. },
  740. },
  741. },
  742. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  743. },
  744. },
  745. {
  746. s: `SELECT t1.abc+2 FROM tbl AS t1`,
  747. stmt: &ast.SelectStatement{
  748. Fields: []ast.Field{
  749. {
  750. AName: "",
  751. Name: "kuiper_field_0",
  752. Expr: &ast.BinaryExpr{
  753. LHS: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "abc"},
  754. OP: ast.ADD,
  755. RHS: &ast.IntegerLiteral{Val: 2},
  756. },
  757. },
  758. },
  759. Sources: []ast.Source{&ast.Table{Name: "tbl", Alias: "t1"}},
  760. },
  761. },
  762. {
  763. s: `SELECT abc + "hello" FROM tbl`,
  764. stmt: &ast.SelectStatement{
  765. Fields: []ast.Field{
  766. {
  767. AName: "",
  768. Name: "kuiper_field_0",
  769. Expr: &ast.BinaryExpr{
  770. LHS: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream},
  771. OP: ast.ADD,
  772. RHS: &ast.StringLiteral{Val: "hello"},
  773. },
  774. },
  775. },
  776. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  777. },
  778. },
  779. {
  780. s: `SELECT abc*2 + 3 FROM tbl`,
  781. stmt: &ast.SelectStatement{
  782. Fields: []ast.Field{
  783. {
  784. AName: "",
  785. Name: "kuiper_field_0",
  786. Expr: &ast.BinaryExpr{
  787. LHS: &ast.BinaryExpr{
  788. LHS: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream},
  789. OP: ast.MUL,
  790. RHS: &ast.IntegerLiteral{Val: 2},
  791. },
  792. OP: ast.ADD,
  793. RHS: &ast.IntegerLiteral{Val: 3},
  794. },
  795. },
  796. },
  797. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  798. },
  799. },
  800. {
  801. s: `SELECT ln(abc*2 + 3) FROM tbl`,
  802. stmt: &ast.SelectStatement{
  803. Fields: []ast.Field{
  804. {
  805. AName: "",
  806. Name: "ln",
  807. Expr: &ast.Call{
  808. Name: "ln",
  809. Args: []ast.Expr{
  810. &ast.BinaryExpr{
  811. LHS: &ast.BinaryExpr{
  812. LHS: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream},
  813. OP: ast.MUL,
  814. RHS: &ast.IntegerLiteral{Val: 2},
  815. },
  816. OP: ast.ADD,
  817. RHS: &ast.IntegerLiteral{Val: 3},
  818. },
  819. },
  820. },
  821. },
  822. },
  823. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  824. },
  825. },
  826. {
  827. s: `SELECT ln(t1.abc*2 + 3) FROM tbl AS t1`,
  828. stmt: &ast.SelectStatement{
  829. Fields: []ast.Field{
  830. {
  831. AName: "",
  832. Name: "ln",
  833. Expr: &ast.Call{
  834. Name: "ln",
  835. Args: []ast.Expr{
  836. &ast.BinaryExpr{
  837. LHS: &ast.BinaryExpr{
  838. LHS: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "abc"},
  839. OP: ast.MUL,
  840. RHS: &ast.IntegerLiteral{Val: 2},
  841. },
  842. OP: ast.ADD,
  843. RHS: &ast.IntegerLiteral{Val: 3},
  844. },
  845. },
  846. },
  847. },
  848. },
  849. Sources: []ast.Source{&ast.Table{Name: "tbl", Alias: "t1"}},
  850. },
  851. },
  852. {
  853. s: `SELECT lpad("param2", abc*2 + 3) FROM tbl`,
  854. stmt: &ast.SelectStatement{
  855. Fields: []ast.Field{
  856. {
  857. AName: "",
  858. Name: "lpad",
  859. Expr: &ast.Call{
  860. Name: "lpad",
  861. Args: []ast.Expr{
  862. &ast.StringLiteral{Val: "param2"},
  863. &ast.BinaryExpr{
  864. LHS: &ast.BinaryExpr{
  865. LHS: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream},
  866. OP: ast.MUL,
  867. RHS: &ast.IntegerLiteral{Val: 2},
  868. },
  869. OP: ast.ADD,
  870. RHS: &ast.IntegerLiteral{Val: 3},
  871. },
  872. },
  873. },
  874. },
  875. },
  876. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  877. },
  878. },
  879. {
  880. s: `SELECT 0.2 FROM tbl`,
  881. stmt: &ast.SelectStatement{
  882. Fields: []ast.Field{
  883. {
  884. AName: "",
  885. Name: "kuiper_field_0",
  886. Expr: &ast.NumberLiteral{Val: 0.2},
  887. },
  888. },
  889. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  890. },
  891. },
  892. {
  893. s: `SELECT .2 FROM tbl`,
  894. stmt: &ast.SelectStatement{
  895. Fields: []ast.Field{
  896. {
  897. AName: "",
  898. Name: "kuiper_field_0",
  899. Expr: &ast.NumberLiteral{Val: 0.2},
  900. },
  901. },
  902. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  903. },
  904. },
  905. {
  906. s: `SELECT sin(.2) FROM tbl`,
  907. stmt: &ast.SelectStatement{
  908. Fields: []ast.Field{
  909. {
  910. AName: "",
  911. Name: "sin",
  912. Expr: &ast.Call{
  913. Name: "sin",
  914. Args: []ast.Expr{&ast.NumberLiteral{Val: 0.2}},
  915. },
  916. },
  917. },
  918. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  919. },
  920. },
  921. {
  922. s: `SELECT power(.2, 4) FROM tbl`,
  923. stmt: &ast.SelectStatement{
  924. Fields: []ast.Field{
  925. {
  926. AName: "",
  927. Name: "power",
  928. Expr: &ast.Call{
  929. Name: "power",
  930. Args: []ast.Expr{&ast.NumberLiteral{Val: 0.2}, &ast.IntegerLiteral{Val: 4}},
  931. },
  932. },
  933. },
  934. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  935. },
  936. },
  937. {
  938. s: `SELECT power(.2, 4) AS f1 FROM tbl WHERE f1 > 2.2`,
  939. stmt: &ast.SelectStatement{
  940. Fields: []ast.Field{
  941. {
  942. AName: "f1",
  943. Name: "power",
  944. Expr: &ast.Call{
  945. Name: "power",
  946. Args: []ast.Expr{&ast.NumberLiteral{Val: 0.2}, &ast.IntegerLiteral{Val: 4}},
  947. },
  948. },
  949. },
  950. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  951. Condition: &ast.BinaryExpr{
  952. LHS: &ast.FieldRef{Name: "f1", StreamName: ast.DefaultStream},
  953. OP: ast.GT,
  954. RHS: &ast.NumberLiteral{Val: 2.2},
  955. },
  956. },
  957. },
  958. {
  959. s: `SELECT power(.2, 4) AS f1 FROM tbl WHERE f1 BETWEEN 1 AND 2`,
  960. stmt: &ast.SelectStatement{
  961. Fields: []ast.Field{
  962. {
  963. AName: "f1",
  964. Name: "power",
  965. Expr: &ast.Call{
  966. Name: "power",
  967. Args: []ast.Expr{&ast.NumberLiteral{Val: 0.2}, &ast.IntegerLiteral{Val: 4}},
  968. },
  969. },
  970. },
  971. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  972. Condition: &ast.BinaryExpr{
  973. LHS: &ast.FieldRef{Name: "f1", StreamName: ast.DefaultStream},
  974. OP: ast.BETWEEN,
  975. RHS: &ast.BetweenExpr{
  976. Lower: &ast.IntegerLiteral{Val: 1},
  977. Higher: &ast.IntegerLiteral{Val: 2},
  978. },
  979. },
  980. },
  981. },
  982. {
  983. s: `SELECT a FROM tbl WHERE f1 > 4 AND f2 BETWEEN 1 AND 2`,
  984. stmt: &ast.SelectStatement{
  985. Fields: []ast.Field{
  986. {
  987. AName: "",
  988. Name: "a",
  989. Expr: &ast.FieldRef{Name: "a", StreamName: ast.DefaultStream},
  990. },
  991. },
  992. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  993. Condition: &ast.BinaryExpr{
  994. OP: ast.AND,
  995. LHS: &ast.BinaryExpr{
  996. LHS: &ast.FieldRef{Name: "f1", StreamName: ast.DefaultStream},
  997. OP: ast.GT,
  998. RHS: &ast.IntegerLiteral{Val: 4},
  999. },
  1000. RHS: &ast.BinaryExpr{
  1001. LHS: &ast.FieldRef{Name: "f2", StreamName: ast.DefaultStream},
  1002. OP: ast.BETWEEN,
  1003. RHS: &ast.BetweenExpr{
  1004. Lower: &ast.IntegerLiteral{Val: 1},
  1005. Higher: &ast.IntegerLiteral{Val: 2},
  1006. },
  1007. },
  1008. },
  1009. },
  1010. },
  1011. {
  1012. s: `SELECT a FROM tbl WHERE f1 NOT BETWEEN b AND c AND f2 BETWEEN 1 AND 2 AND f3 > 4`,
  1013. stmt: &ast.SelectStatement{
  1014. Fields: []ast.Field{
  1015. {
  1016. AName: "",
  1017. Name: "a",
  1018. Expr: &ast.FieldRef{Name: "a", StreamName: ast.DefaultStream},
  1019. },
  1020. },
  1021. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1022. Condition: &ast.BinaryExpr{
  1023. OP: ast.AND,
  1024. LHS: &ast.BinaryExpr{
  1025. OP: ast.AND,
  1026. LHS: &ast.BinaryExpr{
  1027. LHS: &ast.FieldRef{Name: "f1", StreamName: ast.DefaultStream},
  1028. OP: ast.NOTBETWEEN,
  1029. RHS: &ast.BetweenExpr{
  1030. Lower: &ast.FieldRef{Name: "b", StreamName: ast.DefaultStream},
  1031. Higher: &ast.FieldRef{Name: "c", StreamName: ast.DefaultStream},
  1032. },
  1033. },
  1034. RHS: &ast.BinaryExpr{
  1035. LHS: &ast.FieldRef{Name: "f2", StreamName: ast.DefaultStream},
  1036. OP: ast.BETWEEN,
  1037. RHS: &ast.BetweenExpr{
  1038. Lower: &ast.IntegerLiteral{Val: 1},
  1039. Higher: &ast.IntegerLiteral{Val: 2},
  1040. },
  1041. },
  1042. },
  1043. RHS: &ast.BinaryExpr{
  1044. OP: ast.GT,
  1045. LHS: &ast.FieldRef{Name: "f3", StreamName: ast.DefaultStream},
  1046. RHS: &ast.IntegerLiteral{Val: 4},
  1047. },
  1048. },
  1049. },
  1050. },
  1051. {
  1052. s: `SELECT a FROM tbl WHERE f1 NOT BETWEEN b`,
  1053. err: "expect AND expression after between but found EOF",
  1054. },
  1055. {
  1056. s: `SELECT a FROM tbl WHERE f1 NOT BETWEEN 1 OR 2`,
  1057. err: "expect AND expression after between but found OR",
  1058. },
  1059. {
  1060. s: `SELECT a FROM tbl WHERE a LIKE "foo"`,
  1061. stmt: &ast.SelectStatement{
  1062. Fields: []ast.Field{
  1063. {
  1064. AName: "",
  1065. Name: "a",
  1066. Expr: &ast.FieldRef{Name: "a", StreamName: ast.DefaultStream},
  1067. },
  1068. },
  1069. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1070. Condition: &ast.BinaryExpr{
  1071. LHS: &ast.FieldRef{Name: "a", StreamName: ast.DefaultStream},
  1072. OP: ast.LIKE,
  1073. RHS: &ast.LikePattern{Expr: &ast.StringLiteral{Val: "foo"}, Pattern: re1},
  1074. },
  1075. },
  1076. },
  1077. {
  1078. s: `SELECT a FROM tbl WHERE a NOT LIKE "fo_o%"`,
  1079. stmt: &ast.SelectStatement{
  1080. Fields: []ast.Field{
  1081. {
  1082. AName: "",
  1083. Name: "a",
  1084. Expr: &ast.FieldRef{Name: "a", StreamName: ast.DefaultStream},
  1085. },
  1086. },
  1087. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1088. Condition: &ast.BinaryExpr{
  1089. LHS: &ast.FieldRef{Name: "a", StreamName: ast.DefaultStream},
  1090. OP: ast.NOTLIKE,
  1091. RHS: &ast.LikePattern{Expr: &ast.StringLiteral{Val: "fo_o%"}, Pattern: re2},
  1092. },
  1093. },
  1094. },
  1095. {
  1096. s: `SELECT a FROM tbl WHERE a LIKE "foo\\%"`,
  1097. stmt: &ast.SelectStatement{
  1098. Fields: []ast.Field{
  1099. {
  1100. AName: "",
  1101. Name: "a",
  1102. Expr: &ast.FieldRef{Name: "a", StreamName: ast.DefaultStream},
  1103. },
  1104. },
  1105. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1106. Condition: &ast.BinaryExpr{
  1107. LHS: &ast.FieldRef{Name: "a", StreamName: ast.DefaultStream},
  1108. OP: ast.LIKE,
  1109. RHS: &ast.LikePattern{Expr: &ast.StringLiteral{Val: "foo\\%"}, Pattern: re3},
  1110. },
  1111. },
  1112. },
  1113. {
  1114. s: `SELECT deviceId, name FROM topic/sensor1 WHERE deviceId=1 AND name = "dname"`,
  1115. stmt: &ast.SelectStatement{
  1116. Fields: []ast.Field{
  1117. {Expr: &ast.FieldRef{Name: "deviceId", StreamName: ast.DefaultStream}, Name: "deviceId", AName: ""},
  1118. {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
  1119. },
  1120. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  1121. Condition: &ast.BinaryExpr{
  1122. LHS: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "deviceId", StreamName: ast.DefaultStream}, OP: ast.EQ, RHS: &ast.IntegerLiteral{Val: 1}},
  1123. OP: ast.AND,
  1124. RHS: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, OP: ast.EQ, RHS: &ast.StringLiteral{Val: "dname"}},
  1125. },
  1126. },
  1127. },
  1128. {
  1129. s: `SELECT deviceId, name FROM topic/sensor1 AS t1 WHERE t1.deviceId=1 AND t1.name = "dname"`,
  1130. stmt: &ast.SelectStatement{
  1131. Fields: []ast.Field{
  1132. {Expr: &ast.FieldRef{Name: "deviceId", StreamName: ast.DefaultStream}, Name: "deviceId", AName: ""},
  1133. {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
  1134. },
  1135. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
  1136. Condition: &ast.BinaryExpr{
  1137. LHS: &ast.BinaryExpr{LHS: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "deviceId"}, OP: ast.EQ, RHS: &ast.IntegerLiteral{Val: 1}},
  1138. OP: ast.AND,
  1139. RHS: &ast.BinaryExpr{LHS: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "name"}, OP: ast.EQ, RHS: &ast.StringLiteral{Val: "dname"}},
  1140. },
  1141. },
  1142. },
  1143. {
  1144. s: `SELECT temp AS t, name FROM topic/sensor1 WHERE t> = 20.5 OR name = "dname"`,
  1145. stmt: &ast.SelectStatement{
  1146. Fields: []ast.Field{
  1147. {Expr: &ast.FieldRef{Name: "temp", StreamName: ast.DefaultStream}, Name: "temp", AName: "t"},
  1148. {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
  1149. },
  1150. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  1151. Condition: &ast.BinaryExpr{
  1152. LHS: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "t", StreamName: ast.DefaultStream}, OP: ast.GTE, RHS: &ast.NumberLiteral{Val: 20.5}},
  1153. OP: ast.OR,
  1154. RHS: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, OP: ast.EQ, RHS: &ast.StringLiteral{Val: "dname"}},
  1155. },
  1156. },
  1157. },
  1158. {
  1159. s: `SELECT temp AS t, name FROM topic/sensor1 WHERE t IN arraySet OR name IN arraySet`,
  1160. stmt: &ast.SelectStatement{
  1161. Fields: []ast.Field{
  1162. {Expr: &ast.FieldRef{Name: "temp", StreamName: ast.DefaultStream}, Name: "temp", AName: "t"},
  1163. {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
  1164. },
  1165. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  1166. Condition: &ast.BinaryExpr{
  1167. LHS: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "t", StreamName: ast.DefaultStream}, OP: ast.IN, RHS: &ast.FieldRef{Name: "arraySet", StreamName: ast.DefaultStream}},
  1168. OP: ast.OR,
  1169. RHS: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, OP: ast.IN, RHS: &ast.FieldRef{Name: "arraySet", StreamName: ast.DefaultStream}},
  1170. },
  1171. },
  1172. },
  1173. {
  1174. s: `SELECT temp AS t, name FROM topic/sensor1 WHERE t NOT IN arraySet OR name NOT IN arraySet`,
  1175. stmt: &ast.SelectStatement{
  1176. Fields: []ast.Field{
  1177. {Expr: &ast.FieldRef{Name: "temp", StreamName: ast.DefaultStream}, Name: "temp", AName: "t"},
  1178. {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
  1179. },
  1180. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  1181. Condition: &ast.BinaryExpr{
  1182. LHS: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "t", StreamName: ast.DefaultStream}, OP: ast.NOTIN, RHS: &ast.FieldRef{Name: "arraySet", StreamName: ast.DefaultStream}},
  1183. OP: ast.OR,
  1184. RHS: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, OP: ast.NOTIN, RHS: &ast.FieldRef{Name: "arraySet", StreamName: ast.DefaultStream}},
  1185. },
  1186. },
  1187. },
  1188. {
  1189. s: `SELECT temp AS t, name FROM topic/sensor1 WHERE t IN (20.5, 20.4) OR name IN ("dname", "ename")`,
  1190. stmt: &ast.SelectStatement{
  1191. Fields: []ast.Field{
  1192. {Expr: &ast.FieldRef{Name: "temp", StreamName: ast.DefaultStream}, Name: "temp", AName: "t"},
  1193. {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
  1194. },
  1195. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  1196. Condition: &ast.BinaryExpr{
  1197. LHS: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "t", StreamName: ast.DefaultStream}, OP: ast.IN, RHS: &ast.ValueSetExpr{LiteralExprs: []ast.Expr{&ast.NumberLiteral{Val: 20.5}, &ast.NumberLiteral{Val: 20.4}}}},
  1198. OP: ast.OR,
  1199. RHS: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, OP: ast.IN, RHS: &ast.ValueSetExpr{LiteralExprs: []ast.Expr{&ast.StringLiteral{Val: "dname"}, &ast.StringLiteral{Val: "ename"}}}},
  1200. },
  1201. },
  1202. },
  1203. {
  1204. s: `SELECT temp AS t, name FROM topic/sensor1 WHERE t NOT IN (20.5, 20.4) OR name IN ("dname", "ename")`,
  1205. stmt: &ast.SelectStatement{
  1206. Fields: []ast.Field{
  1207. {Expr: &ast.FieldRef{Name: "temp", StreamName: ast.DefaultStream}, Name: "temp", AName: "t"},
  1208. {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
  1209. },
  1210. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  1211. Condition: &ast.BinaryExpr{
  1212. LHS: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "t", StreamName: ast.DefaultStream}, OP: ast.NOTIN, RHS: &ast.ValueSetExpr{LiteralExprs: []ast.Expr{&ast.NumberLiteral{Val: 20.5}, &ast.NumberLiteral{Val: 20.4}}}},
  1213. OP: ast.OR,
  1214. RHS: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, OP: ast.IN, RHS: &ast.ValueSetExpr{LiteralExprs: []ast.Expr{&ast.StringLiteral{Val: "dname"}, &ast.StringLiteral{Val: "ename"}}}},
  1215. },
  1216. },
  1217. },
  1218. {
  1219. s: `SELECT temp AS t, name FROM topic/sensor1 WHERE name = "dname" GROUP BY name`,
  1220. stmt: &ast.SelectStatement{
  1221. Fields: []ast.Field{
  1222. {Expr: &ast.FieldRef{Name: "temp", StreamName: ast.DefaultStream}, Name: "temp", AName: "t"},
  1223. {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
  1224. },
  1225. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  1226. Condition: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, OP: ast.EQ, RHS: &ast.StringLiteral{Val: "dname"}},
  1227. Dimensions: ast.Dimensions{ast.Dimension{Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}}},
  1228. },
  1229. },
  1230. {
  1231. s: `SELECT temp AS t, name FROM topic/sensor1 WHERE name = "dname" GROUP BY name HAVING count(name) > 3`,
  1232. stmt: &ast.SelectStatement{
  1233. Fields: []ast.Field{
  1234. {Expr: &ast.FieldRef{Name: "temp", StreamName: ast.DefaultStream}, Name: "temp", AName: "t"},
  1235. {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
  1236. },
  1237. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  1238. Condition: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, OP: ast.EQ, RHS: &ast.StringLiteral{Val: "dname"}},
  1239. Dimensions: ast.Dimensions{ast.Dimension{Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}}},
  1240. Having: &ast.BinaryExpr{LHS: &ast.Call{Name: "count", Args: []ast.Expr{&ast.FieldRef{StreamName: ast.DefaultStream, Name: "name"}}, FuncType: ast.FuncTypeAgg}, OP: ast.GT, RHS: &ast.IntegerLiteral{Val: 3}},
  1241. },
  1242. },
  1243. {
  1244. s: `SELECT temp AS t, name FROM topic/sensor1 WHERE name = "dname" HAVING count(name) > 3`,
  1245. stmt: &ast.SelectStatement{
  1246. Fields: []ast.Field{
  1247. {Expr: &ast.FieldRef{Name: "temp", StreamName: ast.DefaultStream}, Name: "temp", AName: "t"},
  1248. {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
  1249. },
  1250. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  1251. Condition: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, OP: ast.EQ, RHS: &ast.StringLiteral{Val: "dname"}},
  1252. Having: &ast.BinaryExpr{LHS: &ast.Call{Name: "count", Args: []ast.Expr{&ast.FieldRef{StreamName: ast.DefaultStream, Name: "name"}}, FuncType: ast.FuncTypeAgg}, OP: ast.GT, RHS: &ast.IntegerLiteral{Val: 3}},
  1253. },
  1254. },
  1255. {
  1256. s: `SELECT id,AVG(data) FROM t GROUP BY SUM(data)>10`,
  1257. stmt: nil,
  1258. err: "Not allowed to call aggregate functions in GROUP BY clause.",
  1259. },
  1260. {
  1261. s: `SELECT temp AS t, name FROM topic/sensor1 WHERE count(name) = 3`,
  1262. stmt: nil,
  1263. err: "Not allowed to call aggregate functions in WHERE clause.",
  1264. },
  1265. {
  1266. s: `SELECT s1.temp AS t, name FROM topic/sensor1 AS s1 WHERE t = "dname" GROUP BY s1.temp`,
  1267. stmt: &ast.SelectStatement{
  1268. Fields: []ast.Field{
  1269. {Expr: &ast.FieldRef{StreamName: "s1", Name: "temp"}, Name: "temp", AName: "t"},
  1270. {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
  1271. },
  1272. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "s1"}},
  1273. Condition: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "t", StreamName: ast.DefaultStream}, OP: ast.EQ, RHS: &ast.StringLiteral{Val: "dname"}},
  1274. Dimensions: ast.Dimensions{ast.Dimension{Expr: &ast.FieldRef{StreamName: "s1", Name: "temp"}}},
  1275. },
  1276. },
  1277. {
  1278. s: `SELECT temp AS t, name FROM topic/sensor1 WHERE name = "dname" GROUP BY lpad(name,1)`,
  1279. stmt: &ast.SelectStatement{
  1280. Fields: []ast.Field{
  1281. {Expr: &ast.FieldRef{Name: "temp", StreamName: ast.DefaultStream}, Name: "temp", AName: "t"},
  1282. {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
  1283. },
  1284. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  1285. Condition: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, OP: ast.EQ, RHS: &ast.StringLiteral{Val: "dname"}},
  1286. Dimensions: ast.Dimensions{
  1287. ast.Dimension{
  1288. Expr: &ast.Call{Name: "lpad", Args: []ast.Expr{&ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, &ast.IntegerLiteral{Val: 1}}},
  1289. },
  1290. },
  1291. },
  1292. },
  1293. {
  1294. s: `SELECT temp AS t, name FROM topic/sensor1 AS s1 WHERE name = "dname" GROUP BY lpad(s1.name,1)`,
  1295. stmt: &ast.SelectStatement{
  1296. Fields: []ast.Field{
  1297. {Expr: &ast.FieldRef{Name: "temp", StreamName: ast.DefaultStream}, Name: "temp", AName: "t"},
  1298. {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
  1299. },
  1300. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "s1"}},
  1301. Condition: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, OP: ast.EQ, RHS: &ast.StringLiteral{Val: "dname"}},
  1302. Dimensions: ast.Dimensions{
  1303. ast.Dimension{
  1304. Expr: &ast.Call{Name: "lpad", Args: []ast.Expr{&ast.FieldRef{StreamName: ast.StreamName("s1"), Name: "name"}, &ast.IntegerLiteral{Val: 1}}},
  1305. },
  1306. },
  1307. },
  1308. },
  1309. {
  1310. s: `SELECT temp AS t, name FROM topic/sensor1 WHERE name = "dname" GROUP BY lpad(name,1) ORDER BY name`,
  1311. stmt: &ast.SelectStatement{
  1312. Fields: []ast.Field{
  1313. {Expr: &ast.FieldRef{Name: "temp", StreamName: ast.DefaultStream}, Name: "temp", AName: "t"},
  1314. {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
  1315. },
  1316. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  1317. Condition: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, OP: ast.EQ, RHS: &ast.StringLiteral{Val: "dname"}},
  1318. Dimensions: ast.Dimensions{
  1319. ast.Dimension{
  1320. Expr: &ast.Call{
  1321. Name: "lpad", Args: []ast.Expr{
  1322. &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  1323. &ast.IntegerLiteral{Val: 1},
  1324. },
  1325. },
  1326. },
  1327. },
  1328. SortFields: []ast.SortField{{Uname: "name", Name: "name", Ascending: true, FieldExpr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}}},
  1329. },
  1330. },
  1331. {
  1332. 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`,
  1333. stmt: &ast.SelectStatement{
  1334. Fields: []ast.Field{
  1335. {Expr: &ast.FieldRef{Name: "temp", StreamName: ast.DefaultStream}, Name: "temp", AName: "t"},
  1336. {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
  1337. },
  1338. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "s1"}},
  1339. Condition: &ast.BinaryExpr{LHS: &ast.FieldRef{StreamName: ast.StreamName("s1"), Name: "name"}, OP: ast.EQ, RHS: &ast.StringLiteral{Val: "dname"}},
  1340. Dimensions: ast.Dimensions{
  1341. ast.Dimension{
  1342. Expr: &ast.Call{
  1343. Name: "lpad", Args: []ast.Expr{
  1344. &ast.FieldRef{StreamName: ast.StreamName("s1"), Name: "name"},
  1345. &ast.IntegerLiteral{Val: 1},
  1346. },
  1347. },
  1348. },
  1349. },
  1350. SortFields: []ast.SortField{{Uname: "s1\007name", Name: "name", StreamName: ast.StreamName("s1"), Ascending: true, FieldExpr: &ast.FieldRef{Name: "name", StreamName: "s1"}}},
  1351. },
  1352. },
  1353. {
  1354. s: `SELECT temp AS t, name FROM topic/sensor1 WHERE name = "dname" GROUP BY lpad(name,1) ORDER BY name DESC`,
  1355. stmt: &ast.SelectStatement{
  1356. Fields: []ast.Field{
  1357. {Expr: &ast.FieldRef{Name: "temp", StreamName: ast.DefaultStream}, Name: "temp", AName: "t"},
  1358. {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
  1359. },
  1360. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  1361. Condition: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, OP: ast.EQ, RHS: &ast.StringLiteral{Val: "dname"}},
  1362. Dimensions: ast.Dimensions{
  1363. ast.Dimension{
  1364. Expr: &ast.Call{
  1365. Name: "lpad", Args: []ast.Expr{
  1366. &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  1367. &ast.IntegerLiteral{Val: 1},
  1368. },
  1369. },
  1370. },
  1371. },
  1372. SortFields: []ast.SortField{{Uname: "name", Name: "name", Ascending: false, FieldExpr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}}},
  1373. },
  1374. },
  1375. {
  1376. s: `SELECT * FROM topic/sensor1 ORDER BY name DESC`,
  1377. stmt: &ast.SelectStatement{
  1378. Fields: []ast.Field{
  1379. {
  1380. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  1381. Name: "*",
  1382. AName: "",
  1383. },
  1384. },
  1385. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  1386. SortFields: []ast.SortField{{Uname: "name", Name: "name", Ascending: false, FieldExpr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}}},
  1387. },
  1388. },
  1389. {
  1390. s: `SELECT * FROM topic/sensor1 ORDER BY name DESC, name2 ASC`,
  1391. stmt: &ast.SelectStatement{
  1392. Fields: []ast.Field{
  1393. {
  1394. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  1395. Name: "*",
  1396. AName: "",
  1397. },
  1398. },
  1399. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  1400. 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}}},
  1401. },
  1402. },
  1403. {
  1404. s: `SELECT * FROM topic/sensor1 GROUP BY name, name2,power(name3,1.8) ORDER BY name DESC, name2 ASC`,
  1405. stmt: &ast.SelectStatement{
  1406. Fields: []ast.Field{
  1407. {
  1408. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  1409. Name: "*",
  1410. AName: "",
  1411. },
  1412. },
  1413. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  1414. Dimensions: ast.Dimensions{
  1415. ast.Dimension{Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}},
  1416. ast.Dimension{Expr: &ast.FieldRef{Name: "name2", StreamName: ast.DefaultStream}},
  1417. ast.Dimension{
  1418. Expr: &ast.Call{
  1419. Name: "power", Args: []ast.Expr{
  1420. &ast.FieldRef{Name: "name3", StreamName: ast.DefaultStream},
  1421. &ast.NumberLiteral{Val: 1.8},
  1422. },
  1423. },
  1424. },
  1425. },
  1426. 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}}},
  1427. },
  1428. },
  1429. //{
  1430. // s: `SELECT .2sd FROM tbl`,
  1431. // stmt: &SelectStatement{
  1432. // Fields: []Field{
  1433. // Field{
  1434. // AName:"",
  1435. // Expr: &NumberLiteral{Val: 0.2},
  1436. // },
  1437. // },
  1438. // TableName: "tbl",
  1439. // },
  1440. //},
  1441. {
  1442. s: `SELECT name FROM tbl/*SELECT comment FROM testComments*/`,
  1443. stmt: &ast.SelectStatement{
  1444. Fields: []ast.Field{
  1445. {
  1446. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  1447. Name: "name",
  1448. AName: "",
  1449. },
  1450. },
  1451. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1452. },
  1453. },
  1454. {
  1455. s: `/*SELECT comment FROM testComments*/SELECT name FROM tbl`,
  1456. stmt: &ast.SelectStatement{
  1457. Fields: []ast.Field{
  1458. {
  1459. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  1460. Name: "name",
  1461. AName: "",
  1462. },
  1463. },
  1464. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1465. },
  1466. },
  1467. {
  1468. s: `SELECT name /*SELECT comment FROM testComments*/ FROM tbl`,
  1469. stmt: &ast.SelectStatement{
  1470. Fields: []ast.Field{
  1471. {
  1472. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  1473. Name: "name",
  1474. AName: "",
  1475. },
  1476. },
  1477. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1478. },
  1479. },
  1480. {
  1481. s: `SELECT true AS f1, FALSE as f2 FROM tbl`,
  1482. stmt: &ast.SelectStatement{
  1483. Fields: []ast.Field{
  1484. {AName: "f1", Name: "", Expr: &ast.BooleanLiteral{Val: true}},
  1485. {AName: "f2", Name: "", Expr: &ast.BooleanLiteral{Val: false}},
  1486. },
  1487. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1488. },
  1489. },
  1490. {
  1491. s: `SELECT true AS f1 FROM tbl WHERE f2 = true`,
  1492. stmt: &ast.SelectStatement{
  1493. Fields: []ast.Field{
  1494. {AName: "f1", Name: "", Expr: &ast.BooleanLiteral{Val: true}},
  1495. },
  1496. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1497. Condition: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "f2", StreamName: ast.DefaultStream}, OP: ast.EQ, RHS: &ast.BooleanLiteral{Val: true}},
  1498. },
  1499. },
  1500. {
  1501. s: `SELECT indexof(field1, "abc") FROM tbl`,
  1502. stmt: &ast.SelectStatement{
  1503. Fields: []ast.Field{
  1504. {
  1505. AName: "",
  1506. Name: "indexof",
  1507. Expr: &ast.Call{
  1508. Name: "indexof",
  1509. Args: []ast.Expr{&ast.FieldRef{Name: "field1", StreamName: ast.DefaultStream}, &ast.StringLiteral{Val: "abc"}},
  1510. },
  1511. },
  1512. },
  1513. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1514. },
  1515. },
  1516. // The negative value expression support.
  1517. {
  1518. s: `SELECT -3 AS t1 FROM tbl`,
  1519. stmt: &ast.SelectStatement{
  1520. Fields: []ast.Field{
  1521. {
  1522. Expr: &ast.IntegerLiteral{Val: -3},
  1523. Name: "",
  1524. AName: "t1",
  1525. },
  1526. },
  1527. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1528. },
  1529. },
  1530. {
  1531. s: `SELECT - 3 AS t1 FROM tbl`,
  1532. stmt: &ast.SelectStatement{
  1533. Fields: []ast.Field{
  1534. {
  1535. Expr: &ast.IntegerLiteral{Val: -3},
  1536. Name: "",
  1537. AName: "t1",
  1538. },
  1539. },
  1540. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1541. },
  1542. },
  1543. {
  1544. s: `SELECT -. 3 AS t1 FROM tbl`,
  1545. stmt: &ast.SelectStatement{
  1546. Fields: []ast.Field{
  1547. {
  1548. Expr: &ast.NumberLiteral{Val: -.3},
  1549. Name: "",
  1550. AName: "t1",
  1551. },
  1552. },
  1553. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1554. },
  1555. },
  1556. {
  1557. s: `SELECT -3.3 AS t1 FROM tbl`,
  1558. stmt: &ast.SelectStatement{
  1559. Fields: []ast.Field{
  1560. {
  1561. Expr: &ast.NumberLiteral{Val: -3.3},
  1562. Name: "",
  1563. AName: "t1",
  1564. },
  1565. },
  1566. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1567. },
  1568. },
  1569. {
  1570. s: `SELECT sample(-.3,) FROM tbl`,
  1571. stmt: nil,
  1572. err: "function sample not found",
  1573. },
  1574. {
  1575. s: `select timestamp() as tp from demo`,
  1576. stmt: nil,
  1577. err: "function timestamp not found",
  1578. },
  1579. {
  1580. s: `select tstamp() as tp from demo`,
  1581. stmt: &ast.SelectStatement{
  1582. Fields: []ast.Field{
  1583. {
  1584. Expr: &ast.Call{
  1585. Name: "tstamp",
  1586. Args: nil,
  1587. },
  1588. Name: "tstamp",
  1589. AName: "tp",
  1590. },
  1591. },
  1592. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  1593. },
  1594. err: "",
  1595. },
  1596. {
  1597. s: `select rule_id() as rule_id from demo`,
  1598. stmt: &ast.SelectStatement{
  1599. Fields: []ast.Field{
  1600. {
  1601. Expr: &ast.Call{
  1602. Name: "rule_id",
  1603. Args: nil,
  1604. },
  1605. Name: "rule_id",
  1606. AName: "rule_id",
  1607. },
  1608. },
  1609. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  1610. },
  1611. err: "",
  1612. },
  1613. {
  1614. s: "SELECT `half FROM tb",
  1615. stmt: nil,
  1616. err: `found "EOF", expected FROM.`,
  1617. },
  1618. {
  1619. s: "SELECT `space var` FROM tbl",
  1620. stmt: &ast.SelectStatement{
  1621. Fields: []ast.Field{
  1622. {
  1623. Expr: &ast.FieldRef{Name: "space var", StreamName: ast.DefaultStream},
  1624. Name: "space var",
  1625. AName: "",
  1626. },
  1627. },
  1628. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1629. },
  1630. },
  1631. {
  1632. s: "SELECT `中文 Chinese` FROM tbl",
  1633. stmt: &ast.SelectStatement{
  1634. Fields: []ast.Field{
  1635. {
  1636. Expr: &ast.FieldRef{Name: "中文 Chinese", StreamName: ast.DefaultStream},
  1637. Name: "中文 Chinese",
  1638. AName: "",
  1639. },
  1640. },
  1641. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1642. },
  1643. },
  1644. {
  1645. s: "SELECT CASE temperature WHEN 25 THEN \"bingo\" WHEN 30 THEN \"high\" ELSE \"low\" END as label, humidity FROM tbl",
  1646. stmt: &ast.SelectStatement{
  1647. Fields: []ast.Field{
  1648. {
  1649. Expr: &ast.CaseExpr{
  1650. Value: &ast.FieldRef{Name: "temperature", StreamName: ast.DefaultStream},
  1651. WhenClauses: []*ast.WhenClause{
  1652. {
  1653. Expr: &ast.IntegerLiteral{Val: 25},
  1654. Result: &ast.StringLiteral{Val: "bingo"},
  1655. }, {
  1656. Expr: &ast.IntegerLiteral{Val: 30},
  1657. Result: &ast.StringLiteral{Val: "high"},
  1658. },
  1659. },
  1660. ElseClause: &ast.StringLiteral{Val: "low"},
  1661. },
  1662. Name: "",
  1663. AName: "label",
  1664. }, {
  1665. Expr: &ast.FieldRef{Name: "humidity", StreamName: ast.DefaultStream},
  1666. Name: "humidity",
  1667. AName: "",
  1668. },
  1669. },
  1670. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1671. },
  1672. },
  1673. {
  1674. s: "SELECT CASE temperature WHEN 25 THEN \"bingo\" WHEN 30 THEN \"high\" END as label, humidity FROM tbl",
  1675. stmt: &ast.SelectStatement{
  1676. Fields: []ast.Field{
  1677. {
  1678. Expr: &ast.CaseExpr{
  1679. Value: &ast.FieldRef{Name: "temperature", StreamName: ast.DefaultStream},
  1680. WhenClauses: []*ast.WhenClause{
  1681. {
  1682. Expr: &ast.IntegerLiteral{Val: 25},
  1683. Result: &ast.StringLiteral{Val: "bingo"},
  1684. }, {
  1685. Expr: &ast.IntegerLiteral{Val: 30},
  1686. Result: &ast.StringLiteral{Val: "high"},
  1687. },
  1688. },
  1689. ElseClause: nil,
  1690. },
  1691. Name: "",
  1692. AName: "label",
  1693. }, {
  1694. Expr: &ast.FieldRef{Name: "humidity", StreamName: ast.DefaultStream},
  1695. Name: "humidity",
  1696. AName: "",
  1697. },
  1698. },
  1699. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1700. },
  1701. },
  1702. {
  1703. s: "SELECT CASE temperature ELSE \"low\" END as label, humidity FROM tbl",
  1704. stmt: nil,
  1705. err: "invalid CASE expression, WHEN expected before ELSE",
  1706. },
  1707. {
  1708. s: "SELECT CASE WHEN temperature > 30 THEN \"high\" ELSE \"low\" END as label, humidity FROM tbl",
  1709. stmt: &ast.SelectStatement{
  1710. Fields: []ast.Field{
  1711. {
  1712. Expr: &ast.CaseExpr{
  1713. Value: nil,
  1714. WhenClauses: []*ast.WhenClause{
  1715. {
  1716. Expr: &ast.BinaryExpr{
  1717. OP: ast.GT,
  1718. LHS: &ast.FieldRef{Name: "temperature", StreamName: ast.DefaultStream},
  1719. RHS: &ast.IntegerLiteral{Val: 30},
  1720. },
  1721. Result: &ast.StringLiteral{Val: "high"},
  1722. },
  1723. },
  1724. ElseClause: &ast.StringLiteral{Val: "low"},
  1725. },
  1726. Name: "",
  1727. AName: "label",
  1728. }, {
  1729. Expr: &ast.FieldRef{Name: "humidity", StreamName: ast.DefaultStream},
  1730. Name: "humidity",
  1731. AName: "",
  1732. },
  1733. },
  1734. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1735. },
  1736. },
  1737. {
  1738. s: "SELECT CASE WHEN 30 THEN \"high\" ELSE \"low\" END as label, humidity FROM tbl",
  1739. stmt: nil,
  1740. err: "invalid CASE expression, WHEN expression must be a bool condition",
  1741. },
  1742. {
  1743. s: `SELECT count(*)-10 FROM demo`,
  1744. stmt: &ast.SelectStatement{
  1745. Fields: []ast.Field{
  1746. {
  1747. AName: "",
  1748. Name: "kuiper_field_0",
  1749. Expr: &ast.BinaryExpr{
  1750. OP: ast.SUB,
  1751. LHS: &ast.Call{
  1752. Name: "count",
  1753. Args: []ast.Expr{
  1754. &ast.Wildcard{Token: ast.ASTERISK},
  1755. },
  1756. FuncType: ast.FuncTypeAgg,
  1757. },
  1758. RHS: &ast.IntegerLiteral{Val: 10},
  1759. },
  1760. },
  1761. },
  1762. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  1763. },
  1764. },
  1765. {
  1766. s: `SELECT -abc FROM demo`,
  1767. stmt: nil,
  1768. err: "found \"-\", expected expression.",
  1769. },
  1770. {
  1771. s: `SELECT meta(*) FROM tbl`,
  1772. stmt: &ast.SelectStatement{
  1773. Fields: []ast.Field{
  1774. {
  1775. AName: "",
  1776. Name: "meta",
  1777. Expr: &ast.Call{
  1778. Name: "meta",
  1779. Args: []ast.Expr{
  1780. &ast.MetaRef{
  1781. Name: "*",
  1782. StreamName: ast.DefaultStream,
  1783. },
  1784. },
  1785. },
  1786. },
  1787. },
  1788. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1789. },
  1790. },
  1791. {
  1792. s: `SELECT changed_cols("",true,a,b,c) FROM tbl`,
  1793. stmt: &ast.SelectStatement{
  1794. Fields: []ast.Field{
  1795. {
  1796. AName: "",
  1797. Name: "changed_cols",
  1798. Expr: &ast.Call{
  1799. Name: "changed_cols",
  1800. Args: []ast.Expr{
  1801. &ast.ColFuncField{
  1802. Name: "",
  1803. Expr: &ast.StringLiteral{Val: ""},
  1804. },
  1805. &ast.ColFuncField{
  1806. Name: "",
  1807. Expr: &ast.BooleanLiteral{Val: true},
  1808. },
  1809. &ast.ColFuncField{Name: "a", Expr: &ast.FieldRef{
  1810. StreamName: ast.DefaultStream,
  1811. Name: "a",
  1812. }},
  1813. &ast.ColFuncField{Name: "b", Expr: &ast.FieldRef{
  1814. StreamName: ast.DefaultStream,
  1815. Name: "b",
  1816. }},
  1817. &ast.ColFuncField{Name: "c", Expr: &ast.FieldRef{
  1818. StreamName: ast.DefaultStream,
  1819. Name: "c",
  1820. }},
  1821. },
  1822. FuncType: ast.FuncTypeCols,
  1823. },
  1824. },
  1825. },
  1826. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1827. },
  1828. },
  1829. {
  1830. s: `SELECT changed_cols("",true,a,*,c) FROM tbl`,
  1831. stmt: &ast.SelectStatement{
  1832. Fields: []ast.Field{
  1833. {
  1834. AName: "",
  1835. Name: "changed_cols",
  1836. Expr: &ast.Call{
  1837. Name: "changed_cols",
  1838. Args: []ast.Expr{
  1839. &ast.ColFuncField{
  1840. Name: "",
  1841. Expr: &ast.StringLiteral{Val: ""},
  1842. },
  1843. &ast.ColFuncField{
  1844. Name: "",
  1845. Expr: &ast.BooleanLiteral{Val: true},
  1846. },
  1847. &ast.ColFuncField{Name: "a", Expr: &ast.FieldRef{
  1848. StreamName: ast.DefaultStream,
  1849. Name: "a",
  1850. }},
  1851. &ast.ColFuncField{Name: "*", Expr: &ast.Wildcard{
  1852. Token: ast.ASTERISK,
  1853. }},
  1854. &ast.ColFuncField{Name: "c", Expr: &ast.FieldRef{
  1855. StreamName: ast.DefaultStream,
  1856. Name: "c",
  1857. }},
  1858. },
  1859. FuncType: ast.FuncTypeCols,
  1860. },
  1861. },
  1862. },
  1863. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1864. },
  1865. },
  1866. {
  1867. s: `SELECT a FROM tbl WHERE changed_cols("",true,a,b,c) > 3`,
  1868. err: "function changed_cols can only be used inside the select clause",
  1869. },
  1870. {
  1871. s: `SELECT ".*(/)(?!.*\1)" FROM topic/sensor1 AS t1`,
  1872. err: `found "invalid string: \".*(/)(?!.*\\1)\"", expected expression.`,
  1873. },
  1874. {
  1875. s: `SELECT name FROM tbl WHERE name IN ("A", "B","C")`,
  1876. stmt: &ast.SelectStatement{
  1877. Fields: []ast.Field{
  1878. {
  1879. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  1880. Name: "name",
  1881. AName: "",
  1882. },
  1883. },
  1884. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1885. Condition: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, OP: ast.IN, RHS: &ast.ValueSetExpr{LiteralExprs: []ast.Expr{&ast.StringLiteral{Val: "A"}, &ast.StringLiteral{Val: "B"}, &ast.StringLiteral{Val: "C"}}}},
  1886. },
  1887. },
  1888. {
  1889. s: `SELECT name FROM tbl WHERE name IN ()`,
  1890. err: `expect elements for IN expression, but found ")", expected expression.`,
  1891. },
  1892. {
  1893. s: `SELECT name FROM tbl WHERE name IN (abc,def OR name in (abc)`,
  1894. err: `expect ) for IN expression, but got "EOF"`,
  1895. },
  1896. {
  1897. s: `SELECT lag(name) OVER (PARTITION BY device) FROM tbl`,
  1898. stmt: &ast.SelectStatement{
  1899. Fields: []ast.Field{
  1900. {
  1901. Expr: &ast.Call{
  1902. Name: "lag",
  1903. FuncId: 0,
  1904. Args: []ast.Expr{
  1905. &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  1906. },
  1907. Partition: &ast.PartitionExpr{
  1908. Exprs: []ast.Expr{
  1909. &ast.FieldRef{Name: "device", StreamName: ast.DefaultStream},
  1910. },
  1911. },
  1912. },
  1913. Name: "lag",
  1914. AName: "",
  1915. },
  1916. },
  1917. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1918. },
  1919. },
  1920. {
  1921. s: `SELECT name OVER (PARTITION BY device) FROM tbl`,
  1922. err: `found "OVER", expected FROM.`,
  1923. },
  1924. {
  1925. s: `SELECT avg(name) OVER (PARTITION BY device) FROM tbl`,
  1926. err: `Found OVER after non analytic function avg`,
  1927. },
  1928. {
  1929. s: `SELECT name FROM tbl WHERE lag(name) OVER (PARTITION BY device, groupName) > 3`,
  1930. stmt: &ast.SelectStatement{
  1931. Fields: []ast.Field{
  1932. {
  1933. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  1934. Name: "name",
  1935. AName: "",
  1936. },
  1937. },
  1938. Condition: &ast.BinaryExpr{
  1939. LHS: &ast.Call{
  1940. Name: "lag",
  1941. FuncId: 0,
  1942. Args: []ast.Expr{
  1943. &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  1944. },
  1945. Partition: &ast.PartitionExpr{
  1946. Exprs: []ast.Expr{
  1947. &ast.FieldRef{Name: "device", StreamName: ast.DefaultStream},
  1948. &ast.FieldRef{Name: "groupName", StreamName: ast.DefaultStream},
  1949. },
  1950. },
  1951. },
  1952. OP: ast.GT,
  1953. RHS: &ast.IntegerLiteral{Val: 3},
  1954. },
  1955. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1956. },
  1957. },
  1958. {
  1959. s: `SELECT lag(name) OVER (PARTITION BY device) as ll FROM tbl`,
  1960. stmt: &ast.SelectStatement{
  1961. Fields: []ast.Field{
  1962. {
  1963. Expr: &ast.Call{
  1964. Name: "lag",
  1965. FuncId: 0,
  1966. Args: []ast.Expr{
  1967. &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  1968. },
  1969. Partition: &ast.PartitionExpr{
  1970. Exprs: []ast.Expr{
  1971. &ast.FieldRef{Name: "device", StreamName: ast.DefaultStream},
  1972. },
  1973. },
  1974. },
  1975. Name: "lag",
  1976. AName: "ll",
  1977. },
  1978. },
  1979. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1980. },
  1981. },
  1982. {
  1983. s: `SELECT lag(name) OVER (PARTITION BY device WHEN abc > 12) as ll FROM tbl`,
  1984. stmt: &ast.SelectStatement{
  1985. Fields: []ast.Field{
  1986. {
  1987. Expr: &ast.Call{
  1988. Name: "lag",
  1989. FuncId: 0,
  1990. Args: []ast.Expr{
  1991. &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  1992. },
  1993. Partition: &ast.PartitionExpr{
  1994. Exprs: []ast.Expr{
  1995. &ast.FieldRef{Name: "device", StreamName: ast.DefaultStream},
  1996. },
  1997. },
  1998. WhenExpr: &ast.BinaryExpr{
  1999. LHS: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream},
  2000. OP: ast.GT,
  2001. RHS: &ast.IntegerLiteral{Val: 12},
  2002. },
  2003. },
  2004. Name: "lag",
  2005. AName: "ll",
  2006. },
  2007. },
  2008. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  2009. },
  2010. },
  2011. {
  2012. s: `SELECT lag(name) OVER (PARTITION BY device WHEN had_changed(true, StatusCode)) as ll FROM tbl`,
  2013. stmt: &ast.SelectStatement{
  2014. Fields: []ast.Field{
  2015. {
  2016. Expr: &ast.Call{
  2017. Name: "lag",
  2018. FuncId: 0,
  2019. Args: []ast.Expr{
  2020. &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  2021. },
  2022. Partition: &ast.PartitionExpr{
  2023. Exprs: []ast.Expr{
  2024. &ast.FieldRef{Name: "device", StreamName: ast.DefaultStream},
  2025. },
  2026. },
  2027. WhenExpr: &ast.Call{
  2028. Name: "had_changed",
  2029. FuncId: 1,
  2030. Args: []ast.Expr{&ast.BooleanLiteral{Val: true}, &ast.FieldRef{Name: "StatusCode", StreamName: ast.DefaultStream}},
  2031. },
  2032. },
  2033. Name: "lag",
  2034. AName: "ll",
  2035. },
  2036. },
  2037. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  2038. },
  2039. },
  2040. {
  2041. s: `SELECT lag(name) OVER (WHEN had_changed(true, StatusCode)) as ll FROM tbl`,
  2042. stmt: &ast.SelectStatement{
  2043. Fields: []ast.Field{
  2044. {
  2045. Expr: &ast.Call{
  2046. Name: "lag",
  2047. FuncId: 0,
  2048. Args: []ast.Expr{
  2049. &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  2050. },
  2051. WhenExpr: &ast.Call{
  2052. Name: "had_changed",
  2053. FuncId: 1,
  2054. Args: []ast.Expr{&ast.BooleanLiteral{Val: true}, &ast.FieldRef{Name: "StatusCode", StreamName: ast.DefaultStream}},
  2055. },
  2056. },
  2057. Name: "lag",
  2058. AName: "ll",
  2059. },
  2060. },
  2061. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  2062. },
  2063. },
  2064. {
  2065. s: `SELECT name OVER (WHEN a > b) FROM tbl`,
  2066. err: `found "OVER", expected FROM.`,
  2067. },
  2068. {
  2069. s: `SELECT avg(name) OVER (WHEN a > b) FROM tbl`,
  2070. err: `Found OVER after non analytic function avg`,
  2071. },
  2072. {
  2073. s: `SELECT *, name, lower(name) as ln FROM tbl`,
  2074. stmt: &ast.SelectStatement{
  2075. Fields: []ast.Field{
  2076. {
  2077. Expr: &ast.Wildcard{
  2078. Token: ast.ASTERISK,
  2079. },
  2080. Name: "*",
  2081. },
  2082. {
  2083. Expr: &ast.FieldRef{
  2084. Name: "name",
  2085. StreamName: ast.DefaultStream,
  2086. },
  2087. Name: "name",
  2088. AName: "",
  2089. },
  2090. {
  2091. Expr: &ast.Call{
  2092. Name: "lower",
  2093. FuncId: 0,
  2094. Args: []ast.Expr{
  2095. &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  2096. },
  2097. },
  2098. Name: "lower",
  2099. AName: "ln",
  2100. },
  2101. },
  2102. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  2103. },
  2104. },
  2105. {
  2106. s: `SELECT name, * FROM tbl`,
  2107. stmt: &ast.SelectStatement{
  2108. Fields: []ast.Field{
  2109. {
  2110. Expr: &ast.FieldRef{
  2111. Name: "name",
  2112. StreamName: ast.DefaultStream,
  2113. },
  2114. Name: "name",
  2115. AName: "",
  2116. },
  2117. {
  2118. Expr: &ast.Wildcard{
  2119. Token: ast.ASTERISK,
  2120. },
  2121. Name: "*",
  2122. },
  2123. },
  2124. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  2125. },
  2126. },
  2127. {
  2128. s: `SELECT name, * FROM tbl`,
  2129. stmt: &ast.SelectStatement{
  2130. Fields: []ast.Field{
  2131. {
  2132. Expr: &ast.FieldRef{
  2133. Name: "name",
  2134. StreamName: ast.DefaultStream,
  2135. },
  2136. Name: "name",
  2137. AName: "",
  2138. },
  2139. {
  2140. Expr: &ast.Wildcard{
  2141. Token: ast.ASTERISK,
  2142. },
  2143. Name: "*",
  2144. },
  2145. },
  2146. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  2147. },
  2148. },
  2149. }
  2150. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  2151. for i, tt := range tests {
  2152. // fmt.Printf("Parsing SQL %q.\n", tt.s)
  2153. stmt, err := NewParser(strings.NewReader(tt.s)).Parse()
  2154. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  2155. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.s, tt.err, err)
  2156. } else if tt.err == "" && !reflect.DeepEqual(tt.stmt, stmt) {
  2157. t.Errorf("%d. %q\n\nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.s, tt.stmt, stmt)
  2158. }
  2159. }
  2160. }
  2161. func TestParser_ParseWindowsExpr(t *testing.T) {
  2162. tests := []struct {
  2163. s string
  2164. stmt *ast.SelectStatement
  2165. err string
  2166. }{
  2167. {
  2168. s: `SELECT f1 FROM tbl GROUP BY TUMBLINGWINDOW(ss, 10)`,
  2169. stmt: &ast.SelectStatement{
  2170. Fields: []ast.Field{
  2171. {
  2172. Expr: &ast.FieldRef{Name: "f1", StreamName: ast.DefaultStream},
  2173. Name: "f1",
  2174. AName: "",
  2175. },
  2176. },
  2177. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  2178. Dimensions: ast.Dimensions{
  2179. ast.Dimension{
  2180. Expr: &ast.Window{
  2181. WindowType: ast.TUMBLING_WINDOW,
  2182. Length: &ast.IntegerLiteral{Val: 10000},
  2183. Interval: &ast.IntegerLiteral{Val: 0},
  2184. },
  2185. },
  2186. },
  2187. },
  2188. },
  2189. {
  2190. s: `SELECT f1 FROM tbl GROUP BY HOPPINGWINDOW(mi, 5, 1)`,
  2191. stmt: &ast.SelectStatement{
  2192. Fields: []ast.Field{
  2193. {
  2194. Expr: &ast.FieldRef{Name: "f1", StreamName: ast.DefaultStream},
  2195. Name: "f1",
  2196. AName: "",
  2197. },
  2198. },
  2199. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  2200. Dimensions: ast.Dimensions{
  2201. ast.Dimension{
  2202. Expr: &ast.Window{
  2203. WindowType: ast.HOPPING_WINDOW,
  2204. Length: &ast.IntegerLiteral{Val: 3e5},
  2205. Interval: &ast.IntegerLiteral{Val: 6e4},
  2206. },
  2207. },
  2208. },
  2209. },
  2210. },
  2211. {
  2212. s: `SELECT f1 FROM tbl GROUP BY SESSIONWINDOW(hh, 5, 1)`,
  2213. stmt: &ast.SelectStatement{
  2214. Fields: []ast.Field{
  2215. {
  2216. Expr: &ast.FieldRef{Name: "f1", StreamName: ast.DefaultStream},
  2217. Name: "f1",
  2218. AName: "",
  2219. },
  2220. },
  2221. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  2222. Dimensions: ast.Dimensions{
  2223. ast.Dimension{
  2224. Expr: &ast.Window{
  2225. WindowType: ast.SESSION_WINDOW,
  2226. Length: &ast.IntegerLiteral{Val: 1.8e7},
  2227. Interval: &ast.IntegerLiteral{Val: 3.6e6},
  2228. },
  2229. },
  2230. },
  2231. },
  2232. },
  2233. {
  2234. s: `SELECT f1 FROM tbl GROUP BY SLIDINGWINDOW(ms, 5)`,
  2235. stmt: &ast.SelectStatement{
  2236. Fields: []ast.Field{
  2237. {
  2238. Expr: &ast.FieldRef{Name: "f1", StreamName: ast.DefaultStream},
  2239. Name: "f1",
  2240. AName: "",
  2241. },
  2242. },
  2243. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  2244. Dimensions: ast.Dimensions{
  2245. ast.Dimension{
  2246. Expr: &ast.Window{
  2247. WindowType: ast.SLIDING_WINDOW,
  2248. Length: &ast.IntegerLiteral{Val: 5},
  2249. Interval: &ast.IntegerLiteral{Val: 0},
  2250. },
  2251. },
  2252. },
  2253. },
  2254. },
  2255. {
  2256. s: `SELECT f1 FROM tbl GROUP BY SLIDINGWINDOW(mi, 5, 1)`,
  2257. stmt: nil,
  2258. err: "The arguments for slidingwindow should be 2.\n",
  2259. },
  2260. {
  2261. s: `SELECT f1 FROM tbl GROUP BY SLIDINGWINDOW("mi", 5)`,
  2262. stmt: nil,
  2263. err: "The 1st argument for slidingwindow is expecting timer literal expression. One value of [dd|hh|mi|ss|ms].\n",
  2264. },
  2265. {
  2266. s: `SELECT f1 FROM tbl GROUP BY COUNTWINDOW(10)`,
  2267. stmt: &ast.SelectStatement{
  2268. Fields: []ast.Field{
  2269. {
  2270. Expr: &ast.FieldRef{Name: "f1", StreamName: ast.DefaultStream},
  2271. Name: "f1",
  2272. AName: "",
  2273. },
  2274. },
  2275. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  2276. Dimensions: ast.Dimensions{
  2277. ast.Dimension{
  2278. Expr: &ast.Window{
  2279. WindowType: ast.COUNT_WINDOW,
  2280. Length: &ast.IntegerLiteral{Val: 10},
  2281. },
  2282. },
  2283. },
  2284. },
  2285. },
  2286. {
  2287. s: `SELECT f1 FROM tbl GROUP BY COUNTWINDOW(10, 5)`,
  2288. stmt: &ast.SelectStatement{
  2289. Fields: []ast.Field{
  2290. {
  2291. Expr: &ast.FieldRef{Name: "f1", StreamName: ast.DefaultStream},
  2292. Name: "f1",
  2293. AName: "",
  2294. },
  2295. },
  2296. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  2297. Dimensions: ast.Dimensions{
  2298. ast.Dimension{
  2299. Expr: &ast.Window{
  2300. WindowType: ast.COUNT_WINDOW,
  2301. Length: &ast.IntegerLiteral{Val: 10},
  2302. Interval: &ast.IntegerLiteral{Val: 5},
  2303. },
  2304. },
  2305. },
  2306. },
  2307. },
  2308. {
  2309. s: `SELECT f1 FROM tbl GROUP BY COUNTWINDOW(3, 5)`,
  2310. stmt: nil,
  2311. err: "The second parameter value 5 should be less than the first parameter 3.",
  2312. },
  2313. {
  2314. s: `SELECT * FROM demo GROUP BY COUNTWINDOW(3,1) FILTER( where revenue > 100 )`,
  2315. stmt: &ast.SelectStatement{
  2316. Fields: []ast.Field{
  2317. {
  2318. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  2319. Name: "*",
  2320. AName: "",
  2321. },
  2322. },
  2323. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2324. Dimensions: ast.Dimensions{
  2325. ast.Dimension{
  2326. Expr: &ast.Window{
  2327. WindowType: ast.COUNT_WINDOW,
  2328. Length: &ast.IntegerLiteral{Val: 3},
  2329. Interval: &ast.IntegerLiteral{Val: 1},
  2330. Filter: &ast.BinaryExpr{
  2331. LHS: &ast.FieldRef{Name: "revenue", StreamName: ast.DefaultStream},
  2332. OP: ast.GT,
  2333. RHS: &ast.IntegerLiteral{Val: 100},
  2334. },
  2335. },
  2336. },
  2337. },
  2338. },
  2339. },
  2340. {
  2341. s: `SELECT * FROM demo GROUP BY department, COUNTWINDOW(3,1) FILTER( where revenue > 100 ), year`,
  2342. stmt: &ast.SelectStatement{
  2343. Fields: []ast.Field{
  2344. {
  2345. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  2346. Name: "*",
  2347. AName: "",
  2348. },
  2349. },
  2350. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2351. Dimensions: ast.Dimensions{
  2352. ast.Dimension{Expr: &ast.FieldRef{Name: "department", StreamName: ast.DefaultStream}},
  2353. ast.Dimension{
  2354. Expr: &ast.Window{
  2355. WindowType: ast.COUNT_WINDOW,
  2356. Length: &ast.IntegerLiteral{Val: 3},
  2357. Interval: &ast.IntegerLiteral{Val: 1},
  2358. Filter: &ast.BinaryExpr{
  2359. LHS: &ast.FieldRef{Name: "revenue", StreamName: ast.DefaultStream},
  2360. OP: ast.GT,
  2361. RHS: &ast.IntegerLiteral{Val: 100},
  2362. },
  2363. },
  2364. },
  2365. ast.Dimension{Expr: &ast.FieldRef{Name: "year", StreamName: ast.DefaultStream}},
  2366. },
  2367. },
  2368. },
  2369. {
  2370. s: `SELECT * FROM demo GROUP BY department, COUNTWINDOW(3,1) FILTER( where revenue IN (100, 200)), year`,
  2371. stmt: &ast.SelectStatement{
  2372. Fields: []ast.Field{
  2373. {
  2374. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  2375. Name: "*",
  2376. AName: "",
  2377. },
  2378. },
  2379. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2380. Dimensions: ast.Dimensions{
  2381. ast.Dimension{Expr: &ast.FieldRef{Name: "department", StreamName: ast.DefaultStream}},
  2382. ast.Dimension{
  2383. Expr: &ast.Window{
  2384. WindowType: ast.COUNT_WINDOW,
  2385. Length: &ast.IntegerLiteral{Val: 3},
  2386. Interval: &ast.IntegerLiteral{Val: 1},
  2387. Filter: &ast.BinaryExpr{
  2388. LHS: &ast.FieldRef{Name: "revenue", StreamName: ast.DefaultStream},
  2389. OP: ast.IN,
  2390. RHS: &ast.ValueSetExpr{
  2391. LiteralExprs: []ast.Expr{&ast.IntegerLiteral{Val: 100}, &ast.IntegerLiteral{Val: 200}},
  2392. },
  2393. },
  2394. },
  2395. },
  2396. ast.Dimension{Expr: &ast.FieldRef{Name: "year", StreamName: ast.DefaultStream}},
  2397. },
  2398. },
  2399. },
  2400. // to be supported
  2401. {
  2402. s: `SELECT sum(f1) FILTER( where revenue > 100 ) FROM tbl GROUP BY year`,
  2403. stmt: nil,
  2404. err: "found \"FILTER\", expected FROM.",
  2405. },
  2406. {
  2407. s: `SELECT * FROM demo GROUP BY COUNTWINDOW(3,1) FILTER where revenue > 100`,
  2408. stmt: nil,
  2409. err: "Found \"WHERE\" after FILTER, expect parentheses.",
  2410. },
  2411. {
  2412. s: `SELECT * FROM demo GROUP BY COUNTWINDOW(3,1) where revenue > 100`,
  2413. stmt: nil,
  2414. err: "found \"WHERE\", expected EOF.",
  2415. },
  2416. }
  2417. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  2418. for i, tt := range tests {
  2419. // fmt.Printf("Parsing SQL %q.\n", tt.s)
  2420. stmt, err := NewParser(strings.NewReader(tt.s)).Parse()
  2421. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  2422. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.s, tt.err, err)
  2423. } else if tt.err == "" && !reflect.DeepEqual(tt.stmt, stmt) {
  2424. t.Errorf("%d. %q\n\nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.s, tt.stmt, stmt)
  2425. }
  2426. }
  2427. }
  2428. func TestParser_ParseJsonExpr(t *testing.T) {
  2429. tests := []struct {
  2430. s string
  2431. stmt *ast.SelectStatement
  2432. err string
  2433. }{
  2434. {
  2435. s: `SELECT children[0] FROM demo`,
  2436. stmt: &ast.SelectStatement{
  2437. Fields: []ast.Field{
  2438. {
  2439. Expr: &ast.BinaryExpr{
  2440. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  2441. OP: ast.SUBSET,
  2442. RHS: &ast.IndexExpr{Index: &ast.IntegerLiteral{Val: 0}},
  2443. },
  2444. Name: "kuiper_field_0",
  2445. AName: "",
  2446. },
  2447. },
  2448. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2449. },
  2450. },
  2451. {
  2452. s: `SELECT children[0]->first FROM demo`,
  2453. stmt: &ast.SelectStatement{
  2454. Fields: []ast.Field{
  2455. {
  2456. Expr: &ast.BinaryExpr{
  2457. LHS: &ast.BinaryExpr{
  2458. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  2459. OP: ast.SUBSET,
  2460. RHS: &ast.IndexExpr{Index: &ast.IntegerLiteral{Val: 0}},
  2461. },
  2462. OP: ast.ARROW,
  2463. RHS: &ast.JsonFieldRef{Name: "first"},
  2464. },
  2465. Name: "kuiper_field_0",
  2466. AName: "",
  2467. },
  2468. },
  2469. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2470. },
  2471. },
  2472. {
  2473. s: `SELECT children->first[2] FROM demo`,
  2474. stmt: &ast.SelectStatement{
  2475. Fields: []ast.Field{
  2476. {
  2477. Expr: &ast.BinaryExpr{
  2478. LHS: &ast.BinaryExpr{
  2479. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  2480. OP: ast.ARROW,
  2481. RHS: &ast.JsonFieldRef{Name: "first"},
  2482. },
  2483. OP: ast.SUBSET,
  2484. RHS: &ast.IndexExpr{Index: &ast.IntegerLiteral{Val: 2}},
  2485. },
  2486. Name: "kuiper_field_0",
  2487. AName: "",
  2488. },
  2489. },
  2490. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2491. },
  2492. },
  2493. {
  2494. s: `SELECT children->first[2]->test FROM demo`,
  2495. stmt: &ast.SelectStatement{
  2496. Fields: []ast.Field{
  2497. {
  2498. Expr: &ast.BinaryExpr{
  2499. LHS: &ast.BinaryExpr{
  2500. LHS: &ast.BinaryExpr{
  2501. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  2502. OP: ast.ARROW,
  2503. RHS: &ast.JsonFieldRef{Name: "first"},
  2504. },
  2505. OP: ast.SUBSET,
  2506. RHS: &ast.IndexExpr{Index: &ast.IntegerLiteral{Val: 2}},
  2507. },
  2508. OP: ast.ARROW,
  2509. RHS: &ast.JsonFieldRef{Name: "test"},
  2510. },
  2511. Name: "kuiper_field_0",
  2512. AName: "",
  2513. },
  2514. },
  2515. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2516. },
  2517. },
  2518. {
  2519. s: `SELECT demo.children->first->test FROM demo`,
  2520. stmt: &ast.SelectStatement{
  2521. Fields: []ast.Field{
  2522. {
  2523. Expr: &ast.BinaryExpr{
  2524. LHS: &ast.BinaryExpr{
  2525. LHS: &ast.FieldRef{Name: "children", StreamName: "demo"},
  2526. OP: ast.ARROW,
  2527. RHS: &ast.JsonFieldRef{Name: "first"},
  2528. },
  2529. OP: ast.ARROW,
  2530. RHS: &ast.JsonFieldRef{Name: "test"},
  2531. },
  2532. Name: "kuiper_field_0",
  2533. AName: "",
  2534. },
  2535. },
  2536. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2537. },
  2538. },
  2539. {
  2540. s: `SELECT demo.children.first.test FROM demo`,
  2541. stmt: &ast.SelectStatement{
  2542. Fields: []ast.Field{
  2543. {
  2544. Expr: &ast.BinaryExpr{
  2545. LHS: &ast.BinaryExpr{
  2546. LHS: &ast.FieldRef{Name: "children", StreamName: "demo"},
  2547. OP: ast.ARROW,
  2548. RHS: &ast.JsonFieldRef{Name: "first"},
  2549. },
  2550. OP: ast.ARROW,
  2551. RHS: &ast.JsonFieldRef{Name: "test"},
  2552. },
  2553. Name: "kuiper_field_0",
  2554. AName: "",
  2555. },
  2556. },
  2557. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2558. },
  2559. },
  2560. {
  2561. s: `SELECT demo.children.first->test FROM demo`,
  2562. stmt: &ast.SelectStatement{
  2563. Fields: []ast.Field{
  2564. {
  2565. Expr: &ast.BinaryExpr{
  2566. LHS: &ast.BinaryExpr{
  2567. LHS: &ast.FieldRef{Name: "children", StreamName: "demo"},
  2568. OP: ast.ARROW,
  2569. RHS: &ast.JsonFieldRef{Name: "first"},
  2570. },
  2571. OP: ast.ARROW,
  2572. RHS: &ast.JsonFieldRef{Name: "test"},
  2573. },
  2574. Name: "kuiper_field_0",
  2575. AName: "",
  2576. },
  2577. },
  2578. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2579. },
  2580. },
  2581. {
  2582. s: `SELECT demo.children->first.test FROM demo`,
  2583. stmt: &ast.SelectStatement{
  2584. Fields: []ast.Field{
  2585. {
  2586. Expr: &ast.BinaryExpr{
  2587. LHS: &ast.BinaryExpr{
  2588. LHS: &ast.FieldRef{Name: "children", StreamName: "demo"},
  2589. OP: ast.ARROW,
  2590. RHS: &ast.JsonFieldRef{Name: "first"},
  2591. },
  2592. OP: ast.ARROW,
  2593. RHS: &ast.JsonFieldRef{Name: "test"},
  2594. },
  2595. Name: "kuiper_field_0",
  2596. AName: "",
  2597. },
  2598. },
  2599. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2600. },
  2601. },
  2602. {
  2603. s: `SELECT children[0:1] FROM demo`,
  2604. stmt: &ast.SelectStatement{
  2605. Fields: []ast.Field{
  2606. {
  2607. Expr: &ast.BinaryExpr{
  2608. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  2609. OP: ast.SUBSET,
  2610. RHS: &ast.ColonExpr{Start: &ast.IntegerLiteral{Val: 0}, End: &ast.IntegerLiteral{Val: 1}},
  2611. },
  2612. Name: "kuiper_field_0",
  2613. AName: "",
  2614. },
  2615. },
  2616. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2617. },
  2618. },
  2619. {
  2620. s: `SELECT children[:1] FROM demo`,
  2621. stmt: &ast.SelectStatement{
  2622. Fields: []ast.Field{
  2623. {
  2624. Expr: &ast.BinaryExpr{
  2625. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  2626. OP: ast.SUBSET,
  2627. RHS: &ast.ColonExpr{Start: &ast.IntegerLiteral{Val: 0}, End: &ast.IntegerLiteral{Val: 1}},
  2628. },
  2629. Name: "kuiper_field_0",
  2630. AName: "",
  2631. },
  2632. },
  2633. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2634. },
  2635. },
  2636. {
  2637. s: `SELECT children[:] FROM demo`,
  2638. stmt: &ast.SelectStatement{
  2639. Fields: []ast.Field{
  2640. {
  2641. Expr: &ast.BinaryExpr{
  2642. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  2643. OP: ast.SUBSET,
  2644. RHS: &ast.ColonExpr{Start: &ast.IntegerLiteral{Val: 0}, End: &ast.IntegerLiteral{Val: math.MinInt32}},
  2645. },
  2646. Name: "kuiper_field_0",
  2647. AName: "",
  2648. },
  2649. },
  2650. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2651. },
  2652. },
  2653. {
  2654. s: `SELECT children[2:] AS c FROM demo`,
  2655. stmt: &ast.SelectStatement{
  2656. Fields: []ast.Field{
  2657. {
  2658. Expr: &ast.BinaryExpr{
  2659. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  2660. OP: ast.SUBSET,
  2661. RHS: &ast.ColonExpr{Start: &ast.IntegerLiteral{Val: 2}, End: &ast.IntegerLiteral{Val: math.MinInt32}},
  2662. },
  2663. Name: "",
  2664. AName: "c",
  2665. },
  2666. },
  2667. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2668. },
  2669. },
  2670. {
  2671. s: `SELECT children[2:]->first AS c FROM demo`,
  2672. stmt: &ast.SelectStatement{
  2673. Fields: []ast.Field{
  2674. {
  2675. Expr: &ast.BinaryExpr{
  2676. 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}}},
  2677. OP: ast.ARROW,
  2678. RHS: &ast.JsonFieldRef{Name: "first"},
  2679. },
  2680. Name: "",
  2681. AName: "c",
  2682. },
  2683. },
  2684. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2685. },
  2686. },
  2687. {
  2688. s: `SELECT demo.* FROM demo`,
  2689. stmt: &ast.SelectStatement{
  2690. Fields: []ast.Field{
  2691. {
  2692. Expr: &ast.FieldRef{StreamName: ast.StreamName("demo"), Name: "*"},
  2693. Name: "*",
  2694. AName: "",
  2695. },
  2696. },
  2697. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2698. },
  2699. },
  2700. {
  2701. s: `SELECT demo.children[2:]->first AS c FROM demo`,
  2702. stmt: &ast.SelectStatement{
  2703. Fields: []ast.Field{
  2704. {
  2705. Expr: &ast.BinaryExpr{
  2706. 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}}},
  2707. OP: ast.ARROW,
  2708. RHS: &ast.JsonFieldRef{Name: "first"},
  2709. },
  2710. Name: "",
  2711. AName: "c",
  2712. },
  2713. },
  2714. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2715. },
  2716. },
  2717. {
  2718. s: `SELECT lower(demo.children[2:]->first) AS c FROM demo`,
  2719. stmt: &ast.SelectStatement{
  2720. Fields: []ast.Field{
  2721. {
  2722. Expr: &ast.Call{
  2723. Name: "lower",
  2724. Args: []ast.Expr{
  2725. &ast.BinaryExpr{
  2726. 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}}},
  2727. OP: ast.ARROW,
  2728. RHS: &ast.JsonFieldRef{Name: "first"},
  2729. },
  2730. },
  2731. },
  2732. Name: "lower",
  2733. AName: "c",
  2734. },
  2735. },
  2736. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2737. },
  2738. },
  2739. {
  2740. s: `SELECT children[:1] FROM demo WHERE abc[0] > 12`,
  2741. stmt: &ast.SelectStatement{
  2742. Fields: []ast.Field{
  2743. {
  2744. Expr: &ast.BinaryExpr{
  2745. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  2746. OP: ast.SUBSET,
  2747. RHS: &ast.ColonExpr{Start: &ast.IntegerLiteral{Val: 0}, End: &ast.IntegerLiteral{Val: 1}},
  2748. },
  2749. Name: "kuiper_field_0",
  2750. AName: "",
  2751. },
  2752. },
  2753. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2754. Condition: &ast.BinaryExpr{
  2755. LHS: &ast.BinaryExpr{
  2756. LHS: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream},
  2757. OP: ast.SUBSET,
  2758. RHS: &ast.IndexExpr{Index: &ast.IntegerLiteral{Val: 0}},
  2759. },
  2760. OP: ast.GT,
  2761. RHS: &ast.IntegerLiteral{Val: 12},
  2762. },
  2763. },
  2764. },
  2765. {
  2766. s: `SELECT children[:1] FROM demo WHERE abc[0] IN demo.children[2:].first`,
  2767. stmt: &ast.SelectStatement{
  2768. Fields: []ast.Field{
  2769. {
  2770. Expr: &ast.BinaryExpr{
  2771. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  2772. OP: ast.SUBSET,
  2773. RHS: &ast.ColonExpr{Start: &ast.IntegerLiteral{Val: 0}, End: &ast.IntegerLiteral{Val: 1}},
  2774. },
  2775. Name: "kuiper_field_0",
  2776. AName: "",
  2777. },
  2778. },
  2779. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2780. Condition: &ast.BinaryExpr{
  2781. LHS: &ast.BinaryExpr{
  2782. LHS: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream},
  2783. OP: ast.SUBSET,
  2784. RHS: &ast.IndexExpr{Index: &ast.IntegerLiteral{Val: 0}},
  2785. },
  2786. OP: ast.IN,
  2787. RHS: &ast.BinaryExpr{
  2788. 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}}},
  2789. OP: ast.ARROW,
  2790. RHS: &ast.JsonFieldRef{Name: "first"},
  2791. },
  2792. },
  2793. },
  2794. },
  2795. {
  2796. s: `SELECT children[:1] FROM demo WHERE abc[0] IN children[2:].first`,
  2797. stmt: &ast.SelectStatement{
  2798. Fields: []ast.Field{
  2799. {
  2800. Expr: &ast.BinaryExpr{
  2801. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  2802. OP: ast.SUBSET,
  2803. RHS: &ast.ColonExpr{Start: &ast.IntegerLiteral{Val: 0}, End: &ast.IntegerLiteral{Val: 1}},
  2804. },
  2805. Name: "kuiper_field_0",
  2806. AName: "",
  2807. },
  2808. },
  2809. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2810. Condition: &ast.BinaryExpr{
  2811. LHS: &ast.BinaryExpr{
  2812. LHS: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream},
  2813. OP: ast.SUBSET,
  2814. RHS: &ast.IndexExpr{Index: &ast.IntegerLiteral{Val: 0}},
  2815. },
  2816. OP: ast.IN,
  2817. RHS: &ast.BinaryExpr{
  2818. LHS: &ast.BinaryExpr{LHS: &ast.FieldRef{StreamName: ast.DefaultStream, Name: "children"}, OP: ast.SUBSET, RHS: &ast.ColonExpr{Start: &ast.IntegerLiteral{Val: 2}, End: &ast.IntegerLiteral{Val: math.MinInt32}}},
  2819. OP: ast.ARROW,
  2820. RHS: &ast.JsonFieldRef{Name: "first"},
  2821. },
  2822. },
  2823. },
  2824. },
  2825. {
  2826. s: `SELECT children[:1] FROM demo WHERE abc[0] IN demo.children[2:]->first`,
  2827. stmt: &ast.SelectStatement{
  2828. Fields: []ast.Field{
  2829. {
  2830. Expr: &ast.BinaryExpr{
  2831. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  2832. OP: ast.SUBSET,
  2833. RHS: &ast.ColonExpr{Start: &ast.IntegerLiteral{Val: 0}, End: &ast.IntegerLiteral{Val: 1}},
  2834. },
  2835. Name: "kuiper_field_0",
  2836. AName: "",
  2837. },
  2838. },
  2839. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2840. Condition: &ast.BinaryExpr{
  2841. LHS: &ast.BinaryExpr{
  2842. LHS: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream},
  2843. OP: ast.SUBSET,
  2844. RHS: &ast.IndexExpr{Index: &ast.IntegerLiteral{Val: 0}},
  2845. },
  2846. OP: ast.IN,
  2847. RHS: &ast.BinaryExpr{
  2848. 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}}},
  2849. OP: ast.ARROW,
  2850. RHS: &ast.JsonFieldRef{Name: "first"},
  2851. },
  2852. },
  2853. },
  2854. },
  2855. {
  2856. s: `SELECT demo.children.first AS c FROM demo`,
  2857. stmt: &ast.SelectStatement{
  2858. Fields: []ast.Field{
  2859. {
  2860. Expr: &ast.BinaryExpr{
  2861. LHS: &ast.FieldRef{Name: "children", StreamName: "demo"},
  2862. OP: ast.ARROW,
  2863. RHS: &ast.JsonFieldRef{Name: "first"},
  2864. },
  2865. Name: "",
  2866. AName: "c",
  2867. },
  2868. },
  2869. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2870. },
  2871. },
  2872. {
  2873. s: `SELECT children[index] FROM demo`,
  2874. stmt: &ast.SelectStatement{
  2875. Fields: []ast.Field{
  2876. {
  2877. Expr: &ast.BinaryExpr{
  2878. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  2879. OP: ast.SUBSET,
  2880. RHS: &ast.IndexExpr{Index: &ast.FieldRef{Name: "index", StreamName: ast.DefaultStream}},
  2881. },
  2882. Name: "kuiper_field_0",
  2883. AName: "",
  2884. },
  2885. },
  2886. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2887. },
  2888. },
  2889. }
  2890. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  2891. for i, tt := range tests {
  2892. stmt, err := NewParser(strings.NewReader(tt.s)).Parse()
  2893. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  2894. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.s, tt.err, err)
  2895. } else if tt.err == "" && !reflect.DeepEqual(tt.stmt, stmt) {
  2896. t.Errorf("%d. %q\n\nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.s, tt.stmt, stmt)
  2897. }
  2898. }
  2899. }
  2900. func TestParser_ParseJoins(t *testing.T) {
  2901. tests := []struct {
  2902. s string
  2903. stmt *ast.SelectStatement
  2904. err string
  2905. }{
  2906. {
  2907. s: `SELECT * FROM topic/sensor1 LEFT JOIN topic1 ON f=k`,
  2908. stmt: &ast.SelectStatement{
  2909. Fields: []ast.Field{
  2910. {
  2911. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  2912. Name: "*",
  2913. AName: "",
  2914. },
  2915. },
  2916. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  2917. Joins: []ast.Join{
  2918. {
  2919. Name: "topic1", Alias: "", JoinType: ast.LEFT_JOIN, Expr: &ast.BinaryExpr{
  2920. LHS: &ast.FieldRef{Name: "f", StreamName: ast.DefaultStream},
  2921. OP: ast.EQ,
  2922. RHS: &ast.FieldRef{Name: "k", StreamName: ast.DefaultStream},
  2923. },
  2924. },
  2925. },
  2926. },
  2927. },
  2928. {
  2929. s: `SELECT * FROM topic/sensor1 AS t1 INNER JOIN topic1 AS t2 ON f=k`,
  2930. stmt: &ast.SelectStatement{
  2931. Fields: []ast.Field{
  2932. {
  2933. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  2934. Name: "*",
  2935. AName: "",
  2936. },
  2937. },
  2938. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
  2939. Joins: []ast.Join{
  2940. {
  2941. Name: "topic1", Alias: "t2", JoinType: ast.INNER_JOIN, Expr: &ast.BinaryExpr{
  2942. LHS: &ast.FieldRef{Name: "f", StreamName: ast.DefaultStream},
  2943. OP: ast.EQ,
  2944. RHS: &ast.FieldRef{Name: "k", StreamName: ast.DefaultStream},
  2945. },
  2946. },
  2947. },
  2948. },
  2949. },
  2950. {
  2951. s: `SELECT * FROM topic/sensor1 AS t1 LEFT JOIN topic1/sensor2 AS t2 ON f=k`,
  2952. stmt: &ast.SelectStatement{
  2953. Fields: []ast.Field{
  2954. {
  2955. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  2956. Name: "*",
  2957. AName: "",
  2958. },
  2959. },
  2960. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
  2961. Joins: []ast.Join{
  2962. {
  2963. Name: "topic1/sensor2", Alias: "t2", JoinType: ast.LEFT_JOIN, Expr: &ast.BinaryExpr{
  2964. LHS: &ast.FieldRef{Name: "f", StreamName: ast.DefaultStream},
  2965. OP: ast.EQ,
  2966. RHS: &ast.FieldRef{Name: "k", StreamName: ast.DefaultStream},
  2967. },
  2968. },
  2969. },
  2970. },
  2971. },
  2972. {
  2973. s: `SELECT t1.name FROM topic/sensor1 AS t1 LEFT JOIN topic1/sensor2 AS t2 ON f=k`,
  2974. stmt: &ast.SelectStatement{
  2975. Fields: []ast.Field{
  2976. {
  2977. Expr: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "name"},
  2978. Name: "name",
  2979. AName: "",
  2980. },
  2981. },
  2982. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
  2983. Joins: []ast.Join{
  2984. {
  2985. Name: "topic1/sensor2", Alias: "t2", JoinType: ast.LEFT_JOIN, Expr: &ast.BinaryExpr{
  2986. LHS: &ast.FieldRef{Name: "f", StreamName: ast.DefaultStream},
  2987. OP: ast.EQ,
  2988. RHS: &ast.FieldRef{Name: "k", StreamName: ast.DefaultStream},
  2989. },
  2990. },
  2991. },
  2992. },
  2993. },
  2994. {
  2995. s: `SELECT t1.name FROM topic/sensor1 AS t1 LEFT JOIN topic1/sensor2 AS t2 ON t1.f=t2.k`,
  2996. stmt: &ast.SelectStatement{
  2997. Fields: []ast.Field{
  2998. {
  2999. Expr: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "name"},
  3000. Name: "name",
  3001. AName: "",
  3002. },
  3003. },
  3004. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
  3005. Joins: []ast.Join{
  3006. {
  3007. Name: "topic1/sensor2", Alias: "t2", JoinType: ast.LEFT_JOIN, Expr: &ast.BinaryExpr{
  3008. LHS: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "f"},
  3009. OP: ast.EQ,
  3010. RHS: &ast.FieldRef{StreamName: ast.StreamName("t2"), Name: "k"},
  3011. },
  3012. },
  3013. },
  3014. },
  3015. },
  3016. {
  3017. s: `SELECT t1.name FROM topic/sensor1 AS t1 RIGHT JOIN topic1/sensor2 AS t2 ON t1.f=t2.k`,
  3018. stmt: &ast.SelectStatement{
  3019. Fields: []ast.Field{
  3020. {
  3021. Expr: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "name"},
  3022. Name: "name",
  3023. AName: "",
  3024. },
  3025. },
  3026. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
  3027. Joins: []ast.Join{
  3028. {
  3029. Name: "topic1/sensor2", Alias: "t2", JoinType: ast.RIGHT_JOIN, Expr: &ast.BinaryExpr{
  3030. LHS: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "f"},
  3031. OP: ast.EQ,
  3032. RHS: &ast.FieldRef{StreamName: ast.StreamName("t2"), Name: "k"},
  3033. },
  3034. },
  3035. },
  3036. },
  3037. },
  3038. {
  3039. s: `SELECT t1.name FROM topic/sensor1 AS t1 FULL JOIN topic1/sensor2 AS t2 ON t1.f=t2.k`,
  3040. stmt: &ast.SelectStatement{
  3041. Fields: []ast.Field{
  3042. {
  3043. Expr: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "name"},
  3044. Name: "name",
  3045. AName: "",
  3046. },
  3047. },
  3048. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
  3049. Joins: []ast.Join{
  3050. {
  3051. Name: "topic1/sensor2", Alias: "t2", JoinType: ast.FULL_JOIN, Expr: &ast.BinaryExpr{
  3052. LHS: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "f"},
  3053. OP: ast.EQ,
  3054. RHS: &ast.FieldRef{StreamName: ast.StreamName("t2"), Name: "k"},
  3055. },
  3056. },
  3057. },
  3058. },
  3059. },
  3060. {
  3061. s: `SELECT t1.name FROM topic/sensor1 AS t1 CROSS JOIN topic1/sensor2 AS t2 ON t1.f=t2.k`,
  3062. stmt: nil,
  3063. err: "On expression is not required for cross join type.\n",
  3064. },
  3065. {
  3066. s: `SELECT t1.name FROM topic/sensor1 AS t1 CROSS JOIN topic1/sensor2 AS t2`,
  3067. stmt: &ast.SelectStatement{
  3068. Fields: []ast.Field{
  3069. {
  3070. Expr: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "name"},
  3071. Name: "name",
  3072. AName: "",
  3073. },
  3074. },
  3075. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
  3076. Joins: []ast.Join{
  3077. {
  3078. Name: "topic1/sensor2", Alias: "t2", JoinType: ast.CROSS_JOIN, Expr: nil,
  3079. },
  3080. },
  3081. },
  3082. },
  3083. {
  3084. s: `SELECT demo.*, demo2.* FROM demo LEFT JOIN demo2 on demo.f1 = demo2.f2`,
  3085. stmt: &ast.SelectStatement{
  3086. Fields: []ast.Field{
  3087. {
  3088. Expr: &ast.FieldRef{StreamName: ast.StreamName("demo"), Name: "*"},
  3089. Name: "*",
  3090. AName: "",
  3091. },
  3092. {
  3093. Expr: &ast.FieldRef{StreamName: ast.StreamName("demo2"), Name: "*"},
  3094. Name: "*",
  3095. AName: "",
  3096. },
  3097. },
  3098. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  3099. Joins: []ast.Join{
  3100. {
  3101. Name: "demo2", Alias: "", JoinType: ast.LEFT_JOIN, Expr: &ast.BinaryExpr{
  3102. LHS: &ast.FieldRef{StreamName: ast.StreamName("demo"), Name: "f1"},
  3103. OP: ast.EQ,
  3104. RHS: &ast.FieldRef{StreamName: ast.StreamName("demo2"), Name: "f2"},
  3105. },
  3106. },
  3107. },
  3108. },
  3109. },
  3110. }
  3111. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  3112. for i, tt := range tests {
  3113. stmt, err := NewParser(strings.NewReader(tt.s)).Parse()
  3114. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  3115. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.s, tt.err, err)
  3116. } else if tt.err == "" && !reflect.DeepEqual(tt.stmt, stmt) {
  3117. t.Errorf("%d. %q\n\nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.s, tt.stmt, stmt)
  3118. }
  3119. }
  3120. }
  3121. func TestParser_ParseStatements(t *testing.T) {
  3122. tests := []struct {
  3123. s string
  3124. stmts []ast.SelectStatement
  3125. err string
  3126. }{
  3127. {
  3128. s: "SELECT name FROM tbl;\nSELECT name FROM topic/sensor1\n",
  3129. stmts: []ast.SelectStatement{
  3130. {
  3131. Fields: []ast.Field{
  3132. {
  3133. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  3134. Name: "name",
  3135. AName: "",
  3136. },
  3137. },
  3138. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  3139. },
  3140. {
  3141. Fields: []ast.Field{
  3142. {
  3143. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  3144. Name: "name",
  3145. AName: "",
  3146. },
  3147. },
  3148. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  3149. },
  3150. },
  3151. },
  3152. {
  3153. s: "SELECT name FROM tbl;\nSELECT name FROM topic/sensor1;\n--SELECT comment FROM topic/comment",
  3154. stmts: []ast.SelectStatement{
  3155. {
  3156. Fields: []ast.Field{
  3157. {
  3158. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  3159. Name: "name",
  3160. AName: "",
  3161. },
  3162. },
  3163. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  3164. },
  3165. {
  3166. Fields: []ast.Field{
  3167. {
  3168. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  3169. Name: "name",
  3170. AName: "",
  3171. },
  3172. },
  3173. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  3174. },
  3175. },
  3176. },
  3177. }
  3178. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  3179. for i, tt := range tests {
  3180. stmts, err := NewParser(strings.NewReader(tt.s)).ParseQueries()
  3181. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  3182. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.s, tt.err, err)
  3183. } else if tt.err == "" && !reflect.DeepEqual(tt.stmts, stmts) {
  3184. t.Errorf("%d. %q\n\nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.s, tt.stmts, stmts)
  3185. }
  3186. }
  3187. }