parser_test.go 119 KB

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