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
17package arrow
18
19import (
20	"fmt"
21	"strconv"
22)
23
24type BooleanType struct{}
25
26func (t *BooleanType) ID() Type       { return BOOL }
27func (t *BooleanType) Name() string   { return "bool" }
28func (t *BooleanType) String() string { return "bool" }
29
30// BitWidth returns the number of bits required to store a single element of this data type in memory.
31func (t *BooleanType) BitWidth() int { return 1 }
32
33type FixedSizeBinaryType struct {
34	ByteWidth int
35}
36
37func (*FixedSizeBinaryType) ID() Type        { return FIXED_SIZE_BINARY }
38func (*FixedSizeBinaryType) Name() string    { return "fixed_size_binary" }
39func (t *FixedSizeBinaryType) BitWidth() int { return 8 * t.ByteWidth }
40
41func (t *FixedSizeBinaryType) String() string {
42	return "fixed_size_binary[" + strconv.Itoa(t.ByteWidth) + "]"
43}
44
45type (
46	Timestamp int64
47	Time32    int32
48	Time64    int64
49	TimeUnit  int
50	Date32    int32
51	Date64    int64
52	Duration  int64
53)
54
55const (
56	Nanosecond TimeUnit = iota
57	Microsecond
58	Millisecond
59	Second
60)
61
62func (u TimeUnit) String() string { return [...]string{"ns", "us", "ms", "s"}[uint(u)&3] }
63
64// TimestampType is encoded as a 64-bit signed integer since the UNIX epoch (2017-01-01T00:00:00Z).
65// The zero-value is a nanosecond and time zone neutral. Time zone neutral can be
66// considered UTC without having "UTC" as a time zone.
67type TimestampType struct {
68	Unit     TimeUnit
69	TimeZone string
70}
71
72func (*TimestampType) ID() Type     { return TIMESTAMP }
73func (*TimestampType) Name() string { return "timestamp" }
74func (t *TimestampType) String() string {
75	switch len(t.TimeZone) {
76	case 0:
77		return "timestamp[" + t.Unit.String() + "]"
78	default:
79		return "timestamp[" + t.Unit.String() + ", tz=" + t.TimeZone + "]"
80	}
81}
82
83// BitWidth returns the number of bits required to store a single element of this data type in memory.
84func (*TimestampType) BitWidth() int { return 64 }
85
86// Time32Type is encoded as a 32-bit signed integer, representing either seconds or milliseconds since midnight.
87type Time32Type struct {
88	Unit TimeUnit
89}
90
91func (*Time32Type) ID() Type         { return TIME32 }
92func (*Time32Type) Name() string     { return "time32" }
93func (*Time32Type) BitWidth() int    { return 32 }
94func (t *Time32Type) String() string { return "time32[" + t.Unit.String() + "]" }
95
96// Time64Type is encoded as a 64-bit signed integer, representing either microseconds or nanoseconds since midnight.
97type Time64Type struct {
98	Unit TimeUnit
99}
100
101func (*Time64Type) ID() Type         { return TIME64 }
102func (*Time64Type) Name() string     { return "time64" }
103func (*Time64Type) BitWidth() int    { return 64 }
104func (t *Time64Type) String() string { return "time64[" + t.Unit.String() + "]" }
105
106// DurationType is encoded as a 64-bit signed integer, representing an amount
107// of elapsed time without any relation to a calendar artifact.
108type DurationType struct {
109	Unit TimeUnit
110}
111
112func (*DurationType) ID() Type         { return DURATION }
113func (*DurationType) Name() string     { return "duration" }
114func (*DurationType) BitWidth() int    { return 64 }
115func (t *DurationType) String() string { return "duration[" + t.Unit.String() + "]" }
116
117// Float16Type represents a floating point value encoded with a 16-bit precision.
118type Float16Type struct{}
119
120func (t *Float16Type) ID() Type       { return FLOAT16 }
121func (t *Float16Type) Name() string   { return "float16" }
122func (t *Float16Type) String() string { return "float16" }
123
124// BitWidth returns the number of bits required to store a single element of this data type in memory.
125func (t *Float16Type) BitWidth() int { return 16 }
126
127// Decimal128Type represents a fixed-size 128-bit decimal type.
128type Decimal128Type struct {
129	Precision int32
130	Scale     int32
131}
132
133func (*Decimal128Type) ID() Type      { return DECIMAL }
134func (*Decimal128Type) Name() string  { return "decimal" }
135func (*Decimal128Type) BitWidth() int { return 16 }
136func (t *Decimal128Type) String() string {
137	return fmt.Sprintf("%s(%d, %d)", t.Name(), t.Precision, t.Scale)
138}
139
140// MonthInterval represents a number of months.
141type MonthInterval int32
142
143// MonthIntervalType is encoded as a 32-bit signed integer,
144// representing a number of months.
145type MonthIntervalType struct{}
146
147func (*MonthIntervalType) ID() Type       { return INTERVAL }
148func (*MonthIntervalType) Name() string   { return "month_interval" }
149func (*MonthIntervalType) String() string { return "month_interval" }
150
151// BitWidth returns the number of bits required to store a single element of this data type in memory.
152func (t *MonthIntervalType) BitWidth() int { return 32 }
153
154// DayTimeInterval represents a number of days and milliseconds (fraction of day).
155type DayTimeInterval struct {
156	Days         int32 `json:"days"`
157	Milliseconds int32 `json:"milliseconds"`
158}
159
160// DayTimeIntervalType is encoded as a pair of 32-bit signed integer,
161// representing a number of days and milliseconds (fraction of day).
162type DayTimeIntervalType struct{}
163
164func (*DayTimeIntervalType) ID() Type       { return INTERVAL }
165func (*DayTimeIntervalType) Name() string   { return "day_time_interval" }
166func (*DayTimeIntervalType) String() string { return "day_time_interval" }
167
168// BitWidth returns the number of bits required to store a single element of this data type in memory.
169func (t *DayTimeIntervalType) BitWidth() int { return 64 }
170
171var (
172	FixedWidthTypes = struct {
173		Boolean         FixedWidthDataType
174		Date32          FixedWidthDataType
175		Date64          FixedWidthDataType
176		DayTimeInterval FixedWidthDataType
177		Duration_s      FixedWidthDataType
178		Duration_ms     FixedWidthDataType
179		Duration_us     FixedWidthDataType
180		Duration_ns     FixedWidthDataType
181		Float16         FixedWidthDataType
182		MonthInterval   FixedWidthDataType
183		Time32s         FixedWidthDataType
184		Time32ms        FixedWidthDataType
185		Time64us        FixedWidthDataType
186		Time64ns        FixedWidthDataType
187		Timestamp_s     FixedWidthDataType
188		Timestamp_ms    FixedWidthDataType
189		Timestamp_us    FixedWidthDataType
190		Timestamp_ns    FixedWidthDataType
191	}{
192		Boolean:         &BooleanType{},
193		Date32:          &Date32Type{},
194		Date64:          &Date64Type{},
195		DayTimeInterval: &DayTimeIntervalType{},
196		Duration_s:      &DurationType{Unit: Second},
197		Duration_ms:     &DurationType{Unit: Millisecond},
198		Duration_us:     &DurationType{Unit: Microsecond},
199		Duration_ns:     &DurationType{Unit: Nanosecond},
200		Float16:         &Float16Type{},
201		MonthInterval:   &MonthIntervalType{},
202		Time32s:         &Time32Type{Unit: Second},
203		Time32ms:        &Time32Type{Unit: Millisecond},
204		Time64us:        &Time64Type{Unit: Microsecond},
205		Time64ns:        &Time64Type{Unit: Nanosecond},
206		Timestamp_s:     &TimestampType{Unit: Second, TimeZone: "UTC"},
207		Timestamp_ms:    &TimestampType{Unit: Millisecond, TimeZone: "UTC"},
208		Timestamp_us:    &TimestampType{Unit: Microsecond, TimeZone: "UTC"},
209		Timestamp_ns:    &TimestampType{Unit: Nanosecond, TimeZone: "UTC"},
210	}
211
212	_ FixedWidthDataType = (*FixedSizeBinaryType)(nil)
213)
214