parser_tree_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // Copyright 2021-2022 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. STRICT_VALIDATION: true,
  44. },
  45. },
  46. },
  47. {
  48. s: `CREATE STREAM demo (
  49. USERID BIGINT,
  50. ) WITH (DATASOURCE="users", FORMAT="PROTOBUF", KEY="USERID", SCHEMAID="proto1.Book", SHARED="true");`,
  51. stmt: &ast.StreamStmt{
  52. Name: ast.StreamName("demo"),
  53. StreamFields: []ast.StreamField{
  54. {Name: "USERID", FieldType: &ast.BasicType{Type: ast.BIGINT}},
  55. },
  56. Options: &ast.Options{
  57. DATASOURCE: "users",
  58. FORMAT: "PROTOBUF",
  59. SCHEMAID: "proto1.Book",
  60. KEY: "USERID",
  61. SHARED: true,
  62. STRICT_VALIDATION: true,
  63. },
  64. },
  65. },
  66. {
  67. s: `CREATE TABLE demo (
  68. USERID BIGINT,
  69. ) WITH (DATASOURCE="users", FORMAT="JSON", KEY="USERID", RETAIN_SIZE="3");`,
  70. stmt: &ast.StreamStmt{
  71. Name: ast.StreamName("demo"),
  72. StreamFields: []ast.StreamField{
  73. {Name: "USERID", FieldType: &ast.BasicType{Type: ast.BIGINT}},
  74. },
  75. Options: &ast.Options{
  76. DATASOURCE: "users",
  77. FORMAT: "JSON",
  78. KEY: "USERID",
  79. RETAIN_SIZE: 3,
  80. STRICT_VALIDATION: true,
  81. },
  82. StreamType: ast.TypeTable,
  83. },
  84. },
  85. {
  86. s: `CREATE TABLE table1 (
  87. name STRING,
  88. size BIGINT,
  89. id BIGINT
  90. ) WITH (DATASOURCE="lookup.json", FORMAT="json", CONF_KEY="test");`,
  91. stmt: &ast.StreamStmt{
  92. Name: ast.StreamName("table1"),
  93. StreamFields: []ast.StreamField{
  94. {Name: "name", FieldType: &ast.BasicType{Type: ast.STRINGS}},
  95. {Name: "size", FieldType: &ast.BasicType{Type: ast.BIGINT}},
  96. {Name: "id", FieldType: &ast.BasicType{Type: ast.BIGINT}},
  97. },
  98. Options: &ast.Options{
  99. DATASOURCE: "lookup.json",
  100. FORMAT: "json",
  101. CONF_KEY: "test",
  102. STRICT_VALIDATION: true,
  103. },
  104. StreamType: ast.TypeTable,
  105. },
  106. },
  107. {
  108. s: `SHOW STREAMS`,
  109. stmt: &ast.ShowStreamsStatement{},
  110. },
  111. {
  112. s: `SHOW TABLES`,
  113. stmt: &ast.ShowTablesStatement{},
  114. },
  115. {
  116. s: `SHOW STREAMSf`,
  117. stmt: nil,
  118. err: `found "STREAMSf", expected keyword streams or tables.`,
  119. },
  120. {
  121. s: `SHOW STREAMS d`,
  122. stmt: nil,
  123. err: `found "d", expected semecolon or EOF.`,
  124. },
  125. {
  126. s: `DESCRIBE STREAM demo`,
  127. stmt: &ast.DescribeStreamStatement{
  128. Name: "demo",
  129. },
  130. err: ``,
  131. },
  132. {
  133. s: `EXPLAIN STREAM demo1`,
  134. stmt: &ast.ExplainStreamStatement{
  135. Name: "demo1",
  136. },
  137. err: ``,
  138. },
  139. {
  140. s: `DROP STREAM demo1`,
  141. stmt: &ast.DropStreamStatement{
  142. Name: "demo1",
  143. },
  144. err: ``,
  145. },
  146. {
  147. s: `DESCRIBE TABLE demo`,
  148. stmt: &ast.DescribeTableStatement{
  149. Name: "demo",
  150. },
  151. err: ``,
  152. },
  153. {
  154. s: `EXPLAIN TABLE demo1`,
  155. stmt: &ast.ExplainTableStatement{
  156. Name: "demo1",
  157. },
  158. err: ``,
  159. },
  160. {
  161. s: `DROP TABLE demo1`,
  162. stmt: &ast.DropTableStatement{
  163. Name: "demo1",
  164. },
  165. err: ``,
  166. },
  167. }
  168. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  169. for i, tt := range tests {
  170. p := NewParser(strings.NewReader(tt.s))
  171. stmt, err := Language.Parse(p)
  172. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  173. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.s, tt.err, err)
  174. } else if tt.err == "" && !reflect.DeepEqual(tt.stmt, stmt) {
  175. t.Errorf("%d. %q\n\nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.s, tt.stmt, stmt)
  176. }
  177. }
  178. }