window_rule_test.go 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666
  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. "testing"
  17. "github.com/lf-edge/ekuiper/pkg/api"
  18. )
  19. func TestWindow(t *testing.T) {
  20. // Reset
  21. streamList := []string{"demo", "demoError", "demo1", "sessionDemo", "table1"}
  22. HandleStream(false, streamList, t)
  23. tests := []RuleTest{
  24. {
  25. Name: `TestWindowRule0`,
  26. Sql: `SELECT size,color FROM demo GROUP BY SlidingWindow(ss, 5) Filter (where color = "red") Over (when size = 1)`,
  27. R: [][]map[string]interface{}{
  28. {
  29. {
  30. "size": float64(3),
  31. "color": "red",
  32. },
  33. {
  34. "size": float64(1),
  35. "color": "red",
  36. },
  37. },
  38. },
  39. M: map[string]interface{}{},
  40. },
  41. {
  42. Name: `TestWindowRule1`,
  43. Sql: `SELECT *, event_time() as et FROM demo GROUP BY HOPPINGWINDOW(ss, 2, 1)`,
  44. R: [][]map[string]interface{}{
  45. {{
  46. "color": "red",
  47. "size": float64(3),
  48. "ts": float64(1541152486013),
  49. "et": float64(1541152486013),
  50. }, {
  51. "color": "blue",
  52. "size": float64(6),
  53. "ts": float64(1541152486822),
  54. "et": float64(1541152486822),
  55. }},
  56. {{
  57. "color": "red",
  58. "size": float64(3),
  59. "ts": float64(1541152486013),
  60. "et": float64(1541152486013),
  61. }, {
  62. "color": "blue",
  63. "size": float64(6),
  64. "ts": float64(1541152486822),
  65. "et": float64(1541152486822),
  66. }, {
  67. "color": "blue",
  68. "size": float64(2),
  69. "ts": float64(1541152487632),
  70. "et": float64(1541152487632),
  71. }},
  72. {{
  73. "color": "blue",
  74. "size": float64(2),
  75. "ts": float64(1541152487632),
  76. "et": float64(1541152487632),
  77. }, {
  78. "color": "yellow",
  79. "size": float64(4),
  80. "ts": float64(1541152488442),
  81. "et": float64(1541152488442),
  82. }},
  83. {{
  84. "color": "yellow",
  85. "size": float64(4),
  86. "ts": float64(1541152488442),
  87. "et": float64(1541152488442),
  88. }, {
  89. "color": "red",
  90. "size": float64(1),
  91. "ts": float64(1541152489252),
  92. "et": float64(1541152489252),
  93. }},
  94. },
  95. M: map[string]interface{}{
  96. "op_3_project_0_exceptions_total": int64(0),
  97. "op_3_project_0_process_latency_us": int64(0),
  98. "op_3_project_0_records_in_total": int64(4),
  99. "op_3_project_0_records_out_total": int64(4),
  100. "sink_mockSink_0_exceptions_total": int64(0),
  101. "sink_mockSink_0_records_in_total": int64(4),
  102. "sink_mockSink_0_records_out_total": int64(4),
  103. "source_demo_0_exceptions_total": int64(0),
  104. "source_demo_0_records_in_total": int64(5),
  105. "source_demo_0_records_out_total": int64(5),
  106. "op_2_window_0_exceptions_total": int64(0),
  107. "op_2_window_0_process_latency_us": int64(0),
  108. "op_2_window_0_records_in_total": int64(5),
  109. "op_2_window_0_records_out_total": int64(4),
  110. },
  111. },
  112. {
  113. Name: `TestWindowRule2`,
  114. Sql: `SELECT color, ts FROM demo where size > 2 GROUP BY tumblingwindow(ss, 1)`,
  115. R: [][]map[string]interface{}{
  116. {{
  117. "color": "red",
  118. "ts": float64(1541152486013),
  119. }, {
  120. "color": "blue",
  121. "ts": float64(1541152486822),
  122. }},
  123. {},
  124. {{
  125. "color": "yellow",
  126. "ts": float64(1541152488442),
  127. }},
  128. {},
  129. },
  130. M: map[string]interface{}{
  131. "op_4_project_0_exceptions_total": int64(0),
  132. "op_4_project_0_process_latency_us": int64(0),
  133. "op_4_project_0_records_in_total": int64(4),
  134. "op_4_project_0_records_out_total": int64(4),
  135. "sink_mockSink_0_exceptions_total": int64(0),
  136. "sink_mockSink_0_records_in_total": int64(4),
  137. "sink_mockSink_0_records_out_total": int64(4),
  138. "source_demo_0_exceptions_total": int64(0),
  139. "source_demo_0_records_in_total": int64(5),
  140. "source_demo_0_records_out_total": int64(5),
  141. "op_3_window_0_exceptions_total": int64(0),
  142. "op_3_window_0_process_latency_us": int64(0),
  143. "op_3_window_0_records_in_total": int64(3),
  144. "op_3_window_0_records_out_total": int64(4),
  145. "op_2_filter_0_exceptions_total": int64(0),
  146. "op_2_filter_0_process_latency_us": int64(0),
  147. "op_2_filter_0_records_in_total": int64(5),
  148. "op_2_filter_0_records_out_total": int64(3),
  149. },
  150. },
  151. {
  152. Name: `TestWindowRule3`,
  153. Sql: `SELECT color, temp, demo.ts as ts1, demo1.ts as ts2, demo.ts - demo1.ts as diff FROM demo INNER JOIN demo1 ON ts1 = ts2 GROUP BY SlidingWindow(ss, 1) HAVING last_agg_hit_count() < 7`,
  154. R: [][]map[string]interface{}{
  155. {{
  156. "color": "red",
  157. "temp": 25.5,
  158. "ts1": float64(1541152486013),
  159. "ts2": float64(1541152486013),
  160. "diff": float64(0),
  161. }}, {{
  162. "color": "red",
  163. "temp": 25.5,
  164. "ts1": float64(1541152486013),
  165. "ts2": float64(1541152486013),
  166. "diff": float64(0),
  167. }}, {{
  168. "color": "red",
  169. "temp": 25.5,
  170. "ts1": float64(1541152486013),
  171. "ts2": float64(1541152486013),
  172. "diff": float64(0),
  173. }}, {{
  174. "color": "blue",
  175. "temp": 28.1,
  176. "ts1": float64(1541152487632),
  177. "ts2": float64(1541152487632),
  178. "diff": float64(0),
  179. }}, {{
  180. "color": "blue",
  181. "temp": 28.1,
  182. "ts1": float64(1541152487632),
  183. "ts2": float64(1541152487632),
  184. "diff": float64(0),
  185. }}, {{
  186. "color": "blue",
  187. "temp": 28.1,
  188. "ts1": float64(1541152487632),
  189. "ts2": float64(1541152487632),
  190. "diff": float64(0),
  191. }, {
  192. "color": "yellow",
  193. "temp": 27.4,
  194. "ts1": float64(1541152488442),
  195. "ts2": float64(1541152488442),
  196. "diff": float64(0),
  197. }}, {{
  198. "color": "yellow",
  199. "temp": 27.4,
  200. "ts1": float64(1541152488442),
  201. "ts2": float64(1541152488442),
  202. "diff": float64(0),
  203. }},
  204. },
  205. M: map[string]interface{}{
  206. "sink_mockSink_0_exceptions_total": int64(0),
  207. "sink_mockSink_0_records_in_total": int64(7),
  208. "sink_mockSink_0_records_out_total": int64(7),
  209. "source_demo_0_exceptions_total": int64(0),
  210. "source_demo_0_records_in_total": int64(5),
  211. "source_demo_0_records_out_total": int64(5),
  212. "source_demo1_0_exceptions_total": int64(0),
  213. "source_demo1_0_records_in_total": int64(5),
  214. "source_demo1_0_records_out_total": int64(5),
  215. "op_3_window_0_exceptions_total": int64(0),
  216. "op_3_window_0_process_latency_us": int64(0),
  217. "op_3_window_0_records_in_total": int64(10),
  218. "op_3_window_0_records_out_total": int64(10),
  219. "op_4_join_0_exceptions_total": int64(0),
  220. "op_4_join_0_process_latency_us": int64(0),
  221. "op_4_join_0_records_in_total": int64(10),
  222. "op_4_join_0_records_out_total": int64(8),
  223. },
  224. T: &api.PrintableTopo{
  225. Sources: []string{"source_demo", "source_demo1"},
  226. Edges: map[string][]interface{}{
  227. "source_demo": {"op_3_window"},
  228. "source_demo1": {"op_3_window"},
  229. "op_3_window": {"op_4_join"},
  230. "op_4_join": {"op_5_having"},
  231. "op_5_having": {"op_6_project"},
  232. "op_6_project": {"sink_mockSink"},
  233. },
  234. },
  235. },
  236. {
  237. Name: `TestWindowRule4`,
  238. Sql: `SELECT color, count(*) as c FROM demo GROUP BY SlidingWindow(ss, 2) OVER(WHEN ts - last_hit_time() > 1000) , color ORDER BY color`,
  239. R: [][]map[string]interface{}{
  240. {{
  241. "color": "red",
  242. "c": float64(1),
  243. }}, {{
  244. "color": "blue",
  245. "c": float64(2),
  246. }, {
  247. "color": "red",
  248. "c": float64(1),
  249. }}, {{
  250. "color": "blue",
  251. "c": float64(1),
  252. }, {
  253. "color": "red",
  254. "c": float64(1),
  255. }, {
  256. "color": "yellow",
  257. "c": float64(1),
  258. }},
  259. },
  260. M: map[string]interface{}{
  261. "op_5_project_0_exceptions_total": int64(0),
  262. "op_5_project_0_process_latency_us": int64(0),
  263. "op_5_project_0_records_in_total": int64(3),
  264. "op_5_project_0_records_out_total": int64(3),
  265. "sink_mockSink_0_exceptions_total": int64(0),
  266. "sink_mockSink_0_records_in_total": int64(3),
  267. "sink_mockSink_0_records_out_total": int64(3),
  268. "source_demo_0_exceptions_total": int64(0),
  269. "source_demo_0_records_in_total": int64(5),
  270. "source_demo_0_records_out_total": int64(5),
  271. "op_2_window_0_exceptions_total": int64(0),
  272. "op_2_window_0_process_latency_us": int64(0),
  273. "op_2_window_0_records_in_total": int64(5),
  274. "op_2_window_0_records_out_total": int64(3),
  275. },
  276. },
  277. {
  278. Name: `TestWindowRule5`,
  279. Sql: `SELECT count(temp), window_start() as ws, window_end(), event_time() as et FROM sessionDemo GROUP BY SessionWindow(ss, 2, 1) `,
  280. R: [][]map[string]interface{}{
  281. {{
  282. "count": float64(2),
  283. "ws": float64(1541152486013),
  284. "window_end": float64(1541152487823), // timeout
  285. "et": float64(1541152487823),
  286. }}, {{
  287. "count": float64(3),
  288. "ws": float64(1541152487932),
  289. "window_end": float64(1541152490000), // tick
  290. "et": float64(1541152490000),
  291. }}, {{
  292. "count": float64(5),
  293. "ws": float64(1541152490000),
  294. "window_end": float64(1541152494000), // tick
  295. "et": float64(1541152494000),
  296. }}, {{
  297. "count": float64(1),
  298. "ws": float64(1541152494000),
  299. "window_end": float64(1541152495112), // timeout
  300. "et": float64(1541152495112),
  301. }},
  302. },
  303. M: map[string]interface{}{
  304. "op_3_project_0_exceptions_total": int64(0),
  305. "op_3_project_0_process_latency_us": int64(0),
  306. "op_3_project_0_records_in_total": int64(4),
  307. "op_3_project_0_records_out_total": int64(4),
  308. "sink_mockSink_0_exceptions_total": int64(0),
  309. "sink_mockSink_0_records_in_total": int64(4),
  310. "sink_mockSink_0_records_out_total": int64(4),
  311. "source_sessionDemo_0_exceptions_total": int64(0),
  312. "source_sessionDemo_0_records_in_total": int64(11),
  313. "source_sessionDemo_0_records_out_total": int64(11),
  314. "op_2_window_0_exceptions_total": int64(0),
  315. "op_2_window_0_process_latency_us": int64(0),
  316. "op_2_window_0_records_in_total": int64(11),
  317. "op_2_window_0_records_out_total": int64(4),
  318. },
  319. },
  320. {
  321. Name: `TestWindowRule6`,
  322. Sql: `SELECT window_end(), event_time(), sum(temp) as temp, count(color) as c, window_start() FROM demo INNER JOIN demo1 ON demo.ts = demo1.ts GROUP BY SlidingWindow(ss, 1)`,
  323. R: [][]map[string]interface{}{
  324. {{
  325. "temp": 25.5,
  326. "c": float64(1),
  327. "window_start": float64(1541152485115),
  328. "window_end": float64(1541152486115),
  329. "event_time": float64(1541152486115),
  330. }}, {{
  331. "temp": 25.5,
  332. "c": float64(1),
  333. "window_start": float64(1541152485822),
  334. "window_end": float64(1541152486822),
  335. "event_time": float64(1541152486822),
  336. }}, {{
  337. "temp": 25.5,
  338. "c": float64(1),
  339. "window_start": float64(1541152485903),
  340. "window_end": float64(1541152486903),
  341. "event_time": float64(1541152486903),
  342. }}, {{
  343. "temp": 28.1,
  344. "c": float64(1),
  345. "window_start": float64(1541152486702),
  346. "window_end": float64(1541152487702),
  347. "event_time": float64(1541152487702),
  348. }}, {{
  349. "temp": 28.1,
  350. "c": float64(1),
  351. "window_start": float64(1541152487442),
  352. "window_end": float64(1541152488442),
  353. "event_time": float64(1541152488442),
  354. }}, {{
  355. "temp": 55.5,
  356. "c": float64(2),
  357. "window_start": float64(1541152487605),
  358. "window_end": float64(1541152488605),
  359. "event_time": float64(1541152488605),
  360. }}, {{
  361. "temp": 27.4,
  362. "c": float64(1),
  363. "window_start": float64(1541152488252),
  364. "window_end": float64(1541152489252),
  365. "event_time": float64(1541152489252),
  366. }}, {{
  367. "temp": 52.9,
  368. "c": float64(2),
  369. "window_start": float64(1541152488305),
  370. "window_end": float64(1541152489305),
  371. "event_time": float64(1541152489305),
  372. }},
  373. },
  374. M: map[string]interface{}{
  375. "op_5_project_0_exceptions_total": int64(0),
  376. "op_5_project_0_process_latency_us": int64(0),
  377. "op_5_project_0_records_in_total": int64(8),
  378. "op_5_project_0_records_out_total": int64(8),
  379. "sink_mockSink_0_exceptions_total": int64(0),
  380. "sink_mockSink_0_records_in_total": int64(8),
  381. "sink_mockSink_0_records_out_total": int64(8),
  382. "source_demo_0_exceptions_total": int64(0),
  383. "source_demo_0_records_in_total": int64(5),
  384. "source_demo_0_records_out_total": int64(5),
  385. "source_demo1_0_exceptions_total": int64(0),
  386. "source_demo1_0_records_in_total": int64(5),
  387. "source_demo1_0_records_out_total": int64(5),
  388. "op_3_window_0_exceptions_total": int64(0),
  389. "op_3_window_0_process_latency_us": int64(0),
  390. "op_3_window_0_records_in_total": int64(10),
  391. "op_3_window_0_records_out_total": int64(10),
  392. "op_4_join_0_exceptions_total": int64(0),
  393. "op_4_join_0_process_latency_us": int64(0),
  394. "op_4_join_0_records_in_total": int64(10),
  395. "op_4_join_0_records_out_total": int64(8),
  396. },
  397. },
  398. {
  399. Name: `TestWindowRule7`,
  400. Sql: `SELECT * FROM demoError GROUP BY HOPPINGWINDOW(ss, 2, 1)`,
  401. R: [][]map[string]interface{}{
  402. {{
  403. "error": "error in preprocessor: field size type mismatch: cannot convert string(red) to int64",
  404. }},
  405. {{
  406. "color": "blue",
  407. "size": float64(6),
  408. "ts": float64(1541152486822),
  409. }},
  410. {{
  411. "color": "blue",
  412. "size": float64(6),
  413. "ts": float64(1541152486822),
  414. }, {
  415. "color": "blue",
  416. "size": float64(2),
  417. "ts": float64(1541152487632),
  418. }},
  419. {{
  420. "error": "error in preprocessor: field color type mismatch: cannot convert int(7) to string",
  421. }},
  422. {{
  423. "color": "blue",
  424. "size": float64(2),
  425. "ts": float64(1541152487632),
  426. }},
  427. {{
  428. "error": "error in preprocessor: field size type mismatch: cannot convert string(blue) to int64",
  429. }},
  430. {},
  431. },
  432. M: map[string]interface{}{
  433. "op_3_project_0_exceptions_total": int64(3),
  434. "op_3_project_0_process_latency_us": int64(0),
  435. "op_3_project_0_records_in_total": int64(4),
  436. "op_3_project_0_records_out_total": int64(4),
  437. "sink_mockSink_0_exceptions_total": int64(0),
  438. "sink_mockSink_0_records_in_total": int64(7),
  439. "sink_mockSink_0_records_out_total": int64(7),
  440. "source_demoError_0_exceptions_total": int64(3),
  441. "source_demoError_0_records_in_total": int64(5),
  442. "source_demoError_0_records_out_total": int64(2),
  443. "op_2_window_0_exceptions_total": int64(3),
  444. "op_2_window_0_process_latency_us": int64(0),
  445. "op_2_window_0_records_in_total": int64(5),
  446. "op_2_window_0_records_out_total": int64(4),
  447. },
  448. },
  449. {
  450. Name: `TestWindowRule8`,
  451. Sql: `SELECT color, window_end(), event_time() as et, ts, count(*) as c, window_start() FROM demo where size > 2 GROUP BY tumblingwindow(ss, 1) having c > 1`,
  452. R: [][]map[string]interface{}{
  453. {{
  454. "color": "red",
  455. "ts": float64(1541152486013),
  456. "c": float64(2),
  457. "window_start": float64(1541152486000),
  458. "window_end": float64(1541152487000),
  459. "et": float64(1541152487000),
  460. }},
  461. },
  462. M: map[string]interface{}{
  463. "op_5_project_0_exceptions_total": int64(0),
  464. "op_5_project_0_process_latency_us": int64(0),
  465. "op_5_project_0_records_in_total": int64(1),
  466. "op_5_project_0_records_out_total": int64(1),
  467. "sink_mockSink_0_exceptions_total": int64(0),
  468. "sink_mockSink_0_records_in_total": int64(1),
  469. "sink_mockSink_0_records_out_total": int64(1),
  470. "source_demo_0_exceptions_total": int64(0),
  471. "source_demo_0_records_in_total": int64(5),
  472. "source_demo_0_records_out_total": int64(5),
  473. "op_3_window_0_exceptions_total": int64(0),
  474. "op_3_window_0_process_latency_us": int64(0),
  475. "op_3_window_0_records_in_total": int64(3),
  476. "op_3_window_0_records_out_total": int64(4),
  477. "op_2_filter_0_exceptions_total": int64(0),
  478. "op_2_filter_0_process_latency_us": int64(0),
  479. "op_2_filter_0_records_in_total": int64(5),
  480. "op_2_filter_0_records_out_total": int64(3),
  481. "op_4_having_0_exceptions_total": int64(0),
  482. "op_4_having_0_process_latency_us": int64(0),
  483. "op_4_having_0_records_in_total": int64(4),
  484. "op_4_having_0_records_out_total": int64(1),
  485. },
  486. },
  487. {
  488. Name: `TestWindowRule9`,
  489. Sql: `SELECT color, window_start(), window_end() FROM demo GROUP BY HOPPINGWINDOW(ss, 2, 1) FILTER( WHERE size > 2)`,
  490. R: [][]map[string]interface{}{
  491. {{
  492. "color": "red",
  493. "window_start": float64(1541152485000),
  494. "window_end": float64(1541152487000),
  495. }, {
  496. "color": "blue",
  497. "window_start": float64(1541152485000),
  498. "window_end": float64(1541152487000),
  499. }},
  500. {{
  501. "color": "red",
  502. "window_start": float64(1541152486000),
  503. "window_end": float64(1541152488000),
  504. }, {
  505. "color": "blue",
  506. "window_start": float64(1541152486000),
  507. "window_end": float64(1541152488000),
  508. }},
  509. {{
  510. "color": "yellow",
  511. "window_start": float64(1541152487000),
  512. "window_end": float64(1541152489000),
  513. }},
  514. {{
  515. "color": "yellow",
  516. "window_start": float64(1541152488000),
  517. "window_end": float64(1541152490000),
  518. }},
  519. },
  520. M: map[string]interface{}{
  521. "op_4_project_0_exceptions_total": int64(0),
  522. "op_4_project_0_process_latency_us": int64(0),
  523. "op_4_project_0_records_in_total": int64(4),
  524. "op_4_project_0_records_out_total": int64(4),
  525. "sink_mockSink_0_exceptions_total": int64(0),
  526. "sink_mockSink_0_records_in_total": int64(4),
  527. "sink_mockSink_0_records_out_total": int64(4),
  528. "source_demo_0_exceptions_total": int64(0),
  529. "source_demo_0_records_in_total": int64(5),
  530. "source_demo_0_records_out_total": int64(5),
  531. "op_3_window_0_exceptions_total": int64(0),
  532. "op_3_window_0_process_latency_us": int64(0),
  533. "op_3_window_0_records_in_total": int64(3),
  534. "op_3_window_0_records_out_total": int64(4),
  535. },
  536. },
  537. {
  538. Name: `TestCountWindowRule1`,
  539. Sql: `SELECT collect(*)[0]->color as c, window_end() as we FROM demo GROUP BY COUNTWINDOW(3)`,
  540. R: [][]map[string]interface{}{
  541. {{
  542. "c": "red",
  543. "we": 1.541152487632e+12,
  544. }},
  545. },
  546. M: map[string]interface{}{
  547. "op_3_project_0_exceptions_total": int64(0),
  548. "op_3_project_0_process_latency_us": int64(0),
  549. "op_3_project_0_records_in_total": int64(1),
  550. "op_3_project_0_records_out_total": int64(1),
  551. "sink_mockSink_0_exceptions_total": int64(0),
  552. "sink_mockSink_0_records_in_total": int64(1),
  553. "sink_mockSink_0_records_out_total": int64(1),
  554. "source_demo_0_exceptions_total": int64(0),
  555. "source_demo_0_records_in_total": int64(5),
  556. "source_demo_0_records_out_total": int64(5),
  557. "op_2_window_0_exceptions_total": int64(0),
  558. "op_2_window_0_process_latency_us": int64(0),
  559. "op_2_window_0_records_in_total": int64(5),
  560. "op_2_window_0_records_out_total": int64(1),
  561. },
  562. },
  563. {
  564. Name: `TestWindowRule10`,
  565. Sql: `SELECT deduplicate(color, false)->color as c FROM demo GROUP BY SlidingWindow(hh, 1)`,
  566. R: [][]map[string]interface{}{
  567. {{
  568. "c": "red",
  569. }}, {{
  570. "c": "blue",
  571. }}, {{}}, {{
  572. "c": "yellow",
  573. }}, {{}},
  574. },
  575. M: map[string]interface{}{
  576. "op_3_project_0_exceptions_total": int64(0),
  577. "op_3_project_0_process_latency_us": int64(0),
  578. "op_3_project_0_records_in_total": int64(5),
  579. "op_3_project_0_records_out_total": int64(5),
  580. "sink_mockSink_0_exceptions_total": int64(0),
  581. "sink_mockSink_0_records_in_total": int64(5),
  582. "sink_mockSink_0_records_out_total": int64(5),
  583. "source_demo_0_exceptions_total": int64(0),
  584. "source_demo_0_records_in_total": int64(5),
  585. "source_demo_0_records_out_total": int64(5),
  586. "op_2_window_0_exceptions_total": int64(0),
  587. "op_2_window_0_process_latency_us": int64(0),
  588. "op_2_window_0_records_in_total": int64(5),
  589. "op_2_window_0_records_out_total": int64(5),
  590. },
  591. },
  592. {
  593. Name: `TestWindowRule11`,
  594. Sql: `SELECT color, name, window_start(), window_end() FROM demo INNER JOIN table1 on demo.ts = table1.id where demo.size > 2 and table1.size > 1 GROUP BY tumblingwindow(ss, 1)`,
  595. R: [][]map[string]interface{}{
  596. {{
  597. "color": "red",
  598. "name": "name1",
  599. "window_start": float64(1541152486000),
  600. "window_end": float64(1541152487000),
  601. }},
  602. },
  603. M: map[string]interface{}{
  604. "op_2_window_0_exceptions_total": int64(0),
  605. "op_2_window_0_process_latency_us": int64(0),
  606. "op_2_window_0_records_in_total": int64(5),
  607. "op_2_window_0_records_out_total": int64(4),
  608. "op_4_join_aligner_0_records_in_total": int64(5),
  609. "op_4_join_aligner_0_records_out_total": int64(4),
  610. "op_5_join_0_exceptions_total": int64(0),
  611. "op_5_join_0_records_in_total": int64(4),
  612. "op_5_join_0_records_out_total": int64(1),
  613. "op_6_project_0_exceptions_total": int64(0),
  614. "op_6_project_0_records_in_total": int64(1),
  615. "op_6_project_0_records_out_total": int64(1),
  616. "sink_mockSink_0_exceptions_total": int64(0),
  617. "sink_mockSink_0_records_in_total": int64(1),
  618. "sink_mockSink_0_records_out_total": int64(1),
  619. "source_demo_0_exceptions_total": int64(0),
  620. "source_demo_0_records_in_total": int64(5),
  621. "source_demo_0_records_out_total": int64(5),
  622. "source_table1_0_exceptions_total": int64(0),
  623. "source_table1_0_records_in_total": int64(4),
  624. "source_table1_0_records_out_total": int64(1),
  625. },
  626. },
  627. {
  628. Name: `TestWindowRule12`,
  629. Sql: `SELECT collect(size) as allSize FROM demo GROUP BY HOPPINGWINDOW(ss, 2, 1), color ORDER BY color`,
  630. R: [][]map[string]interface{}{
  631. {{
  632. "allSize": []interface{}{float64(6)},
  633. }, {
  634. "allSize": []interface{}{float64(3)},
  635. }},
  636. {{
  637. "allSize": []interface{}{float64(6), float64(2)},
  638. }, {
  639. "allSize": []interface{}{float64(3)},
  640. }},
  641. {{
  642. "allSize": []interface{}{float64(2)},
  643. }, {
  644. "allSize": []interface{}{float64(4)},
  645. }},
  646. {{
  647. "allSize": []interface{}{float64(1)},
  648. }, {
  649. "allSize": []interface{}{float64(4)},
  650. }},
  651. },
  652. M: map[string]interface{}{
  653. "sink_mockSink_0_exceptions_total": int64(0),
  654. "sink_mockSink_0_records_in_total": int64(4),
  655. "sink_mockSink_0_records_out_total": int64(4),
  656. "source_demo_0_exceptions_total": int64(0),
  657. "source_demo_0_records_in_total": int64(5),
  658. "source_demo_0_records_out_total": int64(5),
  659. "op_2_window_0_exceptions_total": int64(0),
  660. "op_2_window_0_process_latency_us": int64(0),
  661. "op_2_window_0_records_in_total": int64(5),
  662. "op_2_window_0_records_out_total": int64(4),
  663. },
  664. },
  665. {
  666. Name: `TestWindowRule13`,
  667. Sql: `SELECT color as c FROM demo GROUP BY SlidingWindow(ss, 3600,1) filter (where size = 3 )`,
  668. R: [][]map[string]interface{}{
  669. {{
  670. "c": "red",
  671. }},
  672. },
  673. M: map[string]interface{}{
  674. "op_3_project_0_exceptions_total": int64(0),
  675. "op_3_project_0_process_latency_us": int64(0),
  676. "op_3_project_0_records_in_total": int64(1),
  677. "op_3_project_0_records_out_total": int64(1),
  678. "sink_mockSink_0_exceptions_total": int64(0),
  679. "sink_mockSink_0_records_in_total": int64(1),
  680. "sink_mockSink_0_records_out_total": int64(1),
  681. "source_demo_0_exceptions_total": int64(0),
  682. "source_demo_0_records_in_total": int64(5),
  683. "source_demo_0_records_out_total": int64(5),
  684. "op_2_window_0_exceptions_total": int64(0),
  685. "op_2_window_0_process_latency_us": int64(0),
  686. "op_2_window_0_records_in_total": int64(1),
  687. "op_2_window_0_records_out_total": int64(1),
  688. },
  689. },
  690. }
  691. HandleStream(true, streamList, t)
  692. options := []*api.RuleOption{
  693. {
  694. BufferLength: 100,
  695. SendError: true,
  696. }, {
  697. BufferLength: 100,
  698. SendError: true,
  699. Qos: api.AtLeastOnce,
  700. CheckpointInterval: 5000,
  701. }, {
  702. BufferLength: 100,
  703. SendError: true,
  704. Qos: api.ExactlyOnce,
  705. CheckpointInterval: 5000,
  706. },
  707. }
  708. for j, opt := range options {
  709. DoRuleTest(t, tests, j, opt, 15)
  710. }
  711. }
  712. func TestEventWindow(t *testing.T) {
  713. // Reset
  714. streamList := []string{"demoE", "demoErr", "demo1E", "sessionDemoE"}
  715. HandleStream(false, streamList, t)
  716. tests := []RuleTest{
  717. {
  718. Name: `TestEventWindowDelayRule0`,
  719. Sql: `SELECT size FROM demoE GROUP BY SlidingWindow(ss, 1,4) FILTER (where color = "red")`,
  720. R: [][]map[string]interface{}{
  721. {
  722. {
  723. "size": float64(3),
  724. },
  725. {
  726. "size": float64(1),
  727. },
  728. },
  729. },
  730. M: map[string]interface{}{
  731. "op_2_watermark_0_records_in_total": int64(6),
  732. "op_2_watermark_0_records_out_total": int64(4),
  733. "op_2_watermark_0_exceptions_total": int64(0),
  734. "op_3_windowFilter_0_records_in_total": int64(4),
  735. "op_3_windowFilter_0_records_out_total": int64(2),
  736. "op_3_windowFilter_0_exceptions_total": int64(0),
  737. "op_3_window_0_records_in_total": int64(2),
  738. "op_3_window_0_records_out_total": int64(1),
  739. "op_3_window_0_exceptions_total": int64(0),
  740. },
  741. },
  742. {
  743. Name: `TestEventWindowRule1`,
  744. Sql: `SELECT count(*), last_agg_hit_time() as lt, last_agg_hit_count() as lc, event_time() as et FROM demoE GROUP BY HOPPINGWINDOW(ss, 2, 1) HAVING lc < 4`,
  745. R: [][]map[string]interface{}{
  746. {{
  747. "count": float64(1),
  748. "lc": float64(0),
  749. "lt": float64(0),
  750. "et": float64(1541152487000),
  751. }},
  752. {{
  753. "count": float64(2),
  754. "lc": float64(1),
  755. "lt": float64(1541152487000),
  756. "et": float64(1541152488000),
  757. }},
  758. {{
  759. "count": float64(2),
  760. "lc": float64(2),
  761. "lt": float64(1541152488000),
  762. "et": float64(1541152489000),
  763. }},
  764. {{
  765. "count": float64(2),
  766. "lc": float64(3),
  767. "lt": float64(1541152489000),
  768. "et": float64(1541152490000),
  769. }},
  770. },
  771. M: map[string]interface{}{
  772. "sink_mockSink_0_exceptions_total": int64(0),
  773. "sink_mockSink_0_records_in_total": int64(4),
  774. "sink_mockSink_0_records_out_total": int64(4),
  775. "source_demoE_0_exceptions_total": int64(0),
  776. "source_demoE_0_records_in_total": int64(6),
  777. "source_demoE_0_records_out_total": int64(6),
  778. "op_3_window_0_exceptions_total": int64(0),
  779. "op_3_window_0_process_latency_us": int64(0),
  780. "op_3_window_0_records_in_total": int64(4),
  781. "op_3_window_0_records_out_total": int64(5),
  782. "op_2_watermark_0_records_in_total": int64(6),
  783. "op_2_watermark_0_records_out_total": int64(4),
  784. },
  785. },
  786. {
  787. Name: `TestEventWindowRule2`,
  788. Sql: `SELECT window_start(), window_end(), color, ts FROM demoE where size > 2 GROUP BY tumblingwindow(ss, 1)`,
  789. R: [][]map[string]interface{}{
  790. {{
  791. "window_start": float64(1541152486013),
  792. "window_end": float64(1541152487000),
  793. "color": "red",
  794. "ts": float64(1541152486013),
  795. }},
  796. {{
  797. "window_start": float64(1541152488000),
  798. "window_end": float64(1541152489000),
  799. "color": "yellow",
  800. "ts": float64(1541152488442),
  801. }},
  802. },
  803. M: map[string]interface{}{
  804. "op_5_project_0_exceptions_total": int64(0),
  805. "op_5_project_0_process_latency_us": int64(0),
  806. "op_5_project_0_records_in_total": int64(2),
  807. "op_5_project_0_records_out_total": int64(2),
  808. "sink_mockSink_0_exceptions_total": int64(0),
  809. "sink_mockSink_0_records_in_total": int64(2),
  810. "sink_mockSink_0_records_out_total": int64(2),
  811. "source_demoE_0_exceptions_total": int64(0),
  812. "source_demoE_0_records_in_total": int64(6),
  813. "source_demoE_0_records_out_total": int64(6),
  814. "op_3_window_0_exceptions_total": int64(0),
  815. "op_3_window_0_process_latency_us": int64(0),
  816. "op_3_window_0_records_in_total": int64(4),
  817. "op_3_window_0_records_out_total": int64(5),
  818. "op_4_filter_0_exceptions_total": int64(0),
  819. "op_4_filter_0_process_latency_us": int64(0),
  820. "op_4_filter_0_records_in_total": int64(5),
  821. "op_4_filter_0_records_out_total": int64(2),
  822. },
  823. },
  824. {
  825. Name: `TestEventWindowRule3`,
  826. Sql: `SELECT color, temp, demoE.ts FROM demoE INNER JOIN demo1E ON demoE.ts = demo1E.ts GROUP BY SlidingWindow(ss, 1) OVER (WHEN demoE.ts - last_hit_time() > 400 or demo1E.ts - last_hit_time() > 400)`,
  827. R: [][]map[string]interface{}{
  828. {
  829. {
  830. "color": "red",
  831. "temp": 25.5,
  832. "ts": float64(1541152486013),
  833. },
  834. },
  835. {
  836. {
  837. "color": "red",
  838. "temp": 25.5,
  839. "ts": float64(1541152486013),
  840. },
  841. },
  842. {
  843. {
  844. "color": "blue",
  845. "temp": 28.1,
  846. "ts": float64(1541152487632),
  847. },
  848. },
  849. {
  850. {
  851. "color": "blue",
  852. "temp": 28.1,
  853. "ts": float64(1541152487632),
  854. },
  855. {
  856. "color": "yellow",
  857. "temp": 27.4,
  858. "ts": float64(1541152488442),
  859. },
  860. },
  861. {
  862. {
  863. "color": "yellow",
  864. "temp": 27.4,
  865. "ts": float64(1541152488442),
  866. },
  867. {
  868. "color": "red",
  869. "temp": 25.5,
  870. "ts": float64(1541152489252),
  871. },
  872. },
  873. },
  874. M: map[string]interface{}{
  875. "op_6_project_0_exceptions_total": int64(0),
  876. "op_6_project_0_process_latency_us": int64(0),
  877. "op_6_project_0_records_in_total": int64(5),
  878. "op_6_project_0_records_out_total": int64(5),
  879. "sink_mockSink_0_exceptions_total": int64(0),
  880. "sink_mockSink_0_records_in_total": int64(5),
  881. "sink_mockSink_0_records_out_total": int64(5),
  882. "source_demoE_0_exceptions_total": int64(0),
  883. "source_demoE_0_records_in_total": int64(6),
  884. "source_demoE_0_records_out_total": int64(6),
  885. "source_demo1E_0_exceptions_total": int64(0),
  886. "source_demo1E_0_records_in_total": int64(6),
  887. "source_demo1E_0_records_out_total": int64(6),
  888. "op_4_window_0_exceptions_total": int64(0),
  889. "op_4_window_0_process_latency_us": int64(0),
  890. "op_4_window_0_records_in_total": int64(9),
  891. "op_4_window_0_records_out_total": int64(5),
  892. "op_5_join_0_exceptions_total": int64(0),
  893. "op_5_join_0_process_latency_us": int64(0),
  894. "op_5_join_0_records_in_total": int64(5),
  895. "op_5_join_0_records_out_total": int64(5),
  896. },
  897. },
  898. {
  899. Name: `TestEventWindowRule4`,
  900. Sql: `SELECT window_start() as ws, color, window_end() as we FROM demoE GROUP BY SlidingWindow(ss, 2) OVER (WHEN ts - last_hit_time() > 1000), color ORDER BY color`,
  901. R: [][]map[string]interface{}{
  902. {{
  903. "color": "red",
  904. "ws": float64(1541152484013),
  905. "we": float64(1541152486013),
  906. }}, {{
  907. "color": "blue",
  908. "ws": float64(1541152485632),
  909. "we": float64(1541152487632),
  910. }, {
  911. "color": "red",
  912. "ws": float64(1541152485632),
  913. "we": float64(1541152487632),
  914. }}, {{
  915. "color": "blue",
  916. "ws": float64(1541152487252),
  917. "we": float64(1541152489252),
  918. }, {
  919. "color": "red",
  920. "ws": float64(1541152487252),
  921. "we": float64(1541152489252),
  922. }, {
  923. "color": "yellow",
  924. "ws": float64(1541152487252),
  925. "we": float64(1541152489252),
  926. }},
  927. },
  928. M: map[string]interface{}{
  929. "op_6_project_0_exceptions_total": int64(0),
  930. "op_6_project_0_process_latency_us": int64(0),
  931. "op_6_project_0_records_in_total": int64(3),
  932. "op_6_project_0_records_out_total": int64(3),
  933. "sink_mockSink_0_exceptions_total": int64(0),
  934. "sink_mockSink_0_records_in_total": int64(3),
  935. "sink_mockSink_0_records_out_total": int64(3),
  936. "source_demoE_0_exceptions_total": int64(0),
  937. "source_demoE_0_records_in_total": int64(6),
  938. "source_demoE_0_records_out_total": int64(6),
  939. "op_3_window_0_exceptions_total": int64(0),
  940. "op_3_window_0_process_latency_us": int64(0),
  941. "op_3_window_0_records_in_total": int64(4),
  942. "op_3_window_0_records_out_total": int64(3),
  943. "op_4_aggregate_0_exceptions_total": int64(0),
  944. "op_4_aggregate_0_process_latency_us": int64(0),
  945. "op_4_aggregate_0_records_in_total": int64(3),
  946. "op_4_aggregate_0_records_out_total": int64(3),
  947. "op_5_order_0_exceptions_total": int64(0),
  948. "op_5_order_0_process_latency_us": int64(0),
  949. "op_5_order_0_records_in_total": int64(3),
  950. "op_5_order_0_records_out_total": int64(3),
  951. },
  952. },
  953. {
  954. Name: `TestEventWindowRule5`,
  955. Sql: `SELECT temp FROM sessionDemoE GROUP BY SessionWindow(ss, 2, 1) `,
  956. R: [][]map[string]interface{}{
  957. {{
  958. "temp": 25.5,
  959. }}, {{
  960. "temp": 28.1,
  961. }, {
  962. "temp": 27.4,
  963. }, {
  964. "temp": 25.5,
  965. }}, {{
  966. "temp": 26.2,
  967. }, {
  968. "temp": 26.8,
  969. }, {
  970. "temp": 28.9,
  971. }, {
  972. "temp": 29.1,
  973. }, {
  974. "temp": 32.2,
  975. }}, {{
  976. "temp": 30.9,
  977. }},
  978. },
  979. M: map[string]interface{}{
  980. "op_4_project_0_exceptions_total": int64(0),
  981. "op_4_project_0_process_latency_us": int64(0),
  982. "op_4_project_0_records_in_total": int64(4),
  983. "op_4_project_0_records_out_total": int64(4),
  984. "sink_mockSink_0_exceptions_total": int64(0),
  985. "sink_mockSink_0_records_in_total": int64(4),
  986. "sink_mockSink_0_records_out_total": int64(4),
  987. "source_sessionDemoE_0_exceptions_total": int64(0),
  988. "source_sessionDemoE_0_records_in_total": int64(12),
  989. "source_sessionDemoE_0_records_out_total": int64(12),
  990. "op_3_window_0_exceptions_total": int64(0),
  991. "op_3_window_0_process_latency_us": int64(0),
  992. "op_3_window_0_records_in_total": int64(10),
  993. "op_3_window_0_records_out_total": int64(4),
  994. },
  995. },
  996. {
  997. Name: `TestEventWindowRule6`,
  998. Sql: `SELECT max(temp) as m, count(color) as c FROM demoE INNER JOIN demo1E ON demoE.ts = demo1E.ts GROUP BY SlidingWindow(ss, 1)`,
  999. R: [][]map[string]interface{}{
  1000. {
  1001. {
  1002. "m": 25.5,
  1003. "c": float64(1),
  1004. },
  1005. },
  1006. {
  1007. {
  1008. "m": 25.5,
  1009. "c": float64(1),
  1010. },
  1011. },
  1012. {
  1013. {
  1014. "m": 25.5,
  1015. "c": float64(1),
  1016. },
  1017. },
  1018. {
  1019. {
  1020. "m": 28.1,
  1021. "c": float64(1),
  1022. },
  1023. },
  1024. {
  1025. {
  1026. "m": 28.1,
  1027. "c": float64(1),
  1028. },
  1029. },
  1030. {
  1031. {
  1032. "m": 28.1,
  1033. "c": float64(2),
  1034. },
  1035. },
  1036. {
  1037. {
  1038. "m": 28.1,
  1039. "c": float64(2),
  1040. },
  1041. },
  1042. {
  1043. {
  1044. "m": 27.4,
  1045. "c": float64(2),
  1046. },
  1047. },
  1048. {
  1049. {
  1050. "m": 27.4,
  1051. "c": float64(2),
  1052. },
  1053. },
  1054. },
  1055. M: map[string]interface{}{
  1056. "op_6_project_0_exceptions_total": int64(0),
  1057. "op_6_project_0_process_latency_us": int64(0),
  1058. "op_6_project_0_records_in_total": int64(9),
  1059. "op_6_project_0_records_out_total": int64(9),
  1060. "sink_mockSink_0_exceptions_total": int64(0),
  1061. "sink_mockSink_0_records_in_total": int64(9),
  1062. "sink_mockSink_0_records_out_total": int64(9),
  1063. "source_demoE_0_exceptions_total": int64(0),
  1064. "source_demoE_0_records_in_total": int64(6),
  1065. "source_demoE_0_records_out_total": int64(6),
  1066. "source_demo1E_0_exceptions_total": int64(0),
  1067. "source_demo1E_0_records_in_total": int64(6),
  1068. "source_demo1E_0_records_out_total": int64(6),
  1069. "op_4_window_0_exceptions_total": int64(0),
  1070. "op_4_window_0_records_in_total": int64(9),
  1071. "op_4_window_0_records_out_total": int64(9),
  1072. "op_5_join_0_exceptions_total": int64(0),
  1073. "op_5_join_0_process_latency_us": int64(0),
  1074. "op_5_join_0_records_in_total": int64(9),
  1075. "op_5_join_0_records_out_total": int64(9),
  1076. },
  1077. },
  1078. {
  1079. Name: `TestEventWindowRule7`,
  1080. Sql: `SELECT * FROM demoErr GROUP BY HOPPINGWINDOW(ss, 2, 1)`,
  1081. R: [][]map[string]interface{}{
  1082. {{
  1083. "error": "error in preprocessor: field color type mismatch: cannot convert int(2) to string",
  1084. }},
  1085. {{
  1086. "color": "red",
  1087. "size": float64(3),
  1088. "ts": float64(1541152486013),
  1089. }},
  1090. {{
  1091. "color": "red",
  1092. "size": float64(3),
  1093. "ts": float64(1541152486013),
  1094. }},
  1095. {{
  1096. "color": "yellow",
  1097. "size": float64(4),
  1098. "ts": float64(1541152488442),
  1099. }},
  1100. {{
  1101. "color": "yellow",
  1102. "size": float64(4),
  1103. "ts": float64(1541152488442),
  1104. }, {
  1105. "color": "red",
  1106. "size": float64(1),
  1107. "ts": float64(1541152489252),
  1108. }},
  1109. {{
  1110. "color": "red",
  1111. "size": float64(1),
  1112. "ts": float64(1541152489252),
  1113. }},
  1114. },
  1115. M: map[string]interface{}{
  1116. "op_4_project_0_exceptions_total": int64(1),
  1117. "op_4_project_0_process_latency_us": int64(0),
  1118. "op_4_project_0_records_in_total": int64(5),
  1119. "op_4_project_0_records_out_total": int64(5),
  1120. "sink_mockSink_0_exceptions_total": int64(0),
  1121. "sink_mockSink_0_records_in_total": int64(6),
  1122. "sink_mockSink_0_records_out_total": int64(6),
  1123. "source_demoErr_0_exceptions_total": int64(1),
  1124. "source_demoErr_0_records_in_total": int64(6),
  1125. "source_demoErr_0_records_out_total": int64(5),
  1126. "op_3_window_0_exceptions_total": int64(1),
  1127. "op_3_window_0_process_latency_us": int64(0),
  1128. "op_3_window_0_records_in_total": int64(3),
  1129. "op_3_window_0_records_out_total": int64(5),
  1130. },
  1131. },
  1132. {
  1133. Name: `TestEventWindowRule8`,
  1134. Sql: `SELECT temp, window_start(), window_end() FROM sessionDemoE GROUP BY SessionWindow(ss, 2, 1) `,
  1135. R: [][]map[string]interface{}{
  1136. {{
  1137. "temp": 25.5,
  1138. "window_start": float64(1541152486013),
  1139. "window_end": float64(1541152487013),
  1140. }}, {{
  1141. "temp": 28.1,
  1142. "window_start": float64(1541152487932),
  1143. "window_end": float64(1541152490000),
  1144. }, {
  1145. "temp": 27.4,
  1146. "window_start": float64(1541152487932),
  1147. "window_end": float64(1541152490000),
  1148. }, {
  1149. "temp": 25.5,
  1150. "window_start": float64(1541152487932),
  1151. "window_end": float64(1541152490000),
  1152. }}, {{
  1153. "temp": 26.2,
  1154. "window_start": float64(1541152490000),
  1155. "window_end": float64(1541152494000),
  1156. }, {
  1157. "temp": 26.8,
  1158. "window_start": float64(1541152490000),
  1159. "window_end": float64(1541152494000),
  1160. }, {
  1161. "temp": 28.9,
  1162. "window_start": float64(1541152490000),
  1163. "window_end": float64(1541152494000),
  1164. }, {
  1165. "temp": 29.1,
  1166. "window_start": float64(1541152490000),
  1167. "window_end": float64(1541152494000),
  1168. }, {
  1169. "temp": 32.2,
  1170. "window_start": float64(1541152490000),
  1171. "window_end": float64(1541152494000),
  1172. }}, {{
  1173. "temp": 30.9,
  1174. "window_start": float64(1541152494000),
  1175. "window_end": float64(1541152495112),
  1176. }},
  1177. },
  1178. M: map[string]interface{}{
  1179. "op_4_project_0_exceptions_total": int64(0),
  1180. "op_4_project_0_process_latency_us": int64(0),
  1181. "op_4_project_0_records_in_total": int64(4),
  1182. "op_4_project_0_records_out_total": int64(4),
  1183. "sink_mockSink_0_exceptions_total": int64(0),
  1184. "sink_mockSink_0_records_in_total": int64(4),
  1185. "sink_mockSink_0_records_out_total": int64(4),
  1186. "source_sessionDemoE_0_exceptions_total": int64(0),
  1187. "source_sessionDemoE_0_records_in_total": int64(12),
  1188. "source_sessionDemoE_0_records_out_total": int64(12),
  1189. "op_3_window_0_exceptions_total": int64(0),
  1190. "op_3_window_0_process_latency_us": int64(0),
  1191. "op_3_window_0_records_in_total": int64(10),
  1192. "op_3_window_0_records_out_total": int64(4),
  1193. },
  1194. },
  1195. {
  1196. Name: `TestEventWindowRule9`,
  1197. Sql: `SELECT window_end(), color, window_start() FROM demoE GROUP BY HOPPINGWINDOW(ss, 2, 1)`,
  1198. R: [][]map[string]interface{}{
  1199. {{
  1200. "color": "red",
  1201. "window_start": float64(1541152485013),
  1202. "window_end": float64(1541152487000),
  1203. }},
  1204. {{
  1205. "color": "red",
  1206. "window_start": float64(1541152486000),
  1207. "window_end": float64(1541152488000),
  1208. }, {
  1209. "color": "blue",
  1210. "window_start": float64(1541152486000),
  1211. "window_end": float64(1541152488000),
  1212. }},
  1213. {{
  1214. "color": "blue",
  1215. "window_start": float64(1541152487000),
  1216. "window_end": float64(1541152489000),
  1217. }, {
  1218. "color": "yellow",
  1219. "window_start": float64(1541152487000),
  1220. "window_end": float64(1541152489000),
  1221. }},
  1222. {{
  1223. "color": "yellow",
  1224. "window_start": float64(1541152488000),
  1225. "window_end": float64(1541152490000),
  1226. }, {
  1227. "color": "red",
  1228. "window_start": float64(1541152488000),
  1229. "window_end": float64(1541152490000),
  1230. }},
  1231. {{
  1232. "color": "red",
  1233. "window_start": float64(1541152489000),
  1234. "window_end": float64(1541152491000),
  1235. }},
  1236. },
  1237. M: map[string]interface{}{
  1238. "op_4_project_0_exceptions_total": int64(0),
  1239. "op_4_project_0_process_latency_us": int64(0),
  1240. "op_4_project_0_records_in_total": int64(5),
  1241. "op_4_project_0_records_out_total": int64(5),
  1242. "sink_mockSink_0_exceptions_total": int64(0),
  1243. "sink_mockSink_0_records_in_total": int64(5),
  1244. "sink_mockSink_0_records_out_total": int64(5),
  1245. "source_demoE_0_exceptions_total": int64(0),
  1246. "source_demoE_0_records_in_total": int64(6),
  1247. "source_demoE_0_records_out_total": int64(6),
  1248. "op_3_window_0_exceptions_total": int64(0),
  1249. "op_3_window_0_process_latency_us": int64(0),
  1250. "op_3_window_0_records_in_total": int64(4),
  1251. "op_3_window_0_records_out_total": int64(5),
  1252. },
  1253. },
  1254. {
  1255. Name: `TestEventWindowCondition10`,
  1256. Sql: `SELECT color FROM demoE GROUP BY SlidingWindow(ss, 1) Over (When size = 3)`,
  1257. R: [][]map[string]interface{}{
  1258. {
  1259. {
  1260. "color": "red",
  1261. },
  1262. },
  1263. },
  1264. M: map[string]interface{}{
  1265. "op_2_watermark_0_records_in_total": int64(6),
  1266. "op_2_watermark_0_records_out_total": int64(4),
  1267. "op_2_watermark_0_exceptions_total": int64(0),
  1268. "op_3_window_0_records_in_total": int64(4),
  1269. "op_3_window_0_records_out_total": int64(1),
  1270. "op_3_window_0_exceptions_total": int64(0),
  1271. },
  1272. },
  1273. }
  1274. HandleStream(true, streamList, t)
  1275. options := []*api.RuleOption{
  1276. {
  1277. BufferLength: 100,
  1278. SendError: true,
  1279. IsEventTime: true,
  1280. LateTol: 1000,
  1281. },
  1282. {
  1283. BufferLength: 100,
  1284. SendError: true,
  1285. Qos: api.AtLeastOnce,
  1286. CheckpointInterval: 5000,
  1287. IsEventTime: true,
  1288. LateTol: 1000,
  1289. },
  1290. {
  1291. BufferLength: 100,
  1292. SendError: true,
  1293. Qos: api.ExactlyOnce,
  1294. CheckpointInterval: 5000,
  1295. IsEventTime: true,
  1296. LateTol: 1000,
  1297. },
  1298. }
  1299. for j, opt := range options {
  1300. DoRuleTest(t, tests, j, opt, 10)
  1301. }
  1302. }
  1303. func TestWindowError(t *testing.T) {
  1304. // Reset
  1305. streamList := []string{"ldemo", "ldemo1"}
  1306. HandleStream(false, streamList, t)
  1307. tests := []RuleTest{
  1308. {
  1309. Name: `TestWindowErrorRule1`,
  1310. Sql: `SELECT size * 3 FROM ldemo GROUP BY TUMBLINGWINDOW(ss, 2)`,
  1311. R: [][]map[string]interface{}{
  1312. {{
  1313. "error": "run Select error: invalid operation string(string) * int64(3)",
  1314. }}, {{
  1315. "kuiper_field_0": float64(6),
  1316. }, {}},
  1317. },
  1318. M: map[string]interface{}{
  1319. "op_3_project_0_exceptions_total": int64(1),
  1320. "op_3_project_0_process_latency_us": int64(0),
  1321. "op_3_project_0_records_in_total": int64(2),
  1322. "op_3_project_0_records_out_total": int64(1),
  1323. "sink_mockSink_0_exceptions_total": int64(0),
  1324. "sink_mockSink_0_records_in_total": int64(2),
  1325. "sink_mockSink_0_records_out_total": int64(2),
  1326. "source_ldemo_0_exceptions_total": int64(0),
  1327. "source_ldemo_0_records_in_total": int64(5),
  1328. "source_ldemo_0_records_out_total": int64(5),
  1329. "op_2_window_0_exceptions_total": int64(0),
  1330. "op_2_window_0_process_latency_us": int64(0),
  1331. "op_2_window_0_records_in_total": int64(5),
  1332. "op_2_window_0_records_out_total": int64(2),
  1333. },
  1334. }, {
  1335. Name: `TestWindowErrorRule2`,
  1336. Sql: `SELECT color, ts FROM ldemo where size > 2 GROUP BY tumblingwindow(ss, 1)`,
  1337. R: [][]map[string]interface{}{
  1338. {{
  1339. "error": "run Where error: invalid operation string(string) > int64(2)",
  1340. }}, {{
  1341. "color": "red",
  1342. "ts": float64(1541152486013),
  1343. }}, {{
  1344. "ts": float64(1541152487632),
  1345. }}, {}, {},
  1346. },
  1347. M: map[string]interface{}{
  1348. "op_4_project_0_exceptions_total": int64(1),
  1349. "op_4_project_0_process_latency_us": int64(0),
  1350. "op_4_project_0_records_in_total": int64(4),
  1351. "op_4_project_0_records_out_total": int64(4),
  1352. "sink_mockSink_0_exceptions_total": int64(0),
  1353. "sink_mockSink_0_records_in_total": int64(5),
  1354. "sink_mockSink_0_records_out_total": int64(5),
  1355. "source_ldemo_0_exceptions_total": int64(0),
  1356. "source_ldemo_0_records_in_total": int64(5),
  1357. "source_ldemo_0_records_out_total": int64(5),
  1358. "op_3_window_0_exceptions_total": int64(1),
  1359. "op_3_window_0_process_latency_us": int64(0),
  1360. "op_3_window_0_records_in_total": int64(3),
  1361. "op_3_window_0_records_out_total": int64(4),
  1362. "op_2_filter_0_exceptions_total": int64(1),
  1363. "op_2_filter_0_process_latency_us": int64(0),
  1364. "op_2_filter_0_records_in_total": int64(5),
  1365. "op_2_filter_0_records_out_total": int64(2),
  1366. },
  1367. }, {
  1368. Name: `TestWindowErrorRule3`,
  1369. Sql: `SELECT color, temp, ts FROM ldemo INNER JOIN ldemo1 ON ldemo.ts = ldemo1.ts GROUP BY SlidingWindow(ss, 1)`,
  1370. R: [][]map[string]interface{}{
  1371. {{
  1372. "color": "red",
  1373. "temp": 25.5,
  1374. "ts": float64(1541152486013),
  1375. }}, {{
  1376. "color": "red",
  1377. "temp": 25.5,
  1378. "ts": float64(1541152486013),
  1379. }}, {{
  1380. "color": "red",
  1381. "temp": 25.5,
  1382. "ts": float64(1541152486013),
  1383. }}, {{
  1384. "temp": 28.1,
  1385. "ts": float64(1541152487632),
  1386. }}, {{
  1387. "temp": 28.1,
  1388. "ts": float64(1541152487632),
  1389. }}, {{
  1390. "error": "run Join error: invalid operation int64(1541152487632) = string(1541152488442)",
  1391. }}, {{
  1392. "error": "run Join error: invalid operation int64(1541152488442) = string(1541152488442)",
  1393. }}, {{
  1394. "error": "run Join error: invalid operation int64(1541152488442) = string(1541152488442)",
  1395. }},
  1396. },
  1397. M: map[string]interface{}{
  1398. "op_5_project_0_exceptions_total": int64(3),
  1399. "op_5_project_0_process_latency_us": int64(0),
  1400. "op_5_project_0_records_in_total": int64(5),
  1401. "op_5_project_0_records_out_total": int64(5),
  1402. "sink_mockSink_0_exceptions_total": int64(0),
  1403. "sink_mockSink_0_records_in_total": int64(8),
  1404. "sink_mockSink_0_records_out_total": int64(8),
  1405. "source_ldemo_0_exceptions_total": int64(0),
  1406. "source_ldemo_0_records_in_total": int64(5),
  1407. "source_ldemo_0_records_out_total": int64(5),
  1408. "source_ldemo1_0_exceptions_total": int64(0),
  1409. "source_ldemo1_0_records_in_total": int64(5),
  1410. "source_ldemo1_0_records_out_total": int64(5),
  1411. "op_3_window_0_exceptions_total": int64(0),
  1412. "op_3_window_0_process_latency_us": int64(0),
  1413. "op_3_window_0_records_in_total": int64(10),
  1414. "op_3_window_0_records_out_total": int64(10),
  1415. "op_4_join_0_exceptions_total": int64(3),
  1416. "op_4_join_0_process_latency_us": int64(0),
  1417. "op_4_join_0_records_in_total": int64(10),
  1418. "op_4_join_0_records_out_total": int64(5),
  1419. },
  1420. }, {
  1421. Name: `TestWindowErrorRule4`,
  1422. Sql: `SELECT color FROM ldemo GROUP BY SlidingWindow(ss, 2), color having collect(size)[0] >= 2 order by color`,
  1423. R: [][]map[string]interface{}{
  1424. {{
  1425. "color": "red",
  1426. }}, {{
  1427. "error": "run Having error: invalid operation string(string) >= int64(2)",
  1428. }}, {{
  1429. "error": "run Having error: invalid operation string(string) >= int64(2)",
  1430. }}, {{
  1431. "error": "run Having error: invalid operation string(string) >= int64(2)",
  1432. }}, {{
  1433. "color": float64(49),
  1434. }, {}},
  1435. },
  1436. M: map[string]interface{}{
  1437. "op_6_project_0_exceptions_total": int64(3),
  1438. "op_6_project_0_process_latency_us": int64(0),
  1439. "op_6_project_0_records_in_total": int64(2),
  1440. "op_6_project_0_records_out_total": int64(2),
  1441. "sink_mockSink_0_exceptions_total": int64(0),
  1442. "sink_mockSink_0_records_in_total": int64(5),
  1443. "sink_mockSink_0_records_out_total": int64(5),
  1444. "source_ldemo_0_exceptions_total": int64(0),
  1445. "source_ldemo_0_records_in_total": int64(5),
  1446. "source_ldemo_0_records_out_total": int64(5),
  1447. "op_2_window_0_exceptions_total": int64(0),
  1448. "op_2_window_0_process_latency_us": int64(0),
  1449. "op_2_window_0_records_in_total": int64(5),
  1450. "op_2_window_0_records_out_total": int64(5),
  1451. "op_3_aggregate_0_exceptions_total": int64(0),
  1452. "op_3_aggregate_0_process_latency_us": int64(0),
  1453. "op_3_aggregate_0_records_in_total": int64(5),
  1454. "op_3_aggregate_0_records_out_total": int64(5),
  1455. "op_4_having_0_exceptions_total": int64(3),
  1456. "op_4_having_0_process_latency_us": int64(0),
  1457. "op_4_having_0_records_in_total": int64(5),
  1458. "op_4_having_0_records_out_total": int64(2),
  1459. },
  1460. }, {
  1461. Name: `TestWindowErrorRule5`,
  1462. Sql: `SELECT color, size FROM ldemo GROUP BY tumblingwindow(ss, 1) ORDER BY size`,
  1463. R: [][]map[string]interface{}{
  1464. {{
  1465. "error": "run Order By error: incompatible types for comparison: int and string",
  1466. }}, {{
  1467. "size": float64(3),
  1468. }}, {{
  1469. "color": float64(49),
  1470. "size": float64(2),
  1471. }}, {{
  1472. "color": "red",
  1473. }},
  1474. },
  1475. M: map[string]interface{}{
  1476. "op_4_project_0_exceptions_total": int64(1),
  1477. "op_4_project_0_process_latency_us": int64(0),
  1478. "op_4_project_0_records_in_total": int64(3),
  1479. "op_4_project_0_records_out_total": int64(3),
  1480. "sink_mockSink_0_exceptions_total": int64(0),
  1481. "sink_mockSink_0_records_in_total": int64(4),
  1482. "sink_mockSink_0_records_out_total": int64(4),
  1483. "source_ldemo_0_exceptions_total": int64(0),
  1484. "source_ldemo_0_records_in_total": int64(5),
  1485. "source_ldemo_0_records_out_total": int64(5),
  1486. "op_2_window_0_exceptions_total": int64(0),
  1487. "op_2_window_0_process_latency_us": int64(0),
  1488. "op_2_window_0_records_in_total": int64(5),
  1489. "op_2_window_0_records_out_total": int64(4),
  1490. "op_3_order_0_exceptions_total": int64(1),
  1491. "op_3_order_0_process_latency_us": int64(0),
  1492. "op_3_order_0_records_in_total": int64(4),
  1493. "op_3_order_0_records_out_total": int64(3),
  1494. },
  1495. },
  1496. }
  1497. HandleStream(true, streamList, t)
  1498. DoRuleTest(t, tests, 0, &api.RuleOption{
  1499. BufferLength: 100,
  1500. SendError: true,
  1501. }, 0)
  1502. }
  1503. func TestEventSlidingWindow(t *testing.T) {
  1504. // Reset
  1505. streamList := []string{"demoE", "demoErr", "demo1E", "sessionDemoE"}
  1506. HandleStream(false, streamList, t)
  1507. tests := []RuleTest{
  1508. {
  1509. Name: `TestEventWindowRuleDelay`,
  1510. Sql: `SELECT color FROM demoE GROUP BY SlidingWindow(ss, 1,1) FILTER (where size = 3)`,
  1511. R: [][]map[string]interface{}{
  1512. {{
  1513. "color": "red",
  1514. }},
  1515. },
  1516. M: map[string]interface{}{
  1517. "source_demoE_0_exceptions_total": int64(0),
  1518. "source_demoE_0_records_in_total": int64(6),
  1519. "source_demoE_0_records_out_total": int64(6),
  1520. },
  1521. },
  1522. }
  1523. HandleStream(true, streamList, t)
  1524. options := []*api.RuleOption{
  1525. {
  1526. BufferLength: 100,
  1527. SendError: true,
  1528. Qos: api.AtLeastOnce,
  1529. CheckpointInterval: 5000,
  1530. IsEventTime: true,
  1531. LateTol: 1000,
  1532. },
  1533. {
  1534. BufferLength: 100,
  1535. SendError: true,
  1536. Qos: api.ExactlyOnce,
  1537. CheckpointInterval: 5000,
  1538. IsEventTime: true,
  1539. LateTol: 1000,
  1540. },
  1541. {
  1542. BufferLength: 100,
  1543. SendError: true,
  1544. IsEventTime: true,
  1545. LateTol: 1000,
  1546. },
  1547. }
  1548. for j, opt := range options {
  1549. DoRuleTest(t, tests, j, opt, 10)
  1550. }
  1551. }