analyzer_test.go 11 KB

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