rule_test.go 29 KB

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