Pinky Joe
Pinky Joe

Reputation: 79

Fewest number of buckets to bag elements in bigquery

I have a matrix with buckets and elements like below. If an element can fit in a bucket it is 1 in the corresponding cell

enter image description here

For example: If you look at the image, element x can fit in bucket-a,b,c and not in d and e

I want to find the fewest buckets to group my elements. In this case, buckets c and d could group all the elements in just two buckets.

Any idea if i can do this in bigquery dynamically and efficiently ? original data is not as simple as this.

 select "element-x" as element , 1 as bucketa, 1 as bucketb, 1 as bucketc, 0 as bucketd, 0 as buckete
 union all
 select "element-y" as element , 0 as bucketa, 0 as bucketb, 1 as bucketc, 0 as bucketd, 0 as buckete
 union all
 select "element-z" as element , 1 as bucketa, 0 as bucketb, 1 as bucketc, 0 as bucketd, 0 as buckete
 union all
 select "element-p" as element , 0 as bucketa, 0 as bucketb, 1 as bucketc, 0 as bucketd, 0 as buckete
 union all
 select "element-q" as element , 1 as bucketa, 0 as bucketb, 0 as bucketc, 1 as bucketd, 0 as buckete
 union all
 select "element-r" as element , 0 as bucketa, 1 as bucketb, 0 as bucketc, 1 as bucketd, 1 as buckete

Upvotes: 1

Views: 353

Answers (1)

Mikhail Berlyant
Mikhail Berlyant

Reputation: 172993

Consider below solution - obviously you need to make sure you provide accurate data in matrix CTE and also you need respectively adjust buckets_elements CTE to reflect all buckets in matrix. The rest of CTE's and final query will make a work for you!

with matrix as (
  select "element-x" as element, 1 as bucketa, 1 as bucketb, 1 as bucketc, 0 as bucketd, 0 as buckete union all
  select "element-y", 0, 0, 1, 0, 0 union all
  select "element-z", 1, 0, 1, 0, 0 union all
  select "element-p", 0, 0, 1, 0, 0 union all
  select "element-q", 1, 0, 0, 1, 0 union all
  select "element-r", 0, 1, 0, 1, 1    
), buckets_elements as ( 
  select array[struct(a), struct(b), struct(c), struct(d), struct(e)] buckets
  from (
    select 
      array_agg(if(bucketa = 1, element, null) ignore nulls) a,
      array_agg(if(bucketb = 1, element, null) ignore nulls) b,
      array_agg(if(bucketc = 1, element, null) ignore nulls) c,
      array_agg(if(bucketd = 1, element, null) ignore nulls) d,
      array_agg(if(buckete = 1, element, null) ignore nulls) e
    from matrix
  )
), columns_names as (
  select 
    regexp_extract_all(to_json_string((select as struct * except(element) from unnest([t]))), r'"([^"]+)"') cols
  from matrix t limit 1
), columns_index as (
  select generate_array(0, array_length(cols) - 1) as arr  
  from columns_names
), buckets_combinations as (
  select  
    (select array_agg(
      case when n & (1<<pos) <> 0 then arr[offset(pos)] end 
      ignore nulls)
     from unnest(generate_array(0, array_length(arr) - 1)) pos
    ) as combo
  from columns_index cross join 
  unnest(generate_array(1, cast(power(2, array_length(arr)) - 1 as int64))) n
)
select 
  array(select cols[offset(i)] from columns_names, unnest(combo) i) winners
from (
  select combo, 
    rank() over(order by (select count(distinct  el) from unnest(val) v, unnest(v.a) el) desc, array_length(combo)) as rnk
  from (
    select any_value(c).combo, array_agg(buckets[offset(i)]) val
    from buckets_combinations c, unnest(combo) i, buckets_elements b
    group by format('%t', c)
  )
)
where rnk = 1

with output

enter image description here

Upvotes: 1

Related Questions