123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434 |
- package xsql
- import (
- "fmt"
- "reflect"
- "strings"
- "testing"
- )
- // Ensure the parser can parse strings into Statement ASTs.
- func TestFuncValidator(t *testing.T) {
- var tests = []struct {
- s string
- stmt *SelectStatement
- err string
- }{
- {
- s: `SELECT abs(1) FROM tbl`,
- stmt: &SelectStatement{Fields: []Field{Field{ AName:"", Name: "abs", Expr:&Call{Name:"abs", Args: []Expr{&IntegerLiteral{Val:1}}}}},
- Sources: []Source{&Table{Name:"tbl"}},
- },
- },
- {
- s: `SELECT abs(field1) FROM tbl`,
- stmt: &SelectStatement{Fields: []Field{Field{ AName:"", Name: "abs", Expr:&Call{Name:"abs", Args: []Expr{&FieldRef{Name:"field1"}}}}},
- Sources: []Source{&Table{Name:"tbl"}},
- },
- },
- {
- s: `SELECT abs(1,2) FROM tbl`,
- stmt: nil,
- err: "The arguments for abs should be 1.",
- },
- {
- s: `SELECT abs(1.1) FROM tbl`,
- stmt: &SelectStatement{Fields: []Field{Field{ AName:"", Name: "abs", Expr:&Call{Name:"abs", Args: []Expr{&NumberLiteral{Val:1.1}}}}},
- Sources: []Source{&Table{Name:"tbl"}},
- },
- },
- {
- s: `SELECT abs(true) FROM tbl`,
- stmt: nil,
- err: "Expect number - float or int type for 1 parameter of function abs.",
- },
- {
- s: `SELECT abs("test") FROM tbl`,
- stmt: nil,
- err: "Expect number - float or int type for 1 parameter of function abs.",
- },
- {
- s: `SELECT abs(ss) FROM tbl`,
- stmt: nil,
- err: "Expect number - float or int type for 1 parameter of function abs.",
- },
- ///
- {
- s: `SELECT sin(1) FROM tbl`,
- stmt: &SelectStatement{Fields: []Field{Field{ AName:"", Name: "sin", Expr:&Call{Name:"sin", Args: []Expr{&IntegerLiteral{Val:1}}}}},
- Sources: []Source{&Table{Name:"tbl"}},
- },
- },
- {
- s: `SELECT sin(1.1) FROM tbl`,
- stmt: &SelectStatement{Fields: []Field{Field{ AName:"", Name: "sin", Expr:&Call{Name:"sin", Args: []Expr{&NumberLiteral{Val:1.1}}}}},
- Sources: []Source{&Table{Name:"tbl"}},
- },
- },
- {
- s: `SELECT sin(true) FROM tbl`,
- stmt: nil,
- err: "Expect number - float or int type for 1 parameter of function sin.",
- },
- {
- s: `SELECT sin("test") FROM tbl`,
- stmt: nil,
- err: "Expect number - float or int type for 1 parameter of function sin.",
- },
- {
- s: `SELECT sin(ss) FROM tbl`,
- stmt: nil,
- err: "Expect number - float or int type for 1 parameter of function sin.",
- },
- ///
- {
- s: `SELECT tanh(1) FROM tbl`,
- stmt: &SelectStatement{Fields: []Field{Field{ AName:"", Name: "tanh", Expr:&Call{Name:"tanh", Args: []Expr{&IntegerLiteral{Val:1}}}}},
- Sources: []Source{&Table{Name:"tbl"}},
- },
- },
- {
- s: `SELECT tanh(1.1) FROM tbl`,
- stmt: &SelectStatement{Fields: []Field{Field{ AName:"", Name: "tanh", Expr:&Call{Name:"tanh", Args: []Expr{&NumberLiteral{Val:1.1}}}}},
- Sources: []Source{&Table{Name:"tbl"}},
- },
- },
- {
- s: `SELECT tanh(true) FROM tbl`,
- stmt: nil,
- err: "Expect number - float or int type for 1 parameter of function tanh.",
- },
- {
- s: `SELECT tanh("test") FROM tbl`,
- stmt: nil,
- err: "Expect number - float or int type for 1 parameter of function tanh.",
- },
- {
- s: `SELECT tanh(ss) FROM tbl`,
- stmt: nil,
- err: "Expect number - float or int type for 1 parameter of function tanh.",
- },
- ///
- {
- s: `SELECT bitxor(1, 2) FROM tbl`,
- stmt: &SelectStatement{Fields: []Field{Field{ AName:"", Name: "bitxor", Expr:&Call{Name:"bitxor", Args: []Expr{&IntegerLiteral{Val:1}, &IntegerLiteral{Val:2}}}}},
- Sources: []Source{&Table{Name:"tbl"}},
- },
- },
- {
- s: `SELECT bitxor(1.1, 2) FROM tbl`,
- stmt: nil,
- err: "Expect int type for 1 parameter of function bitxor.",
- },
- {
- s: `SELECT bitxor(true, 2) FROM tbl`,
- stmt: nil,
- err: "Expect int type for 1 parameter of function bitxor.",
- },
- {
- s: `SELECT bitxor(1, ss) FROM tbl`,
- stmt: nil,
- err: "Expect int type for 2 parameter of function bitxor.",
- },
- {
- s: `SELECT bitxor(1, 2.2) FROM tbl`,
- stmt: nil,
- err: "Expect int type for 2 parameter of function bitxor.",
- },
- ///
- {
- s: `SELECT bitnot(1) FROM tbl`,
- stmt: &SelectStatement{Fields: []Field{Field{ AName:"", Name: "bitnot", Expr:&Call{Name:"bitnot", Args: []Expr{&IntegerLiteral{Val:1}}}}},
- Sources: []Source{&Table{Name:"tbl"}},
- },
- },
- {
- s: `SELECT bitnot(1.1) FROM tbl`,
- stmt: nil,
- err: "Expect int type for 1 parameter of function bitnot.",
- },
- {
- s: `SELECT bitnot(true) FROM tbl`,
- stmt: nil,
- err: "Expect int type for 1 parameter of function bitnot.",
- },
- ///
- {
- s: `SELECT mod(1, 2) FROM tbl`,
- stmt: &SelectStatement{Fields: []Field{Field{ AName:"", Name: "mod", Expr:&Call{Name:"mod", Args: []Expr{&IntegerLiteral{Val:1}, &IntegerLiteral{Val:2}}}}},
- Sources: []Source{&Table{Name:"tbl"}},
- },
- },
- {
- s: `SELECT mod("1.1", 2) FROM tbl`,
- stmt: nil,
- err: "Expect number - float or int type for 1 parameter of function mod.",
- },
- {
- s: `SELECT mod(1.1, true) FROM tbl`,
- stmt: nil,
- err: "Expect number - float or int type for 2 parameter of function mod.",
- },
- {
- s: `SELECT mod(1, ss) FROM tbl`,
- stmt: nil,
- err: "Expect number - float or int type for 2 parameter of function mod.",
- },
- ///
- {
- s: `SELECT concat(field, "hello") FROM tbl`,
- stmt: &SelectStatement{Fields: []Field{Field{ AName:"", Name: "concat", Expr:&Call{Name:"concat", Args: []Expr{&FieldRef{Name:"field"}, &StringLiteral{Val:"hello"}}}}},
- Sources: []Source{&Table{Name:"tbl"}},
- },
- },
- {
- s: `SELECT concat("1.1", 2) FROM tbl`,
- stmt: nil,
- err: "Expect string type for 2 parameter of function concat.",
- },
- {
- s: `SELECT concat("1.1", true) FROM tbl`,
- stmt: nil,
- err: "Expect string type for 2 parameter of function concat.",
- },
- {
- s: `SELECT concat("1", ss) FROM tbl`,
- stmt: nil,
- err: "Expect string type for 2 parameter of function concat.",
- },
- ///
- {
- s: `SELECT regexp_matches(field, "hello") FROM tbl`,
- stmt: &SelectStatement{Fields: []Field{Field{ AName:"", Name: "regexp_matches", Expr:&Call{Name:"regexp_matches", Args: []Expr{&FieldRef{Name:"field"}, &StringLiteral{Val:"hello"}}}}},
- Sources: []Source{&Table{Name:"tbl"}},
- },
- },
- {
- s: `SELECT regexp_matches(1, "true") FROM tbl`,
- stmt: nil,
- err: "Expect string type for 1 parameter of function regexp_matches.",
- },
- {
- s: `SELECT regexp_matches("1.1", 2) FROM tbl`,
- stmt: nil,
- err: "Expect string type for 2 parameter of function regexp_matches.",
- },
- ///
- {
- s: `SELECT regexp_replace(field, "hello", "h") FROM tbl`,
- stmt: &SelectStatement{Fields: []Field{Field{ AName:"", Name: "regexp_replace", Expr:&Call{Name:"regexp_replace", Args: []Expr{&FieldRef{Name:"field"}, &StringLiteral{Val:"hello"}, &StringLiteral{Val:"h"}}}}},
- Sources: []Source{&Table{Name:"tbl"}},
- },
- },
- {
- s: `SELECT regexp_replace(field1, "true", true) FROM tbl`,
- stmt: nil,
- err: "Expect string type for 3 parameter of function regexp_replace.",
- },
- ///
- {
- s: `SELECT trim(field) FROM tbl`,
- stmt: &SelectStatement{Fields: []Field{Field{ AName:"", Name: "trim", Expr:&Call{Name:"trim", Args: []Expr{&FieldRef{Name:"field"}}}}},
- Sources: []Source{&Table{Name:"tbl"}},
- },
- },
- {
- s: `SELECT trim(1) FROM tbl`,
- stmt: nil,
- err: "Expect string type for 1 parameter of function trim.",
- },
- ///
- {
- s: `SELECT rpad(field, 3) FROM tbl`,
- stmt: &SelectStatement{Fields: []Field{Field{ AName:"", Name: "rpad", Expr:&Call{Name:"rpad", Args: []Expr{&FieldRef{Name:"field"}, &IntegerLiteral{Val:3}}}}},
- Sources: []Source{&Table{Name:"tbl"}},
- },
- },
- {
- s: `SELECT rpad("ff", true) FROM tbl`,
- stmt: nil,
- err: "Expect int type for 2 parameter of function rpad.",
- },
- ///
- {
- s: `SELECT substring(field, 3, 4) FROM tbl`,
- stmt: &SelectStatement{Fields: []Field{Field{ AName:"", Name: "substring", Expr:&Call{Name:"substring", Args: []Expr{&FieldRef{Name:"field"}, &IntegerLiteral{Val:3}, &IntegerLiteral{Val:4}}}}},
- Sources: []Source{&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 3 parameter of function substring.",
- },
- ///
- {
- s: `SELECT cast(field, "bigint") FROM tbl`,
- stmt: &SelectStatement{Fields: []Field{Field{ AName:"", Name: "cast", Expr:&Call{Name:"cast", Args: []Expr{&FieldRef{Name:"field"}, &StringLiteral{Val:"bigint"}}}}},
- Sources: []Source{&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.",
- },
- ///
- {
- s: `SELECT chr(field) FROM tbl`,
- stmt: &SelectStatement{Fields: []Field{Field{ AName:"", Name: "chr", Expr:&Call{Name:"chr", Args: []Expr{&FieldRef{Name:"field"}}}}},
- Sources: []Source{&Table{Name:"tbl"}},
- },
- },
- {
- s: `SELECT chr(true) FROM tbl`,
- stmt: nil,
- err: "Expect int type for 1 parameter of function chr.",
- },
- ///
- {
- s: `SELECT encode(field, "base64") FROM tbl`,
- stmt: &SelectStatement{Fields: []Field{Field{ AName:"", Name: "encode", Expr:&Call{Name:"encode", Args: []Expr{&FieldRef{Name:"field"}, &StringLiteral{Val:"base64"}}}}},
- Sources: []Source{&Table{Name:"tbl"}},
- },
- },
- {
- s: `SELECT encode(field, true) FROM tbl`,
- stmt: nil,
- err: "Expect string type for 2 parameter of function encode.",
- },
- ///
- {
- s: `SELECT trunc(field, 3) FROM tbl`,
- stmt: &SelectStatement{Fields: []Field{Field{ AName:"", Name: "trunc", Expr:&Call{Name:"trunc", Args: []Expr{&FieldRef{Name:"field"}, &IntegerLiteral{Val:3}}}}},
- Sources: []Source{&Table{Name:"tbl"}},
- },
- },
- {
- s: `SELECT trunc(5, ss) FROM tbl`,
- stmt: nil,
- err: "Expect int type for 2 parameter of function trunc.",
- },
- ///
- {
- s: `SELECT sha512(field) FROM tbl`,
- stmt: &SelectStatement{Fields: []Field{Field{ AName:"", Name: "sha512", Expr:&Call{Name:"sha512", Args: []Expr{&FieldRef{Name:"field"}}}}},
- Sources: []Source{&Table{Name:"tbl"}},
- },
- },
- {
- s: `SELECT sha512(20) FROM tbl`,
- stmt: nil,
- err: "Expect string type for 1 parameter of function sha512.",
- },
- {
- s: `SELECT mqtt("topic") FROM tbl`,
- stmt: nil,
- err: "Expect field reference type for 1 parameter of function mqtt.",
- },
- {
- 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 2 parameter of function split_value.",
- },
- {
- s: `SELECT split_value(topic1, "hello", -1) FROM tbl`,
- stmt: nil,
- err: "The index should not be a nagtive integer.",
- },
- }
- 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, 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)
- }
- }
- }
|