dataSourcePlan.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // Copyright 2021 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. "fmt"
  17. "github.com/lf-edge/ekuiper/internal/conf"
  18. "github.com/lf-edge/ekuiper/pkg/ast"
  19. "github.com/lf-edge/ekuiper/pkg/message"
  20. "sort"
  21. "strings"
  22. )
  23. type DataSourcePlan struct {
  24. baseLogicalPlan
  25. name ast.StreamName
  26. // calculated properties
  27. // initialized with stream definition, pruned with rule
  28. streamFields []interface{}
  29. metaFields []string
  30. // passon properties
  31. streamStmt *ast.StreamStmt
  32. allMeta bool
  33. isBinary bool
  34. iet bool
  35. timestampFormat string
  36. timestampField string
  37. // intermediate status
  38. isWildCard bool
  39. fields map[string]interface{}
  40. metaMap map[string]string
  41. }
  42. func (p DataSourcePlan) Init() *DataSourcePlan {
  43. p.baseLogicalPlan.self = &p
  44. return &p
  45. }
  46. // Presume no children for data source
  47. func (p *DataSourcePlan) PushDownPredicate(condition ast.Expr) (ast.Expr, LogicalPlan) {
  48. owned, other := p.extract(condition)
  49. if owned != nil {
  50. // Add a filter plan for children
  51. f := FilterPlan{
  52. condition: owned,
  53. }.Init()
  54. f.SetChildren([]LogicalPlan{p})
  55. return other, f
  56. }
  57. return other, p
  58. }
  59. func (p *DataSourcePlan) extract(expr ast.Expr) (ast.Expr, ast.Expr) {
  60. s, hasDefault := getRefSources(expr)
  61. l := len(s)
  62. if hasDefault {
  63. l += 1
  64. }
  65. switch len(s) {
  66. case 0:
  67. return expr, nil
  68. case 1:
  69. if s[0] == p.name || s[0] == ast.DefaultStream {
  70. return expr, nil
  71. } else {
  72. return nil, expr
  73. }
  74. default:
  75. if be, ok := expr.(*ast.BinaryExpr); ok && be.OP == ast.AND {
  76. ul, pl := p.extract(be.LHS)
  77. ur, pr := p.extract(be.RHS)
  78. owned := combine(ul, ur)
  79. other := combine(pl, pr)
  80. return owned, other
  81. }
  82. return nil, expr
  83. }
  84. }
  85. func (p *DataSourcePlan) PruneColumns(fields []ast.Expr) error {
  86. //init values
  87. p.getProps()
  88. p.fields = make(map[string]interface{})
  89. if !p.allMeta {
  90. p.metaMap = make(map[string]string)
  91. }
  92. if p.timestampField != "" {
  93. p.fields[p.timestampField] = p.timestampField
  94. }
  95. for _, field := range fields {
  96. switch f := field.(type) {
  97. case *ast.Wildcard:
  98. p.isWildCard = true
  99. case *ast.FieldRef:
  100. if !p.isWildCard && (f.StreamName == ast.DefaultStream || f.StreamName == p.name) {
  101. if _, ok := p.fields[f.Name]; !ok {
  102. sf := p.getField(f.Name)
  103. if sf != nil {
  104. p.fields[f.Name] = sf
  105. }
  106. }
  107. }
  108. case *ast.MetaRef:
  109. if p.allMeta {
  110. break
  111. }
  112. if f.StreamName == ast.DefaultStream || f.StreamName == p.name {
  113. if f.Name == "*" {
  114. p.allMeta = true
  115. p.metaMap = nil
  116. } else if !p.allMeta {
  117. p.metaMap[strings.ToLower(f.Name)] = f.Name
  118. }
  119. }
  120. case *ast.SortField:
  121. if !p.isWildCard {
  122. sf := p.getField(f.Name)
  123. if sf != nil {
  124. p.fields[f.Name] = sf
  125. }
  126. }
  127. default:
  128. return fmt.Errorf("unsupported field %v", field)
  129. }
  130. }
  131. p.getAllFields()
  132. return nil
  133. }
  134. func (p *DataSourcePlan) getField(name string) interface{} {
  135. if p.streamStmt.StreamFields != nil {
  136. for _, f := range p.streamStmt.StreamFields { // The input can only be StreamFields
  137. if f.Name == name {
  138. return &f
  139. }
  140. }
  141. } else {
  142. return name
  143. }
  144. return nil
  145. }
  146. func (p *DataSourcePlan) getAllFields() {
  147. // convert fields
  148. p.streamFields = make([]interface{}, 0)
  149. if p.isWildCard {
  150. if p.streamStmt.StreamFields != nil {
  151. for k, _ := range p.streamStmt.StreamFields { // The input can only be StreamFields
  152. p.streamFields = append(p.streamFields, &p.streamStmt.StreamFields[k])
  153. }
  154. } else {
  155. p.streamFields = nil
  156. }
  157. } else {
  158. sfs := make([]interface{}, 0, len(p.fields))
  159. if conf.IsTesting {
  160. var keys []string
  161. for k, _ := range p.fields {
  162. keys = append(keys, k)
  163. }
  164. sort.Strings(keys)
  165. for _, k := range keys {
  166. sfs = append(sfs, p.fields[k])
  167. }
  168. } else {
  169. for _, v := range p.fields {
  170. sfs = append(sfs, v)
  171. }
  172. }
  173. p.streamFields = sfs
  174. }
  175. p.metaFields = make([]string, 0, len(p.metaMap))
  176. for _, v := range p.metaMap {
  177. p.metaFields = append(p.metaFields, v)
  178. }
  179. // for consistency of results for testing
  180. sort.Strings(p.metaFields)
  181. p.fields = nil
  182. p.metaMap = nil
  183. }
  184. func (p *DataSourcePlan) getProps() error {
  185. if p.iet {
  186. if p.streamStmt.Options.TIMESTAMP != "" {
  187. p.timestampField = p.streamStmt.Options.TIMESTAMP
  188. } else {
  189. return fmt.Errorf("preprocessor is set to be event time but stream option TIMESTAMP not found")
  190. }
  191. if p.streamStmt.Options.TIMESTAMP_FORMAT != "" {
  192. p.timestampFormat = p.streamStmt.Options.TIMESTAMP_FORMAT
  193. }
  194. }
  195. if strings.ToLower(p.streamStmt.Options.FORMAT) == message.FormatBinary {
  196. p.isBinary = true
  197. }
  198. return nil
  199. }