analyzer_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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/stretchr/testify/assert"
  22. "github.com/lf-edge/ekuiper/internal/pkg/store"
  23. "github.com/lf-edge/ekuiper/internal/testx"
  24. "github.com/lf-edge/ekuiper/internal/xsql"
  25. "github.com/lf-edge/ekuiper/pkg/api"
  26. "github.com/lf-edge/ekuiper/pkg/ast"
  27. )
  28. func init() {
  29. }
  30. type errorStruct struct {
  31. err string
  32. serr *string
  33. }
  34. func newErrorStruct(err string) *errorStruct {
  35. return &errorStruct{
  36. err: err,
  37. }
  38. }
  39. func newErrorStructWithS(err string, serr string) *errorStruct {
  40. return &errorStruct{
  41. err: err,
  42. serr: &serr,
  43. }
  44. }
  45. func (e *errorStruct) Serr() string {
  46. if e.serr != nil {
  47. return *e.serr
  48. }
  49. return e.err
  50. }
  51. var tests = []struct {
  52. sql string
  53. r *errorStruct
  54. }{
  55. { // 0
  56. sql: `SELECT count(*) FROM src1 HAVING sin(temp) > 0.3`,
  57. r: newErrorStruct("Not allowed to call non-aggregate functions in HAVING clause."),
  58. },
  59. { // 1
  60. sql: `SELECT count(*) FROM src1 WHERE name = "dname" HAVING sin(count(*)) > 0.3`,
  61. r: newErrorStruct(""),
  62. },
  63. { // 2
  64. sql: `SELECT count(*) as c FROM src1 WHERE name = "dname" HAVING sin(c) > 0.3`,
  65. r: newErrorStruct(""),
  66. },
  67. { // 3
  68. sql: `SELECT count(*) as c FROM src1 WHERE name = "dname" HAVING sum(c) > 0.3`,
  69. r: newErrorStruct("invalid argument for func sum: aggregate argument is not allowed"),
  70. },
  71. { // 4
  72. sql: `SELECT count(*) as c FROM src1 WHERE name = "dname" GROUP BY sin(c)`,
  73. r: newErrorStruct("Not allowed to call aggregate functions in GROUP BY clause."),
  74. },
  75. { // 5
  76. sql: `SELECT count(*) as c FROM src1 WHERE name = "dname" HAVING sum(c) > 0.3 OR sin(temp) > 3`,
  77. r: newErrorStruct("Not allowed to call non-aggregate functions in HAVING clause."),
  78. },
  79. { // 6
  80. sql: `SELECT collect(*) as c FROM src1 WHERE name = "dname" HAVING c[2]->temp > 20 AND sin(c[0]->temp) > 0`,
  81. r: newErrorStruct(""),
  82. },
  83. { // 7
  84. sql: `SELECT collect(*) as c FROM src1 WHERE name = "dname" HAVING c[2]->temp + temp > 0`,
  85. r: newErrorStruct("Not allowed to call non-aggregate functions in HAVING clause."),
  86. },
  87. { // 8
  88. sql: `SELECT deduplicate(temp, true) as de FROM src1 HAVING cardinality(de) > 20`,
  89. r: newErrorStruct(""),
  90. },
  91. { // 9
  92. sql: `SELECT sin(temp) as temp FROM src1`,
  93. r: newErrorStruct(""),
  94. },
  95. { // 10
  96. sql: `SELECT sum(temp) as temp1, count(temp) as temp FROM src1`,
  97. r: newErrorStruct("invalid argument for func sum: aggregate argument is not allowed"),
  98. },
  99. { // 11
  100. sql: `SELECT sum(temp) as temp1, count(temp) as ct FROM src1`,
  101. r: newErrorStruct(""),
  102. },
  103. { // 12
  104. sql: `SELECT collect(*)->abc FROM src1`,
  105. r: newErrorStruct(""),
  106. },
  107. { // 13
  108. sql: `SELECT sin(temp) as temp1, cos(temp1) FROM src1`,
  109. r: newErrorStruct(""),
  110. },
  111. { // 14
  112. sql: `SELECT collect(*)[-1] as current FROM src1 GROUP BY COUNTWINDOW(2, 1) HAVING isNull(current->name) = false`,
  113. r: newErrorStruct(""),
  114. },
  115. { // 15
  116. sql: `SELECT sum(next->nid) as nid FROM src1 WHERE next->nid > 20 `,
  117. r: newErrorStruct(""),
  118. },
  119. { // 16
  120. sql: `SELECT collect(*)[0] as last FROM src1 GROUP BY SlidingWindow(ss,5) HAVING last.temp > 30`,
  121. r: newErrorStruct(""),
  122. },
  123. { // 17
  124. sql: `SELECT last_hit_time() FROM src1 GROUP BY SlidingWindow(ss,5) HAVING last_agg_hit_count() < 3`,
  125. r: newErrorStruct("function last_hit_time is not allowed in an aggregate query"),
  126. },
  127. { // 18
  128. sql: `SELECT * FROM src1 GROUP BY SlidingWindow(ss,5) Over (WHEN last_hit_time() > 1) HAVING last_agg_hit_count() < 3`,
  129. r: newErrorStruct(""),
  130. },
  131. {
  132. sql: "select a + 1 as b, b + 1 as a from src1",
  133. r: newErrorStruct("select fields have cycled alias"),
  134. },
  135. {
  136. sql: "select a + 1 as b, b * 2 as c, c + 1 as a from src1",
  137. r: newErrorStruct("select fields have cycled alias"),
  138. },
  139. //{ // 19 already captured in parser
  140. // sql: `SELECT * FROM src1 GROUP BY SlidingWindow(ss,5) Over (WHEN abs(sum(a)) > 1) HAVING last_agg_hit_count() < 3`,
  141. // r: newErrorStruct("error compile sql: Not allowed to call aggregate functions in GROUP BY clause."),
  142. //},
  143. }
  144. func Test_validation(t *testing.T) {
  145. tests[10].r = newErrorStruct("invalid argument for func sum: aggregate argument is not allowed")
  146. store, err := store.GetKV("stream")
  147. if err != nil {
  148. t.Error(err)
  149. return
  150. }
  151. streamSqls := map[string]string{
  152. "src1": `CREATE STREAM src1 (
  153. id1 BIGINT,
  154. temp BIGINT,
  155. name string,
  156. next STRUCT(NAME STRING, NID BIGINT)
  157. ) WITH (DATASOURCE="src1", FORMAT="json", KEY="ts");`,
  158. }
  159. types := map[string]ast.StreamType{
  160. "src1": ast.TypeStream,
  161. }
  162. for name, sql := range streamSqls {
  163. s, err := json.Marshal(&xsql.StreamInfo{
  164. StreamType: types[name],
  165. Statement: sql,
  166. })
  167. if err != nil {
  168. t.Error(err)
  169. t.Fail()
  170. }
  171. store.Set(name, string(s))
  172. }
  173. streams := make(map[string]*ast.StreamStmt)
  174. for n := range streamSqls {
  175. streamStmt, err := xsql.GetDataSource(store, n)
  176. if err != nil {
  177. t.Errorf("fail to get stream %s, please check if stream is created", n)
  178. return
  179. }
  180. streams[n] = streamStmt
  181. }
  182. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  183. for i, tt := range tests {
  184. stmt, err := xsql.NewParser(strings.NewReader(tt.sql)).Parse()
  185. if err != nil {
  186. t.Errorf("%d. %q: error compile sql: %s\n", i, tt.sql, err)
  187. continue
  188. }
  189. _, err = createLogicalPlan(stmt, &api.RuleOption{
  190. IsEventTime: false,
  191. LateTol: 0,
  192. Concurrency: 0,
  193. BufferLength: 0,
  194. SendMetaToSink: false,
  195. Qos: 0,
  196. CheckpointInterval: 0,
  197. SendError: true,
  198. }, store)
  199. assert.Equal(t, tt.r.err, testx.Errstring(err))
  200. }
  201. }
  202. func Test_validationSchemaless(t *testing.T) {
  203. store, err := store.GetKV("stream")
  204. if err != nil {
  205. t.Error(err)
  206. return
  207. }
  208. streamSqls := map[string]string{
  209. "src1": `CREATE STREAM src1 (
  210. ) WITH (DATASOURCE="src1", FORMAT="json", KEY="ts");`,
  211. }
  212. types := map[string]ast.StreamType{
  213. "src1": ast.TypeStream,
  214. }
  215. for name, sql := range streamSqls {
  216. s, err := json.Marshal(&xsql.StreamInfo{
  217. StreamType: types[name],
  218. Statement: sql,
  219. })
  220. if err != nil {
  221. t.Error(err)
  222. t.Fail()
  223. }
  224. store.Set(name, string(s))
  225. }
  226. streams := make(map[string]*ast.StreamStmt)
  227. for n := range streamSqls {
  228. streamStmt, err := xsql.GetDataSource(store, n)
  229. if err != nil {
  230. t.Errorf("fail to get stream %s, please check if stream is created", n)
  231. return
  232. }
  233. streams[n] = streamStmt
  234. }
  235. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  236. for i, tt := range tests {
  237. stmt, err := xsql.NewParser(strings.NewReader(tt.sql)).Parse()
  238. if err != nil {
  239. t.Errorf("%d. %q: error compile sql: %s\n", i, tt.sql, err)
  240. continue
  241. }
  242. _, err = createLogicalPlan(stmt, &api.RuleOption{
  243. IsEventTime: false,
  244. LateTol: 0,
  245. Concurrency: 0,
  246. BufferLength: 0,
  247. SendMetaToSink: false,
  248. Qos: 0,
  249. CheckpointInterval: 0,
  250. SendError: true,
  251. }, store)
  252. serr := tt.r.Serr()
  253. if !reflect.DeepEqual(serr, testx.Errstring(err)) {
  254. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.sql, serr, err)
  255. }
  256. }
  257. }
  258. func TestConvertStreamInfo(t *testing.T) {
  259. testCases := []struct {
  260. name string
  261. streamStmt *ast.StreamStmt
  262. expected ast.StreamFields
  263. }{
  264. {
  265. name: "with match fields & schema",
  266. streamStmt: &ast.StreamStmt{
  267. StreamFields: []ast.StreamField{
  268. {
  269. Name: "field1",
  270. FieldType: &ast.BasicType{
  271. Type: ast.BIGINT,
  272. },
  273. },
  274. {
  275. Name: "field2",
  276. FieldType: &ast.BasicType{
  277. Type: ast.STRINGS,
  278. },
  279. },
  280. },
  281. Options: &ast.Options{
  282. FORMAT: "protobuf",
  283. SCHEMAID: "myschema.schema1",
  284. TIMESTAMP: "ts",
  285. },
  286. },
  287. expected: []ast.StreamField{
  288. {
  289. Name: "field1",
  290. FieldType: &ast.BasicType{
  291. Type: ast.BIGINT,
  292. },
  293. },
  294. {
  295. Name: "field2",
  296. FieldType: &ast.BasicType{
  297. Type: ast.STRINGS,
  298. },
  299. },
  300. },
  301. },
  302. {
  303. name: "with unmatch fields & schema",
  304. streamStmt: &ast.StreamStmt{
  305. StreamFields: []ast.StreamField{
  306. {
  307. Name: "field1",
  308. FieldType: &ast.BasicType{
  309. Type: ast.STRINGS,
  310. },
  311. },
  312. {
  313. Name: "field2",
  314. FieldType: &ast.BasicType{
  315. Type: ast.STRINGS,
  316. },
  317. },
  318. },
  319. Options: &ast.Options{
  320. FORMAT: "protobuf",
  321. SCHEMAID: "myschema.schema1",
  322. TIMESTAMP: "ts",
  323. },
  324. },
  325. expected: []ast.StreamField{
  326. {
  327. Name: "field1",
  328. FieldType: &ast.BasicType{
  329. Type: ast.BIGINT,
  330. },
  331. },
  332. {
  333. Name: "field2",
  334. FieldType: &ast.BasicType{
  335. Type: ast.STRINGS,
  336. },
  337. },
  338. },
  339. },
  340. {
  341. name: "without schema",
  342. streamStmt: &ast.StreamStmt{
  343. StreamFields: []ast.StreamField{
  344. {
  345. Name: "field1",
  346. FieldType: &ast.BasicType{
  347. Type: ast.FLOAT,
  348. },
  349. },
  350. {
  351. Name: "field2",
  352. FieldType: &ast.BasicType{
  353. Type: ast.STRINGS,
  354. },
  355. },
  356. },
  357. Options: &ast.Options{
  358. FORMAT: "json",
  359. TIMESTAMP: "ts",
  360. },
  361. },
  362. expected: []ast.StreamField{
  363. {
  364. Name: "field1",
  365. FieldType: &ast.BasicType{
  366. Type: ast.FLOAT,
  367. },
  368. },
  369. {
  370. Name: "field2",
  371. FieldType: &ast.BasicType{
  372. Type: ast.STRINGS,
  373. },
  374. },
  375. },
  376. },
  377. {
  378. name: "without fields",
  379. streamStmt: &ast.StreamStmt{
  380. Options: &ast.Options{
  381. FORMAT: "protobuf",
  382. SCHEMAID: "myschema.schema1",
  383. TIMESTAMP: "ts",
  384. },
  385. },
  386. expected: []ast.StreamField{
  387. {
  388. Name: "field1",
  389. FieldType: &ast.BasicType{
  390. Type: ast.BIGINT,
  391. },
  392. },
  393. {
  394. Name: "field2",
  395. FieldType: &ast.BasicType{
  396. Type: ast.STRINGS,
  397. },
  398. },
  399. },
  400. },
  401. {
  402. name: "schemaless",
  403. streamStmt: &ast.StreamStmt{
  404. Options: &ast.Options{
  405. FORMAT: "json",
  406. TIMESTAMP: "ts",
  407. },
  408. },
  409. expected: nil,
  410. },
  411. }
  412. for _, tc := range testCases {
  413. t.Run(tc.name, func(t *testing.T) {
  414. actual, err := convertStreamInfo(tc.streamStmt)
  415. if err != nil {
  416. t.Errorf("unexpected error: %v", err)
  417. return
  418. }
  419. if !reflect.DeepEqual(actual.schema, tc.expected) {
  420. t.Errorf("unexpected result: got %v, want %v", actual.schema, tc.expected)
  421. }
  422. })
  423. }
  424. }