rule_test.go 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008
  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, 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. "changeRate": float64(0),
  545. }},
  546. {{
  547. "lastSize": float64(2),
  548. "size": float64(4),
  549. "changeRate": float64(0),
  550. }},
  551. },
  552. M: map[string]interface{}{
  553. "sink_mockSink_0_exceptions_total": int64(0),
  554. "sink_mockSink_0_records_in_total": int64(3),
  555. "sink_mockSink_0_records_out_total": int64(3),
  556. "source_demo_0_exceptions_total": int64(0),
  557. "source_demo_0_records_in_total": int64(5),
  558. "source_demo_0_records_out_total": int64(5),
  559. },
  560. },
  561. {
  562. Name: `TestLagPartition`,
  563. Sql: "SELECT color, lag(size) over (partition by color) as lastSize, size, lastSize/size as changeRate FROM demo",
  564. R: [][]map[string]interface{}{
  565. {{
  566. "color": "red",
  567. "size": float64(3),
  568. }},
  569. {{
  570. "color": "blue",
  571. "size": float64(6),
  572. }},
  573. {{
  574. "color": "blue",
  575. "lastSize": float64(6),
  576. "size": float64(2),
  577. "changeRate": float64(3),
  578. }},
  579. {{
  580. "color": "yellow",
  581. "size": float64(4),
  582. }},
  583. {{
  584. "color": "red",
  585. "lastSize": float64(3),
  586. "size": float64(1),
  587. "changeRate": float64(3),
  588. }},
  589. },
  590. M: map[string]interface{}{
  591. "sink_mockSink_0_exceptions_total": int64(0),
  592. "sink_mockSink_0_records_in_total": int64(5),
  593. "sink_mockSink_0_records_out_total": int64(5),
  594. "source_demo_0_exceptions_total": int64(0),
  595. "source_demo_0_records_in_total": int64(5),
  596. "source_demo_0_records_out_total": int64(5),
  597. },
  598. },
  599. }
  600. HandleStream(true, streamList, t)
  601. options := []*api.RuleOption{
  602. {
  603. BufferLength: 100,
  604. SendError: true,
  605. }, {
  606. BufferLength: 100,
  607. SendError: true,
  608. Qos: api.AtLeastOnce,
  609. CheckpointInterval: 5000,
  610. }, {
  611. BufferLength: 100,
  612. SendError: true,
  613. Qos: api.ExactlyOnce,
  614. CheckpointInterval: 5000,
  615. },
  616. }
  617. for j, opt := range options {
  618. DoRuleTest(t, tests, j, opt, 0)
  619. }
  620. }
  621. func TestSingleSQLError(t *testing.T) {
  622. //Reset
  623. streamList := []string{"ldemo"}
  624. HandleStream(false, streamList, t)
  625. //Data setup
  626. var tests = []RuleTest{
  627. {
  628. Name: `TestSingleSQLErrorRule1`,
  629. Sql: `SELECT color, ts FROM ldemo where size >= 3`,
  630. R: [][]map[string]interface{}{
  631. {{
  632. "color": "red",
  633. "ts": float64(1541152486013),
  634. }},
  635. {{
  636. "error": "run Where error: invalid operation string(string) >= int64(3)",
  637. }},
  638. {{
  639. "ts": float64(1541152487632),
  640. }},
  641. },
  642. M: map[string]interface{}{
  643. "op_3_project_0_exceptions_total": int64(1),
  644. "op_3_project_0_process_latency_us": int64(0),
  645. "op_3_project_0_records_in_total": int64(3),
  646. "op_3_project_0_records_out_total": int64(2),
  647. "sink_mockSink_0_exceptions_total": int64(0),
  648. "sink_mockSink_0_records_in_total": int64(3),
  649. "sink_mockSink_0_records_out_total": int64(3),
  650. "source_ldemo_0_exceptions_total": int64(0),
  651. "source_ldemo_0_records_in_total": int64(5),
  652. "source_ldemo_0_records_out_total": int64(5),
  653. "op_2_filter_0_exceptions_total": int64(1),
  654. "op_2_filter_0_process_latency_us": int64(0),
  655. "op_2_filter_0_records_in_total": int64(5),
  656. "op_2_filter_0_records_out_total": int64(2),
  657. },
  658. }, {
  659. Name: `TestSingleSQLErrorRule2`,
  660. Sql: `SELECT size * 5 FROM ldemo`,
  661. R: [][]map[string]interface{}{
  662. {{
  663. "kuiper_field_0": float64(15),
  664. }},
  665. {{
  666. "error": "run Select error: invalid operation string(string) * int64(5)",
  667. }},
  668. {{
  669. "kuiper_field_0": float64(15),
  670. }},
  671. {{
  672. "kuiper_field_0": float64(10),
  673. }},
  674. {{}},
  675. },
  676. M: map[string]interface{}{
  677. "op_2_project_0_exceptions_total": int64(1),
  678. "op_2_project_0_process_latency_us": int64(0),
  679. "op_2_project_0_records_in_total": int64(5),
  680. "op_2_project_0_records_out_total": int64(4),
  681. "sink_mockSink_0_exceptions_total": int64(0),
  682. "sink_mockSink_0_records_in_total": int64(5),
  683. "sink_mockSink_0_records_out_total": int64(5),
  684. "source_ldemo_0_exceptions_total": int64(0),
  685. "source_ldemo_0_records_in_total": int64(5),
  686. "source_ldemo_0_records_out_total": int64(5),
  687. },
  688. },
  689. }
  690. HandleStream(true, streamList, t)
  691. DoRuleTest(t, tests, 0, &api.RuleOption{
  692. BufferLength: 100,
  693. SendError: true,
  694. }, 0)
  695. }
  696. func TestSingleSQLOmitError(t *testing.T) {
  697. //Reset
  698. streamList := []string{"ldemo"}
  699. HandleStream(false, streamList, t)
  700. //Data setup
  701. var tests = []RuleTest{
  702. {
  703. Name: `TestSingleSQLErrorRule1`,
  704. Sql: `SELECT color, ts FROM ldemo where size >= 3`,
  705. R: [][]map[string]interface{}{
  706. {{
  707. "color": "red",
  708. "ts": float64(1541152486013),
  709. }},
  710. {{
  711. "ts": float64(1541152487632),
  712. }},
  713. },
  714. M: map[string]interface{}{
  715. "op_3_project_0_exceptions_total": int64(0),
  716. "op_3_project_0_process_latency_us": int64(0),
  717. "op_3_project_0_records_in_total": int64(2),
  718. "op_3_project_0_records_out_total": int64(2),
  719. "sink_mockSink_0_exceptions_total": int64(0),
  720. "sink_mockSink_0_records_in_total": int64(2),
  721. "sink_mockSink_0_records_out_total": int64(2),
  722. "source_ldemo_0_exceptions_total": int64(0),
  723. "source_ldemo_0_records_in_total": int64(5),
  724. "source_ldemo_0_records_out_total": int64(5),
  725. "op_2_filter_0_exceptions_total": int64(1),
  726. "op_2_filter_0_process_latency_us": int64(0),
  727. "op_2_filter_0_records_in_total": int64(5),
  728. "op_2_filter_0_records_out_total": int64(2),
  729. },
  730. }, {
  731. Name: `TestSingleSQLErrorRule2`,
  732. Sql: `SELECT size * 5 FROM ldemo`,
  733. R: [][]map[string]interface{}{
  734. {{
  735. "kuiper_field_0": float64(15),
  736. }},
  737. {{
  738. "kuiper_field_0": float64(15),
  739. }},
  740. {{
  741. "kuiper_field_0": float64(10),
  742. }},
  743. {{}},
  744. },
  745. M: map[string]interface{}{
  746. "op_2_project_0_exceptions_total": int64(1),
  747. "op_2_project_0_process_latency_us": int64(0),
  748. "op_2_project_0_records_in_total": int64(5),
  749. "op_2_project_0_records_out_total": int64(4),
  750. "sink_mockSink_0_exceptions_total": int64(0),
  751. "sink_mockSink_0_records_in_total": int64(4),
  752. "sink_mockSink_0_records_out_total": int64(4),
  753. "source_ldemo_0_exceptions_total": int64(0),
  754. "source_ldemo_0_records_in_total": int64(5),
  755. "source_ldemo_0_records_out_total": int64(5),
  756. },
  757. },
  758. }
  759. HandleStream(true, streamList, t)
  760. DoRuleTest(t, tests, 0, &api.RuleOption{
  761. BufferLength: 100,
  762. SendError: false,
  763. }, 0)
  764. }
  765. func TestSingleSQLTemplate(t *testing.T) {
  766. //Reset
  767. streamList := []string{"demo"}
  768. HandleStream(false, streamList, t)
  769. //Data setup
  770. var tests = []RuleTest{
  771. {
  772. Name: `TestSingleSQLTemplateRule1`,
  773. Sql: `SELECT * FROM demo`,
  774. R: []map[string]interface{}{
  775. {
  776. "c": "red",
  777. "wrapper": "w1",
  778. },
  779. {
  780. "c": "blue",
  781. "wrapper": "w1",
  782. },
  783. {
  784. "c": "blue",
  785. "wrapper": "w1",
  786. },
  787. {
  788. "c": "yellow",
  789. "wrapper": "w1",
  790. },
  791. {
  792. "c": "red",
  793. "wrapper": "w1",
  794. },
  795. },
  796. M: map[string]interface{}{
  797. "op_2_project_0_exceptions_total": int64(0),
  798. "op_2_project_0_process_latency_us": int64(0),
  799. "op_2_project_0_records_in_total": int64(5),
  800. "op_2_project_0_records_out_total": int64(5),
  801. "sink_mockSink_0_exceptions_total": int64(0),
  802. "sink_mockSink_0_records_in_total": int64(5),
  803. "sink_mockSink_0_records_out_total": int64(5),
  804. "source_demo_0_exceptions_total": int64(0),
  805. "source_demo_0_records_in_total": int64(5),
  806. "source_demo_0_records_out_total": int64(5),
  807. },
  808. },
  809. }
  810. HandleStream(true, streamList, t)
  811. doRuleTestBySinkProps(t, tests, 0, &api.RuleOption{
  812. BufferLength: 100,
  813. SendError: true,
  814. }, 0, map[string]interface{}{
  815. "dataTemplate": `{"wrapper":"w1", "c":"{{.color}}"}`,
  816. "sendSingle": true,
  817. }, func(result [][]byte) interface{} {
  818. var maps []map[string]interface{}
  819. for _, v := range result {
  820. var mapRes map[string]interface{}
  821. err := json.Unmarshal(v, &mapRes)
  822. if err != nil {
  823. t.Errorf("Failed to parse the input into map")
  824. continue
  825. }
  826. maps = append(maps, mapRes)
  827. }
  828. return maps
  829. })
  830. }
  831. func TestNoneSingleSQLTemplate(t *testing.T) {
  832. //Reset
  833. streamList := []string{"demo"}
  834. HandleStream(false, streamList, t)
  835. //Data setup
  836. var tests = []RuleTest{
  837. {
  838. Name: `TestNoneSingleSQLTemplateRule1`,
  839. Sql: `SELECT * FROM demo`,
  840. R: [][]byte{
  841. []byte("<div>results</div><ul><li>red - 3</li></ul>"),
  842. []byte("<div>results</div><ul><li>blue - 6</li></ul>"),
  843. []byte("<div>results</div><ul><li>blue - 2</li></ul>"),
  844. []byte("<div>results</div><ul><li>yellow - 4</li></ul>"),
  845. []byte("<div>results</div><ul><li>red - 1</li></ul>"),
  846. },
  847. M: map[string]interface{}{
  848. "op_2_project_0_exceptions_total": int64(0),
  849. "op_2_project_0_process_latency_us": int64(0),
  850. "op_2_project_0_records_in_total": int64(5),
  851. "op_2_project_0_records_out_total": int64(5),
  852. "sink_mockSink_0_exceptions_total": int64(0),
  853. "sink_mockSink_0_records_in_total": int64(5),
  854. "sink_mockSink_0_records_out_total": int64(5),
  855. "source_demo_0_exceptions_total": int64(0),
  856. "source_demo_0_records_in_total": int64(5),
  857. "source_demo_0_records_out_total": int64(5),
  858. },
  859. },
  860. }
  861. HandleStream(true, streamList, t)
  862. doRuleTestBySinkProps(t, tests, 0, &api.RuleOption{
  863. BufferLength: 100,
  864. SendError: true,
  865. }, 0, map[string]interface{}{
  866. "dataTemplate": `<div>results</div><ul>{{range .}}<li>{{.color}} - {{.size}}</li>{{end}}</ul>`,
  867. }, func(result [][]byte) interface{} {
  868. return result
  869. })
  870. }
  871. func TestSingleSQLForBinary(t *testing.T) {
  872. //Reset
  873. streamList := []string{"binDemo"}
  874. HandleStream(false, streamList, t)
  875. //Data setup
  876. var tests = []RuleTest{
  877. {
  878. Name: `TestSingleSQLRule1`,
  879. Sql: `SELECT * FROM binDemo`,
  880. R: [][]map[string]interface{}{
  881. {{
  882. "self": mocknode.Image,
  883. }},
  884. },
  885. W: 50,
  886. M: map[string]interface{}{
  887. "op_2_project_0_exceptions_total": int64(0),
  888. "op_2_project_0_process_latency_us": int64(0),
  889. "op_2_project_0_records_in_total": int64(1),
  890. "op_2_project_0_records_out_total": int64(1),
  891. "sink_mockSink_0_exceptions_total": int64(0),
  892. "sink_mockSink_0_records_in_total": int64(1),
  893. "sink_mockSink_0_records_out_total": int64(1),
  894. "source_binDemo_0_exceptions_total": int64(0),
  895. "source_binDemo_0_records_in_total": int64(1),
  896. "source_binDemo_0_records_out_total": int64(1),
  897. },
  898. },
  899. }
  900. HandleStream(true, streamList, t)
  901. options := []*api.RuleOption{
  902. {
  903. BufferLength: 100,
  904. SendError: true,
  905. }, {
  906. BufferLength: 100,
  907. SendError: true,
  908. Qos: api.AtLeastOnce,
  909. CheckpointInterval: 5000,
  910. }, {
  911. BufferLength: 100,
  912. SendError: true,
  913. Qos: api.ExactlyOnce,
  914. CheckpointInterval: 5000,
  915. },
  916. }
  917. byteFunc := func(result [][]byte) interface{} {
  918. var maps [][]map[string]interface{}
  919. for _, v := range result {
  920. var mapRes []map[string][]byte
  921. err := json.Unmarshal(v, &mapRes)
  922. if err != nil {
  923. panic("Failed to parse the input into map")
  924. }
  925. mapInt := make([]map[string]interface{}, len(mapRes))
  926. for i, mv := range mapRes {
  927. mapInt[i] = make(map[string]interface{})
  928. //assume only one key
  929. for k, v := range mv {
  930. mapInt[i][k] = v
  931. }
  932. }
  933. maps = append(maps, mapInt)
  934. }
  935. return maps
  936. }
  937. for j, opt := range options {
  938. doRuleTestBySinkProps(t, tests, j, opt, 0, nil, byteFunc)
  939. }
  940. }