valuer.go 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196
  1. // Copyright 2022-2023 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package xsql
  15. import (
  16. "fmt"
  17. "math"
  18. "reflect"
  19. "regexp"
  20. "time"
  21. "github.com/lf-edge/ekuiper/internal/binder/function"
  22. "github.com/lf-edge/ekuiper/pkg/ast"
  23. "github.com/lf-edge/ekuiper/pkg/cast"
  24. )
  25. var 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 functions 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, ast.FuncTypeSrf:
  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.(map[string]interface{})
  299. if !ok {
  300. return fmt.Errorf("wildcarder return non map 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 t, n string
  352. if expr.IsAlias() {
  353. if valuer, ok := v.Valuer.(AliasValuer); ok {
  354. val, ok := valuer.AliasValue(expr.Name)
  355. if ok {
  356. return val
  357. } else {
  358. r := v.Eval(expr.Expression)
  359. // TODO possible performance elevation to eliminate this cal
  360. valuer.AppendAlias(expr.Name, r)
  361. return r
  362. }
  363. }
  364. } else if expr.StreamName == ast.DefaultStream {
  365. n = expr.Name
  366. } else {
  367. t = string(expr.StreamName)
  368. n = expr.Name
  369. }
  370. if n != "" {
  371. val, ok := v.Valuer.Value(n, t)
  372. if ok {
  373. return val
  374. }
  375. }
  376. return nil
  377. case *ast.MetaRef:
  378. if expr.StreamName == "" || expr.StreamName == ast.DefaultStream {
  379. val, _ := v.Valuer.Meta(expr.Name, "")
  380. return val
  381. } else {
  382. // The field specified with stream source
  383. val, _ := v.Valuer.Meta(expr.Name, string(expr.StreamName))
  384. return val
  385. }
  386. case *ast.JsonFieldRef:
  387. val, ok := v.Valuer.Value(expr.Name, "")
  388. if ok {
  389. return val
  390. } else {
  391. return nil
  392. }
  393. case *ast.Wildcard:
  394. val, _ := v.Valuer.Value("*", "")
  395. return val
  396. case *ast.CaseExpr:
  397. return v.evalCase(expr)
  398. case *ast.ValueSetExpr:
  399. return v.evalValueSet(expr)
  400. case *ast.BetweenExpr:
  401. return []interface{}{
  402. v.Eval(expr.Lower), v.Eval(expr.Higher),
  403. }
  404. case *ast.LikePattern:
  405. if expr.Pattern != nil {
  406. return expr.Pattern
  407. }
  408. v := v.Eval(expr.Expr)
  409. str, ok := v.(string)
  410. if !ok {
  411. return fmt.Errorf("invalid LIKE pattern, must be a string but got %v", v)
  412. }
  413. re, err := expr.Compile(str)
  414. if err != nil {
  415. return err
  416. }
  417. return re
  418. default:
  419. return nil
  420. }
  421. }
  422. func (v *ValuerEval) evalBinaryExpr(expr *ast.BinaryExpr) interface{} {
  423. lhs := v.Eval(expr.LHS)
  424. switch val := lhs.(type) {
  425. case map[string]interface{}:
  426. return v.evalJsonExpr(val, expr.OP, expr.RHS)
  427. case Message:
  428. return v.evalJsonExpr(map[string]interface{}(val), expr.OP, expr.RHS)
  429. case error:
  430. return val
  431. }
  432. // shortcut for bool
  433. switch expr.OP {
  434. case ast.AND:
  435. if bv, ok := lhs.(bool); ok && !bv {
  436. return false
  437. }
  438. case ast.OR:
  439. if bv, ok := lhs.(bool); ok && bv {
  440. return true
  441. }
  442. }
  443. if isSliceOrArray(lhs) {
  444. return v.evalJsonExpr(lhs, expr.OP, expr.RHS)
  445. }
  446. rhs := v.Eval(expr.RHS)
  447. if _, ok := rhs.(error); ok {
  448. return rhs
  449. }
  450. if isSetOperator(expr.OP) {
  451. return v.evalSetsExpr(lhs, expr.OP, rhs)
  452. }
  453. switch expr.OP {
  454. case ast.BETWEEN, ast.NOTBETWEEN:
  455. arr, ok := rhs.([]interface{})
  456. if !ok {
  457. return fmt.Errorf("between operator expects two arguments, but found %v", rhs)
  458. }
  459. andLeft := v.simpleDataEval(lhs, arr[0], ast.GTE)
  460. switch andLeft.(type) {
  461. case error:
  462. return fmt.Errorf("between operator cannot compare %[1]T(%[1]v) and %[2]T(%[2]v)", lhs, arr[0])
  463. }
  464. andRight := v.simpleDataEval(lhs, arr[1], ast.LTE)
  465. switch andRight.(type) {
  466. case error:
  467. return fmt.Errorf("between operator cannot compare %[1]T(%[1]v) and %[2]T(%[2]v)", lhs, arr[1])
  468. }
  469. r := v.simpleDataEval(andLeft, andRight, ast.AND)
  470. br, ok := r.(bool)
  471. if expr.OP == ast.NOTBETWEEN && ok {
  472. return !br
  473. } else {
  474. return r
  475. }
  476. case ast.LIKE, ast.NOTLIKE:
  477. ls, ok := lhs.(string)
  478. if !ok {
  479. return fmt.Errorf("LIKE operator left operand expects string, but found %v", lhs)
  480. }
  481. var result bool
  482. rs, ok := rhs.(*regexp.Regexp)
  483. if !ok {
  484. return fmt.Errorf("LIKE operator right operand expects string, but found %v", rhs)
  485. }
  486. result = rs.MatchString(ls)
  487. if expr.OP == ast.NOTLIKE {
  488. result = !result
  489. }
  490. return result
  491. default:
  492. return v.simpleDataEval(lhs, rhs, expr.OP)
  493. }
  494. }
  495. func (v *ValuerEval) evalCase(expr *ast.CaseExpr) interface{} {
  496. if expr.Value != nil { // compare value to all when clause
  497. ev := v.Eval(expr.Value)
  498. for _, w := range expr.WhenClauses {
  499. wv := v.Eval(w.Expr)
  500. switch r := v.simpleDataEval(ev, wv, ast.EQ).(type) {
  501. case error:
  502. return fmt.Errorf("evaluate case expression error: %s", r)
  503. case bool:
  504. if r {
  505. return v.Eval(w.Result)
  506. }
  507. }
  508. }
  509. } else {
  510. for _, w := range expr.WhenClauses {
  511. switch r := v.Eval(w.Expr).(type) {
  512. case error:
  513. return fmt.Errorf("evaluate case expression error: %s", r)
  514. case bool:
  515. if r {
  516. return v.Eval(w.Result)
  517. }
  518. }
  519. }
  520. }
  521. if expr.ElseClause != nil {
  522. return v.Eval(expr.ElseClause)
  523. }
  524. return nil
  525. }
  526. func (v *ValuerEval) evalValueSet(expr *ast.ValueSetExpr) interface{} {
  527. var valueSet []interface{}
  528. if expr.LiteralExprs != nil {
  529. for _, exp := range expr.LiteralExprs {
  530. valueSet = append(valueSet, v.Eval(exp))
  531. }
  532. return valueSet
  533. }
  534. value := v.Eval(expr.ArrayExpr)
  535. if isSliceOrArray(value) {
  536. return value
  537. }
  538. return nil
  539. }
  540. func (v *ValuerEval) evalSetsExpr(lhs interface{}, op ast.Token, rhsSet interface{}) interface{} {
  541. switch op {
  542. /*Semantic rules
  543. When using the IN operator, the following semantics apply in this order:
  544. Returns FALSE if value_set is empty.
  545. Returns NULL if search_value is NULL.
  546. Returns TRUE if value_set contains a value equal to search_value.
  547. Returns NULL if value_set contains a NULL.
  548. Returns FALSE.
  549. When using the NOT IN operator, the following semantics apply in this order:
  550. Returns TRUE if value_set is empty.
  551. Returns NULL if search_value is NULL.
  552. Returns FALSE if value_set contains a value equal to search_value.
  553. Returns NULL if value_set contains a NULL.
  554. Returns TRUE.
  555. */
  556. case ast.IN, ast.NOTIN:
  557. if rhsSet == nil {
  558. if op == ast.IN {
  559. return false
  560. } else {
  561. return true
  562. }
  563. }
  564. if lhs == nil {
  565. return nil
  566. }
  567. rhsSetVals := reflect.ValueOf(rhsSet)
  568. for i := 0; i < rhsSetVals.Len(); i++ {
  569. switch r := v.simpleDataEval(lhs, rhsSetVals.Index(i).Interface(), ast.EQ).(type) {
  570. case error:
  571. return fmt.Errorf("evaluate in expression error: %s", r)
  572. case bool:
  573. if r {
  574. if op == ast.IN {
  575. return true
  576. } else {
  577. return false
  578. }
  579. }
  580. }
  581. }
  582. if op == ast.IN {
  583. return false
  584. } else {
  585. return true
  586. }
  587. default:
  588. return fmt.Errorf("%v is an invalid operation for %T", op, lhs)
  589. }
  590. }
  591. func (v *ValuerEval) evalJsonExpr(result interface{}, op ast.Token, expr ast.Expr) interface{} {
  592. switch op {
  593. case ast.ARROW:
  594. if val, ok := result.(map[string]interface{}); ok {
  595. switch e := expr.(type) {
  596. case *ast.JsonFieldRef:
  597. ve := &ValuerEval{Valuer: Message(val)}
  598. return ve.Eval(e)
  599. default:
  600. return fmt.Errorf("the right expression is not a field reference node")
  601. }
  602. } else {
  603. return fmt.Errorf("the result %v is not a type of map[string]interface{}", result)
  604. }
  605. case ast.SUBSET:
  606. if isSliceOrArray(result) {
  607. return v.subset(result, expr)
  608. } else {
  609. return fmt.Errorf("%v is an invalid operation for %T", op, result)
  610. }
  611. default:
  612. return fmt.Errorf("%v is an invalid operation for %T", op, result)
  613. }
  614. }
  615. func (v *ValuerEval) subset(result interface{}, expr ast.Expr) interface{} {
  616. val := reflect.ValueOf(result)
  617. ber := v.Eval(expr)
  618. if berVal, ok1 := ber.(*BracketEvalResult); ok1 {
  619. if berVal.isIndex() {
  620. if 0 > berVal.Start {
  621. if 0 > berVal.Start+val.Len() {
  622. return fmt.Errorf("out of index: %d of %d", berVal.Start, val.Len())
  623. }
  624. berVal.Start += val.Len()
  625. } else if berVal.Start >= val.Len() {
  626. return fmt.Errorf("out of index: %d of %d", berVal.Start, val.Len())
  627. }
  628. return val.Index(berVal.Start).Interface()
  629. } else {
  630. if 0 > berVal.Start {
  631. if 0 > berVal.Start+val.Len() {
  632. return fmt.Errorf("out of index: %d of %d", berVal.Start, val.Len())
  633. }
  634. berVal.Start += val.Len()
  635. } else if berVal.Start >= val.Len() {
  636. return fmt.Errorf("start value is out of index: %d of %d", berVal.Start, val.Len())
  637. }
  638. if math.MinInt32 == berVal.End {
  639. berVal.End = val.Len()
  640. } else if 0 > berVal.End {
  641. if 0 > berVal.End+val.Len() {
  642. return fmt.Errorf("out of index: %d of %d", berVal.End, val.Len())
  643. }
  644. berVal.End += val.Len()
  645. } else if berVal.End > val.Len() {
  646. return fmt.Errorf("end value is out of index: %d of %d", berVal.End, val.Len())
  647. } else if berVal.Start >= berVal.End {
  648. return fmt.Errorf("start cannot be greater than end. start:%d end:%d", berVal.Start, berVal.End)
  649. }
  650. return val.Slice(berVal.Start, berVal.End).Interface()
  651. }
  652. } else {
  653. return fmt.Errorf("invalid evaluation result - %v", berVal)
  654. }
  655. }
  656. // lhs and rhs are non-nil
  657. func (v *ValuerEval) simpleDataEval(lhs, rhs interface{}, op ast.Token) interface{} {
  658. if lhs == nil || rhs == nil {
  659. switch op {
  660. case ast.EQ, ast.LTE, ast.GTE:
  661. if lhs == nil && rhs == nil {
  662. return true
  663. } else {
  664. return false
  665. }
  666. case ast.NEQ:
  667. if lhs == nil && rhs == nil {
  668. return false
  669. } else {
  670. return true
  671. }
  672. case ast.LT, ast.GT:
  673. return false
  674. default:
  675. return nil
  676. }
  677. }
  678. lhs = convertNum(lhs)
  679. rhs = convertNum(rhs)
  680. // Evaluate if both sides are simple types.
  681. switch lhs := lhs.(type) {
  682. case bool:
  683. rhs, ok := rhs.(bool)
  684. if !ok {
  685. return invalidOpError(lhs, op, rhs)
  686. }
  687. switch op {
  688. case ast.AND:
  689. return lhs && rhs
  690. case ast.OR:
  691. return lhs || rhs
  692. case ast.BITWISE_AND:
  693. return lhs && rhs
  694. case ast.BITWISE_OR:
  695. return lhs || rhs
  696. case ast.BITWISE_XOR:
  697. return lhs != rhs
  698. case ast.EQ:
  699. return lhs == rhs
  700. case ast.NEQ:
  701. return lhs != rhs
  702. default:
  703. return invalidOpError(lhs, op, rhs)
  704. }
  705. case float64:
  706. // Try the rhs as a float64, int64, or uint64
  707. rhsf, ok := rhs.(float64)
  708. if !ok {
  709. switch val := rhs.(type) {
  710. case int64:
  711. rhsf, ok = float64(val), true
  712. case uint64:
  713. rhsf, ok = float64(val), true
  714. }
  715. }
  716. if !ok {
  717. return invalidOpError(lhs, op, rhs)
  718. }
  719. rhs := rhsf
  720. switch op {
  721. case ast.EQ:
  722. return lhs == rhs
  723. case ast.NEQ:
  724. return lhs != rhs
  725. case ast.LT:
  726. return lhs < rhs
  727. case ast.LTE:
  728. return lhs <= rhs
  729. case ast.GT:
  730. return lhs > rhs
  731. case ast.GTE:
  732. return lhs >= rhs
  733. case ast.ADD:
  734. return lhs + rhs
  735. case ast.SUB:
  736. return lhs - rhs
  737. case ast.MUL:
  738. return lhs * rhs
  739. case ast.DIV:
  740. if rhs == 0 {
  741. return fmt.Errorf("divided by zero")
  742. }
  743. return lhs / rhs
  744. case ast.MOD:
  745. if rhs == 0 {
  746. return fmt.Errorf("divided by zero")
  747. }
  748. return math.Mod(lhs, rhs)
  749. default:
  750. return invalidOpError(lhs, op, rhs)
  751. }
  752. case int64:
  753. // Try as a float64 to see if a float cast is required.
  754. switch rhs := rhs.(type) {
  755. case float64:
  756. lhs := float64(lhs)
  757. switch op {
  758. case ast.EQ:
  759. return lhs == rhs
  760. case ast.NEQ:
  761. return lhs != rhs
  762. case ast.LT:
  763. return lhs < rhs
  764. case ast.LTE:
  765. return lhs <= rhs
  766. case ast.GT:
  767. return lhs > rhs
  768. case ast.GTE:
  769. return lhs >= rhs
  770. case ast.ADD:
  771. return lhs + rhs
  772. case ast.SUB:
  773. return lhs - rhs
  774. case ast.MUL:
  775. return lhs * rhs
  776. case ast.DIV:
  777. if rhs == 0 {
  778. return fmt.Errorf("divided by zero")
  779. }
  780. return lhs / rhs
  781. case ast.MOD:
  782. if rhs == 0 {
  783. return fmt.Errorf("divided by zero")
  784. }
  785. return math.Mod(lhs, rhs)
  786. default:
  787. return invalidOpError(lhs, op, rhs)
  788. }
  789. case int64:
  790. switch op {
  791. case ast.EQ:
  792. return lhs == rhs
  793. case ast.NEQ:
  794. return lhs != rhs
  795. case ast.LT:
  796. return lhs < rhs
  797. case ast.LTE:
  798. return lhs <= rhs
  799. case ast.GT:
  800. return lhs > rhs
  801. case ast.GTE:
  802. return lhs >= rhs
  803. case ast.ADD:
  804. return lhs + rhs
  805. case ast.SUB:
  806. return lhs - rhs
  807. case ast.MUL:
  808. return lhs * rhs
  809. case ast.DIV:
  810. if v.IntegerFloatDivision {
  811. if rhs == 0 {
  812. return fmt.Errorf("divided by zero")
  813. }
  814. return float64(lhs) / float64(rhs)
  815. }
  816. if rhs == 0 {
  817. return fmt.Errorf("divided by zero")
  818. }
  819. return lhs / rhs
  820. case ast.MOD:
  821. if rhs == 0 {
  822. return fmt.Errorf("divided by zero")
  823. }
  824. return lhs % rhs
  825. case ast.BITWISE_AND:
  826. return lhs & rhs
  827. case ast.BITWISE_OR:
  828. return lhs | rhs
  829. case ast.BITWISE_XOR:
  830. return lhs ^ rhs
  831. default:
  832. return invalidOpError(lhs, op, rhs)
  833. }
  834. case uint64:
  835. switch op {
  836. case ast.EQ:
  837. return uint64(lhs) == rhs
  838. case ast.NEQ:
  839. return uint64(lhs) != rhs
  840. case ast.LT:
  841. if lhs < 0 {
  842. return true
  843. }
  844. return uint64(lhs) < rhs
  845. case ast.LTE:
  846. if lhs < 0 {
  847. return true
  848. }
  849. return uint64(lhs) <= rhs
  850. case ast.GT:
  851. if lhs < 0 {
  852. return false
  853. }
  854. return uint64(lhs) > rhs
  855. case ast.GTE:
  856. if lhs < 0 {
  857. return false
  858. }
  859. return uint64(lhs) >= rhs
  860. case ast.ADD:
  861. return uint64(lhs) + rhs
  862. case ast.SUB:
  863. return uint64(lhs) - rhs
  864. case ast.MUL:
  865. return uint64(lhs) * rhs
  866. case ast.DIV:
  867. if rhs == 0 {
  868. return fmt.Errorf("divided by zero")
  869. }
  870. return uint64(lhs) / rhs
  871. case ast.MOD:
  872. if rhs == 0 {
  873. return fmt.Errorf("divided by zero")
  874. }
  875. return uint64(lhs) % rhs
  876. case ast.BITWISE_AND:
  877. return uint64(lhs) & rhs
  878. case ast.BITWISE_OR:
  879. return uint64(lhs) | rhs
  880. case ast.BITWISE_XOR:
  881. return uint64(lhs) ^ rhs
  882. default:
  883. return invalidOpError(lhs, op, rhs)
  884. }
  885. default:
  886. return invalidOpError(lhs, op, rhs)
  887. }
  888. case uint64:
  889. // Try as a float64 to see if a float cast is required.
  890. switch rhs := rhs.(type) {
  891. case float64:
  892. lhs := float64(lhs)
  893. switch op {
  894. case ast.EQ:
  895. return lhs == rhs
  896. case ast.NEQ:
  897. return lhs != rhs
  898. case ast.LT:
  899. return lhs < rhs
  900. case ast.LTE:
  901. return lhs <= rhs
  902. case ast.GT:
  903. return lhs > rhs
  904. case ast.GTE:
  905. return lhs >= rhs
  906. case ast.ADD:
  907. return lhs + rhs
  908. case ast.SUB:
  909. return lhs - rhs
  910. case ast.MUL:
  911. return lhs * rhs
  912. case ast.DIV:
  913. if rhs == 0 {
  914. return fmt.Errorf("divided by zero")
  915. }
  916. return lhs / rhs
  917. case ast.MOD:
  918. if rhs == 0 {
  919. return fmt.Errorf("divided by zero")
  920. }
  921. return math.Mod(lhs, rhs)
  922. default:
  923. return invalidOpError(lhs, op, rhs)
  924. }
  925. case int64:
  926. switch op {
  927. case ast.EQ:
  928. return lhs == uint64(rhs)
  929. case ast.NEQ:
  930. return lhs != uint64(rhs)
  931. case ast.LT:
  932. if rhs < 0 {
  933. return false
  934. }
  935. return lhs < uint64(rhs)
  936. case ast.LTE:
  937. if rhs < 0 {
  938. return false
  939. }
  940. return lhs <= uint64(rhs)
  941. case ast.GT:
  942. if rhs < 0 {
  943. return true
  944. }
  945. return lhs > uint64(rhs)
  946. case ast.GTE:
  947. if rhs < 0 {
  948. return true
  949. }
  950. return lhs >= uint64(rhs)
  951. case ast.ADD:
  952. return lhs + uint64(rhs)
  953. case ast.SUB:
  954. return lhs - uint64(rhs)
  955. case ast.MUL:
  956. return lhs * uint64(rhs)
  957. case ast.DIV:
  958. if rhs == 0 {
  959. return fmt.Errorf("divided by zero")
  960. }
  961. return lhs / uint64(rhs)
  962. case ast.MOD:
  963. if rhs == 0 {
  964. return fmt.Errorf("divided by zero")
  965. }
  966. return lhs % uint64(rhs)
  967. case ast.BITWISE_AND:
  968. return lhs & uint64(rhs)
  969. case ast.BITWISE_OR:
  970. return lhs | uint64(rhs)
  971. case ast.BITWISE_XOR:
  972. return lhs ^ uint64(rhs)
  973. default:
  974. return invalidOpError(lhs, op, rhs)
  975. }
  976. case uint64:
  977. switch op {
  978. case ast.EQ:
  979. return lhs == rhs
  980. case ast.NEQ:
  981. return lhs != rhs
  982. case ast.LT:
  983. return lhs < rhs
  984. case ast.LTE:
  985. return lhs <= rhs
  986. case ast.GT:
  987. return lhs > rhs
  988. case ast.GTE:
  989. return lhs >= rhs
  990. case ast.ADD:
  991. return lhs + rhs
  992. case ast.SUB:
  993. return lhs - rhs
  994. case ast.MUL:
  995. return lhs * rhs
  996. case ast.DIV:
  997. if rhs == 0 {
  998. return fmt.Errorf("divided by zero")
  999. }
  1000. return lhs / rhs
  1001. case ast.MOD:
  1002. if rhs == 0 {
  1003. return fmt.Errorf("divided by zero")
  1004. }
  1005. return lhs % rhs
  1006. case ast.BITWISE_AND:
  1007. return lhs & rhs
  1008. case ast.BITWISE_OR:
  1009. return lhs | rhs
  1010. case ast.BITWISE_XOR:
  1011. return lhs ^ rhs
  1012. default:
  1013. return invalidOpError(lhs, op, rhs)
  1014. }
  1015. default:
  1016. return invalidOpError(lhs, op, rhs)
  1017. }
  1018. case string:
  1019. rhss, ok := rhs.(string)
  1020. if !ok {
  1021. return invalidOpError(lhs, op, rhs)
  1022. }
  1023. switch op {
  1024. case ast.EQ:
  1025. return lhs == rhss
  1026. case ast.NEQ:
  1027. return lhs != rhss
  1028. case ast.LT:
  1029. return lhs < rhss
  1030. case ast.LTE:
  1031. return lhs <= rhss
  1032. case ast.GT:
  1033. return lhs > rhss
  1034. case ast.GTE:
  1035. return lhs >= rhss
  1036. default:
  1037. return invalidOpError(lhs, op, rhs)
  1038. }
  1039. case time.Time:
  1040. rt, err := cast.InterfaceToTime(rhs, "")
  1041. if err != nil {
  1042. return invalidOpError(lhs, op, rhs)
  1043. }
  1044. switch op {
  1045. case ast.EQ:
  1046. return lhs.Equal(rt)
  1047. case ast.NEQ:
  1048. return !lhs.Equal(rt)
  1049. case ast.LT:
  1050. return lhs.Before(rt)
  1051. case ast.LTE:
  1052. return lhs.Before(rt) || lhs.Equal(rt)
  1053. case ast.GT:
  1054. return lhs.After(rt)
  1055. case ast.GTE:
  1056. return lhs.After(rt) || lhs.Equal(rt)
  1057. default:
  1058. return invalidOpError(lhs, op, rhs)
  1059. }
  1060. default:
  1061. return invalidOpError(lhs, op, rhs)
  1062. }
  1063. }
  1064. /*
  1065. * Helper functions
  1066. */
  1067. type BracketEvalResult struct {
  1068. Start, End int
  1069. }
  1070. func (ber *BracketEvalResult) isIndex() bool {
  1071. return ber.Start == ber.End
  1072. }
  1073. func isSliceOrArray(v interface{}) bool {
  1074. kind := reflect.ValueOf(v).Kind()
  1075. return kind == reflect.Array || kind == reflect.Slice
  1076. }
  1077. func isSetOperator(op ast.Token) bool {
  1078. return op == ast.IN || op == ast.NOTIN
  1079. }
  1080. func invalidOpError(lhs interface{}, op ast.Token, rhs interface{}) error {
  1081. return fmt.Errorf("invalid operation %[1]T(%[1]v) %s %[3]T(%[3]v)", lhs, ast.Tokens[op], rhs)
  1082. }
  1083. func convertNum(para interface{}) interface{} {
  1084. if isInt(para) {
  1085. para = toInt64(para)
  1086. } else if isFloat(para) {
  1087. para = toFloat64(para)
  1088. }
  1089. return para
  1090. }
  1091. func isInt(para interface{}) bool {
  1092. switch para.(type) {
  1093. case int:
  1094. return true
  1095. case int8:
  1096. return true
  1097. case int16:
  1098. return true
  1099. case int32:
  1100. return true
  1101. case int64:
  1102. return true
  1103. }
  1104. return false
  1105. }
  1106. func toInt64(para interface{}) int64 {
  1107. if v, ok := para.(int); ok {
  1108. return int64(v)
  1109. } else if v, ok := para.(int8); ok {
  1110. return int64(v)
  1111. } else if v, ok := para.(int16); ok {
  1112. return int64(v)
  1113. } else if v, ok := para.(int32); ok {
  1114. return int64(v)
  1115. } else if v, ok := para.(int64); ok {
  1116. return v
  1117. }
  1118. return 0
  1119. }
  1120. func isFloat(para interface{}) bool {
  1121. switch para.(type) {
  1122. case float32:
  1123. return true
  1124. case float64:
  1125. return true
  1126. }
  1127. return false
  1128. }
  1129. func toFloat64(para interface{}) float64 {
  1130. if v, ok := para.(float32); ok {
  1131. return float64(v)
  1132. } else if v, ok := para.(float64); ok {
  1133. return v
  1134. }
  1135. return 0
  1136. }