1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192
|
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package schema
import (
"fmt"
"math"
"github.com/apache/arrow-go/v18/internal/json"
"github.com/apache/arrow-go/v18/parquet"
"github.com/apache/arrow-go/v18/parquet/internal/debug"
format "github.com/apache/arrow-go/v18/parquet/internal/gen-go/parquet"
)
// DecimalMetadata is a struct for managing scale and precision information between
// converted and logical types.
type DecimalMetadata struct {
IsSet bool
Scale int32
Precision int32
}
func getLogicalType(l *format.LogicalType) LogicalType {
switch {
case l.IsSetSTRING():
return StringLogicalType{}
case l.IsSetMAP():
return MapLogicalType{}
case l.IsSetLIST():
return ListLogicalType{}
case l.IsSetENUM():
return EnumLogicalType{}
case l.IsSetDECIMAL():
return DecimalLogicalType{typ: l.DECIMAL}
case l.IsSetDATE():
return DateLogicalType{}
case l.IsSetTIME():
if timeUnitFromThrift(l.TIME.Unit) == TimeUnitUnknown {
panic("parquet: TimeUnit must be one of MILLIS, MICROS, or NANOS for Time logical type")
}
return TimeLogicalType{typ: l.TIME}
case l.IsSetTIMESTAMP():
if timeUnitFromThrift(l.TIMESTAMP.Unit) == TimeUnitUnknown {
panic("parquet: TimeUnit must be one of MILLIS, MICROS, or NANOS for Timestamp logical type")
}
return TimestampLogicalType{typ: l.TIMESTAMP}
case l.IsSetINTEGER():
return IntLogicalType{typ: l.INTEGER}
case l.IsSetUNKNOWN():
return NullLogicalType{}
case l.IsSetJSON():
return JSONLogicalType{}
case l.IsSetBSON():
return BSONLogicalType{}
case l.IsSetUUID():
return UUIDLogicalType{}
case l.IsSetFLOAT16():
return Float16LogicalType{}
case l == nil:
return NoLogicalType{}
default:
panic("invalid logical type")
}
}
// TimeUnitType is an enum for denoting whether a time based logical type
// is using milliseconds, microseconds or nanoseconds.
type TimeUnitType int
// Constants for the TimeUnitType
const (
TimeUnitMillis TimeUnitType = iota
TimeUnitMicros
TimeUnitNanos
TimeUnitUnknown
)
// LogicalType is the descriptor that defines the usage of a physical primitive
// type in the schema, such as an Interval, Date, etc.
type LogicalType interface {
// Returns true if a nested type like List or Map
IsNested() bool
// Returns true if this type can be serialized, ie: not Unknown/NoType/Interval
IsSerialized() bool
// Returns true if not NoLogicalType
IsValid() bool
// Returns true if it is NoType
IsNone() bool
// returns a string representation of the Logical Type
String() string
toThrift() *format.LogicalType
// Return the equivalent ConvertedType for legacy Parquet systems
ToConvertedType() (ConvertedType, DecimalMetadata)
// Returns true if the specified ConvertedType is compatible with this
// logical type
IsCompatible(ConvertedType, DecimalMetadata) bool
// Returns true if this logical type can be used with the provided physical type
IsApplicable(t parquet.Type, tlen int32) bool
// Returns true if the logical types are the same
Equals(LogicalType) bool
// Returns the default stat sort order for this logical type
SortOrder() SortOrder
}
// TemporalLogicalType is a smaller interface for Time based logical types
// like Time / Timestamp
type TemporalLogicalType interface {
LogicalType
IsAdjustedToUTC() bool
TimeUnit() TimeUnitType
}
// SortOrder mirrors the parquet.thrift sort order type
type SortOrder int8
// Constants for the Stat sort order definitions
const (
SortSIGNED SortOrder = iota
SortUNSIGNED
SortUNKNOWN
)
// DefaultSortOrder returns the default stat sort order for the given physical type
func DefaultSortOrder(primitive format.Type) SortOrder {
switch primitive {
case format.Type_BOOLEAN, format.Type_INT32, format.Type_INT64, format.Type_FLOAT, format.Type_DOUBLE:
return SortSIGNED
case format.Type_BYTE_ARRAY, format.Type_FIXED_LEN_BYTE_ARRAY:
return SortUNSIGNED
case format.Type_INT96:
fallthrough
default:
return SortUNKNOWN
}
}
// GetLogicalSortOrder returns the default sort order for this logical type
// or falls back to the default sort order for the physical type if not valid
func GetLogicalSortOrder(logical LogicalType, primitive format.Type) SortOrder {
switch {
case logical == nil || !logical.IsValid():
return SortUNKNOWN
case logical.Equals(NoLogicalType{}):
return DefaultSortOrder(primitive)
default:
return logical.SortOrder()
}
}
type baseLogicalType struct{}
func (baseLogicalType) IsSerialized() bool {
return true
}
func (baseLogicalType) IsValid() bool {
return true
}
func (baseLogicalType) IsNested() bool {
return false
}
func (baseLogicalType) IsNone() bool { return false }
// StringLogicalType is a UTF8 string, only usable with ByteArray and FixedLenByteArray
type StringLogicalType struct{ baseLogicalType }
func (StringLogicalType) SortOrder() SortOrder {
return SortUNSIGNED
}
func (StringLogicalType) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]string{"Type": StringLogicalType{}.String()})
}
func (StringLogicalType) String() string {
return "String"
}
func (StringLogicalType) ToConvertedType() (ConvertedType, DecimalMetadata) {
return ConvertedTypes.UTF8, DecimalMetadata{}
}
func (StringLogicalType) IsCompatible(t ConvertedType, dec DecimalMetadata) bool {
return t == ConvertedTypes.UTF8 && !dec.IsSet
}
func (StringLogicalType) IsApplicable(t parquet.Type, _ int32) bool {
return t == parquet.Types.ByteArray
}
func (StringLogicalType) toThrift() *format.LogicalType {
return &format.LogicalType{STRING: format.NewStringType()}
}
func (StringLogicalType) Equals(rhs LogicalType) bool {
_, ok := rhs.(StringLogicalType)
return ok
}
// MapLogicalType represents a mapped type
type MapLogicalType struct{ baseLogicalType }
func (MapLogicalType) SortOrder() SortOrder {
return SortUNKNOWN
}
func (MapLogicalType) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]string{"Type": MapLogicalType{}.String()})
}
func (MapLogicalType) String() string {
return "Map"
}
func (MapLogicalType) IsNested() bool {
return true
}
func (MapLogicalType) ToConvertedType() (ConvertedType, DecimalMetadata) {
return ConvertedTypes.Map, DecimalMetadata{}
}
func (MapLogicalType) IsCompatible(t ConvertedType, dec DecimalMetadata) bool {
return (t == ConvertedTypes.Map || t == ConvertedTypes.MapKeyValue) && !dec.IsSet
}
func (MapLogicalType) IsApplicable(parquet.Type, int32) bool {
return false
}
func (MapLogicalType) toThrift() *format.LogicalType {
return &format.LogicalType{MAP: format.NewMapType()}
}
func (MapLogicalType) Equals(rhs LogicalType) bool {
_, ok := rhs.(MapLogicalType)
return ok
}
func NewListLogicalType() LogicalType {
return ListLogicalType{}
}
// ListLogicalType is used for columns which are themselves nested lists
type ListLogicalType struct{ baseLogicalType }
func (ListLogicalType) SortOrder() SortOrder {
return SortUNKNOWN
}
func (ListLogicalType) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]string{"Type": ListLogicalType{}.String()})
}
func (ListLogicalType) String() string {
return "List"
}
func (ListLogicalType) IsNested() bool {
return true
}
func (ListLogicalType) ToConvertedType() (ConvertedType, DecimalMetadata) {
return ConvertedTypes.List, DecimalMetadata{}
}
func (ListLogicalType) IsCompatible(t ConvertedType, dec DecimalMetadata) bool {
return t == ConvertedTypes.List && !dec.IsSet
}
func (ListLogicalType) IsApplicable(parquet.Type, int32) bool {
return false
}
func (ListLogicalType) toThrift() *format.LogicalType {
return &format.LogicalType{LIST: format.NewListType()}
}
func (ListLogicalType) Equals(rhs LogicalType) bool {
_, ok := rhs.(ListLogicalType)
return ok
}
// EnumLogicalType is for representing an enum, which should be a byte array type
type EnumLogicalType struct{ baseLogicalType }
func (EnumLogicalType) SortOrder() SortOrder {
return SortUNSIGNED
}
func (EnumLogicalType) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]string{"Type": EnumLogicalType{}.String()})
}
func (EnumLogicalType) String() string {
return "Enum"
}
func (EnumLogicalType) ToConvertedType() (ConvertedType, DecimalMetadata) {
return ConvertedTypes.Enum, DecimalMetadata{}
}
func (EnumLogicalType) IsCompatible(t ConvertedType, dec DecimalMetadata) bool {
return t == ConvertedTypes.Enum && !dec.IsSet
}
func (EnumLogicalType) IsApplicable(t parquet.Type, _ int32) bool {
return t == parquet.Types.ByteArray
}
func (EnumLogicalType) toThrift() *format.LogicalType {
return &format.LogicalType{ENUM: format.NewEnumType()}
}
func (EnumLogicalType) Equals(rhs LogicalType) bool {
_, ok := rhs.(EnumLogicalType)
return ok
}
// NewDecimalLogicalType returns a Decimal logical type with the given
// precision and scale.
//
// Panics if precision < 1 or scale is not in the range (0, precision)
func NewDecimalLogicalType(precision int32, scale int32) LogicalType {
if precision < 1 {
panic("parquet: precision must be greater than or equal to 1 for decimal logical type")
}
if scale < 0 || scale > precision {
panic("parquet: scale must be a non-negative integer that does not exceed precision for decimal logical type")
}
return DecimalLogicalType{typ: &format.DecimalType{Precision: precision, Scale: scale}}
}
// DecimalLogicalType is used to represent a decimal value of a given
// precision and scale
type DecimalLogicalType struct {
baseLogicalType
typ *format.DecimalType
}
func (t DecimalLogicalType) Precision() int32 {
return t.typ.Precision
}
func (t DecimalLogicalType) Scale() int32 {
return t.typ.Scale
}
func (DecimalLogicalType) SortOrder() SortOrder {
return SortSIGNED
}
func (t DecimalLogicalType) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{"Type": "Decimal", "precision": t.typ.Precision, "scale": t.typ.Scale})
}
func (t DecimalLogicalType) String() string {
return fmt.Sprintf("Decimal(precision=%d, scale=%d)", t.typ.Precision, t.typ.Scale)
}
func (t DecimalLogicalType) ToConvertedType() (ConvertedType, DecimalMetadata) {
return ConvertedTypes.Decimal, DecimalMetadata{IsSet: true, Scale: t.typ.GetScale(), Precision: t.typ.GetPrecision()}
}
func (t DecimalLogicalType) IsCompatible(c ConvertedType, dec DecimalMetadata) bool {
return c == ConvertedTypes.Decimal &&
dec.IsSet && dec.Scale == t.typ.Scale && dec.Precision == t.typ.Precision
}
func (t DecimalLogicalType) IsApplicable(typ parquet.Type, tlen int32) bool {
switch typ {
case parquet.Types.Int32:
return 1 <= t.typ.Precision && t.typ.Precision <= 9
case parquet.Types.Int64:
if t.typ.Precision < 10 {
debug.Log("int64 used for decimal logical, precision is small enough to use int32")
}
return 1 <= t.typ.Precision && t.typ.Precision <= 18
case parquet.Types.FixedLenByteArray:
return t.typ.Precision <= int32(math.Floor(math.Log10(math.Pow(2.0, (8.0*float64(tlen)-1.0)))))
case parquet.Types.ByteArray:
return true
}
return false
}
func (t DecimalLogicalType) toThrift() *format.LogicalType {
return &format.LogicalType{DECIMAL: t.typ}
}
func (t DecimalLogicalType) Equals(rhs LogicalType) bool {
other, ok := rhs.(DecimalLogicalType)
if !ok {
return false
}
return t.typ.Precision == other.typ.Precision && t.typ.Scale == other.typ.Scale
}
// DateLogicalType is an int32 representing the number of days since the Unix Epoch
// 1 January 1970
type DateLogicalType struct{ baseLogicalType }
func (DateLogicalType) SortOrder() SortOrder {
return SortSIGNED
}
func (DateLogicalType) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]string{"Type": DateLogicalType{}.String()})
}
func (DateLogicalType) String() string {
return "Date"
}
func (DateLogicalType) ToConvertedType() (ConvertedType, DecimalMetadata) {
return ConvertedTypes.Date, DecimalMetadata{}
}
func (DateLogicalType) IsCompatible(t ConvertedType, dec DecimalMetadata) bool {
return t == ConvertedTypes.Date && !dec.IsSet
}
func (DateLogicalType) IsApplicable(t parquet.Type, _ int32) bool {
return t == parquet.Types.Int32
}
func (DateLogicalType) toThrift() *format.LogicalType {
return &format.LogicalType{DATE: format.NewDateType()}
}
func (DateLogicalType) Equals(rhs LogicalType) bool {
_, ok := rhs.(DateLogicalType)
return ok
}
func timeUnitFromThrift(unit *format.TimeUnit) TimeUnitType {
switch {
case unit == nil:
return TimeUnitUnknown
case unit.IsSetMILLIS():
return TimeUnitMillis
case unit.IsSetMICROS():
return TimeUnitMicros
case unit.IsSetNANOS():
return TimeUnitNanos
default:
return TimeUnitUnknown
}
}
func timeUnitToString(unit *format.TimeUnit) string {
switch {
case unit == nil:
return "unknown"
case unit.IsSetMILLIS():
return "milliseconds"
case unit.IsSetMICROS():
return "microseconds"
case unit.IsSetNANOS():
return "nanoseconds"
default:
return "unknown"
}
}
func timeUnitFromString(v string) TimeUnitType {
switch v {
case "millis":
return TimeUnitMillis
case "micros":
return TimeUnitMicros
case "nanos":
return TimeUnitNanos
default:
return TimeUnitUnknown
}
}
func createTimeUnit(unit TimeUnitType) *format.TimeUnit {
tunit := format.NewTimeUnit()
switch unit {
case TimeUnitMicros:
tunit.MICROS = format.NewMicroSeconds()
case TimeUnitMillis:
tunit.MILLIS = format.NewMilliSeconds()
case TimeUnitNanos:
tunit.NANOS = format.NewNanoSeconds()
default:
panic("parquet: time unit must be one of MILLIS, MICROS, or NANOS for Time logical type")
}
return tunit
}
// NewTimeLogicalType returns a time type of the given unit.
func NewTimeLogicalType(isAdjustedToUTC bool, unit TimeUnitType) LogicalType {
return TimeLogicalType{typ: &format.TimeType{
IsAdjustedToUTC: isAdjustedToUTC,
Unit: createTimeUnit(unit),
}}
}
// TimeLogicalType is a time type without a date and must be an
// int32 for milliseconds, or an int64 for micro or nano seconds.
type TimeLogicalType struct {
baseLogicalType
typ *format.TimeType
}
func (t TimeLogicalType) IsAdjustedToUTC() bool {
return t.typ.IsAdjustedToUTC
}
func (t TimeLogicalType) TimeUnit() TimeUnitType {
return timeUnitFromThrift(t.typ.Unit)
}
func (TimeLogicalType) SortOrder() SortOrder {
return SortSIGNED
}
func (t TimeLogicalType) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
"Type": "Time", "isAdjustedToUTC": t.typ.IsAdjustedToUTC, "timeUnit": timeUnitToString(t.typ.GetUnit())})
}
func (t TimeLogicalType) String() string {
return fmt.Sprintf("Time(isAdjustedToUTC=%t, timeUnit=%s)", t.typ.GetIsAdjustedToUTC(), timeUnitToString(t.typ.GetUnit()))
}
func (t TimeLogicalType) ToConvertedType() (ConvertedType, DecimalMetadata) {
unit := timeUnitFromThrift(t.typ.Unit)
if t.typ.IsAdjustedToUTC {
switch unit {
case TimeUnitMillis:
return ConvertedTypes.TimeMillis, DecimalMetadata{}
case TimeUnitMicros:
return ConvertedTypes.TimeMicros, DecimalMetadata{}
}
}
return ConvertedTypes.None, DecimalMetadata{}
}
func (t TimeLogicalType) IsCompatible(c ConvertedType, dec DecimalMetadata) bool {
if dec.IsSet {
return false
}
unit := timeUnitFromThrift(t.typ.Unit)
if t.typ.IsAdjustedToUTC {
switch unit {
case TimeUnitMillis:
return c == ConvertedTypes.TimeMillis
case TimeUnitMicros:
return c == ConvertedTypes.TimeMicros
}
}
return c == ConvertedTypes.None || c == ConvertedTypes.NA
}
func (t TimeLogicalType) IsApplicable(typ parquet.Type, _ int32) bool {
return (typ == parquet.Types.Int32 && t.typ.GetUnit().IsSetMILLIS()) ||
(typ == parquet.Types.Int64 &&
(t.typ.GetUnit().IsSetMICROS() || t.typ.GetUnit().IsSetNANOS()))
}
func (t TimeLogicalType) toThrift() *format.LogicalType {
return &format.LogicalType{TIME: t.typ}
}
func (t TimeLogicalType) Equals(rhs LogicalType) bool {
other, ok := rhs.(TimeLogicalType)
if !ok {
return false
}
return t.typ.IsAdjustedToUTC == other.typ.IsAdjustedToUTC &&
timeUnitFromThrift(t.typ.Unit) == timeUnitFromThrift(other.typ.Unit)
}
// NewTimestampLogicalType returns a logical timestamp type with "forceConverted"
// set to false
func NewTimestampLogicalType(isAdjustedToUTC bool, unit TimeUnitType) LogicalType {
return TimestampLogicalType{
typ: &format.TimestampType{
IsAdjustedToUTC: isAdjustedToUTC,
Unit: createTimeUnit(unit),
},
forceConverted: false,
fromConverted: false,
}
}
// NewTimestampLogicalTypeForce returns a timestamp logical type with
// "forceConverted" set to true
func NewTimestampLogicalTypeForce(isAdjustedToUTC bool, unit TimeUnitType) LogicalType {
return TimestampLogicalType{
typ: &format.TimestampType{
IsAdjustedToUTC: isAdjustedToUTC,
Unit: createTimeUnit(unit),
},
forceConverted: true,
fromConverted: false,
}
}
// TimestampOpt options used with New Timestamp Logical Type
type TimestampOpt func(*TimestampLogicalType)
// WithTSIsAdjustedToUTC sets the IsAdjustedToUTC field of the timestamp type.
func WithTSIsAdjustedToUTC() TimestampOpt {
return func(t *TimestampLogicalType) {
t.typ.IsAdjustedToUTC = true
}
}
// WithTSTimeUnitType sets the time unit for the timestamp type
func WithTSTimeUnitType(unit TimeUnitType) TimestampOpt {
return func(t *TimestampLogicalType) {
t.typ.Unit = createTimeUnit(unit)
}
}
// WithTSForceConverted enable force converted mode
func WithTSForceConverted() TimestampOpt {
return func(t *TimestampLogicalType) {
t.forceConverted = true
}
}
// WithTSFromConverted enable the timestamp logical type to be
// constructed from a converted type.
func WithTSFromConverted() TimestampOpt {
return func(t *TimestampLogicalType) {
t.fromConverted = true
}
}
// NewTimestampLogicalTypeWithOpts creates a new TimestampLogicalType with the provided options.
//
// TimestampType Unit defaults to milliseconds (TimeUnitMillis)
func NewTimestampLogicalTypeWithOpts(opts ...TimestampOpt) LogicalType {
ts := TimestampLogicalType{
typ: &format.TimestampType{
Unit: createTimeUnit(TimeUnitMillis), // default to milliseconds
},
}
for _, o := range opts {
o(&ts)
}
return ts
}
// TimestampLogicalType represents an int64 number that can be decoded
// into a year, month, day, hour, minute, second, and subsecond
type TimestampLogicalType struct {
baseLogicalType
typ *format.TimestampType
// forceConverted denotes whether or not the resulting serialized
// type when writing to parquet will be written as the legacy
// ConvertedType TIMESTAMP_MICROS/TIMESTAMP_MILLIS (true)
// or if it will write the proper current Logical Types (false, default)
forceConverted bool
// fromConverted denotes if the timestamp type was created by
// translating a legacy converted type of TIMESTAMP_MILLIS or
// TIMESTAMP_MICROS rather than by using the current logical
// types. Default is false.
fromConverted bool
}
func (t TimestampLogicalType) IsFromConvertedType() bool {
return t.fromConverted
}
func (t TimestampLogicalType) IsAdjustedToUTC() bool {
return t.typ.IsAdjustedToUTC
}
func (t TimestampLogicalType) TimeUnit() TimeUnitType {
return timeUnitFromThrift(t.typ.Unit)
}
func (TimestampLogicalType) SortOrder() SortOrder {
return SortSIGNED
}
func (t TimestampLogicalType) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
"Type": "Timestamp",
"isAdjustedToUTC": t.typ.IsAdjustedToUTC,
"timeUnit": timeUnitToString(t.typ.GetUnit()),
"is_from_converted_type": t.fromConverted,
"force_set_converted_type": t.forceConverted,
})
}
func (t TimestampLogicalType) IsSerialized() bool {
return !t.fromConverted
}
func (t TimestampLogicalType) String() string {
return fmt.Sprintf("Timestamp(isAdjustedToUTC=%t, timeUnit=%s, is_from_converted_type=%t, force_set_converted_type=%t)",
t.typ.GetIsAdjustedToUTC(), timeUnitToString(t.typ.GetUnit()), t.fromConverted, t.forceConverted)
}
func (t TimestampLogicalType) ToConvertedType() (ConvertedType, DecimalMetadata) {
unit := timeUnitFromThrift(t.typ.Unit)
if t.typ.IsAdjustedToUTC || t.forceConverted {
switch unit {
case TimeUnitMillis:
return ConvertedTypes.TimestampMillis, DecimalMetadata{}
case TimeUnitMicros:
return ConvertedTypes.TimestampMicros, DecimalMetadata{}
}
}
return ConvertedTypes.None, DecimalMetadata{}
}
func (t TimestampLogicalType) IsCompatible(c ConvertedType, dec DecimalMetadata) bool {
if dec.IsSet {
return false
}
switch timeUnitFromThrift(t.typ.Unit) {
case TimeUnitMillis:
if t.typ.GetIsAdjustedToUTC() || t.forceConverted {
return c == ConvertedTypes.TimestampMillis
}
case TimeUnitMicros:
if t.typ.GetIsAdjustedToUTC() || t.forceConverted {
return c == ConvertedTypes.TimestampMicros
}
}
return c == ConvertedTypes.None || c == ConvertedTypes.NA
}
func (TimestampLogicalType) IsApplicable(t parquet.Type, _ int32) bool {
return t == parquet.Types.Int64
}
func (t TimestampLogicalType) toThrift() *format.LogicalType {
return &format.LogicalType{TIMESTAMP: t.typ}
}
func (t TimestampLogicalType) Equals(rhs LogicalType) bool {
other, ok := rhs.(TimestampLogicalType)
if !ok {
return false
}
return t.typ.IsAdjustedToUTC == other.typ.IsAdjustedToUTC &&
timeUnitFromThrift(t.typ.Unit) == timeUnitFromThrift(other.typ.Unit)
}
// NewIntLogicalType creates an integer logical type of the desired bitwidth
// and whether it is signed or not.
//
// Bit width must be exactly 8, 16, 32 or 64 for an integer logical type
func NewIntLogicalType(bitWidth int8, signed bool) LogicalType {
switch bitWidth {
case 8, 16, 32, 64:
default:
panic("parquet: bit width must be exactly 8, 16, 32, or 64 for Int logical type")
}
return IntLogicalType{
typ: &format.IntType{
BitWidth: bitWidth,
IsSigned: signed,
},
}
}
// IntLogicalType represents an integer type of a specific bit width and
// is either signed or unsigned.
type IntLogicalType struct {
baseLogicalType
typ *format.IntType
}
func (t IntLogicalType) BitWidth() int8 {
return t.typ.BitWidth
}
func (t IntLogicalType) IsSigned() bool {
return t.typ.IsSigned
}
func (t IntLogicalType) SortOrder() SortOrder {
if t.typ.IsSigned {
return SortSIGNED
}
return SortUNSIGNED
}
func (t IntLogicalType) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
"Type": "Int", "bitWidth": t.typ.BitWidth, "isSigned": t.typ.IsSigned,
})
}
func (t IntLogicalType) String() string {
return fmt.Sprintf("Int(bitWidth=%d, isSigned=%t)", t.typ.GetBitWidth(), t.typ.GetIsSigned())
}
func (t IntLogicalType) ToConvertedType() (ConvertedType, DecimalMetadata) {
var d DecimalMetadata
if t.typ.IsSigned {
switch t.typ.BitWidth {
case 8:
return ConvertedTypes.Int8, d
case 16:
return ConvertedTypes.Int16, d
case 32:
return ConvertedTypes.Int32, d
case 64:
return ConvertedTypes.Int64, d
}
} else {
switch t.typ.BitWidth {
case 8:
return ConvertedTypes.Uint8, d
case 16:
return ConvertedTypes.Uint16, d
case 32:
return ConvertedTypes.Uint32, d
case 64:
return ConvertedTypes.Uint64, d
}
}
return ConvertedTypes.None, d
}
func (t IntLogicalType) IsCompatible(c ConvertedType, dec DecimalMetadata) bool {
if dec.IsSet {
return false
}
v, _ := t.ToConvertedType()
return c == v
}
func (t IntLogicalType) IsApplicable(typ parquet.Type, _ int32) bool {
return (typ == parquet.Types.Int32 && t.typ.GetBitWidth() <= 32) ||
(typ == parquet.Types.Int64 && t.typ.GetBitWidth() == 64)
}
func (t IntLogicalType) toThrift() *format.LogicalType {
return &format.LogicalType{INTEGER: t.typ}
}
func (t IntLogicalType) Equals(rhs LogicalType) bool {
other, ok := rhs.(IntLogicalType)
if !ok {
return false
}
return t.typ.GetIsSigned() == other.typ.GetIsSigned() &&
t.typ.GetBitWidth() == other.typ.GetBitWidth()
}
// UnknownLogicalType is a type that is essentially a placeholder for when
// we don't know the type.
type UnknownLogicalType struct{ baseLogicalType }
func (UnknownLogicalType) SortOrder() SortOrder {
return SortUNKNOWN
}
func (UnknownLogicalType) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]string{"Type": UnknownLogicalType{}.String()})
}
func (UnknownLogicalType) IsValid() bool { return false }
func (UnknownLogicalType) IsSerialized() bool { return false }
func (UnknownLogicalType) String() string {
return "Unknown"
}
func (UnknownLogicalType) ToConvertedType() (ConvertedType, DecimalMetadata) {
return ConvertedTypes.NA, DecimalMetadata{}
}
func (UnknownLogicalType) IsCompatible(c ConvertedType, dec DecimalMetadata) bool {
return c == ConvertedTypes.NA && !dec.IsSet
}
func (UnknownLogicalType) IsApplicable(parquet.Type, int32) bool { return true }
func (UnknownLogicalType) toThrift() *format.LogicalType {
return &format.LogicalType{UNKNOWN: format.NewNullType()}
}
func (UnknownLogicalType) Equals(rhs LogicalType) bool {
_, ok := rhs.(UnknownLogicalType)
return ok
}
// JSONLogicalType represents a byte array column which is to be interpreted
// as a JSON string.
type JSONLogicalType struct{ baseLogicalType }
func (JSONLogicalType) SortOrder() SortOrder {
return SortUNSIGNED
}
func (JSONLogicalType) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]string{"Type": JSONLogicalType{}.String()})
}
func (JSONLogicalType) String() string {
return "JSON"
}
func (JSONLogicalType) ToConvertedType() (ConvertedType, DecimalMetadata) {
return ConvertedTypes.JSON, DecimalMetadata{}
}
func (JSONLogicalType) IsCompatible(c ConvertedType, dec DecimalMetadata) bool {
return c == ConvertedTypes.JSON && !dec.IsSet
}
func (JSONLogicalType) IsApplicable(t parquet.Type, _ int32) bool {
return t == parquet.Types.ByteArray
}
func (JSONLogicalType) toThrift() *format.LogicalType {
return &format.LogicalType{JSON: format.NewJsonType()}
}
func (JSONLogicalType) Equals(rhs LogicalType) bool {
_, ok := rhs.(JSONLogicalType)
return ok
}
// BSONLogicalType represents a binary JSON string in the byte array
type BSONLogicalType struct{ baseLogicalType }
func (BSONLogicalType) SortOrder() SortOrder {
return SortUNSIGNED
}
func (BSONLogicalType) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]string{"Type": BSONLogicalType{}.String()})
}
func (BSONLogicalType) String() string {
return "BSON"
}
func (BSONLogicalType) ToConvertedType() (ConvertedType, DecimalMetadata) {
return ConvertedTypes.BSON, DecimalMetadata{}
}
func (BSONLogicalType) IsCompatible(c ConvertedType, dec DecimalMetadata) bool {
return c == ConvertedTypes.BSON && !dec.IsSet
}
func (BSONLogicalType) IsApplicable(t parquet.Type, _ int32) bool {
return t == parquet.Types.ByteArray
}
func (BSONLogicalType) toThrift() *format.LogicalType {
return &format.LogicalType{BSON: format.NewBsonType()}
}
func (BSONLogicalType) Equals(rhs LogicalType) bool {
_, ok := rhs.(BSONLogicalType)
return ok
}
// UUIDLogicalType can only be used with a FixedLength byte array column
// that is exactly 16 bytes long
type UUIDLogicalType struct{ baseLogicalType }
func (UUIDLogicalType) SortOrder() SortOrder {
return SortUNSIGNED
}
func (UUIDLogicalType) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]string{"Type": UUIDLogicalType{}.String()})
}
func (UUIDLogicalType) String() string {
return "UUID"
}
func (UUIDLogicalType) ToConvertedType() (ConvertedType, DecimalMetadata) {
return ConvertedTypes.None, DecimalMetadata{}
}
func (UUIDLogicalType) IsCompatible(c ConvertedType, dec DecimalMetadata) bool {
if dec.IsSet {
return false
}
switch c {
case ConvertedTypes.None, ConvertedTypes.NA:
return true
}
return false
}
func (UUIDLogicalType) IsApplicable(t parquet.Type, tlen int32) bool {
return t == parquet.Types.FixedLenByteArray && tlen == 16
}
func (UUIDLogicalType) toThrift() *format.LogicalType {
return &format.LogicalType{UUID: format.NewUUIDType()}
}
func (UUIDLogicalType) Equals(rhs LogicalType) bool {
_, ok := rhs.(UUIDLogicalType)
return ok
}
// IntervalLogicalType is not yet in the thrift spec, but represents
// an interval time and needs to be a fixed length byte array of 12 bytes
type IntervalLogicalType struct{ baseLogicalType }
func (IntervalLogicalType) SortOrder() SortOrder {
return SortUNKNOWN
}
func (IntervalLogicalType) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]string{"Type": IntervalLogicalType{}.String()})
}
func (IntervalLogicalType) String() string {
return "Interval"
}
func (IntervalLogicalType) ToConvertedType() (ConvertedType, DecimalMetadata) {
return ConvertedTypes.Interval, DecimalMetadata{}
}
func (IntervalLogicalType) IsCompatible(c ConvertedType, dec DecimalMetadata) bool {
return c == ConvertedTypes.Interval && !dec.IsSet
}
func (IntervalLogicalType) IsApplicable(t parquet.Type, tlen int32) bool {
return t == parquet.Types.FixedLenByteArray && tlen == 12
}
func (IntervalLogicalType) toThrift() *format.LogicalType {
panic("no parquet IntervalLogicalType yet implemented")
}
func (IntervalLogicalType) Equals(rhs LogicalType) bool {
_, ok := rhs.(IntervalLogicalType)
return ok
}
// Float16LogicalType can only be used with a FixedLength byte array column
// that is exactly 2 bytes long
type Float16LogicalType struct{ baseLogicalType }
func (Float16LogicalType) SortOrder() SortOrder {
return SortSIGNED
}
func (Float16LogicalType) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]string{"Type": Float16LogicalType{}.String()})
}
func (Float16LogicalType) String() string {
return "Float16"
}
func (Float16LogicalType) ToConvertedType() (ConvertedType, DecimalMetadata) {
return ConvertedTypes.None, DecimalMetadata{}
}
func (Float16LogicalType) IsCompatible(c ConvertedType, dec DecimalMetadata) bool {
if dec.IsSet {
return false
}
switch c {
case ConvertedTypes.None, ConvertedTypes.NA:
return true
}
return false
}
func (Float16LogicalType) IsApplicable(t parquet.Type, tlen int32) bool {
return t == parquet.Types.FixedLenByteArray && tlen == 2
}
func (Float16LogicalType) toThrift() *format.LogicalType {
return &format.LogicalType{FLOAT16: format.NewFloat16Type()}
}
func (Float16LogicalType) Equals(rhs LogicalType) bool {
_, ok := rhs.(Float16LogicalType)
return ok
}
type NullLogicalType struct{ baseLogicalType }
func (NullLogicalType) SortOrder() SortOrder {
return SortUNKNOWN
}
func (NullLogicalType) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]string{"Type": NullLogicalType{}.String()})
}
func (NullLogicalType) String() string {
return "Null"
}
func (NullLogicalType) ToConvertedType() (ConvertedType, DecimalMetadata) {
return ConvertedTypes.None, DecimalMetadata{}
}
func (NullLogicalType) IsCompatible(c ConvertedType, dec DecimalMetadata) bool {
if dec.IsSet {
return false
}
switch c {
case ConvertedTypes.None, ConvertedTypes.NA:
return true
}
return false
}
func (NullLogicalType) IsApplicable(parquet.Type, int32) bool {
return true
}
func (NullLogicalType) toThrift() *format.LogicalType {
return &format.LogicalType{UNKNOWN: format.NewNullType()}
}
func (NullLogicalType) Equals(rhs LogicalType) bool {
_, ok := rhs.(NullLogicalType)
return ok
}
type NoLogicalType struct{ baseLogicalType }
func (NoLogicalType) SortOrder() SortOrder {
return SortUNKNOWN
}
func (NoLogicalType) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]string{"Type": NoLogicalType{}.String()})
}
func (NoLogicalType) IsSerialized() bool { return false }
func (NoLogicalType) String() string {
return "None"
}
func (NoLogicalType) ToConvertedType() (ConvertedType, DecimalMetadata) {
return ConvertedTypes.None, DecimalMetadata{}
}
func (NoLogicalType) IsCompatible(c ConvertedType, dec DecimalMetadata) bool {
return c == ConvertedTypes.None && !dec.IsSet
}
func (NoLogicalType) IsApplicable(parquet.Type, int32) bool {
return true
}
func (NoLogicalType) toThrift() *format.LogicalType {
panic("cannot convert NoLogicalType to thrift")
}
func (NoLogicalType) Equals(rhs LogicalType) bool {
_, ok := rhs.(NoLogicalType)
return ok
}
func (NoLogicalType) IsNone() bool { return true }
|