preprocessor_test.go 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132
  1. // Copyright 2021-2022 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package operator
  15. import (
  16. "encoding/base64"
  17. "encoding/json"
  18. "errors"
  19. "fmt"
  20. "github.com/lf-edge/ekuiper/internal/conf"
  21. "github.com/lf-edge/ekuiper/internal/converter"
  22. "github.com/lf-edge/ekuiper/internal/topo/context"
  23. "github.com/lf-edge/ekuiper/internal/xsql"
  24. "github.com/lf-edge/ekuiper/pkg/ast"
  25. "github.com/lf-edge/ekuiper/pkg/cast"
  26. "github.com/lf-edge/ekuiper/pkg/message"
  27. "io/ioutil"
  28. "log"
  29. "path"
  30. "reflect"
  31. "testing"
  32. "time"
  33. )
  34. func TestPreprocessor_Apply(t *testing.T) {
  35. var tests = []struct {
  36. stmt *ast.StreamStmt
  37. data []byte
  38. result interface{}
  39. }{
  40. //Basic type
  41. {
  42. stmt: &ast.StreamStmt{
  43. Name: ast.StreamName("demo"),
  44. StreamFields: []ast.StreamField{
  45. {Name: "abc", FieldType: &ast.BasicType{Type: ast.BIGINT}},
  46. },
  47. },
  48. data: []byte(`{"a": 6}`),
  49. result: errors.New("error in preprocessor: invalid data map[a:%!s(float64=6)], field abc not found"),
  50. },
  51. {
  52. stmt: &ast.StreamStmt{
  53. Name: ast.StreamName("demo"),
  54. StreamFields: []ast.StreamField{
  55. {Name: "abc", FieldType: &ast.BasicType{Type: ast.BIGINT}},
  56. },
  57. },
  58. data: []byte(`{"abc": null}`),
  59. result: errors.New("error in preprocessor: invalid data type for abc, expect bigint but found <nil>(<nil>)"),
  60. },
  61. {
  62. stmt: &ast.StreamStmt{
  63. Name: ast.StreamName("demo"),
  64. StreamFields: nil,
  65. },
  66. data: []byte(`{"a": 6}`),
  67. result: &xsql.Tuple{Message: xsql.Message{
  68. "a": float64(6),
  69. },
  70. },
  71. },
  72. {
  73. stmt: &ast.StreamStmt{
  74. Name: ast.StreamName("demo"),
  75. StreamFields: []ast.StreamField{
  76. {Name: "abc", FieldType: &ast.BasicType{Type: ast.BIGINT}},
  77. },
  78. },
  79. data: []byte(`{"abc": 6}`),
  80. result: &xsql.Tuple{Message: xsql.Message{
  81. "abc": 6,
  82. },
  83. },
  84. },
  85. {
  86. stmt: &ast.StreamStmt{
  87. Name: ast.StreamName("demo"),
  88. StreamFields: nil,
  89. },
  90. data: []byte(`{"abc": 6}`),
  91. result: &xsql.Tuple{Message: xsql.Message{
  92. "abc": float64(6),
  93. },
  94. },
  95. },
  96. {
  97. stmt: &ast.StreamStmt{
  98. Name: ast.StreamName("demo"),
  99. StreamFields: []ast.StreamField{
  100. {Name: "abc", FieldType: &ast.BasicType{Type: ast.FLOAT}},
  101. {Name: "dEf", FieldType: &ast.BasicType{Type: ast.STRINGS}},
  102. },
  103. },
  104. data: []byte(`{"abc": 34, "def" : "hello", "ghi": 50}`),
  105. result: &xsql.Tuple{Message: xsql.Message{
  106. "abc": float64(34),
  107. "dEf": "hello",
  108. },
  109. },
  110. },
  111. {
  112. stmt: &ast.StreamStmt{
  113. Name: ast.StreamName("demo"),
  114. StreamFields: nil,
  115. },
  116. data: []byte(`{"abc": 34, "def" : "hello", "ghi": 50}`),
  117. result: &xsql.Tuple{Message: xsql.Message{
  118. "abc": float64(34),
  119. "def": "hello",
  120. "ghi": float64(50),
  121. },
  122. },
  123. },
  124. {
  125. stmt: &ast.StreamStmt{
  126. Name: ast.StreamName("demo"),
  127. StreamFields: []ast.StreamField{
  128. {Name: "abc", FieldType: &ast.BasicType{Type: ast.FLOAT}},
  129. {Name: "def", FieldType: &ast.BasicType{Type: ast.STRINGS}},
  130. },
  131. },
  132. data: []byte(`{"abc": "34", "def" : "hello", "ghi": "50"}`),
  133. result: &xsql.Tuple{Message: xsql.Message{
  134. "abc": float64(34),
  135. "def": "hello",
  136. },
  137. },
  138. },
  139. {
  140. stmt: &ast.StreamStmt{
  141. Name: ast.StreamName("demo"),
  142. StreamFields: []ast.StreamField{
  143. {Name: "abc", FieldType: &ast.BasicType{Type: ast.FLOAT}},
  144. {Name: "def", FieldType: &ast.BasicType{Type: ast.BOOLEAN}},
  145. },
  146. },
  147. data: []byte(`{"abc": 77, "def" : "hello"}`),
  148. result: errors.New("error in preprocessor: invalid data type for def, expect boolean but found string(hello)"),
  149. },
  150. {
  151. stmt: &ast.StreamStmt{
  152. Name: ast.StreamName("demo"),
  153. StreamFields: []ast.StreamField{
  154. {Name: "abc", FieldType: &ast.BasicType{Type: ast.FLOAT}},
  155. {Name: "def", FieldType: &ast.BasicType{Type: ast.BOOLEAN}},
  156. },
  157. },
  158. data: []byte(`{"a": {"b" : "hello"}}`),
  159. result: errors.New("error in preprocessor: invalid data map[a:map[b:hello]], field abc not found"),
  160. },
  161. {
  162. stmt: &ast.StreamStmt{
  163. Name: ast.StreamName("demo"),
  164. StreamFields: nil,
  165. },
  166. data: []byte(`{"a": {"b" : "hello"}}`),
  167. result: &xsql.Tuple{Message: xsql.Message{
  168. "a": map[string]interface{}{
  169. "b": "hello",
  170. },
  171. },
  172. },
  173. },
  174. //Rec type
  175. {
  176. stmt: &ast.StreamStmt{
  177. Name: ast.StreamName("demo"),
  178. StreamFields: []ast.StreamField{
  179. {Name: "a", FieldType: &ast.RecType{
  180. StreamFields: []ast.StreamField{
  181. {Name: "b", FieldType: &ast.BasicType{Type: ast.STRINGS}},
  182. },
  183. }},
  184. },
  185. },
  186. data: []byte(`{"a": {"b" : "hello"}}`),
  187. result: &xsql.Tuple{Message: xsql.Message{
  188. "a": map[string]interface{}{
  189. "b": "hello",
  190. },
  191. },
  192. },
  193. },
  194. {
  195. stmt: &ast.StreamStmt{
  196. Name: ast.StreamName("demo"),
  197. StreamFields: []ast.StreamField{
  198. {Name: "a", FieldType: &ast.RecType{
  199. StreamFields: []ast.StreamField{
  200. {Name: "b", FieldType: &ast.BasicType{Type: ast.FLOAT}},
  201. },
  202. }},
  203. },
  204. },
  205. data: []byte(`{"a": "{\"b\" : \"32\"}"}`),
  206. result: &xsql.Tuple{Message: xsql.Message{
  207. "a": map[string]interface{}{
  208. "b": float64(32),
  209. },
  210. },
  211. },
  212. },
  213. {
  214. stmt: &ast.StreamStmt{
  215. Name: ast.StreamName("demo"),
  216. StreamFields: nil,
  217. },
  218. data: []byte(`{"a": {"b" : "32"}}`),
  219. result: &xsql.Tuple{Message: xsql.Message{
  220. "a": map[string]interface{}{
  221. "b": "32",
  222. },
  223. },
  224. },
  225. },
  226. //Array of complex type
  227. {
  228. stmt: &ast.StreamStmt{
  229. Name: ast.StreamName("demo"),
  230. StreamFields: []ast.StreamField{
  231. {Name: "a", FieldType: &ast.ArrayType{
  232. Type: ast.STRUCT,
  233. FieldType: &ast.RecType{
  234. StreamFields: []ast.StreamField{
  235. {Name: "b", FieldType: &ast.BasicType{Type: ast.STRINGS}},
  236. },
  237. },
  238. }},
  239. },
  240. },
  241. data: []byte(`{"a": [{"b" : "hello1"}, {"b" : "hello2"}]}`),
  242. result: &xsql.Tuple{Message: xsql.Message{
  243. "a": []map[string]interface{}{
  244. {"b": "hello1"},
  245. {"b": "hello2"},
  246. },
  247. },
  248. },
  249. },
  250. {
  251. stmt: &ast.StreamStmt{
  252. Name: ast.StreamName("demo"),
  253. StreamFields: []ast.StreamField{
  254. {Name: "a", FieldType: &ast.ArrayType{
  255. Type: ast.STRUCT,
  256. FieldType: &ast.RecType{
  257. StreamFields: []ast.StreamField{
  258. {Name: "b", FieldType: &ast.BasicType{Type: ast.STRINGS}},
  259. },
  260. },
  261. }},
  262. },
  263. },
  264. data: []byte(`{"a": []}`),
  265. result: &xsql.Tuple{Message: xsql.Message{
  266. "a": make([]map[string]interface{}, 0),
  267. },
  268. },
  269. },
  270. {
  271. stmt: &ast.StreamStmt{
  272. Name: ast.StreamName("demo"),
  273. StreamFields: []ast.StreamField{
  274. {Name: "a", FieldType: &ast.ArrayType{
  275. Type: ast.STRUCT,
  276. FieldType: &ast.RecType{
  277. StreamFields: []ast.StreamField{
  278. {Name: "b", FieldType: &ast.BasicType{Type: ast.STRINGS}},
  279. },
  280. },
  281. }},
  282. },
  283. },
  284. data: []byte(`{"a": null}`),
  285. result: &xsql.Tuple{Message: xsql.Message{
  286. "a": []map[string]interface{}(nil),
  287. },
  288. },
  289. },
  290. {
  291. stmt: &ast.StreamStmt{
  292. Name: ast.StreamName("demo"),
  293. StreamFields: []ast.StreamField{
  294. {Name: "a", FieldType: &ast.ArrayType{
  295. Type: ast.STRUCT,
  296. FieldType: &ast.RecType{
  297. StreamFields: []ast.StreamField{
  298. {Name: "b", FieldType: &ast.BasicType{Type: ast.STRINGS}},
  299. },
  300. },
  301. }},
  302. },
  303. },
  304. data: []byte(`{"a": [null, {"b" : "hello2"}]}`),
  305. result: &xsql.Tuple{Message: xsql.Message{
  306. "a": []map[string]interface{}{
  307. nil,
  308. {"b": "hello2"},
  309. },
  310. },
  311. },
  312. },
  313. {
  314. stmt: &ast.StreamStmt{
  315. Name: ast.StreamName("demo"),
  316. StreamFields: []ast.StreamField{
  317. {Name: "a", FieldType: &ast.ArrayType{
  318. Type: ast.ARRAY,
  319. FieldType: &ast.ArrayType{
  320. Type: ast.BIGINT,
  321. },
  322. }},
  323. },
  324. },
  325. data: []byte(`{"a": [[50, 60, 70],[66], [77]]}`),
  326. result: &xsql.Tuple{Message: xsql.Message{
  327. "a": [][]int{
  328. {50, 60, 70},
  329. {66},
  330. {77},
  331. },
  332. },
  333. },
  334. },
  335. {
  336. stmt: &ast.StreamStmt{
  337. Name: ast.StreamName("demo"),
  338. StreamFields: []ast.StreamField{
  339. {Name: "a", FieldType: &ast.ArrayType{
  340. Type: ast.ARRAY,
  341. FieldType: &ast.ArrayType{
  342. Type: ast.BIGINT,
  343. },
  344. }},
  345. },
  346. },
  347. data: []byte(`{"a": [null, [66], [77]]}`),
  348. result: &xsql.Tuple{Message: xsql.Message{
  349. "a": [][]int{
  350. []int(nil),
  351. {66},
  352. {77},
  353. },
  354. },
  355. },
  356. },
  357. {
  358. stmt: &ast.StreamStmt{
  359. Name: ast.StreamName("demo"),
  360. StreamFields: nil,
  361. },
  362. data: []byte(`{"a": [{"b" : "hello1"}, {"b" : "hello2"}]}`),
  363. result: &xsql.Tuple{Message: xsql.Message{
  364. "a": []interface{}{
  365. map[string]interface{}{"b": "hello1"},
  366. map[string]interface{}{"b": "hello2"},
  367. },
  368. },
  369. },
  370. },
  371. {
  372. stmt: &ast.StreamStmt{
  373. Name: ast.StreamName("demo"),
  374. StreamFields: []ast.StreamField{
  375. {Name: "a", FieldType: &ast.ArrayType{
  376. Type: ast.FLOAT,
  377. }},
  378. },
  379. },
  380. data: []byte(`{"a": "[\"55\", \"77\"]"}`),
  381. result: &xsql.Tuple{Message: xsql.Message{
  382. "a": []float64{
  383. 55,
  384. 77,
  385. },
  386. },
  387. },
  388. },
  389. {
  390. stmt: &ast.StreamStmt{
  391. Name: ast.StreamName("demo"),
  392. StreamFields: nil,
  393. },
  394. data: []byte(`{"a": [55, 77]}`),
  395. result: &xsql.Tuple{Message: xsql.Message{
  396. "a": []interface{}{
  397. float64(55),
  398. float64(77),
  399. },
  400. },
  401. },
  402. },
  403. //Rec of complex type
  404. {
  405. stmt: &ast.StreamStmt{
  406. Name: ast.StreamName("demo"),
  407. StreamFields: []ast.StreamField{
  408. {Name: "a", FieldType: &ast.RecType{
  409. StreamFields: []ast.StreamField{
  410. {Name: "b", FieldType: &ast.BasicType{Type: ast.STRINGS}},
  411. {Name: "c", FieldType: &ast.RecType{
  412. StreamFields: []ast.StreamField{
  413. {Name: "d", FieldType: &ast.BasicType{Type: ast.BIGINT}},
  414. },
  415. }},
  416. },
  417. }},
  418. },
  419. },
  420. data: []byte(`{"a": {"b" : "hello", "c": {"d": 35.2}}}`),
  421. result: &xsql.Tuple{Message: xsql.Message{
  422. "a": map[string]interface{}{
  423. "b": "hello",
  424. "c": map[string]interface{}{
  425. "d": 35,
  426. },
  427. },
  428. },
  429. },
  430. },
  431. {
  432. stmt: &ast.StreamStmt{
  433. Name: ast.StreamName("demo"),
  434. StreamFields: []ast.StreamField{
  435. {Name: "a", FieldType: &ast.RecType{
  436. StreamFields: []ast.StreamField{
  437. {Name: "b", FieldType: &ast.BasicType{Type: ast.STRINGS}},
  438. {Name: "c", FieldType: &ast.RecType{
  439. StreamFields: []ast.StreamField{
  440. {Name: "d", FieldType: &ast.BasicType{Type: ast.BIGINT}},
  441. },
  442. }},
  443. },
  444. }},
  445. },
  446. },
  447. data: []byte(`{"a": null}`),
  448. result: &xsql.Tuple{Message: xsql.Message{
  449. "a": map[string]interface{}(nil),
  450. },
  451. },
  452. },
  453. {
  454. stmt: &ast.StreamStmt{
  455. Name: ast.StreamName("demo"),
  456. StreamFields: []ast.StreamField{
  457. {Name: "a", FieldType: &ast.RecType{
  458. StreamFields: []ast.StreamField{
  459. {Name: "b", FieldType: &ast.BasicType{Type: ast.STRINGS}},
  460. {Name: "c", FieldType: &ast.ArrayType{
  461. Type: ast.FLOAT,
  462. }},
  463. },
  464. }},
  465. },
  466. },
  467. data: []byte(`{"a": {"b" : "hello", "c": [35.2, 38.2]}}`),
  468. result: &xsql.Tuple{Message: xsql.Message{
  469. "a": map[string]interface{}{
  470. "b": "hello",
  471. "c": []float64{
  472. 35.2, 38.2,
  473. },
  474. },
  475. },
  476. },
  477. },
  478. {
  479. stmt: &ast.StreamStmt{
  480. Name: ast.StreamName("demo"),
  481. StreamFields: []ast.StreamField{
  482. {Name: "a", FieldType: &ast.RecType{
  483. StreamFields: []ast.StreamField{
  484. {Name: "b", FieldType: &ast.BasicType{Type: ast.STRINGS}},
  485. {Name: "c", FieldType: &ast.ArrayType{
  486. Type: ast.FLOAT,
  487. }},
  488. },
  489. }},
  490. },
  491. },
  492. data: []byte(`{"a": {"b" : "hello", "c": null}}`),
  493. result: &xsql.Tuple{Message: xsql.Message{
  494. "a": map[string]interface{}{
  495. "b": "hello",
  496. "c": []float64(nil),
  497. },
  498. },
  499. },
  500. },
  501. {
  502. stmt: &ast.StreamStmt{
  503. Name: ast.StreamName("demo"),
  504. StreamFields: []ast.StreamField{
  505. {Name: "a", FieldType: &ast.RecType{
  506. StreamFields: []ast.StreamField{
  507. {Name: "b", FieldType: &ast.BasicType{Type: ast.STRINGS}},
  508. {Name: "c", FieldType: &ast.ArrayType{
  509. Type: ast.FLOAT,
  510. }},
  511. },
  512. }},
  513. },
  514. },
  515. data: []byte(`{"a": {"b" : "hello", "c": [null, 35.4]}}`),
  516. result: errors.New("error in preprocessor: fail to parse field c: invalid data type for [0], expect float but found <nil>(<nil>)"),
  517. },
  518. {
  519. stmt: &ast.StreamStmt{
  520. Name: ast.StreamName("demo"),
  521. StreamFields: nil,
  522. },
  523. data: []byte(`{"a": {"b" : "hello", "c": {"d": 35.2}}}`),
  524. result: &xsql.Tuple{Message: xsql.Message{
  525. "a": map[string]interface{}{
  526. "b": "hello",
  527. "c": map[string]interface{}{
  528. "d": 35.2,
  529. },
  530. },
  531. },
  532. },
  533. }, {
  534. stmt: &ast.StreamStmt{
  535. Name: ast.StreamName("demo"),
  536. StreamFields: []ast.StreamField{
  537. {Name: "a", FieldType: &ast.RecType{
  538. StreamFields: []ast.StreamField{
  539. {Name: "b", FieldType: &ast.BasicType{Type: ast.STRINGS}},
  540. },
  541. }},
  542. {Name: "b", FieldType: &ast.BasicType{Type: ast.FLOAT}},
  543. {Name: "c", FieldType: &ast.ArrayType{Type: ast.BIGINT}},
  544. },
  545. Options: &ast.Options{
  546. STRICT_VALIDATION: false,
  547. },
  548. },
  549. data: []byte(`{"a": {"d" : "hello"}}`),
  550. result: &xsql.Tuple{Message: xsql.Message{
  551. "a": map[string]interface{}{
  552. "b": "",
  553. },
  554. "b": 0.0,
  555. "c": []int(nil),
  556. },
  557. },
  558. },
  559. }
  560. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  561. defer conf.CloseLogger()
  562. contextLogger := conf.Log.WithField("rule", "TestPreprocessor_Apply")
  563. ctx := context.WithValue(context.Background(), context.LoggerKey, contextLogger)
  564. for i, tt := range tests {
  565. pp := &Preprocessor{}
  566. if tt.stmt.Options != nil {
  567. pp.strictValidation = tt.stmt.Options.STRICT_VALIDATION
  568. } else {
  569. pp.strictValidation = true
  570. }
  571. pp.streamFields = convertFields(tt.stmt.StreamFields)
  572. dm := make(map[string]interface{})
  573. if e := json.Unmarshal(tt.data, &dm); e != nil {
  574. log.Fatal(e)
  575. return
  576. } else {
  577. tuple := &xsql.Tuple{Message: dm}
  578. fv, afv := xsql.NewFunctionValuersForOp(nil)
  579. result := pp.Apply(ctx, tuple, fv, afv)
  580. if !reflect.DeepEqual(tt.result, result) {
  581. t.Errorf("%d. %q\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tuple, tt.result, result)
  582. }
  583. }
  584. }
  585. }
  586. func TestPreprocessorTime_Apply(t *testing.T) {
  587. var tests = []struct {
  588. stmt *ast.StreamStmt
  589. data []byte
  590. result interface{}
  591. }{
  592. {
  593. stmt: &ast.StreamStmt{
  594. Name: ast.StreamName("demo"),
  595. StreamFields: []ast.StreamField{
  596. {Name: "abc", FieldType: &ast.BasicType{Type: ast.DATETIME}},
  597. {Name: "def", FieldType: &ast.BasicType{Type: ast.DATETIME}},
  598. },
  599. },
  600. data: []byte(`{"abc": "2019-09-19T00:55:15.000Z", "def" : 1568854573431}`),
  601. result: &xsql.Tuple{Message: xsql.Message{
  602. "abc": cast.TimeFromUnixMilli(1568854515000),
  603. "def": cast.TimeFromUnixMilli(1568854573431),
  604. },
  605. },
  606. },
  607. {
  608. stmt: &ast.StreamStmt{
  609. Name: ast.StreamName("demo"),
  610. StreamFields: nil,
  611. },
  612. data: []byte(`{"abc": "2019-09-19T00:55:15.000Z", "def" : 1568854573431}`),
  613. result: &xsql.Tuple{Message: xsql.Message{
  614. "abc": "2019-09-19T00:55:15.000Z",
  615. "def": float64(1568854573431),
  616. },
  617. },
  618. },
  619. {
  620. stmt: &ast.StreamStmt{
  621. Name: ast.StreamName("demo"),
  622. StreamFields: []ast.StreamField{
  623. {Name: "abc", FieldType: &ast.BasicType{Type: ast.DATETIME}},
  624. {Name: "def", FieldType: &ast.BasicType{Type: ast.DATETIME}},
  625. },
  626. },
  627. data: []byte(`{"abc": "2019-09-19T00:55:1dd5Z", "def" : 111568854573431}`),
  628. result: errors.New("error in preprocessor: invalid data type for abc, cannot convert to datetime: parsing time \"2019-09-19T00:55:1dd5Z\" as \"2006-01-02T15:04:05.000Z07:00\": cannot parse \"1dd5Z\" as \"05\""),
  629. },
  630. {
  631. stmt: &ast.StreamStmt{
  632. Name: ast.StreamName("demo"),
  633. StreamFields: []ast.StreamField{
  634. {Name: "abc", FieldType: &ast.BasicType{Type: ast.DATETIME}},
  635. {Name: "def", FieldType: &ast.BasicType{Type: ast.DATETIME}},
  636. },
  637. Options: &ast.Options{
  638. DATASOURCE: "users",
  639. FORMAT: "JSON",
  640. KEY: "USERID",
  641. CONF_KEY: "srv1",
  642. TYPE: "MQTT",
  643. TIMESTAMP: "USERID",
  644. TIMESTAMP_FORMAT: "yyyy-MM-dd 'at' HH:mm:ss'Z'X",
  645. },
  646. },
  647. data: []byte(`{"abc": "2019-09-19 at 18:55:15Z+07", "def" : 1568854573431}`),
  648. result: &xsql.Tuple{Message: xsql.Message{
  649. "abc": cast.TimeFromUnixMilli(1568894115000),
  650. "def": cast.TimeFromUnixMilli(1568854573431),
  651. }},
  652. },
  653. //Array type
  654. {
  655. stmt: &ast.StreamStmt{
  656. Name: ast.StreamName("demo"),
  657. StreamFields: []ast.StreamField{
  658. {Name: "a", FieldType: &ast.ArrayType{
  659. Type: ast.DATETIME,
  660. }},
  661. },
  662. },
  663. data: []byte(`{"a": [1568854515123, 1568854573431]}`),
  664. result: &xsql.Tuple{Message: xsql.Message{
  665. "a": []time.Time{
  666. cast.TimeFromUnixMilli(1568854515123),
  667. cast.TimeFromUnixMilli(1568854573431),
  668. },
  669. },
  670. },
  671. },
  672. {
  673. stmt: &ast.StreamStmt{
  674. Name: ast.StreamName("demo"),
  675. StreamFields: []ast.StreamField{
  676. {Name: "a", FieldType: &ast.RecType{
  677. StreamFields: []ast.StreamField{
  678. {Name: "b", FieldType: &ast.BasicType{Type: ast.STRINGS}},
  679. {Name: "c", FieldType: &ast.BasicType{Type: ast.DATETIME}},
  680. },
  681. }},
  682. },
  683. },
  684. data: []byte(`{"a": {"b" : "hello", "c": 1568854515000}}`),
  685. result: &xsql.Tuple{Message: xsql.Message{
  686. "a": map[string]interface{}{
  687. "b": "hello",
  688. "c": cast.TimeFromUnixMilli(1568854515000),
  689. },
  690. },
  691. },
  692. },
  693. }
  694. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  695. defer conf.CloseLogger()
  696. contextLogger := conf.Log.WithField("rule", "TestPreprocessorTime_Apply")
  697. ctx := context.WithValue(context.Background(), context.LoggerKey, contextLogger)
  698. for i, tt := range tests {
  699. pp := &Preprocessor{}
  700. pp.streamFields = convertFields(tt.stmt.StreamFields)
  701. if tt.stmt.Options != nil {
  702. pp.timestampFormat = tt.stmt.Options.TIMESTAMP_FORMAT
  703. }
  704. dm := make(map[string]interface{})
  705. if e := json.Unmarshal(tt.data, &dm); e != nil {
  706. log.Fatal(e)
  707. return
  708. } else {
  709. tuple := &xsql.Tuple{Message: dm}
  710. fv, afv := xsql.NewFunctionValuersForOp(nil)
  711. result := pp.Apply(ctx, tuple, fv, afv)
  712. //workaround make sure all the timezone are the same for time vars or the DeepEqual will be false.
  713. if rt, ok := result.(*xsql.Tuple); ok {
  714. if rtt, ok := rt.Message["abc"].(time.Time); ok {
  715. rt.Message["abc"] = rtt.UTC()
  716. }
  717. }
  718. if !reflect.DeepEqual(tt.result, result) {
  719. t.Errorf("%d. %q\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tuple, tt.result, result)
  720. }
  721. }
  722. }
  723. }
  724. func convertFields(o ast.StreamFields) []interface{} {
  725. if o == nil {
  726. return nil
  727. }
  728. fields := make([]interface{}, len(o))
  729. for i := range o {
  730. fields[i] = &o[i]
  731. }
  732. return fields
  733. }
  734. func TestPreprocessorEventtime_Apply(t *testing.T) {
  735. var tests = []struct {
  736. stmt *ast.StreamStmt
  737. data []byte
  738. result interface{}
  739. }{
  740. //Basic type
  741. {
  742. stmt: &ast.StreamStmt{
  743. Name: ast.StreamName("demo"),
  744. StreamFields: []ast.StreamField{
  745. {Name: "abc", FieldType: &ast.BasicType{Type: ast.BIGINT}},
  746. },
  747. Options: &ast.Options{
  748. DATASOURCE: "users",
  749. FORMAT: "JSON",
  750. KEY: "USERID",
  751. CONF_KEY: "srv1",
  752. TYPE: "MQTT",
  753. TIMESTAMP: "abc",
  754. TIMESTAMP_FORMAT: "yyyy-MM-dd''T''HH:mm:ssX'",
  755. },
  756. },
  757. data: []byte(`{"abc": 1568854515000}`),
  758. result: &xsql.Tuple{Message: xsql.Message{
  759. "abc": 1568854515000,
  760. }, Timestamp: 1568854515000,
  761. },
  762. },
  763. {
  764. stmt: &ast.StreamStmt{
  765. Name: ast.StreamName("demo"),
  766. StreamFields: nil,
  767. Options: &ast.Options{
  768. DATASOURCE: "users",
  769. FORMAT: "JSON",
  770. KEY: "USERID",
  771. CONF_KEY: "srv1",
  772. TYPE: "MQTT",
  773. TIMESTAMP: "abc",
  774. TIMESTAMP_FORMAT: "yyyy-MM-dd''T''HH:mm:ssX'",
  775. },
  776. },
  777. data: []byte(`{"abc": 1568854515000}`),
  778. result: &xsql.Tuple{Message: xsql.Message{
  779. "abc": float64(1568854515000),
  780. }, Timestamp: 1568854515000,
  781. },
  782. },
  783. {
  784. stmt: &ast.StreamStmt{
  785. Name: ast.StreamName("demo"),
  786. StreamFields: []ast.StreamField{
  787. {Name: "abc", FieldType: &ast.BasicType{Type: ast.BOOLEAN}},
  788. },
  789. Options: &ast.Options{
  790. DATASOURCE: "users",
  791. TIMESTAMP: "abc",
  792. },
  793. },
  794. data: []byte(`{"abc": true}`),
  795. result: errors.New("cannot convert timestamp field abc to timestamp with error unsupported type to convert to timestamp true"),
  796. },
  797. {
  798. stmt: &ast.StreamStmt{
  799. Name: ast.StreamName("demo"),
  800. StreamFields: []ast.StreamField{
  801. {Name: "abc", FieldType: &ast.BasicType{Type: ast.FLOAT}},
  802. {Name: "def", FieldType: &ast.BasicType{Type: ast.STRINGS}},
  803. },
  804. Options: &ast.Options{
  805. DATASOURCE: "users",
  806. TIMESTAMP: "def",
  807. },
  808. },
  809. data: []byte(`{"abc": 34, "def" : "2019-09-23T02:47:29.754Z", "ghi": 50}`),
  810. result: &xsql.Tuple{Message: xsql.Message{
  811. "abc": float64(34),
  812. "def": "2019-09-23T02:47:29.754Z",
  813. }, Timestamp: int64(1569206849754),
  814. },
  815. },
  816. {
  817. stmt: &ast.StreamStmt{
  818. Name: ast.StreamName("demo"),
  819. StreamFields: []ast.StreamField{
  820. {Name: "abc", FieldType: &ast.BasicType{Type: ast.DATETIME}},
  821. {Name: "def", FieldType: &ast.BasicType{Type: ast.DATETIME}},
  822. },
  823. Options: &ast.Options{
  824. DATASOURCE: "users",
  825. TIMESTAMP: "abc",
  826. },
  827. },
  828. data: []byte(`{"abc": "2019-09-19T00:55:15.000Z", "def" : 1568854573431}`),
  829. result: &xsql.Tuple{Message: xsql.Message{
  830. "abc": cast.TimeFromUnixMilli(1568854515000),
  831. "def": cast.TimeFromUnixMilli(1568854573431),
  832. }, Timestamp: int64(1568854515000),
  833. },
  834. },
  835. {
  836. stmt: &ast.StreamStmt{
  837. Name: ast.StreamName("demo"),
  838. StreamFields: []ast.StreamField{
  839. {Name: "abc", FieldType: &ast.BasicType{Type: ast.FLOAT}},
  840. {Name: "def", FieldType: &ast.BasicType{Type: ast.STRINGS}},
  841. },
  842. Options: &ast.Options{
  843. DATASOURCE: "users",
  844. TIMESTAMP: "def",
  845. TIMESTAMP_FORMAT: "yyyy-MM-dd'AT'HH:mm:ss",
  846. },
  847. },
  848. data: []byte(`{"abc": 34, "def" : "2019-09-23AT02:47:29", "ghi": 50}`),
  849. result: &xsql.Tuple{Message: xsql.Message{
  850. "abc": float64(34),
  851. "def": "2019-09-23AT02:47:29",
  852. }, Timestamp: int64(1569206849000),
  853. },
  854. },
  855. {
  856. stmt: &ast.StreamStmt{
  857. Name: ast.StreamName("demo"),
  858. StreamFields: []ast.StreamField{
  859. {Name: "abc", FieldType: &ast.BasicType{Type: ast.FLOAT}},
  860. {Name: "def", FieldType: &ast.BasicType{Type: ast.STRINGS}},
  861. },
  862. Options: &ast.Options{
  863. DATASOURCE: "users",
  864. TIMESTAMP: "def",
  865. TIMESTAMP_FORMAT: "yyyy-MM-ddaHH:mm:ss",
  866. },
  867. },
  868. data: []byte(`{"abc": 34, "def" : "2019-09-23AT02:47:29", "ghi": 50}`),
  869. result: errors.New("cannot convert timestamp field def to timestamp with error parsing time \"2019-09-23AT02:47:29\" as \"2006-01-02PM15:04:05\": cannot parse \"02:47:29\" as \"PM\""),
  870. },
  871. }
  872. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  873. defer conf.CloseLogger()
  874. contextLogger := conf.Log.WithField("rule", "TestPreprocessorEventtime_Apply")
  875. ctx := context.WithValue(context.Background(), context.LoggerKey, contextLogger)
  876. for i, tt := range tests {
  877. pp := &Preprocessor{
  878. defaultFieldProcessor: defaultFieldProcessor{
  879. streamFields: convertFields(tt.stmt.StreamFields),
  880. isBinary: false,
  881. timestampFormat: tt.stmt.Options.TIMESTAMP_FORMAT,
  882. },
  883. isEventTime: true,
  884. timestampField: tt.stmt.Options.TIMESTAMP,
  885. }
  886. dm := make(map[string]interface{})
  887. if e := json.Unmarshal(tt.data, &dm); e != nil {
  888. log.Fatal(e)
  889. return
  890. } else {
  891. tuple := &xsql.Tuple{Message: dm}
  892. fv, afv := xsql.NewFunctionValuersForOp(nil)
  893. result := pp.Apply(ctx, tuple, fv, afv)
  894. //workaround make sure all the timezone are the same for time vars or the DeepEqual will be false.
  895. if rt, ok := result.(*xsql.Tuple); ok {
  896. if rtt, ok := rt.Message["abc"].(time.Time); ok {
  897. rt.Message["abc"] = rtt.UTC()
  898. }
  899. }
  900. if !reflect.DeepEqual(tt.result, result) {
  901. t.Errorf("%d. %q\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tuple, tt.result, result)
  902. }
  903. }
  904. }
  905. }
  906. func TestPreprocessorError(t *testing.T) {
  907. tests := []struct {
  908. stmt *ast.StreamStmt
  909. data []byte
  910. result interface{}
  911. }{
  912. {
  913. stmt: &ast.StreamStmt{
  914. Name: ast.StreamName("demo"),
  915. StreamFields: []ast.StreamField{
  916. {Name: "abc", FieldType: &ast.BasicType{Type: ast.BIGINT}},
  917. },
  918. },
  919. data: []byte(`{"abc": "dafsad"}`),
  920. result: errors.New("error in preprocessor: invalid data type for abc, expect bigint but found string(dafsad)"),
  921. }, {
  922. stmt: &ast.StreamStmt{
  923. Name: ast.StreamName("demo"),
  924. StreamFields: []ast.StreamField{
  925. {Name: "a", FieldType: &ast.RecType{
  926. StreamFields: []ast.StreamField{
  927. {Name: "b", FieldType: &ast.BasicType{Type: ast.STRINGS}},
  928. },
  929. }},
  930. },
  931. },
  932. data: []byte(`{"a": {"d" : "hello"}}`),
  933. result: errors.New("error in preprocessor: invalid data map[d:hello], field b not found"),
  934. }, {
  935. stmt: &ast.StreamStmt{
  936. Name: ast.StreamName("demo"),
  937. StreamFields: []ast.StreamField{
  938. {Name: "abc", FieldType: &ast.BasicType{Type: ast.BIGINT}},
  939. },
  940. Options: &ast.Options{
  941. DATASOURCE: "users",
  942. FORMAT: "JSON",
  943. KEY: "USERID",
  944. CONF_KEY: "srv1",
  945. TYPE: "MQTT",
  946. TIMESTAMP: "abc",
  947. TIMESTAMP_FORMAT: "yyyy-MM-dd''T''HH:mm:ssX'",
  948. },
  949. },
  950. data: []byte(`{"abc": "not a time"}`),
  951. result: errors.New("error in preprocessor: invalid data type for abc, expect bigint but found string(not a time)"),
  952. },
  953. }
  954. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  955. defer conf.CloseLogger()
  956. contextLogger := conf.Log.WithField("rule", "TestPreprocessorError")
  957. ctx := context.WithValue(context.Background(), context.LoggerKey, contextLogger)
  958. for i, tt := range tests {
  959. pp := &Preprocessor{}
  960. pp.strictValidation = true
  961. pp.streamFields = convertFields(tt.stmt.StreamFields)
  962. dm := make(map[string]interface{})
  963. if e := json.Unmarshal(tt.data, &dm); e != nil {
  964. log.Fatal(e)
  965. return
  966. } else {
  967. tuple := &xsql.Tuple{Message: dm}
  968. fv, afv := xsql.NewFunctionValuersForOp(nil)
  969. result := pp.Apply(ctx, tuple, fv, afv)
  970. if !reflect.DeepEqual(tt.result, result) {
  971. t.Errorf("%d. %q\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tuple, tt.result, result)
  972. }
  973. }
  974. }
  975. }
  976. func TestPreprocessorForBinary(t *testing.T) {
  977. docsFolder, err := conf.GetLoc("docs/")
  978. if err != nil {
  979. t.Errorf("Cannot find docs folder: %v", err)
  980. }
  981. image, err := ioutil.ReadFile(path.Join(docsFolder, "cover.jpg"))
  982. if err != nil {
  983. t.Errorf("Cannot read image: %v", err)
  984. }
  985. b64img := base64.StdEncoding.EncodeToString(image)
  986. //TODO test bytea type conversion to string or else
  987. var tests = []struct {
  988. stmt *ast.StreamStmt
  989. data []byte
  990. isBinary bool
  991. result interface{}
  992. }{
  993. {
  994. stmt: &ast.StreamStmt{
  995. Name: ast.StreamName("demo"),
  996. StreamFields: nil,
  997. },
  998. data: image,
  999. isBinary: true,
  1000. result: &xsql.Tuple{Message: xsql.Message{
  1001. "self": image,
  1002. },
  1003. },
  1004. },
  1005. {
  1006. stmt: &ast.StreamStmt{
  1007. Name: ast.StreamName("demo"),
  1008. StreamFields: []ast.StreamField{
  1009. {Name: "img", FieldType: &ast.BasicType{Type: ast.BYTEA}},
  1010. },
  1011. },
  1012. data: image,
  1013. isBinary: true,
  1014. result: &xsql.Tuple{Message: xsql.Message{
  1015. "img": image,
  1016. },
  1017. },
  1018. },
  1019. {
  1020. stmt: &ast.StreamStmt{
  1021. Name: ast.StreamName("demo"),
  1022. StreamFields: []ast.StreamField{
  1023. {Name: "a", FieldType: &ast.RecType{
  1024. StreamFields: []ast.StreamField{
  1025. {Name: "b", FieldType: &ast.BasicType{Type: ast.BYTEA}},
  1026. },
  1027. }},
  1028. },
  1029. },
  1030. data: []byte(fmt.Sprintf(`{"a": {"b" : "%s"}}`, b64img)),
  1031. result: &xsql.Tuple{Message: xsql.Message{
  1032. "a": map[string]interface{}{
  1033. "b": image,
  1034. },
  1035. },
  1036. },
  1037. },
  1038. {
  1039. stmt: &ast.StreamStmt{
  1040. Name: ast.StreamName("demo"),
  1041. StreamFields: []ast.StreamField{
  1042. {Name: "a", FieldType: &ast.ArrayType{
  1043. Type: ast.BYTEA,
  1044. }},
  1045. },
  1046. },
  1047. data: []byte(fmt.Sprintf(`{"a": ["%s"]}`, b64img)),
  1048. result: &xsql.Tuple{Message: xsql.Message{
  1049. "a": [][]byte{
  1050. image,
  1051. },
  1052. },
  1053. },
  1054. },
  1055. {
  1056. stmt: &ast.StreamStmt{
  1057. Name: ast.StreamName("demo"),
  1058. StreamFields: []ast.StreamField{
  1059. {Name: "a", FieldType: &ast.ArrayType{
  1060. Type: ast.STRUCT,
  1061. FieldType: &ast.RecType{
  1062. StreamFields: []ast.StreamField{
  1063. {Name: "b", FieldType: &ast.BasicType{Type: ast.BYTEA}},
  1064. },
  1065. },
  1066. }},
  1067. },
  1068. },
  1069. data: []byte(fmt.Sprintf(`{"a": [{"b":"%s"}]}`, b64img)),
  1070. result: &xsql.Tuple{Message: xsql.Message{
  1071. "a": []map[string]interface{}{
  1072. {"b": image},
  1073. },
  1074. },
  1075. },
  1076. },
  1077. }
  1078. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  1079. defer conf.CloseLogger()
  1080. contextLogger := conf.Log.WithField("rule", "TestPreprocessorForBinary")
  1081. ctx := context.WithValue(context.Background(), context.LoggerKey, contextLogger)
  1082. for i, tt := range tests {
  1083. pp := &Preprocessor{}
  1084. pp.streamFields = convertFields(tt.stmt.StreamFields)
  1085. pp.isBinary = tt.isBinary
  1086. format := message.FormatJson
  1087. if tt.isBinary {
  1088. format = message.FormatBinary
  1089. }
  1090. converter, _ := converter.GetOrCreateConverter(format, "", "")
  1091. nCtx := context.WithValue(ctx, context.DecodeKey, converter)
  1092. if dm, e := nCtx.Decode(tt.data); e != nil {
  1093. log.Fatal(e)
  1094. return
  1095. } else {
  1096. tuple := &xsql.Tuple{Message: dm}
  1097. fv, afv := xsql.NewFunctionValuersForOp(nil)
  1098. result := pp.Apply(ctx, tuple, fv, afv)
  1099. if !reflect.DeepEqual(tt.result, result) {
  1100. t.Errorf("%d. %q\n\nresult mismatch", i, tuple)
  1101. }
  1102. }
  1103. }
  1104. }