planner_graph.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  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. "errors"
  17. "fmt"
  18. "strings"
  19. "github.com/lf-edge/ekuiper/internal/binder/function"
  20. store2 "github.com/lf-edge/ekuiper/internal/pkg/store"
  21. "github.com/lf-edge/ekuiper/internal/topo"
  22. "github.com/lf-edge/ekuiper/internal/topo/graph"
  23. "github.com/lf-edge/ekuiper/internal/topo/node"
  24. "github.com/lf-edge/ekuiper/internal/topo/operator"
  25. "github.com/lf-edge/ekuiper/internal/xsql"
  26. "github.com/lf-edge/ekuiper/pkg/api"
  27. "github.com/lf-edge/ekuiper/pkg/ast"
  28. "github.com/lf-edge/ekuiper/pkg/cast"
  29. "github.com/lf-edge/ekuiper/pkg/kv"
  30. "github.com/lf-edge/ekuiper/pkg/message"
  31. )
  32. type genNodeFunc func(name string, props map[string]interface{}, options *api.RuleOption) (api.TopNode, error)
  33. var extNodes = map[string]genNodeFunc{}
  34. // PlanByGraph returns a topo.Topo object by a graph
  35. func PlanByGraph(rule *api.Rule) (*topo.Topo, error) {
  36. ruleGraph := rule.Graph
  37. if ruleGraph == nil {
  38. return nil, errors.New("no graph")
  39. }
  40. tp, err := topo.NewWithNameAndQos(rule.Id, rule.Options.Qos, rule.Options.CheckpointInterval)
  41. if err != nil {
  42. return nil, err
  43. }
  44. var (
  45. nodeMap = make(map[string]api.TopNode)
  46. sinks = make(map[string]bool)
  47. sources = make(map[string]bool)
  48. store kv.KeyValue
  49. lookupTableChildren = make(map[string]*ast.Options)
  50. scanTableEmitters []string
  51. streamEmitters = make(map[string]struct{})
  52. )
  53. for _, srcName := range ruleGraph.Topo.Sources {
  54. gn, ok := ruleGraph.Nodes[srcName]
  55. if !ok {
  56. return nil, fmt.Errorf("source node %s not defined", srcName)
  57. }
  58. if _, ok := ruleGraph.Topo.Edges[srcName]; !ok {
  59. return nil, fmt.Errorf("no edge defined for source node %s", srcName)
  60. }
  61. var srcNode *node.SourceNode
  62. srcNode, scanTableEmitters, err = parseSource(srcName, gn, rule, store, lookupTableChildren, streamEmitters)
  63. if err != nil {
  64. return nil, fmt.Errorf("parse source %s with %v error: %w", srcName, gn.Props, err)
  65. }
  66. if srcNode != nil {
  67. nodeMap[srcName] = srcNode
  68. tp.AddSrc(srcNode)
  69. }
  70. sources[srcName] = true
  71. }
  72. for nodeName, gn := range ruleGraph.Nodes {
  73. switch gn.Type {
  74. case "source": // handled above,
  75. continue
  76. case "sink":
  77. if _, ok := ruleGraph.Topo.Edges[nodeName]; ok {
  78. return nil, fmt.Errorf("sink %s has edge", nodeName)
  79. }
  80. nodeMap[nodeName] = node.NewSinkNode(nodeName, gn.NodeType, gn.Props)
  81. sinks[nodeName] = true
  82. case "operator":
  83. if _, ok := ruleGraph.Topo.Edges[nodeName]; !ok {
  84. return nil, fmt.Errorf("no edge defined for operator node %s", nodeName)
  85. }
  86. nt := strings.ToLower(gn.NodeType)
  87. switch nt {
  88. case "function":
  89. fop, err := parseFunc(gn.Props)
  90. if err != nil {
  91. return nil, fmt.Errorf("parse function %s with %v error: %w", nodeName, gn.Props, err)
  92. }
  93. op := Transform(fop, nodeName, rule.Options)
  94. nodeMap[nodeName] = op
  95. case "aggfunc":
  96. fop, err := parseFunc(gn.Props)
  97. if err != nil {
  98. return nil, fmt.Errorf("parse aggfunc %s with %v error: %w", nodeName, gn.Props, err)
  99. }
  100. fop.IsAgg = true
  101. op := Transform(fop, nodeName, rule.Options)
  102. nodeMap[nodeName] = op
  103. case "filter":
  104. fop, err := parseFilter(gn.Props)
  105. if err != nil {
  106. return nil, fmt.Errorf("parse filter %s with %v error: %w", nodeName, gn.Props, err)
  107. }
  108. op := Transform(fop, nodeName, rule.Options)
  109. nodeMap[nodeName] = op
  110. case "pick":
  111. pop, err := parsePick(gn.Props)
  112. if err != nil {
  113. return nil, fmt.Errorf("parse pick %s with %v error: %w", nodeName, gn.Props, err)
  114. }
  115. op := Transform(pop, nodeName, rule.Options)
  116. nodeMap[nodeName] = op
  117. case "window":
  118. wconf, err := parseWindow(gn.Props)
  119. if err != nil {
  120. return nil, fmt.Errorf("parse window conf %s with %v error: %w", nodeName, gn.Props, err)
  121. }
  122. op, err := node.NewWindowOp(nodeName, *wconf, ruleGraph.Topo.Sources, rule.Options)
  123. if err != nil {
  124. return nil, fmt.Errorf("parse window %s with %v error: %w", nodeName, gn.Props, err)
  125. }
  126. nodeMap[nodeName] = op
  127. case "join":
  128. stmt, err := parseJoinAst(gn.Props)
  129. if err != nil {
  130. return nil, fmt.Errorf("parse join %s with %v error: %w", nodeName, gn.Props, err)
  131. }
  132. fromNode := stmt.Sources[0].(*ast.Table)
  133. if _, ok := streamEmitters[fromNode.Name]; !ok {
  134. return nil, fmt.Errorf("parse join %s with %v error: join source %s is not a stream", nodeName, gn.Props, fromNode.Name)
  135. }
  136. hasLookup := false
  137. if stmt.Joins != nil {
  138. if len(lookupTableChildren) > 0 {
  139. var joins []ast.Join
  140. for _, join := range stmt.Joins {
  141. if hasLookup {
  142. return nil, fmt.Errorf("parse join %s with %v error: only support to join one lookup table with one stream", nodeName, gn.Props)
  143. }
  144. if streamOpt, ok := lookupTableChildren[join.Name]; ok {
  145. hasLookup = true
  146. lookupPlan := LookupPlan{
  147. joinExpr: join,
  148. options: streamOpt,
  149. }
  150. if !lookupPlan.validateAndExtractCondition() {
  151. return nil, fmt.Errorf("parse join %s with %v error: join condition %s is invalid, at least one equi-join predicate is required", nodeName, gn.Props, join.Expr)
  152. }
  153. op, err := node.NewLookupNode(lookupPlan.joinExpr.Name, lookupPlan.fields, lookupPlan.keys, lookupPlan.joinExpr.JoinType, lookupPlan.valvars, lookupPlan.options, rule.Options)
  154. if err != nil {
  155. return nil, fmt.Errorf("parse join %s with %v error: fail to create lookup node", nodeName, gn.Props)
  156. }
  157. nodeMap[nodeName] = op
  158. } else {
  159. joins = append(joins, join)
  160. }
  161. }
  162. stmt.Joins = joins
  163. }
  164. // Not all joins are lookup joins, so we need to create a join plan for the remaining joins
  165. if len(stmt.Joins) > 0 && !hasLookup {
  166. if len(scanTableEmitters) > 0 {
  167. return nil, fmt.Errorf("parse join %s with %v error: do not support scan table %s yet", nodeName, gn.Props, scanTableEmitters)
  168. }
  169. jop := &operator.JoinOp{Joins: stmt.Joins, From: fromNode}
  170. op := Transform(jop, nodeName, rule.Options)
  171. nodeMap[nodeName] = op
  172. }
  173. }
  174. case "groupby":
  175. gop, err := parseGroupBy(gn.Props)
  176. if err != nil {
  177. return nil, fmt.Errorf("parse groupby %s with %v error: %w", nodeName, gn.Props, err)
  178. }
  179. op := Transform(gop, nodeName, rule.Options)
  180. nodeMap[nodeName] = op
  181. case "orderby":
  182. oop, err := parseOrderBy(gn.Props)
  183. if err != nil {
  184. return nil, fmt.Errorf("parse orderby %s with %v error: %w", nodeName, gn.Props, err)
  185. }
  186. op := Transform(oop, nodeName, rule.Options)
  187. nodeMap[nodeName] = op
  188. case "switch":
  189. sconf, err := parseSwitch(gn.Props)
  190. if err != nil {
  191. return nil, fmt.Errorf("parse switch %s with %v error: %w", nodeName, gn.Props, err)
  192. }
  193. op, err := node.NewSwitchNode(nodeName, sconf, rule.Options)
  194. if err != nil {
  195. return nil, fmt.Errorf("create switch %s with %v error: %w", nodeName, gn.Props, err)
  196. }
  197. nodeMap[nodeName] = op
  198. default:
  199. gnf, ok := extNodes[nt]
  200. if !ok {
  201. return nil, fmt.Errorf("unknown operator type %s", gn.NodeType)
  202. }
  203. op, err := gnf(nodeName, gn.Props, rule.Options)
  204. if err != nil {
  205. return nil, err
  206. }
  207. nodeMap[nodeName] = op
  208. }
  209. default:
  210. return nil, fmt.Errorf("unknown node type %s", gn.Type)
  211. }
  212. }
  213. // validate source node
  214. for _, nodeName := range ruleGraph.Topo.Sources {
  215. if _, ok := sources[nodeName]; !ok {
  216. return nil, fmt.Errorf("source %s is not a source type node", nodeName)
  217. }
  218. }
  219. // reverse edges, value is a 2-dim array. Only switch node will have the second dim
  220. reversedEdges := make(map[string][][]string)
  221. rclone := make(map[string][]string)
  222. for fromNode, toNodes := range ruleGraph.Topo.Edges {
  223. if _, ok := ruleGraph.Nodes[fromNode]; !ok {
  224. return nil, fmt.Errorf("node %s is not defined", fromNode)
  225. }
  226. for i, toNode := range toNodes {
  227. switch tn := toNode.(type) {
  228. case string:
  229. if _, ok := ruleGraph.Nodes[tn]; !ok {
  230. return nil, fmt.Errorf("node %s is not defined", tn)
  231. }
  232. if _, ok := reversedEdges[tn]; !ok {
  233. reversedEdges[tn] = make([][]string, 1)
  234. }
  235. reversedEdges[tn][0] = append(reversedEdges[tn][0], fromNode)
  236. rclone[tn] = append(rclone[tn], fromNode)
  237. case []interface{}:
  238. for _, tni := range tn {
  239. tnn, ok := tni.(string)
  240. if !ok { // never happen
  241. return nil, fmt.Errorf("invalid edge toNode %v", toNode)
  242. }
  243. if _, ok := ruleGraph.Nodes[tnn]; !ok {
  244. return nil, fmt.Errorf("node %s is not defined", tnn)
  245. }
  246. for len(reversedEdges[tnn]) <= i {
  247. reversedEdges[tnn] = append(reversedEdges[tnn], []string{})
  248. }
  249. reversedEdges[tnn][i] = append(reversedEdges[tnn][i], fromNode)
  250. rclone[tnn] = append(rclone[tnn], fromNode)
  251. }
  252. }
  253. }
  254. }
  255. // sort the nodes by topological order
  256. nodesInOrder := make([]string, len(ruleGraph.Nodes))
  257. i := 0
  258. genNodesInOrder(ruleGraph.Topo.Sources, ruleGraph.Topo.Edges, rclone, nodesInOrder, i)
  259. // validate the typo
  260. // the map is to record the output for each node
  261. dataFlow := make(map[string]*graph.IOType)
  262. for _, n := range nodesInOrder {
  263. gn := ruleGraph.Nodes[n]
  264. if gn == nil {
  265. return nil, fmt.Errorf("can't find node %s", n)
  266. }
  267. if gn.Type == "source" {
  268. dataFlow[n] = &graph.IOType{
  269. Type: graph.IOINPUT_TYPE_ROW,
  270. RowType: graph.IOROW_TYPE_SINGLE,
  271. CollectionType: graph.IOCOLLECTION_TYPE_ANY,
  272. AllowMulti: false,
  273. }
  274. } else if gn.Type == "sink" {
  275. continue
  276. } else {
  277. nodeIO, ok := graph.OpIO[strings.ToLower(gn.NodeType)]
  278. if !ok {
  279. return nil, fmt.Errorf("can't find the io definition for node type %s", gn.NodeType)
  280. }
  281. dataInCondition := nodeIO[0]
  282. indim := reversedEdges[n]
  283. var innodes []string
  284. for _, in := range indim {
  285. innodes = append(innodes, in...)
  286. }
  287. if len(innodes) > 1 {
  288. if dataInCondition.AllowMulti {
  289. // special case for join which does not allow multiple streams
  290. if gn.NodeType == "join" {
  291. joinStreams := 0
  292. for _, innode := range innodes {
  293. if _, isLookup := lookupTableChildren[innode]; !isLookup {
  294. joinStreams++
  295. }
  296. if joinStreams > 1 {
  297. return nil, fmt.Errorf("join node %s does not allow multiple stream inputs", n)
  298. }
  299. }
  300. }
  301. for _, innode := range innodes {
  302. _, err = graph.Fit(dataFlow[innode], dataInCondition)
  303. if err != nil {
  304. return nil, fmt.Errorf("node %s output does not match node %s input: %v", innode, n, err)
  305. }
  306. }
  307. } else {
  308. return nil, fmt.Errorf("operator %s of type %s does not allow multiple inputs", n, gn.NodeType)
  309. }
  310. } else if len(innodes) == 1 {
  311. _, err := graph.Fit(dataFlow[innodes[0]], dataInCondition)
  312. if err != nil {
  313. return nil, fmt.Errorf("node %s output does not match node %s input: %v", innodes[0], n, err)
  314. }
  315. } else {
  316. return nil, fmt.Errorf("operator %s of type %s has no input", n, gn.NodeType)
  317. }
  318. out := nodeIO[1]
  319. in := dataFlow[innodes[0]]
  320. dataFlow[n] = graph.MapOut(in, out)
  321. // convert filter to having if the input is aggregated
  322. if gn.NodeType == "filter" && in.Type == graph.IOINPUT_TYPE_COLLECTION && in.CollectionType == graph.IOCOLLECTION_TYPE_GROUPED {
  323. fop, err := parseHaving(gn.Props)
  324. if err != nil {
  325. return nil, err
  326. }
  327. op := Transform(fop, n, rule.Options)
  328. nodeMap[n] = op
  329. }
  330. }
  331. }
  332. // add the linkages
  333. for nodeName, fromNodes := range reversedEdges {
  334. totalLen := 0
  335. for _, fromNode := range fromNodes {
  336. totalLen += len(fromNode)
  337. }
  338. inputs := make([]api.Emitter, 0, totalLen)
  339. for i, fromNode := range fromNodes {
  340. for _, from := range fromNode {
  341. if i == 0 {
  342. if src, ok := nodeMap[from].(api.Emitter); ok {
  343. inputs = append(inputs, src)
  344. }
  345. } else {
  346. switch sn := nodeMap[from].(type) {
  347. case *node.SwitchNode:
  348. inputs = append(inputs, sn.GetEmitter(i))
  349. default:
  350. return nil, fmt.Errorf("node %s is not a switch node but have multiple output", from)
  351. }
  352. }
  353. }
  354. }
  355. n := nodeMap[nodeName]
  356. if n == nil {
  357. return nil, fmt.Errorf("node %s is not defined", nodeName)
  358. }
  359. if _, ok := sinks[nodeName]; ok {
  360. tp.AddSink(inputs, n.(*node.SinkNode))
  361. } else {
  362. tp.AddOperator(inputs, n.(node.OperatorNode))
  363. }
  364. }
  365. return tp, nil
  366. }
  367. func genNodesInOrder(toNodes []string, edges map[string][]interface{}, flatReversedEdges map[string][]string, nodesInOrder []string, i int) int {
  368. for _, src := range toNodes {
  369. if len(flatReversedEdges[src]) > 1 {
  370. flatReversedEdges[src] = flatReversedEdges[src][1:]
  371. continue
  372. }
  373. nodesInOrder[i] = src
  374. i++
  375. tns := make([]string, 0, len(edges[src]))
  376. for _, toNode := range edges[src] {
  377. switch toNode.(type) {
  378. case string:
  379. tns = append(tns, toNode.(string))
  380. case []interface{}:
  381. for _, tni := range toNode.([]interface{}) {
  382. tns = append(tns, tni.(string))
  383. }
  384. }
  385. }
  386. i = genNodesInOrder(tns, edges, flatReversedEdges, nodesInOrder, i)
  387. }
  388. return i
  389. }
  390. func parseSource(nodeName string, gn *api.GraphNode, rule *api.Rule, store kv.KeyValue, lookupTableChildren map[string]*ast.Options, streamEmitters map[string]struct{}) (*node.SourceNode, []string, error) {
  391. scanTableEmitters := make([]string, 0)
  392. sourceMeta := &api.SourceMeta{
  393. SourceType: "stream",
  394. }
  395. err := cast.MapToStruct(gn.Props, sourceMeta)
  396. if err != nil {
  397. return nil, scanTableEmitters, err
  398. }
  399. if sourceMeta.SourceType != "stream" && sourceMeta.SourceType != "table" {
  400. return nil, scanTableEmitters, fmt.Errorf("source type %s not supported", sourceMeta.SourceType)
  401. }
  402. // If source name is specified, find the created stream/table from store
  403. if sourceMeta.SourceName != "" {
  404. if store == nil {
  405. store, err = store2.GetKV("stream")
  406. if err != nil {
  407. return nil, scanTableEmitters, err
  408. }
  409. }
  410. streamStmt, e := xsql.GetDataSource(store, sourceMeta.SourceName)
  411. if e != nil {
  412. return nil, scanTableEmitters, fmt.Errorf("fail to get stream %s, please check if stream is created", sourceMeta.SourceName)
  413. }
  414. if streamStmt.StreamType == ast.TypeStream && sourceMeta.SourceType == "table" {
  415. return nil, scanTableEmitters, fmt.Errorf("stream %s is not a table", sourceMeta.SourceName)
  416. } else if streamStmt.StreamType == ast.TypeTable && sourceMeta.SourceType == "stream" {
  417. return nil, scanTableEmitters, fmt.Errorf("table %s is not a stream", sourceMeta.SourceName)
  418. }
  419. st := streamStmt.Options.TYPE
  420. if st == "" {
  421. st = "mqtt"
  422. }
  423. if st != gn.NodeType {
  424. return nil, scanTableEmitters, fmt.Errorf("source type %s does not match the stream type %s", gn.NodeType, st)
  425. }
  426. sInfo, err := convertStreamInfo(streamStmt)
  427. if err != nil {
  428. return nil, scanTableEmitters, err
  429. }
  430. if sInfo.stmt.StreamType == ast.TypeTable && sInfo.stmt.Options.KIND == ast.StreamKindLookup {
  431. lookupTableChildren[string(sInfo.stmt.Name)] = sInfo.stmt.Options
  432. return nil, scanTableEmitters, nil
  433. } else {
  434. // Use the plan to calculate the schema and other meta info
  435. p := DataSourcePlan{
  436. name: sInfo.stmt.Name,
  437. streamStmt: sInfo.stmt,
  438. streamFields: sInfo.schema.ToJsonSchema(),
  439. isSchemaless: sInfo.schema == nil,
  440. iet: rule.Options.IsEventTime,
  441. allMeta: rule.Options.SendMetaToSink,
  442. }.Init()
  443. if sInfo.stmt.StreamType == ast.TypeStream {
  444. err = p.PruneColumns(nil)
  445. if err != nil {
  446. return nil, scanTableEmitters, err
  447. }
  448. srcNode, e := transformSourceNode(p, nil, rule.Options)
  449. if e != nil {
  450. return nil, scanTableEmitters, e
  451. }
  452. streamEmitters[string(sInfo.stmt.Name)] = struct{}{}
  453. return srcNode, scanTableEmitters, nil
  454. } else {
  455. scanTableEmitters = append(scanTableEmitters, string(sInfo.stmt.Name))
  456. return nil, scanTableEmitters, nil
  457. }
  458. }
  459. } else {
  460. sourceOption := &ast.Options{}
  461. err = cast.MapToStruct(gn.Props, sourceOption)
  462. if err != nil {
  463. return nil, scanTableEmitters, err
  464. }
  465. sourceOption.TYPE = gn.NodeType
  466. switch sourceMeta.SourceType {
  467. case "stream":
  468. pp, err := operator.NewPreprocessor(true, nil, true, nil, rule.Options.IsEventTime, sourceOption.TIMESTAMP, sourceOption.TIMESTAMP_FORMAT, strings.EqualFold(sourceOption.FORMAT, message.FormatBinary), sourceOption.STRICT_VALIDATION)
  469. if err != nil {
  470. return nil, scanTableEmitters, err
  471. }
  472. srcNode := node.NewSourceNode(nodeName, ast.TypeStream, pp, sourceOption, rule.Options.SendError)
  473. streamEmitters[nodeName] = struct{}{}
  474. return srcNode, scanTableEmitters, nil
  475. case "table":
  476. return nil, scanTableEmitters, fmt.Errorf("anonymouse table source is not supported, please create it prior to the rule")
  477. }
  478. }
  479. return nil, scanTableEmitters, errors.New("invalid source node")
  480. }
  481. func parseOrderBy(props map[string]interface{}) (*operator.OrderOp, error) {
  482. n := &graph.Orderby{}
  483. err := cast.MapToStruct(props, n)
  484. if err != nil {
  485. return nil, err
  486. }
  487. stmt := "SELECT * FROM unknown ORDER BY"
  488. for _, s := range n.Sorts {
  489. stmt += " " + s.Field + " "
  490. if s.Desc {
  491. stmt += "DESC"
  492. }
  493. }
  494. p, err := xsql.NewParser(strings.NewReader(stmt)).Parse()
  495. if err != nil {
  496. return nil, fmt.Errorf("invalid order by statement error: %v", err)
  497. }
  498. if len(p.SortFields) == 0 {
  499. return nil, fmt.Errorf("order by statement is empty")
  500. }
  501. return &operator.OrderOp{
  502. SortFields: p.SortFields,
  503. }, nil
  504. }
  505. func parseGroupBy(props map[string]interface{}) (*operator.AggregateOp, error) {
  506. n := &graph.Groupby{}
  507. err := cast.MapToStruct(props, n)
  508. if err != nil {
  509. return nil, err
  510. }
  511. if len(n.Dimensions) == 0 {
  512. return nil, fmt.Errorf("groupby must have at least one dimension")
  513. }
  514. stmt := "SELECT * FROM unknown Group By " + strings.Join(n.Dimensions, ",")
  515. p, err := xsql.NewParser(strings.NewReader(stmt)).Parse()
  516. if err != nil {
  517. return nil, fmt.Errorf("invalid join statement error: %v", err)
  518. }
  519. return &operator.AggregateOp{Dimensions: p.Dimensions}, nil
  520. }
  521. func parseJoinAst(props map[string]interface{}) (*ast.SelectStatement, error) {
  522. n := &graph.Join{}
  523. err := cast.MapToStruct(props, n)
  524. if err != nil {
  525. return nil, err
  526. }
  527. stmt := "SELECT * FROM " + n.From
  528. for _, join := range n.Joins {
  529. stmt += " " + join.Type + " JOIN " + join.Name + " ON " + join.On
  530. }
  531. return xsql.NewParser(strings.NewReader(stmt)).Parse()
  532. }
  533. func parseWindow(props map[string]interface{}) (*node.WindowConfig, error) {
  534. n := &graph.Window{}
  535. err := cast.MapToStruct(props, n)
  536. if err != nil {
  537. return nil, err
  538. }
  539. if n.Size <= 0 {
  540. return nil, fmt.Errorf("window size %d is invalid", n.Size)
  541. }
  542. var (
  543. wt ast.WindowType
  544. length int
  545. interval int
  546. )
  547. switch strings.ToLower(n.Type) {
  548. case "tumblingwindow":
  549. wt = ast.TUMBLING_WINDOW
  550. if n.Interval != 0 && n.Interval != n.Size {
  551. return nil, fmt.Errorf("tumbling window interval must equal to size")
  552. }
  553. case "hoppingwindow":
  554. wt = ast.HOPPING_WINDOW
  555. if n.Interval <= 0 {
  556. return nil, fmt.Errorf("hopping window interval must be greater than 0")
  557. }
  558. if n.Interval > n.Size {
  559. return nil, fmt.Errorf("hopping window interval must be less than size")
  560. }
  561. case "sessionwindow":
  562. wt = ast.SESSION_WINDOW
  563. if n.Interval <= 0 {
  564. return nil, fmt.Errorf("hopping window interval must be greater than 0")
  565. }
  566. case "slidingwindow":
  567. wt = ast.SLIDING_WINDOW
  568. if n.Interval != 0 && n.Interval != n.Size {
  569. return nil, fmt.Errorf("tumbling window interval must equal to size")
  570. }
  571. case "countwindow":
  572. wt = ast.COUNT_WINDOW
  573. if n.Interval < 0 {
  574. return nil, fmt.Errorf("count window interval must be greater or equal to 0")
  575. }
  576. if n.Interval > n.Size {
  577. return nil, fmt.Errorf("count window interval must be less than size")
  578. }
  579. if n.Interval == 0 {
  580. n.Interval = n.Size
  581. }
  582. default:
  583. return nil, fmt.Errorf("unknown window type %s", n.Type)
  584. }
  585. if wt == ast.COUNT_WINDOW {
  586. length = n.Size
  587. interval = n.Interval
  588. } else {
  589. unit := 1
  590. switch strings.ToLower(n.Unit) {
  591. case "dd":
  592. unit = 24 * 3600 * 1000
  593. case "hh":
  594. unit = 3600 * 1000
  595. case "mi":
  596. unit = 60 * 1000
  597. case "ss":
  598. unit = 1000
  599. case "ms":
  600. unit = 1
  601. default:
  602. return nil, fmt.Errorf("Invalid unit %s", n.Unit)
  603. }
  604. length = n.Size * unit
  605. interval = n.Interval * unit
  606. }
  607. return &node.WindowConfig{
  608. Type: wt,
  609. Length: length,
  610. Interval: interval,
  611. }, nil
  612. }
  613. func parsePick(props map[string]interface{}) (*operator.ProjectOp, error) {
  614. n := &graph.Select{}
  615. err := cast.MapToStruct(props, n)
  616. if err != nil {
  617. return nil, err
  618. }
  619. stmt, err := xsql.NewParser(strings.NewReader("select " + strings.Join(n.Fields, ",") + " from nonexist")).Parse()
  620. if err != nil {
  621. return nil, err
  622. }
  623. t := ProjectPlan{
  624. fields: stmt.Fields,
  625. isAggregate: xsql.IsAggStatement(stmt),
  626. }.Init()
  627. return &operator.ProjectOp{ColNames: t.colNames, AliasNames: t.aliasNames, AliasFields: t.aliasFields, ExprFields: t.exprFields, IsAggregate: t.isAggregate, AllWildcard: t.allWildcard, WildcardEmitters: t.wildcardEmitters, ExprNames: t.exprNames, SendMeta: t.sendMeta}, nil
  628. }
  629. func parseFunc(props map[string]interface{}) (*operator.FuncOp, error) {
  630. m, ok := props["expr"]
  631. if !ok {
  632. return nil, errors.New("no expr")
  633. }
  634. funcExpr, ok := m.(string)
  635. if !ok {
  636. return nil, fmt.Errorf("expr %v is not string", m)
  637. }
  638. stmt, err := xsql.NewParser(strings.NewReader("select " + funcExpr + " from nonexist")).Parse()
  639. if err != nil {
  640. return nil, err
  641. }
  642. f := stmt.Fields[0]
  643. c, ok := f.Expr.(*ast.Call)
  644. if !ok {
  645. // never happen
  646. return nil, fmt.Errorf("expr %s is not ast.Call", funcExpr)
  647. }
  648. var name string
  649. if f.AName != "" {
  650. name = f.AName
  651. } else {
  652. name = f.Name
  653. }
  654. return &operator.FuncOp{CallExpr: c, Name: name, IsAgg: function.IsAggFunc(name)}, nil
  655. }
  656. func parseFilter(props map[string]interface{}) (*operator.FilterOp, error) {
  657. m, ok := props["expr"]
  658. if !ok {
  659. return nil, errors.New("no expr")
  660. }
  661. conditionExpr, ok := m.(string)
  662. if !ok {
  663. return nil, fmt.Errorf("expr %v is not string", m)
  664. }
  665. p := xsql.NewParser(strings.NewReader("where " + conditionExpr))
  666. if exp, err := p.ParseCondition(); err != nil {
  667. return nil, err
  668. } else {
  669. if exp != nil {
  670. return &operator.FilterOp{Condition: exp}, nil
  671. }
  672. }
  673. return nil, fmt.Errorf("expr %v is not a condition", m)
  674. }
  675. func parseHaving(props map[string]interface{}) (*operator.HavingOp, error) {
  676. m, ok := props["expr"]
  677. if !ok {
  678. return nil, errors.New("no expr")
  679. }
  680. conditionExpr, ok := m.(string)
  681. if !ok {
  682. return nil, fmt.Errorf("expr %v is not string", m)
  683. }
  684. p := xsql.NewParser(strings.NewReader("where " + conditionExpr))
  685. if exp, err := p.ParseCondition(); err != nil {
  686. return nil, err
  687. } else {
  688. if exp != nil {
  689. return &operator.HavingOp{Condition: exp}, nil
  690. }
  691. }
  692. return nil, fmt.Errorf("expr %v is not a condition", m)
  693. }
  694. func parseSwitch(props map[string]interface{}) (*node.SwitchConfig, error) {
  695. n := &graph.Switch{}
  696. err := cast.MapToStruct(props, n)
  697. if err != nil {
  698. return nil, err
  699. }
  700. if len(n.Cases) == 0 {
  701. return nil, fmt.Errorf("switch node must have at least one case")
  702. }
  703. caseExprs := make([]ast.Expr, len(n.Cases))
  704. for i, c := range n.Cases {
  705. p := xsql.NewParser(strings.NewReader("where " + c))
  706. if exp, err := p.ParseCondition(); err != nil {
  707. return nil, fmt.Errorf("parse case %d error: %v", i, err)
  708. } else {
  709. if exp != nil {
  710. caseExprs[i] = exp
  711. }
  712. }
  713. }
  714. return &node.SwitchConfig{
  715. Cases: caseExprs,
  716. StopAtFirstMatch: n.StopAtFirstMatch,
  717. }, nil
  718. }