/
snowflake.go
991 lines (873 loc) · 33.5 KB
/
snowflake.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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
package snowflake
import (
"bytes"
"context"
"database/sql"
"encoding/csv"
"fmt"
"sort"
"strings"
"time"
"github.com/rudderlabs/rudder-server/config"
"github.com/rudderlabs/rudder-server/utils/logger"
"github.com/rudderlabs/rudder-server/utils/misc"
"github.com/rudderlabs/rudder-server/warehouse/client"
warehouseutils "github.com/rudderlabs/rudder-server/warehouse/utils"
snowflake "github.com/snowflakedb/gosnowflake" // blank comment
)
const (
provider = warehouseutils.SNOWFLAKE
tableNameLimit = 127
)
var (
pkgLogger logger.Logger
enableDeleteByJobs bool
)
func Init() {
loadConfig()
pkgLogger = logger.NewLogger().Child("warehouse").Child("snowflake")
}
func loadConfig() {
config.RegisterBoolConfigVariable(false, &enableDeleteByJobs, true, "Warehouse.snowflake.enableDeleteByJobs")
}
type HandleT struct {
Db *sql.DB
Namespace string
CloudProvider string
ObjectStorage string
Warehouse warehouseutils.Warehouse
Uploader warehouseutils.UploaderI
ConnectTimeout time.Duration
}
// String constants for snowflake destination config
const (
StorageIntegration = "storageIntegration"
SFAccount = "account"
SFWarehouse = "warehouse"
SFDbName = "database"
SFUserName = "user"
SFPassword = "password"
)
var dataTypesMap = map[string]string{
"boolean": "boolean",
"int": "number",
"bigint": "number",
"float": "double precision",
"string": "varchar",
"datetime": "timestamp",
"json": "variant",
}
var dataTypesMapToRudder = map[string]string{
"NUMBER": "int",
"DECIMAL": "int",
"NUMERIC": "int",
"INT": "int",
"INTEGER": "int",
"BIGINT": "int",
"SMALLINT": "int",
"FLOAT": "float",
"FLOAT4": "float",
"FLOAT8": "float",
"DOUBLE": "float",
"REAL": "float",
"DOUBLE PRECISION": "float",
"BOOLEAN": "boolean",
"TEXT": "string",
"VARCHAR": "string",
"CHAR": "string",
"CHARACTER": "string",
"STRING": "string",
"BINARY": "string",
"VARBINARY": "string",
"TIMESTAMP_NTZ": "datetime",
"DATE": "datetime",
"DATETIME": "datetime",
"TIME": "datetime",
"TIMESTAMP": "datetime",
"TIMESTAMP_LTZ": "datetime",
"TIMESTAMP_TZ": "datetime",
"VARIANT": "json",
}
var primaryKeyMap = map[string]string{
usersTable: "ID",
identifiesTable: "ID",
discardsTable: "ROW_ID",
}
var partitionKeyMap = map[string]string{
usersTable: `"ID"`,
identifiesTable: `"ID"`,
discardsTable: `"ROW_ID", "COLUMN_NAME", "TABLE_NAME"`,
}
var (
usersTable = warehouseutils.ToProviderCase(warehouseutils.SNOWFLAKE, warehouseutils.UsersTable)
identifiesTable = warehouseutils.ToProviderCase(warehouseutils.SNOWFLAKE, warehouseutils.IdentifiesTable)
discardsTable = warehouseutils.ToProviderCase(warehouseutils.SNOWFLAKE, warehouseutils.DiscardsTable)
identityMergeRulesTable = warehouseutils.ToProviderCase(warehouseutils.SNOWFLAKE, warehouseutils.IdentityMergeRulesTable)
identityMappingsTable = warehouseutils.ToProviderCase(warehouseutils.SNOWFLAKE, warehouseutils.IdentityMappingsTable)
)
type tableLoadRespT struct {
dbHandle *sql.DB
stagingTable string
}
func ColumnsWithDataTypes(columns map[string]string, prefix string) string {
var arr []string
for name, dataType := range columns {
arr = append(arr, fmt.Sprintf(`"%s%s" %s`, prefix, name, dataTypesMap[dataType]))
}
return strings.Join(arr, ",")
}
// schemaIdentifier returns [DATABASE_NAME].[NAMESPACE] format to access the schema directly.
func (sf *HandleT) schemaIdentifier() string {
return fmt.Sprintf(`"%s"`,
sf.Namespace,
)
}
func (sf *HandleT) createTable(tableName string, columns map[string]string) (err error) {
schemaIdentifier := sf.schemaIdentifier()
sqlStatement := fmt.Sprintf(`CREATE TABLE IF NOT EXISTS %s."%s" ( %v )`, schemaIdentifier, tableName, ColumnsWithDataTypes(columns, ""))
pkgLogger.Infof("Creating table in snowflake for SF:%s : %v", sf.Warehouse.Destination.ID, sqlStatement)
_, err = sf.Db.Exec(sqlStatement)
return
}
func (sf *HandleT) tableExists(tableName string) (exists bool, err error) {
sqlStatement := fmt.Sprintf(`SELECT EXISTS ( SELECT 1
FROM information_schema.tables
WHERE table_schema = '%s'
AND table_name = '%s'
)`, sf.Namespace, tableName)
err = sf.Db.QueryRow(sqlStatement).Scan(&exists)
return
}
func (sf *HandleT) columnExists(columnName, tableName string) (exists bool, err error) {
sqlStatement := fmt.Sprintf(`SELECT EXISTS ( SELECT 1
FROM information_schema.columns
WHERE table_schema = '%s'
AND table_name = '%s'
AND column_name = '%s'
)`, sf.Namespace, tableName, columnName)
err = sf.Db.QueryRow(sqlStatement).Scan(&exists)
return
}
func (sf *HandleT) schemaExists() (exists bool, err error) {
sqlStatement := fmt.Sprintf("SELECT EXISTS ( SELECT 1 FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '%s' )", sf.Namespace)
r := sf.Db.QueryRow(sqlStatement)
err = r.Scan(&exists)
// ignore err if no results for query
if err == sql.ErrNoRows {
err = nil
}
return
}
func (sf *HandleT) createSchema() (err error) {
schemaIdentifier := sf.schemaIdentifier()
sqlStatement := fmt.Sprintf(`CREATE SCHEMA IF NOT EXISTS %s`, schemaIdentifier)
pkgLogger.Infof("SF: Creating schema name in snowflake for %s:%s : %v", sf.Warehouse.Namespace, sf.Warehouse.Destination.ID, sqlStatement)
_, err = sf.Db.Exec(sqlStatement)
return
}
func checkAndIgnoreAlreadyExistError(err error) bool {
if err != nil {
// TODO: throw error if column already exists but of different type
if e, ok := err.(*snowflake.SnowflakeError); ok {
if e.SQLState == "42601" {
return true
}
}
return false
}
return true
}
func (sf *HandleT) authString() string {
var auth string
if misc.IsConfiguredToUseRudderObjectStorage(sf.Warehouse.Destination.Config) || (sf.CloudProvider == "AWS" && warehouseutils.GetConfigValue(StorageIntegration, sf.Warehouse) == "") {
tempAccessKeyId, tempSecretAccessKey, token, _ := warehouseutils.GetTemporaryS3Cred(&sf.Warehouse.Destination)
auth = fmt.Sprintf(`CREDENTIALS = (AWS_KEY_ID='%s' AWS_SECRET_KEY='%s' AWS_TOKEN='%s')`, tempAccessKeyId, tempSecretAccessKey, token)
} else {
auth = fmt.Sprintf(`STORAGE_INTEGRATION = %s`, warehouseutils.GetConfigValue(StorageIntegration, sf.Warehouse))
}
return auth
}
func (sf *HandleT) DeleteBy(tableNames []string, params warehouseutils.DeleteByParams) (err error) {
pkgLogger.Infof("SF: Cleaning up the following tables in snowflake for SF:%s : %v", tableNames)
for _, tb := range tableNames {
sqlStatement := fmt.Sprintf(`
DELETE FROM
%[1]q.%[2]q
WHERE
context_sources_job_run_id <> '%[3]s' AND
context_sources_task_run_id <> '%[4]s' AND
context_source_id = '%[5]s' AND
received_at < '%[6]s';
`,
sf.Namespace,
tb,
params.JobRunId,
params.TaskRunId,
params.SourceId,
params.StartTime,
)
pkgLogger.Infof("SF: Deleting rows in table in snowflake for SF:%s", sf.Warehouse.Destination.ID)
pkgLogger.Debugf("SF: Executing the sql statement %v", sqlStatement)
if enableDeleteByJobs {
_, err = sf.Db.Exec(sqlStatement)
if err != nil {
pkgLogger.Errorf("Error %s", err)
return err
}
}
}
return nil
}
func (sf *HandleT) loadTable(tableName string, tableSchemaInUpload warehouseutils.TableSchemaT, dbHandle *sql.DB, skipClosingDBSession bool) (tableLoadResp tableLoadRespT, err error) {
pkgLogger.Infof("SF: Starting load for table:%s\n", tableName)
if dbHandle == nil {
dbHandle, err = Connect(sf.getConnectionCredentials(OptionalCredsT{schemaName: sf.Namespace}))
if err != nil {
pkgLogger.Errorf("SF: Error establishing connection for copying table:%s: %v\n", tableName, err)
return
}
}
tableLoadResp.dbHandle = dbHandle
if !skipClosingDBSession {
defer dbHandle.Close()
}
strKeys := warehouseutils.GetColumnsFromTableSchema(tableSchemaInUpload)
sort.Strings(strKeys)
sortedColumnNames := warehouseutils.JoinWithFormatting(strKeys, func(idx int, name string) string {
return fmt.Sprintf(`%q`, name)
}, ",")
schemaIdentifier := sf.schemaIdentifier()
stagingTableName := warehouseutils.StagingTableName(provider, tableName, tableNameLimit)
sqlStatement := fmt.Sprintf(`CREATE TEMPORARY TABLE %[1]s."%[2]s" LIKE %[1]s."%[3]s"`, schemaIdentifier, stagingTableName, tableName)
pkgLogger.Debugf("SF: Creating temporary table for table:%s at %s\n", tableName, sqlStatement)
_, err = dbHandle.Exec(sqlStatement)
if err != nil {
pkgLogger.Errorf("SF: Error creating temporary table for table:%s: %v\n", tableName, err)
return
}
tableLoadResp.stagingTable = stagingTableName
csvObjectLocation, err := sf.Uploader.GetSampleLoadFileLocation(tableName)
if err != nil {
return
}
loadFolder := warehouseutils.GetObjectFolder(sf.ObjectStorage, csvObjectLocation)
// Truncating the columns by default to avoid size limitation errors
// https://docs.snowflake.com/en/sql-reference/sql/copy-into-table.html#copy-options-copyoptions
sqlStatement = fmt.Sprintf(`COPY INTO %v(%v) FROM '%v' %s PATTERN = '.*\.csv\.gz'
FILE_FORMAT = ( TYPE = csv FIELD_OPTIONALLY_ENCLOSED_BY = '"' ESCAPE_UNENCLOSED_FIELD = NONE ) TRUNCATECOLUMNS = TRUE`, fmt.Sprintf(`%s."%s"`, schemaIdentifier, stagingTableName), sortedColumnNames, loadFolder, sf.authString())
sanitisedSQLStmt, regexErr := misc.ReplaceMultiRegex(sqlStatement, map[string]string{
"AWS_KEY_ID='[^']*'": "AWS_KEY_ID='***'",
"AWS_SECRET_KEY='[^']*'": "AWS_SECRET_KEY='***'",
"AWS_TOKEN='[^']*'": "AWS_TOKEN='***'",
})
if regexErr == nil {
pkgLogger.Infof("SF: Running COPY command for table:%s at %s\n", tableName, sanitisedSQLStmt)
}
_, err = dbHandle.Exec(sqlStatement)
if err != nil {
pkgLogger.Errorf("SF: Error running COPY command: %v\n", err)
return
}
primaryKey := "ID"
if column, ok := primaryKeyMap[tableName]; ok {
primaryKey = column
}
partitionKey := `"ID"`
if column, ok := partitionKeyMap[tableName]; ok {
partitionKey = column
}
stagingColumnNames := warehouseutils.JoinWithFormatting(strKeys, func(_ int, name string) string {
return fmt.Sprintf(`staging."%s"`, name)
}, ",")
columnsWithValues := warehouseutils.JoinWithFormatting(strKeys, func(_ int, name string) string {
return fmt.Sprintf(`original."%[1]s" = staging."%[1]s"`, name)
}, ",")
var additionalJoinClause string
if tableName == discardsTable {
additionalJoinClause = fmt.Sprintf(`AND original."%[1]s" = staging."%[1]s" AND original."%[2]s" = staging."%[2]s"`, "TABLE_NAME", "COLUMN_NAME")
}
keepLatestRecordOnDedup := sf.Uploader.ShouldOnDedupUseNewRecord()
if keepLatestRecordOnDedup {
sqlStatement = fmt.Sprintf(`MERGE INTO %[9]s."%[1]s" AS original
USING (
SELECT * FROM (
SELECT *, row_number() OVER (PARTITION BY %[8]s ORDER BY RECEIVED_AT DESC) AS _rudder_staging_row_number FROM %[9]s."%[2]s"
) AS q WHERE _rudder_staging_row_number = 1
) AS staging
ON (original."%[3]s" = staging."%[3]s" %[7]s)
WHEN MATCHED THEN
UPDATE SET %[6]s
WHEN NOT MATCHED THEN
INSERT (%[4]s) VALUES (%[5]s)`, tableName, stagingTableName, primaryKey, sortedColumnNames, stagingColumnNames, columnsWithValues, additionalJoinClause, partitionKey, schemaIdentifier)
} else {
sqlStatement = fmt.Sprintf(`MERGE INTO %[8]s."%[1]s" AS original
USING (
SELECT * FROM (
SELECT *, row_number() OVER (PARTITION BY %[7]s ORDER BY RECEIVED_AT DESC) AS _rudder_staging_row_number FROM %[8]s."%[2]s"
) AS q WHERE _rudder_staging_row_number = 1
) AS staging
ON (original."%[3]s" = staging."%[3]s" %[6]s)
WHEN NOT MATCHED THEN
INSERT (%[4]s) VALUES (%[5]s)`, tableName, stagingTableName, primaryKey, sortedColumnNames, stagingColumnNames, additionalJoinClause, partitionKey, schemaIdentifier)
}
pkgLogger.Infof("SF: Dedup records for table:%s using staging table: %s\n", tableName, sqlStatement)
_, err = dbHandle.Exec(sqlStatement)
if err != nil {
pkgLogger.Errorf("SF: Error running MERGE for dedup: %v\n", err)
return
}
pkgLogger.Infof("SF: Complete load for table:%s\n", tableName)
return
}
func (sf *HandleT) LoadIdentityMergeRulesTable() (err error) {
pkgLogger.Infof("SF: Starting load for table:%s\n", identityMergeRulesTable)
pkgLogger.Infof("SF: Fetching load file location for %s", identityMergeRulesTable)
var loadFile warehouseutils.LoadFileT
loadFile, err = sf.Uploader.GetSingleLoadFile(identityMergeRulesTable)
if err != nil {
return err
}
dbHandle, err := Connect(sf.getConnectionCredentials(OptionalCredsT{schemaName: sf.Namespace}))
if err != nil {
pkgLogger.Errorf("SF: Error establishing connection for copying table:%s: %v\n", identityMergeRulesTable, err)
return
}
sortedColumnNames := strings.Join([]string{"MERGE_PROPERTY_1_TYPE", "MERGE_PROPERTY_1_VALUE", "MERGE_PROPERTY_2_TYPE", "MERGE_PROPERTY_2_VALUE"}, ",")
loadLocation := warehouseutils.GetObjectLocation(sf.ObjectStorage, loadFile.Location)
schemaIdentifier := sf.schemaIdentifier()
sqlStatement := fmt.Sprintf(`COPY INTO %v(%v) FROM '%v' %s PATTERN = '.*\.csv\.gz'
FILE_FORMAT = ( TYPE = csv FIELD_OPTIONALLY_ENCLOSED_BY = '"' ESCAPE_UNENCLOSED_FIELD = NONE ) TRUNCATECOLUMNS = TRUE`, fmt.Sprintf(`%s."%s"`, schemaIdentifier, identityMergeRulesTable), sortedColumnNames, loadLocation, sf.authString())
sanitisedSQLStmt, regexErr := misc.ReplaceMultiRegex(sqlStatement, map[string]string{
"AWS_KEY_ID='[^']*'": "AWS_KEY_ID='***'",
"AWS_SECRET_KEY='[^']*'": "AWS_SECRET_KEY='***'",
"AWS_TOKEN='[^']*'": "AWS_TOKEN='***'",
})
if regexErr == nil {
pkgLogger.Infof("SF: Dedup records for table:%s using staging table: %s\n", identityMergeRulesTable, sanitisedSQLStmt)
}
_, err = dbHandle.Exec(sqlStatement)
if err != nil {
pkgLogger.Errorf("SF: Error running MERGE for dedup: %v\n", err)
return
}
pkgLogger.Infof("SF: Complete load for table:%s\n", identityMergeRulesTable)
return
}
func (sf *HandleT) LoadIdentityMappingsTable() (err error) {
pkgLogger.Infof("SF: Starting load for table:%s\n", identityMappingsTable)
pkgLogger.Infof("SF: Fetching load file location for %s", identityMappingsTable)
var loadFile warehouseutils.LoadFileT
loadFile, err = sf.Uploader.GetSingleLoadFile(identityMappingsTable)
if err != nil {
return err
}
dbHandle, err := Connect(sf.getConnectionCredentials(OptionalCredsT{schemaName: sf.Namespace}))
if err != nil {
pkgLogger.Errorf("SF: Error establishing connection for copying table:%s: %v\n", identityMappingsTable, err)
return
}
schemaIdentifier := sf.schemaIdentifier()
stagingTableName := warehouseutils.StagingTableName(provider, identityMappingsTable, tableNameLimit)
sqlStatement := fmt.Sprintf(`CREATE TEMPORARY TABLE %[1]s."%[2]s" LIKE %[1]s."%[3]s"`, schemaIdentifier, stagingTableName, identityMappingsTable)
pkgLogger.Infof("SF: Creating temporary table for table:%s at %s\n", identityMappingsTable, sqlStatement)
_, err = dbHandle.Exec(sqlStatement)
if err != nil {
pkgLogger.Errorf("SF: Error creating temporary table for table:%s: %v\n", identityMappingsTable, err)
return
}
sqlStatement = fmt.Sprintf(`ALTER TABLE %s."%s" ADD COLUMN "ID" int AUTOINCREMENT start 1 increment 1`, schemaIdentifier, stagingTableName)
pkgLogger.Infof("SF: Adding autoincrement column for table:%s at %s\n", stagingTableName, sqlStatement)
_, err = dbHandle.Exec(sqlStatement)
if err != nil && !checkAndIgnoreAlreadyExistError(err) {
pkgLogger.Errorf("SF: Error adding autoincrement column for table:%s: %v\n", stagingTableName, err)
return
}
loadLocation := warehouseutils.GetObjectLocation(sf.ObjectStorage, loadFile.Location)
sqlStatement = fmt.Sprintf(`COPY INTO %v("MERGE_PROPERTY_TYPE", "MERGE_PROPERTY_VALUE", "RUDDER_ID", "UPDATED_AT") FROM '%v' %s PATTERN = '.*\.csv\.gz'
FILE_FORMAT = ( TYPE = csv FIELD_OPTIONALLY_ENCLOSED_BY = '"' ESCAPE_UNENCLOSED_FIELD = NONE ) TRUNCATECOLUMNS = TRUE`, fmt.Sprintf(`%s."%s"`, schemaIdentifier, stagingTableName), loadLocation, sf.authString())
pkgLogger.Infof("SF: Dedup records for table:%s using staging table: %s\n", identityMappingsTable, sqlStatement)
_, err = dbHandle.Exec(sqlStatement)
if err != nil {
pkgLogger.Errorf("SF: Error running MERGE for dedup: %v\n", err)
return
}
sqlStatement = fmt.Sprintf(`MERGE INTO %[3]s."%[1]s" AS original
USING (
SELECT * FROM (
SELECT *, row_number() OVER (PARTITION BY "MERGE_PROPERTY_TYPE", "MERGE_PROPERTY_VALUE" ORDER BY "ID" DESC) AS _rudder_staging_row_number FROM %[3]s."%[2]s"
) AS q WHERE _rudder_staging_row_number = 1
) AS staging
ON (original."MERGE_PROPERTY_TYPE" = staging."MERGE_PROPERTY_TYPE" AND original."MERGE_PROPERTY_VALUE" = staging."MERGE_PROPERTY_VALUE")
WHEN MATCHED THEN
UPDATE SET original."RUDDER_ID" = staging."RUDDER_ID", original."UPDATED_AT" = staging."UPDATED_AT"
WHEN NOT MATCHED THEN
INSERT ("MERGE_PROPERTY_TYPE", "MERGE_PROPERTY_VALUE", "RUDDER_ID", "UPDATED_AT") VALUES (staging."MERGE_PROPERTY_TYPE", staging."MERGE_PROPERTY_VALUE", staging."RUDDER_ID", staging."UPDATED_AT")`, identityMappingsTable, stagingTableName, schemaIdentifier)
pkgLogger.Infof("SF: Dedup records for table:%s using staging table: %s\n", identityMappingsTable, sqlStatement)
_, err = dbHandle.Exec(sqlStatement)
if err != nil {
pkgLogger.Errorf("SF: Error running MERGE for dedup: %v\n", err)
return
}
pkgLogger.Infof("SF: Complete load for table:%s\n", identityMappingsTable)
return
}
func (sf *HandleT) loadUserTables() (errorMap map[string]error) {
identifyColMap := sf.Uploader.GetTableSchemaInUpload(identifiesTable)
if len(identifyColMap) == 0 {
return errorMap
}
errorMap = map[string]error{identifiesTable: nil}
pkgLogger.Infof("SF: Starting load for identifies and users tables\n")
resp, err := sf.loadTable(identifiesTable, sf.Uploader.GetTableSchemaInUpload(identifiesTable), nil, true)
if err != nil {
errorMap[identifiesTable] = err
return errorMap
}
defer resp.dbHandle.Close()
if len(sf.Uploader.GetTableSchemaInUpload(usersTable)) == 0 {
return errorMap
}
errorMap[usersTable] = nil
userColMap := sf.Uploader.GetTableSchemaInWarehouse(usersTable)
var userColNames, firstValProps, identifyColNames []string
for colName := range userColMap {
if colName == "ID" {
continue
}
userColNames = append(userColNames, fmt.Sprintf(`"%s"`, colName))
if _, ok := identifyColMap[colName]; ok {
identifyColNames = append(identifyColNames, fmt.Sprintf(`"%s"`, colName))
} else {
// This is to handle cases when column in users table not present in identities table
identifyColNames = append(identifyColNames, fmt.Sprintf(`NULL as "%s"`, colName))
}
firstValProps = append(firstValProps, fmt.Sprintf(`FIRST_VALUE("%[1]s" IGNORE NULLS) OVER (PARTITION BY ID ORDER BY RECEIVED_AT DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS "%[1]s"`, colName))
}
schemaIdentifier := sf.schemaIdentifier()
stagingTableName := warehouseutils.StagingTableName(provider, usersTable, tableNameLimit)
sqlStatement := fmt.Sprintf(`CREATE TEMPORARY TABLE %[1]s."%[2]s" AS (SELECT DISTINCT * FROM
(
SELECT
"ID", %[3]s
FROM (
(
SELECT "ID", %[6]s FROM %[1]s."%[4]s" WHERE "ID" in (SELECT "USER_ID" FROM %[1]s."%[5]s" WHERE "USER_ID" IS NOT NULL)
) UNION
(
SELECT "USER_ID", %[7]s FROM %[1]s."%[5]s" WHERE "USER_ID" IS NOT NULL
)
)
)
)`,
schemaIdentifier, // 1
stagingTableName, // 2
strings.Join(firstValProps, ","), // 3
usersTable, // 4
resp.stagingTable, // 5
strings.Join(userColNames, ","), // 6
strings.Join(identifyColNames, ","), // 7
)
pkgLogger.Infof("SF: Creating staging table for users: %s\n", sqlStatement)
_, err = resp.dbHandle.Exec(sqlStatement)
if err != nil {
pkgLogger.Errorf("SF: Error creating temporary table for table:%s: %v\n", usersTable, err)
errorMap[usersTable] = err
return errorMap
}
primaryKey := `"ID"`
columnNames := append([]string{`"ID"`}, userColNames...)
columnNamesStr := strings.Join(columnNames, ",")
var columnsWithValues, stagingColumnValues string
for idx, colName := range columnNames {
columnsWithValues += fmt.Sprintf(`original.%[1]s = staging.%[1]s`, colName)
stagingColumnValues += fmt.Sprintf(`staging.%s`, colName)
if idx != len(columnNames)-1 {
columnsWithValues += `,`
stagingColumnValues += `,`
}
}
sqlStatement = fmt.Sprintf(`MERGE INTO %[7]s."%[1]s" AS original
USING (
SELECT %[3]s FROM %[7]s."%[2]s"
) AS staging
ON (original.%[4]s = staging.%[4]s)
WHEN MATCHED THEN
UPDATE SET %[5]s
WHEN NOT MATCHED THEN
INSERT (%[3]s) VALUES (%[6]s)`, usersTable, stagingTableName, columnNamesStr, primaryKey, columnsWithValues, stagingColumnValues, schemaIdentifier)
pkgLogger.Infof("SF: Dedup records for table:%s using staging table: %s\n", usersTable, sqlStatement)
_, err = resp.dbHandle.Exec(sqlStatement)
if err != nil {
pkgLogger.Errorf("SF: Error running MERGE for dedup: %v\n", err)
errorMap[usersTable] = err
return errorMap
}
pkgLogger.Infof("SF: Complete load for table:%s", usersTable)
return errorMap
}
type SnowflakeCredentialsT struct {
Account string
WHName string
DBName string
Username string
Password string
schemaName string
timeout time.Duration
}
func Connect(cred SnowflakeCredentialsT) (*sql.DB, error) {
urlConfig := snowflake.Config{
Account: cred.Account,
User: cred.Username,
Password: cred.Password,
Database: cred.DBName,
Schema: cred.schemaName,
Warehouse: cred.WHName,
Application: "Rudderstack",
}
if cred.timeout > 0 {
urlConfig.LoginTimeout = cred.timeout
}
var err error
dsn, err := snowflake.DSN(&urlConfig)
if err != nil {
return nil, fmt.Errorf("SF: Error costructing DSN to connect : (%v)", err)
}
var db *sql.DB
if db, err = sql.Open("snowflake", dsn); err != nil {
return nil, fmt.Errorf("SF: snowflake connect error : (%v)", err)
}
alterStatement := `ALTER SESSION SET ABORT_DETACHED_QUERY=TRUE`
pkgLogger.Infof("SF: Altering session with abort_detached_query for snowflake: %v", alterStatement)
_, err = db.Exec(alterStatement)
if err != nil {
return nil, fmt.Errorf("SF: snowflake alter session error : (%v)", err)
}
return db, nil
}
func (sf *HandleT) CreateSchema() (err error) {
var schemaExists bool
schemaIdentifier := sf.schemaIdentifier()
schemaExists, err = sf.schemaExists()
if err != nil {
pkgLogger.Errorf("SF: Error checking if schema: %s exists: %v", schemaIdentifier, err)
return err
}
if schemaExists {
pkgLogger.Infof("SF: Skipping creating schema: %s since it already exists", schemaIdentifier)
return
}
return sf.createSchema()
}
func (sf *HandleT) CreateTable(tableName string, columnMap map[string]string) (err error) {
return sf.createTable(tableName, columnMap)
}
func (sf *HandleT) DropTable(tableName string) (err error) {
schemaIdentifier := sf.schemaIdentifier()
sqlStatement := fmt.Sprintf(`DROP TABLE %[1]s."%[2]s"`, schemaIdentifier, tableName)
pkgLogger.Infof("SF: Dropping table in snowflake for SF:%s : %v", sf.Warehouse.Destination.ID, sqlStatement)
_, err = sf.Db.Exec(sqlStatement)
return
}
func (sf *HandleT) AddColumns(tableName string, columnsInfo []warehouseutils.ColumnInfo) (err error) {
var (
query string
queryBuilder strings.Builder
schemaIdentifier string
)
schemaIdentifier = sf.schemaIdentifier()
queryBuilder.WriteString(fmt.Sprintf(`
ALTER TABLE
%s.%q
ADD COLUMN`,
schemaIdentifier,
tableName,
))
for _, columnInfo := range columnsInfo {
queryBuilder.WriteString(fmt.Sprintf(` %q %s,`, columnInfo.Name, dataTypesMap[columnInfo.Type]))
}
query = strings.TrimSuffix(queryBuilder.String(), ",")
query += ";"
pkgLogger.Infof("SF: Adding columns for destinationID: %s, tableName: %s with query: %v", sf.Warehouse.Destination.ID, tableName, query)
_, err = sf.Db.Exec(query)
// Handle error in case of single column
if len(columnsInfo) == 1 {
if err != nil {
if checkAndIgnoreAlreadyExistError(err) {
pkgLogger.Infof("SF: Column %s already exists on %s.%s \nResponse: %v", columnsInfo[0].Name, schemaIdentifier, tableName, err)
err = nil
}
}
}
return
}
func (*HandleT) AlterColumn(_, _, _ string) (err error) {
return
}
// DownloadIdentityRules gets distinct combinations of anonymous_id, user_id from tables in warehouse
func (sf *HandleT) DownloadIdentityRules(gzWriter *misc.GZipWriter) (err error) {
getFromTable := func(tableName string) (err error) {
var exists bool
exists, err = sf.tableExists(tableName)
if err != nil || !exists {
return
}
schemaIdentifier := sf.schemaIdentifier()
sqlStatement := fmt.Sprintf(`SELECT count(*) FROM %s."%s"`, schemaIdentifier, tableName)
var totalRows int64
err = sf.Db.QueryRow(sqlStatement).Scan(&totalRows)
if err != nil {
return
}
// check if table in warehouse has anonymous_id and user_id and construct accordingly
hasAnonymousID, err := sf.columnExists("ANONYMOUS_ID", tableName)
if err != nil {
return
}
hasUserID, err := sf.columnExists("USER_ID", tableName)
if err != nil {
return
}
var toSelectFields string
if hasAnonymousID && hasUserID {
toSelectFields = `"ANONYMOUS_ID", "USER_ID"`
} else if hasAnonymousID {
toSelectFields = `"ANONYMOUS_ID", NULL AS "USER_ID"`
} else if hasUserID {
toSelectFields = `NULL AS "ANONYMOUS_ID", "USER_ID"`
} else {
pkgLogger.Infof("SF: ANONYMOUS_ID, USER_ID columns not present in table: %s", tableName)
return nil
}
batchSize := int64(10000)
var offset int64
for {
// TODO: Handle case for missing anonymous_id, user_id columns
sqlStatement = fmt.Sprintf(`SELECT DISTINCT %s FROM %s."%s" LIMIT %d OFFSET %d`, toSelectFields, schemaIdentifier, tableName, batchSize, offset)
pkgLogger.Infof("SF: Downloading distinct combinations of anonymous_id, user_id: %s, totalRows: %d", sqlStatement, totalRows)
var rows *sql.Rows
rows, err = sf.Db.Query(sqlStatement)
if err != nil {
return
}
for rows.Next() {
var buff bytes.Buffer
csvWriter := csv.NewWriter(&buff)
var csvRow []string
var anonymousID, userID sql.NullString
err = rows.Scan(&anonymousID, &userID)
if err != nil {
return
}
if !anonymousID.Valid && !userID.Valid {
continue
}
// avoid setting null merge_property_1 to avoid not null constraint in local postgres
if anonymousID.Valid {
csvRow = append(csvRow, "anonymous_id", anonymousID.String, "user_id", userID.String)
} else {
csvRow = append(csvRow, "user_id", userID.String, "anonymous_id", anonymousID.String)
}
csvWriter.Write(csvRow)
csvWriter.Flush()
gzWriter.WriteGZ(buff.String())
}
offset += batchSize
if offset >= totalRows {
break
}
}
return
}
tables := []string{"TRACKS", "PAGES", "SCREENS", "IDENTIFIES", "ALIASES"}
for _, table := range tables {
err = getFromTable(table)
if err != nil {
return
}
}
return nil
}
func (*HandleT) CrashRecover(_ warehouseutils.Warehouse) (err error) {
return
}
func (sf *HandleT) IsEmpty(warehouse warehouseutils.Warehouse) (empty bool, err error) {
empty = true
sf.Warehouse = warehouse
sf.Namespace = warehouse.Namespace
sf.Db, err = Connect(sf.getConnectionCredentials(OptionalCredsT{}))
if err != nil {
return
}
defer sf.Db.Close()
tables := []string{"TRACKS", "PAGES", "SCREENS", "IDENTIFIES", "ALIASES"}
for _, tableName := range tables {
var exists bool
exists, err = sf.tableExists(tableName)
if err != nil {
return
}
if !exists {
continue
}
schemaIdentifier := sf.schemaIdentifier()
sqlStatement := fmt.Sprintf(`SELECT COUNT(*) FROM %s."%s"`, schemaIdentifier, tableName)
var count int64
err = sf.Db.QueryRow(sqlStatement).Scan(&count)
if err != nil {
return
}
if count > 0 {
empty = false
return
}
}
return
}
type OptionalCredsT struct {
schemaName string
}
func (sf *HandleT) getConnectionCredentials(opts OptionalCredsT) SnowflakeCredentialsT {
return SnowflakeCredentialsT{
Account: warehouseutils.GetConfigValue(SFAccount, sf.Warehouse),
WHName: warehouseutils.GetConfigValue(SFWarehouse, sf.Warehouse),
DBName: warehouseutils.GetConfigValue(SFDbName, sf.Warehouse),
Username: warehouseutils.GetConfigValue(SFUserName, sf.Warehouse),
Password: warehouseutils.GetConfigValue(SFPassword, sf.Warehouse),
schemaName: opts.schemaName,
timeout: sf.ConnectTimeout,
}
}
func (sf *HandleT) Setup(warehouse warehouseutils.Warehouse, uploader warehouseutils.UploaderI) (err error) {
sf.Warehouse = warehouse
sf.Namespace = warehouse.Namespace
sf.CloudProvider = warehouseutils.SnowflakeCloudProvider(warehouse.Destination.Config)
sf.Uploader = uploader
sf.ObjectStorage = warehouseutils.ObjectStorageType(warehouseutils.SNOWFLAKE, warehouse.Destination.Config, sf.Uploader.UseRudderStorage())
sf.Db, err = Connect(sf.getConnectionCredentials(OptionalCredsT{}))
return err
}
func (sf *HandleT) TestConnection(warehouse warehouseutils.Warehouse) (err error) {
sf.Warehouse = warehouse
sf.Db, err = Connect(sf.getConnectionCredentials(OptionalCredsT{}))
if err != nil {
return
}
defer sf.Db.Close()
ctx, cancel := context.WithTimeout(context.TODO(), sf.ConnectTimeout)
defer cancel()
err = sf.Db.PingContext(ctx)
if err == context.DeadlineExceeded {
return fmt.Errorf("connection testing timed out after %d sec", sf.ConnectTimeout/time.Second)
}
if err != nil {
return err
}
return nil
}
// FetchSchema queries snowflake and returns the schema associated with provided namespace
func (sf *HandleT) FetchSchema(warehouse warehouseutils.Warehouse) (schema, unrecognizedSchema warehouseutils.SchemaT, err error) {
sf.Warehouse = warehouse
sf.Namespace = warehouse.Namespace
dbHandle, err := Connect(sf.getConnectionCredentials(OptionalCredsT{}))
if err != nil {
return
}
defer dbHandle.Close()
schema = make(warehouseutils.SchemaT)
unrecognizedSchema = make(warehouseutils.SchemaT)
sqlStatement := fmt.Sprintf(`
SELECT
table_name,
column_name,
data_type
FROM
INFORMATION_SCHEMA.COLUMNS
WHERE
table_schema = '%s'
`,
sf.Namespace,
)
rows, err := dbHandle.Query(sqlStatement)
if err != nil && err != sql.ErrNoRows {
pkgLogger.Errorf("SF: Error in fetching schema from snowflake destination:%v, query: %v", sf.Warehouse.Destination.ID, sqlStatement)
return
}
if err == sql.ErrNoRows {
pkgLogger.Infof("SF: No rows, while fetching schema from destination:%v, query: %v", sf.Warehouse.Identifier, sqlStatement)
return schema, unrecognizedSchema, nil
}
defer rows.Close()
for rows.Next() {
var tName, cName, cType string
err = rows.Scan(&tName, &cName, &cType)
if err != nil {
pkgLogger.Errorf("SF: Error in processing fetched schema from snowflake destination:%v", sf.Warehouse.Destination.ID)
return
}
if _, ok := schema[tName]; !ok {
schema[tName] = make(map[string]string)
}
if datatype, ok := dataTypesMapToRudder[cType]; ok {
schema[tName][cName] = datatype
} else {
if _, ok := unrecognizedSchema[tName]; !ok {
unrecognizedSchema[tName] = make(map[string]string)
}
unrecognizedSchema[tName][cName] = warehouseutils.MISSING_DATATYPE
warehouseutils.WHCounterStat(warehouseutils.RUDDER_MISSING_DATATYPE, &sf.Warehouse, warehouseutils.Tag{Name: "datatype", Value: cType}).Count(1)
}
}
return
}
func (sf *HandleT) Cleanup() {
if sf.Db != nil {
sf.Db.Close()
}
}
func (sf *HandleT) LoadUserTables() map[string]error {
return sf.loadUserTables()
}
func (sf *HandleT) LoadTable(tableName string) error {
_, err := sf.loadTable(tableName, sf.Uploader.GetTableSchemaInUpload(tableName), nil, false)
return err
}
func (sf *HandleT) GetTotalCountInTable(ctx context.Context, tableName string) (total int64, err error) {
schemaIdentifier := sf.schemaIdentifier()
sqlStatement := fmt.Sprintf(`SELECT count(*) FROM %[1]s."%[2]s"`, schemaIdentifier, tableName)
err = sf.Db.QueryRowContext(ctx, sqlStatement).Scan(&total)
if err != nil {
pkgLogger.Errorf(`SF: Error getting total count in table %s:%s`, schemaIdentifier, tableName)
}
return
}
func (sf *HandleT) Connect(warehouse warehouseutils.Warehouse) (client.Client, error) {
sf.Warehouse = warehouse
sf.Namespace = warehouse.Namespace
sf.CloudProvider = warehouseutils.SnowflakeCloudProvider(warehouse.Destination.Config)
sf.ObjectStorage = warehouseutils.ObjectStorageType(
warehouseutils.SNOWFLAKE,
warehouse.Destination.Config,
misc.IsConfiguredToUseRudderObjectStorage(sf.Warehouse.Destination.Config),
)
dbHandle, err := Connect(sf.getConnectionCredentials(OptionalCredsT{}))
if err != nil {
return client.Client{}, err
}
return client.Client{Type: client.SQLClient, SQL: dbHandle}, err
}
func (sf *HandleT) LoadTestTable(location, tableName string, _ map[string]interface{}, _ string) (err error) {
loadFolder := warehouseutils.GetObjectFolder(sf.ObjectStorage, location)
schemaIdentifier := sf.schemaIdentifier()
sqlStatement := fmt.Sprintf(`COPY INTO %v(%v) FROM '%v' %s PATTERN = '.*\.csv\.gz'
FILE_FORMAT = ( TYPE = csv FIELD_OPTIONALLY_ENCLOSED_BY = '"' ESCAPE_UNENCLOSED_FIELD = NONE ) TRUNCATECOLUMNS = TRUE`,
fmt.Sprintf(`%s."%s"`, schemaIdentifier, tableName),
fmt.Sprintf(`"%s", "%s"`, "id", "val"),
loadFolder,
sf.authString(),
)
_, err = sf.Db.Exec(sqlStatement)
return
}
func (sf *HandleT) SetConnectionTimeout(timeout time.Duration) {
sf.ConnectTimeout = timeout
}