parser_tree_test.go 4.0 KB

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