planner_graph_test.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998
  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. {
  118. graph: `{
  119. "nodes": {
  120. "abc": {
  121. "type": "source",
  122. "nodeType": "mqtt",
  123. "props": {
  124. "datasource": "demo"
  125. }
  126. },
  127. "mqtt2": {
  128. "type": "sink",
  129. "nodeType": "mqtt",
  130. "props": {
  131. "server": "tcp://syno.home:1883",
  132. "topic": "result2",
  133. "sendSingle": true
  134. }
  135. }
  136. },
  137. "topo": {
  138. "sources": [
  139. "abc"
  140. ],
  141. "edges": {
  142. "abc": [
  143. "myfilter"
  144. ]
  145. }
  146. }
  147. }`,
  148. err: "node myfilter is not defined",
  149. },
  150. {
  151. graph: `{
  152. "nodes": {
  153. "abc": {
  154. "type": "source",
  155. "nodeType": "mqtt",
  156. "props": {
  157. "datasource": "demo"
  158. }
  159. },
  160. "mqtt2": {
  161. "type": "sink",
  162. "nodeType": "mqtt",
  163. "props": {
  164. "server": "tcp://syno.home:1883",
  165. "topic": "result2",
  166. "sendSingle": true
  167. }
  168. }
  169. },
  170. "topo": {
  171. "sources": [
  172. "abc"
  173. ],
  174. "edges": {
  175. }
  176. }
  177. }`,
  178. err: "no edge defined for source node abc",
  179. },
  180. {
  181. graph: `{
  182. "nodes": {
  183. "abc": {
  184. "type": "source",
  185. "nodeType": "mqtt",
  186. "props": {
  187. "datasource": "demo"
  188. }
  189. },
  190. "aggfunc": {
  191. "type": "operator",
  192. "nodeType": "aggfunc",
  193. "props": {
  194. "expr": "avg(temperature) as avg_temperature"
  195. }
  196. },
  197. "mqtt2": {
  198. "type": "sink",
  199. "nodeType": "mqtt",
  200. "props": {
  201. "server": "tcp://syno.home:1883",
  202. "topic": "result2",
  203. "sendSingle": true
  204. }
  205. }
  206. },
  207. "topo": {
  208. "sources": [
  209. "abc"
  210. ],
  211. "edges": {
  212. "abc": ["aggfunc"],
  213. "aggfunc": ["mqtt2"]
  214. }
  215. }
  216. }`,
  217. err: "node abc output does not match node aggfunc input: input type mismatch, expect collection, got row",
  218. },
  219. {
  220. graph: `{
  221. "nodes": {
  222. "abc": {
  223. "type": "source",
  224. "nodeType": "mqtt",
  225. "props": {
  226. "datasource": "demo"
  227. }
  228. },
  229. "abc2": {
  230. "type": "source",
  231. "nodeType": "mqtt",
  232. "props": {
  233. "datasource": "demo1"
  234. }
  235. },
  236. "joinop": {
  237. "type": "operator",
  238. "nodeType": "join",
  239. "props": {
  240. "from": "abc",
  241. "joins": [
  242. {
  243. "name": "abc2",
  244. "type": "inner",
  245. "on": "abc.id = abc2.id"
  246. }
  247. ]
  248. }
  249. },
  250. "mqtt2": {
  251. "type": "sink",
  252. "nodeType": "mqtt",
  253. "props": {
  254. "server": "tcp://syno.home:1883",
  255. "topic": "result2",
  256. "sendSingle": true
  257. }
  258. }
  259. },
  260. "topo": {
  261. "sources": [
  262. "abc","abc2"
  263. ],
  264. "edges": {
  265. "abc": ["joinop"],
  266. "abc2": ["joinop"],
  267. "joinop": ["mqtt2"]
  268. }
  269. }
  270. }`,
  271. err: "join node joinop does not allow multiple stream inputs",
  272. },
  273. {
  274. graph: `{
  275. "nodes": {
  276. "abc": {
  277. "type": "source",
  278. "nodeType": "mqtt",
  279. "props": {
  280. "datasource": "demo"
  281. }
  282. },
  283. "abc2": {
  284. "type": "source",
  285. "nodeType": "mqtt",
  286. "props": {
  287. "datasource": "demo1"
  288. }
  289. },
  290. "windowop": {
  291. "type": "operator",
  292. "nodeType": "window",
  293. "props": {
  294. "type": "hoppingwindow",
  295. "unit": "ss",
  296. "size": 10,
  297. "interval": 5
  298. }
  299. },
  300. "joinop": {
  301. "type": "operator",
  302. "nodeType": "join",
  303. "props": {
  304. "from": "abc",
  305. "joins": [
  306. {
  307. "name": "abc2",
  308. "type": "inner",
  309. "on": "abc.id = abc2.id"
  310. }
  311. ]
  312. }
  313. },
  314. "groupop": {
  315. "type": "operator",
  316. "nodeType": "groupby",
  317. "props": {
  318. "dimensions": ["id","userId"]
  319. }
  320. },
  321. "mqtt2": {
  322. "type": "sink",
  323. "nodeType": "mqtt",
  324. "props": {
  325. "server": "tcp://syno.home:1883",
  326. "topic": "result2",
  327. "sendSingle": true
  328. }
  329. }
  330. },
  331. "topo": {
  332. "sources": [
  333. "abc","abc2"
  334. ],
  335. "edges": {
  336. "abc": ["windowop"],
  337. "abc2": ["windowop"],
  338. "windowop": ["joinop"],
  339. "joinop": ["groupop"],
  340. "groupop": ["mqtt2"]
  341. }
  342. }
  343. }`,
  344. err: "",
  345. },
  346. {
  347. graph: `{
  348. "nodes": {
  349. "abc": {
  350. "type": "source",
  351. "nodeType": "mqtt",
  352. "props": {
  353. "datasource": "demo"
  354. }
  355. },
  356. "abc2": {
  357. "type": "source",
  358. "nodeType": "mqtt",
  359. "props": {
  360. "datasource": "demo1"
  361. }
  362. },
  363. "windowop": {
  364. "type": "operator",
  365. "nodeType": "window",
  366. "props": {
  367. "type": "hoppingwindow",
  368. "unit": "ss",
  369. "size": 10,
  370. "interval": 5
  371. }
  372. },
  373. "joinop": {
  374. "type": "operator",
  375. "nodeType": "join",
  376. "props": {
  377. "from": "abc",
  378. "joins": [
  379. {
  380. "name": "abc2",
  381. "type": "inner",
  382. "on": "abc.id = abc2.id"
  383. }
  384. ]
  385. }
  386. },
  387. "groupop": {
  388. "type": "operator",
  389. "nodeType": "groupby",
  390. "props": {
  391. "dimensions": ["id","userId"]
  392. }
  393. },
  394. "mqtt2": {
  395. "type": "sink",
  396. "nodeType": "mqtt",
  397. "props": {
  398. "server": "tcp://syno.home:1883",
  399. "topic": "result2",
  400. "sendSingle": true
  401. }
  402. }
  403. },
  404. "topo": {
  405. "sources": [
  406. "abc","abc2"
  407. ],
  408. "edges": {
  409. "abc": ["windowop"],
  410. "abc2": ["windowop"],
  411. "windowop": ["groupop"],
  412. "joinop": ["mqtt2"],
  413. "groupop": ["joinop"]
  414. }
  415. }
  416. }`,
  417. err: "node groupop output does not match node joinop input: collection type mismatch, expect non-grouped collection, got grouped collection",
  418. },
  419. {
  420. graph: `{
  421. "nodes": {
  422. "abc": {
  423. "type": "source",
  424. "nodeType": "mqtt",
  425. "props": {
  426. "datasource": "demo"
  427. }
  428. },
  429. "abc2": {
  430. "type": "source",
  431. "nodeType": "mqtt",
  432. "props": {
  433. "datasource": "demo1"
  434. }
  435. },
  436. "windowop": {
  437. "type": "operator",
  438. "nodeType": "window",
  439. "props": {
  440. "type": "hoppingwindow",
  441. "unit": "ss",
  442. "size": 10,
  443. "interval": 5
  444. }
  445. },
  446. "joinop": {
  447. "type": "operator",
  448. "nodeType": "join",
  449. "props": {
  450. "from": "abc",
  451. "joins": [
  452. {
  453. "name": "abc2",
  454. "type": "inner",
  455. "on": "abc.id = abc2.id"
  456. }
  457. ]
  458. }
  459. },
  460. "groupop": {
  461. "type": "operator",
  462. "nodeType": "groupby",
  463. "props": {
  464. "dimensions": ["id","userId"]
  465. }
  466. },
  467. "aggfunc": {
  468. "type": "operator",
  469. "nodeType": "aggFunc",
  470. "props": {
  471. "expr": "avg(temperature) as avg_temperature"
  472. }
  473. },
  474. "mqtt2": {
  475. "type": "sink",
  476. "nodeType": "mqtt",
  477. "props": {
  478. "server": "tcp://syno.home:1883",
  479. "topic": "result2",
  480. "sendSingle": true
  481. }
  482. }
  483. },
  484. "topo": {
  485. "sources": [
  486. "abc","abc2"
  487. ],
  488. "edges": {
  489. "abc": ["windowop"],
  490. "abc2": ["windowop"],
  491. "windowop": ["groupop"],
  492. "joinop": ["mqtt2"],
  493. "groupop": ["aggfunc"],
  494. "aggfunc": ["joinop"]
  495. }
  496. }
  497. }`,
  498. err: "node aggfunc output does not match node joinop input: collection type mismatch, expect non-grouped collection, got grouped collection",
  499. },
  500. {
  501. graph: `{
  502. "nodes": {
  503. "abc": {
  504. "type": "source",
  505. "nodeType": "mqtt",
  506. "props": {
  507. "datasource": "demo"
  508. }
  509. },
  510. "aggfunc": {
  511. "type": "operator",
  512. "nodeType": "aggfunc",
  513. "props": {
  514. "expr": "avg(,temperature) as avg_temperature"
  515. }
  516. },
  517. "mqtt2": {
  518. "type": "sink",
  519. "nodeType": "mqtt",
  520. "props": {
  521. "server": "tcp://syno.home:1883",
  522. "topic": "result2",
  523. "sendSingle": true
  524. }
  525. }
  526. },
  527. "topo": {
  528. "sources": [
  529. "abc"
  530. ],
  531. "edges": {
  532. "abc": ["aggfunc"],
  533. "aggfunc": ["mqtt2"]
  534. }
  535. }
  536. }`,
  537. err: "parse aggfunc aggfunc with map[expr:avg(,temperature) as avg_temperature] error: found \",\", expected expression.",
  538. },
  539. {
  540. graph: `{
  541. "nodes": {
  542. "abc": {
  543. "type": "source",
  544. "nodeType": "mqtt",
  545. "props": {
  546. "datasource": "demo"
  547. }
  548. },
  549. "myfilter": {
  550. "type": "operator",
  551. "nodeType": "filter",
  552. "props": {
  553. "expr": "data.nested.temperature > 20"
  554. }
  555. },
  556. "mqttpv": {
  557. "type": "sink",
  558. "nodeType": "mqtt",
  559. "props": {
  560. "server": "tcp://syno.home:1883",
  561. "topic": "result",
  562. "sendSingle": true
  563. }
  564. }
  565. },
  566. "topo": {
  567. "sources": [
  568. "abc"
  569. ],
  570. "edges": {
  571. "abc": [
  572. "myfilter"
  573. ],
  574. "myfilter": [
  575. "mqttpv"
  576. ]
  577. }
  578. }
  579. }`,
  580. err: "",
  581. },
  582. {
  583. graph: `{
  584. "nodes": {
  585. "log": {
  586. "type": "sink",
  587. "nodeType": "log",
  588. "props": {}
  589. },
  590. "mqtt": {
  591. "type": "source",
  592. "nodeType": "mqtt",
  593. "props": {
  594. "datasource": "demo",
  595. "format": "protobuf",
  596. "schemaMessage": "PropertiesReport",
  597. "schemaName": "EventBusMessage",
  598. "shared": false
  599. }
  600. }
  601. },
  602. "topo": {
  603. "sources": [
  604. "mqtt"
  605. ],
  606. "edges": {
  607. "mqtt": [
  608. "log"
  609. ]
  610. }
  611. }
  612. }`,
  613. err: "",
  614. },
  615. }
  616. t.Logf("The test bucket size is %d.\n\n", len(tests))
  617. for i, tt := range tests {
  618. rg := &api.RuleGraph{}
  619. err := json.Unmarshal([]byte(tt.graph), rg)
  620. if err != nil {
  621. t.Error(err)
  622. continue
  623. }
  624. _, err = PlanByGraph(&api.Rule{
  625. Triggered: false,
  626. Id: fmt.Sprintf("rule%d", i),
  627. Name: fmt.Sprintf("rule%d", i),
  628. Graph: rg,
  629. Options: &api.RuleOption{
  630. IsEventTime: false,
  631. LateTol: 1000,
  632. Concurrency: 1,
  633. BufferLength: 1024,
  634. SendMetaToSink: false,
  635. SendError: true,
  636. Qos: api.AtMostOnce,
  637. CheckpointInterval: 300000,
  638. },
  639. })
  640. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  641. t.Errorf("%d: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.err, err)
  642. }
  643. }
  644. }
  645. func TestPlannerGraphWithStream(t *testing.T) {
  646. store, err := store.GetKV("stream")
  647. if err != nil {
  648. t.Error(err)
  649. return
  650. }
  651. streamSqls := map[string]string{
  652. "src1": `CREATE STREAM src1 (
  653. id1 BIGINT,
  654. temp BIGINT,
  655. name string,
  656. myarray array(string)
  657. ) WITH (DATASOURCE="src1", FORMAT="json", KEY="ts");`,
  658. "src2": `CREATE STREAM src2 (
  659. id2 BIGINT,
  660. hum BIGINT
  661. ) WITH (DATASOURCE="src2", FORMAT="json", KEY="ts", TIMESTAMP_FORMAT="YYYY-MM-dd HH:mm:ss");`,
  662. "tableInPlanner": `CREATE TABLE tableInPlanner (
  663. id BIGINT,
  664. name STRING,
  665. value STRING,
  666. hum BIGINT
  667. ) WITH (TYPE="file");`,
  668. "lookupT": `CREATE TABLE lookupT () WITH (DATASOURCE="alertVal", TYPE="memory", KIND="lookup", KEY="id");`,
  669. }
  670. types := map[string]ast.StreamType{
  671. "src1": ast.TypeStream,
  672. "src2": ast.TypeStream,
  673. "tableInPlanner": ast.TypeTable,
  674. "lookupT": ast.TypeTable,
  675. }
  676. for name, sql := range streamSqls {
  677. s, err := json.Marshal(&xsql.StreamInfo{
  678. StreamType: types[name],
  679. Statement: sql,
  680. })
  681. if err != nil {
  682. t.Error(err)
  683. t.Fail()
  684. }
  685. err = store.Set(name, string(s))
  686. if err != nil {
  687. t.Error(err)
  688. t.Fail()
  689. }
  690. }
  691. testCases := []struct {
  692. name string
  693. graph string
  694. err error
  695. }{
  696. {
  697. name: "test stream",
  698. graph: `{
  699. "nodes": {
  700. "demo": {
  701. "type": "source",
  702. "nodeType": "mqtt",
  703. "props": {
  704. "sourceType": "stream",
  705. "sourceName": "src1"
  706. }
  707. },
  708. "log": {
  709. "type": "sink",
  710. "nodeType": "log",
  711. "props": {}
  712. }
  713. },
  714. "topo": {
  715. "sources": ["demo"],
  716. "edges": {
  717. "demo": ["log"]
  718. }
  719. }
  720. }`,
  721. err: nil,
  722. },
  723. {
  724. name: "stream type wrong",
  725. graph: `{
  726. "nodes": {
  727. "demo": {
  728. "type": "source",
  729. "nodeType": "file",
  730. "props": {
  731. "sourceType": "stream",
  732. "sourceName": "src1"
  733. }
  734. },
  735. "log": {
  736. "type": "sink",
  737. "nodeType": "log",
  738. "props": {}
  739. }
  740. },
  741. "topo": {
  742. "sources": ["demo"],
  743. "edges": {
  744. "demo": ["log"]
  745. }
  746. }
  747. }`,
  748. err: fmt.Errorf("parse source demo with map[sourceName:src1 sourceType:stream] error: source type file does not match the stream type mqtt"),
  749. },
  750. {
  751. name: "non exist stream",
  752. graph: `{
  753. "nodes": {
  754. "demo": {
  755. "type": "source",
  756. "nodeType": "mqtt",
  757. "props": {
  758. "sourceType": "stream",
  759. "sourceName": "unknown"
  760. }
  761. },
  762. "log": {
  763. "type": "sink",
  764. "nodeType": "log",
  765. "props": {}
  766. }
  767. },
  768. "topo": {
  769. "sources": ["demo"],
  770. "edges": {
  771. "demo": ["log"]
  772. }
  773. }
  774. }`,
  775. err: fmt.Errorf("parse source demo with map[sourceName:unknown sourceType:stream] error: fail to get stream unknown, please check if stream is created"),
  776. },
  777. {
  778. name: "wrong source type",
  779. graph: `{
  780. "nodes": {
  781. "demo": {
  782. "type": "source",
  783. "nodeType": "mqtt",
  784. "props": {
  785. "sourceType": "stream",
  786. "sourceName": "tableInPlanner"
  787. }
  788. },
  789. "log": {
  790. "type": "sink",
  791. "nodeType": "log",
  792. "props": {}
  793. }
  794. },
  795. "topo": {
  796. "sources": ["demo"],
  797. "edges": {
  798. "demo": ["log"]
  799. }
  800. }
  801. }`,
  802. err: fmt.Errorf("parse source demo with map[sourceName:tableInPlanner sourceType:stream] error: table tableInPlanner is not a stream"),
  803. },
  804. {
  805. name: "stream and table",
  806. graph: `{
  807. "nodes": {
  808. "demo": {
  809. "type": "source",
  810. "nodeType": "mqtt",
  811. "props": {
  812. "sourceType": "stream",
  813. "sourceName": "src1"
  814. }
  815. },
  816. "lookupT":{
  817. "type": "source",
  818. "nodeType": "memory",
  819. "props": {
  820. "sourceType": "table",
  821. "sourceName": "lookupT"
  822. }
  823. },
  824. "joinop": {
  825. "type": "operator",
  826. "nodeType": "join",
  827. "props": {
  828. "from": "src1",
  829. "joins": [
  830. {
  831. "name": "lookupT",
  832. "type": "inner",
  833. "on": "src1.deviceKind = lookupT.id"
  834. }
  835. ]
  836. }
  837. },
  838. "log": {
  839. "type": "sink",
  840. "nodeType": "log",
  841. "props": {}
  842. }
  843. },
  844. "topo": {
  845. "sources": ["demo", "lookupT"],
  846. "edges": {
  847. "demo": ["joinop"],
  848. "lookupT": ["joinop"],
  849. "joinop": ["log"]
  850. }
  851. }
  852. }`,
  853. err: nil,
  854. },
  855. {
  856. name: "wrong join stream name",
  857. graph: `{
  858. "nodes": {
  859. "demo": {
  860. "type": "source",
  861. "nodeType": "mqtt",
  862. "props": {
  863. "sourceType": "stream",
  864. "sourceName": "src1"
  865. }
  866. },
  867. "lookupT":{
  868. "type": "source",
  869. "nodeType": "memory",
  870. "props": {
  871. "sourceType": "table",
  872. "sourceName": "lookupT"
  873. }
  874. },
  875. "joinop": {
  876. "type": "operator",
  877. "nodeType": "join",
  878. "props": {
  879. "from": "demo",
  880. "joins": [
  881. {
  882. "name": "lookupT",
  883. "type": "inner",
  884. "on": "demo.deviceKind = lookupT.id"
  885. }
  886. ]
  887. }
  888. },
  889. "log": {
  890. "type": "sink",
  891. "nodeType": "log",
  892. "props": {}
  893. }
  894. },
  895. "topo": {
  896. "sources": ["demo", "lookupT"],
  897. "edges": {
  898. "demo": ["joinop"],
  899. "lookupT": ["joinop"],
  900. "joinop": ["log"]
  901. }
  902. }
  903. }`,
  904. err: fmt.Errorf("parse join joinop with map[from:demo joins:[map[name:lookupT on:demo.deviceKind = lookupT.id type:inner]]] error: join source demo is not a stream"),
  905. },
  906. {
  907. name: "stream and scan table",
  908. graph: `{
  909. "nodes": {
  910. "demo": {
  911. "type": "source",
  912. "nodeType": "mqtt",
  913. "props": {
  914. "sourceType": "stream",
  915. "sourceName": "src1"
  916. }
  917. },
  918. "lookupT":{
  919. "type": "source",
  920. "nodeType": "file",
  921. "props": {
  922. "sourceType": "table",
  923. "sourceName": "tableInPlanner"
  924. }
  925. },
  926. "joinop": {
  927. "type": "operator",
  928. "nodeType": "join",
  929. "props": {
  930. "from": "src1",
  931. "joins": [
  932. {
  933. "name": "lookupT",
  934. "type": "inner",
  935. "on": "demo.deviceKind = lookupT.id"
  936. }
  937. ]
  938. }
  939. },
  940. "log": {
  941. "type": "sink",
  942. "nodeType": "log",
  943. "props": {}
  944. }
  945. },
  946. "topo": {
  947. "sources": ["demo", "lookupT"],
  948. "edges": {
  949. "demo": ["joinop"],
  950. "lookupT": ["joinop"],
  951. "joinop": ["log"]
  952. }
  953. }
  954. }`,
  955. err: fmt.Errorf("parse join joinop with map[from:src1 joins:[map[name:lookupT on:demo.deviceKind = lookupT.id type:inner]]] error: do not support scan table [tableInPlanner] yet"),
  956. },
  957. }
  958. for _, tc := range testCases {
  959. t.Run(tc.name, func(t *testing.T) {
  960. rg := &api.RuleGraph{}
  961. err := json.Unmarshal([]byte(tc.graph), rg)
  962. if err != nil {
  963. t.Error(err)
  964. return
  965. }
  966. _, err = PlanByGraph(&api.Rule{
  967. Triggered: false,
  968. Id: "test",
  969. Graph: rg,
  970. Options: &api.RuleOption{
  971. IsEventTime: false,
  972. LateTol: 1000,
  973. Concurrency: 1,
  974. BufferLength: 1024,
  975. SendMetaToSink: false,
  976. SendError: true,
  977. Qos: api.AtMostOnce,
  978. CheckpointInterval: 300000,
  979. },
  980. })
  981. if tc.err == nil {
  982. if err != nil {
  983. t.Errorf("error mismatch:\n exp=%s\n got=%s\n\n", tc.err, err)
  984. }
  985. return
  986. }
  987. if !reflect.DeepEqual(tc.err.Error(), err.Error()) {
  988. t.Errorf("error mismatch:\n exp=%s\n got=%s\n\n", tc.err, err)
  989. }
  990. })
  991. }
  992. }