parser_test.go 116 KB

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