analyzer_test.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. // Copyright 2021-2023 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. "reflect"
  19. "strings"
  20. "testing"
  21. "github.com/lf-edge/ekuiper/internal/pkg/store"
  22. "github.com/lf-edge/ekuiper/internal/testx"
  23. "github.com/lf-edge/ekuiper/internal/xsql"
  24. "github.com/lf-edge/ekuiper/pkg/api"
  25. "github.com/lf-edge/ekuiper/pkg/ast"
  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(""),
  121. },
  122. { // 17
  123. sql: `SELECT last_hit_time() FROM src1 GROUP BY SlidingWindow(ss,5) HAVING last_agg_hit_count() < 3`,
  124. r: newErrorStruct("function last_hit_time is not allowed in an aggregate query"),
  125. },
  126. }
  127. func Test_validation(t *testing.T) {
  128. store, err := store.GetKV("stream")
  129. if err != nil {
  130. t.Error(err)
  131. return
  132. }
  133. streamSqls := map[string]string{
  134. "src1": `CREATE STREAM src1 (
  135. id1 BIGINT,
  136. temp BIGINT,
  137. name string,
  138. next STRUCT(NAME STRING, NID BIGINT)
  139. ) WITH (DATASOURCE="src1", FORMAT="json", KEY="ts");`,
  140. }
  141. types := map[string]ast.StreamType{
  142. "src1": ast.TypeStream,
  143. }
  144. for name, sql := range streamSqls {
  145. s, err := json.Marshal(&xsql.StreamInfo{
  146. StreamType: types[name],
  147. Statement: sql,
  148. })
  149. if err != nil {
  150. t.Error(err)
  151. t.Fail()
  152. }
  153. store.Set(name, string(s))
  154. }
  155. streams := make(map[string]*ast.StreamStmt)
  156. for n := range streamSqls {
  157. streamStmt, err := xsql.GetDataSource(store, n)
  158. if err != nil {
  159. t.Errorf("fail to get stream %s, please check if stream is created", n)
  160. return
  161. }
  162. streams[n] = streamStmt
  163. }
  164. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  165. for i, tt := range tests {
  166. stmt, err := xsql.NewParser(strings.NewReader(tt.sql)).Parse()
  167. if err != nil {
  168. t.Errorf("%d. %q: error compile sql: %s\n", i, tt.sql, err)
  169. continue
  170. }
  171. _, err = createLogicalPlan(stmt, &api.RuleOption{
  172. IsEventTime: false,
  173. LateTol: 0,
  174. Concurrency: 0,
  175. BufferLength: 0,
  176. SendMetaToSink: false,
  177. Qos: 0,
  178. CheckpointInterval: 0,
  179. SendError: true,
  180. }, store)
  181. if !reflect.DeepEqual(tt.r.err, testx.Errstring(err)) {
  182. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.sql, tt.r.err, err)
  183. }
  184. }
  185. }
  186. func Test_validationSchemaless(t *testing.T) {
  187. store, err := store.GetKV("stream")
  188. if err != nil {
  189. t.Error(err)
  190. return
  191. }
  192. streamSqls := map[string]string{
  193. "src1": `CREATE STREAM src1 (
  194. ) WITH (DATASOURCE="src1", FORMAT="json", KEY="ts");`,
  195. }
  196. types := map[string]ast.StreamType{
  197. "src1": ast.TypeStream,
  198. }
  199. for name, sql := range streamSqls {
  200. s, err := json.Marshal(&xsql.StreamInfo{
  201. StreamType: types[name],
  202. Statement: sql,
  203. })
  204. if err != nil {
  205. t.Error(err)
  206. t.Fail()
  207. }
  208. store.Set(name, string(s))
  209. }
  210. streams := make(map[string]*ast.StreamStmt)
  211. for n := range streamSqls {
  212. streamStmt, err := xsql.GetDataSource(store, n)
  213. if err != nil {
  214. t.Errorf("fail to get stream %s, please check if stream is created", n)
  215. return
  216. }
  217. streams[n] = streamStmt
  218. }
  219. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  220. for i, tt := range tests {
  221. stmt, err := xsql.NewParser(strings.NewReader(tt.sql)).Parse()
  222. if err != nil {
  223. t.Errorf("%d. %q: error compile sql: %s\n", i, tt.sql, err)
  224. continue
  225. }
  226. _, err = createLogicalPlan(stmt, &api.RuleOption{
  227. IsEventTime: false,
  228. LateTol: 0,
  229. Concurrency: 0,
  230. BufferLength: 0,
  231. SendMetaToSink: false,
  232. Qos: 0,
  233. CheckpointInterval: 0,
  234. SendError: true,
  235. }, store)
  236. serr := tt.r.Serr()
  237. if !reflect.DeepEqual(serr, testx.Errstring(err)) {
  238. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.sql, serr, err)
  239. }
  240. }
  241. }
  242. func TestConvertStreamInfo(t *testing.T) {
  243. testCases := []struct {
  244. name string
  245. streamStmt *ast.StreamStmt
  246. expected ast.StreamFields
  247. }{
  248. {
  249. name: "with match fields & schema",
  250. streamStmt: &ast.StreamStmt{
  251. StreamFields: []ast.StreamField{
  252. {
  253. Name: "field1",
  254. FieldType: &ast.BasicType{
  255. Type: ast.BIGINT,
  256. },
  257. },
  258. {
  259. Name: "field2",
  260. FieldType: &ast.BasicType{
  261. Type: ast.STRINGS,
  262. },
  263. },
  264. },
  265. Options: &ast.Options{
  266. FORMAT: "protobuf",
  267. SCHEMAID: "myschema.schema1",
  268. TIMESTAMP: "ts",
  269. },
  270. },
  271. expected: []ast.StreamField{
  272. {
  273. Name: "field1",
  274. FieldType: &ast.BasicType{
  275. Type: ast.BIGINT,
  276. },
  277. },
  278. {
  279. Name: "field2",
  280. FieldType: &ast.BasicType{
  281. Type: ast.STRINGS,
  282. },
  283. },
  284. },
  285. },
  286. {
  287. name: "with unmatch fields & schema",
  288. streamStmt: &ast.StreamStmt{
  289. StreamFields: []ast.StreamField{
  290. {
  291. Name: "field1",
  292. FieldType: &ast.BasicType{
  293. Type: ast.STRINGS,
  294. },
  295. },
  296. {
  297. Name: "field2",
  298. FieldType: &ast.BasicType{
  299. Type: ast.STRINGS,
  300. },
  301. },
  302. },
  303. Options: &ast.Options{
  304. FORMAT: "protobuf",
  305. SCHEMAID: "myschema.schema1",
  306. TIMESTAMP: "ts",
  307. },
  308. },
  309. expected: []ast.StreamField{
  310. {
  311. Name: "field1",
  312. FieldType: &ast.BasicType{
  313. Type: ast.BIGINT,
  314. },
  315. },
  316. {
  317. Name: "field2",
  318. FieldType: &ast.BasicType{
  319. Type: ast.STRINGS,
  320. },
  321. },
  322. },
  323. },
  324. {
  325. name: "without schema",
  326. streamStmt: &ast.StreamStmt{
  327. StreamFields: []ast.StreamField{
  328. {
  329. Name: "field1",
  330. FieldType: &ast.BasicType{
  331. Type: ast.FLOAT,
  332. },
  333. },
  334. {
  335. Name: "field2",
  336. FieldType: &ast.BasicType{
  337. Type: ast.STRINGS,
  338. },
  339. },
  340. },
  341. Options: &ast.Options{
  342. FORMAT: "json",
  343. TIMESTAMP: "ts",
  344. },
  345. },
  346. expected: []ast.StreamField{
  347. {
  348. Name: "field1",
  349. FieldType: &ast.BasicType{
  350. Type: ast.FLOAT,
  351. },
  352. },
  353. {
  354. Name: "field2",
  355. FieldType: &ast.BasicType{
  356. Type: ast.STRINGS,
  357. },
  358. },
  359. },
  360. },
  361. {
  362. name: "without fields",
  363. streamStmt: &ast.StreamStmt{
  364. Options: &ast.Options{
  365. FORMAT: "protobuf",
  366. SCHEMAID: "myschema.schema1",
  367. TIMESTAMP: "ts",
  368. },
  369. },
  370. expected: []ast.StreamField{
  371. {
  372. Name: "field1",
  373. FieldType: &ast.BasicType{
  374. Type: ast.BIGINT,
  375. },
  376. },
  377. {
  378. Name: "field2",
  379. FieldType: &ast.BasicType{
  380. Type: ast.STRINGS,
  381. },
  382. },
  383. },
  384. },
  385. {
  386. name: "schemaless",
  387. streamStmt: &ast.StreamStmt{
  388. Options: &ast.Options{
  389. FORMAT: "json",
  390. TIMESTAMP: "ts",
  391. },
  392. },
  393. expected: nil,
  394. },
  395. }
  396. for _, tc := range testCases {
  397. t.Run(tc.name, func(t *testing.T) {
  398. actual, err := convertStreamInfo(tc.streamStmt)
  399. if err != nil {
  400. t.Errorf("unexpected error: %v", err)
  401. return
  402. }
  403. if !reflect.DeepEqual(actual.schema, tc.expected) {
  404. t.Errorf("unexpected result: got %v, want %v", actual.schema, tc.expected)
  405. }
  406. })
  407. }
  408. }