valuer.go 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199
  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 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, 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.(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. rs, ok := rhs.(*regexp.Regexp)
  485. if !ok {
  486. return fmt.Errorf("LIKE operator right operand expects string, but found %v", rhs)
  487. }
  488. result = rs.MatchString(ls)
  489. if expr.OP == ast.NOTLIKE {
  490. result = !result
  491. }
  492. return result
  493. default:
  494. return v.simpleDataEval(lhs, rhs, expr.OP)
  495. }
  496. }
  497. func (v *ValuerEval) evalCase(expr *ast.CaseExpr) interface{} {
  498. if expr.Value != nil { // compare value to all when clause
  499. ev := v.Eval(expr.Value)
  500. for _, w := range expr.WhenClauses {
  501. wv := v.Eval(w.Expr)
  502. switch r := v.simpleDataEval(ev, wv, ast.EQ).(type) {
  503. case error:
  504. return fmt.Errorf("evaluate case expression error: %s", r)
  505. case bool:
  506. if r {
  507. return v.Eval(w.Result)
  508. }
  509. }
  510. }
  511. } else {
  512. for _, w := range expr.WhenClauses {
  513. switch r := v.Eval(w.Expr).(type) {
  514. case error:
  515. return fmt.Errorf("evaluate case expression error: %s", r)
  516. case bool:
  517. if r {
  518. return v.Eval(w.Result)
  519. }
  520. }
  521. }
  522. }
  523. if expr.ElseClause != nil {
  524. return v.Eval(expr.ElseClause)
  525. }
  526. return nil
  527. }
  528. func (v *ValuerEval) evalValueSet(expr *ast.ValueSetExpr) interface{} {
  529. var valueSet []interface{}
  530. if expr.LiteralExprs != nil {
  531. for _, exp := range expr.LiteralExprs {
  532. valueSet = append(valueSet, v.Eval(exp))
  533. }
  534. return valueSet
  535. }
  536. value := v.Eval(expr.ArrayExpr)
  537. if isSliceOrArray(value) {
  538. return value
  539. }
  540. return nil
  541. }
  542. func (v *ValuerEval) evalSetsExpr(lhs interface{}, op ast.Token, rhsSet interface{}) interface{} {
  543. switch op {
  544. /*Semantic rules
  545. When using the IN operator, the following semantics apply in this order:
  546. Returns FALSE if value_set is empty.
  547. Returns NULL if search_value is NULL.
  548. Returns TRUE if value_set contains a value equal to search_value.
  549. Returns NULL if value_set contains a NULL.
  550. Returns FALSE.
  551. When using the NOT IN operator, the following semantics apply in this order:
  552. Returns TRUE if value_set is empty.
  553. Returns NULL if search_value is NULL.
  554. Returns FALSE if value_set contains a value equal to search_value.
  555. Returns NULL if value_set contains a NULL.
  556. Returns TRUE.
  557. */
  558. case ast.IN, ast.NOTIN:
  559. if rhsSet == nil {
  560. if op == ast.IN {
  561. return false
  562. } else {
  563. return true
  564. }
  565. }
  566. if lhs == nil {
  567. return nil
  568. }
  569. rhsSetVals := reflect.ValueOf(rhsSet)
  570. for i := 0; i < rhsSetVals.Len(); i++ {
  571. switch r := v.simpleDataEval(lhs, rhsSetVals.Index(i).Interface(), ast.EQ).(type) {
  572. case error:
  573. return fmt.Errorf("evaluate in expression error: %s", r)
  574. case bool:
  575. if r {
  576. if op == ast.IN {
  577. return true
  578. } else {
  579. return false
  580. }
  581. }
  582. }
  583. }
  584. if op == ast.IN {
  585. return false
  586. } else {
  587. return true
  588. }
  589. default:
  590. return fmt.Errorf("%v is an invalid operation for %T", op, lhs)
  591. }
  592. }
  593. func (v *ValuerEval) evalJsonExpr(result interface{}, op ast.Token, expr ast.Expr) interface{} {
  594. switch op {
  595. case ast.ARROW:
  596. if val, ok := result.(map[string]interface{}); ok {
  597. switch e := expr.(type) {
  598. case *ast.JsonFieldRef:
  599. ve := &ValuerEval{Valuer: Message(val)}
  600. return ve.Eval(e)
  601. default:
  602. return fmt.Errorf("the right expression is not a field reference node")
  603. }
  604. } else {
  605. return fmt.Errorf("the result %v is not a type of map[string]interface{}", result)
  606. }
  607. case ast.SUBSET:
  608. if isSliceOrArray(result) {
  609. return v.subset(result, expr)
  610. } else {
  611. return fmt.Errorf("%v is an invalid operation for %T", op, result)
  612. }
  613. default:
  614. return fmt.Errorf("%v is an invalid operation for %T", op, result)
  615. }
  616. }
  617. func (v *ValuerEval) subset(result interface{}, expr ast.Expr) interface{} {
  618. val := reflect.ValueOf(result)
  619. ber := v.Eval(expr)
  620. if berVal, ok1 := ber.(*BracketEvalResult); ok1 {
  621. if berVal.isIndex() {
  622. if 0 > berVal.Start {
  623. if 0 > berVal.Start+val.Len() {
  624. return fmt.Errorf("out of index: %d of %d", berVal.Start, val.Len())
  625. }
  626. berVal.Start += val.Len()
  627. } else if berVal.Start >= val.Len() {
  628. return fmt.Errorf("out of index: %d of %d", berVal.Start, val.Len())
  629. }
  630. return val.Index(berVal.Start).Interface()
  631. } else {
  632. if 0 > berVal.Start {
  633. if 0 > berVal.Start+val.Len() {
  634. return fmt.Errorf("out of index: %d of %d", berVal.Start, val.Len())
  635. }
  636. berVal.Start += val.Len()
  637. } else if berVal.Start >= val.Len() {
  638. return fmt.Errorf("start value is out of index: %d of %d", berVal.Start, val.Len())
  639. }
  640. if math.MinInt32 == berVal.End {
  641. berVal.End = val.Len()
  642. } else if 0 > berVal.End {
  643. if 0 > berVal.End+val.Len() {
  644. return fmt.Errorf("out of index: %d of %d", berVal.End, val.Len())
  645. }
  646. berVal.End += val.Len()
  647. } else if berVal.End > val.Len() {
  648. return fmt.Errorf("end value is out of index: %d of %d", berVal.End, val.Len())
  649. } else if berVal.Start >= berVal.End {
  650. return fmt.Errorf("start cannot be greater than end. start:%d end:%d", berVal.Start, berVal.End)
  651. }
  652. return val.Slice(berVal.Start, berVal.End).Interface()
  653. }
  654. } else {
  655. return fmt.Errorf("invalid evaluation result - %v", berVal)
  656. }
  657. }
  658. // lhs and rhs are non-nil
  659. func (v *ValuerEval) simpleDataEval(lhs, rhs interface{}, op ast.Token) interface{} {
  660. if lhs == nil || rhs == nil {
  661. switch op {
  662. case ast.EQ, ast.LTE, ast.GTE:
  663. if lhs == nil && rhs == nil {
  664. return true
  665. } else {
  666. return false
  667. }
  668. case ast.NEQ:
  669. if lhs == nil && rhs == nil {
  670. return false
  671. } else {
  672. return true
  673. }
  674. case ast.LT, ast.GT:
  675. return false
  676. default:
  677. return nil
  678. }
  679. }
  680. lhs = convertNum(lhs)
  681. rhs = convertNum(rhs)
  682. // Evaluate if both sides are simple types.
  683. switch lhs := lhs.(type) {
  684. case bool:
  685. rhs, ok := rhs.(bool)
  686. if !ok {
  687. return invalidOpError(lhs, op, rhs)
  688. }
  689. switch op {
  690. case ast.AND:
  691. return lhs && rhs
  692. case ast.OR:
  693. return lhs || rhs
  694. case ast.BITWISE_AND:
  695. return lhs && rhs
  696. case ast.BITWISE_OR:
  697. return lhs || rhs
  698. case ast.BITWISE_XOR:
  699. return lhs != rhs
  700. case ast.EQ:
  701. return lhs == rhs
  702. case ast.NEQ:
  703. return lhs != rhs
  704. default:
  705. return invalidOpError(lhs, op, rhs)
  706. }
  707. case float64:
  708. // Try the rhs as a float64, int64, or uint64
  709. rhsf, ok := rhs.(float64)
  710. if !ok {
  711. switch val := rhs.(type) {
  712. case int64:
  713. rhsf, ok = float64(val), true
  714. case uint64:
  715. rhsf, ok = float64(val), true
  716. }
  717. }
  718. if !ok {
  719. return invalidOpError(lhs, op, rhs)
  720. }
  721. rhs := rhsf
  722. switch op {
  723. case ast.EQ:
  724. return lhs == rhs
  725. case ast.NEQ:
  726. return lhs != rhs
  727. case ast.LT:
  728. return lhs < rhs
  729. case ast.LTE:
  730. return lhs <= rhs
  731. case ast.GT:
  732. return lhs > rhs
  733. case ast.GTE:
  734. return lhs >= rhs
  735. case ast.ADD:
  736. return lhs + rhs
  737. case ast.SUB:
  738. return lhs - rhs
  739. case ast.MUL:
  740. return lhs * rhs
  741. case ast.DIV:
  742. if rhs == 0 {
  743. return fmt.Errorf("divided by zero")
  744. }
  745. return lhs / rhs
  746. case ast.MOD:
  747. if rhs == 0 {
  748. return fmt.Errorf("divided by zero")
  749. }
  750. return math.Mod(lhs, rhs)
  751. default:
  752. return invalidOpError(lhs, op, rhs)
  753. }
  754. case int64:
  755. // Try as a float64 to see if a float cast is required.
  756. switch rhs := rhs.(type) {
  757. case float64:
  758. lhs := float64(lhs)
  759. switch op {
  760. case ast.EQ:
  761. return lhs == rhs
  762. case ast.NEQ:
  763. return lhs != rhs
  764. case ast.LT:
  765. return lhs < rhs
  766. case ast.LTE:
  767. return lhs <= rhs
  768. case ast.GT:
  769. return lhs > rhs
  770. case ast.GTE:
  771. return lhs >= rhs
  772. case ast.ADD:
  773. return lhs + rhs
  774. case ast.SUB:
  775. return lhs - rhs
  776. case ast.MUL:
  777. return lhs * rhs
  778. case ast.DIV:
  779. if rhs == 0 {
  780. return fmt.Errorf("divided by zero")
  781. }
  782. return lhs / rhs
  783. case ast.MOD:
  784. if rhs == 0 {
  785. return fmt.Errorf("divided by zero")
  786. }
  787. return math.Mod(lhs, rhs)
  788. default:
  789. return invalidOpError(lhs, op, rhs)
  790. }
  791. case int64:
  792. switch op {
  793. case ast.EQ:
  794. return lhs == rhs
  795. case ast.NEQ:
  796. return lhs != rhs
  797. case ast.LT:
  798. return lhs < rhs
  799. case ast.LTE:
  800. return lhs <= rhs
  801. case ast.GT:
  802. return lhs > rhs
  803. case ast.GTE:
  804. return lhs >= rhs
  805. case ast.ADD:
  806. return lhs + rhs
  807. case ast.SUB:
  808. return lhs - rhs
  809. case ast.MUL:
  810. return lhs * rhs
  811. case ast.DIV:
  812. if v.IntegerFloatDivision {
  813. if rhs == 0 {
  814. return fmt.Errorf("divided by zero")
  815. }
  816. return float64(lhs) / float64(rhs)
  817. }
  818. if rhs == 0 {
  819. return fmt.Errorf("divided by zero")
  820. }
  821. return lhs / rhs
  822. case ast.MOD:
  823. if rhs == 0 {
  824. return fmt.Errorf("divided by zero")
  825. }
  826. return lhs % rhs
  827. case ast.BITWISE_AND:
  828. return lhs & rhs
  829. case ast.BITWISE_OR:
  830. return lhs | rhs
  831. case ast.BITWISE_XOR:
  832. return lhs ^ rhs
  833. default:
  834. return invalidOpError(lhs, op, rhs)
  835. }
  836. case uint64:
  837. switch op {
  838. case ast.EQ:
  839. return uint64(lhs) == rhs
  840. case ast.NEQ:
  841. return uint64(lhs) != rhs
  842. case ast.LT:
  843. if lhs < 0 {
  844. return true
  845. }
  846. return uint64(lhs) < rhs
  847. case ast.LTE:
  848. if lhs < 0 {
  849. return true
  850. }
  851. return uint64(lhs) <= rhs
  852. case ast.GT:
  853. if lhs < 0 {
  854. return false
  855. }
  856. return uint64(lhs) > rhs
  857. case ast.GTE:
  858. if lhs < 0 {
  859. return false
  860. }
  861. return uint64(lhs) >= rhs
  862. case ast.ADD:
  863. return uint64(lhs) + rhs
  864. case ast.SUB:
  865. return uint64(lhs) - rhs
  866. case ast.MUL:
  867. return uint64(lhs) * rhs
  868. case ast.DIV:
  869. if rhs == 0 {
  870. return fmt.Errorf("divided by zero")
  871. }
  872. return uint64(lhs) / rhs
  873. case ast.MOD:
  874. if rhs == 0 {
  875. return fmt.Errorf("divided by zero")
  876. }
  877. return uint64(lhs) % rhs
  878. case ast.BITWISE_AND:
  879. return uint64(lhs) & rhs
  880. case ast.BITWISE_OR:
  881. return uint64(lhs) | rhs
  882. case ast.BITWISE_XOR:
  883. return uint64(lhs) ^ rhs
  884. default:
  885. return invalidOpError(lhs, op, rhs)
  886. }
  887. default:
  888. return invalidOpError(lhs, op, rhs)
  889. }
  890. case uint64:
  891. // Try as a float64 to see if a float cast is required.
  892. switch rhs := rhs.(type) {
  893. case float64:
  894. lhs := float64(lhs)
  895. switch op {
  896. case ast.EQ:
  897. return lhs == rhs
  898. case ast.NEQ:
  899. return lhs != rhs
  900. case ast.LT:
  901. return lhs < rhs
  902. case ast.LTE:
  903. return lhs <= rhs
  904. case ast.GT:
  905. return lhs > rhs
  906. case ast.GTE:
  907. return lhs >= rhs
  908. case ast.ADD:
  909. return lhs + rhs
  910. case ast.SUB:
  911. return lhs - rhs
  912. case ast.MUL:
  913. return lhs * rhs
  914. case ast.DIV:
  915. if rhs == 0 {
  916. return fmt.Errorf("divided by zero")
  917. }
  918. return lhs / rhs
  919. case ast.MOD:
  920. if rhs == 0 {
  921. return fmt.Errorf("divided by zero")
  922. }
  923. return math.Mod(lhs, rhs)
  924. default:
  925. return invalidOpError(lhs, op, rhs)
  926. }
  927. case int64:
  928. switch op {
  929. case ast.EQ:
  930. return lhs == uint64(rhs)
  931. case ast.NEQ:
  932. return lhs != uint64(rhs)
  933. case ast.LT:
  934. if rhs < 0 {
  935. return false
  936. }
  937. return lhs < uint64(rhs)
  938. case ast.LTE:
  939. if rhs < 0 {
  940. return false
  941. }
  942. return lhs <= uint64(rhs)
  943. case ast.GT:
  944. if rhs < 0 {
  945. return true
  946. }
  947. return lhs > uint64(rhs)
  948. case ast.GTE:
  949. if rhs < 0 {
  950. return true
  951. }
  952. return lhs >= uint64(rhs)
  953. case ast.ADD:
  954. return lhs + uint64(rhs)
  955. case ast.SUB:
  956. return lhs - uint64(rhs)
  957. case ast.MUL:
  958. return lhs * uint64(rhs)
  959. case ast.DIV:
  960. if rhs == 0 {
  961. return fmt.Errorf("divided by zero")
  962. }
  963. return lhs / uint64(rhs)
  964. case ast.MOD:
  965. if rhs == 0 {
  966. return fmt.Errorf("divided by zero")
  967. }
  968. return lhs % uint64(rhs)
  969. case ast.BITWISE_AND:
  970. return lhs & uint64(rhs)
  971. case ast.BITWISE_OR:
  972. return lhs | uint64(rhs)
  973. case ast.BITWISE_XOR:
  974. return lhs ^ uint64(rhs)
  975. default:
  976. return invalidOpError(lhs, op, rhs)
  977. }
  978. case uint64:
  979. switch op {
  980. case ast.EQ:
  981. return lhs == rhs
  982. case ast.NEQ:
  983. return lhs != rhs
  984. case ast.LT:
  985. return lhs < rhs
  986. case ast.LTE:
  987. return lhs <= rhs
  988. case ast.GT:
  989. return lhs > rhs
  990. case ast.GTE:
  991. return lhs >= rhs
  992. case ast.ADD:
  993. return lhs + rhs
  994. case ast.SUB:
  995. return lhs - rhs
  996. case ast.MUL:
  997. return lhs * rhs
  998. case ast.DIV:
  999. if rhs == 0 {
  1000. return fmt.Errorf("divided by zero")
  1001. }
  1002. return lhs / rhs
  1003. case ast.MOD:
  1004. if rhs == 0 {
  1005. return fmt.Errorf("divided by zero")
  1006. }
  1007. return lhs % rhs
  1008. case ast.BITWISE_AND:
  1009. return lhs & rhs
  1010. case ast.BITWISE_OR:
  1011. return lhs | rhs
  1012. case ast.BITWISE_XOR:
  1013. return lhs ^ rhs
  1014. default:
  1015. return invalidOpError(lhs, op, rhs)
  1016. }
  1017. default:
  1018. return invalidOpError(lhs, op, rhs)
  1019. }
  1020. case string:
  1021. rhss, ok := rhs.(string)
  1022. if !ok {
  1023. return invalidOpError(lhs, op, rhs)
  1024. }
  1025. switch op {
  1026. case ast.EQ:
  1027. return lhs == rhss
  1028. case ast.NEQ:
  1029. return lhs != rhss
  1030. case ast.LT:
  1031. return lhs < rhss
  1032. case ast.LTE:
  1033. return lhs <= rhss
  1034. case ast.GT:
  1035. return lhs > rhss
  1036. case ast.GTE:
  1037. return lhs >= rhss
  1038. default:
  1039. return invalidOpError(lhs, op, rhs)
  1040. }
  1041. case time.Time:
  1042. rt, err := cast.InterfaceToTime(rhs, "")
  1043. if err != nil {
  1044. return invalidOpError(lhs, op, rhs)
  1045. }
  1046. switch op {
  1047. case ast.EQ:
  1048. return lhs.Equal(rt)
  1049. case ast.NEQ:
  1050. return !lhs.Equal(rt)
  1051. case ast.LT:
  1052. return lhs.Before(rt)
  1053. case ast.LTE:
  1054. return lhs.Before(rt) || lhs.Equal(rt)
  1055. case ast.GT:
  1056. return lhs.After(rt)
  1057. case ast.GTE:
  1058. return lhs.After(rt) || lhs.Equal(rt)
  1059. default:
  1060. return invalidOpError(lhs, op, rhs)
  1061. }
  1062. default:
  1063. return invalidOpError(lhs, op, rhs)
  1064. }
  1065. }
  1066. /*
  1067. * Helper functions
  1068. */
  1069. type BracketEvalResult struct {
  1070. Start, End int
  1071. }
  1072. func (ber *BracketEvalResult) isIndex() bool {
  1073. return ber.Start == ber.End
  1074. }
  1075. func isSliceOrArray(v interface{}) bool {
  1076. kind := reflect.ValueOf(v).Kind()
  1077. return kind == reflect.Array || kind == reflect.Slice
  1078. }
  1079. func isSetOperator(op ast.Token) bool {
  1080. return op == ast.IN || op == ast.NOTIN
  1081. }
  1082. func invalidOpError(lhs interface{}, op ast.Token, rhs interface{}) error {
  1083. return fmt.Errorf("invalid operation %[1]T(%[1]v) %s %[3]T(%[3]v)", lhs, ast.Tokens[op], rhs)
  1084. }
  1085. func convertNum(para interface{}) interface{} {
  1086. if isInt(para) {
  1087. para = toInt64(para)
  1088. } else if isFloat(para) {
  1089. para = toFloat64(para)
  1090. }
  1091. return para
  1092. }
  1093. func isInt(para interface{}) bool {
  1094. switch para.(type) {
  1095. case int:
  1096. return true
  1097. case int8:
  1098. return true
  1099. case int16:
  1100. return true
  1101. case int32:
  1102. return true
  1103. case int64:
  1104. return true
  1105. }
  1106. return false
  1107. }
  1108. func toInt64(para interface{}) int64 {
  1109. if v, ok := para.(int); ok {
  1110. return int64(v)
  1111. } else if v, ok := para.(int8); ok {
  1112. return int64(v)
  1113. } else if v, ok := para.(int16); ok {
  1114. return int64(v)
  1115. } else if v, ok := para.(int32); ok {
  1116. return int64(v)
  1117. } else if v, ok := para.(int64); ok {
  1118. return v
  1119. }
  1120. return 0
  1121. }
  1122. func isFloat(para interface{}) bool {
  1123. switch para.(type) {
  1124. case float32:
  1125. return true
  1126. case float64:
  1127. return true
  1128. }
  1129. return false
  1130. }
  1131. func toFloat64(para interface{}) float64 {
  1132. if v, ok := para.(float32); ok {
  1133. return float64(v)
  1134. } else if v, ok := para.(float64); ok {
  1135. return v
  1136. }
  1137. return 0
  1138. }