lookupPlan_test.go 6.5 KB

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