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