preprocessor_test.go 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133
  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. "log"
  21. "os"
  22. "path"
  23. "reflect"
  24. "testing"
  25. "time"
  26. "github.com/lf-edge/ekuiper/internal/conf"
  27. "github.com/lf-edge/ekuiper/internal/converter"
  28. "github.com/lf-edge/ekuiper/internal/topo/context"
  29. "github.com/lf-edge/ekuiper/internal/xsql"
  30. "github.com/lf-edge/ekuiper/pkg/ast"
  31. "github.com/lf-edge/ekuiper/pkg/cast"
  32. "github.com/lf-edge/ekuiper/pkg/message"
  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 := os.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. }