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: schema field of type NUMERIC is not assignable to struct field #6492

Closed
ejstembler opened this issue Aug 9, 2022 · 3 comments
Closed
Assignees
Labels
api: bigquery Issues related to the BigQuery API. priority: p2 Moderately-important priority. Fix may not be included in next release. type: docs Improvement to the documentation for an API.

Comments

@ejstembler
Copy link

Client

BigQuery

Environment

golang:1.18

Go Environment

go version go1.18.5 darwin/amd64

GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/my-user/Library/Caches/go-build"
GOENV="/Users/my-user/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/my-user/.asdf/installs/golang/1.18.5/packages/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/my-user/.asdf/installs/golang/1.18.5/packages"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/Users/my-user/.asdf/installs/golang/1.18.5/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/Users/my-user/.asdf/installs/golang/1.18.5/go/pkg/tool/darwin_amd64"
GOVCS=""
GOVERSION="go1.18.5"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/my-user/Projects/my-project/go.mod"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/j3/zqjvh17x5hz0d87m0wtkq6cm0000gn/T/go-build792818999=/tmp/go-build -gno-record-gcc-switches -fno-common"

Code

package main

type MyRecord struct {
    Quantity float64 `bigquery:"Quantity,nullable"` // NUMERIC BigQuery type
    // other fields...
}
package main

func (repo *MyBigQueryRepository) FindMyRecordById(id string) (MyRecord, error) {

        // snip...

	var record MyRecord
	for {

		err := it.Next(&record)
		if err == iterator.Done {
			break
		}
		if err != nil {
			return MyRecord{}, err
		}
	}
	return record, nil
}

Expected behavior

BigQuery row loaded into struct.

Actual behavior

bigquery: schema field Quantity of type NUMERIC is not assignable to struct field Quantity of type float64

Notes

I have a record struct in Go which attempts to match the corresponding BigQuery schema. However, I haven't found any documentation on how certain BigQuery types should be matched to Go's types.

For this particular example, nullable NUMERIC, I've tried using: float64, bigquery.NullFloat64 and I've even tried using null.Float from gopkg.in/guregu/null.v4 which I used for Postgres databases.

It would be nice if there were documentation somewhere which illustrated the BiqQuery data types to Go data types. For my particular table, I have other BigQuery data types I need to support besides nullable NUMERIC, nullable BIGNUMERIC, nullable BOOLEAN, nullable DATE, nullable TIMESTAMP, etc...

@ejstembler ejstembler added the triage me I really want to be triaged. label Aug 9, 2022
@product-auto-label product-auto-label bot added the api: bigquery Issues related to the BigQuery API. label Aug 9, 2022
@ejstembler
Copy link
Author

After reading the unit tests, bigquery/value_test.go , I discovered that nullable NUMERIC maps to *big.Rat from math/big.

This should be documented somewhere.

@shollyman
Copy link
Contributor

Sorry this wasn't clearer. It looks like we've allowed the docs to drift a bit. https://pkg.go.dev/cloud.google.com/go/bigquery#RowIterator.Next doesn't inform the user (BQ->Go datatypes), but
https://pkg.go.dev/cloud.google.com/go/bigquery#InferSchema does (Go->BQ).

Reopening this to clarify the docs a bit more.

@shollyman shollyman reopened this Aug 9, 2022
@shollyman shollyman added priority: p2 Moderately-important priority. Fix may not be included in next release. type: docs Improvement to the documentation for an API. and removed triage me I really want to be triaged. labels Aug 9, 2022
gcf-merge-on-green bot pushed a commit that referenced this issue Aug 25, 2022
Document field type conversions for `NUMERIC` and `BIGNUMERIC` types when using the `RowIterator` for reading data from BigQuery. 

Solves issue #6492
@alvarowolfx
Copy link
Contributor

Hopefully this is resolve by #6560

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: docs Improvement to the documentation for an API.
Projects
None yet
Development

No branches or pull requests

3 participants