1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129 |
- // Copyright 2021 EMQ Technologies Co., Ltd.
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- package operator
- import (
- "encoding/base64"
- "encoding/json"
- "errors"
- "fmt"
- "github.com/lf-edge/ekuiper/internal/conf"
- "github.com/lf-edge/ekuiper/internal/topo/context"
- "github.com/lf-edge/ekuiper/internal/xsql"
- "github.com/lf-edge/ekuiper/pkg/ast"
- "github.com/lf-edge/ekuiper/pkg/cast"
- "github.com/lf-edge/ekuiper/pkg/message"
- "io/ioutil"
- "log"
- "path"
- "reflect"
- "testing"
- "time"
- )
- func TestPreprocessor_Apply(t *testing.T) {
- var tests = []struct {
- stmt *ast.StreamStmt
- data []byte
- result interface{}
- }{
- //Basic type
- {
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "abc", FieldType: &ast.BasicType{Type: ast.BIGINT}},
- },
- },
- data: []byte(`{"a": 6}`),
- result: errors.New("error in preprocessor: invalid data map[a:%!s(float64=6)], field abc not found"),
- },
- {
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "abc", FieldType: &ast.BasicType{Type: ast.BIGINT}},
- },
- },
- data: []byte(`{"abc": null}`),
- result: errors.New("error in preprocessor: invalid data type for abc, expect bigint but found <nil>(<nil>)"),
- },
- {
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: nil,
- },
- data: []byte(`{"a": 6}`),
- result: &xsql.Tuple{Message: xsql.Message{
- "a": float64(6),
- },
- },
- },
- {
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "abc", FieldType: &ast.BasicType{Type: ast.BIGINT}},
- },
- },
- data: []byte(`{"abc": 6}`),
- result: &xsql.Tuple{Message: xsql.Message{
- "abc": 6,
- },
- },
- },
- {
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: nil,
- },
- data: []byte(`{"abc": 6}`),
- result: &xsql.Tuple{Message: xsql.Message{
- "abc": float64(6),
- },
- },
- },
- {
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "abc", FieldType: &ast.BasicType{Type: ast.FLOAT}},
- {Name: "def", FieldType: &ast.BasicType{Type: ast.STRINGS}},
- },
- },
- data: []byte(`{"abc": 34, "def" : "hello", "ghi": 50}`),
- result: &xsql.Tuple{Message: xsql.Message{
- "abc": float64(34),
- "def": "hello",
- },
- },
- },
- {
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: nil,
- },
- data: []byte(`{"abc": 34, "def" : "hello", "ghi": 50}`),
- result: &xsql.Tuple{Message: xsql.Message{
- "abc": float64(34),
- "def": "hello",
- "ghi": float64(50),
- },
- },
- },
- {
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "abc", FieldType: &ast.BasicType{Type: ast.FLOAT}},
- {Name: "def", FieldType: &ast.BasicType{Type: ast.STRINGS}},
- },
- },
- data: []byte(`{"abc": "34", "def" : "hello", "ghi": "50"}`),
- result: &xsql.Tuple{Message: xsql.Message{
- "abc": float64(34),
- "def": "hello",
- },
- },
- },
- {
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "abc", FieldType: &ast.BasicType{Type: ast.FLOAT}},
- {Name: "def", FieldType: &ast.BasicType{Type: ast.BOOLEAN}},
- },
- },
- data: []byte(`{"abc": 77, "def" : "hello"}`),
- result: errors.New("error in preprocessor: invalid data type for def, expect boolean but found string(hello)"),
- },
- {
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "abc", FieldType: &ast.BasicType{Type: ast.FLOAT}},
- {Name: "def", FieldType: &ast.BasicType{Type: ast.BOOLEAN}},
- },
- },
- data: []byte(`{"a": {"b" : "hello"}}`),
- result: errors.New("error in preprocessor: invalid data map[a:map[b:hello]], field abc not found"),
- },
- {
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: nil,
- },
- data: []byte(`{"a": {"b" : "hello"}}`),
- result: &xsql.Tuple{Message: xsql.Message{
- "a": map[string]interface{}{
- "b": "hello",
- },
- },
- },
- },
- //Rec type
- {
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "a", FieldType: &ast.RecType{
- StreamFields: []ast.StreamField{
- {Name: "b", FieldType: &ast.BasicType{Type: ast.STRINGS}},
- },
- }},
- },
- },
- data: []byte(`{"a": {"b" : "hello"}}`),
- result: &xsql.Tuple{Message: xsql.Message{
- "a": map[string]interface{}{
- "b": "hello",
- },
- },
- },
- },
- {
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "a", FieldType: &ast.RecType{
- StreamFields: []ast.StreamField{
- {Name: "b", FieldType: &ast.BasicType{Type: ast.FLOAT}},
- },
- }},
- },
- },
- data: []byte(`{"a": "{\"b\" : \"32\"}"}`),
- result: &xsql.Tuple{Message: xsql.Message{
- "a": map[string]interface{}{
- "b": float64(32),
- },
- },
- },
- },
- {
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: nil,
- },
- data: []byte(`{"a": {"b" : "32"}}`),
- result: &xsql.Tuple{Message: xsql.Message{
- "a": map[string]interface{}{
- "b": "32",
- },
- },
- },
- },
- //Array of complex type
- {
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "a", FieldType: &ast.ArrayType{
- Type: ast.STRUCT,
- FieldType: &ast.RecType{
- StreamFields: []ast.StreamField{
- {Name: "b", FieldType: &ast.BasicType{Type: ast.STRINGS}},
- },
- },
- }},
- },
- },
- data: []byte(`{"a": [{"b" : "hello1"}, {"b" : "hello2"}]}`),
- result: &xsql.Tuple{Message: xsql.Message{
- "a": []map[string]interface{}{
- {"b": "hello1"},
- {"b": "hello2"},
- },
- },
- },
- },
- {
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "a", FieldType: &ast.ArrayType{
- Type: ast.STRUCT,
- FieldType: &ast.RecType{
- StreamFields: []ast.StreamField{
- {Name: "b", FieldType: &ast.BasicType{Type: ast.STRINGS}},
- },
- },
- }},
- },
- },
- data: []byte(`{"a": []}`),
- result: &xsql.Tuple{Message: xsql.Message{
- "a": make([]map[string]interface{}, 0),
- },
- },
- },
- {
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "a", FieldType: &ast.ArrayType{
- Type: ast.STRUCT,
- FieldType: &ast.RecType{
- StreamFields: []ast.StreamField{
- {Name: "b", FieldType: &ast.BasicType{Type: ast.STRINGS}},
- },
- },
- }},
- },
- },
- data: []byte(`{"a": null}`),
- result: &xsql.Tuple{Message: xsql.Message{
- "a": []map[string]interface{}(nil),
- },
- },
- },
- {
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "a", FieldType: &ast.ArrayType{
- Type: ast.STRUCT,
- FieldType: &ast.RecType{
- StreamFields: []ast.StreamField{
- {Name: "b", FieldType: &ast.BasicType{Type: ast.STRINGS}},
- },
- },
- }},
- },
- },
- data: []byte(`{"a": [null, {"b" : "hello2"}]}`),
- result: &xsql.Tuple{Message: xsql.Message{
- "a": []map[string]interface{}{
- nil,
- {"b": "hello2"},
- },
- },
- },
- },
- {
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "a", FieldType: &ast.ArrayType{
- Type: ast.ARRAY,
- FieldType: &ast.ArrayType{
- Type: ast.BIGINT,
- },
- }},
- },
- },
- data: []byte(`{"a": [[50, 60, 70],[66], [77]]}`),
- result: &xsql.Tuple{Message: xsql.Message{
- "a": [][]int{
- {50, 60, 70},
- {66},
- {77},
- },
- },
- },
- },
- {
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "a", FieldType: &ast.ArrayType{
- Type: ast.ARRAY,
- FieldType: &ast.ArrayType{
- Type: ast.BIGINT,
- },
- }},
- },
- },
- data: []byte(`{"a": [null, [66], [77]]}`),
- result: &xsql.Tuple{Message: xsql.Message{
- "a": [][]int{
- []int(nil),
- {66},
- {77},
- },
- },
- },
- },
- {
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: nil,
- },
- data: []byte(`{"a": [{"b" : "hello1"}, {"b" : "hello2"}]}`),
- result: &xsql.Tuple{Message: xsql.Message{
- "a": []interface{}{
- map[string]interface{}{"b": "hello1"},
- map[string]interface{}{"b": "hello2"},
- },
- },
- },
- },
- {
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "a", FieldType: &ast.ArrayType{
- Type: ast.FLOAT,
- }},
- },
- },
- data: []byte(`{"a": "[\"55\", \"77\"]"}`),
- result: &xsql.Tuple{Message: xsql.Message{
- "a": []float64{
- 55,
- 77,
- },
- },
- },
- },
- {
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: nil,
- },
- data: []byte(`{"a": [55, 77]}`),
- result: &xsql.Tuple{Message: xsql.Message{
- "a": []interface{}{
- float64(55),
- float64(77),
- },
- },
- },
- },
- //Rec of complex type
- {
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "a", FieldType: &ast.RecType{
- StreamFields: []ast.StreamField{
- {Name: "b", FieldType: &ast.BasicType{Type: ast.STRINGS}},
- {Name: "c", FieldType: &ast.RecType{
- StreamFields: []ast.StreamField{
- {Name: "d", FieldType: &ast.BasicType{Type: ast.BIGINT}},
- },
- }},
- },
- }},
- },
- },
- data: []byte(`{"a": {"b" : "hello", "c": {"d": 35.2}}}`),
- result: &xsql.Tuple{Message: xsql.Message{
- "a": map[string]interface{}{
- "b": "hello",
- "c": map[string]interface{}{
- "d": 35,
- },
- },
- },
- },
- },
- {
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "a", FieldType: &ast.RecType{
- StreamFields: []ast.StreamField{
- {Name: "b", FieldType: &ast.BasicType{Type: ast.STRINGS}},
- {Name: "c", FieldType: &ast.RecType{
- StreamFields: []ast.StreamField{
- {Name: "d", FieldType: &ast.BasicType{Type: ast.BIGINT}},
- },
- }},
- },
- }},
- },
- },
- data: []byte(`{"a": null}`),
- result: &xsql.Tuple{Message: xsql.Message{
- "a": map[string]interface{}(nil),
- },
- },
- },
- {
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "a", FieldType: &ast.RecType{
- StreamFields: []ast.StreamField{
- {Name: "b", FieldType: &ast.BasicType{Type: ast.STRINGS}},
- {Name: "c", FieldType: &ast.ArrayType{
- Type: ast.FLOAT,
- }},
- },
- }},
- },
- },
- data: []byte(`{"a": {"b" : "hello", "c": [35.2, 38.2]}}`),
- result: &xsql.Tuple{Message: xsql.Message{
- "a": map[string]interface{}{
- "b": "hello",
- "c": []float64{
- 35.2, 38.2,
- },
- },
- },
- },
- },
- {
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "a", FieldType: &ast.RecType{
- StreamFields: []ast.StreamField{
- {Name: "b", FieldType: &ast.BasicType{Type: ast.STRINGS}},
- {Name: "c", FieldType: &ast.ArrayType{
- Type: ast.FLOAT,
- }},
- },
- }},
- },
- },
- data: []byte(`{"a": {"b" : "hello", "c": null}}`),
- result: &xsql.Tuple{Message: xsql.Message{
- "a": map[string]interface{}{
- "b": "hello",
- "c": []float64(nil),
- },
- },
- },
- },
- {
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "a", FieldType: &ast.RecType{
- StreamFields: []ast.StreamField{
- {Name: "b", FieldType: &ast.BasicType{Type: ast.STRINGS}},
- {Name: "c", FieldType: &ast.ArrayType{
- Type: ast.FLOAT,
- }},
- },
- }},
- },
- },
- data: []byte(`{"a": {"b" : "hello", "c": [null, 35.4]}}`),
- result: errors.New("error in preprocessor: fail to parse field c: invalid data type for [0], expect float but found <nil>(<nil>)"),
- },
- {
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: nil,
- },
- data: []byte(`{"a": {"b" : "hello", "c": {"d": 35.2}}}`),
- result: &xsql.Tuple{Message: xsql.Message{
- "a": map[string]interface{}{
- "b": "hello",
- "c": map[string]interface{}{
- "d": 35.2,
- },
- },
- },
- },
- }, {
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "a", FieldType: &ast.RecType{
- StreamFields: []ast.StreamField{
- {Name: "b", FieldType: &ast.BasicType{Type: ast.STRINGS}},
- },
- }},
- {Name: "b", FieldType: &ast.BasicType{Type: ast.FLOAT}},
- {Name: "c", FieldType: &ast.ArrayType{Type: ast.BIGINT}},
- },
- Options: &ast.Options{
- STRICT_VALIDATION: false,
- },
- },
- data: []byte(`{"a": {"d" : "hello"}}`),
- result: &xsql.Tuple{Message: xsql.Message{
- "a": map[string]interface{}{
- "b": "",
- },
- "b": 0.0,
- "c": []int(nil),
- },
- },
- },
- }
- fmt.Printf("The test bucket size is %d.\n\n", len(tests))
- defer conf.CloseLogger()
- contextLogger := conf.Log.WithField("rule", "TestPreprocessor_Apply")
- ctx := context.WithValue(context.Background(), context.LoggerKey, contextLogger)
- for i, tt := range tests {
- pp := &Preprocessor{}
- if tt.stmt.Options != nil {
- pp.strictValidation = tt.stmt.Options.STRICT_VALIDATION
- } else {
- pp.strictValidation = true
- }
- pp.streamFields = convertFields(tt.stmt.StreamFields)
- dm := make(map[string]interface{})
- if e := json.Unmarshal(tt.data, &dm); e != nil {
- log.Fatal(e)
- return
- } else {
- tuple := &xsql.Tuple{Message: dm}
- fv, afv := xsql.NewFunctionValuersForOp(nil)
- result := pp.Apply(ctx, tuple, fv, afv)
- if !reflect.DeepEqual(tt.result, result) {
- t.Errorf("%d. %q\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tuple, tt.result, result)
- }
- }
- }
- }
- func TestPreprocessorTime_Apply(t *testing.T) {
- var tests = []struct {
- stmt *ast.StreamStmt
- data []byte
- result interface{}
- }{
- {
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "abc", FieldType: &ast.BasicType{Type: ast.DATETIME}},
- {Name: "def", FieldType: &ast.BasicType{Type: ast.DATETIME}},
- },
- },
- data: []byte(`{"abc": "2019-09-19T00:55:15.000Z", "def" : 1568854573431}`),
- result: &xsql.Tuple{Message: xsql.Message{
- "abc": cast.TimeFromUnixMilli(1568854515000),
- "def": cast.TimeFromUnixMilli(1568854573431),
- },
- },
- },
- {
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: nil,
- },
- data: []byte(`{"abc": "2019-09-19T00:55:15.000Z", "def" : 1568854573431}`),
- result: &xsql.Tuple{Message: xsql.Message{
- "abc": "2019-09-19T00:55:15.000Z",
- "def": float64(1568854573431),
- },
- },
- },
- {
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "abc", FieldType: &ast.BasicType{Type: ast.DATETIME}},
- {Name: "def", FieldType: &ast.BasicType{Type: ast.DATETIME}},
- },
- },
- data: []byte(`{"abc": "2019-09-19T00:55:1dd5Z", "def" : 111568854573431}`),
- 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\""),
- },
- {
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "abc", FieldType: &ast.BasicType{Type: ast.DATETIME}},
- {Name: "def", FieldType: &ast.BasicType{Type: ast.DATETIME}},
- },
- Options: &ast.Options{
- DATASOURCE: "users",
- FORMAT: "JSON",
- KEY: "USERID",
- CONF_KEY: "srv1",
- TYPE: "MQTT",
- TIMESTAMP: "USERID",
- TIMESTAMP_FORMAT: "yyyy-MM-dd 'at' HH:mm:ss'Z'X",
- },
- },
- data: []byte(`{"abc": "2019-09-19 at 18:55:15Z+07", "def" : 1568854573431}`),
- result: &xsql.Tuple{Message: xsql.Message{
- "abc": cast.TimeFromUnixMilli(1568894115000),
- "def": cast.TimeFromUnixMilli(1568854573431),
- }},
- },
- //Array type
- {
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "a", FieldType: &ast.ArrayType{
- Type: ast.DATETIME,
- }},
- },
- },
- data: []byte(`{"a": [1568854515123, 1568854573431]}`),
- result: &xsql.Tuple{Message: xsql.Message{
- "a": []time.Time{
- cast.TimeFromUnixMilli(1568854515123),
- cast.TimeFromUnixMilli(1568854573431),
- },
- },
- },
- },
- {
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "a", FieldType: &ast.RecType{
- StreamFields: []ast.StreamField{
- {Name: "b", FieldType: &ast.BasicType{Type: ast.STRINGS}},
- {Name: "c", FieldType: &ast.BasicType{Type: ast.DATETIME}},
- },
- }},
- },
- },
- data: []byte(`{"a": {"b" : "hello", "c": 1568854515000}}`),
- result: &xsql.Tuple{Message: xsql.Message{
- "a": map[string]interface{}{
- "b": "hello",
- "c": cast.TimeFromUnixMilli(1568854515000),
- },
- },
- },
- },
- }
- fmt.Printf("The test bucket size is %d.\n\n", len(tests))
- defer conf.CloseLogger()
- contextLogger := conf.Log.WithField("rule", "TestPreprocessorTime_Apply")
- ctx := context.WithValue(context.Background(), context.LoggerKey, contextLogger)
- for i, tt := range tests {
- pp := &Preprocessor{}
- pp.streamFields = convertFields(tt.stmt.StreamFields)
- if tt.stmt.Options != nil {
- pp.timestampFormat = tt.stmt.Options.TIMESTAMP_FORMAT
- }
- dm := make(map[string]interface{})
- if e := json.Unmarshal(tt.data, &dm); e != nil {
- log.Fatal(e)
- return
- } else {
- tuple := &xsql.Tuple{Message: dm}
- fv, afv := xsql.NewFunctionValuersForOp(nil)
- result := pp.Apply(ctx, tuple, fv, afv)
- //workaround make sure all the timezone are the same for time vars or the DeepEqual will be false.
- if rt, ok := result.(*xsql.Tuple); ok {
- if rtt, ok := rt.Message["abc"].(time.Time); ok {
- rt.Message["abc"] = rtt.UTC()
- }
- }
- if !reflect.DeepEqual(tt.result, result) {
- t.Errorf("%d. %q\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tuple, tt.result, result)
- }
- }
- }
- }
- func convertFields(o ast.StreamFields) []interface{} {
- if o == nil {
- return nil
- }
- fields := make([]interface{}, len(o))
- for i := range o {
- fields[i] = &o[i]
- }
- return fields
- }
- func TestPreprocessorEventtime_Apply(t *testing.T) {
- var tests = []struct {
- stmt *ast.StreamStmt
- data []byte
- result interface{}
- }{
- //Basic type
- {
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "abc", FieldType: &ast.BasicType{Type: ast.BIGINT}},
- },
- Options: &ast.Options{
- DATASOURCE: "users",
- FORMAT: "JSON",
- KEY: "USERID",
- CONF_KEY: "srv1",
- TYPE: "MQTT",
- TIMESTAMP: "abc",
- TIMESTAMP_FORMAT: "yyyy-MM-dd''T''HH:mm:ssX'",
- },
- },
- data: []byte(`{"abc": 1568854515000}`),
- result: &xsql.Tuple{Message: xsql.Message{
- "abc": 1568854515000,
- }, Timestamp: 1568854515000,
- },
- },
- {
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: nil,
- Options: &ast.Options{
- DATASOURCE: "users",
- FORMAT: "JSON",
- KEY: "USERID",
- CONF_KEY: "srv1",
- TYPE: "MQTT",
- TIMESTAMP: "abc",
- TIMESTAMP_FORMAT: "yyyy-MM-dd''T''HH:mm:ssX'",
- },
- },
- data: []byte(`{"abc": 1568854515000}`),
- result: &xsql.Tuple{Message: xsql.Message{
- "abc": float64(1568854515000),
- }, Timestamp: 1568854515000,
- },
- },
- {
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "abc", FieldType: &ast.BasicType{Type: ast.BOOLEAN}},
- },
- Options: &ast.Options{
- DATASOURCE: "users",
- TIMESTAMP: "abc",
- },
- },
- data: []byte(`{"abc": true}`),
- result: errors.New("cannot convert timestamp field abc to timestamp with error unsupported type to convert to timestamp true"),
- },
- {
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "abc", FieldType: &ast.BasicType{Type: ast.FLOAT}},
- {Name: "def", FieldType: &ast.BasicType{Type: ast.STRINGS}},
- },
- Options: &ast.Options{
- DATASOURCE: "users",
- TIMESTAMP: "def",
- },
- },
- data: []byte(`{"abc": 34, "def" : "2019-09-23T02:47:29.754Z", "ghi": 50}`),
- result: &xsql.Tuple{Message: xsql.Message{
- "abc": float64(34),
- "def": "2019-09-23T02:47:29.754Z",
- }, Timestamp: int64(1569206849754),
- },
- },
- {
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "abc", FieldType: &ast.BasicType{Type: ast.DATETIME}},
- {Name: "def", FieldType: &ast.BasicType{Type: ast.DATETIME}},
- },
- Options: &ast.Options{
- DATASOURCE: "users",
- TIMESTAMP: "abc",
- },
- },
- data: []byte(`{"abc": "2019-09-19T00:55:15.000Z", "def" : 1568854573431}`),
- result: &xsql.Tuple{Message: xsql.Message{
- "abc": cast.TimeFromUnixMilli(1568854515000),
- "def": cast.TimeFromUnixMilli(1568854573431),
- }, Timestamp: int64(1568854515000),
- },
- },
- {
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "abc", FieldType: &ast.BasicType{Type: ast.FLOAT}},
- {Name: "def", FieldType: &ast.BasicType{Type: ast.STRINGS}},
- },
- Options: &ast.Options{
- DATASOURCE: "users",
- TIMESTAMP: "def",
- TIMESTAMP_FORMAT: "yyyy-MM-dd'AT'HH:mm:ss",
- },
- },
- data: []byte(`{"abc": 34, "def" : "2019-09-23AT02:47:29", "ghi": 50}`),
- result: &xsql.Tuple{Message: xsql.Message{
- "abc": float64(34),
- "def": "2019-09-23AT02:47:29",
- }, Timestamp: int64(1569206849000),
- },
- },
- {
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "abc", FieldType: &ast.BasicType{Type: ast.FLOAT}},
- {Name: "def", FieldType: &ast.BasicType{Type: ast.STRINGS}},
- },
- Options: &ast.Options{
- DATASOURCE: "users",
- TIMESTAMP: "def",
- TIMESTAMP_FORMAT: "yyyy-MM-ddaHH:mm:ss",
- },
- },
- data: []byte(`{"abc": 34, "def" : "2019-09-23AT02:47:29", "ghi": 50}`),
- 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\""),
- },
- }
- fmt.Printf("The test bucket size is %d.\n\n", len(tests))
- defer conf.CloseLogger()
- contextLogger := conf.Log.WithField("rule", "TestPreprocessorEventtime_Apply")
- ctx := context.WithValue(context.Background(), context.LoggerKey, contextLogger)
- for i, tt := range tests {
- pp := &Preprocessor{
- defaultFieldProcessor: defaultFieldProcessor{
- streamFields: convertFields(tt.stmt.StreamFields),
- isBinary: false,
- timestampFormat: tt.stmt.Options.TIMESTAMP_FORMAT,
- },
- isEventTime: true,
- timestampField: tt.stmt.Options.TIMESTAMP,
- }
- dm := make(map[string]interface{})
- if e := json.Unmarshal(tt.data, &dm); e != nil {
- log.Fatal(e)
- return
- } else {
- tuple := &xsql.Tuple{Message: dm}
- fv, afv := xsql.NewFunctionValuersForOp(nil)
- result := pp.Apply(ctx, tuple, fv, afv)
- //workaround make sure all the timezone are the same for time vars or the DeepEqual will be false.
- if rt, ok := result.(*xsql.Tuple); ok {
- if rtt, ok := rt.Message["abc"].(time.Time); ok {
- rt.Message["abc"] = rtt.UTC()
- }
- }
- if !reflect.DeepEqual(tt.result, result) {
- t.Errorf("%d. %q\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tuple, tt.result, result)
- }
- }
- }
- }
- func TestPreprocessorError(t *testing.T) {
- tests := []struct {
- stmt *ast.StreamStmt
- data []byte
- result interface{}
- }{
- {
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "abc", FieldType: &ast.BasicType{Type: ast.BIGINT}},
- },
- },
- data: []byte(`{"abc": "dafsad"}`),
- result: errors.New("error in preprocessor: invalid data type for abc, expect bigint but found string(dafsad)"),
- }, {
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "a", FieldType: &ast.RecType{
- StreamFields: []ast.StreamField{
- {Name: "b", FieldType: &ast.BasicType{Type: ast.STRINGS}},
- },
- }},
- },
- },
- data: []byte(`{"a": {"d" : "hello"}}`),
- result: errors.New("error in preprocessor: invalid data map[d:hello], field b not found"),
- }, {
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "abc", FieldType: &ast.BasicType{Type: ast.BIGINT}},
- },
- Options: &ast.Options{
- DATASOURCE: "users",
- FORMAT: "JSON",
- KEY: "USERID",
- CONF_KEY: "srv1",
- TYPE: "MQTT",
- TIMESTAMP: "abc",
- TIMESTAMP_FORMAT: "yyyy-MM-dd''T''HH:mm:ssX'",
- },
- },
- data: []byte(`{"abc": "not a time"}`),
- result: errors.New("error in preprocessor: invalid data type for abc, expect bigint but found string(not a time)"),
- },
- }
- fmt.Printf("The test bucket size is %d.\n\n", len(tests))
- defer conf.CloseLogger()
- contextLogger := conf.Log.WithField("rule", "TestPreprocessorError")
- ctx := context.WithValue(context.Background(), context.LoggerKey, contextLogger)
- for i, tt := range tests {
- pp := &Preprocessor{}
- pp.strictValidation = true
- pp.streamFields = convertFields(tt.stmt.StreamFields)
- dm := make(map[string]interface{})
- if e := json.Unmarshal(tt.data, &dm); e != nil {
- log.Fatal(e)
- return
- } else {
- tuple := &xsql.Tuple{Message: dm}
- fv, afv := xsql.NewFunctionValuersForOp(nil)
- result := pp.Apply(ctx, tuple, fv, afv)
- if !reflect.DeepEqual(tt.result, result) {
- t.Errorf("%d. %q\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tuple, tt.result, result)
- }
- }
- }
- }
- func TestPreprocessorForBinary(t *testing.T) {
- docsFolder, err := conf.GetLoc("docs/")
- if err != nil {
- t.Errorf("Cannot find docs folder: %v", err)
- }
- image, err := ioutil.ReadFile(path.Join(docsFolder, "cover.jpg"))
- if err != nil {
- t.Errorf("Cannot read image: %v", err)
- }
- b64img := base64.StdEncoding.EncodeToString(image)
- //TODO test bytea type conversion to string or else
- var tests = []struct {
- stmt *ast.StreamStmt
- data []byte
- isBinary bool
- result interface{}
- }{
- {
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: nil,
- },
- data: image,
- isBinary: true,
- result: &xsql.Tuple{Message: xsql.Message{
- "self": image,
- },
- },
- },
- {
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "img", FieldType: &ast.BasicType{Type: ast.BYTEA}},
- },
- },
- data: image,
- isBinary: true,
- result: &xsql.Tuple{Message: xsql.Message{
- "img": image,
- },
- },
- },
- {
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "a", FieldType: &ast.RecType{
- StreamFields: []ast.StreamField{
- {Name: "b", FieldType: &ast.BasicType{Type: ast.BYTEA}},
- },
- }},
- },
- },
- data: []byte(fmt.Sprintf(`{"a": {"b" : "%s"}}`, b64img)),
- result: &xsql.Tuple{Message: xsql.Message{
- "a": map[string]interface{}{
- "b": image,
- },
- },
- },
- },
- {
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "a", FieldType: &ast.ArrayType{
- Type: ast.BYTEA,
- }},
- },
- },
- data: []byte(fmt.Sprintf(`{"a": ["%s"]}`, b64img)),
- result: &xsql.Tuple{Message: xsql.Message{
- "a": [][]byte{
- image,
- },
- },
- },
- },
- {
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "a", FieldType: &ast.ArrayType{
- Type: ast.STRUCT,
- FieldType: &ast.RecType{
- StreamFields: []ast.StreamField{
- {Name: "b", FieldType: &ast.BasicType{Type: ast.BYTEA}},
- },
- },
- }},
- },
- },
- data: []byte(fmt.Sprintf(`{"a": [{"b":"%s"}]}`, b64img)),
- result: &xsql.Tuple{Message: xsql.Message{
- "a": []map[string]interface{}{
- {"b": image},
- },
- },
- },
- },
- }
- fmt.Printf("The test bucket size is %d.\n\n", len(tests))
- defer conf.CloseLogger()
- contextLogger := conf.Log.WithField("rule", "TestPreprocessorForBinary")
- ctx := context.WithValue(context.Background(), context.LoggerKey, contextLogger)
- for i, tt := range tests {
- pp := &Preprocessor{}
- pp.streamFields = convertFields(tt.stmt.StreamFields)
- pp.isBinary = tt.isBinary
- format := "json"
- if tt.isBinary {
- format = "binary"
- }
- if dm, e := message.Decode(tt.data, format); e != nil {
- log.Fatal(e)
- return
- } else {
- tuple := &xsql.Tuple{Message: dm}
- fv, afv := xsql.NewFunctionValuersForOp(nil)
- result := pp.Apply(ctx, tuple, fv, afv)
- if !reflect.DeepEqual(tt.result, result) {
- t.Errorf("%d. %q\n\nresult mismatch", i, tuple)
- }
- }
- }
- }
|