window_rule_test.go 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619
  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. "color": "red",
  849. "temp": 25.5,
  850. "ts": float64(1541152486013),
  851. }}, {{
  852. "color": "red",
  853. "temp": 25.5,
  854. "ts": float64(1541152486013),
  855. }}, {{
  856. "color": "blue",
  857. "temp": 28.1,
  858. "ts": float64(1541152487632),
  859. }}, {{
  860. "color": "blue",
  861. "temp": 28.1,
  862. "ts": float64(1541152487632),
  863. }, {
  864. "color": "yellow",
  865. "temp": 27.4,
  866. "ts": float64(1541152488442),
  867. }}, {{
  868. "color": "yellow",
  869. "temp": 27.4,
  870. "ts": float64(1541152488442),
  871. }, {
  872. "color": "red",
  873. "temp": 25.5,
  874. "ts": float64(1541152489252),
  875. }},
  876. },
  877. M: map[string]interface{}{
  878. "op_6_project_0_exceptions_total": int64(0),
  879. "op_6_project_0_process_latency_us": int64(0),
  880. "op_6_project_0_records_in_total": int64(5),
  881. "op_6_project_0_records_out_total": int64(5),
  882. "sink_mockSink_0_exceptions_total": int64(0),
  883. "sink_mockSink_0_records_in_total": int64(5),
  884. "sink_mockSink_0_records_out_total": int64(5),
  885. "source_demoE_0_exceptions_total": int64(0),
  886. "source_demoE_0_records_in_total": int64(6),
  887. "source_demoE_0_records_out_total": int64(6),
  888. "source_demo1E_0_exceptions_total": int64(0),
  889. "source_demo1E_0_records_in_total": int64(6),
  890. "source_demo1E_0_records_out_total": int64(6),
  891. "op_4_window_0_exceptions_total": int64(0),
  892. "op_4_window_0_process_latency_us": int64(0),
  893. "op_4_window_0_records_in_total": int64(9),
  894. "op_4_window_0_records_out_total": int64(5),
  895. "op_5_join_0_exceptions_total": int64(0),
  896. "op_5_join_0_process_latency_us": int64(0),
  897. "op_5_join_0_records_in_total": int64(5),
  898. "op_5_join_0_records_out_total": int64(5),
  899. },
  900. },
  901. {
  902. Name: `TestEventWindowRule4`,
  903. Sql: `SELECT window_start() as ws, color, window_end() as we FROM demoE GROUP BY SlidingWindow(ss, 2), color ORDER BY color`,
  904. R: [][]map[string]interface{}{
  905. {{
  906. "color": "red",
  907. "ws": float64(1541152484013),
  908. "we": float64(1541152486013),
  909. }}, {{
  910. "color": "blue",
  911. "ws": float64(1541152485632),
  912. "we": float64(1541152487632),
  913. }, {
  914. "color": "red",
  915. "ws": float64(1541152485632),
  916. "we": float64(1541152487632),
  917. }}, {{
  918. "color": "blue",
  919. "ws": float64(1541152486442),
  920. "we": float64(1541152488442),
  921. }, {
  922. "color": "yellow",
  923. "ws": float64(1541152486442),
  924. "we": float64(1541152488442),
  925. }}, {{
  926. "color": "blue",
  927. "ws": float64(1541152487252),
  928. "we": float64(1541152489252),
  929. }, {
  930. "color": "red",
  931. "ws": float64(1541152487252),
  932. "we": float64(1541152489252),
  933. }, {
  934. "color": "yellow",
  935. "ws": float64(1541152487252),
  936. "we": float64(1541152489252),
  937. }},
  938. },
  939. M: map[string]interface{}{
  940. "op_6_project_0_exceptions_total": int64(0),
  941. "op_6_project_0_process_latency_us": int64(0),
  942. "op_6_project_0_records_in_total": int64(4),
  943. "op_6_project_0_records_out_total": int64(4),
  944. "sink_mockSink_0_exceptions_total": int64(0),
  945. "sink_mockSink_0_records_in_total": int64(4),
  946. "sink_mockSink_0_records_out_total": int64(4),
  947. "source_demoE_0_exceptions_total": int64(0),
  948. "source_demoE_0_records_in_total": int64(6),
  949. "source_demoE_0_records_out_total": int64(6),
  950. "op_3_window_0_exceptions_total": int64(0),
  951. "op_3_window_0_process_latency_us": int64(0),
  952. "op_3_window_0_records_in_total": int64(4),
  953. "op_3_window_0_records_out_total": int64(4),
  954. "op_4_aggregate_0_exceptions_total": int64(0),
  955. "op_4_aggregate_0_process_latency_us": int64(0),
  956. "op_4_aggregate_0_records_in_total": int64(4),
  957. "op_4_aggregate_0_records_out_total": int64(4),
  958. "op_5_order_0_exceptions_total": int64(0),
  959. "op_5_order_0_process_latency_us": int64(0),
  960. "op_5_order_0_records_in_total": int64(4),
  961. "op_5_order_0_records_out_total": int64(4),
  962. },
  963. },
  964. {
  965. Name: `TestEventWindowRule5`,
  966. Sql: `SELECT temp FROM sessionDemoE GROUP BY SessionWindow(ss, 2, 1) `,
  967. R: [][]map[string]interface{}{
  968. {{
  969. "temp": 25.5,
  970. }}, {{
  971. "temp": 28.1,
  972. }, {
  973. "temp": 27.4,
  974. }, {
  975. "temp": 25.5,
  976. }}, {{
  977. "temp": 26.2,
  978. }, {
  979. "temp": 26.8,
  980. }, {
  981. "temp": 28.9,
  982. }, {
  983. "temp": 29.1,
  984. }, {
  985. "temp": 32.2,
  986. }}, {{
  987. "temp": 30.9,
  988. }},
  989. },
  990. M: map[string]interface{}{
  991. "op_4_project_0_exceptions_total": int64(0),
  992. "op_4_project_0_process_latency_us": int64(0),
  993. "op_4_project_0_records_in_total": int64(4),
  994. "op_4_project_0_records_out_total": int64(4),
  995. "sink_mockSink_0_exceptions_total": int64(0),
  996. "sink_mockSink_0_records_in_total": int64(4),
  997. "sink_mockSink_0_records_out_total": int64(4),
  998. "source_sessionDemoE_0_exceptions_total": int64(0),
  999. "source_sessionDemoE_0_records_in_total": int64(12),
  1000. "source_sessionDemoE_0_records_out_total": int64(12),
  1001. "op_3_window_0_exceptions_total": int64(0),
  1002. "op_3_window_0_process_latency_us": int64(0),
  1003. "op_3_window_0_records_in_total": int64(10),
  1004. "op_3_window_0_records_out_total": int64(4),
  1005. },
  1006. },
  1007. {
  1008. Name: `TestEventWindowRule6`,
  1009. 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)`,
  1010. R: [][]map[string]interface{}{
  1011. {{
  1012. "m": 25.5,
  1013. "c": float64(1),
  1014. }}, {{
  1015. "m": 25.5,
  1016. "c": float64(1),
  1017. }}, {{
  1018. "m": 28.1,
  1019. "c": float64(1),
  1020. }}, {{
  1021. "m": 28.1,
  1022. "c": float64(2),
  1023. }}, {{
  1024. "m": 27.4,
  1025. "c": float64(2),
  1026. }},
  1027. },
  1028. M: map[string]interface{}{
  1029. "op_6_project_0_exceptions_total": int64(0),
  1030. "op_6_project_0_process_latency_us": int64(0),
  1031. "op_6_project_0_records_in_total": int64(5),
  1032. "op_6_project_0_records_out_total": int64(5),
  1033. "sink_mockSink_0_exceptions_total": int64(0),
  1034. "sink_mockSink_0_records_in_total": int64(5),
  1035. "sink_mockSink_0_records_out_total": int64(5),
  1036. "source_demoE_0_exceptions_total": int64(0),
  1037. "source_demoE_0_records_in_total": int64(6),
  1038. "source_demoE_0_records_out_total": int64(6),
  1039. "source_demo1E_0_exceptions_total": int64(0),
  1040. "source_demo1E_0_records_in_total": int64(6),
  1041. "source_demo1E_0_records_out_total": int64(6),
  1042. "op_4_window_0_exceptions_total": int64(0),
  1043. "op_4_window_0_records_in_total": int64(9),
  1044. "op_4_window_0_records_out_total": int64(5),
  1045. "op_5_join_0_exceptions_total": int64(0),
  1046. "op_5_join_0_process_latency_us": int64(0),
  1047. "op_5_join_0_records_in_total": int64(5),
  1048. "op_5_join_0_records_out_total": int64(5),
  1049. },
  1050. },
  1051. {
  1052. Name: `TestEventWindowRule7`,
  1053. Sql: `SELECT * FROM demoErr GROUP BY HOPPINGWINDOW(ss, 2, 1)`,
  1054. R: [][]map[string]interface{}{
  1055. {{
  1056. "error": "error in preprocessor: field color type mismatch: cannot convert int(2) to string",
  1057. }},
  1058. {{
  1059. "color": "red",
  1060. "size": float64(3),
  1061. "ts": float64(1541152486013),
  1062. }},
  1063. {{
  1064. "color": "red",
  1065. "size": float64(3),
  1066. "ts": float64(1541152486013),
  1067. }},
  1068. {{
  1069. "color": "yellow",
  1070. "size": float64(4),
  1071. "ts": float64(1541152488442),
  1072. }},
  1073. {{
  1074. "color": "yellow",
  1075. "size": float64(4),
  1076. "ts": float64(1541152488442),
  1077. }, {
  1078. "color": "red",
  1079. "size": float64(1),
  1080. "ts": float64(1541152489252),
  1081. }},
  1082. {{
  1083. "color": "red",
  1084. "size": float64(1),
  1085. "ts": float64(1541152489252),
  1086. }},
  1087. },
  1088. M: map[string]interface{}{
  1089. "op_4_project_0_exceptions_total": int64(1),
  1090. "op_4_project_0_process_latency_us": int64(0),
  1091. "op_4_project_0_records_in_total": int64(5),
  1092. "op_4_project_0_records_out_total": int64(5),
  1093. "sink_mockSink_0_exceptions_total": int64(0),
  1094. "sink_mockSink_0_records_in_total": int64(6),
  1095. "sink_mockSink_0_records_out_total": int64(6),
  1096. "source_demoErr_0_exceptions_total": int64(1),
  1097. "source_demoErr_0_records_in_total": int64(6),
  1098. "source_demoErr_0_records_out_total": int64(5),
  1099. "op_3_window_0_exceptions_total": int64(1),
  1100. "op_3_window_0_process_latency_us": int64(0),
  1101. "op_3_window_0_records_in_total": int64(3),
  1102. "op_3_window_0_records_out_total": int64(5),
  1103. },
  1104. },
  1105. {
  1106. Name: `TestEventWindowRule8`,
  1107. Sql: `SELECT temp, window_start(), window_end() FROM sessionDemoE GROUP BY SessionWindow(ss, 2, 1) `,
  1108. R: [][]map[string]interface{}{
  1109. {{
  1110. "temp": 25.5,
  1111. "window_start": float64(1541152486013),
  1112. "window_end": float64(1541152487013),
  1113. }}, {{
  1114. "temp": 28.1,
  1115. "window_start": float64(1541152487932),
  1116. "window_end": float64(1541152490000),
  1117. }, {
  1118. "temp": 27.4,
  1119. "window_start": float64(1541152487932),
  1120. "window_end": float64(1541152490000),
  1121. }, {
  1122. "temp": 25.5,
  1123. "window_start": float64(1541152487932),
  1124. "window_end": float64(1541152490000),
  1125. }}, {{
  1126. "temp": 26.2,
  1127. "window_start": float64(1541152490000),
  1128. "window_end": float64(1541152494000),
  1129. }, {
  1130. "temp": 26.8,
  1131. "window_start": float64(1541152490000),
  1132. "window_end": float64(1541152494000),
  1133. }, {
  1134. "temp": 28.9,
  1135. "window_start": float64(1541152490000),
  1136. "window_end": float64(1541152494000),
  1137. }, {
  1138. "temp": 29.1,
  1139. "window_start": float64(1541152490000),
  1140. "window_end": float64(1541152494000),
  1141. }, {
  1142. "temp": 32.2,
  1143. "window_start": float64(1541152490000),
  1144. "window_end": float64(1541152494000),
  1145. }}, {{
  1146. "temp": 30.9,
  1147. "window_start": float64(1541152494000),
  1148. "window_end": float64(1541152495112),
  1149. }},
  1150. },
  1151. M: map[string]interface{}{
  1152. "op_4_project_0_exceptions_total": int64(0),
  1153. "op_4_project_0_process_latency_us": int64(0),
  1154. "op_4_project_0_records_in_total": int64(4),
  1155. "op_4_project_0_records_out_total": int64(4),
  1156. "sink_mockSink_0_exceptions_total": int64(0),
  1157. "sink_mockSink_0_records_in_total": int64(4),
  1158. "sink_mockSink_0_records_out_total": int64(4),
  1159. "source_sessionDemoE_0_exceptions_total": int64(0),
  1160. "source_sessionDemoE_0_records_in_total": int64(12),
  1161. "source_sessionDemoE_0_records_out_total": int64(12),
  1162. "op_3_window_0_exceptions_total": int64(0),
  1163. "op_3_window_0_process_latency_us": int64(0),
  1164. "op_3_window_0_records_in_total": int64(10),
  1165. "op_3_window_0_records_out_total": int64(4),
  1166. },
  1167. },
  1168. {
  1169. Name: `TestEventWindowRule9`,
  1170. Sql: `SELECT window_end(), color, window_start() FROM demoE GROUP BY HOPPINGWINDOW(ss, 2, 1)`,
  1171. R: [][]map[string]interface{}{
  1172. {{
  1173. "color": "red",
  1174. "window_start": float64(1541152485013),
  1175. "window_end": float64(1541152487000),
  1176. }},
  1177. {{
  1178. "color": "red",
  1179. "window_start": float64(1541152486000),
  1180. "window_end": float64(1541152488000),
  1181. }, {
  1182. "color": "blue",
  1183. "window_start": float64(1541152486000),
  1184. "window_end": float64(1541152488000),
  1185. }},
  1186. {{
  1187. "color": "blue",
  1188. "window_start": float64(1541152487000),
  1189. "window_end": float64(1541152489000),
  1190. }, {
  1191. "color": "yellow",
  1192. "window_start": float64(1541152487000),
  1193. "window_end": float64(1541152489000),
  1194. }},
  1195. {{
  1196. "color": "yellow",
  1197. "window_start": float64(1541152488000),
  1198. "window_end": float64(1541152490000),
  1199. }, {
  1200. "color": "red",
  1201. "window_start": float64(1541152488000),
  1202. "window_end": float64(1541152490000),
  1203. }},
  1204. {{
  1205. "color": "red",
  1206. "window_start": float64(1541152489000),
  1207. "window_end": float64(1541152491000),
  1208. }},
  1209. },
  1210. M: map[string]interface{}{
  1211. "op_4_project_0_exceptions_total": int64(0),
  1212. "op_4_project_0_process_latency_us": int64(0),
  1213. "op_4_project_0_records_in_total": int64(5),
  1214. "op_4_project_0_records_out_total": int64(5),
  1215. "sink_mockSink_0_exceptions_total": int64(0),
  1216. "sink_mockSink_0_records_in_total": int64(5),
  1217. "sink_mockSink_0_records_out_total": int64(5),
  1218. "source_demoE_0_exceptions_total": int64(0),
  1219. "source_demoE_0_records_in_total": int64(6),
  1220. "source_demoE_0_records_out_total": int64(6),
  1221. "op_3_window_0_exceptions_total": int64(0),
  1222. "op_3_window_0_process_latency_us": int64(0),
  1223. "op_3_window_0_records_in_total": int64(4),
  1224. "op_3_window_0_records_out_total": int64(5),
  1225. },
  1226. },
  1227. }
  1228. HandleStream(true, streamList, t)
  1229. options := []*api.RuleOption{
  1230. {
  1231. BufferLength: 100,
  1232. SendError: true,
  1233. IsEventTime: true,
  1234. LateTol: 1000,
  1235. }, {
  1236. BufferLength: 100,
  1237. SendError: true,
  1238. Qos: api.AtLeastOnce,
  1239. CheckpointInterval: 5000,
  1240. IsEventTime: true,
  1241. LateTol: 1000,
  1242. }, {
  1243. BufferLength: 100,
  1244. SendError: true,
  1245. Qos: api.ExactlyOnce,
  1246. CheckpointInterval: 5000,
  1247. IsEventTime: true,
  1248. LateTol: 1000,
  1249. },
  1250. }
  1251. for j, opt := range options {
  1252. DoRuleTest(t, tests, j, opt, 10)
  1253. }
  1254. }
  1255. func TestWindowError(t *testing.T) {
  1256. // Reset
  1257. streamList := []string{"ldemo", "ldemo1"}
  1258. HandleStream(false, streamList, t)
  1259. tests := []RuleTest{
  1260. {
  1261. Name: `TestWindowErrorRule1`,
  1262. Sql: `SELECT size * 3 FROM ldemo GROUP BY TUMBLINGWINDOW(ss, 2)`,
  1263. R: [][]map[string]interface{}{
  1264. {{
  1265. "error": "run Select error: invalid operation string(string) * int64(3)",
  1266. }}, {{
  1267. "kuiper_field_0": float64(6),
  1268. }, {}},
  1269. },
  1270. M: map[string]interface{}{
  1271. "op_3_project_0_exceptions_total": int64(1),
  1272. "op_3_project_0_process_latency_us": int64(0),
  1273. "op_3_project_0_records_in_total": int64(2),
  1274. "op_3_project_0_records_out_total": int64(1),
  1275. "sink_mockSink_0_exceptions_total": int64(0),
  1276. "sink_mockSink_0_records_in_total": int64(2),
  1277. "sink_mockSink_0_records_out_total": int64(2),
  1278. "source_ldemo_0_exceptions_total": int64(0),
  1279. "source_ldemo_0_records_in_total": int64(5),
  1280. "source_ldemo_0_records_out_total": int64(5),
  1281. "op_2_window_0_exceptions_total": int64(0),
  1282. "op_2_window_0_process_latency_us": int64(0),
  1283. "op_2_window_0_records_in_total": int64(5),
  1284. "op_2_window_0_records_out_total": int64(2),
  1285. },
  1286. }, {
  1287. Name: `TestWindowErrorRule2`,
  1288. Sql: `SELECT color, ts FROM ldemo where size > 2 GROUP BY tumblingwindow(ss, 1)`,
  1289. R: [][]map[string]interface{}{
  1290. {{
  1291. "error": "run Where error: invalid operation string(string) > int64(2)",
  1292. }}, {{
  1293. "color": "red",
  1294. "ts": float64(1541152486013),
  1295. }}, {{
  1296. "ts": float64(1541152487632),
  1297. }}, {}, {},
  1298. },
  1299. M: map[string]interface{}{
  1300. "op_4_project_0_exceptions_total": int64(1),
  1301. "op_4_project_0_process_latency_us": int64(0),
  1302. "op_4_project_0_records_in_total": int64(4),
  1303. "op_4_project_0_records_out_total": int64(4),
  1304. "sink_mockSink_0_exceptions_total": int64(0),
  1305. "sink_mockSink_0_records_in_total": int64(5),
  1306. "sink_mockSink_0_records_out_total": int64(5),
  1307. "source_ldemo_0_exceptions_total": int64(0),
  1308. "source_ldemo_0_records_in_total": int64(5),
  1309. "source_ldemo_0_records_out_total": int64(5),
  1310. "op_3_window_0_exceptions_total": int64(1),
  1311. "op_3_window_0_process_latency_us": int64(0),
  1312. "op_3_window_0_records_in_total": int64(3),
  1313. "op_3_window_0_records_out_total": int64(4),
  1314. "op_2_filter_0_exceptions_total": int64(1),
  1315. "op_2_filter_0_process_latency_us": int64(0),
  1316. "op_2_filter_0_records_in_total": int64(5),
  1317. "op_2_filter_0_records_out_total": int64(2),
  1318. },
  1319. }, {
  1320. Name: `TestWindowErrorRule3`,
  1321. Sql: `SELECT color, temp, ts FROM ldemo INNER JOIN ldemo1 ON ldemo.ts = ldemo1.ts GROUP BY SlidingWindow(ss, 1)`,
  1322. R: [][]map[string]interface{}{
  1323. {{
  1324. "color": "red",
  1325. "temp": 25.5,
  1326. "ts": float64(1541152486013),
  1327. }}, {{
  1328. "color": "red",
  1329. "temp": 25.5,
  1330. "ts": float64(1541152486013),
  1331. }}, {{
  1332. "color": "red",
  1333. "temp": 25.5,
  1334. "ts": float64(1541152486013),
  1335. }}, {{
  1336. "temp": 28.1,
  1337. "ts": float64(1541152487632),
  1338. }}, {{
  1339. "temp": 28.1,
  1340. "ts": float64(1541152487632),
  1341. }}, {{
  1342. "error": "run Join error: invalid operation int64(1541152487632) = string(1541152488442)",
  1343. }}, {{
  1344. "error": "run Join error: invalid operation int64(1541152488442) = string(1541152488442)",
  1345. }}, {{
  1346. "error": "run Join error: invalid operation int64(1541152488442) = string(1541152488442)",
  1347. }},
  1348. },
  1349. M: map[string]interface{}{
  1350. "op_5_project_0_exceptions_total": int64(3),
  1351. "op_5_project_0_process_latency_us": int64(0),
  1352. "op_5_project_0_records_in_total": int64(5),
  1353. "op_5_project_0_records_out_total": int64(5),
  1354. "sink_mockSink_0_exceptions_total": int64(0),
  1355. "sink_mockSink_0_records_in_total": int64(8),
  1356. "sink_mockSink_0_records_out_total": int64(8),
  1357. "source_ldemo_0_exceptions_total": int64(0),
  1358. "source_ldemo_0_records_in_total": int64(5),
  1359. "source_ldemo_0_records_out_total": int64(5),
  1360. "source_ldemo1_0_exceptions_total": int64(0),
  1361. "source_ldemo1_0_records_in_total": int64(5),
  1362. "source_ldemo1_0_records_out_total": int64(5),
  1363. "op_3_window_0_exceptions_total": int64(0),
  1364. "op_3_window_0_process_latency_us": int64(0),
  1365. "op_3_window_0_records_in_total": int64(10),
  1366. "op_3_window_0_records_out_total": int64(10),
  1367. "op_4_join_0_exceptions_total": int64(3),
  1368. "op_4_join_0_process_latency_us": int64(0),
  1369. "op_4_join_0_records_in_total": int64(10),
  1370. "op_4_join_0_records_out_total": int64(5),
  1371. },
  1372. }, {
  1373. Name: `TestWindowErrorRule4`,
  1374. Sql: `SELECT color FROM ldemo GROUP BY SlidingWindow(ss, 2), color having collect(size)[0] >= 2 order by color`,
  1375. R: [][]map[string]interface{}{
  1376. {{
  1377. "color": "red",
  1378. }}, {{
  1379. "error": "run Having error: invalid operation string(string) >= int64(2)",
  1380. }}, {{
  1381. "error": "run Having error: invalid operation string(string) >= int64(2)",
  1382. }}, {{
  1383. "error": "run Having error: invalid operation string(string) >= int64(2)",
  1384. }}, {{
  1385. "color": float64(49),
  1386. }, {}},
  1387. },
  1388. M: map[string]interface{}{
  1389. "op_6_project_0_exceptions_total": int64(3),
  1390. "op_6_project_0_process_latency_us": int64(0),
  1391. "op_6_project_0_records_in_total": int64(2),
  1392. "op_6_project_0_records_out_total": int64(2),
  1393. "sink_mockSink_0_exceptions_total": int64(0),
  1394. "sink_mockSink_0_records_in_total": int64(5),
  1395. "sink_mockSink_0_records_out_total": int64(5),
  1396. "source_ldemo_0_exceptions_total": int64(0),
  1397. "source_ldemo_0_records_in_total": int64(5),
  1398. "source_ldemo_0_records_out_total": int64(5),
  1399. "op_2_window_0_exceptions_total": int64(0),
  1400. "op_2_window_0_process_latency_us": int64(0),
  1401. "op_2_window_0_records_in_total": int64(5),
  1402. "op_2_window_0_records_out_total": int64(5),
  1403. "op_3_aggregate_0_exceptions_total": int64(0),
  1404. "op_3_aggregate_0_process_latency_us": int64(0),
  1405. "op_3_aggregate_0_records_in_total": int64(5),
  1406. "op_3_aggregate_0_records_out_total": int64(5),
  1407. "op_4_having_0_exceptions_total": int64(3),
  1408. "op_4_having_0_process_latency_us": int64(0),
  1409. "op_4_having_0_records_in_total": int64(5),
  1410. "op_4_having_0_records_out_total": int64(2),
  1411. },
  1412. }, {
  1413. Name: `TestWindowErrorRule5`,
  1414. Sql: `SELECT color, size FROM ldemo GROUP BY tumblingwindow(ss, 1) ORDER BY size`,
  1415. R: [][]map[string]interface{}{
  1416. {{
  1417. "error": "run Order By error: incompatible types for comparison: int and string",
  1418. }}, {{
  1419. "size": float64(3),
  1420. }}, {{
  1421. "color": float64(49),
  1422. "size": float64(2),
  1423. }}, {{
  1424. "color": "red",
  1425. }},
  1426. },
  1427. M: map[string]interface{}{
  1428. "op_4_project_0_exceptions_total": int64(1),
  1429. "op_4_project_0_process_latency_us": int64(0),
  1430. "op_4_project_0_records_in_total": int64(3),
  1431. "op_4_project_0_records_out_total": int64(3),
  1432. "sink_mockSink_0_exceptions_total": int64(0),
  1433. "sink_mockSink_0_records_in_total": int64(4),
  1434. "sink_mockSink_0_records_out_total": int64(4),
  1435. "source_ldemo_0_exceptions_total": int64(0),
  1436. "source_ldemo_0_records_in_total": int64(5),
  1437. "source_ldemo_0_records_out_total": int64(5),
  1438. "op_2_window_0_exceptions_total": int64(0),
  1439. "op_2_window_0_process_latency_us": int64(0),
  1440. "op_2_window_0_records_in_total": int64(5),
  1441. "op_2_window_0_records_out_total": int64(4),
  1442. "op_3_order_0_exceptions_total": int64(1),
  1443. "op_3_order_0_process_latency_us": int64(0),
  1444. "op_3_order_0_records_in_total": int64(4),
  1445. "op_3_order_0_records_out_total": int64(3),
  1446. },
  1447. },
  1448. }
  1449. HandleStream(true, streamList, t)
  1450. DoRuleTest(t, tests, 0, &api.RuleOption{
  1451. BufferLength: 100,
  1452. SendError: true,
  1453. }, 0)
  1454. }
  1455. func TestEventSlidingWindow(t *testing.T) {
  1456. // Reset
  1457. streamList := []string{"demoE", "demoErr", "demo1E", "sessionDemoE"}
  1458. HandleStream(false, streamList, t)
  1459. tests := []RuleTest{
  1460. {
  1461. Name: `TestEventWindowRuleDelay`,
  1462. Sql: `SELECT color FROM demoE GROUP BY SlidingWindow(ss, 1,1) FILTER (where size = 3)`,
  1463. R: [][]map[string]interface{}{
  1464. {{
  1465. "color": "red",
  1466. }},
  1467. },
  1468. M: map[string]interface{}{
  1469. "source_demoE_0_exceptions_total": int64(0),
  1470. "source_demoE_0_records_in_total": int64(6),
  1471. "source_demoE_0_records_out_total": int64(6),
  1472. },
  1473. },
  1474. }
  1475. HandleStream(true, streamList, t)
  1476. options := []*api.RuleOption{
  1477. {
  1478. BufferLength: 100,
  1479. SendError: true,
  1480. Qos: api.AtLeastOnce,
  1481. CheckpointInterval: 5000,
  1482. IsEventTime: true,
  1483. LateTol: 1000,
  1484. },
  1485. {
  1486. BufferLength: 100,
  1487. SendError: true,
  1488. Qos: api.ExactlyOnce,
  1489. CheckpointInterval: 5000,
  1490. IsEventTime: true,
  1491. LateTol: 1000,
  1492. },
  1493. {
  1494. BufferLength: 100,
  1495. SendError: true,
  1496. IsEventTime: true,
  1497. LateTol: 1000,
  1498. },
  1499. }
  1500. for j, opt := range options {
  1501. DoRuleTest(t, tests, j, opt, 10)
  1502. }
  1503. }