valuer.go 27 KB

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