123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216 |
- // Copyright 2021-2022 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"
- )
- func TestParser_ParseTree(t *testing.T) {
- tests := []struct {
- s string
- stmt ast.Statement
- err string
- }{
- {
- s: `CREATE STREAM demo (
- USERID BIGINT,
- ) WITH (DATASOURCE="users", FORMAT="JSON", KEY="USERID", SHARED="true");`,
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "USERID", FieldType: &ast.BasicType{Type: ast.BIGINT}},
- },
- Options: &ast.Options{
- DATASOURCE: "users",
- FORMAT: "JSON",
- KEY: "USERID",
- SHARED: true,
- },
- },
- },
- {
- s: `CREATE STREAM demo (
- USERID BIGINT,
- ) WITH (DATASOURCE="users", FORMAT="PROTOBUF", KEY="USERID", SCHEMAID="proto1.Book", SHARED="true");`,
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "USERID", FieldType: &ast.BasicType{Type: ast.BIGINT}},
- },
- Options: &ast.Options{
- DATASOURCE: "users",
- FORMAT: "PROTOBUF",
- SCHEMAID: "proto1.Book",
- KEY: "USERID",
- SHARED: true,
- },
- },
- },
- {
- s: `CREATE TABLE demo (
- USERID BIGINT,
- ) WITH (DATASOURCE="users", FORMAT="JSON", KEY="USERID", RETAIN_SIZE="3");`,
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("demo"),
- StreamFields: []ast.StreamField{
- {Name: "USERID", FieldType: &ast.BasicType{Type: ast.BIGINT}},
- },
- Options: &ast.Options{
- DATASOURCE: "users",
- FORMAT: "JSON",
- KEY: "USERID",
- RETAIN_SIZE: 3,
- },
- StreamType: ast.TypeTable,
- },
- },
- {
- s: `CREATE TABLE table1 (
- name STRING,
- size BIGINT,
- id BIGINT
- ) WITH (DATASOURCE="lookup.json", FORMAT="json", CONF_KEY="test");`,
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("table1"),
- StreamFields: []ast.StreamField{
- {Name: "name", FieldType: &ast.BasicType{Type: ast.STRINGS}},
- {Name: "size", FieldType: &ast.BasicType{Type: ast.BIGINT}},
- {Name: "id", FieldType: &ast.BasicType{Type: ast.BIGINT}},
- },
- Options: &ast.Options{
- DATASOURCE: "lookup.json",
- FORMAT: "json",
- CONF_KEY: "test",
- },
- StreamType: ast.TypeTable,
- },
- },
- {
- s: `CREATE TABLE table1 (
- name STRING,
- size BIGINT,
- id BIGINT
- ) WITH (DATASOURCE="devices", KIND="LOOKUP", TYPE="sql", KEY="id");`,
- stmt: &ast.StreamStmt{
- Name: ast.StreamName("table1"),
- StreamFields: []ast.StreamField{
- {Name: "name", FieldType: &ast.BasicType{Type: ast.STRINGS}},
- {Name: "size", FieldType: &ast.BasicType{Type: ast.BIGINT}},
- {Name: "id", FieldType: &ast.BasicType{Type: ast.BIGINT}},
- },
- Options: &ast.Options{
- DATASOURCE: "devices",
- KIND: ast.StreamKindLookup,
- TYPE: "sql",
- KEY: "id",
- },
- StreamType: ast.TypeTable,
- },
- },
- {
- s: `CREATE TABLE table1 (
- name STRING,
- size BIGINT,
- id BIGINT
- ) WITH (DATASOURCE="devices", KIND="LOOKUP", TYPE="memory");`,
- err: `Option "key" is required for memory lookup table.`,
- },
- {
- s: `SHOW STREAMS`,
- stmt: &ast.ShowStreamsStatement{},
- },
- {
- s: `SHOW TABLES`,
- stmt: &ast.ShowTablesStatement{},
- },
- {
- s: `SHOW STREAMSf`,
- stmt: nil,
- err: `found "STREAMSF", expected keyword streams or tables.`,
- },
- {
- s: `SHOW STREAMS d`,
- stmt: nil,
- err: `found "d", expected semecolon or EOF.`,
- },
- {
- s: `DESCRIBE STREAM demo`,
- stmt: &ast.DescribeStreamStatement{
- Name: "demo",
- },
- err: ``,
- },
- {
- s: `EXPLAIN STREAM demo1`,
- stmt: &ast.ExplainStreamStatement{
- Name: "demo1",
- },
- err: ``,
- },
- {
- s: `DROP STREAM demo1`,
- stmt: &ast.DropStreamStatement{
- Name: "demo1",
- },
- err: ``,
- },
- {
- s: `DESCRIBE TABLE demo`,
- stmt: &ast.DescribeTableStatement{
- Name: "demo",
- },
- err: ``,
- },
- {
- s: `EXPLAIN TABLE demo1`,
- stmt: &ast.ExplainTableStatement{
- Name: "demo1",
- },
- err: ``,
- },
- {
- s: `DROP TABLE demo1`,
- stmt: &ast.DropTableStatement{
- Name: "demo1",
- },
- err: ``,
- },
- }
- fmt.Printf("The test bucket size is %d.\n\n", len(tests))
- for i, tt := range tests {
- p := NewParser(strings.NewReader(tt.s))
- stmt, err := Language.Parse(p)
- 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)
- }
- }
- }
|