1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156 |
- // Copyright 2022-2023 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 (
- "fmt"
- "reflect"
- "strconv"
- "strings"
- "testing"
- "github.com/lf-edge/ekuiper/internal/conf"
- "github.com/lf-edge/ekuiper/internal/testx"
- "github.com/lf-edge/ekuiper/internal/topo/context"
- "github.com/lf-edge/ekuiper/internal/topo/state"
- "github.com/lf-edge/ekuiper/internal/xsql"
- "github.com/lf-edge/ekuiper/pkg/api"
- "github.com/lf-edge/ekuiper/pkg/cast"
- )
- func TestMiscFunc_Apply1(t *testing.T) {
- tests := []struct {
- sql string
- data *xsql.Tuple
- result []map[string]interface{}
- }{
- {
- sql: "SELECT md5(a) AS a FROM test",
- data: &xsql.Tuple{
- Emitter: "test",
- Message: xsql.Message{
- "a": "The quick brown fox jumps over the lazy dog",
- "b": "myb",
- "c": "myc",
- },
- },
- result: []map[string]interface{}{{
- "a": strings.ToLower("9E107D9D372BB6826BD81D3542A419D6"),
- }},
- },
- {
- sql: "SELECT md5(d) AS a FROM test",
- data: &xsql.Tuple{
- Emitter: "test",
- Message: xsql.Message{
- "a": "The quick brown fox jumps over the lazy dog",
- "b": "myb",
- "c": "myc",
- },
- },
- result: []map[string]interface{}{{}},
- },
- {
- sql: "SELECT sha1(a) AS a FROM test",
- data: &xsql.Tuple{
- Emitter: "test",
- Message: xsql.Message{
- "a": "The quick brown fox jumps over the lazy dog",
- "b": "myb",
- "c": "myc",
- },
- },
- result: []map[string]interface{}{{
- "a": strings.ToLower("2FD4E1C67A2D28FCED849EE1BB76E7391B93EB12"),
- }},
- },
- {
- sql: "SELECT sha256(a) AS a FROM test",
- data: &xsql.Tuple{
- Emitter: "test",
- Message: xsql.Message{
- "a": "The quick brown fox jumps over the lazy dog",
- "b": "myb",
- "c": "myc",
- },
- },
- result: []map[string]interface{}{{
- "a": strings.ToLower("D7A8FBB307D7809469CA9ABCB0082E4F8D5651E46D3CDB762D02D0BF37C9E592"),
- }},
- },
- {
- sql: "SELECT sha384(a) AS a FROM test",
- data: &xsql.Tuple{
- Emitter: "test",
- Message: xsql.Message{
- "a": "The quick brown fox jumps over the lazy dog",
- "b": "myb",
- "c": "myc",
- },
- },
- result: []map[string]interface{}{{
- "a": strings.ToLower("CA737F1014A48F4C0B6DD43CB177B0AFD9E5169367544C494011E3317DBF9A509CB1E5DC1E85A941BBEE3D7F2AFBC9B1"),
- }},
- },
- {
- sql: "SELECT sha512(a) AS a FROM test",
- data: &xsql.Tuple{
- Emitter: "test",
- Message: xsql.Message{
- "a": "The quick brown fox jumps over the lazy dog",
- "b": "myb",
- "c": "myc",
- },
- },
- result: []map[string]interface{}{{
- "a": strings.ToLower("07E547D9586F6A73F73FBAC0435ED76951218FB7D0C8D788A309D785436BBB642E93A252A954F23912547D1E8A3B5ED6E1BFD7097821233FA0538F3DB854FEE6"),
- }},
- },
- {
- sql: "SELECT mqtt(topic) AS a FROM test",
- data: &xsql.Tuple{
- Emitter: "test",
- Message: xsql.Message{},
- Metadata: xsql.Metadata{
- "topic": "devices/device_001/message",
- },
- },
- result: []map[string]interface{}{{
- "a": "devices/device_001/message",
- }},
- },
- {
- sql: "SELECT mqtt(topic) AS a FROM test",
- data: &xsql.Tuple{
- Emitter: "test",
- Message: xsql.Message{},
- Metadata: xsql.Metadata{
- "topic": "devices/device_001/message",
- },
- },
- result: []map[string]interface{}{{
- "a": "devices/device_001/message",
- }},
- },
- {
- sql: "SELECT topic, mqtt(topic) AS a FROM test",
- data: &xsql.Tuple{
- Emitter: "test",
- Message: xsql.Message{
- "topic": "fff",
- },
- Metadata: xsql.Metadata{
- "topic": "devices/device_001/message",
- },
- },
- result: []map[string]interface{}{{
- "topic": "fff",
- "a": "devices/device_001/message",
- }},
- },
- {
- sql: "SELECT cardinality(arr) as r FROM test",
- data: &xsql.Tuple{
- Emitter: "test",
- Message: xsql.Message{
- "temperature": 43.2,
- "arr": []int{},
- },
- },
- result: []map[string]interface{}{{
- "r": 0,
- }},
- },
- {
- sql: "SELECT cardinality(arr) as r FROM test",
- data: &xsql.Tuple{
- Emitter: "test",
- Message: xsql.Message{
- "temperature": 43.2,
- "arr": []int{1, 2, 3, 4, 5},
- },
- },
- result: []map[string]interface{}{{
- "r": 5,
- }},
- },
- {
- sql: "SELECT isNull(arr) as r FROM test",
- data: &xsql.Tuple{
- Emitter: "test",
- Message: xsql.Message{
- "temperature": 43.2,
- "arr": []int{},
- },
- },
- result: []map[string]interface{}{{
- "r": false,
- }},
- },
- {
- sql: "SELECT isNull(arr) as r FROM test",
- data: &xsql.Tuple{
- Emitter: "test",
- Message: xsql.Message{
- "temperature": 43.2,
- "arr": []float64(nil),
- },
- },
- result: []map[string]interface{}{{
- "r": true,
- }},
- },
- {
- sql: "SELECT isNull(rec) as r FROM test",
- data: &xsql.Tuple{
- Emitter: "test",
- Message: xsql.Message{
- "temperature": 43.2,
- "rec": map[string]interface{}(nil),
- },
- },
- result: []map[string]interface{}{{
- "r": true,
- }},
- },
- {
- sql: "SELECT cast(a * 1000, \"datetime\") AS a FROM test",
- data: &xsql.Tuple{
- Emitter: "test",
- Message: xsql.Message{
- "a": 1.62000273e+09,
- "b": "ya",
- "c": "myc",
- },
- },
- result: []map[string]interface{}{{
- "a": cast.TimeFromUnixMilli(1.62000273e+12),
- }},
- },
- {
- sql: "SELECT rule_id() AS rule_id, rule_start() as rule_start FROM test",
- data: &xsql.Tuple{
- Emitter: "test",
- Message: xsql.Message{},
- },
- result: []map[string]interface{}{{
- "rule_id": "rule0",
- "rule_start": 12345,
- }},
- },
- }
- fmt.Printf("The test bucket size is %d.\n\n", len(tests))
- contextLogger := conf.Log.WithField("rule", "TestMiscFunc_Apply1")
- ctx := context.WithValue(context.Background(), context.LoggerKey, contextLogger)
- ctx = context.WithValue(ctx, context.RuleStartKey, 12345)
- ctx = ctx.WithMeta("rule0", "op1", &state.MemoryStore{}).(*context.DefaultContext)
- for i, tt := range tests {
- stmt, err := xsql.NewParser(strings.NewReader(tt.sql)).Parse()
- if err != nil || stmt == nil {
- t.Errorf("parse sql %s error %v", tt.sql, err)
- }
- pp := &ProjectOp{}
- parseStmt(pp, stmt.Fields)
- fv, afv := xsql.NewFunctionValuersForOp(ctx)
- opResult := pp.Apply(ctx, tt.data, fv, afv)
- result, err := parseResult(opResult, pp.IsAggregate)
- if err != nil {
- t.Errorf("parse result error: %s", err)
- continue
- }
- if !reflect.DeepEqual(tt.result, result) {
- t.Errorf("%d. %q\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.sql, tt.result, result)
- }
- }
- }
- func TestMqttFunc_Apply2(t *testing.T) {
- tests := []struct {
- sql string
- data *xsql.JoinTuples
- result []map[string]interface{}
- }{
- {
- sql: "SELECT id1, mqtt(src1.topic) AS a, mqtt(src2.topic) as b FROM src1 LEFT JOIN src2 ON src1.id1 = src2.id1",
- data: &xsql.JoinTuples{
- Content: []*xsql.JoinTuple{
- {
- Tuples: []xsql.TupleRow{
- &xsql.Tuple{Emitter: "src1", Message: xsql.Message{"id1": "1", "f1": "v1"}, Metadata: xsql.Metadata{"topic": "devices/type1/device001"}},
- &xsql.Tuple{Emitter: "src2", Message: xsql.Message{"id2": "1", "f2": "w1"}, Metadata: xsql.Metadata{"topic": "devices/type2/device001"}},
- },
- },
- },
- },
- result: []map[string]interface{}{{
- "id1": "1",
- "a": "devices/type1/device001",
- "b": "devices/type2/device001",
- }},
- },
- }
- fmt.Printf("The test bucket size is %d.\n\n", len(tests))
- contextLogger := conf.Log.WithField("rule", "TestMqttFunc_Apply2")
- ctx := context.WithValue(context.Background(), context.LoggerKey, contextLogger)
- for i, tt := range tests {
- stmt, err := xsql.NewParser(strings.NewReader(tt.sql)).Parse()
- if err != nil || stmt == nil {
- t.Errorf("parse sql %s error %v", tt.sql, err)
- }
- pp := &ProjectOp{}
- parseStmt(pp, stmt.Fields)
- fv, afv := xsql.NewFunctionValuersForOp(nil)
- opResult := pp.Apply(ctx, tt.data, fv, afv)
- result, err := parseResult(opResult, pp.IsAggregate)
- if err != nil {
- t.Errorf("parse result error: %s", err)
- continue
- }
- if !reflect.DeepEqual(tt.result, result) {
- t.Errorf("%d. %q\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.sql, tt.result, result)
- }
- }
- }
- func TestMetaFunc_Apply1(t *testing.T) {
- tests := []struct {
- sql string
- data interface{}
- result interface{}
- }{
- {
- sql: "SELECT topic, meta(topic) AS a FROM test",
- data: &xsql.Tuple{
- Emitter: "test",
- Message: xsql.Message{
- "topic": "fff",
- },
- Metadata: xsql.Metadata{
- "topic": "devices/device_001/message",
- },
- },
- result: []map[string]interface{}{{
- "topic": "fff",
- "a": "devices/device_001/message",
- }},
- },
- {
- sql: "SELECT meta(device) as d, meta(temperature->device) as r FROM test",
- data: &xsql.Tuple{
- Emitter: "test",
- Message: xsql.Message{
- "temperature": 43.2,
- },
- Metadata: xsql.Metadata{
- "temperature": map[string]interface{}{
- "id": "dfadfasfas",
- "device": "device2",
- },
- "device": "gateway",
- },
- },
- result: []map[string]interface{}{{
- "d": "gateway",
- "r": "device2",
- }},
- },
- {
- sql: "SELECT meta(*) as r FROM test",
- data: &xsql.Tuple{
- Emitter: "test",
- Message: xsql.Message{
- "temperature": 43.2,
- },
- Metadata: xsql.Metadata{
- "temperature": map[string]interface{}{
- "id": "dfadfasfas",
- "device": "device2",
- },
- "device": "gateway",
- },
- },
- result: []map[string]interface{}{{
- "r": map[string]interface{}{
- "temperature": map[string]interface{}{
- "id": "dfadfasfas",
- "device": "device2",
- },
- "device": "gateway",
- },
- }},
- },
- {
- sql: "SELECT topic, meta(`Light-diming`->device) AS a FROM test",
- data: &xsql.Tuple{
- Emitter: "test",
- Message: xsql.Message{
- "topic": "fff",
- },
- Metadata: xsql.Metadata{
- "Light-diming": map[string]interface{}{
- "device": "device2",
- },
- },
- },
- result: []map[string]interface{}{{
- "topic": "fff",
- "a": "device2",
- }},
- },
- }
- fmt.Printf("The test bucket size is %d.\n\n", len(tests))
- contextLogger := conf.Log.WithField("rule", "TestMetaFunc_Apply1")
- ctx := context.WithValue(context.Background(), context.LoggerKey, contextLogger)
- for i, tt := range tests {
- stmt, err := xsql.NewParser(strings.NewReader(tt.sql)).Parse()
- if err != nil || stmt == nil {
- t.Errorf("parse sql %s error %v", tt.sql, err)
- }
- pp := &ProjectOp{}
- parseStmt(pp, stmt.Fields)
- fv, afv := xsql.NewFunctionValuersForOp(nil)
- opResult := pp.Apply(ctx, tt.data, fv, afv)
- result, err := parseResult(opResult, pp.IsAggregate)
- if err != nil {
- t.Errorf("parse result error: %s", err)
- continue
- }
- if !reflect.DeepEqual(tt.result, result) {
- t.Errorf("%d. %q\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.sql, tt.result, result)
- }
- }
- }
- func TestJsonPathFunc_Apply1(t *testing.T) {
- tests := []struct {
- sql string
- data interface{}
- result interface{}
- err string
- }{
- {
- sql: `SELECT json_path_query(equipment, "$.arm_right") AS a FROM test`,
- data: &xsql.Tuple{
- Emitter: "test",
- Message: xsql.Message{
- "class": "warrior",
- "equipment": map[string]interface{}{
- "rings": []map[string]interface{}{
- {
- "name": "ring of despair",
- "weight": 0.1,
- }, {
- "name": "ring of strength",
- "weight": 2.4,
- },
- },
- "arm_right": "Sword of flame",
- "arm_left": "Shield of faith",
- },
- },
- },
- result: []map[string]interface{}{{
- "a": "Sword of flame",
- }},
- }, {
- sql: `SELECT json_path_query(equipment, "$.rings[*].weight") AS a FROM test`,
- data: &xsql.Tuple{
- Emitter: "test",
- Message: xsql.Message{
- "class": "warrior",
- "equipment": map[string]interface{}{
- "rings": []interface{}{
- map[string]interface{}{
- "name": "ring of despair",
- "weight": 0.1,
- }, map[string]interface{}{
- "name": "ring of strength",
- "weight": 2.4,
- },
- },
- "arm_right": "Sword of flame",
- "arm_left": "Shield of faith",
- },
- },
- },
- result: []map[string]interface{}{{
- "a": []interface{}{
- 0.1, 2.4,
- },
- }},
- }, {
- sql: `SELECT json_path_query_first(equipment, "$.rings[*].weight") AS a FROM test`,
- data: &xsql.Tuple{
- Emitter: "test",
- Message: xsql.Message{
- "class": "warrior",
- "equipment": map[string]interface{}{
- "rings": []interface{}{
- map[string]interface{}{
- "name": "ring of despair",
- "weight": 0.1,
- }, map[string]interface{}{
- "name": "ring of strength",
- "weight": 2.4,
- },
- },
- "arm_right": "Sword of flame",
- "arm_left": "Shield of faith",
- },
- },
- },
- result: []map[string]interface{}{{
- "a": 0.1,
- }},
- }, {
- sql: `SELECT json_path_query(equipment, "$.rings[? @.weight>1]") AS a FROM test`,
- data: &xsql.Tuple{
- Emitter: "test",
- Message: xsql.Message{
- "class": "warrior",
- "equipment": map[string]interface{}{
- "rings": []interface{}{
- map[string]interface{}{
- "name": "ring of despair",
- "weight": 0.1,
- }, map[string]interface{}{
- "name": "ring of strength",
- "weight": 2.4,
- },
- },
- "arm_right": "Sword of flame",
- "arm_left": "Shield of faith",
- },
- },
- },
- result: []map[string]interface{}{{
- "a": []interface{}{
- map[string]interface{}{
- "name": "ring of strength",
- "weight": 2.4,
- },
- },
- }},
- }, {
- sql: `SELECT json_path_query(equipment, "$.rings[? @.weight>1].name") AS a FROM test`,
- data: &xsql.Tuple{
- Emitter: "test",
- Message: xsql.Message{
- "class": "warrior",
- "equipment": map[string]interface{}{
- "rings": []interface{}{
- map[string]interface{}{
- "name": "ring of despair",
- "weight": 0.1,
- }, map[string]interface{}{
- "name": "ring of strength",
- "weight": 2.4,
- },
- },
- "arm_right": "Sword of flame",
- "arm_left": "Shield of faith",
- },
- },
- },
- result: []map[string]interface{}{{
- "a": []interface{}{
- "ring of strength",
- },
- }},
- }, {
- sql: `SELECT json_path_exists(equipment, "$.rings[? @.weight>5]") AS a FROM test`,
- data: &xsql.Tuple{
- Emitter: "test",
- Message: xsql.Message{
- "class": "warrior",
- "equipment": map[string]interface{}{
- "rings": []interface{}{
- map[string]interface{}{
- "name": "ring of despair",
- "weight": 0.1,
- }, map[string]interface{}{
- "name": "ring of strength",
- "weight": 2.4,
- },
- },
- "arm_right": "Sword of flame",
- "arm_left": "Shield of faith",
- },
- },
- },
- result: []map[string]interface{}{{
- "a": false,
- }},
- }, {
- sql: `SELECT json_path_exists(equipment, "$.ring1") AS a FROM test`,
- data: &xsql.Tuple{
- Emitter: "test",
- Message: xsql.Message{
- "class": "warrior",
- "equipment": map[string]interface{}{
- "rings": []interface{}{
- map[string]interface{}{
- "name": "ring of despair",
- "weight": 0.1,
- }, map[string]interface{}{
- "name": "ring of strength",
- "weight": 2.4,
- },
- },
- "arm_right": "Sword of flame",
- "arm_left": "Shield of faith",
- },
- },
- },
- result: []map[string]interface{}{{
- "a": false,
- }},
- }, {
- sql: `SELECT json_path_exists(equipment, "$.rings") AS a FROM test`,
- data: &xsql.Tuple{
- Emitter: "test",
- Message: xsql.Message{
- "class": "warrior",
- "equipment": map[string]interface{}{
- "rings": []interface{}{
- map[string]interface{}{
- "name": "ring of despair",
- "weight": 0.1,
- }, map[string]interface{}{
- "name": "ring of strength",
- "weight": 2.4,
- },
- },
- "arm_right": "Sword of flame",
- "arm_left": "Shield of faith",
- },
- },
- },
- result: []map[string]interface{}{{
- "a": true,
- }},
- }, {
- sql: `SELECT json_path_query(equipment, "$.rings[? (@.weight>1)].name") AS a FROM test`,
- data: &xsql.Tuple{
- Emitter: "test",
- Message: xsql.Message{
- "class": "warrior",
- "equipment": map[string]interface{}{
- "rings": []map[string]interface{}{
- {
- "name": "ring of despair",
- "weight": 0.1,
- }, {
- "name": "ring of strength",
- "weight": 2.4,
- },
- },
- "arm_right": "Sword of flame",
- "arm_left": "Shield of faith",
- },
- },
- },
- result: []map[string]interface{}{{
- "a": []interface{}{
- "ring of strength",
- },
- }},
- }, {
- sql: `SELECT json_path_query(equipment, "$.rings[*]") AS a FROM test`,
- data: &xsql.Tuple{
- Emitter: "test",
- Message: xsql.Message{
- "class": "warrior",
- "equipment": map[string]interface{}{
- "rings": []float64{
- 0.1, 2.4,
- },
- "arm_right": "Sword of flame",
- "arm_left": "Shield of faith",
- },
- },
- },
- result: []map[string]interface{}{{
- "a": []interface{}{
- 0.1, 2.4,
- },
- }},
- }, {
- sql: `SELECT json_path_query(equipment, "$.rings") AS a FROM test`,
- data: &xsql.Tuple{
- Emitter: "test",
- Message: xsql.Message{
- "class": "warrior",
- "equipment": map[string]interface{}{
- "rings": []float64{
- 0.1, 2.4,
- },
- "arm_right": "Sword of flame",
- "arm_left": "Shield of faith",
- },
- },
- },
- result: []map[string]interface{}{{
- "a": []interface{}{
- 0.1, 2.4,
- },
- }},
- }, {
- sql: `SELECT json_path_query(equipment, "$[0].rings[1]") AS a FROM test`,
- data: &xsql.Tuple{
- Emitter: "test",
- Message: xsql.Message{
- "class": "warrior",
- "equipment": []map[string]interface{}{
- {
- "rings": []float64{
- 0.1, 2.4,
- },
- "arm_right": "Sword of flame",
- "arm_left": "Shield of faith",
- },
- },
- },
- },
- result: []map[string]interface{}{{
- "a": 2.4,
- }},
- }, {
- sql: "SELECT json_path_query(equipment, \"$[0][\\\"arm.left\\\"]\") AS a FROM test",
- data: &xsql.Tuple{
- Emitter: "test",
- Message: xsql.Message{
- "class": "warrior",
- "equipment": []map[string]interface{}{
- {
- "rings": []float64{
- 0.1, 2.4,
- },
- "arm.right": "Sword of flame",
- "arm.left": "Shield of faith",
- },
- },
- },
- },
- result: []map[string]interface{}{{
- "a": "Shield of faith",
- }},
- }, {
- sql: "SELECT json_path_query(equipment, \"$[\\\"arm.left\\\"]\") AS a FROM test",
- data: &xsql.Tuple{
- Emitter: "test",
- Message: xsql.Message{
- "class": "warrior",
- "equipment": `{"rings": [0.1, 2.4],"arm.right": "Sword of flame","arm.left": "Shield of faith"}`,
- },
- },
- result: []map[string]interface{}{{
- "a": "Shield of faith",
- }},
- }, {
- sql: "SELECT json_path_query(equipment, \"$[0][\\\"arm.left\\\"]\") AS a FROM test",
- data: &xsql.Tuple{
- Emitter: "test",
- Message: xsql.Message{
- "class": "warrior",
- "equipment": `[{"rings": [0.1, 2.4],"arm.right": "Sword of flame","arm.left": "Shield of faith"}]`,
- },
- },
- result: []map[string]interface{}{{
- "a": "Shield of faith",
- }},
- }, {
- sql: `SELECT all[poi[-1] + 1]->ts as powerOnTs FROM test`,
- data: &xsql.Tuple{
- Emitter: "test",
- Message: xsql.Message{
- "all": []map[string]interface{}{
- {"SystemPowerMode": 0, "VehicleSpeed": 0, "FLWdwPosition": 0, "FrontWiperSwitchStatus": float64(1), "ts": 0},
- {"SystemPowerMode": 0, "VehicleSpeed": 0, "FLWdwPosition": 0, "FrontWiperSwitchStatus": float64(4), "ts": 500},
- {"SystemPowerMode": 2, "VehicleSpeed": 0, "FLWdwPosition": 0, "FrontWiperSwitchStatus": 0, "ts": 1000},
- {"SystemPowerMode": 2, "VehicleSpeed": 10, "FLWdwPosition": 20, "FrontWiperSwitchStatus": 0, "ts": 60000},
- {"SystemPowerMode": 2, "VehicleSpeed": 10, "FLWdwPosition": 20, "FrontWiperSwitchStatus": 0, "ts": 89500},
- {"SystemPowerMode": 2, "VehicleSpeed": 20, "FLWdwPosition": 50, "FrontWiperSwitchStatus": 5, "ts": 90000},
- {"SystemPowerMode": 2, "VehicleSpeed": 40, "FLWdwPosition": 60, "FrontWiperSwitchStatus": 5, "ts": 121000},
- },
- "poi": []interface{}{0, 1},
- },
- },
- result: []map[string]interface{}{{
- "powerOnTs": 1000,
- }},
- }, {
- sql: `SELECT json_path_query(equipment, "$.arm_right") AS a FROM test`,
- data: &xsql.Tuple{
- Emitter: "test",
- Message: xsql.Message{
- "class": "warrior",
- "equipment2": map[string]interface{}{
- "rings": []map[string]interface{}{
- {
- "name": "ring of despair",
- "weight": 0.1,
- }, {
- "name": "ring of strength",
- "weight": 2.4,
- },
- },
- "arm_right": "Sword of flame",
- "arm_left": "Shield of faith",
- },
- },
- },
- err: "run Select error: call func json_path_query error: invalid data nil for jsonpath",
- },
- }
- fmt.Printf("The test bucket size is %d.\n\n", len(tests))
- contextLogger := conf.Log.WithField("rule", "TestJsonFunc_Apply1")
- ctx := context.WithValue(context.Background(), context.LoggerKey, contextLogger)
- for i, tt := range tests {
- stmt, err := xsql.NewParser(strings.NewReader(tt.sql)).Parse()
- if err != nil || stmt == nil {
- t.Errorf("parse sql %s error %v", tt.sql, err)
- }
- pp := &ProjectOp{}
- parseStmt(pp, stmt.Fields)
- fv, afv := xsql.NewFunctionValuersForOp(ctx)
- opResult := pp.Apply(ctx, tt.data, fv, afv)
- if rt, ok := opResult.(error); ok {
- if tt.err == "" {
- t.Errorf("%d: got error:\n exp=%s\n got=%s\n\n", i, tt.result, rt)
- } else if !reflect.DeepEqual(tt.err, testx.Errstring(rt)) {
- t.Errorf("%d: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.err, rt)
- }
- } else {
- result, _ := parseResult(opResult, pp.IsAggregate)
- if tt.err == "" {
- if !reflect.DeepEqual(tt.result, result) {
- t.Errorf("%d. %q\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.sql, tt.result, result)
- }
- } else {
- t.Errorf("%d: invalid result:\n exp error %s\n got=%s\n\n", i, tt.err, result)
- }
- }
- }
- }
- func TestChangedFuncs_Apply1(t *testing.T) {
- tests := []struct {
- sql string
- data []interface{}
- result [][]map[string]interface{}
- }{
- {
- sql: `SELECT changed_col(true, a), b FROM test`,
- data: []interface{}{
- &xsql.Tuple{
- Emitter: "test",
- Message: xsql.Message{
- "a": "a1",
- "b": "b1",
- "c": "c1",
- },
- },
- &xsql.Tuple{
- Emitter: "test",
- Message: xsql.Message{
- "a": "a1",
- "b": "b2",
- "c": "c1",
- },
- },
- &xsql.Tuple{
- Emitter: "test",
- Message: xsql.Message{
- "a": "a1",
- "c": "c1",
- },
- },
- &xsql.Tuple{
- Emitter: "test",
- Message: xsql.Message{
- "a": "a1",
- "b": "b2",
- "c": "c2",
- },
- },
- },
- result: [][]map[string]interface{}{{{
- "changed_col": "a1",
- "b": "b1",
- }}, {{
- "b": "b2",
- }}, {{}}, {{
- "b": "b2",
- }}},
- }, {
- sql: `SELECT changed_col(true, *) FROM test`,
- data: []interface{}{
- &xsql.Tuple{
- Emitter: "test",
- Message: xsql.Message{
- "a": "a1",
- "b": "b1",
- },
- },
- &xsql.Tuple{
- Emitter: "test",
- Message: xsql.Message{
- "a": "a1",
- "c": "c1",
- },
- },
- &xsql.Tuple{
- Emitter: "test",
- Message: xsql.Message{
- "a": "a1",
- "c": "c1",
- },
- },
- &xsql.Tuple{
- Emitter: "test",
- Message: xsql.Message{
- "a": "a1",
- "b": "b2",
- "c": "c2",
- },
- },
- },
- result: [][]map[string]interface{}{{{
- "changed_col": map[string]interface{}{
- "a": "a1",
- "b": "b1",
- },
- }}, {{
- "changed_col": map[string]interface{}{
- "a": "a1",
- "c": "c1",
- },
- }}, {{}}, {{
- "changed_col": map[string]interface{}{
- "a": "a1",
- "b": "b2",
- "c": "c2",
- },
- }}},
- },
- }
- fmt.Printf("The test bucket size is %d.\n\n", len(tests))
- contextLogger := conf.Log.WithField("rule", "TestChangedFuncs_Apply1")
- for i, tt := range tests {
- tempStore, _ := state.CreateStore("mockRule"+strconv.Itoa(i), api.AtMostOnce)
- ctx := context.WithValue(context.Background(), context.LoggerKey, contextLogger).WithMeta("mockRule"+strconv.Itoa(i), "project", tempStore)
- stmt, err := xsql.NewParser(strings.NewReader(tt.sql)).Parse()
- if err != nil || stmt == nil {
- t.Errorf("parse sql %s error %v", tt.sql, err)
- }
- pp := &ProjectOp{}
- parseStmt(pp, stmt.Fields)
- fv, afv := xsql.NewFunctionValuersForOp(ctx)
- r := make([][]map[string]interface{}, 0, len(tt.data))
- for _, d := range tt.data {
- opResult := pp.Apply(ctx, d, fv, afv)
- result, err := parseResult(opResult, pp.IsAggregate)
- if err != nil {
- t.Errorf("parse result error: %s", err)
- continue
- }
- r = append(r, result)
- }
- if !reflect.DeepEqual(tt.result, r) {
- t.Errorf("%d. %q\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.sql, tt.result, r)
- }
- }
- }
- func TestLagFuncs_Apply1(t *testing.T) {
- tests := []struct {
- sql string
- data []interface{}
- result [][]map[string]interface{}
- }{
- {
- sql: `SELECT lag(a) as a, lag(b) as b FROM test`,
- data: []interface{}{
- &xsql.Tuple{
- Emitter: "test",
- Message: xsql.Message{
- "a": "a1",
- "b": "b1",
- "c": "c1",
- },
- },
- &xsql.Tuple{
- Emitter: "test",
- Message: xsql.Message{
- "a": "a1",
- "b": "b2",
- "c": "c1",
- },
- },
- &xsql.Tuple{
- Emitter: "test",
- Message: xsql.Message{
- "a": "a1",
- "c": "c1",
- },
- },
- &xsql.Tuple{
- Emitter: "test",
- Message: xsql.Message{
- "a": "a1",
- "b": "b2",
- "c": "c2",
- },
- },
- },
- result: [][]map[string]interface{}{{{}}, {{
- "a": "a1",
- "b": "b1",
- }}, {{
- "a": "a1",
- "b": "b2",
- }}, {{
- "a": "a1",
- }}},
- },
- {
- sql: `SELECT lag(a, 2, "a10") as a FROM test`,
- data: []interface{}{
- &xsql.Tuple{
- Emitter: "test",
- Message: xsql.Message{
- "a": "a1",
- "b": "b1",
- },
- },
- &xsql.Tuple{
- Emitter: "test",
- Message: xsql.Message{
- "a": "a2",
- "c": "c1",
- },
- },
- &xsql.Tuple{
- Emitter: "test",
- Message: xsql.Message{
- "a": "a1",
- "c": "c1",
- },
- },
- &xsql.Tuple{
- Emitter: "test",
- Message: xsql.Message{
- "a": "a1",
- "b": "b2",
- "c": "c2",
- },
- },
- },
- result: [][]map[string]interface{}{{{
- "a": "a10",
- }}, {{
- "a": "a10",
- }}, {{
- "a": "a1",
- }}, {{
- "a": "a2",
- }}},
- },
- {
- sql: `SELECT lag(a, 2) as a FROM test`,
- data: []interface{}{
- &xsql.Tuple{
- Emitter: "test",
- Message: xsql.Message{
- "a": "a1",
- "b": "b1",
- },
- },
- &xsql.Tuple{
- Emitter: "test",
- Message: xsql.Message{
- "a": "a2",
- "c": "c1",
- },
- },
- &xsql.Tuple{
- Emitter: "test",
- Message: xsql.Message{
- "a": "a1",
- "c": "c1",
- },
- },
- &xsql.Tuple{
- Emitter: "test",
- Message: xsql.Message{
- "a": "a1",
- "b": "b2",
- "c": "c2",
- },
- },
- },
- result: [][]map[string]interface{}{{{}}, {{}}, {{
- "a": "a1",
- }}, {{
- "a": "a2",
- }}},
- },
- }
- fmt.Printf("The test bucket size is %d.\n\n", len(tests))
- contextLogger := conf.Log.WithField("rule", "TestChangedFuncs_Apply1")
- for i, tt := range tests {
- tempStore, _ := state.CreateStore("mockRule"+strconv.Itoa(i), api.AtMostOnce)
- ctx := context.WithValue(context.Background(), context.LoggerKey, contextLogger).WithMeta("mockRule"+strconv.Itoa(i), "project", tempStore)
- stmt, err := xsql.NewParser(strings.NewReader(tt.sql)).Parse()
- if err != nil || stmt == nil {
- t.Errorf("parse sql %s error %v", tt.sql, err)
- }
- pp := &ProjectOp{}
- parseStmt(pp, stmt.Fields)
- fv, afv := xsql.NewFunctionValuersForOp(ctx)
- r := make([][]map[string]interface{}, 0, len(tt.data))
- for _, d := range tt.data {
- opResult := pp.Apply(ctx, d, fv, afv)
- result, err := parseResult(opResult, pp.IsAggregate)
- if err != nil {
- t.Errorf("parse result error: %s", err)
- continue
- }
- r = append(r, result)
- }
- if !reflect.DeepEqual(tt.result, r) {
- t.Errorf("%d. %q\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.sql, tt.result, r)
- }
- }
- }
|