analyzer_test.go 6.2 KB

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