123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518 |
- // 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 xsql
- import (
- "fmt"
- "reflect"
- "strings"
- "testing"
- "github.com/lf-edge/ekuiper/internal/testx"
- "github.com/lf-edge/ekuiper/pkg/ast"
- )
- // Ensure the parser can parse strings into Statement ASTs.
- func TestFuncValidator(t *testing.T) {
- tests := []struct {
- s string
- stmt *ast.SelectStatement
- err string
- }{
- {
- s: `SELECT abs(1) FROM tbl`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{{AName: "", Name: "abs", Expr: &ast.Call{Name: "abs", Args: []ast.Expr{&ast.IntegerLiteral{Val: 1}}}}},
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- },
- {
- s: `SELECT abs(field1) FROM tbl`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{{AName: "", Name: "abs", Expr: &ast.Call{Name: "abs", Args: []ast.Expr{&ast.FieldRef{Name: "field1", StreamName: ast.DefaultStream}}}}},
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- },
- {
- s: `SELECT abs(1,2) FROM tbl`,
- stmt: nil,
- err: "Expect 1 arguments but found 2.",
- },
- {
- s: `SELECT abs(1.1) FROM tbl`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{{AName: "", Name: "abs", Expr: &ast.Call{Name: "abs", Args: []ast.Expr{&ast.NumberLiteral{Val: 1.1}}}}},
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- },
- {
- s: `SELECT abs(true) FROM tbl`,
- stmt: nil,
- err: "Expect number - float or int type for parameter 1",
- },
- {
- s: `SELECT abs("test") FROM tbl`,
- stmt: nil,
- err: "Expect number - float or int type for parameter 1",
- },
- {
- s: `SELECT abs(ss) FROM tbl`,
- stmt: nil,
- err: "Expect number - float or int type for parameter 1",
- },
- ///
- {
- s: `SELECT sin(1) FROM tbl`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{{AName: "", Name: "sin", Expr: &ast.Call{Name: "sin", Args: []ast.Expr{&ast.IntegerLiteral{Val: 1}}}}},
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- },
- {
- s: `SELECT sin(1.1) FROM tbl`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{{AName: "", Name: "sin", Expr: &ast.Call{Name: "sin", Args: []ast.Expr{&ast.NumberLiteral{Val: 1.1}}}}},
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- },
- {
- s: `SELECT sin(true) FROM tbl`,
- stmt: nil,
- err: "Expect number - float or int type for parameter 1",
- },
- {
- s: `SELECT sin("test") FROM tbl`,
- stmt: nil,
- err: "Expect number - float or int type for parameter 1",
- },
- {
- s: `SELECT sin(ss) FROM tbl`,
- stmt: nil,
- err: "Expect number - float or int type for parameter 1",
- },
- ///
- {
- s: `SELECT tanh(1) FROM tbl`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{{AName: "", Name: "tanh", Expr: &ast.Call{Name: "tanh", Args: []ast.Expr{&ast.IntegerLiteral{Val: 1}}}}},
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- },
- {
- s: `SELECT tanh(1.1) FROM tbl`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{{AName: "", Name: "tanh", Expr: &ast.Call{Name: "tanh", Args: []ast.Expr{&ast.NumberLiteral{Val: 1.1}}}}},
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- },
- {
- s: `SELECT tanh(true) FROM tbl`,
- stmt: nil,
- err: "Expect number - float or int type for parameter 1",
- },
- {
- s: `SELECT tanh("test") FROM tbl`,
- stmt: nil,
- err: "Expect number - float or int type for parameter 1",
- },
- {
- s: `SELECT tanh(ss) FROM tbl`,
- stmt: nil,
- err: "Expect number - float or int type for parameter 1",
- },
- ///
- {
- s: `SELECT bitxor(1, 2) FROM tbl`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{{AName: "", Name: "bitxor", Expr: &ast.Call{Name: "bitxor", Args: []ast.Expr{&ast.IntegerLiteral{Val: 1}, &ast.IntegerLiteral{Val: 2}}}}},
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- },
- {
- s: `SELECT bitxor(1.1, 2) FROM tbl`,
- stmt: nil,
- err: "Expect int type for parameter 1",
- },
- {
- s: `SELECT bitxor(true, 2) FROM tbl`,
- stmt: nil,
- err: "Expect int type for parameter 1",
- },
- {
- s: `SELECT bitxor(1, ss) FROM tbl`,
- stmt: nil,
- err: "Expect int type for parameter 2",
- },
- {
- s: `SELECT bitxor(1, 2.2) FROM tbl`,
- stmt: nil,
- err: "Expect int type for parameter 2",
- },
- ///
- {
- s: `SELECT bitnot(1) FROM tbl`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{{AName: "", Name: "bitnot", Expr: &ast.Call{Name: "bitnot", Args: []ast.Expr{&ast.IntegerLiteral{Val: 1}}}}},
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- },
- {
- s: `SELECT bitnot(1.1) FROM tbl`,
- stmt: nil,
- err: "Expect int type for parameter 1",
- },
- {
- s: `SELECT bitnot(true) FROM tbl`,
- stmt: nil,
- err: "Expect int type for parameter 1",
- },
- ///
- {
- s: `SELECT mod(1, 2) FROM tbl`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{{AName: "", Name: "mod", Expr: &ast.Call{Name: "mod", Args: []ast.Expr{&ast.IntegerLiteral{Val: 1}, &ast.IntegerLiteral{Val: 2}}}}},
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- },
- {
- s: `SELECT mod("1.1", 2) FROM tbl`,
- stmt: nil,
- err: "Expect number - float or int type for parameter 1",
- },
- {
- s: `SELECT mod(1.1, true) FROM tbl`,
- stmt: nil,
- err: "Expect number - float or int type for parameter 2",
- },
- {
- s: `SELECT mod(1, ss) FROM tbl`,
- stmt: nil,
- err: "Expect number - float or int type for parameter 2",
- },
- ///
- {
- s: `SELECT concat(field, "hello") FROM tbl`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{{AName: "", Name: "concat", Expr: &ast.Call{Name: "concat", Args: []ast.Expr{&ast.FieldRef{Name: "field", StreamName: ast.DefaultStream}, &ast.StringLiteral{Val: "hello"}}}}},
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- },
- {
- s: `SELECT concat("1.1", 2) FROM tbl`,
- stmt: nil,
- err: "Expect string type for parameter 2",
- },
- {
- s: `SELECT concat("1.1", true) FROM tbl`,
- stmt: nil,
- err: "Expect string type for parameter 2",
- },
- {
- s: `SELECT concat("1", ss) FROM tbl`,
- stmt: nil,
- err: "Expect string type for parameter 2",
- },
- ///
- {
- s: `SELECT regexp_matches(field, "hello") FROM tbl`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{{AName: "", Name: "regexp_matches", Expr: &ast.Call{Name: "regexp_matches", Args: []ast.Expr{&ast.FieldRef{Name: "field", StreamName: ast.DefaultStream}, &ast.StringLiteral{Val: "hello"}}}}},
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- },
- {
- s: `SELECT regexp_matches(1, "true") FROM tbl`,
- stmt: nil,
- err: "Expect string type for parameter 1",
- },
- {
- s: `SELECT regexp_matches("1.1", 2) FROM tbl`,
- stmt: nil,
- err: "Expect string type for parameter 2",
- },
- ///
- {
- s: `SELECT regexp_replace(field, "hello", "h") FROM tbl`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{{AName: "", Name: "regexp_replace", Expr: &ast.Call{Name: "regexp_replace", Args: []ast.Expr{&ast.FieldRef{Name: "field", StreamName: ast.DefaultStream}, &ast.StringLiteral{Val: "hello"}, &ast.StringLiteral{Val: "h"}}}}},
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- },
- {
- s: `SELECT regexp_replace(field1, "true", true) FROM tbl`,
- stmt: nil,
- err: "Expect string type for parameter 3",
- },
- ///
- {
- s: `SELECT trim(field) FROM tbl`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{{AName: "", Name: "trim", Expr: &ast.Call{Name: "trim", Args: []ast.Expr{&ast.FieldRef{Name: "field", StreamName: ast.DefaultStream}}}}},
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- },
- {
- s: `SELECT trim(1) FROM tbl`,
- stmt: nil,
- err: "Expect string type for parameter 1",
- },
- ///
- {
- s: `SELECT rpad(field, 3) FROM tbl`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{{AName: "", Name: "rpad", Expr: &ast.Call{Name: "rpad", Args: []ast.Expr{&ast.FieldRef{Name: "field", StreamName: ast.DefaultStream}, &ast.IntegerLiteral{Val: 3}}}}},
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- },
- {
- s: `SELECT rpad("ff", true) FROM tbl`,
- stmt: nil,
- err: "Expect int type for parameter 2",
- },
- ///
- {
- s: `SELECT substring(field, 3, 4) FROM tbl`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{{AName: "", Name: "substring", Expr: &ast.Call{Name: "substring", Args: []ast.Expr{&ast.FieldRef{Name: "field", StreamName: ast.DefaultStream}, &ast.IntegerLiteral{Val: 3}, &ast.IntegerLiteral{Val: 4}}}}},
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- },
- {
- s: `SELECT substring(field, -1, 4) FROM tbl`,
- stmt: nil,
- err: "The start index should not be a nagtive integer.",
- },
- {
- s: `SELECT substring(field, 0, -1) FROM tbl`,
- stmt: nil,
- err: "The end index should be larger than start index.",
- },
- {
- s: `SELECT substring(field, 0, true) FROM tbl`,
- stmt: nil,
- err: "Expect int type for parameter 3",
- },
- ///
- {
- s: `SELECT cast(field, "bigint") FROM tbl`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{{AName: "", Name: "cast", Expr: &ast.Call{Name: "cast", Args: []ast.Expr{&ast.FieldRef{Name: "field", StreamName: ast.DefaultStream}, &ast.StringLiteral{Val: "bigint"}}}}},
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- },
- {
- s: `SELECT cast("12", "bool") FROM tbl`,
- stmt: nil,
- err: "Expect one of following value for the 2nd parameter: bigint, float, string, boolean, datetime, bytea.",
- },
- ///
- {
- s: `SELECT chr(field) FROM tbl`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{{AName: "", Name: "chr", Expr: &ast.Call{Name: "chr", Args: []ast.Expr{&ast.FieldRef{Name: "field", StreamName: ast.DefaultStream}}}}},
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- },
- {
- s: `SELECT chr(true) FROM tbl`,
- stmt: nil,
- err: "Expect int type for parameter 1",
- },
- ///
- {
- s: `SELECT encode(field, "base64") FROM tbl`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{{AName: "", Name: "encode", Expr: &ast.Call{Name: "encode", Args: []ast.Expr{&ast.FieldRef{Name: "field", StreamName: ast.DefaultStream}, &ast.StringLiteral{Val: "base64"}}}}},
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- },
- {
- s: `SELECT encode(field, true) FROM tbl`,
- stmt: nil,
- err: "Expect string type for parameter 2",
- },
- ///
- {
- s: `SELECT trunc(field, 3) FROM tbl`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{{AName: "", Name: "trunc", Expr: &ast.Call{Name: "trunc", Args: []ast.Expr{&ast.FieldRef{Name: "field", StreamName: ast.DefaultStream}, &ast.IntegerLiteral{Val: 3}}}}},
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- },
- {
- s: `SELECT trunc(5, ss) FROM tbl`,
- stmt: nil,
- err: "Expect int type for parameter 2",
- },
- ///
- {
- s: `SELECT sha512(field) FROM tbl`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{{AName: "", Name: "sha512", Expr: &ast.Call{Name: "sha512", Args: []ast.Expr{&ast.FieldRef{Name: "field", StreamName: ast.DefaultStream}}}}},
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- },
- {
- s: `SELECT sha512(20) FROM tbl`,
- stmt: nil,
- err: "Expect string type for parameter 1",
- },
- {
- s: `SELECT mqtt("topic") FROM tbl`,
- stmt: nil,
- err: "Expect meta reference type for parameter 1",
- },
- {
- s: `SELECT mqtt(topic1) FROM tbl`,
- stmt: nil,
- err: "Parameter of mqtt function can be only topic or messageid.",
- },
- {
- s: `SELECT split_value(topic1) FROM tbl`,
- stmt: nil,
- err: "the arguments for split_value should be 3",
- },
- {
- s: `SELECT split_value(topic1, 3, 1) FROM tbl`,
- stmt: nil,
- err: "Expect string type for parameter 2",
- },
- {
- s: `SELECT split_value(topic1, "hello", -1) FROM tbl`,
- stmt: nil,
- err: "The index should not be a nagtive integer.",
- },
- {
- s: `SELECT meta(tbl, "timestamp", 1) FROM tbl`,
- stmt: nil,
- err: "Expect 1 arguments but found 3.",
- },
- {
- s: `SELECT meta("src1.device") FROM tbl`,
- stmt: nil,
- err: "Expect meta reference type for parameter 1",
- },
- {
- s: `SELECT meta(device) FROM tbl`,
- stmt: &ast.SelectStatement{Fields: []ast.Field{{AName: "", Name: "meta", Expr: &ast.Call{Name: "meta", Args: []ast.Expr{&ast.MetaRef{Name: "device", StreamName: ast.DefaultStream}}}}}, Sources: []ast.Source{&ast.Table{Name: "tbl"}}},
- },
- {
- s: `SELECT meta(tbl.device) FROM tbl`,
- stmt: &ast.SelectStatement{Fields: []ast.Field{{AName: "", Name: "meta", Expr: &ast.Call{Name: "meta", Args: []ast.Expr{&ast.MetaRef{StreamName: "tbl", Name: "device"}}}}}, Sources: []ast.Source{&ast.Table{Name: "tbl"}}},
- },
- {
- s: `SELECT meta(device->reading->topic) FROM tbl`,
- stmt: &ast.SelectStatement{Fields: []ast.Field{{AName: "", Name: "meta", Expr: &ast.Call{Name: "meta", Args: []ast.Expr{&ast.BinaryExpr{
- OP: ast.ARROW,
- LHS: &ast.BinaryExpr{
- OP: ast.ARROW,
- LHS: &ast.MetaRef{Name: "device", StreamName: ast.DefaultStream},
- RHS: &ast.JsonFieldRef{Name: "reading"},
- },
- RHS: &ast.JsonFieldRef{Name: "topic"},
- }}}}}, Sources: []ast.Source{&ast.Table{Name: "tbl"}}},
- },
- {
- s: `SELECT json_path_query(data, 44) AS data
- FROM characters;`,
- stmt: nil,
- err: "Expect string type for parameter 2",
- },
- {
- s: `SELECT collect() from tbl`,
- stmt: nil,
- err: "Expect 1 arguments but found 0.",
- },
- {
- s: `SELECT deduplicate(abc, temp, true) from tbl`,
- stmt: nil,
- err: "Expect 2 arguments but found 3.",
- },
- {
- s: `SELECT deduplicate(temp, "string") from tbl`,
- stmt: nil,
- err: "Expect bool type for parameter 2",
- },
- }
- fmt.Printf("The test bucket size is %d.\n\n", len(tests))
- for i, tt := range tests {
- // fmt.Printf("Parsing SQL %q.\n", tt.s)
- stmt, err := NewParser(strings.NewReader(tt.s)).Parse()
- if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
- t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.s, tt.err, err)
- } else if tt.err == "" && !reflect.DeepEqual(tt.stmt, stmt) {
- t.Errorf("%d. %q\n\nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.s, tt.stmt, stmt)
- }
- }
- }
|