@@ -1081,7 +1081,6 @@ def __init__(
1081
1081
self ,
1082
1082
arraysize = 5000 ,
1083
1083
credentials_path = None ,
1084
- project_id = None ,
1085
1084
billing_project_id = None ,
1086
1085
location = None ,
1087
1086
credentials_info = None ,
@@ -1095,7 +1094,7 @@ def __init__(
1095
1094
self .credentials_path = credentials_path
1096
1095
self .credentials_info = credentials_info
1097
1096
self .credentials_base64 = credentials_base64
1098
- self .project_id = project_id
1097
+ self .project_id = None
1099
1098
self .billing_project_id = billing_project_id
1100
1099
self .location = location
1101
1100
self .identifier_preparer = self .preparer (self )
@@ -1119,18 +1118,20 @@ def _build_formatted_table_id(table):
1119
1118
"""Build '<dataset_id>.<table_id>' string using given table."""
1120
1119
return "{}.{}" .format (table .reference .dataset_id , table .table_id )
1121
1120
1122
- @staticmethod
1123
- def _add_default_dataset_to_job_config (job_config : QueryJobConfig , project_id , dataset_id ):
1124
- # If dataset_id is set, then we know the job_config isn't None
1125
- if project_id :
1126
- dataset = ConnectionProperty (key = 'dataset_project_id' , value = project_id )
1127
- job_config .connection_properties = [ dataset ]
1128
- if dataset_id :
1129
- # If project_id is missing, use default project_id for the current environment
1121
+
1122
+ def create_job_config (self , provided_config : QueryJobConfig ):
1123
+ project_id = self .project_id
1124
+ if self .dataset_id is None and project_id == self .billing_project_id :
1125
+ return provided_config
1126
+ job_config = provided_config or QueryJobConfig ()
1127
+ if project_id != self .billing_project_id :
1128
+ job_config .connection_properties = [ConnectionProperty (key = "dataset_project_id" , value = project_id )]
1129
+ if self .dataset_id :
1130
1130
if not project_id :
1131
1131
_ , project_id = auth .default ()
1132
+ job_config .default_dataset = "{}.{}" .format (project_id , self .dataset_id )
1133
+ return job_config
1132
1134
1133
- job_config .default_dataset = "{}.{}" .format (project_id , dataset_id )
1134
1135
1135
1136
def do_execute (self , cursor , statement , parameters , context = None ):
1136
1137
kwargs = {}
@@ -1140,13 +1141,13 @@ def do_execute(self, cursor, statement, parameters, context=None):
1140
1141
1141
1142
def create_connect_args (self , url ):
1142
1143
(
1143
- project_id ,
1144
+ self . project_id ,
1144
1145
location ,
1145
1146
dataset_id ,
1146
1147
arraysize ,
1147
1148
credentials_path ,
1148
1149
credentials_base64 ,
1149
- default_query_job_config ,
1150
+ provided_job_config ,
1150
1151
list_tables_page_size ,
1151
1152
user_supplied_client ,
1152
1153
) = parse_url (url )
@@ -1157,17 +1158,10 @@ def create_connect_args(self, url):
1157
1158
self .credentials_path = credentials_path or self .credentials_path
1158
1159
self .credentials_base64 = credentials_base64 or self .credentials_base64
1159
1160
self .dataset_id = dataset_id
1160
- from google .cloud .bigquery import QueryJobConfig
1161
- if default_query_job_config is None :
1162
- default_query_job_config = QueryJobConfig ()
1163
1161
if self .billing_project_id is None :
1164
- self .billing_project_id = project_id
1165
- if self .project_id is None :
1166
- self .project_id = project_id
1162
+ self .billing_project_id = self .project_id
1167
1163
1168
- self ._add_default_dataset_to_job_config (
1169
- default_query_job_config , project_id , dataset_id
1170
- )
1164
+ default_query_job_config = self .create_job_config (provided_job_config )
1171
1165
1172
1166
if user_supplied_client :
1173
1167
# The user is expected to supply a client with
0 commit comments