BigQuery · Array function
BigQuery GENERATE_ARRAY
GENERATE_ARRAY(start, end[, step]) returns an array of numbers from start to end, both inclusive, stepping by 1 or by step. Wrap it in UNNEST to get a number series as rows for gap filling, bucketing or test data, without a numbers table.
GENERATE_ARRAY builds the whole sequence as one array: GENERATE_ARRAY(1, 5) is [1, 2, 3, 4, 5]. Both ends are inclusive, unlike Python's range. A negative step counts down. If the step points away from the end value, the result is an empty array rather than an error, and if any argument is NULL, the result is NULL.
Arguments can be INT64, NUMERIC, BIGNUMERIC or FLOAT64. Use NUMERIC for decimal steps: in FLOAT64, rounding can push the last value past the end, so GENERATE_ARRAY(0, 0.3, 0.1) returns [0.0, 0.1, 0.2] and silently drops 0.3.
An array can hold at most 1,048,576 elements; a larger range fails with "produced too many elements", and a step of 0 is an error. To get rows, unnest the array: SELECT n FROM UNNEST(GENERATE_ARRAY(1, 100)) AS n, which is the BigQuery form of PostgreSQL's generate_series. For dates use GENERATE_DATE_ARRAY; for timestamps, GENERATE_TIMESTAMP_ARRAY.
Syntax
GENERATE_ARRAY(start_expression, end_expression[, step_expression])Arguments and return value
| Argument | Description |
|---|---|
start_expression | First value. INT64, NUMERIC, BIGNUMERIC or FLOAT64. |
end_expression | Last value, included if the steps land on it. |
step_expression | Increment, default 1. Negative values count down; 0 is an error. |
Returns: An ARRAY of the arguments' common numeric type. Empty if the range is empty, NULL if any argument is NULL.
Examples
Ranges, steps and countdowns
SELECT
GENERATE_ARRAY(1, 5) AS one_to_five,
GENERATE_ARRAY(0, 100, 25) AS step_25,
GENERATE_ARRAY(10, 1, -3) AS countdown;| one_to_five | step_25 | countdown |
|---|---|---|
| [1, 2, 3, 4, 5] | [0, 25, 50, 75, 100] | [10, 7, 4, 1] |
Empty ranges, NULLs and decimal steps
A positive step from 10 to 1 yields an empty array. The FLOAT64 series stops at 0.2 because its third step lands slightly above 0.3; the NUMERIC series is exact.
SELECT
GENERATE_ARRAY(10, 1) AS wrong_direction,
GENERATE_ARRAY(1, NULL) IS NULL AS null_end,
GENERATE_ARRAY(0, 0.3, 0.1) AS float_step,
GENERATE_ARRAY(NUMERIC '0', NUMERIC '0.3', NUMERIC '0.1') AS numeric_step;| wrong_direction | null_end | float_step | numeric_step |
|---|---|---|---|
| [] | true | [0.0, 0.1, 0.2] | [0, 0.1, 0.2, 0.3] |
Fill missing hours with zeros
Join the full series to sparse data so every hour gets a row.
WITH hourly_errors AS (
SELECT 0 AS hour, 4 AS errors UNION ALL
SELECT 2, 1 UNION ALL
SELECT 5, 7
)
SELECT h AS hour, IFNULL(e.errors, 0) AS errors
FROM UNNEST(GENERATE_ARRAY(0, 5)) AS h
LEFT JOIN hourly_errors AS e ON e.hour = h
ORDER BY hour;| hour | errors |
|---|---|
| 0 | 4 |
| 1 | 0 |
| 2 | 1 |
| 3 | 0 |
| 4 | 0 |
| 5 | 7 |
Bucket boundaries for RANGE_BUCKET
GENERATE_ARRAY(0, 200, 50) gives the boundaries [0, 50, 100, 150, 200]; RANGE_BUCKET returns how many boundaries are less than or equal to each amount.
WITH orders AS (
SELECT 'A-1' AS order_id, 18 AS amount UNION ALL
SELECT 'A-2', 45 UNION ALL
SELECT 'A-3', 120 UNION ALL
SELECT 'A-4', 260
)
SELECT
order_id,
amount,
RANGE_BUCKET(amount, GENERATE_ARRAY(0, 200, 50)) AS bucket
FROM orders
ORDER BY order_id;| order_id | amount | bucket |
|---|---|---|
| A-1 | 18 | 1 |
| A-2 | 45 | 1 |
| A-3 | 120 | 3 |
| A-4 | 260 | 5 |
Common mistakes
- Expecting an exclusive end, as in Python's
range(1, 10).GENERATE_ARRAY(1, 10)includes 10. - Using FLOAT64 for decimal steps. Rounding can drop the end value; pass NUMERIC arguments such as
NUMERIC '0.1'. - Generating more than 1,048,576 elements, for example one per second over two weeks. Use a coarser step or combine two smaller series.
- Calling it in FROM like PostgreSQL's
generate_series. It returns an array, so writeFROM UNNEST(GENERATE_ARRAY(1, 10)) AS n.
Cost and performance
- GENERATE_ARRAY reads no table data, so a query built only on generated series processes 0 bytes. Joining the series to a table bills that table's columns as usual.
FAQ
How do I generate a series of numbers in BigQuery?
SELECT n FROM UNNEST(GENERATE_ARRAY(1, 100)) AS n returns the numbers 1 to 100 as rows. Add a third argument for a different step.
What is the maximum size of GENERATE_ARRAY?
1,048,576 elements. Beyond that the query fails with an error saying GENERATE_ARRAY produced too many elements.
How do I generate a range of dates?
Use GENERATE_DATE_ARRAY, for example GENERATE_DATE_ARRAY('2026-11-01', '2026-11-30'), and unnest it the same way.
Related
More array functions
All BigQuery array functions · All functions · BigQuery error messages
