BigQuery · Math function
BigQuery LOG, LN, LOG10
LOG(x) returns the natural logarithm of x (the same as LN), LOG(x, base) uses any base, and LOG10(x) uses base 10. INT64 input returns FLOAT64, and x <= 0 fails. Watch out when porting: PostgreSQL's one-argument log is base 10, and Snowflake puts the base first.
BigQuery has three logarithm functions. LN(x) is the natural logarithm. LOG(x) with one argument is a synonym for LN, and LOG(x, y) is the logarithm of x to base y, with the base second. LOG10(x) is the base-10 logarithm. INT64 and FLOAT64 inputs return FLOAT64; NUMERIC and BIGNUMERIC inputs keep their type.
Defaults and argument order differ between warehouses, which makes LOG a common migration bug. PostgreSQL's log(x) is base 10 and its log(b, x) puts the base first; Snowflake's LOG(base, x) also puts the base first. In BigQuery LOG(8, 2) is 3.0, the logarithm of 8 to base 2.
An input of 0 or less, a base of 0 or less and a base of 1 all fail with Floating point error in function. Guard zeros with NULLIF(x, 0) or SAFE.LOG(x), or use LN(x + 1) when a zero should map to zero. Prefer LOG10 for base 10: LOG(1000, 10) returns 2.9999999999999996, and FLOOR of that is 2.0, not 3.0.
Syntax
LN(X)
LOG(X [, Y])
LOG10(X)Arguments and return value
| Argument | Description |
|---|---|
X | The value: INT64, NUMERIC, BIGNUMERIC or FLOAT64. Must be greater than 0. |
Y | Optional base for LOG. Must be greater than 0 and not 1. Without it, LOG is the natural logarithm. |
Returns: LN, LOG10 and one-argument LOG: FLOAT64 for INT64 or FLOAT64 input, NUMERIC for NUMERIC, BIGNUMERIC for BIGNUMERIC. LOG(X, Y): FLOAT64 if either argument is FLOAT64 or both are INT64, otherwise NUMERIC, or BIGNUMERIC if either argument is BIGNUMERIC.
Examples
LN, LOG and LOG10
One-argument LOG equals LN. In two-argument LOG the base goes second.
SELECT
LN(100) AS ln_100,
LOG(100) AS log_100,
LOG(100, 10) AS log_base_10,
LOG10(100) AS log10_100,
LOG(8, 2) AS log_base_2;| ln_100 | log_100 | log_base_10 | log10_100 | log_base_2 |
|---|---|---|---|---|
| 4.605170185988092 | 4.605170185988092 | 2.0 | 2.0 | 3.0 |
Use LOG10 for base 10
LOG with a base divides two natural logarithms, which can land just below a whole number. FLOOR then gives the wrong order of magnitude.
SELECT
LOG(1000, 10) AS log_with_base_10,
LOG10(1000) AS log10,
FLOOR(LOG(1000, 10)) AS floor_of_log_with_base;| log_with_base_10 | log10 | floor_of_log_with_base |
|---|---|---|
| 2.9999999999999996 | 3.0 | 2.0 |
Order of magnitude of table sizes
NULLIF turns the empty table's 0 bytes into NULL, so LOG10 returns NULL instead of failing the query.
WITH tables AS (
SELECT 'events' AS table_name, 52428800000 AS size_bytes UNION ALL
SELECT 'users', 734003200 UNION ALL
SELECT 'countries', 8192 UNION ALL
SELECT 'staging_empty', 0
)
SELECT
table_name,
CAST(FLOOR(LOG10(NULLIF(size_bytes, 0))) AS INT64) AS order_of_magnitude
FROM tables
ORDER BY size_bytes DESC;| table_name | order_of_magnitude |
|---|---|
| events | 10 |
| users | 8 |
| countries | 3 |
| staging_empty | NULL |
Geometric mean of growth factors
EXP(AVG(LN(x))) is the geometric mean, the right average for growth rates that compound. The arithmetic mean overstates it.
WITH monthly_growth AS (
SELECT growth_factor FROM UNNEST([1.10, 0.95, 1.20]) AS growth_factor
)
SELECT
ROUND(EXP(AVG(LN(growth_factor))), 4) AS geometric_mean,
ROUND(AVG(growth_factor), 4) AS arithmetic_mean
FROM monthly_growth;| geometric_mean | arithmetic_mean |
|---|---|
| 1.0784 | 1.0833 |
Common mistakes
- Porting one-argument LOG from PostgreSQL. There
log(x)is base 10; in BigQuery it is the natural logarithm. UseLOG10(x). - Porting
LOG(base, x)from Snowflake or PostgreSQL without swapping the arguments. BigQuery's order isLOG(x, base). - Taking logs of zero or negative values.
LOG10(0)fails with Floating point error in function: LOG10(0); useNULLIF(x, 0),SAFE.LOG10(x)orLN(x + 1). - Counting digits with
FLOOR(LOG(x, 10)). Rounding error gives 2.9999999999999996 for 1000; LOG10 returns exactly 3.0.
FAQ
Is LOG in BigQuery the natural log or base 10?
With one argument, LOG is the natural logarithm, the same as LN. Use LOG10(x) or LOG(x, 10) for base 10.
How do I calculate log base 2 in BigQuery?
Use LOG(x, 2). The base is the second argument, so LOG(1024, 2) is 10.0.
How do I take the log of a column that contains zeros?
Use SAFE.LN(x) or LN(NULLIF(x, 0)) to get NULL for zeros, or LN(x + 1) to keep zeros at 0 on a log scale.
Related
More mathematical functions
All BigQuery mathematical functions · All functions · BigQuery error messages
