dataSourcePlan.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. err := p.getProps()
  88. if err != nil {
  89. return err
  90. }
  91. p.fields = make(map[string]interface{})
  92. if !p.allMeta {
  93. p.metaMap = make(map[string]string)
  94. }
  95. if p.timestampField != "" {
  96. p.fields[p.timestampField] = p.timestampField
  97. }
  98. for _, field := range fields {
  99. switch f := field.(type) {
  100. case *ast.Wildcard:
  101. p.isWildCard = true
  102. case *ast.FieldRef:
  103. if !p.isWildCard && (f.StreamName == ast.DefaultStream || f.StreamName == p.name) {
  104. if _, ok := p.fields[f.Name]; !ok {
  105. sf := p.getField(f.Name)
  106. if sf != nil {
  107. p.fields[f.Name] = sf
  108. }
  109. }
  110. }
  111. case *ast.MetaRef:
  112. if p.allMeta {
  113. break
  114. }
  115. if f.StreamName == ast.DefaultStream || f.StreamName == p.name {
  116. if f.Name == "*" {
  117. p.allMeta = true
  118. p.metaMap = nil
  119. } else if !p.allMeta {
  120. p.metaMap[strings.ToLower(f.Name)] = f.Name
  121. }
  122. }
  123. case *ast.SortField:
  124. if !p.isWildCard {
  125. sf := p.getField(f.Name)
  126. if sf != nil {
  127. p.fields[f.Name] = sf
  128. }
  129. }
  130. default:
  131. return fmt.Errorf("unsupported field %v", field)
  132. }
  133. }
  134. p.getAllFields()
  135. return nil
  136. }
  137. func (p *DataSourcePlan) getField(name string) interface{} {
  138. if p.streamStmt.StreamFields != nil {
  139. for _, f := range p.streamStmt.StreamFields { // The input can only be StreamFields
  140. if f.Name == name {
  141. return &f
  142. }
  143. }
  144. } else {
  145. return name
  146. }
  147. return nil
  148. }
  149. func (p *DataSourcePlan) getAllFields() {
  150. // convert fields
  151. p.streamFields = make([]interface{}, 0)
  152. if p.isWildCard {
  153. if p.streamStmt.StreamFields != nil {
  154. for k := range p.streamStmt.StreamFields { // The input can only be StreamFields
  155. p.streamFields = append(p.streamFields, &p.streamStmt.StreamFields[k])
  156. }
  157. } else {
  158. p.streamFields = nil
  159. }
  160. } else {
  161. sfs := make([]interface{}, 0, len(p.fields))
  162. if conf.IsTesting {
  163. var keys []string
  164. for k := range p.fields {
  165. keys = append(keys, k)
  166. }
  167. sort.Strings(keys)
  168. for _, k := range keys {
  169. sfs = append(sfs, p.fields[k])
  170. }
  171. } else {
  172. for _, v := range p.fields {
  173. sfs = append(sfs, v)
  174. }
  175. }
  176. p.streamFields = sfs
  177. }
  178. p.metaFields = make([]string, 0, len(p.metaMap))
  179. for _, v := range p.metaMap {
  180. p.metaFields = append(p.metaFields, v)
  181. }
  182. // for consistency of results for testing
  183. sort.Strings(p.metaFields)
  184. p.fields = nil
  185. p.metaMap = nil
  186. }
  187. func (p *DataSourcePlan) getProps() error {
  188. if p.iet {
  189. if p.streamStmt.Options.TIMESTAMP != "" {
  190. p.timestampField = p.streamStmt.Options.TIMESTAMP
  191. } else {
  192. return fmt.Errorf("preprocessor is set to be event time but stream option TIMESTAMP not found")
  193. }
  194. }
  195. if p.streamStmt.Options.TIMESTAMP_FORMAT != "" {
  196. p.timestampFormat = p.streamStmt.Options.TIMESTAMP_FORMAT
  197. }
  198. if strings.EqualFold(p.streamStmt.Options.FORMAT, message.FormatBinary) {
  199. p.isBinary = true
  200. }
  201. return nil
  202. }