dataSourcePlan.go 5.1 KB

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