parser_test.go 79 KB

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