dataSourcePlan.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. // Copyright 2021-2023 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. pruneFields []string
  43. }
  44. func (p DataSourcePlan) Init() *DataSourcePlan {
  45. p.baseLogicalPlan.self = &p
  46. p.baseLogicalPlan.setPlanType(DATASOURCE)
  47. return &p
  48. }
  49. func (p *DataSourcePlan) BuildExplainInfo(id int64) {
  50. info := ""
  51. if p.name != "" {
  52. info += "StreamName: " + string(p.name)
  53. }
  54. if p.fields != nil && len(p.fields) != 0 {
  55. info += ", Fields:[ "
  56. keys := make([]string, 0, len(p.fields))
  57. for k := range p.fields {
  58. keys = append(keys, k)
  59. }
  60. sort.Strings(keys)
  61. for i := 0; i < len(keys); i++ {
  62. info += keys[i]
  63. if i != len(keys)-1 {
  64. info += ", "
  65. }
  66. }
  67. info += " ]"
  68. }
  69. if p.streamFields != nil && len(p.streamFields) != 0 {
  70. info += ", StreamFields:[ "
  71. keys := make([]string, 0, len(p.streamFields))
  72. for k := range p.streamFields {
  73. keys = append(keys, k)
  74. }
  75. sort.Strings(keys)
  76. for i := 0; i < len(keys); i++ {
  77. info += keys[i]
  78. if i != len(keys)-1 {
  79. info += ", "
  80. }
  81. }
  82. info += " ]"
  83. }
  84. p.baseLogicalPlan.ExplainInfo.ID = id
  85. p.baseLogicalPlan.ExplainInfo.Info = info
  86. }
  87. // PushDownPredicate Presume no children for data source
  88. func (p *DataSourcePlan) PushDownPredicate(condition ast.Expr) (ast.Expr, LogicalPlan) {
  89. if p.streamStmt.StreamType == ast.TypeTable {
  90. return condition, p.self
  91. }
  92. owned, other := p.extract(condition)
  93. if owned != nil {
  94. // Add a filter plan for children
  95. f := FilterPlan{
  96. condition: owned,
  97. }.Init()
  98. f.SetChildren([]LogicalPlan{p})
  99. return other, f
  100. }
  101. return other, p
  102. }
  103. func (p *DataSourcePlan) extract(expr ast.Expr) (ast.Expr, ast.Expr) {
  104. s, hasDefault := getRefSources(expr)
  105. l := len(s)
  106. if hasDefault {
  107. l += 1
  108. }
  109. switch len(s) {
  110. case 0:
  111. return expr, nil
  112. case 1:
  113. if s[0] == p.name || s[0] == ast.DefaultStream {
  114. return expr, nil
  115. } else {
  116. return nil, expr
  117. }
  118. default:
  119. if be, ok := expr.(*ast.BinaryExpr); ok && be.OP == ast.AND {
  120. ul, pl := p.extract(be.LHS)
  121. ur, pr := p.extract(be.RHS)
  122. owned := combine(ul, ur)
  123. other := combine(pl, pr)
  124. return owned, other
  125. }
  126. return nil, expr
  127. }
  128. }
  129. func (p *DataSourcePlan) PruneColumns(fields []ast.Expr) error {
  130. // init values
  131. err := p.getProps()
  132. if err != nil {
  133. return err
  134. }
  135. p.fields = make(map[string]*ast.JsonStreamField)
  136. p.pruneFields = make([]string, 0)
  137. if !p.allMeta {
  138. p.metaMap = make(map[string]string)
  139. }
  140. if p.timestampField != "" {
  141. if !p.isSchemaless {
  142. tsf, ok := p.streamFields[p.timestampField]
  143. if !ok {
  144. return fmt.Errorf("timestamp field %s not found", p.timestampField)
  145. }
  146. p.fields[p.timestampField] = tsf
  147. } else {
  148. p.fields[p.timestampField] = nil
  149. }
  150. }
  151. for _, field := range fields {
  152. switch f := field.(type) {
  153. case *ast.Wildcard:
  154. if len(f.Except) == 0 && len(f.Replace) == 0 {
  155. p.isWildCard = true
  156. } else {
  157. for _, except := range f.Except {
  158. p.pruneFields = append(p.pruneFields, except)
  159. }
  160. for _, replace := range f.Replace {
  161. p.pruneFields = append(p.pruneFields, replace.AName)
  162. }
  163. }
  164. case *ast.FieldRef:
  165. if !p.isWildCard && (f.StreamName == ast.DefaultStream || f.StreamName == p.name) {
  166. if _, ok := p.fields[f.Name]; !ok {
  167. sf, err := p.getField(f.Name, f.StreamName == p.name)
  168. if err != nil {
  169. return err
  170. }
  171. if p.isSchemaless || sf != nil {
  172. p.fields[f.Name] = sf
  173. }
  174. }
  175. }
  176. case *ast.MetaRef:
  177. if p.allMeta {
  178. break
  179. }
  180. if f.StreamName == ast.DefaultStream || f.StreamName == p.name {
  181. if f.Name == "*" {
  182. p.allMeta = true
  183. p.metaMap = nil
  184. } else if !p.allMeta {
  185. p.metaMap[strings.ToLower(f.Name)] = f.Name
  186. }
  187. }
  188. case *ast.SortField:
  189. if !p.isWildCard {
  190. sf, err := p.getField(f.Name, f.StreamName == p.name)
  191. if err != nil {
  192. return err
  193. }
  194. if p.isSchemaless || sf != nil {
  195. p.fields[f.Name] = sf
  196. }
  197. }
  198. default:
  199. return fmt.Errorf("unsupported field %v", field)
  200. }
  201. }
  202. p.getAllFields()
  203. return nil
  204. }
  205. func (p *DataSourcePlan) getField(name string, strict bool) (*ast.JsonStreamField, error) {
  206. if !p.isSchemaless {
  207. r, ok := p.streamFields[name]
  208. if !ok {
  209. if strict {
  210. return nil, fmt.Errorf("field %s not found in stream %s", name, p.name)
  211. }
  212. } else {
  213. return r, nil
  214. }
  215. }
  216. // always return nil for schemaless
  217. return nil, nil
  218. }
  219. // Do not prune fields now for preprocessor
  220. // TODO provide field information to the source for it to prune
  221. func (p *DataSourcePlan) getAllFields() {
  222. if !p.isWildCard {
  223. if len(p.pruneFields) == 0 {
  224. p.streamFields = p.fields
  225. } else {
  226. for _, pf := range p.pruneFields {
  227. prune := true
  228. for f := range p.fields {
  229. if pf == f {
  230. prune = false
  231. break
  232. }
  233. }
  234. if prune {
  235. delete(p.streamFields, pf)
  236. }
  237. }
  238. }
  239. }
  240. p.metaFields = make([]string, 0, len(p.metaMap))
  241. for _, v := range p.metaMap {
  242. p.metaFields = append(p.metaFields, v)
  243. }
  244. // for consistency of results for testing
  245. sort.Strings(p.metaFields)
  246. p.fields = nil
  247. p.metaMap = nil
  248. }
  249. func (p *DataSourcePlan) getProps() error {
  250. if p.iet {
  251. if p.streamStmt.Options.TIMESTAMP != "" {
  252. p.timestampField = p.streamStmt.Options.TIMESTAMP
  253. } else {
  254. return fmt.Errorf("preprocessor is set to be event time but stream option TIMESTAMP not found")
  255. }
  256. }
  257. if p.streamStmt.Options.TIMESTAMP_FORMAT != "" {
  258. p.timestampFormat = p.streamStmt.Options.TIMESTAMP_FORMAT
  259. }
  260. if strings.EqualFold(p.streamStmt.Options.FORMAT, message.FormatBinary) {
  261. p.isBinary = true
  262. }
  263. return nil
  264. }