Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bigquery: add missing_value_interpretations as append option #8387

Closed
brachipa opened this issue Aug 6, 2023 · 2 comments · Fixed by #8686
Closed

bigquery: add missing_value_interpretations as append option #8387

brachipa opened this issue Aug 6, 2023 · 2 comments · Fixed by #8686
Assignees
Labels
api: bigquery Issues related to the BigQuery API. priority: p2 Moderately-important priority. Fix may not be included in next release. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.

Comments

@brachipa
Copy link

brachipa commented Aug 6, 2023

As describe here

The Storage Write API only populates default values when the write stream schema is missing a field that is contained in the destination table schema

So when we want to default value we need to make sure the field is missing from the schema, what makes our code ugly, we need to iterate over the schema fields and delete the column with the default value (field_with_default_value)

	md, err := bqClient.Dataset(datasetName).Table(tableName).Metadata(ctx)
	if err != nil {
		...
	}

	schema := md.Schema
	convertedSchema, err := adapt.BQSchemaToStorageTableSchema(schema)
	if err != nil {
		...
	}
	savedInd := -1
	for i, field := range convertedSchema.Fields {
		if field.Name == "field_with_default_value" {
			savedInd = i
			break
		}
	}
	if savedI > 0 {
		convertedSchema.Fields = append(convertedSchema.Fields[:savedInd], convertedSchema.Fields[savedInd+1:]...)
	}

Or:

You can also specify request-level default values in the missing_value_interpretations map within the AppendRowsRequest message. Each key is the name of a column and its value indicates how to interpret missing values.
For example, the map {'col1': NULL_VALUE, 'col2': DEFAULT_VALUE} means that all missing values in col1 are interpreted as NULL, and all missing values in col2 are interpreted as the default value set for col2 in the table schema.

Unfortunately, we have no way to pass this map into the append request!

The solution:

Add new option WithMissingValueInterpretations to pass this information

missingValueInterpretation := map[string]storagepb.AppendRowsRequest_MissingValueInterpretation{
"field_with_default_value": storagepb.AppendRowsRequest_DEFAULT_VALUE}

....

result, err = stream.AppendRows(ctx, [][]byte{b}, managedwriter.WithMissingValueInterpretations(missingValueInterpretation))
@brachipa brachipa added the triage me I really want to be triaged. label Aug 6, 2023
@product-auto-label product-auto-label bot added the api: bigquery Issues related to the BigQuery API. label Aug 6, 2023
@shollyman shollyman added type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design. priority: p2 Moderately-important priority. Fix may not be included in next release. and removed triage me I really want to be triaged. labels Aug 7, 2023
@shollyman
Copy link
Contributor

Thanks for the request.

Currently the missing_value_interpretations is per-request in the underlying API. Would you expect the option in managedwriter to behave more like schema, which persists across requests, as a per-append option?

@brachipa
Copy link
Author

brachipa commented Aug 7, 2023

@shollyman yes, per request is ok.
I already submit a pr with a fix.
#8388

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: bigquery Issues related to the BigQuery API. priority: p2 Moderately-important priority. Fix may not be included in next release. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants