Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 14 additions & 13 deletions pybigquery/sqlalchemy_bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
from google.cloud import bigquery
from google.cloud.bigquery import dbapi, QueryJobConfig
from google.cloud.bigquery.schema import SchemaField
from google.cloud.bigquery.table import EncryptionConfiguration
from google.cloud.bigquery.dataset import DatasetReference
from google.cloud.bigquery.table import EncryptionConfiguration, TableReference
from google.oauth2 import service_account
from google.api_core.exceptions import NotFound
from sqlalchemy.exc import NoSuchTableError
Expand Down Expand Up @@ -350,7 +349,8 @@ def _json_deserializer(self, row):
"""
return row

def _split_table_name(self, full_table_name):
@staticmethod
def _split_table_name(full_table_name):
# Split full_table_name to get project, dataset and table name
dataset = None
table_name = None
Expand All @@ -370,19 +370,20 @@ def _get_table(self, connection, table_name, schema=None):
if isinstance(connection, Engine):
connection = connection.connect()

project, dataset, table_name_prepared = self._split_table_name(table_name)
if dataset is None:
if schema is not None:
dataset = schema
elif self.dataset_id:
dataset = self.dataset_id
client = connection.connection._client

project_id, dataset_id, table_id = self._split_table_name(table_name)
project_id = project_id or client.project
dataset_id = dataset_id or schema or self.dataset_id

table = connection.connection._client.dataset(dataset, project=project).table(table_name_prepared)
table_ref = TableReference.from_string("{}.{}.{}".format(
project_id, dataset_id, table_id
))
try:
t = connection.connection._client.get_table(table)
except NotFound as e:
table = client.get_table(table_ref)
except NotFound:
raise NoSuchTableError(table_name)
return t
return table

def has_table(self, connection, table_name, schema=None):
try:
Expand Down
2 changes: 2 additions & 0 deletions test/test_sqlalchemy_bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,8 @@ def test_get_columns(inspector, inspector_using_test_dataset):
def test_has_table(engine, engine_using_test_dataset):
assert engine.has_table('sample', 'test_pybigquery') is True
assert engine.has_table('test_pybigquery.sample') is True
assert engine.has_table('test_pybigquery.nonexistent_table') is False
assert engine.has_table('nonexistent_table', 'nonexistent_dataset') is False

assert engine.has_table('sample_alt', 'test_pybigquery_alt') is True
assert engine.has_table('test_pybigquery_alt.sample_alt') is True
Expand Down