xsql_processor_test.go 88 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288
  1. package processors
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/emqx/kuiper/common"
  6. "github.com/emqx/kuiper/xsql"
  7. "github.com/emqx/kuiper/xstream"
  8. "github.com/emqx/kuiper/xstream/api"
  9. "github.com/emqx/kuiper/xstream/nodes"
  10. "github.com/emqx/kuiper/xstream/test"
  11. "path"
  12. "reflect"
  13. "strings"
  14. "testing"
  15. "time"
  16. )
  17. var DbDir = getDbDir()
  18. func getDbDir() string {
  19. dbDir, err := common.GetAndCreateDataLoc("test")
  20. if err != nil {
  21. log.Panic(err)
  22. }
  23. log.Infof("db location is %s", dbDir)
  24. return dbDir
  25. }
  26. func TestStreamCreateProcessor(t *testing.T) {
  27. var tests = []struct {
  28. s string
  29. r []string
  30. err string
  31. }{
  32. {
  33. s: `SHOW STREAMS;`,
  34. r: []string{"No stream definitions are found."},
  35. },
  36. {
  37. s: `EXPLAIN STREAM topic1;`,
  38. err: "Stream topic1 is not found.",
  39. },
  40. {
  41. s: `CREATE STREAM topic1 (
  42. USERID BIGINT,
  43. FIRST_NAME STRING,
  44. LAST_NAME STRING,
  45. NICKNAMES ARRAY(STRING),
  46. Gender BOOLEAN,
  47. ADDRESS STRUCT(STREET_NAME STRING, NUMBER BIGINT),
  48. ) WITH (DATASOURCE="users", FORMAT="AVRO", KEY="USERID");`,
  49. r: []string{"Stream topic1 is created."},
  50. },
  51. {
  52. s: `CREATE STREAM topic1 (
  53. USERID BIGINT,
  54. ) WITH (DATASOURCE="users", FORMAT="AVRO", KEY="USERID");`,
  55. err: "Create stream fails: Item topic1 already exists.",
  56. },
  57. {
  58. s: `EXPLAIN STREAM topic1;`,
  59. r: []string{"TO BE SUPPORTED"},
  60. },
  61. {
  62. s: `DESCRIBE STREAM topic1;`,
  63. r: []string{"Fields\n--------------------------------------------------------------------------------\nUSERID\tbigint\nFIRST_NAME\tstring\nLAST_NAME\tstring\nNICKNAMES\t" +
  64. "array(string)\nGender\tboolean\nADDRESS\tstruct(STREET_NAME string, NUMBER bigint)\n\n" +
  65. "DATASOURCE: users\nFORMAT: AVRO\nKEY: USERID\n"},
  66. },
  67. {
  68. s: `SHOW STREAMS;`,
  69. r: []string{"topic1"},
  70. },
  71. {
  72. s: `DROP STREAM topic1;`,
  73. r: []string{"Stream topic1 is dropped."},
  74. },
  75. {
  76. s: `DESCRIBE STREAM topic1;`,
  77. err: "Stream topic1 is not found.",
  78. },
  79. {
  80. s: `DROP STREAM topic1;`,
  81. err: "Drop stream fails: topic1 is not found.",
  82. },
  83. }
  84. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  85. streamDB := path.Join(getDbDir(), "streamTest")
  86. for i, tt := range tests {
  87. results, err := NewStreamProcessor(streamDB).ExecStmt(tt.s)
  88. if !reflect.DeepEqual(tt.err, errstring(err)) {
  89. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.s, tt.err, err)
  90. } else if tt.err == "" {
  91. if !reflect.DeepEqual(tt.r, results) {
  92. t.Errorf("%d. %q\n\nstmt mismatch:\nexp=%s\ngot=%#v\n\n", i, tt.s, tt.r, results)
  93. }
  94. }
  95. }
  96. }
  97. func createStreams(t *testing.T) {
  98. p := NewStreamProcessor(path.Join(DbDir, "stream"))
  99. demo := `CREATE STREAM demo (
  100. color STRING,
  101. size BIGINT,
  102. ts BIGINT
  103. ) WITH (DATASOURCE="demo", FORMAT="json", KEY="ts");`
  104. _, err := p.ExecStmt(demo)
  105. if err != nil {
  106. t.Log(err)
  107. }
  108. demoE := `CREATE STREAM demoE (
  109. color STRING,
  110. size BIGINT,
  111. ts BIGINT
  112. ) WITH (DATASOURCE="demoE", FORMAT="json", KEY="ts");`
  113. _, err = p.ExecStmt(demoE)
  114. if err != nil {
  115. t.Log(err)
  116. }
  117. demo1 := `CREATE STREAM demo1 (
  118. temp FLOAT,
  119. hum BIGINT,
  120. ts BIGINT
  121. ) WITH (DATASOURCE="demo1", FORMAT="json", KEY="ts");`
  122. _, err = p.ExecStmt(demo1)
  123. if err != nil {
  124. t.Log(err)
  125. }
  126. sessionDemo := `CREATE STREAM sessionDemo (
  127. temp FLOAT,
  128. hum BIGINT,
  129. ts BIGINT
  130. ) WITH (DATASOURCE="sessionDemo", FORMAT="json", KEY="ts");`
  131. _, err = p.ExecStmt(sessionDemo)
  132. if err != nil {
  133. t.Log(err)
  134. }
  135. }
  136. func dropStreams(t *testing.T) {
  137. p := NewStreamProcessor(path.Join(DbDir, "stream"))
  138. demo := `DROP STREAM demo`
  139. _, err := p.ExecStmt(demo)
  140. if err != nil {
  141. t.Log(err)
  142. }
  143. demoE := `DROP STREAM demoE`
  144. _, err = p.ExecStmt(demoE)
  145. if err != nil {
  146. t.Log(err)
  147. }
  148. demo1 := `DROP STREAM demo1`
  149. _, err = p.ExecStmt(demo1)
  150. if err != nil {
  151. t.Log(err)
  152. }
  153. sessionDemo := `DROP STREAM sessionDemo`
  154. _, err = p.ExecStmt(sessionDemo)
  155. if err != nil {
  156. t.Log(err)
  157. }
  158. }
  159. func createSchemalessStreams(t *testing.T) {
  160. p := NewStreamProcessor(path.Join(DbDir, "stream"))
  161. demo := `CREATE STREAM ldemo (
  162. ) WITH (DATASOURCE="ldemo", FORMAT="json");`
  163. _, err := p.ExecStmt(demo)
  164. if err != nil {
  165. t.Log(err)
  166. }
  167. demo1 := `CREATE STREAM ldemo1 (
  168. ) WITH (DATASOURCE="ldemo1", FORMAT="json");`
  169. _, err = p.ExecStmt(demo1)
  170. if err != nil {
  171. t.Log(err)
  172. }
  173. sessionDemo := `CREATE STREAM lsessionDemo (
  174. ) WITH (DATASOURCE="lsessionDemo", FORMAT="json");`
  175. _, err = p.ExecStmt(sessionDemo)
  176. if err != nil {
  177. t.Log(err)
  178. }
  179. }
  180. func dropSchemalessStreams(t *testing.T) {
  181. p := NewStreamProcessor(path.Join(DbDir, "stream"))
  182. demo := `DROP STREAM ldemo`
  183. _, err := p.ExecStmt(demo)
  184. if err != nil {
  185. t.Log(err)
  186. }
  187. demo1 := `DROP STREAM ldemo1`
  188. _, err = p.ExecStmt(demo1)
  189. if err != nil {
  190. t.Log(err)
  191. }
  192. sessionDemo := `DROP STREAM lsessionDemo`
  193. _, err = p.ExecStmt(sessionDemo)
  194. if err != nil {
  195. t.Log(err)
  196. }
  197. }
  198. func getMockSource(name string, done <-chan int, size int) *nodes.SourceNode {
  199. var data []*xsql.Tuple
  200. switch name {
  201. case "demo":
  202. data = []*xsql.Tuple{
  203. {
  204. Emitter: name,
  205. Message: map[string]interface{}{
  206. "color": "red",
  207. "size": 3,
  208. "ts": 1541152486013,
  209. },
  210. Timestamp: 1541152486013,
  211. },
  212. {
  213. Emitter: name,
  214. Message: map[string]interface{}{
  215. "color": "blue",
  216. "size": 6,
  217. "ts": 1541152486822,
  218. },
  219. Timestamp: 1541152486822,
  220. },
  221. {
  222. Emitter: name,
  223. Message: map[string]interface{}{
  224. "color": "blue",
  225. "size": 2,
  226. "ts": 1541152487632,
  227. },
  228. Timestamp: 1541152487632,
  229. },
  230. {
  231. Emitter: name,
  232. Message: map[string]interface{}{
  233. "color": "yellow",
  234. "size": 4,
  235. "ts": 1541152488442,
  236. },
  237. Timestamp: 1541152488442,
  238. },
  239. {
  240. Emitter: name,
  241. Message: map[string]interface{}{
  242. "color": "red",
  243. "size": 1,
  244. "ts": 1541152489252,
  245. },
  246. Timestamp: 1541152489252,
  247. },
  248. }
  249. case "demoE":
  250. data = []*xsql.Tuple{
  251. {
  252. Emitter: name,
  253. Message: map[string]interface{}{
  254. "color": 3,
  255. "size": "red",
  256. "ts": 1541152486013,
  257. },
  258. Timestamp: 1541152486013,
  259. },
  260. {
  261. Emitter: name,
  262. Message: map[string]interface{}{
  263. "color": "blue",
  264. "size": 6,
  265. "ts": "1541152486822",
  266. },
  267. Timestamp: 1541152486822,
  268. },
  269. {
  270. Emitter: name,
  271. Message: map[string]interface{}{
  272. "color": "blue",
  273. "size": 2,
  274. "ts": 1541152487632,
  275. },
  276. Timestamp: 1541152487632,
  277. },
  278. {
  279. Emitter: name,
  280. Message: map[string]interface{}{
  281. "color": 7,
  282. "size": 4,
  283. "ts": 1541152488442,
  284. },
  285. Timestamp: 1541152488442,
  286. },
  287. {
  288. Emitter: name,
  289. Message: map[string]interface{}{
  290. "color": "red",
  291. "size": "blue",
  292. "ts": 1541152489252,
  293. },
  294. Timestamp: 1541152489252,
  295. },
  296. }
  297. case "demo1":
  298. data = []*xsql.Tuple{
  299. {
  300. Emitter: name,
  301. Message: map[string]interface{}{
  302. "temp": 25.5,
  303. "hum": 65,
  304. "ts": 1541152486013,
  305. },
  306. Timestamp: 1541152486013,
  307. },
  308. {
  309. Emitter: name,
  310. Message: map[string]interface{}{
  311. "temp": 27.5,
  312. "hum": 59,
  313. "ts": 1541152486823,
  314. },
  315. Timestamp: 1541152486823,
  316. },
  317. {
  318. Emitter: name,
  319. Message: map[string]interface{}{
  320. "temp": 28.1,
  321. "hum": 75,
  322. "ts": 1541152487632,
  323. },
  324. Timestamp: 1541152487632,
  325. },
  326. {
  327. Emitter: name,
  328. Message: map[string]interface{}{
  329. "temp": 27.4,
  330. "hum": 80,
  331. "ts": 1541152488442,
  332. },
  333. Timestamp: 1541152488442,
  334. },
  335. {
  336. Emitter: name,
  337. Message: map[string]interface{}{
  338. "temp": 25.5,
  339. "hum": 62,
  340. "ts": 1541152489252,
  341. },
  342. Timestamp: 1541152489252,
  343. },
  344. }
  345. case "sessionDemo":
  346. data = []*xsql.Tuple{
  347. {
  348. Emitter: name,
  349. Message: map[string]interface{}{
  350. "temp": 25.5,
  351. "hum": 65,
  352. "ts": 1541152486013,
  353. },
  354. Timestamp: 1541152486013,
  355. },
  356. {
  357. Emitter: name,
  358. Message: map[string]interface{}{
  359. "temp": 27.5,
  360. "hum": 59,
  361. "ts": 1541152486823,
  362. },
  363. Timestamp: 1541152486823,
  364. },
  365. {
  366. Emitter: name,
  367. Message: map[string]interface{}{
  368. "temp": 28.1,
  369. "hum": 75,
  370. "ts": 1541152487932,
  371. },
  372. Timestamp: 1541152487932,
  373. },
  374. {
  375. Emitter: name,
  376. Message: map[string]interface{}{
  377. "temp": 27.4,
  378. "hum": 80,
  379. "ts": 1541152488442,
  380. },
  381. Timestamp: 1541152488442,
  382. },
  383. {
  384. Emitter: name,
  385. Message: map[string]interface{}{
  386. "temp": 25.5,
  387. "hum": 62,
  388. "ts": 1541152489252,
  389. },
  390. Timestamp: 1541152489252,
  391. },
  392. {
  393. Emitter: name,
  394. Message: map[string]interface{}{
  395. "temp": 26.2,
  396. "hum": 63,
  397. "ts": 1541152490062,
  398. },
  399. Timestamp: 1541152490062,
  400. },
  401. {
  402. Emitter: name,
  403. Message: map[string]interface{}{
  404. "temp": 26.8,
  405. "hum": 71,
  406. "ts": 1541152490872,
  407. },
  408. Timestamp: 1541152490872,
  409. },
  410. {
  411. Emitter: name,
  412. Message: map[string]interface{}{
  413. "temp": 28.9,
  414. "hum": 85,
  415. "ts": 1541152491682,
  416. },
  417. Timestamp: 1541152491682,
  418. },
  419. {
  420. Emitter: name,
  421. Message: map[string]interface{}{
  422. "temp": 29.1,
  423. "hum": 92,
  424. "ts": 1541152492492,
  425. },
  426. Timestamp: 1541152492492,
  427. },
  428. {
  429. Emitter: name,
  430. Message: map[string]interface{}{
  431. "temp": 32.2,
  432. "hum": 99,
  433. "ts": 1541152493202,
  434. },
  435. Timestamp: 1541152493202,
  436. },
  437. {
  438. Emitter: name,
  439. Message: map[string]interface{}{
  440. "temp": 30.9,
  441. "hum": 87,
  442. "ts": 1541152494112,
  443. },
  444. Timestamp: 1541152494112,
  445. },
  446. }
  447. }
  448. return nodes.NewSourceNodeWithSource(name, test.NewMockSource(data[:size], done, false), map[string]string{
  449. "DATASOURCE": name,
  450. })
  451. }
  452. func TestSingleSQL(t *testing.T) {
  453. var tests = []struct {
  454. name string
  455. sql string
  456. r [][]map[string]interface{}
  457. s string
  458. m map[string]interface{}
  459. }{
  460. {
  461. name: `rule1`,
  462. sql: `SELECT * FROM demo`,
  463. r: [][]map[string]interface{}{
  464. {{
  465. "color": "red",
  466. "size": float64(3),
  467. "ts": float64(1541152486013),
  468. }},
  469. {{
  470. "color": "blue",
  471. "size": float64(6),
  472. "ts": float64(1541152486822),
  473. }},
  474. {{
  475. "color": "blue",
  476. "size": float64(2),
  477. "ts": float64(1541152487632),
  478. }},
  479. {{
  480. "color": "yellow",
  481. "size": float64(4),
  482. "ts": float64(1541152488442),
  483. }},
  484. {{
  485. "color": "red",
  486. "size": float64(1),
  487. "ts": float64(1541152489252),
  488. }},
  489. },
  490. m: map[string]interface{}{
  491. "op_preprocessor_demo_0_exceptions_total": int64(0),
  492. "op_preprocessor_demo_0_process_latency_ms": int64(0),
  493. "op_preprocessor_demo_0_records_in_total": int64(5),
  494. "op_preprocessor_demo_0_records_out_total": int64(5),
  495. "op_project_0_exceptions_total": int64(0),
  496. "op_project_0_process_latency_ms": int64(0),
  497. "op_project_0_records_in_total": int64(5),
  498. "op_project_0_records_out_total": int64(5),
  499. "sink_mockSink_0_exceptions_total": int64(0),
  500. "sink_mockSink_0_records_in_total": int64(5),
  501. "sink_mockSink_0_records_out_total": int64(5),
  502. "source_demo_0_exceptions_total": int64(0),
  503. "source_demo_0_records_in_total": int64(5),
  504. "source_demo_0_records_out_total": int64(5),
  505. },
  506. s: "sink_mockSink_0_records_out_total",
  507. }, {
  508. name: `rule2`,
  509. sql: `SELECT color, ts FROM demo where size > 3`,
  510. r: [][]map[string]interface{}{
  511. {{
  512. "color": "blue",
  513. "ts": float64(1541152486822),
  514. }},
  515. {{
  516. "color": "yellow",
  517. "ts": float64(1541152488442),
  518. }},
  519. },
  520. s: "op_filter_0_records_in_total",
  521. m: map[string]interface{}{
  522. "op_preprocessor_demo_0_exceptions_total": int64(0),
  523. "op_preprocessor_demo_0_process_latency_ms": int64(0),
  524. "op_preprocessor_demo_0_records_in_total": int64(5),
  525. "op_preprocessor_demo_0_records_out_total": int64(5),
  526. "op_project_0_exceptions_total": int64(0),
  527. "op_project_0_process_latency_ms": int64(0),
  528. "op_project_0_records_in_total": int64(2),
  529. "op_project_0_records_out_total": int64(2),
  530. "sink_mockSink_0_exceptions_total": int64(0),
  531. "sink_mockSink_0_records_in_total": int64(2),
  532. "sink_mockSink_0_records_out_total": int64(2),
  533. "source_demo_0_exceptions_total": int64(0),
  534. "source_demo_0_records_in_total": int64(5),
  535. "source_demo_0_records_out_total": int64(5),
  536. "op_filter_0_exceptions_total": int64(0),
  537. "op_filter_0_process_latency_ms": int64(0),
  538. "op_filter_0_records_in_total": int64(5),
  539. "op_filter_0_records_out_total": int64(2),
  540. },
  541. }, {
  542. name: `rule3`,
  543. sql: `SELECT size as Int8, ts FROM demo where size > 3`,
  544. r: [][]map[string]interface{}{
  545. {{
  546. "Int8": float64(6),
  547. "ts": float64(1541152486822),
  548. }},
  549. {{
  550. "Int8": float64(4),
  551. "ts": float64(1541152488442),
  552. }},
  553. },
  554. s: "op_filter_0_records_in_total",
  555. m: map[string]interface{}{
  556. "op_preprocessor_demo_0_exceptions_total": int64(0),
  557. "op_preprocessor_demo_0_process_latency_ms": int64(0),
  558. "op_preprocessor_demo_0_records_in_total": int64(5),
  559. "op_preprocessor_demo_0_records_out_total": int64(5),
  560. "op_project_0_exceptions_total": int64(0),
  561. "op_project_0_process_latency_ms": int64(0),
  562. "op_project_0_records_in_total": int64(2),
  563. "op_project_0_records_out_total": int64(2),
  564. "sink_mockSink_0_exceptions_total": int64(0),
  565. "sink_mockSink_0_records_in_total": int64(2),
  566. "sink_mockSink_0_records_out_total": int64(2),
  567. "source_demo_0_exceptions_total": int64(0),
  568. "source_demo_0_records_in_total": int64(5),
  569. "source_demo_0_records_out_total": int64(5),
  570. "op_filter_0_exceptions_total": int64(0),
  571. "op_filter_0_process_latency_ms": int64(0),
  572. "op_filter_0_records_in_total": int64(5),
  573. "op_filter_0_records_out_total": int64(2),
  574. },
  575. }, {
  576. name: `rule4`,
  577. sql: `SELECT size as Int8, ts FROM demoE where size > 3`,
  578. r: [][]map[string]interface{}{
  579. {{
  580. "error": "error in preprocessor: invalid data type for color, expect string but found int(3)",
  581. }},
  582. {{
  583. "Int8": float64(6),
  584. "ts": float64(1541152486822),
  585. }},
  586. {{
  587. "error": "error in preprocessor: invalid data type for color, expect string but found int(7)",
  588. }},
  589. {{
  590. "error": "error in preprocessor: invalid data type for size, expect bigint but found string(blue)",
  591. }},
  592. },
  593. s: "op_filter_0_records_in_total",
  594. m: map[string]interface{}{
  595. "op_preprocessor_demoE_0_exceptions_total": int64(3),
  596. "op_preprocessor_demoE_0_process_latency_ms": int64(0),
  597. "op_preprocessor_demoE_0_records_in_total": int64(5),
  598. "op_preprocessor_demoE_0_records_out_total": int64(2),
  599. "op_project_0_exceptions_total": int64(3),
  600. "op_project_0_process_latency_ms": int64(0),
  601. "op_project_0_records_in_total": int64(4),
  602. "op_project_0_records_out_total": int64(1),
  603. "sink_mockSink_0_exceptions_total": int64(0),
  604. "sink_mockSink_0_records_in_total": int64(4),
  605. "sink_mockSink_0_records_out_total": int64(4),
  606. "source_demoE_0_exceptions_total": int64(0),
  607. "source_demoE_0_records_in_total": int64(5),
  608. "source_demoE_0_records_out_total": int64(5),
  609. "op_filter_0_exceptions_total": int64(3),
  610. "op_filter_0_process_latency_ms": int64(0),
  611. "op_filter_0_records_in_total": int64(5),
  612. "op_filter_0_records_out_total": int64(1),
  613. },
  614. }, {
  615. name: `rule4`,
  616. sql: `SELECT size as Int8, ts FROM demoE where size > 3`,
  617. r: [][]map[string]interface{}{
  618. {{
  619. "error": "error in preprocessor: invalid data type for color, expect string but found int(3)",
  620. }},
  621. {{
  622. "Int8": float64(6),
  623. "ts": float64(1541152486822),
  624. }},
  625. {{
  626. "error": "error in preprocessor: invalid data type for color, expect string but found int(7)",
  627. }},
  628. {{
  629. "error": "error in preprocessor: invalid data type for size, expect bigint but found string(blue)",
  630. }},
  631. },
  632. s: "op_filter_0_records_in_total",
  633. m: map[string]interface{}{
  634. "op_preprocessor_demoE_0_exceptions_total": int64(3),
  635. "op_preprocessor_demoE_0_process_latency_ms": int64(0),
  636. "op_preprocessor_demoE_0_records_in_total": int64(5),
  637. "op_preprocessor_demoE_0_records_out_total": int64(2),
  638. "op_project_0_exceptions_total": int64(3),
  639. "op_project_0_process_latency_ms": int64(0),
  640. "op_project_0_records_in_total": int64(4),
  641. "op_project_0_records_out_total": int64(1),
  642. "sink_mockSink_0_exceptions_total": int64(0),
  643. "sink_mockSink_0_records_in_total": int64(4),
  644. "sink_mockSink_0_records_out_total": int64(4),
  645. "source_demoE_0_exceptions_total": int64(0),
  646. "source_demoE_0_records_in_total": int64(5),
  647. "source_demoE_0_records_out_total": int64(5),
  648. "op_filter_0_exceptions_total": int64(3),
  649. "op_filter_0_process_latency_ms": int64(0),
  650. "op_filter_0_records_in_total": int64(5),
  651. "op_filter_0_records_out_total": int64(1),
  652. },
  653. }, {
  654. name: `rule5`,
  655. sql: `SELECT meta(topic) as m, ts FROM demo`,
  656. r: [][]map[string]interface{}{
  657. {{
  658. "m": "mock",
  659. "ts": float64(1541152486013),
  660. }},
  661. {{
  662. "m": "mock",
  663. "ts": float64(1541152486822),
  664. }},
  665. {{
  666. "m": "mock",
  667. "ts": float64(1541152487632),
  668. }},
  669. {{
  670. "m": "mock",
  671. "ts": float64(1541152488442),
  672. }},
  673. {{
  674. "m": "mock",
  675. "ts": float64(1541152489252),
  676. }},
  677. },
  678. m: map[string]interface{}{
  679. "op_preprocessor_demo_0_exceptions_total": int64(0),
  680. "op_preprocessor_demo_0_process_latency_ms": int64(0),
  681. "op_preprocessor_demo_0_records_in_total": int64(5),
  682. "op_preprocessor_demo_0_records_out_total": int64(5),
  683. "op_project_0_exceptions_total": int64(0),
  684. "op_project_0_process_latency_ms": int64(0),
  685. "op_project_0_records_in_total": int64(5),
  686. "op_project_0_records_out_total": int64(5),
  687. "sink_mockSink_0_exceptions_total": int64(0),
  688. "sink_mockSink_0_records_in_total": int64(5),
  689. "sink_mockSink_0_records_out_total": int64(5),
  690. "source_demo_0_exceptions_total": int64(0),
  691. "source_demo_0_records_in_total": int64(5),
  692. "source_demo_0_records_out_total": int64(5),
  693. },
  694. s: "sink_mockSink_0_records_out_total",
  695. }, {
  696. name: `rule6`,
  697. sql: `SELECT color, ts FROM demo where size > 3 and meta(topic)="mock"`,
  698. r: [][]map[string]interface{}{
  699. {{
  700. "color": "blue",
  701. "ts": float64(1541152486822),
  702. }},
  703. {{
  704. "color": "yellow",
  705. "ts": float64(1541152488442),
  706. }},
  707. },
  708. s: "op_filter_0_records_in_total",
  709. m: map[string]interface{}{
  710. "op_preprocessor_demo_0_exceptions_total": int64(0),
  711. "op_preprocessor_demo_0_process_latency_ms": int64(0),
  712. "op_preprocessor_demo_0_records_in_total": int64(5),
  713. "op_preprocessor_demo_0_records_out_total": int64(5),
  714. "op_project_0_exceptions_total": int64(0),
  715. "op_project_0_process_latency_ms": int64(0),
  716. "op_project_0_records_in_total": int64(2),
  717. "op_project_0_records_out_total": int64(2),
  718. "sink_mockSink_0_exceptions_total": int64(0),
  719. "sink_mockSink_0_records_in_total": int64(2),
  720. "sink_mockSink_0_records_out_total": int64(2),
  721. "source_demo_0_exceptions_total": int64(0),
  722. "source_demo_0_records_in_total": int64(5),
  723. "source_demo_0_records_out_total": int64(5),
  724. "op_filter_0_exceptions_total": int64(0),
  725. "op_filter_0_process_latency_ms": int64(0),
  726. "op_filter_0_records_in_total": int64(5),
  727. "op_filter_0_records_out_total": int64(2),
  728. },
  729. },
  730. }
  731. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  732. createStreams(t)
  733. defer dropStreams(t)
  734. //defer close(done)
  735. for i, tt := range tests {
  736. test.ResetClock(1541152486000)
  737. p := NewRuleProcessor(DbDir)
  738. parser := xsql.NewParser(strings.NewReader(tt.sql))
  739. var (
  740. sources []*nodes.SourceNode
  741. syncs []chan int
  742. )
  743. if stmt, err := xsql.Language.Parse(parser); err != nil {
  744. t.Errorf("parse sql %s error: %s", tt.sql, err)
  745. } else {
  746. if selectStmt, ok := stmt.(*xsql.SelectStatement); !ok {
  747. t.Errorf("sql %s is not a select statement", tt.sql)
  748. } else {
  749. streams := xsql.GetStreams(selectStmt)
  750. for _, stream := range streams {
  751. next := make(chan int)
  752. syncs = append(syncs, next)
  753. source := getMockSource(stream, next, 5)
  754. sources = append(sources, source)
  755. }
  756. }
  757. }
  758. tp, inputs, err := p.createTopoWithSources(&api.Rule{Id: tt.name, Sql: tt.sql, Options: map[string]interface{}{
  759. "bufferLength": float64(100),
  760. }}, sources)
  761. if err != nil {
  762. t.Error(err)
  763. }
  764. mockSink := test.NewMockSink()
  765. sink := nodes.NewSinkNodeWithSink("mockSink", mockSink, nil)
  766. tp.AddSink(inputs, sink)
  767. errCh := tp.Open()
  768. func() {
  769. for i := 0; i < 5; i++ {
  770. syncs[i%len(syncs)] <- i
  771. select {
  772. case err = <-errCh:
  773. t.Log(err)
  774. tp.Cancel()
  775. return
  776. default:
  777. }
  778. }
  779. for retry := 100; retry > 0; retry-- {
  780. if err := compareMetrics(tp, tt.m, tt.sql); err == nil {
  781. break
  782. }
  783. time.Sleep(time.Duration(retry) * time.Millisecond)
  784. }
  785. }()
  786. results := mockSink.GetResults()
  787. var maps [][]map[string]interface{}
  788. for _, v := range results {
  789. var mapRes []map[string]interface{}
  790. err := json.Unmarshal(v, &mapRes)
  791. if err != nil {
  792. t.Errorf("Failed to parse the input into map")
  793. continue
  794. }
  795. maps = append(maps, mapRes)
  796. }
  797. if !reflect.DeepEqual(tt.r, maps) {
  798. t.Errorf("%d. %q\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.sql, tt.r, maps)
  799. continue
  800. }
  801. if err := compareMetrics(tp, tt.m, tt.sql); err != nil {
  802. t.Errorf("%d. %q\n\n%v", i, tt.sql, err)
  803. }
  804. tp.Cancel()
  805. }
  806. }
  807. func TestSingleSQLTemplate(t *testing.T) {
  808. var tests = []struct {
  809. name string
  810. sql string
  811. r []map[string]interface{}
  812. s string
  813. m map[string]interface{}
  814. }{
  815. {
  816. name: `rule1`,
  817. sql: `SELECT * FROM demo`,
  818. r: []map[string]interface{}{
  819. {
  820. "c": "red",
  821. "wrapper": "w1",
  822. },
  823. {
  824. "c": "blue",
  825. "wrapper": "w1",
  826. },
  827. {
  828. "c": "blue",
  829. "wrapper": "w1",
  830. },
  831. {
  832. "c": "yellow",
  833. "wrapper": "w1",
  834. },
  835. {
  836. "c": "red",
  837. "wrapper": "w1",
  838. },
  839. },
  840. m: map[string]interface{}{
  841. "op_preprocessor_demo_0_exceptions_total": int64(0),
  842. "op_preprocessor_demo_0_process_latency_ms": int64(0),
  843. "op_preprocessor_demo_0_records_in_total": int64(5),
  844. "op_preprocessor_demo_0_records_out_total": int64(5),
  845. "op_project_0_exceptions_total": int64(0),
  846. "op_project_0_process_latency_ms": int64(0),
  847. "op_project_0_records_in_total": int64(5),
  848. "op_project_0_records_out_total": int64(5),
  849. "sink_mockSink_0_exceptions_total": int64(0),
  850. "sink_mockSink_0_records_in_total": int64(5),
  851. "sink_mockSink_0_records_out_total": int64(5),
  852. "source_demo_0_exceptions_total": int64(0),
  853. "source_demo_0_records_in_total": int64(5),
  854. "source_demo_0_records_out_total": int64(5),
  855. },
  856. s: "sink_mockSink_0_records_out_total",
  857. },
  858. }
  859. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  860. createStreams(t)
  861. defer dropStreams(t)
  862. //defer close(done)
  863. for i, tt := range tests {
  864. test.ResetClock(1541152486000)
  865. p := NewRuleProcessor(DbDir)
  866. parser := xsql.NewParser(strings.NewReader(tt.sql))
  867. var (
  868. sources []*nodes.SourceNode
  869. syncs []chan int
  870. )
  871. if stmt, err := xsql.Language.Parse(parser); err != nil {
  872. t.Errorf("parse sql %s error: %s", tt.sql, err)
  873. } else {
  874. if selectStmt, ok := stmt.(*xsql.SelectStatement); !ok {
  875. t.Errorf("sql %s is not a select statement", tt.sql)
  876. } else {
  877. streams := xsql.GetStreams(selectStmt)
  878. for _, stream := range streams {
  879. next := make(chan int)
  880. syncs = append(syncs, next)
  881. source := getMockSource(stream, next, 5)
  882. sources = append(sources, source)
  883. }
  884. }
  885. }
  886. tp, inputs, err := p.createTopoWithSources(&api.Rule{Id: tt.name, Sql: tt.sql, Options: map[string]interface{}{
  887. "bufferLength": float64(100),
  888. }}, sources)
  889. if err != nil {
  890. t.Error(err)
  891. }
  892. mockSink := test.NewMockSink()
  893. sink := nodes.NewSinkNodeWithSink("mockSink", mockSink, map[string]interface{}{
  894. "dataTemplate": `{"wrapper":"w1", "c":"{{.color}}"}`,
  895. "sendSingle": true,
  896. })
  897. tp.AddSink(inputs, sink)
  898. errCh := tp.Open()
  899. func() {
  900. for i := 0; i < 5; i++ {
  901. syncs[i%len(syncs)] <- i
  902. select {
  903. case err = <-errCh:
  904. t.Log(err)
  905. tp.Cancel()
  906. return
  907. default:
  908. }
  909. }
  910. for retry := 100; retry > 0; retry-- {
  911. if err := compareMetrics(tp, tt.m, tt.sql); err == nil {
  912. break
  913. }
  914. time.Sleep(time.Duration(retry) * time.Millisecond)
  915. }
  916. }()
  917. results := mockSink.GetResults()
  918. var maps []map[string]interface{}
  919. for _, v := range results {
  920. var mapRes map[string]interface{}
  921. err := json.Unmarshal(v, &mapRes)
  922. if err != nil {
  923. t.Errorf("Failed to parse the input into map")
  924. continue
  925. }
  926. maps = append(maps, mapRes)
  927. }
  928. if !reflect.DeepEqual(tt.r, maps) {
  929. t.Errorf("%d. %q\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.sql, tt.r, maps)
  930. continue
  931. }
  932. if err := compareMetrics(tp, tt.m, tt.sql); err != nil {
  933. t.Errorf("%d. %q\n\n%v", i, tt.sql, err)
  934. }
  935. tp.Cancel()
  936. }
  937. }
  938. func TestNoneSingleSQLTemplate(t *testing.T) {
  939. var tests = []struct {
  940. name string
  941. sql string
  942. r [][]byte
  943. s string
  944. m map[string]interface{}
  945. }{
  946. {
  947. name: `rule1`,
  948. sql: `SELECT * FROM demo`,
  949. r: [][]byte{
  950. []byte("<div>results</div><ul><li>red - 3</li></ul>"),
  951. []byte("<div>results</div><ul><li>blue - 6</li></ul>"),
  952. []byte("<div>results</div><ul><li>blue - 2</li></ul>"),
  953. []byte("<div>results</div><ul><li>yellow - 4</li></ul>"),
  954. []byte("<div>results</div><ul><li>red - 1</li></ul>"),
  955. },
  956. m: map[string]interface{}{
  957. "op_preprocessor_demo_0_exceptions_total": int64(0),
  958. "op_preprocessor_demo_0_process_latency_ms": int64(0),
  959. "op_preprocessor_demo_0_records_in_total": int64(5),
  960. "op_preprocessor_demo_0_records_out_total": int64(5),
  961. "op_project_0_exceptions_total": int64(0),
  962. "op_project_0_process_latency_ms": int64(0),
  963. "op_project_0_records_in_total": int64(5),
  964. "op_project_0_records_out_total": int64(5),
  965. "sink_mockSink_0_exceptions_total": int64(0),
  966. "sink_mockSink_0_records_in_total": int64(5),
  967. "sink_mockSink_0_records_out_total": int64(5),
  968. "source_demo_0_exceptions_total": int64(0),
  969. "source_demo_0_records_in_total": int64(5),
  970. "source_demo_0_records_out_total": int64(5),
  971. },
  972. s: "sink_mockSink_0_records_out_total",
  973. },
  974. }
  975. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  976. createStreams(t)
  977. defer dropStreams(t)
  978. //defer close(done)
  979. for i, tt := range tests {
  980. test.ResetClock(1541152486000)
  981. p := NewRuleProcessor(DbDir)
  982. parser := xsql.NewParser(strings.NewReader(tt.sql))
  983. var (
  984. sources []*nodes.SourceNode
  985. syncs []chan int
  986. )
  987. if stmt, err := xsql.Language.Parse(parser); err != nil {
  988. t.Errorf("parse sql %s error: %s", tt.sql, err)
  989. } else {
  990. if selectStmt, ok := stmt.(*xsql.SelectStatement); !ok {
  991. t.Errorf("sql %s is not a select statement", tt.sql)
  992. } else {
  993. streams := xsql.GetStreams(selectStmt)
  994. for _, stream := range streams {
  995. next := make(chan int)
  996. syncs = append(syncs, next)
  997. source := getMockSource(stream, next, 5)
  998. sources = append(sources, source)
  999. }
  1000. }
  1001. }
  1002. tp, inputs, err := p.createTopoWithSources(&api.Rule{Id: tt.name, Sql: tt.sql, Options: map[string]interface{}{
  1003. "bufferLength": float64(100),
  1004. }}, sources)
  1005. if err != nil {
  1006. t.Error(err)
  1007. }
  1008. mockSink := test.NewMockSink()
  1009. sink := nodes.NewSinkNodeWithSink("mockSink", mockSink, map[string]interface{}{
  1010. "dataTemplate": `<div>results</div><ul>{{range .}}<li>{{.color}} - {{.size}}</li>{{end}}</ul>`,
  1011. })
  1012. tp.AddSink(inputs, sink)
  1013. errCh := tp.Open()
  1014. func() {
  1015. for i := 0; i < 5; i++ {
  1016. syncs[i%len(syncs)] <- i
  1017. select {
  1018. case err = <-errCh:
  1019. t.Log(err)
  1020. tp.Cancel()
  1021. return
  1022. default:
  1023. }
  1024. }
  1025. for retry := 100; retry > 0; retry-- {
  1026. if err := compareMetrics(tp, tt.m, tt.sql); err == nil {
  1027. break
  1028. }
  1029. time.Sleep(time.Duration(retry) * time.Millisecond)
  1030. }
  1031. }()
  1032. results := mockSink.GetResults()
  1033. if !reflect.DeepEqual(tt.r, results) {
  1034. t.Errorf("%d. %q\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.sql, tt.r, results)
  1035. continue
  1036. }
  1037. if err := compareMetrics(tp, tt.m, tt.sql); err != nil {
  1038. t.Errorf("%d. %q\n\n%v", i, tt.sql, err)
  1039. }
  1040. tp.Cancel()
  1041. }
  1042. }
  1043. func getMockSourceL(name string, done <-chan int, size int) *nodes.SourceNode {
  1044. var data []*xsql.Tuple
  1045. switch name {
  1046. case "ldemo":
  1047. data = []*xsql.Tuple{
  1048. {
  1049. Emitter: name,
  1050. Message: map[string]interface{}{
  1051. "color": "red",
  1052. "size": 3,
  1053. "ts": 1541152486013,
  1054. },
  1055. Timestamp: 1541152486013,
  1056. },
  1057. {
  1058. Emitter: name,
  1059. Message: map[string]interface{}{
  1060. "color": "blue",
  1061. "size": "string",
  1062. "ts": 1541152486822,
  1063. },
  1064. Timestamp: 1541152486822,
  1065. },
  1066. {
  1067. Emitter: name,
  1068. Message: map[string]interface{}{
  1069. "size": 3,
  1070. "ts": 1541152487632,
  1071. },
  1072. Timestamp: 1541152487632,
  1073. },
  1074. {
  1075. Emitter: name,
  1076. Message: map[string]interface{}{
  1077. "color": 49,
  1078. "size": 2,
  1079. "ts": 1541152488442,
  1080. },
  1081. Timestamp: 1541152488442,
  1082. },
  1083. {
  1084. Emitter: name,
  1085. Message: map[string]interface{}{
  1086. "color": "red",
  1087. "ts": 1541152489252,
  1088. },
  1089. Timestamp: 1541152489252,
  1090. },
  1091. }
  1092. case "ldemo1":
  1093. data = []*xsql.Tuple{
  1094. {
  1095. Emitter: name,
  1096. Message: map[string]interface{}{
  1097. "temp": 25.5,
  1098. "hum": 65,
  1099. "ts": 1541152486013,
  1100. },
  1101. Timestamp: 1541152486013,
  1102. },
  1103. {
  1104. Emitter: name,
  1105. Message: map[string]interface{}{
  1106. "temp": 27.5,
  1107. "hum": 59,
  1108. "ts": 1541152486823,
  1109. },
  1110. Timestamp: 1541152486823,
  1111. },
  1112. {
  1113. Emitter: name,
  1114. Message: map[string]interface{}{
  1115. "temp": 28.1,
  1116. "hum": 75,
  1117. "ts": 1541152487632,
  1118. },
  1119. Timestamp: 1541152487632,
  1120. },
  1121. {
  1122. Emitter: name,
  1123. Message: map[string]interface{}{
  1124. "temp": 27.4,
  1125. "hum": 80,
  1126. "ts": "1541152488442",
  1127. },
  1128. Timestamp: 1541152488442,
  1129. },
  1130. {
  1131. Emitter: name,
  1132. Message: map[string]interface{}{
  1133. "temp": 25.5,
  1134. "hum": 62,
  1135. "ts": 1541152489252,
  1136. },
  1137. Timestamp: 1541152489252,
  1138. },
  1139. }
  1140. case "lsessionDemo":
  1141. data = []*xsql.Tuple{
  1142. {
  1143. Emitter: name,
  1144. Message: map[string]interface{}{
  1145. "temp": 25.5,
  1146. "hum": 65,
  1147. "ts": 1541152486013,
  1148. },
  1149. Timestamp: 1541152486013,
  1150. },
  1151. {
  1152. Emitter: name,
  1153. Message: map[string]interface{}{
  1154. "temp": 27.5,
  1155. "hum": 59,
  1156. "ts": 1541152486823,
  1157. },
  1158. Timestamp: 1541152486823,
  1159. },
  1160. {
  1161. Emitter: name,
  1162. Message: map[string]interface{}{
  1163. "temp": 28.1,
  1164. "hum": 75,
  1165. "ts": 1541152487932,
  1166. },
  1167. Timestamp: 1541152487932,
  1168. },
  1169. {
  1170. Emitter: name,
  1171. Message: map[string]interface{}{
  1172. "temp": 27.4,
  1173. "hum": 80,
  1174. "ts": 1541152488442,
  1175. },
  1176. Timestamp: 1541152488442,
  1177. },
  1178. {
  1179. Emitter: name,
  1180. Message: map[string]interface{}{
  1181. "temp": 25.5,
  1182. "hum": 62,
  1183. "ts": 1541152489252,
  1184. },
  1185. Timestamp: 1541152489252,
  1186. },
  1187. {
  1188. Emitter: name,
  1189. Message: map[string]interface{}{
  1190. "temp": 26.2,
  1191. "hum": 63,
  1192. "ts": 1541152490062,
  1193. },
  1194. Timestamp: 1541152490062,
  1195. },
  1196. {
  1197. Emitter: name,
  1198. Message: map[string]interface{}{
  1199. "temp": 26.8,
  1200. "hum": 71,
  1201. "ts": 1541152490872,
  1202. },
  1203. Timestamp: 1541152490872,
  1204. },
  1205. {
  1206. Emitter: name,
  1207. Message: map[string]interface{}{
  1208. "temp": 28.9,
  1209. "hum": 85,
  1210. "ts": 1541152491682,
  1211. },
  1212. Timestamp: 1541152491682,
  1213. },
  1214. {
  1215. Emitter: name,
  1216. Message: map[string]interface{}{
  1217. "temp": 29.1,
  1218. "hum": 92,
  1219. "ts": 1541152492492,
  1220. },
  1221. Timestamp: 1541152492492,
  1222. },
  1223. {
  1224. Emitter: name,
  1225. Message: map[string]interface{}{
  1226. "temp": 2.2,
  1227. "hum": 99,
  1228. "ts": 1541152493202,
  1229. },
  1230. Timestamp: 1541152493202,
  1231. },
  1232. {
  1233. Emitter: name,
  1234. Message: map[string]interface{}{
  1235. "temp": 30.9,
  1236. "hum": 87,
  1237. "ts": 1541152494112,
  1238. },
  1239. Timestamp: 1541152494112,
  1240. },
  1241. }
  1242. }
  1243. return nodes.NewSourceNodeWithSource(name, test.NewMockSource(data[:size], done, false), map[string]string{
  1244. "DATASOURCE": name,
  1245. })
  1246. }
  1247. func TestSingleSQLError(t *testing.T) {
  1248. var tests = []struct {
  1249. name string
  1250. sql string
  1251. r [][]map[string]interface{}
  1252. s string
  1253. m map[string]interface{}
  1254. }{
  1255. {
  1256. name: `rule1`,
  1257. sql: `SELECT color, ts FROM ldemo where size >= 3`,
  1258. r: [][]map[string]interface{}{
  1259. {{
  1260. "color": "red",
  1261. "ts": float64(1541152486013),
  1262. }},
  1263. {{
  1264. "error": "run Where error: invalid operation string(string) >= int64(3)",
  1265. }},
  1266. {{
  1267. "ts": float64(1541152487632),
  1268. }},
  1269. },
  1270. s: "op_filter_0_records_in_total",
  1271. m: map[string]interface{}{
  1272. "op_preprocessor_ldemo_0_exceptions_total": int64(0),
  1273. "op_preprocessor_ldemo_0_process_latency_ms": int64(0),
  1274. "op_preprocessor_ldemo_0_records_in_total": int64(5),
  1275. "op_preprocessor_ldemo_0_records_out_total": int64(5),
  1276. "op_project_0_exceptions_total": int64(1),
  1277. "op_project_0_process_latency_ms": int64(0),
  1278. "op_project_0_records_in_total": int64(3),
  1279. "op_project_0_records_out_total": int64(2),
  1280. "sink_mockSink_0_exceptions_total": int64(0),
  1281. "sink_mockSink_0_records_in_total": int64(3),
  1282. "sink_mockSink_0_records_out_total": int64(3),
  1283. "source_ldemo_0_exceptions_total": int64(0),
  1284. "source_ldemo_0_records_in_total": int64(5),
  1285. "source_ldemo_0_records_out_total": int64(5),
  1286. "op_filter_0_exceptions_total": int64(1),
  1287. "op_filter_0_process_latency_ms": int64(0),
  1288. "op_filter_0_records_in_total": int64(5),
  1289. "op_filter_0_records_out_total": int64(2),
  1290. },
  1291. }, {
  1292. name: `rule2`,
  1293. sql: `SELECT size * 5 FROM ldemo`,
  1294. r: [][]map[string]interface{}{
  1295. {{
  1296. "rengine_field_0": float64(15),
  1297. }},
  1298. {{
  1299. "error": "run Select error: invalid operation string(string) * int64(5)",
  1300. }},
  1301. {{
  1302. "rengine_field_0": float64(15),
  1303. }},
  1304. {{
  1305. "rengine_field_0": float64(10),
  1306. }},
  1307. {{}},
  1308. },
  1309. s: "op_filter_0_records_in_total",
  1310. m: map[string]interface{}{
  1311. "op_preprocessor_ldemo_0_exceptions_total": int64(0),
  1312. "op_preprocessor_ldemo_0_process_latency_ms": int64(0),
  1313. "op_preprocessor_ldemo_0_records_in_total": int64(5),
  1314. "op_preprocessor_ldemo_0_records_out_total": int64(5),
  1315. "op_project_0_exceptions_total": int64(1),
  1316. "op_project_0_process_latency_ms": int64(0),
  1317. "op_project_0_records_in_total": int64(5),
  1318. "op_project_0_records_out_total": int64(4),
  1319. "sink_mockSink_0_exceptions_total": int64(0),
  1320. "sink_mockSink_0_records_in_total": int64(5),
  1321. "sink_mockSink_0_records_out_total": int64(5),
  1322. "source_ldemo_0_exceptions_total": int64(0),
  1323. "source_ldemo_0_records_in_total": int64(5),
  1324. "source_ldemo_0_records_out_total": int64(5),
  1325. },
  1326. },
  1327. }
  1328. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  1329. createSchemalessStreams(t)
  1330. defer dropSchemalessStreams(t)
  1331. //defer close(done)
  1332. for i, tt := range tests {
  1333. test.ResetClock(1541152486000)
  1334. p := NewRuleProcessor(DbDir)
  1335. parser := xsql.NewParser(strings.NewReader(tt.sql))
  1336. var (
  1337. sources []*nodes.SourceNode
  1338. syncs []chan int
  1339. )
  1340. if stmt, err := xsql.Language.Parse(parser); err != nil {
  1341. t.Errorf("parse sql %s error: %s", tt.sql, err)
  1342. } else {
  1343. if selectStmt, ok := stmt.(*xsql.SelectStatement); !ok {
  1344. t.Errorf("sql %s is not a select statement", tt.sql)
  1345. } else {
  1346. streams := xsql.GetStreams(selectStmt)
  1347. for _, stream := range streams {
  1348. next := make(chan int)
  1349. syncs = append(syncs, next)
  1350. source := getMockSourceL(stream, next, 5)
  1351. sources = append(sources, source)
  1352. }
  1353. }
  1354. }
  1355. tp, inputs, err := p.createTopoWithSources(&api.Rule{Id: tt.name, Sql: tt.sql, Options: map[string]interface{}{
  1356. "bufferLength": float64(100),
  1357. }}, sources)
  1358. if err != nil {
  1359. t.Error(err)
  1360. }
  1361. mockSink := test.NewMockSink()
  1362. sink := nodes.NewSinkNodeWithSink("mockSink", mockSink, nil)
  1363. tp.AddSink(inputs, sink)
  1364. errCh := tp.Open()
  1365. func() {
  1366. for i := 0; i < 5; i++ {
  1367. syncs[i%len(syncs)] <- i
  1368. select {
  1369. case err = <-errCh:
  1370. t.Log(err)
  1371. tp.Cancel()
  1372. return
  1373. default:
  1374. }
  1375. }
  1376. for retry := 100; retry > 0; retry-- {
  1377. if err := compareMetrics(tp, tt.m, tt.sql); err == nil {
  1378. break
  1379. }
  1380. time.Sleep(time.Duration(retry) * time.Millisecond)
  1381. }
  1382. }()
  1383. results := mockSink.GetResults()
  1384. var maps [][]map[string]interface{}
  1385. for _, v := range results {
  1386. var mapRes []map[string]interface{}
  1387. err := json.Unmarshal(v, &mapRes)
  1388. if err != nil {
  1389. t.Errorf("Failed to parse the input into map")
  1390. continue
  1391. }
  1392. maps = append(maps, mapRes)
  1393. }
  1394. if !reflect.DeepEqual(tt.r, maps) {
  1395. t.Errorf("%d. %q\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.sql, tt.r, maps)
  1396. continue
  1397. }
  1398. if err := compareMetrics(tp, tt.m, tt.sql); err != nil {
  1399. t.Errorf("%d. %q\n\n%v", i, tt.sql, err)
  1400. }
  1401. tp.Cancel()
  1402. }
  1403. }
  1404. func TestWindow(t *testing.T) {
  1405. var tests = []struct {
  1406. name string
  1407. sql string
  1408. size int
  1409. r [][]map[string]interface{}
  1410. m map[string]interface{}
  1411. }{
  1412. {
  1413. name: `rule1`,
  1414. sql: `SELECT * FROM demo GROUP BY HOPPINGWINDOW(ss, 2, 1)`,
  1415. size: 5,
  1416. r: [][]map[string]interface{}{
  1417. {{
  1418. "color": "red",
  1419. "size": float64(3),
  1420. "ts": float64(1541152486013),
  1421. }, {
  1422. "color": "blue",
  1423. "size": float64(6),
  1424. "ts": float64(1541152486822),
  1425. }},
  1426. {{
  1427. "color": "red",
  1428. "size": float64(3),
  1429. "ts": float64(1541152486013),
  1430. }, {
  1431. "color": "blue",
  1432. "size": float64(6),
  1433. "ts": float64(1541152486822),
  1434. }, {
  1435. "color": "blue",
  1436. "size": float64(2),
  1437. "ts": float64(1541152487632),
  1438. }},
  1439. {{
  1440. "color": "blue",
  1441. "size": float64(2),
  1442. "ts": float64(1541152487632),
  1443. }, {
  1444. "color": "yellow",
  1445. "size": float64(4),
  1446. "ts": float64(1541152488442),
  1447. }},
  1448. },
  1449. m: map[string]interface{}{
  1450. "op_preprocessor_demo_0_exceptions_total": int64(0),
  1451. "op_preprocessor_demo_0_process_latency_ms": int64(0),
  1452. "op_preprocessor_demo_0_records_in_total": int64(5),
  1453. "op_preprocessor_demo_0_records_out_total": int64(5),
  1454. "op_project_0_exceptions_total": int64(0),
  1455. "op_project_0_process_latency_ms": int64(0),
  1456. "op_project_0_records_in_total": int64(3),
  1457. "op_project_0_records_out_total": int64(3),
  1458. "sink_mockSink_0_exceptions_total": int64(0),
  1459. "sink_mockSink_0_records_in_total": int64(3),
  1460. "sink_mockSink_0_records_out_total": int64(3),
  1461. "source_demo_0_exceptions_total": int64(0),
  1462. "source_demo_0_records_in_total": int64(5),
  1463. "source_demo_0_records_out_total": int64(5),
  1464. "op_window_0_exceptions_total": int64(0),
  1465. "op_window_0_process_latency_ms": int64(0),
  1466. "op_window_0_records_in_total": int64(5),
  1467. "op_window_0_records_out_total": int64(3),
  1468. },
  1469. }, {
  1470. name: `rule2`,
  1471. sql: `SELECT color, ts FROM demo where size > 2 GROUP BY tumblingwindow(ss, 1)`,
  1472. size: 5,
  1473. r: [][]map[string]interface{}{
  1474. {{
  1475. "color": "red",
  1476. "ts": float64(1541152486013),
  1477. }, {
  1478. "color": "blue",
  1479. "ts": float64(1541152486822),
  1480. }},
  1481. {{
  1482. "color": "yellow",
  1483. "ts": float64(1541152488442),
  1484. }},
  1485. },
  1486. m: map[string]interface{}{
  1487. "op_preprocessor_demo_0_exceptions_total": int64(0),
  1488. "op_preprocessor_demo_0_process_latency_ms": int64(0),
  1489. "op_preprocessor_demo_0_records_in_total": int64(5),
  1490. "op_preprocessor_demo_0_records_out_total": int64(5),
  1491. "op_project_0_exceptions_total": int64(0),
  1492. "op_project_0_process_latency_ms": int64(0),
  1493. "op_project_0_records_in_total": int64(2),
  1494. "op_project_0_records_out_total": int64(2),
  1495. "sink_mockSink_0_exceptions_total": int64(0),
  1496. "sink_mockSink_0_records_in_total": int64(2),
  1497. "sink_mockSink_0_records_out_total": int64(2),
  1498. "source_demo_0_exceptions_total": int64(0),
  1499. "source_demo_0_records_in_total": int64(5),
  1500. "source_demo_0_records_out_total": int64(5),
  1501. "op_window_0_exceptions_total": int64(0),
  1502. "op_window_0_process_latency_ms": int64(0),
  1503. "op_window_0_records_in_total": int64(5),
  1504. "op_window_0_records_out_total": int64(3),
  1505. "op_filter_0_exceptions_total": int64(0),
  1506. "op_filter_0_process_latency_ms": int64(0),
  1507. "op_filter_0_records_in_total": int64(3),
  1508. "op_filter_0_records_out_total": int64(2),
  1509. },
  1510. }, {
  1511. name: `rule3`,
  1512. sql: `SELECT color, temp, ts FROM demo INNER JOIN demo1 ON demo.ts = demo1.ts GROUP BY SlidingWindow(ss, 1)`,
  1513. size: 5,
  1514. r: [][]map[string]interface{}{
  1515. {{
  1516. "color": "red",
  1517. "temp": 25.5,
  1518. "ts": float64(1541152486013),
  1519. }}, {{
  1520. "color": "red",
  1521. "temp": 25.5,
  1522. "ts": float64(1541152486013),
  1523. }}, {{
  1524. "color": "red",
  1525. "temp": 25.5,
  1526. "ts": float64(1541152486013),
  1527. }}, {{
  1528. "color": "blue",
  1529. "temp": 28.1,
  1530. "ts": float64(1541152487632),
  1531. }}, {{
  1532. "color": "blue",
  1533. "temp": 28.1,
  1534. "ts": float64(1541152487632),
  1535. }}, {{
  1536. "color": "blue",
  1537. "temp": 28.1,
  1538. "ts": float64(1541152487632),
  1539. }, {
  1540. "color": "yellow",
  1541. "temp": 27.4,
  1542. "ts": float64(1541152488442),
  1543. }}, {{
  1544. "color": "yellow",
  1545. "temp": 27.4,
  1546. "ts": float64(1541152488442),
  1547. }}, {{
  1548. "color": "yellow",
  1549. "temp": 27.4,
  1550. "ts": float64(1541152488442),
  1551. }, {
  1552. "color": "red",
  1553. "temp": 25.5,
  1554. "ts": float64(1541152489252),
  1555. }},
  1556. },
  1557. m: map[string]interface{}{
  1558. "op_preprocessor_demo_0_exceptions_total": int64(0),
  1559. "op_preprocessor_demo_0_process_latency_ms": int64(0),
  1560. "op_preprocessor_demo_0_records_in_total": int64(5),
  1561. "op_preprocessor_demo_0_records_out_total": int64(5),
  1562. "op_preprocessor_demo1_0_exceptions_total": int64(0),
  1563. "op_preprocessor_demo1_0_process_latency_ms": int64(0),
  1564. "op_preprocessor_demo1_0_records_in_total": int64(5),
  1565. "op_preprocessor_demo1_0_records_out_total": int64(5),
  1566. "op_project_0_exceptions_total": int64(0),
  1567. "op_project_0_process_latency_ms": int64(0),
  1568. "op_project_0_records_in_total": int64(8),
  1569. "op_project_0_records_out_total": int64(8),
  1570. "sink_mockSink_0_exceptions_total": int64(0),
  1571. "sink_mockSink_0_records_in_total": int64(8),
  1572. "sink_mockSink_0_records_out_total": int64(8),
  1573. "source_demo_0_exceptions_total": int64(0),
  1574. "source_demo_0_records_in_total": int64(5),
  1575. "source_demo_0_records_out_total": int64(5),
  1576. "source_demo1_0_exceptions_total": int64(0),
  1577. "source_demo1_0_records_in_total": int64(5),
  1578. "source_demo1_0_records_out_total": int64(5),
  1579. "op_window_0_exceptions_total": int64(0),
  1580. "op_window_0_process_latency_ms": int64(0),
  1581. "op_window_0_records_in_total": int64(10),
  1582. "op_window_0_records_out_total": int64(10),
  1583. "op_join_0_exceptions_total": int64(0),
  1584. "op_join_0_process_latency_ms": int64(0),
  1585. "op_join_0_records_in_total": int64(10),
  1586. "op_join_0_records_out_total": int64(8),
  1587. },
  1588. }, {
  1589. name: `rule4`,
  1590. sql: `SELECT color FROM demo GROUP BY SlidingWindow(ss, 2), color ORDER BY color`,
  1591. size: 5,
  1592. r: [][]map[string]interface{}{
  1593. {{
  1594. "color": "red",
  1595. }}, {{
  1596. "color": "blue",
  1597. }, {
  1598. "color": "red",
  1599. }}, {{
  1600. "color": "blue",
  1601. }, {
  1602. "color": "red",
  1603. }}, {{
  1604. "color": "blue",
  1605. }, {
  1606. "color": "yellow",
  1607. }}, {{
  1608. "color": "blue",
  1609. }, {
  1610. "color": "red",
  1611. }, {
  1612. "color": "yellow",
  1613. }},
  1614. },
  1615. m: map[string]interface{}{
  1616. "op_preprocessor_demo_0_exceptions_total": int64(0),
  1617. "op_preprocessor_demo_0_process_latency_ms": int64(0),
  1618. "op_preprocessor_demo_0_records_in_total": int64(5),
  1619. "op_preprocessor_demo_0_records_out_total": int64(5),
  1620. "op_project_0_exceptions_total": int64(0),
  1621. "op_project_0_process_latency_ms": int64(0),
  1622. "op_project_0_records_in_total": int64(5),
  1623. "op_project_0_records_out_total": int64(5),
  1624. "sink_mockSink_0_exceptions_total": int64(0),
  1625. "sink_mockSink_0_records_in_total": int64(5),
  1626. "sink_mockSink_0_records_out_total": int64(5),
  1627. "source_demo_0_exceptions_total": int64(0),
  1628. "source_demo_0_records_in_total": int64(5),
  1629. "source_demo_0_records_out_total": int64(5),
  1630. "op_window_0_exceptions_total": int64(0),
  1631. "op_window_0_process_latency_ms": int64(0),
  1632. "op_window_0_records_in_total": int64(5),
  1633. "op_window_0_records_out_total": int64(5),
  1634. "op_aggregate_0_exceptions_total": int64(0),
  1635. "op_aggregate_0_process_latency_ms": int64(0),
  1636. "op_aggregate_0_records_in_total": int64(5),
  1637. "op_aggregate_0_records_out_total": int64(5),
  1638. "op_order_0_exceptions_total": int64(0),
  1639. "op_order_0_process_latency_ms": int64(0),
  1640. "op_order_0_records_in_total": int64(5),
  1641. "op_order_0_records_out_total": int64(5),
  1642. },
  1643. }, {
  1644. name: `rule5`,
  1645. sql: `SELECT temp FROM sessionDemo GROUP BY SessionWindow(ss, 2, 1) `,
  1646. size: 11,
  1647. r: [][]map[string]interface{}{
  1648. {{
  1649. "temp": 25.5,
  1650. }, {
  1651. "temp": 27.5,
  1652. }}, {{
  1653. "temp": 28.1,
  1654. }, {
  1655. "temp": 27.4,
  1656. }, {
  1657. "temp": 25.5,
  1658. }}, {{
  1659. "temp": 26.2,
  1660. }, {
  1661. "temp": 26.8,
  1662. }, {
  1663. "temp": 28.9,
  1664. }, {
  1665. "temp": 29.1,
  1666. }, {
  1667. "temp": 32.2,
  1668. }},
  1669. },
  1670. m: map[string]interface{}{
  1671. "op_preprocessor_sessionDemo_0_exceptions_total": int64(0),
  1672. "op_preprocessor_sessionDemo_0_process_latency_ms": int64(0),
  1673. "op_preprocessor_sessionDemo_0_records_in_total": int64(11),
  1674. "op_preprocessor_sessionDemo_0_records_out_total": int64(11),
  1675. "op_project_0_exceptions_total": int64(0),
  1676. "op_project_0_process_latency_ms": int64(0),
  1677. "op_project_0_records_in_total": int64(3),
  1678. "op_project_0_records_out_total": int64(3),
  1679. "sink_mockSink_0_exceptions_total": int64(0),
  1680. "sink_mockSink_0_records_in_total": int64(3),
  1681. "sink_mockSink_0_records_out_total": int64(3),
  1682. "source_sessionDemo_0_exceptions_total": int64(0),
  1683. "source_sessionDemo_0_records_in_total": int64(11),
  1684. "source_sessionDemo_0_records_out_total": int64(11),
  1685. "op_window_0_exceptions_total": int64(0),
  1686. "op_window_0_process_latency_ms": int64(0),
  1687. "op_window_0_records_in_total": int64(11),
  1688. "op_window_0_records_out_total": int64(3),
  1689. },
  1690. }, {
  1691. name: `rule6`,
  1692. sql: `SELECT max(temp) as m, count(color) as c FROM demo INNER JOIN demo1 ON demo.ts = demo1.ts GROUP BY SlidingWindow(ss, 1)`,
  1693. size: 5,
  1694. r: [][]map[string]interface{}{
  1695. {{
  1696. "m": 25.5,
  1697. "c": float64(1),
  1698. }}, {{
  1699. "m": 25.5,
  1700. "c": float64(1),
  1701. }}, {{
  1702. "m": 25.5,
  1703. "c": float64(1),
  1704. }}, {{
  1705. "m": 28.1,
  1706. "c": float64(1),
  1707. }}, {{
  1708. "m": 28.1,
  1709. "c": float64(1),
  1710. }}, {{
  1711. "m": 28.1,
  1712. "c": float64(2),
  1713. }}, {{
  1714. "m": 27.4,
  1715. "c": float64(1),
  1716. }}, {{
  1717. "m": 27.4,
  1718. "c": float64(2),
  1719. }},
  1720. },
  1721. m: map[string]interface{}{
  1722. "op_preprocessor_demo_0_exceptions_total": int64(0),
  1723. "op_preprocessor_demo_0_process_latency_ms": int64(0),
  1724. "op_preprocessor_demo_0_records_in_total": int64(5),
  1725. "op_preprocessor_demo_0_records_out_total": int64(5),
  1726. "op_preprocessor_demo1_0_exceptions_total": int64(0),
  1727. "op_preprocessor_demo1_0_process_latency_ms": int64(0),
  1728. "op_preprocessor_demo1_0_records_in_total": int64(5),
  1729. "op_preprocessor_demo1_0_records_out_total": int64(5),
  1730. "op_project_0_exceptions_total": int64(0),
  1731. "op_project_0_process_latency_ms": int64(0),
  1732. "op_project_0_records_in_total": int64(8),
  1733. "op_project_0_records_out_total": int64(8),
  1734. "sink_mockSink_0_exceptions_total": int64(0),
  1735. "sink_mockSink_0_records_in_total": int64(8),
  1736. "sink_mockSink_0_records_out_total": int64(8),
  1737. "source_demo_0_exceptions_total": int64(0),
  1738. "source_demo_0_records_in_total": int64(5),
  1739. "source_demo_0_records_out_total": int64(5),
  1740. "source_demo1_0_exceptions_total": int64(0),
  1741. "source_demo1_0_records_in_total": int64(5),
  1742. "source_demo1_0_records_out_total": int64(5),
  1743. "op_window_0_exceptions_total": int64(0),
  1744. "op_window_0_process_latency_ms": int64(0),
  1745. "op_window_0_records_in_total": int64(10),
  1746. "op_window_0_records_out_total": int64(10),
  1747. "op_join_0_exceptions_total": int64(0),
  1748. "op_join_0_process_latency_ms": int64(0),
  1749. "op_join_0_records_in_total": int64(10),
  1750. "op_join_0_records_out_total": int64(8),
  1751. },
  1752. }, {
  1753. name: `rule7`,
  1754. sql: `SELECT * FROM demoE GROUP BY HOPPINGWINDOW(ss, 2, 1)`,
  1755. size: 5,
  1756. r: [][]map[string]interface{}{
  1757. {{
  1758. "error": "error in preprocessor: invalid data type for color, expect string but found int(3)",
  1759. }},
  1760. {{
  1761. "color": "blue",
  1762. "size": float64(6),
  1763. "ts": float64(1541152486822),
  1764. }},
  1765. {{
  1766. "color": "blue",
  1767. "size": float64(6),
  1768. "ts": float64(1541152486822),
  1769. }, {
  1770. "color": "blue",
  1771. "size": float64(2),
  1772. "ts": float64(1541152487632),
  1773. }},
  1774. {{
  1775. "error": "error in preprocessor: invalid data type for color, expect string but found int(7)",
  1776. }},
  1777. {{
  1778. "color": "blue",
  1779. "size": float64(2),
  1780. "ts": float64(1541152487632),
  1781. }},
  1782. {{
  1783. "error": "error in preprocessor: invalid data type for size, expect bigint but found string(blue)",
  1784. }},
  1785. },
  1786. m: map[string]interface{}{
  1787. "op_preprocessor_demoE_0_exceptions_total": int64(3),
  1788. "op_preprocessor_demoE_0_process_latency_ms": int64(0),
  1789. "op_preprocessor_demoE_0_records_in_total": int64(5),
  1790. "op_preprocessor_demoE_0_records_out_total": int64(2),
  1791. "op_project_0_exceptions_total": int64(3),
  1792. "op_project_0_process_latency_ms": int64(0),
  1793. "op_project_0_records_in_total": int64(6),
  1794. "op_project_0_records_out_total": int64(3),
  1795. "sink_mockSink_0_exceptions_total": int64(0),
  1796. "sink_mockSink_0_records_in_total": int64(6),
  1797. "sink_mockSink_0_records_out_total": int64(6),
  1798. "source_demoE_0_exceptions_total": int64(0),
  1799. "source_demoE_0_records_in_total": int64(5),
  1800. "source_demoE_0_records_out_total": int64(5),
  1801. "op_window_0_exceptions_total": int64(3),
  1802. "op_window_0_process_latency_ms": int64(0),
  1803. "op_window_0_records_in_total": int64(5),
  1804. "op_window_0_records_out_total": int64(3),
  1805. },
  1806. }, {
  1807. name: `rule8`,
  1808. sql: `SELECT color, ts, count(*) as c FROM demo where size > 2 GROUP BY tumblingwindow(ss, 1) having c > 1`,
  1809. size: 5,
  1810. r: [][]map[string]interface{}{
  1811. {{
  1812. "color": "red",
  1813. "ts": float64(1541152486013),
  1814. "c": float64(2),
  1815. }},
  1816. },
  1817. m: map[string]interface{}{
  1818. "op_preprocessor_demo_0_exceptions_total": int64(0),
  1819. "op_preprocessor_demo_0_process_latency_ms": int64(0),
  1820. "op_preprocessor_demo_0_records_in_total": int64(5),
  1821. "op_preprocessor_demo_0_records_out_total": int64(5),
  1822. "op_project_0_exceptions_total": int64(0),
  1823. "op_project_0_process_latency_ms": int64(0),
  1824. "op_project_0_records_in_total": int64(1),
  1825. "op_project_0_records_out_total": int64(1),
  1826. "sink_mockSink_0_exceptions_total": int64(0),
  1827. "sink_mockSink_0_records_in_total": int64(1),
  1828. "sink_mockSink_0_records_out_total": int64(1),
  1829. "source_demo_0_exceptions_total": int64(0),
  1830. "source_demo_0_records_in_total": int64(5),
  1831. "source_demo_0_records_out_total": int64(5),
  1832. "op_window_0_exceptions_total": int64(0),
  1833. "op_window_0_process_latency_ms": int64(0),
  1834. "op_window_0_records_in_total": int64(5),
  1835. "op_window_0_records_out_total": int64(3),
  1836. "op_filter_0_exceptions_total": int64(0),
  1837. "op_filter_0_process_latency_ms": int64(0),
  1838. "op_filter_0_records_in_total": int64(3),
  1839. "op_filter_0_records_out_total": int64(2),
  1840. "op_aggregate_0_exceptions_total": int64(0),
  1841. "op_aggregate_0_process_latency_ms": int64(0),
  1842. "op_aggregate_0_records_in_total": int64(2),
  1843. "op_aggregate_0_records_out_total": int64(2),
  1844. "op_having_0_exceptions_total": int64(0),
  1845. "op_having_0_process_latency_ms": int64(0),
  1846. "op_having_0_records_in_total": int64(2),
  1847. "op_having_0_records_out_total": int64(1),
  1848. },
  1849. },
  1850. }
  1851. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  1852. createStreams(t)
  1853. defer dropStreams(t)
  1854. for i, tt := range tests {
  1855. test.ResetClock(1541152486000)
  1856. p := NewRuleProcessor(DbDir)
  1857. parser := xsql.NewParser(strings.NewReader(tt.sql))
  1858. var (
  1859. sources []*nodes.SourceNode
  1860. syncs []chan int
  1861. )
  1862. if stmt, err := xsql.Language.Parse(parser); err != nil {
  1863. t.Errorf("parse sql %s error: %s", tt.sql, err)
  1864. } else {
  1865. if selectStmt, ok := stmt.(*xsql.SelectStatement); !ok {
  1866. t.Errorf("sql %s is not a select statement", tt.sql)
  1867. } else {
  1868. streams := xsql.GetStreams(selectStmt)
  1869. for _, stream := range streams {
  1870. next := make(chan int)
  1871. syncs = append(syncs, next)
  1872. source := getMockSource(stream, next, tt.size)
  1873. sources = append(sources, source)
  1874. }
  1875. }
  1876. }
  1877. tp, inputs, err := p.createTopoWithSources(&api.Rule{Id: tt.name, Sql: tt.sql}, sources)
  1878. if err != nil {
  1879. t.Error(err)
  1880. }
  1881. mockSink := test.NewMockSink()
  1882. sink := nodes.NewSinkNodeWithSink("mockSink", mockSink, nil)
  1883. tp.AddSink(inputs, sink)
  1884. errCh := tp.Open()
  1885. func() {
  1886. for i := 0; i < tt.size*len(syncs); i++ {
  1887. syncs[i%len(syncs)] <- i
  1888. for {
  1889. time.Sleep(1)
  1890. if getMetric(tp, "op_window_0_records_in_total") == (i + 1) {
  1891. break
  1892. }
  1893. }
  1894. select {
  1895. case err = <-errCh:
  1896. t.Log(err)
  1897. tp.Cancel()
  1898. return
  1899. default:
  1900. }
  1901. }
  1902. retry := 100
  1903. for ; retry > 0; retry-- {
  1904. if err := compareMetrics(tp, tt.m, tt.sql); err == nil {
  1905. break
  1906. }
  1907. t.Logf("wait to try another %d times", retry)
  1908. time.Sleep(time.Duration(retry) * time.Millisecond)
  1909. }
  1910. if retry == 0 {
  1911. err := compareMetrics(tp, tt.m, tt.sql)
  1912. t.Errorf("could not get correct metrics: %v", err)
  1913. }
  1914. }()
  1915. results := mockSink.GetResults()
  1916. var maps [][]map[string]interface{}
  1917. for _, v := range results {
  1918. var mapRes []map[string]interface{}
  1919. err := json.Unmarshal(v, &mapRes)
  1920. if err != nil {
  1921. t.Errorf("Failed to parse the input into map")
  1922. continue
  1923. }
  1924. maps = append(maps, mapRes)
  1925. }
  1926. if !reflect.DeepEqual(tt.r, maps) {
  1927. t.Errorf("%d. %q\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.sql, tt.r, maps)
  1928. }
  1929. if err := compareMetrics(tp, tt.m, tt.sql); err != nil {
  1930. t.Errorf("%d. %q\n\n%v", i, tt.sql, err)
  1931. }
  1932. tp.Cancel()
  1933. }
  1934. }
  1935. func TestWindowError(t *testing.T) {
  1936. var tests = []struct {
  1937. name string
  1938. sql string
  1939. size int
  1940. r [][]map[string]interface{}
  1941. m map[string]interface{}
  1942. }{
  1943. {
  1944. name: `rule1`,
  1945. sql: `SELECT size * 3 FROM ldemo GROUP BY TUMBLINGWINDOW(ss, 2)`,
  1946. size: 5,
  1947. r: [][]map[string]interface{}{
  1948. {{
  1949. "error": "run Select error: invalid operation string(string) * int64(3)",
  1950. }},
  1951. },
  1952. m: map[string]interface{}{
  1953. "op_preprocessor_ldemo_0_exceptions_total": int64(0),
  1954. "op_preprocessor_ldemo_0_process_latency_ms": int64(0),
  1955. "op_preprocessor_ldemo_0_records_in_total": int64(5),
  1956. "op_preprocessor_ldemo_0_records_out_total": int64(5),
  1957. "op_project_0_exceptions_total": int64(1),
  1958. "op_project_0_process_latency_ms": int64(0),
  1959. "op_project_0_records_in_total": int64(1),
  1960. "op_project_0_records_out_total": int64(0),
  1961. "sink_mockSink_0_exceptions_total": int64(0),
  1962. "sink_mockSink_0_records_in_total": int64(1),
  1963. "sink_mockSink_0_records_out_total": int64(1),
  1964. "source_ldemo_0_exceptions_total": int64(0),
  1965. "source_ldemo_0_records_in_total": int64(5),
  1966. "source_ldemo_0_records_out_total": int64(5),
  1967. "op_window_0_exceptions_total": int64(0),
  1968. "op_window_0_process_latency_ms": int64(0),
  1969. "op_window_0_records_in_total": int64(5),
  1970. "op_window_0_records_out_total": int64(1),
  1971. },
  1972. }, {
  1973. name: `rule2`,
  1974. sql: `SELECT color, ts FROM ldemo where size > 2 GROUP BY tumblingwindow(ss, 1)`,
  1975. size: 5,
  1976. r: [][]map[string]interface{}{
  1977. {{
  1978. "error": "run Where error: invalid operation string(string) > int64(2)",
  1979. }}, {{
  1980. "ts": float64(1541152487632),
  1981. }},
  1982. },
  1983. m: map[string]interface{}{
  1984. "op_preprocessor_ldemo_0_exceptions_total": int64(0),
  1985. "op_preprocessor_ldemo_0_process_latency_ms": int64(0),
  1986. "op_preprocessor_ldemo_0_records_in_total": int64(5),
  1987. "op_preprocessor_ldemo_0_records_out_total": int64(5),
  1988. "op_project_0_exceptions_total": int64(1),
  1989. "op_project_0_process_latency_ms": int64(0),
  1990. "op_project_0_records_in_total": int64(2),
  1991. "op_project_0_records_out_total": int64(1),
  1992. "sink_mockSink_0_exceptions_total": int64(0),
  1993. "sink_mockSink_0_records_in_total": int64(2),
  1994. "sink_mockSink_0_records_out_total": int64(2),
  1995. "source_ldemo_0_exceptions_total": int64(0),
  1996. "source_ldemo_0_records_in_total": int64(5),
  1997. "source_ldemo_0_records_out_total": int64(5),
  1998. "op_window_0_exceptions_total": int64(0),
  1999. "op_window_0_process_latency_ms": int64(0),
  2000. "op_window_0_records_in_total": int64(5),
  2001. "op_window_0_records_out_total": int64(3),
  2002. "op_filter_0_exceptions_total": int64(1),
  2003. "op_filter_0_process_latency_ms": int64(0),
  2004. "op_filter_0_records_in_total": int64(3),
  2005. "op_filter_0_records_out_total": int64(1),
  2006. },
  2007. }, {
  2008. name: `rule3`,
  2009. sql: `SELECT color, temp, ts FROM ldemo INNER JOIN ldemo1 ON ldemo.ts = ldemo1.ts GROUP BY SlidingWindow(ss, 1)`,
  2010. size: 5,
  2011. r: [][]map[string]interface{}{
  2012. {{
  2013. "color": "red",
  2014. "temp": 25.5,
  2015. "ts": float64(1541152486013),
  2016. }}, {{
  2017. "color": "red",
  2018. "temp": 25.5,
  2019. "ts": float64(1541152486013),
  2020. }}, {{
  2021. "color": "red",
  2022. "temp": 25.5,
  2023. "ts": float64(1541152486013),
  2024. }}, {{
  2025. "temp": 28.1,
  2026. "ts": float64(1541152487632),
  2027. }}, {{
  2028. "temp": 28.1,
  2029. "ts": float64(1541152487632),
  2030. }}, {{
  2031. "error": "run Join error: invalid operation int64(1541152487632) = string(1541152488442)",
  2032. }}, {{
  2033. "error": "run Join error: invalid operation int64(1541152488442) = string(1541152488442)",
  2034. }}, {{
  2035. "error": "run Join error: invalid operation int64(1541152488442) = string(1541152488442)",
  2036. }},
  2037. },
  2038. m: map[string]interface{}{
  2039. "op_preprocessor_ldemo_0_exceptions_total": int64(0),
  2040. "op_preprocessor_ldemo_0_process_latency_ms": int64(0),
  2041. "op_preprocessor_ldemo_0_records_in_total": int64(5),
  2042. "op_preprocessor_ldemo_0_records_out_total": int64(5),
  2043. "op_preprocessor_ldemo1_0_exceptions_total": int64(0),
  2044. "op_preprocessor_ldemo1_0_process_latency_ms": int64(0),
  2045. "op_preprocessor_ldemo1_0_records_in_total": int64(5),
  2046. "op_preprocessor_ldemo1_0_records_out_total": int64(5),
  2047. "op_project_0_exceptions_total": int64(3),
  2048. "op_project_0_process_latency_ms": int64(0),
  2049. "op_project_0_records_in_total": int64(8),
  2050. "op_project_0_records_out_total": int64(5),
  2051. "sink_mockSink_0_exceptions_total": int64(0),
  2052. "sink_mockSink_0_records_in_total": int64(8),
  2053. "sink_mockSink_0_records_out_total": int64(8),
  2054. "source_ldemo_0_exceptions_total": int64(0),
  2055. "source_ldemo_0_records_in_total": int64(5),
  2056. "source_ldemo_0_records_out_total": int64(5),
  2057. "source_ldemo1_0_exceptions_total": int64(0),
  2058. "source_ldemo1_0_records_in_total": int64(5),
  2059. "source_ldemo1_0_records_out_total": int64(5),
  2060. "op_window_0_exceptions_total": int64(0),
  2061. "op_window_0_process_latency_ms": int64(0),
  2062. "op_window_0_records_in_total": int64(10),
  2063. "op_window_0_records_out_total": int64(10),
  2064. "op_join_0_exceptions_total": int64(3),
  2065. "op_join_0_process_latency_ms": int64(0),
  2066. "op_join_0_records_in_total": int64(10),
  2067. "op_join_0_records_out_total": int64(5),
  2068. },
  2069. }, {
  2070. name: `rule4`,
  2071. sql: `SELECT color FROM ldemo GROUP BY SlidingWindow(ss, 2), color having size >= 2 order by color`,
  2072. size: 5,
  2073. r: [][]map[string]interface{}{
  2074. {{
  2075. "color": "red",
  2076. }}, {{
  2077. "error": "run Having error: invalid operation string(string) >= int64(2)",
  2078. }}, {{
  2079. "error": "run Having error: invalid operation string(string) >= int64(2)",
  2080. }}, {{
  2081. "error": "run Having error: invalid operation string(string) >= int64(2)",
  2082. }}, {{
  2083. "color": float64(49),
  2084. }, {}},
  2085. },
  2086. m: map[string]interface{}{
  2087. "op_preprocessor_ldemo_0_exceptions_total": int64(0),
  2088. "op_preprocessor_ldemo_0_process_latency_ms": int64(0),
  2089. "op_preprocessor_ldemo_0_records_in_total": int64(5),
  2090. "op_preprocessor_ldemo_0_records_out_total": int64(5),
  2091. "op_project_0_exceptions_total": int64(3),
  2092. "op_project_0_process_latency_ms": int64(0),
  2093. "op_project_0_records_in_total": int64(5),
  2094. "op_project_0_records_out_total": int64(2),
  2095. "sink_mockSink_0_exceptions_total": int64(0),
  2096. "sink_mockSink_0_records_in_total": int64(5),
  2097. "sink_mockSink_0_records_out_total": int64(5),
  2098. "source_ldemo_0_exceptions_total": int64(0),
  2099. "source_ldemo_0_records_in_total": int64(5),
  2100. "source_ldemo_0_records_out_total": int64(5),
  2101. "op_window_0_exceptions_total": int64(0),
  2102. "op_window_0_process_latency_ms": int64(0),
  2103. "op_window_0_records_in_total": int64(5),
  2104. "op_window_0_records_out_total": int64(5),
  2105. "op_aggregate_0_exceptions_total": int64(0),
  2106. "op_aggregate_0_process_latency_ms": int64(0),
  2107. "op_aggregate_0_records_in_total": int64(5),
  2108. "op_aggregate_0_records_out_total": int64(5),
  2109. "op_having_0_exceptions_total": int64(3),
  2110. "op_having_0_process_latency_ms": int64(0),
  2111. "op_having_0_records_in_total": int64(5),
  2112. "op_having_0_records_out_total": int64(2),
  2113. },
  2114. }, {
  2115. name: `rule5`,
  2116. sql: `SELECT color, size FROM ldemo GROUP BY tumblingwindow(ss, 1) ORDER BY size`,
  2117. size: 5,
  2118. r: [][]map[string]interface{}{
  2119. {{
  2120. "error": "run Order By error: incompatible types for comparison: int and string",
  2121. }}, {{
  2122. "size": float64(3),
  2123. }}, {{
  2124. "color": float64(49),
  2125. "size": float64(2),
  2126. }},
  2127. },
  2128. m: map[string]interface{}{
  2129. "op_preprocessor_ldemo_0_exceptions_total": int64(0),
  2130. "op_preprocessor_ldemo_0_process_latency_ms": int64(0),
  2131. "op_preprocessor_ldemo_0_records_in_total": int64(5),
  2132. "op_preprocessor_ldemo_0_records_out_total": int64(5),
  2133. "op_project_0_exceptions_total": int64(1),
  2134. "op_project_0_process_latency_ms": int64(0),
  2135. "op_project_0_records_in_total": int64(3),
  2136. "op_project_0_records_out_total": int64(2),
  2137. "sink_mockSink_0_exceptions_total": int64(0),
  2138. "sink_mockSink_0_records_in_total": int64(3),
  2139. "sink_mockSink_0_records_out_total": int64(3),
  2140. "source_ldemo_0_exceptions_total": int64(0),
  2141. "source_ldemo_0_records_in_total": int64(5),
  2142. "source_ldemo_0_records_out_total": int64(5),
  2143. "op_window_0_exceptions_total": int64(0),
  2144. "op_window_0_process_latency_ms": int64(0),
  2145. "op_window_0_records_in_total": int64(5),
  2146. "op_window_0_records_out_total": int64(3),
  2147. "op_order_0_exceptions_total": int64(1),
  2148. "op_order_0_process_latency_ms": int64(0),
  2149. "op_order_0_records_in_total": int64(3),
  2150. "op_order_0_records_out_total": int64(2),
  2151. },
  2152. },
  2153. }
  2154. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  2155. createSchemalessStreams(t)
  2156. defer dropSchemalessStreams(t)
  2157. for i, tt := range tests {
  2158. test.ResetClock(1541152486000)
  2159. p := NewRuleProcessor(DbDir)
  2160. parser := xsql.NewParser(strings.NewReader(tt.sql))
  2161. var (
  2162. sources []*nodes.SourceNode
  2163. syncs []chan int
  2164. )
  2165. if stmt, err := xsql.Language.Parse(parser); err != nil {
  2166. t.Errorf("parse sql %s error: %s", tt.sql, err)
  2167. } else {
  2168. if selectStmt, ok := stmt.(*xsql.SelectStatement); !ok {
  2169. t.Errorf("sql %s is not a select statement", tt.sql)
  2170. } else {
  2171. streams := xsql.GetStreams(selectStmt)
  2172. for _, stream := range streams {
  2173. next := make(chan int)
  2174. syncs = append(syncs, next)
  2175. source := getMockSourceL(stream, next, tt.size)
  2176. sources = append(sources, source)
  2177. }
  2178. }
  2179. }
  2180. tp, inputs, err := p.createTopoWithSources(&api.Rule{Id: tt.name, Sql: tt.sql}, sources)
  2181. if err != nil {
  2182. t.Error(err)
  2183. }
  2184. mockSink := test.NewMockSink()
  2185. sink := nodes.NewSinkNodeWithSink("mockSink", mockSink, nil)
  2186. tp.AddSink(inputs, sink)
  2187. errCh := tp.Open()
  2188. func() {
  2189. for i := 0; i < tt.size*len(syncs); i++ {
  2190. syncs[i%len(syncs)] <- i
  2191. for {
  2192. time.Sleep(1)
  2193. if getMetric(tp, "op_window_0_records_in_total") == (i + 1) {
  2194. break
  2195. }
  2196. }
  2197. select {
  2198. case err = <-errCh:
  2199. t.Log(err)
  2200. tp.Cancel()
  2201. return
  2202. default:
  2203. }
  2204. }
  2205. retry := 100
  2206. for ; retry > 0; retry-- {
  2207. if err := compareMetrics(tp, tt.m, tt.sql); err == nil {
  2208. break
  2209. }
  2210. t.Logf("wait to try another %d times", retry)
  2211. time.Sleep(time.Duration(retry) * time.Millisecond)
  2212. }
  2213. if retry == 0 {
  2214. err := compareMetrics(tp, tt.m, tt.sql)
  2215. t.Errorf("could not get correct metrics: %v", err)
  2216. }
  2217. }()
  2218. results := mockSink.GetResults()
  2219. var maps [][]map[string]interface{}
  2220. for _, v := range results {
  2221. var mapRes []map[string]interface{}
  2222. err := json.Unmarshal(v, &mapRes)
  2223. if err != nil {
  2224. t.Errorf("Failed to parse the input into map")
  2225. continue
  2226. }
  2227. maps = append(maps, mapRes)
  2228. }
  2229. if !reflect.DeepEqual(tt.r, maps) {
  2230. t.Errorf("%d. %q\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.sql, tt.r, maps)
  2231. }
  2232. if err := compareMetrics(tp, tt.m, tt.sql); err != nil {
  2233. t.Errorf("%d. %q\n\n%v", i, tt.sql, err)
  2234. }
  2235. tp.Cancel()
  2236. }
  2237. }
  2238. func createEventStreams(t *testing.T) {
  2239. p := NewStreamProcessor(path.Join(DbDir, "stream"))
  2240. demo := `CREATE STREAM demoE (
  2241. color STRING,
  2242. size BIGINT,
  2243. ts BIGINT
  2244. ) WITH (DATASOURCE="demoE", FORMAT="json", KEY="ts", TIMESTAMP="ts");`
  2245. _, err := p.ExecStmt(demo)
  2246. if err != nil {
  2247. t.Log(err)
  2248. }
  2249. demo1 := `CREATE STREAM demo1E (
  2250. temp FLOAT,
  2251. hum BIGINT,
  2252. ts BIGINT
  2253. ) WITH (DATASOURCE="demo1E", FORMAT="json", KEY="ts", TIMESTAMP="ts");`
  2254. _, err = p.ExecStmt(demo1)
  2255. if err != nil {
  2256. t.Log(err)
  2257. }
  2258. sessionDemo := `CREATE STREAM sessionDemoE (
  2259. temp FLOAT,
  2260. hum BIGINT,
  2261. ts BIGINT
  2262. ) WITH (DATASOURCE="sessionDemoE", FORMAT="json", KEY="ts", TIMESTAMP="ts");`
  2263. _, err = p.ExecStmt(sessionDemo)
  2264. if err != nil {
  2265. t.Log(err)
  2266. }
  2267. demoErr := `CREATE STREAM demoErr (
  2268. color STRING,
  2269. size BIGINT,
  2270. ts BIGINT
  2271. ) WITH (DATASOURCE="demoErr", FORMAT="json", KEY="ts", TIMESTAMP="ts");`
  2272. _, err = p.ExecStmt(demoErr)
  2273. if err != nil {
  2274. t.Log(err)
  2275. }
  2276. }
  2277. func dropEventStreams(t *testing.T) {
  2278. p := NewStreamProcessor(path.Join(DbDir, "stream"))
  2279. demo := `DROP STREAM demoE`
  2280. _, err := p.ExecStmt(demo)
  2281. if err != nil {
  2282. t.Log(err)
  2283. }
  2284. demo1 := `DROP STREAM demo1E`
  2285. _, err = p.ExecStmt(demo1)
  2286. if err != nil {
  2287. t.Log(err)
  2288. }
  2289. sessionDemo := `DROP STREAM sessionDemoE`
  2290. _, err = p.ExecStmt(sessionDemo)
  2291. if err != nil {
  2292. t.Log(err)
  2293. }
  2294. demoErr := `DROP STREAM demoErr`
  2295. _, err = p.ExecStmt(demoErr)
  2296. if err != nil {
  2297. t.Log(err)
  2298. }
  2299. }
  2300. func getEventMockSource(name string, done <-chan int, size int) *nodes.SourceNode {
  2301. var data []*xsql.Tuple
  2302. switch name {
  2303. case "demoE":
  2304. data = []*xsql.Tuple{
  2305. {
  2306. Emitter: name,
  2307. Message: map[string]interface{}{
  2308. "color": "red",
  2309. "size": 3,
  2310. "ts": 1541152486013,
  2311. },
  2312. Timestamp: 1541152486013,
  2313. },
  2314. {
  2315. Emitter: name,
  2316. Message: map[string]interface{}{
  2317. "color": "blue",
  2318. "size": 2,
  2319. "ts": 1541152487632,
  2320. },
  2321. Timestamp: 1541152487632,
  2322. },
  2323. {
  2324. Emitter: name,
  2325. Message: map[string]interface{}{
  2326. "color": "red",
  2327. "size": 1,
  2328. "ts": 1541152489252,
  2329. },
  2330. Timestamp: 1541152489252,
  2331. },
  2332. { //dropped item
  2333. Emitter: name,
  2334. Message: map[string]interface{}{
  2335. "color": "blue",
  2336. "size": 6,
  2337. "ts": 1541152486822,
  2338. },
  2339. Timestamp: 1541152486822,
  2340. },
  2341. {
  2342. Emitter: name,
  2343. Message: map[string]interface{}{
  2344. "color": "yellow",
  2345. "size": 4,
  2346. "ts": 1541152488442,
  2347. },
  2348. Timestamp: 1541152488442,
  2349. },
  2350. { //To lift the watermark and issue all windows
  2351. Emitter: name,
  2352. Message: map[string]interface{}{
  2353. "color": "yellow",
  2354. "size": 4,
  2355. "ts": 1541152492342,
  2356. },
  2357. Timestamp: 1541152488442,
  2358. },
  2359. }
  2360. case "demo1E":
  2361. data = []*xsql.Tuple{
  2362. {
  2363. Emitter: name,
  2364. Message: map[string]interface{}{
  2365. "temp": 27.5,
  2366. "hum": 59,
  2367. "ts": 1541152486823,
  2368. },
  2369. Timestamp: 1541152486823,
  2370. },
  2371. {
  2372. Emitter: name,
  2373. Message: map[string]interface{}{
  2374. "temp": 25.5,
  2375. "hum": 65,
  2376. "ts": 1541152486013,
  2377. },
  2378. Timestamp: 1541152486013,
  2379. },
  2380. {
  2381. Emitter: name,
  2382. Message: map[string]interface{}{
  2383. "temp": 27.4,
  2384. "hum": 80,
  2385. "ts": 1541152488442,
  2386. },
  2387. Timestamp: 1541152488442,
  2388. },
  2389. {
  2390. Emitter: name,
  2391. Message: map[string]interface{}{
  2392. "temp": 28.1,
  2393. "hum": 75,
  2394. "ts": 1541152487632,
  2395. },
  2396. Timestamp: 1541152487632,
  2397. },
  2398. {
  2399. Emitter: name,
  2400. Message: map[string]interface{}{
  2401. "temp": 25.5,
  2402. "hum": 62,
  2403. "ts": 1541152489252,
  2404. },
  2405. Timestamp: 1541152489252,
  2406. },
  2407. {
  2408. Emitter: name,
  2409. Message: map[string]interface{}{
  2410. "temp": 25.5,
  2411. "hum": 62,
  2412. "ts": 1541152499252,
  2413. },
  2414. Timestamp: 1541152499252,
  2415. },
  2416. }
  2417. case "sessionDemoE":
  2418. data = []*xsql.Tuple{
  2419. {
  2420. Emitter: name,
  2421. Message: map[string]interface{}{
  2422. "temp": 25.5,
  2423. "hum": 65,
  2424. "ts": 1541152486013,
  2425. },
  2426. Timestamp: 1541152486013,
  2427. },
  2428. {
  2429. Emitter: name,
  2430. Message: map[string]interface{}{
  2431. "temp": 28.1,
  2432. "hum": 75,
  2433. "ts": 1541152487932,
  2434. },
  2435. Timestamp: 1541152487932,
  2436. },
  2437. {
  2438. Emitter: name,
  2439. Message: map[string]interface{}{
  2440. "temp": 27.5,
  2441. "hum": 59,
  2442. "ts": 1541152486823,
  2443. },
  2444. Timestamp: 1541152486823,
  2445. },
  2446. {
  2447. Emitter: name,
  2448. Message: map[string]interface{}{
  2449. "temp": 25.5,
  2450. "hum": 62,
  2451. "ts": 1541152489252,
  2452. },
  2453. Timestamp: 1541152489252,
  2454. },
  2455. {
  2456. Emitter: name,
  2457. Message: map[string]interface{}{
  2458. "temp": 27.4,
  2459. "hum": 80,
  2460. "ts": 1541152488442,
  2461. },
  2462. Timestamp: 1541152488442,
  2463. },
  2464. {
  2465. Emitter: name,
  2466. Message: map[string]interface{}{
  2467. "temp": 26.2,
  2468. "hum": 63,
  2469. "ts": 1541152490062,
  2470. },
  2471. Timestamp: 1541152490062,
  2472. },
  2473. {
  2474. Emitter: name,
  2475. Message: map[string]interface{}{
  2476. "temp": 28.9,
  2477. "hum": 85,
  2478. "ts": 1541152491682,
  2479. },
  2480. Timestamp: 1541152491682,
  2481. },
  2482. {
  2483. Emitter: name,
  2484. Message: map[string]interface{}{
  2485. "temp": 26.8,
  2486. "hum": 71,
  2487. "ts": 1541152490872,
  2488. },
  2489. Timestamp: 1541152490872,
  2490. },
  2491. {
  2492. Emitter: name,
  2493. Message: map[string]interface{}{
  2494. "temp": 29.1,
  2495. "hum": 92,
  2496. "ts": 1541152492492,
  2497. },
  2498. Timestamp: 1541152492492,
  2499. },
  2500. {
  2501. Emitter: name,
  2502. Message: map[string]interface{}{
  2503. "temp": 30.9,
  2504. "hum": 87,
  2505. "ts": 1541152494112,
  2506. },
  2507. Timestamp: 1541152494112,
  2508. },
  2509. {
  2510. Emitter: name,
  2511. Message: map[string]interface{}{
  2512. "temp": 32.2,
  2513. "hum": 99,
  2514. "ts": 1541152493202,
  2515. },
  2516. Timestamp: 1541152493202,
  2517. },
  2518. {
  2519. Emitter: name,
  2520. Message: map[string]interface{}{
  2521. "temp": 32.2,
  2522. "hum": 99,
  2523. "ts": 1541152499202,
  2524. },
  2525. Timestamp: 1541152499202,
  2526. },
  2527. }
  2528. case "demoErr":
  2529. data = []*xsql.Tuple{
  2530. {
  2531. Emitter: name,
  2532. Message: map[string]interface{}{
  2533. "color": "red",
  2534. "size": 3,
  2535. "ts": 1541152486013,
  2536. },
  2537. Timestamp: 1541152486013,
  2538. },
  2539. {
  2540. Emitter: name,
  2541. Message: map[string]interface{}{
  2542. "color": 2,
  2543. "size": "blue",
  2544. "ts": 1541152487632,
  2545. },
  2546. Timestamp: 1541152487632,
  2547. },
  2548. {
  2549. Emitter: name,
  2550. Message: map[string]interface{}{
  2551. "color": "red",
  2552. "size": 1,
  2553. "ts": 1541152489252,
  2554. },
  2555. Timestamp: 1541152489252,
  2556. },
  2557. { //dropped item
  2558. Emitter: name,
  2559. Message: map[string]interface{}{
  2560. "color": "blue",
  2561. "size": 6,
  2562. "ts": 1541152486822,
  2563. },
  2564. Timestamp: 1541152486822,
  2565. },
  2566. {
  2567. Emitter: name,
  2568. Message: map[string]interface{}{
  2569. "color": "yellow",
  2570. "size": 4,
  2571. "ts": 1541152488442,
  2572. },
  2573. Timestamp: 1541152488442,
  2574. },
  2575. { //To lift the watermark and issue all windows
  2576. Emitter: name,
  2577. Message: map[string]interface{}{
  2578. "color": "yellow",
  2579. "size": 4,
  2580. "ts": 1541152492342,
  2581. },
  2582. Timestamp: 1541152488442,
  2583. },
  2584. }
  2585. }
  2586. return nodes.NewSourceNodeWithSource(name, test.NewMockSource(data[:size], done, true), map[string]string{
  2587. "DATASOURCE": name,
  2588. })
  2589. }
  2590. func TestEventWindow(t *testing.T) {
  2591. var tests = []struct {
  2592. name string
  2593. sql string
  2594. size int
  2595. r [][]map[string]interface{}
  2596. m map[string]interface{}
  2597. }{
  2598. {
  2599. name: `rule1`,
  2600. sql: `SELECT * FROM demoE GROUP BY HOPPINGWINDOW(ss, 2, 1)`,
  2601. size: 6,
  2602. r: [][]map[string]interface{}{
  2603. {{
  2604. "color": "red",
  2605. "size": float64(3),
  2606. "ts": float64(1541152486013),
  2607. }},
  2608. {{
  2609. "color": "red",
  2610. "size": float64(3),
  2611. "ts": float64(1541152486013),
  2612. }, {
  2613. "color": "blue",
  2614. "size": float64(2),
  2615. "ts": float64(1541152487632),
  2616. }},
  2617. {{
  2618. "color": "blue",
  2619. "size": float64(2),
  2620. "ts": float64(1541152487632),
  2621. }, {
  2622. "color": "yellow",
  2623. "size": float64(4),
  2624. "ts": float64(1541152488442),
  2625. }}, {{
  2626. "color": "yellow",
  2627. "size": float64(4),
  2628. "ts": float64(1541152488442),
  2629. }, {
  2630. "color": "red",
  2631. "size": float64(1),
  2632. "ts": float64(1541152489252),
  2633. }}, {{
  2634. "color": "red",
  2635. "size": float64(1),
  2636. "ts": float64(1541152489252),
  2637. }},
  2638. },
  2639. m: map[string]interface{}{
  2640. "op_preprocessor_demoE_0_exceptions_total": int64(0),
  2641. "op_preprocessor_demoE_0_process_latency_ms": int64(0),
  2642. "op_preprocessor_demoE_0_records_in_total": int64(6),
  2643. "op_preprocessor_demoE_0_records_out_total": int64(6),
  2644. "op_project_0_exceptions_total": int64(0),
  2645. "op_project_0_process_latency_ms": int64(0),
  2646. "op_project_0_records_in_total": int64(5),
  2647. "op_project_0_records_out_total": int64(5),
  2648. "sink_mockSink_0_exceptions_total": int64(0),
  2649. "sink_mockSink_0_records_in_total": int64(5),
  2650. "sink_mockSink_0_records_out_total": int64(5),
  2651. "source_demoE_0_exceptions_total": int64(0),
  2652. "source_demoE_0_records_in_total": int64(6),
  2653. "source_demoE_0_records_out_total": int64(6),
  2654. "op_window_0_exceptions_total": int64(0),
  2655. "op_window_0_process_latency_ms": int64(0),
  2656. "op_window_0_records_in_total": int64(6),
  2657. "op_window_0_records_out_total": int64(5),
  2658. },
  2659. }, {
  2660. name: `rule2`,
  2661. sql: `SELECT color, ts FROM demoE where size > 2 GROUP BY tumblingwindow(ss, 1)`,
  2662. size: 6,
  2663. r: [][]map[string]interface{}{
  2664. {{
  2665. "color": "red",
  2666. "ts": float64(1541152486013),
  2667. }},
  2668. {{
  2669. "color": "yellow",
  2670. "ts": float64(1541152488442),
  2671. }},
  2672. },
  2673. m: map[string]interface{}{
  2674. "op_preprocessor_demoE_0_exceptions_total": int64(0),
  2675. "op_preprocessor_demoE_0_process_latency_ms": int64(0),
  2676. "op_preprocessor_demoE_0_records_in_total": int64(6),
  2677. "op_preprocessor_demoE_0_records_out_total": int64(6),
  2678. "op_project_0_exceptions_total": int64(0),
  2679. "op_project_0_process_latency_ms": int64(0),
  2680. "op_project_0_records_in_total": int64(2),
  2681. "op_project_0_records_out_total": int64(2),
  2682. "sink_mockSink_0_exceptions_total": int64(0),
  2683. "sink_mockSink_0_records_in_total": int64(2),
  2684. "sink_mockSink_0_records_out_total": int64(2),
  2685. "source_demoE_0_exceptions_total": int64(0),
  2686. "source_demoE_0_records_in_total": int64(6),
  2687. "source_demoE_0_records_out_total": int64(6),
  2688. "op_window_0_exceptions_total": int64(0),
  2689. "op_window_0_process_latency_ms": int64(0),
  2690. "op_window_0_records_in_total": int64(6),
  2691. "op_window_0_records_out_total": int64(4),
  2692. "op_filter_0_exceptions_total": int64(0),
  2693. "op_filter_0_process_latency_ms": int64(0),
  2694. "op_filter_0_records_in_total": int64(4),
  2695. "op_filter_0_records_out_total": int64(2),
  2696. },
  2697. }, {
  2698. name: `rule3`,
  2699. sql: `SELECT color, temp, ts FROM demoE INNER JOIN demo1E ON demoE.ts = demo1E.ts GROUP BY SlidingWindow(ss, 1)`,
  2700. size: 6,
  2701. r: [][]map[string]interface{}{
  2702. {{
  2703. "color": "red",
  2704. "temp": 25.5,
  2705. "ts": float64(1541152486013),
  2706. }}, {{
  2707. "color": "red",
  2708. "temp": 25.5,
  2709. "ts": float64(1541152486013),
  2710. }}, {{
  2711. "color": "blue",
  2712. "temp": 28.1,
  2713. "ts": float64(1541152487632),
  2714. }}, {{
  2715. "color": "blue",
  2716. "temp": 28.1,
  2717. "ts": float64(1541152487632),
  2718. }, {
  2719. "color": "yellow",
  2720. "temp": 27.4,
  2721. "ts": float64(1541152488442),
  2722. }}, {{
  2723. "color": "yellow",
  2724. "temp": 27.4,
  2725. "ts": float64(1541152488442),
  2726. }, {
  2727. "color": "red",
  2728. "temp": 25.5,
  2729. "ts": float64(1541152489252),
  2730. }},
  2731. },
  2732. m: map[string]interface{}{
  2733. "op_preprocessor_demoE_0_exceptions_total": int64(0),
  2734. "op_preprocessor_demoE_0_process_latency_ms": int64(0),
  2735. "op_preprocessor_demoE_0_records_in_total": int64(6),
  2736. "op_preprocessor_demoE_0_records_out_total": int64(6),
  2737. "op_preprocessor_demo1E_0_exceptions_total": int64(0),
  2738. "op_preprocessor_demo1E_0_process_latency_ms": int64(0),
  2739. "op_preprocessor_demo1E_0_records_in_total": int64(6),
  2740. "op_preprocessor_demo1E_0_records_out_total": int64(6),
  2741. "op_project_0_exceptions_total": int64(0),
  2742. "op_project_0_process_latency_ms": int64(0),
  2743. "op_project_0_records_in_total": int64(5),
  2744. "op_project_0_records_out_total": int64(5),
  2745. "sink_mockSink_0_exceptions_total": int64(0),
  2746. "sink_mockSink_0_records_in_total": int64(5),
  2747. "sink_mockSink_0_records_out_total": int64(5),
  2748. "source_demoE_0_exceptions_total": int64(0),
  2749. "source_demoE_0_records_in_total": int64(6),
  2750. "source_demoE_0_records_out_total": int64(6),
  2751. "source_demo1E_0_exceptions_total": int64(0),
  2752. "source_demo1E_0_records_in_total": int64(6),
  2753. "source_demo1E_0_records_out_total": int64(6),
  2754. "op_window_0_exceptions_total": int64(0),
  2755. "op_window_0_process_latency_ms": int64(0),
  2756. "op_window_0_records_in_total": int64(12),
  2757. "op_window_0_records_out_total": int64(5),
  2758. "op_join_0_exceptions_total": int64(0),
  2759. "op_join_0_process_latency_ms": int64(0),
  2760. "op_join_0_records_in_total": int64(5),
  2761. "op_join_0_records_out_total": int64(5),
  2762. },
  2763. }, {
  2764. name: `rule4`,
  2765. sql: `SELECT color FROM demoE GROUP BY SlidingWindow(ss, 2), color ORDER BY color`,
  2766. size: 6,
  2767. r: [][]map[string]interface{}{
  2768. {{
  2769. "color": "red",
  2770. }}, {{
  2771. "color": "blue",
  2772. }, {
  2773. "color": "red",
  2774. }}, {{
  2775. "color": "blue",
  2776. }, {
  2777. "color": "yellow",
  2778. }}, {{
  2779. "color": "blue",
  2780. }, {
  2781. "color": "red",
  2782. }, {
  2783. "color": "yellow",
  2784. }},
  2785. },
  2786. m: map[string]interface{}{
  2787. "op_preprocessor_demoE_0_exceptions_total": int64(0),
  2788. "op_preprocessor_demoE_0_process_latency_ms": int64(0),
  2789. "op_preprocessor_demoE_0_records_in_total": int64(6),
  2790. "op_preprocessor_demoE_0_records_out_total": int64(6),
  2791. "op_project_0_exceptions_total": int64(0),
  2792. "op_project_0_process_latency_ms": int64(0),
  2793. "op_project_0_records_in_total": int64(4),
  2794. "op_project_0_records_out_total": int64(4),
  2795. "sink_mockSink_0_exceptions_total": int64(0),
  2796. "sink_mockSink_0_records_in_total": int64(4),
  2797. "sink_mockSink_0_records_out_total": int64(4),
  2798. "source_demoE_0_exceptions_total": int64(0),
  2799. "source_demoE_0_records_in_total": int64(6),
  2800. "source_demoE_0_records_out_total": int64(6),
  2801. "op_window_0_exceptions_total": int64(0),
  2802. "op_window_0_process_latency_ms": int64(0),
  2803. "op_window_0_records_in_total": int64(6),
  2804. "op_window_0_records_out_total": int64(4),
  2805. "op_aggregate_0_exceptions_total": int64(0),
  2806. "op_aggregate_0_process_latency_ms": int64(0),
  2807. "op_aggregate_0_records_in_total": int64(4),
  2808. "op_aggregate_0_records_out_total": int64(4),
  2809. "op_order_0_exceptions_total": int64(0),
  2810. "op_order_0_process_latency_ms": int64(0),
  2811. "op_order_0_records_in_total": int64(4),
  2812. "op_order_0_records_out_total": int64(4),
  2813. },
  2814. }, {
  2815. name: `rule5`,
  2816. sql: `SELECT temp FROM sessionDemoE GROUP BY SessionWindow(ss, 2, 1) `,
  2817. size: 12,
  2818. r: [][]map[string]interface{}{
  2819. {{
  2820. "temp": 25.5,
  2821. }}, {{
  2822. "temp": 28.1,
  2823. }, {
  2824. "temp": 27.4,
  2825. }, {
  2826. "temp": 25.5,
  2827. }}, {{
  2828. "temp": 26.2,
  2829. }, {
  2830. "temp": 26.8,
  2831. }, {
  2832. "temp": 28.9,
  2833. }, {
  2834. "temp": 29.1,
  2835. }, {
  2836. "temp": 32.2,
  2837. }}, {{
  2838. "temp": 30.9,
  2839. }},
  2840. },
  2841. m: map[string]interface{}{
  2842. "op_preprocessor_sessionDemoE_0_exceptions_total": int64(0),
  2843. "op_preprocessor_sessionDemoE_0_process_latency_ms": int64(0),
  2844. "op_preprocessor_sessionDemoE_0_records_in_total": int64(12),
  2845. "op_preprocessor_sessionDemoE_0_records_out_total": int64(12),
  2846. "op_project_0_exceptions_total": int64(0),
  2847. "op_project_0_process_latency_ms": int64(0),
  2848. "op_project_0_records_in_total": int64(4),
  2849. "op_project_0_records_out_total": int64(4),
  2850. "sink_mockSink_0_exceptions_total": int64(0),
  2851. "sink_mockSink_0_records_in_total": int64(4),
  2852. "sink_mockSink_0_records_out_total": int64(4),
  2853. "source_sessionDemoE_0_exceptions_total": int64(0),
  2854. "source_sessionDemoE_0_records_in_total": int64(12),
  2855. "source_sessionDemoE_0_records_out_total": int64(12),
  2856. "op_window_0_exceptions_total": int64(0),
  2857. "op_window_0_process_latency_ms": int64(0),
  2858. "op_window_0_records_in_total": int64(12),
  2859. "op_window_0_records_out_total": int64(4),
  2860. },
  2861. }, {
  2862. name: `rule6`,
  2863. 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)`,
  2864. size: 6,
  2865. r: [][]map[string]interface{}{
  2866. {{
  2867. "m": 25.5,
  2868. "c": float64(1),
  2869. }}, {{
  2870. "m": 25.5,
  2871. "c": float64(1),
  2872. }}, {{
  2873. "m": 28.1,
  2874. "c": float64(1),
  2875. }}, {{
  2876. "m": 28.1,
  2877. "c": float64(2),
  2878. }}, {{
  2879. "m": 27.4,
  2880. "c": float64(2),
  2881. }},
  2882. },
  2883. m: map[string]interface{}{
  2884. "op_preprocessor_demoE_0_exceptions_total": int64(0),
  2885. "op_preprocessor_demoE_0_process_latency_ms": int64(0),
  2886. "op_preprocessor_demoE_0_records_in_total": int64(6),
  2887. "op_preprocessor_demoE_0_records_out_total": int64(6),
  2888. "op_preprocessor_demo1E_0_exceptions_total": int64(0),
  2889. "op_preprocessor_demo1E_0_process_latency_ms": int64(0),
  2890. "op_preprocessor_demo1E_0_records_in_total": int64(6),
  2891. "op_preprocessor_demo1E_0_records_out_total": int64(6),
  2892. "op_project_0_exceptions_total": int64(0),
  2893. "op_project_0_process_latency_ms": int64(0),
  2894. "op_project_0_records_in_total": int64(5),
  2895. "op_project_0_records_out_total": int64(5),
  2896. "sink_mockSink_0_exceptions_total": int64(0),
  2897. "sink_mockSink_0_records_in_total": int64(5),
  2898. "sink_mockSink_0_records_out_total": int64(5),
  2899. "source_demoE_0_exceptions_total": int64(0),
  2900. "source_demoE_0_records_in_total": int64(6),
  2901. "source_demoE_0_records_out_total": int64(6),
  2902. "source_demo1E_0_exceptions_total": int64(0),
  2903. "source_demo1E_0_records_in_total": int64(6),
  2904. "source_demo1E_0_records_out_total": int64(6),
  2905. "op_window_0_exceptions_total": int64(0),
  2906. "op_window_0_records_in_total": int64(12),
  2907. "op_window_0_records_out_total": int64(5),
  2908. "op_join_0_exceptions_total": int64(0),
  2909. "op_join_0_process_latency_ms": int64(0),
  2910. "op_join_0_records_in_total": int64(5),
  2911. "op_join_0_records_out_total": int64(5),
  2912. },
  2913. }, {
  2914. name: `rule7`,
  2915. sql: `SELECT * FROM demoErr GROUP BY HOPPINGWINDOW(ss, 2, 1)`,
  2916. size: 6,
  2917. r: [][]map[string]interface{}{
  2918. {{
  2919. "error": "error in preprocessor: invalid data type for color, expect string but found int(2)",
  2920. }},
  2921. {{
  2922. "color": "red",
  2923. "size": float64(3),
  2924. "ts": float64(1541152486013),
  2925. }},
  2926. {{
  2927. "color": "red",
  2928. "size": float64(3),
  2929. "ts": float64(1541152486013),
  2930. }},
  2931. {{
  2932. "color": "yellow",
  2933. "size": float64(4),
  2934. "ts": float64(1541152488442),
  2935. }}, {{
  2936. "color": "yellow",
  2937. "size": float64(4),
  2938. "ts": float64(1541152488442),
  2939. }, {
  2940. "color": "red",
  2941. "size": float64(1),
  2942. "ts": float64(1541152489252),
  2943. }}, {{
  2944. "color": "red",
  2945. "size": float64(1),
  2946. "ts": float64(1541152489252),
  2947. }},
  2948. },
  2949. m: map[string]interface{}{
  2950. "op_preprocessor_demoErr_0_exceptions_total": int64(1),
  2951. "op_preprocessor_demoErr_0_process_latency_ms": int64(0),
  2952. "op_preprocessor_demoErr_0_records_in_total": int64(6),
  2953. "op_preprocessor_demoErr_0_records_out_total": int64(5),
  2954. "op_project_0_exceptions_total": int64(1),
  2955. "op_project_0_process_latency_ms": int64(0),
  2956. "op_project_0_records_in_total": int64(6),
  2957. "op_project_0_records_out_total": int64(5),
  2958. "sink_mockSink_0_exceptions_total": int64(0),
  2959. "sink_mockSink_0_records_in_total": int64(6),
  2960. "sink_mockSink_0_records_out_total": int64(6),
  2961. "source_demoErr_0_exceptions_total": int64(0),
  2962. "source_demoErr_0_records_in_total": int64(6),
  2963. "source_demoErr_0_records_out_total": int64(6),
  2964. "op_window_0_exceptions_total": int64(1),
  2965. "op_window_0_process_latency_ms": int64(0),
  2966. "op_window_0_records_in_total": int64(6),
  2967. "op_window_0_records_out_total": int64(5),
  2968. },
  2969. },
  2970. }
  2971. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  2972. createEventStreams(t)
  2973. defer dropEventStreams(t)
  2974. for i, tt := range tests {
  2975. test.ResetClock(1541152486000)
  2976. p := NewRuleProcessor(DbDir)
  2977. parser := xsql.NewParser(strings.NewReader(tt.sql))
  2978. var (
  2979. sources []*nodes.SourceNode
  2980. syncs []chan int
  2981. )
  2982. if stmt, err := xsql.Language.Parse(parser); err != nil {
  2983. t.Errorf("parse sql %s error: %s", tt.sql, err)
  2984. } else {
  2985. if selectStmt, ok := stmt.(*xsql.SelectStatement); !ok {
  2986. t.Errorf("sql %s is not a select statement", tt.sql)
  2987. } else {
  2988. streams := xsql.GetStreams(selectStmt)
  2989. for _, stream := range streams {
  2990. next := make(chan int)
  2991. syncs = append(syncs, next)
  2992. source := getEventMockSource(stream, next, tt.size)
  2993. sources = append(sources, source)
  2994. }
  2995. }
  2996. }
  2997. tp, inputs, err := p.createTopoWithSources(&api.Rule{
  2998. Id: tt.name, Sql: tt.sql,
  2999. Options: map[string]interface{}{
  3000. "isEventTime": true,
  3001. "lateTolerance": float64(1000),
  3002. },
  3003. }, sources)
  3004. if err != nil {
  3005. t.Error(err)
  3006. }
  3007. mockSink := test.NewMockSink()
  3008. sink := nodes.NewSinkNodeWithSink("mockSink", mockSink, nil)
  3009. tp.AddSink(inputs, sink)
  3010. errCh := tp.Open()
  3011. func() {
  3012. for i := 0; i < tt.size*len(syncs); i++ {
  3013. syncs[i%len(syncs)] <- i
  3014. for {
  3015. time.Sleep(1)
  3016. if getMetric(tp, "op_window_0_records_in_total") == (i + 1) {
  3017. break
  3018. }
  3019. }
  3020. select {
  3021. case err = <-errCh:
  3022. t.Log(err)
  3023. tp.Cancel()
  3024. return
  3025. default:
  3026. }
  3027. }
  3028. mockClock := test.GetMockClock()
  3029. mockClock.Add(1000 * time.Millisecond)
  3030. retry := 100
  3031. for ; retry > 0; retry-- {
  3032. if err := compareMetrics(tp, tt.m, tt.sql); err == nil {
  3033. break
  3034. }
  3035. t.Logf("wait to try another %d times", retry)
  3036. time.Sleep(time.Duration(retry) * time.Millisecond)
  3037. }
  3038. if retry == 0 {
  3039. err := compareMetrics(tp, tt.m, tt.sql)
  3040. t.Errorf("could not get correct metrics: %v", err)
  3041. }
  3042. }()
  3043. results := mockSink.GetResults()
  3044. var maps [][]map[string]interface{}
  3045. for _, v := range results {
  3046. var mapRes []map[string]interface{}
  3047. err := json.Unmarshal(v, &mapRes)
  3048. if err != nil {
  3049. t.Errorf("Failed to parse the input into map")
  3050. continue
  3051. }
  3052. maps = append(maps, mapRes)
  3053. }
  3054. if !reflect.DeepEqual(tt.r, maps) {
  3055. t.Errorf("%d. %q\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.sql, tt.r, maps)
  3056. }
  3057. if err := compareMetrics(tp, tt.m, tt.sql); err != nil {
  3058. t.Errorf("%d. %q\n\n%v", i, tt.sql, err)
  3059. }
  3060. tp.Cancel()
  3061. }
  3062. }
  3063. func getMetric(tp *xstream.TopologyNew, name string) int {
  3064. keys, values := tp.GetMetrics()
  3065. for index, key := range keys {
  3066. if key == name {
  3067. return int(values[index].(int64))
  3068. }
  3069. }
  3070. fmt.Println("can't find " + name)
  3071. return 0
  3072. }
  3073. func compareMetrics(tp *xstream.TopologyNew, m map[string]interface{}, sql string) (err error) {
  3074. keys, values := tp.GetMetrics()
  3075. //for i, k := range keys {
  3076. // log.Printf("%s:%v", k, values[i])
  3077. //}
  3078. for k, v := range m {
  3079. var (
  3080. index int
  3081. key string
  3082. matched bool
  3083. )
  3084. for index, key = range keys {
  3085. if k == key {
  3086. if strings.HasSuffix(k, "process_latency_ms") {
  3087. if values[index].(int64) >= v.(int64) {
  3088. matched = true
  3089. continue
  3090. } else {
  3091. break
  3092. }
  3093. }
  3094. if values[index] == v {
  3095. matched = true
  3096. }
  3097. break
  3098. }
  3099. }
  3100. if matched {
  3101. continue
  3102. }
  3103. //do not find
  3104. if index < len(values) {
  3105. return fmt.Errorf("metrics mismatch for %s:\n\nexp=%#v(%t)\n\ngot=%#v(%t)\n\n", k, v, v, values[index], values[index])
  3106. } else {
  3107. return fmt.Errorf("metrics mismatch for %s:\n\nexp=%#v\n\ngot=nil\n\n", k, v)
  3108. }
  3109. }
  3110. return nil
  3111. }
  3112. func errstring(err error) string {
  3113. if err != nil {
  3114. return err.Error()
  3115. }
  3116. return ""
  3117. }