rule_test.go 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981
  1. // Copyright 2021-2023 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package topotest
  15. import (
  16. "encoding/json"
  17. "testing"
  18. "github.com/lf-edge/ekuiper/internal/topo/topotest/mocknode"
  19. "github.com/lf-edge/ekuiper/pkg/api"
  20. )
  21. func TestAccAggSQL(t *testing.T) {
  22. // Reset
  23. streamList := []string{"demo"}
  24. HandleStream(false, streamList, t)
  25. tests := []RuleTest{
  26. {
  27. Name: "TestAccAggSql1",
  28. Sql: `select acc_sum(size) over (partition by color), color from demo`,
  29. R: [][]map[string]interface{}{
  30. {
  31. {
  32. "acc_sum": float64(3),
  33. "color": "red",
  34. },
  35. },
  36. {
  37. {
  38. "acc_sum": float64(6),
  39. "color": "blue",
  40. },
  41. },
  42. {
  43. {
  44. "acc_sum": float64(8),
  45. "color": "blue",
  46. },
  47. },
  48. {
  49. {
  50. "acc_sum": float64(4),
  51. "color": "yellow",
  52. },
  53. },
  54. {
  55. {
  56. "acc_sum": float64(4),
  57. "color": "red",
  58. },
  59. },
  60. },
  61. },
  62. {
  63. Name: "TestAccAggSql2",
  64. Sql: `select acc_sum(size) over (when color = "red"), color from demo`,
  65. R: [][]map[string]interface{}{
  66. {
  67. {
  68. "acc_sum": float64(3),
  69. "color": "red",
  70. },
  71. },
  72. {
  73. {
  74. "acc_sum": float64(3),
  75. "color": "blue",
  76. },
  77. },
  78. {
  79. {
  80. "acc_sum": float64(3),
  81. "color": "blue",
  82. },
  83. },
  84. {
  85. {
  86. "acc_sum": float64(3),
  87. "color": "yellow",
  88. },
  89. },
  90. {
  91. {
  92. "acc_sum": float64(4),
  93. "color": "red",
  94. },
  95. },
  96. },
  97. },
  98. {
  99. Name: "TestAccAggSql3",
  100. Sql: `select acc_min(size) over (when color = "red"), color from demo`,
  101. R: [][]map[string]interface{}{
  102. {
  103. {
  104. "acc_min": float64(3),
  105. "color": "red",
  106. },
  107. },
  108. {
  109. {
  110. "acc_min": float64(3),
  111. "color": "blue",
  112. },
  113. },
  114. {
  115. {
  116. "acc_min": float64(3),
  117. "color": "blue",
  118. },
  119. },
  120. {
  121. {
  122. "acc_min": float64(3),
  123. "color": "yellow",
  124. },
  125. },
  126. {
  127. {
  128. "acc_min": float64(1),
  129. "color": "red",
  130. },
  131. },
  132. },
  133. },
  134. {
  135. Name: "TestAccAggSql4",
  136. Sql: `select acc_max(size) over (when color = "red"), color from demo`,
  137. R: [][]map[string]interface{}{
  138. {
  139. {
  140. "acc_max": float64(3),
  141. "color": "red",
  142. },
  143. },
  144. {
  145. {
  146. "acc_max": float64(3),
  147. "color": "blue",
  148. },
  149. },
  150. {
  151. {
  152. "acc_max": float64(3),
  153. "color": "blue",
  154. },
  155. },
  156. {
  157. {
  158. "acc_max": float64(3),
  159. "color": "yellow",
  160. },
  161. },
  162. {
  163. {
  164. "acc_max": float64(3),
  165. "color": "red",
  166. },
  167. },
  168. },
  169. },
  170. {
  171. Name: "TestAccAggSql5",
  172. Sql: `select acc_count(size) over (when color = "red"), color from demo`,
  173. R: [][]map[string]interface{}{
  174. {
  175. {
  176. "acc_count": float64(1),
  177. "color": "red",
  178. },
  179. },
  180. {
  181. {
  182. "acc_count": float64(1),
  183. "color": "blue",
  184. },
  185. },
  186. {
  187. {
  188. "acc_count": float64(1),
  189. "color": "blue",
  190. },
  191. },
  192. {
  193. {
  194. "acc_count": float64(1),
  195. "color": "yellow",
  196. },
  197. },
  198. {
  199. {
  200. "acc_count": float64(2),
  201. "color": "red",
  202. },
  203. },
  204. },
  205. },
  206. {
  207. Name: "TestAccAggSql6",
  208. Sql: `select acc_avg(size) over (when color = "red"), color from demo`,
  209. R: [][]map[string]interface{}{
  210. {
  211. {
  212. "acc_avg": float64(3),
  213. "color": "red",
  214. },
  215. },
  216. {
  217. {
  218. "acc_avg": float64(3),
  219. "color": "blue",
  220. },
  221. },
  222. {
  223. {
  224. "acc_avg": float64(3),
  225. "color": "blue",
  226. },
  227. },
  228. {
  229. {
  230. "acc_avg": float64(3),
  231. "color": "yellow",
  232. },
  233. },
  234. {
  235. {
  236. "acc_avg": float64(2),
  237. "color": "red",
  238. },
  239. },
  240. },
  241. },
  242. }
  243. // Data setup
  244. HandleStream(true, streamList, t)
  245. options := []*api.RuleOption{
  246. {
  247. BufferLength: 100,
  248. SendError: true,
  249. },
  250. {
  251. BufferLength: 100,
  252. SendError: true,
  253. Qos: api.AtLeastOnce,
  254. CheckpointInterval: 5000,
  255. },
  256. {
  257. BufferLength: 100,
  258. SendError: true,
  259. Qos: api.ExactlyOnce,
  260. CheckpointInterval: 5000,
  261. },
  262. }
  263. for j, opt := range options {
  264. DoRuleTest(t, tests, j, opt, 0)
  265. }
  266. }
  267. func TestLimitSQL(t *testing.T) {
  268. // Reset
  269. streamList := []string{"demo", "demoArr", "demoArr2"}
  270. HandleStream(false, streamList, t)
  271. var r [][]map[string]interface{}
  272. tests := []RuleTest{
  273. {
  274. Name: "TestLimitSQL01",
  275. Sql: `SELECT unnest(demoArr2.arr) as col, demo.size FROM demo inner join demoArr2 on demo.size = demoArr2.x group by SESSIONWINDOW(ss, 2, 1) limit 1;`,
  276. R: [][]map[string]interface{}{
  277. {
  278. {
  279. "col": float64(1),
  280. "size": float64(1),
  281. },
  282. },
  283. },
  284. },
  285. {
  286. Name: "TestLimitSQL0",
  287. Sql: `SELECT unnest(demoArr.arr3) as col, demo.size FROM demo inner join demoArr on demo.size = demoArr.x group by SESSIONWINDOW(ss, 2, 1) limit 1;`,
  288. R: [][]map[string]interface{}{
  289. {
  290. {
  291. "col": float64(1),
  292. "size": float64(1),
  293. },
  294. },
  295. },
  296. },
  297. {
  298. Name: "TestLimitSQL1",
  299. Sql: `SELECT unnest(demoArr.arr3) as col, demo.size FROM demo inner join demoArr on demo.size = demoArr.x group by SESSIONWINDOW(ss, 2, 1) limit 0;`,
  300. R: r,
  301. },
  302. {
  303. Name: "TestLimitSQL2",
  304. Sql: `SELECT demo.size FROM demo inner join demoArr on demo.size = demoArr.x group by SESSIONWINDOW(ss, 2, 1) limit 1;`,
  305. R: [][]map[string]interface{}{
  306. {
  307. {
  308. "size": float64(1),
  309. },
  310. },
  311. },
  312. },
  313. {
  314. Name: "TestLimitSQL3",
  315. Sql: `SELECT demo.size FROM demo inner join demoArr on demo.size = demoArr.x group by SESSIONWINDOW(ss, 2, 1) limit 0;`,
  316. R: r,
  317. },
  318. }
  319. // Data setup
  320. HandleStream(true, streamList, t)
  321. options := []*api.RuleOption{
  322. {
  323. BufferLength: 100,
  324. SendError: true,
  325. },
  326. {
  327. BufferLength: 100,
  328. SendError: true,
  329. Qos: api.AtLeastOnce,
  330. CheckpointInterval: 5000,
  331. },
  332. {
  333. BufferLength: 100,
  334. SendError: true,
  335. Qos: api.ExactlyOnce,
  336. CheckpointInterval: 5000,
  337. },
  338. }
  339. for j, opt := range options {
  340. DoRuleTest(t, tests, j, opt, 0)
  341. }
  342. }
  343. func TestSRFSQL(t *testing.T) {
  344. // Reset
  345. streamList := []string{"demo", "demoArr"}
  346. HandleStream(false, streamList, t)
  347. tests := []RuleTest{
  348. {
  349. Name: "TestSingleSQLRule25",
  350. Sql: "SELECT unnest(a) from demoArr group by SESSIONWINDOW(ss, 2, 1);",
  351. R: [][]map[string]interface{}{
  352. {
  353. {
  354. "error": "the argument for the unnest function should be array",
  355. },
  356. },
  357. },
  358. },
  359. {
  360. Name: "TestSingleSQLRule24",
  361. Sql: "Select unnest(a) from demoArr;",
  362. R: [][]map[string]interface{}{
  363. {
  364. {
  365. "error": "the argument for the unnest function should be array",
  366. },
  367. },
  368. },
  369. },
  370. {
  371. Name: "TestSingleSQLRule21",
  372. Sql: `SELECT unnest(demoArr.arr3) as col, demo.size FROM demo inner join demoArr on demo.size = demoArr.x group by SESSIONWINDOW(ss, 2, 1);`,
  373. R: [][]map[string]interface{}{
  374. {
  375. {
  376. "col": float64(1),
  377. "size": float64(1),
  378. },
  379. {
  380. "col": float64(2),
  381. "size": float64(1),
  382. },
  383. {
  384. "col": float64(3),
  385. "size": float64(1),
  386. },
  387. },
  388. },
  389. },
  390. {
  391. Name: "TestSingleSQLRule22",
  392. Sql: `SELECT unnest(arr3) as col,y From demoArr group by y, SESSIONWINDOW(ss, 2, 1);`,
  393. R: [][]map[string]interface{}{
  394. {
  395. {
  396. "col": float64(1),
  397. "y": float64(2),
  398. },
  399. {
  400. "col": float64(2),
  401. "y": float64(2),
  402. },
  403. {
  404. "col": float64(3),
  405. "y": float64(2),
  406. },
  407. },
  408. },
  409. },
  410. {
  411. Name: "TestSingleSQLRule23",
  412. Sql: "SELECT unnest(arr3) as col,a from demoArr group by SESSIONWINDOW(ss, 2, 1);",
  413. R: [][]map[string]interface{}{
  414. {
  415. {
  416. "col": float64(1),
  417. "a": float64(6),
  418. },
  419. {
  420. "col": float64(2),
  421. "a": float64(6),
  422. },
  423. {
  424. "col": float64(3),
  425. "a": float64(6),
  426. },
  427. },
  428. },
  429. },
  430. {
  431. Name: `TestSingleSQLRule18`,
  432. Sql: `SELECT unnest(arr2) FROM demoArr where x=1`,
  433. R: [][]map[string]interface{}{
  434. {
  435. {
  436. "a": float64(1),
  437. "b": float64(2),
  438. },
  439. },
  440. {
  441. {
  442. "a": float64(3),
  443. "b": float64(4),
  444. },
  445. },
  446. },
  447. },
  448. // The mapping schema created by unnest function will cover the original column if they have the same column name
  449. {
  450. Name: `TestSingleSQLRule19`,
  451. Sql: `SELECT unnest(arr2),a FROM demoArr where x=1`,
  452. R: [][]map[string]interface{}{
  453. {
  454. {
  455. "a": float64(1),
  456. "b": float64(2),
  457. },
  458. },
  459. {
  460. {
  461. "a": float64(3),
  462. "b": float64(4),
  463. },
  464. },
  465. },
  466. },
  467. {
  468. Name: `TestSingleSQLRule20`,
  469. Sql: `SELECT unnest(arr3) as col FROM demoArr where x=1`,
  470. R: [][]map[string]interface{}{
  471. {
  472. {
  473. "col": float64(1),
  474. },
  475. },
  476. {
  477. {
  478. "col": float64(2),
  479. },
  480. },
  481. {
  482. {
  483. "col": float64(3),
  484. },
  485. },
  486. },
  487. },
  488. {
  489. Name: `TestSingleSQLRule21`,
  490. Sql: `SELECT unnest(arr2),x FROM demoArr where x=1`,
  491. R: [][]map[string]interface{}{
  492. {
  493. {
  494. "a": float64(1),
  495. "b": float64(2),
  496. "x": float64(1),
  497. },
  498. },
  499. {
  500. {
  501. "a": float64(3),
  502. "b": float64(4),
  503. "x": float64(1),
  504. },
  505. },
  506. },
  507. },
  508. }
  509. // Data setup
  510. HandleStream(true, streamList, t)
  511. options := []*api.RuleOption{
  512. {
  513. BufferLength: 100,
  514. SendError: true,
  515. }, {
  516. BufferLength: 100,
  517. SendError: true,
  518. Qos: api.AtLeastOnce,
  519. CheckpointInterval: 5000,
  520. }, {
  521. BufferLength: 100,
  522. SendError: true,
  523. Qos: api.ExactlyOnce,
  524. CheckpointInterval: 5000,
  525. },
  526. }
  527. for j, opt := range options {
  528. DoRuleTest(t, tests, j, opt, 0)
  529. }
  530. }
  531. func TestSingleSQL(t *testing.T) {
  532. // Reset
  533. streamList := []string{"demo", "demoError", "demo1", "table1", "demoTable", "demoArr"}
  534. HandleStream(false, streamList, t)
  535. // Data setup
  536. tests := []RuleTest{
  537. {
  538. Name: `TestAnalyzeFuncAlias1`,
  539. Sql: `SELECT lag(size,1,0) + 1 as b, lag(b,1,0),size FROM demo Group by COUNTWINDOW(5)`,
  540. R: [][]map[string]interface{}{
  541. {
  542. {
  543. "b": float64(1),
  544. "lag": float64(0),
  545. "size": float64(3),
  546. },
  547. {
  548. "b": float64(4),
  549. "lag": float64(1),
  550. "size": float64(6),
  551. },
  552. {
  553. "b": float64(7),
  554. "lag": float64(4),
  555. "size": float64(2),
  556. },
  557. {
  558. "b": float64(3),
  559. "lag": float64(7),
  560. "size": float64(4),
  561. },
  562. {
  563. "b": float64(5),
  564. "lag": float64(3),
  565. "size": float64(1),
  566. },
  567. },
  568. },
  569. },
  570. {
  571. Name: `TestAnalyzeFuncAlias2`,
  572. Sql: `SELECT lag(size,1,0) + 1 as b, lag(b,1,0),size FROM demo`,
  573. R: [][]map[string]interface{}{
  574. {
  575. {
  576. "b": float64(1),
  577. "lag": float64(0),
  578. "size": float64(3),
  579. },
  580. },
  581. {
  582. {
  583. "b": float64(4),
  584. "lag": float64(1),
  585. "size": float64(6),
  586. },
  587. },
  588. {
  589. {
  590. "b": float64(7),
  591. "lag": float64(4),
  592. "size": float64(2),
  593. },
  594. },
  595. {
  596. {
  597. "b": float64(3),
  598. "lag": float64(7),
  599. "size": float64(4),
  600. },
  601. },
  602. {
  603. {
  604. "b": float64(5),
  605. "lag": float64(3),
  606. "size": float64(1),
  607. },
  608. },
  609. },
  610. },
  611. {
  612. Name: `TestSingleSQLRule0`,
  613. Sql: `SELECT arr[x:y+1] as col1 FROM demoArr where x=1`,
  614. R: [][]map[string]interface{}{
  615. {{
  616. "col1": []interface{}{
  617. float64(2), float64(3),
  618. },
  619. }},
  620. },
  621. },
  622. {
  623. Name: `TestSingleSQLRule1`,
  624. Sql: `SELECT *, upper(color), event_time() FROM demo`,
  625. R: [][]map[string]interface{}{
  626. {{
  627. "color": "red",
  628. "size": float64(3),
  629. "ts": float64(1541152486013),
  630. "upper": "RED",
  631. "event_time": float64(1541152486013),
  632. }},
  633. {{
  634. "color": "blue",
  635. "size": float64(6),
  636. "ts": float64(1541152486822),
  637. "upper": "BLUE",
  638. "event_time": float64(1541152486822),
  639. }},
  640. {{
  641. "color": "blue",
  642. "size": float64(2),
  643. "ts": float64(1541152487632),
  644. "upper": "BLUE",
  645. "event_time": float64(1541152487632),
  646. }},
  647. {{
  648. "color": "yellow",
  649. "size": float64(4),
  650. "ts": float64(1541152488442),
  651. "upper": "YELLOW",
  652. "event_time": float64(1541152488442),
  653. }},
  654. {{
  655. "color": "red",
  656. "size": float64(1),
  657. "ts": float64(1541152489252),
  658. "upper": "RED",
  659. "event_time": float64(1541152489252),
  660. }},
  661. },
  662. M: map[string]interface{}{
  663. "op_2_project_0_exceptions_total": int64(0),
  664. "op_2_project_0_process_latency_us": int64(0),
  665. "op_2_project_0_records_in_total": int64(5),
  666. "op_2_project_0_records_out_total": int64(5),
  667. "sink_mockSink_0_exceptions_total": int64(0),
  668. "sink_mockSink_0_records_in_total": int64(5),
  669. "sink_mockSink_0_records_out_total": int64(5),
  670. "source_demo_0_exceptions_total": int64(0),
  671. "source_demo_0_records_in_total": int64(5),
  672. "source_demo_0_records_out_total": int64(5),
  673. },
  674. T: &api.PrintableTopo{
  675. Sources: []string{"source_demo"},
  676. Edges: map[string][]interface{}{
  677. "source_demo": {"op_2_project"},
  678. "op_2_project": {"sink_mockSink"},
  679. },
  680. },
  681. },
  682. {
  683. Name: `TestSingleSQLRule2`,
  684. Sql: `SELECT color, ts, last_hit_count() + 1 as lc FROM demo where size > 3`,
  685. R: [][]map[string]interface{}{
  686. {{
  687. "color": "blue",
  688. "ts": float64(1541152486822),
  689. "lc": float64(1),
  690. }},
  691. {{
  692. "color": "yellow",
  693. "ts": float64(1541152488442),
  694. "lc": float64(2),
  695. }},
  696. },
  697. M: map[string]interface{}{
  698. "op_3_project_0_exceptions_total": int64(0),
  699. "op_3_project_0_process_latency_us": int64(0),
  700. "op_3_project_0_records_in_total": int64(2),
  701. "op_3_project_0_records_out_total": int64(2),
  702. "sink_mockSink_0_exceptions_total": int64(0),
  703. "sink_mockSink_0_records_in_total": int64(2),
  704. "sink_mockSink_0_records_out_total": int64(2),
  705. "source_demo_0_exceptions_total": int64(0),
  706. "source_demo_0_records_in_total": int64(5),
  707. "source_demo_0_records_out_total": int64(5),
  708. "op_2_filter_0_exceptions_total": int64(0),
  709. "op_2_filter_0_process_latency_us": int64(0),
  710. "op_2_filter_0_records_in_total": int64(5),
  711. "op_2_filter_0_records_out_total": int64(2),
  712. },
  713. },
  714. {
  715. Name: `TestSingleSQLRule3`,
  716. Sql: `SELECT size as Int8, ts FROM demo where size > 3`,
  717. R: [][]map[string]interface{}{
  718. {{
  719. "Int8": float64(6),
  720. "ts": float64(1541152486822),
  721. }},
  722. {{
  723. "Int8": float64(4),
  724. "ts": float64(1541152488442),
  725. }},
  726. },
  727. M: map[string]interface{}{
  728. "op_3_project_0_exceptions_total": int64(0),
  729. "op_3_project_0_process_latency_us": int64(0),
  730. "op_3_project_0_records_in_total": int64(2),
  731. "op_3_project_0_records_out_total": int64(2),
  732. "sink_mockSink_0_exceptions_total": int64(0),
  733. "sink_mockSink_0_records_in_total": int64(2),
  734. "sink_mockSink_0_records_out_total": int64(2),
  735. "source_demo_0_exceptions_total": int64(0),
  736. "source_demo_0_records_in_total": int64(5),
  737. "source_demo_0_records_out_total": int64(5),
  738. "op_2_filter_0_exceptions_total": int64(0),
  739. "op_2_filter_0_process_latency_us": int64(0),
  740. "op_2_filter_0_records_in_total": int64(5),
  741. "op_2_filter_0_records_out_total": int64(2),
  742. },
  743. },
  744. {
  745. Name: `TestSingleSQLRule4`,
  746. Sql: `SELECT size as Int8, ts FROM demoError where size > 3`,
  747. R: [][]map[string]interface{}{
  748. {{
  749. "error": "error in preprocessor: field size type mismatch: cannot convert string(red) to int64",
  750. }},
  751. {{
  752. "Int8": float64(6),
  753. "ts": float64(1541152486822),
  754. }},
  755. {{
  756. "Int8": float64(4),
  757. "ts": float64(1541152488442),
  758. }},
  759. {{
  760. "error": "error in preprocessor: field size type mismatch: cannot convert string(blue) to int64",
  761. }},
  762. },
  763. M: map[string]interface{}{
  764. "op_3_project_0_exceptions_total": int64(2),
  765. "op_3_project_0_process_latency_us": int64(0),
  766. "op_3_project_0_records_in_total": int64(2),
  767. "op_3_project_0_records_out_total": int64(2),
  768. "sink_mockSink_0_exceptions_total": int64(0),
  769. "sink_mockSink_0_records_in_total": int64(4),
  770. "sink_mockSink_0_records_out_total": int64(4),
  771. "source_demoError_0_exceptions_total": int64(2),
  772. "source_demoError_0_records_in_total": int64(5),
  773. "source_demoError_0_records_out_total": int64(3),
  774. "op_2_filter_0_exceptions_total": int64(2),
  775. "op_2_filter_0_process_latency_us": int64(0),
  776. "op_2_filter_0_records_in_total": int64(3),
  777. "op_2_filter_0_records_out_total": int64(2),
  778. },
  779. },
  780. {
  781. Name: `TestSingleSQLRule5`,
  782. Sql: `SELECT meta(topic) as m, ts FROM demo WHERE last_hit_count() < 4`,
  783. R: [][]map[string]interface{}{
  784. {{
  785. "m": "mock",
  786. "ts": float64(1541152486013),
  787. }},
  788. {{
  789. "m": "mock",
  790. "ts": float64(1541152486822),
  791. }},
  792. {{
  793. "m": "mock",
  794. "ts": float64(1541152487632),
  795. }},
  796. {{
  797. "m": "mock",
  798. "ts": float64(1541152488442),
  799. }},
  800. },
  801. M: map[string]interface{}{
  802. "sink_mockSink_0_exceptions_total": int64(0),
  803. "sink_mockSink_0_records_in_total": int64(4),
  804. "sink_mockSink_0_records_out_total": int64(4),
  805. "source_demo_0_exceptions_total": int64(0),
  806. "source_demo_0_records_in_total": int64(5),
  807. "source_demo_0_records_out_total": int64(5),
  808. },
  809. },
  810. {
  811. Name: `TestSingleSQLRule6`,
  812. Sql: `SELECT color, ts FROM demo where size > 3 and meta(topic)="mock"`,
  813. R: [][]map[string]interface{}{
  814. {{
  815. "color": "blue",
  816. "ts": float64(1541152486822),
  817. }},
  818. {{
  819. "color": "yellow",
  820. "ts": float64(1541152488442),
  821. }},
  822. },
  823. M: map[string]interface{}{
  824. "op_3_project_0_exceptions_total": int64(0),
  825. "op_3_project_0_process_latency_us": int64(0),
  826. "op_3_project_0_records_in_total": int64(2),
  827. "op_3_project_0_records_out_total": int64(2),
  828. "sink_mockSink_0_exceptions_total": int64(0),
  829. "sink_mockSink_0_records_in_total": int64(2),
  830. "sink_mockSink_0_records_out_total": int64(2),
  831. "source_demo_0_exceptions_total": int64(0),
  832. "source_demo_0_records_in_total": int64(5),
  833. "source_demo_0_records_out_total": int64(5),
  834. "op_2_filter_0_exceptions_total": int64(0),
  835. "op_2_filter_0_process_latency_us": int64(0),
  836. "op_2_filter_0_records_in_total": int64(5),
  837. "op_2_filter_0_records_out_total": int64(2),
  838. },
  839. },
  840. {
  841. Name: `TestSingleSQLRule7`,
  842. Sql: "SELECT `from` FROM demo1",
  843. R: [][]map[string]interface{}{
  844. {{
  845. "from": "device1",
  846. }},
  847. {{
  848. "from": "device2",
  849. }},
  850. {{
  851. "from": "device3",
  852. }},
  853. {{
  854. "from": "device1",
  855. }},
  856. {{
  857. "from": "device3",
  858. }},
  859. },
  860. M: map[string]interface{}{
  861. "op_2_project_0_exceptions_total": int64(0),
  862. "op_2_project_0_process_latency_us": int64(0),
  863. "op_2_project_0_records_in_total": int64(5),
  864. "op_2_project_0_records_out_total": int64(5),
  865. "sink_mockSink_0_exceptions_total": int64(0),
  866. "sink_mockSink_0_records_in_total": int64(5),
  867. "sink_mockSink_0_records_out_total": int64(5),
  868. "source_demo1_0_exceptions_total": int64(0),
  869. "source_demo1_0_records_in_total": int64(5),
  870. "source_demo1_0_records_out_total": int64(5),
  871. },
  872. },
  873. {
  874. Name: `TestSingleSQLRule8`,
  875. Sql: "SELECT * FROM demo1 where `from`=\"device1\"",
  876. R: [][]map[string]interface{}{
  877. {{
  878. "temp": float64(25.5),
  879. "hum": float64(65),
  880. "from": "device1",
  881. "ts": float64(1541152486013),
  882. }},
  883. {{
  884. "temp": float64(27.4),
  885. "hum": float64(80),
  886. "from": "device1",
  887. "ts": float64(1541152488442),
  888. }},
  889. },
  890. M: map[string]interface{}{
  891. "op_3_project_0_exceptions_total": int64(0),
  892. "op_3_project_0_process_latency_us": int64(0),
  893. "op_3_project_0_records_in_total": int64(2),
  894. "op_3_project_0_records_out_total": int64(2),
  895. "op_2_filter_0_exceptions_total": int64(0),
  896. "op_2_filter_0_process_latency_us": int64(0),
  897. "op_2_filter_0_records_in_total": int64(5),
  898. "op_2_filter_0_records_out_total": int64(2),
  899. "sink_mockSink_0_exceptions_total": int64(0),
  900. "sink_mockSink_0_records_in_total": int64(2),
  901. "sink_mockSink_0_records_out_total": int64(2),
  902. "source_demo1_0_exceptions_total": int64(0),
  903. "source_demo1_0_records_in_total": int64(5),
  904. "source_demo1_0_records_out_total": int64(5),
  905. },
  906. },
  907. {
  908. Name: `TestSingleSQLRule9`,
  909. Sql: `SELECT color, CASE WHEN size < 2 THEN "S" WHEN size < 4 THEN "M" ELSE "L" END as s, ts FROM demo`,
  910. R: [][]map[string]interface{}{
  911. {{
  912. "color": "red",
  913. "s": "M",
  914. "ts": float64(1541152486013),
  915. }},
  916. {{
  917. "color": "blue",
  918. "s": "L",
  919. "ts": float64(1541152486822),
  920. }},
  921. {{
  922. "color": "blue",
  923. "s": "M",
  924. "ts": float64(1541152487632),
  925. }},
  926. {{
  927. "color": "yellow",
  928. "s": "L",
  929. "ts": float64(1541152488442),
  930. }},
  931. {{
  932. "color": "red",
  933. "s": "S",
  934. "ts": float64(1541152489252),
  935. }},
  936. },
  937. M: map[string]interface{}{
  938. "op_2_project_0_exceptions_total": int64(0),
  939. "op_2_project_0_process_latency_us": int64(0),
  940. "op_2_project_0_records_in_total": int64(5),
  941. "op_2_project_0_records_out_total": int64(5),
  942. "sink_mockSink_0_exceptions_total": int64(0),
  943. "sink_mockSink_0_records_in_total": int64(5),
  944. "sink_mockSink_0_records_out_total": int64(5),
  945. "source_demo_0_exceptions_total": int64(0),
  946. "source_demo_0_records_in_total": int64(5),
  947. "source_demo_0_records_out_total": int64(5),
  948. },
  949. T: &api.PrintableTopo{
  950. Sources: []string{"source_demo"},
  951. Edges: map[string][]interface{}{
  952. "source_demo": {"op_2_project"},
  953. "op_2_project": {"sink_mockSink"},
  954. },
  955. },
  956. },
  957. {
  958. Name: `TestSingleSQLRule10`,
  959. Sql: "SELECT * FROM demo INNER JOIN table1 on demo.ts = table1.id",
  960. R: [][]map[string]interface{}{
  961. {{
  962. "id": float64(1541152486013),
  963. "name": "name1",
  964. "color": "red",
  965. "size": float64(3),
  966. "ts": float64(1541152486013),
  967. }},
  968. {{
  969. "id": float64(1541152487632),
  970. "name": "name2",
  971. "color": "blue",
  972. "size": float64(2),
  973. "ts": float64(1541152487632),
  974. }},
  975. {{
  976. "id": float64(1541152489252),
  977. "name": "name3",
  978. "color": "red",
  979. "size": float64(1),
  980. "ts": float64(1541152489252),
  981. }},
  982. },
  983. W: 15,
  984. M: map[string]interface{}{
  985. "op_3_join_aligner_0_records_in_total": int64(6),
  986. "op_3_join_aligner_0_records_out_total": int64(5),
  987. "op_4_join_0_exceptions_total": int64(0),
  988. "op_4_join_0_records_in_total": int64(5),
  989. "op_4_join_0_records_out_total": int64(3),
  990. "op_5_project_0_exceptions_total": int64(0),
  991. "op_5_project_0_records_in_total": int64(3),
  992. "op_5_project_0_records_out_total": int64(3),
  993. "sink_mockSink_0_exceptions_total": int64(0),
  994. "sink_mockSink_0_records_in_total": int64(3),
  995. "sink_mockSink_0_records_out_total": int64(3),
  996. "source_demo_0_exceptions_total": int64(0),
  997. "source_demo_0_records_in_total": int64(5),
  998. "source_demo_0_records_out_total": int64(5),
  999. "source_table1_0_exceptions_total": int64(0),
  1000. "source_table1_0_records_in_total": int64(4),
  1001. "source_table1_0_records_out_total": int64(1),
  1002. },
  1003. },
  1004. {
  1005. Name: `TestSingleSQLRule11`,
  1006. Sql: "SELECT device FROM demo INNER JOIN demoTable on demo.ts = demoTable.ts",
  1007. R: [][]map[string]interface{}{
  1008. {{
  1009. "device": "device2",
  1010. }},
  1011. {{
  1012. "device": "device4",
  1013. }},
  1014. {{
  1015. "device": "device5",
  1016. }},
  1017. },
  1018. M: map[string]interface{}{
  1019. "op_3_join_aligner_0_records_in_total": int64(10),
  1020. "op_3_join_aligner_0_records_out_total": int64(5),
  1021. "op_4_join_0_exceptions_total": int64(0),
  1022. "op_4_join_0_records_in_total": int64(5),
  1023. "op_4_join_0_records_out_total": int64(3),
  1024. "op_5_project_0_exceptions_total": int64(0),
  1025. "op_5_project_0_records_in_total": int64(3),
  1026. "op_5_project_0_records_out_total": int64(3),
  1027. "sink_mockSink_0_exceptions_total": int64(0),
  1028. "sink_mockSink_0_records_in_total": int64(3),
  1029. "sink_mockSink_0_records_out_total": int64(3),
  1030. "source_demo_0_exceptions_total": int64(0),
  1031. "source_demo_0_records_in_total": int64(5),
  1032. "source_demo_0_records_out_total": int64(5),
  1033. "source_demoTable_0_exceptions_total": int64(0),
  1034. "source_demoTable_0_records_in_total": int64(5),
  1035. "source_demoTable_0_records_out_total": int64(5),
  1036. },
  1037. },
  1038. {
  1039. Name: `TestSingleSQLRule12`,
  1040. Sql: "SELECT demo.ts as demoTs, table1.id as table1Id FROM demo INNER JOIN table1 on demoTs = table1Id",
  1041. R: [][]map[string]interface{}{
  1042. {{
  1043. "table1Id": float64(1541152486013),
  1044. "demoTs": float64(1541152486013),
  1045. }},
  1046. {{
  1047. "table1Id": float64(1541152487632),
  1048. "demoTs": float64(1541152487632),
  1049. }},
  1050. {{
  1051. "table1Id": float64(1541152489252),
  1052. "demoTs": float64(1541152489252),
  1053. }},
  1054. },
  1055. W: 15,
  1056. M: map[string]interface{}{
  1057. "op_3_join_aligner_0_records_in_total": int64(6),
  1058. "op_3_join_aligner_0_records_out_total": int64(5),
  1059. "op_4_join_0_exceptions_total": int64(0),
  1060. "op_4_join_0_records_in_total": int64(5),
  1061. "op_4_join_0_records_out_total": int64(3),
  1062. "op_5_project_0_exceptions_total": int64(0),
  1063. "op_5_project_0_records_in_total": int64(3),
  1064. "op_5_project_0_records_out_total": int64(3),
  1065. "sink_mockSink_0_exceptions_total": int64(0),
  1066. "sink_mockSink_0_records_in_total": int64(3),
  1067. "sink_mockSink_0_records_out_total": int64(3),
  1068. "source_demo_0_exceptions_total": int64(0),
  1069. "source_demo_0_records_in_total": int64(5),
  1070. "source_demo_0_records_out_total": int64(5),
  1071. "source_table1_0_exceptions_total": int64(0),
  1072. "source_table1_0_records_in_total": int64(4),
  1073. "source_table1_0_records_out_total": int64(1),
  1074. },
  1075. },
  1076. {
  1077. Name: `TestChanged13`,
  1078. Sql: "SELECT changed_cols(\"tt_\", true, color, size) FROM demo",
  1079. R: [][]map[string]interface{}{
  1080. {{
  1081. "tt_color": "red",
  1082. "tt_size": float64(3),
  1083. }},
  1084. {{
  1085. "tt_color": "blue",
  1086. "tt_size": float64(6),
  1087. }},
  1088. {{
  1089. "tt_size": float64(2),
  1090. }},
  1091. {{
  1092. "tt_color": "yellow",
  1093. "tt_size": float64(4),
  1094. }},
  1095. {{
  1096. "tt_color": "red",
  1097. "tt_size": float64(1),
  1098. }},
  1099. },
  1100. M: map[string]interface{}{
  1101. "op_2_project_0_exceptions_total": int64(0),
  1102. "op_2_project_0_process_latency_us": int64(0),
  1103. "op_2_project_0_records_in_total": int64(5),
  1104. "op_2_project_0_records_out_total": int64(5),
  1105. "sink_mockSink_0_exceptions_total": int64(0),
  1106. "sink_mockSink_0_records_in_total": int64(5),
  1107. "sink_mockSink_0_records_out_total": int64(5),
  1108. "source_demo_0_exceptions_total": int64(0),
  1109. "source_demo_0_records_in_total": int64(5),
  1110. "source_demo_0_records_out_total": int64(5),
  1111. },
  1112. },
  1113. {
  1114. Name: `TestAliasOrderBy14`,
  1115. Sql: "SELECT color, count(*) as c FROM demo where color != \"red\" GROUP BY COUNTWINDOW(5), color Order by c DESC",
  1116. R: [][]map[string]interface{}{
  1117. {
  1118. {
  1119. "color": "blue",
  1120. "c": float64(2),
  1121. },
  1122. {
  1123. "color": "yellow",
  1124. "c": float64(1),
  1125. },
  1126. },
  1127. },
  1128. M: map[string]interface{}{
  1129. "op_6_project_0_exceptions_total": int64(0),
  1130. "op_6_project_0_process_latency_us": int64(0),
  1131. "op_6_project_0_records_in_total": int64(1),
  1132. "op_6_project_0_records_out_total": int64(1),
  1133. "sink_mockSink_0_exceptions_total": int64(0),
  1134. "sink_mockSink_0_records_in_total": int64(1),
  1135. "sink_mockSink_0_records_out_total": int64(1),
  1136. "source_demo_0_exceptions_total": int64(0),
  1137. "source_demo_0_records_in_total": int64(5),
  1138. "source_demo_0_records_out_total": int64(5),
  1139. },
  1140. },
  1141. {
  1142. Name: `TestSingleSQLRule17`,
  1143. Sql: `SELECT arr[x:4] as col1 FROM demoArr where x=1`,
  1144. R: [][]map[string]interface{}{
  1145. {{
  1146. "col1": []interface{}{
  1147. float64(2), float64(3), float64(4),
  1148. },
  1149. }},
  1150. },
  1151. },
  1152. {
  1153. Name: `TestSingleSQLRule16`,
  1154. Sql: `SELECT arr[1:y] as col1 FROM demoArr where x=1`,
  1155. R: [][]map[string]interface{}{
  1156. {{
  1157. "col1": []interface{}{
  1158. float64(2),
  1159. },
  1160. }},
  1161. },
  1162. },
  1163. {
  1164. Name: `TestSingleSQLRule15`,
  1165. Sql: `SELECT arr[1] as col1 FROM demoArr where x=1`,
  1166. R: [][]map[string]interface{}{
  1167. {{
  1168. "col1": float64(2),
  1169. }},
  1170. },
  1171. },
  1172. {
  1173. Name: `TestLagAlias`,
  1174. Sql: "SELECT lag(size) as lastSize, lag(had_changed(true,size)), size, lastSize/size as changeRate FROM demo WHERE size > 2",
  1175. R: [][]map[string]interface{}{
  1176. {{
  1177. "size": float64(3),
  1178. }},
  1179. {{
  1180. "lastSize": float64(3),
  1181. "size": float64(6),
  1182. "lag": true,
  1183. "changeRate": float64(0),
  1184. }},
  1185. {{
  1186. "lastSize": float64(2),
  1187. "size": float64(4),
  1188. "lag": true,
  1189. "changeRate": float64(0),
  1190. }},
  1191. },
  1192. M: map[string]interface{}{
  1193. "sink_mockSink_0_exceptions_total": int64(0),
  1194. "sink_mockSink_0_records_in_total": int64(3),
  1195. "sink_mockSink_0_records_out_total": int64(3),
  1196. "source_demo_0_exceptions_total": int64(0),
  1197. "source_demo_0_records_in_total": int64(5),
  1198. "source_demo_0_records_out_total": int64(5),
  1199. },
  1200. },
  1201. {
  1202. Name: `TestLagPartition`,
  1203. Sql: "SELECT color, lag(size) over (partition by color) as lastSize, size, lastSize/size as changeRate FROM demo",
  1204. R: [][]map[string]interface{}{
  1205. {{
  1206. "color": "red",
  1207. "size": float64(3),
  1208. }},
  1209. {{
  1210. "color": "blue",
  1211. "size": float64(6),
  1212. }},
  1213. {{
  1214. "color": "blue",
  1215. "lastSize": float64(6),
  1216. "size": float64(2),
  1217. "changeRate": float64(3),
  1218. }},
  1219. {{
  1220. "color": "yellow",
  1221. "size": float64(4),
  1222. }},
  1223. {{
  1224. "color": "red",
  1225. "lastSize": float64(3),
  1226. "size": float64(1),
  1227. "changeRate": float64(3),
  1228. }},
  1229. },
  1230. M: map[string]interface{}{
  1231. "sink_mockSink_0_exceptions_total": int64(0),
  1232. "sink_mockSink_0_records_in_total": int64(5),
  1233. "sink_mockSink_0_records_out_total": int64(5),
  1234. "source_demo_0_exceptions_total": int64(0),
  1235. "source_demo_0_records_in_total": int64(5),
  1236. "source_demo_0_records_out_total": int64(5),
  1237. },
  1238. },
  1239. }
  1240. HandleStream(true, streamList, t)
  1241. options := []*api.RuleOption{
  1242. {
  1243. BufferLength: 100,
  1244. SendError: true,
  1245. },
  1246. {
  1247. BufferLength: 100,
  1248. SendError: true,
  1249. Qos: api.AtLeastOnce,
  1250. CheckpointInterval: 5000,
  1251. },
  1252. {
  1253. BufferLength: 100,
  1254. SendError: true,
  1255. Qos: api.ExactlyOnce,
  1256. CheckpointInterval: 5000,
  1257. },
  1258. }
  1259. for j, opt := range options {
  1260. DoRuleTest(t, tests, j, opt, 0)
  1261. }
  1262. }
  1263. func TestSingleSQLWithEventTime(t *testing.T) {
  1264. // Reset
  1265. streamList := []string{"demoE"}
  1266. HandleStream(false, streamList, t)
  1267. // Data setup
  1268. tests := []RuleTest{
  1269. {
  1270. Name: `TestSingleSQLRule1`,
  1271. Sql: `SELECT *, upper(color) FROM demoE`,
  1272. R: [][]map[string]interface{}{
  1273. {{
  1274. "color": "red",
  1275. "size": float64(3),
  1276. "ts": float64(1541152486013),
  1277. "upper": "RED",
  1278. }},
  1279. {{
  1280. "color": "blue",
  1281. "size": float64(2),
  1282. "ts": float64(1541152487632),
  1283. "upper": "BLUE",
  1284. }},
  1285. {{
  1286. "color": "yellow",
  1287. "size": float64(4),
  1288. "ts": float64(1541152488442),
  1289. "upper": "YELLOW",
  1290. }},
  1291. {{
  1292. "color": "red",
  1293. "size": float64(1),
  1294. "ts": float64(1541152489252),
  1295. "upper": "RED",
  1296. }},
  1297. },
  1298. M: map[string]interface{}{
  1299. "op_3_project_0_exceptions_total": int64(0),
  1300. "op_3_project_0_process_latency_us": int64(0),
  1301. "op_3_project_0_records_in_total": int64(4),
  1302. "op_3_project_0_records_out_total": int64(4),
  1303. "sink_mockSink_0_exceptions_total": int64(0),
  1304. "sink_mockSink_0_records_in_total": int64(4),
  1305. "sink_mockSink_0_records_out_total": int64(4),
  1306. "source_demoE_0_exceptions_total": int64(0),
  1307. "source_demoE_0_records_in_total": int64(6),
  1308. "source_demoE_0_records_out_total": int64(6),
  1309. },
  1310. T: &api.PrintableTopo{
  1311. Sources: []string{"source_demoE"},
  1312. Edges: map[string][]interface{}{
  1313. "source_demoE": {"op_2_watermark"},
  1314. "op_2_watermark": {"op_3_project"},
  1315. "op_3_project": {"sink_mockSink"},
  1316. },
  1317. },
  1318. },
  1319. {
  1320. Name: `TestStateFunc`,
  1321. Sql: `SELECT *, last_hit_time() as lt, last_hit_count() as lc, event_time() as et FROM demoE WHERE size < 3 AND lc < 2`,
  1322. R: [][]map[string]interface{}{
  1323. {{
  1324. "color": "blue",
  1325. "size": float64(2),
  1326. "ts": float64(1541152487632),
  1327. "lc": float64(0),
  1328. "lt": float64(0),
  1329. "et": float64(1541152487632),
  1330. }},
  1331. {{
  1332. "color": "red",
  1333. "size": float64(1),
  1334. "ts": float64(1541152489252),
  1335. "lc": float64(1),
  1336. "lt": float64(1541152487632),
  1337. "et": float64(1541152489252),
  1338. }},
  1339. },
  1340. M: map[string]interface{}{
  1341. "sink_mockSink_0_exceptions_total": int64(0),
  1342. "sink_mockSink_0_records_in_total": int64(2),
  1343. "sink_mockSink_0_records_out_total": int64(2),
  1344. "source_demoE_0_exceptions_total": int64(0),
  1345. "source_demoE_0_records_in_total": int64(6),
  1346. "source_demoE_0_records_out_total": int64(6),
  1347. },
  1348. },
  1349. {
  1350. Name: `TestChanged`,
  1351. Sql: "SELECT changed_cols(\"tt_\", true, color, size) FROM demoE",
  1352. R: [][]map[string]interface{}{
  1353. {{
  1354. "tt_color": "red",
  1355. "tt_size": float64(3),
  1356. }},
  1357. {{
  1358. "tt_color": "blue",
  1359. "tt_size": float64(2),
  1360. }},
  1361. {{
  1362. "tt_color": "yellow",
  1363. "tt_size": float64(4),
  1364. }},
  1365. {{
  1366. "tt_color": "red",
  1367. "tt_size": float64(1),
  1368. }},
  1369. },
  1370. M: map[string]interface{}{
  1371. "op_3_project_0_exceptions_total": int64(0),
  1372. "op_3_project_0_process_latency_us": int64(0),
  1373. "op_3_project_0_records_in_total": int64(4),
  1374. "op_3_project_0_records_out_total": int64(4),
  1375. "sink_mockSink_0_exceptions_total": int64(0),
  1376. "sink_mockSink_0_records_in_total": int64(4),
  1377. "sink_mockSink_0_records_out_total": int64(4),
  1378. "source_demoE_0_exceptions_total": int64(0),
  1379. "source_demoE_0_records_in_total": int64(6),
  1380. "source_demoE_0_records_out_total": int64(6),
  1381. },
  1382. },
  1383. }
  1384. HandleStream(true, streamList, t)
  1385. options := []*api.RuleOption{
  1386. {
  1387. BufferLength: 100,
  1388. SendError: true,
  1389. IsEventTime: true,
  1390. LateTol: 1000,
  1391. }, {
  1392. BufferLength: 100,
  1393. SendError: true,
  1394. Qos: api.AtLeastOnce,
  1395. CheckpointInterval: 5000,
  1396. IsEventTime: true,
  1397. LateTol: 1000,
  1398. }, {
  1399. BufferLength: 100,
  1400. SendError: true,
  1401. Qos: api.ExactlyOnce,
  1402. CheckpointInterval: 5000,
  1403. IsEventTime: true,
  1404. LateTol: 1000,
  1405. },
  1406. }
  1407. for j, opt := range options {
  1408. DoRuleTest(t, tests, j, opt, 0)
  1409. }
  1410. }
  1411. func TestSingleSQLError(t *testing.T) {
  1412. // Reset
  1413. streamList := []string{"ldemo"}
  1414. HandleStream(false, streamList, t)
  1415. // Data setup
  1416. tests := []RuleTest{
  1417. {
  1418. Name: `TestSingleSQLErrorRule1`,
  1419. Sql: `SELECT color, ts FROM ldemo where size >= 3`,
  1420. R: [][]map[string]interface{}{
  1421. {{
  1422. "color": "red",
  1423. "ts": float64(1541152486013),
  1424. }},
  1425. {{
  1426. "error": "run Where error: invalid operation string(string) >= int64(3)",
  1427. }},
  1428. {{
  1429. "ts": float64(1541152487632),
  1430. }},
  1431. },
  1432. M: map[string]interface{}{
  1433. "op_3_project_0_exceptions_total": int64(1),
  1434. "op_3_project_0_process_latency_us": int64(0),
  1435. "op_3_project_0_records_in_total": int64(2),
  1436. "op_3_project_0_records_out_total": int64(2),
  1437. "sink_mockSink_0_exceptions_total": int64(0),
  1438. "sink_mockSink_0_records_in_total": int64(3),
  1439. "sink_mockSink_0_records_out_total": int64(3),
  1440. "source_ldemo_0_exceptions_total": int64(0),
  1441. "source_ldemo_0_records_in_total": int64(5),
  1442. "source_ldemo_0_records_out_total": int64(5),
  1443. "op_2_filter_0_exceptions_total": int64(1),
  1444. "op_2_filter_0_process_latency_us": int64(0),
  1445. "op_2_filter_0_records_in_total": int64(5),
  1446. "op_2_filter_0_records_out_total": int64(2),
  1447. },
  1448. }, {
  1449. Name: `TestSingleSQLErrorRule2`,
  1450. Sql: `SELECT size * 5 FROM ldemo`,
  1451. R: [][]map[string]interface{}{
  1452. {{
  1453. "kuiper_field_0": float64(15),
  1454. }},
  1455. {{
  1456. "error": "run Select error: invalid operation string(string) * int64(5)",
  1457. }},
  1458. {{
  1459. "kuiper_field_0": float64(15),
  1460. }},
  1461. {{
  1462. "kuiper_field_0": float64(10),
  1463. }},
  1464. {{}},
  1465. },
  1466. M: map[string]interface{}{
  1467. "op_2_project_0_exceptions_total": int64(1),
  1468. "op_2_project_0_process_latency_us": int64(0),
  1469. "op_2_project_0_records_in_total": int64(5),
  1470. "op_2_project_0_records_out_total": int64(4),
  1471. "sink_mockSink_0_exceptions_total": int64(0),
  1472. "sink_mockSink_0_records_in_total": int64(5),
  1473. "sink_mockSink_0_records_out_total": int64(5),
  1474. "source_ldemo_0_exceptions_total": int64(0),
  1475. "source_ldemo_0_records_in_total": int64(5),
  1476. "source_ldemo_0_records_out_total": int64(5),
  1477. },
  1478. },
  1479. }
  1480. HandleStream(true, streamList, t)
  1481. DoRuleTest(t, tests, 0, &api.RuleOption{
  1482. BufferLength: 100,
  1483. SendError: true,
  1484. }, 0)
  1485. }
  1486. func TestSingleSQLOmitError(t *testing.T) {
  1487. // Reset
  1488. streamList := []string{"ldemo"}
  1489. HandleStream(false, streamList, t)
  1490. // Data setup
  1491. tests := []RuleTest{
  1492. {
  1493. Name: `TestSingleSQLErrorRule1`,
  1494. Sql: `SELECT color, ts FROM ldemo where size >= 3`,
  1495. R: [][]map[string]interface{}{
  1496. {{
  1497. "color": "red",
  1498. "ts": float64(1541152486013),
  1499. }},
  1500. {{
  1501. "ts": float64(1541152487632),
  1502. }},
  1503. },
  1504. M: map[string]interface{}{
  1505. "op_3_project_0_exceptions_total": int64(0),
  1506. "op_3_project_0_process_latency_us": int64(0),
  1507. "op_3_project_0_records_in_total": int64(2),
  1508. "op_3_project_0_records_out_total": int64(2),
  1509. "sink_mockSink_0_exceptions_total": int64(0),
  1510. "sink_mockSink_0_records_in_total": int64(2),
  1511. "sink_mockSink_0_records_out_total": int64(2),
  1512. "source_ldemo_0_exceptions_total": int64(0),
  1513. "source_ldemo_0_records_in_total": int64(5),
  1514. "source_ldemo_0_records_out_total": int64(5),
  1515. "op_2_filter_0_exceptions_total": int64(1),
  1516. "op_2_filter_0_process_latency_us": int64(0),
  1517. "op_2_filter_0_records_in_total": int64(5),
  1518. "op_2_filter_0_records_out_total": int64(2),
  1519. },
  1520. }, {
  1521. Name: `TestSingleSQLErrorRule2`,
  1522. Sql: `SELECT size * 5 FROM ldemo`,
  1523. R: [][]map[string]interface{}{
  1524. {{
  1525. "kuiper_field_0": float64(15),
  1526. }},
  1527. {{
  1528. "kuiper_field_0": float64(15),
  1529. }},
  1530. {{
  1531. "kuiper_field_0": float64(10),
  1532. }},
  1533. {{}},
  1534. },
  1535. M: map[string]interface{}{
  1536. "op_2_project_0_exceptions_total": int64(1),
  1537. "op_2_project_0_process_latency_us": int64(0),
  1538. "op_2_project_0_records_in_total": int64(5),
  1539. "op_2_project_0_records_out_total": int64(4),
  1540. "sink_mockSink_0_exceptions_total": int64(0),
  1541. "sink_mockSink_0_records_in_total": int64(4),
  1542. "sink_mockSink_0_records_out_total": int64(4),
  1543. "source_ldemo_0_exceptions_total": int64(0),
  1544. "source_ldemo_0_records_in_total": int64(5),
  1545. "source_ldemo_0_records_out_total": int64(5),
  1546. },
  1547. },
  1548. }
  1549. HandleStream(true, streamList, t)
  1550. DoRuleTest(t, tests, 0, &api.RuleOption{
  1551. BufferLength: 100,
  1552. SendError: false,
  1553. }, 0)
  1554. }
  1555. func TestSingleSQLTemplate(t *testing.T) {
  1556. // Reset
  1557. streamList := []string{"demo"}
  1558. HandleStream(false, streamList, t)
  1559. // Data setup
  1560. tests := []RuleTest{
  1561. {
  1562. Name: `TestSingleSQLTemplateRule1`,
  1563. Sql: `SELECT * FROM demo`,
  1564. R: []map[string]interface{}{
  1565. {
  1566. "c": "red",
  1567. "wrapper": "w1",
  1568. },
  1569. {
  1570. "c": "blue",
  1571. "wrapper": "w1",
  1572. },
  1573. {
  1574. "c": "blue",
  1575. "wrapper": "w1",
  1576. },
  1577. {
  1578. "c": "yellow",
  1579. "wrapper": "w1",
  1580. },
  1581. {
  1582. "c": "red",
  1583. "wrapper": "w1",
  1584. },
  1585. },
  1586. M: map[string]interface{}{
  1587. "op_2_project_0_exceptions_total": int64(0),
  1588. "op_2_project_0_process_latency_us": int64(0),
  1589. "op_2_project_0_records_in_total": int64(5),
  1590. "op_2_project_0_records_out_total": int64(5),
  1591. "sink_mockSink_0_exceptions_total": int64(0),
  1592. "sink_mockSink_0_records_in_total": int64(5),
  1593. "sink_mockSink_0_records_out_total": int64(5),
  1594. "source_demo_0_exceptions_total": int64(0),
  1595. "source_demo_0_records_in_total": int64(5),
  1596. "source_demo_0_records_out_total": int64(5),
  1597. },
  1598. },
  1599. }
  1600. HandleStream(true, streamList, t)
  1601. doRuleTestBySinkProps(t, tests, 0, &api.RuleOption{
  1602. BufferLength: 100,
  1603. SendError: true,
  1604. }, 0, map[string]interface{}{
  1605. "dataTemplate": `{"wrapper":"w1", "c":"{{.color}}"}`,
  1606. "sendSingle": true,
  1607. }, func(result [][]byte) interface{} {
  1608. var maps []map[string]interface{}
  1609. for _, v := range result {
  1610. var mapRes map[string]interface{}
  1611. err := json.Unmarshal(v, &mapRes)
  1612. if err != nil {
  1613. t.Errorf("Failed to parse the input into map")
  1614. continue
  1615. }
  1616. maps = append(maps, mapRes)
  1617. }
  1618. return maps
  1619. })
  1620. }
  1621. func TestNoneSingleSQLTemplate(t *testing.T) {
  1622. // Reset
  1623. streamList := []string{"demo"}
  1624. HandleStream(false, streamList, t)
  1625. // Data setup
  1626. tests := []RuleTest{
  1627. {
  1628. Name: `TestNoneSingleSQLTemplateRule1`,
  1629. Sql: `SELECT * FROM demo`,
  1630. R: [][]byte{
  1631. []byte("<div>results</div><ul><li>red - 3</li></ul>"),
  1632. []byte("<div>results</div><ul><li>blue - 6</li></ul>"),
  1633. []byte("<div>results</div><ul><li>blue - 2</li></ul>"),
  1634. []byte("<div>results</div><ul><li>yellow - 4</li></ul>"),
  1635. []byte("<div>results</div><ul><li>red - 1</li></ul>"),
  1636. },
  1637. M: map[string]interface{}{
  1638. "op_2_project_0_exceptions_total": int64(0),
  1639. "op_2_project_0_process_latency_us": int64(0),
  1640. "op_2_project_0_records_in_total": int64(5),
  1641. "op_2_project_0_records_out_total": int64(5),
  1642. "sink_mockSink_0_exceptions_total": int64(0),
  1643. "sink_mockSink_0_records_in_total": int64(5),
  1644. "sink_mockSink_0_records_out_total": int64(5),
  1645. "source_demo_0_exceptions_total": int64(0),
  1646. "source_demo_0_records_in_total": int64(5),
  1647. "source_demo_0_records_out_total": int64(5),
  1648. },
  1649. },
  1650. }
  1651. HandleStream(true, streamList, t)
  1652. doRuleTestBySinkProps(t, tests, 0, &api.RuleOption{
  1653. BufferLength: 100,
  1654. SendError: true,
  1655. }, 0, map[string]interface{}{
  1656. "dataTemplate": `<div>results</div><ul>{{range .}}<li>{{.color}} - {{.size}}</li>{{end}}</ul>`,
  1657. }, func(result [][]byte) interface{} {
  1658. return result
  1659. })
  1660. }
  1661. func TestSingleSQLForBinary(t *testing.T) {
  1662. // Reset
  1663. streamList := []string{"binDemo"}
  1664. HandleStream(false, streamList, t)
  1665. // Data setup
  1666. tests := []RuleTest{
  1667. {
  1668. Name: `TestSingleSQLRule1`,
  1669. Sql: `SELECT * FROM binDemo`,
  1670. R: [][]map[string]interface{}{
  1671. {{
  1672. "self": mocknode.Image,
  1673. }},
  1674. },
  1675. W: 50,
  1676. M: map[string]interface{}{
  1677. "op_2_project_0_exceptions_total": int64(0),
  1678. "op_2_project_0_process_latency_us": int64(0),
  1679. "op_2_project_0_records_in_total": int64(1),
  1680. "op_2_project_0_records_out_total": int64(1),
  1681. "sink_mockSink_0_exceptions_total": int64(0),
  1682. "sink_mockSink_0_records_in_total": int64(1),
  1683. "sink_mockSink_0_records_out_total": int64(1),
  1684. "source_binDemo_0_exceptions_total": int64(0),
  1685. "source_binDemo_0_records_in_total": int64(1),
  1686. "source_binDemo_0_records_out_total": int64(1),
  1687. },
  1688. },
  1689. }
  1690. HandleStream(true, streamList, t)
  1691. options := []*api.RuleOption{
  1692. {
  1693. BufferLength: 100,
  1694. SendError: true,
  1695. }, {
  1696. BufferLength: 100,
  1697. SendError: true,
  1698. Qos: api.AtLeastOnce,
  1699. CheckpointInterval: 5000,
  1700. }, {
  1701. BufferLength: 100,
  1702. SendError: true,
  1703. Qos: api.ExactlyOnce,
  1704. CheckpointInterval: 5000,
  1705. },
  1706. }
  1707. byteFunc := func(result [][]byte) interface{} {
  1708. var maps [][]map[string]interface{}
  1709. for _, v := range result {
  1710. var mapRes []map[string][]byte
  1711. err := json.Unmarshal(v, &mapRes)
  1712. if err != nil {
  1713. panic("Failed to parse the input into map")
  1714. }
  1715. mapInt := make([]map[string]interface{}, len(mapRes))
  1716. for i, mv := range mapRes {
  1717. mapInt[i] = make(map[string]interface{})
  1718. // assume only one key
  1719. for k, v := range mv {
  1720. mapInt[i][k] = v
  1721. }
  1722. }
  1723. maps = append(maps, mapInt)
  1724. }
  1725. return maps
  1726. }
  1727. for j, opt := range options {
  1728. doRuleTestBySinkProps(t, tests, j, opt, 0, nil, byteFunc)
  1729. }
  1730. }
  1731. func TestWindowSQL(t *testing.T) {
  1732. // Reset
  1733. streamList := []string{"demoE"}
  1734. HandleStream(false, streamList, t)
  1735. tests := []RuleTest{
  1736. {
  1737. Name: "TestHoppingWindowSQL1",
  1738. Sql: `select size,color from demoE GROUP BY HOPPINGWINDOW(ss, 3, 5)`,
  1739. R: [][]map[string]interface{}{
  1740. {
  1741. {
  1742. "color": "blue",
  1743. "size": float64(2),
  1744. },
  1745. {
  1746. "color": "red",
  1747. "size": float64(1),
  1748. },
  1749. },
  1750. },
  1751. },
  1752. {
  1753. Name: "TestHoppingWindowSQL2",
  1754. Sql: `select size,color from demoE GROUP BY HOPPINGWINDOW(ss, 1, 2)`,
  1755. R: [][]map[string]interface{}{
  1756. {
  1757. {
  1758. "color": "blue",
  1759. "size": float64(2),
  1760. },
  1761. },
  1762. {
  1763. {
  1764. "color": "red",
  1765. "size": float64(1),
  1766. },
  1767. },
  1768. {},
  1769. },
  1770. },
  1771. {
  1772. Name: "TestHoppingWindowSQL3",
  1773. Sql: `select size,color from demoE GROUP BY HOPPINGWINDOW(ss, 2, 5)`,
  1774. R: [][]map[string]interface{}{
  1775. {
  1776. {
  1777. "color": "red",
  1778. "size": float64(1),
  1779. },
  1780. },
  1781. },
  1782. },
  1783. }
  1784. // Data setup
  1785. HandleStream(true, streamList, t)
  1786. options := []*api.RuleOption{
  1787. {
  1788. BufferLength: 100,
  1789. SendError: true,
  1790. Qos: api.AtLeastOnce,
  1791. CheckpointInterval: 5000,
  1792. IsEventTime: true,
  1793. },
  1794. {
  1795. BufferLength: 100,
  1796. SendError: true,
  1797. Qos: api.ExactlyOnce,
  1798. CheckpointInterval: 5000,
  1799. IsEventTime: true,
  1800. },
  1801. }
  1802. for j, opt := range options {
  1803. DoRuleTest(t, tests, j, opt, 0)
  1804. }
  1805. }
  1806. func TestAliasSQL(t *testing.T) {
  1807. streamList := []string{"demo"}
  1808. HandleStream(false, streamList, t)
  1809. tests := []RuleTest{
  1810. {
  1811. Name: "TestAliasSQL1",
  1812. Sql: `select size as a, a + 1 as b from demo`,
  1813. R: [][]map[string]interface{}{
  1814. {
  1815. {
  1816. "a": float64(3),
  1817. "b": float64(4),
  1818. },
  1819. },
  1820. {
  1821. {
  1822. "a": float64(6),
  1823. "b": float64(7),
  1824. },
  1825. },
  1826. {
  1827. {
  1828. "a": float64(2),
  1829. "b": float64(3),
  1830. },
  1831. },
  1832. {
  1833. {
  1834. "a": float64(4),
  1835. "b": float64(5),
  1836. },
  1837. },
  1838. {
  1839. {
  1840. "a": float64(1),
  1841. "b": float64(2),
  1842. },
  1843. },
  1844. },
  1845. },
  1846. {
  1847. Name: "TestAliasSQL2",
  1848. Sql: `select a + 1 as b, size as a from demo`,
  1849. R: [][]map[string]interface{}{
  1850. {
  1851. {
  1852. "a": float64(3),
  1853. "b": float64(4),
  1854. },
  1855. },
  1856. {
  1857. {
  1858. "a": float64(6),
  1859. "b": float64(7),
  1860. },
  1861. },
  1862. {
  1863. {
  1864. "a": float64(2),
  1865. "b": float64(3),
  1866. },
  1867. },
  1868. {
  1869. {
  1870. "a": float64(4),
  1871. "b": float64(5),
  1872. },
  1873. },
  1874. {
  1875. {
  1876. "a": float64(1),
  1877. "b": float64(2),
  1878. },
  1879. },
  1880. },
  1881. },
  1882. }
  1883. // Data setup
  1884. HandleStream(true, streamList, t)
  1885. options := []*api.RuleOption{
  1886. {
  1887. BufferLength: 100,
  1888. SendError: true,
  1889. Qos: api.AtLeastOnce,
  1890. CheckpointInterval: 5000,
  1891. },
  1892. {
  1893. BufferLength: 100,
  1894. SendError: true,
  1895. Qos: api.ExactlyOnce,
  1896. CheckpointInterval: 5000,
  1897. },
  1898. }
  1899. for j, opt := range options {
  1900. DoRuleTest(t, tests, j, opt, 0)
  1901. }
  1902. }