Skip to content

Commit

Permalink
feat(bigquery): add media options to LoadConfig (#8640)
Browse files Browse the repository at this point in the history
  • Loading branch information
alvarowolfx committed Oct 4, 2023
1 parent 22ab199 commit 62baf56
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
4 changes: 2 additions & 2 deletions bigquery/bigquery.go
Expand Up @@ -140,11 +140,11 @@ func (c *Client) Close() error {
}

// Calls the Jobs.Insert RPC and returns a Job.
func (c *Client) insertJob(ctx context.Context, job *bq.Job, media io.Reader) (*Job, error) {
func (c *Client) insertJob(ctx context.Context, job *bq.Job, media io.Reader, mediaOpts ...googleapi.MediaOption) (*Job, error) {
call := c.bqs.Jobs.Insert(c.projectID, job).Context(ctx)
setClientHeader(call.Header())
if media != nil {
call.Media(media)
call.Media(media, mediaOpts...)
}
var res *bq.Job
var err error
Expand Down
7 changes: 6 additions & 1 deletion bigquery/integration_test.go
Expand Up @@ -1461,6 +1461,10 @@ func TestIntegration_Load(t *testing.T) {
loader := table.LoaderFrom(rs)
loader.WriteDisposition = WriteTruncate
loader.Labels = map[string]string{"test": "go"}
loader.MediaOptions = []googleapi.MediaOption{
googleapi.ContentType("text/csv"),
googleapi.ChunkSize(googleapi.MinUploadChunkSize),
}
job, err := loader.Run(ctx)
if err != nil {
t.Fatal(err)
Expand All @@ -1480,7 +1484,8 @@ func TestIntegration_Load(t *testing.T) {
cmp.AllowUnexported(Table{}),
cmpopts.IgnoreUnexported(Client{}, ReaderSource{}),
// returned schema is at top level, not in the config
cmpopts.IgnoreFields(FileConfig{}, "Schema"))
cmpopts.IgnoreFields(FileConfig{}, "Schema"),
cmpopts.IgnoreFields(LoadConfig{}, "MediaOptions"))
if diff != "" {
t.Errorf("got=-, want=+:\n%s", diff)
}
Expand Down
6 changes: 5 additions & 1 deletion bigquery/load.go
Expand Up @@ -21,6 +21,7 @@ import (

"cloud.google.com/go/internal/trace"
bq "google.golang.org/api/bigquery/v2"
"google.golang.org/api/googleapi"
)

// LoadConfig holds the configuration for a load job.
Expand Down Expand Up @@ -101,6 +102,9 @@ type LoadConfig struct {

// ConnectionProperties are optional key-values settings.
ConnectionProperties []*ConnectionProperty

// MediaOptions stores options for customizing media upload.
MediaOptions []googleapi.MediaOption
}

func (l *LoadConfig) toBQ() (*bq.JobConfiguration, io.Reader) {
Expand Down Expand Up @@ -210,7 +214,7 @@ func (l *Loader) Run(ctx context.Context) (j *Job, err error) {
defer func() { trace.EndSpan(ctx, err) }()

job, media := l.newJob()
return l.c.insertJob(ctx, job, media)
return l.c.insertJob(ctx, job, media, l.LoadConfig.MediaOptions...)
}

func (l *Loader) newJob() (*bq.Job, io.Reader) {
Expand Down

0 comments on commit 62baf56

Please sign in to comment.