/
redshift.go
386 lines (344 loc) · 13.3 KB
/
redshift.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
package sql
import (
"context"
"fmt"
"github.com/jitsucom/bulker/base/errorj"
"github.com/jitsucom/bulker/base/timestamp"
"github.com/jitsucom/bulker/base/utils"
"github.com/jitsucom/bulker/bulker"
"github.com/jitsucom/bulker/types"
_ "github.com/lib/pq"
"strings"
)
func init() {
bulker.RegisterBulker(RedshiftBulkerTypeId, NewRedshift)
}
const (
RedshiftBulkerTypeId = "redshift"
redshiftCopyTemplate = `copy %s (%s)
from 's3://%s/%s'
ACCESS_KEY_ID '%s'
SECRET_ACCESS_KEY '%s'
region '%s'
csv
gzip
IGNOREHEADER 1
dateformat 'auto'
timeformat 'auto'`
redshiftAlterSortKeyTemplate = `ALTER TABLE %s ALTER SORTKEY (%s)`
redshiftDeleteBeforeBulkMergeUsing = `DELETE FROM %s using %s where %s`
redshiftPrimaryKeyFieldsQuery = `select tco.constraint_name as constraint_name, kcu.column_name as key_column
from information_schema.table_constraints tco
join information_schema.key_column_usage kcu
on kcu.constraint_name = tco.constraint_name
and kcu.constraint_schema = tco.constraint_schema
and kcu.constraint_name = tco.constraint_name
where tco.table_schema = $1 and tco.table_name = $2 and tco.constraint_type = 'PRIMARY KEY'
order by kcu.ordinal_position`
credentialsMask = "*****"
)
var (
redshiftTypes = map[types.DataType][]string{
types.STRING: {"character varying(65535)"},
types.INT64: {"bigint"},
types.FLOAT64: {"double precision"},
types.TIMESTAMP: {"timestamp with time zone", "timestamp", "timestamp without time zone"},
types.BOOL: {"boolean"},
types.JSON: {"character varying(65535)"},
types.UNKNOWN: {"character varying(65535)"},
}
)
type RedshiftConfig struct {
DataSourceConfig `mapstructure:",squash"`
S3OptionConfig `mapstructure:",squash" yaml:"-,inline"`
}
// Redshift adapter for creating,patching (schema or table), inserting and copying data from s3 to redshift
type Redshift struct {
//Aws Redshift uses Postgres fork under the hood
*Postgres
s3Config *S3OptionConfig
}
// NewRedshift returns configured Redshift adapter instance
func NewRedshift(bulkerConfig bulker.Config) (bulker.Bulker, error) {
config := &RedshiftConfig{}
if err := utils.ParseObject(bulkerConfig.DestinationConfig, config); err != nil {
return nil, fmt.Errorf("failed to parse destination config: %w", err)
}
if config.Port == 0 {
config.Port = 5439
}
bulkerConfig.DestinationConfig = PostgresConfig{DataSourceConfig: config.DataSourceConfig}
postgres, err := NewPostgres(bulkerConfig)
if err != nil {
return nil, err
}
r := &Redshift{Postgres: postgres.(*Postgres), s3Config: &config.S3OptionConfig}
r.batchFileFormat = types.FileFormatCSV
r.batchFileCompression = types.FileCompressionGZIP
r.temporaryTables = true
r._columnDDLFunc = redshiftColumnDDL
r.initTypes(redshiftTypes)
r.tableHelper = NewTableHelper(r, 127, '"')
//// Redshift is case insensitive by default
//r._columnNameFunc = strings.ToLower
//r._tableNameFunc = func(config *DataSourceConfig, tableName string) string { return tableName }
return r, err
}
func (p *Redshift) CreateStream(id, tableName string, mode bulker.BulkMode, streamOptions ...bulker.StreamOption) (bulker.BulkerStream, error) {
streamOptions = append(streamOptions, withLocalBatchFile(fmt.Sprintf("bulker_%s", utils.SanitizeString(id))))
if p.s3Config != nil {
streamOptions = append(streamOptions, withS3BatchFile(p.s3Config))
}
if err := p.validateOptions(streamOptions); err != nil {
return nil, err
}
switch mode {
case bulker.Stream:
return newAutoCommitStream(id, p, tableName, streamOptions...)
case bulker.Batch:
return newTransactionalStream(id, p, tableName, streamOptions...)
case bulker.ReplaceTable:
return newReplaceTableStream(id, p, tableName, streamOptions...)
case bulker.ReplacePartition:
return newReplacePartitionStream(id, p, tableName, streamOptions...)
}
return nil, fmt.Errorf("unsupported bulk mode: %s", mode)
}
func (p *Redshift) validateOptions(streamOptions []bulker.StreamOption) error {
options := &bulker.StreamOptions{}
for _, option := range streamOptions {
options.Add(option)
}
return nil
}
// Type returns Postgres type
func (p *Redshift) Type() string {
return RedshiftBulkerTypeId
}
// OpenTx opens underline sql transaction and return wrapped instance
func (p *Redshift) OpenTx(ctx context.Context) (*TxSQLAdapter, error) {
return p.openTx(ctx, p)
}
func (p *Redshift) Insert(ctx context.Context, table *Table, merge bool, objects ...types.Object) error {
if !merge || len(table.GetPKFields()) == 0 {
return p.insert(ctx, table, objects)
}
for _, object := range objects {
pkMatchConditions := &WhenConditions{}
for _, pkColumn := range table.GetPKFields() {
value := object[pkColumn]
if value == nil {
pkMatchConditions = pkMatchConditions.Add(pkColumn, "IS NULL", nil)
} else {
pkMatchConditions = pkMatchConditions.Add(pkColumn, "=", value)
}
}
res, err := p.Select(ctx, table.Name, pkMatchConditions, nil)
if err != nil {
return errorj.ExecuteInsertError.Wrap(err, "failed check primary key collision").
WithProperty(errorj.DBInfo, &types.ErrorPayload{
Schema: p.config.Schema,
Table: p.quotedTableName(table.Name),
PrimaryKeys: table.GetPKFields(),
})
}
if len(res) > 0 {
return p.Update(ctx, table, object, pkMatchConditions)
} else {
return p.insert(ctx, table, []types.Object{object})
}
}
return nil
}
// LoadTable copy transfer data from s3 to redshift by passing COPY request to redshift
func (p *Redshift) LoadTable(ctx context.Context, targetTable *Table, loadSource *LoadSource) (err error) {
quotedTableName := p.quotedTableName(targetTable.Name)
if loadSource.Type != AmazonS3 {
return fmt.Errorf("LoadTable: only Amazon S3 file is supported")
}
if loadSource.Format != p.batchFileFormat {
return fmt.Errorf("LoadTable: only %s format is supported", p.batchFileFormat)
}
columns := targetTable.SortedColumnNames()
columnNames := make([]string, len(columns))
for i, name := range columns {
columnNames[i] = p.quotedColumnName(name)
}
s3Config := loadSource.S3Config
fileKey := loadSource.Path
//add folder prefix if configured
if s3Config.Folder != "" {
fileKey = s3Config.Folder + "/" + fileKey
}
statement := fmt.Sprintf(redshiftCopyTemplate, quotedTableName, strings.Join(columnNames, ","), s3Config.Bucket, fileKey, s3Config.AccessKeyID, s3Config.SecretKey, s3Config.Region)
if _, err := p.txOrDb(ctx).ExecContext(ctx, statement); err != nil {
return errorj.CopyError.Wrap(err, "failed to copy data from s3").
WithProperty(errorj.DBInfo, &types.ErrorPayload{
Schema: p.config.Schema,
Table: quotedTableName,
Statement: fmt.Sprintf(redshiftCopyTemplate, quotedTableName, strings.Join(columnNames, ","), s3Config.Bucket, fileKey, credentialsMask, credentialsMask, s3Config.Region),
})
}
return nil
}
func (p *Redshift) CopyTables(ctx context.Context, targetTable *Table, sourceTable *Table, merge bool) (err error) {
quotedTargetTableName := p.quotedTableName(targetTable.Name)
quotedSourceTableName := p.quotedTableName(sourceTable.Name)
if merge && len(targetTable.PKFields) > 0 {
//delete duplicates from table
var pkMatchConditions string
for i, pkColumn := range targetTable.GetPKFields() {
if i > 0 {
pkMatchConditions += " AND "
}
pkMatchConditions += fmt.Sprintf(`%s.%s = %s.%s`, quotedTargetTableName, pkColumn, quotedSourceTableName, pkColumn)
}
deleteStatement := fmt.Sprintf(redshiftDeleteBeforeBulkMergeUsing, quotedTargetTableName, quotedSourceTableName, pkMatchConditions)
if _, err = p.txOrDb(ctx).ExecContext(ctx, deleteStatement); err != nil {
return errorj.BulkMergeError.Wrap(err, "failed to delete duplicated rows").
WithProperty(errorj.DBInfo, &types.ErrorPayload{
Schema: p.config.Schema,
Table: quotedTargetTableName,
PrimaryKeys: targetTable.GetPKFields(),
Statement: deleteStatement,
})
}
}
return p.copy(ctx, targetTable, sourceTable)
}
func (p *Redshift) ReplaceTable(ctx context.Context, targetTableName string, replacementTable *Table, dropOldTable bool) (err error) {
tmpTable := "deprecated_" + targetTableName + timestamp.Now().Format("_20060102_150405")
err1 := p.renameTable(ctx, true, targetTableName, tmpTable)
err = p.renameTable(ctx, false, replacementTable.Name, targetTableName)
if dropOldTable && err1 == nil && err == nil {
return p.DropTable(ctx, tmpTable, true)
}
return
}
func (p *Redshift) renameTable(ctx context.Context, ifExists bool, tableName, newTableName string) error {
if ifExists {
row := p.txOrDb(ctx).QueryRowContext(ctx, fmt.Sprintf(`SELECT EXISTS (SELECT * FROM information_schema.tables WHERE table_schema = '%s' AND table_name = '%s')`, p.config.Schema, tableName))
exists := false
err := row.Scan(&exists)
if err != nil {
return err
}
if !exists {
return nil
}
}
return p.SQLAdapterBase.renameTable(ctx, false, tableName, newTableName)
}
// GetTableSchema return table (name,columns, primary key) representation wrapped in Table struct
func (p *Redshift) GetTableSchema(ctx context.Context, tableName string) (*Table, error) {
table, err := p.getTable(ctx, strings.ToLower(tableName))
if err != nil {
return nil, err
}
//don't select primary keys of non-existent table
if len(table.Columns) == 0 {
return table, nil
}
primaryKeyName, pkFields, err := p.getPrimaryKeys(ctx, table.Name)
if err != nil {
return nil, err
}
table.PKFields = pkFields
table.PrimaryKeyName = primaryKeyName
jitsuPrimaryKeyName := BuildConstraintName(table.Name)
if primaryKeyName != "" && primaryKeyName != jitsuPrimaryKeyName {
p.Warnf("table: %s.%s has a custom primary key with name: %s that isn't managed by Jitsu. Custom primary key will be used in rows deduplication and updates. primary_key_fields configuration provided in Jitsu config will be ignored.", p.config.Schema, table.Name, primaryKeyName)
}
return table, nil
}
func (p *Redshift) getPrimaryKeys(ctx context.Context, tableName string) (string, utils.Set[string], error) {
tableName = p.TableName(tableName)
primaryKeys := utils.NewSet[string]()
pkFieldsRows, err := p.txOrDb(ctx).QueryContext(ctx, redshiftPrimaryKeyFieldsQuery, p.config.Schema, tableName)
if err != nil {
return "", nil, errorj.GetPrimaryKeysError.Wrap(err, "failed to get primary key").
WithProperty(errorj.DBInfo, &types.ErrorPayload{
Schema: p.config.Schema,
Table: tableName,
Statement: redshiftPrimaryKeyFieldsQuery,
Values: []any{p.config.Schema, tableName},
})
}
defer pkFieldsRows.Close()
var pkFields []string
var primaryKeyName string
for pkFieldsRows.Next() {
var constraintName, fieldName string
if err := pkFieldsRows.Scan(&constraintName, &fieldName); err != nil {
return "", nil, errorj.GetPrimaryKeysError.Wrap(err, "failed to scan result").
WithProperty(errorj.DBInfo, &types.ErrorPayload{
Schema: p.config.Schema,
Table: tableName,
Statement: redshiftPrimaryKeyFieldsQuery,
Values: []any{p.config.Schema, tableName},
})
}
if primaryKeyName == "" && constraintName != "" {
primaryKeyName = constraintName
}
pkFields = append(pkFields, fieldName)
}
if err := pkFieldsRows.Err(); err != nil {
return "", nil, errorj.GetPrimaryKeysError.Wrap(err, "failed read last row").
WithProperty(errorj.DBInfo, &types.ErrorPayload{
Schema: p.config.Schema,
Table: tableName,
Statement: redshiftPrimaryKeyFieldsQuery,
Values: []any{p.config.Schema, tableName},
})
}
for _, field := range pkFields {
primaryKeys.Put(field)
}
return primaryKeyName, primaryKeys, nil
}
func (p *Redshift) CreateTable(ctx context.Context, schemaToCreate *Table) error {
err := p.SQLAdapterBase.CreateTable(ctx, schemaToCreate)
if err != nil {
return err
}
if !schemaToCreate.Temporary && schemaToCreate.TimestampColumn != "" {
err = p.createSortKey(ctx, schemaToCreate)
if err != nil {
p.DropTable(ctx, schemaToCreate.Name, true)
return fmt.Errorf("failed to create sort key: %v", err)
}
}
return nil
}
func (p *Redshift) createSortKey(ctx context.Context, table *Table) error {
if table.TimestampColumn == "" {
return nil
}
quotedTableName := p.quotedTableName(table.Name)
statement := fmt.Sprintf(redshiftAlterSortKeyTemplate,
quotedTableName, p.quotedColumnName(table.TimestampColumn))
if _, err := p.txOrDb(ctx).ExecContext(ctx, statement); err != nil {
return errorj.AlterTableError.Wrap(err, "failed to set sort key").
WithProperty(errorj.DBInfo, &types.ErrorPayload{
Table: quotedTableName,
PrimaryKeys: table.GetPKFields(),
Statement: statement,
})
}
return nil
}
// redshiftColumnDDL returns column DDL (quoted column name, mapped sql type and 'not null' if pk field)
func redshiftColumnDDL(quotedName, name string, table *Table) string {
var columnConstaints string
var columnAttributes string
column := table.Columns[name]
sqlType := column.GetDDLType()
if _, ok := table.PKFields[name]; ok {
columnConstaints = " not null " + getDefaultValueStatement(sqlType)
if len(table.PKFields) == 1 {
columnAttributes = " DISTKEY "
}
}
return fmt.Sprintf(`%s %s%s%s`, quotedName, sqlType, columnAttributes, columnConstaints)
}