row.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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. "strings"
  19. )
  20. // The original message map may be big. Make sure it is immutable so that never make a copy of it.
  21. // The tuple clone should be cheap.
  22. /*
  23. * Interfaces definition
  24. */
  25. type Wildcarder interface {
  26. // All Value returns the value and existence flag for a given key.
  27. All(stream string) (Message, bool)
  28. }
  29. type Event interface {
  30. GetTimestamp() int64
  31. IsWatermark() bool
  32. }
  33. type ReadonlyRow interface {
  34. Valuer
  35. AliasValuer
  36. Wildcarder
  37. }
  38. type Row interface {
  39. ReadonlyRow
  40. // Set Only for some ops like functionOp *
  41. Set(col string, value interface{})
  42. // ToMap converts the row to a map to export to other systems *
  43. ToMap() map[string]interface{}
  44. // Pick the columns and discard others. It replaces the underlying message with a new value. There are 3 types to pick: column, alias and annonymous expressions.
  45. // cols is a list [columnname, tablename]
  46. Pick(allWildcard bool, cols [][]string, wildcardEmitters map[string]bool)
  47. }
  48. // TupleRow is a mutable row. Function with * could modify the row.
  49. type TupleRow interface {
  50. Row
  51. // GetEmitter returns the emitter of the row
  52. GetEmitter() string
  53. // Clone when broadcast to make sure each row are dealt single threaded
  54. Clone() TupleRow
  55. }
  56. // CollectionRow is the aggregation row of a non-grouped collection. Thinks of it as a single group.
  57. // The row data is immutable
  58. type CollectionRow interface {
  59. Row
  60. AggregateData
  61. // Clone when broadcast to make sure each row are dealt single threaded
  62. //Clone() CollectionRow
  63. }
  64. // AffiliateRow part of other row types do help calculation of newly added cols
  65. type AffiliateRow struct {
  66. CalCols map[string]interface{} // mutable and must be cloned when broadcast
  67. Alias
  68. }
  69. func (d *AffiliateRow) Value(key, table string) (interface{}, bool) {
  70. if table == "" {
  71. r, ok := d.AliasValue(key)
  72. if ok {
  73. return r, ok
  74. }
  75. r, ok = d.CalCols[key]
  76. if ok {
  77. return r, ok
  78. }
  79. }
  80. return nil, false
  81. }
  82. func (d *AffiliateRow) Set(col string, value interface{}) {
  83. if d.CalCols == nil {
  84. d.CalCols = make(map[string]interface{})
  85. }
  86. d.CalCols[col] = value
  87. }
  88. func (d *AffiliateRow) Clone() AffiliateRow {
  89. nd := &AffiliateRow{}
  90. if d.CalCols != nil && len(d.CalCols) > 0 {
  91. nd.CalCols = make(map[string]interface{}, len(d.CalCols))
  92. for k, v := range d.CalCols {
  93. nd.CalCols[k] = v
  94. }
  95. }
  96. if d.AliasMap != nil && len(d.AliasMap) > 0 {
  97. nd.AliasMap = make(map[string]interface{}, len(d.AliasMap))
  98. for k, v := range d.AliasMap {
  99. nd.AliasMap[k] = v
  100. }
  101. }
  102. return *nd
  103. }
  104. func (d *AffiliateRow) IsEmpty() bool {
  105. return len(d.CalCols) == 0 && len(d.AliasMap) == 0
  106. }
  107. func (d *AffiliateRow) MergeMap(cachedMap map[string]interface{}) {
  108. for k, v := range d.CalCols {
  109. cachedMap[k] = v
  110. }
  111. for k, v := range d.AliasMap {
  112. cachedMap[k] = v
  113. }
  114. }
  115. func (d *AffiliateRow) Reset() {
  116. d.CalCols = nil
  117. d.AliasMap = nil
  118. }
  119. /*
  120. * Message definition
  121. */
  122. // Message is a valuer that substitutes values for the mapped interface. It is the basic type for data events.
  123. type Message map[string]interface{}
  124. var _ Valuer = Message{}
  125. type Metadata Message
  126. // Alias will not need to convert cases
  127. type Alias struct {
  128. AliasMap map[string]interface{}
  129. }
  130. /*
  131. * All row types definitions, watermark, barrier
  132. */
  133. // Tuple The input row, produced by the source
  134. type Tuple struct {
  135. Emitter string
  136. Message Message // the original pointer is immutable & big; may be cloned
  137. Timestamp int64
  138. Metadata Metadata // immutable
  139. AffiliateRow
  140. cachedMap map[string]interface{} // clone of the row and cached for performance
  141. }
  142. var _ TupleRow = &Tuple{}
  143. // JoinTuple is a row produced by a join operation
  144. type JoinTuple struct {
  145. Tuples []TupleRow // The content is immutable, but the slice may be add or removed
  146. AffiliateRow
  147. cachedMap map[string]interface{} // clone of the row and cached for performance of toMap
  148. }
  149. func (jt *JoinTuple) AggregateEval(expr ast.Expr, v CallValuer) []interface{} {
  150. return []interface{}{Eval(expr, MultiValuer(jt, v, &WildcardValuer{jt}))}
  151. }
  152. var _ TupleRow = &JoinTuple{}
  153. // GroupedTuples is a collection of tuples grouped by a key
  154. type GroupedTuples struct {
  155. Content []TupleRow
  156. *WindowRange
  157. AffiliateRow
  158. cachedMap map[string]interface{} // clone of the row and cached for performance of toMap
  159. }
  160. var _ CollectionRow = &GroupedTuples{}
  161. /*
  162. * Implementations
  163. */
  164. func ToMessage(input interface{}) (Message, bool) {
  165. var result Message
  166. switch m := input.(type) {
  167. case Message:
  168. result = m
  169. case Metadata:
  170. result = Message(m)
  171. case map[string]interface{}:
  172. result = m
  173. default:
  174. return nil, false
  175. }
  176. return result, true
  177. }
  178. func (m Message) Value(key, _ string) (interface{}, bool) {
  179. if v, ok := m[key]; ok {
  180. return v, ok
  181. } else if conf.Config == nil || conf.Config.Basic.IgnoreCase {
  182. //Only when with 'SELECT * FROM ...' and 'schemaless', the key in map is not convert to lower case.
  183. //So all of keys in map should be convert to lowercase and then compare them.
  184. return m.getIgnoreCase(key)
  185. } else {
  186. return nil, false
  187. }
  188. }
  189. func (m Message) getIgnoreCase(key interface{}) (interface{}, bool) {
  190. if k, ok := key.(string); ok {
  191. for mk, v := range m {
  192. if strings.EqualFold(k, mk) {
  193. return v, true
  194. }
  195. }
  196. }
  197. return nil, false
  198. }
  199. func (m Message) Meta(key, table string) (interface{}, bool) {
  200. if key == "*" {
  201. return map[string]interface{}(m), true
  202. }
  203. return m.Value(key, table)
  204. }
  205. // MetaData implementation
  206. func (m Metadata) Value(key, table string) (interface{}, bool) {
  207. msg := Message(m)
  208. return msg.Value(key, table)
  209. }
  210. func (m Metadata) Meta(key, table string) (interface{}, bool) {
  211. if key == "*" {
  212. return map[string]interface{}(m), true
  213. }
  214. msg := Message(m)
  215. return msg.Meta(key, table)
  216. }
  217. // Alias implementation
  218. func (a *Alias) AppendAlias(key string, value interface{}) bool {
  219. if a.AliasMap == nil {
  220. a.AliasMap = make(map[string]interface{})
  221. }
  222. a.AliasMap[key] = value
  223. return true
  224. }
  225. func (a *Alias) AliasValue(key string) (interface{}, bool) {
  226. if a.AliasMap == nil {
  227. return nil, false
  228. }
  229. v, ok := a.AliasMap[key]
  230. return v, ok
  231. }
  232. // Tuple implementation
  233. func (t *Tuple) Value(key, table string) (interface{}, bool) {
  234. r, ok := t.AffiliateRow.Value(key, table)
  235. if ok {
  236. return r, ok
  237. }
  238. return t.Message.Value(key, table)
  239. }
  240. func (t *Tuple) All(string) (Message, bool) {
  241. return t.ToMap(), true
  242. }
  243. func (t *Tuple) Clone() TupleRow {
  244. return &Tuple{
  245. Emitter: t.Emitter,
  246. Timestamp: t.Timestamp,
  247. Message: t.Message,
  248. Metadata: t.Metadata,
  249. AffiliateRow: t.AffiliateRow.Clone(),
  250. }
  251. }
  252. // ToMap should only use in sink.
  253. func (t *Tuple) ToMap() map[string]interface{} {
  254. if t.AffiliateRow.IsEmpty() {
  255. return t.Message
  256. }
  257. if t.cachedMap == nil { // clone the message
  258. m := make(map[string]interface{})
  259. for k, v := range t.Message {
  260. m[k] = v
  261. }
  262. t.cachedMap = m
  263. t.Message = t.cachedMap
  264. }
  265. t.AffiliateRow.MergeMap(t.cachedMap)
  266. return t.cachedMap
  267. }
  268. func (t *Tuple) Meta(key, table string) (interface{}, bool) {
  269. if key == "*" {
  270. return map[string]interface{}(t.Metadata), true
  271. }
  272. return t.Metadata.Value(key, table)
  273. }
  274. func (t *Tuple) GetEmitter() string {
  275. return t.Emitter
  276. }
  277. func (t *Tuple) AggregateEval(expr ast.Expr, v CallValuer) []interface{} {
  278. return []interface{}{Eval(expr, MultiValuer(t, v, &WildcardValuer{t}))}
  279. }
  280. func (t *Tuple) GetTimestamp() int64 {
  281. return t.Timestamp
  282. }
  283. func (t *Tuple) IsWatermark() bool {
  284. return false
  285. }
  286. func (t *Tuple) Pick(allWildcard bool, cols [][]string, wildcardEmitters map[string]bool) {
  287. if !allWildcard && wildcardEmitters[t.Emitter] {
  288. allWildcard = true
  289. }
  290. if !allWildcard {
  291. if len(cols) > 0 {
  292. t.cachedMap = make(map[string]interface{})
  293. for _, colTab := range cols {
  294. if colTab[1] == "" || colTab[1] == string(ast.DefaultStream) || colTab[1] == t.Emitter {
  295. if v, ok := t.Message.Value(colTab[0], colTab[1]); ok {
  296. t.cachedMap[colTab[0]] = v
  297. }
  298. }
  299. }
  300. t.Message = t.cachedMap
  301. } else {
  302. t.Message = make(map[string]interface{})
  303. t.cachedMap = t.Message
  304. }
  305. }
  306. t.AffiliateRow.Reset()
  307. }
  308. // JoinTuple implementation
  309. func (jt *JoinTuple) AddTuple(tuple TupleRow) {
  310. jt.Tuples = append(jt.Tuples, tuple)
  311. }
  312. func (jt *JoinTuple) AddTuples(tuples []TupleRow) {
  313. for _, t := range tuples {
  314. jt.Tuples = append(jt.Tuples, t)
  315. }
  316. }
  317. func (jt *JoinTuple) doGetValue(key, table string, isVal bool) (interface{}, bool) {
  318. tuples := jt.Tuples
  319. if table == "" {
  320. if len(tuples) > 1 {
  321. for _, tuple := range tuples { //TODO support key without modifier?
  322. v, ok := getTupleValue(tuple, key, isVal)
  323. if ok {
  324. return v, ok
  325. }
  326. }
  327. conf.Log.Debugf("Wrong key: %s not found", key)
  328. return nil, false
  329. } else {
  330. return getTupleValue(tuples[0], key, isVal)
  331. }
  332. } else {
  333. //TODO should use hash here
  334. for _, tuple := range tuples {
  335. if tuple.GetEmitter() == table {
  336. return getTupleValue(tuple, key, isVal)
  337. }
  338. }
  339. return nil, false
  340. }
  341. }
  342. func getTupleValue(tuple Row, key string, isVal bool) (interface{}, bool) {
  343. if isVal {
  344. return tuple.Value(key, "")
  345. } else {
  346. return tuple.Meta(key, "")
  347. }
  348. }
  349. func (jt *JoinTuple) GetEmitter() string {
  350. return "$$JOIN"
  351. }
  352. func (jt *JoinTuple) Value(key, table string) (interface{}, bool) {
  353. r, ok := jt.AffiliateRow.Value(key, table)
  354. if ok {
  355. return r, ok
  356. }
  357. return jt.doGetValue(key, table, true)
  358. }
  359. func (jt *JoinTuple) Meta(key, table string) (interface{}, bool) {
  360. return jt.doGetValue(key, table, false)
  361. }
  362. func (jt *JoinTuple) All(stream string) (Message, bool) {
  363. if stream != "" {
  364. for _, t := range jt.Tuples {
  365. if t.GetEmitter() == stream {
  366. return t.ToMap(), true
  367. }
  368. }
  369. } else {
  370. return jt.ToMap(), true
  371. }
  372. return nil, false
  373. }
  374. func (jt *JoinTuple) Clone() TupleRow {
  375. ts := make([]TupleRow, len(jt.Tuples))
  376. for i, t := range jt.Tuples {
  377. ts[i] = t.Clone()
  378. }
  379. c := &JoinTuple{
  380. Tuples: ts,
  381. AffiliateRow: jt.AffiliateRow.Clone(),
  382. }
  383. return c
  384. }
  385. func (jt *JoinTuple) ToMap() map[string]interface{} {
  386. if jt.cachedMap == nil { // clone the message
  387. m := make(map[string]interface{})
  388. for i := len(jt.Tuples) - 1; i >= 0; i-- {
  389. for k, v := range jt.Tuples[i].ToMap() {
  390. m[k] = v
  391. }
  392. }
  393. jt.cachedMap = m
  394. }
  395. jt.AffiliateRow.MergeMap(jt.cachedMap)
  396. return jt.cachedMap
  397. }
  398. func (jt *JoinTuple) Pick(allWildcard bool, cols [][]string, wildcardEmitters map[string]bool) {
  399. if !allWildcard {
  400. if len(cols) > 0 {
  401. for _, tuple := range jt.Tuples {
  402. if _, ok := wildcardEmitters[tuple.GetEmitter()]; ok {
  403. continue
  404. }
  405. tuple.Pick(allWildcard, cols, wildcardEmitters)
  406. }
  407. } else {
  408. jt.Tuples = jt.Tuples[:0]
  409. }
  410. }
  411. jt.cachedMap = nil
  412. jt.AffiliateRow.Reset()
  413. }
  414. // GroupedTuple implementation
  415. func (s *GroupedTuples) AggregateEval(expr ast.Expr, v CallValuer) []interface{} {
  416. var result []interface{}
  417. for _, t := range s.Content {
  418. result = append(result, Eval(expr, MultiValuer(t, &WindowRangeValuer{WindowRange: s.WindowRange}, v, &WildcardValuer{t})))
  419. }
  420. return result
  421. }
  422. func (s *GroupedTuples) Value(key, table string) (interface{}, bool) {
  423. r, ok := s.AffiliateRow.Value(key, table)
  424. if ok {
  425. return r, ok
  426. }
  427. return s.Content[0].Value(key, table)
  428. }
  429. func (s *GroupedTuples) Meta(key, table string) (interface{}, bool) {
  430. return s.Content[0].Meta(key, table)
  431. }
  432. func (s *GroupedTuples) All(_ string) (Message, bool) {
  433. return s.ToMap(), true
  434. }
  435. func (s *GroupedTuples) ToMap() map[string]interface{} {
  436. if s.cachedMap == nil {
  437. m := make(map[string]interface{})
  438. for k, v := range s.Content[0].ToMap() {
  439. m[k] = v
  440. }
  441. s.cachedMap = m
  442. }
  443. s.AffiliateRow.MergeMap(s.cachedMap)
  444. return s.cachedMap
  445. }
  446. func (s *GroupedTuples) Clone() CollectionRow {
  447. ts := make([]TupleRow, len(s.Content))
  448. for i, t := range s.Content {
  449. ts[i] = t
  450. }
  451. c := &GroupedTuples{
  452. Content: ts,
  453. WindowRange: s.WindowRange,
  454. AffiliateRow: s.AffiliateRow.Clone(),
  455. }
  456. return c
  457. }
  458. func (s *GroupedTuples) Pick(allWildcard bool, cols [][]string, wildcardEmitters map[string]bool) {
  459. s.Content[0].Pick(allWildcard, cols, wildcardEmitters)
  460. s.AffiliateRow.Reset()
  461. }