BigQuery · Math function
BigQuery MOD
MOD(x, y) returns the remainder of x divided by y, with the sign of x: MOD(25, 7) is 4 and MOD(-25, 7) is -4. BigQuery has no % operator, so MOD is how you write modulo, for even/odd checks, every-Nth-row filters and hash buckets.
MOD returns what is left after dividing X by Y. The result has the sign of the dividend, so MOD(-25, 7) is -4, not 3 as in Python. For a remainder that is never negative, use MOD(MOD(x, n) + n, n).
BigQuery doesn't support the % operator that PostgreSQL, MySQL, Snowflake and Redshift use for modulo: x % 2 fails with a syntax error: Expected end of input but got "%". MOD accepts INT64, NUMERIC and BIGNUMERIC but not FLOAT64, so cast floats first: MOD(CAST(price AS NUMERIC), 1).
A zero divisor raises a division by zero error (division by zero: MOD(25, 0)); SAFE.MOD(x, 0) returns NULL instead. Paired with DIV, MOD splits durations into hours, minutes and seconds, and ABS(MOD(FARM_FINGERPRINT(id), n)) assigns rows to n stable buckets.
Syntax
MOD(X, Y)Arguments and return value
| Argument | Description |
|---|---|
X | The dividend: INT64, NUMERIC or BIGNUMERIC. The result takes its sign. |
Y | The divisor: INT64, NUMERIC or BIGNUMERIC. Zero raises an error. |
Returns: INT64 when both arguments are INT64, NUMERIC when either is NUMERIC and neither is BIGNUMERIC, BIGNUMERIC when either is BIGNUMERIC.
Examples
Negative operands
The remainder follows the sign of the first argument. Adding n and taking MOD again gives the non-negative remainder.
SELECT
MOD(25, 7) AS positive,
MOD(-25, 7) AS negative_dividend,
MOD(25, -7) AS negative_divisor,
MOD(MOD(-25, 7) + 7, 7) AS non_negative;| positive | negative_dividend | negative_divisor | non_negative |
|---|---|---|---|
| 4 | -4 | 4 | 3 |
Seconds to hours, minutes and seconds
WITH jobs AS (
SELECT 'nightly_load' AS job, 3725 AS runtime_s UNION ALL
SELECT 'hourly_sync', 59
)
SELECT
job,
DIV(runtime_s, 3600) AS hours,
MOD(DIV(runtime_s, 60), 60) AS minutes,
MOD(runtime_s, 60) AS seconds
FROM jobs
ORDER BY job;| job | hours | minutes | seconds |
|---|---|---|---|
| hourly_sync | 0 | 0 | 59 |
| nightly_load | 1 | 2 | 5 |
Stable shards from a hash
FARM_FINGERPRINT returns a signed INT64, so MOD can be negative. ABS outside MOD maps it to 0 through 3 without the overflow risk of ABS(FARM_FINGERPRINT(...)).
WITH users AS (
SELECT user_id FROM UNNEST(['u-1001', 'u-1005', 'u-1007', 'u-1018']) AS user_id
)
SELECT
user_id,
ABS(MOD(FARM_FINGERPRINT(user_id), 4)) AS shard
FROM users
ORDER BY user_id;| user_id | shard |
|---|---|
| u-1001 | 3 |
| u-1005 | 1 |
| u-1007 | 2 |
| u-1018 | 0 |
Remainders of decimal values
MOD has no FLOAT64 version. NUMERIC works and keeps the fraction.
SELECT
MOD(NUMERIC '19.99', 1) AS fractional_part,
MOD(NUMERIC '7.5', 2) AS remainder;| fractional_part | remainder |
|---|---|
| 0.99 | 1.5 |
Common mistakes
- Writing
x % n. BigQuery has no % operator and the query fails with a syntax error; useMOD(x, n). - Expecting a non-negative result for negative input, as in Python.
MOD(-1, 7)is -1; useMOD(MOD(x, 7) + 7, 7). - Passing FLOAT64.
MOD(7.5, 2)fails with No matching signature for function MOD, even with literals; useMOD(NUMERIC '7.5', 2). - Dividing by zero.
MOD(x, 0)fails the whole query; useSAFE.MOD(x, y)orMOD(x, NULLIF(y, 0)). - Bucketing with
MOD(ABS(FARM_FINGERPRINT(id)), n). ABS fails with an integer overflow if the hash is the smallest INT64;ABS(MOD(FARM_FINGERPRINT(id), n))avoids it.
Cost and performance
- A filter such as
MOD(customer_id, 10) = 0reduces rows, not bytes scanned. On an integer-range partitioned table it also reads every partition, because BigQuery doesn't prune partitions for functions over the partitioning column.
FAQ
What is the modulo operator in BigQuery?
There isn't one. Use MOD(x, y); x % y is a syntax error in GoogleSQL.
How do I check if a number is even in BigQuery?
MOD(n, 2) = 0 is TRUE for even numbers. For negative odd numbers MOD(n, 2) is -1, so test odd numbers with MOD(n, 2) != 0 rather than = 1.
Why does MOD return a negative number?
MOD takes the sign of the dividend, as in C, Java and PostgreSQL. Use MOD(MOD(x, n) + n, n) to get a result between 0 and n - 1.
Related
Errors you may hit
More mathematical functions
All BigQuery mathematical functions · All functions · BigQuery error messages
