rule_test.go 33 KB

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