valuer.go 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246
  1. // Copyright 2022-2023 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. "fmt"
  17. "math"
  18. "reflect"
  19. "regexp"
  20. "time"
  21. "github.com/lf-edge/ekuiper/internal/binder/function"
  22. "github.com/lf-edge/ekuiper/pkg/ast"
  23. "github.com/lf-edge/ekuiper/pkg/cast"
  24. )
  25. var (
  26. // implicitValueFuncs is a set of functions that event implicitly passes the value.
  27. implicitValueFuncs = map[string]bool{
  28. "window_start": true,
  29. "window_end": true,
  30. "event_time": true,
  31. }
  32. // ImplicitStateFuncs is a set of functions that read/update global state implicitly.
  33. ImplicitStateFuncs = map[string]bool{
  34. "last_hit_time": true,
  35. "last_hit_count": true,
  36. "last_agg_hit_time": true,
  37. "last_agg_hit_count": true,
  38. }
  39. )
  40. /*
  41. * Valuer definitions
  42. */
  43. // Valuer is the interface that wraps the Value() method.
  44. type Valuer interface {
  45. // Value returns the value and existence flag for a given key.
  46. Value(key, table string) (interface{}, bool)
  47. Meta(key, table string) (interface{}, bool)
  48. }
  49. // AliasValuer is used to calculate and cache the alias value
  50. type AliasValuer interface {
  51. // AliasValue Get the value of alias
  52. AliasValue(name string) (interface{}, bool)
  53. // AppendAlias set the alias result
  54. AppendAlias(key string, value interface{}) bool
  55. }
  56. // CallValuer implements the Call method for evaluating function calls.
  57. type CallValuer interface {
  58. Valuer
  59. // Call is invoked to evaluate a function call (if possible).
  60. Call(name string, funcId int, args []interface{}) (interface{}, bool)
  61. }
  62. // FuncValuer can calculate function type value like window_start and window_end
  63. type FuncValuer interface {
  64. FuncValue(key string) (interface{}, bool)
  65. }
  66. type AggregateCallValuer interface {
  67. CallValuer
  68. GetAllTuples() AggregateData
  69. GetSingleCallValuer() CallValuer
  70. }
  71. type WildcardValuer struct {
  72. Data Wildcarder
  73. }
  74. func (wv *WildcardValuer) Value(key, table string) (interface{}, bool) {
  75. if key == "*" {
  76. return wv.Data.All(table)
  77. }
  78. return nil, false
  79. }
  80. func (wv *WildcardValuer) Meta(_, _ string) (interface{}, bool) {
  81. return nil, false
  82. }
  83. // MultiValuer returns a Valuer that iterates over multiple Valuer instances
  84. // to find a match.
  85. func MultiValuer(valuers ...Valuer) Valuer {
  86. return multiValuer(valuers)
  87. }
  88. type multiValuer []Valuer
  89. func (a multiValuer) Value(key, table string) (interface{}, bool) {
  90. for _, valuer := range a {
  91. if v, ok := valuer.Value(key, table); ok {
  92. return v, true
  93. }
  94. }
  95. return nil, false
  96. }
  97. func (a multiValuer) Meta(key, table string) (interface{}, bool) {
  98. for _, valuer := range a {
  99. if v, ok := valuer.Meta(key, table); ok {
  100. return v, true
  101. }
  102. }
  103. return nil, false
  104. }
  105. func (a multiValuer) AppendAlias(key string, value interface{}) bool {
  106. for _, valuer := range a {
  107. if vv, ok := valuer.(AliasValuer); ok {
  108. if ok := vv.AppendAlias(key, value); ok {
  109. return true
  110. }
  111. }
  112. }
  113. return false
  114. }
  115. func (a multiValuer) AliasValue(key string) (interface{}, bool) {
  116. for _, valuer := range a {
  117. if vv, ok := valuer.(AliasValuer); ok {
  118. return vv.AliasValue(key)
  119. }
  120. }
  121. return nil, false
  122. }
  123. func (a multiValuer) FuncValue(key string) (interface{}, bool) {
  124. for _, valuer := range a {
  125. if vv, ok := valuer.(FuncValuer); ok {
  126. if r, ok := vv.FuncValue(key); ok {
  127. return r, true
  128. }
  129. }
  130. }
  131. return nil, false
  132. }
  133. func (a multiValuer) Call(name string, funcId int, args []interface{}) (interface{}, bool) {
  134. for _, valuer := range a {
  135. if valuer, ok := valuer.(CallValuer); ok {
  136. if v, ok := valuer.Call(name, funcId, args); ok {
  137. return v, true
  138. } else {
  139. return fmt.Errorf("call func %s error: %v", name, v), false
  140. }
  141. }
  142. }
  143. return nil, false
  144. }
  145. type multiAggregateValuer struct {
  146. data AggregateData
  147. multiValuer
  148. singleCallValuer CallValuer
  149. }
  150. func MultiAggregateValuer(data AggregateData, singleCallValuer CallValuer, valuers ...Valuer) Valuer {
  151. return &multiAggregateValuer{
  152. data: data,
  153. multiValuer: valuers,
  154. singleCallValuer: singleCallValuer,
  155. }
  156. }
  157. func (a *multiAggregateValuer) Call(name string, funcId int, args []interface{}) (interface{}, bool) {
  158. // assume the aggFuncMap already cache the custom agg funcs in IsAggFunc()
  159. isAgg := function.IsAggFunc(name)
  160. for _, valuer := range a.multiValuer {
  161. if a, ok := valuer.(AggregateCallValuer); ok && isAgg {
  162. if v, ok := a.Call(name, funcId, args); ok {
  163. return v, true
  164. } else {
  165. return fmt.Errorf("call func %s error: %v", name, v), false
  166. }
  167. } else if c, ok := valuer.(CallValuer); ok && !isAgg {
  168. if v, ok := c.Call(name, funcId, args); ok {
  169. return v, true
  170. }
  171. }
  172. }
  173. return nil, false
  174. }
  175. func (a *multiAggregateValuer) GetAllTuples() AggregateData {
  176. return a.data
  177. }
  178. func (a *multiAggregateValuer) GetSingleCallValuer() CallValuer {
  179. return a.singleCallValuer
  180. }
  181. func (a *multiAggregateValuer) AppendAlias(key string, value interface{}) bool {
  182. if vv, ok := a.data.(AliasValuer); ok {
  183. if ok := vv.AppendAlias(key, value); ok {
  184. return true
  185. }
  186. return false
  187. } else {
  188. return a.multiValuer.AppendAlias(key, value)
  189. }
  190. }
  191. func (a *multiAggregateValuer) AliasValue(key string) (interface{}, bool) {
  192. if vv, ok := a.data.(AliasValuer); ok {
  193. return vv.AliasValue(key)
  194. } else {
  195. return a.multiValuer.AliasValue(key)
  196. }
  197. }
  198. /*
  199. * Eval Logics
  200. */
  201. // Eval evaluates expr against a map.
  202. func Eval(expr ast.Expr, m Valuer) interface{} {
  203. eval := ValuerEval{Valuer: m}
  204. return eval.Eval(expr)
  205. }
  206. // ValuerEval will evaluate an expression using the Valuer.
  207. type ValuerEval struct {
  208. Valuer Valuer
  209. // IntegerFloatDivision will set the eval system to treat
  210. // a division between two integers as a floating point division.
  211. IntegerFloatDivision bool
  212. }
  213. // Eval evaluates an expression and returns a value.
  214. // map the expression to the correct valuer
  215. func (v *ValuerEval) Eval(expr ast.Expr) interface{} {
  216. if expr == nil {
  217. return nil
  218. }
  219. switch expr := expr.(type) {
  220. case *ast.BinaryExpr:
  221. return v.evalBinaryExpr(expr)
  222. case *ast.IntegerLiteral:
  223. return expr.Val
  224. case *ast.NumberLiteral:
  225. return expr.Val
  226. case *ast.ParenExpr:
  227. return v.Eval(expr.Expr)
  228. case *ast.StringLiteral:
  229. return expr.Val
  230. case *ast.BooleanLiteral:
  231. return expr.Val
  232. case *ast.ColonExpr:
  233. s, e := v.Eval(expr.Start), v.Eval(expr.End)
  234. si, err := cast.ToInt(s, cast.CONVERT_SAMEKIND)
  235. if err != nil {
  236. return fmt.Errorf("colon start %v is not int: %v", expr.Start, err)
  237. }
  238. ei, err := cast.ToInt(e, cast.CONVERT_SAMEKIND)
  239. if err != nil {
  240. return fmt.Errorf("colon end %v is not int: %v", expr.End, err)
  241. }
  242. return &BracketEvalResult{Start: si, End: ei}
  243. case *ast.IndexExpr:
  244. i := v.Eval(expr.Index)
  245. ii, err := cast.ToInt(i, cast.CONVERT_SAMEKIND)
  246. if err != nil {
  247. return fmt.Errorf("index %v is not int: %v", expr.Index, err)
  248. }
  249. return &BracketEvalResult{Start: ii, End: ii}
  250. case *ast.Call:
  251. // The analytic functions are calculated prior to all ops, so just get the cached field value
  252. if expr.Cached && expr.CachedField != "" {
  253. val, _ := v.Valuer.Value(expr.CachedField, "")
  254. // nil is also cached
  255. return val
  256. }
  257. if _, ok := implicitValueFuncs[expr.Name]; ok {
  258. if vv, ok := v.Valuer.(FuncValuer); ok {
  259. val, ok := vv.FuncValue(expr.Name)
  260. if ok {
  261. return val
  262. }
  263. }
  264. } else {
  265. if valuer, ok := v.Valuer.(CallValuer); ok {
  266. var (
  267. args []interface{}
  268. ft = expr.FuncType
  269. )
  270. if _, ok := ImplicitStateFuncs[expr.Name]; ok {
  271. args = make([]interface{}, 1)
  272. // This is the implicit arg set by the filter planner
  273. // If set, it will only return the value, no updating the value
  274. if expr.Cached {
  275. args[0] = false
  276. } else {
  277. args[0] = true
  278. }
  279. if expr.Name == "last_hit_time" || expr.Name == "last_agg_hit_time" {
  280. if vv, ok := v.Valuer.(FuncValuer); ok {
  281. val, ok := vv.FuncValue("event_time")
  282. if ok {
  283. args = append(args, val)
  284. } else {
  285. return fmt.Errorf("call %s error: %v", expr.Name, val)
  286. }
  287. } else {
  288. return fmt.Errorf("call %s error: %v", expr.Name, "cannot get current time")
  289. }
  290. }
  291. val, _ := valuer.Call(expr.Name, expr.FuncId, args)
  292. return val
  293. }
  294. if len(expr.Args) > 0 {
  295. switch ft {
  296. case ast.FuncTypeAgg:
  297. args = make([]interface{}, len(expr.Args))
  298. for i, arg := range expr.Args {
  299. if aggreValuer, ok := valuer.(AggregateCallValuer); ok {
  300. args[i] = aggreValuer.GetAllTuples().AggregateEval(arg, aggreValuer.GetSingleCallValuer())
  301. } else {
  302. args[i] = v.Eval(arg)
  303. if _, ok := args[i].(error); ok {
  304. return args[i]
  305. }
  306. }
  307. }
  308. case ast.FuncTypeScalar, ast.FuncTypeSrf:
  309. args = make([]interface{}, len(expr.Args))
  310. for i, arg := range expr.Args {
  311. args[i] = v.Eval(arg)
  312. if _, ok := args[i].(error); ok {
  313. return args[i]
  314. }
  315. }
  316. case ast.FuncTypeCols:
  317. var keys []string
  318. for _, arg := range expr.Args { // In the parser, the col func arguments must be ColField
  319. cf, ok := arg.(*ast.ColFuncField)
  320. if !ok {
  321. // won't happen
  322. return fmt.Errorf("expect colFuncField but got %v", arg)
  323. }
  324. temp := v.Eval(cf.Expr)
  325. if _, ok := temp.(error); ok {
  326. return temp
  327. }
  328. switch cf.Expr.(type) {
  329. case *ast.Wildcard:
  330. m, ok := temp.(map[string]interface{})
  331. if !ok {
  332. return fmt.Errorf("wildcarder return non map result")
  333. }
  334. for kk, vv := range m {
  335. args = append(args, vv)
  336. keys = append(keys, kk)
  337. }
  338. default:
  339. args = append(args, temp)
  340. keys = append(keys, cf.Name)
  341. }
  342. }
  343. args = append(args, keys)
  344. default:
  345. // won't happen
  346. return fmt.Errorf("unknown function type")
  347. }
  348. }
  349. if function.IsAnalyticFunc(expr.Name) {
  350. // this data should be recorded or not ? default answer is yes
  351. if expr.WhenExpr != nil {
  352. validData := true
  353. temp := v.Eval(expr.WhenExpr)
  354. whenExprVal, ok := temp.(bool)
  355. if ok {
  356. validData = whenExprVal
  357. }
  358. args = append(args, validData)
  359. } else {
  360. args = append(args, true)
  361. }
  362. // analytic func must put the partition key into the args
  363. if expr.Partition != nil && len(expr.Partition.Exprs) > 0 {
  364. pk := ""
  365. for _, pe := range expr.Partition.Exprs {
  366. temp := v.Eval(pe)
  367. if _, ok := temp.(error); ok {
  368. return temp
  369. }
  370. pk += fmt.Sprintf("%v", temp)
  371. }
  372. args = append(args, pk)
  373. } else {
  374. args = append(args, "self")
  375. }
  376. }
  377. val, _ := valuer.Call(expr.Name, expr.FuncId, args)
  378. return val
  379. }
  380. }
  381. return nil
  382. case *ast.FieldRef:
  383. var t, n string
  384. if expr.IsAlias() {
  385. if valuer, ok := v.Valuer.(AliasValuer); ok {
  386. val, ok := valuer.AliasValue(expr.Name)
  387. if ok {
  388. return val
  389. } else {
  390. r := v.Eval(expr.Expression)
  391. // TODO possible performance elevation to eliminate this cal
  392. valuer.AppendAlias(expr.Name, r)
  393. return r
  394. }
  395. }
  396. } else if expr.StreamName == ast.DefaultStream {
  397. n = expr.Name
  398. } else {
  399. t = string(expr.StreamName)
  400. n = expr.Name
  401. }
  402. if n != "" {
  403. val, ok := v.Valuer.Value(n, t)
  404. if ok {
  405. return val
  406. }
  407. }
  408. return nil
  409. case *ast.MetaRef:
  410. if expr.StreamName == "" || expr.StreamName == ast.DefaultStream {
  411. val, _ := v.Valuer.Meta(expr.Name, "")
  412. return val
  413. } else {
  414. // The field specified with stream source
  415. val, _ := v.Valuer.Meta(expr.Name, string(expr.StreamName))
  416. return val
  417. }
  418. case *ast.JsonFieldRef:
  419. val, ok := v.Valuer.Value(expr.Name, "")
  420. if ok {
  421. return val
  422. } else {
  423. return nil
  424. }
  425. case *ast.Wildcard:
  426. all, _ := v.Valuer.Value("*", "")
  427. al, ok := all.(map[string]interface{})
  428. if !ok {
  429. return fmt.Errorf("unexpected wildcard value %v", all)
  430. }
  431. val := make(map[string]interface{})
  432. for k, v := range al {
  433. if !contains(expr.Except, k) {
  434. val[k] = v
  435. }
  436. }
  437. for _, field := range expr.Replace {
  438. vi := v.Eval(field.Expr)
  439. if e, ok := vi.(error); ok {
  440. return e
  441. }
  442. val[field.AName] = vi
  443. }
  444. return val
  445. case *ast.CaseExpr:
  446. return v.evalCase(expr)
  447. case *ast.ValueSetExpr:
  448. return v.evalValueSet(expr)
  449. case *ast.BetweenExpr:
  450. return []interface{}{
  451. v.Eval(expr.Lower), v.Eval(expr.Higher),
  452. }
  453. case *ast.LikePattern:
  454. if expr.Pattern != nil {
  455. return expr.Pattern
  456. }
  457. v := v.Eval(expr.Expr)
  458. str, ok := v.(string)
  459. if !ok {
  460. return fmt.Errorf("invalid LIKE pattern, must be a string but got %v", v)
  461. }
  462. re, err := expr.Compile(str)
  463. if err != nil {
  464. return err
  465. }
  466. return re
  467. default:
  468. return nil
  469. }
  470. }
  471. func (v *ValuerEval) evalBinaryExpr(expr *ast.BinaryExpr) interface{} {
  472. lhs := v.Eval(expr.LHS)
  473. switch val := lhs.(type) {
  474. case map[string]interface{}:
  475. return v.evalJsonExpr(val, expr.OP, expr.RHS)
  476. case Message:
  477. return v.evalJsonExpr(map[string]interface{}(val), expr.OP, expr.RHS)
  478. case error:
  479. return val
  480. }
  481. // shortcut for bool
  482. switch expr.OP {
  483. case ast.AND:
  484. if bv, ok := lhs.(bool); ok && !bv {
  485. return false
  486. }
  487. case ast.OR:
  488. if bv, ok := lhs.(bool); ok && bv {
  489. return true
  490. }
  491. }
  492. if isSliceOrArray(lhs) {
  493. return v.evalJsonExpr(lhs, expr.OP, expr.RHS)
  494. }
  495. rhs := v.Eval(expr.RHS)
  496. if _, ok := rhs.(error); ok {
  497. return rhs
  498. }
  499. if isSetOperator(expr.OP) {
  500. return v.evalSetsExpr(lhs, expr.OP, rhs)
  501. }
  502. switch expr.OP {
  503. case ast.BETWEEN, ast.NOTBETWEEN:
  504. arr, ok := rhs.([]interface{})
  505. if !ok {
  506. return fmt.Errorf("between operator expects two arguments, but found %v", rhs)
  507. }
  508. andLeft := v.simpleDataEval(lhs, arr[0], ast.GTE)
  509. switch andLeft.(type) {
  510. case error:
  511. return fmt.Errorf("between operator cannot compare %[1]T(%[1]v) and %[2]T(%[2]v)", lhs, arr[0])
  512. }
  513. andRight := v.simpleDataEval(lhs, arr[1], ast.LTE)
  514. switch andRight.(type) {
  515. case error:
  516. return fmt.Errorf("between operator cannot compare %[1]T(%[1]v) and %[2]T(%[2]v)", lhs, arr[1])
  517. }
  518. r := v.simpleDataEval(andLeft, andRight, ast.AND)
  519. br, ok := r.(bool)
  520. if expr.OP == ast.NOTBETWEEN && ok {
  521. return !br
  522. } else {
  523. return r
  524. }
  525. case ast.LIKE, ast.NOTLIKE:
  526. ls, ok := lhs.(string)
  527. if !ok {
  528. return fmt.Errorf("LIKE operator left operand expects string, but found %v", lhs)
  529. }
  530. var result bool
  531. rs, ok := rhs.(*regexp.Regexp)
  532. if !ok {
  533. return fmt.Errorf("LIKE operator right operand expects string, but found %v", rhs)
  534. }
  535. result = rs.MatchString(ls)
  536. if expr.OP == ast.NOTLIKE {
  537. result = !result
  538. }
  539. return result
  540. default:
  541. return v.simpleDataEval(lhs, rhs, expr.OP)
  542. }
  543. }
  544. func (v *ValuerEval) evalCase(expr *ast.CaseExpr) interface{} {
  545. if expr.Value != nil { // compare value to all when clause
  546. ev := v.Eval(expr.Value)
  547. for _, w := range expr.WhenClauses {
  548. wv := v.Eval(w.Expr)
  549. switch r := v.simpleDataEval(ev, wv, ast.EQ).(type) {
  550. case error:
  551. return fmt.Errorf("evaluate case expression error: %s", r)
  552. case bool:
  553. if r {
  554. return v.Eval(w.Result)
  555. }
  556. }
  557. }
  558. } else {
  559. for _, w := range expr.WhenClauses {
  560. switch r := v.Eval(w.Expr).(type) {
  561. case error:
  562. return fmt.Errorf("evaluate case expression error: %s", r)
  563. case bool:
  564. if r {
  565. return v.Eval(w.Result)
  566. }
  567. }
  568. }
  569. }
  570. if expr.ElseClause != nil {
  571. return v.Eval(expr.ElseClause)
  572. }
  573. return nil
  574. }
  575. func (v *ValuerEval) evalValueSet(expr *ast.ValueSetExpr) interface{} {
  576. var valueSet []interface{}
  577. if expr.LiteralExprs != nil {
  578. for _, exp := range expr.LiteralExprs {
  579. valueSet = append(valueSet, v.Eval(exp))
  580. }
  581. return valueSet
  582. }
  583. value := v.Eval(expr.ArrayExpr)
  584. if isSliceOrArray(value) {
  585. return value
  586. }
  587. return nil
  588. }
  589. func (v *ValuerEval) evalSetsExpr(lhs interface{}, op ast.Token, rhsSet interface{}) interface{} {
  590. switch op {
  591. /*Semantic rules
  592. When using the IN operator, the following semantics apply in this order:
  593. Returns FALSE if value_set is empty.
  594. Returns NULL if search_value is NULL.
  595. Returns TRUE if value_set contains a value equal to search_value.
  596. Returns NULL if value_set contains a NULL.
  597. Returns FALSE.
  598. When using the NOT IN operator, the following semantics apply in this order:
  599. Returns TRUE if value_set is empty.
  600. Returns NULL if search_value is NULL.
  601. Returns FALSE if value_set contains a value equal to search_value.
  602. Returns NULL if value_set contains a NULL.
  603. Returns TRUE.
  604. */
  605. case ast.IN, ast.NOTIN:
  606. if rhsSet == nil {
  607. if op == ast.IN {
  608. return false
  609. } else {
  610. return true
  611. }
  612. }
  613. if lhs == nil {
  614. return nil
  615. }
  616. rhsSetVals := reflect.ValueOf(rhsSet)
  617. for i := 0; i < rhsSetVals.Len(); i++ {
  618. switch r := v.simpleDataEval(lhs, rhsSetVals.Index(i).Interface(), ast.EQ).(type) {
  619. case error:
  620. return fmt.Errorf("evaluate in expression error: %s", r)
  621. case bool:
  622. if r {
  623. if op == ast.IN {
  624. return true
  625. } else {
  626. return false
  627. }
  628. }
  629. }
  630. }
  631. if op == ast.IN {
  632. return false
  633. } else {
  634. return true
  635. }
  636. default:
  637. return fmt.Errorf("%v is an invalid operation for %T", op, lhs)
  638. }
  639. }
  640. func (v *ValuerEval) evalJsonExpr(result interface{}, op ast.Token, expr ast.Expr) interface{} {
  641. switch op {
  642. case ast.ARROW:
  643. if val, ok := result.(map[string]interface{}); ok {
  644. switch e := expr.(type) {
  645. case *ast.JsonFieldRef:
  646. ve := &ValuerEval{Valuer: Message(val)}
  647. return ve.Eval(e)
  648. default:
  649. return fmt.Errorf("the right expression is not a field reference node")
  650. }
  651. } else {
  652. return fmt.Errorf("the result %v is not a type of map[string]interface{}", result)
  653. }
  654. case ast.SUBSET:
  655. if isSliceOrArray(result) {
  656. return v.subset(result, expr)
  657. } else {
  658. return fmt.Errorf("%v is an invalid operation for %T", op, result)
  659. }
  660. default:
  661. return fmt.Errorf("%v is an invalid operation for %T", op, result)
  662. }
  663. }
  664. func (v *ValuerEval) subset(result interface{}, expr ast.Expr) interface{} {
  665. val := reflect.ValueOf(result)
  666. ber := v.Eval(expr)
  667. if berVal, ok1 := ber.(*BracketEvalResult); ok1 {
  668. if berVal.isIndex() {
  669. if 0 > berVal.Start {
  670. if 0 > berVal.Start+val.Len() {
  671. return fmt.Errorf("out of index: %d of %d", berVal.Start, val.Len())
  672. }
  673. berVal.Start += val.Len()
  674. } else if berVal.Start >= val.Len() {
  675. return fmt.Errorf("out of index: %d of %d", berVal.Start, val.Len())
  676. }
  677. return val.Index(berVal.Start).Interface()
  678. } else {
  679. if 0 > berVal.Start {
  680. if 0 > berVal.Start+val.Len() {
  681. return fmt.Errorf("out of index: %d of %d", berVal.Start, val.Len())
  682. }
  683. berVal.Start += val.Len()
  684. } else if berVal.Start >= val.Len() {
  685. return fmt.Errorf("start value is out of index: %d of %d", berVal.Start, val.Len())
  686. }
  687. if math.MinInt32 == berVal.End {
  688. berVal.End = val.Len()
  689. } else if 0 > berVal.End {
  690. if 0 > berVal.End+val.Len() {
  691. return fmt.Errorf("out of index: %d of %d", berVal.End, val.Len())
  692. }
  693. berVal.End += val.Len()
  694. } else if berVal.End > val.Len() {
  695. return fmt.Errorf("end value is out of index: %d of %d", berVal.End, val.Len())
  696. } else if berVal.Start >= berVal.End {
  697. return fmt.Errorf("start cannot be greater than end. start:%d end:%d", berVal.Start, berVal.End)
  698. }
  699. return val.Slice(berVal.Start, berVal.End).Interface()
  700. }
  701. } else {
  702. return fmt.Errorf("invalid evaluation result - %v", berVal)
  703. }
  704. }
  705. // lhs and rhs are non-nil
  706. func (v *ValuerEval) simpleDataEval(lhs, rhs interface{}, op ast.Token) interface{} {
  707. if lhs == nil || rhs == nil {
  708. switch op {
  709. case ast.EQ, ast.LTE, ast.GTE:
  710. if lhs == nil && rhs == nil {
  711. return true
  712. } else {
  713. return false
  714. }
  715. case ast.NEQ:
  716. if lhs == nil && rhs == nil {
  717. return false
  718. } else {
  719. return true
  720. }
  721. case ast.LT, ast.GT:
  722. return false
  723. default:
  724. return nil
  725. }
  726. }
  727. lhs = convertNum(lhs)
  728. rhs = convertNum(rhs)
  729. // Evaluate if both sides are simple types.
  730. switch lhs := lhs.(type) {
  731. case bool:
  732. originRHS := rhs
  733. rhs, ok := originRHS.(bool)
  734. if !ok {
  735. return invalidOpError(lhs, op, originRHS)
  736. }
  737. switch op {
  738. case ast.AND:
  739. return lhs && rhs
  740. case ast.OR:
  741. return lhs || rhs
  742. case ast.BITWISE_AND:
  743. return lhs && rhs
  744. case ast.BITWISE_OR:
  745. return lhs || rhs
  746. case ast.BITWISE_XOR:
  747. return lhs != rhs
  748. case ast.EQ:
  749. return lhs == rhs
  750. case ast.NEQ:
  751. return lhs != rhs
  752. default:
  753. return invalidOpError(lhs, op, rhs)
  754. }
  755. case float64:
  756. // Try the rhs as a float64, int64, or uint64
  757. rhsf, ok := rhs.(float64)
  758. if !ok {
  759. switch val := rhs.(type) {
  760. case int64:
  761. rhsf, ok = float64(val), true
  762. case uint64:
  763. rhsf, ok = float64(val), true
  764. }
  765. }
  766. if !ok {
  767. return invalidOpError(lhs, op, rhs)
  768. }
  769. rhs := rhsf
  770. switch op {
  771. case ast.EQ:
  772. return lhs == rhs
  773. case ast.NEQ:
  774. return lhs != rhs
  775. case ast.LT:
  776. return lhs < rhs
  777. case ast.LTE:
  778. return lhs <= rhs
  779. case ast.GT:
  780. return lhs > rhs
  781. case ast.GTE:
  782. return lhs >= rhs
  783. case ast.ADD:
  784. return lhs + rhs
  785. case ast.SUB:
  786. return lhs - rhs
  787. case ast.MUL:
  788. return lhs * rhs
  789. case ast.DIV:
  790. if rhs == 0 {
  791. return fmt.Errorf("divided by zero")
  792. }
  793. return lhs / rhs
  794. case ast.MOD:
  795. if rhs == 0 {
  796. return fmt.Errorf("divided by zero")
  797. }
  798. return math.Mod(lhs, rhs)
  799. default:
  800. return invalidOpError(lhs, op, rhs)
  801. }
  802. case int64:
  803. // Try as a float64 to see if a float cast is required.
  804. switch rhs := rhs.(type) {
  805. case float64:
  806. lhs := float64(lhs)
  807. switch op {
  808. case ast.EQ:
  809. return lhs == rhs
  810. case ast.NEQ:
  811. return lhs != rhs
  812. case ast.LT:
  813. return lhs < rhs
  814. case ast.LTE:
  815. return lhs <= rhs
  816. case ast.GT:
  817. return lhs > rhs
  818. case ast.GTE:
  819. return lhs >= rhs
  820. case ast.ADD:
  821. return lhs + rhs
  822. case ast.SUB:
  823. return lhs - rhs
  824. case ast.MUL:
  825. return lhs * rhs
  826. case ast.DIV:
  827. if rhs == 0 {
  828. return fmt.Errorf("divided by zero")
  829. }
  830. return lhs / rhs
  831. case ast.MOD:
  832. if rhs == 0 {
  833. return fmt.Errorf("divided by zero")
  834. }
  835. return math.Mod(lhs, rhs)
  836. default:
  837. return invalidOpError(lhs, op, rhs)
  838. }
  839. case int64:
  840. switch op {
  841. case ast.EQ:
  842. return lhs == rhs
  843. case ast.NEQ:
  844. return lhs != rhs
  845. case ast.LT:
  846. return lhs < rhs
  847. case ast.LTE:
  848. return lhs <= rhs
  849. case ast.GT:
  850. return lhs > rhs
  851. case ast.GTE:
  852. return lhs >= rhs
  853. case ast.ADD:
  854. return lhs + rhs
  855. case ast.SUB:
  856. return lhs - rhs
  857. case ast.MUL:
  858. return lhs * rhs
  859. case ast.DIV:
  860. if v.IntegerFloatDivision {
  861. if rhs == 0 {
  862. return fmt.Errorf("divided by zero")
  863. }
  864. return float64(lhs) / float64(rhs)
  865. }
  866. if rhs == 0 {
  867. return fmt.Errorf("divided by zero")
  868. }
  869. return lhs / rhs
  870. case ast.MOD:
  871. if rhs == 0 {
  872. return fmt.Errorf("divided by zero")
  873. }
  874. return lhs % rhs
  875. case ast.BITWISE_AND:
  876. return lhs & rhs
  877. case ast.BITWISE_OR:
  878. return lhs | rhs
  879. case ast.BITWISE_XOR:
  880. return lhs ^ rhs
  881. default:
  882. return invalidOpError(lhs, op, rhs)
  883. }
  884. case uint64:
  885. switch op {
  886. case ast.EQ:
  887. return uint64(lhs) == rhs
  888. case ast.NEQ:
  889. return uint64(lhs) != rhs
  890. case ast.LT:
  891. if lhs < 0 {
  892. return true
  893. }
  894. return uint64(lhs) < rhs
  895. case ast.LTE:
  896. if lhs < 0 {
  897. return true
  898. }
  899. return uint64(lhs) <= rhs
  900. case ast.GT:
  901. if lhs < 0 {
  902. return false
  903. }
  904. return uint64(lhs) > rhs
  905. case ast.GTE:
  906. if lhs < 0 {
  907. return false
  908. }
  909. return uint64(lhs) >= rhs
  910. case ast.ADD:
  911. return uint64(lhs) + rhs
  912. case ast.SUB:
  913. return uint64(lhs) - rhs
  914. case ast.MUL:
  915. return uint64(lhs) * rhs
  916. case ast.DIV:
  917. if rhs == 0 {
  918. return fmt.Errorf("divided by zero")
  919. }
  920. return uint64(lhs) / rhs
  921. case ast.MOD:
  922. if rhs == 0 {
  923. return fmt.Errorf("divided by zero")
  924. }
  925. return uint64(lhs) % rhs
  926. case ast.BITWISE_AND:
  927. return uint64(lhs) & rhs
  928. case ast.BITWISE_OR:
  929. return uint64(lhs) | rhs
  930. case ast.BITWISE_XOR:
  931. return uint64(lhs) ^ rhs
  932. default:
  933. return invalidOpError(lhs, op, rhs)
  934. }
  935. default:
  936. return invalidOpError(lhs, op, rhs)
  937. }
  938. case uint64:
  939. // Try as a float64 to see if a float cast is required.
  940. switch rhs := rhs.(type) {
  941. case float64:
  942. lhs := float64(lhs)
  943. switch op {
  944. case ast.EQ:
  945. return lhs == rhs
  946. case ast.NEQ:
  947. return lhs != rhs
  948. case ast.LT:
  949. return lhs < rhs
  950. case ast.LTE:
  951. return lhs <= rhs
  952. case ast.GT:
  953. return lhs > rhs
  954. case ast.GTE:
  955. return lhs >= rhs
  956. case ast.ADD:
  957. return lhs + rhs
  958. case ast.SUB:
  959. return lhs - rhs
  960. case ast.MUL:
  961. return lhs * rhs
  962. case ast.DIV:
  963. if rhs == 0 {
  964. return fmt.Errorf("divided by zero")
  965. }
  966. return lhs / rhs
  967. case ast.MOD:
  968. if rhs == 0 {
  969. return fmt.Errorf("divided by zero")
  970. }
  971. return math.Mod(lhs, rhs)
  972. default:
  973. return invalidOpError(lhs, op, rhs)
  974. }
  975. case int64:
  976. switch op {
  977. case ast.EQ:
  978. return lhs == uint64(rhs)
  979. case ast.NEQ:
  980. return lhs != uint64(rhs)
  981. case ast.LT:
  982. if rhs < 0 {
  983. return false
  984. }
  985. return lhs < uint64(rhs)
  986. case ast.LTE:
  987. if rhs < 0 {
  988. return false
  989. }
  990. return lhs <= uint64(rhs)
  991. case ast.GT:
  992. if rhs < 0 {
  993. return true
  994. }
  995. return lhs > uint64(rhs)
  996. case ast.GTE:
  997. if rhs < 0 {
  998. return true
  999. }
  1000. return lhs >= uint64(rhs)
  1001. case ast.ADD:
  1002. return lhs + uint64(rhs)
  1003. case ast.SUB:
  1004. return lhs - uint64(rhs)
  1005. case ast.MUL:
  1006. return lhs * uint64(rhs)
  1007. case ast.DIV:
  1008. if rhs == 0 {
  1009. return fmt.Errorf("divided by zero")
  1010. }
  1011. return lhs / uint64(rhs)
  1012. case ast.MOD:
  1013. if rhs == 0 {
  1014. return fmt.Errorf("divided by zero")
  1015. }
  1016. return lhs % uint64(rhs)
  1017. case ast.BITWISE_AND:
  1018. return lhs & uint64(rhs)
  1019. case ast.BITWISE_OR:
  1020. return lhs | uint64(rhs)
  1021. case ast.BITWISE_XOR:
  1022. return lhs ^ uint64(rhs)
  1023. default:
  1024. return invalidOpError(lhs, op, rhs)
  1025. }
  1026. case uint64:
  1027. switch op {
  1028. case ast.EQ:
  1029. return lhs == rhs
  1030. case ast.NEQ:
  1031. return lhs != rhs
  1032. case ast.LT:
  1033. return lhs < rhs
  1034. case ast.LTE:
  1035. return lhs <= rhs
  1036. case ast.GT:
  1037. return lhs > rhs
  1038. case ast.GTE:
  1039. return lhs >= rhs
  1040. case ast.ADD:
  1041. return lhs + rhs
  1042. case ast.SUB:
  1043. return lhs - rhs
  1044. case ast.MUL:
  1045. return lhs * rhs
  1046. case ast.DIV:
  1047. if rhs == 0 {
  1048. return fmt.Errorf("divided by zero")
  1049. }
  1050. return lhs / rhs
  1051. case ast.MOD:
  1052. if rhs == 0 {
  1053. return fmt.Errorf("divided by zero")
  1054. }
  1055. return lhs % rhs
  1056. case ast.BITWISE_AND:
  1057. return lhs & rhs
  1058. case ast.BITWISE_OR:
  1059. return lhs | rhs
  1060. case ast.BITWISE_XOR:
  1061. return lhs ^ rhs
  1062. default:
  1063. return invalidOpError(lhs, op, rhs)
  1064. }
  1065. default:
  1066. return invalidOpError(lhs, op, rhs)
  1067. }
  1068. case string:
  1069. rhss, ok := rhs.(string)
  1070. if !ok {
  1071. return invalidOpError(lhs, op, rhs)
  1072. }
  1073. switch op {
  1074. case ast.EQ:
  1075. return lhs == rhss
  1076. case ast.NEQ:
  1077. return lhs != rhss
  1078. case ast.LT:
  1079. return lhs < rhss
  1080. case ast.LTE:
  1081. return lhs <= rhss
  1082. case ast.GT:
  1083. return lhs > rhss
  1084. case ast.GTE:
  1085. return lhs >= rhss
  1086. default:
  1087. return invalidOpError(lhs, op, rhs)
  1088. }
  1089. case time.Time:
  1090. rt, err := cast.InterfaceToTime(rhs, "")
  1091. if err != nil {
  1092. return invalidOpError(lhs, op, rhs)
  1093. }
  1094. switch op {
  1095. case ast.EQ:
  1096. return lhs.Equal(rt)
  1097. case ast.NEQ:
  1098. return !lhs.Equal(rt)
  1099. case ast.LT:
  1100. return lhs.Before(rt)
  1101. case ast.LTE:
  1102. return lhs.Before(rt) || lhs.Equal(rt)
  1103. case ast.GT:
  1104. return lhs.After(rt)
  1105. case ast.GTE:
  1106. return lhs.After(rt) || lhs.Equal(rt)
  1107. default:
  1108. return invalidOpError(lhs, op, rhs)
  1109. }
  1110. default:
  1111. return invalidOpError(lhs, op, rhs)
  1112. }
  1113. }
  1114. /*
  1115. * Helper functions
  1116. */
  1117. type BracketEvalResult struct {
  1118. Start, End int
  1119. }
  1120. func (ber *BracketEvalResult) isIndex() bool {
  1121. return ber.Start == ber.End
  1122. }
  1123. func isSliceOrArray(v interface{}) bool {
  1124. kind := reflect.ValueOf(v).Kind()
  1125. return kind == reflect.Array || kind == reflect.Slice
  1126. }
  1127. func isSetOperator(op ast.Token) bool {
  1128. return op == ast.IN || op == ast.NOTIN
  1129. }
  1130. func invalidOpError(lhs interface{}, op ast.Token, rhs interface{}) error {
  1131. return fmt.Errorf("invalid operation %[1]T(%[1]v) %s %[3]T(%[3]v)", lhs, ast.Tokens[op], rhs)
  1132. }
  1133. func convertNum(para interface{}) interface{} {
  1134. if isInt(para) {
  1135. para = toInt64(para)
  1136. } else if isFloat(para) {
  1137. para = toFloat64(para)
  1138. }
  1139. return para
  1140. }
  1141. func isInt(para interface{}) bool {
  1142. switch para.(type) {
  1143. case int:
  1144. return true
  1145. case int8:
  1146. return true
  1147. case int16:
  1148. return true
  1149. case int32:
  1150. return true
  1151. case int64:
  1152. return true
  1153. }
  1154. return false
  1155. }
  1156. func toInt64(para interface{}) int64 {
  1157. if v, ok := para.(int); ok {
  1158. return int64(v)
  1159. } else if v, ok := para.(int8); ok {
  1160. return int64(v)
  1161. } else if v, ok := para.(int16); ok {
  1162. return int64(v)
  1163. } else if v, ok := para.(int32); ok {
  1164. return int64(v)
  1165. } else if v, ok := para.(int64); ok {
  1166. return v
  1167. }
  1168. return 0
  1169. }
  1170. func isFloat(para interface{}) bool {
  1171. switch para.(type) {
  1172. case float32:
  1173. return true
  1174. case float64:
  1175. return true
  1176. }
  1177. return false
  1178. }
  1179. func toFloat64(para interface{}) float64 {
  1180. if v, ok := para.(float32); ok {
  1181. return float64(v)
  1182. } else if v, ok := para.(float64); ok {
  1183. return v
  1184. }
  1185. return 0
  1186. }