parser_tree_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // Copyright 2021 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package xsql
  15. import (
  16. "fmt"
  17. "github.com/lf-edge/ekuiper/internal/testx"
  18. "github.com/lf-edge/ekuiper/pkg/ast"
  19. "reflect"
  20. "strings"
  21. "testing"
  22. )
  23. func TestParser_ParseTree(t *testing.T) {
  24. var tests = []struct {
  25. s string
  26. stmt ast.Statement
  27. err string
  28. }{
  29. {
  30. s: `CREATE STREAM demo (
  31. USERID BIGINT,
  32. ) WITH (DATASOURCE="users", FORMAT="JSON", KEY="USERID", SHARED="true");`,
  33. stmt: &ast.StreamStmt{
  34. Name: ast.StreamName("demo"),
  35. StreamFields: []ast.StreamField{
  36. {Name: "USERID", FieldType: &ast.BasicType{Type: ast.BIGINT}},
  37. },
  38. Options: &ast.Options{
  39. DATASOURCE: "users",
  40. FORMAT: "JSON",
  41. KEY: "USERID",
  42. SHARED: true,
  43. },
  44. },
  45. },
  46. {
  47. s: `CREATE TABLE demo (
  48. USERID BIGINT,
  49. ) WITH (DATASOURCE="users", FORMAT="JSON", KEY="USERID", RETAIN_SIZE="3");`,
  50. stmt: &ast.StreamStmt{
  51. Name: ast.StreamName("demo"),
  52. StreamFields: []ast.StreamField{
  53. {Name: "USERID", FieldType: &ast.BasicType{Type: ast.BIGINT}},
  54. },
  55. Options: &ast.Options{
  56. DATASOURCE: "users",
  57. FORMAT: "JSON",
  58. KEY: "USERID",
  59. RETAIN_SIZE: 3,
  60. },
  61. StreamType: ast.TypeTable,
  62. },
  63. },
  64. {
  65. s: `CREATE TABLE table1 (
  66. name STRING,
  67. size BIGINT,
  68. id BIGINT
  69. ) WITH (DATASOURCE="lookup.json", FORMAT="json", CONF_KEY="test");`,
  70. stmt: &ast.StreamStmt{
  71. Name: ast.StreamName("table1"),
  72. StreamFields: []ast.StreamField{
  73. {Name: "name", FieldType: &ast.BasicType{Type: ast.STRINGS}},
  74. {Name: "size", FieldType: &ast.BasicType{Type: ast.BIGINT}},
  75. {Name: "id", FieldType: &ast.BasicType{Type: ast.BIGINT}},
  76. },
  77. Options: &ast.Options{
  78. DATASOURCE: "lookup.json",
  79. FORMAT: "json",
  80. CONF_KEY: "test",
  81. },
  82. StreamType: ast.TypeTable,
  83. },
  84. },
  85. {
  86. s: `SHOW STREAMS`,
  87. stmt: &ast.ShowStreamsStatement{},
  88. },
  89. {
  90. s: `SHOW TABLES`,
  91. stmt: &ast.ShowTablesStatement{},
  92. },
  93. {
  94. s: `SHOW STREAMSf`,
  95. stmt: nil,
  96. err: `found "STREAMSf", expected keyword streams or tables.`,
  97. },
  98. {
  99. s: `SHOW STREAMS d`,
  100. stmt: nil,
  101. err: `found "d", expected semecolon or EOF.`,
  102. },
  103. {
  104. s: `DESCRIBE STREAM demo`,
  105. stmt: &ast.DescribeStreamStatement{
  106. Name: "demo",
  107. },
  108. err: ``,
  109. },
  110. {
  111. s: `EXPLAIN STREAM demo1`,
  112. stmt: &ast.ExplainStreamStatement{
  113. Name: "demo1",
  114. },
  115. err: ``,
  116. },
  117. {
  118. s: `DROP STREAM demo1`,
  119. stmt: &ast.DropStreamStatement{
  120. Name: "demo1",
  121. },
  122. err: ``,
  123. },
  124. {
  125. s: `DESCRIBE TABLE demo`,
  126. stmt: &ast.DescribeTableStatement{
  127. Name: "demo",
  128. },
  129. err: ``,
  130. },
  131. {
  132. s: `EXPLAIN TABLE demo1`,
  133. stmt: &ast.ExplainTableStatement{
  134. Name: "demo1",
  135. },
  136. err: ``,
  137. },
  138. {
  139. s: `DROP TABLE demo1`,
  140. stmt: &ast.DropTableStatement{
  141. Name: "demo1",
  142. },
  143. err: ``,
  144. },
  145. }
  146. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  147. for i, tt := range tests {
  148. p := NewParser(strings.NewReader(tt.s))
  149. stmt, err := Language.Parse(p)
  150. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  151. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.s, tt.err, err)
  152. } else if tt.err == "" && !reflect.DeepEqual(tt.stmt, stmt) {
  153. t.Errorf("%d. %q\n\nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.s, tt.stmt, stmt)
  154. }
  155. }
  156. }