collections.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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 xsql
  15. import (
  16. "github.com/lf-edge/ekuiper/internal/conf"
  17. "github.com/lf-edge/ekuiper/pkg/ast"
  18. "sort"
  19. "strings"
  20. )
  21. /**********************************
  22. ** Various Data Types for SQL transformation
  23. */
  24. type AggregateData interface {
  25. AggregateEval(expr ast.Expr, v CallValuer) []interface{}
  26. GetWindowStart() int64
  27. GetWindowEnd() int64
  28. }
  29. // Message is a valuer that substitutes values for the mapped interface.
  30. type Message map[string]interface{}
  31. func ToMessage(input interface{}) (Message, bool) {
  32. var result Message
  33. switch m := input.(type) {
  34. case Message:
  35. result = m
  36. case Metadata:
  37. result = Message(m)
  38. case map[string]interface{}:
  39. result = m
  40. default:
  41. return nil, false
  42. }
  43. return result, true
  44. }
  45. // Value returns the value for a key in the Message.
  46. func (m Message) Value(key string) (interface{}, bool) {
  47. var colkey string
  48. if keys := strings.Split(key, ast.COLUMN_SEPARATOR); len(keys) == 1 {
  49. colkey = key
  50. } else if len(keys) == 2 {
  51. colkey = keys[1]
  52. } else {
  53. conf.Log.Println("Invalid key: " + key + ", expect source.field or field.")
  54. return nil, false
  55. }
  56. key1 := strings.ToLower(colkey)
  57. if v, ok := m[key1]; ok {
  58. return v, ok
  59. } else {
  60. //Only when with 'SELECT * FROM ...' and 'schemaless', the key in map is not convert to lower case.
  61. //So all of keys in map should be convert to lowercase and then compare them.
  62. return m.getIgnoreCase(colkey)
  63. }
  64. }
  65. func (m Message) getIgnoreCase(key interface{}) (interface{}, bool) {
  66. if k, ok := key.(string); ok {
  67. key = strings.ToLower(k)
  68. for k, v := range m {
  69. if strings.ToLower(k) == key {
  70. return v, true
  71. }
  72. }
  73. }
  74. return nil, false
  75. }
  76. func (m Message) Meta(key string) (interface{}, bool) {
  77. if key == "*" {
  78. return map[string]interface{}(m), true
  79. }
  80. return m.Value(key)
  81. }
  82. func (m Message) AppendAlias(k string, v interface{}) bool {
  83. conf.Log.Debugf("append alias %s:%v\n", k, v)
  84. return false
  85. }
  86. type Event interface {
  87. GetTimestamp() int64
  88. IsWatermark() bool
  89. }
  90. type Metadata Message
  91. func (m Metadata) Value(key string) (interface{}, bool) {
  92. msg := Message(m)
  93. return msg.Value(key)
  94. }
  95. func (m Metadata) Meta(key string) (interface{}, bool) {
  96. if key == "*" {
  97. return map[string]interface{}(m), true
  98. }
  99. msg := Message(m)
  100. return msg.Meta(key)
  101. }
  102. type Alias struct {
  103. AliasMap Message
  104. }
  105. func (a *Alias) AppendAlias(key string, value interface{}) bool {
  106. if a.AliasMap == nil {
  107. a.AliasMap = make(map[string]interface{})
  108. }
  109. a.AliasMap[PRIVATE_PREFIX+key] = value
  110. return true
  111. }
  112. func (a *Alias) AliasValue(key string) (interface{}, bool) {
  113. if a.AliasMap == nil {
  114. return nil, false
  115. }
  116. return a.AliasMap.Value(key)
  117. }
  118. type Tuple struct {
  119. Emitter string
  120. Message Message // immutable
  121. Timestamp int64
  122. Metadata Metadata // immutable
  123. Alias
  124. }
  125. func (t *Tuple) Value(key string) (interface{}, bool) {
  126. r, ok := t.AliasValue(key)
  127. if ok {
  128. return r, ok
  129. }
  130. return t.Message.Value(key)
  131. }
  132. func (t *Tuple) Meta(key string) (interface{}, bool) {
  133. if key == "*" {
  134. return map[string]interface{}(t.Metadata), true
  135. }
  136. return t.Metadata.Value(key)
  137. }
  138. func (t *Tuple) All(string) (interface{}, bool) {
  139. return t.Message, true
  140. }
  141. func (t *Tuple) AggregateEval(expr ast.Expr, v CallValuer) []interface{} {
  142. return []interface{}{Eval(expr, MultiValuer(t, v, &WildcardValuer{t}))}
  143. }
  144. func (t *Tuple) GetWindowStart() int64 {
  145. return 0
  146. }
  147. func (t *Tuple) GetWindowEnd() int64 {
  148. return 0
  149. }
  150. func (t *Tuple) GetTimestamp() int64 {
  151. return t.Timestamp
  152. }
  153. func (t *Tuple) GetMetadata() Metadata {
  154. return t.Metadata
  155. }
  156. func (t *Tuple) IsWatermark() bool {
  157. return false
  158. }
  159. func (t *Tuple) Clone() DataValuer {
  160. c := &Tuple{
  161. Emitter: t.Emitter,
  162. Timestamp: t.Timestamp,
  163. }
  164. if t.Message != nil {
  165. m := Message{}
  166. for k, v := range t.Message {
  167. m[k] = v
  168. }
  169. c.Message = m
  170. }
  171. if t.Metadata != nil {
  172. md := Metadata{}
  173. for k, v := range t.Metadata {
  174. md[k] = v
  175. }
  176. c.Metadata = md
  177. }
  178. return c
  179. }
  180. type WindowTuples struct {
  181. Emitter string
  182. Tuples []Tuple
  183. }
  184. type WindowRange struct {
  185. WindowStart int64
  186. WindowEnd int64
  187. }
  188. func (r *WindowRange) GetWindowStart() int64 {
  189. return r.WindowStart
  190. }
  191. func (r *WindowRange) GetWindowEnd() int64 {
  192. return r.WindowEnd
  193. }
  194. type WindowTuplesSet struct {
  195. Content []WindowTuples
  196. *WindowRange
  197. }
  198. func (w WindowTuplesSet) GetBySrc(src string) []Tuple {
  199. for _, me := range w.Content {
  200. if me.Emitter == src {
  201. return me.Tuples
  202. }
  203. }
  204. return nil
  205. }
  206. func (w WindowTuplesSet) Len() int {
  207. if len(w.Content) > 0 {
  208. return len(w.Content[0].Tuples)
  209. }
  210. return 0
  211. }
  212. func (w WindowTuplesSet) Swap(i, j int) {
  213. if len(w.Content) > 0 {
  214. s := w.Content[0].Tuples
  215. s[i], s[j] = s[j], s[i]
  216. }
  217. }
  218. func (w WindowTuplesSet) Index(i int) Valuer {
  219. if len(w.Content) > 0 {
  220. s := w.Content[0].Tuples
  221. return &(s[i])
  222. }
  223. return nil
  224. }
  225. func (w WindowTuplesSet) AddTuple(tuple *Tuple) WindowTuplesSet {
  226. found := false
  227. for i, t := range w.Content {
  228. if t.Emitter == tuple.Emitter {
  229. t.Tuples = append(t.Tuples, *tuple)
  230. found = true
  231. w.Content[i] = t
  232. break
  233. }
  234. }
  235. if !found {
  236. ets := &WindowTuples{Emitter: tuple.Emitter}
  237. ets.Tuples = append(ets.Tuples, *tuple)
  238. w.Content = append(w.Content, *ets)
  239. }
  240. return w
  241. }
  242. //Sort by tuple timestamp
  243. func (w WindowTuplesSet) Sort() {
  244. for _, t := range w.Content {
  245. tuples := t.Tuples
  246. sort.SliceStable(tuples, func(i, j int) bool {
  247. return tuples[i].Timestamp < tuples[j].Timestamp
  248. })
  249. t.Tuples = tuples
  250. }
  251. }
  252. func (w WindowTuplesSet) AggregateEval(expr ast.Expr, v CallValuer) []interface{} {
  253. var result []interface{}
  254. if len(w.Content) != 1 { //should never happen
  255. return nil
  256. }
  257. for _, t := range w.Content[0].Tuples {
  258. result = append(result, Eval(expr, MultiValuer(&t, v, &WildcardValuer{&t})))
  259. }
  260. return result
  261. }
  262. type JoinTuple struct {
  263. Tuples []Tuple
  264. Alias
  265. }
  266. func (jt *JoinTuple) AddTuple(tuple Tuple) {
  267. jt.Tuples = append(jt.Tuples, tuple)
  268. }
  269. func (jt *JoinTuple) AddTuples(tuples []Tuple) {
  270. for _, t := range tuples {
  271. jt.Tuples = append(jt.Tuples, t)
  272. }
  273. }
  274. func getTupleValue(tuple Tuple, t string, key string) (interface{}, bool) {
  275. switch t {
  276. case "value":
  277. return tuple.Value(key)
  278. case "meta":
  279. return tuple.Meta(key)
  280. default:
  281. conf.Log.Errorf("cannot get tuple for type %s", t)
  282. return nil, false
  283. }
  284. }
  285. func (jt *JoinTuple) doGetValue(t string, key string) (interface{}, bool) {
  286. keys := strings.Split(key, ast.COLUMN_SEPARATOR)
  287. tuples := jt.Tuples
  288. switch len(keys) {
  289. case 1:
  290. if len(tuples) > 1 {
  291. for _, tuple := range tuples { //TODO support key without modifier?
  292. v, ok := getTupleValue(tuple, t, key)
  293. if ok {
  294. return v, ok
  295. }
  296. }
  297. conf.Log.Debugf("Wrong key: %s not found", key)
  298. return nil, false
  299. } else {
  300. return getTupleValue(tuples[0], t, key)
  301. }
  302. case 2:
  303. emitter, key := keys[0], keys[1]
  304. //TODO should use hash here
  305. for _, tuple := range tuples {
  306. if tuple.Emitter == emitter {
  307. return getTupleValue(tuple, t, key)
  308. }
  309. }
  310. return nil, false
  311. default:
  312. conf.Log.Infoln("Wrong key: ", key, ", expect dot in the expression.")
  313. return nil, false
  314. }
  315. }
  316. func (jt *JoinTuple) Value(key string) (interface{}, bool) {
  317. r, ok := jt.AliasValue(key)
  318. if ok {
  319. return r, ok
  320. }
  321. return jt.doGetValue("value", key)
  322. }
  323. func (jt *JoinTuple) Meta(key string) (interface{}, bool) {
  324. return jt.doGetValue("meta", key)
  325. }
  326. func (jt *JoinTuple) All(stream string) (interface{}, bool) {
  327. if stream != "" {
  328. for _, t := range jt.Tuples {
  329. if t.Emitter == stream {
  330. return t.Message, true
  331. }
  332. }
  333. } else {
  334. var r Message = make(map[string]interface{})
  335. for _, t := range jt.Tuples {
  336. for k, v := range t.Message {
  337. if _, ok := r[k]; !ok {
  338. r[k] = v
  339. }
  340. }
  341. }
  342. return r, true
  343. }
  344. return nil, false
  345. }
  346. func (jt *JoinTuple) Clone() DataValuer {
  347. ts := make([]Tuple, len(jt.Tuples))
  348. for i, t := range jt.Tuples {
  349. ts[i] = *(t.Clone().(*Tuple))
  350. }
  351. return &JoinTuple{Tuples: ts}
  352. }
  353. type JoinTupleSets struct {
  354. Content []JoinTuple
  355. *WindowRange
  356. }
  357. func (s *JoinTupleSets) Len() int { return len(s.Content) }
  358. func (s *JoinTupleSets) Swap(i, j int) { s.Content[i], s.Content[j] = s.Content[j], s.Content[i] }
  359. func (s *JoinTupleSets) Index(i int) Valuer { return &(s.Content[i]) }
  360. func (s *JoinTupleSets) AggregateEval(expr ast.Expr, v CallValuer) []interface{} {
  361. var result []interface{}
  362. for _, t := range s.Content {
  363. result = append(result, Eval(expr, MultiValuer(&t, v, &WildcardValuer{&t})))
  364. }
  365. return result
  366. }
  367. type GroupedTuples struct {
  368. Content []DataValuer
  369. *WindowRange
  370. }
  371. func (s GroupedTuples) AggregateEval(expr ast.Expr, v CallValuer) []interface{} {
  372. var result []interface{}
  373. for _, t := range s.Content {
  374. result = append(result, Eval(expr, MultiValuer(t, v, &WildcardValuer{t})))
  375. }
  376. return result
  377. }
  378. type GroupedTuplesSet []GroupedTuples
  379. func (s GroupedTuplesSet) Len() int { return len(s) }
  380. func (s GroupedTuplesSet) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
  381. func (s GroupedTuplesSet) Index(i int) Valuer { return s[i].Content[0] }