BigQuery · Math function
BigQuery RANGE_BUCKET
RANGE_BUCKET(point, boundaries) returns the 0-based index of the bucket a value falls into, given a sorted array of boundaries: RANGE_BUCKET(25, [10, 20, 30]) is 2. Use it for histograms and custom bands, and in PARTITION BY for integer-range partitioning.
RANGE_BUCKET returns how many boundaries are less than or equal to the point. Values below the first boundary get 0, a value equal to a boundary goes into the bucket that starts there, and values at or above the last boundary get the array length. With boundaries [50, 100, 500], 49 is bucket 0, 50 is bucket 1 and 500 is bucket 3. The result is an INT64.
The array must be sorted in ascending order and contain no NULLs; otherwise the query fails with errors such as Elements in input array to RANGE_BUCKET must be in ascending order. A NULL or NaN point returns NULL, and an empty array returns 0. The point and the array elements must have the same type: RANGE_BUCKET(1.2, [1, 2, 4, 6]) fails because the array is ARRAY<INT64>, so write the boundaries as [1.0, 2.0, 4.0, 6.0].
The same function defines integer-range partitioned tables: PARTITION BY RANGE_BUCKET(customer_id, GENERATE_ARRAY(0, 1000000, 10000)) creates one partition per 10,000 IDs. Rows outside the range go to the __UNPARTITIONED__ partition and NULL keys to __NULL__. See PARTITION BY.
Syntax
RANGE_BUCKET(point, boundaries_array)Arguments and return value
| Argument | Description |
|---|---|
point | The value to place. Any comparable type; NULL or NaN returns NULL. |
boundaries_array | An ARRAY of the same type as point, sorted ascending, without NULL elements. An empty array returns 0. |
Returns: INT64: the 0-based bucket index, from 0 up to the length of the array.
Examples
How values map to buckets
SELECT
RANGE_BUCKET(5, [10, 20, 30]) AS below_first,
RANGE_BUCKET(10, [10, 20, 30]) AS on_boundary,
RANGE_BUCKET(25, [10, 20, 30]) AS between_bounds,
RANGE_BUCKET(99, [10, 20, 30]) AS above_last;| below_first | on_boundary | between_bounds | above_last |
|---|---|---|---|
| 0 | 1 | 2 | 3 |
Order value histogram with labels
The bucket index doubles as an OFFSET into an array of labels, which needs one element more than the boundaries array.
WITH orders AS (
SELECT amount FROM UNNEST([12, 48, 75, 160, 420, 999, 1500]) AS amount
)
SELECT
RANGE_BUCKET(amount, [50, 100, 500, 1000]) AS bucket,
['under 50', '50 to 99', '100 to 499', '500 to 999', '1000 and up']
[OFFSET(RANGE_BUCKET(amount, [50, 100, 500, 1000]))] AS label,
COUNT(*) AS orders
FROM orders
GROUP BY bucket, label
ORDER BY bucket;| bucket | label | orders |
|---|---|---|
| 0 | under 50 | 2 |
| 1 | 50 to 99 | 1 |
| 2 | 100 to 499 | 2 |
| 3 | 500 to 999 | 1 |
| 4 | 1000 and up | 1 |
NULL points, generated boundaries and empty arrays
GENERATE_ARRAY(0, 100, 10) makes equal-width bins. 37 is at or above 0, 10, 20 and 30, so it lands in bucket 4.
SELECT
RANGE_BUCKET(NULL, [0, 10, 20]) AS null_point,
RANGE_BUCKET(37, GENERATE_ARRAY(0, 100, 10)) AS with_generate_array,
RANGE_BUCKET(80, []) AS empty_array;| null_point | with_generate_array | empty_array |
|---|---|---|
| NULL | 4 | 0 |
Integer-range partitioned table
Creates 100 partitions of 10,000 customer IDs each. Queries that filter customer_id with constant ranges read only the matching partitions.
CREATE TABLE `project.dataset.customers`
(
customer_id INT64,
plan STRING,
signup_date DATE
)
PARTITION BY RANGE_BUCKET(customer_id, GENERATE_ARRAY(0, 1000000, 10000));Common mistakes
- Passing unsorted boundaries or boundaries with NULLs. Both fail at run time; build boundaries with GENERATE_ARRAY or sort them first.
- Off-by-one errors at the edges. A value equal to a boundary goes into the bucket to its right, so with
[100, 500]the value 100 is bucket 1, not 0. - Making the label array one element too short. The index runs from 0 to the boundaries length, so labels need length + 1 elements, or OFFSET fails with Array index out of bounds.
- Mixing types, such as a FLOAT64 point with INT64 boundaries. Write the boundaries as FLOAT64 or cast the point.
- Looking for WIDTH_BUCKET from PostgreSQL or Snowflake. BigQuery doesn't have it; use
RANGE_BUCKET(x, GENERATE_ARRAY(low, high, step))orFLOOR(x / step).
Cost and performance
- On an integer-range partitioned table, filter the partitioning column itself, as in
WHERE customer_id BETWEEN 30000 AND 39999. BigQuery doesn't prune partitions for functions over the partitioning column, soWHERE customer_id + 1 BETWEEN ...scans the whole table. - A table can have at most 10,000 partitions, so choose an interval that keeps the number of ranges well below that.
FAQ
How do I build a histogram in BigQuery?
Group by RANGE_BUCKET(value, [boundaries]) for custom bands, or by FLOOR(value / width) for equal widths, and count rows per bucket. APPROX_QUANTILES helps you pick boundaries that split rows evenly.
What does RANGE_BUCKET return for a value equal to a boundary?
The index of the bucket that starts at that boundary. RANGE_BUCKET(20, [10, 20, 30]) is 2, because two boundaries, 10 and 20, are less than or equal to 20.
How do I create an integer-range partitioned table?
Use PARTITION BY RANGE_BUCKET(int64_column, GENERATE_ARRAY(start, end, interval)) in CREATE TABLE. The start is inclusive, the end is exclusive, and the column must be INT64.
Related
Errors you may hit
More mathematical functions
All BigQuery mathematical functions · All functions · BigQuery error messages
