valuer.go 26 KB

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