collections.go 9.4 KB

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