github.com/runner-mei/ql@v1.1.0/etc.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 "bytes" 9 "fmt" 10 "io" 11 "math" 12 "math/big" 13 "time" 14 ) 15 16 // QL types. 17 const ( 18 qBool = 0x62 // 'b' 19 qComplex64 = 0x63 // 'c' 20 qComplex128 = 0x64 // 'd' 21 qFloat32 = 0x66 // 'f' 22 qFloat64 = 0x67 // 'g', alias float 23 qInt8 = 0x69 // 'i' 24 qInt16 = 0x6a // 'j' 25 qInt32 = 0x6b // 'k' 26 qInt64 = 0x6c // 'l', alias int 27 qString = 0x73 // 's' 28 qUint8 = 0x75 // 'u', alias byte 29 qUint16 = 0x76 // 'v' 30 qUint32 = 0x77 // 'w' 31 qUint64 = 0x78 // 'x', alias uint 32 33 qBigInt = 0x49 // 'I' 34 qBigRat = 0x52 // 'R' 35 qBlob = 0x42 // 'B' 36 qDuration = 0x44 // 'D' 37 qTime = 0x54 // 'T' 38 ) 39 40 var ( 41 type2Str = map[int]string{ 42 qBigInt: "bigint", 43 qBigRat: "bigrat", 44 qBlob: "blob", 45 qBool: "bool", 46 qComplex128: "complex128", 47 qComplex64: "complex64", 48 qDuration: "duration", 49 qFloat32: "float32", 50 qFloat64: "float64", 51 qInt16: "int16", 52 qInt32: "int32", 53 qInt64: "int64", 54 qInt8: "int8", 55 qString: "string", 56 qTime: "time", 57 qUint16: "uint16", 58 qUint32: "uint32", 59 qUint64: "uint64", 60 qUint8: "uint8", 61 } 62 ) 63 64 func typeStr(typ int) (r string) { 65 return type2Str[typ] 66 } 67 68 func noEOF(err error) error { 69 if err == io.EOF { 70 err = nil 71 } 72 return err 73 } 74 75 func runErr(err error) error { return fmt.Errorf("run time error: %s", err) } 76 77 func invXOp(s, x interface{}) error { 78 return fmt.Errorf("invalid operation: %v[%v] (index of type %T)", s, x, x) 79 } 80 81 func invSOp(s interface{}) error { 82 return fmt.Errorf("cannot slice %s (type %T)", s, s) 83 } 84 85 func invNegX(x interface{}) error { 86 return fmt.Errorf("invalid string index %v (index must be non-negative)", x) 87 } 88 89 func invNegLO(x interface{}) error { 90 return fmt.Errorf("invalid LIMIT or OFFSET value %v (must be non-negative)", x) 91 } 92 93 func invSliceNegX(x interface{}) error { 94 return fmt.Errorf("invalid slice index %v (index must be non-negative)", x) 95 } 96 97 func invBoundX(s string, x uint64) error { 98 return fmt.Errorf("invalid string index %d (out of bounds for %d-byte string)", x, len(s)) 99 } 100 101 func invSliceBoundX(s string, x uint64) error { 102 return fmt.Errorf("invalid slice index %d (out of bounds for %d-byte string)", x, len(s)) 103 } 104 105 func intExpr(x interface{}) (i int64, err error) { 106 switch x := x.(type) { 107 case idealInt: 108 if x < 0 { 109 return 0, invNegLO(x) 110 } 111 112 return int64(x), nil 113 case idealRune: 114 if x < 0 { 115 return 0, invNegLO(x) 116 } 117 118 return int64(x), nil 119 case idealUint: 120 if x < 0 { 121 return 0, invNegLO(x) 122 } 123 124 return int64(x), nil 125 case int8: 126 if x < 0 { 127 return 0, invNegLO(x) 128 } 129 130 return int64(x), nil 131 case int16: 132 if x < 0 { 133 return 0, invNegLO(x) 134 } 135 136 return int64(x), nil 137 case int32: 138 if x < 0 { 139 return 0, invNegLO(x) 140 } 141 142 return int64(x), nil 143 case int64: 144 if x < 0 { 145 return 0, invNegLO(x) 146 } 147 148 return int64(x), nil 149 case uint8: 150 return int64(x), nil 151 case uint16: 152 return int64(x), nil 153 case uint32: 154 return int64(x), nil 155 case uint64: 156 return int64(x), nil 157 default: 158 return 0, fmt.Errorf("non-integer expression: %v (value of type %T)", x, x) 159 } 160 } 161 162 func limOffExpr(x interface{}) (i uint64, err error) { 163 switch x := x.(type) { 164 case idealInt: 165 if x < 0 { 166 return 0, invNegLO(x) 167 } 168 169 return uint64(x), nil 170 case idealRune: 171 if x < 0 { 172 return 0, invNegLO(x) 173 } 174 175 return uint64(x), nil 176 case idealUint: 177 if x < 0 { 178 return 0, invNegLO(x) 179 } 180 181 return uint64(x), nil 182 case int8: 183 if x < 0 { 184 return 0, invNegLO(x) 185 } 186 187 return uint64(x), nil 188 case int16: 189 if x < 0 { 190 return 0, invNegLO(x) 191 } 192 193 return uint64(x), nil 194 case int32: 195 if x < 0 { 196 return 0, invNegLO(x) 197 } 198 199 return uint64(x), nil 200 case int64: 201 if x < 0 { 202 return 0, invNegLO(x) 203 } 204 205 return uint64(x), nil 206 case uint8: 207 return uint64(x), nil 208 case uint16: 209 return uint64(x), nil 210 case uint32: 211 return uint64(x), nil 212 case uint64: 213 return uint64(x), nil 214 default: 215 return 0, fmt.Errorf("non-integer used in LIMIT or OFFSET: %v (value of type %T)", x, x) 216 } 217 } 218 219 func indexExpr(s *string, x interface{}) (i uint64, err error) { 220 switch x := x.(type) { 221 case idealFloat: 222 if x < 0 { 223 return 0, invNegX(x) 224 } 225 226 if s != nil && int(x) >= len(*s) { 227 return 0, invBoundX(*s, uint64(x)) 228 } 229 230 return uint64(x), nil 231 case idealInt: 232 if x < 0 { 233 return 0, invNegX(x) 234 } 235 236 if s != nil && int64(x) >= int64(len(*s)) { 237 return 0, invBoundX(*s, uint64(x)) 238 } 239 240 return uint64(x), nil 241 case idealRune: 242 if x < 0 { 243 return 0, invNegX(x) 244 } 245 246 if s != nil && int32(x) >= int32(len(*s)) { 247 return 0, invBoundX(*s, uint64(x)) 248 } 249 250 return uint64(x), nil 251 case idealUint: 252 if x < 0 { 253 return 0, invNegX(x) 254 } 255 256 if s != nil && uint64(x) >= uint64(len(*s)) { 257 return 0, invBoundX(*s, uint64(x)) 258 } 259 260 return uint64(x), nil 261 case int8: 262 if x < 0 { 263 return 0, invNegX(x) 264 } 265 266 if s != nil && int(x) >= len(*s) { 267 return 0, invBoundX(*s, uint64(x)) 268 } 269 270 return uint64(x), nil 271 case int16: 272 if x < 0 { 273 return 0, invNegX(x) 274 } 275 276 if s != nil && int(x) >= len(*s) { 277 return 0, invBoundX(*s, uint64(x)) 278 } 279 280 return uint64(x), nil 281 case int32: 282 if x < 0 { 283 return 0, invNegX(x) 284 } 285 286 if s != nil && int(x) >= len(*s) { 287 return 0, invBoundX(*s, uint64(x)) 288 } 289 290 return uint64(x), nil 291 case int64: 292 if x < 0 { 293 return 0, invNegX(x) 294 } 295 296 if s != nil && x >= int64(len(*s)) { 297 return 0, invBoundX(*s, uint64(x)) 298 } 299 300 return uint64(x), nil 301 case uint8: 302 if s != nil && int(x) >= len(*s) { 303 return 0, invBoundX(*s, uint64(x)) 304 } 305 306 return uint64(x), nil 307 case uint16: 308 if s != nil && int(x) >= len(*s) { 309 return 0, invBoundX(*s, uint64(x)) 310 } 311 312 return uint64(x), nil 313 case uint32: 314 if s != nil && x >= uint32(len(*s)) { 315 return 0, invBoundX(*s, uint64(x)) 316 } 317 318 return uint64(x), nil 319 case uint64: 320 if s != nil && x >= uint64(len(*s)) { 321 return 0, invBoundX(*s, uint64(x)) 322 } 323 324 return uint64(x), nil 325 default: 326 return 0, fmt.Errorf("non-integer string index %v (value of type %T)", x, x) 327 } 328 } 329 330 func sliceExpr(s *string, x interface{}, mod int) (i uint64, err error) { 331 switch x := x.(type) { 332 case idealFloat: 333 if x < 0 { 334 return 0, invSliceNegX(x) 335 } 336 337 if s != nil && int(x) >= len(*s)+mod { 338 return 0, invSliceBoundX(*s, uint64(x)) 339 } 340 341 return uint64(x), nil 342 case idealInt: 343 if x < 0 { 344 return 0, invSliceNegX(x) 345 } 346 347 if s != nil && int64(x) >= int64(len(*s)+mod) { 348 return 0, invSliceBoundX(*s, uint64(x)) 349 } 350 351 return uint64(x), nil 352 case idealRune: 353 if x < 0 { 354 return 0, invSliceNegX(x) 355 } 356 357 if s != nil && int32(x) >= int32(len(*s)+mod) { 358 return 0, invSliceBoundX(*s, uint64(x)) 359 } 360 361 return uint64(x), nil 362 case idealUint: 363 if x < 0 { 364 return 0, invSliceNegX(x) 365 } 366 367 if s != nil && uint64(x) >= uint64(len(*s)+mod) { 368 return 0, invSliceBoundX(*s, uint64(x)) 369 } 370 371 return uint64(x), nil 372 case int8: 373 if x < 0 { 374 return 0, invSliceNegX(x) 375 } 376 377 if s != nil && int(x) >= len(*s)+mod { 378 return 0, invSliceBoundX(*s, uint64(x)) 379 } 380 381 return uint64(x), nil 382 case int16: 383 if x < 0 { 384 return 0, invSliceNegX(x) 385 } 386 387 if s != nil && int(x) >= len(*s)+mod { 388 return 0, invSliceBoundX(*s, uint64(x)) 389 } 390 391 return uint64(x), nil 392 case int32: 393 if x < 0 { 394 return 0, invSliceNegX(x) 395 } 396 397 if s != nil && int(x) >= len(*s)+mod { 398 return 0, invSliceBoundX(*s, uint64(x)) 399 } 400 401 return uint64(x), nil 402 case int64: 403 if x < 0 { 404 return 0, invSliceNegX(x) 405 } 406 407 if s != nil && x >= int64(len(*s)+mod) { 408 return 0, invSliceBoundX(*s, uint64(x)) 409 } 410 411 return uint64(x), nil 412 case uint8: 413 if s != nil && int(x) >= len(*s)+mod { 414 return 0, invSliceBoundX(*s, uint64(x)) 415 } 416 417 return uint64(x), nil 418 case uint16: 419 if s != nil && int(x) >= len(*s)+mod { 420 return 0, invSliceBoundX(*s, uint64(x)) 421 } 422 423 return uint64(x), nil 424 case uint32: 425 if s != nil && x >= uint32(len(*s)+mod) { 426 return 0, invSliceBoundX(*s, uint64(x)) 427 } 428 429 return uint64(x), nil 430 case uint64: 431 if s != nil && x >= uint64(len(*s)+mod) { 432 return 0, invSliceBoundX(*s, uint64(x)) 433 } 434 435 return uint64(x), nil 436 default: 437 return 0, fmt.Errorf("invalid slice index %s (type %T)", x, x) 438 } 439 } 440 441 type iop int 442 443 func (o iop) String() string { 444 switch i := int(o); i { 445 case andand: 446 return "&&" 447 case andnot: 448 return "&^" 449 case lsh: 450 return "<<" 451 case le: 452 return "<=" 453 case eq: 454 return "==" 455 case ge: 456 return ">=" 457 case neq: 458 return "!=" 459 case oror: 460 return "||" 461 case rsh: 462 return ">>" 463 default: 464 return string(i) 465 } 466 } 467 468 func ideal(v interface{}) interface{} { 469 switch x := v.(type) { 470 case idealComplex: 471 return complex128(x) 472 case idealFloat: 473 return float64(x) 474 case idealInt: 475 return int64(x) 476 case idealRune: 477 return int64(x) 478 case idealUint: 479 return uint64(x) 480 default: 481 return v 482 } 483 } 484 485 func eval(v expression, execCtx *execCtx, ctx map[interface{}]interface{}) (y interface{}) { 486 y, err := expand1(v.eval(execCtx, ctx)) 487 if err != nil { 488 panic(err) // panic ok here 489 } 490 return 491 } 492 493 func eval2(a, b expression, execCtx *execCtx, ctx map[interface{}]interface{}) (x, y interface{}) { 494 return eval(a, execCtx, ctx), eval(b, execCtx, ctx) 495 } 496 497 func invOp2(x, y interface{}, o int) (interface{}, error) { 498 return nil, fmt.Errorf("invalid operation: %v %v %v (mismatched types %T and %T)", x, iop(o), y, ideal(x), ideal(y)) 499 } 500 501 func undOp(x interface{}, o int) (interface{}, error) { 502 return nil, fmt.Errorf("invalid operation: %v%v (operator %v not defined on %T)", iop(o), x, iop(o), x) 503 } 504 505 func undOp2(x, y interface{}, o int) (interface{}, error) { 506 return nil, fmt.Errorf("invalid operation: %v %v %v (operator %v not defined on %T)", x, iop(o), y, iop(o), x) 507 } 508 509 func invConv(val interface{}, typ int) (interface{}, error) { 510 return nil, fmt.Errorf("cannot convert %v (type %T) to type %s", val, val, typeStr(typ)) 511 } 512 513 func truncConv(val interface{}) (interface{}, error) { 514 return nil, fmt.Errorf("constant %v truncated to integer", val) 515 } 516 517 func convert(val interface{}, typ int) (v interface{}, err error) { //NTYPE 518 if val == nil { 519 return nil, nil 520 } 521 522 switch typ { 523 case qBool: 524 switch x := val.(type) { 525 //case nil: 526 //case idealComplex: 527 //case idealFloat: 528 //case idealInt: 529 //case idealRune: 530 //case idealUint: 531 case bool: 532 return bool(x), nil 533 //case complex64: 534 //case complex128: 535 //case float32: 536 //case float64: 537 //case int8: 538 //case int16: 539 //case int32: 540 //case int64: 541 //case string: 542 //case uint8: 543 //case uint16: 544 //case uint32: 545 //case uint64: 546 default: 547 return invConv(val, typ) 548 } 549 case qComplex64: 550 switch x := val.(type) { 551 //case nil: 552 case idealComplex: 553 return complex64(x), nil 554 case idealFloat: 555 return complex(float32(x), 0), nil 556 case idealInt: 557 return complex(float32(x), 0), nil 558 case idealRune: 559 return complex(float32(x), 0), nil 560 case idealUint: 561 return complex(float32(x), 0), nil 562 //case bool: 563 case complex64: 564 return complex64(x), nil 565 case complex128: 566 return complex64(x), nil 567 //case float32: 568 //case float64: 569 //case int8: 570 //case int16: 571 //case int32: 572 //case int64: 573 //case string: 574 //case uint8: 575 //case uint16: 576 //case uint32: 577 //case uint64: 578 default: 579 return invConv(val, typ) 580 } 581 case qComplex128: 582 switch x := val.(type) { 583 //case nil: 584 case idealComplex: 585 return complex128(x), nil 586 case idealFloat: 587 return complex(float64(x), 0), nil 588 case idealInt: 589 return complex(float64(x), 0), nil 590 case idealRune: 591 return complex(float64(x), 0), nil 592 case idealUint: 593 return complex(float64(x), 0), nil 594 //case bool: 595 case complex64: 596 return complex128(x), nil 597 case complex128: 598 return complex128(x), nil 599 //case float32: 600 //case float64: 601 //case int8: 602 //case int16: 603 //case int32: 604 //case int64: 605 //case string: 606 //case uint8: 607 //case uint16: 608 //case uint32: 609 //case uint64: 610 default: 611 return invConv(val, typ) 612 } 613 case qFloat32: 614 switch x := val.(type) { 615 //case nil: 616 //case idealComplex: 617 case idealFloat: 618 return float32(x), nil 619 case idealInt: 620 return float32(x), nil 621 case idealRune: 622 return float32(x), nil 623 case idealUint: 624 return float32(x), nil 625 //case bool: 626 //case complex64: 627 //case complex128: 628 case float32: 629 return float32(x), nil 630 case float64: 631 return float32(x), nil 632 case int8: 633 return float32(x), nil 634 case int16: 635 return float32(x), nil 636 case int32: 637 return float32(x), nil 638 case int64: 639 return float32(x), nil 640 //case string: 641 case uint8: 642 return float32(x), nil 643 case uint16: 644 return float32(x), nil 645 case uint32: 646 return float32(x), nil 647 case uint64: 648 return float32(x), nil 649 case *big.Int: 650 v, _ := big.NewRat(1, 1).SetInt(x).Float64() 651 return float32(v), nil 652 case *big.Rat: 653 v, _ := x.Float64() 654 return float32(v), nil 655 case time.Duration: 656 return float32(x), nil 657 default: 658 return invConv(val, typ) 659 } 660 case qFloat64: 661 switch x := val.(type) { 662 //case nil: 663 //case idealComplex: 664 case idealFloat: 665 return float64(x), nil 666 case idealInt: 667 return float64(x), nil 668 case idealRune: 669 return float64(x), nil 670 case idealUint: 671 return float64(x), nil 672 //case bool: 673 //case complex64: 674 //case complex128: 675 case float32: 676 return float64(x), nil 677 case float64: 678 return float64(x), nil 679 case int8: 680 return float64(x), nil 681 case int16: 682 return float64(x), nil 683 case int32: 684 return float64(x), nil 685 case int64: 686 return float64(x), nil 687 //case string: 688 case uint8: 689 return float64(x), nil 690 case uint16: 691 return float64(x), nil 692 case uint32: 693 return float64(x), nil 694 case uint64: 695 return float64(x), nil 696 case *big.Int: 697 v, _ := big.NewRat(1, 1).SetInt(x).Float64() 698 return v, nil 699 case *big.Rat: 700 v, _ := x.Float64() 701 return v, nil 702 case time.Duration: 703 return float64(x), nil 704 default: 705 return invConv(val, typ) 706 } 707 case qInt8: 708 switch x := val.(type) { 709 //case nil: 710 //case idealComplex: 711 case idealFloat: 712 if _, frac := math.Modf(float64(x)); frac != 0 { 713 return truncConv(x) 714 } 715 716 return int8(x), nil 717 case idealInt: 718 return int8(x), nil 719 case idealRune: 720 return int8(x), nil 721 case idealUint: 722 return int8(x), nil 723 //case bool: 724 //case complex64: 725 //case complex128: 726 case float32: 727 return int8(x), nil 728 case float64: 729 return int8(x), nil 730 case int8: 731 return int8(x), nil 732 case int16: 733 return int8(x), nil 734 case int32: 735 return int8(x), nil 736 case int64: 737 return int8(x), nil 738 //case string: 739 case uint8: 740 return int8(x), nil 741 case uint16: 742 return int8(x), nil 743 case uint32: 744 return int8(x), nil 745 case uint64: 746 return int8(x), nil 747 case *big.Int: 748 return int8(x.Int64()), nil 749 case time.Duration: 750 return int8(x), nil 751 default: 752 return invConv(val, typ) 753 } 754 case qInt16: 755 switch x := val.(type) { 756 //case nil: 757 //case idealComplex: 758 case idealFloat: 759 if _, frac := math.Modf(float64(x)); frac != 0 { 760 return truncConv(x) 761 } 762 763 return int16(x), nil 764 case idealInt: 765 return int16(x), nil 766 case idealRune: 767 return int16(x), nil 768 case idealUint: 769 return int16(x), nil 770 //case bool: 771 //case complex64: 772 //case complex128: 773 case float32: 774 return int16(x), nil 775 case float64: 776 return int16(x), nil 777 case int8: 778 return int16(x), nil 779 case int16: 780 return int16(x), nil 781 case int32: 782 return int16(x), nil 783 case int64: 784 return int16(x), nil 785 //case string: 786 case uint8: 787 return int16(x), nil 788 case uint16: 789 return int16(x), nil 790 case uint32: 791 return int16(x), nil 792 case uint64: 793 return int16(x), nil 794 case *big.Int: 795 return int16(x.Int64()), nil 796 case time.Duration: 797 return int16(x), nil 798 default: 799 return invConv(val, typ) 800 } 801 case qInt32: 802 switch x := val.(type) { 803 //case nil: 804 //case idealComplex: 805 case idealFloat: 806 if _, frac := math.Modf(float64(x)); frac != 0 { 807 return truncConv(x) 808 } 809 810 return int32(x), nil 811 case idealInt: 812 return int32(x), nil 813 case idealRune: 814 return int32(x), nil 815 case idealUint: 816 return int32(x), nil 817 //case bool: 818 //case complex64: 819 //case complex128: 820 case float32: 821 return int32(x), nil 822 case float64: 823 return int32(x), nil 824 case int8: 825 return int32(x), nil 826 case int16: 827 return int32(x), nil 828 case int32: 829 return int32(x), nil 830 case int64: 831 return int32(x), nil 832 //case string: 833 case uint8: 834 return int32(x), nil 835 case uint16: 836 return int32(x), nil 837 case uint32: 838 return int32(x), nil 839 case uint64: 840 return int32(x), nil 841 case *big.Int: 842 return int32(x.Int64()), nil 843 case time.Duration: 844 return int32(x), nil 845 default: 846 return invConv(val, typ) 847 } 848 case qInt64: 849 switch x := val.(type) { 850 //case nil: 851 //case idealComplex: 852 case idealFloat: 853 if _, frac := math.Modf(float64(x)); frac != 0 { 854 return truncConv(x) 855 } 856 857 return int64(x), nil 858 case idealInt: 859 return int64(x), nil 860 case idealRune: 861 return int64(x), nil 862 case idealUint: 863 return int64(x), nil 864 //case bool: 865 //case complex64: 866 //case complex128: 867 case float32: 868 return int64(x), nil 869 case float64: 870 return int64(x), nil 871 case int8: 872 return int64(x), nil 873 case int16: 874 return int64(x), nil 875 case int32: 876 return int64(x), nil 877 case int64: 878 return int64(x), nil 879 //case string: 880 case uint8: 881 return int64(x), nil 882 case uint16: 883 return int64(x), nil 884 case uint32: 885 return int64(x), nil 886 case uint64: 887 return int64(x), nil 888 case *big.Int: 889 return x.Int64(), nil 890 case time.Duration: 891 return int64(x), nil 892 default: 893 return invConv(val, typ) 894 } 895 case qString: 896 switch x := val.(type) { 897 //case nil: 898 //case idealComplex: 899 //case idealFloat: 900 case idealInt: 901 return string(x), nil 902 case idealRune: 903 return string(x), nil 904 case idealUint: 905 return string(x), nil 906 //case bool: 907 //case complex64: 908 //case complex128: 909 //case float32: 910 //case float64: 911 case int8: 912 return string(x), nil 913 case int16: 914 return string(x), nil 915 case int32: 916 return string(x), nil 917 case int64: 918 return string(x), nil 919 case string: 920 return string(x), nil 921 case uint8: 922 return string(x), nil 923 case uint16: 924 return string(x), nil 925 case uint32: 926 return string(x), nil 927 case uint64: 928 return string(x), nil 929 case []byte: 930 return string(x), nil 931 case *big.Int: 932 return x.String(), nil 933 case time.Time: 934 return x.String(), nil 935 case time.Duration: 936 return x.String(), nil 937 default: 938 return invConv(val, typ) 939 } 940 case qUint8: 941 switch x := val.(type) { 942 //case nil: 943 //case idealComplex: 944 case idealFloat: 945 if _, frac := math.Modf(float64(x)); frac != 0 { 946 return truncConv(x) 947 } 948 949 return uint8(x), nil 950 case idealInt: 951 return uint8(x), nil 952 case idealRune: 953 return uint8(x), nil 954 case idealUint: 955 return uint8(x), nil 956 //case bool: 957 //case complex64: 958 //case complex128: 959 case float32: 960 return uint8(x), nil 961 case float64: 962 return uint8(x), nil 963 case int8: 964 return uint8(x), nil 965 case int16: 966 return uint8(x), nil 967 case int32: 968 return uint8(x), nil 969 case int64: 970 return uint8(x), nil 971 //case string: 972 case uint8: 973 return uint8(x), nil 974 case uint16: 975 return uint8(x), nil 976 case uint32: 977 return uint8(x), nil 978 case uint64: 979 return uint8(x), nil 980 case *big.Int: 981 return uint8(x.Int64()), nil 982 case time.Duration: 983 return uint8(x), nil 984 default: 985 return invConv(val, typ) 986 } 987 case qUint16: 988 switch x := val.(type) { 989 //case nil: 990 //case idealComplex: 991 case idealFloat: 992 if _, frac := math.Modf(float64(x)); frac != 0 { 993 return truncConv(x) 994 } 995 996 return uint16(x), nil 997 case idealInt: 998 return uint16(x), nil 999 case idealRune: 1000 return uint16(x), nil 1001 case idealUint: 1002 return uint16(x), nil 1003 //case bool: 1004 //case complex64: 1005 //case complex128: 1006 case float32: 1007 return uint16(x), nil 1008 case float64: 1009 return uint16(x), nil 1010 case int8: 1011 return uint16(x), nil 1012 case int16: 1013 return uint16(x), nil 1014 case int32: 1015 return uint16(x), nil 1016 case int64: 1017 return uint16(x), nil 1018 //case string: 1019 case uint8: 1020 return uint16(x), nil 1021 case uint16: 1022 return uint16(x), nil 1023 case uint32: 1024 return uint16(x), nil 1025 case uint64: 1026 return uint16(x), nil 1027 case *big.Int: 1028 return uint16(x.Int64()), nil 1029 case time.Duration: 1030 return uint16(x), nil 1031 default: 1032 return invConv(val, typ) 1033 } 1034 case qUint32: 1035 switch x := val.(type) { 1036 //case nil: 1037 //case idealComplex: 1038 case idealFloat: 1039 if _, frac := math.Modf(float64(x)); frac != 0 { 1040 return truncConv(x) 1041 } 1042 1043 return uint32(x), nil 1044 case idealInt: 1045 return uint32(x), nil 1046 case idealRune: 1047 return uint32(x), nil 1048 case idealUint: 1049 return uint32(x), nil 1050 //case bool: 1051 //case complex64: 1052 //case complex128: 1053 case float32: 1054 return uint32(x), nil 1055 case float64: 1056 return uint32(x), nil 1057 case int8: 1058 return uint32(x), nil 1059 case int16: 1060 return uint32(x), nil 1061 case int32: 1062 return uint32(x), nil 1063 case int64: 1064 return uint32(x), nil 1065 //case string: 1066 case uint8: 1067 return uint32(x), nil 1068 case uint16: 1069 return uint32(x), nil 1070 case uint32: 1071 return uint32(x), nil 1072 case uint64: 1073 return uint32(x), nil 1074 case *big.Int: 1075 return uint32(x.Int64()), nil 1076 case time.Duration: 1077 return uint32(x), nil 1078 default: 1079 return invConv(val, typ) 1080 } 1081 case qUint64: 1082 switch x := val.(type) { 1083 //case nil: 1084 //case idealComplex: 1085 case idealFloat: 1086 if _, frac := math.Modf(float64(x)); frac != 0 { 1087 return truncConv(x) 1088 } 1089 1090 return uint64(x), nil 1091 case idealInt: 1092 return uint64(x), nil 1093 case idealRune: 1094 return uint64(x), nil 1095 case idealUint: 1096 return uint64(x), nil 1097 //case bool: 1098 //case complex64: 1099 //case complex128: 1100 case float32: 1101 return uint64(x), nil 1102 case float64: 1103 return uint64(x), nil 1104 case int8: 1105 return uint64(x), nil 1106 case int16: 1107 return uint64(x), nil 1108 case int32: 1109 return uint64(x), nil 1110 case int64: 1111 return uint64(x), nil 1112 //case string: 1113 case uint8: 1114 return uint64(x), nil 1115 case uint16: 1116 return uint64(x), nil 1117 case uint32: 1118 return uint64(x), nil 1119 case uint64: 1120 return uint64(x), nil 1121 case *big.Int: 1122 return x.Uint64(), nil 1123 case time.Duration: 1124 return uint64(x), nil 1125 default: 1126 return invConv(val, typ) 1127 } 1128 case qBlob: 1129 switch x := val.(type) { 1130 case string: 1131 return []byte(x), nil 1132 case []byte: 1133 return x, nil 1134 default: 1135 return invConv(val, typ) 1136 } 1137 case qBigInt: 1138 switch x := val.(type) { 1139 // case blob 1140 // case bool 1141 //case idealComplex: 1142 case idealFloat: 1143 if _, frac := math.Modf(float64(x)); frac != 0 { 1144 return truncConv(x) 1145 } 1146 1147 rr := big.NewRat(1, 1).SetFloat64(float64(x)) 1148 ii := big.NewInt(0).Set(rr.Num()) 1149 ii.Quo(ii, rr.Denom()) 1150 return ii, nil 1151 case idealInt: 1152 return big.NewInt(0).SetInt64(int64(x)), nil 1153 case idealRune: 1154 return big.NewInt(0).SetInt64(int64(x)), nil 1155 case idealUint: 1156 return big.NewInt(0).SetUint64(uint64(x)), nil 1157 //case complex64 1158 //case complex128 1159 case float32: 1160 rr := big.NewRat(1, 1).SetFloat64(float64(x)) 1161 ii := big.NewInt(0).Set(rr.Num()) 1162 ii.Quo(ii, rr.Denom()) 1163 return ii, nil 1164 case float64: 1165 rr := big.NewRat(1, 1).SetFloat64(float64(x)) 1166 ii := big.NewInt(0).Set(rr.Num()) 1167 ii.Quo(ii, rr.Denom()) 1168 return ii, nil 1169 case int8: 1170 return big.NewInt(0).SetInt64(int64(x)), nil 1171 case int16: 1172 return big.NewInt(0).SetInt64(int64(x)), nil 1173 case int32: 1174 return big.NewInt(0).SetInt64(int64(x)), nil 1175 case int64: 1176 return big.NewInt(0).SetInt64(x), nil 1177 case string: 1178 y := big.NewInt(0) 1179 if _, ok := y.SetString(x, 0); !ok { 1180 return invConv(val, typ) 1181 } 1182 1183 return y, nil 1184 case uint8: 1185 return big.NewInt(0).SetUint64(uint64(x)), nil 1186 case uint16: 1187 return big.NewInt(0).SetUint64(uint64(x)), nil 1188 case uint32: 1189 return big.NewInt(0).SetUint64(uint64(x)), nil 1190 case uint64: 1191 return big.NewInt(0).SetUint64(x), nil 1192 case *big.Int: 1193 return x, nil 1194 case *big.Rat: 1195 ii := big.NewInt(0).Set(x.Num()) 1196 ii.Div(ii, x.Denom()) 1197 return ii, nil 1198 default: 1199 return invConv(val, typ) 1200 } 1201 case qBigRat: 1202 switch x := val.(type) { 1203 // case blob 1204 // case bool 1205 //case idealComplex: 1206 case idealFloat: 1207 return big.NewRat(1, 1).SetFloat64(float64(x)), nil 1208 case idealInt: 1209 return big.NewRat(1, 1).SetInt64(int64(x)), nil 1210 case idealRune: 1211 return big.NewRat(1, 1).SetInt64(int64(x)), nil 1212 case idealUint: 1213 return big.NewRat(1, 1).SetInt(big.NewInt(0).SetUint64(uint64(x))), nil 1214 //case complex64 1215 //case complex128 1216 case float32: 1217 return big.NewRat(1, 1).SetFloat64(float64(x)), nil 1218 case float64: 1219 return big.NewRat(1, 1).SetFloat64(x), nil 1220 case int8: 1221 return big.NewRat(1, 1).SetInt64(int64(x)), nil 1222 case int16: 1223 return big.NewRat(1, 1).SetInt64(int64(x)), nil 1224 case int32: 1225 return big.NewRat(1, 1).SetInt64(int64(x)), nil 1226 case int64: 1227 return big.NewRat(1, 1).SetInt64(x), nil 1228 case string: 1229 y := big.NewRat(1, 1) 1230 if _, ok := y.SetString(x); !ok { 1231 return invConv(val, typ) 1232 } 1233 1234 return y, nil 1235 case uint8: 1236 return big.NewRat(1, 1).SetInt64(int64(x)), nil 1237 case uint16: 1238 return big.NewRat(1, 1).SetInt64(int64(x)), nil 1239 case uint32: 1240 return big.NewRat(1, 1).SetInt64(int64(x)), nil 1241 case uint64: 1242 return big.NewRat(1, 1).SetInt(big.NewInt(0).SetUint64(x)), nil 1243 case *big.Int: 1244 return big.NewRat(1, 1).SetInt(x), nil 1245 case *big.Rat: 1246 return x, nil 1247 default: 1248 return invConv(val, typ) 1249 } 1250 case qDuration: 1251 switch x := val.(type) { 1252 // case blob 1253 // case bool 1254 //case idealComplex: 1255 case idealFloat: 1256 return time.Duration(x), nil 1257 case idealInt: 1258 return time.Duration(x), nil 1259 case idealRune: 1260 return time.Duration(x), nil 1261 case idealUint: 1262 return time.Duration(x), nil 1263 //case complex64 1264 //case complex128 1265 case float32: 1266 return time.Duration(x), nil 1267 case float64: 1268 return time.Duration(x), nil 1269 case int8: 1270 return time.Duration(x), nil 1271 case int16: 1272 return time.Duration(x), nil 1273 case int32: 1274 return time.Duration(x), nil 1275 case int64: 1276 return time.Duration(x), nil 1277 case string: 1278 return time.ParseDuration(x) 1279 case uint8: 1280 return time.Duration(x), nil 1281 case uint16: 1282 return time.Duration(x), nil 1283 case uint32: 1284 return time.Duration(x), nil 1285 case uint64: 1286 return time.Duration(x), nil 1287 case *big.Int: 1288 return time.Duration(x.Int64()), nil 1289 case *big.Rat: 1290 f, _ := x.Float64() 1291 return time.Duration(f), nil 1292 case time.Duration: 1293 return x, nil 1294 default: 1295 return invConv(val, typ) 1296 } 1297 case qTime: 1298 switch x := val.(type) { 1299 // case blob 1300 // case bool 1301 //case idealComplex: 1302 //case idealFloat: 1303 //case idealInt: 1304 //case idealRune: 1305 //case idealUint: 1306 //case complex64 1307 //case complex128 1308 //case float32: 1309 //case float64: 1310 //case int8: 1311 //case int16: 1312 //case int32: 1313 //case int64: 1314 //case string: 1315 //case uint8: 1316 //case uint16: 1317 //case uint32: 1318 //case uint64: 1319 //case *big.Int: 1320 //case *big.Rat: 1321 //case time.Duration: 1322 case time.Time: 1323 return x, nil 1324 default: 1325 return invConv(val, typ) 1326 } 1327 default: 1328 panic("internal error 006") 1329 } 1330 } 1331 1332 func invShiftRHS(lhs, rhs interface{}) (interface{}, error) { 1333 return nil, fmt.Errorf("invalid operation: %v << %v (shift count type %T, must be unsigned integer)", lhs, rhs, rhs) 1334 } 1335 1336 func invTruncInt(v interface{}) error { 1337 return fmt.Errorf("constant %v truncated to integer", v) 1338 } 1339 1340 func overflow(v interface{}, typ int) error { 1341 return fmt.Errorf("constant %v overflows %s", v, typeStr(typ)) 1342 } 1343 1344 func typeCheck1(val interface{}, c *col) (interface{}, error) { 1345 rec := []interface{}{val} 1346 c = c.clone() 1347 c.index = 0 1348 if err := typeCheck(rec, []*col{c}); err != nil { 1349 return nil, err 1350 } 1351 1352 return rec[0], nil 1353 } 1354 1355 func typeCheck(rec []interface{}, cols []*col) (err error) { 1356 for _, c := range cols { 1357 i := c.index 1358 if v := rec[i]; !c.typeCheck(v) { 1359 switch v.(type) { 1360 case idealComplex: 1361 y := complex128(v.(idealComplex)) 1362 switch c.typ { 1363 case qBool: 1364 case qComplex64: 1365 rec[i] = complex64(y) 1366 continue 1367 case qComplex128: 1368 rec[i] = complex128(y) 1369 continue 1370 case qFloat32, qFloat64, qInt8, qInt16, qInt32, qInt64, qUint8, qUint16, qUint32, qUint64: 1371 return fmt.Errorf("constant %v truncated to real", y) 1372 } 1373 case idealFloat: 1374 y := float64(v.(idealFloat)) 1375 switch c.typ { 1376 case qBool: 1377 case qComplex64: 1378 rec[i] = complex(float32(y), 0) 1379 continue 1380 case qComplex128: 1381 rec[i] = complex(float64(y), 0) 1382 continue 1383 case qFloat32: 1384 rec[i] = float32(y) 1385 continue 1386 case qFloat64: 1387 rec[i] = float64(y) 1388 continue 1389 case qInt8: 1390 if math.Floor(y) != y { 1391 return invTruncInt(y) 1392 } 1393 1394 if y < math.MinInt8 || y > math.MaxInt8 { 1395 return overflow(y, c.typ) 1396 } 1397 1398 rec[i] = int8(y) 1399 continue 1400 case qInt16: 1401 if math.Floor(y) != y { 1402 return invTruncInt(y) 1403 } 1404 1405 if y < math.MinInt16 || y > math.MaxInt16 { 1406 return overflow(y, c.typ) 1407 } 1408 1409 rec[i] = int16(y) 1410 continue 1411 case qInt32: 1412 if math.Floor(y) != y { 1413 return invTruncInt(y) 1414 } 1415 1416 if y < math.MinInt32 || y > math.MaxInt32 { 1417 return overflow(y, c.typ) 1418 } 1419 1420 rec[i] = int32(y) 1421 continue 1422 case qInt64: 1423 if math.Floor(y) != y { 1424 return invTruncInt(y) 1425 } 1426 1427 if y < math.MinInt64 || y > math.MaxInt64 { 1428 return overflow(y, c.typ) 1429 } 1430 1431 rec[i] = int64(y) 1432 continue 1433 case qString: 1434 case qUint8: 1435 if math.Floor(y) != y { 1436 return invTruncInt(y) 1437 } 1438 1439 if y < 0 || y > math.MaxUint8 { 1440 return overflow(y, c.typ) 1441 } 1442 1443 rec[i] = uint8(y) 1444 continue 1445 case qUint16: 1446 if math.Floor(y) != y { 1447 return invTruncInt(y) 1448 } 1449 1450 if y < 0 || y > math.MaxUint16 { 1451 return overflow(y, c.typ) 1452 } 1453 1454 rec[i] = uint16(y) 1455 continue 1456 case qUint32: 1457 if math.Floor(y) != y { 1458 return invTruncInt(y) 1459 } 1460 1461 if y < 0 || y > math.MaxUint32 { 1462 return overflow(y, c.typ) 1463 } 1464 1465 rec[i] = uint32(y) 1466 continue 1467 case qUint64: 1468 if math.Floor(y) != y { 1469 return invTruncInt(y) 1470 } 1471 1472 if y < 0 || y > math.MaxUint64 { 1473 return overflow(y, c.typ) 1474 } 1475 1476 rec[i] = uint64(y) 1477 continue 1478 case qBigInt: 1479 if math.Floor(y) != y { 1480 return invTruncInt(y) 1481 } 1482 1483 rr := big.NewRat(1, 1).SetFloat64(y) 1484 ii := big.NewInt(0) 1485 ii.Set(rr.Num()) 1486 ii.Quo(ii, rr.Denom()) 1487 rec[i] = ii 1488 continue 1489 case qBigRat: 1490 rec[i] = big.NewRat(1, 1).SetFloat64(y) 1491 continue 1492 } 1493 case idealInt: 1494 y := int64(v.(idealInt)) 1495 switch c.typ { 1496 case qBool: 1497 case qComplex64: 1498 rec[i] = complex(float32(y), 0) 1499 continue 1500 case qComplex128: 1501 rec[i] = complex(float64(y), 0) 1502 continue 1503 case qFloat32: 1504 rec[i] = float32(y) 1505 continue 1506 case qFloat64: 1507 rec[i] = float64(y) 1508 continue 1509 case qInt8: 1510 if y < math.MinInt8 || y > math.MaxInt8 { 1511 return overflow(y, c.typ) 1512 } 1513 1514 rec[i] = int8(y) 1515 continue 1516 case qInt16: 1517 if y < math.MinInt16 || y > math.MaxInt16 { 1518 return overflow(y, c.typ) 1519 } 1520 1521 rec[i] = int16(y) 1522 continue 1523 case qInt32: 1524 if y < math.MinInt32 || y > math.MaxInt32 { 1525 return overflow(y, c.typ) 1526 } 1527 1528 rec[i] = int32(y) 1529 continue 1530 case qInt64: 1531 if y < math.MinInt64 || y > math.MaxInt64 { 1532 return overflow(y, c.typ) 1533 } 1534 1535 rec[i] = int64(y) 1536 continue 1537 case qString: 1538 case qUint8: 1539 if y < 0 || y > math.MaxUint8 { 1540 return overflow(y, c.typ) 1541 } 1542 1543 rec[i] = uint8(y) 1544 continue 1545 case qUint16: 1546 if y < 0 || y > math.MaxUint16 { 1547 return overflow(y, c.typ) 1548 } 1549 1550 rec[i] = uint16(y) 1551 continue 1552 case qUint32: 1553 if y < 0 || y > math.MaxUint32 { 1554 return overflow(y, c.typ) 1555 } 1556 1557 rec[i] = uint32(y) 1558 continue 1559 case qUint64: 1560 if y < 0 { 1561 return overflow(y, c.typ) 1562 } 1563 1564 rec[i] = uint64(y) 1565 continue 1566 case qBigInt: 1567 rec[i] = big.NewInt(y) 1568 continue 1569 case qBigRat: 1570 rec[i] = big.NewRat(1, 1).SetInt64(y) 1571 continue 1572 } 1573 case idealRune: 1574 y := int64(v.(idealRune)) 1575 switch c.typ { 1576 case qBool: 1577 case qComplex64: 1578 rec[i] = complex(float32(y), 0) 1579 continue 1580 case qComplex128: 1581 rec[i] = complex(float64(y), 0) 1582 continue 1583 case qFloat32: 1584 rec[i] = float32(y) 1585 continue 1586 case qFloat64: 1587 rec[i] = float64(y) 1588 continue 1589 case qInt8: 1590 if y < math.MinInt8 || y > math.MaxInt8 { 1591 return overflow(y, c.typ) 1592 } 1593 1594 rec[i] = int8(y) 1595 continue 1596 case qInt16: 1597 if y < math.MinInt16 || y > math.MaxInt16 { 1598 return overflow(y, c.typ) 1599 } 1600 1601 rec[i] = int16(y) 1602 continue 1603 case qInt32: 1604 if y < math.MinInt32 || y > math.MaxInt32 { 1605 return overflow(y, c.typ) 1606 } 1607 1608 rec[i] = int32(y) 1609 continue 1610 case qInt64: 1611 if y < math.MinInt64 || y > math.MaxInt64 { 1612 return overflow(y, c.typ) 1613 } 1614 1615 rec[i] = int64(y) 1616 continue 1617 case qString: 1618 case qUint8: 1619 if y < 0 || y > math.MaxUint8 { 1620 return overflow(y, c.typ) 1621 } 1622 1623 rec[i] = uint8(y) 1624 continue 1625 case qUint16: 1626 if y < 0 || y > math.MaxUint16 { 1627 return overflow(y, c.typ) 1628 } 1629 1630 rec[i] = uint16(y) 1631 continue 1632 case qUint32: 1633 if y < 0 { 1634 return overflow(y, c.typ) 1635 } 1636 1637 rec[i] = uint32(y) 1638 continue 1639 case qUint64: 1640 if y < 0 { 1641 return overflow(y, c.typ) 1642 } 1643 1644 rec[i] = uint64(y) 1645 continue 1646 case qBigInt: 1647 rec[i] = big.NewInt(y) 1648 continue 1649 case qBigRat: 1650 rec[i] = big.NewRat(1, 1).SetInt64(y) 1651 continue 1652 } 1653 case idealUint: 1654 y := uint64(v.(idealUint)) 1655 switch c.typ { 1656 case qBool: 1657 case qComplex64: 1658 rec[i] = complex(float32(y), 0) 1659 continue 1660 case qComplex128: 1661 rec[i] = complex(float64(y), 0) 1662 continue 1663 case qFloat32: 1664 rec[i] = float32(y) 1665 continue 1666 case qFloat64: 1667 rec[i] = float64(y) 1668 continue 1669 case qInt8: 1670 if y > math.MaxInt8 { 1671 return overflow(y, c.typ) 1672 } 1673 1674 rec[i] = int8(y) 1675 continue 1676 case qInt16: 1677 if y > math.MaxInt16 { 1678 return overflow(y, c.typ) 1679 } 1680 1681 rec[i] = int16(y) 1682 continue 1683 case qInt32: 1684 if y > math.MaxInt32 { 1685 return overflow(y, c.typ) 1686 } 1687 1688 rec[i] = int32(y) 1689 continue 1690 case qInt64: 1691 if y > math.MaxInt64 { 1692 return overflow(y, c.typ) 1693 } 1694 1695 rec[i] = int64(y) 1696 continue 1697 case qString: 1698 rec[i] = string(y) 1699 continue 1700 case qUint8: 1701 if y > math.MaxUint8 { 1702 return overflow(y, c.typ) 1703 } 1704 1705 rec[i] = uint8(y) 1706 continue 1707 case qUint16: 1708 if y > math.MaxUint16 { 1709 return overflow(y, c.typ) 1710 } 1711 1712 rec[i] = uint16(y) 1713 continue 1714 case qUint32: 1715 if y > math.MaxUint32 { 1716 return overflow(y, c.typ) 1717 } 1718 1719 rec[i] = uint32(y) 1720 continue 1721 case qUint64: 1722 rec[i] = uint64(y) 1723 continue 1724 case qBigInt: 1725 rec[i] = big.NewInt(0).SetUint64(y) 1726 continue 1727 case qBigRat: 1728 ii := big.NewInt(0).SetUint64(y) 1729 rec[i] = big.NewRat(1, 1).SetInt(ii) 1730 continue 1731 } 1732 } 1733 return fmt.Errorf("cannot use %v (type %T) in assignment to, or comparison with, column %s (type %s)", v, ideal(v), c.name, typeStr(c.typ)) 1734 } 1735 } 1736 return 1737 } 1738 1739 //TODO collate1 should return errors instead of panicing 1740 func collate1(a, b interface{}) int { 1741 switch x := a.(type) { 1742 case nil: 1743 if b != nil { 1744 return -1 1745 } 1746 1747 return 0 1748 case bool: 1749 switch y := b.(type) { 1750 case nil: 1751 return 1 1752 case bool: 1753 if !x && y { 1754 return -1 1755 } 1756 1757 if x == y { 1758 return 0 1759 } 1760 1761 return 1 1762 default: 1763 // Make bool collate before anything except nil and 1764 // other bool for index seeking first non NULL value. 1765 return -1 1766 } 1767 case idealComplex: 1768 switch y := b.(type) { 1769 case nil: 1770 return 1 1771 case idealComplex: 1772 if x == y { 1773 return 0 1774 } 1775 1776 if real(x) < real(y) { 1777 return -1 1778 } 1779 1780 if real(x) > real(y) { 1781 return 1 1782 } 1783 1784 if imag(x) < imag(y) { 1785 return -1 1786 } 1787 1788 return 1 1789 case complex64: 1790 { 1791 x, y := complex64(x), complex64(y) 1792 if x == y { 1793 return 0 1794 } 1795 1796 if real(x) < real(y) { 1797 return -1 1798 } 1799 1800 if real(x) > real(y) { 1801 return 1 1802 } 1803 1804 if imag(x) < imag(y) { 1805 return -1 1806 } 1807 1808 return 1 1809 } 1810 case complex128: 1811 { 1812 x := complex128(x) 1813 if x == y { 1814 return 0 1815 } 1816 1817 if real(x) < real(y) { 1818 return -1 1819 } 1820 1821 if real(x) > real(y) { 1822 return 1 1823 } 1824 1825 if imag(x) < imag(y) { 1826 return -1 1827 } 1828 1829 return 1 1830 } 1831 default: 1832 panic("internal error 012") 1833 } 1834 case idealUint: 1835 switch y := b.(type) { 1836 case nil: 1837 return 1 1838 case idealUint: 1839 if x < y { 1840 return -1 1841 } 1842 1843 if x == y { 1844 return 0 1845 } 1846 1847 return 1 1848 case uint8: 1849 { 1850 x, y := uint64(x), uint64(y) 1851 if x < y { 1852 return -1 1853 } 1854 1855 if x == y { 1856 return 0 1857 } 1858 1859 return 1 1860 } 1861 case uint16: 1862 { 1863 x, y := uint64(x), uint64(y) 1864 if x < y { 1865 return -1 1866 } 1867 1868 if x == y { 1869 return 0 1870 } 1871 1872 return 1 1873 } 1874 case uint32: 1875 { 1876 x, y := uint64(x), uint64(y) 1877 if x < y { 1878 return -1 1879 } 1880 1881 if x == y { 1882 return 0 1883 } 1884 1885 return 1 1886 } 1887 case uint64: 1888 { 1889 x, y := uint64(x), uint64(y) 1890 if x < y { 1891 return -1 1892 } 1893 1894 if x == y { 1895 return 0 1896 } 1897 1898 return 1 1899 } 1900 case uint: 1901 { 1902 x, y := uint64(x), uint64(y) 1903 if x < y { 1904 return -1 1905 } 1906 1907 if x == y { 1908 return 0 1909 } 1910 1911 return 1 1912 } 1913 default: 1914 panic("internal error 013") 1915 } 1916 case idealRune: 1917 switch y := b.(type) { 1918 case nil: 1919 return 1 1920 case idealRune: 1921 if x < y { 1922 return -1 1923 } 1924 1925 if x == y { 1926 return 0 1927 } 1928 1929 return 1 1930 case int8: 1931 { 1932 x, y := int64(x), int64(y) 1933 if x < y { 1934 return -1 1935 } 1936 1937 if x == y { 1938 return 0 1939 } 1940 1941 return 1 1942 } 1943 case int16: 1944 { 1945 x, y := int64(x), int64(y) 1946 if x < y { 1947 return -1 1948 } 1949 1950 if x == y { 1951 return 0 1952 } 1953 1954 return 1 1955 } 1956 case int32: 1957 { 1958 x, y := int64(x), int64(y) 1959 if x < y { 1960 return -1 1961 } 1962 1963 if x == y { 1964 return 0 1965 } 1966 1967 return 1 1968 } 1969 case int64: 1970 { 1971 x, y := int64(x), int64(y) 1972 if x < y { 1973 return -1 1974 } 1975 1976 if x == y { 1977 return 0 1978 } 1979 1980 return 1 1981 } 1982 case int: 1983 { 1984 x, y := int64(x), int64(y) 1985 if x < y { 1986 return -1 1987 } 1988 1989 if x == y { 1990 return 0 1991 } 1992 1993 return 1 1994 } 1995 default: 1996 panic("internal error 014") 1997 } 1998 case idealInt: 1999 switch y := b.(type) { 2000 case nil: 2001 return 1 2002 case idealInt: 2003 if x < y { 2004 return -1 2005 } 2006 2007 if x == y { 2008 return 0 2009 } 2010 2011 return 1 2012 case int8: 2013 { 2014 x, y := int64(x), int64(y) 2015 if x < y { 2016 return -1 2017 } 2018 2019 if x == y { 2020 return 0 2021 } 2022 2023 return 1 2024 } 2025 case int16: 2026 { 2027 x, y := int64(x), int64(y) 2028 if x < y { 2029 return -1 2030 } 2031 2032 if x == y { 2033 return 0 2034 } 2035 2036 return 1 2037 } 2038 case int32: 2039 { 2040 x, y := int64(x), int64(y) 2041 if x < y { 2042 return -1 2043 } 2044 2045 if x == y { 2046 return 0 2047 } 2048 2049 return 1 2050 } 2051 case int64: 2052 { 2053 x, y := int64(x), int64(y) 2054 if x < y { 2055 return -1 2056 } 2057 2058 if x == y { 2059 return 0 2060 } 2061 2062 return 1 2063 } 2064 case int: 2065 { 2066 x, y := int64(x), int64(y) 2067 if x < y { 2068 return -1 2069 } 2070 2071 if x == y { 2072 return 0 2073 } 2074 2075 return 1 2076 } 2077 default: 2078 panic("internal error 015") 2079 } 2080 case idealFloat: 2081 switch y := b.(type) { 2082 case nil: 2083 return 1 2084 case idealFloat: 2085 if x < y { 2086 return -1 2087 } 2088 2089 if x == y { 2090 return 0 2091 } 2092 2093 return 1 2094 case float32: 2095 { 2096 x, y := float64(x), float64(y) 2097 if x < y { 2098 return -1 2099 } 2100 2101 if x == y { 2102 return 0 2103 } 2104 2105 return 1 2106 } 2107 case float64: 2108 { 2109 x, y := float64(x), float64(y) 2110 if x < y { 2111 return -1 2112 } 2113 2114 if x == y { 2115 return 0 2116 } 2117 2118 return 1 2119 } 2120 default: 2121 panic("internal error 016") 2122 } 2123 case complex64: 2124 switch y := b.(type) { 2125 case nil: 2126 return 1 2127 case complex64: 2128 if x == y { 2129 return 0 2130 } 2131 2132 if real(x) < real(y) { 2133 return -1 2134 } 2135 2136 if real(x) > real(y) { 2137 return 1 2138 } 2139 2140 if imag(x) < imag(y) { 2141 return -1 2142 } 2143 2144 return 1 2145 case idealComplex: 2146 { 2147 x, y := complex64(x), complex64(y) 2148 if x == y { 2149 return 0 2150 } 2151 2152 if real(x) < real(y) { 2153 return -1 2154 } 2155 2156 if real(x) > real(y) { 2157 return 1 2158 } 2159 2160 if imag(x) < imag(y) { 2161 return -1 2162 } 2163 2164 return 1 2165 } 2166 default: 2167 panic("internal error 017") 2168 } 2169 case complex128: 2170 switch y := b.(type) { 2171 case nil: 2172 return 1 2173 case complex128: 2174 if x == y { 2175 return 0 2176 } 2177 2178 if real(x) < real(y) { 2179 return -1 2180 } 2181 2182 if real(x) > real(y) { 2183 return 1 2184 } 2185 2186 if imag(x) < imag(y) { 2187 return -1 2188 } 2189 2190 return 1 2191 case idealComplex: 2192 { 2193 x, y := complex128(x), complex128(y) 2194 if x == y { 2195 return 0 2196 } 2197 2198 if real(x) < real(y) { 2199 return -1 2200 } 2201 2202 if real(x) > real(y) { 2203 return 1 2204 } 2205 2206 if imag(x) < imag(y) { 2207 return -1 2208 } 2209 2210 return 1 2211 } 2212 default: 2213 panic("internal error 018") 2214 } 2215 case float32: 2216 switch y := b.(type) { 2217 case nil: 2218 return 1 2219 case float32: 2220 if x < y { 2221 return -1 2222 } 2223 2224 if x == y { 2225 return 0 2226 } 2227 2228 return 1 2229 case idealFloat: 2230 { 2231 x, y := float32(x), float32(y) 2232 if x < y { 2233 return -1 2234 } 2235 2236 if x == y { 2237 return 0 2238 } 2239 2240 return 1 2241 } 2242 default: 2243 panic("internal error 019") 2244 } 2245 case float64: 2246 switch y := b.(type) { 2247 case nil: 2248 return 1 2249 case float64: 2250 if x < y { 2251 return -1 2252 } 2253 2254 if x == y { 2255 return 0 2256 } 2257 2258 return 1 2259 case idealFloat: 2260 { 2261 x, y := float64(x), float64(y) 2262 if x < y { 2263 return -1 2264 } 2265 2266 if x == y { 2267 return 0 2268 } 2269 2270 return 1 2271 } 2272 default: 2273 panic("internal error 020") 2274 } 2275 case int8: 2276 switch y := b.(type) { 2277 case nil: 2278 return 1 2279 case int8: 2280 if x < y { 2281 return -1 2282 } 2283 2284 if x == y { 2285 return 0 2286 } 2287 2288 return 1 2289 case idealInt: 2290 { 2291 x, y := int64(x), int64(y) 2292 if x < y { 2293 return -1 2294 } 2295 2296 if x == y { 2297 return 0 2298 } 2299 2300 return 1 2301 } 2302 default: 2303 panic("internal error 021") 2304 } 2305 case int16: 2306 switch y := b.(type) { 2307 case nil: 2308 return 1 2309 case int16: 2310 if x < y { 2311 return -1 2312 } 2313 2314 if x == y { 2315 return 0 2316 } 2317 2318 return 1 2319 case idealInt: 2320 { 2321 x, y := int64(x), int64(y) 2322 if x < y { 2323 return -1 2324 } 2325 2326 if x == y { 2327 return 0 2328 } 2329 2330 return 1 2331 } 2332 default: 2333 panic("internal error 022") 2334 } 2335 case int32: 2336 switch y := b.(type) { 2337 case nil: 2338 return 1 2339 case int32: 2340 if x < y { 2341 return -1 2342 } 2343 2344 if x == y { 2345 return 0 2346 } 2347 2348 return 1 2349 case idealInt: 2350 { 2351 x, y := int64(x), int64(y) 2352 if x < y { 2353 return -1 2354 } 2355 2356 if x == y { 2357 return 0 2358 } 2359 2360 return 1 2361 } 2362 default: 2363 panic("internal error 023") 2364 } 2365 case int64: 2366 switch y := b.(type) { 2367 case nil: 2368 return 1 2369 case int64: 2370 if x < y { 2371 return -1 2372 } 2373 2374 if x == y { 2375 return 0 2376 } 2377 2378 return 1 2379 case idealInt: 2380 { 2381 x, y := int64(x), int64(y) 2382 if x < y { 2383 return -1 2384 } 2385 2386 if x == y { 2387 return 0 2388 } 2389 2390 return 1 2391 } 2392 default: 2393 panic("internal error 024") 2394 } 2395 case uint8: 2396 switch y := b.(type) { 2397 case nil: 2398 return 1 2399 case uint8: 2400 if x < y { 2401 return -1 2402 } 2403 2404 if x == y { 2405 return 0 2406 } 2407 2408 return 1 2409 case idealInt: 2410 { 2411 x, y := uint64(x), uint64(y) 2412 if x < y { 2413 return -1 2414 } 2415 2416 if x == y { 2417 return 0 2418 } 2419 2420 return 1 2421 } 2422 case idealUint: 2423 { 2424 x, y := uint64(x), uint64(y) 2425 if x < y { 2426 return -1 2427 } 2428 2429 if x == y { 2430 return 0 2431 } 2432 2433 return 1 2434 } 2435 default: 2436 panic("internal error 025") 2437 } 2438 case uint16: 2439 switch y := b.(type) { 2440 case nil: 2441 return 1 2442 case uint16: 2443 if x < y { 2444 return -1 2445 } 2446 2447 if x == y { 2448 return 0 2449 } 2450 2451 return 1 2452 case idealInt: 2453 { 2454 x, y := uint64(x), uint64(y) 2455 if x < y { 2456 return -1 2457 } 2458 2459 if x == y { 2460 return 0 2461 } 2462 2463 return 1 2464 } 2465 case idealUint: 2466 { 2467 x, y := uint64(x), uint64(y) 2468 if x < y { 2469 return -1 2470 } 2471 2472 if x == y { 2473 return 0 2474 } 2475 2476 return 1 2477 } 2478 default: 2479 panic("internal error 026") 2480 } 2481 case uint32: 2482 switch y := b.(type) { 2483 case nil: 2484 return 1 2485 case uint32: 2486 if x < y { 2487 return -1 2488 } 2489 2490 if x == y { 2491 return 0 2492 } 2493 2494 return 1 2495 case idealInt: 2496 { 2497 x, y := uint64(x), uint64(y) 2498 if x < y { 2499 return -1 2500 } 2501 2502 if x == y { 2503 return 0 2504 } 2505 2506 return 1 2507 } 2508 case idealUint: 2509 { 2510 x, y := uint64(x), uint64(y) 2511 if x < y { 2512 return -1 2513 } 2514 2515 if x == y { 2516 return 0 2517 } 2518 2519 return 1 2520 } 2521 default: 2522 panic("internal error 027") 2523 } 2524 case uint64: 2525 switch y := b.(type) { 2526 case nil: 2527 return 1 2528 case uint64: 2529 if x < y { 2530 return -1 2531 } 2532 2533 if x == y { 2534 return 0 2535 } 2536 2537 return 1 2538 case idealInt: 2539 { 2540 x, y := uint64(x), uint64(y) 2541 if x < y { 2542 return -1 2543 } 2544 2545 if x == y { 2546 return 0 2547 } 2548 2549 return 1 2550 } 2551 case idealUint: 2552 { 2553 x, y := uint64(x), uint64(y) 2554 if x < y { 2555 return -1 2556 } 2557 2558 if x == y { 2559 return 0 2560 } 2561 2562 return 1 2563 } 2564 default: 2565 panic("internal error 028") 2566 } 2567 case string: 2568 switch y := b.(type) { 2569 case nil: 2570 return 1 2571 case string: 2572 if x < y { 2573 return -1 2574 } 2575 2576 if x == y { 2577 return 0 2578 } 2579 2580 return 1 2581 default: 2582 panic("internal error 029") 2583 } 2584 case []byte: 2585 switch y := b.(type) { 2586 case nil: 2587 return 1 2588 case []byte: 2589 return bytes.Compare(x, y) 2590 default: 2591 panic("internal error 030") 2592 } 2593 case *big.Int: 2594 switch y := b.(type) { 2595 case nil: 2596 return 1 2597 case *big.Int: 2598 return x.Cmp(y) 2599 case idealInt: 2600 { 2601 y := big.NewInt(int64(y)) 2602 return x.Cmp(y) 2603 } 2604 case idealUint: 2605 { 2606 u := big.NewInt(0) 2607 u.SetUint64(uint64(y)) 2608 return x.Cmp(u) 2609 } 2610 default: 2611 panic("internal error 031") 2612 } 2613 case *big.Rat: 2614 switch y := b.(type) { 2615 case nil: 2616 return 1 2617 case *big.Rat: 2618 return x.Cmp(y) 2619 case idealInt: 2620 { 2621 y := big.NewRat(int64(y), 1) 2622 return x.Cmp(y) 2623 } 2624 case idealUint: 2625 { 2626 u := big.NewInt(0) 2627 u.SetUint64(uint64(y)) 2628 var y big.Rat 2629 y.SetInt(u) 2630 return x.Cmp(&y) 2631 } 2632 default: 2633 panic("internal error 032") 2634 } 2635 case time.Time: 2636 switch y := b.(type) { 2637 case nil: 2638 return 1 2639 case time.Time: 2640 if x.Before(y) { 2641 return -1 2642 } 2643 2644 if x.Equal(y) { 2645 return 0 2646 } 2647 2648 return 1 2649 default: 2650 panic("internal error 033") 2651 } 2652 case time.Duration: 2653 switch y := b.(type) { 2654 case nil: 2655 return 1 2656 case time.Duration: 2657 if x < y { 2658 return -1 2659 } 2660 2661 if x == y { 2662 return 0 2663 } 2664 2665 return 1 2666 default: 2667 panic("internal error 034") 2668 } 2669 case chunk: 2670 switch y := b.(type) { 2671 case nil: 2672 return 1 2673 case chunk: 2674 a, err := x.expand() 2675 if err != nil { 2676 panic(err) 2677 } 2678 2679 b, err := y.expand() 2680 if err != nil { 2681 panic(err) 2682 } 2683 2684 return collate1(a, b) 2685 default: 2686 panic("internal error 035") 2687 } 2688 default: 2689 //dbg("%T(%v) %T(%v)", a, a, b, b) 2690 panic("internal error 036") 2691 } 2692 } 2693 2694 //TODO collate should return errors from collate1 2695 func collate(x, y []interface{}) (r int) { 2696 //defer func() { dbg("%v %v -> %v", x, y, r) }() 2697 nx, ny := len(x), len(y) 2698 2699 switch { 2700 case nx == 0 && ny != 0: 2701 return -1 2702 case nx == 0 && ny == 0: 2703 return 0 2704 case nx != 0 && ny == 0: 2705 return 1 2706 } 2707 2708 r = 1 2709 if nx > ny { 2710 x, y, r = y, x, -r 2711 } 2712 2713 for i, xi := range x { 2714 if c := collate1(xi, y[i]); c != 0 { 2715 return c * r 2716 } 2717 } 2718 2719 if nx == ny { 2720 return 0 2721 } 2722 2723 return -r 2724 } 2725 2726 var collators = map[bool]func(a, b []interface{}) int{false: collateDesc, true: collate} 2727 2728 func collateDesc(a, b []interface{}) int { 2729 return -collate(a, b) 2730 } 2731 2732 func isOrderedType(v interface{}) (y interface{}, r bool, err error) { 2733 //dbg("====") 2734 //dbg("%T(%v)", v, v) 2735 //defer func() { dbg("%T(%v)", y, y) }() 2736 switch x := v.(type) { 2737 case idealFloat, idealInt, idealRune, idealUint, 2738 float32, float64, 2739 int8, int16, int32, int64, 2740 uint8, uint16, uint32, uint64, 2741 string: 2742 return v, true, nil 2743 case *big.Int, *big.Rat, time.Time, time.Duration: 2744 return x, true, nil 2745 case chunk: 2746 if y, err = x.expand(); err != nil { 2747 return 2748 } 2749 2750 return isOrderedType(y) 2751 } 2752 2753 return v, false, nil 2754 } 2755 2756 var isSystemName = map[string]bool{ 2757 "__Column": true, 2758 "__Column2": true, 2759 "__Index": true, 2760 "__Index2": true, 2761 "__Index2_Column": true, 2762 "__Index2_Expr": true, 2763 "__Table": true, 2764 } 2765 2766 func qnames(l []string) []string { 2767 r := make([]string, len(l)) 2768 for i, v := range l { 2769 r[i] = fmt.Sprintf("%q", v) 2770 } 2771 return r 2772 }