window_rule_test.go 52 KB

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