parser_test.go 86 KB

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