xsql_processor.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. package processors
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/emqx/kuiper/common"
  7. "github.com/emqx/kuiper/xsql"
  8. "github.com/emqx/kuiper/xsql/plans"
  9. "github.com/emqx/kuiper/xstream"
  10. "github.com/emqx/kuiper/xstream/api"
  11. "github.com/emqx/kuiper/xstream/nodes"
  12. "github.com/emqx/kuiper/xstream/operators"
  13. "path"
  14. "strings"
  15. )
  16. var log = common.Log
  17. type StreamProcessor struct {
  18. dbDir string
  19. }
  20. //@params d : the directory of the DB to save the stream info
  21. func NewStreamProcessor(d string) *StreamProcessor {
  22. processor := &StreamProcessor{
  23. dbDir: d,
  24. }
  25. return processor
  26. }
  27. func (p *StreamProcessor) ExecStmt(statement string) (result []string, err error) {
  28. parser := xsql.NewParser(strings.NewReader(statement))
  29. stmt, err := xsql.Language.Parse(parser)
  30. if err != nil {
  31. return
  32. }
  33. store := common.GetSimpleKVStore(p.dbDir)
  34. err = store.Open()
  35. if err != nil {
  36. return
  37. }
  38. defer store.Close()
  39. switch s := stmt.(type) {
  40. case *xsql.StreamStmt:
  41. var r string
  42. r, err = p.execCreateStream(s, store, statement)
  43. result = append(result, r)
  44. case *xsql.ShowStreamsStatement:
  45. result, err = p.execShowStream(s, store)
  46. case *xsql.DescribeStreamStatement:
  47. var r string
  48. r, err = p.execDescribeStream(s, store)
  49. result = append(result, r)
  50. case *xsql.ExplainStreamStatement:
  51. var r string
  52. r, err = p.execExplainStream(s, store)
  53. result = append(result, r)
  54. case *xsql.DropStreamStatement:
  55. var r string
  56. r, err = p.execDropStream(s, store)
  57. result = append(result, r)
  58. }
  59. return
  60. }
  61. func (p *StreamProcessor) execCreateStream(stmt *xsql.StreamStmt, db common.KeyValue, statement string) (string, error) {
  62. err := db.Set(string(stmt.Name), statement)
  63. if err != nil {
  64. return "", fmt.Errorf("Create stream fails: %v.", err)
  65. } else {
  66. return fmt.Sprintf("Stream %s is created.", stmt.Name), nil
  67. }
  68. }
  69. func (p *StreamProcessor) execShowStream(stmt *xsql.ShowStreamsStatement, db common.KeyValue) ([]string, error) {
  70. keys, err := db.Keys()
  71. if len(keys) == 0 {
  72. keys = append(keys, "No stream definitions are found.")
  73. }
  74. return keys, err
  75. }
  76. func (p *StreamProcessor) execDescribeStream(stmt *xsql.DescribeStreamStatement, db common.KeyValue) (string, error) {
  77. s, f := db.Get(stmt.Name)
  78. s1, _ := s.(string)
  79. if !f {
  80. return "", fmt.Errorf("Stream %s is not found.", stmt.Name)
  81. }
  82. parser := xsql.NewParser(strings.NewReader(s1))
  83. stream, err := xsql.Language.Parse(parser)
  84. streamStmt, ok := stream.(*xsql.StreamStmt)
  85. if !ok {
  86. return "", fmt.Errorf("Error resolving the stream %s, the data in db may be corrupted.", stmt.Name)
  87. }
  88. var buff bytes.Buffer
  89. buff.WriteString("Fields\n--------------------------------------------------------------------------------\n")
  90. for _, f := range streamStmt.StreamFields {
  91. buff.WriteString(f.Name + "\t")
  92. xsql.PrintFieldType(f.FieldType, &buff)
  93. buff.WriteString("\n")
  94. }
  95. buff.WriteString("\n")
  96. common.PrintMap(streamStmt.Options, &buff)
  97. return buff.String(), err
  98. }
  99. func (p *StreamProcessor) execExplainStream(stmt *xsql.ExplainStreamStatement, db common.KeyValue) (string, error) {
  100. _, f := db.Get(stmt.Name)
  101. if !f {
  102. return "", fmt.Errorf("Stream %s is not found.", stmt.Name)
  103. }
  104. return "TO BE SUPPORTED", nil
  105. }
  106. func (p *StreamProcessor) execDropStream(stmt *xsql.DropStreamStatement, db common.KeyValue) (string, error) {
  107. err := db.Delete(stmt.Name)
  108. if err != nil {
  109. return "", fmt.Errorf("Drop stream fails: %v.", err)
  110. } else {
  111. return fmt.Sprintf("Stream %s is dropped.", stmt.Name), nil
  112. }
  113. }
  114. func GetStream(m *common.SimpleKVStore, name string) (stmt *xsql.StreamStmt, err error) {
  115. s, f := m.Get(name)
  116. if !f {
  117. return nil, fmt.Errorf("Cannot find key %s. ", name)
  118. }
  119. s1, _ := s.(string)
  120. parser := xsql.NewParser(strings.NewReader(s1))
  121. stream, err := xsql.Language.Parse(parser)
  122. stmt, ok := stream.(*xsql.StreamStmt)
  123. if !ok {
  124. err = fmt.Errorf("Error resolving the stream %s, the data in db may be corrupted.", name)
  125. }
  126. return
  127. }
  128. type RuleProcessor struct {
  129. dbDir string
  130. }
  131. func NewRuleProcessor(d string) *RuleProcessor {
  132. processor := &RuleProcessor{
  133. dbDir: d,
  134. }
  135. return processor
  136. }
  137. func (p *RuleProcessor) ExecCreate(name, ruleJson string) (*api.Rule, error) {
  138. rule, err := p.getRuleByJson(name, ruleJson)
  139. if err != nil {
  140. return nil, err
  141. }
  142. store := common.GetSimpleKVStore(path.Join(p.dbDir, "rule"))
  143. err = store.Open()
  144. if err != nil {
  145. return nil, err
  146. }
  147. err = store.Set(string(name), ruleJson)
  148. defer store.Close()
  149. if err != nil {
  150. return nil, err
  151. } else {
  152. log.Infof("Rule %s is created.", name)
  153. }
  154. return rule, nil
  155. }
  156. func (p *RuleProcessor) GetRuleByName(name string) (*api.Rule, error) {
  157. store := common.GetSimpleKVStore(path.Join(p.dbDir, "rule"))
  158. err := store.Open()
  159. if err != nil {
  160. return nil, err
  161. }
  162. defer store.Close()
  163. s, f := store.Get(string(name))
  164. if !f {
  165. return nil, fmt.Errorf("Rule %s is not found.", name)
  166. }
  167. s1, _ := s.(string)
  168. return p.getRuleByJson(name, s1)
  169. }
  170. func (p *RuleProcessor) getRuleByJson(name, ruleJson string) (*api.Rule, error) {
  171. var rule api.Rule
  172. if err := json.Unmarshal([]byte(ruleJson), &rule); err != nil {
  173. return nil, fmt.Errorf("Parse rule %s error : %s.", ruleJson, err)
  174. }
  175. rule.Id = name
  176. //validation
  177. if name == "" {
  178. return nil, fmt.Errorf("Missing rule id.")
  179. }
  180. if rule.Sql == "" {
  181. return nil, fmt.Errorf("Missing rule SQL.")
  182. }
  183. if rule.Actions == nil || len(rule.Actions) == 0 {
  184. return nil, fmt.Errorf("Missing rule actions.")
  185. }
  186. return &rule, nil
  187. }
  188. func (p *RuleProcessor) ExecInitRule(rule *api.Rule) (*xstream.TopologyNew, error) {
  189. if tp, inputs, err := p.createTopo(rule); err != nil {
  190. return nil, err
  191. } else {
  192. for _, m := range rule.Actions {
  193. for name, action := range m {
  194. props, ok := action.(map[string]interface{})
  195. if !ok {
  196. return nil, fmt.Errorf("expect map[string]interface{} type for the action properties, but found %v", action)
  197. }
  198. tp.AddSink(inputs, nodes.NewSinkNode("sink_"+name, name, props))
  199. }
  200. }
  201. return tp, nil
  202. }
  203. }
  204. func (p *RuleProcessor) ExecQuery(ruleid, sql string) (*xstream.TopologyNew, error) {
  205. if tp, inputs, err := p.createTopo(&api.Rule{Id: ruleid, Sql: sql}); err != nil {
  206. return nil, err
  207. } else {
  208. tp.AddSink(inputs, nodes.NewSinkNode("sink_memory_log", "logToMemory", nil))
  209. go func() {
  210. select {
  211. case err := <-tp.Open():
  212. log.Infof("closing query for error: %v", err)
  213. tp.GetContext().SetError(err)
  214. tp.Cancel()
  215. }
  216. }()
  217. return tp, nil
  218. }
  219. }
  220. func (p *RuleProcessor) ExecDesc(name string) (string, error) {
  221. store := common.GetSimpleKVStore(path.Join(p.dbDir, "rule"))
  222. err := store.Open()
  223. if err != nil {
  224. return "", err
  225. }
  226. defer store.Close()
  227. s, f := store.Get(string(name))
  228. if !f {
  229. return "", fmt.Errorf("Rule %s is not found.", name)
  230. }
  231. s1, _ := s.(string)
  232. dst := &bytes.Buffer{}
  233. if err := json.Indent(dst, []byte(s1), "", " "); err != nil {
  234. return "", err
  235. }
  236. return fmt.Sprintln(dst.String()), nil
  237. }
  238. func (p *RuleProcessor) ExecShow() (string, error) {
  239. keys, err := p.GetAllRules()
  240. if err != nil {
  241. return "", err
  242. }
  243. if len(keys) == 0 {
  244. keys = append(keys, "No rule definitions are found.")
  245. }
  246. var result string
  247. for _, c := range keys {
  248. result = result + fmt.Sprintln(c)
  249. }
  250. return result, nil
  251. }
  252. func (p *RuleProcessor) GetAllRules() ([]string, error) {
  253. store := common.GetSimpleKVStore(path.Join(p.dbDir, "rule"))
  254. err := store.Open()
  255. if err != nil {
  256. return nil, err
  257. }
  258. defer store.Close()
  259. return store.Keys()
  260. }
  261. func (p *RuleProcessor) ExecDrop(name string) (string, error) {
  262. store := common.GetSimpleKVStore(path.Join(p.dbDir, "rule"))
  263. err := store.Open()
  264. if err != nil {
  265. return "", err
  266. }
  267. defer store.Close()
  268. err = store.Delete(string(name))
  269. if err != nil {
  270. return "", err
  271. } else {
  272. return fmt.Sprintf("Rule %s is dropped.", name), nil
  273. }
  274. }
  275. func (p *RuleProcessor) createTopo(rule *api.Rule) (*xstream.TopologyNew, []api.Emitter, error) {
  276. return p.createTopoWithSources(rule, nil)
  277. }
  278. //For test to mock source
  279. func (p *RuleProcessor) createTopoWithSources(rule *api.Rule, sources []*nodes.SourceNode) (*xstream.TopologyNew, []api.Emitter, error) {
  280. name := rule.Id
  281. sql := rule.Sql
  282. var (
  283. isEventTime bool
  284. lateTol int64
  285. concurrency = 1
  286. bufferLength = 1024
  287. )
  288. if iet, ok := rule.Options["isEventTime"]; ok {
  289. isEventTime, ok = iet.(bool)
  290. if !ok {
  291. return nil, nil, fmt.Errorf("Invalid rule option isEventTime %v, bool type is required.", iet)
  292. }
  293. }
  294. if isEventTime {
  295. if l, ok := rule.Options["lateTolerance"]; ok {
  296. if fl, ok := l.(float64); ok {
  297. lateTol = int64(fl)
  298. } else {
  299. return nil, nil, fmt.Errorf("Invalid rule option lateTolerance %v, int type is required.", l)
  300. }
  301. }
  302. }
  303. if l, ok := rule.Options["concurrency"]; ok {
  304. if fl, ok := l.(float64); ok {
  305. concurrency = int(fl)
  306. } else {
  307. return nil, nil, fmt.Errorf("Invalid rule option concurrency %v, int type is required.", l)
  308. }
  309. }
  310. if l, ok := rule.Options["bufferLength"]; ok {
  311. if fl, ok := l.(float64); ok {
  312. bufferLength = int(fl)
  313. } else {
  314. return nil, nil, fmt.Errorf("Invalid rule option bufferLength %v, int type is required.", l)
  315. }
  316. }
  317. log.Infof("Init rule with options {isEventTime: %v, lateTolerance: %d, concurrency: %d, bufferLength: %d", isEventTime, lateTol, concurrency, bufferLength)
  318. shouldCreateSource := sources == nil
  319. parser := xsql.NewParser(strings.NewReader(sql))
  320. if stmt, err := xsql.Language.Parse(parser); err != nil {
  321. return nil, nil, fmt.Errorf("Parse SQL %s error: %s.", sql, err)
  322. } else {
  323. if selectStmt, ok := stmt.(*xsql.SelectStatement); !ok {
  324. return nil, nil, fmt.Errorf("SQL %s is not a select statement.", sql)
  325. } else {
  326. tp := xstream.NewWithName(name)
  327. var inputs []api.Emitter
  328. streamsFromStmt := xsql.GetStreams(selectStmt)
  329. if !shouldCreateSource && len(streamsFromStmt) != len(sources) {
  330. return nil, nil, fmt.Errorf("Invalid parameter sources or streams, the length cannot match the statement, expect %d sources.", len(streamsFromStmt))
  331. }
  332. store := common.GetSimpleKVStore(path.Join(p.dbDir, "stream"))
  333. err := store.Open()
  334. if err != nil {
  335. return nil, nil, err
  336. }
  337. defer store.Close()
  338. for i, s := range streamsFromStmt {
  339. streamStmt, err := GetStream(store, s)
  340. if err != nil {
  341. return nil, nil, fmt.Errorf("fail to get stream %s, please check if stream is created", s)
  342. }
  343. pp, err := plans.NewPreprocessor(streamStmt, selectStmt.Fields, isEventTime)
  344. if err != nil {
  345. return nil, nil, err
  346. }
  347. if shouldCreateSource {
  348. node := nodes.NewSourceNode(s, streamStmt.Options)
  349. tp.AddSrc(node)
  350. preprocessorOp := xstream.Transform(pp, "preprocessor_"+s, bufferLength)
  351. preprocessorOp.SetConcurrency(concurrency)
  352. tp.AddOperator([]api.Emitter{node}, preprocessorOp)
  353. inputs = append(inputs, preprocessorOp)
  354. } else {
  355. tp.AddSrc(sources[i])
  356. preprocessorOp := xstream.Transform(pp, "preprocessor_"+s, bufferLength)
  357. preprocessorOp.SetConcurrency(concurrency)
  358. tp.AddOperator([]api.Emitter{sources[i]}, preprocessorOp)
  359. inputs = append(inputs, preprocessorOp)
  360. }
  361. }
  362. dimensions := selectStmt.Dimensions
  363. var w *xsql.Window
  364. if dimensions != nil {
  365. w = dimensions.GetWindow()
  366. if w != nil {
  367. wop, err := operators.NewWindowOp("window", w, isEventTime, lateTol, streamsFromStmt, bufferLength)
  368. if err != nil {
  369. return nil, nil, err
  370. }
  371. tp.AddOperator(inputs, wop)
  372. inputs = []api.Emitter{wop}
  373. }
  374. }
  375. if w != nil && selectStmt.Joins != nil {
  376. joinOp := xstream.Transform(&plans.JoinPlan{Joins: selectStmt.Joins, From: selectStmt.Sources[0].(*xsql.Table)}, "join", bufferLength)
  377. joinOp.SetConcurrency(concurrency)
  378. tp.AddOperator(inputs, joinOp)
  379. inputs = []api.Emitter{joinOp}
  380. }
  381. if selectStmt.Condition != nil {
  382. filterOp := xstream.Transform(&plans.FilterPlan{Condition: selectStmt.Condition}, "filter", bufferLength)
  383. filterOp.SetConcurrency(concurrency)
  384. tp.AddOperator(inputs, filterOp)
  385. inputs = []api.Emitter{filterOp}
  386. }
  387. var ds xsql.Dimensions
  388. if dimensions != nil {
  389. ds = dimensions.GetGroups()
  390. if ds != nil && len(ds) > 0 {
  391. aggregateOp := xstream.Transform(&plans.AggregatePlan{Dimensions: ds}, "aggregate", bufferLength)
  392. aggregateOp.SetConcurrency(concurrency)
  393. tp.AddOperator(inputs, aggregateOp)
  394. inputs = []api.Emitter{aggregateOp}
  395. }
  396. }
  397. if selectStmt.Having != nil {
  398. havingOp := xstream.Transform(&plans.HavingPlan{selectStmt.Having}, "having", bufferLength)
  399. havingOp.SetConcurrency(concurrency)
  400. tp.AddOperator(inputs, havingOp)
  401. inputs = []api.Emitter{havingOp}
  402. }
  403. if selectStmt.SortFields != nil {
  404. orderOp := xstream.Transform(&plans.OrderPlan{SortFields: selectStmt.SortFields}, "order", bufferLength)
  405. orderOp.SetConcurrency(concurrency)
  406. tp.AddOperator(inputs, orderOp)
  407. inputs = []api.Emitter{orderOp}
  408. }
  409. if selectStmt.Fields != nil {
  410. projectOp := xstream.Transform(&plans.ProjectPlan{Fields: selectStmt.Fields, IsAggregate: xsql.IsAggStatement(selectStmt)}, "project", bufferLength)
  411. projectOp.SetConcurrency(concurrency)
  412. tp.AddOperator(inputs, projectOp)
  413. inputs = []api.Emitter{projectOp}
  414. }
  415. return tp, inputs, nil
  416. }
  417. }
  418. }