BigQuery · Aggregate function
BigQuery LOGICAL_AND
LOGICAL_AND returns TRUE if a BOOL expression is TRUE for every non-NULL row of a group and FALSE if any row is FALSE. Use it for checks such as 'did every task of this run succeed'. NULLs are ignored, and an empty or all-NULL group returns NULL.
LOGICAL_AND is the aggregate form of AND: it answers 'is this true for all rows?' per group. It returns FALSE as soon as one row is FALSE, TRUE if all non-NULL rows are TRUE, and NULL when there are no rows or only NULLs. PostgreSQL calls it BOOL_AND or EVERY and Snowflake BOOLAND_AGG; none of those names exist in BigQuery.
Because NULLs are ignored, a missing result counts as a pass: LOGICAL_AND(check_passed) over TRUE, NULL returns TRUE. When NULL should mean failure, write LOGICAL_AND(IFNULL(check_passed, FALSE)) or LOGICAL_AND(check_passed IS TRUE). The argument must be a BOOL; compare a 0/1 integer flag explicitly.
The opposite question, 'is it true for at least one row?', is LOGICAL_OR. COUNTIF(NOT x) = 0 gives the same answer as LOGICAL_AND for non-NULL data, but returns TRUE instead of NULL for an empty input.
Syntax
LOGICAL_AND(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
Did every task of the run succeed?
WITH task_runs AS (
SELECT 'run-101' AS run_id, 'extract' AS task, TRUE AS succeeded UNION ALL
SELECT 'run-101', 'transform', TRUE UNION ALL
SELECT 'run-101', 'load', TRUE UNION ALL
SELECT 'run-102', 'extract', TRUE UNION ALL
SELECT 'run-102', 'transform', FALSE UNION ALL
SELECT 'run-102', 'load', TRUE
)
SELECT
run_id,
LOGICAL_AND(succeeded) AS all_tasks_succeeded
FROM task_runs
GROUP BY run_id
ORDER BY run_id;| run_id | all_tasks_succeeded |
|---|---|
| run-101 | true |
| run-102 | false |
NULL counts as a pass unless you say otherwise
WITH checks AS (
SELECT 'orders' AS table_name, TRUE AS passed UNION ALL
SELECT 'orders', NULL UNION ALL
SELECT 'invoices', NULL
)
SELECT
table_name,
LOGICAL_AND(passed) AS all_passed,
LOGICAL_AND(IFNULL(passed, FALSE)) AS all_passed_strict
FROM checks
GROUP BY table_name
ORDER BY table_name;| table_name | all_passed | all_passed_strict |
|---|---|---|
| invoices | NULL | false |
| orders | true | false |
Data quality check per source
WITH signups AS (
SELECT 'web' AS source, 'ana@example.com' AS email UNION ALL
SELECT 'web', 'li@example.org' UNION ALL
SELECT 'import', 'no-at-sign.example.com' UNION ALL
SELECT 'import', 'sam@example.com'
)
SELECT
source,
LOGICAL_AND(email LIKE '%@%') AS all_emails_have_at,
COUNTIF(NOT email LIKE '%@%') AS bad_emails
FROM signups
GROUP BY source
ORDER BY source;| source | all_emails_have_at | bad_emails |
|---|---|---|
| import | false | 1 |
| web | true | 0 |
Common mistakes
- Treating NULL as failure. NULLs are skipped, so a check that didn't run looks like a pass. Use
LOGICAL_AND(IFNULL(passed, FALSE)). - Expecting TRUE when no rows match. An aggregate over zero rows returns NULL, and
IF(LOGICAL_AND(ok), 'pass', 'fail')then reports fail. Decide explicitly:IFNULL(LOGICAL_AND(ok), TRUE). - Passing an integer flag.
LOGICAL_AND(is_active)on an INT64 column fails with No matching signature for aggregate function LOGICAL_AND; writeLOGICAL_AND(is_active = 1). - Using BOOL_AND, EVERY or BOOLAND_AGG from other databases. BigQuery answers with
Function not found.
FAQ
How do I check whether all rows meet a condition in BigQuery?
Use LOGICAL_AND(condition) per group. COUNTIF(NOT condition) = 0 works too and returns TRUE rather than NULL when the group has no rows.
What is the BigQuery equivalent of BOOL_AND or EVERY?
LOGICAL_AND. It has the same semantics as PostgreSQL's BOOL_AND: NULLs are ignored and an empty input returns NULL.
What does LOGICAL_AND return when all values are NULL?
NULL, the same as for an empty group. Wrap the argument in IFNULL if a NULL should count as FALSE.
Related
Errors you may hit
More aggregate functions
All BigQuery aggregate functions · All functions · BigQuery error messages
