planner_graph_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  1. // Copyright 2022-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. "testing"
  20. "github.com/lf-edge/ekuiper/internal/pkg/store"
  21. "github.com/lf-edge/ekuiper/internal/testx"
  22. "github.com/lf-edge/ekuiper/internal/xsql"
  23. "github.com/lf-edge/ekuiper/pkg/api"
  24. "github.com/lf-edge/ekuiper/pkg/ast"
  25. )
  26. func TestPlannerGraphValidate(t *testing.T) {
  27. tests := []struct {
  28. graph string
  29. err string
  30. }{
  31. {
  32. graph: `{
  33. "nodes": {
  34. "abc": {
  35. "type": "source",
  36. "nodeType": "mqtt",
  37. "props": {
  38. "datasource": "demo"
  39. }
  40. },
  41. "myfilter": {
  42. "type": "operator",
  43. "nodeType": "filter",
  44. "props": {
  45. "expr": "temperature > 20"
  46. }
  47. },
  48. "logfunc": {
  49. "type": "operator",
  50. "nodeType": "function",
  51. "props": {
  52. "expr": "log(temperature) as log_temperature"
  53. }
  54. },
  55. "sinfunc": {
  56. "type": "operator",
  57. "nodeType": "function",
  58. "props": {
  59. "expr": "sin(temperature) as sin_temperature"
  60. }
  61. },
  62. "pick": {
  63. "type": "operator",
  64. "nodeType": "pick",
  65. "props": {
  66. "fields": [
  67. "log_temperature",
  68. "humidity"
  69. ]
  70. }
  71. },
  72. "mqttpv": {
  73. "type": "sink",
  74. "nodeType": "mqtt",
  75. "props": {
  76. "server": "tcp://syno.home:1883",
  77. "topic": "result",
  78. "sendSingle": true
  79. }
  80. },
  81. "mqtt2": {
  82. "type": "sink",
  83. "nodeType": "mqtt",
  84. "props": {
  85. "server": "tcp://syno.home:1883",
  86. "topic": "result2",
  87. "sendSingle": true
  88. }
  89. }
  90. },
  91. "topo": {
  92. "sources": [
  93. "abc"
  94. ],
  95. "edges": {
  96. "abc": [
  97. "myfilter",
  98. "sinfunc"
  99. ],
  100. "myfilter": [
  101. "logfunc"
  102. ],
  103. "logfunc": [
  104. "pick"
  105. ],
  106. "pick": [
  107. "mqttpv"
  108. ],
  109. "sinfunc": [
  110. "mqtt2"
  111. ]
  112. }
  113. }
  114. }`,
  115. err: "",
  116. }, {
  117. graph: `{
  118. "nodes": {
  119. "abc": {
  120. "type": "source",
  121. "nodeType": "mqtt",
  122. "props": {
  123. "datasource": "demo"
  124. }
  125. },
  126. "mqtt2": {
  127. "type": "sink",
  128. "nodeType": "mqtt",
  129. "props": {
  130. "server": "tcp://syno.home:1883",
  131. "topic": "result2",
  132. "sendSingle": true
  133. }
  134. }
  135. },
  136. "topo": {
  137. "sources": [
  138. "abc"
  139. ],
  140. "edges": {
  141. "abc": [
  142. "myfilter"
  143. ]
  144. }
  145. }
  146. }`,
  147. err: "node myfilter is not defined",
  148. }, {
  149. graph: `{
  150. "nodes": {
  151. "abc": {
  152. "type": "source",
  153. "nodeType": "mqtt",
  154. "props": {
  155. "datasource": "demo"
  156. }
  157. },
  158. "mqtt2": {
  159. "type": "sink",
  160. "nodeType": "mqtt",
  161. "props": {
  162. "server": "tcp://syno.home:1883",
  163. "topic": "result2",
  164. "sendSingle": true
  165. }
  166. }
  167. },
  168. "topo": {
  169. "sources": [
  170. "abc"
  171. ],
  172. "edges": {
  173. }
  174. }
  175. }`,
  176. err: "no edge defined for source node abc",
  177. }, {
  178. graph: `{
  179. "nodes": {
  180. "abc": {
  181. "type": "source",
  182. "nodeType": "mqtt",
  183. "props": {
  184. "datasource": "demo"
  185. }
  186. },
  187. "aggfunc": {
  188. "type": "operator",
  189. "nodeType": "aggfunc",
  190. "props": {
  191. "expr": "avg(temperature) as avg_temperature"
  192. }
  193. },
  194. "mqtt2": {
  195. "type": "sink",
  196. "nodeType": "mqtt",
  197. "props": {
  198. "server": "tcp://syno.home:1883",
  199. "topic": "result2",
  200. "sendSingle": true
  201. }
  202. }
  203. },
  204. "topo": {
  205. "sources": [
  206. "abc"
  207. ],
  208. "edges": {
  209. "abc": ["aggfunc"],
  210. "aggfunc": ["mqtt2"]
  211. }
  212. }
  213. }`,
  214. err: "node abc output does not match node aggfunc input: input type mismatch, expect collection, got row",
  215. }, {
  216. graph: `{
  217. "nodes": {
  218. "abc": {
  219. "type": "source",
  220. "nodeType": "mqtt",
  221. "props": {
  222. "datasource": "demo"
  223. }
  224. },
  225. "abc2": {
  226. "type": "source",
  227. "nodeType": "mqtt",
  228. "props": {
  229. "datasource": "demo1"
  230. }
  231. },
  232. "joinop": {
  233. "type": "operator",
  234. "nodeType": "join",
  235. "props": {
  236. "from": "abc",
  237. "joins": [
  238. {
  239. "name": "abc2",
  240. "type": "inner",
  241. "on": "abc.id = abc2.id"
  242. }
  243. ]
  244. }
  245. },
  246. "mqtt2": {
  247. "type": "sink",
  248. "nodeType": "mqtt",
  249. "props": {
  250. "server": "tcp://syno.home:1883",
  251. "topic": "result2",
  252. "sendSingle": true
  253. }
  254. }
  255. },
  256. "topo": {
  257. "sources": [
  258. "abc","abc2"
  259. ],
  260. "edges": {
  261. "abc": ["joinop"],
  262. "abc2": ["joinop"],
  263. "joinop": ["mqtt2"]
  264. }
  265. }
  266. }`,
  267. err: "operator joinop of type join does not allow multiple inputs",
  268. }, {
  269. graph: `{
  270. "nodes": {
  271. "abc": {
  272. "type": "source",
  273. "nodeType": "mqtt",
  274. "props": {
  275. "datasource": "demo"
  276. }
  277. },
  278. "abc2": {
  279. "type": "source",
  280. "nodeType": "mqtt",
  281. "props": {
  282. "datasource": "demo1"
  283. }
  284. },
  285. "windowop": {
  286. "type": "operator",
  287. "nodeType": "window",
  288. "props": {
  289. "type": "hoppingwindow",
  290. "unit": "ss",
  291. "size": 10,
  292. "interval": 5
  293. }
  294. },
  295. "joinop": {
  296. "type": "operator",
  297. "nodeType": "join",
  298. "props": {
  299. "from": "abc",
  300. "joins": [
  301. {
  302. "name": "abc2",
  303. "type": "inner",
  304. "on": "abc.id = abc2.id"
  305. }
  306. ]
  307. }
  308. },
  309. "groupop": {
  310. "type": "operator",
  311. "nodeType": "groupby",
  312. "props": {
  313. "dimensions": ["id","userId"]
  314. }
  315. },
  316. "mqtt2": {
  317. "type": "sink",
  318. "nodeType": "mqtt",
  319. "props": {
  320. "server": "tcp://syno.home:1883",
  321. "topic": "result2",
  322. "sendSingle": true
  323. }
  324. }
  325. },
  326. "topo": {
  327. "sources": [
  328. "abc","abc2"
  329. ],
  330. "edges": {
  331. "abc": ["windowop"],
  332. "abc2": ["windowop"],
  333. "windowop": ["joinop"],
  334. "joinop": ["groupop"],
  335. "groupop": ["mqtt2"]
  336. }
  337. }
  338. }`,
  339. err: "",
  340. }, {
  341. graph: `{
  342. "nodes": {
  343. "abc": {
  344. "type": "source",
  345. "nodeType": "mqtt",
  346. "props": {
  347. "datasource": "demo"
  348. }
  349. },
  350. "abc2": {
  351. "type": "source",
  352. "nodeType": "mqtt",
  353. "props": {
  354. "datasource": "demo1"
  355. }
  356. },
  357. "windowop": {
  358. "type": "operator",
  359. "nodeType": "window",
  360. "props": {
  361. "type": "hoppingwindow",
  362. "unit": "ss",
  363. "size": 10,
  364. "interval": 5
  365. }
  366. },
  367. "joinop": {
  368. "type": "operator",
  369. "nodeType": "join",
  370. "props": {
  371. "from": "abc",
  372. "joins": [
  373. {
  374. "name": "abc2",
  375. "type": "inner",
  376. "on": "abc.id = abc2.id"
  377. }
  378. ]
  379. }
  380. },
  381. "groupop": {
  382. "type": "operator",
  383. "nodeType": "groupby",
  384. "props": {
  385. "dimensions": ["id","userId"]
  386. }
  387. },
  388. "mqtt2": {
  389. "type": "sink",
  390. "nodeType": "mqtt",
  391. "props": {
  392. "server": "tcp://syno.home:1883",
  393. "topic": "result2",
  394. "sendSingle": true
  395. }
  396. }
  397. },
  398. "topo": {
  399. "sources": [
  400. "abc","abc2"
  401. ],
  402. "edges": {
  403. "abc": ["windowop"],
  404. "abc2": ["windowop"],
  405. "windowop": ["groupop"],
  406. "joinop": ["mqtt2"],
  407. "groupop": ["joinop"]
  408. }
  409. }
  410. }`,
  411. err: "node groupop output does not match node joinop input: collection type mismatch, expect non-grouped collection, got grouped collection",
  412. }, {
  413. graph: `{
  414. "nodes": {
  415. "abc": {
  416. "type": "source",
  417. "nodeType": "mqtt",
  418. "props": {
  419. "datasource": "demo"
  420. }
  421. },
  422. "abc2": {
  423. "type": "source",
  424. "nodeType": "mqtt",
  425. "props": {
  426. "datasource": "demo1"
  427. }
  428. },
  429. "windowop": {
  430. "type": "operator",
  431. "nodeType": "window",
  432. "props": {
  433. "type": "hoppingwindow",
  434. "unit": "ss",
  435. "size": 10,
  436. "interval": 5
  437. }
  438. },
  439. "joinop": {
  440. "type": "operator",
  441. "nodeType": "join",
  442. "props": {
  443. "from": "abc",
  444. "joins": [
  445. {
  446. "name": "abc2",
  447. "type": "inner",
  448. "on": "abc.id = abc2.id"
  449. }
  450. ]
  451. }
  452. },
  453. "groupop": {
  454. "type": "operator",
  455. "nodeType": "groupby",
  456. "props": {
  457. "dimensions": ["id","userId"]
  458. }
  459. },
  460. "aggfunc": {
  461. "type": "operator",
  462. "nodeType": "aggFunc",
  463. "props": {
  464. "expr": "avg(temperature) as avg_temperature"
  465. }
  466. },
  467. "mqtt2": {
  468. "type": "sink",
  469. "nodeType": "mqtt",
  470. "props": {
  471. "server": "tcp://syno.home:1883",
  472. "topic": "result2",
  473. "sendSingle": true
  474. }
  475. }
  476. },
  477. "topo": {
  478. "sources": [
  479. "abc","abc2"
  480. ],
  481. "edges": {
  482. "abc": ["windowop"],
  483. "abc2": ["windowop"],
  484. "windowop": ["groupop"],
  485. "joinop": ["mqtt2"],
  486. "groupop": ["aggfunc"],
  487. "aggfunc": ["joinop"]
  488. }
  489. }
  490. }`,
  491. err: "node aggfunc output does not match node joinop input: collection type mismatch, expect non-grouped collection, got grouped collection",
  492. },
  493. }
  494. t.Logf("The test bucket size is %d.\n\n", len(tests))
  495. for i, tt := range tests {
  496. rg := &api.RuleGraph{}
  497. err := json.Unmarshal([]byte(tt.graph), rg)
  498. if err != nil {
  499. t.Error(err)
  500. continue
  501. }
  502. _, err = PlanByGraph(&api.Rule{
  503. Triggered: false,
  504. Id: fmt.Sprintf("rule%d", i),
  505. Name: fmt.Sprintf("rule%d", i),
  506. Graph: rg,
  507. Options: &api.RuleOption{
  508. IsEventTime: false,
  509. LateTol: 1000,
  510. Concurrency: 1,
  511. BufferLength: 1024,
  512. SendMetaToSink: false,
  513. SendError: true,
  514. Qos: api.AtMostOnce,
  515. CheckpointInterval: 300000,
  516. },
  517. })
  518. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  519. t.Errorf("%d: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.err, err)
  520. }
  521. }
  522. }
  523. func TestPlannerGraphWithStream(t *testing.T) {
  524. store, err := store.GetKV("stream")
  525. if err != nil {
  526. t.Error(err)
  527. return
  528. }
  529. streamSqls := map[string]string{
  530. "src1": `CREATE STREAM src1 (
  531. id1 BIGINT,
  532. temp BIGINT,
  533. name string,
  534. myarray array(string)
  535. ) WITH (DATASOURCE="src1", FORMAT="json", KEY="ts");`,
  536. "src2": `CREATE STREAM src2 (
  537. id2 BIGINT,
  538. hum BIGINT
  539. ) WITH (DATASOURCE="src2", FORMAT="json", KEY="ts", TIMESTAMP_FORMAT="YYYY-MM-dd HH:mm:ss");`,
  540. "tableInPlanner": `CREATE TABLE tableInPlanner (
  541. id BIGINT,
  542. name STRING,
  543. value STRING,
  544. hum BIGINT
  545. ) WITH (TYPE="file");`,
  546. }
  547. types := map[string]ast.StreamType{
  548. "src1": ast.TypeStream,
  549. "src2": ast.TypeStream,
  550. "tableInPlanner": ast.TypeTable,
  551. }
  552. for name, sql := range streamSqls {
  553. s, err := json.Marshal(&xsql.StreamInfo{
  554. StreamType: types[name],
  555. Statement: sql,
  556. })
  557. if err != nil {
  558. t.Error(err)
  559. t.Fail()
  560. }
  561. err = store.Set(name, string(s))
  562. if err != nil {
  563. t.Error(err)
  564. t.Fail()
  565. }
  566. }
  567. testCases := []struct {
  568. name string
  569. graph string
  570. err error
  571. }{
  572. {
  573. name: "test stream",
  574. graph: `{
  575. "nodes": {
  576. "demo": {
  577. "type": "source",
  578. "nodeType": "mqtt",
  579. "props": {
  580. "sourceType": "stream",
  581. "sourceName": "src1"
  582. }
  583. },
  584. "log": {
  585. "type": "sink",
  586. "nodeType": "log",
  587. "props": {}
  588. }
  589. },
  590. "topo": {
  591. "sources": ["demo"],
  592. "edges": {
  593. "demo": ["log"]
  594. }
  595. }
  596. }`,
  597. err: nil,
  598. },
  599. {
  600. name: "stream type wrong",
  601. graph: `{
  602. "nodes": {
  603. "demo": {
  604. "type": "source",
  605. "nodeType": "file",
  606. "props": {
  607. "sourceType": "stream",
  608. "sourceName": "src1"
  609. }
  610. },
  611. "log": {
  612. "type": "sink",
  613. "nodeType": "log",
  614. "props": {}
  615. }
  616. },
  617. "topo": {
  618. "sources": ["demo"],
  619. "edges": {
  620. "demo": ["log"]
  621. }
  622. }
  623. }`,
  624. err: fmt.Errorf("source type file does not match the stream type mqtt"),
  625. },
  626. {
  627. name: "non exist stream",
  628. graph: `{
  629. "nodes": {
  630. "demo": {
  631. "type": "source",
  632. "nodeType": "mqtt",
  633. "props": {
  634. "sourceType": "stream",
  635. "sourceName": "unknown"
  636. }
  637. },
  638. "log": {
  639. "type": "sink",
  640. "nodeType": "log",
  641. "props": {}
  642. }
  643. },
  644. "topo": {
  645. "sources": ["demo"],
  646. "edges": {
  647. "demo": ["log"]
  648. }
  649. }
  650. }`,
  651. err: fmt.Errorf("fail to get stream unknown, please check if stream is created"),
  652. },
  653. {
  654. name: "wrong source type",
  655. graph: `{
  656. "nodes": {
  657. "demo": {
  658. "type": "source",
  659. "nodeType": "mqtt",
  660. "props": {
  661. "sourceType": "stream",
  662. "sourceName": "tableInPlanner"
  663. }
  664. },
  665. "log": {
  666. "type": "sink",
  667. "nodeType": "log",
  668. "props": {}
  669. }
  670. },
  671. "topo": {
  672. "sources": ["demo"],
  673. "edges": {
  674. "demo": ["log"]
  675. }
  676. }
  677. }`,
  678. err: fmt.Errorf("table tableInPlanner is not a stream"),
  679. },
  680. }
  681. for _, tc := range testCases {
  682. t.Run(tc.name, func(t *testing.T) {
  683. rg := &api.RuleGraph{}
  684. err := json.Unmarshal([]byte(tc.graph), rg)
  685. if err != nil {
  686. t.Error(err)
  687. return
  688. }
  689. _, err = PlanByGraph(&api.Rule{
  690. Triggered: false,
  691. Id: "test",
  692. Graph: rg,
  693. Options: &api.RuleOption{
  694. IsEventTime: false,
  695. LateTol: 1000,
  696. Concurrency: 1,
  697. BufferLength: 1024,
  698. SendMetaToSink: false,
  699. SendError: true,
  700. Qos: api.AtMostOnce,
  701. CheckpointInterval: 300000,
  702. },
  703. })
  704. if tc.err == nil {
  705. if err != nil {
  706. t.Errorf("error mismatch:\n exp=%s\n got=%s\n\n", tc.err, err)
  707. }
  708. return
  709. }
  710. if !reflect.DeepEqual(tc.err.Error(), err.Error()) {
  711. t.Errorf("error mismatch:\n exp=%s\n got=%s\n\n", tc.err, err)
  712. }
  713. })
  714. }
  715. }