parser_test.go 127 KB

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