misc_func_test.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  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 operator
  15. import (
  16. "fmt"
  17. "github.com/lf-edge/ekuiper/internal/conf"
  18. "github.com/lf-edge/ekuiper/internal/testx"
  19. "github.com/lf-edge/ekuiper/internal/topo/context"
  20. "github.com/lf-edge/ekuiper/internal/topo/state"
  21. "github.com/lf-edge/ekuiper/internal/xsql"
  22. "github.com/lf-edge/ekuiper/pkg/api"
  23. "github.com/lf-edge/ekuiper/pkg/cast"
  24. "reflect"
  25. "strconv"
  26. "strings"
  27. "testing"
  28. )
  29. func TestMiscFunc_Apply1(t *testing.T) {
  30. var tests = []struct {
  31. sql string
  32. data *xsql.Tuple
  33. result []map[string]interface{}
  34. }{
  35. {
  36. sql: "SELECT md5(a) AS a FROM test",
  37. data: &xsql.Tuple{
  38. Emitter: "test",
  39. Message: xsql.Message{
  40. "a": "The quick brown fox jumps over the lazy dog",
  41. "b": "myb",
  42. "c": "myc",
  43. },
  44. },
  45. result: []map[string]interface{}{{
  46. "a": strings.ToLower("9E107D9D372BB6826BD81D3542A419D6"),
  47. }},
  48. },
  49. {
  50. sql: "SELECT md5(d) AS a FROM test",
  51. data: &xsql.Tuple{
  52. Emitter: "test",
  53. Message: xsql.Message{
  54. "a": "The quick brown fox jumps over the lazy dog",
  55. "b": "myb",
  56. "c": "myc",
  57. },
  58. },
  59. result: []map[string]interface{}{{}},
  60. },
  61. {
  62. sql: "SELECT sha1(a) AS a FROM test",
  63. data: &xsql.Tuple{
  64. Emitter: "test",
  65. Message: xsql.Message{
  66. "a": "The quick brown fox jumps over the lazy dog",
  67. "b": "myb",
  68. "c": "myc",
  69. },
  70. },
  71. result: []map[string]interface{}{{
  72. "a": strings.ToLower("2FD4E1C67A2D28FCED849EE1BB76E7391B93EB12"),
  73. }},
  74. },
  75. {
  76. sql: "SELECT sha256(a) AS a FROM test",
  77. data: &xsql.Tuple{
  78. Emitter: "test",
  79. Message: xsql.Message{
  80. "a": "The quick brown fox jumps over the lazy dog",
  81. "b": "myb",
  82. "c": "myc",
  83. },
  84. },
  85. result: []map[string]interface{}{{
  86. "a": strings.ToLower("D7A8FBB307D7809469CA9ABCB0082E4F8D5651E46D3CDB762D02D0BF37C9E592"),
  87. }},
  88. },
  89. {
  90. sql: "SELECT sha384(a) AS a FROM test",
  91. data: &xsql.Tuple{
  92. Emitter: "test",
  93. Message: xsql.Message{
  94. "a": "The quick brown fox jumps over the lazy dog",
  95. "b": "myb",
  96. "c": "myc",
  97. },
  98. },
  99. result: []map[string]interface{}{{
  100. "a": strings.ToLower("CA737F1014A48F4C0B6DD43CB177B0AFD9E5169367544C494011E3317DBF9A509CB1E5DC1E85A941BBEE3D7F2AFBC9B1"),
  101. }},
  102. },
  103. {
  104. sql: "SELECT sha512(a) AS a FROM test",
  105. data: &xsql.Tuple{
  106. Emitter: "test",
  107. Message: xsql.Message{
  108. "a": "The quick brown fox jumps over the lazy dog",
  109. "b": "myb",
  110. "c": "myc",
  111. },
  112. },
  113. result: []map[string]interface{}{{
  114. "a": strings.ToLower("07E547D9586F6A73F73FBAC0435ED76951218FB7D0C8D788A309D785436BBB642E93A252A954F23912547D1E8A3B5ED6E1BFD7097821233FA0538F3DB854FEE6"),
  115. }},
  116. },
  117. {
  118. sql: "SELECT mqtt(topic) AS a FROM test",
  119. data: &xsql.Tuple{
  120. Emitter: "test",
  121. Message: xsql.Message{},
  122. Metadata: xsql.Metadata{
  123. "topic": "devices/device_001/message",
  124. },
  125. },
  126. result: []map[string]interface{}{{
  127. "a": "devices/device_001/message",
  128. }},
  129. },
  130. {
  131. sql: "SELECT mqtt(topic) AS a FROM test",
  132. data: &xsql.Tuple{
  133. Emitter: "test",
  134. Message: xsql.Message{},
  135. Metadata: xsql.Metadata{
  136. "topic": "devices/device_001/message",
  137. },
  138. },
  139. result: []map[string]interface{}{{
  140. "a": "devices/device_001/message",
  141. }},
  142. },
  143. {
  144. sql: "SELECT topic, mqtt(topic) AS a FROM test",
  145. data: &xsql.Tuple{
  146. Emitter: "test",
  147. Message: xsql.Message{
  148. "topic": "fff",
  149. },
  150. Metadata: xsql.Metadata{
  151. "topic": "devices/device_001/message",
  152. },
  153. },
  154. result: []map[string]interface{}{{
  155. "topic": "fff",
  156. "a": "devices/device_001/message",
  157. }},
  158. },
  159. {
  160. sql: "SELECT cardinality(arr) as r FROM test",
  161. data: &xsql.Tuple{
  162. Emitter: "test",
  163. Message: xsql.Message{
  164. "temperature": 43.2,
  165. "arr": []int{},
  166. },
  167. },
  168. result: []map[string]interface{}{{
  169. "r": 0,
  170. }},
  171. },
  172. {
  173. sql: "SELECT cardinality(arr) as r FROM test",
  174. data: &xsql.Tuple{
  175. Emitter: "test",
  176. Message: xsql.Message{
  177. "temperature": 43.2,
  178. "arr": []int{1, 2, 3, 4, 5},
  179. },
  180. },
  181. result: []map[string]interface{}{{
  182. "r": 5,
  183. }},
  184. },
  185. {
  186. sql: "SELECT isNull(arr) as r FROM test",
  187. data: &xsql.Tuple{
  188. Emitter: "test",
  189. Message: xsql.Message{
  190. "temperature": 43.2,
  191. "arr": []int{},
  192. },
  193. },
  194. result: []map[string]interface{}{{
  195. "r": false,
  196. }},
  197. },
  198. {
  199. sql: "SELECT isNull(arr) as r FROM test",
  200. data: &xsql.Tuple{
  201. Emitter: "test",
  202. Message: xsql.Message{
  203. "temperature": 43.2,
  204. "arr": []float64(nil),
  205. },
  206. },
  207. result: []map[string]interface{}{{
  208. "r": true,
  209. }},
  210. },
  211. {
  212. sql: "SELECT isNull(rec) as r FROM test",
  213. data: &xsql.Tuple{
  214. Emitter: "test",
  215. Message: xsql.Message{
  216. "temperature": 43.2,
  217. "rec": map[string]interface{}(nil),
  218. },
  219. },
  220. result: []map[string]interface{}{{
  221. "r": true,
  222. }},
  223. },
  224. {
  225. sql: "SELECT cast(a * 1000, \"datetime\") AS a FROM test",
  226. data: &xsql.Tuple{
  227. Emitter: "test",
  228. Message: xsql.Message{
  229. "a": 1.62000273e+09,
  230. "b": "ya",
  231. "c": "myc",
  232. },
  233. },
  234. result: []map[string]interface{}{{
  235. "a": cast.TimeFromUnixMilli(1.62000273e+12),
  236. }},
  237. },
  238. }
  239. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  240. contextLogger := conf.Log.WithField("rule", "TestMiscFunc_Apply1")
  241. ctx := context.WithValue(context.Background(), context.LoggerKey, contextLogger)
  242. for i, tt := range tests {
  243. stmt, err := xsql.NewParser(strings.NewReader(tt.sql)).Parse()
  244. if err != nil || stmt == nil {
  245. t.Errorf("parse sql %s error %v", tt.sql, err)
  246. }
  247. pp := &ProjectOp{Fields: stmt.Fields}
  248. fv, afv := xsql.NewFunctionValuersForOp(nil)
  249. result := pp.Apply(ctx, tt.data, fv, afv)
  250. if !reflect.DeepEqual(tt.result, result) {
  251. t.Errorf("%d. %q\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.sql, tt.result, result)
  252. }
  253. }
  254. }
  255. func TestMqttFunc_Apply2(t *testing.T) {
  256. var tests = []struct {
  257. sql string
  258. data *xsql.JoinTupleSets
  259. result []map[string]interface{}
  260. }{
  261. {
  262. sql: "SELECT id1, mqtt(src1.topic) AS a, mqtt(src2.topic) as b FROM src1 LEFT JOIN src2 ON src1.id1 = src2.id1",
  263. data: &xsql.JoinTupleSets{
  264. Content: []xsql.JoinTuple{
  265. {
  266. Tuples: []xsql.Tuple{
  267. {Emitter: "src1", Message: xsql.Message{"id1": "1", "f1": "v1"}, Metadata: xsql.Metadata{"topic": "devices/type1/device001"}},
  268. {Emitter: "src2", Message: xsql.Message{"id2": "1", "f2": "w1"}, Metadata: xsql.Metadata{"topic": "devices/type2/device001"}},
  269. },
  270. },
  271. },
  272. },
  273. result: []map[string]interface{}{{
  274. "id1": "1",
  275. "a": "devices/type1/device001",
  276. "b": "devices/type2/device001",
  277. }},
  278. },
  279. }
  280. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  281. contextLogger := conf.Log.WithField("rule", "TestMqttFunc_Apply2")
  282. ctx := context.WithValue(context.Background(), context.LoggerKey, contextLogger)
  283. for i, tt := range tests {
  284. stmt, err := xsql.NewParser(strings.NewReader(tt.sql)).Parse()
  285. if err != nil || stmt == nil {
  286. t.Errorf("parse sql %s error %v", tt.sql, err)
  287. }
  288. pp := &ProjectOp{Fields: stmt.Fields}
  289. fv, afv := xsql.NewFunctionValuersForOp(nil)
  290. result := pp.Apply(ctx, tt.data, fv, afv)
  291. if !reflect.DeepEqual(tt.result, result) {
  292. t.Errorf("%d. %q\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.sql, tt.result, result)
  293. }
  294. }
  295. }
  296. func TestMetaFunc_Apply1(t *testing.T) {
  297. var tests = []struct {
  298. sql string
  299. data interface{}
  300. result interface{}
  301. }{
  302. {
  303. sql: "SELECT topic, meta(topic) AS a FROM test",
  304. data: &xsql.Tuple{
  305. Emitter: "test",
  306. Message: xsql.Message{
  307. "topic": "fff",
  308. },
  309. Metadata: xsql.Metadata{
  310. "topic": "devices/device_001/message",
  311. },
  312. },
  313. result: []map[string]interface{}{{
  314. "topic": "fff",
  315. "a": "devices/device_001/message",
  316. }},
  317. },
  318. {
  319. sql: "SELECT meta(device) as d, meta(temperature->device) as r FROM test",
  320. data: &xsql.Tuple{
  321. Emitter: "test",
  322. Message: xsql.Message{
  323. "temperature": 43.2,
  324. },
  325. Metadata: xsql.Metadata{
  326. "temperature": map[string]interface{}{
  327. "id": "dfadfasfas",
  328. "device": "device2",
  329. },
  330. "device": "gateway",
  331. },
  332. },
  333. result: []map[string]interface{}{{
  334. "d": "gateway",
  335. "r": "device2",
  336. }},
  337. },
  338. {
  339. sql: "SELECT meta(*) as r FROM test",
  340. data: &xsql.Tuple{
  341. Emitter: "test",
  342. Message: xsql.Message{
  343. "temperature": 43.2,
  344. },
  345. Metadata: xsql.Metadata{
  346. "temperature": map[string]interface{}{
  347. "id": "dfadfasfas",
  348. "device": "device2",
  349. },
  350. "device": "gateway",
  351. },
  352. },
  353. result: []map[string]interface{}{{
  354. "r": map[string]interface{}{
  355. "temperature": map[string]interface{}{
  356. "id": "dfadfasfas",
  357. "device": "device2",
  358. },
  359. "device": "gateway",
  360. },
  361. }},
  362. },
  363. {
  364. sql: "SELECT topic, meta(`Light-diming`->device) AS a FROM test",
  365. data: &xsql.Tuple{
  366. Emitter: "test",
  367. Message: xsql.Message{
  368. "topic": "fff",
  369. },
  370. Metadata: xsql.Metadata{
  371. "Light-diming": map[string]interface{}{
  372. "device": "device2",
  373. },
  374. },
  375. },
  376. result: []map[string]interface{}{{
  377. "topic": "fff",
  378. "a": "device2",
  379. }},
  380. },
  381. }
  382. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  383. contextLogger := conf.Log.WithField("rule", "TestMetaFunc_Apply1")
  384. ctx := context.WithValue(context.Background(), context.LoggerKey, contextLogger)
  385. for i, tt := range tests {
  386. stmt, err := xsql.NewParser(strings.NewReader(tt.sql)).Parse()
  387. if err != nil || stmt == nil {
  388. t.Errorf("parse sql %s error %v", tt.sql, err)
  389. }
  390. pp := &ProjectOp{Fields: stmt.Fields}
  391. fv, afv := xsql.NewFunctionValuersForOp(nil)
  392. result := pp.Apply(ctx, tt.data, fv, afv)
  393. if !reflect.DeepEqual(tt.result, result) {
  394. t.Errorf("%d. %q\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.sql, tt.result, result)
  395. }
  396. }
  397. }
  398. func TestJsonPathFunc_Apply1(t *testing.T) {
  399. var tests = []struct {
  400. sql string
  401. data interface{}
  402. result interface{}
  403. err string
  404. }{
  405. {
  406. sql: `SELECT json_path_query(equipment, "$.arm_right") AS a FROM test`,
  407. data: &xsql.Tuple{
  408. Emitter: "test",
  409. Message: xsql.Message{
  410. "class": "warrior",
  411. "equipment": map[string]interface{}{
  412. "rings": []map[string]interface{}{
  413. {
  414. "name": "ring of despair",
  415. "weight": 0.1,
  416. }, {
  417. "name": "ring of strength",
  418. "weight": 2.4,
  419. },
  420. },
  421. "arm_right": "Sword of flame",
  422. "arm_left": "Shield of faith",
  423. },
  424. },
  425. },
  426. result: []map[string]interface{}{{
  427. "a": "Sword of flame",
  428. }},
  429. }, {
  430. sql: `SELECT json_path_query(equipment, "$.rings[*].weight") AS a FROM test`,
  431. data: &xsql.Tuple{
  432. Emitter: "test",
  433. Message: xsql.Message{
  434. "class": "warrior",
  435. "equipment": map[string]interface{}{
  436. "rings": []interface{}{
  437. map[string]interface{}{
  438. "name": "ring of despair",
  439. "weight": 0.1,
  440. }, map[string]interface{}{
  441. "name": "ring of strength",
  442. "weight": 2.4,
  443. },
  444. },
  445. "arm_right": "Sword of flame",
  446. "arm_left": "Shield of faith",
  447. },
  448. },
  449. },
  450. result: []map[string]interface{}{{
  451. "a": []interface{}{
  452. 0.1, 2.4,
  453. },
  454. }},
  455. }, {
  456. sql: `SELECT json_path_query_first(equipment, "$.rings[*].weight") AS a FROM test`,
  457. data: &xsql.Tuple{
  458. Emitter: "test",
  459. Message: xsql.Message{
  460. "class": "warrior",
  461. "equipment": map[string]interface{}{
  462. "rings": []interface{}{
  463. map[string]interface{}{
  464. "name": "ring of despair",
  465. "weight": 0.1,
  466. }, map[string]interface{}{
  467. "name": "ring of strength",
  468. "weight": 2.4,
  469. },
  470. },
  471. "arm_right": "Sword of flame",
  472. "arm_left": "Shield of faith",
  473. },
  474. },
  475. },
  476. result: []map[string]interface{}{{
  477. "a": 0.1,
  478. }},
  479. }, {
  480. sql: `SELECT json_path_query(equipment, "$.rings[? @.weight>1]") AS a FROM test`,
  481. data: &xsql.Tuple{
  482. Emitter: "test",
  483. Message: xsql.Message{
  484. "class": "warrior",
  485. "equipment": map[string]interface{}{
  486. "rings": []interface{}{
  487. map[string]interface{}{
  488. "name": "ring of despair",
  489. "weight": 0.1,
  490. }, map[string]interface{}{
  491. "name": "ring of strength",
  492. "weight": 2.4,
  493. },
  494. },
  495. "arm_right": "Sword of flame",
  496. "arm_left": "Shield of faith",
  497. },
  498. },
  499. },
  500. result: []map[string]interface{}{{
  501. "a": []interface{}{
  502. map[string]interface{}{
  503. "name": "ring of strength",
  504. "weight": 2.4,
  505. },
  506. },
  507. }},
  508. }, {
  509. sql: `SELECT json_path_query(equipment, "$.rings[? @.weight>1].name") AS a FROM test`,
  510. data: &xsql.Tuple{
  511. Emitter: "test",
  512. Message: xsql.Message{
  513. "class": "warrior",
  514. "equipment": map[string]interface{}{
  515. "rings": []interface{}{
  516. map[string]interface{}{
  517. "name": "ring of despair",
  518. "weight": 0.1,
  519. }, map[string]interface{}{
  520. "name": "ring of strength",
  521. "weight": 2.4,
  522. },
  523. },
  524. "arm_right": "Sword of flame",
  525. "arm_left": "Shield of faith",
  526. },
  527. },
  528. },
  529. result: []map[string]interface{}{{
  530. "a": []interface{}{
  531. "ring of strength",
  532. },
  533. }},
  534. }, {
  535. sql: `SELECT json_path_exists(equipment, "$.rings[? @.weight>5]") AS a FROM test`,
  536. data: &xsql.Tuple{
  537. Emitter: "test",
  538. Message: xsql.Message{
  539. "class": "warrior",
  540. "equipment": map[string]interface{}{
  541. "rings": []interface{}{
  542. map[string]interface{}{
  543. "name": "ring of despair",
  544. "weight": 0.1,
  545. }, map[string]interface{}{
  546. "name": "ring of strength",
  547. "weight": 2.4,
  548. },
  549. },
  550. "arm_right": "Sword of flame",
  551. "arm_left": "Shield of faith",
  552. },
  553. },
  554. },
  555. result: []map[string]interface{}{{
  556. "a": false,
  557. }},
  558. }, {
  559. sql: `SELECT json_path_exists(equipment, "$.ring1") AS a FROM test`,
  560. data: &xsql.Tuple{
  561. Emitter: "test",
  562. Message: xsql.Message{
  563. "class": "warrior",
  564. "equipment": map[string]interface{}{
  565. "rings": []interface{}{
  566. map[string]interface{}{
  567. "name": "ring of despair",
  568. "weight": 0.1,
  569. }, map[string]interface{}{
  570. "name": "ring of strength",
  571. "weight": 2.4,
  572. },
  573. },
  574. "arm_right": "Sword of flame",
  575. "arm_left": "Shield of faith",
  576. },
  577. },
  578. },
  579. result: []map[string]interface{}{{
  580. "a": false,
  581. }},
  582. }, {
  583. sql: `SELECT json_path_exists(equipment, "$.rings") AS a FROM test`,
  584. data: &xsql.Tuple{
  585. Emitter: "test",
  586. Message: xsql.Message{
  587. "class": "warrior",
  588. "equipment": map[string]interface{}{
  589. "rings": []interface{}{
  590. map[string]interface{}{
  591. "name": "ring of despair",
  592. "weight": 0.1,
  593. }, map[string]interface{}{
  594. "name": "ring of strength",
  595. "weight": 2.4,
  596. },
  597. },
  598. "arm_right": "Sword of flame",
  599. "arm_left": "Shield of faith",
  600. },
  601. },
  602. },
  603. result: []map[string]interface{}{{
  604. "a": true,
  605. }},
  606. }, {
  607. sql: `SELECT json_path_query(equipment, "$.rings[? (@.weight>1)].name") AS a FROM test`,
  608. data: &xsql.Tuple{
  609. Emitter: "test",
  610. Message: xsql.Message{
  611. "class": "warrior",
  612. "equipment": map[string]interface{}{
  613. "rings": []map[string]interface{}{
  614. {
  615. "name": "ring of despair",
  616. "weight": 0.1,
  617. }, {
  618. "name": "ring of strength",
  619. "weight": 2.4,
  620. },
  621. },
  622. "arm_right": "Sword of flame",
  623. "arm_left": "Shield of faith",
  624. },
  625. },
  626. },
  627. result: []map[string]interface{}{{
  628. "a": []interface{}{
  629. "ring of strength",
  630. },
  631. }},
  632. }, {
  633. sql: `SELECT json_path_query(equipment, "$.rings[*]") AS a FROM test`,
  634. data: &xsql.Tuple{
  635. Emitter: "test",
  636. Message: xsql.Message{
  637. "class": "warrior",
  638. "equipment": map[string]interface{}{
  639. "rings": []float64{
  640. 0.1, 2.4,
  641. },
  642. "arm_right": "Sword of flame",
  643. "arm_left": "Shield of faith",
  644. },
  645. },
  646. },
  647. result: []map[string]interface{}{{
  648. "a": []interface{}{
  649. 0.1, 2.4,
  650. },
  651. }},
  652. }, {
  653. sql: `SELECT json_path_query(equipment, "$.rings") AS a FROM test`,
  654. data: &xsql.Tuple{
  655. Emitter: "test",
  656. Message: xsql.Message{
  657. "class": "warrior",
  658. "equipment": map[string]interface{}{
  659. "rings": []float64{
  660. 0.1, 2.4,
  661. },
  662. "arm_right": "Sword of flame",
  663. "arm_left": "Shield of faith",
  664. },
  665. },
  666. },
  667. result: []map[string]interface{}{{
  668. "a": []interface{}{
  669. 0.1, 2.4,
  670. },
  671. }},
  672. }, {
  673. sql: `SELECT json_path_query(equipment, "$[0].rings[1]") AS a FROM test`,
  674. data: &xsql.Tuple{
  675. Emitter: "test",
  676. Message: xsql.Message{
  677. "class": "warrior",
  678. "equipment": []map[string]interface{}{
  679. {
  680. "rings": []float64{
  681. 0.1, 2.4,
  682. },
  683. "arm_right": "Sword of flame",
  684. "arm_left": "Shield of faith",
  685. },
  686. },
  687. },
  688. },
  689. result: []map[string]interface{}{{
  690. "a": 2.4,
  691. }},
  692. }, {
  693. sql: "SELECT json_path_query(equipment, \"$[0][\\\"arm.left\\\"]\") AS a FROM test",
  694. data: &xsql.Tuple{
  695. Emitter: "test",
  696. Message: xsql.Message{
  697. "class": "warrior",
  698. "equipment": []map[string]interface{}{
  699. {
  700. "rings": []float64{
  701. 0.1, 2.4,
  702. },
  703. "arm.right": "Sword of flame",
  704. "arm.left": "Shield of faith",
  705. },
  706. },
  707. },
  708. },
  709. result: []map[string]interface{}{{
  710. "a": "Shield of faith",
  711. }},
  712. }, {
  713. sql: "SELECT json_path_query(equipment, \"$[\\\"arm.left\\\"]\") AS a FROM test",
  714. data: &xsql.Tuple{
  715. Emitter: "test",
  716. Message: xsql.Message{
  717. "class": "warrior",
  718. "equipment": `{"rings": [0.1, 2.4],"arm.right": "Sword of flame","arm.left": "Shield of faith"}`,
  719. },
  720. },
  721. result: []map[string]interface{}{{
  722. "a": "Shield of faith",
  723. }},
  724. }, {
  725. sql: "SELECT json_path_query(equipment, \"$[0][\\\"arm.left\\\"]\") AS a FROM test",
  726. data: &xsql.Tuple{
  727. Emitter: "test",
  728. Message: xsql.Message{
  729. "class": "warrior",
  730. "equipment": `[{"rings": [0.1, 2.4],"arm.right": "Sword of flame","arm.left": "Shield of faith"}]`,
  731. },
  732. },
  733. result: []map[string]interface{}{{
  734. "a": "Shield of faith",
  735. }},
  736. }, {
  737. sql: `SELECT all[poi[-1] + 1]->ts as powerOnTs FROM test`,
  738. data: &xsql.Tuple{
  739. Emitter: "test",
  740. Message: xsql.Message{
  741. "all": []map[string]interface{}{
  742. {"SystemPowerMode": 0, "VehicleSpeed": 0, "FLWdwPosition": 0, "FrontWiperSwitchStatus": float64(1), "ts": 0},
  743. {"SystemPowerMode": 0, "VehicleSpeed": 0, "FLWdwPosition": 0, "FrontWiperSwitchStatus": float64(4), "ts": 500},
  744. {"SystemPowerMode": 2, "VehicleSpeed": 0, "FLWdwPosition": 0, "FrontWiperSwitchStatus": 0, "ts": 1000},
  745. {"SystemPowerMode": 2, "VehicleSpeed": 10, "FLWdwPosition": 20, "FrontWiperSwitchStatus": 0, "ts": 60000},
  746. {"SystemPowerMode": 2, "VehicleSpeed": 10, "FLWdwPosition": 20, "FrontWiperSwitchStatus": 0, "ts": 89500},
  747. {"SystemPowerMode": 2, "VehicleSpeed": 20, "FLWdwPosition": 50, "FrontWiperSwitchStatus": 5, "ts": 90000},
  748. {"SystemPowerMode": 2, "VehicleSpeed": 40, "FLWdwPosition": 60, "FrontWiperSwitchStatus": 5, "ts": 121000},
  749. },
  750. "poi": []interface{}{0, 1},
  751. },
  752. },
  753. result: []map[string]interface{}{{
  754. "powerOnTs": 1000,
  755. }},
  756. }, {
  757. sql: `SELECT json_path_query(equipment, "$.arm_right") AS a FROM test`,
  758. data: &xsql.Tuple{
  759. Emitter: "test",
  760. Message: xsql.Message{
  761. "class": "warrior",
  762. "equipment2": map[string]interface{}{
  763. "rings": []map[string]interface{}{
  764. {
  765. "name": "ring of despair",
  766. "weight": 0.1,
  767. }, {
  768. "name": "ring of strength",
  769. "weight": 2.4,
  770. },
  771. },
  772. "arm_right": "Sword of flame",
  773. "arm_left": "Shield of faith",
  774. },
  775. },
  776. },
  777. err: "run Select error: call func json_path_query error: invalid data nil for jsonpath",
  778. },
  779. }
  780. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  781. contextLogger := conf.Log.WithField("rule", "TestJsonFunc_Apply1")
  782. ctx := context.WithValue(context.Background(), context.LoggerKey, contextLogger)
  783. for i, tt := range tests {
  784. stmt, err := xsql.NewParser(strings.NewReader(tt.sql)).Parse()
  785. if err != nil || stmt == nil {
  786. t.Errorf("parse sql %s error %v", tt.sql, err)
  787. }
  788. pp := &ProjectOp{Fields: stmt.Fields}
  789. fv, afv := xsql.NewFunctionValuersForOp(ctx)
  790. result := pp.Apply(ctx, tt.data, fv, afv)
  791. switch rt := result.(type) {
  792. case []map[string]interface{}:
  793. if tt.err == "" {
  794. if !reflect.DeepEqual(tt.result, result) {
  795. t.Errorf("%d. %q\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.sql, tt.result, result)
  796. }
  797. } else {
  798. t.Errorf("%d: invalid result:\n exp error %s\n got=%s\n\n", i, tt.err, result)
  799. }
  800. case error:
  801. if tt.err == "" {
  802. t.Errorf("%d: got error:\n exp=%s\n got=%s\n\n", i, tt.result, rt)
  803. } else if !reflect.DeepEqual(tt.err, testx.Errstring(rt)) {
  804. t.Errorf("%d: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.err, rt)
  805. }
  806. default:
  807. t.Errorf("%d: Invalid returned result found %v", i, result)
  808. }
  809. }
  810. }
  811. func TestChangedFuncs_Apply1(t *testing.T) {
  812. var tests = []struct {
  813. sql string
  814. data []interface{}
  815. result [][]map[string]interface{}
  816. }{
  817. {
  818. sql: `SELECT changed_col(true, a), b FROM test`,
  819. data: []interface{}{
  820. &xsql.Tuple{
  821. Emitter: "test",
  822. Message: xsql.Message{
  823. "a": "a1",
  824. "b": "b1",
  825. "c": "c1",
  826. },
  827. },
  828. &xsql.Tuple{
  829. Emitter: "test",
  830. Message: xsql.Message{
  831. "a": "a1",
  832. "b": "b2",
  833. "c": "c1",
  834. },
  835. },
  836. &xsql.Tuple{
  837. Emitter: "test",
  838. Message: xsql.Message{
  839. "a": "a1",
  840. "c": "c1",
  841. },
  842. },
  843. &xsql.Tuple{
  844. Emitter: "test",
  845. Message: xsql.Message{
  846. "a": "a1",
  847. "b": "b2",
  848. "c": "c2",
  849. },
  850. },
  851. },
  852. result: [][]map[string]interface{}{{{
  853. "changed_col": "a1",
  854. "b": "b1",
  855. }}, {{
  856. "b": "b2",
  857. }}, {{}}, {{
  858. "b": "b2",
  859. }}},
  860. }, {
  861. sql: `SELECT changed_col(true, *) FROM test`,
  862. data: []interface{}{
  863. &xsql.Tuple{
  864. Emitter: "test",
  865. Message: xsql.Message{
  866. "a": "a1",
  867. "b": "b1",
  868. },
  869. },
  870. &xsql.Tuple{
  871. Emitter: "test",
  872. Message: xsql.Message{
  873. "a": "a1",
  874. "c": "c1",
  875. },
  876. },
  877. &xsql.Tuple{
  878. Emitter: "test",
  879. Message: xsql.Message{
  880. "a": "a1",
  881. "c": "c1",
  882. },
  883. },
  884. &xsql.Tuple{
  885. Emitter: "test",
  886. Message: xsql.Message{
  887. "a": "a1",
  888. "b": "b2",
  889. "c": "c2",
  890. },
  891. },
  892. },
  893. result: [][]map[string]interface{}{{{
  894. "changed_col": xsql.Message{
  895. "a": "a1",
  896. "b": "b1",
  897. },
  898. }}, {{
  899. "changed_col": xsql.Message{
  900. "a": "a1",
  901. "c": "c1",
  902. },
  903. }}, {{}}, {{
  904. "changed_col": xsql.Message{
  905. "a": "a1",
  906. "b": "b2",
  907. "c": "c2",
  908. },
  909. }}},
  910. },
  911. }
  912. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  913. contextLogger := conf.Log.WithField("rule", "TestChangedFuncs_Apply1")
  914. for i, tt := range tests[1:] {
  915. tempStore, _ := state.CreateStore("mockRule"+strconv.Itoa(i), api.AtMostOnce)
  916. ctx := context.WithValue(context.Background(), context.LoggerKey, contextLogger).WithMeta("mockRule"+strconv.Itoa(i), "project", tempStore)
  917. stmt, err := xsql.NewParser(strings.NewReader(tt.sql)).Parse()
  918. if err != nil || stmt == nil {
  919. t.Errorf("parse sql %s error %v", tt.sql, err)
  920. }
  921. pp := &ProjectOp{Fields: stmt.Fields}
  922. fv, afv := xsql.NewFunctionValuersForOp(ctx)
  923. r := make([][]map[string]interface{}, 0, len(tt.data))
  924. for _, d := range tt.data {
  925. result := pp.Apply(ctx, d, fv, afv)
  926. r = append(r, result.([]map[string]interface{}))
  927. }
  928. if !reflect.DeepEqual(tt.result, r) {
  929. t.Errorf("%d. %q\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.sql, tt.result, r)
  930. }
  931. }
  932. }