parser_srf_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Copyright 2023 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_ParserSRFStatement(t *testing.T) {
  24. tests := []struct {
  25. s string
  26. stmt *ast.SelectStatement
  27. err string
  28. }{
  29. {
  30. s: "select a from demo order by unnest(b)",
  31. err: "select statement shouldn't has srf except fields",
  32. },
  33. {
  34. s: "select abc from demo left join demo on unnest(demo.a) = b GROUP BY ID, TUMBLINGWINDOW(ss, 10)",
  35. err: "select statement shouldn't has srf except fields",
  36. },
  37. {
  38. s: "select a from demo group by id having unnest(a)",
  39. err: "select statement shouldn't has srf except fields",
  40. },
  41. {
  42. s: "select a from demo group by unnest(a)",
  43. err: "select statement shouldn't has srf except fields",
  44. },
  45. {
  46. s: "select a,b from demo where unnest(a)",
  47. err: "select statement shouldn't has srf except fields",
  48. },
  49. {
  50. s: "select unnest(unnest(arr)) from demo",
  51. err: "select clause shouldn't has nested set-returning-functions",
  52. },
  53. {
  54. s: "select abs(unnest(arr)) from demo",
  55. err: "select clause shouldn't has nested set-returning-functions",
  56. },
  57. {
  58. s: "select unnest(arr1), unnest(arr2) from demo",
  59. err: "select clause shouldn't has multi set-returning-functions",
  60. },
  61. {
  62. s: "select unnest(arr), a from demo",
  63. stmt: &ast.SelectStatement{
  64. Fields: []ast.Field{
  65. {
  66. Name: "unnest",
  67. Expr: &ast.Call{
  68. Name: "unnest",
  69. FuncId: 0,
  70. FuncType: ast.FuncTypeSrf,
  71. Args: []ast.Expr{
  72. &ast.FieldRef{
  73. StreamName: ast.DefaultStream,
  74. Name: "arr",
  75. },
  76. },
  77. },
  78. },
  79. {
  80. Name: "a",
  81. Expr: &ast.FieldRef{
  82. StreamName: ast.DefaultStream,
  83. Name: "a",
  84. },
  85. },
  86. },
  87. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  88. },
  89. },
  90. {
  91. s: "select unnest(arr) from demo",
  92. stmt: &ast.SelectStatement{
  93. Fields: []ast.Field{
  94. {
  95. Name: "unnest",
  96. Expr: &ast.Call{
  97. Name: "unnest",
  98. FuncId: 0,
  99. FuncType: ast.FuncTypeSrf,
  100. Args: []ast.Expr{
  101. &ast.FieldRef{
  102. StreamName: ast.DefaultStream,
  103. Name: "arr",
  104. },
  105. },
  106. },
  107. },
  108. },
  109. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  110. },
  111. },
  112. }
  113. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  114. for i, tt := range tests {
  115. stmt, err := NewParser(strings.NewReader(tt.s)).Parse()
  116. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  117. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.s, tt.err, err)
  118. } else if tt.err == "" && !reflect.DeepEqual(tt.stmt, stmt) {
  119. t.Errorf("%d. %q\n\nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.s, tt.stmt, stmt)
  120. }
  121. }
  122. }