valuer.go 29 KB

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