analyzer_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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. store, err := store.GetKV("stream")
  200. if err != nil {
  201. t.Error(err)
  202. return
  203. }
  204. streamSqls := map[string]string{
  205. "src1": `CREATE STREAM src1 (
  206. ) WITH (DATASOURCE="src1", FORMAT="json", KEY="ts");`,
  207. }
  208. types := map[string]ast.StreamType{
  209. "src1": ast.TypeStream,
  210. }
  211. for name, sql := range streamSqls {
  212. s, err := json.Marshal(&xsql.StreamInfo{
  213. StreamType: types[name],
  214. Statement: sql,
  215. })
  216. if err != nil {
  217. t.Error(err)
  218. t.Fail()
  219. }
  220. store.Set(name, string(s))
  221. }
  222. streams := make(map[string]*ast.StreamStmt)
  223. for n := range streamSqls {
  224. streamStmt, err := xsql.GetDataSource(store, n)
  225. if err != nil {
  226. t.Errorf("fail to get stream %s, please check if stream is created", n)
  227. return
  228. }
  229. streams[n] = streamStmt
  230. }
  231. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  232. for i, tt := range tests {
  233. stmt, err := xsql.NewParser(strings.NewReader(tt.sql)).Parse()
  234. if err != nil {
  235. t.Errorf("%d. %q: error compile sql: %s\n", i, tt.sql, err)
  236. continue
  237. }
  238. _, err = createLogicalPlan(stmt, &api.RuleOption{
  239. IsEventTime: false,
  240. LateTol: 0,
  241. Concurrency: 0,
  242. BufferLength: 0,
  243. SendMetaToSink: false,
  244. Qos: 0,
  245. CheckpointInterval: 0,
  246. SendError: true,
  247. }, store)
  248. serr := tt.r.Serr()
  249. if !reflect.DeepEqual(serr, testx.Errstring(err)) {
  250. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.sql, serr, err)
  251. }
  252. }
  253. }
  254. func TestConvertStreamInfo(t *testing.T) {
  255. testCases := []struct {
  256. name string
  257. streamStmt *ast.StreamStmt
  258. expected ast.StreamFields
  259. }{
  260. {
  261. name: "with match fields & schema",
  262. streamStmt: &ast.StreamStmt{
  263. StreamFields: []ast.StreamField{
  264. {
  265. Name: "field1",
  266. FieldType: &ast.BasicType{
  267. Type: ast.BIGINT,
  268. },
  269. },
  270. {
  271. Name: "field2",
  272. FieldType: &ast.BasicType{
  273. Type: ast.STRINGS,
  274. },
  275. },
  276. },
  277. Options: &ast.Options{
  278. FORMAT: "protobuf",
  279. SCHEMAID: "myschema.schema1",
  280. TIMESTAMP: "ts",
  281. },
  282. },
  283. expected: []ast.StreamField{
  284. {
  285. Name: "field1",
  286. FieldType: &ast.BasicType{
  287. Type: ast.BIGINT,
  288. },
  289. },
  290. {
  291. Name: "field2",
  292. FieldType: &ast.BasicType{
  293. Type: ast.STRINGS,
  294. },
  295. },
  296. },
  297. },
  298. {
  299. name: "with unmatch fields & schema",
  300. streamStmt: &ast.StreamStmt{
  301. StreamFields: []ast.StreamField{
  302. {
  303. Name: "field1",
  304. FieldType: &ast.BasicType{
  305. Type: ast.STRINGS,
  306. },
  307. },
  308. {
  309. Name: "field2",
  310. FieldType: &ast.BasicType{
  311. Type: ast.STRINGS,
  312. },
  313. },
  314. },
  315. Options: &ast.Options{
  316. FORMAT: "protobuf",
  317. SCHEMAID: "myschema.schema1",
  318. TIMESTAMP: "ts",
  319. },
  320. },
  321. expected: []ast.StreamField{
  322. {
  323. Name: "field1",
  324. FieldType: &ast.BasicType{
  325. Type: ast.BIGINT,
  326. },
  327. },
  328. {
  329. Name: "field2",
  330. FieldType: &ast.BasicType{
  331. Type: ast.STRINGS,
  332. },
  333. },
  334. },
  335. },
  336. {
  337. name: "without schema",
  338. streamStmt: &ast.StreamStmt{
  339. StreamFields: []ast.StreamField{
  340. {
  341. Name: "field1",
  342. FieldType: &ast.BasicType{
  343. Type: ast.FLOAT,
  344. },
  345. },
  346. {
  347. Name: "field2",
  348. FieldType: &ast.BasicType{
  349. Type: ast.STRINGS,
  350. },
  351. },
  352. },
  353. Options: &ast.Options{
  354. FORMAT: "json",
  355. TIMESTAMP: "ts",
  356. },
  357. },
  358. expected: []ast.StreamField{
  359. {
  360. Name: "field1",
  361. FieldType: &ast.BasicType{
  362. Type: ast.FLOAT,
  363. },
  364. },
  365. {
  366. Name: "field2",
  367. FieldType: &ast.BasicType{
  368. Type: ast.STRINGS,
  369. },
  370. },
  371. },
  372. },
  373. {
  374. name: "without fields",
  375. streamStmt: &ast.StreamStmt{
  376. Options: &ast.Options{
  377. FORMAT: "protobuf",
  378. SCHEMAID: "myschema.schema1",
  379. TIMESTAMP: "ts",
  380. },
  381. },
  382. expected: []ast.StreamField{
  383. {
  384. Name: "field1",
  385. FieldType: &ast.BasicType{
  386. Type: ast.BIGINT,
  387. },
  388. },
  389. {
  390. Name: "field2",
  391. FieldType: &ast.BasicType{
  392. Type: ast.STRINGS,
  393. },
  394. },
  395. },
  396. },
  397. {
  398. name: "schemaless",
  399. streamStmt: &ast.StreamStmt{
  400. Options: &ast.Options{
  401. FORMAT: "json",
  402. TIMESTAMP: "ts",
  403. },
  404. },
  405. expected: nil,
  406. },
  407. }
  408. for _, tc := range testCases {
  409. t.Run(tc.name, func(t *testing.T) {
  410. actual, err := convertStreamInfo(tc.streamStmt)
  411. if err != nil {
  412. t.Errorf("unexpected error: %v", err)
  413. return
  414. }
  415. if !reflect.DeepEqual(actual.schema, tc.expected) {
  416. t.Errorf("unexpected result: got %v, want %v", actual.schema, tc.expected)
  417. }
  418. })
  419. }
  420. }