The Problem
The ARRAY_INTERSECT function requires a constant amount of arrays - for example, a column of arrays.
The Solution
To implement ARRAY_INTERSECT on an unknown or random number of arrays, you can use a Lambda function inside of another Lambda function.
Use ARRAY_AGG to bring the arrays per group into an array of arrays
Split the array of arrays into `first_array` and `rest_arrays`, using ELEMENT_AT and SLICE array functions.
-
For every element in the first array:
Example Syntax
WITH base AS (
SELECT ['a','b','c'] AS arr
UNION ALL
SELECT ['a','b'] AS arr
UNION ALL
SELECT ['a'] AS arr
),
base2 AS (
SELECT
ELEMENT_AT(ARRAY_AGG(arr),1) AS first_array, -- use ARRAY_AGG to bring the arrays per group into an array of arrays
SLICE(ARRAY_AGG(arr), 2) AS rest_arrays -- use ARRAY_AGG to bring the arrays per group into an array of arrays and extract the rest of the arrays
FROM base)
SELECT
-- inner lambda (ALL MATCH) , outer lambda (FILTER)
FILTER(y -> ALL_MATCH(x -> CONTAINS(x,y), rest_arrays), first_array)
FROM base2;