Home > Net >  Union null values to a array of struct in Big Query
Union null values to a array of struct in Big Query

Time:11-28

I have the following table in Big Query which has an array of struct type. I have to perform a union operation with a simple table and want to add null values in place of the nested columns.

Actual nested table example - enter image description here

Simple table (which needs to be union-ed)

acc date count
acc_6 11/29/2022 2
acc_8 11/30/2022 3

I tried the following query but it gives an error of incompatible types on the nested columns

select * from actual_table 
union all
select acc, date, count,
array_agg(struct(cast(null as string) as device_id, cast(null as date) as to_date, cast(null as string) as from_date) as d
from simple_table

The resultant table should look like this - enter image description here

CodePudding user response:

Since d has a type of array of struct<string, string, string>, you need to write a null struct like below.

SELECT * FROM actual_table
 UNION ALL
SELECT *, [STRUCT(CAST(null AS STRING), CAST(null AS STRING), CAST(null AS STRING))] FROM simple_table;
  • [] is for array literal. see enter image description here

  • Related