analyzer_test.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 planner
  15. import (
  16. "encoding/json"
  17. "fmt"
  18. "github.com/lf-edge/ekuiper/internal/testx"
  19. "github.com/lf-edge/ekuiper/internal/xsql"
  20. "github.com/lf-edge/ekuiper/pkg/api"
  21. "github.com/lf-edge/ekuiper/pkg/ast"
  22. "github.com/lf-edge/ekuiper/pkg/kv"
  23. "path"
  24. "reflect"
  25. "strings"
  26. "testing"
  27. )
  28. type errorStruct struct {
  29. err string
  30. serr *string
  31. }
  32. func newErrorStruct(err string) *errorStruct {
  33. return &errorStruct{
  34. err: err,
  35. }
  36. }
  37. func newErrorStructWithS(err string, serr string) *errorStruct {
  38. return &errorStruct{
  39. err: err,
  40. serr: &serr,
  41. }
  42. }
  43. func (e *errorStruct) Serr() string {
  44. if e.serr != nil {
  45. return *e.serr
  46. }
  47. return e.err
  48. }
  49. var tests = []struct {
  50. sql string
  51. r *errorStruct
  52. }{
  53. { // 0
  54. sql: `SELECT count(*) FROM src1 HAVING sin(temp) > 0.3`,
  55. r: newErrorStruct("Not allowed to call non-aggregate functions in HAVING clause."),
  56. },
  57. { // 1
  58. sql: `SELECT count(*) FROM src1 WHERE name = "dname" HAVING sin(count(*)) > 0.3`,
  59. r: newErrorStruct(""),
  60. },
  61. { // 2
  62. sql: `SELECT count(*) as c FROM src1 WHERE name = "dname" HAVING sin(c) > 0.3`,
  63. r: newErrorStruct(""),
  64. },
  65. { // 3
  66. sql: `SELECT count(*) as c FROM src1 WHERE name = "dname" HAVING sum(c) > 0.3`,
  67. r: newErrorStruct("invalid argument for func sum: aggregate argument is not allowed"),
  68. },
  69. { // 4
  70. sql: `SELECT count(*) as c FROM src1 WHERE name = "dname" GROUP BY sin(c)`,
  71. r: newErrorStruct("Not allowed to call aggregate functions in GROUP BY clause."),
  72. },
  73. { // 5
  74. sql: `SELECT count(*) as c FROM src1 WHERE name = "dname" HAVING sum(c) > 0.3 OR sin(temp) > 3`,
  75. r: newErrorStruct("Not allowed to call non-aggregate functions in HAVING clause."),
  76. },
  77. { // 6
  78. sql: `SELECT collect(*) as c FROM src1 WHERE name = "dname" HAVING c[2]->temp > 20 AND sin(c[0]->temp) > 0`,
  79. r: newErrorStruct(""),
  80. },
  81. { // 7
  82. sql: `SELECT collect(*) as c FROM src1 WHERE name = "dname" HAVING c[2]->temp + temp > 0`,
  83. r: newErrorStruct("Not allowed to call non-aggregate functions in HAVING clause."),
  84. },
  85. { // 8
  86. sql: `SELECT deduplicate(temp, true) as de FROM src1 HAVING cardinality(de) > 20`,
  87. r: newErrorStruct(""),
  88. },
  89. { // 9
  90. sql: `SELECT sin(temp) as temp FROM src1`,
  91. r: newErrorStruct(""),
  92. },
  93. { // 10
  94. sql: `SELECT sum(temp) as temp, count(temp) as temp FROM src1`,
  95. r: newErrorStruct("duplicate alias temp"),
  96. },
  97. { // 11
  98. sql: `SELECT sum(temp) as temp, count(temp) as ct FROM src1`,
  99. r: newErrorStruct(""),
  100. },
  101. { // 12
  102. sql: `SELECT collect(*)->abc FROM src1`,
  103. r: newErrorStruct(""),
  104. },
  105. { // 13
  106. sql: `SELECT sin(temp) as temp1, cos(temp1) FROM src1`,
  107. r: newErrorStructWithS("unknown field temp1", ""),
  108. },
  109. }
  110. func Test_validation(t *testing.T) {
  111. store := kv.GetDefaultKVStore(path.Join(DbDir, "stream"))
  112. err := store.Open()
  113. if err != nil {
  114. t.Error(err)
  115. return
  116. }
  117. defer store.Close()
  118. streamSqls := map[string]string{
  119. "src1": `CREATE STREAM src1 (
  120. id1 BIGINT,
  121. temp BIGINT,
  122. name string
  123. ) WITH (DATASOURCE="src1", FORMAT="json", KEY="ts");`,
  124. }
  125. types := map[string]ast.StreamType{
  126. "src1": ast.TypeStream,
  127. }
  128. for name, sql := range streamSqls {
  129. s, err := json.Marshal(&xsql.StreamInfo{
  130. StreamType: types[name],
  131. Statement: sql,
  132. })
  133. if err != nil {
  134. t.Error(err)
  135. t.Fail()
  136. }
  137. store.Set(name, string(s))
  138. }
  139. streams := make(map[string]*ast.StreamStmt)
  140. for n := range streamSqls {
  141. streamStmt, err := xsql.GetDataSource(store, n)
  142. if err != nil {
  143. t.Errorf("fail to get stream %s, please check if stream is created", n)
  144. return
  145. }
  146. streams[n] = streamStmt
  147. }
  148. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  149. for i, tt := range tests {
  150. stmt, err := xsql.NewParser(strings.NewReader(tt.sql)).Parse()
  151. if err != nil {
  152. t.Errorf("%d. %q: error compile sql: %s\n", i, tt.sql, err)
  153. continue
  154. }
  155. _, err = createLogicalPlan(stmt, &api.RuleOption{
  156. IsEventTime: false,
  157. LateTol: 0,
  158. Concurrency: 0,
  159. BufferLength: 0,
  160. SendMetaToSink: false,
  161. Qos: 0,
  162. CheckpointInterval: 0,
  163. SendError: true,
  164. }, store)
  165. if !reflect.DeepEqual(tt.r.err, testx.Errstring(err)) {
  166. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.sql, tt.r.err, err)
  167. }
  168. }
  169. }
  170. func Test_validationSchemaless(t *testing.T) {
  171. store := kv.GetDefaultKVStore(path.Join(DbDir, "stream"))
  172. err := store.Open()
  173. if err != nil {
  174. t.Error(err)
  175. return
  176. }
  177. defer store.Close()
  178. streamSqls := map[string]string{
  179. "src1": `CREATE STREAM src1 (
  180. ) WITH (DATASOURCE="src1", FORMAT="json", KEY="ts");`,
  181. }
  182. types := map[string]ast.StreamType{
  183. "src1": ast.TypeStream,
  184. }
  185. for name, sql := range streamSqls {
  186. s, err := json.Marshal(&xsql.StreamInfo{
  187. StreamType: types[name],
  188. Statement: sql,
  189. })
  190. if err != nil {
  191. t.Error(err)
  192. t.Fail()
  193. }
  194. store.Set(name, string(s))
  195. }
  196. streams := make(map[string]*ast.StreamStmt)
  197. for n := range streamSqls {
  198. streamStmt, err := xsql.GetDataSource(store, n)
  199. if err != nil {
  200. t.Errorf("fail to get stream %s, please check if stream is created", n)
  201. return
  202. }
  203. streams[n] = streamStmt
  204. }
  205. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  206. for i, tt := range tests {
  207. stmt, err := xsql.NewParser(strings.NewReader(tt.sql)).Parse()
  208. if err != nil {
  209. t.Errorf("%d. %q: error compile sql: %s\n", i, tt.sql, err)
  210. continue
  211. }
  212. _, err = createLogicalPlan(stmt, &api.RuleOption{
  213. IsEventTime: false,
  214. LateTol: 0,
  215. Concurrency: 0,
  216. BufferLength: 0,
  217. SendMetaToSink: false,
  218. Qos: 0,
  219. CheckpointInterval: 0,
  220. SendError: true,
  221. }, store)
  222. serr := tt.r.Serr()
  223. if !reflect.DeepEqual(serr, testx.Errstring(err)) {
  224. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.sql, serr, err)
  225. }
  226. }
  227. }