analyzer_test.go 10 KB

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