Skip to content

Commit 9a1d333

Browse files
BigQuery: support multiple locations (#7540)
1 parent 56c06ad commit 9a1d333

File tree

1 file changed

+33
-26
lines changed

1 file changed

+33
-26
lines changed

redash/query_runner/big_query.py

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -220,11 +220,12 @@ def _get_query_result(self, jobs, query):
220220
job_data = self._get_job_data(query)
221221
insert_response = jobs.insert(projectId=project_id, body=job_data).execute()
222222
self.current_job_id = insert_response["jobReference"]["jobId"]
223+
self.current_job_location = insert_response["jobReference"]["location"]
223224
current_row = 0
224225
query_reply = _get_query_results(
225226
jobs,
226227
project_id=project_id,
227-
location=self._get_location(),
228+
location=self.current_job_location,
228229
job_id=self.current_job_id,
229230
start_index=current_row,
230231
)
@@ -241,13 +242,11 @@ def _get_query_result(self, jobs, query):
241242

242243
query_result_request = {
243244
"projectId": project_id,
244-
"jobId": query_reply["jobReference"]["jobId"],
245+
"jobId": self.current_job_id,
245246
"startIndex": current_row,
247+
"location": self.current_job_location,
246248
}
247249

248-
if self._get_location():
249-
query_result_request["location"] = self._get_location()
250-
251250
query_reply = jobs.getQueryResults(**query_result_request).execute()
252251

253252
columns = [
@@ -314,33 +313,41 @@ def get_schema(self, get_stats=False):
314313
WHERE table_schema NOT IN ('information_schema')
315314
"""
316315

316+
location_dataset_ids = {}
317317
schema = {}
318-
queries = []
319318
for dataset in datasets:
320319
dataset_id = dataset["datasetReference"]["datasetId"]
321320
location = dataset["location"]
322321
if self._get_location() and location != self._get_location():
323322
logger.debug("dataset location is different: %s", location)
324323
continue
325-
query = query_base.format(dataset_id=dataset_id)
326-
queries.append(query)
327-
328-
query = "\nUNION ALL\n".join(queries)
329-
results, error = self.run_query(query, None)
330-
if error is not None:
331-
self._handle_run_query_error(error)
332-
333-
for row in results["rows"]:
334-
table_name = "{0}.{1}".format(row["table_schema"], row["table_name"])
335-
if table_name not in schema:
336-
schema[table_name] = {"name": table_name, "columns": []}
337-
schema[table_name]["columns"].append(
338-
{
339-
"name": row["field_path"],
340-
"type": row["data_type"],
341-
"description": row["description"],
342-
}
343-
)
324+
325+
if location not in location_dataset_ids:
326+
location_dataset_ids[location] = []
327+
location_dataset_ids[location].append(dataset_id)
328+
329+
for location, datasets in location_dataset_ids.items():
330+
queries = []
331+
for dataset_id in datasets:
332+
query = query_base.format(dataset_id=dataset_id)
333+
queries.append(query)
334+
335+
query = "\nUNION ALL\n".join(queries)
336+
results, error = self.run_query(query, None)
337+
if error is not None:
338+
self._handle_run_query_error(error)
339+
340+
for row in results["rows"]:
341+
table_name = "{0}.{1}".format(row["table_schema"], row["table_name"])
342+
if table_name not in schema:
343+
schema[table_name] = {"name": table_name, "columns": []}
344+
schema[table_name]["columns"].append(
345+
{
346+
"name": row["field_path"],
347+
"type": row["data_type"],
348+
"description": row["description"],
349+
}
350+
)
344351

345352
return list(schema.values())
346353

@@ -374,7 +381,7 @@ def run_query(self, query, user):
374381
self._get_bigquery_service().jobs().cancel(
375382
projectId=self._get_project_id(),
376383
jobId=self.current_job_id,
377-
location=self._get_location(),
384+
location=self.current_job_location,
378385
).execute()
379386

380387
raise

0 commit comments

Comments
 (0)