Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Port field from NULLABLE to REQUIRED in BigQuery

I am trying to move a BigQuery table to a new schema that has some additional new NULLABLE fields and in which a field f has become REQUIRED (they were NULLABLE in the old schema.)

I tried to update the table with the new schema through the command

bq update <table> <new_schema>

and I get the error

BigQuery error in update operation: Provided Schema does not match Table

As a second attempt, I created a temporary empty table with the new field and then tried to append there the data coming from a query (SELECT * from the old table), but I get the error:

Invalid schema update. Field f has changed mode from REQUIRED to NULLABLE

Is there a way to do this migration easily? Of course I am fine with ignoring rows of the table where the field f is actually NULL. It would be cool if BigQuery could infer that from the query. I tried to do

SELECT * FROM old_table WHERE f IS NOT NULL

and append the result to the table with the new schema, but that doesn't work.

like image 803
Alessandro Cosentino Avatar asked Jul 20 '16 13:07

Alessandro Cosentino


People also ask

How do you handle null in BigQuery?

IFNULL() allows to return the first value if the value is NULL; otherwise, the second value is returned. COALESCE() is a function that returns the first non-null value in a set of arguments. NVL() can be used to replace a NULL value with a value specified by the user.

How do you write not equal to in BigQuery?

NOT EQUAL TO (!=) and EXISTS... EQUAL TO Giving Different Results.


1 Answers

Try to use empty string where f is null.

CASE
    WHEN f IS NULL THEN ""
    ELSE f
END as f
like image 144
Mohammed Sherif KK Avatar answered Nov 03 '22 04:11

Mohammed Sherif KK