builtin.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991
  1. // Copyright 2014 The ql Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package ql
  5. import (
  6. "fmt"
  7. "math/rand"
  8. "reflect"
  9. "strconv"
  10. "strings"
  11. "time"
  12. )
  13. //TODO agg bigint, bigrat, time, duration
  14. var builtin = map[string]struct {
  15. f func([]interface{}, map[interface{}]interface{}) (interface{}, error)
  16. minArgs int
  17. maxArgs int
  18. isStatic bool
  19. isAggregate bool
  20. }{
  21. "__testBlob": {builtinTestBlob, 1, 1, true, false},
  22. "__testString": {builtinTestString, 1, 1, true, false},
  23. "avg": {builtinAvg, 1, 1, false, true},
  24. "complex": {builtinComplex, 2, 2, true, false},
  25. "contains": {builtinContains, 2, 2, true, false},
  26. "count": {builtinCount, 0, 1, false, true},
  27. "date": {builtinDate, 8, 8, true, false},
  28. "day": {builtinDay, 1, 1, true, false},
  29. "formatTime": {builtinFormatTime, 2, 2, true, false},
  30. "formatFloat": {builtinFormatFloat, 1, 4, true, false},
  31. "formatInt": {builtinFormatInt, 1, 2, true, false},
  32. "hasPrefix": {builtinHasPrefix, 2, 2, true, false},
  33. "hasSuffix": {builtinHasSuffix, 2, 2, true, false},
  34. "hour": {builtinHour, 1, 1, true, false},
  35. "hours": {builtinHours, 1, 1, true, false},
  36. "id": {builtinID, 0, 1, false, false},
  37. "imag": {builtinImag, 1, 1, true, false},
  38. "len": {builtinLen, 1, 1, true, false},
  39. "max": {builtinMax, 1, 1, false, true},
  40. "min": {builtinMin, 1, 1, false, true},
  41. "minute": {builtinMinute, 1, 1, true, false},
  42. "minutes": {builtinMinutes, 1, 1, true, false},
  43. "month": {builtinMonth, 1, 1, true, false},
  44. "nanosecond": {builtinNanosecond, 1, 1, true, false},
  45. "nanoseconds": {builtinNanoseconds, 1, 1, true, false},
  46. "now": {builtinNow, 0, 0, false, false},
  47. "parseTime": {builtinParseTime, 2, 2, true, false},
  48. "real": {builtinReal, 1, 1, true, false},
  49. "second": {builtinSecond, 1, 1, true, false},
  50. "seconds": {builtinSeconds, 1, 1, true, false},
  51. "since": {builtinSince, 1, 1, false, false},
  52. "sum": {builtinSum, 1, 1, false, true},
  53. "timeIn": {builtinTimeIn, 2, 2, true, false},
  54. "weekday": {builtinWeekday, 1, 1, true, false},
  55. "year": {builtinYear, 1, 1, true, false},
  56. "yearDay": {builtinYearday, 1, 1, true, false},
  57. }
  58. func badNArgs(min int, s string, arg []interface{}) error {
  59. a := []string{}
  60. for _, v := range arg {
  61. a = append(a, fmt.Sprintf("%v", v))
  62. }
  63. switch len(arg) < min {
  64. case true:
  65. return fmt.Errorf("missing argument to %s(%s)", s, strings.Join(a, ", "))
  66. default: //case false:
  67. return fmt.Errorf("too many arguments to %s(%s)", s, strings.Join(a, ", "))
  68. }
  69. }
  70. func invArg(arg interface{}, s string) error {
  71. return fmt.Errorf("invalid argument %v (type %T) for %s", arg, arg, s)
  72. }
  73. func builtinTestBlob(arg []interface{}, ctx map[interface{}]interface{}) (v interface{}, err error) {
  74. n, err := intExpr(arg[0])
  75. if err != nil {
  76. return nil, err
  77. }
  78. rng := rand.New(rand.NewSource(n))
  79. b := make([]byte, n)
  80. for i := range b {
  81. b[i] = byte(rng.Int())
  82. }
  83. return b, nil
  84. }
  85. func builtinTestString(arg []interface{}, ctx map[interface{}]interface{}) (v interface{}, err error) {
  86. n, err := intExpr(arg[0])
  87. if err != nil {
  88. return nil, err
  89. }
  90. rng := rand.New(rand.NewSource(n))
  91. b := make([]byte, n)
  92. for i := range b {
  93. b[i] = byte(rng.Int())
  94. }
  95. return string(b), nil
  96. }
  97. func builtinAvg(arg []interface{}, ctx map[interface{}]interface{}) (v interface{}, err error) {
  98. type avg struct {
  99. sum interface{}
  100. n uint64
  101. }
  102. if _, ok := ctx["$agg0"]; ok {
  103. return
  104. }
  105. fn := ctx["$fn"]
  106. if _, ok := ctx["$agg"]; ok {
  107. data, ok := ctx[fn].(avg)
  108. if !ok {
  109. return
  110. }
  111. switch x := data.sum.(type) {
  112. case complex64:
  113. return complex64(complex128(x) / complex(float64(data.n), 0)), nil
  114. case complex128:
  115. return complex64(x / complex(float64(data.n), 0)), nil
  116. case float32:
  117. return float32(float64(x) / float64(data.n)), nil
  118. case float64:
  119. return x / float64(data.n), nil
  120. case int8:
  121. return int8(int64(x) / int64(data.n)), nil
  122. case int16:
  123. return int16(int64(x) / int64(data.n)), nil
  124. case int32:
  125. return int32(int64(x) / int64(data.n)), nil
  126. case int64:
  127. return x / int64(data.n), nil
  128. case uint8:
  129. return uint8(uint64(x) / data.n), nil
  130. case uint16:
  131. return uint16(uint64(x) / data.n), nil
  132. case uint32:
  133. return uint32(uint64(x) / data.n), nil
  134. case uint64:
  135. return x / data.n, nil
  136. }
  137. }
  138. data, _ := ctx[fn].(avg)
  139. y := arg[0]
  140. if y == nil {
  141. return
  142. }
  143. switch x := data.sum.(type) {
  144. case nil:
  145. switch y := y.(type) {
  146. case float32, float64, int8, int16, int32, int64, uint8, uint16, uint32, uint64:
  147. data = avg{y, 0}
  148. default:
  149. return nil, fmt.Errorf("avg: cannot accept %v (value if type %T)", y, y)
  150. }
  151. case complex64:
  152. data.sum = x + y.(complex64)
  153. case complex128:
  154. data.sum = x + y.(complex128)
  155. case float32:
  156. data.sum = x + y.(float32)
  157. case float64:
  158. data.sum = x + y.(float64)
  159. case int8:
  160. data.sum = x + y.(int8)
  161. case int16:
  162. data.sum = x + y.(int16)
  163. case int32:
  164. data.sum = x + y.(int32)
  165. case int64:
  166. data.sum = x + y.(int64)
  167. case uint8:
  168. data.sum = x + y.(uint8)
  169. case uint16:
  170. data.sum = x + y.(uint16)
  171. case uint32:
  172. data.sum = x + y.(uint32)
  173. case uint64:
  174. data.sum = x + y.(uint64)
  175. }
  176. data.n++
  177. ctx[fn] = data
  178. return
  179. }
  180. func builtinComplex(arg []interface{}, _ map[interface{}]interface{}) (v interface{}, err error) {
  181. re, im := arg[0], arg[1]
  182. if re == nil || im == nil {
  183. return nil, nil
  184. }
  185. re, im = coerce(re, im)
  186. if reflect.TypeOf(re) != reflect.TypeOf(im) {
  187. return nil, fmt.Errorf("complex(%T(%#v), %T(%#v)): invalid types", re, re, im, im)
  188. }
  189. switch re := re.(type) {
  190. case idealFloat:
  191. return idealComplex(complex(float64(re), float64(im.(idealFloat)))), nil
  192. case idealInt:
  193. return idealComplex(complex(float64(re), float64(im.(idealInt)))), nil
  194. case idealRune:
  195. return idealComplex(complex(float64(re), float64(im.(idealRune)))), nil
  196. case idealUint:
  197. return idealComplex(complex(float64(re), float64(im.(idealUint)))), nil
  198. case float32:
  199. return complex(re, im.(float32)), nil
  200. case float64:
  201. return complex(re, im.(float64)), nil
  202. case int8:
  203. return complex(float64(re), float64(im.(int8))), nil
  204. case int16:
  205. return complex(float64(re), float64(im.(int16))), nil
  206. case int32:
  207. return complex(float64(re), float64(im.(int32))), nil
  208. case int64:
  209. return complex(float64(re), float64(im.(int64))), nil
  210. case uint8:
  211. return complex(float64(re), float64(im.(uint8))), nil
  212. case uint16:
  213. return complex(float64(re), float64(im.(uint16))), nil
  214. case uint32:
  215. return complex(float64(re), float64(im.(uint32))), nil
  216. case uint64:
  217. return complex(float64(re), float64(im.(uint64))), nil
  218. default:
  219. return nil, invArg(re, "complex")
  220. }
  221. }
  222. func builtinContains(arg []interface{}, _ map[interface{}]interface{}) (v interface{}, err error) {
  223. switch s := arg[0].(type) {
  224. case nil:
  225. return nil, nil
  226. case string:
  227. switch chars := arg[1].(type) {
  228. case nil:
  229. return nil, nil
  230. case string:
  231. return strings.Contains(s, chars), nil
  232. default:
  233. return nil, invArg(chars, "string")
  234. }
  235. default:
  236. return nil, invArg(s, "string")
  237. }
  238. }
  239. func builtinCount(arg []interface{}, ctx map[interface{}]interface{}) (v interface{}, err error) {
  240. if _, ok := ctx["$agg0"]; ok {
  241. return int64(0), nil
  242. }
  243. fn := ctx["$fn"]
  244. if _, ok := ctx["$agg"]; ok {
  245. return ctx[fn].(int64), nil
  246. }
  247. n, _ := ctx[fn].(int64)
  248. switch len(arg) {
  249. case 0:
  250. n++
  251. case 1:
  252. if arg[0] != nil {
  253. n++
  254. }
  255. default:
  256. panic("internal error 067")
  257. }
  258. ctx[fn] = n
  259. return
  260. }
  261. func builtinDate(arg []interface{}, _ map[interface{}]interface{}) (v interface{}, err error) {
  262. for i, v := range arg {
  263. switch i {
  264. case 7:
  265. switch x := v.(type) {
  266. case string:
  267. default:
  268. return nil, invArg(x, "date")
  269. }
  270. default:
  271. switch x := v.(type) {
  272. case int64:
  273. case idealInt:
  274. arg[i] = int64(x)
  275. default:
  276. return nil, invArg(x, "date")
  277. }
  278. }
  279. }
  280. sloc := arg[7].(string)
  281. loc := time.Local
  282. switch sloc {
  283. case "local":
  284. default:
  285. loc, err = time.LoadLocation(sloc)
  286. if err != nil {
  287. return
  288. }
  289. }
  290. return time.Date(
  291. int(arg[0].(int64)),
  292. time.Month(arg[1].(int64)),
  293. int(arg[2].(int64)),
  294. int(arg[3].(int64)),
  295. int(arg[4].(int64)),
  296. int(arg[5].(int64)),
  297. int(arg[6].(int64)),
  298. loc,
  299. ), nil
  300. }
  301. func builtinLen(arg []interface{}, _ map[interface{}]interface{}) (v interface{}, err error) {
  302. switch x := arg[0].(type) {
  303. case nil:
  304. return nil, nil
  305. case string:
  306. return int64(len(x)), nil
  307. default:
  308. return nil, invArg(x, "len")
  309. }
  310. }
  311. func builtinDay(arg []interface{}, ctx map[interface{}]interface{}) (v interface{}, err error) {
  312. switch x := arg[0].(type) {
  313. case nil:
  314. return nil, nil
  315. case time.Time:
  316. return int64(x.Day()), nil
  317. default:
  318. return nil, invArg(x, "day")
  319. }
  320. }
  321. func builtinFormatTime(arg []interface{}, ctx map[interface{}]interface{}) (v interface{}, err error) {
  322. switch x := arg[0].(type) {
  323. case nil:
  324. return nil, nil
  325. case time.Time:
  326. switch y := arg[1].(type) {
  327. case nil:
  328. return nil, nil
  329. case string:
  330. return x.Format(y), nil
  331. default:
  332. return nil, invArg(y, "formatTime")
  333. }
  334. default:
  335. return nil, invArg(x, "formatTime")
  336. }
  337. }
  338. func builtinFormatFloat(arg []interface{}, ctx map[interface{}]interface{}) (v interface{}, err error) {
  339. var val float64
  340. var fmt byte = 'g'
  341. prec := -1
  342. bitSize := 64
  343. switch x := arg[0].(type) {
  344. case nil:
  345. return nil, nil
  346. case float32:
  347. val = float64(x)
  348. bitSize = 32
  349. case float64:
  350. val = x
  351. default:
  352. return nil, invArg(x, "formatFloat")
  353. }
  354. switch len(arg) {
  355. case 4:
  356. arg3 := coerce1(arg[3], int64(0))
  357. switch y := arg3.(type) {
  358. case nil:
  359. return nil, nil
  360. case int64:
  361. bitSize = int(y)
  362. default:
  363. return nil, invArg(y, "formatFloat")
  364. }
  365. fallthrough
  366. case 3:
  367. arg2 := coerce1(arg[2], int64(0))
  368. switch y := arg2.(type) {
  369. case nil:
  370. return nil, nil
  371. case int64:
  372. prec = int(y)
  373. default:
  374. return nil, invArg(y, "formatFloat")
  375. }
  376. fallthrough
  377. case 2:
  378. arg1 := coerce1(arg[1], byte(0))
  379. switch y := arg1.(type) {
  380. case nil:
  381. return nil, nil
  382. case byte:
  383. fmt = y
  384. default:
  385. return nil, invArg(y, "formatFloat")
  386. }
  387. }
  388. return strconv.FormatFloat(val, fmt, prec, bitSize), nil
  389. }
  390. func builtinFormatInt(arg []interface{}, ctx map[interface{}]interface{}) (v interface{}, err error) {
  391. var intVal int64
  392. var uintVal uint64
  393. uintType := false
  394. base := 10
  395. switch x := arg[0].(type) {
  396. case nil:
  397. return nil, nil
  398. case int8:
  399. intVal = int64(x)
  400. case int16:
  401. intVal = int64(x)
  402. case int32:
  403. intVal = int64(x)
  404. case int64:
  405. intVal = x
  406. case uint8:
  407. uintType = true
  408. uintVal = uint64(x)
  409. case uint16:
  410. uintType = true
  411. uintVal = uint64(x)
  412. case uint32:
  413. uintType = true
  414. uintVal = uint64(x)
  415. case uint64:
  416. uintType = true
  417. uintVal = x
  418. default:
  419. return nil, invArg(x, "formatInt")
  420. }
  421. switch len(arg) {
  422. case 2:
  423. arg1 := coerce1(arg[1], int64(0))
  424. switch y := arg1.(type) {
  425. case nil:
  426. return nil, nil
  427. case int64:
  428. base = int(y)
  429. default:
  430. return nil, invArg(y, "formatInt")
  431. }
  432. }
  433. if uintType {
  434. return strconv.FormatUint(uintVal, base), nil
  435. }
  436. return strconv.FormatInt(intVal, base), nil
  437. }
  438. func builtinHasPrefix(arg []interface{}, _ map[interface{}]interface{}) (v interface{}, err error) {
  439. switch s := arg[0].(type) {
  440. case nil:
  441. return nil, nil
  442. case string:
  443. switch prefix := arg[1].(type) {
  444. case nil:
  445. return nil, nil
  446. case string:
  447. return strings.HasPrefix(s, prefix), nil
  448. default:
  449. return nil, invArg(prefix, "string")
  450. }
  451. default:
  452. return nil, invArg(s, "string")
  453. }
  454. }
  455. func builtinHasSuffix(arg []interface{}, _ map[interface{}]interface{}) (v interface{}, err error) {
  456. switch s := arg[0].(type) {
  457. case nil:
  458. return nil, nil
  459. case string:
  460. switch suffix := arg[1].(type) {
  461. case nil:
  462. return nil, nil
  463. case string:
  464. return strings.HasSuffix(s, suffix), nil
  465. default:
  466. return nil, invArg(suffix, "string")
  467. }
  468. default:
  469. return nil, invArg(s, "string")
  470. }
  471. }
  472. func builtinHour(arg []interface{}, ctx map[interface{}]interface{}) (v interface{}, err error) {
  473. switch x := arg[0].(type) {
  474. case nil:
  475. return nil, nil
  476. case time.Time:
  477. return int64(x.Hour()), nil
  478. default:
  479. return nil, invArg(x, "hour")
  480. }
  481. }
  482. func builtinHours(arg []interface{}, ctx map[interface{}]interface{}) (v interface{}, err error) {
  483. switch x := arg[0].(type) {
  484. case nil:
  485. return nil, nil
  486. case time.Duration:
  487. return x.Hours(), nil
  488. default:
  489. return nil, invArg(x, "hours")
  490. }
  491. }
  492. func builtinID(arg []interface{}, ctx map[interface{}]interface{}) (v interface{}, err error) {
  493. switch x := ctx["$id"].(type) {
  494. case map[string]interface{}:
  495. if len(arg) == 0 {
  496. return nil, nil
  497. }
  498. tab := arg[0].(*ident)
  499. id, ok := x[tab.s]
  500. if !ok {
  501. return nil, fmt.Errorf("value not available: id(%s)", tab)
  502. }
  503. if _, ok := id.(int64); ok {
  504. return id, nil
  505. }
  506. return nil, fmt.Errorf("value not available: id(%s)", tab)
  507. case int64:
  508. return x, nil
  509. default:
  510. return nil, nil
  511. }
  512. }
  513. func builtinImag(arg []interface{}, _ map[interface{}]interface{}) (v interface{}, err error) {
  514. switch x := arg[0].(type) {
  515. case nil:
  516. return nil, nil
  517. case idealComplex:
  518. return imag(x), nil
  519. case complex64:
  520. return imag(x), nil
  521. case complex128:
  522. return imag(x), nil
  523. default:
  524. return nil, invArg(x, "imag")
  525. }
  526. }
  527. func builtinMax(arg []interface{}, ctx map[interface{}]interface{}) (v interface{}, err error) {
  528. if _, ok := ctx["$agg0"]; ok {
  529. return
  530. }
  531. fn := ctx["$fn"]
  532. if _, ok := ctx["$agg"]; ok {
  533. if v, ok = ctx[fn]; ok {
  534. return
  535. }
  536. return nil, nil
  537. }
  538. max := ctx[fn]
  539. y := arg[0]
  540. if y == nil {
  541. return
  542. }
  543. switch x := max.(type) {
  544. case nil:
  545. switch y := y.(type) {
  546. case float32, float64, string, int8, int16, int32, int64, uint8, uint16, uint32, uint64, time.Time:
  547. max = y
  548. default:
  549. return nil, fmt.Errorf("max: cannot accept %v (value if type %T)", y, y)
  550. }
  551. case float32:
  552. if y := y.(float32); y > x {
  553. max = y
  554. }
  555. case float64:
  556. if y := y.(float64); y > x {
  557. max = y
  558. }
  559. case string:
  560. if y := y.(string); y > x {
  561. max = y
  562. }
  563. case int8:
  564. if y := y.(int8); y > x {
  565. max = y
  566. }
  567. case int16:
  568. if y := y.(int16); y > x {
  569. max = y
  570. }
  571. case int32:
  572. if y := y.(int32); y > x {
  573. max = y
  574. }
  575. case int64:
  576. if y := y.(int64); y > x {
  577. max = y
  578. }
  579. case uint8:
  580. if y := y.(uint8); y > x {
  581. max = y
  582. }
  583. case uint16:
  584. if y := y.(uint16); y > x {
  585. max = y
  586. }
  587. case uint32:
  588. if y := y.(uint32); y > x {
  589. max = y
  590. }
  591. case uint64:
  592. if y := y.(uint64); y > x {
  593. max = y
  594. }
  595. case time.Time:
  596. if y := y.(time.Time); y.After(x) {
  597. max = y
  598. }
  599. }
  600. ctx[fn] = max
  601. return
  602. }
  603. func builtinMin(arg []interface{}, ctx map[interface{}]interface{}) (v interface{}, err error) {
  604. if _, ok := ctx["$agg0"]; ok {
  605. return
  606. }
  607. fn := ctx["$fn"]
  608. if _, ok := ctx["$agg"]; ok {
  609. if v, ok = ctx[fn]; ok {
  610. return
  611. }
  612. return nil, nil
  613. }
  614. min := ctx[fn]
  615. y := arg[0]
  616. if y == nil {
  617. return
  618. }
  619. switch x := min.(type) {
  620. case nil:
  621. switch y := y.(type) {
  622. case float32, float64, string, int8, int16, int32, int64, uint8, uint16, uint32, uint64, time.Time:
  623. min = y
  624. default:
  625. return nil, fmt.Errorf("min: cannot accept %v (value if type %T)", y, y)
  626. }
  627. case float32:
  628. if y := y.(float32); y < x {
  629. min = y
  630. }
  631. case float64:
  632. if y := y.(float64); y < x {
  633. min = y
  634. }
  635. case string:
  636. if y := y.(string); y < x {
  637. min = y
  638. }
  639. case int8:
  640. if y := y.(int8); y < x {
  641. min = y
  642. }
  643. case int16:
  644. if y := y.(int16); y < x {
  645. min = y
  646. }
  647. case int32:
  648. if y := y.(int32); y < x {
  649. min = y
  650. }
  651. case int64:
  652. if y := y.(int64); y < x {
  653. min = y
  654. }
  655. case uint8:
  656. if y := y.(uint8); y < x {
  657. min = y
  658. }
  659. case uint16:
  660. if y := y.(uint16); y < x {
  661. min = y
  662. }
  663. case uint32:
  664. if y := y.(uint32); y < x {
  665. min = y
  666. }
  667. case uint64:
  668. if y := y.(uint64); y < x {
  669. min = y
  670. }
  671. case time.Time:
  672. if y := y.(time.Time); y.Before(x) {
  673. min = y
  674. }
  675. }
  676. ctx[fn] = min
  677. return
  678. }
  679. func builtinMinute(arg []interface{}, ctx map[interface{}]interface{}) (v interface{}, err error) {
  680. switch x := arg[0].(type) {
  681. case nil:
  682. return nil, nil
  683. case time.Time:
  684. return int64(x.Minute()), nil
  685. default:
  686. return nil, invArg(x, "minute")
  687. }
  688. }
  689. func builtinMinutes(arg []interface{}, ctx map[interface{}]interface{}) (v interface{}, err error) {
  690. switch x := arg[0].(type) {
  691. case nil:
  692. return nil, nil
  693. case time.Duration:
  694. return x.Minutes(), nil
  695. default:
  696. return nil, invArg(x, "minutes")
  697. }
  698. }
  699. func builtinMonth(arg []interface{}, ctx map[interface{}]interface{}) (v interface{}, err error) {
  700. switch x := arg[0].(type) {
  701. case nil:
  702. return nil, nil
  703. case time.Time:
  704. return int64(x.Month()), nil
  705. default:
  706. return nil, invArg(x, "month")
  707. }
  708. }
  709. func builtinNanosecond(arg []interface{}, ctx map[interface{}]interface{}) (v interface{}, err error) {
  710. switch x := arg[0].(type) {
  711. case nil:
  712. return nil, nil
  713. case time.Time:
  714. return int64(x.Nanosecond()), nil
  715. default:
  716. return nil, invArg(x, "nanosecond")
  717. }
  718. }
  719. func builtinNanoseconds(arg []interface{}, ctx map[interface{}]interface{}) (v interface{}, err error) {
  720. switch x := arg[0].(type) {
  721. case nil:
  722. return nil, nil
  723. case time.Duration:
  724. return x.Nanoseconds(), nil
  725. default:
  726. return nil, invArg(x, "nanoseconds")
  727. }
  728. }
  729. func builtinNow(arg []interface{}, ctx map[interface{}]interface{}) (v interface{}, err error) {
  730. return time.Now(), nil
  731. }
  732. func builtinParseTime(arg []interface{}, ctx map[interface{}]interface{}) (v interface{}, err error) {
  733. var a [2]string
  734. for i, v := range arg {
  735. switch x := v.(type) {
  736. case nil:
  737. return nil, nil
  738. case string:
  739. a[i] = x
  740. default:
  741. return nil, invArg(x, "parseTime")
  742. }
  743. }
  744. t, err := time.Parse(a[0], a[1])
  745. if err != nil {
  746. return nil, err
  747. }
  748. ls := t.Location().String()
  749. if ls == "UTC" {
  750. return t, nil
  751. }
  752. l, err := time.LoadLocation(ls)
  753. if err != nil {
  754. return t, nil
  755. }
  756. return time.ParseInLocation(a[0], a[1], l)
  757. }
  758. func builtinReal(arg []interface{}, _ map[interface{}]interface{}) (v interface{}, err error) {
  759. switch x := arg[0].(type) {
  760. case nil:
  761. return nil, nil
  762. case idealComplex:
  763. return real(x), nil
  764. case complex64:
  765. return real(x), nil
  766. case complex128:
  767. return real(x), nil
  768. default:
  769. return nil, invArg(x, "real")
  770. }
  771. }
  772. func builtinSecond(arg []interface{}, ctx map[interface{}]interface{}) (v interface{}, err error) {
  773. switch x := arg[0].(type) {
  774. case nil:
  775. return nil, nil
  776. case time.Time:
  777. return int64(x.Second()), nil
  778. default:
  779. return nil, invArg(x, "second")
  780. }
  781. }
  782. func builtinSeconds(arg []interface{}, ctx map[interface{}]interface{}) (v interface{}, err error) {
  783. switch x := arg[0].(type) {
  784. case nil:
  785. return nil, nil
  786. case time.Duration:
  787. return x.Seconds(), nil
  788. default:
  789. return nil, invArg(x, "seconds")
  790. }
  791. }
  792. func builtinSince(arg []interface{}, ctx map[interface{}]interface{}) (v interface{}, err error) {
  793. switch x := arg[0].(type) {
  794. case nil:
  795. return nil, nil
  796. case time.Time:
  797. return time.Since(x), nil
  798. default:
  799. return nil, invArg(x, "since")
  800. }
  801. }
  802. func builtinSum(arg []interface{}, ctx map[interface{}]interface{}) (v interface{}, err error) {
  803. if _, ok := ctx["$agg0"]; ok {
  804. return
  805. }
  806. fn := ctx["$fn"]
  807. if _, ok := ctx["$agg"]; ok {
  808. if v, ok = ctx[fn]; ok {
  809. return
  810. }
  811. return nil, nil
  812. }
  813. sum := ctx[fn]
  814. y := arg[0]
  815. if y == nil {
  816. return
  817. }
  818. switch x := sum.(type) {
  819. case nil:
  820. switch y := y.(type) {
  821. case complex64, complex128, float32, float64, int8, int16, int32, int64, uint8, uint16, uint32, uint64:
  822. sum = y
  823. default:
  824. return nil, fmt.Errorf("sum: cannot accept %v (value if type %T)", y, y)
  825. }
  826. case complex64:
  827. sum = x + y.(complex64)
  828. case complex128:
  829. sum = x + y.(complex128)
  830. case float32:
  831. sum = x + y.(float32)
  832. case float64:
  833. sum = x + y.(float64)
  834. case int8:
  835. sum = x + y.(int8)
  836. case int16:
  837. sum = x + y.(int16)
  838. case int32:
  839. sum = x + y.(int32)
  840. case int64:
  841. sum = x + y.(int64)
  842. case uint8:
  843. sum = x + y.(uint8)
  844. case uint16:
  845. sum = x + y.(uint16)
  846. case uint32:
  847. sum = x + y.(uint32)
  848. case uint64:
  849. sum = x + y.(uint64)
  850. }
  851. ctx[fn] = sum
  852. return
  853. }
  854. func builtinTimeIn(arg []interface{}, ctx map[interface{}]interface{}) (v interface{}, err error) {
  855. switch x := arg[0].(type) {
  856. case nil:
  857. return nil, nil
  858. case time.Time:
  859. switch y := arg[1].(type) {
  860. case nil:
  861. return nil, nil
  862. case string:
  863. loc := time.Local
  864. switch y {
  865. case "local":
  866. default:
  867. loc, err = time.LoadLocation(y)
  868. if err != nil {
  869. return
  870. }
  871. }
  872. return x.In(loc), nil
  873. default:
  874. return nil, invArg(x, "timeIn")
  875. }
  876. default:
  877. return nil, invArg(x, "timeIn")
  878. }
  879. }
  880. func builtinWeekday(arg []interface{}, ctx map[interface{}]interface{}) (v interface{}, err error) {
  881. switch x := arg[0].(type) {
  882. case nil:
  883. return nil, nil
  884. case time.Time:
  885. return int64(x.Weekday()), nil
  886. default:
  887. return nil, invArg(x, "weekday")
  888. }
  889. }
  890. func builtinYear(arg []interface{}, ctx map[interface{}]interface{}) (v interface{}, err error) {
  891. switch x := arg[0].(type) {
  892. case nil:
  893. return nil, nil
  894. case time.Time:
  895. return int64(x.Year()), nil
  896. default:
  897. return nil, invArg(x, "year")
  898. }
  899. }
  900. func builtinYearday(arg []interface{}, ctx map[interface{}]interface{}) (v interface{}, err error) {
  901. switch x := arg[0].(type) {
  902. case nil:
  903. return nil, nil
  904. case time.Time:
  905. return int64(x.YearDay()), nil
  906. default:
  907. return nil, invArg(x, "yearDay")
  908. }
  909. }