google-cloud-go icon indicating copy to clipboard operation
google-cloud-go copied to clipboard

BigQuery: uint64 not supported - but really it is a NUMERIC

Open RobHumphris opened this issue 3 years ago • 4 comments

Client

BigQuery

Environment

All,

Go Environment

go version go1.20.3 linux/amd64

Code

Discovered using Protobuf generated schemas, we use a perfectly valid uint64, which I then use [(gen_bq_schema.bigquery)={type_override: 'NUMERIC'}] to override the schema generation. As the data type conversions suggests,

But when I try to infer the schema from the protobuffer the uint64 error is raised.

I created a function in the same style as isSupportedUintType:

// isUint64Type reports whether t is a uint64 type, this exceptional
// type can be represented by the BigQuery NUMERIC type
func isUint64Type(t reflect.Type) bool {
	switch t.Kind() {
	case reflect.Uint64:
		return true
	default:
		return false
	}
}

Calling the function between lines 462 & 436 of schema.go :

if isUint64Type(rt) {
	return &FieldSchema{Required: !nullable, Type: NumericFieldType}, nil
}

Yielded a successful result. Could this be used? My project is rather dependent on it as I have a few thousand sensors reporting in that use protobuffers for their measurements.

RobHumphris avatar May 16 '23 17:05 RobHumphris

Can you walk me through how your overall flow a bit? You mention a proto annotation presumably from https://github.com/GoogleCloudPlatform/protoc-gen-bq-schema, which emits schema for a proto, but you're also talking about InferSchema, which emits a schema for a native go datastructure but doesn't have any special awareness of protobuf.

shollyman avatar May 16 '23 18:05 shollyman

Sure thing. We have a large number of protobufs that define the communications between our IoT assets and the cloud. I generate the structures with the Go plugin for protoc and as of this tech spike I create BigQuery schemas (using protoc-gen-bq-schema) to define the BigQuery table. Now I want to do a streaming insert to the table so use inferSchema via the function:

func (c *BigQueryClient[T]) StreamingWrite(ctx context.Context, o any, ts ...int64) error {
	inserter := c.table(ts...).Inserter()
	err := inserter.Put(ctx, o)
	if err != nil {
		return fmt.Errorf("inserter.Put: %w", err)
	}
	return nil
}

The whole idea is that we use generics to avoid having to write and maintain all the DAL mapping.

RobHumphris avatar May 16 '23 19:05 RobHumphris

Thanks for the details.

I think you probably want to use the new write API, rather than old streaming insert method that gets called in that snippet. With the newer API, you just need to serialize your messages to bytes and send them. Take a look at https://pkg.go.dev/cloud.google.com/go/bigquery/storage/managedwriter, which is the API corresponding to the data type mapping you referenced in the initial report.

shollyman avatar May 16 '23 19:05 shollyman

Thanks I will look at that, but before I disappeared down that rabbit hole with the fear that I would run into the self-same issue I wanted to use my existing code base.

RobHumphris avatar May 16 '23 19:05 RobHumphris