parser_test.go 116 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086
  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 "abc' FROM tbl`,
  2866. stmt: nil,
  2867. err: `found "\"abc' FROM tbl", expected expression.`,
  2868. },
  2869. }
  2870. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  2871. for i, tt := range tests {
  2872. // fmt.Printf("Parsing SQL %q.\n", tt.s)
  2873. stmt, err := NewParser(strings.NewReader(tt.s)).Parse()
  2874. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  2875. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.s, tt.err, err)
  2876. } else if tt.err == "" && !reflect.DeepEqual(tt.stmt, stmt) {
  2877. t.Errorf("%d. %q\n\nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.s, tt.stmt, stmt)
  2878. }
  2879. }
  2880. }
  2881. func TestParser_ParseWindowsExpr(t *testing.T) {
  2882. tests := []struct {
  2883. s string
  2884. stmt *ast.SelectStatement
  2885. err string
  2886. }{
  2887. {
  2888. s: `SELECT f1 FROM tbl GROUP BY TUMBLINGWINDOW(ss, 10)`,
  2889. stmt: &ast.SelectStatement{
  2890. Fields: []ast.Field{
  2891. {
  2892. Expr: &ast.FieldRef{Name: "f1", StreamName: ast.DefaultStream},
  2893. Name: "f1",
  2894. AName: "",
  2895. },
  2896. },
  2897. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  2898. Dimensions: ast.Dimensions{
  2899. ast.Dimension{
  2900. Expr: &ast.Window{
  2901. WindowType: ast.TUMBLING_WINDOW,
  2902. Length: &ast.IntegerLiteral{Val: 10000},
  2903. Interval: &ast.IntegerLiteral{Val: 0},
  2904. },
  2905. },
  2906. },
  2907. },
  2908. },
  2909. {
  2910. s: `SELECT f1 FROM tbl GROUP BY HOPPINGWINDOW(mi, 5, 1)`,
  2911. stmt: &ast.SelectStatement{
  2912. Fields: []ast.Field{
  2913. {
  2914. Expr: &ast.FieldRef{Name: "f1", StreamName: ast.DefaultStream},
  2915. Name: "f1",
  2916. AName: "",
  2917. },
  2918. },
  2919. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  2920. Dimensions: ast.Dimensions{
  2921. ast.Dimension{
  2922. Expr: &ast.Window{
  2923. WindowType: ast.HOPPING_WINDOW,
  2924. Length: &ast.IntegerLiteral{Val: 3e5},
  2925. Interval: &ast.IntegerLiteral{Val: 6e4},
  2926. },
  2927. },
  2928. },
  2929. },
  2930. },
  2931. {
  2932. s: `SELECT f1 FROM tbl GROUP BY SESSIONWINDOW(hh, 5, 1)`,
  2933. stmt: &ast.SelectStatement{
  2934. Fields: []ast.Field{
  2935. {
  2936. Expr: &ast.FieldRef{Name: "f1", StreamName: ast.DefaultStream},
  2937. Name: "f1",
  2938. AName: "",
  2939. },
  2940. },
  2941. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  2942. Dimensions: ast.Dimensions{
  2943. ast.Dimension{
  2944. Expr: &ast.Window{
  2945. WindowType: ast.SESSION_WINDOW,
  2946. Length: &ast.IntegerLiteral{Val: 1.8e7},
  2947. Interval: &ast.IntegerLiteral{Val: 3.6e6},
  2948. },
  2949. },
  2950. },
  2951. },
  2952. },
  2953. {
  2954. s: `SELECT f1 FROM tbl GROUP BY SLIDINGWINDOW(ms, 5)`,
  2955. stmt: &ast.SelectStatement{
  2956. Fields: []ast.Field{
  2957. {
  2958. Expr: &ast.FieldRef{Name: "f1", StreamName: ast.DefaultStream},
  2959. Name: "f1",
  2960. AName: "",
  2961. },
  2962. },
  2963. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  2964. Dimensions: ast.Dimensions{
  2965. ast.Dimension{
  2966. Expr: &ast.Window{
  2967. WindowType: ast.SLIDING_WINDOW,
  2968. Length: &ast.IntegerLiteral{Val: 5},
  2969. Interval: &ast.IntegerLiteral{Val: 0},
  2970. },
  2971. },
  2972. },
  2973. },
  2974. },
  2975. {
  2976. s: `SELECT f1 FROM tbl GROUP BY SLIDINGWINDOW(mi, 5, 1)`,
  2977. stmt: nil,
  2978. err: "The arguments for slidingwindow should be 2.\n",
  2979. },
  2980. {
  2981. s: `SELECT f1 FROM tbl GROUP BY SLIDINGWINDOW("mi", 5)`,
  2982. stmt: nil,
  2983. err: "The 1st argument for slidingwindow is expecting timer literal expression. One value of [dd|hh|mi|ss|ms].\n",
  2984. },
  2985. {
  2986. s: `SELECT f1 FROM tbl GROUP BY COUNTWINDOW(10)`,
  2987. stmt: &ast.SelectStatement{
  2988. Fields: []ast.Field{
  2989. {
  2990. Expr: &ast.FieldRef{Name: "f1", StreamName: ast.DefaultStream},
  2991. Name: "f1",
  2992. AName: "",
  2993. },
  2994. },
  2995. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  2996. Dimensions: ast.Dimensions{
  2997. ast.Dimension{
  2998. Expr: &ast.Window{
  2999. WindowType: ast.COUNT_WINDOW,
  3000. Length: &ast.IntegerLiteral{Val: 10},
  3001. },
  3002. },
  3003. },
  3004. },
  3005. },
  3006. {
  3007. s: `SELECT f1 FROM tbl GROUP BY COUNTWINDOW(10, 5)`,
  3008. stmt: &ast.SelectStatement{
  3009. Fields: []ast.Field{
  3010. {
  3011. Expr: &ast.FieldRef{Name: "f1", StreamName: ast.DefaultStream},
  3012. Name: "f1",
  3013. AName: "",
  3014. },
  3015. },
  3016. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  3017. Dimensions: ast.Dimensions{
  3018. ast.Dimension{
  3019. Expr: &ast.Window{
  3020. WindowType: ast.COUNT_WINDOW,
  3021. Length: &ast.IntegerLiteral{Val: 10},
  3022. Interval: &ast.IntegerLiteral{Val: 5},
  3023. },
  3024. },
  3025. },
  3026. },
  3027. },
  3028. {
  3029. s: `SELECT f1 FROM tbl GROUP BY COUNTWINDOW(3, 5)`,
  3030. stmt: nil,
  3031. err: "The second parameter value 5 should be less than the first parameter 3.",
  3032. },
  3033. {
  3034. s: `SELECT * FROM demo GROUP BY COUNTWINDOW(3,1) FILTER( where revenue > 100 )`,
  3035. stmt: &ast.SelectStatement{
  3036. Fields: []ast.Field{
  3037. {
  3038. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  3039. Name: "*",
  3040. AName: "",
  3041. },
  3042. },
  3043. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  3044. Dimensions: ast.Dimensions{
  3045. ast.Dimension{
  3046. Expr: &ast.Window{
  3047. WindowType: ast.COUNT_WINDOW,
  3048. Length: &ast.IntegerLiteral{Val: 3},
  3049. Interval: &ast.IntegerLiteral{Val: 1},
  3050. Filter: &ast.BinaryExpr{
  3051. LHS: &ast.FieldRef{Name: "revenue", StreamName: ast.DefaultStream},
  3052. OP: ast.GT,
  3053. RHS: &ast.IntegerLiteral{Val: 100},
  3054. },
  3055. },
  3056. },
  3057. },
  3058. },
  3059. },
  3060. {
  3061. s: `SELECT * FROM demo GROUP BY department, COUNTWINDOW(3,1) FILTER( where revenue > 100 ), year`,
  3062. stmt: &ast.SelectStatement{
  3063. Fields: []ast.Field{
  3064. {
  3065. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  3066. Name: "*",
  3067. AName: "",
  3068. },
  3069. },
  3070. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  3071. Dimensions: ast.Dimensions{
  3072. ast.Dimension{Expr: &ast.FieldRef{Name: "department", StreamName: ast.DefaultStream}},
  3073. ast.Dimension{
  3074. Expr: &ast.Window{
  3075. WindowType: ast.COUNT_WINDOW,
  3076. Length: &ast.IntegerLiteral{Val: 3},
  3077. Interval: &ast.IntegerLiteral{Val: 1},
  3078. Filter: &ast.BinaryExpr{
  3079. LHS: &ast.FieldRef{Name: "revenue", StreamName: ast.DefaultStream},
  3080. OP: ast.GT,
  3081. RHS: &ast.IntegerLiteral{Val: 100},
  3082. },
  3083. },
  3084. },
  3085. ast.Dimension{Expr: &ast.FieldRef{Name: "year", StreamName: ast.DefaultStream}},
  3086. },
  3087. },
  3088. },
  3089. {
  3090. s: `SELECT * FROM demo GROUP BY department, COUNTWINDOW(3,1) FILTER( where revenue IN (100, 200)), year`,
  3091. stmt: &ast.SelectStatement{
  3092. Fields: []ast.Field{
  3093. {
  3094. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  3095. Name: "*",
  3096. AName: "",
  3097. },
  3098. },
  3099. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  3100. Dimensions: ast.Dimensions{
  3101. ast.Dimension{Expr: &ast.FieldRef{Name: "department", StreamName: ast.DefaultStream}},
  3102. ast.Dimension{
  3103. Expr: &ast.Window{
  3104. WindowType: ast.COUNT_WINDOW,
  3105. Length: &ast.IntegerLiteral{Val: 3},
  3106. Interval: &ast.IntegerLiteral{Val: 1},
  3107. Filter: &ast.BinaryExpr{
  3108. LHS: &ast.FieldRef{Name: "revenue", StreamName: ast.DefaultStream},
  3109. OP: ast.IN,
  3110. RHS: &ast.ValueSetExpr{
  3111. LiteralExprs: []ast.Expr{&ast.IntegerLiteral{Val: 100}, &ast.IntegerLiteral{Val: 200}},
  3112. },
  3113. },
  3114. },
  3115. },
  3116. ast.Dimension{Expr: &ast.FieldRef{Name: "year", StreamName: ast.DefaultStream}},
  3117. },
  3118. },
  3119. },
  3120. // to be supported
  3121. {
  3122. s: `SELECT sum(f1) FILTER( where revenue > 100 ) FROM tbl GROUP BY year`,
  3123. stmt: nil,
  3124. err: "found \"FILTER\", expected FROM.",
  3125. },
  3126. {
  3127. s: `SELECT * FROM demo GROUP BY COUNTWINDOW(3,1) FILTER where revenue > 100`,
  3128. stmt: nil,
  3129. err: "Found \"WHERE\" after FILTER, expect parentheses.",
  3130. },
  3131. {
  3132. s: `SELECT * FROM demo GROUP BY COUNTWINDOW(3,1) where revenue > 100`,
  3133. stmt: nil,
  3134. err: "found \"WHERE\", expected EOF.",
  3135. },
  3136. }
  3137. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  3138. for i, tt := range tests {
  3139. // fmt.Printf("Parsing SQL %q.\n", tt.s)
  3140. stmt, err := NewParser(strings.NewReader(tt.s)).Parse()
  3141. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  3142. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.s, tt.err, err)
  3143. } else if tt.err == "" && !reflect.DeepEqual(tt.stmt, stmt) {
  3144. t.Errorf("%d. %q\n\nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.s, tt.stmt, stmt)
  3145. }
  3146. }
  3147. }
  3148. func TestParser_ParseJsonExpr(t *testing.T) {
  3149. tests := []struct {
  3150. s string
  3151. stmt *ast.SelectStatement
  3152. err string
  3153. }{
  3154. {
  3155. s: `SELECT children[0] FROM demo`,
  3156. stmt: &ast.SelectStatement{
  3157. Fields: []ast.Field{
  3158. {
  3159. Expr: &ast.BinaryExpr{
  3160. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  3161. OP: ast.SUBSET,
  3162. RHS: &ast.IndexExpr{Index: &ast.IntegerLiteral{Val: 0}},
  3163. },
  3164. Name: "kuiper_field_0",
  3165. AName: "",
  3166. },
  3167. },
  3168. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  3169. },
  3170. },
  3171. {
  3172. s: `SELECT children[0]->first FROM demo`,
  3173. stmt: &ast.SelectStatement{
  3174. Fields: []ast.Field{
  3175. {
  3176. Expr: &ast.BinaryExpr{
  3177. LHS: &ast.BinaryExpr{
  3178. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  3179. OP: ast.SUBSET,
  3180. RHS: &ast.IndexExpr{Index: &ast.IntegerLiteral{Val: 0}},
  3181. },
  3182. OP: ast.ARROW,
  3183. RHS: &ast.JsonFieldRef{Name: "first"},
  3184. },
  3185. Name: "kuiper_field_0",
  3186. AName: "",
  3187. },
  3188. },
  3189. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  3190. },
  3191. },
  3192. {
  3193. s: `SELECT children->first[2] FROM demo`,
  3194. stmt: &ast.SelectStatement{
  3195. Fields: []ast.Field{
  3196. {
  3197. Expr: &ast.BinaryExpr{
  3198. LHS: &ast.BinaryExpr{
  3199. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  3200. OP: ast.ARROW,
  3201. RHS: &ast.JsonFieldRef{Name: "first"},
  3202. },
  3203. OP: ast.SUBSET,
  3204. RHS: &ast.IndexExpr{Index: &ast.IntegerLiteral{Val: 2}},
  3205. },
  3206. Name: "kuiper_field_0",
  3207. AName: "",
  3208. },
  3209. },
  3210. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  3211. },
  3212. },
  3213. {
  3214. s: `SELECT children->first[2]->test FROM demo`,
  3215. stmt: &ast.SelectStatement{
  3216. Fields: []ast.Field{
  3217. {
  3218. Expr: &ast.BinaryExpr{
  3219. LHS: &ast.BinaryExpr{
  3220. LHS: &ast.BinaryExpr{
  3221. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  3222. OP: ast.ARROW,
  3223. RHS: &ast.JsonFieldRef{Name: "first"},
  3224. },
  3225. OP: ast.SUBSET,
  3226. RHS: &ast.IndexExpr{Index: &ast.IntegerLiteral{Val: 2}},
  3227. },
  3228. OP: ast.ARROW,
  3229. RHS: &ast.JsonFieldRef{Name: "test"},
  3230. },
  3231. Name: "kuiper_field_0",
  3232. AName: "",
  3233. },
  3234. },
  3235. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  3236. },
  3237. },
  3238. {
  3239. s: `SELECT demo.children->first->test FROM demo`,
  3240. stmt: &ast.SelectStatement{
  3241. Fields: []ast.Field{
  3242. {
  3243. Expr: &ast.BinaryExpr{
  3244. LHS: &ast.BinaryExpr{
  3245. LHS: &ast.FieldRef{Name: "children", StreamName: "demo"},
  3246. OP: ast.ARROW,
  3247. RHS: &ast.JsonFieldRef{Name: "first"},
  3248. },
  3249. OP: ast.ARROW,
  3250. RHS: &ast.JsonFieldRef{Name: "test"},
  3251. },
  3252. Name: "kuiper_field_0",
  3253. AName: "",
  3254. },
  3255. },
  3256. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  3257. },
  3258. },
  3259. {
  3260. s: `SELECT demo.children.first.test FROM demo`,
  3261. stmt: &ast.SelectStatement{
  3262. Fields: []ast.Field{
  3263. {
  3264. Expr: &ast.BinaryExpr{
  3265. LHS: &ast.BinaryExpr{
  3266. LHS: &ast.FieldRef{Name: "children", StreamName: "demo"},
  3267. OP: ast.ARROW,
  3268. RHS: &ast.JsonFieldRef{Name: "first"},
  3269. },
  3270. OP: ast.ARROW,
  3271. RHS: &ast.JsonFieldRef{Name: "test"},
  3272. },
  3273. Name: "kuiper_field_0",
  3274. AName: "",
  3275. },
  3276. },
  3277. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  3278. },
  3279. },
  3280. {
  3281. s: `SELECT demo.children.first->test FROM demo`,
  3282. stmt: &ast.SelectStatement{
  3283. Fields: []ast.Field{
  3284. {
  3285. Expr: &ast.BinaryExpr{
  3286. LHS: &ast.BinaryExpr{
  3287. LHS: &ast.FieldRef{Name: "children", StreamName: "demo"},
  3288. OP: ast.ARROW,
  3289. RHS: &ast.JsonFieldRef{Name: "first"},
  3290. },
  3291. OP: ast.ARROW,
  3292. RHS: &ast.JsonFieldRef{Name: "test"},
  3293. },
  3294. Name: "kuiper_field_0",
  3295. AName: "",
  3296. },
  3297. },
  3298. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  3299. },
  3300. },
  3301. {
  3302. s: `SELECT demo.children->first.test FROM demo`,
  3303. stmt: &ast.SelectStatement{
  3304. Fields: []ast.Field{
  3305. {
  3306. Expr: &ast.BinaryExpr{
  3307. LHS: &ast.BinaryExpr{
  3308. LHS: &ast.FieldRef{Name: "children", StreamName: "demo"},
  3309. OP: ast.ARROW,
  3310. RHS: &ast.JsonFieldRef{Name: "first"},
  3311. },
  3312. OP: ast.ARROW,
  3313. RHS: &ast.JsonFieldRef{Name: "test"},
  3314. },
  3315. Name: "kuiper_field_0",
  3316. AName: "",
  3317. },
  3318. },
  3319. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  3320. },
  3321. },
  3322. {
  3323. s: `SELECT children[0:1] FROM demo`,
  3324. stmt: &ast.SelectStatement{
  3325. Fields: []ast.Field{
  3326. {
  3327. Expr: &ast.BinaryExpr{
  3328. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  3329. OP: ast.SUBSET,
  3330. RHS: &ast.ColonExpr{Start: &ast.IntegerLiteral{Val: 0}, End: &ast.IntegerLiteral{Val: 1}},
  3331. },
  3332. Name: "kuiper_field_0",
  3333. AName: "",
  3334. },
  3335. },
  3336. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  3337. },
  3338. },
  3339. {
  3340. s: `SELECT children[:1] FROM demo`,
  3341. stmt: &ast.SelectStatement{
  3342. Fields: []ast.Field{
  3343. {
  3344. Expr: &ast.BinaryExpr{
  3345. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  3346. OP: ast.SUBSET,
  3347. RHS: &ast.ColonExpr{Start: &ast.IntegerLiteral{Val: 0}, End: &ast.IntegerLiteral{Val: 1}},
  3348. },
  3349. Name: "kuiper_field_0",
  3350. AName: "",
  3351. },
  3352. },
  3353. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  3354. },
  3355. },
  3356. {
  3357. s: `SELECT children[:] FROM demo`,
  3358. stmt: &ast.SelectStatement{
  3359. Fields: []ast.Field{
  3360. {
  3361. Expr: &ast.BinaryExpr{
  3362. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  3363. OP: ast.SUBSET,
  3364. RHS: &ast.ColonExpr{Start: &ast.IntegerLiteral{Val: 0}, End: &ast.IntegerLiteral{Val: math.MinInt32}},
  3365. },
  3366. Name: "kuiper_field_0",
  3367. AName: "",
  3368. },
  3369. },
  3370. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  3371. },
  3372. },
  3373. {
  3374. s: `SELECT children[2:] AS c FROM demo`,
  3375. stmt: &ast.SelectStatement{
  3376. Fields: []ast.Field{
  3377. {
  3378. Expr: &ast.BinaryExpr{
  3379. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  3380. OP: ast.SUBSET,
  3381. RHS: &ast.ColonExpr{Start: &ast.IntegerLiteral{Val: 2}, End: &ast.IntegerLiteral{Val: math.MinInt32}},
  3382. },
  3383. Name: "",
  3384. AName: "c",
  3385. },
  3386. },
  3387. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  3388. },
  3389. },
  3390. {
  3391. s: `SELECT children[2:]->first AS c FROM demo`,
  3392. stmt: &ast.SelectStatement{
  3393. Fields: []ast.Field{
  3394. {
  3395. Expr: &ast.BinaryExpr{
  3396. 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}}},
  3397. OP: ast.ARROW,
  3398. RHS: &ast.JsonFieldRef{Name: "first"},
  3399. },
  3400. Name: "",
  3401. AName: "c",
  3402. },
  3403. },
  3404. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  3405. },
  3406. },
  3407. {
  3408. s: `SELECT demo.* FROM demo`,
  3409. stmt: &ast.SelectStatement{
  3410. Fields: []ast.Field{
  3411. {
  3412. Expr: &ast.FieldRef{StreamName: ast.StreamName("demo"), Name: "*"},
  3413. Name: "*",
  3414. AName: "",
  3415. },
  3416. },
  3417. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  3418. },
  3419. },
  3420. {
  3421. s: `SELECT demo.children[2:]->first AS c FROM demo`,
  3422. stmt: &ast.SelectStatement{
  3423. Fields: []ast.Field{
  3424. {
  3425. Expr: &ast.BinaryExpr{
  3426. 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}}},
  3427. OP: ast.ARROW,
  3428. RHS: &ast.JsonFieldRef{Name: "first"},
  3429. },
  3430. Name: "",
  3431. AName: "c",
  3432. },
  3433. },
  3434. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  3435. },
  3436. },
  3437. {
  3438. s: `SELECT lower(demo.children[2:]->first) AS c FROM demo`,
  3439. stmt: &ast.SelectStatement{
  3440. Fields: []ast.Field{
  3441. {
  3442. Expr: &ast.Call{
  3443. Name: "lower",
  3444. Args: []ast.Expr{
  3445. &ast.BinaryExpr{
  3446. 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}}},
  3447. OP: ast.ARROW,
  3448. RHS: &ast.JsonFieldRef{Name: "first"},
  3449. },
  3450. },
  3451. },
  3452. Name: "lower",
  3453. AName: "c",
  3454. },
  3455. },
  3456. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  3457. },
  3458. },
  3459. {
  3460. s: `SELECT children[:1] FROM demo WHERE abc[0] > 12`,
  3461. stmt: &ast.SelectStatement{
  3462. Fields: []ast.Field{
  3463. {
  3464. Expr: &ast.BinaryExpr{
  3465. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  3466. OP: ast.SUBSET,
  3467. RHS: &ast.ColonExpr{Start: &ast.IntegerLiteral{Val: 0}, End: &ast.IntegerLiteral{Val: 1}},
  3468. },
  3469. Name: "kuiper_field_0",
  3470. AName: "",
  3471. },
  3472. },
  3473. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  3474. Condition: &ast.BinaryExpr{
  3475. LHS: &ast.BinaryExpr{
  3476. LHS: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream},
  3477. OP: ast.SUBSET,
  3478. RHS: &ast.IndexExpr{Index: &ast.IntegerLiteral{Val: 0}},
  3479. },
  3480. OP: ast.GT,
  3481. RHS: &ast.IntegerLiteral{Val: 12},
  3482. },
  3483. },
  3484. },
  3485. {
  3486. s: `SELECT children[:1] FROM demo WHERE abc[0] IN demo.children[2:].first`,
  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.IN,
  3507. RHS: &ast.BinaryExpr{
  3508. 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}}},
  3509. OP: ast.ARROW,
  3510. RHS: &ast.JsonFieldRef{Name: "first"},
  3511. },
  3512. },
  3513. },
  3514. },
  3515. {
  3516. s: `SELECT children[:1] FROM demo WHERE abc[0] IN children[2:].first`,
  3517. stmt: &ast.SelectStatement{
  3518. Fields: []ast.Field{
  3519. {
  3520. Expr: &ast.BinaryExpr{
  3521. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  3522. OP: ast.SUBSET,
  3523. RHS: &ast.ColonExpr{Start: &ast.IntegerLiteral{Val: 0}, End: &ast.IntegerLiteral{Val: 1}},
  3524. },
  3525. Name: "kuiper_field_0",
  3526. AName: "",
  3527. },
  3528. },
  3529. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  3530. Condition: &ast.BinaryExpr{
  3531. LHS: &ast.BinaryExpr{
  3532. LHS: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream},
  3533. OP: ast.SUBSET,
  3534. RHS: &ast.IndexExpr{Index: &ast.IntegerLiteral{Val: 0}},
  3535. },
  3536. OP: ast.IN,
  3537. RHS: &ast.BinaryExpr{
  3538. 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}}},
  3539. OP: ast.ARROW,
  3540. RHS: &ast.JsonFieldRef{Name: "first"},
  3541. },
  3542. },
  3543. },
  3544. },
  3545. {
  3546. s: `SELECT children[:1] FROM demo WHERE abc[0] IN demo.children[2:]->first`,
  3547. stmt: &ast.SelectStatement{
  3548. Fields: []ast.Field{
  3549. {
  3550. Expr: &ast.BinaryExpr{
  3551. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  3552. OP: ast.SUBSET,
  3553. RHS: &ast.ColonExpr{Start: &ast.IntegerLiteral{Val: 0}, End: &ast.IntegerLiteral{Val: 1}},
  3554. },
  3555. Name: "kuiper_field_0",
  3556. AName: "",
  3557. },
  3558. },
  3559. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  3560. Condition: &ast.BinaryExpr{
  3561. LHS: &ast.BinaryExpr{
  3562. LHS: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream},
  3563. OP: ast.SUBSET,
  3564. RHS: &ast.IndexExpr{Index: &ast.IntegerLiteral{Val: 0}},
  3565. },
  3566. OP: ast.IN,
  3567. RHS: &ast.BinaryExpr{
  3568. 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}}},
  3569. OP: ast.ARROW,
  3570. RHS: &ast.JsonFieldRef{Name: "first"},
  3571. },
  3572. },
  3573. },
  3574. },
  3575. {
  3576. s: `SELECT demo.children.first AS c FROM demo`,
  3577. stmt: &ast.SelectStatement{
  3578. Fields: []ast.Field{
  3579. {
  3580. Expr: &ast.BinaryExpr{
  3581. LHS: &ast.FieldRef{Name: "children", StreamName: "demo"},
  3582. OP: ast.ARROW,
  3583. RHS: &ast.JsonFieldRef{Name: "first"},
  3584. },
  3585. Name: "",
  3586. AName: "c",
  3587. },
  3588. },
  3589. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  3590. },
  3591. },
  3592. {
  3593. s: `SELECT children[index] FROM demo`,
  3594. stmt: &ast.SelectStatement{
  3595. Fields: []ast.Field{
  3596. {
  3597. Expr: &ast.BinaryExpr{
  3598. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  3599. OP: ast.SUBSET,
  3600. RHS: &ast.IndexExpr{Index: &ast.FieldRef{Name: "index", StreamName: ast.DefaultStream}},
  3601. },
  3602. Name: "kuiper_field_0",
  3603. AName: "",
  3604. },
  3605. },
  3606. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  3607. },
  3608. },
  3609. }
  3610. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  3611. for i, tt := range tests {
  3612. stmt, err := NewParser(strings.NewReader(tt.s)).Parse()
  3613. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  3614. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.s, tt.err, err)
  3615. } else if tt.err == "" && !reflect.DeepEqual(tt.stmt, stmt) {
  3616. t.Errorf("%d. %q\n\nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.s, tt.stmt, stmt)
  3617. }
  3618. }
  3619. }
  3620. func TestParser_ParseJoins(t *testing.T) {
  3621. tests := []struct {
  3622. s string
  3623. stmt *ast.SelectStatement
  3624. err string
  3625. }{
  3626. {
  3627. s: `SELECT * FROM topic/sensor1 LEFT JOIN topic1 ON f=k`,
  3628. stmt: &ast.SelectStatement{
  3629. Fields: []ast.Field{
  3630. {
  3631. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  3632. Name: "*",
  3633. AName: "",
  3634. },
  3635. },
  3636. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  3637. Joins: []ast.Join{
  3638. {
  3639. Name: "topic1", Alias: "", JoinType: ast.LEFT_JOIN, Expr: &ast.BinaryExpr{
  3640. LHS: &ast.FieldRef{Name: "f", StreamName: ast.DefaultStream},
  3641. OP: ast.EQ,
  3642. RHS: &ast.FieldRef{Name: "k", StreamName: ast.DefaultStream},
  3643. },
  3644. },
  3645. },
  3646. },
  3647. },
  3648. {
  3649. s: `SELECT * FROM topic/sensor1 AS t1 INNER JOIN topic1 AS t2 ON f=k`,
  3650. stmt: &ast.SelectStatement{
  3651. Fields: []ast.Field{
  3652. {
  3653. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  3654. Name: "*",
  3655. AName: "",
  3656. },
  3657. },
  3658. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
  3659. Joins: []ast.Join{
  3660. {
  3661. Name: "topic1", Alias: "t2", JoinType: ast.INNER_JOIN, Expr: &ast.BinaryExpr{
  3662. LHS: &ast.FieldRef{Name: "f", StreamName: ast.DefaultStream},
  3663. OP: ast.EQ,
  3664. RHS: &ast.FieldRef{Name: "k", StreamName: ast.DefaultStream},
  3665. },
  3666. },
  3667. },
  3668. },
  3669. },
  3670. {
  3671. s: `SELECT * FROM topic/sensor1 AS t1 LEFT JOIN topic1/sensor2 AS t2 ON f=k`,
  3672. stmt: &ast.SelectStatement{
  3673. Fields: []ast.Field{
  3674. {
  3675. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  3676. Name: "*",
  3677. AName: "",
  3678. },
  3679. },
  3680. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
  3681. Joins: []ast.Join{
  3682. {
  3683. Name: "topic1/sensor2", Alias: "t2", JoinType: ast.LEFT_JOIN, Expr: &ast.BinaryExpr{
  3684. LHS: &ast.FieldRef{Name: "f", StreamName: ast.DefaultStream},
  3685. OP: ast.EQ,
  3686. RHS: &ast.FieldRef{Name: "k", StreamName: ast.DefaultStream},
  3687. },
  3688. },
  3689. },
  3690. },
  3691. },
  3692. {
  3693. s: `SELECT t1.name FROM topic/sensor1 AS t1 LEFT JOIN topic1/sensor2 AS t2 ON f=k`,
  3694. stmt: &ast.SelectStatement{
  3695. Fields: []ast.Field{
  3696. {
  3697. Expr: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "name"},
  3698. Name: "name",
  3699. AName: "",
  3700. },
  3701. },
  3702. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
  3703. Joins: []ast.Join{
  3704. {
  3705. Name: "topic1/sensor2", Alias: "t2", JoinType: ast.LEFT_JOIN, Expr: &ast.BinaryExpr{
  3706. LHS: &ast.FieldRef{Name: "f", StreamName: ast.DefaultStream},
  3707. OP: ast.EQ,
  3708. RHS: &ast.FieldRef{Name: "k", StreamName: ast.DefaultStream},
  3709. },
  3710. },
  3711. },
  3712. },
  3713. },
  3714. {
  3715. s: `SELECT t1.name FROM topic/sensor1 AS t1 LEFT JOIN topic1/sensor2 AS t2 ON t1.f=t2.k`,
  3716. stmt: &ast.SelectStatement{
  3717. Fields: []ast.Field{
  3718. {
  3719. Expr: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "name"},
  3720. Name: "name",
  3721. AName: "",
  3722. },
  3723. },
  3724. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
  3725. Joins: []ast.Join{
  3726. {
  3727. Name: "topic1/sensor2", Alias: "t2", JoinType: ast.LEFT_JOIN, Expr: &ast.BinaryExpr{
  3728. LHS: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "f"},
  3729. OP: ast.EQ,
  3730. RHS: &ast.FieldRef{StreamName: ast.StreamName("t2"), Name: "k"},
  3731. },
  3732. },
  3733. },
  3734. },
  3735. },
  3736. {
  3737. s: `SELECT t1.name FROM topic/sensor1 AS t1 RIGHT JOIN topic1/sensor2 AS t2 ON t1.f=t2.k`,
  3738. stmt: &ast.SelectStatement{
  3739. Fields: []ast.Field{
  3740. {
  3741. Expr: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "name"},
  3742. Name: "name",
  3743. AName: "",
  3744. },
  3745. },
  3746. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
  3747. Joins: []ast.Join{
  3748. {
  3749. Name: "topic1/sensor2", Alias: "t2", JoinType: ast.RIGHT_JOIN, Expr: &ast.BinaryExpr{
  3750. LHS: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "f"},
  3751. OP: ast.EQ,
  3752. RHS: &ast.FieldRef{StreamName: ast.StreamName("t2"), Name: "k"},
  3753. },
  3754. },
  3755. },
  3756. },
  3757. },
  3758. {
  3759. s: `SELECT t1.name FROM topic/sensor1 AS t1 FULL JOIN topic1/sensor2 AS t2 ON t1.f=t2.k`,
  3760. stmt: &ast.SelectStatement{
  3761. Fields: []ast.Field{
  3762. {
  3763. Expr: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "name"},
  3764. Name: "name",
  3765. AName: "",
  3766. },
  3767. },
  3768. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
  3769. Joins: []ast.Join{
  3770. {
  3771. Name: "topic1/sensor2", Alias: "t2", JoinType: ast.FULL_JOIN, Expr: &ast.BinaryExpr{
  3772. LHS: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "f"},
  3773. OP: ast.EQ,
  3774. RHS: &ast.FieldRef{StreamName: ast.StreamName("t2"), Name: "k"},
  3775. },
  3776. },
  3777. },
  3778. },
  3779. },
  3780. {
  3781. s: `SELECT t1.name FROM topic/sensor1 AS t1 CROSS JOIN topic1/sensor2 AS t2 ON t1.f=t2.k`,
  3782. stmt: nil,
  3783. err: "On expression is not required for cross join type.\n",
  3784. },
  3785. {
  3786. s: `SELECT t1.name FROM topic/sensor1 AS t1 CROSS JOIN topic1/sensor2 AS t2`,
  3787. stmt: &ast.SelectStatement{
  3788. Fields: []ast.Field{
  3789. {
  3790. Expr: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "name"},
  3791. Name: "name",
  3792. AName: "",
  3793. },
  3794. },
  3795. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
  3796. Joins: []ast.Join{
  3797. {
  3798. Name: "topic1/sensor2", Alias: "t2", JoinType: ast.CROSS_JOIN, Expr: nil,
  3799. },
  3800. },
  3801. },
  3802. },
  3803. {
  3804. s: `SELECT demo.*, demo2.* FROM demo LEFT JOIN demo2 on demo.f1 = demo2.f2`,
  3805. stmt: &ast.SelectStatement{
  3806. Fields: []ast.Field{
  3807. {
  3808. Expr: &ast.FieldRef{StreamName: ast.StreamName("demo"), Name: "*"},
  3809. Name: "*",
  3810. AName: "",
  3811. },
  3812. {
  3813. Expr: &ast.FieldRef{StreamName: ast.StreamName("demo2"), Name: "*"},
  3814. Name: "*",
  3815. AName: "",
  3816. },
  3817. },
  3818. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  3819. Joins: []ast.Join{
  3820. {
  3821. Name: "demo2", Alias: "", JoinType: ast.LEFT_JOIN, Expr: &ast.BinaryExpr{
  3822. LHS: &ast.FieldRef{StreamName: ast.StreamName("demo"), Name: "f1"},
  3823. OP: ast.EQ,
  3824. RHS: &ast.FieldRef{StreamName: ast.StreamName("demo2"), Name: "f2"},
  3825. },
  3826. },
  3827. },
  3828. },
  3829. },
  3830. }
  3831. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  3832. for i, tt := range tests {
  3833. stmt, err := NewParser(strings.NewReader(tt.s)).Parse()
  3834. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  3835. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.s, tt.err, err)
  3836. } else if tt.err == "" && !reflect.DeepEqual(tt.stmt, stmt) {
  3837. t.Errorf("%d. %q\n\nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.s, tt.stmt, stmt)
  3838. }
  3839. }
  3840. }
  3841. func TestParser_ParseStatements(t *testing.T) {
  3842. tests := []struct {
  3843. s string
  3844. stmts []ast.SelectStatement
  3845. err string
  3846. }{
  3847. {
  3848. s: "SELECT name FROM tbl;\nSELECT name FROM topic/sensor1\n",
  3849. stmts: []ast.SelectStatement{
  3850. {
  3851. Fields: []ast.Field{
  3852. {
  3853. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  3854. Name: "name",
  3855. AName: "",
  3856. },
  3857. },
  3858. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  3859. },
  3860. {
  3861. Fields: []ast.Field{
  3862. {
  3863. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  3864. Name: "name",
  3865. AName: "",
  3866. },
  3867. },
  3868. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  3869. },
  3870. },
  3871. },
  3872. {
  3873. s: "SELECT name FROM tbl;\nSELECT name FROM topic/sensor1;\n--SELECT comment FROM topic/comment",
  3874. stmts: []ast.SelectStatement{
  3875. {
  3876. Fields: []ast.Field{
  3877. {
  3878. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  3879. Name: "name",
  3880. AName: "",
  3881. },
  3882. },
  3883. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  3884. },
  3885. {
  3886. Fields: []ast.Field{
  3887. {
  3888. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  3889. Name: "name",
  3890. AName: "",
  3891. },
  3892. },
  3893. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  3894. },
  3895. },
  3896. },
  3897. }
  3898. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  3899. for i, tt := range tests {
  3900. stmts, err := NewParser(strings.NewReader(tt.s)).ParseQueries()
  3901. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  3902. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.s, tt.err, err)
  3903. } else if tt.err == "" && !reflect.DeepEqual(tt.stmts, stmts) {
  3904. t.Errorf("%d. %q\n\nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.s, tt.stmts, stmts)
  3905. }
  3906. }
  3907. }