analyzer_test.go 5.7 KB

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