rule_test.go 30 KB

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