valuer.go 26 KB

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