forked from apache/iceberg-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransforms.go
173 lines (134 loc) · 5.04 KB
/
transforms.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
172
173
// 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 iceberg
import (
"encoding"
"fmt"
"strconv"
"strings"
)
// ParseTransform takes the string representation of a transform as
// defined in the iceberg spec, and produces the appropriate Transform
// object or an error if the string is not a valid transform string.
func ParseTransform(s string) (Transform, error) {
s = strings.ToLower(s)
switch {
case strings.HasPrefix(s, "bucket"):
matches := regexFromBrackets.FindStringSubmatch(s)
if len(matches) != 2 {
break
}
n, _ := strconv.Atoi(matches[1])
return BucketTransform{NumBuckets: n}, nil
case strings.HasPrefix(s, "truncate"):
matches := regexFromBrackets.FindStringSubmatch(s)
if len(matches) != 2 {
break
}
n, _ := strconv.Atoi(matches[1])
return TruncateTransform{Width: n}, nil
default:
switch s {
case "identity":
return IdentityTransform{}, nil
case "void":
return VoidTransform{}, nil
case "year":
return YearTransform{}, nil
case "month":
return MonthTransform{}, nil
case "day":
return DayTransform{}, nil
case "hour":
return HourTransform{}, nil
}
}
return nil, fmt.Errorf("%w: %s", ErrInvalidTransform, s)
}
// Transform is an interface for the various Transformation types
// in partition specs. Currently, they do not yet provide actual
// transformation functions or implementation. That will come later as
// data reading gets implemented.
type Transform interface {
fmt.Stringer
encoding.TextMarshaler
ResultType(t Type) Type
}
// IdentityTransform uses the identity function, performing no transformation
// but instead partitioning on the value itself.
type IdentityTransform struct{}
func (t IdentityTransform) MarshalText() ([]byte, error) {
return []byte(t.String()), nil
}
func (IdentityTransform) String() string { return "identity" }
func (IdentityTransform) ResultType(t Type) Type { return t }
// VoidTransform is a transformation that always returns nil.
type VoidTransform struct{}
func (t VoidTransform) MarshalText() ([]byte, error) {
return []byte(t.String()), nil
}
func (VoidTransform) String() string { return "void" }
func (VoidTransform) ResultType(t Type) Type { return t }
// BucketTransform transforms values into a bucket partition value. It is
// parameterized by a number of buckets. Bucket partition transforms use
// a 32-bit hash of the source value to produce a positive value by mod
// the bucket number.
type BucketTransform struct {
NumBuckets int
}
func (t BucketTransform) MarshalText() ([]byte, error) {
return []byte(t.String()), nil
}
func (t BucketTransform) String() string { return fmt.Sprintf("bucket[%d]", t.NumBuckets) }
func (BucketTransform) ResultType(Type) Type { return PrimitiveTypes.Int32 }
// TruncateTransform is a transformation for truncating a value to a specified width.
type TruncateTransform struct {
Width int
}
func (t TruncateTransform) MarshalText() ([]byte, error) {
return []byte(t.String()), nil
}
func (t TruncateTransform) String() string { return fmt.Sprintf("truncate[%d]", t.Width) }
func (TruncateTransform) ResultType(t Type) Type { return t }
// YearTransform transforms a datetime value into a year value.
type YearTransform struct{}
func (t YearTransform) MarshalText() ([]byte, error) {
return []byte(t.String()), nil
}
func (YearTransform) String() string { return "year" }
func (YearTransform) ResultType(Type) Type { return PrimitiveTypes.Int32 }
// MonthTransform transforms a datetime value into a month value.
type MonthTransform struct{}
func (t MonthTransform) MarshalText() ([]byte, error) {
return []byte(t.String()), nil
}
func (MonthTransform) String() string { return "month" }
func (MonthTransform) ResultType(Type) Type { return PrimitiveTypes.Int32 }
// DayTransform transforms a datetime value into a date value.
type DayTransform struct{}
func (t DayTransform) MarshalText() ([]byte, error) {
return []byte(t.String()), nil
}
func (DayTransform) String() string { return "day" }
func (DayTransform) ResultType(Type) Type { return PrimitiveTypes.Date }
// HourTransform transforms a datetime value into an hour value.
type HourTransform struct{}
func (t HourTransform) MarshalText() ([]byte, error) {
return []byte(t.String()), nil
}
func (HourTransform) String() string { return "hour" }
func (HourTransform) ResultType(Type) Type { return PrimitiveTypes.Int32 }