parser_test.go 126 KB

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