Request a demo

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.

GoogleSQL for BigQuery · Checked against the official reference in November 2026

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

ArgumentDescription
start_expressionFirst value. INT64, NUMERIC, BIGNUMERIC or FLOAT64.
end_expressionLast value, included if the steps land on it.
step_expressionIncrement, 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;
Result
one_to_fivestep_25countdown
[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;
Result
wrong_directionnull_endfloat_stepnumeric_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;
Result
hourerrors
04
10
21
30
40
57

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;
Result
order_idamountbucket
A-1181
A-2451
A-31203
A-42605

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 write FROM 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.

More array functions

All BigQuery array functions · All functions · BigQuery error messages

Observability for every BigQuery table, in about 15 minutes

Masthead detects freshness, volume and schema anomalies, monitors pipeline errors and maps column-level lineage across all your projects, from logs and metadata only.