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