BigQuery · Aggregate function
BigQuery LOGICAL_OR
LOGICAL_OR returns TRUE if a BOOL expression is TRUE for at least one row of a group and FALSE if every non-NULL row is FALSE. Use it for 'did anything fail' or 'did this session purchase'. NULLs are ignored; an empty or all-NULL group returns NULL.
LOGICAL_OR is the aggregate form of OR: it answers 'is this true for any row?' per group. It returns TRUE if at least one row is TRUE, FALSE if all non-NULL rows are FALSE, and NULL when there are no rows or only NULLs. PostgreSQL calls it BOOL_OR and Snowflake BOOLOR_AGG; in BigQuery only LOGICAL_OR exists.
It is handy in HAVING to keep groups where something happened at least once, such as sessions that added to cart but never purchased: HAVING LOGICAL_OR(event_name = 'add_to_cart') AND NOT LOGICAL_OR(event_name = 'purchase'). COUNTIF(x) > 0 gives the same answer for non-NULL data.
NULLs are ignored, so LOGICAL_OR(failed) over FALSE, NULL returns FALSE even though one result is unknown. If unknown should count as a problem, test for it explicitly: LOGICAL_OR(failed IS NOT FALSE) is TRUE for both TRUE and NULL.
Syntax
LOGICAL_OR(expression [WHERE where_expression])Arguments and return value
| Argument | Description |
|---|---|
expression | A BOOL expression. NULLs are ignored. |
WHERE where_expression | Optional filter inside the call (Preview). |
Returns: BOOL. NULL for an empty group or when every value is NULL.
Examples
Which steps did each session reach?
WITH events AS (
SELECT 's1' AS session_id, 'view' AS event_name UNION ALL
SELECT 's1', 'add_to_cart' UNION ALL
SELECT 's1', 'purchase' UNION ALL
SELECT 's2', 'view' UNION ALL
SELECT 's2', 'add_to_cart' UNION ALL
SELECT 's3', 'view'
)
SELECT
session_id,
LOGICAL_OR(event_name = 'add_to_cart') AS added_to_cart,
LOGICAL_OR(event_name = 'purchase') AS purchased
FROM events
GROUP BY session_id
ORDER BY session_id;| session_id | added_to_cart | purchased |
|---|---|---|
| s1 | true | true |
| s2 | true | false |
| s3 | false | false |
Abandoned carts with HAVING
WITH events AS (
SELECT 's1' AS session_id, 'add_to_cart' AS event_name UNION ALL
SELECT 's1', 'purchase' UNION ALL
SELECT 's2', 'view' UNION ALL
SELECT 's2', 'add_to_cart' UNION ALL
SELECT 's3', 'view'
)
SELECT session_id
FROM events
GROUP BY session_id
HAVING LOGICAL_OR(event_name = 'add_to_cart')
AND NOT LOGICAL_OR(event_name = 'purchase');| session_id |
|---|
| s2 |
Unknown results are ignored
WITH runs AS (
SELECT 'orders_daily' AS pipeline, FALSE AS failed UNION ALL
SELECT 'orders_daily', NULL UNION ALL
SELECT 'users_sync', FALSE
)
SELECT
pipeline,
LOGICAL_OR(failed) AS any_failed,
LOGICAL_OR(failed IS NOT FALSE) AS any_failed_or_unknown
FROM runs
GROUP BY pipeline
ORDER BY pipeline;| pipeline | any_failed | any_failed_or_unknown |
|---|---|---|
| orders_daily | false | true |
| users_sync | false | false |
Common mistakes
- Treating NULL as a problem by default. NULLs are ignored, so a run with an unknown result doesn't make LOGICAL_OR TRUE. Use
failed IS NOT FALSEwhen unknown should count. - Expecting FALSE for no rows. An aggregate over zero rows returns NULL. Wrap it as
IFNULL(LOGICAL_OR(x), FALSE)if the caller expects a BOOL. - Passing an integer flag.
LOGICAL_OR(has_error)on an INT64 column fails with No matching signature for aggregate function LOGICAL_OR; writeLOGICAL_OR(has_error = 1). - Using BOOL_OR or BOOLOR_AGG from PostgreSQL or Snowflake. BigQuery answers with
Function not found.
FAQ
How do I check whether any row meets a condition in BigQuery?
Use LOGICAL_OR(condition) per group, or in HAVING to keep only matching groups. COUNTIF(condition) > 0 is an alternative that returns FALSE instead of NULL for an empty input.
What is the BigQuery equivalent of BOOL_OR?
LOGICAL_OR. It ignores NULLs and returns NULL for an empty input, like PostgreSQL's BOOL_OR.
What is the difference between LOGICAL_OR and MAX on a BOOL column?
For BOOL values they agree: MAX returns TRUE when any value is TRUE, because FALSE sorts before TRUE. LOGICAL_OR states the intent more clearly and only accepts BOOL input.
Related
Errors you may hit
More aggregate functions
All BigQuery aggregate functions · All functions · BigQuery error messages
