I have a BigQuery table with a STRUCT field and I would like to be able to automatically increase its list of elements whenever I attempt to insert a previously unseen element. Is this possible?
-- initially meta only has elements: hair, eyes
CREATE TEMP TABLE tt AS
SELECT
1 AS id,
STRUCT (
'brown' AS hair,
'brown' AS eyes
) AS meta;
-- now I would like to add a neverbefore seen element: weight
INSERT INTO tt
SELECT
2 AS id,
STRUCT (
'brown' AS hair,
160 AS weight
) AS meta;
This obviously does not work and returns the error Query column 2 has type STRUCT<hair STRING, weight INT64> which cannot be inserted into column meta, which has type STRUCT<hair STRING, eyes STRING> at [10:1].
The resulting temp table looks like the following after the initial construction:
| id | meta.hair | meta.eyes |
|---|---|---|
| 1 | brown | brown |
And then it would ideally automatically add the element "weight" to meta after inserting row 2:
| id | meta.hair | meta.eyes | meta.weight |
|---|---|---|---|
| 1 | brown | brown | NULL |
| 2 | brown | NULL | 160 |
This is probably wishful thinking.
As a real-world example, I know that Stitch's Webhook --> BigQuery integration is somehow achieving this behavior when it syncs data from some of our SaaS products into BigQuery. Stitch handles new, never-seen-before nested fields inside JSON payloads by adding new elements to corresponding STRUCT fields. I am just not sure how this magic is happening.


