lookupPlan_test.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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 planner
  15. import (
  16. "fmt"
  17. "github.com/lf-edge/ekuiper/pkg/ast"
  18. "reflect"
  19. "testing"
  20. )
  21. func TestValidate(t *testing.T) {
  22. var tests = []struct {
  23. p *LookupPlan
  24. v bool
  25. c ast.Expr
  26. k []string
  27. vv []ast.Expr
  28. }{
  29. { // 0
  30. p: &LookupPlan{
  31. joinExpr: ast.Join{
  32. Name: "good",
  33. JoinType: 0,
  34. Expr: &ast.BinaryExpr{
  35. OP: ast.EQ,
  36. LHS: &ast.FieldRef{
  37. StreamName: "left",
  38. Name: "device_id",
  39. },
  40. RHS: &ast.FieldRef{
  41. StreamName: "good",
  42. Name: "id",
  43. },
  44. },
  45. },
  46. },
  47. v: true,
  48. k: []string{
  49. "id",
  50. },
  51. vv: []ast.Expr{
  52. &ast.FieldRef{
  53. StreamName: "left",
  54. Name: "device_id",
  55. },
  56. },
  57. c: nil,
  58. }, { // 1
  59. p: &LookupPlan{
  60. joinExpr: ast.Join{
  61. Name: "good",
  62. JoinType: 0,
  63. Expr: &ast.BinaryExpr{
  64. OP: ast.GT,
  65. LHS: &ast.FieldRef{
  66. StreamName: "left",
  67. Name: "device_id",
  68. },
  69. RHS: &ast.FieldRef{
  70. StreamName: "good",
  71. Name: "id",
  72. },
  73. },
  74. },
  75. },
  76. v: false,
  77. c: nil,
  78. }, { // 2
  79. p: &LookupPlan{
  80. joinExpr: ast.Join{
  81. Name: "good",
  82. JoinType: 0,
  83. Expr: &ast.BinaryExpr{
  84. OP: ast.EQ,
  85. LHS: &ast.FieldRef{
  86. StreamName: "left",
  87. Name: "device_id",
  88. },
  89. RHS: &ast.IntegerLiteral{Val: 23},
  90. },
  91. },
  92. },
  93. v: false,
  94. c: nil,
  95. }, { // 3
  96. p: &LookupPlan{
  97. joinExpr: ast.Join{
  98. Name: "good",
  99. JoinType: 0,
  100. Expr: &ast.BinaryExpr{
  101. OP: ast.OR,
  102. LHS: &ast.BinaryExpr{
  103. OP: ast.EQ,
  104. LHS: &ast.FieldRef{
  105. StreamName: "left",
  106. Name: "device_id",
  107. },
  108. RHS: &ast.FieldRef{
  109. StreamName: "good",
  110. Name: "id",
  111. },
  112. },
  113. RHS: &ast.BinaryExpr{
  114. OP: ast.EQ,
  115. LHS: &ast.FieldRef{
  116. StreamName: "left",
  117. Name: "device_id",
  118. },
  119. RHS: &ast.FieldRef{
  120. StreamName: "good",
  121. Name: "id1",
  122. },
  123. },
  124. },
  125. },
  126. },
  127. v: false,
  128. c: nil,
  129. }, { // 4
  130. p: &LookupPlan{
  131. joinExpr: ast.Join{
  132. Name: "good",
  133. JoinType: 0,
  134. Expr: &ast.BinaryExpr{
  135. OP: ast.AND,
  136. LHS: &ast.BinaryExpr{
  137. OP: ast.EQ,
  138. LHS: &ast.FieldRef{
  139. StreamName: "left",
  140. Name: "device_id",
  141. },
  142. RHS: &ast.FieldRef{
  143. StreamName: "good",
  144. Name: "id",
  145. },
  146. },
  147. RHS: &ast.BinaryExpr{
  148. OP: ast.EQ,
  149. LHS: &ast.FieldRef{
  150. StreamName: "left",
  151. Name: "device_id",
  152. },
  153. RHS: &ast.FieldRef{
  154. StreamName: "good",
  155. Name: "id1",
  156. },
  157. },
  158. },
  159. },
  160. },
  161. v: true,
  162. k: []string{
  163. "id", "id1",
  164. },
  165. vv: []ast.Expr{
  166. &ast.FieldRef{
  167. StreamName: "left",
  168. Name: "device_id",
  169. },
  170. &ast.FieldRef{
  171. StreamName: "left",
  172. Name: "device_id",
  173. },
  174. },
  175. c: nil,
  176. }, { // 5
  177. p: &LookupPlan{
  178. joinExpr: ast.Join{
  179. Name: "good",
  180. JoinType: 0,
  181. Expr: &ast.BinaryExpr{
  182. OP: ast.AND,
  183. LHS: &ast.BinaryExpr{
  184. OP: ast.GT,
  185. LHS: &ast.FieldRef{
  186. StreamName: "left",
  187. Name: "device_id",
  188. },
  189. RHS: &ast.IntegerLiteral{
  190. Val: 33,
  191. },
  192. },
  193. RHS: &ast.BinaryExpr{
  194. OP: ast.EQ,
  195. LHS: &ast.FieldRef{
  196. StreamName: "left",
  197. Name: "device_id",
  198. },
  199. RHS: &ast.FieldRef{
  200. StreamName: "good",
  201. Name: "id1",
  202. },
  203. },
  204. },
  205. },
  206. },
  207. v: true,
  208. k: []string{
  209. "id1",
  210. },
  211. vv: []ast.Expr{
  212. &ast.FieldRef{
  213. StreamName: "left",
  214. Name: "device_id",
  215. },
  216. },
  217. c: &ast.BinaryExpr{
  218. OP: ast.GT,
  219. LHS: &ast.FieldRef{
  220. StreamName: "left",
  221. Name: "device_id",
  222. },
  223. RHS: &ast.IntegerLiteral{
  224. Val: 33,
  225. },
  226. },
  227. }, { // 6
  228. p: &LookupPlan{
  229. joinExpr: ast.Join{
  230. Name: "good",
  231. JoinType: 0,
  232. Expr: &ast.BinaryExpr{
  233. OP: ast.AND,
  234. LHS: &ast.BinaryExpr{
  235. OP: ast.EQ,
  236. LHS: &ast.FieldRef{
  237. StreamName: "left",
  238. Name: "device_id",
  239. },
  240. RHS: &ast.FieldRef{
  241. StreamName: "good",
  242. Name: "id",
  243. },
  244. },
  245. RHS: &ast.BinaryExpr{
  246. OP: ast.EQ,
  247. LHS: &ast.FieldRef{
  248. StreamName: "left",
  249. Name: "device_id",
  250. },
  251. RHS: &ast.FieldRef{
  252. StreamName: "good",
  253. Name: "id",
  254. },
  255. },
  256. },
  257. },
  258. },
  259. v: false,
  260. }, { // 7
  261. p: &LookupPlan{
  262. joinExpr: ast.Join{
  263. Name: "good",
  264. JoinType: 0,
  265. Expr: &ast.BinaryExpr{
  266. OP: ast.AND,
  267. LHS: &ast.BinaryExpr{
  268. OP: ast.EQ,
  269. LHS: &ast.FieldRef{
  270. StreamName: "left",
  271. Name: "device_id",
  272. },
  273. RHS: &ast.FieldRef{
  274. StreamName: "right",
  275. Name: "id",
  276. },
  277. },
  278. RHS: &ast.BinaryExpr{
  279. OP: ast.EQ,
  280. LHS: &ast.FieldRef{
  281. StreamName: "left",
  282. Name: "device_id",
  283. },
  284. RHS: &ast.FieldRef{
  285. StreamName: "right",
  286. Name: "id2",
  287. },
  288. },
  289. },
  290. },
  291. },
  292. v: false,
  293. },
  294. }
  295. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  296. for i, tt := range tests {
  297. rv := tt.p.validateAndExtractCondition()
  298. if rv != tt.v {
  299. t.Errorf("case %d: expect validate %v but got %v", i, tt.v, rv)
  300. continue
  301. }
  302. if rv {
  303. if !reflect.DeepEqual(tt.c, tt.p.conditions) {
  304. t.Errorf("case %d: expect conditions %v but got %v", i, tt.c, tt.p.conditions)
  305. continue
  306. }
  307. if !reflect.DeepEqual(tt.k, tt.p.keys) {
  308. t.Errorf("case %d: expect keys %v but got %v", i, tt.k, tt.p.keys)
  309. continue
  310. }
  311. if !reflect.DeepEqual(tt.vv, tt.p.valvars) {
  312. t.Errorf("case %d: expect val vars %v but got %v", i, tt.vv, tt.p.valvars)
  313. }
  314. }
  315. }
  316. }