rule_test.go 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255
  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 TestSRFSQL(t *testing.T) {
  22. // Reset
  23. streamList := []string{"demo", "demoArr"}
  24. HandleStream(false, streamList, t)
  25. tests := []RuleTest{
  26. {
  27. Name: "TestSingleSQLRule25",
  28. Sql: "SELECT unnest(a) from demoArr group by SESSIONWINDOW(ss, 2, 1);",
  29. R: [][]map[string]interface{}{
  30. {
  31. {
  32. "error": "the argument for the unnest function should be array",
  33. },
  34. },
  35. },
  36. },
  37. {
  38. Name: "TestSingleSQLRule24",
  39. Sql: "Select unnest(a) from demoArr;",
  40. R: [][]map[string]interface{}{
  41. {
  42. {
  43. "error": "the argument for the unnest function should be array",
  44. },
  45. },
  46. },
  47. },
  48. {
  49. Name: "TestSingleSQLRule21",
  50. 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);`,
  51. R: [][]map[string]interface{}{
  52. {
  53. {
  54. "col": float64(1),
  55. "size": float64(1),
  56. },
  57. {
  58. "col": float64(2),
  59. "size": float64(1),
  60. },
  61. {
  62. "col": float64(3),
  63. "size": float64(1),
  64. },
  65. },
  66. },
  67. },
  68. {
  69. Name: "TestSingleSQLRule22",
  70. Sql: `SELECT unnest(arr3) as col,y From demoArr group by y, SESSIONWINDOW(ss, 2, 1);`,
  71. R: [][]map[string]interface{}{
  72. {
  73. {
  74. "col": float64(1),
  75. "y": float64(2),
  76. },
  77. {
  78. "col": float64(2),
  79. "y": float64(2),
  80. },
  81. {
  82. "col": float64(3),
  83. "y": float64(2),
  84. },
  85. },
  86. },
  87. },
  88. {
  89. Name: "TestSingleSQLRule23",
  90. Sql: "SELECT unnest(arr3) as col,a from demoArr group by SESSIONWINDOW(ss, 2, 1);",
  91. R: [][]map[string]interface{}{
  92. {
  93. {
  94. "col": float64(1),
  95. "a": float64(6),
  96. },
  97. {
  98. "col": float64(2),
  99. "a": float64(6),
  100. },
  101. {
  102. "col": float64(3),
  103. "a": float64(6),
  104. },
  105. },
  106. },
  107. },
  108. {
  109. Name: `TestSingleSQLRule18`,
  110. Sql: `SELECT unnest(arr2) FROM demoArr where x=1`,
  111. R: [][]map[string]interface{}{
  112. {
  113. {
  114. "a": float64(1),
  115. "b": float64(2),
  116. },
  117. },
  118. {
  119. {
  120. "a": float64(3),
  121. "b": float64(4),
  122. },
  123. },
  124. },
  125. },
  126. // The mapping schema created by unnest function will cover the original column if they have the same column name
  127. {
  128. Name: `TestSingleSQLRule19`,
  129. Sql: `SELECT unnest(arr2),a FROM demoArr where x=1`,
  130. R: [][]map[string]interface{}{
  131. {
  132. {
  133. "a": float64(1),
  134. "b": float64(2),
  135. },
  136. },
  137. {
  138. {
  139. "a": float64(3),
  140. "b": float64(4),
  141. },
  142. },
  143. },
  144. },
  145. {
  146. Name: `TestSingleSQLRule20`,
  147. Sql: `SELECT unnest(arr3) as col FROM demoArr where x=1`,
  148. R: [][]map[string]interface{}{
  149. {
  150. {
  151. "col": float64(1),
  152. },
  153. },
  154. {
  155. {
  156. "col": float64(2),
  157. },
  158. },
  159. {
  160. {
  161. "col": float64(3),
  162. },
  163. },
  164. },
  165. },
  166. {
  167. Name: `TestSingleSQLRule21`,
  168. Sql: `SELECT unnest(arr2),x FROM demoArr where x=1`,
  169. R: [][]map[string]interface{}{
  170. {
  171. {
  172. "a": float64(1),
  173. "b": float64(2),
  174. "x": float64(1),
  175. },
  176. },
  177. {
  178. {
  179. "a": float64(3),
  180. "b": float64(4),
  181. "x": float64(1),
  182. },
  183. },
  184. },
  185. },
  186. }
  187. // Data setup
  188. HandleStream(true, streamList, t)
  189. options := []*api.RuleOption{
  190. {
  191. BufferLength: 100,
  192. SendError: true,
  193. }, {
  194. BufferLength: 100,
  195. SendError: true,
  196. Qos: api.AtLeastOnce,
  197. CheckpointInterval: 5000,
  198. }, {
  199. BufferLength: 100,
  200. SendError: true,
  201. Qos: api.ExactlyOnce,
  202. CheckpointInterval: 5000,
  203. },
  204. }
  205. for j, opt := range options {
  206. DoRuleTest(t, tests, j, opt, 0)
  207. }
  208. }
  209. func TestSingleSQL(t *testing.T) {
  210. // Reset
  211. streamList := []string{"demo", "demoError", "demo1", "table1", "demoTable", "demoArr"}
  212. HandleStream(false, streamList, t)
  213. // Data setup
  214. tests := []RuleTest{
  215. {
  216. Name: `TestSingleSQLRule0`,
  217. Sql: `SELECT arr[x:y+1] as col1 FROM demoArr where x=1`,
  218. R: [][]map[string]interface{}{
  219. {{
  220. "col1": []interface{}{
  221. float64(2), float64(3),
  222. },
  223. }},
  224. },
  225. },
  226. {
  227. Name: `TestSingleSQLRule1`,
  228. Sql: `SELECT *, upper(color) FROM demo`,
  229. R: [][]map[string]interface{}{
  230. {{
  231. "color": "red",
  232. "size": float64(3),
  233. "ts": float64(1541152486013),
  234. "upper": "RED",
  235. }},
  236. {{
  237. "color": "blue",
  238. "size": float64(6),
  239. "ts": float64(1541152486822),
  240. "upper": "BLUE",
  241. }},
  242. {{
  243. "color": "blue",
  244. "size": float64(2),
  245. "ts": float64(1541152487632),
  246. "upper": "BLUE",
  247. }},
  248. {{
  249. "color": "yellow",
  250. "size": float64(4),
  251. "ts": float64(1541152488442),
  252. "upper": "YELLOW",
  253. }},
  254. {{
  255. "color": "red",
  256. "size": float64(1),
  257. "ts": float64(1541152489252),
  258. "upper": "RED",
  259. }},
  260. },
  261. M: map[string]interface{}{
  262. "op_2_project_0_exceptions_total": int64(0),
  263. "op_2_project_0_process_latency_us": int64(0),
  264. "op_2_project_0_records_in_total": int64(5),
  265. "op_2_project_0_records_out_total": int64(5),
  266. "sink_mockSink_0_exceptions_total": int64(0),
  267. "sink_mockSink_0_records_in_total": int64(5),
  268. "sink_mockSink_0_records_out_total": int64(5),
  269. "source_demo_0_exceptions_total": int64(0),
  270. "source_demo_0_records_in_total": int64(5),
  271. "source_demo_0_records_out_total": int64(5),
  272. },
  273. T: &api.PrintableTopo{
  274. Sources: []string{"source_demo"},
  275. Edges: map[string][]interface{}{
  276. "source_demo": {"op_2_project"},
  277. "op_2_project": {"sink_mockSink"},
  278. },
  279. },
  280. },
  281. {
  282. Name: `TestSingleSQLRule2`,
  283. Sql: `SELECT color, ts FROM demo where size > 3`,
  284. R: [][]map[string]interface{}{
  285. {{
  286. "color": "blue",
  287. "ts": float64(1541152486822),
  288. }},
  289. {{
  290. "color": "yellow",
  291. "ts": float64(1541152488442),
  292. }},
  293. },
  294. M: map[string]interface{}{
  295. "op_3_project_0_exceptions_total": int64(0),
  296. "op_3_project_0_process_latency_us": int64(0),
  297. "op_3_project_0_records_in_total": int64(2),
  298. "op_3_project_0_records_out_total": int64(2),
  299. "sink_mockSink_0_exceptions_total": int64(0),
  300. "sink_mockSink_0_records_in_total": int64(2),
  301. "sink_mockSink_0_records_out_total": int64(2),
  302. "source_demo_0_exceptions_total": int64(0),
  303. "source_demo_0_records_in_total": int64(5),
  304. "source_demo_0_records_out_total": int64(5),
  305. "op_2_filter_0_exceptions_total": int64(0),
  306. "op_2_filter_0_process_latency_us": int64(0),
  307. "op_2_filter_0_records_in_total": int64(5),
  308. "op_2_filter_0_records_out_total": int64(2),
  309. },
  310. },
  311. {
  312. Name: `TestSingleSQLRule3`,
  313. Sql: `SELECT size as Int8, ts FROM demo where size > 3`,
  314. R: [][]map[string]interface{}{
  315. {{
  316. "Int8": float64(6),
  317. "ts": float64(1541152486822),
  318. }},
  319. {{
  320. "Int8": float64(4),
  321. "ts": float64(1541152488442),
  322. }},
  323. },
  324. M: map[string]interface{}{
  325. "op_3_project_0_exceptions_total": int64(0),
  326. "op_3_project_0_process_latency_us": int64(0),
  327. "op_3_project_0_records_in_total": int64(2),
  328. "op_3_project_0_records_out_total": int64(2),
  329. "sink_mockSink_0_exceptions_total": int64(0),
  330. "sink_mockSink_0_records_in_total": int64(2),
  331. "sink_mockSink_0_records_out_total": int64(2),
  332. "source_demo_0_exceptions_total": int64(0),
  333. "source_demo_0_records_in_total": int64(5),
  334. "source_demo_0_records_out_total": int64(5),
  335. "op_2_filter_0_exceptions_total": int64(0),
  336. "op_2_filter_0_process_latency_us": int64(0),
  337. "op_2_filter_0_records_in_total": int64(5),
  338. "op_2_filter_0_records_out_total": int64(2),
  339. },
  340. },
  341. {
  342. Name: `TestSingleSQLRule4`,
  343. Sql: `SELECT size as Int8, ts FROM demoError where size > 3`,
  344. R: [][]map[string]interface{}{
  345. {{
  346. "error": "error in preprocessor: field size type mismatch: cannot convert string(red) to int64",
  347. }},
  348. {{
  349. "Int8": float64(6),
  350. "ts": float64(1541152486822),
  351. }},
  352. {{
  353. "Int8": float64(4),
  354. "ts": float64(1541152488442),
  355. }},
  356. {{
  357. "error": "error in preprocessor: field size type mismatch: cannot convert string(blue) to int64",
  358. }},
  359. },
  360. M: map[string]interface{}{
  361. "op_3_project_0_exceptions_total": int64(2),
  362. "op_3_project_0_process_latency_us": int64(0),
  363. "op_3_project_0_records_in_total": int64(4),
  364. "op_3_project_0_records_out_total": int64(2),
  365. "sink_mockSink_0_exceptions_total": int64(0),
  366. "sink_mockSink_0_records_in_total": int64(4),
  367. "sink_mockSink_0_records_out_total": int64(4),
  368. "source_demoError_0_exceptions_total": int64(2),
  369. "source_demoError_0_records_in_total": int64(5),
  370. "source_demoError_0_records_out_total": int64(5),
  371. "op_2_filter_0_exceptions_total": int64(2),
  372. "op_2_filter_0_process_latency_us": int64(0),
  373. "op_2_filter_0_records_in_total": int64(5),
  374. "op_2_filter_0_records_out_total": int64(2),
  375. },
  376. },
  377. {
  378. Name: `TestSingleSQLRule5`,
  379. Sql: `SELECT meta(topic) as m, ts FROM demo`,
  380. R: [][]map[string]interface{}{
  381. {{
  382. "m": "mock",
  383. "ts": float64(1541152486013),
  384. }},
  385. {{
  386. "m": "mock",
  387. "ts": float64(1541152486822),
  388. }},
  389. {{
  390. "m": "mock",
  391. "ts": float64(1541152487632),
  392. }},
  393. {{
  394. "m": "mock",
  395. "ts": float64(1541152488442),
  396. }},
  397. {{
  398. "m": "mock",
  399. "ts": float64(1541152489252),
  400. }},
  401. },
  402. M: map[string]interface{}{
  403. "op_2_project_0_exceptions_total": int64(0),
  404. "op_2_project_0_process_latency_us": int64(0),
  405. "op_2_project_0_records_in_total": int64(5),
  406. "op_2_project_0_records_out_total": int64(5),
  407. "sink_mockSink_0_exceptions_total": int64(0),
  408. "sink_mockSink_0_records_in_total": int64(5),
  409. "sink_mockSink_0_records_out_total": int64(5),
  410. "source_demo_0_exceptions_total": int64(0),
  411. "source_demo_0_records_in_total": int64(5),
  412. "source_demo_0_records_out_total": int64(5),
  413. },
  414. },
  415. {
  416. Name: `TestSingleSQLRule6`,
  417. Sql: `SELECT color, ts FROM demo where size > 3 and meta(topic)="mock"`,
  418. R: [][]map[string]interface{}{
  419. {{
  420. "color": "blue",
  421. "ts": float64(1541152486822),
  422. }},
  423. {{
  424. "color": "yellow",
  425. "ts": float64(1541152488442),
  426. }},
  427. },
  428. M: map[string]interface{}{
  429. "op_3_project_0_exceptions_total": int64(0),
  430. "op_3_project_0_process_latency_us": int64(0),
  431. "op_3_project_0_records_in_total": int64(2),
  432. "op_3_project_0_records_out_total": int64(2),
  433. "sink_mockSink_0_exceptions_total": int64(0),
  434. "sink_mockSink_0_records_in_total": int64(2),
  435. "sink_mockSink_0_records_out_total": int64(2),
  436. "source_demo_0_exceptions_total": int64(0),
  437. "source_demo_0_records_in_total": int64(5),
  438. "source_demo_0_records_out_total": int64(5),
  439. "op_2_filter_0_exceptions_total": int64(0),
  440. "op_2_filter_0_process_latency_us": int64(0),
  441. "op_2_filter_0_records_in_total": int64(5),
  442. "op_2_filter_0_records_out_total": int64(2),
  443. },
  444. },
  445. {
  446. Name: `TestSingleSQLRule7`,
  447. Sql: "SELECT `from` FROM demo1",
  448. R: [][]map[string]interface{}{
  449. {{
  450. "from": "device1",
  451. }},
  452. {{
  453. "from": "device2",
  454. }},
  455. {{
  456. "from": "device3",
  457. }},
  458. {{
  459. "from": "device1",
  460. }},
  461. {{
  462. "from": "device3",
  463. }},
  464. },
  465. M: map[string]interface{}{
  466. "op_2_project_0_exceptions_total": int64(0),
  467. "op_2_project_0_process_latency_us": int64(0),
  468. "op_2_project_0_records_in_total": int64(5),
  469. "op_2_project_0_records_out_total": int64(5),
  470. "sink_mockSink_0_exceptions_total": int64(0),
  471. "sink_mockSink_0_records_in_total": int64(5),
  472. "sink_mockSink_0_records_out_total": int64(5),
  473. "source_demo1_0_exceptions_total": int64(0),
  474. "source_demo1_0_records_in_total": int64(5),
  475. "source_demo1_0_records_out_total": int64(5),
  476. },
  477. },
  478. {
  479. Name: `TestSingleSQLRule8`,
  480. Sql: "SELECT * FROM demo1 where `from`=\"device1\"",
  481. R: [][]map[string]interface{}{
  482. {{
  483. "temp": float64(25.5),
  484. "hum": float64(65),
  485. "from": "device1",
  486. "ts": float64(1541152486013),
  487. }},
  488. {{
  489. "temp": float64(27.4),
  490. "hum": float64(80),
  491. "from": "device1",
  492. "ts": float64(1541152488442),
  493. }},
  494. },
  495. M: map[string]interface{}{
  496. "op_3_project_0_exceptions_total": int64(0),
  497. "op_3_project_0_process_latency_us": int64(0),
  498. "op_3_project_0_records_in_total": int64(2),
  499. "op_3_project_0_records_out_total": int64(2),
  500. "op_2_filter_0_exceptions_total": int64(0),
  501. "op_2_filter_0_process_latency_us": int64(0),
  502. "op_2_filter_0_records_in_total": int64(5),
  503. "op_2_filter_0_records_out_total": int64(2),
  504. "sink_mockSink_0_exceptions_total": int64(0),
  505. "sink_mockSink_0_records_in_total": int64(2),
  506. "sink_mockSink_0_records_out_total": int64(2),
  507. "source_demo1_0_exceptions_total": int64(0),
  508. "source_demo1_0_records_in_total": int64(5),
  509. "source_demo1_0_records_out_total": int64(5),
  510. },
  511. },
  512. {
  513. Name: `TestSingleSQLRule9`,
  514. Sql: `SELECT color, CASE WHEN size < 2 THEN "S" WHEN size < 4 THEN "M" ELSE "L" END as s, ts FROM demo`,
  515. R: [][]map[string]interface{}{
  516. {{
  517. "color": "red",
  518. "s": "M",
  519. "ts": float64(1541152486013),
  520. }},
  521. {{
  522. "color": "blue",
  523. "s": "L",
  524. "ts": float64(1541152486822),
  525. }},
  526. {{
  527. "color": "blue",
  528. "s": "M",
  529. "ts": float64(1541152487632),
  530. }},
  531. {{
  532. "color": "yellow",
  533. "s": "L",
  534. "ts": float64(1541152488442),
  535. }},
  536. {{
  537. "color": "red",
  538. "s": "S",
  539. "ts": float64(1541152489252),
  540. }},
  541. },
  542. M: map[string]interface{}{
  543. "op_2_project_0_exceptions_total": int64(0),
  544. "op_2_project_0_process_latency_us": int64(0),
  545. "op_2_project_0_records_in_total": int64(5),
  546. "op_2_project_0_records_out_total": int64(5),
  547. "sink_mockSink_0_exceptions_total": int64(0),
  548. "sink_mockSink_0_records_in_total": int64(5),
  549. "sink_mockSink_0_records_out_total": int64(5),
  550. "source_demo_0_exceptions_total": int64(0),
  551. "source_demo_0_records_in_total": int64(5),
  552. "source_demo_0_records_out_total": int64(5),
  553. },
  554. T: &api.PrintableTopo{
  555. Sources: []string{"source_demo"},
  556. Edges: map[string][]interface{}{
  557. "source_demo": {"op_2_project"},
  558. "op_2_project": {"sink_mockSink"},
  559. },
  560. },
  561. },
  562. {
  563. Name: `TestSingleSQLRule10`,
  564. Sql: "SELECT * FROM demo INNER JOIN table1 on demo.ts = table1.id",
  565. R: [][]map[string]interface{}{
  566. {{
  567. "id": float64(1541152486013),
  568. "name": "name1",
  569. "color": "red",
  570. "size": float64(3),
  571. "ts": float64(1541152486013),
  572. }},
  573. {{
  574. "id": float64(1541152487632),
  575. "name": "name2",
  576. "color": "blue",
  577. "size": float64(2),
  578. "ts": float64(1541152487632),
  579. }},
  580. {{
  581. "id": float64(1541152489252),
  582. "name": "name3",
  583. "color": "red",
  584. "size": float64(1),
  585. "ts": float64(1541152489252),
  586. }},
  587. },
  588. W: 15,
  589. M: map[string]interface{}{
  590. "op_3_join_aligner_0_records_in_total": int64(6),
  591. "op_3_join_aligner_0_records_out_total": int64(5),
  592. "op_4_join_0_exceptions_total": int64(0),
  593. "op_4_join_0_records_in_total": int64(5),
  594. "op_4_join_0_records_out_total": int64(3),
  595. "op_5_project_0_exceptions_total": int64(0),
  596. "op_5_project_0_records_in_total": int64(3),
  597. "op_5_project_0_records_out_total": int64(3),
  598. "sink_mockSink_0_exceptions_total": int64(0),
  599. "sink_mockSink_0_records_in_total": int64(3),
  600. "sink_mockSink_0_records_out_total": int64(3),
  601. "source_demo_0_exceptions_total": int64(0),
  602. "source_demo_0_records_in_total": int64(5),
  603. "source_demo_0_records_out_total": int64(5),
  604. "source_table1_0_exceptions_total": int64(0),
  605. "source_table1_0_records_in_total": int64(4),
  606. "source_table1_0_records_out_total": int64(1),
  607. },
  608. },
  609. {
  610. Name: `TestSingleSQLRule11`,
  611. Sql: "SELECT device FROM demo INNER JOIN demoTable on demo.ts = demoTable.ts",
  612. R: [][]map[string]interface{}{
  613. {{
  614. "device": "device2",
  615. }},
  616. {{
  617. "device": "device4",
  618. }},
  619. {{
  620. "device": "device5",
  621. }},
  622. },
  623. M: map[string]interface{}{
  624. "op_3_join_aligner_0_records_in_total": int64(10),
  625. "op_3_join_aligner_0_records_out_total": int64(5),
  626. "op_4_join_0_exceptions_total": int64(0),
  627. "op_4_join_0_records_in_total": int64(5),
  628. "op_4_join_0_records_out_total": int64(3),
  629. "op_5_project_0_exceptions_total": int64(0),
  630. "op_5_project_0_records_in_total": int64(3),
  631. "op_5_project_0_records_out_total": int64(3),
  632. "sink_mockSink_0_exceptions_total": int64(0),
  633. "sink_mockSink_0_records_in_total": int64(3),
  634. "sink_mockSink_0_records_out_total": int64(3),
  635. "source_demo_0_exceptions_total": int64(0),
  636. "source_demo_0_records_in_total": int64(5),
  637. "source_demo_0_records_out_total": int64(5),
  638. "source_demoTable_0_exceptions_total": int64(0),
  639. "source_demoTable_0_records_in_total": int64(5),
  640. "source_demoTable_0_records_out_total": int64(5),
  641. },
  642. },
  643. {
  644. Name: `TestSingleSQLRule12`,
  645. Sql: "SELECT demo.ts as demoTs, table1.id as table1Id FROM demo INNER JOIN table1 on demoTs = table1Id",
  646. R: [][]map[string]interface{}{
  647. {{
  648. "table1Id": float64(1541152486013),
  649. "demoTs": float64(1541152486013),
  650. }},
  651. {{
  652. "table1Id": float64(1541152487632),
  653. "demoTs": float64(1541152487632),
  654. }},
  655. {{
  656. "table1Id": float64(1541152489252),
  657. "demoTs": float64(1541152489252),
  658. }},
  659. },
  660. W: 15,
  661. M: map[string]interface{}{
  662. "op_3_join_aligner_0_records_in_total": int64(6),
  663. "op_3_join_aligner_0_records_out_total": int64(5),
  664. "op_4_join_0_exceptions_total": int64(0),
  665. "op_4_join_0_records_in_total": int64(5),
  666. "op_4_join_0_records_out_total": int64(3),
  667. "op_5_project_0_exceptions_total": int64(0),
  668. "op_5_project_0_records_in_total": int64(3),
  669. "op_5_project_0_records_out_total": int64(3),
  670. "sink_mockSink_0_exceptions_total": int64(0),
  671. "sink_mockSink_0_records_in_total": int64(3),
  672. "sink_mockSink_0_records_out_total": int64(3),
  673. "source_demo_0_exceptions_total": int64(0),
  674. "source_demo_0_records_in_total": int64(5),
  675. "source_demo_0_records_out_total": int64(5),
  676. "source_table1_0_exceptions_total": int64(0),
  677. "source_table1_0_records_in_total": int64(4),
  678. "source_table1_0_records_out_total": int64(1),
  679. },
  680. },
  681. {
  682. Name: `TestChanged13`,
  683. Sql: "SELECT changed_cols(\"tt_\", true, color, size) FROM demo",
  684. R: [][]map[string]interface{}{
  685. {{
  686. "tt_color": "red",
  687. "tt_size": float64(3),
  688. }},
  689. {{
  690. "tt_color": "blue",
  691. "tt_size": float64(6),
  692. }},
  693. {{
  694. "tt_size": float64(2),
  695. }},
  696. {{
  697. "tt_color": "yellow",
  698. "tt_size": float64(4),
  699. }},
  700. {{
  701. "tt_color": "red",
  702. "tt_size": float64(1),
  703. }},
  704. },
  705. M: map[string]interface{}{
  706. "op_2_project_0_exceptions_total": int64(0),
  707. "op_2_project_0_process_latency_us": int64(0),
  708. "op_2_project_0_records_in_total": int64(5),
  709. "op_2_project_0_records_out_total": int64(5),
  710. "sink_mockSink_0_exceptions_total": int64(0),
  711. "sink_mockSink_0_records_in_total": int64(5),
  712. "sink_mockSink_0_records_out_total": int64(5),
  713. "source_demo_0_exceptions_total": int64(0),
  714. "source_demo_0_records_in_total": int64(5),
  715. "source_demo_0_records_out_total": int64(5),
  716. },
  717. },
  718. {
  719. Name: `TestAliasOrderBy14`,
  720. Sql: "SELECT color, count(*) as c FROM demo where color != \"red\" GROUP BY COUNTWINDOW(5), color Order by c DESC",
  721. R: [][]map[string]interface{}{
  722. {
  723. {
  724. "color": "blue",
  725. "c": float64(2),
  726. },
  727. {
  728. "color": "yellow",
  729. "c": float64(1),
  730. },
  731. },
  732. },
  733. M: map[string]interface{}{
  734. "op_6_project_0_exceptions_total": int64(0),
  735. "op_6_project_0_process_latency_us": int64(0),
  736. "op_6_project_0_records_in_total": int64(1),
  737. "op_6_project_0_records_out_total": int64(1),
  738. "sink_mockSink_0_exceptions_total": int64(0),
  739. "sink_mockSink_0_records_in_total": int64(1),
  740. "sink_mockSink_0_records_out_total": int64(1),
  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. },
  745. },
  746. {
  747. Name: `TestSingleSQLRule17`,
  748. Sql: `SELECT arr[x:4] as col1 FROM demoArr where x=1`,
  749. R: [][]map[string]interface{}{
  750. {{
  751. "col1": []interface{}{
  752. float64(2), float64(3), float64(4),
  753. },
  754. }},
  755. },
  756. },
  757. {
  758. Name: `TestSingleSQLRule16`,
  759. Sql: `SELECT arr[1:y] as col1 FROM demoArr where x=1`,
  760. R: [][]map[string]interface{}{
  761. {{
  762. "col1": []interface{}{
  763. float64(2),
  764. },
  765. }},
  766. },
  767. },
  768. {
  769. Name: `TestSingleSQLRule15`,
  770. Sql: `SELECT arr[1] as col1 FROM demoArr where x=1`,
  771. R: [][]map[string]interface{}{
  772. {{
  773. "col1": float64(2),
  774. }},
  775. },
  776. },
  777. {
  778. Name: `TestLagAlias`,
  779. Sql: "SELECT lag(size) as lastSize, lag(had_changed(true,size)), size, lastSize/size as changeRate FROM demo WHERE size > 2",
  780. R: [][]map[string]interface{}{
  781. {{
  782. "size": float64(3),
  783. }},
  784. {{
  785. "lastSize": float64(3),
  786. "size": float64(6),
  787. "lag": true,
  788. "changeRate": float64(0),
  789. }},
  790. {{
  791. "lastSize": float64(2),
  792. "size": float64(4),
  793. "lag": true,
  794. "changeRate": float64(0),
  795. }},
  796. },
  797. M: map[string]interface{}{
  798. "sink_mockSink_0_exceptions_total": int64(0),
  799. "sink_mockSink_0_records_in_total": int64(3),
  800. "sink_mockSink_0_records_out_total": int64(3),
  801. "source_demo_0_exceptions_total": int64(0),
  802. "source_demo_0_records_in_total": int64(5),
  803. "source_demo_0_records_out_total": int64(5),
  804. },
  805. },
  806. {
  807. Name: `TestLagPartition`,
  808. Sql: "SELECT color, lag(size) over (partition by color) as lastSize, size, lastSize/size as changeRate FROM demo",
  809. R: [][]map[string]interface{}{
  810. {{
  811. "color": "red",
  812. "size": float64(3),
  813. }},
  814. {{
  815. "color": "blue",
  816. "size": float64(6),
  817. }},
  818. {{
  819. "color": "blue",
  820. "lastSize": float64(6),
  821. "size": float64(2),
  822. "changeRate": float64(3),
  823. }},
  824. {{
  825. "color": "yellow",
  826. "size": float64(4),
  827. }},
  828. {{
  829. "color": "red",
  830. "lastSize": float64(3),
  831. "size": float64(1),
  832. "changeRate": float64(3),
  833. }},
  834. },
  835. M: map[string]interface{}{
  836. "sink_mockSink_0_exceptions_total": int64(0),
  837. "sink_mockSink_0_records_in_total": int64(5),
  838. "sink_mockSink_0_records_out_total": int64(5),
  839. "source_demo_0_exceptions_total": int64(0),
  840. "source_demo_0_records_in_total": int64(5),
  841. "source_demo_0_records_out_total": int64(5),
  842. },
  843. },
  844. }
  845. HandleStream(true, streamList, t)
  846. options := []*api.RuleOption{
  847. {
  848. BufferLength: 100,
  849. SendError: true,
  850. }, {
  851. BufferLength: 100,
  852. SendError: true,
  853. Qos: api.AtLeastOnce,
  854. CheckpointInterval: 5000,
  855. }, {
  856. BufferLength: 100,
  857. SendError: true,
  858. Qos: api.ExactlyOnce,
  859. CheckpointInterval: 5000,
  860. },
  861. }
  862. for j, opt := range options {
  863. DoRuleTest(t, tests, j, opt, 0)
  864. }
  865. }
  866. func TestSingleSQLError(t *testing.T) {
  867. // Reset
  868. streamList := []string{"ldemo"}
  869. HandleStream(false, streamList, t)
  870. // Data setup
  871. tests := []RuleTest{
  872. {
  873. Name: `TestSingleSQLErrorRule1`,
  874. Sql: `SELECT color, ts FROM ldemo where size >= 3`,
  875. R: [][]map[string]interface{}{
  876. {{
  877. "color": "red",
  878. "ts": float64(1541152486013),
  879. }},
  880. {{
  881. "error": "run Where error: invalid operation string(string) >= int64(3)",
  882. }},
  883. {{
  884. "ts": float64(1541152487632),
  885. }},
  886. },
  887. M: map[string]interface{}{
  888. "op_3_project_0_exceptions_total": int64(1),
  889. "op_3_project_0_process_latency_us": int64(0),
  890. "op_3_project_0_records_in_total": int64(3),
  891. "op_3_project_0_records_out_total": int64(2),
  892. "sink_mockSink_0_exceptions_total": int64(0),
  893. "sink_mockSink_0_records_in_total": int64(3),
  894. "sink_mockSink_0_records_out_total": int64(3),
  895. "source_ldemo_0_exceptions_total": int64(0),
  896. "source_ldemo_0_records_in_total": int64(5),
  897. "source_ldemo_0_records_out_total": int64(5),
  898. "op_2_filter_0_exceptions_total": int64(1),
  899. "op_2_filter_0_process_latency_us": int64(0),
  900. "op_2_filter_0_records_in_total": int64(5),
  901. "op_2_filter_0_records_out_total": int64(2),
  902. },
  903. }, {
  904. Name: `TestSingleSQLErrorRule2`,
  905. Sql: `SELECT size * 5 FROM ldemo`,
  906. R: [][]map[string]interface{}{
  907. {{
  908. "kuiper_field_0": float64(15),
  909. }},
  910. {{
  911. "error": "run Select error: invalid operation string(string) * int64(5)",
  912. }},
  913. {{
  914. "kuiper_field_0": float64(15),
  915. }},
  916. {{
  917. "kuiper_field_0": float64(10),
  918. }},
  919. {{}},
  920. },
  921. M: map[string]interface{}{
  922. "op_2_project_0_exceptions_total": int64(1),
  923. "op_2_project_0_process_latency_us": int64(0),
  924. "op_2_project_0_records_in_total": int64(5),
  925. "op_2_project_0_records_out_total": int64(4),
  926. "sink_mockSink_0_exceptions_total": int64(0),
  927. "sink_mockSink_0_records_in_total": int64(5),
  928. "sink_mockSink_0_records_out_total": int64(5),
  929. "source_ldemo_0_exceptions_total": int64(0),
  930. "source_ldemo_0_records_in_total": int64(5),
  931. "source_ldemo_0_records_out_total": int64(5),
  932. },
  933. },
  934. }
  935. HandleStream(true, streamList, t)
  936. DoRuleTest(t, tests, 0, &api.RuleOption{
  937. BufferLength: 100,
  938. SendError: true,
  939. }, 0)
  940. }
  941. func TestSingleSQLOmitError(t *testing.T) {
  942. // Reset
  943. streamList := []string{"ldemo"}
  944. HandleStream(false, streamList, t)
  945. // Data setup
  946. tests := []RuleTest{
  947. {
  948. Name: `TestSingleSQLErrorRule1`,
  949. Sql: `SELECT color, ts FROM ldemo where size >= 3`,
  950. R: [][]map[string]interface{}{
  951. {{
  952. "color": "red",
  953. "ts": float64(1541152486013),
  954. }},
  955. {{
  956. "ts": float64(1541152487632),
  957. }},
  958. },
  959. M: map[string]interface{}{
  960. "op_3_project_0_exceptions_total": int64(0),
  961. "op_3_project_0_process_latency_us": int64(0),
  962. "op_3_project_0_records_in_total": int64(2),
  963. "op_3_project_0_records_out_total": int64(2),
  964. "sink_mockSink_0_exceptions_total": int64(0),
  965. "sink_mockSink_0_records_in_total": int64(2),
  966. "sink_mockSink_0_records_out_total": int64(2),
  967. "source_ldemo_0_exceptions_total": int64(0),
  968. "source_ldemo_0_records_in_total": int64(5),
  969. "source_ldemo_0_records_out_total": int64(5),
  970. "op_2_filter_0_exceptions_total": int64(1),
  971. "op_2_filter_0_process_latency_us": int64(0),
  972. "op_2_filter_0_records_in_total": int64(5),
  973. "op_2_filter_0_records_out_total": int64(2),
  974. },
  975. }, {
  976. Name: `TestSingleSQLErrorRule2`,
  977. Sql: `SELECT size * 5 FROM ldemo`,
  978. R: [][]map[string]interface{}{
  979. {{
  980. "kuiper_field_0": float64(15),
  981. }},
  982. {{
  983. "kuiper_field_0": float64(15),
  984. }},
  985. {{
  986. "kuiper_field_0": float64(10),
  987. }},
  988. {{}},
  989. },
  990. M: map[string]interface{}{
  991. "op_2_project_0_exceptions_total": int64(1),
  992. "op_2_project_0_process_latency_us": int64(0),
  993. "op_2_project_0_records_in_total": int64(5),
  994. "op_2_project_0_records_out_total": int64(4),
  995. "sink_mockSink_0_exceptions_total": int64(0),
  996. "sink_mockSink_0_records_in_total": int64(4),
  997. "sink_mockSink_0_records_out_total": int64(4),
  998. "source_ldemo_0_exceptions_total": int64(0),
  999. "source_ldemo_0_records_in_total": int64(5),
  1000. "source_ldemo_0_records_out_total": int64(5),
  1001. },
  1002. },
  1003. }
  1004. HandleStream(true, streamList, t)
  1005. DoRuleTest(t, tests, 0, &api.RuleOption{
  1006. BufferLength: 100,
  1007. SendError: false,
  1008. }, 0)
  1009. }
  1010. func TestSingleSQLTemplate(t *testing.T) {
  1011. // Reset
  1012. streamList := []string{"demo"}
  1013. HandleStream(false, streamList, t)
  1014. // Data setup
  1015. tests := []RuleTest{
  1016. {
  1017. Name: `TestSingleSQLTemplateRule1`,
  1018. Sql: `SELECT * FROM demo`,
  1019. R: []map[string]interface{}{
  1020. {
  1021. "c": "red",
  1022. "wrapper": "w1",
  1023. },
  1024. {
  1025. "c": "blue",
  1026. "wrapper": "w1",
  1027. },
  1028. {
  1029. "c": "blue",
  1030. "wrapper": "w1",
  1031. },
  1032. {
  1033. "c": "yellow",
  1034. "wrapper": "w1",
  1035. },
  1036. {
  1037. "c": "red",
  1038. "wrapper": "w1",
  1039. },
  1040. },
  1041. M: map[string]interface{}{
  1042. "op_2_project_0_exceptions_total": int64(0),
  1043. "op_2_project_0_process_latency_us": int64(0),
  1044. "op_2_project_0_records_in_total": int64(5),
  1045. "op_2_project_0_records_out_total": int64(5),
  1046. "sink_mockSink_0_exceptions_total": int64(0),
  1047. "sink_mockSink_0_records_in_total": int64(5),
  1048. "sink_mockSink_0_records_out_total": int64(5),
  1049. "source_demo_0_exceptions_total": int64(0),
  1050. "source_demo_0_records_in_total": int64(5),
  1051. "source_demo_0_records_out_total": int64(5),
  1052. },
  1053. },
  1054. }
  1055. HandleStream(true, streamList, t)
  1056. doRuleTestBySinkProps(t, tests, 0, &api.RuleOption{
  1057. BufferLength: 100,
  1058. SendError: true,
  1059. }, 0, map[string]interface{}{
  1060. "dataTemplate": `{"wrapper":"w1", "c":"{{.color}}"}`,
  1061. "sendSingle": true,
  1062. }, func(result [][]byte) interface{} {
  1063. var maps []map[string]interface{}
  1064. for _, v := range result {
  1065. var mapRes map[string]interface{}
  1066. err := json.Unmarshal(v, &mapRes)
  1067. if err != nil {
  1068. t.Errorf("Failed to parse the input into map")
  1069. continue
  1070. }
  1071. maps = append(maps, mapRes)
  1072. }
  1073. return maps
  1074. })
  1075. }
  1076. func TestNoneSingleSQLTemplate(t *testing.T) {
  1077. // Reset
  1078. streamList := []string{"demo"}
  1079. HandleStream(false, streamList, t)
  1080. // Data setup
  1081. tests := []RuleTest{
  1082. {
  1083. Name: `TestNoneSingleSQLTemplateRule1`,
  1084. Sql: `SELECT * FROM demo`,
  1085. R: [][]byte{
  1086. []byte("<div>results</div><ul><li>red - 3</li></ul>"),
  1087. []byte("<div>results</div><ul><li>blue - 6</li></ul>"),
  1088. []byte("<div>results</div><ul><li>blue - 2</li></ul>"),
  1089. []byte("<div>results</div><ul><li>yellow - 4</li></ul>"),
  1090. []byte("<div>results</div><ul><li>red - 1</li></ul>"),
  1091. },
  1092. M: map[string]interface{}{
  1093. "op_2_project_0_exceptions_total": int64(0),
  1094. "op_2_project_0_process_latency_us": int64(0),
  1095. "op_2_project_0_records_in_total": int64(5),
  1096. "op_2_project_0_records_out_total": int64(5),
  1097. "sink_mockSink_0_exceptions_total": int64(0),
  1098. "sink_mockSink_0_records_in_total": int64(5),
  1099. "sink_mockSink_0_records_out_total": int64(5),
  1100. "source_demo_0_exceptions_total": int64(0),
  1101. "source_demo_0_records_in_total": int64(5),
  1102. "source_demo_0_records_out_total": int64(5),
  1103. },
  1104. },
  1105. }
  1106. HandleStream(true, streamList, t)
  1107. doRuleTestBySinkProps(t, tests, 0, &api.RuleOption{
  1108. BufferLength: 100,
  1109. SendError: true,
  1110. }, 0, map[string]interface{}{
  1111. "dataTemplate": `<div>results</div><ul>{{range .}}<li>{{.color}} - {{.size}}</li>{{end}}</ul>`,
  1112. }, func(result [][]byte) interface{} {
  1113. return result
  1114. })
  1115. }
  1116. func TestSingleSQLForBinary(t *testing.T) {
  1117. // Reset
  1118. streamList := []string{"binDemo"}
  1119. HandleStream(false, streamList, t)
  1120. // Data setup
  1121. tests := []RuleTest{
  1122. {
  1123. Name: `TestSingleSQLRule1`,
  1124. Sql: `SELECT * FROM binDemo`,
  1125. R: [][]map[string]interface{}{
  1126. {{
  1127. "self": mocknode.Image,
  1128. }},
  1129. },
  1130. W: 50,
  1131. M: map[string]interface{}{
  1132. "op_2_project_0_exceptions_total": int64(0),
  1133. "op_2_project_0_process_latency_us": int64(0),
  1134. "op_2_project_0_records_in_total": int64(1),
  1135. "op_2_project_0_records_out_total": int64(1),
  1136. "sink_mockSink_0_exceptions_total": int64(0),
  1137. "sink_mockSink_0_records_in_total": int64(1),
  1138. "sink_mockSink_0_records_out_total": int64(1),
  1139. "source_binDemo_0_exceptions_total": int64(0),
  1140. "source_binDemo_0_records_in_total": int64(1),
  1141. "source_binDemo_0_records_out_total": int64(1),
  1142. },
  1143. },
  1144. }
  1145. HandleStream(true, streamList, t)
  1146. options := []*api.RuleOption{
  1147. {
  1148. BufferLength: 100,
  1149. SendError: true,
  1150. }, {
  1151. BufferLength: 100,
  1152. SendError: true,
  1153. Qos: api.AtLeastOnce,
  1154. CheckpointInterval: 5000,
  1155. }, {
  1156. BufferLength: 100,
  1157. SendError: true,
  1158. Qos: api.ExactlyOnce,
  1159. CheckpointInterval: 5000,
  1160. },
  1161. }
  1162. byteFunc := func(result [][]byte) interface{} {
  1163. var maps [][]map[string]interface{}
  1164. for _, v := range result {
  1165. var mapRes []map[string][]byte
  1166. err := json.Unmarshal(v, &mapRes)
  1167. if err != nil {
  1168. panic("Failed to parse the input into map")
  1169. }
  1170. mapInt := make([]map[string]interface{}, len(mapRes))
  1171. for i, mv := range mapRes {
  1172. mapInt[i] = make(map[string]interface{})
  1173. // assume only one key
  1174. for k, v := range mv {
  1175. mapInt[i][k] = v
  1176. }
  1177. }
  1178. maps = append(maps, mapInt)
  1179. }
  1180. return maps
  1181. }
  1182. for j, opt := range options {
  1183. doRuleTestBySinkProps(t, tests, j, opt, 0, nil, byteFunc)
  1184. }
  1185. }