parser_test.go 86 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157
  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.FieldRef{StreamName: ast.StreamName("t1"), Name: "name"},
  209. Name: "name",
  210. AName: "",
  211. },
  212. },
  213. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "join"}},
  214. },
  215. },
  216. {
  217. s: `SELECT name FROM topic/sensor1 AS t1`,
  218. stmt: &ast.SelectStatement{
  219. Fields: []ast.Field{
  220. {
  221. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  222. Name: "name",
  223. AName: "",
  224. },
  225. },
  226. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
  227. },
  228. },
  229. {
  230. s: `SELECT name FROM topic/sensor1/#`,
  231. stmt: &ast.SelectStatement{
  232. Fields: []ast.Field{
  233. {
  234. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  235. Name: "name",
  236. AName: "",
  237. },
  238. },
  239. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1/#"}},
  240. },
  241. },
  242. {
  243. s: `SELECT name FROM topic/sensor1/# AS t2 `,
  244. stmt: &ast.SelectStatement{
  245. Fields: []ast.Field{
  246. {
  247. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  248. Name: "name",
  249. AName: "",
  250. },
  251. },
  252. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1/#", Alias: "t2"}},
  253. },
  254. },
  255. {
  256. s: `SELECT name FROM /topic/sensor1/#`,
  257. stmt: &ast.SelectStatement{
  258. Fields: []ast.Field{
  259. {
  260. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  261. Name: "name",
  262. AName: "",
  263. },
  264. },
  265. Sources: []ast.Source{&ast.Table{Name: "/topic/sensor1/#"}},
  266. },
  267. },
  268. {
  269. s: `SELECT name FROM /topic/sensor1/#/`,
  270. stmt: &ast.SelectStatement{
  271. Fields: []ast.Field{
  272. {
  273. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  274. Name: "name",
  275. AName: "",
  276. },
  277. },
  278. Sources: []ast.Source{&ast.Table{Name: "/topic/sensor1/#/"}},
  279. },
  280. },
  281. {
  282. s: `SELECT name FROM /topic/sensor1/+/temp1/`,
  283. stmt: &ast.SelectStatement{
  284. Fields: []ast.Field{
  285. {
  286. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  287. Name: "name",
  288. AName: "",
  289. },
  290. },
  291. Sources: []ast.Source{&ast.Table{Name: "/topic/sensor1/+/temp1/"}},
  292. },
  293. },
  294. {
  295. s: `SELECT name FROM topic/sensor1/+/temp`,
  296. stmt: &ast.SelectStatement{
  297. Fields: []ast.Field{
  298. {
  299. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  300. Name: "name",
  301. AName: "",
  302. },
  303. },
  304. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1/+/temp"}},
  305. },
  306. },
  307. {
  308. s: `SELECT * FROM tbl`,
  309. stmt: &ast.SelectStatement{
  310. Fields: []ast.Field{
  311. {
  312. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  313. Name: "*",
  314. AName: "",
  315. },
  316. },
  317. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  318. },
  319. },
  320. {
  321. s: `SELECT a,b FROM tbl`,
  322. stmt: &ast.SelectStatement{
  323. Fields: []ast.Field{
  324. {Expr: &ast.FieldRef{Name: "a", StreamName: ast.DefaultStream}, Name: "a", AName: ""},
  325. {Expr: &ast.FieldRef{Name: "b", StreamName: ast.DefaultStream}, Name: "b", AName: ""},
  326. },
  327. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  328. },
  329. },
  330. {
  331. s: `SELECT a, b,c FROM tbl`,
  332. stmt: &ast.SelectStatement{
  333. Fields: []ast.Field{
  334. {Expr: &ast.FieldRef{Name: "a", StreamName: ast.DefaultStream}, Name: "a", AName: ""},
  335. {Expr: &ast.FieldRef{Name: "b", StreamName: ast.DefaultStream}, Name: "b", AName: ""},
  336. {Expr: &ast.FieldRef{Name: "c", StreamName: ast.DefaultStream}, Name: "c", AName: ""},
  337. },
  338. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  339. },
  340. },
  341. {
  342. s: `SELECT a AS alias FROM tbl`,
  343. stmt: &ast.SelectStatement{
  344. Fields: []ast.Field{{Expr: &ast.FieldRef{Name: "a", StreamName: ast.DefaultStream}, Name: "a", AName: "alias"}},
  345. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  346. },
  347. },
  348. {
  349. s: `SELECT a AS alias1, b as Alias2 FROM tbl`,
  350. stmt: &ast.SelectStatement{
  351. Fields: []ast.Field{
  352. {Expr: &ast.FieldRef{Name: "a", StreamName: ast.DefaultStream}, Name: "a", AName: "alias1"},
  353. {Expr: &ast.FieldRef{Name: "b", StreamName: ast.DefaultStream}, Name: "b", AName: "Alias2"},
  354. },
  355. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  356. },
  357. },
  358. {
  359. s: `SELECT LenGth("test") FROM tbl`,
  360. stmt: &ast.SelectStatement{
  361. Fields: []ast.Field{
  362. {
  363. AName: "",
  364. Name: "length",
  365. Expr: &ast.Call{
  366. Name: "length",
  367. Args: []ast.Expr{&ast.StringLiteral{Val: "test"}},
  368. },
  369. },
  370. },
  371. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  372. },
  373. },
  374. {
  375. s: `SELECT length(test) FROM tbl`,
  376. stmt: &ast.SelectStatement{
  377. Fields: []ast.Field{
  378. {
  379. AName: "",
  380. Name: "length",
  381. Expr: &ast.Call{
  382. Name: "length",
  383. Args: []ast.Expr{&ast.FieldRef{Name: "test", StreamName: ast.DefaultStream}},
  384. },
  385. },
  386. },
  387. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  388. },
  389. },
  390. {
  391. s: `SELECT sin(123) FROM tbl`,
  392. stmt: &ast.SelectStatement{
  393. Fields: []ast.Field{
  394. {
  395. AName: "",
  396. Name: "sin",
  397. Expr: &ast.Call{
  398. Name: "sin",
  399. Args: []ast.Expr{&ast.IntegerLiteral{Val: 123}},
  400. },
  401. },
  402. },
  403. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  404. },
  405. },
  406. {
  407. s: `SELECT lpad("abc", 123) FROM tbl`,
  408. stmt: &ast.SelectStatement{
  409. Fields: []ast.Field{
  410. {
  411. AName: "",
  412. Name: "lpad",
  413. Expr: &ast.Call{
  414. Name: "lpad",
  415. Args: []ast.Expr{&ast.StringLiteral{Val: "abc"}, &ast.IntegerLiteral{Val: 123}},
  416. },
  417. },
  418. },
  419. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  420. },
  421. },
  422. {
  423. s: `SELECT newuuid() FROM tbl`,
  424. stmt: &ast.SelectStatement{
  425. Fields: []ast.Field{
  426. {
  427. AName: "",
  428. Name: "newuuid",
  429. Expr: &ast.Call{
  430. Name: "newuuid",
  431. Args: nil,
  432. },
  433. },
  434. },
  435. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  436. },
  437. },
  438. {
  439. s: `SELECT indexof("abc", field1) FROM tbl`,
  440. stmt: &ast.SelectStatement{
  441. Fields: []ast.Field{
  442. {
  443. AName: "",
  444. Name: "indexof",
  445. Expr: &ast.Call{
  446. Name: "indexof",
  447. Args: []ast.Expr{
  448. &ast.StringLiteral{Val: "abc"},
  449. &ast.FieldRef{Name: "field1", StreamName: ast.DefaultStream},
  450. },
  451. },
  452. },
  453. },
  454. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  455. },
  456. },
  457. {
  458. s: `SELECT lpad(lower(test),1) FROM tbl`,
  459. stmt: &ast.SelectStatement{
  460. Fields: []ast.Field{
  461. {
  462. AName: "",
  463. Name: "lpad",
  464. Expr: &ast.Call{
  465. Name: "lpad",
  466. FuncId: 1,
  467. Args: []ast.Expr{
  468. &ast.Call{
  469. Name: "lower",
  470. Args: []ast.Expr{
  471. &ast.FieldRef{Name: "test", StreamName: ast.DefaultStream},
  472. },
  473. },
  474. &ast.IntegerLiteral{Val: 1},
  475. },
  476. },
  477. },
  478. },
  479. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  480. },
  481. },
  482. {
  483. s: `SELECT lpad(lower(test),1) AS field1 FROM tbl`,
  484. stmt: &ast.SelectStatement{
  485. Fields: []ast.Field{
  486. {
  487. AName: "field1",
  488. Name: "lpad",
  489. Expr: &ast.Call{
  490. Name: "lpad",
  491. FuncId: 1,
  492. Args: []ast.Expr{
  493. &ast.Call{
  494. Name: "lower",
  495. Args: []ast.Expr{
  496. &ast.FieldRef{Name: "test", StreamName: ast.DefaultStream},
  497. },
  498. },
  499. &ast.IntegerLiteral{Val: 1},
  500. },
  501. },
  502. },
  503. },
  504. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  505. },
  506. },
  507. {
  508. s: `SELECT length(lower("test")) FROM tbl`,
  509. stmt: &ast.SelectStatement{
  510. Fields: []ast.Field{
  511. {
  512. AName: "",
  513. Name: "length",
  514. Expr: &ast.Call{
  515. Name: "length",
  516. FuncId: 1,
  517. Args: []ast.Expr{
  518. &ast.Call{
  519. Name: "lower",
  520. Args: []ast.Expr{
  521. &ast.StringLiteral{Val: "test"},
  522. },
  523. },
  524. },
  525. },
  526. },
  527. },
  528. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  529. },
  530. },
  531. {
  532. s: `SELECT count(*) FROM tbl`,
  533. stmt: &ast.SelectStatement{
  534. Fields: []ast.Field{
  535. {
  536. AName: "",
  537. Name: "count",
  538. Expr: &ast.Call{
  539. Name: "count",
  540. Args: []ast.Expr{&ast.Wildcard{Token: ast.ASTERISK}},
  541. FuncType: ast.FuncTypeAgg,
  542. },
  543. },
  544. },
  545. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  546. },
  547. },
  548. {
  549. s: `SELECT count(*, f1) FROM tbl`,
  550. stmt: nil,
  551. err: `Expect 1 arguments but found 2.`,
  552. },
  553. {
  554. s: `SELECT lag() FROM tbl`,
  555. stmt: nil,
  556. err: `expect one two or three args but got 0`,
  557. },
  558. {
  559. s: `SELECT lag(a, b, "default value") FROM tbl`,
  560. stmt: nil,
  561. err: `Expect int type for parameter 2`,
  562. },
  563. {
  564. s: `SELECT lag(a, 2, 20) FROM tbl`,
  565. stmt: &ast.SelectStatement{
  566. Fields: []ast.Field{
  567. {
  568. AName: "",
  569. Name: "lag",
  570. Expr: &ast.Call{
  571. Name: "lag",
  572. Args: []ast.Expr{&ast.FieldRef{Name: "a", StreamName: ast.DefaultStream}, &ast.IntegerLiteral{Val: 2}, &ast.IntegerLiteral{Val: 20}},
  573. },
  574. },
  575. },
  576. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  577. },
  578. },
  579. {
  580. s: `SELECT deduplicate(temperature, false) FROM tbl`,
  581. stmt: &ast.SelectStatement{
  582. Fields: []ast.Field{
  583. {
  584. AName: "",
  585. Name: "deduplicate",
  586. Expr: &ast.Call{
  587. Name: "deduplicate",
  588. Args: []ast.Expr{&ast.Wildcard{Token: ast.ASTERISK}, &ast.FieldRef{Name: "temperature", StreamName: ast.DefaultStream}, &ast.BooleanLiteral{Val: false}},
  589. FuncType: ast.FuncTypeAgg,
  590. },
  591. },
  592. },
  593. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  594. },
  595. },
  596. {
  597. s: `SELECT "abc" FROM tbl`,
  598. stmt: &ast.SelectStatement{
  599. Fields: []ast.Field{{AName: "", Name: "kuiper_field_0", Expr: &ast.StringLiteral{Val: "abc"}}},
  600. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  601. },
  602. },
  603. {
  604. s: `SELECT "abc" AS field1 FROM tbl`,
  605. stmt: &ast.SelectStatement{
  606. Fields: []ast.Field{{AName: "field1", Name: "", Expr: &ast.StringLiteral{Val: "abc"}}},
  607. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  608. },
  609. },
  610. {
  611. s: `SELECT field0, "abc" AS field1, field2 FROM tbl`,
  612. stmt: &ast.SelectStatement{
  613. Fields: []ast.Field{
  614. {AName: "", Name: "field0", Expr: &ast.FieldRef{Name: "field0", StreamName: ast.DefaultStream}},
  615. {AName: "field1", Name: "", Expr: &ast.StringLiteral{Val: "abc"}},
  616. {AName: "", Name: "field2", Expr: &ast.FieldRef{Name: "field2", StreamName: ast.DefaultStream}},
  617. },
  618. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  619. },
  620. },
  621. {
  622. s: `SELECT * AS alias FROM tbl`,
  623. stmt: nil,
  624. err: `alias is not supported for *`,
  625. },
  626. {
  627. s: `SELECT *, FROM tbl`,
  628. stmt: nil,
  629. err: `found "FROM", expected expression.`,
  630. },
  631. {
  632. s: `SELECTname FROM tbl`,
  633. stmt: nil,
  634. err: `Found "SELECTname", Expected SELECT.` + "\n",
  635. },
  636. {
  637. s: `SELECT abc FROM tbl WHERE abc > 12 `,
  638. stmt: &ast.SelectStatement{
  639. Fields: []ast.Field{{AName: "", Name: "abc", Expr: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream}}},
  640. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  641. Condition: &ast.BinaryExpr{
  642. LHS: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream},
  643. OP: ast.GT,
  644. RHS: &ast.IntegerLiteral{Val: 12},
  645. },
  646. },
  647. },
  648. {
  649. s: `SELECT abc FROM tbl WHERE abc = "hello" `,
  650. stmt: &ast.SelectStatement{
  651. Fields: []ast.Field{{AName: "", Name: "abc", Expr: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream}}},
  652. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  653. Condition: &ast.BinaryExpr{
  654. LHS: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream},
  655. OP: ast.EQ,
  656. RHS: &ast.StringLiteral{Val: "hello"},
  657. },
  658. },
  659. },
  660. {
  661. s: `SELECT t1.abc FROM tbl AS t1 WHERE t1.abc = "hello" `,
  662. stmt: &ast.SelectStatement{
  663. Fields: []ast.Field{{AName: "", Name: "abc", Expr: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "abc"}}},
  664. Sources: []ast.Source{&ast.Table{Name: "tbl", Alias: "t1"}},
  665. Condition: &ast.BinaryExpr{
  666. LHS: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "abc"},
  667. OP: ast.EQ,
  668. RHS: &ast.StringLiteral{Val: "hello"},
  669. },
  670. },
  671. },
  672. {
  673. s: `SELECT abc, "fff" AS fa FROM tbl WHERE fa >= 5 `,
  674. stmt: &ast.SelectStatement{
  675. Fields: []ast.Field{{AName: "", Name: "abc", Expr: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream}}, {AName: "fa", Name: "", Expr: &ast.StringLiteral{Val: "fff"}}},
  676. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  677. Condition: &ast.BinaryExpr{
  678. LHS: &ast.FieldRef{Name: "fa", StreamName: ast.DefaultStream},
  679. OP: ast.GTE,
  680. RHS: &ast.IntegerLiteral{Val: 5},
  681. },
  682. },
  683. },
  684. {
  685. s: `SELECT field2 FROM tbl WHERE field2 != 5 `,
  686. stmt: &ast.SelectStatement{
  687. Fields: []ast.Field{{AName: "", Name: "field2", Expr: &ast.FieldRef{Name: "field2", StreamName: ast.DefaultStream}}},
  688. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  689. Condition: &ast.BinaryExpr{
  690. LHS: &ast.FieldRef{Name: "field2", StreamName: ast.DefaultStream},
  691. OP: ast.NEQ,
  692. RHS: &ast.IntegerLiteral{Val: 5},
  693. },
  694. },
  695. },
  696. {
  697. s: `SELECT field2 FROM tbl WHERE field2 ! = 5 `, // Add space char in expression
  698. stmt: &ast.SelectStatement{
  699. Fields: []ast.Field{{AName: "", Name: "field2", Expr: &ast.FieldRef{Name: "field2", StreamName: ast.DefaultStream}}},
  700. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  701. Condition: &ast.BinaryExpr{
  702. LHS: &ast.FieldRef{Name: "field2", StreamName: ast.DefaultStream},
  703. OP: ast.NEQ,
  704. RHS: &ast.IntegerLiteral{Val: 5},
  705. },
  706. },
  707. },
  708. {
  709. s: `SELECT *f FROM tbl`,
  710. stmt: nil,
  711. err: `found "f", expected FROM.`,
  712. },
  713. ////TODO
  714. //{
  715. // s: `SELECT *from FROM tbl`,
  716. // stmt: nil,
  717. // err: `found "f", expected FROM.`,
  718. //},
  719. {
  720. s: `SELECT abc+2 FROM tbl`,
  721. stmt: &ast.SelectStatement{
  722. Fields: []ast.Field{
  723. {
  724. AName: "",
  725. Name: "kuiper_field_0",
  726. Expr: &ast.BinaryExpr{
  727. LHS: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream},
  728. OP: ast.ADD,
  729. RHS: &ast.IntegerLiteral{Val: 2},
  730. },
  731. },
  732. },
  733. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  734. },
  735. },
  736. {
  737. s: `SELECT t1.abc+2 FROM tbl AS t1`,
  738. stmt: &ast.SelectStatement{
  739. Fields: []ast.Field{
  740. {
  741. AName: "",
  742. Name: "kuiper_field_0",
  743. Expr: &ast.BinaryExpr{
  744. LHS: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "abc"},
  745. OP: ast.ADD,
  746. RHS: &ast.IntegerLiteral{Val: 2},
  747. },
  748. },
  749. },
  750. Sources: []ast.Source{&ast.Table{Name: "tbl", Alias: "t1"}},
  751. },
  752. },
  753. {
  754. s: `SELECT abc + "hello" FROM tbl`,
  755. stmt: &ast.SelectStatement{
  756. Fields: []ast.Field{
  757. {
  758. AName: "",
  759. Name: "kuiper_field_0",
  760. Expr: &ast.BinaryExpr{
  761. LHS: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream},
  762. OP: ast.ADD,
  763. RHS: &ast.StringLiteral{Val: "hello"},
  764. },
  765. },
  766. },
  767. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  768. },
  769. },
  770. {
  771. s: `SELECT abc*2 + 3 FROM tbl`,
  772. stmt: &ast.SelectStatement{
  773. Fields: []ast.Field{
  774. {
  775. AName: "",
  776. Name: "kuiper_field_0",
  777. Expr: &ast.BinaryExpr{
  778. LHS: &ast.BinaryExpr{
  779. LHS: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream},
  780. OP: ast.MUL,
  781. RHS: &ast.IntegerLiteral{Val: 2},
  782. },
  783. OP: ast.ADD,
  784. RHS: &ast.IntegerLiteral{Val: 3},
  785. },
  786. },
  787. },
  788. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  789. },
  790. },
  791. {
  792. s: `SELECT ln(abc*2 + 3) FROM tbl`,
  793. stmt: &ast.SelectStatement{
  794. Fields: []ast.Field{
  795. {
  796. AName: "",
  797. Name: "ln",
  798. Expr: &ast.Call{
  799. Name: "ln",
  800. Args: []ast.Expr{
  801. &ast.BinaryExpr{
  802. LHS: &ast.BinaryExpr{
  803. LHS: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream},
  804. OP: ast.MUL,
  805. RHS: &ast.IntegerLiteral{Val: 2},
  806. },
  807. OP: ast.ADD,
  808. RHS: &ast.IntegerLiteral{Val: 3},
  809. },
  810. },
  811. },
  812. },
  813. },
  814. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  815. },
  816. },
  817. {
  818. s: `SELECT ln(t1.abc*2 + 3) FROM tbl AS t1`,
  819. stmt: &ast.SelectStatement{
  820. Fields: []ast.Field{
  821. {
  822. AName: "",
  823. Name: "ln",
  824. Expr: &ast.Call{
  825. Name: "ln",
  826. Args: []ast.Expr{
  827. &ast.BinaryExpr{
  828. LHS: &ast.BinaryExpr{
  829. LHS: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "abc"},
  830. OP: ast.MUL,
  831. RHS: &ast.IntegerLiteral{Val: 2},
  832. },
  833. OP: ast.ADD,
  834. RHS: &ast.IntegerLiteral{Val: 3},
  835. },
  836. },
  837. },
  838. },
  839. },
  840. Sources: []ast.Source{&ast.Table{Name: "tbl", Alias: "t1"}},
  841. },
  842. },
  843. {
  844. s: `SELECT lpad("param2", abc*2 + 3) FROM tbl`,
  845. stmt: &ast.SelectStatement{
  846. Fields: []ast.Field{
  847. {
  848. AName: "",
  849. Name: "lpad",
  850. Expr: &ast.Call{
  851. Name: "lpad",
  852. Args: []ast.Expr{
  853. &ast.StringLiteral{Val: "param2"},
  854. &ast.BinaryExpr{
  855. LHS: &ast.BinaryExpr{
  856. LHS: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream},
  857. OP: ast.MUL,
  858. RHS: &ast.IntegerLiteral{Val: 2},
  859. },
  860. OP: ast.ADD,
  861. RHS: &ast.IntegerLiteral{Val: 3},
  862. },
  863. },
  864. },
  865. },
  866. },
  867. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  868. },
  869. },
  870. {
  871. s: `SELECT 0.2 FROM tbl`,
  872. stmt: &ast.SelectStatement{
  873. Fields: []ast.Field{
  874. {
  875. AName: "",
  876. Name: "kuiper_field_0",
  877. Expr: &ast.NumberLiteral{Val: 0.2},
  878. },
  879. },
  880. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  881. },
  882. },
  883. {
  884. s: `SELECT .2 FROM tbl`,
  885. stmt: &ast.SelectStatement{
  886. Fields: []ast.Field{
  887. {
  888. AName: "",
  889. Name: "kuiper_field_0",
  890. Expr: &ast.NumberLiteral{Val: 0.2},
  891. },
  892. },
  893. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  894. },
  895. },
  896. {
  897. s: `SELECT sin(.2) FROM tbl`,
  898. stmt: &ast.SelectStatement{
  899. Fields: []ast.Field{
  900. {
  901. AName: "",
  902. Name: "sin",
  903. Expr: &ast.Call{
  904. Name: "sin",
  905. Args: []ast.Expr{&ast.NumberLiteral{Val: 0.2}},
  906. },
  907. },
  908. },
  909. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  910. },
  911. },
  912. {
  913. s: `SELECT power(.2, 4) FROM tbl`,
  914. stmt: &ast.SelectStatement{
  915. Fields: []ast.Field{
  916. {
  917. AName: "",
  918. Name: "power",
  919. Expr: &ast.Call{
  920. Name: "power",
  921. Args: []ast.Expr{&ast.NumberLiteral{Val: 0.2}, &ast.IntegerLiteral{Val: 4}},
  922. },
  923. },
  924. },
  925. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  926. },
  927. },
  928. {
  929. s: `SELECT power(.2, 4) AS f1 FROM tbl WHERE f1 > 2.2`,
  930. stmt: &ast.SelectStatement{
  931. Fields: []ast.Field{
  932. {
  933. AName: "f1",
  934. Name: "power",
  935. Expr: &ast.Call{
  936. Name: "power",
  937. Args: []ast.Expr{&ast.NumberLiteral{Val: 0.2}, &ast.IntegerLiteral{Val: 4}},
  938. },
  939. },
  940. },
  941. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  942. Condition: &ast.BinaryExpr{
  943. LHS: &ast.FieldRef{Name: "f1", StreamName: ast.DefaultStream},
  944. OP: ast.GT,
  945. RHS: &ast.NumberLiteral{Val: 2.2},
  946. },
  947. },
  948. },
  949. {
  950. s: `SELECT power(.2, 4) AS f1 FROM tbl WHERE f1 BETWEEN 1 AND 2`,
  951. stmt: &ast.SelectStatement{
  952. Fields: []ast.Field{
  953. {
  954. AName: "f1",
  955. Name: "power",
  956. Expr: &ast.Call{
  957. Name: "power",
  958. Args: []ast.Expr{&ast.NumberLiteral{Val: 0.2}, &ast.IntegerLiteral{Val: 4}},
  959. },
  960. },
  961. },
  962. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  963. Condition: &ast.BinaryExpr{
  964. LHS: &ast.FieldRef{Name: "f1", StreamName: ast.DefaultStream},
  965. OP: ast.BETWEEN,
  966. RHS: &ast.BetweenExpr{
  967. Lower: &ast.IntegerLiteral{Val: 1},
  968. Higher: &ast.IntegerLiteral{Val: 2},
  969. },
  970. },
  971. },
  972. },
  973. {
  974. s: `SELECT a FROM tbl WHERE f1 > 4 AND f2 BETWEEN 1 AND 2`,
  975. stmt: &ast.SelectStatement{
  976. Fields: []ast.Field{
  977. {
  978. AName: "",
  979. Name: "a",
  980. Expr: &ast.FieldRef{Name: "a", StreamName: ast.DefaultStream},
  981. },
  982. },
  983. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  984. Condition: &ast.BinaryExpr{
  985. OP: ast.AND,
  986. LHS: &ast.BinaryExpr{
  987. LHS: &ast.FieldRef{Name: "f1", StreamName: ast.DefaultStream},
  988. OP: ast.GT,
  989. RHS: &ast.IntegerLiteral{Val: 4},
  990. },
  991. RHS: &ast.BinaryExpr{
  992. LHS: &ast.FieldRef{Name: "f2", StreamName: ast.DefaultStream},
  993. OP: ast.BETWEEN,
  994. RHS: &ast.BetweenExpr{
  995. Lower: &ast.IntegerLiteral{Val: 1},
  996. Higher: &ast.IntegerLiteral{Val: 2},
  997. },
  998. },
  999. },
  1000. },
  1001. },
  1002. {
  1003. s: `SELECT a FROM tbl WHERE f1 NOT BETWEEN b AND c AND f2 BETWEEN 1 AND 2 AND f3 > 4`,
  1004. stmt: &ast.SelectStatement{
  1005. Fields: []ast.Field{
  1006. {
  1007. AName: "",
  1008. Name: "a",
  1009. Expr: &ast.FieldRef{Name: "a", StreamName: ast.DefaultStream},
  1010. },
  1011. },
  1012. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1013. Condition: &ast.BinaryExpr{
  1014. OP: ast.AND,
  1015. LHS: &ast.BinaryExpr{
  1016. OP: ast.AND,
  1017. LHS: &ast.BinaryExpr{
  1018. LHS: &ast.FieldRef{Name: "f1", StreamName: ast.DefaultStream},
  1019. OP: ast.NOTBETWEEN,
  1020. RHS: &ast.BetweenExpr{
  1021. Lower: &ast.FieldRef{Name: "b", StreamName: ast.DefaultStream},
  1022. Higher: &ast.FieldRef{Name: "c", StreamName: ast.DefaultStream},
  1023. },
  1024. },
  1025. RHS: &ast.BinaryExpr{
  1026. LHS: &ast.FieldRef{Name: "f2", StreamName: ast.DefaultStream},
  1027. OP: ast.BETWEEN,
  1028. RHS: &ast.BetweenExpr{
  1029. Lower: &ast.IntegerLiteral{Val: 1},
  1030. Higher: &ast.IntegerLiteral{Val: 2},
  1031. },
  1032. },
  1033. },
  1034. RHS: &ast.BinaryExpr{
  1035. OP: ast.GT,
  1036. LHS: &ast.FieldRef{Name: "f3", StreamName: ast.DefaultStream},
  1037. RHS: &ast.IntegerLiteral{Val: 4},
  1038. },
  1039. },
  1040. },
  1041. },
  1042. {
  1043. s: `SELECT a FROM tbl WHERE f1 NOT BETWEEN b`,
  1044. err: "expect AND expression after between but found EOF",
  1045. },
  1046. {
  1047. s: `SELECT a FROM tbl WHERE f1 NOT BETWEEN 1 OR 2`,
  1048. err: "expect AND expression after between but found OR",
  1049. },
  1050. {
  1051. s: `SELECT a FROM tbl WHERE a LIKE "foo"`,
  1052. stmt: &ast.SelectStatement{
  1053. Fields: []ast.Field{
  1054. {
  1055. AName: "",
  1056. Name: "a",
  1057. Expr: &ast.FieldRef{Name: "a", StreamName: ast.DefaultStream},
  1058. },
  1059. },
  1060. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1061. Condition: &ast.BinaryExpr{
  1062. LHS: &ast.FieldRef{Name: "a", StreamName: ast.DefaultStream},
  1063. OP: ast.LIKE,
  1064. RHS: &ast.LikePattern{Expr: &ast.StringLiteral{Val: "foo"}, Pattern: re1},
  1065. },
  1066. },
  1067. },
  1068. {
  1069. s: `SELECT a FROM tbl WHERE a NOT LIKE "fo_o%"`,
  1070. stmt: &ast.SelectStatement{
  1071. Fields: []ast.Field{
  1072. {
  1073. AName: "",
  1074. Name: "a",
  1075. Expr: &ast.FieldRef{Name: "a", StreamName: ast.DefaultStream},
  1076. },
  1077. },
  1078. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1079. Condition: &ast.BinaryExpr{
  1080. LHS: &ast.FieldRef{Name: "a", StreamName: ast.DefaultStream},
  1081. OP: ast.NOTLIKE,
  1082. RHS: &ast.LikePattern{Expr: &ast.StringLiteral{Val: "fo_o%"}, Pattern: re2},
  1083. },
  1084. },
  1085. },
  1086. {
  1087. s: `SELECT a FROM tbl WHERE a LIKE "foo\\%"`,
  1088. stmt: &ast.SelectStatement{
  1089. Fields: []ast.Field{
  1090. {
  1091. AName: "",
  1092. Name: "a",
  1093. Expr: &ast.FieldRef{Name: "a", StreamName: ast.DefaultStream},
  1094. },
  1095. },
  1096. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1097. Condition: &ast.BinaryExpr{
  1098. LHS: &ast.FieldRef{Name: "a", StreamName: ast.DefaultStream},
  1099. OP: ast.LIKE,
  1100. RHS: &ast.LikePattern{Expr: &ast.StringLiteral{Val: "foo\\%"}, Pattern: re3},
  1101. },
  1102. },
  1103. },
  1104. {
  1105. s: `SELECT deviceId, name FROM topic/sensor1 WHERE deviceId=1 AND name = "dname"`,
  1106. stmt: &ast.SelectStatement{
  1107. Fields: []ast.Field{
  1108. {Expr: &ast.FieldRef{Name: "deviceId", StreamName: ast.DefaultStream}, Name: "deviceId", AName: ""},
  1109. {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
  1110. },
  1111. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  1112. Condition: &ast.BinaryExpr{
  1113. LHS: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "deviceId", StreamName: ast.DefaultStream}, OP: ast.EQ, RHS: &ast.IntegerLiteral{Val: 1}},
  1114. OP: ast.AND,
  1115. RHS: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, OP: ast.EQ, RHS: &ast.StringLiteral{Val: "dname"}},
  1116. },
  1117. },
  1118. },
  1119. {
  1120. s: `SELECT deviceId, name FROM topic/sensor1 AS t1 WHERE t1.deviceId=1 AND t1.name = "dname"`,
  1121. stmt: &ast.SelectStatement{
  1122. Fields: []ast.Field{
  1123. {Expr: &ast.FieldRef{Name: "deviceId", StreamName: ast.DefaultStream}, Name: "deviceId", AName: ""},
  1124. {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
  1125. },
  1126. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
  1127. Condition: &ast.BinaryExpr{
  1128. LHS: &ast.BinaryExpr{LHS: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "deviceId"}, OP: ast.EQ, RHS: &ast.IntegerLiteral{Val: 1}},
  1129. OP: ast.AND,
  1130. RHS: &ast.BinaryExpr{LHS: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "name"}, OP: ast.EQ, RHS: &ast.StringLiteral{Val: "dname"}},
  1131. },
  1132. },
  1133. },
  1134. {
  1135. s: `SELECT temp AS t, name FROM topic/sensor1 WHERE t> = 20.5 OR name = "dname"`,
  1136. stmt: &ast.SelectStatement{
  1137. Fields: []ast.Field{
  1138. {Expr: &ast.FieldRef{Name: "temp", StreamName: ast.DefaultStream}, Name: "temp", AName: "t"},
  1139. {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
  1140. },
  1141. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  1142. Condition: &ast.BinaryExpr{
  1143. LHS: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "t", StreamName: ast.DefaultStream}, OP: ast.GTE, RHS: &ast.NumberLiteral{Val: 20.5}},
  1144. OP: ast.OR,
  1145. RHS: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, OP: ast.EQ, RHS: &ast.StringLiteral{Val: "dname"}},
  1146. },
  1147. },
  1148. },
  1149. {
  1150. s: `SELECT temp AS t, name FROM topic/sensor1 WHERE t IN arraySet OR name IN arraySet`,
  1151. stmt: &ast.SelectStatement{
  1152. Fields: []ast.Field{
  1153. {Expr: &ast.FieldRef{Name: "temp", StreamName: ast.DefaultStream}, Name: "temp", AName: "t"},
  1154. {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
  1155. },
  1156. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  1157. Condition: &ast.BinaryExpr{
  1158. LHS: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "t", StreamName: ast.DefaultStream}, OP: ast.IN, RHS: &ast.FieldRef{Name: "arraySet", StreamName: ast.DefaultStream}},
  1159. OP: ast.OR,
  1160. RHS: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, OP: ast.IN, RHS: &ast.FieldRef{Name: "arraySet", StreamName: ast.DefaultStream}},
  1161. },
  1162. },
  1163. },
  1164. {
  1165. s: `SELECT temp AS t, name FROM topic/sensor1 WHERE t NOT IN arraySet OR name NOT IN arraySet`,
  1166. stmt: &ast.SelectStatement{
  1167. Fields: []ast.Field{
  1168. {Expr: &ast.FieldRef{Name: "temp", StreamName: ast.DefaultStream}, Name: "temp", AName: "t"},
  1169. {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
  1170. },
  1171. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  1172. Condition: &ast.BinaryExpr{
  1173. LHS: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "t", StreamName: ast.DefaultStream}, OP: ast.NOTIN, RHS: &ast.FieldRef{Name: "arraySet", StreamName: ast.DefaultStream}},
  1174. OP: ast.OR,
  1175. RHS: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, OP: ast.NOTIN, RHS: &ast.FieldRef{Name: "arraySet", StreamName: ast.DefaultStream}},
  1176. },
  1177. },
  1178. },
  1179. {
  1180. s: `SELECT temp AS t, name FROM topic/sensor1 WHERE t IN (20.5, 20.4) OR name IN ("dname", "ename")`,
  1181. stmt: &ast.SelectStatement{
  1182. Fields: []ast.Field{
  1183. {Expr: &ast.FieldRef{Name: "temp", StreamName: ast.DefaultStream}, Name: "temp", AName: "t"},
  1184. {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
  1185. },
  1186. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  1187. Condition: &ast.BinaryExpr{
  1188. 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}}}},
  1189. OP: ast.OR,
  1190. 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"}}}},
  1191. },
  1192. },
  1193. },
  1194. {
  1195. s: `SELECT temp AS t, name FROM topic/sensor1 WHERE t NOT IN (20.5, 20.4) OR name IN ("dname", "ename")`,
  1196. stmt: &ast.SelectStatement{
  1197. Fields: []ast.Field{
  1198. {Expr: &ast.FieldRef{Name: "temp", StreamName: ast.DefaultStream}, Name: "temp", AName: "t"},
  1199. {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
  1200. },
  1201. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  1202. Condition: &ast.BinaryExpr{
  1203. 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}}}},
  1204. OP: ast.OR,
  1205. 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"}}}},
  1206. },
  1207. },
  1208. },
  1209. {
  1210. s: `SELECT temp AS t, name FROM topic/sensor1 WHERE name = "dname" GROUP BY name`,
  1211. stmt: &ast.SelectStatement{
  1212. Fields: []ast.Field{
  1213. {Expr: &ast.FieldRef{Name: "temp", StreamName: ast.DefaultStream}, Name: "temp", AName: "t"},
  1214. {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
  1215. },
  1216. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  1217. Condition: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, OP: ast.EQ, RHS: &ast.StringLiteral{Val: "dname"}},
  1218. Dimensions: ast.Dimensions{ast.Dimension{Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}}},
  1219. },
  1220. },
  1221. {
  1222. s: `SELECT temp AS t, name FROM topic/sensor1 WHERE name = "dname" GROUP BY name HAVING count(name) > 3`,
  1223. stmt: &ast.SelectStatement{
  1224. Fields: []ast.Field{
  1225. {Expr: &ast.FieldRef{Name: "temp", StreamName: ast.DefaultStream}, Name: "temp", AName: "t"},
  1226. {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
  1227. },
  1228. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  1229. Condition: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, OP: ast.EQ, RHS: &ast.StringLiteral{Val: "dname"}},
  1230. Dimensions: ast.Dimensions{ast.Dimension{Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}}},
  1231. 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}},
  1232. },
  1233. },
  1234. {
  1235. s: `SELECT temp AS t, name FROM topic/sensor1 WHERE name = "dname" HAVING count(name) > 3`,
  1236. stmt: &ast.SelectStatement{
  1237. Fields: []ast.Field{
  1238. {Expr: &ast.FieldRef{Name: "temp", StreamName: ast.DefaultStream}, Name: "temp", AName: "t"},
  1239. {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
  1240. },
  1241. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  1242. Condition: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, OP: ast.EQ, RHS: &ast.StringLiteral{Val: "dname"}},
  1243. 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}},
  1244. },
  1245. },
  1246. {
  1247. s: `SELECT id,AVG(data) FROM t GROUP BY SUM(data)>10`,
  1248. stmt: nil,
  1249. err: "Not allowed to call aggregate functions in GROUP BY clause.",
  1250. },
  1251. {
  1252. s: `SELECT temp AS t, name FROM topic/sensor1 WHERE count(name) = 3`,
  1253. stmt: nil,
  1254. err: "Not allowed to call aggregate functions in WHERE clause.",
  1255. },
  1256. {
  1257. s: `SELECT s1.temp AS t, name FROM topic/sensor1 AS s1 WHERE t = "dname" GROUP BY s1.temp`,
  1258. stmt: &ast.SelectStatement{
  1259. Fields: []ast.Field{
  1260. {Expr: &ast.FieldRef{StreamName: "s1", Name: "temp"}, Name: "temp", AName: "t"},
  1261. {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
  1262. },
  1263. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "s1"}},
  1264. Condition: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "t", StreamName: ast.DefaultStream}, OP: ast.EQ, RHS: &ast.StringLiteral{Val: "dname"}},
  1265. Dimensions: ast.Dimensions{ast.Dimension{Expr: &ast.FieldRef{StreamName: "s1", Name: "temp"}}},
  1266. },
  1267. },
  1268. {
  1269. s: `SELECT temp AS t, name FROM topic/sensor1 WHERE name = "dname" GROUP BY lpad(name,1)`,
  1270. stmt: &ast.SelectStatement{
  1271. Fields: []ast.Field{
  1272. {Expr: &ast.FieldRef{Name: "temp", StreamName: ast.DefaultStream}, Name: "temp", AName: "t"},
  1273. {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
  1274. },
  1275. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  1276. Condition: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, OP: ast.EQ, RHS: &ast.StringLiteral{Val: "dname"}},
  1277. Dimensions: ast.Dimensions{
  1278. ast.Dimension{
  1279. Expr: &ast.Call{Name: "lpad", Args: []ast.Expr{&ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, &ast.IntegerLiteral{Val: 1}}},
  1280. },
  1281. },
  1282. },
  1283. },
  1284. {
  1285. s: `SELECT temp AS t, name FROM topic/sensor1 AS s1 WHERE name = "dname" GROUP BY lpad(s1.name,1)`,
  1286. stmt: &ast.SelectStatement{
  1287. Fields: []ast.Field{
  1288. {Expr: &ast.FieldRef{Name: "temp", StreamName: ast.DefaultStream}, Name: "temp", AName: "t"},
  1289. {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
  1290. },
  1291. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "s1"}},
  1292. Condition: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, OP: ast.EQ, RHS: &ast.StringLiteral{Val: "dname"}},
  1293. Dimensions: ast.Dimensions{
  1294. ast.Dimension{
  1295. Expr: &ast.Call{Name: "lpad", Args: []ast.Expr{&ast.FieldRef{StreamName: ast.StreamName("s1"), Name: "name"}, &ast.IntegerLiteral{Val: 1}}},
  1296. },
  1297. },
  1298. },
  1299. },
  1300. {
  1301. s: `SELECT temp AS t, name FROM topic/sensor1 WHERE name = "dname" GROUP BY lpad(name,1) ORDER BY name`,
  1302. stmt: &ast.SelectStatement{
  1303. Fields: []ast.Field{
  1304. {Expr: &ast.FieldRef{Name: "temp", StreamName: ast.DefaultStream}, Name: "temp", AName: "t"},
  1305. {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
  1306. },
  1307. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  1308. Condition: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, OP: ast.EQ, RHS: &ast.StringLiteral{Val: "dname"}},
  1309. Dimensions: ast.Dimensions{
  1310. ast.Dimension{
  1311. Expr: &ast.Call{
  1312. Name: "lpad", Args: []ast.Expr{
  1313. &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  1314. &ast.IntegerLiteral{Val: 1},
  1315. },
  1316. },
  1317. },
  1318. },
  1319. SortFields: []ast.SortField{{Uname: "name", Name: "name", Ascending: true, FieldExpr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}}},
  1320. },
  1321. },
  1322. {
  1323. 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`,
  1324. stmt: &ast.SelectStatement{
  1325. Fields: []ast.Field{
  1326. {Expr: &ast.FieldRef{Name: "temp", StreamName: ast.DefaultStream}, Name: "temp", AName: "t"},
  1327. {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
  1328. },
  1329. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "s1"}},
  1330. Condition: &ast.BinaryExpr{LHS: &ast.FieldRef{StreamName: ast.StreamName("s1"), Name: "name"}, OP: ast.EQ, RHS: &ast.StringLiteral{Val: "dname"}},
  1331. Dimensions: ast.Dimensions{
  1332. ast.Dimension{
  1333. Expr: &ast.Call{
  1334. Name: "lpad", Args: []ast.Expr{
  1335. &ast.FieldRef{StreamName: ast.StreamName("s1"), Name: "name"},
  1336. &ast.IntegerLiteral{Val: 1},
  1337. },
  1338. },
  1339. },
  1340. },
  1341. SortFields: []ast.SortField{{Uname: "s1\007name", Name: "name", StreamName: ast.StreamName("s1"), Ascending: true, FieldExpr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}}},
  1342. },
  1343. },
  1344. {
  1345. s: `SELECT temp AS t, name FROM topic/sensor1 WHERE name = "dname" GROUP BY lpad(name,1) ORDER BY name DESC`,
  1346. stmt: &ast.SelectStatement{
  1347. Fields: []ast.Field{
  1348. {Expr: &ast.FieldRef{Name: "temp", StreamName: ast.DefaultStream}, Name: "temp", AName: "t"},
  1349. {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
  1350. },
  1351. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  1352. Condition: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, OP: ast.EQ, RHS: &ast.StringLiteral{Val: "dname"}},
  1353. Dimensions: ast.Dimensions{
  1354. ast.Dimension{
  1355. Expr: &ast.Call{
  1356. Name: "lpad", Args: []ast.Expr{
  1357. &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  1358. &ast.IntegerLiteral{Val: 1},
  1359. },
  1360. },
  1361. },
  1362. },
  1363. SortFields: []ast.SortField{{Uname: "name", Name: "name", Ascending: false, FieldExpr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}}},
  1364. },
  1365. },
  1366. {
  1367. s: `SELECT * FROM topic/sensor1 ORDER BY name DESC`,
  1368. stmt: &ast.SelectStatement{
  1369. Fields: []ast.Field{
  1370. {
  1371. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  1372. Name: "*",
  1373. AName: "",
  1374. },
  1375. },
  1376. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  1377. SortFields: []ast.SortField{{Uname: "name", Name: "name", Ascending: false, FieldExpr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}}},
  1378. },
  1379. },
  1380. {
  1381. s: `SELECT * FROM topic/sensor1 ORDER BY name DESC, name2 ASC`,
  1382. stmt: &ast.SelectStatement{
  1383. Fields: []ast.Field{
  1384. {
  1385. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  1386. Name: "*",
  1387. AName: "",
  1388. },
  1389. },
  1390. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  1391. 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}}},
  1392. },
  1393. },
  1394. {
  1395. s: `SELECT * FROM topic/sensor1 GROUP BY name, name2,power(name3,1.8) ORDER BY name DESC, name2 ASC`,
  1396. stmt: &ast.SelectStatement{
  1397. Fields: []ast.Field{
  1398. {
  1399. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  1400. Name: "*",
  1401. AName: "",
  1402. },
  1403. },
  1404. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  1405. Dimensions: ast.Dimensions{
  1406. ast.Dimension{Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}},
  1407. ast.Dimension{Expr: &ast.FieldRef{Name: "name2", StreamName: ast.DefaultStream}},
  1408. ast.Dimension{
  1409. Expr: &ast.Call{
  1410. Name: "power", Args: []ast.Expr{
  1411. &ast.FieldRef{Name: "name3", StreamName: ast.DefaultStream},
  1412. &ast.NumberLiteral{Val: 1.8},
  1413. },
  1414. },
  1415. },
  1416. },
  1417. 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}}},
  1418. },
  1419. },
  1420. //{
  1421. // s: `SELECT .2sd FROM tbl`,
  1422. // stmt: &SelectStatement{
  1423. // Fields: []Field{
  1424. // Field{
  1425. // AName:"",
  1426. // Expr: &NumberLiteral{Val: 0.2},
  1427. // },
  1428. // },
  1429. // TableName: "tbl",
  1430. // },
  1431. //},
  1432. {
  1433. s: `SELECT name FROM tbl/*SELECT comment FROM testComments*/`,
  1434. stmt: &ast.SelectStatement{
  1435. Fields: []ast.Field{
  1436. {
  1437. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  1438. Name: "name",
  1439. AName: "",
  1440. },
  1441. },
  1442. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1443. },
  1444. },
  1445. {
  1446. s: `/*SELECT comment FROM testComments*/SELECT name FROM tbl`,
  1447. stmt: &ast.SelectStatement{
  1448. Fields: []ast.Field{
  1449. {
  1450. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  1451. Name: "name",
  1452. AName: "",
  1453. },
  1454. },
  1455. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1456. },
  1457. },
  1458. {
  1459. s: `SELECT name /*SELECT comment FROM testComments*/ FROM tbl`,
  1460. stmt: &ast.SelectStatement{
  1461. Fields: []ast.Field{
  1462. {
  1463. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  1464. Name: "name",
  1465. AName: "",
  1466. },
  1467. },
  1468. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1469. },
  1470. },
  1471. {
  1472. s: `SELECT true AS f1, FALSE as f2 FROM tbl`,
  1473. stmt: &ast.SelectStatement{
  1474. Fields: []ast.Field{
  1475. {AName: "f1", Name: "", Expr: &ast.BooleanLiteral{Val: true}},
  1476. {AName: "f2", Name: "", Expr: &ast.BooleanLiteral{Val: false}},
  1477. },
  1478. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1479. },
  1480. },
  1481. {
  1482. s: `SELECT true AS f1 FROM tbl WHERE f2 = true`,
  1483. stmt: &ast.SelectStatement{
  1484. Fields: []ast.Field{
  1485. {AName: "f1", Name: "", Expr: &ast.BooleanLiteral{Val: true}},
  1486. },
  1487. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1488. Condition: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "f2", StreamName: ast.DefaultStream}, OP: ast.EQ, RHS: &ast.BooleanLiteral{Val: true}},
  1489. },
  1490. },
  1491. {
  1492. s: `SELECT indexof(field1, "abc") FROM tbl`,
  1493. stmt: &ast.SelectStatement{
  1494. Fields: []ast.Field{
  1495. {
  1496. AName: "",
  1497. Name: "indexof",
  1498. Expr: &ast.Call{
  1499. Name: "indexof",
  1500. Args: []ast.Expr{&ast.FieldRef{Name: "field1", StreamName: ast.DefaultStream}, &ast.StringLiteral{Val: "abc"}},
  1501. },
  1502. },
  1503. },
  1504. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1505. },
  1506. },
  1507. // The negative value expression support.
  1508. {
  1509. s: `SELECT -3 AS t1 FROM tbl`,
  1510. stmt: &ast.SelectStatement{
  1511. Fields: []ast.Field{
  1512. {
  1513. Expr: &ast.IntegerLiteral{Val: -3},
  1514. Name: "",
  1515. AName: "t1",
  1516. },
  1517. },
  1518. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1519. },
  1520. },
  1521. {
  1522. s: `SELECT - 3 AS t1 FROM tbl`,
  1523. stmt: &ast.SelectStatement{
  1524. Fields: []ast.Field{
  1525. {
  1526. Expr: &ast.IntegerLiteral{Val: -3},
  1527. Name: "",
  1528. AName: "t1",
  1529. },
  1530. },
  1531. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1532. },
  1533. },
  1534. {
  1535. s: `SELECT -. 3 AS t1 FROM tbl`,
  1536. stmt: &ast.SelectStatement{
  1537. Fields: []ast.Field{
  1538. {
  1539. Expr: &ast.NumberLiteral{Val: -.3},
  1540. Name: "",
  1541. AName: "t1",
  1542. },
  1543. },
  1544. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1545. },
  1546. },
  1547. {
  1548. s: `SELECT -3.3 AS t1 FROM tbl`,
  1549. stmt: &ast.SelectStatement{
  1550. Fields: []ast.Field{
  1551. {
  1552. Expr: &ast.NumberLiteral{Val: -3.3},
  1553. Name: "",
  1554. AName: "t1",
  1555. },
  1556. },
  1557. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1558. },
  1559. },
  1560. {
  1561. s: `SELECT sample(-.3,) FROM tbl`,
  1562. stmt: nil,
  1563. err: "function sample not found",
  1564. },
  1565. {
  1566. s: `select timestamp() as tp from demo`,
  1567. stmt: nil,
  1568. err: "function timestamp not found",
  1569. },
  1570. {
  1571. s: `select tstamp() as tp from demo`,
  1572. stmt: &ast.SelectStatement{
  1573. Fields: []ast.Field{
  1574. {
  1575. Expr: &ast.Call{
  1576. Name: "tstamp",
  1577. Args: nil,
  1578. },
  1579. Name: "tstamp",
  1580. AName: "tp",
  1581. },
  1582. },
  1583. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  1584. },
  1585. err: "",
  1586. },
  1587. {
  1588. s: `select rule_id() as rule_id from demo`,
  1589. stmt: &ast.SelectStatement{
  1590. Fields: []ast.Field{
  1591. {
  1592. Expr: &ast.Call{
  1593. Name: "rule_id",
  1594. Args: nil,
  1595. },
  1596. Name: "rule_id",
  1597. AName: "rule_id",
  1598. },
  1599. },
  1600. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  1601. },
  1602. err: "",
  1603. },
  1604. {
  1605. s: "SELECT `half FROM tb",
  1606. stmt: nil,
  1607. err: `found "EOF", expected FROM.`,
  1608. },
  1609. {
  1610. s: "SELECT `space var` FROM tbl",
  1611. stmt: &ast.SelectStatement{
  1612. Fields: []ast.Field{
  1613. {
  1614. Expr: &ast.FieldRef{Name: "space var", StreamName: ast.DefaultStream},
  1615. Name: "space var",
  1616. AName: "",
  1617. },
  1618. },
  1619. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1620. },
  1621. },
  1622. {
  1623. s: "SELECT `中文 Chinese` FROM tbl",
  1624. stmt: &ast.SelectStatement{
  1625. Fields: []ast.Field{
  1626. {
  1627. Expr: &ast.FieldRef{Name: "中文 Chinese", StreamName: ast.DefaultStream},
  1628. Name: "中文 Chinese",
  1629. AName: "",
  1630. },
  1631. },
  1632. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1633. },
  1634. },
  1635. {
  1636. s: "SELECT CASE temperature WHEN 25 THEN \"bingo\" WHEN 30 THEN \"high\" ELSE \"low\" END as label, humidity FROM tbl",
  1637. stmt: &ast.SelectStatement{
  1638. Fields: []ast.Field{
  1639. {
  1640. Expr: &ast.CaseExpr{
  1641. Value: &ast.FieldRef{Name: "temperature", StreamName: ast.DefaultStream},
  1642. WhenClauses: []*ast.WhenClause{
  1643. {
  1644. Expr: &ast.IntegerLiteral{Val: 25},
  1645. Result: &ast.StringLiteral{Val: "bingo"},
  1646. }, {
  1647. Expr: &ast.IntegerLiteral{Val: 30},
  1648. Result: &ast.StringLiteral{Val: "high"},
  1649. },
  1650. },
  1651. ElseClause: &ast.StringLiteral{Val: "low"},
  1652. },
  1653. Name: "",
  1654. AName: "label",
  1655. }, {
  1656. Expr: &ast.FieldRef{Name: "humidity", StreamName: ast.DefaultStream},
  1657. Name: "humidity",
  1658. AName: "",
  1659. },
  1660. },
  1661. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1662. },
  1663. },
  1664. {
  1665. s: "SELECT CASE temperature WHEN 25 THEN \"bingo\" WHEN 30 THEN \"high\" END as label, humidity FROM tbl",
  1666. stmt: &ast.SelectStatement{
  1667. Fields: []ast.Field{
  1668. {
  1669. Expr: &ast.CaseExpr{
  1670. Value: &ast.FieldRef{Name: "temperature", StreamName: ast.DefaultStream},
  1671. WhenClauses: []*ast.WhenClause{
  1672. {
  1673. Expr: &ast.IntegerLiteral{Val: 25},
  1674. Result: &ast.StringLiteral{Val: "bingo"},
  1675. }, {
  1676. Expr: &ast.IntegerLiteral{Val: 30},
  1677. Result: &ast.StringLiteral{Val: "high"},
  1678. },
  1679. },
  1680. ElseClause: nil,
  1681. },
  1682. Name: "",
  1683. AName: "label",
  1684. }, {
  1685. Expr: &ast.FieldRef{Name: "humidity", StreamName: ast.DefaultStream},
  1686. Name: "humidity",
  1687. AName: "",
  1688. },
  1689. },
  1690. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1691. },
  1692. },
  1693. {
  1694. s: "SELECT CASE temperature ELSE \"low\" END as label, humidity FROM tbl",
  1695. stmt: nil,
  1696. err: "invalid CASE expression, WHEN expected before ELSE",
  1697. },
  1698. {
  1699. s: "SELECT CASE WHEN temperature > 30 THEN \"high\" ELSE \"low\" END as label, humidity FROM tbl",
  1700. stmt: &ast.SelectStatement{
  1701. Fields: []ast.Field{
  1702. {
  1703. Expr: &ast.CaseExpr{
  1704. Value: nil,
  1705. WhenClauses: []*ast.WhenClause{
  1706. {
  1707. Expr: &ast.BinaryExpr{
  1708. OP: ast.GT,
  1709. LHS: &ast.FieldRef{Name: "temperature", StreamName: ast.DefaultStream},
  1710. RHS: &ast.IntegerLiteral{Val: 30},
  1711. },
  1712. Result: &ast.StringLiteral{Val: "high"},
  1713. },
  1714. },
  1715. ElseClause: &ast.StringLiteral{Val: "low"},
  1716. },
  1717. Name: "",
  1718. AName: "label",
  1719. }, {
  1720. Expr: &ast.FieldRef{Name: "humidity", StreamName: ast.DefaultStream},
  1721. Name: "humidity",
  1722. AName: "",
  1723. },
  1724. },
  1725. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1726. },
  1727. },
  1728. {
  1729. s: "SELECT CASE WHEN 30 THEN \"high\" ELSE \"low\" END as label, humidity FROM tbl",
  1730. stmt: nil,
  1731. err: "invalid CASE expression, WHEN expression must be a bool condition",
  1732. },
  1733. {
  1734. s: `SELECT count(*)-10 FROM demo`,
  1735. stmt: &ast.SelectStatement{
  1736. Fields: []ast.Field{
  1737. {
  1738. AName: "",
  1739. Name: "kuiper_field_0",
  1740. Expr: &ast.BinaryExpr{
  1741. OP: ast.SUB,
  1742. LHS: &ast.Call{
  1743. Name: "count",
  1744. Args: []ast.Expr{
  1745. &ast.Wildcard{Token: ast.ASTERISK},
  1746. },
  1747. FuncType: ast.FuncTypeAgg,
  1748. },
  1749. RHS: &ast.IntegerLiteral{Val: 10},
  1750. },
  1751. },
  1752. },
  1753. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  1754. },
  1755. },
  1756. {
  1757. s: `SELECT -abc FROM demo`,
  1758. stmt: nil,
  1759. err: "found \"-\", expected expression.",
  1760. },
  1761. {
  1762. s: `SELECT meta(*) FROM tbl`,
  1763. stmt: &ast.SelectStatement{
  1764. Fields: []ast.Field{
  1765. {
  1766. AName: "",
  1767. Name: "meta",
  1768. Expr: &ast.Call{
  1769. Name: "meta",
  1770. Args: []ast.Expr{
  1771. &ast.MetaRef{
  1772. Name: "*",
  1773. StreamName: ast.DefaultStream,
  1774. },
  1775. },
  1776. },
  1777. },
  1778. },
  1779. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1780. },
  1781. },
  1782. {
  1783. s: `SELECT changed_cols("",true,a,b,c) FROM tbl`,
  1784. stmt: &ast.SelectStatement{
  1785. Fields: []ast.Field{
  1786. {
  1787. AName: "",
  1788. Name: "changed_cols",
  1789. Expr: &ast.Call{
  1790. Name: "changed_cols",
  1791. Args: []ast.Expr{
  1792. &ast.ColFuncField{
  1793. Name: "",
  1794. Expr: &ast.StringLiteral{Val: ""},
  1795. },
  1796. &ast.ColFuncField{
  1797. Name: "",
  1798. Expr: &ast.BooleanLiteral{Val: true},
  1799. },
  1800. &ast.ColFuncField{Name: "a", Expr: &ast.FieldRef{
  1801. StreamName: ast.DefaultStream,
  1802. Name: "a",
  1803. }},
  1804. &ast.ColFuncField{Name: "b", Expr: &ast.FieldRef{
  1805. StreamName: ast.DefaultStream,
  1806. Name: "b",
  1807. }},
  1808. &ast.ColFuncField{Name: "c", Expr: &ast.FieldRef{
  1809. StreamName: ast.DefaultStream,
  1810. Name: "c",
  1811. }},
  1812. },
  1813. FuncType: ast.FuncTypeCols,
  1814. },
  1815. },
  1816. },
  1817. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1818. },
  1819. },
  1820. {
  1821. s: `SELECT changed_cols("",true,a,*,c) FROM tbl`,
  1822. stmt: &ast.SelectStatement{
  1823. Fields: []ast.Field{
  1824. {
  1825. AName: "",
  1826. Name: "changed_cols",
  1827. Expr: &ast.Call{
  1828. Name: "changed_cols",
  1829. Args: []ast.Expr{
  1830. &ast.ColFuncField{
  1831. Name: "",
  1832. Expr: &ast.StringLiteral{Val: ""},
  1833. },
  1834. &ast.ColFuncField{
  1835. Name: "",
  1836. Expr: &ast.BooleanLiteral{Val: true},
  1837. },
  1838. &ast.ColFuncField{Name: "a", Expr: &ast.FieldRef{
  1839. StreamName: ast.DefaultStream,
  1840. Name: "a",
  1841. }},
  1842. &ast.ColFuncField{Name: "*", Expr: &ast.Wildcard{
  1843. Token: ast.ASTERISK,
  1844. }},
  1845. &ast.ColFuncField{Name: "c", Expr: &ast.FieldRef{
  1846. StreamName: ast.DefaultStream,
  1847. Name: "c",
  1848. }},
  1849. },
  1850. FuncType: ast.FuncTypeCols,
  1851. },
  1852. },
  1853. },
  1854. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1855. },
  1856. },
  1857. {
  1858. s: `SELECT a FROM tbl WHERE changed_cols("",true,a,b,c) > 3`,
  1859. err: "function changed_cols can only be used inside the select clause",
  1860. },
  1861. {
  1862. s: `SELECT ".*(/)(?!.*\1)" FROM topic/sensor1 AS t1`,
  1863. err: `found "invalid string: \".*(/)(?!.*\\1)\"", expected expression.`,
  1864. },
  1865. {
  1866. s: `SELECT name FROM tbl WHERE name IN ("A", "B","C")`,
  1867. stmt: &ast.SelectStatement{
  1868. Fields: []ast.Field{
  1869. {
  1870. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  1871. Name: "name",
  1872. AName: "",
  1873. },
  1874. },
  1875. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1876. 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"}}}},
  1877. },
  1878. },
  1879. {
  1880. s: `SELECT name FROM tbl WHERE name IN ()`,
  1881. err: `expect elements for IN expression, but found ")", expected expression.`,
  1882. },
  1883. {
  1884. s: `SELECT name FROM tbl WHERE name IN (abc,def OR name in (abc)`,
  1885. err: `expect ) for IN expression, but got "EOF"`,
  1886. },
  1887. {
  1888. s: `SELECT lag(name) OVER (PARTITION BY device) FROM tbl`,
  1889. stmt: &ast.SelectStatement{
  1890. Fields: []ast.Field{
  1891. {
  1892. Expr: &ast.Call{
  1893. Name: "lag",
  1894. FuncId: 0,
  1895. Args: []ast.Expr{
  1896. &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  1897. },
  1898. Partition: &ast.PartitionExpr{
  1899. Exprs: []ast.Expr{
  1900. &ast.FieldRef{Name: "device", StreamName: ast.DefaultStream},
  1901. },
  1902. },
  1903. },
  1904. Name: "lag",
  1905. AName: "",
  1906. },
  1907. },
  1908. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1909. },
  1910. },
  1911. {
  1912. s: `SELECT name OVER (PARTITION BY device) FROM tbl`,
  1913. err: `found "OVER", expected FROM.`,
  1914. },
  1915. {
  1916. s: `SELECT avg(name) OVER (PARTITION BY device) FROM tbl`,
  1917. err: `Found OVER after non analytic function avg`,
  1918. },
  1919. {
  1920. s: `SELECT name FROM tbl WHERE lag(name) OVER (PARTITION BY device, groupName) > 3`,
  1921. stmt: &ast.SelectStatement{
  1922. Fields: []ast.Field{
  1923. {
  1924. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  1925. Name: "name",
  1926. AName: "",
  1927. },
  1928. },
  1929. Condition: &ast.BinaryExpr{
  1930. LHS: &ast.Call{
  1931. Name: "lag",
  1932. FuncId: 0,
  1933. Args: []ast.Expr{
  1934. &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  1935. },
  1936. Partition: &ast.PartitionExpr{
  1937. Exprs: []ast.Expr{
  1938. &ast.FieldRef{Name: "device", StreamName: ast.DefaultStream},
  1939. &ast.FieldRef{Name: "groupName", StreamName: ast.DefaultStream},
  1940. },
  1941. },
  1942. },
  1943. OP: ast.GT,
  1944. RHS: &ast.IntegerLiteral{Val: 3},
  1945. },
  1946. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1947. },
  1948. },
  1949. {
  1950. s: `SELECT lag(name) OVER (PARTITION BY device) as ll FROM tbl`,
  1951. stmt: &ast.SelectStatement{
  1952. Fields: []ast.Field{
  1953. {
  1954. Expr: &ast.Call{
  1955. Name: "lag",
  1956. FuncId: 0,
  1957. Args: []ast.Expr{
  1958. &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  1959. },
  1960. Partition: &ast.PartitionExpr{
  1961. Exprs: []ast.Expr{
  1962. &ast.FieldRef{Name: "device", StreamName: ast.DefaultStream},
  1963. },
  1964. },
  1965. },
  1966. Name: "lag",
  1967. AName: "ll",
  1968. },
  1969. },
  1970. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  1971. },
  1972. },
  1973. {
  1974. s: `SELECT lag(name) OVER (PARTITION BY device WHEN abc > 12) as ll FROM tbl`,
  1975. stmt: &ast.SelectStatement{
  1976. Fields: []ast.Field{
  1977. {
  1978. Expr: &ast.Call{
  1979. Name: "lag",
  1980. FuncId: 0,
  1981. Args: []ast.Expr{
  1982. &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  1983. },
  1984. Partition: &ast.PartitionExpr{
  1985. Exprs: []ast.Expr{
  1986. &ast.FieldRef{Name: "device", StreamName: ast.DefaultStream},
  1987. },
  1988. },
  1989. WhenExpr: &ast.BinaryExpr{
  1990. LHS: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream},
  1991. OP: ast.GT,
  1992. RHS: &ast.IntegerLiteral{Val: 12},
  1993. },
  1994. },
  1995. Name: "lag",
  1996. AName: "ll",
  1997. },
  1998. },
  1999. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  2000. },
  2001. },
  2002. {
  2003. s: `SELECT lag(name) OVER (PARTITION BY device WHEN had_changed(true, StatusCode)) as ll FROM tbl`,
  2004. stmt: &ast.SelectStatement{
  2005. Fields: []ast.Field{
  2006. {
  2007. Expr: &ast.Call{
  2008. Name: "lag",
  2009. FuncId: 0,
  2010. Args: []ast.Expr{
  2011. &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  2012. },
  2013. Partition: &ast.PartitionExpr{
  2014. Exprs: []ast.Expr{
  2015. &ast.FieldRef{Name: "device", StreamName: ast.DefaultStream},
  2016. },
  2017. },
  2018. WhenExpr: &ast.Call{
  2019. Name: "had_changed",
  2020. FuncId: 1,
  2021. Args: []ast.Expr{&ast.BooleanLiteral{Val: true}, &ast.FieldRef{Name: "StatusCode", StreamName: ast.DefaultStream}},
  2022. },
  2023. },
  2024. Name: "lag",
  2025. AName: "ll",
  2026. },
  2027. },
  2028. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  2029. },
  2030. },
  2031. {
  2032. s: `SELECT lag(name) OVER (WHEN had_changed(true, StatusCode)) as ll FROM tbl`,
  2033. stmt: &ast.SelectStatement{
  2034. Fields: []ast.Field{
  2035. {
  2036. Expr: &ast.Call{
  2037. Name: "lag",
  2038. FuncId: 0,
  2039. Args: []ast.Expr{
  2040. &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  2041. },
  2042. WhenExpr: &ast.Call{
  2043. Name: "had_changed",
  2044. FuncId: 1,
  2045. Args: []ast.Expr{&ast.BooleanLiteral{Val: true}, &ast.FieldRef{Name: "StatusCode", StreamName: ast.DefaultStream}},
  2046. },
  2047. },
  2048. Name: "lag",
  2049. AName: "ll",
  2050. },
  2051. },
  2052. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  2053. },
  2054. },
  2055. {
  2056. s: `SELECT name OVER (WHEN a > b) FROM tbl`,
  2057. err: `found "OVER", expected FROM.`,
  2058. },
  2059. {
  2060. s: `SELECT avg(name) OVER (WHEN a > b) FROM tbl`,
  2061. err: `Found OVER after non analytic function avg`,
  2062. },
  2063. {
  2064. s: `SELECT *, name, lower(name) as ln FROM tbl`,
  2065. stmt: &ast.SelectStatement{
  2066. Fields: []ast.Field{
  2067. {
  2068. Expr: &ast.Wildcard{
  2069. Token: ast.ASTERISK,
  2070. },
  2071. Name: "*",
  2072. },
  2073. {
  2074. Expr: &ast.FieldRef{
  2075. Name: "name",
  2076. StreamName: ast.DefaultStream,
  2077. },
  2078. Name: "name",
  2079. AName: "",
  2080. },
  2081. {
  2082. Expr: &ast.Call{
  2083. Name: "lower",
  2084. FuncId: 0,
  2085. Args: []ast.Expr{
  2086. &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  2087. },
  2088. },
  2089. Name: "lower",
  2090. AName: "ln",
  2091. },
  2092. },
  2093. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  2094. },
  2095. },
  2096. {
  2097. s: `SELECT name, * FROM tbl`,
  2098. stmt: &ast.SelectStatement{
  2099. Fields: []ast.Field{
  2100. {
  2101. Expr: &ast.FieldRef{
  2102. Name: "name",
  2103. StreamName: ast.DefaultStream,
  2104. },
  2105. Name: "name",
  2106. AName: "",
  2107. },
  2108. {
  2109. Expr: &ast.Wildcard{
  2110. Token: ast.ASTERISK,
  2111. },
  2112. Name: "*",
  2113. },
  2114. },
  2115. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  2116. },
  2117. },
  2118. {
  2119. s: `SELECT name, * FROM tbl`,
  2120. stmt: &ast.SelectStatement{
  2121. Fields: []ast.Field{
  2122. {
  2123. Expr: &ast.FieldRef{
  2124. Name: "name",
  2125. StreamName: ast.DefaultStream,
  2126. },
  2127. Name: "name",
  2128. AName: "",
  2129. },
  2130. {
  2131. Expr: &ast.Wildcard{
  2132. Token: ast.ASTERISK,
  2133. },
  2134. Name: "*",
  2135. },
  2136. },
  2137. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  2138. },
  2139. },
  2140. }
  2141. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  2142. for i, tt := range tests {
  2143. // fmt.Printf("Parsing SQL %q.\n", tt.s)
  2144. stmt, err := NewParser(strings.NewReader(tt.s)).Parse()
  2145. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  2146. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.s, tt.err, err)
  2147. } else if tt.err == "" && !reflect.DeepEqual(tt.stmt, stmt) {
  2148. t.Errorf("%d. %q\n\nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.s, tt.stmt, stmt)
  2149. }
  2150. }
  2151. }
  2152. func TestParser_ParseWindowsExpr(t *testing.T) {
  2153. tests := []struct {
  2154. s string
  2155. stmt *ast.SelectStatement
  2156. err string
  2157. }{
  2158. {
  2159. s: `SELECT f1 FROM tbl GROUP BY TUMBLINGWINDOW(ss, 10)`,
  2160. stmt: &ast.SelectStatement{
  2161. Fields: []ast.Field{
  2162. {
  2163. Expr: &ast.FieldRef{Name: "f1", StreamName: ast.DefaultStream},
  2164. Name: "f1",
  2165. AName: "",
  2166. },
  2167. },
  2168. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  2169. Dimensions: ast.Dimensions{
  2170. ast.Dimension{
  2171. Expr: &ast.Window{
  2172. WindowType: ast.TUMBLING_WINDOW,
  2173. Length: &ast.IntegerLiteral{Val: 10000},
  2174. Interval: &ast.IntegerLiteral{Val: 0},
  2175. },
  2176. },
  2177. },
  2178. },
  2179. },
  2180. {
  2181. s: `SELECT f1 FROM tbl GROUP BY HOPPINGWINDOW(mi, 5, 1)`,
  2182. stmt: &ast.SelectStatement{
  2183. Fields: []ast.Field{
  2184. {
  2185. Expr: &ast.FieldRef{Name: "f1", StreamName: ast.DefaultStream},
  2186. Name: "f1",
  2187. AName: "",
  2188. },
  2189. },
  2190. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  2191. Dimensions: ast.Dimensions{
  2192. ast.Dimension{
  2193. Expr: &ast.Window{
  2194. WindowType: ast.HOPPING_WINDOW,
  2195. Length: &ast.IntegerLiteral{Val: 3e5},
  2196. Interval: &ast.IntegerLiteral{Val: 6e4},
  2197. },
  2198. },
  2199. },
  2200. },
  2201. },
  2202. {
  2203. s: `SELECT f1 FROM tbl GROUP BY SESSIONWINDOW(hh, 5, 1)`,
  2204. stmt: &ast.SelectStatement{
  2205. Fields: []ast.Field{
  2206. {
  2207. Expr: &ast.FieldRef{Name: "f1", StreamName: ast.DefaultStream},
  2208. Name: "f1",
  2209. AName: "",
  2210. },
  2211. },
  2212. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  2213. Dimensions: ast.Dimensions{
  2214. ast.Dimension{
  2215. Expr: &ast.Window{
  2216. WindowType: ast.SESSION_WINDOW,
  2217. Length: &ast.IntegerLiteral{Val: 1.8e7},
  2218. Interval: &ast.IntegerLiteral{Val: 3.6e6},
  2219. },
  2220. },
  2221. },
  2222. },
  2223. },
  2224. {
  2225. s: `SELECT f1 FROM tbl GROUP BY SLIDINGWINDOW(ms, 5)`,
  2226. stmt: &ast.SelectStatement{
  2227. Fields: []ast.Field{
  2228. {
  2229. Expr: &ast.FieldRef{Name: "f1", StreamName: ast.DefaultStream},
  2230. Name: "f1",
  2231. AName: "",
  2232. },
  2233. },
  2234. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  2235. Dimensions: ast.Dimensions{
  2236. ast.Dimension{
  2237. Expr: &ast.Window{
  2238. WindowType: ast.SLIDING_WINDOW,
  2239. Length: &ast.IntegerLiteral{Val: 5},
  2240. Interval: &ast.IntegerLiteral{Val: 0},
  2241. },
  2242. },
  2243. },
  2244. },
  2245. },
  2246. {
  2247. s: `SELECT f1 FROM tbl GROUP BY SLIDINGWINDOW(mi, 5, 1)`,
  2248. stmt: nil,
  2249. err: "The arguments for slidingwindow should be 2.\n",
  2250. },
  2251. {
  2252. s: `SELECT f1 FROM tbl GROUP BY SLIDINGWINDOW("mi", 5)`,
  2253. stmt: nil,
  2254. err: "The 1st argument for slidingwindow is expecting timer literal expression. One value of [dd|hh|mi|ss|ms].\n",
  2255. },
  2256. {
  2257. s: `SELECT f1 FROM tbl GROUP BY COUNTWINDOW(10)`,
  2258. stmt: &ast.SelectStatement{
  2259. Fields: []ast.Field{
  2260. {
  2261. Expr: &ast.FieldRef{Name: "f1", StreamName: ast.DefaultStream},
  2262. Name: "f1",
  2263. AName: "",
  2264. },
  2265. },
  2266. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  2267. Dimensions: ast.Dimensions{
  2268. ast.Dimension{
  2269. Expr: &ast.Window{
  2270. WindowType: ast.COUNT_WINDOW,
  2271. Length: &ast.IntegerLiteral{Val: 10},
  2272. },
  2273. },
  2274. },
  2275. },
  2276. },
  2277. {
  2278. s: `SELECT f1 FROM tbl GROUP BY COUNTWINDOW(10, 5)`,
  2279. stmt: &ast.SelectStatement{
  2280. Fields: []ast.Field{
  2281. {
  2282. Expr: &ast.FieldRef{Name: "f1", StreamName: ast.DefaultStream},
  2283. Name: "f1",
  2284. AName: "",
  2285. },
  2286. },
  2287. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  2288. Dimensions: ast.Dimensions{
  2289. ast.Dimension{
  2290. Expr: &ast.Window{
  2291. WindowType: ast.COUNT_WINDOW,
  2292. Length: &ast.IntegerLiteral{Val: 10},
  2293. Interval: &ast.IntegerLiteral{Val: 5},
  2294. },
  2295. },
  2296. },
  2297. },
  2298. },
  2299. {
  2300. s: `SELECT f1 FROM tbl GROUP BY COUNTWINDOW(3, 5)`,
  2301. stmt: nil,
  2302. err: "The second parameter value 5 should be less than the first parameter 3.",
  2303. },
  2304. {
  2305. s: `SELECT * FROM demo GROUP BY COUNTWINDOW(3,1) FILTER( where revenue > 100 )`,
  2306. stmt: &ast.SelectStatement{
  2307. Fields: []ast.Field{
  2308. {
  2309. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  2310. Name: "*",
  2311. AName: "",
  2312. },
  2313. },
  2314. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2315. Dimensions: ast.Dimensions{
  2316. ast.Dimension{
  2317. Expr: &ast.Window{
  2318. WindowType: ast.COUNT_WINDOW,
  2319. Length: &ast.IntegerLiteral{Val: 3},
  2320. Interval: &ast.IntegerLiteral{Val: 1},
  2321. Filter: &ast.BinaryExpr{
  2322. LHS: &ast.FieldRef{Name: "revenue", StreamName: ast.DefaultStream},
  2323. OP: ast.GT,
  2324. RHS: &ast.IntegerLiteral{Val: 100},
  2325. },
  2326. },
  2327. },
  2328. },
  2329. },
  2330. },
  2331. {
  2332. s: `SELECT * FROM demo GROUP BY department, COUNTWINDOW(3,1) FILTER( where revenue > 100 ), year`,
  2333. stmt: &ast.SelectStatement{
  2334. Fields: []ast.Field{
  2335. {
  2336. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  2337. Name: "*",
  2338. AName: "",
  2339. },
  2340. },
  2341. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2342. Dimensions: ast.Dimensions{
  2343. ast.Dimension{Expr: &ast.FieldRef{Name: "department", StreamName: ast.DefaultStream}},
  2344. ast.Dimension{
  2345. Expr: &ast.Window{
  2346. WindowType: ast.COUNT_WINDOW,
  2347. Length: &ast.IntegerLiteral{Val: 3},
  2348. Interval: &ast.IntegerLiteral{Val: 1},
  2349. Filter: &ast.BinaryExpr{
  2350. LHS: &ast.FieldRef{Name: "revenue", StreamName: ast.DefaultStream},
  2351. OP: ast.GT,
  2352. RHS: &ast.IntegerLiteral{Val: 100},
  2353. },
  2354. },
  2355. },
  2356. ast.Dimension{Expr: &ast.FieldRef{Name: "year", StreamName: ast.DefaultStream}},
  2357. },
  2358. },
  2359. },
  2360. {
  2361. s: `SELECT * FROM demo GROUP BY department, COUNTWINDOW(3,1) FILTER( where revenue IN (100, 200)), year`,
  2362. stmt: &ast.SelectStatement{
  2363. Fields: []ast.Field{
  2364. {
  2365. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  2366. Name: "*",
  2367. AName: "",
  2368. },
  2369. },
  2370. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2371. Dimensions: ast.Dimensions{
  2372. ast.Dimension{Expr: &ast.FieldRef{Name: "department", StreamName: ast.DefaultStream}},
  2373. ast.Dimension{
  2374. Expr: &ast.Window{
  2375. WindowType: ast.COUNT_WINDOW,
  2376. Length: &ast.IntegerLiteral{Val: 3},
  2377. Interval: &ast.IntegerLiteral{Val: 1},
  2378. Filter: &ast.BinaryExpr{
  2379. LHS: &ast.FieldRef{Name: "revenue", StreamName: ast.DefaultStream},
  2380. OP: ast.IN,
  2381. RHS: &ast.ValueSetExpr{
  2382. LiteralExprs: []ast.Expr{&ast.IntegerLiteral{Val: 100}, &ast.IntegerLiteral{Val: 200}},
  2383. },
  2384. },
  2385. },
  2386. },
  2387. ast.Dimension{Expr: &ast.FieldRef{Name: "year", StreamName: ast.DefaultStream}},
  2388. },
  2389. },
  2390. },
  2391. // to be supported
  2392. {
  2393. s: `SELECT sum(f1) FILTER( where revenue > 100 ) FROM tbl GROUP BY year`,
  2394. stmt: nil,
  2395. err: "found \"FILTER\", expected FROM.",
  2396. },
  2397. {
  2398. s: `SELECT * FROM demo GROUP BY COUNTWINDOW(3,1) FILTER where revenue > 100`,
  2399. stmt: nil,
  2400. err: "Found \"WHERE\" after FILTER, expect parentheses.",
  2401. },
  2402. {
  2403. s: `SELECT * FROM demo GROUP BY COUNTWINDOW(3,1) where revenue > 100`,
  2404. stmt: nil,
  2405. err: "found \"WHERE\", expected EOF.",
  2406. },
  2407. }
  2408. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  2409. for i, tt := range tests {
  2410. // fmt.Printf("Parsing SQL %q.\n", tt.s)
  2411. stmt, err := NewParser(strings.NewReader(tt.s)).Parse()
  2412. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  2413. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.s, tt.err, err)
  2414. } else if tt.err == "" && !reflect.DeepEqual(tt.stmt, stmt) {
  2415. t.Errorf("%d. %q\n\nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.s, tt.stmt, stmt)
  2416. }
  2417. }
  2418. }
  2419. func TestParser_ParseJsonExpr(t *testing.T) {
  2420. tests := []struct {
  2421. s string
  2422. stmt *ast.SelectStatement
  2423. err string
  2424. }{
  2425. {
  2426. s: `SELECT children[0] FROM demo`,
  2427. stmt: &ast.SelectStatement{
  2428. Fields: []ast.Field{
  2429. {
  2430. Expr: &ast.BinaryExpr{
  2431. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  2432. OP: ast.SUBSET,
  2433. RHS: &ast.IndexExpr{Index: &ast.IntegerLiteral{Val: 0}},
  2434. },
  2435. Name: "kuiper_field_0",
  2436. AName: "",
  2437. },
  2438. },
  2439. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2440. },
  2441. },
  2442. {
  2443. s: `SELECT children[0]->first FROM demo`,
  2444. stmt: &ast.SelectStatement{
  2445. Fields: []ast.Field{
  2446. {
  2447. Expr: &ast.BinaryExpr{
  2448. LHS: &ast.BinaryExpr{
  2449. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  2450. OP: ast.SUBSET,
  2451. RHS: &ast.IndexExpr{Index: &ast.IntegerLiteral{Val: 0}},
  2452. },
  2453. OP: ast.ARROW,
  2454. RHS: &ast.JsonFieldRef{Name: "first"},
  2455. },
  2456. Name: "kuiper_field_0",
  2457. AName: "",
  2458. },
  2459. },
  2460. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2461. },
  2462. },
  2463. {
  2464. s: `SELECT children->first[2] FROM demo`,
  2465. stmt: &ast.SelectStatement{
  2466. Fields: []ast.Field{
  2467. {
  2468. Expr: &ast.BinaryExpr{
  2469. LHS: &ast.BinaryExpr{
  2470. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  2471. OP: ast.ARROW,
  2472. RHS: &ast.JsonFieldRef{Name: "first"},
  2473. },
  2474. OP: ast.SUBSET,
  2475. RHS: &ast.IndexExpr{Index: &ast.IntegerLiteral{Val: 2}},
  2476. },
  2477. Name: "kuiper_field_0",
  2478. AName: "",
  2479. },
  2480. },
  2481. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2482. },
  2483. },
  2484. {
  2485. s: `SELECT children->first[2]->test FROM demo`,
  2486. stmt: &ast.SelectStatement{
  2487. Fields: []ast.Field{
  2488. {
  2489. Expr: &ast.BinaryExpr{
  2490. LHS: &ast.BinaryExpr{
  2491. LHS: &ast.BinaryExpr{
  2492. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  2493. OP: ast.ARROW,
  2494. RHS: &ast.JsonFieldRef{Name: "first"},
  2495. },
  2496. OP: ast.SUBSET,
  2497. RHS: &ast.IndexExpr{Index: &ast.IntegerLiteral{Val: 2}},
  2498. },
  2499. OP: ast.ARROW,
  2500. RHS: &ast.JsonFieldRef{Name: "test"},
  2501. },
  2502. Name: "kuiper_field_0",
  2503. AName: "",
  2504. },
  2505. },
  2506. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2507. },
  2508. },
  2509. {
  2510. s: `SELECT children[0:1] FROM demo`,
  2511. stmt: &ast.SelectStatement{
  2512. Fields: []ast.Field{
  2513. {
  2514. Expr: &ast.BinaryExpr{
  2515. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  2516. OP: ast.SUBSET,
  2517. RHS: &ast.ColonExpr{Start: &ast.IntegerLiteral{Val: 0}, End: &ast.IntegerLiteral{Val: 1}},
  2518. },
  2519. Name: "kuiper_field_0",
  2520. AName: "",
  2521. },
  2522. },
  2523. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2524. },
  2525. },
  2526. {
  2527. s: `SELECT children[:1] FROM demo`,
  2528. stmt: &ast.SelectStatement{
  2529. Fields: []ast.Field{
  2530. {
  2531. Expr: &ast.BinaryExpr{
  2532. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  2533. OP: ast.SUBSET,
  2534. RHS: &ast.ColonExpr{Start: &ast.IntegerLiteral{Val: 0}, End: &ast.IntegerLiteral{Val: 1}},
  2535. },
  2536. Name: "kuiper_field_0",
  2537. AName: "",
  2538. },
  2539. },
  2540. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2541. },
  2542. },
  2543. {
  2544. s: `SELECT children[:] FROM demo`,
  2545. stmt: &ast.SelectStatement{
  2546. Fields: []ast.Field{
  2547. {
  2548. Expr: &ast.BinaryExpr{
  2549. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  2550. OP: ast.SUBSET,
  2551. RHS: &ast.ColonExpr{Start: &ast.IntegerLiteral{Val: 0}, End: &ast.IntegerLiteral{Val: math.MinInt32}},
  2552. },
  2553. Name: "kuiper_field_0",
  2554. AName: "",
  2555. },
  2556. },
  2557. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2558. },
  2559. },
  2560. {
  2561. s: `SELECT children[2:] AS c FROM demo`,
  2562. stmt: &ast.SelectStatement{
  2563. Fields: []ast.Field{
  2564. {
  2565. Expr: &ast.BinaryExpr{
  2566. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  2567. OP: ast.SUBSET,
  2568. RHS: &ast.ColonExpr{Start: &ast.IntegerLiteral{Val: 2}, End: &ast.IntegerLiteral{Val: math.MinInt32}},
  2569. },
  2570. Name: "",
  2571. AName: "c",
  2572. },
  2573. },
  2574. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2575. },
  2576. },
  2577. {
  2578. s: `SELECT children[2:]->first AS c FROM demo`,
  2579. stmt: &ast.SelectStatement{
  2580. Fields: []ast.Field{
  2581. {
  2582. Expr: &ast.BinaryExpr{
  2583. 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}}},
  2584. OP: ast.ARROW,
  2585. RHS: &ast.JsonFieldRef{Name: "first"},
  2586. },
  2587. Name: "",
  2588. AName: "c",
  2589. },
  2590. },
  2591. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2592. },
  2593. },
  2594. {
  2595. s: `SELECT demo.* FROM demo`,
  2596. stmt: &ast.SelectStatement{
  2597. Fields: []ast.Field{
  2598. {
  2599. Expr: &ast.FieldRef{StreamName: ast.StreamName("demo"), Name: "*"},
  2600. Name: "*",
  2601. AName: "",
  2602. },
  2603. },
  2604. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2605. },
  2606. },
  2607. {
  2608. s: `SELECT demo.children[2:]->first AS c FROM demo`,
  2609. stmt: &ast.SelectStatement{
  2610. Fields: []ast.Field{
  2611. {
  2612. Expr: &ast.BinaryExpr{
  2613. 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}}},
  2614. OP: ast.ARROW,
  2615. RHS: &ast.JsonFieldRef{Name: "first"},
  2616. },
  2617. Name: "",
  2618. AName: "c",
  2619. },
  2620. },
  2621. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2622. },
  2623. },
  2624. {
  2625. s: `SELECT lower(demo.children[2:]->first) AS c FROM demo`,
  2626. stmt: &ast.SelectStatement{
  2627. Fields: []ast.Field{
  2628. {
  2629. Expr: &ast.Call{
  2630. Name: "lower",
  2631. Args: []ast.Expr{
  2632. &ast.BinaryExpr{
  2633. 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}}},
  2634. OP: ast.ARROW,
  2635. RHS: &ast.JsonFieldRef{Name: "first"},
  2636. },
  2637. },
  2638. },
  2639. Name: "lower",
  2640. AName: "c",
  2641. },
  2642. },
  2643. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2644. },
  2645. },
  2646. {
  2647. s: `SELECT children[:1] FROM demo WHERE abc[0] > 12`,
  2648. stmt: &ast.SelectStatement{
  2649. Fields: []ast.Field{
  2650. {
  2651. Expr: &ast.BinaryExpr{
  2652. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  2653. OP: ast.SUBSET,
  2654. RHS: &ast.ColonExpr{Start: &ast.IntegerLiteral{Val: 0}, End: &ast.IntegerLiteral{Val: 1}},
  2655. },
  2656. Name: "kuiper_field_0",
  2657. AName: "",
  2658. },
  2659. },
  2660. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2661. Condition: &ast.BinaryExpr{
  2662. LHS: &ast.BinaryExpr{
  2663. LHS: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream},
  2664. OP: ast.SUBSET,
  2665. RHS: &ast.IndexExpr{Index: &ast.IntegerLiteral{Val: 0}},
  2666. },
  2667. OP: ast.GT,
  2668. RHS: &ast.IntegerLiteral{Val: 12},
  2669. },
  2670. },
  2671. },
  2672. {
  2673. s: `SELECT children[:1] FROM demo WHERE abc[0] IN demo.children[2:]->first`,
  2674. stmt: &ast.SelectStatement{
  2675. Fields: []ast.Field{
  2676. {
  2677. Expr: &ast.BinaryExpr{
  2678. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  2679. OP: ast.SUBSET,
  2680. RHS: &ast.ColonExpr{Start: &ast.IntegerLiteral{Val: 0}, End: &ast.IntegerLiteral{Val: 1}},
  2681. },
  2682. Name: "kuiper_field_0",
  2683. AName: "",
  2684. },
  2685. },
  2686. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2687. Condition: &ast.BinaryExpr{
  2688. LHS: &ast.BinaryExpr{
  2689. LHS: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream},
  2690. OP: ast.SUBSET,
  2691. RHS: &ast.IndexExpr{Index: &ast.IntegerLiteral{Val: 0}},
  2692. },
  2693. OP: ast.IN,
  2694. RHS: &ast.BinaryExpr{
  2695. 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}}},
  2696. OP: ast.ARROW,
  2697. RHS: &ast.JsonFieldRef{Name: "first"},
  2698. },
  2699. },
  2700. },
  2701. },
  2702. {
  2703. s: `SELECT demo.children.first AS c FROM demo`,
  2704. stmt: nil,
  2705. err: "Too many field names. Please use -> to reference keys in struct.\n",
  2706. },
  2707. {
  2708. s: `SELECT children[index] FROM demo`,
  2709. stmt: &ast.SelectStatement{
  2710. Fields: []ast.Field{
  2711. {
  2712. Expr: &ast.BinaryExpr{
  2713. LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
  2714. OP: ast.SUBSET,
  2715. RHS: &ast.IndexExpr{Index: &ast.FieldRef{Name: "index", StreamName: ast.DefaultStream}},
  2716. },
  2717. Name: "kuiper_field_0",
  2718. AName: "",
  2719. },
  2720. },
  2721. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2722. },
  2723. },
  2724. }
  2725. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  2726. for i, tt := range tests {
  2727. stmt, err := NewParser(strings.NewReader(tt.s)).Parse()
  2728. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  2729. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.s, tt.err, err)
  2730. } else if tt.err == "" && !reflect.DeepEqual(tt.stmt, stmt) {
  2731. t.Errorf("%d. %q\n\nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.s, tt.stmt, stmt)
  2732. }
  2733. }
  2734. }
  2735. func TestParser_ParseJoins(t *testing.T) {
  2736. tests := []struct {
  2737. s string
  2738. stmt *ast.SelectStatement
  2739. err string
  2740. }{
  2741. {
  2742. s: `SELECT * FROM topic/sensor1 LEFT JOIN topic1 ON f=k`,
  2743. stmt: &ast.SelectStatement{
  2744. Fields: []ast.Field{
  2745. {
  2746. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  2747. Name: "*",
  2748. AName: "",
  2749. },
  2750. },
  2751. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  2752. Joins: []ast.Join{
  2753. {
  2754. Name: "topic1", Alias: "", JoinType: ast.LEFT_JOIN, Expr: &ast.BinaryExpr{
  2755. LHS: &ast.FieldRef{Name: "f", StreamName: ast.DefaultStream},
  2756. OP: ast.EQ,
  2757. RHS: &ast.FieldRef{Name: "k", StreamName: ast.DefaultStream},
  2758. },
  2759. },
  2760. },
  2761. },
  2762. },
  2763. {
  2764. s: `SELECT * FROM topic/sensor1 AS t1 INNER JOIN topic1 AS t2 ON f=k`,
  2765. stmt: &ast.SelectStatement{
  2766. Fields: []ast.Field{
  2767. {
  2768. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  2769. Name: "*",
  2770. AName: "",
  2771. },
  2772. },
  2773. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
  2774. Joins: []ast.Join{
  2775. {
  2776. Name: "topic1", Alias: "t2", JoinType: ast.INNER_JOIN, Expr: &ast.BinaryExpr{
  2777. LHS: &ast.FieldRef{Name: "f", StreamName: ast.DefaultStream},
  2778. OP: ast.EQ,
  2779. RHS: &ast.FieldRef{Name: "k", StreamName: ast.DefaultStream},
  2780. },
  2781. },
  2782. },
  2783. },
  2784. },
  2785. {
  2786. s: `SELECT * FROM topic/sensor1 AS t1 LEFT JOIN topic1/sensor2 AS t2 ON f=k`,
  2787. stmt: &ast.SelectStatement{
  2788. Fields: []ast.Field{
  2789. {
  2790. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  2791. Name: "*",
  2792. AName: "",
  2793. },
  2794. },
  2795. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
  2796. Joins: []ast.Join{
  2797. {
  2798. Name: "topic1/sensor2", Alias: "t2", JoinType: ast.LEFT_JOIN, Expr: &ast.BinaryExpr{
  2799. LHS: &ast.FieldRef{Name: "f", StreamName: ast.DefaultStream},
  2800. OP: ast.EQ,
  2801. RHS: &ast.FieldRef{Name: "k", StreamName: ast.DefaultStream},
  2802. },
  2803. },
  2804. },
  2805. },
  2806. },
  2807. {
  2808. s: `SELECT t1.name FROM topic/sensor1 AS t1 LEFT JOIN topic1/sensor2 AS t2 ON f=k`,
  2809. stmt: &ast.SelectStatement{
  2810. Fields: []ast.Field{
  2811. {
  2812. Expr: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "name"},
  2813. Name: "name",
  2814. AName: "",
  2815. },
  2816. },
  2817. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
  2818. Joins: []ast.Join{
  2819. {
  2820. Name: "topic1/sensor2", Alias: "t2", JoinType: ast.LEFT_JOIN, Expr: &ast.BinaryExpr{
  2821. LHS: &ast.FieldRef{Name: "f", StreamName: ast.DefaultStream},
  2822. OP: ast.EQ,
  2823. RHS: &ast.FieldRef{Name: "k", StreamName: ast.DefaultStream},
  2824. },
  2825. },
  2826. },
  2827. },
  2828. },
  2829. {
  2830. s: `SELECT t1.name FROM topic/sensor1 AS t1 LEFT JOIN topic1/sensor2 AS t2 ON t1.f=t2.k`,
  2831. stmt: &ast.SelectStatement{
  2832. Fields: []ast.Field{
  2833. {
  2834. Expr: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "name"},
  2835. Name: "name",
  2836. AName: "",
  2837. },
  2838. },
  2839. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
  2840. Joins: []ast.Join{
  2841. {
  2842. Name: "topic1/sensor2", Alias: "t2", JoinType: ast.LEFT_JOIN, Expr: &ast.BinaryExpr{
  2843. LHS: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "f"},
  2844. OP: ast.EQ,
  2845. RHS: &ast.FieldRef{StreamName: ast.StreamName("t2"), Name: "k"},
  2846. },
  2847. },
  2848. },
  2849. },
  2850. },
  2851. {
  2852. s: `SELECT t1.name FROM topic/sensor1 AS t1 RIGHT JOIN topic1/sensor2 AS t2 ON t1.f=t2.k`,
  2853. stmt: &ast.SelectStatement{
  2854. Fields: []ast.Field{
  2855. {
  2856. Expr: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "name"},
  2857. Name: "name",
  2858. AName: "",
  2859. },
  2860. },
  2861. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
  2862. Joins: []ast.Join{
  2863. {
  2864. Name: "topic1/sensor2", Alias: "t2", JoinType: ast.RIGHT_JOIN, Expr: &ast.BinaryExpr{
  2865. LHS: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "f"},
  2866. OP: ast.EQ,
  2867. RHS: &ast.FieldRef{StreamName: ast.StreamName("t2"), Name: "k"},
  2868. },
  2869. },
  2870. },
  2871. },
  2872. },
  2873. {
  2874. s: `SELECT t1.name FROM topic/sensor1 AS t1 FULL JOIN topic1/sensor2 AS t2 ON t1.f=t2.k`,
  2875. stmt: &ast.SelectStatement{
  2876. Fields: []ast.Field{
  2877. {
  2878. Expr: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "name"},
  2879. Name: "name",
  2880. AName: "",
  2881. },
  2882. },
  2883. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
  2884. Joins: []ast.Join{
  2885. {
  2886. Name: "topic1/sensor2", Alias: "t2", JoinType: ast.FULL_JOIN, Expr: &ast.BinaryExpr{
  2887. LHS: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "f"},
  2888. OP: ast.EQ,
  2889. RHS: &ast.FieldRef{StreamName: ast.StreamName("t2"), Name: "k"},
  2890. },
  2891. },
  2892. },
  2893. },
  2894. },
  2895. {
  2896. s: `SELECT t1.name FROM topic/sensor1 AS t1 CROSS JOIN topic1/sensor2 AS t2 ON t1.f=t2.k`,
  2897. stmt: nil,
  2898. err: "On expression is not required for cross join type.\n",
  2899. },
  2900. {
  2901. s: `SELECT t1.name FROM topic/sensor1 AS t1 CROSS JOIN topic1/sensor2 AS t2`,
  2902. stmt: &ast.SelectStatement{
  2903. Fields: []ast.Field{
  2904. {
  2905. Expr: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "name"},
  2906. Name: "name",
  2907. AName: "",
  2908. },
  2909. },
  2910. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
  2911. Joins: []ast.Join{
  2912. {
  2913. Name: "topic1/sensor2", Alias: "t2", JoinType: ast.CROSS_JOIN, Expr: nil,
  2914. },
  2915. },
  2916. },
  2917. },
  2918. {
  2919. s: `SELECT demo.*, demo2.* FROM demo LEFT JOIN demo2 on demo.f1 = demo2.f2`,
  2920. stmt: &ast.SelectStatement{
  2921. Fields: []ast.Field{
  2922. {
  2923. Expr: &ast.FieldRef{StreamName: ast.StreamName("demo"), Name: "*"},
  2924. Name: "*",
  2925. AName: "",
  2926. },
  2927. {
  2928. Expr: &ast.FieldRef{StreamName: ast.StreamName("demo2"), Name: "*"},
  2929. Name: "*",
  2930. AName: "",
  2931. },
  2932. },
  2933. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  2934. Joins: []ast.Join{
  2935. {
  2936. Name: "demo2", Alias: "", JoinType: ast.LEFT_JOIN, Expr: &ast.BinaryExpr{
  2937. LHS: &ast.FieldRef{StreamName: ast.StreamName("demo"), Name: "f1"},
  2938. OP: ast.EQ,
  2939. RHS: &ast.FieldRef{StreamName: ast.StreamName("demo2"), Name: "f2"},
  2940. },
  2941. },
  2942. },
  2943. },
  2944. },
  2945. }
  2946. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  2947. for i, tt := range tests {
  2948. stmt, err := NewParser(strings.NewReader(tt.s)).Parse()
  2949. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  2950. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.s, tt.err, err)
  2951. } else if tt.err == "" && !reflect.DeepEqual(tt.stmt, stmt) {
  2952. t.Errorf("%d. %q\n\nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.s, tt.stmt, stmt)
  2953. }
  2954. }
  2955. }
  2956. func TestParser_ParseStatements(t *testing.T) {
  2957. tests := []struct {
  2958. s string
  2959. stmts []ast.SelectStatement
  2960. err string
  2961. }{
  2962. {
  2963. s: "SELECT name FROM tbl;\nSELECT name FROM topic/sensor1\n",
  2964. stmts: []ast.SelectStatement{
  2965. {
  2966. Fields: []ast.Field{
  2967. {
  2968. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  2969. Name: "name",
  2970. AName: "",
  2971. },
  2972. },
  2973. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  2974. },
  2975. {
  2976. Fields: []ast.Field{
  2977. {
  2978. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  2979. Name: "name",
  2980. AName: "",
  2981. },
  2982. },
  2983. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  2984. },
  2985. },
  2986. },
  2987. {
  2988. s: "SELECT name FROM tbl;\nSELECT name FROM topic/sensor1;\n--SELECT comment FROM topic/comment",
  2989. stmts: []ast.SelectStatement{
  2990. {
  2991. Fields: []ast.Field{
  2992. {
  2993. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  2994. Name: "name",
  2995. AName: "",
  2996. },
  2997. },
  2998. Sources: []ast.Source{&ast.Table{Name: "tbl"}},
  2999. },
  3000. {
  3001. Fields: []ast.Field{
  3002. {
  3003. Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
  3004. Name: "name",
  3005. AName: "",
  3006. },
  3007. },
  3008. Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
  3009. },
  3010. },
  3011. },
  3012. }
  3013. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  3014. for i, tt := range tests {
  3015. stmts, err := NewParser(strings.NewReader(tt.s)).ParseQueries()
  3016. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  3017. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.s, tt.err, err)
  3018. } else if tt.err == "" && !reflect.DeepEqual(tt.stmts, stmts) {
  3019. t.Errorf("%d. %q\n\nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.s, tt.stmts, stmts)
  3020. }
  3021. }
  3022. }