dataSourcePlan.go 5.1 KB

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