@@ -82,10 +82,6 @@ var stringColumns = lo.Keys(lo.PickBy(azureSynapseDataTypesMapToRudder, func(_,
82
82
return value == "string"
83
83
}))
84
84
85
- var azureSynapseColsWithVariableLength = lo .Keys (lo .PickBy (azureSynapseDataTypesMapToRudder , func (key , _ string ) bool {
86
- return key == "varchar" || key == "nvarchar" || key == "char" || key == "nchar"
87
- }))
88
-
89
85
type AzureSynapse struct {
90
86
db * sqlmw.DB
91
87
namespace string
@@ -214,67 +210,6 @@ func (*AzureSynapse) IsEmpty(_ context.Context, _ model.Warehouse) (empty bool,
214
210
return
215
211
}
216
212
217
- func (as * AzureSynapse ) createStagingTable (ctx context.Context , tableName string ) (string , error ) {
218
- stagingTableName := warehouseutils .StagingTableName (
219
- provider ,
220
- tableName ,
221
- tableNameLimit ,
222
- )
223
-
224
- cols := warehouseutils .SortColumnKeysFromColumnMap (as .uploader .GetTableSchemaInWarehouse (tableName ))
225
-
226
- varcharCols , err := as .getStringColumnsWithVariableLength (ctx , tableName )
227
- if err != nil {
228
- return "" , fmt .Errorf ("getting varchar columns info: %w" , err )
229
- }
230
-
231
- columnDefs := lo .Map (cols , func (name string , _ int ) string {
232
- if dataType , ok := varcharCols [name ]; ok {
233
- var targetType string
234
- // use varchar for char and nvarchar for nchar
235
- // this is because char and nchar have a max length of 8000 and 4000 respectively.
236
- // row size limit is 8060 in Azure Synapse. To fix this, we use varchar and nvarchar with max length
237
- switch dataType {
238
- case "char" :
239
- targetType = "varchar"
240
- case "nchar" :
241
- targetType = "nvarchar"
242
- default :
243
- targetType = string (dataType )
244
- }
245
- return fmt .Sprintf (`CAST('' AS %[1]s(max)) as %[2]s` , targetType , name )
246
- }
247
- return name
248
- })
249
-
250
- // The use of prepared statements for creating temporary tables is not suitable in this context.
251
- // Temporary tables in SQL Server have a limited scope and are automatically purged after the transaction commits.
252
- // Therefore, creating normal tables is chosen as an alternative.
253
- //
254
- // For more information on this behavior:
255
- // - See the discussion at https://github.com/denisenkom/go-mssqldb/issues/149 regarding prepared statements.
256
- // - Refer to Microsoft's documentation on temporary tables at
257
- // https://docs.microsoft.com/en-us/previous-versions/sql/sql-server-2008-r2/ms175528(v=sql.105)?redirectedfrom=MSDN.
258
- createStagingTableStmt := fmt .Sprintf (`
259
- SELECT TOP 0 %[3]s
260
- INTO
261
- %[1]s.%[2]s
262
- FROM
263
- %[1]s.%[4]s
264
- ` ,
265
- as .namespace ,
266
- stagingTableName ,
267
- strings .Join (columnDefs , ", " ),
268
- tableName ,
269
- )
270
-
271
- if _ , err := as .db .ExecContext (ctx , createStagingTableStmt ); err != nil {
272
- return "" , fmt .Errorf ("creating staging table: %w" , err )
273
- }
274
-
275
- return stagingTableName , nil
276
- }
277
-
278
213
func (as * AzureSynapse ) loadTable (
279
214
ctx context.Context ,
280
215
tableName string ,
@@ -300,10 +235,32 @@ func (as *AzureSynapse) loadTable(
300
235
misc .RemoveFilePaths (fileNames ... )
301
236
}()
302
237
238
+ stagingTableName := warehouseutils .StagingTableName (
239
+ provider ,
240
+ tableName ,
241
+ tableNameLimit ,
242
+ )
243
+
244
+ // The use of prepared statements for creating temporary tables is not suitable in this context.
245
+ // Temporary tables in SQL Server have a limited scope and are automatically purged after the transaction commits.
246
+ // Therefore, creating normal tables is chosen as an alternative.
247
+ //
248
+ // For more information on this behavior:
249
+ // - See the discussion at https://github.com/denisenkom/go-mssqldb/issues/149 regarding prepared statements.
250
+ // - Refer to Microsoft's documentation on temporary tables at
251
+ // https://docs.microsoft.com/en-us/previous-versions/sql/sql-server-2008-r2/ms175528(v=sql.105)?redirectedfrom=MSDN.
303
252
log .Debugw ("creating staging table" )
304
- stagingTableName , err := as .createStagingTable (ctx , tableName )
305
- if err != nil {
306
- return nil , "" , fmt .Errorf ("creating staging table: %w" , err )
253
+ createStagingTableStmt := fmt .Sprintf (`
254
+ SELECT
255
+ TOP 0 * INTO %[1]s.%[2]s
256
+ FROM
257
+ %[1]s.%[3]s;` ,
258
+ as .namespace ,
259
+ stagingTableName ,
260
+ tableName ,
261
+ )
262
+ if _ , err = as .db .ExecContext (ctx , createStagingTableStmt ); err != nil {
263
+ return nil , "" , fmt .Errorf ("creating temporary table: %w" , err )
307
264
}
308
265
309
266
if ! skipTempTableDelete {
@@ -395,46 +352,6 @@ func (as *AzureSynapse) loadTable(
395
352
}, stagingTableName , nil
396
353
}
397
354
398
- // dataType is the columnn data type in Azure Synapse
399
- type dataType string
400
-
401
- // getStringColumnsWithVariableLength returns the column name and data type for all columns that are of type varchar, nvarchar, char, or nchar
402
- func (as * AzureSynapse ) getStringColumnsWithVariableLength (ctx context.Context , tableName string ) (map [string ]dataType , error ) {
403
- dataTypes := "'" + strings .Join (azureSynapseColsWithVariableLength , "', '" ) + "'"
404
- query := fmt .Sprintf (`
405
- SELECT column_name, DATA_TYPE
406
- FROM INFORMATION_SCHEMA.COLUMNS
407
- WHERE TABLE_SCHEMA = @schema AND TABLE_NAME = @tableName AND DATA_TYPE IN (%s);
408
- ` ,
409
- dataTypes ,
410
- )
411
-
412
- rows , err := as .db .QueryContext (ctx , query ,
413
- sql .Named ("schema" , as .namespace ),
414
- sql .Named ("tableName" , tableName ),
415
- )
416
- if err != nil {
417
- return nil , fmt .Errorf ("querying string columns: %w" , err )
418
- }
419
- defer func () { _ = rows .Close () }()
420
-
421
- stringColumns := make (map [string ]dataType )
422
- for rows .Next () {
423
- var (
424
- columnName string
425
- dataType dataType
426
- )
427
- if err := rows .Scan (& columnName , & dataType ); err != nil {
428
- return nil , fmt .Errorf ("scanning string columns: %w" , err )
429
- }
430
- stringColumns [columnName ] = dataType
431
- }
432
- if err := rows .Err (); err != nil {
433
- return nil , fmt .Errorf ("iterating string columns: %w" , err )
434
- }
435
- return stringColumns , nil
436
- }
437
-
438
355
// getVarcharLengthMap retrieves the maximum allowed length for varchar columns in a given table.
439
356
// A `CHARACTER_MAXIMUM_LENGTH` of `-1` indicates that the column has the maximum possible length (i.e., `varchar(max)`).
440
357
func (as * AzureSynapse ) getVarcharLengthMap (ctx context.Context , tableName string ) (map [string ]int , error ) {
0 commit comments