valuer.go 28 KB

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