window_rule_test.go 53 KB

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