planner_graph_test.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  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. t.Logf("The test bucket size is %d.\n\n", len(tests))
  584. for i, tt := range tests {
  585. rg := &api.RuleGraph{}
  586. err := json.Unmarshal([]byte(tt.graph), rg)
  587. if err != nil {
  588. t.Error(err)
  589. continue
  590. }
  591. _, err = PlanByGraph(&api.Rule{
  592. Triggered: false,
  593. Id: fmt.Sprintf("rule%d", i),
  594. Name: fmt.Sprintf("rule%d", i),
  595. Graph: rg,
  596. Options: &api.RuleOption{
  597. IsEventTime: false,
  598. LateTol: 1000,
  599. Concurrency: 1,
  600. BufferLength: 1024,
  601. SendMetaToSink: false,
  602. SendError: true,
  603. Qos: api.AtMostOnce,
  604. CheckpointInterval: 300000,
  605. },
  606. })
  607. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  608. t.Errorf("%d: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.err, err)
  609. }
  610. }
  611. }
  612. func TestPlannerGraphWithStream(t *testing.T) {
  613. store, err := store.GetKV("stream")
  614. if err != nil {
  615. t.Error(err)
  616. return
  617. }
  618. streamSqls := map[string]string{
  619. "src1": `CREATE STREAM src1 (
  620. id1 BIGINT,
  621. temp BIGINT,
  622. name string,
  623. myarray array(string)
  624. ) WITH (DATASOURCE="src1", FORMAT="json", KEY="ts");`,
  625. "src2": `CREATE STREAM src2 (
  626. id2 BIGINT,
  627. hum BIGINT
  628. ) WITH (DATASOURCE="src2", FORMAT="json", KEY="ts", TIMESTAMP_FORMAT="YYYY-MM-dd HH:mm:ss");`,
  629. "tableInPlanner": `CREATE TABLE tableInPlanner (
  630. id BIGINT,
  631. name STRING,
  632. value STRING,
  633. hum BIGINT
  634. ) WITH (TYPE="file");`,
  635. "lookupT": `CREATE TABLE lookupT () WITH (DATASOURCE="alertVal", TYPE="memory", KIND="lookup", KEY="id");`,
  636. }
  637. types := map[string]ast.StreamType{
  638. "src1": ast.TypeStream,
  639. "src2": ast.TypeStream,
  640. "tableInPlanner": ast.TypeTable,
  641. "lookupT": ast.TypeTable,
  642. }
  643. for name, sql := range streamSqls {
  644. s, err := json.Marshal(&xsql.StreamInfo{
  645. StreamType: types[name],
  646. Statement: sql,
  647. })
  648. if err != nil {
  649. t.Error(err)
  650. t.Fail()
  651. }
  652. err = store.Set(name, string(s))
  653. if err != nil {
  654. t.Error(err)
  655. t.Fail()
  656. }
  657. }
  658. testCases := []struct {
  659. name string
  660. graph string
  661. err error
  662. }{
  663. {
  664. name: "test stream",
  665. graph: `{
  666. "nodes": {
  667. "demo": {
  668. "type": "source",
  669. "nodeType": "mqtt",
  670. "props": {
  671. "sourceType": "stream",
  672. "sourceName": "src1"
  673. }
  674. },
  675. "log": {
  676. "type": "sink",
  677. "nodeType": "log",
  678. "props": {}
  679. }
  680. },
  681. "topo": {
  682. "sources": ["demo"],
  683. "edges": {
  684. "demo": ["log"]
  685. }
  686. }
  687. }`,
  688. err: nil,
  689. },
  690. {
  691. name: "stream type wrong",
  692. graph: `{
  693. "nodes": {
  694. "demo": {
  695. "type": "source",
  696. "nodeType": "file",
  697. "props": {
  698. "sourceType": "stream",
  699. "sourceName": "src1"
  700. }
  701. },
  702. "log": {
  703. "type": "sink",
  704. "nodeType": "log",
  705. "props": {}
  706. }
  707. },
  708. "topo": {
  709. "sources": ["demo"],
  710. "edges": {
  711. "demo": ["log"]
  712. }
  713. }
  714. }`,
  715. err: fmt.Errorf("parse source demo with map[sourceName:src1 sourceType:stream] error: source type file does not match the stream type mqtt"),
  716. },
  717. {
  718. name: "non exist stream",
  719. graph: `{
  720. "nodes": {
  721. "demo": {
  722. "type": "source",
  723. "nodeType": "mqtt",
  724. "props": {
  725. "sourceType": "stream",
  726. "sourceName": "unknown"
  727. }
  728. },
  729. "log": {
  730. "type": "sink",
  731. "nodeType": "log",
  732. "props": {}
  733. }
  734. },
  735. "topo": {
  736. "sources": ["demo"],
  737. "edges": {
  738. "demo": ["log"]
  739. }
  740. }
  741. }`,
  742. err: fmt.Errorf("parse source demo with map[sourceName:unknown sourceType:stream] error: fail to get stream unknown, please check if stream is created"),
  743. },
  744. {
  745. name: "wrong source type",
  746. graph: `{
  747. "nodes": {
  748. "demo": {
  749. "type": "source",
  750. "nodeType": "mqtt",
  751. "props": {
  752. "sourceType": "stream",
  753. "sourceName": "tableInPlanner"
  754. }
  755. },
  756. "log": {
  757. "type": "sink",
  758. "nodeType": "log",
  759. "props": {}
  760. }
  761. },
  762. "topo": {
  763. "sources": ["demo"],
  764. "edges": {
  765. "demo": ["log"]
  766. }
  767. }
  768. }`,
  769. err: fmt.Errorf("parse source demo with map[sourceName:tableInPlanner sourceType:stream] error: table tableInPlanner is not a stream"),
  770. },
  771. {
  772. name: "stream and table",
  773. graph: `{
  774. "nodes": {
  775. "demo": {
  776. "type": "source",
  777. "nodeType": "mqtt",
  778. "props": {
  779. "sourceType": "stream",
  780. "sourceName": "src1"
  781. }
  782. },
  783. "lookupT":{
  784. "type": "source",
  785. "nodeType": "memory",
  786. "props": {
  787. "sourceType": "table",
  788. "sourceName": "lookupT"
  789. }
  790. },
  791. "joinop": {
  792. "type": "operator",
  793. "nodeType": "join",
  794. "props": {
  795. "from": "src1",
  796. "joins": [
  797. {
  798. "name": "lookupT",
  799. "type": "inner",
  800. "on": "src1.deviceKind = lookupT.id"
  801. }
  802. ]
  803. }
  804. },
  805. "log": {
  806. "type": "sink",
  807. "nodeType": "log",
  808. "props": {}
  809. }
  810. },
  811. "topo": {
  812. "sources": ["demo", "lookupT"],
  813. "edges": {
  814. "demo": ["joinop"],
  815. "lookupT": ["joinop"],
  816. "joinop": ["log"]
  817. }
  818. }
  819. }`,
  820. err: nil,
  821. },
  822. {
  823. name: "wrong join stream name",
  824. graph: `{
  825. "nodes": {
  826. "demo": {
  827. "type": "source",
  828. "nodeType": "mqtt",
  829. "props": {
  830. "sourceType": "stream",
  831. "sourceName": "src1"
  832. }
  833. },
  834. "lookupT":{
  835. "type": "source",
  836. "nodeType": "memory",
  837. "props": {
  838. "sourceType": "table",
  839. "sourceName": "lookupT"
  840. }
  841. },
  842. "joinop": {
  843. "type": "operator",
  844. "nodeType": "join",
  845. "props": {
  846. "from": "demo",
  847. "joins": [
  848. {
  849. "name": "lookupT",
  850. "type": "inner",
  851. "on": "demo.deviceKind = lookupT.id"
  852. }
  853. ]
  854. }
  855. },
  856. "log": {
  857. "type": "sink",
  858. "nodeType": "log",
  859. "props": {}
  860. }
  861. },
  862. "topo": {
  863. "sources": ["demo", "lookupT"],
  864. "edges": {
  865. "demo": ["joinop"],
  866. "lookupT": ["joinop"],
  867. "joinop": ["log"]
  868. }
  869. }
  870. }`,
  871. 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"),
  872. },
  873. {
  874. name: "stream and scan table",
  875. graph: `{
  876. "nodes": {
  877. "demo": {
  878. "type": "source",
  879. "nodeType": "mqtt",
  880. "props": {
  881. "sourceType": "stream",
  882. "sourceName": "src1"
  883. }
  884. },
  885. "lookupT":{
  886. "type": "source",
  887. "nodeType": "file",
  888. "props": {
  889. "sourceType": "table",
  890. "sourceName": "tableInPlanner"
  891. }
  892. },
  893. "joinop": {
  894. "type": "operator",
  895. "nodeType": "join",
  896. "props": {
  897. "from": "src1",
  898. "joins": [
  899. {
  900. "name": "lookupT",
  901. "type": "inner",
  902. "on": "demo.deviceKind = lookupT.id"
  903. }
  904. ]
  905. }
  906. },
  907. "log": {
  908. "type": "sink",
  909. "nodeType": "log",
  910. "props": {}
  911. }
  912. },
  913. "topo": {
  914. "sources": ["demo", "lookupT"],
  915. "edges": {
  916. "demo": ["joinop"],
  917. "lookupT": ["joinop"],
  918. "joinop": ["log"]
  919. }
  920. }
  921. }`,
  922. 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"),
  923. },
  924. }
  925. for _, tc := range testCases {
  926. t.Run(tc.name, func(t *testing.T) {
  927. rg := &api.RuleGraph{}
  928. err := json.Unmarshal([]byte(tc.graph), rg)
  929. if err != nil {
  930. t.Error(err)
  931. return
  932. }
  933. _, err = PlanByGraph(&api.Rule{
  934. Triggered: false,
  935. Id: "test",
  936. Graph: rg,
  937. Options: &api.RuleOption{
  938. IsEventTime: false,
  939. LateTol: 1000,
  940. Concurrency: 1,
  941. BufferLength: 1024,
  942. SendMetaToSink: false,
  943. SendError: true,
  944. Qos: api.AtMostOnce,
  945. CheckpointInterval: 300000,
  946. },
  947. })
  948. if tc.err == nil {
  949. if err != nil {
  950. t.Errorf("error mismatch:\n exp=%s\n got=%s\n\n", tc.err, err)
  951. }
  952. return
  953. }
  954. if !reflect.DeepEqual(tc.err.Error(), err.Error()) {
  955. t.Errorf("error mismatch:\n exp=%s\n got=%s\n\n", tc.err, err)
  956. }
  957. })
  958. }
  959. }