collections.go 9.1 KB

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