analyzer_test.go 6.4 KB

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