rule_test.go 47 KB

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