parser_tree_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. "reflect"
  18. "strings"
  19. "testing"
  20. "github.com/lf-edge/ekuiper/internal/testx"
  21. "github.com/lf-edge/ekuiper/pkg/ast"
  22. )
  23. func TestParser_ParseTree(t *testing.T) {
  24. 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 STREAM demo (
  48. USERID BIGINT,
  49. ) WITH (DATASOURCE="users", FORMAT="PROTOBUF", KEY="USERID", SCHEMAID="proto1.Book", SHARED="true");`,
  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: "PROTOBUF",
  58. SCHEMAID: "proto1.Book",
  59. KEY: "USERID",
  60. SHARED: true,
  61. },
  62. },
  63. },
  64. {
  65. s: `CREATE TABLE demo (
  66. USERID BIGINT,
  67. ) WITH (DATASOURCE="users", FORMAT="JSON", KEY="USERID", RETAIN_SIZE="3");`,
  68. stmt: &ast.StreamStmt{
  69. Name: ast.StreamName("demo"),
  70. StreamFields: []ast.StreamField{
  71. {Name: "USERID", FieldType: &ast.BasicType{Type: ast.BIGINT}},
  72. },
  73. Options: &ast.Options{
  74. DATASOURCE: "users",
  75. FORMAT: "JSON",
  76. KEY: "USERID",
  77. RETAIN_SIZE: 3,
  78. },
  79. StreamType: ast.TypeTable,
  80. },
  81. },
  82. {
  83. s: `CREATE TABLE table1 (
  84. name STRING,
  85. size BIGINT,
  86. id BIGINT
  87. ) WITH (DATASOURCE="lookup.json", FORMAT="json", CONF_KEY="test");`,
  88. stmt: &ast.StreamStmt{
  89. Name: ast.StreamName("table1"),
  90. StreamFields: []ast.StreamField{
  91. {Name: "name", FieldType: &ast.BasicType{Type: ast.STRINGS}},
  92. {Name: "size", FieldType: &ast.BasicType{Type: ast.BIGINT}},
  93. {Name: "id", FieldType: &ast.BasicType{Type: ast.BIGINT}},
  94. },
  95. Options: &ast.Options{
  96. DATASOURCE: "lookup.json",
  97. FORMAT: "json",
  98. CONF_KEY: "test",
  99. },
  100. StreamType: ast.TypeTable,
  101. },
  102. },
  103. {
  104. s: `CREATE TABLE table1 (
  105. name STRING,
  106. size BIGINT,
  107. id BIGINT
  108. ) WITH (DATASOURCE="devices", KIND="LOOKUP", TYPE="sql", KEY="id");`,
  109. stmt: &ast.StreamStmt{
  110. Name: ast.StreamName("table1"),
  111. StreamFields: []ast.StreamField{
  112. {Name: "name", FieldType: &ast.BasicType{Type: ast.STRINGS}},
  113. {Name: "size", FieldType: &ast.BasicType{Type: ast.BIGINT}},
  114. {Name: "id", FieldType: &ast.BasicType{Type: ast.BIGINT}},
  115. },
  116. Options: &ast.Options{
  117. DATASOURCE: "devices",
  118. KIND: ast.StreamKindLookup,
  119. TYPE: "sql",
  120. KEY: "id",
  121. },
  122. StreamType: ast.TypeTable,
  123. },
  124. },
  125. {
  126. s: `CREATE TABLE table1 (
  127. name STRING,
  128. size BIGINT,
  129. id BIGINT
  130. ) WITH (DATASOURCE="devices", KIND="LOOKUP", TYPE="memory");`,
  131. err: `Option "key" is required for memory lookup table.`,
  132. },
  133. {
  134. s: `SHOW STREAMS`,
  135. stmt: &ast.ShowStreamsStatement{},
  136. },
  137. {
  138. s: `SHOW TABLES`,
  139. stmt: &ast.ShowTablesStatement{},
  140. },
  141. {
  142. s: `SHOW STREAMSf`,
  143. stmt: nil,
  144. err: `found "STREAMSF", expected keyword streams or tables.`,
  145. },
  146. {
  147. s: `SHOW STREAMS d`,
  148. stmt: nil,
  149. err: `found "d", expected semecolon or EOF.`,
  150. },
  151. {
  152. s: `DESCRIBE STREAM demo`,
  153. stmt: &ast.DescribeStreamStatement{
  154. Name: "demo",
  155. },
  156. err: ``,
  157. },
  158. {
  159. s: `EXPLAIN STREAM demo1`,
  160. stmt: &ast.ExplainStreamStatement{
  161. Name: "demo1",
  162. },
  163. err: ``,
  164. },
  165. {
  166. s: `DROP STREAM demo1`,
  167. stmt: &ast.DropStreamStatement{
  168. Name: "demo1",
  169. },
  170. err: ``,
  171. },
  172. {
  173. s: `DESCRIBE TABLE demo`,
  174. stmt: &ast.DescribeTableStatement{
  175. Name: "demo",
  176. },
  177. err: ``,
  178. },
  179. {
  180. s: `EXPLAIN TABLE demo1`,
  181. stmt: &ast.ExplainTableStatement{
  182. Name: "demo1",
  183. },
  184. err: ``,
  185. },
  186. {
  187. s: `DROP TABLE demo1`,
  188. stmt: &ast.DropTableStatement{
  189. Name: "demo1",
  190. },
  191. err: ``,
  192. },
  193. }
  194. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  195. for i, tt := range tests {
  196. p := NewParser(strings.NewReader(tt.s))
  197. stmt, err := Language.Parse(p)
  198. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  199. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.s, tt.err, err)
  200. } else if tt.err == "" && !reflect.DeepEqual(tt.stmt, stmt) {
  201. t.Errorf("%d. %q\n\nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.s, tt.stmt, stmt)
  202. }
  203. }
  204. }