collections.go 9.6 KB

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