parser_tree_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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: `CREATE TABLE table1 (
  109. name STRING,
  110. size BIGINT,
  111. id BIGINT
  112. ) WITH (DATASOURCE="devices", KIND="LOOKUP", TYPE="sql");`,
  113. stmt: &ast.StreamStmt{
  114. Name: ast.StreamName("table1"),
  115. StreamFields: []ast.StreamField{
  116. {Name: "name", FieldType: &ast.BasicType{Type: ast.STRINGS}},
  117. {Name: "size", FieldType: &ast.BasicType{Type: ast.BIGINT}},
  118. {Name: "id", FieldType: &ast.BasicType{Type: ast.BIGINT}},
  119. },
  120. Options: &ast.Options{
  121. DATASOURCE: "devices",
  122. STRICT_VALIDATION: true,
  123. KIND: ast.StreamKindLookup,
  124. TYPE: "sql",
  125. },
  126. StreamType: ast.TypeTable,
  127. },
  128. },
  129. {
  130. s: `SHOW STREAMS`,
  131. stmt: &ast.ShowStreamsStatement{},
  132. },
  133. {
  134. s: `SHOW TABLES`,
  135. stmt: &ast.ShowTablesStatement{},
  136. },
  137. {
  138. s: `SHOW STREAMSf`,
  139. stmt: nil,
  140. err: `found "STREAMSf", expected keyword streams or tables.`,
  141. },
  142. {
  143. s: `SHOW STREAMS d`,
  144. stmt: nil,
  145. err: `found "d", expected semecolon or EOF.`,
  146. },
  147. {
  148. s: `DESCRIBE STREAM demo`,
  149. stmt: &ast.DescribeStreamStatement{
  150. Name: "demo",
  151. },
  152. err: ``,
  153. },
  154. {
  155. s: `EXPLAIN STREAM demo1`,
  156. stmt: &ast.ExplainStreamStatement{
  157. Name: "demo1",
  158. },
  159. err: ``,
  160. },
  161. {
  162. s: `DROP STREAM demo1`,
  163. stmt: &ast.DropStreamStatement{
  164. Name: "demo1",
  165. },
  166. err: ``,
  167. },
  168. {
  169. s: `DESCRIBE TABLE demo`,
  170. stmt: &ast.DescribeTableStatement{
  171. Name: "demo",
  172. },
  173. err: ``,
  174. },
  175. {
  176. s: `EXPLAIN TABLE demo1`,
  177. stmt: &ast.ExplainTableStatement{
  178. Name: "demo1",
  179. },
  180. err: ``,
  181. },
  182. {
  183. s: `DROP TABLE demo1`,
  184. stmt: &ast.DropTableStatement{
  185. Name: "demo1",
  186. },
  187. err: ``,
  188. },
  189. }
  190. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  191. for i, tt := range tests {
  192. p := NewParser(strings.NewReader(tt.s))
  193. stmt, err := Language.Parse(p)
  194. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  195. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.s, tt.err, err)
  196. } else if tt.err == "" && !reflect.DeepEqual(tt.stmt, stmt) {
  197. t.Errorf("%d. %q\n\nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.s, tt.stmt, stmt)
  198. }
  199. }
  200. }