valuer.go 30 KB

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