parser_validate_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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/stretchr/testify/require"
  21. "github.com/lf-edge/ekuiper/internal/testx"
  22. "github.com/lf-edge/ekuiper/pkg/ast"
  23. )
  24. func TestParser_ParserSRFStatement(t *testing.T) {
  25. tests := []struct {
  26. s string
  27. stmt *ast.SelectStatement
  28. err string
  29. }{
  30. {
  31. s: "select a from demo order by unnest(b)",
  32. err: "select statement shouldn't has srf except fields",
  33. },
  34. {
  35. s: "select abc from demo left join demo on unnest(demo.a) = b GROUP BY ID, TUMBLINGWINDOW(ss, 10)",
  36. err: "select statement shouldn't has srf except fields",
  37. },
  38. {
  39. s: "select a from demo group by id having unnest(a)",
  40. err: "select statement shouldn't has srf except fields",
  41. },
  42. {
  43. s: "select a from demo group by unnest(a)",
  44. err: "select statement shouldn't has srf except fields",
  45. },
  46. {
  47. s: "select a,b from demo where unnest(a)",
  48. err: "select statement shouldn't has srf except fields",
  49. },
  50. {
  51. s: "select unnest(unnest(arr)) from demo",
  52. err: "select clause shouldn't has nested set-returning-functions",
  53. },
  54. {
  55. s: "select abs(unnest(arr)) from demo",
  56. err: "select clause shouldn't has nested set-returning-functions",
  57. },
  58. {
  59. s: "select unnest(arr1), unnest(arr2) from demo",
  60. err: "select clause shouldn't has multi set-returning-functions",
  61. },
  62. {
  63. s: "select unnest(arr), a from demo",
  64. stmt: &ast.SelectStatement{
  65. Fields: []ast.Field{
  66. {
  67. Name: "unnest",
  68. Expr: &ast.Call{
  69. Name: "unnest",
  70. FuncId: 0,
  71. FuncType: ast.FuncTypeSrf,
  72. Args: []ast.Expr{
  73. &ast.FieldRef{
  74. StreamName: ast.DefaultStream,
  75. Name: "arr",
  76. },
  77. },
  78. },
  79. },
  80. {
  81. Name: "a",
  82. Expr: &ast.FieldRef{
  83. StreamName: ast.DefaultStream,
  84. Name: "a",
  85. },
  86. },
  87. },
  88. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  89. },
  90. },
  91. {
  92. s: "select unnest(arr) from demo",
  93. stmt: &ast.SelectStatement{
  94. Fields: []ast.Field{
  95. {
  96. Name: "unnest",
  97. Expr: &ast.Call{
  98. Name: "unnest",
  99. FuncId: 0,
  100. FuncType: ast.FuncTypeSrf,
  101. Args: []ast.Expr{
  102. &ast.FieldRef{
  103. StreamName: ast.DefaultStream,
  104. Name: "arr",
  105. },
  106. },
  107. },
  108. },
  109. },
  110. Sources: []ast.Source{&ast.Table{Name: "demo"}},
  111. },
  112. },
  113. }
  114. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  115. for i, tt := range tests {
  116. stmt, err := NewParser(strings.NewReader(tt.s)).Parse()
  117. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  118. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.s, tt.err, err)
  119. } else if tt.err == "" && !reflect.DeepEqual(tt.stmt, stmt) {
  120. t.Errorf("%d. %q\n\nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.s, tt.stmt, stmt)
  121. }
  122. }
  123. }
  124. func TestParser_ParserWindowFunctionStatement(t *testing.T) {
  125. tests := []struct {
  126. s string
  127. err string
  128. }{
  129. {
  130. s: "select row_number() from demo ",
  131. err: "",
  132. },
  133. {
  134. s: "select * from demo where row_number() > 1",
  135. err: "window functions can only be in select fields",
  136. },
  137. {
  138. s: "select * from demo having row_number() > 1",
  139. err: "window functions can only be in select fields",
  140. },
  141. {
  142. s: "select * from demo group by row_number()",
  143. err: "window functions can only be in select fields",
  144. },
  145. {
  146. s: "select * from demo left join demo on row_number()",
  147. err: "window functions can only be in select fields",
  148. },
  149. {
  150. s: "select * from demo order by row_number()",
  151. err: "window functions can only be in select fields",
  152. },
  153. }
  154. for _, tt := range tests {
  155. _, err := NewParser(strings.NewReader(tt.s)).Parse()
  156. if len(tt.err) == 0 {
  157. require.NoError(t, err)
  158. } else {
  159. require.Equal(t, tt.err, err.Error())
  160. }
  161. }
  162. }