collections.go 9.4 KB

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