forked from csimplestring/delta-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrecord.go
171 lines (145 loc) · 4.63 KB
/
record.go
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
package deltago
import (
"strconv"
"time"
"github.com/csimplestring/delta-go/errno"
"github.com/csimplestring/delta-go/types"
"github.com/rotisserie/eris"
"github.com/shopspring/decimal"
)
type PartitionRowRecord struct {
partitionSchema *types.StructType
partitionValues map[string]string
}
func (r *PartitionRowRecord) getPrimitive(field *types.StructField) (string, error) {
partitionValue, ok := r.partitionValues[field.Name]
if !ok || len(partitionValue) == 0 {
return "", eris.Wrap(errno.NullValueFoundForPrimitiveTypes(field.Name), "")
}
return partitionValue, nil
}
func (p *PartitionRowRecord) Schema() types.StructType {
return *p.partitionSchema
}
func (p *PartitionRowRecord) Length() int {
return len(p.partitionSchema.Fields)
}
func (p *PartitionRowRecord) IsNullAt(fieldName string) (bool, error) {
if _, err := p.partitionSchema.Get(fieldName); err != nil {
return false, err
}
isNull := true
if v, exist := p.partitionValues[fieldName]; exist {
isNull = len(v) == 0
}
return isNull, nil
}
func (p *PartitionRowRecord) GetInt(fieldName string) (int, error) {
v, err := checkPrimitiveField[*types.IntegerType](p, fieldName, "interger")
if err != nil {
return 0, err
}
return strconv.Atoi(v)
}
func (p *PartitionRowRecord) GetInt64(fieldName string) (int64, error) {
v, err := checkPrimitiveField[*types.LongType](p, fieldName, "long")
if err != nil {
return 0, err
}
return strconv.ParseInt(v, 10, 64)
}
func (p *PartitionRowRecord) GetByte(fieldName string) (int8, error) {
// in GO, byte is uint8, but in Java, byte is int8
s, err := checkPrimitiveField[*types.ByteType](p, fieldName, "byte")
if err != nil {
return 0, err
}
v, err := strconv.ParseInt(s, 10, 8)
if err != nil {
return 0, err
}
return int8(v), nil
}
func (p *PartitionRowRecord) GetShort(fieldName string) (int16, error) {
s, err := checkPrimitiveField[*types.ShortType](p, fieldName, "short")
if err != nil {
return 0, err
}
v, err := strconv.ParseInt(s, 10, 16)
if err != nil {
return 0, err
}
return int16(v), nil
}
func (p *PartitionRowRecord) GetBoolean(fieldName string) (bool, error) {
s, err := checkPrimitiveField[*types.BooleanType](p, fieldName, "bool")
if err != nil {
return false, err
}
return strconv.ParseBool(s)
}
func (p *PartitionRowRecord) GetFloat(fieldName string) (float32, error) {
s, err := checkPrimitiveField[*types.FloatType](p, fieldName, "float")
if err != nil {
return 0, err
}
v, err := strconv.ParseFloat(s, 32)
return float32(v), err
}
func (p *PartitionRowRecord) GetDouble(fieldName string) (float64, error) {
s, err := checkPrimitiveField[*types.DoubleType](p, fieldName, "double")
if err != nil {
return 0, err
}
return strconv.ParseFloat(s, 64)
}
func (p *PartitionRowRecord) GetString(fieldName string) (string, error) {
return checkPrimitiveField[*types.StringType](p, fieldName, "string")
}
func (p *PartitionRowRecord) GetBinary(fieldName string) ([]byte, error) {
s, err := checkPrimitiveField[*types.BinaryType](p, fieldName, "binary")
if err != nil {
return nil, err
}
return []byte(s), err
}
func (p *PartitionRowRecord) GetBigDecimal(fieldName string) (decimal.Decimal, error) {
s, err := checkPrimitiveField[*types.BinaryType](p, fieldName, "decimal")
if err != nil {
return decimal.Decimal{}, err
}
return decimal.NewFromString(s)
}
func (p *PartitionRowRecord) GetTimestamp(fieldName string) (time.Time, error) {
s, err := checkPrimitiveField[*types.TimestampType](p, fieldName, "timestamp")
if err != nil {
return time.Time{}, err
}
return time.Parse("2006-01-02 15:04:05", s)
}
func (p *PartitionRowRecord) GetDate(fieldName string) (time.Time, error) {
s, err := checkPrimitiveField[*types.DateType](p, fieldName, "date")
if err != nil {
return time.Time{}, err
}
return time.Parse("2006-01-02", s)
}
func (p *PartitionRowRecord) GetRecord(fieldName string) (types.RowRecord, error) {
return nil, eris.Wrap(errno.ErrUnsupportedOperation, "Struct is not a supported partition type")
}
func (p *PartitionRowRecord) GetList(fieldName string) ([]any, error) {
return nil, eris.Wrap(errno.ErrUnsupportedOperation, "Array is not a supported partition type")
}
func (p *PartitionRowRecord) GetMap(fieldName string) (map[any]any, error) {
return nil, eris.Wrap(errno.ErrUnsupportedOperation, "Map is not a supported partition type")
}
func checkPrimitiveField[T types.DataType](p *PartitionRowRecord, fieldName string, expectedType string) (string, error) {
f, err := p.partitionSchema.Get(fieldName)
if err != nil {
return "", err
}
if !types.Is[T](f.DataType) {
return "", errno.FieldTypeMismatch(fieldName, f.DataType.Name(), expectedType)
}
return p.getPrimitive(f)
}