analyzer_test.go 6.6 KB

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