rule_test.go 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  1. // Copyright 2021-2022 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package topotest
  15. import (
  16. "encoding/json"
  17. "github.com/lf-edge/ekuiper/internal/topo/topotest/mocknode"
  18. "github.com/lf-edge/ekuiper/pkg/api"
  19. "testing"
  20. )
  21. func TestSingleSQL(t *testing.T) {
  22. //Reset
  23. streamList := []string{"demo", "demoError", "demo1", "table1", "demoTable"}
  24. HandleStream(false, streamList, t)
  25. //Data setup
  26. var tests = []RuleTest{
  27. {
  28. Name: `TestSingleSQLRule1`,
  29. Sql: `SELECT *, upper(color) FROM demo`,
  30. R: [][]map[string]interface{}{
  31. {{
  32. "color": "red",
  33. "size": float64(3),
  34. "ts": float64(1541152486013),
  35. "upper": "RED",
  36. }},
  37. {{
  38. "color": "blue",
  39. "size": float64(6),
  40. "ts": float64(1541152486822),
  41. "upper": "BLUE",
  42. }},
  43. {{
  44. "color": "blue",
  45. "size": float64(2),
  46. "ts": float64(1541152487632),
  47. "upper": "BLUE",
  48. }},
  49. {{
  50. "color": "yellow",
  51. "size": float64(4),
  52. "ts": float64(1541152488442),
  53. "upper": "YELLOW",
  54. }},
  55. {{
  56. "color": "red",
  57. "size": float64(1),
  58. "ts": float64(1541152489252),
  59. "upper": "RED",
  60. }},
  61. },
  62. M: map[string]interface{}{
  63. "op_2_project_0_exceptions_total": int64(0),
  64. "op_2_project_0_process_latency_us": int64(0),
  65. "op_2_project_0_records_in_total": int64(5),
  66. "op_2_project_0_records_out_total": int64(5),
  67. "sink_mockSink_0_exceptions_total": int64(0),
  68. "sink_mockSink_0_records_in_total": int64(5),
  69. "sink_mockSink_0_records_out_total": int64(5),
  70. "source_demo_0_exceptions_total": int64(0),
  71. "source_demo_0_records_in_total": int64(5),
  72. "source_demo_0_records_out_total": int64(5),
  73. },
  74. T: &api.PrintableTopo{
  75. Sources: []string{"source_demo"},
  76. Edges: map[string][]string{
  77. "source_demo": {"op_2_project"},
  78. "op_2_project": {"sink_mockSink"},
  79. },
  80. },
  81. },
  82. {
  83. Name: `TestSingleSQLRule2`,
  84. Sql: `SELECT color, ts FROM demo where size > 3`,
  85. R: [][]map[string]interface{}{
  86. {{
  87. "color": "blue",
  88. "ts": float64(1541152486822),
  89. }},
  90. {{
  91. "color": "yellow",
  92. "ts": float64(1541152488442),
  93. }},
  94. },
  95. M: map[string]interface{}{
  96. "op_3_project_0_exceptions_total": int64(0),
  97. "op_3_project_0_process_latency_us": int64(0),
  98. "op_3_project_0_records_in_total": int64(2),
  99. "op_3_project_0_records_out_total": int64(2),
  100. "sink_mockSink_0_exceptions_total": int64(0),
  101. "sink_mockSink_0_records_in_total": int64(2),
  102. "sink_mockSink_0_records_out_total": int64(2),
  103. "source_demo_0_exceptions_total": int64(0),
  104. "source_demo_0_records_in_total": int64(5),
  105. "source_demo_0_records_out_total": int64(5),
  106. "op_2_filter_0_exceptions_total": int64(0),
  107. "op_2_filter_0_process_latency_us": int64(0),
  108. "op_2_filter_0_records_in_total": int64(5),
  109. "op_2_filter_0_records_out_total": int64(2),
  110. },
  111. }, {
  112. Name: `TestSingleSQLRule3`,
  113. Sql: `SELECT size as Int8, ts FROM demo where size > 3`,
  114. R: [][]map[string]interface{}{
  115. {{
  116. "Int8": float64(6),
  117. "ts": float64(1541152486822),
  118. }},
  119. {{
  120. "Int8": float64(4),
  121. "ts": float64(1541152488442),
  122. }},
  123. },
  124. M: map[string]interface{}{
  125. "op_3_project_0_exceptions_total": int64(0),
  126. "op_3_project_0_process_latency_us": int64(0),
  127. "op_3_project_0_records_in_total": int64(2),
  128. "op_3_project_0_records_out_total": int64(2),
  129. "sink_mockSink_0_exceptions_total": int64(0),
  130. "sink_mockSink_0_records_in_total": int64(2),
  131. "sink_mockSink_0_records_out_total": int64(2),
  132. "source_demo_0_exceptions_total": int64(0),
  133. "source_demo_0_records_in_total": int64(5),
  134. "source_demo_0_records_out_total": int64(5),
  135. "op_2_filter_0_exceptions_total": int64(0),
  136. "op_2_filter_0_process_latency_us": int64(0),
  137. "op_2_filter_0_records_in_total": int64(5),
  138. "op_2_filter_0_records_out_total": int64(2),
  139. },
  140. }, {
  141. Name: `TestSingleSQLRule4`,
  142. Sql: `SELECT size as Int8, ts FROM demoError where size > 3`,
  143. R: [][]map[string]interface{}{
  144. {{
  145. "error": "error in preprocessor: invalid data type for size, expect bigint but found string(red)",
  146. }},
  147. {{
  148. "Int8": float64(6),
  149. "ts": float64(1541152486822),
  150. }},
  151. {{
  152. "Int8": float64(4),
  153. "ts": float64(1541152488442),
  154. }},
  155. {{
  156. "error": "error in preprocessor: invalid data type for size, expect bigint but found string(blue)",
  157. }},
  158. },
  159. M: map[string]interface{}{
  160. "op_3_project_0_exceptions_total": int64(2),
  161. "op_3_project_0_process_latency_us": int64(0),
  162. "op_3_project_0_records_in_total": int64(4),
  163. "op_3_project_0_records_out_total": int64(2),
  164. "sink_mockSink_0_exceptions_total": int64(0),
  165. "sink_mockSink_0_records_in_total": int64(4),
  166. "sink_mockSink_0_records_out_total": int64(4),
  167. "source_demoError_0_exceptions_total": int64(2),
  168. "source_demoError_0_records_in_total": int64(5),
  169. "source_demoError_0_records_out_total": int64(5),
  170. "op_2_filter_0_exceptions_total": int64(2),
  171. "op_2_filter_0_process_latency_us": int64(0),
  172. "op_2_filter_0_records_in_total": int64(5),
  173. "op_2_filter_0_records_out_total": int64(2),
  174. },
  175. }, {
  176. Name: `TestSingleSQLRule5`,
  177. Sql: `SELECT meta(topic) as m, ts FROM demo`,
  178. R: [][]map[string]interface{}{
  179. {{
  180. "m": "mock",
  181. "ts": float64(1541152486013),
  182. }},
  183. {{
  184. "m": "mock",
  185. "ts": float64(1541152486822),
  186. }},
  187. {{
  188. "m": "mock",
  189. "ts": float64(1541152487632),
  190. }},
  191. {{
  192. "m": "mock",
  193. "ts": float64(1541152488442),
  194. }},
  195. {{
  196. "m": "mock",
  197. "ts": float64(1541152489252),
  198. }},
  199. },
  200. M: map[string]interface{}{
  201. "op_2_project_0_exceptions_total": int64(0),
  202. "op_2_project_0_process_latency_us": int64(0),
  203. "op_2_project_0_records_in_total": int64(5),
  204. "op_2_project_0_records_out_total": int64(5),
  205. "sink_mockSink_0_exceptions_total": int64(0),
  206. "sink_mockSink_0_records_in_total": int64(5),
  207. "sink_mockSink_0_records_out_total": int64(5),
  208. "source_demo_0_exceptions_total": int64(0),
  209. "source_demo_0_records_in_total": int64(5),
  210. "source_demo_0_records_out_total": int64(5),
  211. },
  212. }, {
  213. Name: `TestSingleSQLRule6`,
  214. Sql: `SELECT color, ts FROM demo where size > 3 and meta(topic)="mock"`,
  215. R: [][]map[string]interface{}{
  216. {{
  217. "color": "blue",
  218. "ts": float64(1541152486822),
  219. }},
  220. {{
  221. "color": "yellow",
  222. "ts": float64(1541152488442),
  223. }},
  224. },
  225. M: map[string]interface{}{
  226. "op_3_project_0_exceptions_total": int64(0),
  227. "op_3_project_0_process_latency_us": int64(0),
  228. "op_3_project_0_records_in_total": int64(2),
  229. "op_3_project_0_records_out_total": int64(2),
  230. "sink_mockSink_0_exceptions_total": int64(0),
  231. "sink_mockSink_0_records_in_total": int64(2),
  232. "sink_mockSink_0_records_out_total": int64(2),
  233. "source_demo_0_exceptions_total": int64(0),
  234. "source_demo_0_records_in_total": int64(5),
  235. "source_demo_0_records_out_total": int64(5),
  236. "op_2_filter_0_exceptions_total": int64(0),
  237. "op_2_filter_0_process_latency_us": int64(0),
  238. "op_2_filter_0_records_in_total": int64(5),
  239. "op_2_filter_0_records_out_total": int64(2),
  240. },
  241. }, {
  242. Name: `TestSingleSQLRule7`,
  243. Sql: "SELECT `from` FROM demo1",
  244. R: [][]map[string]interface{}{
  245. {{
  246. "from": "device1",
  247. }},
  248. {{
  249. "from": "device2",
  250. }},
  251. {{
  252. "from": "device3",
  253. }},
  254. {{
  255. "from": "device1",
  256. }},
  257. {{
  258. "from": "device3",
  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_demo1_0_exceptions_total": int64(0),
  270. "source_demo1_0_records_in_total": int64(5),
  271. "source_demo1_0_records_out_total": int64(5),
  272. },
  273. }, {
  274. Name: `TestSingleSQLRule8`,
  275. Sql: "SELECT * FROM demo1 where `from`=\"device1\"",
  276. R: [][]map[string]interface{}{
  277. {{
  278. "temp": float64(25.5),
  279. "hum": float64(65),
  280. "from": "device1",
  281. "ts": float64(1541152486013),
  282. }},
  283. {{
  284. "temp": float64(27.4),
  285. "hum": float64(80),
  286. "from": "device1",
  287. "ts": float64(1541152488442),
  288. }},
  289. },
  290. M: map[string]interface{}{
  291. "op_3_project_0_exceptions_total": int64(0),
  292. "op_3_project_0_process_latency_us": int64(0),
  293. "op_3_project_0_records_in_total": int64(2),
  294. "op_3_project_0_records_out_total": int64(2),
  295. "op_2_filter_0_exceptions_total": int64(0),
  296. "op_2_filter_0_process_latency_us": int64(0),
  297. "op_2_filter_0_records_in_total": int64(5),
  298. "op_2_filter_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_demo1_0_exceptions_total": int64(0),
  303. "source_demo1_0_records_in_total": int64(5),
  304. "source_demo1_0_records_out_total": int64(5),
  305. },
  306. }, {
  307. Name: `TestSingleSQLRule9`,
  308. Sql: `SELECT color, CASE WHEN size < 2 THEN "S" WHEN size < 4 THEN "M" ELSE "L" END as s, ts FROM demo`,
  309. R: [][]map[string]interface{}{
  310. {{
  311. "color": "red",
  312. "s": "M",
  313. "ts": float64(1541152486013),
  314. }},
  315. {{
  316. "color": "blue",
  317. "s": "L",
  318. "ts": float64(1541152486822),
  319. }},
  320. {{
  321. "color": "blue",
  322. "s": "M",
  323. "ts": float64(1541152487632),
  324. }},
  325. {{
  326. "color": "yellow",
  327. "s": "L",
  328. "ts": float64(1541152488442),
  329. }},
  330. {{
  331. "color": "red",
  332. "s": "S",
  333. "ts": float64(1541152489252),
  334. }},
  335. },
  336. M: map[string]interface{}{
  337. "op_2_project_0_exceptions_total": int64(0),
  338. "op_2_project_0_process_latency_us": int64(0),
  339. "op_2_project_0_records_in_total": int64(5),
  340. "op_2_project_0_records_out_total": int64(5),
  341. "sink_mockSink_0_exceptions_total": int64(0),
  342. "sink_mockSink_0_records_in_total": int64(5),
  343. "sink_mockSink_0_records_out_total": int64(5),
  344. "source_demo_0_exceptions_total": int64(0),
  345. "source_demo_0_records_in_total": int64(5),
  346. "source_demo_0_records_out_total": int64(5),
  347. },
  348. T: &api.PrintableTopo{
  349. Sources: []string{"source_demo"},
  350. Edges: map[string][]string{
  351. "source_demo": {"op_2_project"},
  352. "op_2_project": {"sink_mockSink"},
  353. },
  354. },
  355. }, {
  356. Name: `TestSingleSQLRule10`,
  357. Sql: "SELECT * FROM demo INNER JOIN table1 on demo.ts = table1.id",
  358. R: [][]map[string]interface{}{
  359. {{
  360. "id": float64(1541152486013),
  361. "name": "name1",
  362. "color": "red",
  363. "size": float64(3),
  364. "ts": float64(1541152486013),
  365. }},
  366. {{
  367. "id": float64(1541152487632),
  368. "name": "name2",
  369. "color": "blue",
  370. "size": float64(2),
  371. "ts": float64(1541152487632),
  372. }},
  373. {{
  374. "id": float64(1541152489252),
  375. "name": "name3",
  376. "color": "red",
  377. "size": float64(1),
  378. "ts": float64(1541152489252),
  379. }},
  380. },
  381. W: 15,
  382. M: map[string]interface{}{
  383. "op_3_join_aligner_0_records_in_total": int64(6),
  384. "op_3_join_aligner_0_records_out_total": int64(5),
  385. "op_4_join_0_exceptions_total": int64(0),
  386. "op_4_join_0_records_in_total": int64(5),
  387. "op_4_join_0_records_out_total": int64(3),
  388. "op_5_project_0_exceptions_total": int64(0),
  389. "op_5_project_0_records_in_total": int64(3),
  390. "op_5_project_0_records_out_total": int64(3),
  391. "sink_mockSink_0_exceptions_total": int64(0),
  392. "sink_mockSink_0_records_in_total": int64(3),
  393. "sink_mockSink_0_records_out_total": int64(3),
  394. "source_demo_0_exceptions_total": int64(0),
  395. "source_demo_0_records_in_total": int64(5),
  396. "source_demo_0_records_out_total": int64(5),
  397. "source_table1_0_exceptions_total": int64(0),
  398. "source_table1_0_records_in_total": int64(4),
  399. "source_table1_0_records_out_total": int64(1),
  400. },
  401. }, {
  402. Name: `TestSingleSQLRule11`,
  403. Sql: "SELECT device FROM demo INNER JOIN demoTable on demo.ts = demoTable.ts",
  404. R: [][]map[string]interface{}{
  405. {{
  406. "device": "device2",
  407. }},
  408. {{
  409. "device": "device4",
  410. }},
  411. {{
  412. "device": "device5",
  413. }},
  414. },
  415. M: map[string]interface{}{
  416. "op_3_join_aligner_0_records_in_total": int64(10),
  417. "op_3_join_aligner_0_records_out_total": int64(5),
  418. "op_4_join_0_exceptions_total": int64(0),
  419. "op_4_join_0_records_in_total": int64(5),
  420. "op_4_join_0_records_out_total": int64(3),
  421. "op_5_project_0_exceptions_total": int64(0),
  422. "op_5_project_0_records_in_total": int64(3),
  423. "op_5_project_0_records_out_total": int64(3),
  424. "sink_mockSink_0_exceptions_total": int64(0),
  425. "sink_mockSink_0_records_in_total": int64(3),
  426. "sink_mockSink_0_records_out_total": int64(3),
  427. "source_demo_0_exceptions_total": int64(0),
  428. "source_demo_0_records_in_total": int64(5),
  429. "source_demo_0_records_out_total": int64(5),
  430. "source_demoTable_0_exceptions_total": int64(0),
  431. "source_demoTable_0_records_in_total": int64(5),
  432. "source_demoTable_0_records_out_total": int64(5),
  433. },
  434. }, {
  435. Name: `TestSingleSQLRule12`,
  436. Sql: "SELECT demo.ts as demoTs, table1.id as table1Id FROM demo INNER JOIN table1 on demoTs = table1Id",
  437. R: [][]map[string]interface{}{
  438. {{
  439. "table1Id": float64(1541152486013),
  440. "demoTs": float64(1541152486013),
  441. }},
  442. {{
  443. "table1Id": float64(1541152487632),
  444. "demoTs": float64(1541152487632),
  445. }},
  446. {{
  447. "table1Id": float64(1541152489252),
  448. "demoTs": float64(1541152489252),
  449. }},
  450. },
  451. W: 15,
  452. M: map[string]interface{}{
  453. "op_3_join_aligner_0_records_in_total": int64(6),
  454. "op_3_join_aligner_0_records_out_total": int64(5),
  455. "op_4_join_0_exceptions_total": int64(0),
  456. "op_4_join_0_records_in_total": int64(5),
  457. "op_4_join_0_records_out_total": int64(3),
  458. "op_5_project_0_exceptions_total": int64(0),
  459. "op_5_project_0_records_in_total": int64(3),
  460. "op_5_project_0_records_out_total": int64(3),
  461. "sink_mockSink_0_exceptions_total": int64(0),
  462. "sink_mockSink_0_records_in_total": int64(3),
  463. "sink_mockSink_0_records_out_total": int64(3),
  464. "source_demo_0_exceptions_total": int64(0),
  465. "source_demo_0_records_in_total": int64(5),
  466. "source_demo_0_records_out_total": int64(5),
  467. "source_table1_0_exceptions_total": int64(0),
  468. "source_table1_0_records_in_total": int64(4),
  469. "source_table1_0_records_out_total": int64(1),
  470. },
  471. }, {
  472. Name: `TestChanged13`,
  473. Sql: "SELECT changed_cols(\"tt_\", true, color, size) FROM demo",
  474. R: [][]map[string]interface{}{
  475. {{
  476. "tt_color": "red",
  477. "tt_size": float64(3),
  478. }},
  479. {{
  480. "tt_color": "blue",
  481. "tt_size": float64(6),
  482. }},
  483. {{
  484. "tt_size": float64(2),
  485. }},
  486. {{
  487. "tt_color": "yellow",
  488. "tt_size": float64(4),
  489. }},
  490. {{
  491. "tt_color": "red",
  492. "tt_size": float64(1),
  493. }},
  494. },
  495. M: map[string]interface{}{
  496. "op_2_project_0_exceptions_total": int64(0),
  497. "op_2_project_0_process_latency_us": int64(0),
  498. "op_2_project_0_records_in_total": int64(5),
  499. "op_2_project_0_records_out_total": int64(5),
  500. "sink_mockSink_0_exceptions_total": int64(0),
  501. "sink_mockSink_0_records_in_total": int64(5),
  502. "sink_mockSink_0_records_out_total": int64(5),
  503. "source_demo_0_exceptions_total": int64(0),
  504. "source_demo_0_records_in_total": int64(5),
  505. "source_demo_0_records_out_total": int64(5),
  506. },
  507. }, {
  508. Name: `TestAliasOrderBy14`,
  509. Sql: "SELECT color, count(*) as c FROM demo where color != \"red\" GROUP BY COUNTWINDOW(5), color Order by c DESC",
  510. R: [][]map[string]interface{}{
  511. {{
  512. "color": "blue",
  513. "c": float64(2),
  514. },
  515. {
  516. "color": "yellow",
  517. "c": float64(1),
  518. },
  519. },
  520. },
  521. M: map[string]interface{}{
  522. "op_6_project_0_exceptions_total": int64(0),
  523. "op_6_project_0_process_latency_us": int64(0),
  524. "op_6_project_0_records_in_total": int64(1),
  525. "op_6_project_0_records_out_total": int64(1),
  526. "sink_mockSink_0_exceptions_total": int64(0),
  527. "sink_mockSink_0_records_in_total": int64(1),
  528. "sink_mockSink_0_records_out_total": int64(1),
  529. "source_demo_0_exceptions_total": int64(0),
  530. "source_demo_0_records_in_total": int64(5),
  531. "source_demo_0_records_out_total": int64(5),
  532. },
  533. },
  534. {
  535. Name: `TestLagAlias`,
  536. Sql: "SELECT lag(size) as lastSize, lag(had_changed(true,size)), size, lastSize/size as changeRate FROM demo WHERE size > 2",
  537. R: [][]map[string]interface{}{
  538. {{
  539. "size": float64(3),
  540. }},
  541. {{
  542. "lastSize": float64(3),
  543. "size": float64(6),
  544. "lag": true,
  545. "changeRate": float64(0),
  546. }},
  547. {{
  548. "lastSize": float64(2),
  549. "size": float64(4),
  550. "lag": true,
  551. "changeRate": float64(0),
  552. }},
  553. },
  554. M: map[string]interface{}{
  555. "sink_mockSink_0_exceptions_total": int64(0),
  556. "sink_mockSink_0_records_in_total": int64(3),
  557. "sink_mockSink_0_records_out_total": int64(3),
  558. "source_demo_0_exceptions_total": int64(0),
  559. "source_demo_0_records_in_total": int64(5),
  560. "source_demo_0_records_out_total": int64(5),
  561. },
  562. },
  563. {
  564. Name: `TestLagPartition`,
  565. Sql: "SELECT color, lag(size) over (partition by color) as lastSize, size, lastSize/size as changeRate FROM demo",
  566. R: [][]map[string]interface{}{
  567. {{
  568. "color": "red",
  569. "size": float64(3),
  570. }},
  571. {{
  572. "color": "blue",
  573. "size": float64(6),
  574. }},
  575. {{
  576. "color": "blue",
  577. "lastSize": float64(6),
  578. "size": float64(2),
  579. "changeRate": float64(3),
  580. }},
  581. {{
  582. "color": "yellow",
  583. "size": float64(4),
  584. }},
  585. {{
  586. "color": "red",
  587. "lastSize": float64(3),
  588. "size": float64(1),
  589. "changeRate": float64(3),
  590. }},
  591. },
  592. M: map[string]interface{}{
  593. "sink_mockSink_0_exceptions_total": int64(0),
  594. "sink_mockSink_0_records_in_total": int64(5),
  595. "sink_mockSink_0_records_out_total": int64(5),
  596. "source_demo_0_exceptions_total": int64(0),
  597. "source_demo_0_records_in_total": int64(5),
  598. "source_demo_0_records_out_total": int64(5),
  599. },
  600. },
  601. }
  602. HandleStream(true, streamList, t)
  603. options := []*api.RuleOption{
  604. {
  605. BufferLength: 100,
  606. SendError: true,
  607. }, {
  608. BufferLength: 100,
  609. SendError: true,
  610. Qos: api.AtLeastOnce,
  611. CheckpointInterval: 5000,
  612. }, {
  613. BufferLength: 100,
  614. SendError: true,
  615. Qos: api.ExactlyOnce,
  616. CheckpointInterval: 5000,
  617. },
  618. }
  619. for j, opt := range options {
  620. DoRuleTest(t, tests, j, opt, 0)
  621. }
  622. }
  623. func TestSingleSQLError(t *testing.T) {
  624. //Reset
  625. streamList := []string{"ldemo"}
  626. HandleStream(false, streamList, t)
  627. //Data setup
  628. var tests = []RuleTest{
  629. {
  630. Name: `TestSingleSQLErrorRule1`,
  631. Sql: `SELECT color, ts FROM ldemo where size >= 3`,
  632. R: [][]map[string]interface{}{
  633. {{
  634. "color": "red",
  635. "ts": float64(1541152486013),
  636. }},
  637. {{
  638. "error": "run Where error: invalid operation string(string) >= int64(3)",
  639. }},
  640. {{
  641. "ts": float64(1541152487632),
  642. }},
  643. },
  644. M: map[string]interface{}{
  645. "op_3_project_0_exceptions_total": int64(1),
  646. "op_3_project_0_process_latency_us": int64(0),
  647. "op_3_project_0_records_in_total": int64(3),
  648. "op_3_project_0_records_out_total": int64(2),
  649. "sink_mockSink_0_exceptions_total": int64(0),
  650. "sink_mockSink_0_records_in_total": int64(3),
  651. "sink_mockSink_0_records_out_total": int64(3),
  652. "source_ldemo_0_exceptions_total": int64(0),
  653. "source_ldemo_0_records_in_total": int64(5),
  654. "source_ldemo_0_records_out_total": int64(5),
  655. "op_2_filter_0_exceptions_total": int64(1),
  656. "op_2_filter_0_process_latency_us": int64(0),
  657. "op_2_filter_0_records_in_total": int64(5),
  658. "op_2_filter_0_records_out_total": int64(2),
  659. },
  660. }, {
  661. Name: `TestSingleSQLErrorRule2`,
  662. Sql: `SELECT size * 5 FROM ldemo`,
  663. R: [][]map[string]interface{}{
  664. {{
  665. "kuiper_field_0": float64(15),
  666. }},
  667. {{
  668. "error": "run Select error: invalid operation string(string) * int64(5)",
  669. }},
  670. {{
  671. "kuiper_field_0": float64(15),
  672. }},
  673. {{
  674. "kuiper_field_0": float64(10),
  675. }},
  676. {{}},
  677. },
  678. M: map[string]interface{}{
  679. "op_2_project_0_exceptions_total": int64(1),
  680. "op_2_project_0_process_latency_us": int64(0),
  681. "op_2_project_0_records_in_total": int64(5),
  682. "op_2_project_0_records_out_total": int64(4),
  683. "sink_mockSink_0_exceptions_total": int64(0),
  684. "sink_mockSink_0_records_in_total": int64(5),
  685. "sink_mockSink_0_records_out_total": int64(5),
  686. "source_ldemo_0_exceptions_total": int64(0),
  687. "source_ldemo_0_records_in_total": int64(5),
  688. "source_ldemo_0_records_out_total": int64(5),
  689. },
  690. },
  691. }
  692. HandleStream(true, streamList, t)
  693. DoRuleTest(t, tests, 0, &api.RuleOption{
  694. BufferLength: 100,
  695. SendError: true,
  696. }, 0)
  697. }
  698. func TestSingleSQLOmitError(t *testing.T) {
  699. //Reset
  700. streamList := []string{"ldemo"}
  701. HandleStream(false, streamList, t)
  702. //Data setup
  703. var tests = []RuleTest{
  704. {
  705. Name: `TestSingleSQLErrorRule1`,
  706. Sql: `SELECT color, ts FROM ldemo where size >= 3`,
  707. R: [][]map[string]interface{}{
  708. {{
  709. "color": "red",
  710. "ts": float64(1541152486013),
  711. }},
  712. {{
  713. "ts": float64(1541152487632),
  714. }},
  715. },
  716. M: map[string]interface{}{
  717. "op_3_project_0_exceptions_total": int64(0),
  718. "op_3_project_0_process_latency_us": int64(0),
  719. "op_3_project_0_records_in_total": int64(2),
  720. "op_3_project_0_records_out_total": int64(2),
  721. "sink_mockSink_0_exceptions_total": int64(0),
  722. "sink_mockSink_0_records_in_total": int64(2),
  723. "sink_mockSink_0_records_out_total": int64(2),
  724. "source_ldemo_0_exceptions_total": int64(0),
  725. "source_ldemo_0_records_in_total": int64(5),
  726. "source_ldemo_0_records_out_total": int64(5),
  727. "op_2_filter_0_exceptions_total": int64(1),
  728. "op_2_filter_0_process_latency_us": int64(0),
  729. "op_2_filter_0_records_in_total": int64(5),
  730. "op_2_filter_0_records_out_total": int64(2),
  731. },
  732. }, {
  733. Name: `TestSingleSQLErrorRule2`,
  734. Sql: `SELECT size * 5 FROM ldemo`,
  735. R: [][]map[string]interface{}{
  736. {{
  737. "kuiper_field_0": float64(15),
  738. }},
  739. {{
  740. "kuiper_field_0": float64(15),
  741. }},
  742. {{
  743. "kuiper_field_0": float64(10),
  744. }},
  745. {{}},
  746. },
  747. M: map[string]interface{}{
  748. "op_2_project_0_exceptions_total": int64(1),
  749. "op_2_project_0_process_latency_us": int64(0),
  750. "op_2_project_0_records_in_total": int64(5),
  751. "op_2_project_0_records_out_total": int64(4),
  752. "sink_mockSink_0_exceptions_total": int64(0),
  753. "sink_mockSink_0_records_in_total": int64(4),
  754. "sink_mockSink_0_records_out_total": int64(4),
  755. "source_ldemo_0_exceptions_total": int64(0),
  756. "source_ldemo_0_records_in_total": int64(5),
  757. "source_ldemo_0_records_out_total": int64(5),
  758. },
  759. },
  760. }
  761. HandleStream(true, streamList, t)
  762. DoRuleTest(t, tests, 0, &api.RuleOption{
  763. BufferLength: 100,
  764. SendError: false,
  765. }, 0)
  766. }
  767. func TestSingleSQLTemplate(t *testing.T) {
  768. //Reset
  769. streamList := []string{"demo"}
  770. HandleStream(false, streamList, t)
  771. //Data setup
  772. var tests = []RuleTest{
  773. {
  774. Name: `TestSingleSQLTemplateRule1`,
  775. Sql: `SELECT * FROM demo`,
  776. R: []map[string]interface{}{
  777. {
  778. "c": "red",
  779. "wrapper": "w1",
  780. },
  781. {
  782. "c": "blue",
  783. "wrapper": "w1",
  784. },
  785. {
  786. "c": "blue",
  787. "wrapper": "w1",
  788. },
  789. {
  790. "c": "yellow",
  791. "wrapper": "w1",
  792. },
  793. {
  794. "c": "red",
  795. "wrapper": "w1",
  796. },
  797. },
  798. M: map[string]interface{}{
  799. "op_2_project_0_exceptions_total": int64(0),
  800. "op_2_project_0_process_latency_us": int64(0),
  801. "op_2_project_0_records_in_total": int64(5),
  802. "op_2_project_0_records_out_total": int64(5),
  803. "sink_mockSink_0_exceptions_total": int64(0),
  804. "sink_mockSink_0_records_in_total": int64(5),
  805. "sink_mockSink_0_records_out_total": int64(5),
  806. "source_demo_0_exceptions_total": int64(0),
  807. "source_demo_0_records_in_total": int64(5),
  808. "source_demo_0_records_out_total": int64(5),
  809. },
  810. },
  811. }
  812. HandleStream(true, streamList, t)
  813. doRuleTestBySinkProps(t, tests, 0, &api.RuleOption{
  814. BufferLength: 100,
  815. SendError: true,
  816. }, 0, map[string]interface{}{
  817. "dataTemplate": `{"wrapper":"w1", "c":"{{.color}}"}`,
  818. "sendSingle": true,
  819. }, func(result [][]byte) interface{} {
  820. var maps []map[string]interface{}
  821. for _, v := range result {
  822. var mapRes map[string]interface{}
  823. err := json.Unmarshal(v, &mapRes)
  824. if err != nil {
  825. t.Errorf("Failed to parse the input into map")
  826. continue
  827. }
  828. maps = append(maps, mapRes)
  829. }
  830. return maps
  831. })
  832. }
  833. func TestNoneSingleSQLTemplate(t *testing.T) {
  834. //Reset
  835. streamList := []string{"demo"}
  836. HandleStream(false, streamList, t)
  837. //Data setup
  838. var tests = []RuleTest{
  839. {
  840. Name: `TestNoneSingleSQLTemplateRule1`,
  841. Sql: `SELECT * FROM demo`,
  842. R: [][]byte{
  843. []byte("<div>results</div><ul><li>red - 3</li></ul>"),
  844. []byte("<div>results</div><ul><li>blue - 6</li></ul>"),
  845. []byte("<div>results</div><ul><li>blue - 2</li></ul>"),
  846. []byte("<div>results</div><ul><li>yellow - 4</li></ul>"),
  847. []byte("<div>results</div><ul><li>red - 1</li></ul>"),
  848. },
  849. M: map[string]interface{}{
  850. "op_2_project_0_exceptions_total": int64(0),
  851. "op_2_project_0_process_latency_us": int64(0),
  852. "op_2_project_0_records_in_total": int64(5),
  853. "op_2_project_0_records_out_total": int64(5),
  854. "sink_mockSink_0_exceptions_total": int64(0),
  855. "sink_mockSink_0_records_in_total": int64(5),
  856. "sink_mockSink_0_records_out_total": int64(5),
  857. "source_demo_0_exceptions_total": int64(0),
  858. "source_demo_0_records_in_total": int64(5),
  859. "source_demo_0_records_out_total": int64(5),
  860. },
  861. },
  862. }
  863. HandleStream(true, streamList, t)
  864. doRuleTestBySinkProps(t, tests, 0, &api.RuleOption{
  865. BufferLength: 100,
  866. SendError: true,
  867. }, 0, map[string]interface{}{
  868. "dataTemplate": `<div>results</div><ul>{{range .}}<li>{{.color}} - {{.size}}</li>{{end}}</ul>`,
  869. }, func(result [][]byte) interface{} {
  870. return result
  871. })
  872. }
  873. func TestSingleSQLForBinary(t *testing.T) {
  874. //Reset
  875. streamList := []string{"binDemo"}
  876. HandleStream(false, streamList, t)
  877. //Data setup
  878. var tests = []RuleTest{
  879. {
  880. Name: `TestSingleSQLRule1`,
  881. Sql: `SELECT * FROM binDemo`,
  882. R: [][]map[string]interface{}{
  883. {{
  884. "self": mocknode.Image,
  885. }},
  886. },
  887. W: 50,
  888. M: map[string]interface{}{
  889. "op_2_project_0_exceptions_total": int64(0),
  890. "op_2_project_0_process_latency_us": int64(0),
  891. "op_2_project_0_records_in_total": int64(1),
  892. "op_2_project_0_records_out_total": int64(1),
  893. "sink_mockSink_0_exceptions_total": int64(0),
  894. "sink_mockSink_0_records_in_total": int64(1),
  895. "sink_mockSink_0_records_out_total": int64(1),
  896. "source_binDemo_0_exceptions_total": int64(0),
  897. "source_binDemo_0_records_in_total": int64(1),
  898. "source_binDemo_0_records_out_total": int64(1),
  899. },
  900. },
  901. }
  902. HandleStream(true, streamList, t)
  903. options := []*api.RuleOption{
  904. {
  905. BufferLength: 100,
  906. SendError: true,
  907. }, {
  908. BufferLength: 100,
  909. SendError: true,
  910. Qos: api.AtLeastOnce,
  911. CheckpointInterval: 5000,
  912. }, {
  913. BufferLength: 100,
  914. SendError: true,
  915. Qos: api.ExactlyOnce,
  916. CheckpointInterval: 5000,
  917. },
  918. }
  919. byteFunc := func(result [][]byte) interface{} {
  920. var maps [][]map[string]interface{}
  921. for _, v := range result {
  922. var mapRes []map[string][]byte
  923. err := json.Unmarshal(v, &mapRes)
  924. if err != nil {
  925. panic("Failed to parse the input into map")
  926. }
  927. mapInt := make([]map[string]interface{}, len(mapRes))
  928. for i, mv := range mapRes {
  929. mapInt[i] = make(map[string]interface{})
  930. //assume only one key
  931. for k, v := range mv {
  932. mapInt[i][k] = v
  933. }
  934. }
  935. maps = append(maps, mapInt)
  936. }
  937. return maps
  938. }
  939. for j, opt := range options {
  940. doRuleTestBySinkProps(t, tests, j, opt, 0, nil, byteFunc)
  941. }
  942. }