valuer_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. // Copyright 2021-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. "errors"
  17. "fmt"
  18. "reflect"
  19. "strings"
  20. "testing"
  21. "github.com/lf-edge/ekuiper/internal/conf"
  22. "github.com/lf-edge/ekuiper/pkg/ast"
  23. "github.com/lf-edge/ekuiper/pkg/cast"
  24. )
  25. func TestComparison(t *testing.T) {
  26. testTime, _ := cast.InterfaceToTime(1541152488442, "")
  27. data := []struct {
  28. m Message
  29. r []interface{}
  30. }{
  31. { // 0
  32. m: map[string]interface{}{
  33. "a": float64(32),
  34. "b": float64(72),
  35. },
  36. r: []interface{}{
  37. false, true, errors.New("invalid operation float64(32) = string(string literal)"),
  38. false, true, false, true, true, false,
  39. },
  40. }, { // 1
  41. m: map[string]interface{}{
  42. "a": int64(32),
  43. "b": int64(72),
  44. },
  45. r: []interface{}{
  46. false, true, errors.New("invalid operation int64(32) = string(string literal)"),
  47. false, true, false, true, true, false,
  48. },
  49. }, { // 2
  50. m: map[string]interface{}{
  51. "a": "32",
  52. "b": "72",
  53. },
  54. r: []interface{}{
  55. errors.New("invalid operation string(32) > int64(72)"), errors.New("invalid operation string(32) <= int64(32)"), false,
  56. false, true, false, true, errors.New("between operator cannot compare string(32) and int(30)"), errors.New("between operator cannot compare string(32) and int(2)"),
  57. },
  58. }, { // 3
  59. m: map[string]interface{}{
  60. "a": []interface{}{32, 72},
  61. "b": []interface{}{32, 72},
  62. },
  63. r: []interface{}{
  64. errors.New("> is an invalid operation for []interface {}"), errors.New("<= is an invalid operation for []interface {}"), errors.New("= is an invalid operation for []interface {}"),
  65. errors.New(">= is an invalid operation for []interface {}"), errors.New("< is an invalid operation for []interface {}"), errors.New("= is an invalid operation for []interface {}"), errors.New("!= is an invalid operation for []interface {}"), errors.New("BETWEEN is an invalid operation for []interface {}"), errors.New("NOT BETWEEN is an invalid operation for []interface {}"),
  66. },
  67. }, { // 4
  68. m: map[string]interface{}{
  69. "a": map[string]interface{}{"c": 5},
  70. "b": map[string]interface{}{"d": 5},
  71. },
  72. r: []interface{}{
  73. errors.New("> is an invalid operation for map[string]interface {}"), errors.New("<= is an invalid operation for map[string]interface {}"), errors.New("= is an invalid operation for map[string]interface {}"),
  74. errors.New(">= is an invalid operation for map[string]interface {}"), errors.New("< is an invalid operation for map[string]interface {}"), errors.New("= is an invalid operation for map[string]interface {}"), errors.New("!= is an invalid operation for map[string]interface {}"), errors.New("BETWEEN is an invalid operation for map[string]interface {}"), errors.New("NOT BETWEEN is an invalid operation for map[string]interface {}"),
  75. },
  76. }, { // 5
  77. m: map[string]interface{}{
  78. "a": float64(55),
  79. "b": int64(55),
  80. },
  81. r: []interface{}{
  82. false, false, errors.New("invalid operation float64(55) = string(string literal)"),
  83. true, false, true, false, true, false,
  84. },
  85. }, { // 6
  86. m: map[string]interface{}{
  87. "a": testTime,
  88. "b": int64(1541152388442),
  89. },
  90. r: []interface{}{
  91. true, false, errors.New("invalid operation time.Time(2018-11-02 09:54:48.442 +0000 UTC) = string(string literal)"),
  92. true, false, false, true, false, true,
  93. },
  94. }, { // 7
  95. m: map[string]interface{}{
  96. "a": testTime,
  97. "b": "2020-02-26T02:37:21.822Z",
  98. },
  99. r: []interface{}{
  100. true, false, errors.New("invalid operation time.Time(2018-11-02 09:54:48.442 +0000 UTC) = string(string literal)"),
  101. false, true, false, true, false, false,
  102. },
  103. }, { // 8
  104. m: map[string]interface{}{
  105. "a": int64(1541152388442),
  106. "b": testTime,
  107. },
  108. r: []interface{}{
  109. true, false, errors.New("invalid operation int64(1541152388442) = string(string literal)"),
  110. errors.New("invalid operation int64(1541152388442) >= time.Time(2018-11-02 09:54:48.442 +0000 UTC)"), errors.New("invalid operation int64(1541152388442) < time.Time(2018-11-02 09:54:48.442 +0000 UTC)"), errors.New("invalid operation int64(1541152388442) = time.Time(2018-11-02 09:54:48.442 +0000 UTC)"), errors.New("invalid operation int64(1541152388442) != time.Time(2018-11-02 09:54:48.442 +0000 UTC)"), false, errors.New("between operator cannot compare int64(1541152388442) and time.Time(2018-11-02 09:54:48.442 +0000 UTC)"),
  111. },
  112. }, { // 9
  113. m: map[string]interface{}{
  114. "a": "2020-02-26T02:37:21.822Z",
  115. "b": testTime,
  116. },
  117. r: []interface{}{
  118. errors.New("invalid operation string(2020-02-26T02:37:21.822Z) > int64(72)"), errors.New("invalid operation string(2020-02-26T02:37:21.822Z) <= int64(32)"), false,
  119. errors.New("invalid operation string(2020-02-26T02:37:21.822Z) >= time.Time(2018-11-02 09:54:48.442 +0000 UTC)"), errors.New("invalid operation string(2020-02-26T02:37:21.822Z) < time.Time(2018-11-02 09:54:48.442 +0000 UTC)"), errors.New("invalid operation string(2020-02-26T02:37:21.822Z) = time.Time(2018-11-02 09:54:48.442 +0000 UTC)"), errors.New("invalid operation string(2020-02-26T02:37:21.822Z) != time.Time(2018-11-02 09:54:48.442 +0000 UTC)"), errors.New("between operator cannot compare string(2020-02-26T02:37:21.822Z) and int(30)"), errors.New("between operator cannot compare string(2020-02-26T02:37:21.822Z) and int(2)"),
  120. },
  121. }, { // 10
  122. m: map[string]interface{}{
  123. "c": "nothing",
  124. },
  125. r: []interface{}{
  126. false, false, false,
  127. true, false, true, false, false, true,
  128. },
  129. }, { // 11
  130. m: map[string]interface{}{
  131. "a": 12,
  132. "c": "nothing",
  133. },
  134. r: []interface{}{
  135. false, true, errors.New("invalid operation int64(12) = string(string literal)"),
  136. false, false, false, true, false, true,
  137. },
  138. },
  139. }
  140. sqls := []string{
  141. "select * from src where a > 72",
  142. "select * from src where a <= 32",
  143. "select * from src where a = \"string literal\"",
  144. "select * from src where a >= b",
  145. "select * from src where a < b",
  146. "select * from src where a = b",
  147. "select * from src where a != b",
  148. "select * from src where a between 30 and 100",
  149. "select * from src where a not between 2 and b",
  150. }
  151. var conditions []ast.Expr
  152. for _, sql := range sqls {
  153. stmt, _ := NewParser(strings.NewReader(sql)).Parse()
  154. conditions = append(conditions, stmt.Condition)
  155. }
  156. fmt.Printf("The test bucket size is %d.\n\n", len(data)*len(sqls))
  157. for i, tt := range data {
  158. for j, c := range conditions {
  159. tuple := &Tuple{Emitter: "src", Message: tt.m, Timestamp: conf.GetNowInMilli(), Metadata: nil}
  160. ve := &ValuerEval{Valuer: MultiValuer(tuple)}
  161. result := ve.Eval(c)
  162. if !reflect.DeepEqual(tt.r[j], result) {
  163. t.Errorf("%d-%d. \nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, j, tt.r[j], result)
  164. }
  165. }
  166. }
  167. }
  168. func TestCalculation(t *testing.T) {
  169. data := []struct {
  170. m Message
  171. r []interface{}
  172. }{
  173. {
  174. m: map[string]interface{}{
  175. "a": float64(32),
  176. "b": float64(72),
  177. },
  178. r: []interface{}{
  179. float64(104), float64(96), float64(0.4444444444444444), float64(32),
  180. },
  181. }, {
  182. m: map[string]interface{}{
  183. "a": int64(32),
  184. "b": int64(72),
  185. },
  186. r: []interface{}{
  187. int64(104), int64(96), int64(0), int64(32),
  188. },
  189. }, {
  190. m: map[string]interface{}{
  191. "a": "32",
  192. "b": "72",
  193. },
  194. r: []interface{}{
  195. errors.New("invalid operation string(32) + string(72)"), errors.New("invalid operation string(32) * int64(3)"),
  196. errors.New("invalid operation string(32) / string(72)"), errors.New("invalid operation string(32) % string(72)"),
  197. },
  198. }, {
  199. m: map[string]interface{}{
  200. "a": float64(55),
  201. "b": int64(55),
  202. },
  203. r: []interface{}{
  204. float64(110), float64(165), float64(1), float64(0),
  205. },
  206. }, {
  207. m: map[string]interface{}{
  208. "a": int64(55),
  209. "b": float64(0),
  210. },
  211. r: []interface{}{
  212. float64(55), int64(165), errors.New("divided by zero"), errors.New("divided by zero"),
  213. },
  214. }, {
  215. m: map[string]interface{}{
  216. "c": "nothing",
  217. },
  218. r: []interface{}{
  219. nil, nil, nil, nil,
  220. },
  221. }, {
  222. m: map[string]interface{}{
  223. "a": 12,
  224. "c": "nothing",
  225. },
  226. r: []interface{}{
  227. nil, int64(36), nil, nil,
  228. },
  229. },
  230. }
  231. sqls := []string{
  232. "select a + b as t from src",
  233. "select a * 3 as t from src",
  234. "select a / b as t from src",
  235. "select a % b as t from src",
  236. }
  237. var projects []ast.Expr
  238. for _, sql := range sqls {
  239. stmt, _ := NewParser(strings.NewReader(sql)).Parse()
  240. projects = append(projects, stmt.Fields[0].Expr)
  241. }
  242. fmt.Printf("The test bucket size is %d.\n\n", len(data)*len(sqls))
  243. for i, tt := range data {
  244. for j, c := range projects {
  245. tuple := &Tuple{Emitter: "src", Message: tt.m, Timestamp: conf.GetNowInMilli(), Metadata: nil}
  246. ve := &ValuerEval{Valuer: MultiValuer(tuple)}
  247. result := ve.Eval(c)
  248. if !reflect.DeepEqual(tt.r[j], result) {
  249. t.Errorf("%d-%d. \nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, j, tt.r[j], result)
  250. }
  251. }
  252. }
  253. }
  254. func TestCase(t *testing.T) {
  255. data := []struct {
  256. m Message
  257. r []interface{}
  258. }{
  259. {
  260. m: map[string]interface{}{
  261. "a": float64(32),
  262. "b": float64(72),
  263. },
  264. r: []interface{}{
  265. 1, 0, 0, 1,
  266. },
  267. }, {
  268. m: map[string]interface{}{
  269. "a": int64(32),
  270. "b": int64(72),
  271. },
  272. r: []interface{}{
  273. 1, 0, 0, 1,
  274. },
  275. }, {
  276. m: map[string]interface{}{
  277. "a": "32",
  278. "b": "72",
  279. },
  280. r: []interface{}{
  281. errors.New("evaluate case expression error: invalid operation string(32) = int64(32)"), errors.New("evaluate case expression error: invalid operation string(32) = int64(72)"),
  282. errors.New("evaluate case expression error: invalid operation string(32) > int64(70)"), errors.New("evaluate case expression error: invalid operation string(32) > int64(30)"),
  283. },
  284. }, {
  285. m: map[string]interface{}{
  286. "a": float64(55),
  287. "b": int64(55),
  288. },
  289. r: []interface{}{
  290. 0, nil, 0, 1,
  291. },
  292. }, {
  293. m: map[string]interface{}{
  294. "a": int64(55),
  295. "b": float64(0),
  296. },
  297. r: []interface{}{0, nil, 0, 1},
  298. }, {
  299. m: map[string]interface{}{
  300. "c": "nothing",
  301. },
  302. r: []interface{}{
  303. 0, nil, -1, nil,
  304. },
  305. }, {
  306. m: map[string]interface{}{
  307. "a": 12,
  308. "c": "nothing",
  309. },
  310. r: []interface{}{
  311. 0, nil, -1, nil,
  312. },
  313. },
  314. }
  315. sqls := []string{
  316. "select CASE a WHEN 32 THEN 1 ELSE 0 END as t from src",
  317. "select CASE a WHEN 72 THEN 1 WHEN 32 THEN 0 END as t from src",
  318. "select CASE WHEN a > 70 THEN 1 WHEN a > 30 AND a < 70 THEN 0 ELSE -1 END as t from src",
  319. "select CASE WHEN a > 30 THEN 1 END as t from src",
  320. }
  321. var projects []ast.Expr
  322. for _, sql := range sqls {
  323. stmt, _ := NewParser(strings.NewReader(sql)).Parse()
  324. projects = append(projects, stmt.Fields[0].Expr)
  325. }
  326. fmt.Printf("The test bucket size is %d.\n\n", len(data)*len(sqls))
  327. for i, tt := range data {
  328. for j, c := range projects {
  329. tuple := &Tuple{Emitter: "src", Message: tt.m, Timestamp: conf.GetNowInMilli(), Metadata: nil}
  330. ve := &ValuerEval{Valuer: MultiValuer(tuple)}
  331. result := ve.Eval(c)
  332. if !reflect.DeepEqual(tt.r[j], result) {
  333. t.Errorf("%d-%d. \nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, j, tt.r[j], result)
  334. }
  335. }
  336. }
  337. }
  338. func TestArray(t *testing.T) {
  339. data := []struct {
  340. m Message
  341. r []interface{}
  342. }{
  343. {
  344. m: map[string]interface{}{
  345. "a": []int64{0, 1, 2, 3, 4, 5},
  346. },
  347. r: []interface{}{
  348. int64(0), int64(5), int64(1), []int64{0, 1}, []int64{4, 5}, []int64{5}, []int64{0, 1, 2, 3, 4}, []int64{1, 2, 3, 4}, []int64{0, 1, 2, 3, 4, 5},
  349. },
  350. },
  351. }
  352. sqls := []string{
  353. "select a[0] as t from src",
  354. "select a[-1] as t from src",
  355. "select a[1] as t from src",
  356. "select a[:2] as t from src",
  357. "select a[4:] as t from src",
  358. "select a[-1:] as t from src",
  359. "select a[0:-1] as t from src",
  360. "select a[-5:-1] as t from src",
  361. "select a[:] as t from src",
  362. }
  363. var projects []ast.Expr
  364. for _, sql := range sqls {
  365. stmt, _ := NewParser(strings.NewReader(sql)).Parse()
  366. projects = append(projects, stmt.Fields[0].Expr)
  367. }
  368. fmt.Printf("The test bucket size is %d.\n\n", len(data)*len(sqls))
  369. for i, tt := range data {
  370. for j, c := range projects {
  371. tuple := &Tuple{Emitter: "src", Message: tt.m, Timestamp: conf.GetNowInMilli(), Metadata: nil}
  372. ve := &ValuerEval{Valuer: MultiValuer(tuple)}
  373. result := ve.Eval(c)
  374. if !reflect.DeepEqual(tt.r[j], result) {
  375. t.Errorf("%d-%d. \nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, j, tt.r[j], result)
  376. }
  377. }
  378. }
  379. }
  380. func TestLike(t *testing.T) {
  381. data := []struct {
  382. m Message
  383. r []interface{}
  384. }{
  385. {
  386. m: map[string]interface{}{
  387. "a": "string1",
  388. "b": 12,
  389. },
  390. r: []interface{}{
  391. false, errors.New("LIKE operator left operand expects string, but found 12"), false, true, false, errors.New("invalid LIKE pattern, must be a string but got 12"),
  392. },
  393. }, {
  394. m: map[string]interface{}{
  395. "a": "string2",
  396. "b": "another",
  397. },
  398. r: []interface{}{
  399. false, true, true, true, false, false,
  400. },
  401. }, {
  402. m: map[string]interface{}{
  403. "a": `str\_ng`,
  404. "b": "str_ng",
  405. },
  406. r: []interface{}{
  407. false, false, true, true, true, false,
  408. },
  409. }, {
  410. m: map[string]interface{}{
  411. "a": `str_ng`,
  412. "b": "str_ng",
  413. },
  414. r: []interface{}{
  415. false, false, true, true, false, true,
  416. },
  417. },
  418. }
  419. sqls := []string{
  420. `select a LIKE "string" as t from src`,
  421. `select b LIKE "an_ther" from src`, //nolint:misspell
  422. `select a NOT LIKE "string1" as t from src`,
  423. `select a LIKE "str%" as t from src`,
  424. `select a LIKE "str\\_ng" as t from src`,
  425. `select a LIKE b as t from src`,
  426. }
  427. var projects []ast.Expr
  428. for _, sql := range sqls {
  429. stmt, err := NewParser(strings.NewReader(sql)).Parse()
  430. if err != nil {
  431. t.Errorf("%s: %s", sql, err)
  432. return
  433. }
  434. projects = append(projects, stmt.Fields[0].Expr)
  435. }
  436. fmt.Printf("The test bucket size is %d.\n\n", len(data)*len(sqls))
  437. for i, tt := range data {
  438. for j, c := range projects {
  439. tuple := &Tuple{Emitter: "src", Message: tt.m, Timestamp: conf.GetNowInMilli(), Metadata: nil}
  440. ve := &ValuerEval{Valuer: MultiValuer(tuple)}
  441. result := ve.Eval(c)
  442. if !reflect.DeepEqual(tt.r[j], result) {
  443. t.Errorf("%d-%s. \nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, sqls[j], tt.r[j], result)
  444. }
  445. }
  446. }
  447. }