valuer.go 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231
  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, ok := v.Valuer.Value(expr.CachedField, "")
  254. if ok {
  255. return val
  256. } else {
  257. return fmt.Errorf("call %s error: %v", expr.Name, val)
  258. }
  259. }
  260. if _, ok := implicitValueFuncs[expr.Name]; ok {
  261. if vv, ok := v.Valuer.(FuncValuer); ok {
  262. val, ok := vv.FuncValue(expr.Name)
  263. if ok {
  264. return val
  265. }
  266. }
  267. } else {
  268. if valuer, ok := v.Valuer.(CallValuer); ok {
  269. var (
  270. args []interface{}
  271. ft = expr.FuncType
  272. )
  273. if _, ok := ImplicitStateFuncs[expr.Name]; ok {
  274. args = make([]interface{}, 1)
  275. // This is the implicit arg set by the filter planner
  276. // If set, it will only return the value, no updating the value
  277. if expr.Cached {
  278. args[0] = false
  279. } else {
  280. args[0] = true
  281. }
  282. if expr.Name == "last_hit_time" || expr.Name == "last_agg_hit_time" {
  283. if vv, ok := v.Valuer.(FuncValuer); ok {
  284. val, ok := vv.FuncValue("event_time")
  285. if ok {
  286. args = append(args, val)
  287. } else {
  288. return fmt.Errorf("call %s error: %v", expr.Name, val)
  289. }
  290. } else {
  291. return fmt.Errorf("call %s error: %v", expr.Name, "cannot get current time")
  292. }
  293. }
  294. val, _ := valuer.Call(expr.Name, expr.FuncId, args)
  295. return val
  296. }
  297. if len(expr.Args) > 0 {
  298. switch ft {
  299. case ast.FuncTypeAgg:
  300. args = make([]interface{}, len(expr.Args))
  301. for i, arg := range expr.Args {
  302. if aggreValuer, ok := valuer.(AggregateCallValuer); ok {
  303. args[i] = aggreValuer.GetAllTuples().AggregateEval(arg, aggreValuer.GetSingleCallValuer())
  304. } else {
  305. args[i] = v.Eval(arg)
  306. if _, ok := args[i].(error); ok {
  307. return args[i]
  308. }
  309. }
  310. }
  311. case ast.FuncTypeScalar, ast.FuncTypeSrf:
  312. args = make([]interface{}, len(expr.Args))
  313. for i, arg := range expr.Args {
  314. args[i] = v.Eval(arg)
  315. if _, ok := args[i].(error); ok {
  316. return args[i]
  317. }
  318. }
  319. case ast.FuncTypeCols:
  320. var keys []string
  321. for _, arg := range expr.Args { // In the parser, the col func arguments must be ColField
  322. cf, ok := arg.(*ast.ColFuncField)
  323. if !ok {
  324. // won't happen
  325. return fmt.Errorf("expect colFuncField but got %v", arg)
  326. }
  327. temp := v.Eval(cf.Expr)
  328. if _, ok := temp.(error); ok {
  329. return temp
  330. }
  331. switch cf.Expr.(type) {
  332. case *ast.Wildcard:
  333. m, ok := temp.(map[string]interface{})
  334. if !ok {
  335. return fmt.Errorf("wildcarder return non map result")
  336. }
  337. for kk, vv := range m {
  338. args = append(args, vv)
  339. keys = append(keys, kk)
  340. }
  341. default:
  342. args = append(args, temp)
  343. keys = append(keys, cf.Name)
  344. }
  345. }
  346. args = append(args, keys)
  347. default:
  348. // won't happen
  349. return fmt.Errorf("unknown function type")
  350. }
  351. }
  352. if function.IsAnalyticFunc(expr.Name) {
  353. // this data should be recorded or not ? default answer is yes
  354. if expr.WhenExpr != nil {
  355. validData := true
  356. temp := v.Eval(expr.WhenExpr)
  357. whenExprVal, ok := temp.(bool)
  358. if ok {
  359. validData = whenExprVal
  360. }
  361. args = append(args, validData)
  362. } else {
  363. args = append(args, true)
  364. }
  365. // analytic func must put the partition key into the args
  366. if expr.Partition != nil && len(expr.Partition.Exprs) > 0 {
  367. pk := ""
  368. for _, pe := range expr.Partition.Exprs {
  369. temp := v.Eval(pe)
  370. if _, ok := temp.(error); ok {
  371. return temp
  372. }
  373. pk += fmt.Sprintf("%v", temp)
  374. }
  375. args = append(args, pk)
  376. } else {
  377. args = append(args, "self")
  378. }
  379. }
  380. val, _ := valuer.Call(expr.Name, expr.FuncId, args)
  381. return val
  382. }
  383. }
  384. return nil
  385. case *ast.FieldRef:
  386. var t, n string
  387. if expr.IsAlias() {
  388. if valuer, ok := v.Valuer.(AliasValuer); ok {
  389. val, ok := valuer.AliasValue(expr.Name)
  390. if ok {
  391. return val
  392. } else {
  393. r := v.Eval(expr.Expression)
  394. // TODO possible performance elevation to eliminate this cal
  395. valuer.AppendAlias(expr.Name, r)
  396. return r
  397. }
  398. }
  399. } else if expr.StreamName == ast.DefaultStream {
  400. n = expr.Name
  401. } else {
  402. t = string(expr.StreamName)
  403. n = expr.Name
  404. }
  405. if n != "" {
  406. val, ok := v.Valuer.Value(n, t)
  407. if ok {
  408. return val
  409. }
  410. }
  411. return nil
  412. case *ast.MetaRef:
  413. if expr.StreamName == "" || expr.StreamName == ast.DefaultStream {
  414. val, _ := v.Valuer.Meta(expr.Name, "")
  415. return val
  416. } else {
  417. // The field specified with stream source
  418. val, _ := v.Valuer.Meta(expr.Name, string(expr.StreamName))
  419. return val
  420. }
  421. case *ast.JsonFieldRef:
  422. val, ok := v.Valuer.Value(expr.Name, "")
  423. if ok {
  424. return val
  425. } else {
  426. return nil
  427. }
  428. case *ast.Wildcard:
  429. val, _ := v.Valuer.Value("*", "")
  430. return val
  431. case *ast.CaseExpr:
  432. return v.evalCase(expr)
  433. case *ast.ValueSetExpr:
  434. return v.evalValueSet(expr)
  435. case *ast.BetweenExpr:
  436. return []interface{}{
  437. v.Eval(expr.Lower), v.Eval(expr.Higher),
  438. }
  439. case *ast.LikePattern:
  440. if expr.Pattern != nil {
  441. return expr.Pattern
  442. }
  443. v := v.Eval(expr.Expr)
  444. str, ok := v.(string)
  445. if !ok {
  446. return fmt.Errorf("invalid LIKE pattern, must be a string but got %v", v)
  447. }
  448. re, err := expr.Compile(str)
  449. if err != nil {
  450. return err
  451. }
  452. return re
  453. default:
  454. return nil
  455. }
  456. }
  457. func (v *ValuerEval) evalBinaryExpr(expr *ast.BinaryExpr) interface{} {
  458. lhs := v.Eval(expr.LHS)
  459. switch val := lhs.(type) {
  460. case map[string]interface{}:
  461. return v.evalJsonExpr(val, expr.OP, expr.RHS)
  462. case Message:
  463. return v.evalJsonExpr(map[string]interface{}(val), expr.OP, expr.RHS)
  464. case error:
  465. return val
  466. }
  467. // shortcut for bool
  468. switch expr.OP {
  469. case ast.AND:
  470. if bv, ok := lhs.(bool); ok && !bv {
  471. return false
  472. }
  473. case ast.OR:
  474. if bv, ok := lhs.(bool); ok && bv {
  475. return true
  476. }
  477. }
  478. if isSliceOrArray(lhs) {
  479. return v.evalJsonExpr(lhs, expr.OP, expr.RHS)
  480. }
  481. rhs := v.Eval(expr.RHS)
  482. if _, ok := rhs.(error); ok {
  483. return rhs
  484. }
  485. if isSetOperator(expr.OP) {
  486. return v.evalSetsExpr(lhs, expr.OP, rhs)
  487. }
  488. switch expr.OP {
  489. case ast.BETWEEN, ast.NOTBETWEEN:
  490. arr, ok := rhs.([]interface{})
  491. if !ok {
  492. return fmt.Errorf("between operator expects two arguments, but found %v", rhs)
  493. }
  494. andLeft := v.simpleDataEval(lhs, arr[0], ast.GTE)
  495. switch andLeft.(type) {
  496. case error:
  497. return fmt.Errorf("between operator cannot compare %[1]T(%[1]v) and %[2]T(%[2]v)", lhs, arr[0])
  498. }
  499. andRight := v.simpleDataEval(lhs, arr[1], ast.LTE)
  500. switch andRight.(type) {
  501. case error:
  502. return fmt.Errorf("between operator cannot compare %[1]T(%[1]v) and %[2]T(%[2]v)", lhs, arr[1])
  503. }
  504. r := v.simpleDataEval(andLeft, andRight, ast.AND)
  505. br, ok := r.(bool)
  506. if expr.OP == ast.NOTBETWEEN && ok {
  507. return !br
  508. } else {
  509. return r
  510. }
  511. case ast.LIKE, ast.NOTLIKE:
  512. ls, ok := lhs.(string)
  513. if !ok {
  514. return fmt.Errorf("LIKE operator left operand expects string, but found %v", lhs)
  515. }
  516. var result bool
  517. rs, ok := rhs.(*regexp.Regexp)
  518. if !ok {
  519. return fmt.Errorf("LIKE operator right operand expects string, but found %v", rhs)
  520. }
  521. result = rs.MatchString(ls)
  522. if expr.OP == ast.NOTLIKE {
  523. result = !result
  524. }
  525. return result
  526. default:
  527. return v.simpleDataEval(lhs, rhs, expr.OP)
  528. }
  529. }
  530. func (v *ValuerEval) evalCase(expr *ast.CaseExpr) interface{} {
  531. if expr.Value != nil { // compare value to all when clause
  532. ev := v.Eval(expr.Value)
  533. for _, w := range expr.WhenClauses {
  534. wv := v.Eval(w.Expr)
  535. switch r := v.simpleDataEval(ev, wv, ast.EQ).(type) {
  536. case error:
  537. return fmt.Errorf("evaluate case expression error: %s", r)
  538. case bool:
  539. if r {
  540. return v.Eval(w.Result)
  541. }
  542. }
  543. }
  544. } else {
  545. for _, w := range expr.WhenClauses {
  546. switch r := v.Eval(w.Expr).(type) {
  547. case error:
  548. return fmt.Errorf("evaluate case expression error: %s", r)
  549. case bool:
  550. if r {
  551. return v.Eval(w.Result)
  552. }
  553. }
  554. }
  555. }
  556. if expr.ElseClause != nil {
  557. return v.Eval(expr.ElseClause)
  558. }
  559. return nil
  560. }
  561. func (v *ValuerEval) evalValueSet(expr *ast.ValueSetExpr) interface{} {
  562. var valueSet []interface{}
  563. if expr.LiteralExprs != nil {
  564. for _, exp := range expr.LiteralExprs {
  565. valueSet = append(valueSet, v.Eval(exp))
  566. }
  567. return valueSet
  568. }
  569. value := v.Eval(expr.ArrayExpr)
  570. if isSliceOrArray(value) {
  571. return value
  572. }
  573. return nil
  574. }
  575. func (v *ValuerEval) evalSetsExpr(lhs interface{}, op ast.Token, rhsSet interface{}) interface{} {
  576. switch op {
  577. /*Semantic rules
  578. When using the IN operator, the following semantics apply in this order:
  579. Returns FALSE if value_set is empty.
  580. Returns NULL if search_value is NULL.
  581. Returns TRUE if value_set contains a value equal to search_value.
  582. Returns NULL if value_set contains a NULL.
  583. Returns FALSE.
  584. When using the NOT IN operator, the following semantics apply in this order:
  585. Returns TRUE if value_set is empty.
  586. Returns NULL if search_value is NULL.
  587. Returns FALSE if value_set contains a value equal to search_value.
  588. Returns NULL if value_set contains a NULL.
  589. Returns TRUE.
  590. */
  591. case ast.IN, ast.NOTIN:
  592. if rhsSet == nil {
  593. if op == ast.IN {
  594. return false
  595. } else {
  596. return true
  597. }
  598. }
  599. if lhs == nil {
  600. return nil
  601. }
  602. rhsSetVals := reflect.ValueOf(rhsSet)
  603. for i := 0; i < rhsSetVals.Len(); i++ {
  604. switch r := v.simpleDataEval(lhs, rhsSetVals.Index(i).Interface(), ast.EQ).(type) {
  605. case error:
  606. return fmt.Errorf("evaluate in expression error: %s", r)
  607. case bool:
  608. if r {
  609. if op == ast.IN {
  610. return true
  611. } else {
  612. return false
  613. }
  614. }
  615. }
  616. }
  617. if op == ast.IN {
  618. return false
  619. } else {
  620. return true
  621. }
  622. default:
  623. return fmt.Errorf("%v is an invalid operation for %T", op, lhs)
  624. }
  625. }
  626. func (v *ValuerEval) evalJsonExpr(result interface{}, op ast.Token, expr ast.Expr) interface{} {
  627. switch op {
  628. case ast.ARROW:
  629. if val, ok := result.(map[string]interface{}); ok {
  630. switch e := expr.(type) {
  631. case *ast.JsonFieldRef:
  632. ve := &ValuerEval{Valuer: Message(val)}
  633. return ve.Eval(e)
  634. default:
  635. return fmt.Errorf("the right expression is not a field reference node")
  636. }
  637. } else {
  638. return fmt.Errorf("the result %v is not a type of map[string]interface{}", result)
  639. }
  640. case ast.SUBSET:
  641. if isSliceOrArray(result) {
  642. return v.subset(result, expr)
  643. } else {
  644. return fmt.Errorf("%v is an invalid operation for %T", op, result)
  645. }
  646. default:
  647. return fmt.Errorf("%v is an invalid operation for %T", op, result)
  648. }
  649. }
  650. func (v *ValuerEval) subset(result interface{}, expr ast.Expr) interface{} {
  651. val := reflect.ValueOf(result)
  652. ber := v.Eval(expr)
  653. if berVal, ok1 := ber.(*BracketEvalResult); ok1 {
  654. if berVal.isIndex() {
  655. if 0 > berVal.Start {
  656. if 0 > berVal.Start+val.Len() {
  657. return fmt.Errorf("out of index: %d of %d", berVal.Start, val.Len())
  658. }
  659. berVal.Start += val.Len()
  660. } else if berVal.Start >= val.Len() {
  661. return fmt.Errorf("out of index: %d of %d", berVal.Start, val.Len())
  662. }
  663. return val.Index(berVal.Start).Interface()
  664. } else {
  665. if 0 > berVal.Start {
  666. if 0 > berVal.Start+val.Len() {
  667. return fmt.Errorf("out of index: %d of %d", berVal.Start, val.Len())
  668. }
  669. berVal.Start += val.Len()
  670. } else if berVal.Start >= val.Len() {
  671. return fmt.Errorf("start value is out of index: %d of %d", berVal.Start, val.Len())
  672. }
  673. if math.MinInt32 == berVal.End {
  674. berVal.End = val.Len()
  675. } else if 0 > berVal.End {
  676. if 0 > berVal.End+val.Len() {
  677. return fmt.Errorf("out of index: %d of %d", berVal.End, val.Len())
  678. }
  679. berVal.End += val.Len()
  680. } else if berVal.End > val.Len() {
  681. return fmt.Errorf("end value is out of index: %d of %d", berVal.End, val.Len())
  682. } else if berVal.Start >= berVal.End {
  683. return fmt.Errorf("start cannot be greater than end. start:%d end:%d", berVal.Start, berVal.End)
  684. }
  685. return val.Slice(berVal.Start, berVal.End).Interface()
  686. }
  687. } else {
  688. return fmt.Errorf("invalid evaluation result - %v", berVal)
  689. }
  690. }
  691. // lhs and rhs are non-nil
  692. func (v *ValuerEval) simpleDataEval(lhs, rhs interface{}, op ast.Token) interface{} {
  693. if lhs == nil || rhs == nil {
  694. switch op {
  695. case ast.EQ, ast.LTE, ast.GTE:
  696. if lhs == nil && rhs == nil {
  697. return true
  698. } else {
  699. return false
  700. }
  701. case ast.NEQ:
  702. if lhs == nil && rhs == nil {
  703. return false
  704. } else {
  705. return true
  706. }
  707. case ast.LT, ast.GT:
  708. return false
  709. default:
  710. return nil
  711. }
  712. }
  713. lhs = convertNum(lhs)
  714. rhs = convertNum(rhs)
  715. // Evaluate if both sides are simple types.
  716. switch lhs := lhs.(type) {
  717. case bool:
  718. rhs, ok := rhs.(bool)
  719. if !ok {
  720. return invalidOpError(lhs, op, rhs)
  721. }
  722. switch op {
  723. case ast.AND:
  724. return lhs && rhs
  725. case ast.OR:
  726. return lhs || rhs
  727. case ast.BITWISE_AND:
  728. return lhs && rhs
  729. case ast.BITWISE_OR:
  730. return lhs || rhs
  731. case ast.BITWISE_XOR:
  732. return lhs != rhs
  733. case ast.EQ:
  734. return lhs == rhs
  735. case ast.NEQ:
  736. return lhs != rhs
  737. default:
  738. return invalidOpError(lhs, op, rhs)
  739. }
  740. case float64:
  741. // Try the rhs as a float64, int64, or uint64
  742. rhsf, ok := rhs.(float64)
  743. if !ok {
  744. switch val := rhs.(type) {
  745. case int64:
  746. rhsf, ok = float64(val), true
  747. case uint64:
  748. rhsf, ok = float64(val), true
  749. }
  750. }
  751. if !ok {
  752. return invalidOpError(lhs, op, rhs)
  753. }
  754. rhs := rhsf
  755. switch op {
  756. case ast.EQ:
  757. return lhs == rhs
  758. case ast.NEQ:
  759. return lhs != rhs
  760. case ast.LT:
  761. return lhs < rhs
  762. case ast.LTE:
  763. return lhs <= rhs
  764. case ast.GT:
  765. return lhs > rhs
  766. case ast.GTE:
  767. return lhs >= rhs
  768. case ast.ADD:
  769. return lhs + rhs
  770. case ast.SUB:
  771. return lhs - rhs
  772. case ast.MUL:
  773. return lhs * rhs
  774. case ast.DIV:
  775. if rhs == 0 {
  776. return fmt.Errorf("divided by zero")
  777. }
  778. return lhs / rhs
  779. case ast.MOD:
  780. if rhs == 0 {
  781. return fmt.Errorf("divided by zero")
  782. }
  783. return math.Mod(lhs, rhs)
  784. default:
  785. return invalidOpError(lhs, op, rhs)
  786. }
  787. case int64:
  788. // Try as a float64 to see if a float cast is required.
  789. switch rhs := rhs.(type) {
  790. case float64:
  791. lhs := float64(lhs)
  792. switch op {
  793. case ast.EQ:
  794. return lhs == rhs
  795. case ast.NEQ:
  796. return lhs != rhs
  797. case ast.LT:
  798. return lhs < rhs
  799. case ast.LTE:
  800. return lhs <= rhs
  801. case ast.GT:
  802. return lhs > rhs
  803. case ast.GTE:
  804. return lhs >= rhs
  805. case ast.ADD:
  806. return lhs + rhs
  807. case ast.SUB:
  808. return lhs - rhs
  809. case ast.MUL:
  810. return lhs * rhs
  811. case ast.DIV:
  812. if rhs == 0 {
  813. return fmt.Errorf("divided by zero")
  814. }
  815. return lhs / rhs
  816. case ast.MOD:
  817. if rhs == 0 {
  818. return fmt.Errorf("divided by zero")
  819. }
  820. return math.Mod(lhs, rhs)
  821. default:
  822. return invalidOpError(lhs, op, rhs)
  823. }
  824. case int64:
  825. switch op {
  826. case ast.EQ:
  827. return lhs == rhs
  828. case ast.NEQ:
  829. return lhs != rhs
  830. case ast.LT:
  831. return lhs < rhs
  832. case ast.LTE:
  833. return lhs <= rhs
  834. case ast.GT:
  835. return lhs > rhs
  836. case ast.GTE:
  837. return lhs >= rhs
  838. case ast.ADD:
  839. return lhs + rhs
  840. case ast.SUB:
  841. return lhs - rhs
  842. case ast.MUL:
  843. return lhs * rhs
  844. case ast.DIV:
  845. if v.IntegerFloatDivision {
  846. if rhs == 0 {
  847. return fmt.Errorf("divided by zero")
  848. }
  849. return float64(lhs) / float64(rhs)
  850. }
  851. if rhs == 0 {
  852. return fmt.Errorf("divided by zero")
  853. }
  854. return lhs / rhs
  855. case ast.MOD:
  856. if rhs == 0 {
  857. return fmt.Errorf("divided by zero")
  858. }
  859. return lhs % rhs
  860. case ast.BITWISE_AND:
  861. return lhs & rhs
  862. case ast.BITWISE_OR:
  863. return lhs | rhs
  864. case ast.BITWISE_XOR:
  865. return lhs ^ rhs
  866. default:
  867. return invalidOpError(lhs, op, rhs)
  868. }
  869. case uint64:
  870. switch op {
  871. case ast.EQ:
  872. return uint64(lhs) == rhs
  873. case ast.NEQ:
  874. return uint64(lhs) != rhs
  875. case ast.LT:
  876. if lhs < 0 {
  877. return true
  878. }
  879. return uint64(lhs) < rhs
  880. case ast.LTE:
  881. if lhs < 0 {
  882. return true
  883. }
  884. return uint64(lhs) <= rhs
  885. case ast.GT:
  886. if lhs < 0 {
  887. return false
  888. }
  889. return uint64(lhs) > rhs
  890. case ast.GTE:
  891. if lhs < 0 {
  892. return false
  893. }
  894. return uint64(lhs) >= rhs
  895. case ast.ADD:
  896. return uint64(lhs) + rhs
  897. case ast.SUB:
  898. return uint64(lhs) - rhs
  899. case ast.MUL:
  900. return uint64(lhs) * rhs
  901. case ast.DIV:
  902. if rhs == 0 {
  903. return fmt.Errorf("divided by zero")
  904. }
  905. return uint64(lhs) / rhs
  906. case ast.MOD:
  907. if rhs == 0 {
  908. return fmt.Errorf("divided by zero")
  909. }
  910. return uint64(lhs) % rhs
  911. case ast.BITWISE_AND:
  912. return uint64(lhs) & rhs
  913. case ast.BITWISE_OR:
  914. return uint64(lhs) | rhs
  915. case ast.BITWISE_XOR:
  916. return uint64(lhs) ^ rhs
  917. default:
  918. return invalidOpError(lhs, op, rhs)
  919. }
  920. default:
  921. return invalidOpError(lhs, op, rhs)
  922. }
  923. case uint64:
  924. // Try as a float64 to see if a float cast is required.
  925. switch rhs := rhs.(type) {
  926. case float64:
  927. lhs := float64(lhs)
  928. switch op {
  929. case ast.EQ:
  930. return lhs == rhs
  931. case ast.NEQ:
  932. return lhs != rhs
  933. case ast.LT:
  934. return lhs < rhs
  935. case ast.LTE:
  936. return lhs <= rhs
  937. case ast.GT:
  938. return lhs > rhs
  939. case ast.GTE:
  940. return lhs >= rhs
  941. case ast.ADD:
  942. return lhs + rhs
  943. case ast.SUB:
  944. return lhs - rhs
  945. case ast.MUL:
  946. return lhs * rhs
  947. case ast.DIV:
  948. if rhs == 0 {
  949. return fmt.Errorf("divided by zero")
  950. }
  951. return lhs / rhs
  952. case ast.MOD:
  953. if rhs == 0 {
  954. return fmt.Errorf("divided by zero")
  955. }
  956. return math.Mod(lhs, rhs)
  957. default:
  958. return invalidOpError(lhs, op, rhs)
  959. }
  960. case int64:
  961. switch op {
  962. case ast.EQ:
  963. return lhs == uint64(rhs)
  964. case ast.NEQ:
  965. return lhs != uint64(rhs)
  966. case ast.LT:
  967. if rhs < 0 {
  968. return false
  969. }
  970. return lhs < uint64(rhs)
  971. case ast.LTE:
  972. if rhs < 0 {
  973. return false
  974. }
  975. return lhs <= uint64(rhs)
  976. case ast.GT:
  977. if rhs < 0 {
  978. return true
  979. }
  980. return lhs > uint64(rhs)
  981. case ast.GTE:
  982. if rhs < 0 {
  983. return true
  984. }
  985. return lhs >= uint64(rhs)
  986. case ast.ADD:
  987. return lhs + uint64(rhs)
  988. case ast.SUB:
  989. return lhs - uint64(rhs)
  990. case ast.MUL:
  991. return lhs * uint64(rhs)
  992. case ast.DIV:
  993. if rhs == 0 {
  994. return fmt.Errorf("divided by zero")
  995. }
  996. return lhs / uint64(rhs)
  997. case ast.MOD:
  998. if rhs == 0 {
  999. return fmt.Errorf("divided by zero")
  1000. }
  1001. return lhs % uint64(rhs)
  1002. case ast.BITWISE_AND:
  1003. return lhs & uint64(rhs)
  1004. case ast.BITWISE_OR:
  1005. return lhs | uint64(rhs)
  1006. case ast.BITWISE_XOR:
  1007. return lhs ^ uint64(rhs)
  1008. default:
  1009. return invalidOpError(lhs, op, rhs)
  1010. }
  1011. case uint64:
  1012. switch op {
  1013. case ast.EQ:
  1014. return lhs == rhs
  1015. case ast.NEQ:
  1016. return lhs != rhs
  1017. case ast.LT:
  1018. return lhs < rhs
  1019. case ast.LTE:
  1020. return lhs <= rhs
  1021. case ast.GT:
  1022. return lhs > rhs
  1023. case ast.GTE:
  1024. return lhs >= rhs
  1025. case ast.ADD:
  1026. return lhs + rhs
  1027. case ast.SUB:
  1028. return lhs - rhs
  1029. case ast.MUL:
  1030. return lhs * rhs
  1031. case ast.DIV:
  1032. if rhs == 0 {
  1033. return fmt.Errorf("divided by zero")
  1034. }
  1035. return lhs / rhs
  1036. case ast.MOD:
  1037. if rhs == 0 {
  1038. return fmt.Errorf("divided by zero")
  1039. }
  1040. return lhs % rhs
  1041. case ast.BITWISE_AND:
  1042. return lhs & rhs
  1043. case ast.BITWISE_OR:
  1044. return lhs | rhs
  1045. case ast.BITWISE_XOR:
  1046. return lhs ^ rhs
  1047. default:
  1048. return invalidOpError(lhs, op, rhs)
  1049. }
  1050. default:
  1051. return invalidOpError(lhs, op, rhs)
  1052. }
  1053. case string:
  1054. rhss, ok := rhs.(string)
  1055. if !ok {
  1056. return invalidOpError(lhs, op, rhs)
  1057. }
  1058. switch op {
  1059. case ast.EQ:
  1060. return lhs == rhss
  1061. case ast.NEQ:
  1062. return lhs != rhss
  1063. case ast.LT:
  1064. return lhs < rhss
  1065. case ast.LTE:
  1066. return lhs <= rhss
  1067. case ast.GT:
  1068. return lhs > rhss
  1069. case ast.GTE:
  1070. return lhs >= rhss
  1071. default:
  1072. return invalidOpError(lhs, op, rhs)
  1073. }
  1074. case time.Time:
  1075. rt, err := cast.InterfaceToTime(rhs, "")
  1076. if err != nil {
  1077. return invalidOpError(lhs, op, rhs)
  1078. }
  1079. switch op {
  1080. case ast.EQ:
  1081. return lhs.Equal(rt)
  1082. case ast.NEQ:
  1083. return !lhs.Equal(rt)
  1084. case ast.LT:
  1085. return lhs.Before(rt)
  1086. case ast.LTE:
  1087. return lhs.Before(rt) || lhs.Equal(rt)
  1088. case ast.GT:
  1089. return lhs.After(rt)
  1090. case ast.GTE:
  1091. return lhs.After(rt) || lhs.Equal(rt)
  1092. default:
  1093. return invalidOpError(lhs, op, rhs)
  1094. }
  1095. default:
  1096. return invalidOpError(lhs, op, rhs)
  1097. }
  1098. }
  1099. /*
  1100. * Helper functions
  1101. */
  1102. type BracketEvalResult struct {
  1103. Start, End int
  1104. }
  1105. func (ber *BracketEvalResult) isIndex() bool {
  1106. return ber.Start == ber.End
  1107. }
  1108. func isSliceOrArray(v interface{}) bool {
  1109. kind := reflect.ValueOf(v).Kind()
  1110. return kind == reflect.Array || kind == reflect.Slice
  1111. }
  1112. func isSetOperator(op ast.Token) bool {
  1113. return op == ast.IN || op == ast.NOTIN
  1114. }
  1115. func invalidOpError(lhs interface{}, op ast.Token, rhs interface{}) error {
  1116. return fmt.Errorf("invalid operation %[1]T(%[1]v) %s %[3]T(%[3]v)", lhs, ast.Tokens[op], rhs)
  1117. }
  1118. func convertNum(para interface{}) interface{} {
  1119. if isInt(para) {
  1120. para = toInt64(para)
  1121. } else if isFloat(para) {
  1122. para = toFloat64(para)
  1123. }
  1124. return para
  1125. }
  1126. func isInt(para interface{}) bool {
  1127. switch para.(type) {
  1128. case int:
  1129. return true
  1130. case int8:
  1131. return true
  1132. case int16:
  1133. return true
  1134. case int32:
  1135. return true
  1136. case int64:
  1137. return true
  1138. }
  1139. return false
  1140. }
  1141. func toInt64(para interface{}) int64 {
  1142. if v, ok := para.(int); ok {
  1143. return int64(v)
  1144. } else if v, ok := para.(int8); ok {
  1145. return int64(v)
  1146. } else if v, ok := para.(int16); ok {
  1147. return int64(v)
  1148. } else if v, ok := para.(int32); ok {
  1149. return int64(v)
  1150. } else if v, ok := para.(int64); ok {
  1151. return v
  1152. }
  1153. return 0
  1154. }
  1155. func isFloat(para interface{}) bool {
  1156. switch para.(type) {
  1157. case float32:
  1158. return true
  1159. case float64:
  1160. return true
  1161. }
  1162. return false
  1163. }
  1164. func toFloat64(para interface{}) float64 {
  1165. if v, ok := para.(float32); ok {
  1166. return float64(v)
  1167. } else if v, ok := para.(float64); ok {
  1168. return v
  1169. }
  1170. return 0
  1171. }