/
create.go
220 lines (212 loc) · 5.65 KB
/
create.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
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
package redshiftavro
import (
"bytes"
"strconv"
"github.com/khezen/avro"
"github.com/khezen/avro/sqlavro"
)
// CreateTableStatement -
func CreateTableStatement(cfg CreateConfig) (string, error) {
buf := bytes.NewBufferString("CREATE TABLE ")
if cfg.IfNotExists {
buf.WriteString("IF NOT EXISTS ")
}
var tableName string
if len(cfg.TableNameOverride) > 0 {
tableName = sqlavro.SQLEscape(cfg.TableNameOverride)
} else {
tableName = sqlavro.SQLEscape(cfg.Schema.Name)
}
buf.WriteString(tableName)
buf.WriteRune('(')
var (
mapSortKeys = mapSortKeys(cfg.SortKeys)
fieldsLen = len(cfg.Schema.Fields)
i int
field avro.RecordFieldSchema
columnStmt string
err error
isSortKey bool
)
for i, field = range cfg.Schema.Fields {
_, isSortKey = mapSortKeys[field.Name]
columnStmt, err = createColumnStatement(field, isSortKey)
if err != nil {
return "", err
}
buf.WriteString(columnStmt)
if i < fieldsLen-1 {
buf.WriteRune(',')
}
}
buf.WriteRune(')')
switch {
case cfg.DistStyle == DistStyleAll:
buf.WriteString(" DISTSTYLE ALL")
case cfg.DistStyle == DistStyleEven:
buf.WriteString(" DISTSTYLE EVEN")
case cfg.DistStyle == DistStyleAuto:
buf.WriteString(" DISTSTYLE AUTO")
case cfg.DistStyle == DistStyleKey && cfg.DistKey != nil:
buf.WriteString(" DISTSTYLE KEY")
buf.WriteString(" DISTKEY(")
buf.WriteString(*cfg.DistKey)
buf.WriteString(") ")
case cfg.DistStyle == "":
if cfg.DistKey != nil {
buf.WriteString(" DISTKEY(")
buf.WriteString(*cfg.DistKey)
buf.WriteString(") ")
}
default:
return "", ErrUnuspportedDistributionStyle
}
if len(cfg.SortKeys) > 0 {
switch cfg.SortStyle {
case SortStyleCompound, SortStyleInterleaved, SortStyleNormal:
buf.WriteString(string(cfg.SortStyle))
buf.WriteRune(' ')
}
buf.WriteString("SORTKEY(")
var (
i int
sortKeysLen = len(cfg.SortKeys)
)
for i = 0; i < sortKeysLen; i++ {
buf.WriteString(cfg.SortKeys[i])
if i < sortKeysLen-1 {
buf.WriteRune(',')
}
}
buf.WriteRune(')')
}
buf.WriteRune(';')
return buf.String(), nil
}
func createColumnStatement(field avro.RecordFieldSchema, isSortKey bool) (string, error) {
var (
columnName = sqlavro.SQLEscape(field.Name)
buf = new(bytes.Buffer)
)
typeStatement, redshiftType, isNullable, err := renderType(field)
if err != nil {
return "", err
}
encoding, err := renderDefaultEncoding(redshiftType, isSortKey)
if err != nil {
return "", err
}
buf.WriteString(columnName)
buf.WriteRune(' ')
buf.WriteString(typeStatement)
buf.WriteRune(' ')
buf.WriteString("ENCODE ")
buf.WriteString(string(encoding))
buf.WriteRune(' ')
if !isNullable {
buf.WriteString("NOT NULL")
} else {
buf.WriteString("NULL")
}
return buf.String(), nil
}
func renderType(field avro.RecordFieldSchema) (typeStatement string, redshiftType RedshiftType, isNullable bool, err error) {
isNullable = field.Type.TypeName() == avro.TypeUnion
var schema avro.Schema
if isNullable {
schema, err = sqlavro.UnderlyingType(field.Type.(avro.UnionSchema))
if err != nil {
return "", "", false, err
}
} else {
schema = field.Type
}
switch schema.TypeName() {
case avro.TypeInt32:
return string(Integer), Integer, isNullable, nil
case avro.TypeInt64, avro.Type(avro.LogialTypeDuration):
return string(BigInt), BigInt, isNullable, nil
case avro.TypeFloat32:
return string(Real), Real, isNullable, nil
case avro.TypeFloat64:
return "DOUBLE PRECISION", Double, isNullable, nil
case avro.Type(avro.LogicalTypeDecimal):
buf := new(bytes.Buffer)
buf.WriteString(string(Decimal))
buf.WriteRune('(')
dec := schema.(*avro.DerivedPrimitiveSchema)
precistionStr := strconv.FormatInt(int64(*dec.Precision), 10)
buf.WriteString(precistionStr)
buf.WriteRune(',')
scaleStr := strconv.FormatInt(int64(*dec.Scale), 10)
buf.WriteString(scaleStr)
buf.WriteRune(')')
return buf.String(), Decimal, isNullable, nil
case avro.TypeEnum, avro.TypeString, avro.TypeBytes:
buf := new(bytes.Buffer)
buf.WriteString(string(VarChar))
buf.WriteRune('(')
buf.WriteString(varcharLenStr)
buf.WriteRune(')')
return buf.String(), VarChar, isNullable, nil
case avro.TypeFixed:
buf := new(bytes.Buffer)
buf.WriteString(string(Char))
fix := schema.(*avro.FixedSchema)
size := strconv.FormatInt(int64(fix.Size), 10)
buf.WriteRune('(')
buf.WriteString(size)
buf.WriteRune(')')
return buf.String(), Char, isNullable, nil
case avro.TypeBoolean:
return string(Boolean), Boolean, isNullable, nil
case avro.Type(avro.LogicalTypeDate):
return string(Date), Date, isNullable, nil
case avro.Type(avro.LogicalTypeTime), avro.Type(avro.LogicalTypeTimestamp):
buf := bytes.NewBufferString(string(Timestamp))
buf.WriteRune(' ')
buf.WriteString("WITHOUT TIME ZONE")
return buf.String(), Timestamp, isNullable, nil
default:
return "", RedshiftType(""), false, ErrUnsupportedRedshiftType
}
}
func renderDefaultEncoding(redshiftType RedshiftType, isSortKey bool) (encoding RedshiftEncoding, err error) {
if isSortKey {
return Raw, nil
}
switch redshiftType {
case Integer:
return LZO, nil
case BigInt:
return LZO, nil
case Decimal:
return Raw, nil
case Real:
return Raw, nil
case Double:
return Raw, nil
case Boolean:
return Raw, nil
case Char:
return ZSTD, nil
case VarChar:
return ZSTD, nil
case Date:
return LZO, nil
case Timestamp:
return LZO, nil
default:
return RedshiftEncoding(""), ErrUnsupportedRedshiftType
}
}
func mapSortKeys(sortKeys []string) map[string]struct{} {
sortKeyMap := make(map[string]struct{})
if sortKeys == nil {
return sortKeyMap
}
for _, sortKey := range sortKeys {
sortKeyMap[sortKey] = struct{}{}
}
return sortKeyMap
}