]> git.proxmox.com Git - ceph.git/blob - ceph/src/arrow/go/arrow/datatype_fixedwidth.go
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / go / arrow / datatype_fixedwidth.go
1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements. See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership. The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16
17 package arrow
18
19 import (
20 "fmt"
21 "strconv"
22 "time"
23 )
24
25 type BooleanType struct{}
26
27 func (t *BooleanType) ID() Type { return BOOL }
28 func (t *BooleanType) Name() string { return "bool" }
29 func (t *BooleanType) String() string { return "bool" }
30 func (t *BooleanType) Fingerprint() string { return typeFingerprint(t) }
31
32 // BitWidth returns the number of bits required to store a single element of this data type in memory.
33 func (t *BooleanType) BitWidth() int { return 1 }
34
35 type FixedSizeBinaryType struct {
36 ByteWidth int
37 }
38
39 func (*FixedSizeBinaryType) ID() Type { return FIXED_SIZE_BINARY }
40 func (*FixedSizeBinaryType) Name() string { return "fixed_size_binary" }
41 func (t *FixedSizeBinaryType) BitWidth() int { return 8 * t.ByteWidth }
42 func (t *FixedSizeBinaryType) Fingerprint() string { return typeFingerprint(t) }
43 func (t *FixedSizeBinaryType) String() string {
44 return "fixed_size_binary[" + strconv.Itoa(t.ByteWidth) + "]"
45 }
46
47 type (
48 Timestamp int64
49 Time32 int32
50 Time64 int64
51 TimeUnit int
52 Date32 int32
53 Date64 int64
54 Duration int64
55 )
56
57 const (
58 Nanosecond TimeUnit = iota
59 Microsecond
60 Millisecond
61 Second
62 )
63
64 func (u TimeUnit) Multiplier() time.Duration {
65 return [...]time.Duration{time.Nanosecond, time.Microsecond, time.Millisecond, time.Second}[uint(u)&3]
66 }
67
68 func (u TimeUnit) String() string { return [...]string{"ns", "us", "ms", "s"}[uint(u)&3] }
69
70 // TimestampType is encoded as a 64-bit signed integer since the UNIX epoch (2017-01-01T00:00:00Z).
71 // The zero-value is a nanosecond and time zone neutral. Time zone neutral can be
72 // considered UTC without having "UTC" as a time zone.
73 type TimestampType struct {
74 Unit TimeUnit
75 TimeZone string
76 }
77
78 func (*TimestampType) ID() Type { return TIMESTAMP }
79 func (*TimestampType) Name() string { return "timestamp" }
80 func (t *TimestampType) String() string {
81 switch len(t.TimeZone) {
82 case 0:
83 return "timestamp[" + t.Unit.String() + "]"
84 default:
85 return "timestamp[" + t.Unit.String() + ", tz=" + t.TimeZone + "]"
86 }
87 }
88
89 func (t *TimestampType) Fingerprint() string {
90 return fmt.Sprintf("%s%d:%s", typeFingerprint(t)+string(timeUnitFingerprint(t.Unit)), len(t.TimeZone), t.TimeZone)
91 }
92
93 // BitWidth returns the number of bits required to store a single element of this data type in memory.
94 func (*TimestampType) BitWidth() int { return 64 }
95
96 // Time32Type is encoded as a 32-bit signed integer, representing either seconds or milliseconds since midnight.
97 type Time32Type struct {
98 Unit TimeUnit
99 }
100
101 func (*Time32Type) ID() Type { return TIME32 }
102 func (*Time32Type) Name() string { return "time32" }
103 func (*Time32Type) BitWidth() int { return 32 }
104 func (t *Time32Type) String() string { return "time32[" + t.Unit.String() + "]" }
105 func (t *Time32Type) Fingerprint() string {
106 return typeFingerprint(t) + string(timeUnitFingerprint(t.Unit))
107 }
108
109 // Time64Type is encoded as a 64-bit signed integer, representing either microseconds or nanoseconds since midnight.
110 type Time64Type struct {
111 Unit TimeUnit
112 }
113
114 func (*Time64Type) ID() Type { return TIME64 }
115 func (*Time64Type) Name() string { return "time64" }
116 func (*Time64Type) BitWidth() int { return 64 }
117 func (t *Time64Type) String() string { return "time64[" + t.Unit.String() + "]" }
118 func (t *Time64Type) Fingerprint() string {
119 return typeFingerprint(t) + string(timeUnitFingerprint(t.Unit))
120 }
121
122 // DurationType is encoded as a 64-bit signed integer, representing an amount
123 // of elapsed time without any relation to a calendar artifact.
124 type DurationType struct {
125 Unit TimeUnit
126 }
127
128 func (*DurationType) ID() Type { return DURATION }
129 func (*DurationType) Name() string { return "duration" }
130 func (*DurationType) BitWidth() int { return 64 }
131 func (t *DurationType) String() string { return "duration[" + t.Unit.String() + "]" }
132 func (t *DurationType) Fingerprint() string {
133 return typeFingerprint(t) + string(timeUnitFingerprint(t.Unit))
134 }
135
136 // Float16Type represents a floating point value encoded with a 16-bit precision.
137 type Float16Type struct{}
138
139 func (t *Float16Type) ID() Type { return FLOAT16 }
140 func (t *Float16Type) Name() string { return "float16" }
141 func (t *Float16Type) String() string { return "float16" }
142 func (t *Float16Type) Fingerprint() string { return typeFingerprint(t) }
143
144 // BitWidth returns the number of bits required to store a single element of this data type in memory.
145 func (t *Float16Type) BitWidth() int { return 16 }
146
147 // Decimal128Type represents a fixed-size 128-bit decimal type.
148 type Decimal128Type struct {
149 Precision int32
150 Scale int32
151 }
152
153 func (*Decimal128Type) ID() Type { return DECIMAL128 }
154 func (*Decimal128Type) Name() string { return "decimal" }
155 func (*Decimal128Type) BitWidth() int { return 128 }
156 func (t *Decimal128Type) String() string {
157 return fmt.Sprintf("%s(%d, %d)", t.Name(), t.Precision, t.Scale)
158 }
159 func (t *Decimal128Type) Fingerprint() string {
160 return fmt.Sprintf("%s[%d,%d,%d]", typeFingerprint(t), t.BitWidth(), t.Precision, t.Scale)
161 }
162
163 // MonthInterval represents a number of months.
164 type MonthInterval int32
165
166 // MonthIntervalType is encoded as a 32-bit signed integer,
167 // representing a number of months.
168 type MonthIntervalType struct{}
169
170 func (*MonthIntervalType) ID() Type { return INTERVAL_MONTHS }
171 func (*MonthIntervalType) Name() string { return "month_interval" }
172 func (*MonthIntervalType) String() string { return "month_interval" }
173 func (*MonthIntervalType) Fingerprint() string { return typeIDFingerprint(INTERVAL_MONTHS) + "M" }
174
175 // BitWidth returns the number of bits required to store a single element of this data type in memory.
176 func (t *MonthIntervalType) BitWidth() int { return 32 }
177
178 // DayTimeInterval represents a number of days and milliseconds (fraction of day).
179 type DayTimeInterval struct {
180 Days int32 `json:"days"`
181 Milliseconds int32 `json:"milliseconds"`
182 }
183
184 // DayTimeIntervalType is encoded as a pair of 32-bit signed integer,
185 // representing a number of days and milliseconds (fraction of day).
186 type DayTimeIntervalType struct{}
187
188 func (*DayTimeIntervalType) ID() Type { return INTERVAL_DAY_TIME }
189 func (*DayTimeIntervalType) Name() string { return "day_time_interval" }
190 func (*DayTimeIntervalType) String() string { return "day_time_interval" }
191 func (*DayTimeIntervalType) Fingerprint() string { return typeIDFingerprint(INTERVAL_DAY_TIME) + "d" }
192
193 // BitWidth returns the number of bits required to store a single element of this data type in memory.
194 func (t *DayTimeIntervalType) BitWidth() int { return 64 }
195
196 // MonthDayNanoInterval represents a number of months, days and nanoseconds (fraction of day).
197 type MonthDayNanoInterval struct {
198 Months int32 `json:"months"`
199 Days int32 `json:"days"`
200 Nanoseconds int64 `json:"nanoseconds"`
201 }
202
203 // MonthDayNanoIntervalType is encoded as two signed 32-bit integers representing
204 // a number of months and a number of days, followed by a 64-bit integer representing
205 // the number of nanoseconds since midnight for fractions of a day.
206 type MonthDayNanoIntervalType struct{}
207
208 func (*MonthDayNanoIntervalType) ID() Type { return INTERVAL_MONTH_DAY_NANO }
209 func (*MonthDayNanoIntervalType) Name() string { return "month_day_nano_interval" }
210 func (*MonthDayNanoIntervalType) String() string { return "month_day_nano_interval" }
211 func (*MonthDayNanoIntervalType) Fingerprint() string {
212 return typeIDFingerprint(INTERVAL_MONTH_DAY_NANO) + "N"
213 }
214
215 // BitWidth returns the number of bits required to store a single element of this data type in memory.
216 func (*MonthDayNanoIntervalType) BitWidth() int { return 128 }
217
218 var (
219 FixedWidthTypes = struct {
220 Boolean FixedWidthDataType
221 Date32 FixedWidthDataType
222 Date64 FixedWidthDataType
223 DayTimeInterval FixedWidthDataType
224 Duration_s FixedWidthDataType
225 Duration_ms FixedWidthDataType
226 Duration_us FixedWidthDataType
227 Duration_ns FixedWidthDataType
228 Float16 FixedWidthDataType
229 MonthInterval FixedWidthDataType
230 Time32s FixedWidthDataType
231 Time32ms FixedWidthDataType
232 Time64us FixedWidthDataType
233 Time64ns FixedWidthDataType
234 Timestamp_s FixedWidthDataType
235 Timestamp_ms FixedWidthDataType
236 Timestamp_us FixedWidthDataType
237 Timestamp_ns FixedWidthDataType
238 MonthDayNanoInterval FixedWidthDataType
239 }{
240 Boolean: &BooleanType{},
241 Date32: &Date32Type{},
242 Date64: &Date64Type{},
243 DayTimeInterval: &DayTimeIntervalType{},
244 Duration_s: &DurationType{Unit: Second},
245 Duration_ms: &DurationType{Unit: Millisecond},
246 Duration_us: &DurationType{Unit: Microsecond},
247 Duration_ns: &DurationType{Unit: Nanosecond},
248 Float16: &Float16Type{},
249 MonthInterval: &MonthIntervalType{},
250 Time32s: &Time32Type{Unit: Second},
251 Time32ms: &Time32Type{Unit: Millisecond},
252 Time64us: &Time64Type{Unit: Microsecond},
253 Time64ns: &Time64Type{Unit: Nanosecond},
254 Timestamp_s: &TimestampType{Unit: Second, TimeZone: "UTC"},
255 Timestamp_ms: &TimestampType{Unit: Millisecond, TimeZone: "UTC"},
256 Timestamp_us: &TimestampType{Unit: Microsecond, TimeZone: "UTC"},
257 Timestamp_ns: &TimestampType{Unit: Nanosecond, TimeZone: "UTC"},
258 MonthDayNanoInterval: &MonthDayNanoIntervalType{},
259 }
260
261 _ FixedWidthDataType = (*FixedSizeBinaryType)(nil)
262 )