parser_test.go 72 KB

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