Skip to content

Commit

Permalink
feat(bigquery): add location parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
cpcloud committed Nov 15, 2023
1 parent 60e7900 commit d652dbb
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions ibis/backends/bigquery/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,11 @@ class Backend(BaseSQLBackend, CanCreateSchema):

def __init__(self, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
self._session_dataset: str | None = None
self._session_dataset: bq.DatasetReference | None = None
self._query_cache.lookup = lambda name: self.table(
name, schema=self._session_dataset, database=self.billing_project
name,
schema=self._session_dataset.dataset_id,
database=self._session_dataset.project,
).op()

def _register_in_memory_table(self, op: ops.InMemoryTable) -> None:
Expand Down Expand Up @@ -166,6 +168,7 @@ def do_connect(
partition_column: str | None = "PARTITIONTIME",
client: bq.Client | None = None,
storage_client: bqstorage.BigQueryReadClient | None = None,
location: str | None = None,
) -> Backend:
"""Create a `Backend` for use with Ibis.
Expand Down Expand Up @@ -216,6 +219,8 @@ def do_connect(
A ``BigQueryReadClient`` from the
``google.cloud.bigquery_storage_v1`` package. If not set, one is
created using the ``project_id`` and ``credentials``.
location
Default location for BigQuery objects.
Returns
-------
Expand Down Expand Up @@ -270,6 +275,7 @@ def do_connect(
project=self.billing_project,
credentials=credentials,
client_info=_create_client_info(application_name),
location=location,
)

self.client.default_query_job_config = bq.QueryJobConfig(
Expand Down Expand Up @@ -421,7 +427,10 @@ def _make_session(self) -> tuple[str, str]:
)
query.result()

self._session_dataset = query.destination.dataset_id
self._session_dataset = bq.DatasetReference(
project=query.destination.project,
dataset_id=query.destination.dataset_id,
)

def _get_schema_using_query(self, query: str) -> sch.Schema:
self._make_session()
Expand Down Expand Up @@ -819,7 +828,10 @@ def create_table(
self._register_in_memory_tables(obj)

if temp:
dataset = self._session_dataset
dataset = self._session_dataset.dataset_id
if database is not None:
raise com.IbisInputError("Cannot specify database for temporary table")
database = self._session_dataset.project
else:
dataset = database or self.current_schema

Expand Down Expand Up @@ -931,7 +943,11 @@ def _load_into_cache(self, name, expr):
self.create_table(name, expr, schema=expr.schema(), temp=True)

def _clean_up_cached_table(self, op):
self.drop_table(op.name, schema=self._session_dataset)
self.drop_table(
op.name,
schema=self._session_dataset.dataset_id,
database=self._session_dataset.project,
)


def compile(expr, params=None, **kwargs):
Expand Down

0 comments on commit d652dbb

Please sign in to comment.