1
votes

I'm making a RESTful API in Go that writes rows in BigQuery. I'm using Google BigQuery package for Go.

In order to create the BigQuery scheme, I'm infering the schema from a struct as described in the example.

The problem is that, the resulting schema has all the non-repeated fields as "Required", so that, when I want to upload a struct with null values, the null values are uploaded as empy fields...

This is an example of my struct:

type Stats struct {
    Name            string        `bigquery:"name"`
    LastName        int           `bigquery:"last_name"`
    PhoneNumber     string        `bigquery:"phone_number"`
}

This is an example of how the schema is created:

testSchema, err := bigquery.InferSchema(Stats{})
if err != nil {
    // TODO: Handle error.
}

And, if I upload a struct with only one field set:

rows := []*Stats{
    {Name: "testA"},
}

u := table.Uploader()
err2 := u.Put(ctx, rows)

The result is that, in BigQuery, the fields "last_name" and "phone_number" is an empty string "" instead of NULL

2

2 Answers

2
votes

For that, apparently you would have to use the specific BigQuery nullable types (NullInt64, NullFloat64, NullString, NullBool, NullTimestamp, NullDate, NullTime and NullDateTime).

Example using bigquery.NullBool: https://godoc.org/cloud.google.com/go/bigquery#ex-InferSchema--Tags

0
votes

I am no Go expert, but looking at code at https://github.com/GoogleCloudPlatform/google-cloud-go/blob/master/bigquery/schema.go#L182 it looks like the inferFieldSchema method always sets Required: true. I would file a bug to allow to control it (although not clear how), or you can patch your schema after it was created - schema modification from REQUIRED to NULLABLE is supported by BigQuery.