join_operator.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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 operator
  15. import (
  16. "fmt"
  17. "github.com/lf-edge/ekuiper/internal/xsql"
  18. "github.com/lf-edge/ekuiper/pkg/api"
  19. "github.com/lf-edge/ekuiper/pkg/ast"
  20. )
  21. //TODO join expr should only be the equal op between 2 streams like tb1.id = tb2.id
  22. type JoinOp struct {
  23. From *ast.Table
  24. Joins ast.Joins
  25. }
  26. // input: xsql.WindowTuplesSet from windowOp, window is required for join
  27. // output: xsql.JoinTupleSets
  28. func (jp *JoinOp) Apply(ctx api.StreamContext, data interface{}, fv *xsql.FunctionValuer, _ *xsql.AggregateFunctionValuer) interface{} {
  29. log := ctx.GetLogger()
  30. var input xsql.WindowTuplesSet
  31. switch v := data.(type) {
  32. case error:
  33. return v
  34. case xsql.WindowTuplesSet:
  35. input = v
  36. log.Debugf("join plan receive %v", data)
  37. default:
  38. return fmt.Errorf("run Join error: join is only supported in window")
  39. }
  40. result := &xsql.JoinTupleSets{Content: make([]xsql.JoinTuple, 0)}
  41. for i, join := range jp.Joins {
  42. if i == 0 {
  43. v, err := jp.evalSet(input, join, fv)
  44. if err != nil {
  45. return fmt.Errorf("run Join error: %s", err)
  46. }
  47. result = v
  48. } else {
  49. r1, err := jp.evalJoinSets(result, input, join, fv)
  50. if err != nil {
  51. return fmt.Errorf("run Join error: %s", err)
  52. }
  53. if v1, ok := r1.(*xsql.JoinTupleSets); ok {
  54. result = v1
  55. }
  56. }
  57. }
  58. if result.Len() <= 0 {
  59. log.Debugf("join plan yields nothing")
  60. return nil
  61. }
  62. result.WindowRange = input.WindowRange
  63. return result
  64. }
  65. func (jp *JoinOp) getStreamNames(join *ast.Join) ([]string, error) {
  66. var srcs []string
  67. keys := make(map[ast.StreamName]bool)
  68. ast.WalkFunc(join, func(node ast.Node) bool {
  69. if f, ok := node.(*ast.FieldRef); ok {
  70. for _, v := range f.RefSources() {
  71. // Exclude default stream as it is a virtual stream name.
  72. if v == ast.DefaultStream {
  73. continue
  74. }
  75. if _, ok := keys[v]; !ok {
  76. srcs = append(srcs, string(v))
  77. keys[v] = true
  78. }
  79. }
  80. }
  81. return true
  82. })
  83. if len(srcs) != 2 {
  84. if jp.From.Alias != "" {
  85. srcs = append(srcs, jp.From.Alias)
  86. } else {
  87. srcs = append(srcs, jp.From.Name)
  88. }
  89. if join.Alias != "" {
  90. srcs = append(srcs, join.Alias)
  91. } else {
  92. srcs = append(srcs, join.Name)
  93. }
  94. }
  95. return srcs, nil
  96. }
  97. func (jp *JoinOp) evalSet(input xsql.WindowTuplesSet, join ast.Join, fv *xsql.FunctionValuer) (*xsql.JoinTupleSets, error) {
  98. var leftStream, rightStream string
  99. if join.JoinType != ast.CROSS_JOIN {
  100. streams, err := jp.getStreamNames(&join)
  101. if err != nil {
  102. return nil, err
  103. }
  104. leftStream = streams[0]
  105. rightStream = streams[1]
  106. } else {
  107. if jp.From.Alias == "" {
  108. leftStream = jp.From.Name
  109. } else {
  110. leftStream = jp.From.Alias
  111. }
  112. if join.Alias == "" {
  113. rightStream = join.Name
  114. } else {
  115. rightStream = join.Alias
  116. }
  117. }
  118. var lefts, rights []xsql.Tuple
  119. lefts = input.GetBySrc(leftStream)
  120. rights = input.GetBySrc(rightStream)
  121. sets := &xsql.JoinTupleSets{Content: make([]xsql.JoinTuple, 0)}
  122. if join.JoinType == ast.RIGHT_JOIN {
  123. return jp.evalSetWithRightJoin(input, join, false, fv)
  124. }
  125. for _, left := range lefts {
  126. leftJoined := false
  127. for index, right := range rights {
  128. tupleJoined := false
  129. merged := &xsql.JoinTuple{}
  130. if join.JoinType == ast.LEFT_JOIN || join.JoinType == ast.FULL_JOIN || join.JoinType == ast.CROSS_JOIN {
  131. merged.AddTuple(left)
  132. }
  133. if join.JoinType == ast.CROSS_JOIN {
  134. tupleJoined = true
  135. merged.AddTuple(right)
  136. } else {
  137. temp := &xsql.JoinTuple{}
  138. temp.AddTuple(left)
  139. temp.AddTuple(right)
  140. ve := &xsql.ValuerEval{Valuer: xsql.MultiValuer(temp, fv)}
  141. result := evalOn(join, ve, &left, &right)
  142. merged.AliasMap = temp.AliasMap
  143. switch val := result.(type) {
  144. case error:
  145. return nil, val
  146. case bool:
  147. if val {
  148. leftJoined = true
  149. tupleJoined = true
  150. if join.JoinType == ast.INNER_JOIN {
  151. merged.AddTuple(left)
  152. merged.AddTuple(right)
  153. } else {
  154. merged.AddTuple(right)
  155. }
  156. }
  157. default:
  158. return nil, fmt.Errorf("invalid join condition that returns non-bool value %[1]T(%[1]v)", val)
  159. }
  160. }
  161. if tupleJoined || (!leftJoined && index == len(rights)-1 && len(merged.Tuples) > 0) {
  162. leftJoined = true
  163. sets.Content = append(sets.Content, *merged)
  164. }
  165. }
  166. // If no messages in the right
  167. if !leftJoined && join.JoinType != ast.INNER_JOIN && join.JoinType != ast.CROSS_JOIN {
  168. merged := &xsql.JoinTuple{}
  169. merged.AddTuple(left)
  170. sets.Content = append(sets.Content, *merged)
  171. }
  172. }
  173. if join.JoinType == ast.FULL_JOIN {
  174. if rightJoinSet, err := jp.evalSetWithRightJoin(input, join, true, fv); err == nil {
  175. if len(rightJoinSet.Content) > 0 {
  176. for _, jt := range rightJoinSet.Content {
  177. sets.Content = append(sets.Content, jt)
  178. }
  179. }
  180. } else {
  181. return nil, err
  182. }
  183. }
  184. return sets, nil
  185. }
  186. func evalOn(join ast.Join, ve *xsql.ValuerEval, left interface{}, right *xsql.Tuple) interface{} {
  187. var result interface{}
  188. if join.Expr != nil {
  189. result = ve.Eval(join.Expr)
  190. } else if join.JoinType == ast.INNER_JOIN { // if no on expression
  191. result = left != nil && right != nil
  192. } else {
  193. result = true
  194. }
  195. return result
  196. }
  197. func (jp *JoinOp) evalSetWithRightJoin(input xsql.WindowTuplesSet, join ast.Join, excludeJoint bool, fv *xsql.FunctionValuer) (*xsql.JoinTupleSets, error) {
  198. streams, err := jp.getStreamNames(&join)
  199. if err != nil {
  200. return nil, err
  201. }
  202. leftStream := streams[0]
  203. rightStream := streams[1]
  204. var lefts, rights []xsql.Tuple
  205. lefts = input.GetBySrc(leftStream)
  206. rights = input.GetBySrc(rightStream)
  207. sets := &xsql.JoinTupleSets{Content: make([]xsql.JoinTuple, 0)}
  208. for _, right := range rights {
  209. isJoint := false
  210. for index, left := range lefts {
  211. tupleJoined := false
  212. merged := &xsql.JoinTuple{}
  213. merged.AddTuple(right)
  214. temp := &xsql.JoinTuple{}
  215. temp.AddTuple(right)
  216. temp.AddTuple(left)
  217. ve := &xsql.ValuerEval{Valuer: xsql.MultiValuer(temp, fv)}
  218. result := evalOn(join, ve, &left, &right)
  219. merged.AliasMap = temp.AliasMap
  220. switch val := result.(type) {
  221. case error:
  222. return nil, val
  223. case bool:
  224. if val {
  225. merged.AddTuple(left)
  226. isJoint = true
  227. tupleJoined = true
  228. }
  229. default:
  230. return nil, fmt.Errorf("invalid join condition that returns non-bool value %[1]T(%[1]v)", val)
  231. }
  232. if !excludeJoint && (tupleJoined || (!isJoint && index == len(lefts)-1 && len(merged.Tuples) > 0)) {
  233. isJoint = true
  234. sets.Content = append(sets.Content, *merged)
  235. }
  236. }
  237. if !isJoint {
  238. merged := &xsql.JoinTuple{}
  239. merged.AddTuple(right)
  240. sets.Content = append(sets.Content, *merged)
  241. }
  242. }
  243. return sets, nil
  244. }
  245. func (jp *JoinOp) evalJoinSets(set *xsql.JoinTupleSets, input xsql.WindowTuplesSet, join ast.Join, fv *xsql.FunctionValuer) (interface{}, error) {
  246. var rightStream string
  247. if join.Alias == "" {
  248. rightStream = join.Name
  249. } else {
  250. rightStream = join.Alias
  251. }
  252. rights := input.GetBySrc(rightStream)
  253. newSets := &xsql.JoinTupleSets{Content: make([]xsql.JoinTuple, 0)}
  254. if join.JoinType == ast.RIGHT_JOIN {
  255. return jp.evalRightJoinSets(set, input, join, false, fv)
  256. }
  257. for _, left := range set.Content {
  258. leftJoined := false
  259. for index, right := range rights {
  260. tupleJoined := false
  261. merged := &xsql.JoinTuple{}
  262. if join.JoinType == ast.LEFT_JOIN || join.JoinType == ast.FULL_JOIN || join.JoinType == ast.CROSS_JOIN {
  263. merged.AddTuples(left.Tuples)
  264. }
  265. if join.JoinType == ast.CROSS_JOIN {
  266. tupleJoined = true
  267. merged.AddTuple(right)
  268. } else {
  269. temp := &xsql.JoinTuple{}
  270. temp.AddTuples(left.Tuples)
  271. temp.AddTuple(right)
  272. ve := &xsql.ValuerEval{Valuer: xsql.MultiValuer(temp, fv)}
  273. result := evalOn(join, ve, &left, &right)
  274. merged.AliasMap = left.AliasMap
  275. switch val := result.(type) {
  276. case error:
  277. return nil, val
  278. case bool:
  279. if val {
  280. leftJoined = true
  281. tupleJoined = true
  282. if join.JoinType == ast.INNER_JOIN {
  283. merged.AddTuples(left.Tuples)
  284. }
  285. merged.AddTuple(right)
  286. }
  287. default:
  288. return nil, fmt.Errorf("invalid join condition that returns non-bool value %[1]T(%[1]v)", val)
  289. }
  290. }
  291. if tupleJoined || (!leftJoined && index == len(rights)-1 && len(merged.Tuples) > 0) {
  292. leftJoined = true
  293. newSets.Content = append(newSets.Content, *merged)
  294. }
  295. }
  296. if !leftJoined && join.JoinType != ast.INNER_JOIN && join.JoinType != ast.CROSS_JOIN {
  297. merged := &xsql.JoinTuple{}
  298. merged.AddTuples(left.Tuples)
  299. newSets.Content = append(newSets.Content, *merged)
  300. }
  301. }
  302. if join.JoinType == ast.FULL_JOIN {
  303. if rightJoinSet, err := jp.evalRightJoinSets(set, input, join, true, fv); err == nil && len(rightJoinSet.Content) > 0 {
  304. for _, jt := range rightJoinSet.Content {
  305. newSets.Content = append(newSets.Content, jt)
  306. }
  307. }
  308. }
  309. return newSets, nil
  310. }
  311. func (jp *JoinOp) evalRightJoinSets(set *xsql.JoinTupleSets, input xsql.WindowTuplesSet, join ast.Join, excludeJoint bool, fv *xsql.FunctionValuer) (*xsql.JoinTupleSets, error) {
  312. var rightStream string
  313. if join.Alias == "" {
  314. rightStream = join.Name
  315. } else {
  316. rightStream = join.Alias
  317. }
  318. rights := input.GetBySrc(rightStream)
  319. newSets := &xsql.JoinTupleSets{Content: make([]xsql.JoinTuple, 0)}
  320. for _, right := range rights {
  321. isJoint := false
  322. for index, left := range set.Content {
  323. tupleJoined := false
  324. merged := &xsql.JoinTuple{}
  325. merged.AddTuple(right)
  326. temp := &xsql.JoinTuple{}
  327. temp.AddTuples(left.Tuples)
  328. temp.AddTuple(right)
  329. ve := &xsql.ValuerEval{Valuer: xsql.MultiValuer(temp, fv)}
  330. result := evalOn(join, ve, &left, &right)
  331. merged.AliasMap = left.AliasMap
  332. switch val := result.(type) {
  333. case error:
  334. return nil, val
  335. case bool:
  336. if val {
  337. isJoint = true
  338. tupleJoined = true
  339. merged.AddTuples(left.Tuples)
  340. }
  341. default:
  342. return nil, fmt.Errorf("invalid join condition that returns non-bool value %[1]T(%[1]v)", val)
  343. }
  344. if !excludeJoint && (tupleJoined || (!isJoint && index == len(set.Content)-1 && len(merged.Tuples) > 0)) {
  345. isJoint = true
  346. newSets.Content = append(newSets.Content, *merged)
  347. }
  348. }
  349. if !isJoint {
  350. merged := &xsql.JoinTuple{}
  351. merged.AddTuple(right)
  352. newSets.Content = append(newSets.Content, *merged)
  353. }
  354. }
  355. return newSets, nil
  356. }