parser_tree_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package xsql
  2. import (
  3. "fmt"
  4. "github.com/emqx/kuiper/internal/testx"
  5. "github.com/emqx/kuiper/pkg/ast"
  6. "reflect"
  7. "strings"
  8. "testing"
  9. )
  10. func TestParser_ParseTree(t *testing.T) {
  11. var tests = []struct {
  12. s string
  13. stmt ast.Statement
  14. err string
  15. }{
  16. {
  17. s: `CREATE STREAM demo (
  18. USERID BIGINT,
  19. ) WITH (DATASOURCE="users", FORMAT="JSON", KEY="USERID", SHARED="true");`,
  20. stmt: &ast.StreamStmt{
  21. Name: ast.StreamName("demo"),
  22. StreamFields: []ast.StreamField{
  23. {Name: "USERID", FieldType: &ast.BasicType{Type: ast.BIGINT}},
  24. },
  25. Options: &ast.Options{
  26. DATASOURCE: "users",
  27. FORMAT: "JSON",
  28. KEY: "USERID",
  29. SHARED: true,
  30. },
  31. },
  32. },
  33. {
  34. s: `CREATE TABLE demo (
  35. USERID BIGINT,
  36. ) WITH (DATASOURCE="users", FORMAT="JSON", KEY="USERID", RETAIN_SIZE="3");`,
  37. stmt: &ast.StreamStmt{
  38. Name: ast.StreamName("demo"),
  39. StreamFields: []ast.StreamField{
  40. {Name: "USERID", FieldType: &ast.BasicType{Type: ast.BIGINT}},
  41. },
  42. Options: &ast.Options{
  43. DATASOURCE: "users",
  44. FORMAT: "JSON",
  45. KEY: "USERID",
  46. RETAIN_SIZE: 3,
  47. },
  48. StreamType: ast.TypeTable,
  49. },
  50. },
  51. {
  52. s: `CREATE TABLE table1 (
  53. name STRING,
  54. size BIGINT,
  55. id BIGINT
  56. ) WITH (DATASOURCE="lookup.json", FORMAT="json", CONF_KEY="test");`,
  57. stmt: &ast.StreamStmt{
  58. Name: ast.StreamName("table1"),
  59. StreamFields: []ast.StreamField{
  60. {Name: "name", FieldType: &ast.BasicType{Type: ast.STRINGS}},
  61. {Name: "size", FieldType: &ast.BasicType{Type: ast.BIGINT}},
  62. {Name: "id", FieldType: &ast.BasicType{Type: ast.BIGINT}},
  63. },
  64. Options: &ast.Options{
  65. DATASOURCE: "lookup.json",
  66. FORMAT: "json",
  67. CONF_KEY: "test",
  68. },
  69. StreamType: ast.TypeTable,
  70. },
  71. },
  72. {
  73. s: `SHOW STREAMS`,
  74. stmt: &ast.ShowStreamsStatement{},
  75. },
  76. {
  77. s: `SHOW TABLES`,
  78. stmt: &ast.ShowTablesStatement{},
  79. },
  80. {
  81. s: `SHOW STREAMSf`,
  82. stmt: nil,
  83. err: `found "STREAMSf", expected keyword streams or tables.`,
  84. },
  85. {
  86. s: `SHOW STREAMS d`,
  87. stmt: nil,
  88. err: `found "d", expected semecolon or EOF.`,
  89. },
  90. {
  91. s: `DESCRIBE STREAM demo`,
  92. stmt: &ast.DescribeStreamStatement{
  93. Name: "demo",
  94. },
  95. err: ``,
  96. },
  97. {
  98. s: `EXPLAIN STREAM demo1`,
  99. stmt: &ast.ExplainStreamStatement{
  100. Name: "demo1",
  101. },
  102. err: ``,
  103. },
  104. {
  105. s: `DROP STREAM demo1`,
  106. stmt: &ast.DropStreamStatement{
  107. Name: "demo1",
  108. },
  109. err: ``,
  110. },
  111. {
  112. s: `DESCRIBE TABLE demo`,
  113. stmt: &ast.DescribeTableStatement{
  114. Name: "demo",
  115. },
  116. err: ``,
  117. },
  118. {
  119. s: `EXPLAIN TABLE demo1`,
  120. stmt: &ast.ExplainTableStatement{
  121. Name: "demo1",
  122. },
  123. err: ``,
  124. },
  125. {
  126. s: `DROP TABLE demo1`,
  127. stmt: &ast.DropTableStatement{
  128. Name: "demo1",
  129. },
  130. err: ``,
  131. },
  132. }
  133. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  134. for i, tt := range tests {
  135. p := NewParser(strings.NewReader(tt.s))
  136. stmt, err := Language.Parse(p)
  137. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  138. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.s, tt.err, err)
  139. } else if tt.err == "" && !reflect.DeepEqual(tt.stmt, stmt) {
  140. t.Errorf("%d. %q\n\nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.s, tt.stmt, stmt)
  141. }
  142. }
  143. }