Sql-server – SQL Server 2016 compress function details – external decompression

azure-data-factorycompressionsnowflakesql-server-2016

According to SQL Server 2016 docs COMPRESS and DECOMPRESS methods are just a blackbox – you put the data in and after some magic it gets compressed or decompressed. The problem is that I need to find a way on how to decompress this data outside SQL Server – using Snowflake or Azure Data Factory preferably. Does anyone please have a clue on how to approach this topic? This difficult in particular due to lack of detailed docs on the compression method/algorithm used.

Best Answer

According to SQL Server 2016 docs COMPRESS and DECOMPRESS methods are just a blackbox

I don't know what documentation page you are referring to but the COMPRESS reference states:

This function compresses the input expression, using the GZIP algorithm. The function returns a byte array of type varbinary(max).

You can then decompress using a GZip library in application code. PowerShell example, using .NET objects:

$connection = New-Object System.Data.SqlClient.SqlConnection("Data Source=.;Integrated Security=SSPI;Initial Catalog=tempdb")
$connection.Open()
$command = New-Object System.Data.SqlClient.SqlCommand("SELECT COMPRESS('this is gzip compressed data') AS GzipData;", $connection)
$gzipBytes = $command.ExecuteScalar()
$connection.Close()

$memoryStream = New-Object IO.MemoryStream
$memoryStream.Write($gzipBytes, 0, $gzipBytes.Length)
[void]$memoryStream.Seek(0,0)

$gzipStream = New-Object IO.Compression.GZipStream($memoryStream, [IO.Compression.CompressionMode]::Decompress)
$streamReader = New-Object IO.StreamReader($gzipStream)
$decompressedData = $streamReader.ReadToEnd()
Write-Host $decompressedData