valuer.go 29 KB

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