analyzer_test.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. { // 14
  115. sql: `SELECT sum(next->nid) as nid FROM src1 WHERE next->nid > 20 `,
  116. r: newErrorStruct(""),
  117. },
  118. }
  119. func Test_validation(t *testing.T) {
  120. err, store := store.GetKV("stream")
  121. if err != nil {
  122. t.Error(err)
  123. return
  124. }
  125. streamSqls := map[string]string{
  126. "src1": `CREATE STREAM src1 (
  127. id1 BIGINT,
  128. temp BIGINT,
  129. name string,
  130. next STRUCT(NAME STRING, NID BIGINT)
  131. ) WITH (DATASOURCE="src1", FORMAT="json", KEY="ts");`,
  132. }
  133. types := map[string]ast.StreamType{
  134. "src1": ast.TypeStream,
  135. }
  136. for name, sql := range streamSqls {
  137. s, err := json.Marshal(&xsql.StreamInfo{
  138. StreamType: types[name],
  139. Statement: sql,
  140. })
  141. if err != nil {
  142. t.Error(err)
  143. t.Fail()
  144. }
  145. store.Set(name, string(s))
  146. }
  147. streams := make(map[string]*ast.StreamStmt)
  148. for n := range streamSqls {
  149. streamStmt, err := xsql.GetDataSource(store, n)
  150. if err != nil {
  151. t.Errorf("fail to get stream %s, please check if stream is created", n)
  152. return
  153. }
  154. streams[n] = streamStmt
  155. }
  156. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  157. for i, tt := range tests {
  158. stmt, err := xsql.NewParser(strings.NewReader(tt.sql)).Parse()
  159. if err != nil {
  160. t.Errorf("%d. %q: error compile sql: %s\n", i, tt.sql, err)
  161. continue
  162. }
  163. _, err = createLogicalPlan(stmt, &api.RuleOption{
  164. IsEventTime: false,
  165. LateTol: 0,
  166. Concurrency: 0,
  167. BufferLength: 0,
  168. SendMetaToSink: false,
  169. Qos: 0,
  170. CheckpointInterval: 0,
  171. SendError: true,
  172. }, store)
  173. if !reflect.DeepEqual(tt.r.err, testx.Errstring(err)) {
  174. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.sql, tt.r.err, err)
  175. }
  176. }
  177. }
  178. func Test_validationSchemaless(t *testing.T) {
  179. err, store := store.GetKV("stream")
  180. if err != nil {
  181. t.Error(err)
  182. return
  183. }
  184. streamSqls := map[string]string{
  185. "src1": `CREATE STREAM src1 (
  186. ) WITH (DATASOURCE="src1", FORMAT="json", KEY="ts");`,
  187. }
  188. types := map[string]ast.StreamType{
  189. "src1": ast.TypeStream,
  190. }
  191. for name, sql := range streamSqls {
  192. s, err := json.Marshal(&xsql.StreamInfo{
  193. StreamType: types[name],
  194. Statement: sql,
  195. })
  196. if err != nil {
  197. t.Error(err)
  198. t.Fail()
  199. }
  200. store.Set(name, string(s))
  201. }
  202. streams := make(map[string]*ast.StreamStmt)
  203. for n := range streamSqls {
  204. streamStmt, err := xsql.GetDataSource(store, n)
  205. if err != nil {
  206. t.Errorf("fail to get stream %s, please check if stream is created", n)
  207. return
  208. }
  209. streams[n] = streamStmt
  210. }
  211. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  212. for i, tt := range tests {
  213. stmt, err := xsql.NewParser(strings.NewReader(tt.sql)).Parse()
  214. if err != nil {
  215. t.Errorf("%d. %q: error compile sql: %s\n", i, tt.sql, err)
  216. continue
  217. }
  218. _, err = createLogicalPlan(stmt, &api.RuleOption{
  219. IsEventTime: false,
  220. LateTol: 0,
  221. Concurrency: 0,
  222. BufferLength: 0,
  223. SendMetaToSink: false,
  224. Qos: 0,
  225. CheckpointInterval: 0,
  226. SendError: true,
  227. }, store)
  228. serr := tt.r.Serr()
  229. if !reflect.DeepEqual(serr, testx.Errstring(err)) {
  230. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.sql, serr, err)
  231. }
  232. }
  233. }