Skip to main content
Creates an order-preserving composite binary key from multiple heterogeneous input columns. This function is essential for creating efficient sort keys that maintain the original sort order when compared lexicographically.

Syntax

Parameters

  • column1, column2, ..., columnN: One or more columns of any supported data type. The function accepts a variable number of arguments.

Return value

Returns a value of the VARBINARY type representing the composite sort key.

Supported data types

The following data types are supported: The following complex types are not supported and will return an error:
  • JSON
  • ARRAY
  • MAP
  • STRUCT
  • HLL
  • BITMAP
  • PERCENTILE

Encoding strategy

The function uses different encoding strategies for different data types to ensure that lexicographic comparison of the resulting binary keys preserves the original sort order:
  • Integer types: Big-endian byte order with sign bit flipped for signed types
  • Floating-point types: Custom encoding to ensure correct sort order
  • String types: Special encoding with 0x00 byte escaping and 0x00 0x00 terminator for non-last fields
  • Date/Time types: Internal integer representation encoded as integral value

NULL handling

  • Each column gets a NULL marker (0x00 for NULL, 0x01 for NOT NULL) for every row
  • NULL markers are added even for non-nullable columns to ensure consistent encoding
  • A separator byte (0x00) is used between columns (except after the last column)

Examples

Generated sort key column

JSON field extraction

Limitations

Data type restrictions

Complex types like JSON, ARRAY, MAP, and STRUCT cannot be directly encoded. Use JSON extraction functions to get primitive values:

Performance considerations

  • Each call to encode_sort_key requires encoding all input columns
  • Binary keys can be significantly larger than original data
  • Use generated columns to avoid repeated encoding

Key size limitations

  • Keep the number of columns reasonable (typically < 10)
  • Prefer shorter string columns when possible
  • Consider using hash functions for very long strings