collections.go 9.5 KB

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