valuer.go 29 KB

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