View a markdown version of this page

Sort key, binning, and hash functions - Amazon Athena

Sort key, binning, and hash functions

Use these functions to compute values that help you sort and group rows, such as one sort key for several columns, the bounds of equal-width bins, or a hash. For more information about cryptographic hash functions, such as sha256, see Blob and bitstring functions in DuckDB.

create_sort_key function

Encodes one or more values, each with its own sort order, into one key. Comparing two keys with < or >, or sorting by the key, gives the same order as an ORDER BY clause with the same values and sort orders.

Use create_sort_key when you need the order of several columns as one value, for example as the second argument of arg_min or arg_max.

Syntax

create_sort_key(value, sort_order [, value, sort_order, ...])

Arguments

The create_sort_key function takes the following arguments:

value

A value to sort by, of any data type.

sort_order

The sort direction and the position of NULL values for the value before it. It must be a constant VARCHAR value: ASC or DESC, followed by NULLS FIRST or NULLS LAST, for example 'DESC NULLS LAST'. Case does not matter.

Return type

BLOB. When the key is short enough to fit in 8 bytes, the result is a BIGINT instead. For example, a single INTEGER, DATE, or BOOLEAN value gives a BIGINT, but a single BIGINT, DOUBLE, or TIMESTAMP value gives a BLOB. A key that includes a VARCHAR value is also a BLOB.

Examples

The following SQL query compares the keys for two strings, first in ascending order and then in descending order.

SELECT create_sort_key( 'apple', 'ASC NULLS LAST' ) < create_sort_key('banana', 'ASC NULLS LAST') AS ascending, create_sort_key( 'apple', 'DESC NULLS LAST' ) < create_sort_key('banana', 'DESC NULLS LAST') AS descending

The query returns the following result.

ascending descending

true

false

The following SQL query sorts rows by score from highest to lowest, with NULL scores last, and then by name.

SELECT name, score FROM (VALUES ('carol', 7), ('alice', 9), ('bob', 7), ('dave', NULL) ) AS t(name, score) ORDER BY create_sort_key( score, 'DESC NULLS LAST', name, 'ASC NULLS LAST' )

The query returns the following results.

name score

alice

9

bob

7

carol

7

dave

NULL

The following SQL query uses a key as the second argument of arg_min to return the name with the highest score. When two names have the same score, arg_min returns the one that comes first in alphabetical order.

SELECT arg_min( name, create_sort_key( score, 'DESC NULLS LAST', name, 'ASC NULLS LAST' ) ) AS result FROM (VALUES ('carol', 7), ('alice', 9), ('bob', 9), ('dave', NULL) ) AS t(name, score)

The query returns the following result.

result

alice

Considerations

Every sort order must include NULLS FIRST or NULLS LAST. A sort order of only 'ASC' or 'DESC' makes the query fail.

Compare only keys that you build from the same data types and sort orders. A key of one data type, such as BIGINT, cannot be compared with a key of another, such as BLOB, and the query fails.

equi_width_bins function

Divides the range from min to max into bin_count bins of equal width, and returns a list that holds the upper bound of each bin, in ascending order. When nice_rounding is false, the last bound equals max. When it is true, the function moves the bounds to round values, such as multiples of 5 or 20. If any argument is NULL, the result is NULL.

Pass the result as the bins argument of histogram to count how many values fall into each bin. For more information about histogram, see histogram function.

Syntax

equi_width_bins(min, max, bin_count, nice_rounding)

Arguments

The equi_width_bins function takes the following arguments:

min

The lower end of the range, as a number, a DATE, or a TIMESTAMP.

max

The upper end of the range, of the same type as min. It must not be less than min.

bin_count

The number of bins to create, as a BIGINT value. It must be at least 1 and at most 1000000.

nice_rounding

A flag that controls rounding, as a BOOLEAN value. Set it to true to move the bounds to round values, such as multiples of 5 or 20. Rounding can change the number of bins and can make the last bound greater than max.

Return type

A list whose element type depends on the type of min and max. For example, the result is INTEGER[] for INTEGER values, BIGINT[] for BIGINT values, DOUBLE[] for DECIMAL or DOUBLE values, DATE[] for DATE values, and TIMESTAMP[] for TIMESTAMP values.

Examples

The following SQL query splits the range from 0 to 10 into two bins, with rounding, and returns the upper bound of each bin.

SELECT equi_width_bins(0, 10, 2, true) AS result

The query returns the following result.

result

[5, 10]

The following SQL query splits the range from 3 to 97 into five bins, without and with rounding. With rounding, the bounds are multiples of 20, and the last bound is greater than max.

SELECT equi_width_bins(3, 97, 5, false) AS exact_bounds, equi_width_bins(3, 97, 5, true) AS rounded_bounds

The query returns the following result.

exact_bounds rounded_bounds

[21, 40, 59, 78, 97]

[20, 40, 60, 80, 100]

The following SQL query shows that rounding can change the number of bins. It asks for four bins between 0.0 and 1.0 and returns the number of bounds in each result.

SELECT len( equi_width_bins(0.0, 1.0, 4, false) ) AS exact_count, len( equi_width_bins(0.0, 1.0, 4, true) ) AS rounded_count

The query returns the following result.

exact_count rounded_count

4

5

The following SQL query passes the bins to histogram, which counts the values in each bin. Each map key is the upper bound of a bin, and each map value is the number of values in that bin.

SELECT histogram( x, equi_width_bins(0, 100, 4, false) ) AS result FROM (VALUES (5), (30), (55), (60), (99)) AS v(x)

The query returns the following result.

result

{25=1, 50=1, 75=2, 100=1}

Considerations

For integer values, every bound is an integer, so the bins can differ in width. For example, equi_width_bins(0, 10, 3, false) returns [3, 6, 10].

With nice_rounding set to true, the number of bounds can differ from bin_count. For example, equi_width_bins(0, 10, 3, true) returns two bounds, [5, 10].

If max is less than min, or bin_count is less than 1, the query fails. If min equals max, the result has one bound.

hash function

Returns an unsigned 64-bit hash, as a UBIGINT, of the values that you pass. When you pass more than one value, the hash combines all of them. The same input always produces the same hash, so you can use hash to compare values or to assign rows to buckets.

hash does not provide cryptographic security. For a cryptographic digest, use a function such as sha256. For more information about sha256, see sha256 function.

Unlike most functions, hash does not return NULL for a NULL input. It returns a fixed hash value instead.

Syntax

hash(value [, ...])

Arguments

The hash function takes the following argument:

value

One or more values to hash, of any type.

Return type

UBIGINT

Examples

The following SQL query returns the hash of a string.

SELECT hash('abc') AS result

The query returns the following result.

result

1924864467101078684

The following SQL query compares hashes. Equal inputs have equal hashes, and the hash is case-sensitive.

SELECT hash('abc') = hash('abc') AS same_input, hash('abc') = hash('ABC') AS different_case

The query returns the following result.

same_input different_case

true

false

The following SQL query shows that the hash of NULL is not NULL.

SELECT hash(NULL) IS NULL AS is_null

The query returns the following result.

is_null

false