@@ -88,6 +88,7 @@ type Config struct {
8888 WriteMode string `yaml:"writeMode"`
8989 AllowedDatasets StringOrStringSlice `yaml:"allowedDatasets"`
9090 UseClientOAuth string `yaml:"useClientOAuth"`
91+ QuotaProject string `yaml:"quotaProject"`
9192 ImpersonateServiceAccount string `yaml:"impersonateServiceAccount"`
9293 Scopes StringOrStringSlice `yaml:"scopes"`
9394 MaxQueryResultRows int `yaml:"maxQueryResultRows"`
@@ -166,7 +167,7 @@ func (r Config) Initialize(ctx context.Context, tracer trace.Tracer) (sources.So
166167
167168 if strings .ToLower (r .UseClientOAuth ) == "false" || r .UseClientOAuth == "" {
168169 // Initializes a BigQuery Google SQL source
169- client , restService , tokenSource , err = initBigQueryConnection (ctx , tracer , r .Name , r .Project , r .Location , r .ImpersonateServiceAccount , r .Scopes )
170+ client , restService , tokenSource , err = initBigQueryConnection (ctx , tracer , r .Name , r .Project , r .Location , r .QuotaProject , r . ImpersonateServiceAccount , r .Scopes )
170171 if err != nil {
171172 return nil , fmt .Errorf ("error creating client from ADC: %w" , err )
172173 }
@@ -183,7 +184,7 @@ func (r Config) Initialize(ctx context.Context, tracer trace.Tracer) (sources.So
183184 s .AuthTokenHeaderName = r .UseClientOAuth
184185 }
185186 // use client OAuth
186- baseClientCreator , err := newBigQueryClientCreator (ctx , tracer , r .Project , r .Location , r .Name )
187+ baseClientCreator , err := newBigQueryClientCreator (ctx , tracer , r .Project , r .Location , r .QuotaProject , r . Name )
187188 if err != nil {
188189 return nil , fmt .Errorf ("error constructing client creator: %w" , err )
189190 }
@@ -446,6 +447,10 @@ func (s *Source) BigQueryLocation() string {
446447 return s .Location
447448}
448449
450+ func (s * Source ) BigQueryQuotaProject () string {
451+ return s .QuotaProject
452+ }
453+
449454func (s * Source ) BigQueryTokenSource () oauth2.TokenSource {
450455 return s .TokenSource
451456}
@@ -690,6 +695,7 @@ func initBigQueryConnection(
690695 name string ,
691696 project string ,
692697 location string ,
698+ quotaProject string ,
693699 impersonateServiceAccount string ,
694700 scopes []string ,
695701) (* bigqueryapi.Client , * bigqueryrestapi.Service , oauth2.TokenSource , error ) {
@@ -741,6 +747,10 @@ func initBigQueryConnection(
741747 }
742748 }
743749
750+ if quotaProject != "" {
751+ opts = append (opts , option .WithQuotaProject (quotaProject ))
752+ }
753+
744754 // Initialize the high-level BigQuery client
745755 client , err := bigqueryapi .NewClient (ctx , project , opts ... )
746756 if err != nil {
@@ -764,6 +774,7 @@ func initBigQueryConnectionWithOAuthToken(
764774 tracer trace.Tracer ,
765775 project string ,
766776 location string ,
777+ quotaProject string ,
767778 name string ,
768779 userAgent string ,
769780 tokenString string ,
@@ -777,16 +788,24 @@ func initBigQueryConnectionWithOAuthToken(
777788 }
778789 ts := oauth2 .StaticTokenSource (token )
779790
791+ opts := []option.ClientOption {
792+ option .WithUserAgent (userAgent ),
793+ option .WithTokenSource (ts ),
794+ }
795+ if quotaProject != "" {
796+ opts = append (opts , option .WithQuotaProject (quotaProject ))
797+ }
798+
780799 // Initialize the BigQuery client with tokenSource
781- client , err := bigqueryapi .NewClient (ctx , project , option . WithUserAgent ( userAgent ), option . WithTokenSource ( ts ) )
800+ client , err := bigqueryapi .NewClient (ctx , project , opts ... )
782801 if err != nil {
783802 return nil , nil , fmt .Errorf ("failed to create BigQuery client for project %q: %w" , project , err )
784803 }
785804 client .Location = location
786805
787806 if wantRestService {
788807 // Initialize the low-level BigQuery REST service using the same credentials
789- restService , err := bigqueryrestapi .NewService (ctx , option . WithUserAgent ( userAgent ), option . WithTokenSource ( ts ) )
808+ restService , err := bigqueryrestapi .NewService (ctx , opts ... )
790809 if err != nil {
791810 return nil , nil , fmt .Errorf ("failed to create BigQuery v2 service: %w" , err )
792811 }
@@ -804,6 +823,7 @@ func newBigQueryClientCreator(
804823 tracer trace.Tracer ,
805824 project string ,
806825 location string ,
826+ quotaProject string ,
807827 name string ,
808828) (func (string , bool ) (* bigqueryapi.Client , * bigqueryrestapi.Service , error ), error ) {
809829 userAgent , err := util .UserAgentFromContext (ctx )
@@ -812,7 +832,7 @@ func newBigQueryClientCreator(
812832 }
813833
814834 return func (tokenString string , wantRestService bool ) (* bigqueryapi.Client , * bigqueryrestapi.Service , error ) {
815- return initBigQueryConnectionWithOAuthToken (ctx , tracer , project , location , name , userAgent , tokenString , wantRestService )
835+ return initBigQueryConnectionWithOAuthToken (ctx , tracer , project , location , quotaProject , name , userAgent , tokenString , wantRestService )
816836 }, nil
817837}
818838
0 commit comments