Compression functions in GoogleSQL

GoogleSQL for Spanner supports compression functions.

Compression functions compress or decompress bytes or string values using theZstandard (Zstd) lossless data compression algorithm.

Function list

NameSummary
ZSTD_COMPRESS CompressesSTRING orBYTES input intoBYTES output using theZstandard (Zstd) lossless data compression algorithm.
ZSTD_DECOMPRESS_TO_BYTES DecompressesBYTES input intoBYTES output using theZstandard (Zstd) lossless data compression algorithm.
ZSTD_DECOMPRESS_TO_STRING DecompressBYTES input intoSTRING output using theZstandard (Zstd) lossless data compression algorithm.

ZSTD_COMPRESS

ZSTD_COMPRESS(string_or_bytes_value,level=>3)

Description

CompressesSTRING orBYTES input intoBYTES output using the Zstandard (Zstd) losslessdata compression algorithm.

Arguments:

  • string_or_bytes_value: The SQL value to compress.
  • level: Optional. The Zstd compression level. The default is 3. You can setlevel to an integer value between -5 and 22. A higher value results in a better compression ratio at the cost of slower performance.

Return type

BYTES: Base64-encoded bytes.

Example

SELECTZSTD_COMPRESS('string_value')ASresult;/*------------------------------+ | result                       | +------------------------------+ | KLUv/SAMYQAAc3RyaW5nX3ZhbHVl | +------------------------------*/
SELECTZSTD_COMPRESS(b'bytes_value',level=>1);/*------------------------------+ | result                       | +------------------------------+ | KLUv/SALWQAAYnl0ZXNfdmFsdWU= | +------------------------------*/

This function returnsNULL if the input isNULL:

SELECTZSTD_COMPRESS(NULL)ASresult;/*------------+ | result     | +------------+ | NULL       | +------------*/

ZSTD_DECOMPRESS_TO_BYTES

ZSTD_DECOMPRESS_TO_BYTES(bytes_value,size_limit=>1024*1024*1024)

Description

DecompressesBYTES input intoBYTES using theZstandard (Zstd) lossless data compression algorithm.

Arguments:

  • bytes_value: The bytes to decompress.
  • size_limit: Optional. The size limit of returned decompressed bytes. The default value is one GiB. You can set this limit to a lower value to minimize the risk ofZSTD_DECOMPRESS_TO_BYTES causing server memory issues.

Return type

BYTES: Base64-encoded bytes.

Example

SELECTZSTD_DECOMPRESS_TO_BYTES(ZSTD_COMPRESS(b'bytes'))ASresult;/*------------+ | result     | +------------+ | Ynl0ZXM=   | +------------*/

If compressed bytes exceed thesize_limit value,ZSTD_DECOMPRESS_TO_BYTESreturns an error:

SELECTZSTD_DECOMPRESS_TO_BYTES(ZSTD_COMPRESS(b'bytes'),size_limit=>1)ASresult;Statementfailed:ZSTDoutputistoolarge:(5bytes) >limit(1bytes)

This function returnsNULL if the input isNULL:

SELECTZSTD_DECOMPRESS_TO_BYTES(NULL)ASresult;/*------------+ | result     | +------------+ | NULL       | +------------*/

ZSTD_DECOMPRESS_TO_STRING

ZSTD_DECOMPRESS_TO_STRING(bytes_value,size_limit=>1024*1024*1024)

Description

DecompressBYTES input intoSTRING output using theZstandard (Zstd) lossless data compression algorithm.

Arguments:

  • bytes_value: The bytes to decompress.
  • size_limit: Optional. The size limit of returned decompressed string. The default value is one GiB. You can set this limit to a lower value to minimize the risk ofZSTD_DECOMPRESS_TO_STRING causing server memory issues.

Return type

STRING

Example

SELECTZSTD_DECOMPRESS_TO_STRING(ZSTD_COMPRESS('zstd'))ASresult;/*----------+ | result   | +----------+ | "zstd"   | +----------*/

If compressed bytes exceed thesize_limit value,ZSTD_DECOMPRESS_TO_STRINGreturns an error:

SELECTZSTD_DECOMPRESS_TO_STRING(ZSTD_COMPRESS('zstd'),size_limit=>1)ASresult;Statementfailed:ZSTDoutputistoolarge:(4bytes) >limit(1bytes)

This function returnsNULL if the input isNULL:

SELECTZSTD_DECOMPRESS_TO_STRING(NULL)ASresult;/*------------+ | result     | +------------+ | NULL       | +------------*/

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2026-02-19 UTC.