Skip to content

Commit bd3605f

Browse files
authored
Merge pull request #53 from bluelabsio/default_dataset_in_has_table
Prefer explicitly provided dataset over default dataset in lookup
2 parents dc87fbd + 6561b22 commit bd3605f

File tree

3 files changed

+27
-16
lines changed

3 files changed

+27
-16
lines changed

packages/sqlalchemy-bigquery/pybigquery/sqlalchemy_bigquery.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,9 @@ def _split_table_name(self, full_table_name):
357357
project = None
358358

359359
table_name_split = full_table_name.split('.')
360-
if len(table_name_split) == 2:
360+
if len(table_name_split) == 1:
361+
table_name = full_table_name
362+
elif len(table_name_split) == 2:
361363
dataset, table_name = table_name_split
362364
elif len(table_name_split) == 3:
363365
project, dataset, table_name = table_name_split
@@ -368,17 +370,12 @@ def _get_table(self, connection, table_name, schema=None):
368370
if isinstance(connection, Engine):
369371
connection = connection.connect()
370372

371-
table_name_prepared = dataset = project = None
372-
if self.dataset_id:
373-
table_name_prepared = table_name
374-
dataset = self.dataset_id
375-
project = None
376-
377-
else:
378-
project, dataset, table_name_prepared = self._split_table_name(table_name)
379-
if dataset is None and schema is not None:
380-
table_name_prepared = table_name
373+
project, dataset, table_name_prepared = self._split_table_name(table_name)
374+
if dataset is None:
375+
if schema is not None:
381376
dataset = schema
377+
elif self.dataset_id:
378+
dataset = self.dataset_id
382379

383380
table = connection.connection._client.dataset(dataset, project=project).table(table_name_prepared)
384381
try:

packages/sqlalchemy-bigquery/scripts/load_test_data.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
bq mk test_pybigquery
22
bq mk --data_location=asia-northeast1 test_pybigquery_location
3+
bq mk test_pybigquery_alt
34

45
bq rm -f -t test_pybigquery.sample
6+
bq rm -f -t test_pybigquery_alt.sample_alt
57
bq rm -f -t test_pybigquery.sample_one_row
68
bq rm -f -t test_pybigquery.sample_dml
79
bq rm -f -t test_pybigquery_location.sample_one_row
810

911
bq mk --table --schema=$(dirname $0)/schema.json --time_partitioning_field timestamp --clustering_fields integer,string test_pybigquery.sample
12+
bq mk --table --schema=$(dirname $0)/schema.json --time_partitioning_field timestamp --clustering_fields integer,string test_pybigquery_alt.sample_alt
1013
bq load --source_format=NEWLINE_DELIMITED_JSON --schema=$(dirname $0)/schema.json test_pybigquery.sample $(dirname $0)/sample.json
1114

1215
bq mk --table --schema=$(dirname $0)/schema.json --time_partitioning_field timestamp --clustering_fields integer,string test_pybigquery.sample_one_row

packages/sqlalchemy-bigquery/test/test_sqlalchemy_bigquery.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -199,9 +199,12 @@ def test_engine_with_dataset(engine_using_test_dataset):
199199
rows = table_one_row.select().execute().fetchall()
200200
assert list(rows[0]) == ONE_ROW_CONTENTS_EXPANDED
201201

202-
# Table name shouldn't include dataset
203-
with pytest.raises(Exception):
204-
table_one_row = Table('test_pybigquery.sample_one_row', MetaData(bind=engine_using_test_dataset), autoload=True)
202+
table_one_row = Table('test_pybigquery.sample_one_row', MetaData(bind=engine_using_test_dataset), autoload=True)
203+
rows = table_one_row.select().execute().fetchall()
204+
# verify that we are pulling from the specifically-named dataset,
205+
# instead of pulling from the default dataset of the engine (which
206+
# does not have this table at all)
207+
assert list(rows[0]) == ONE_ROW_CONTENTS_EXPANDED
205208

206209

207210
def test_dataset_location(engine_with_location):
@@ -478,6 +481,14 @@ def test_has_table(engine, engine_using_test_dataset):
478481
assert engine.has_table('sample', 'test_pybigquery') is True
479482
assert engine.has_table('test_pybigquery.sample') is True
480483

484+
assert engine.has_table('sample_alt', 'test_pybigquery_alt') is True
485+
assert engine.has_table('test_pybigquery_alt.sample_alt') is True
486+
481487
assert engine_using_test_dataset.has_table('sample') is True
482-
with pytest.raises(Exception):
483-
assert engine_using_test_dataset.has_table('test_pybigquery.sample') is True
488+
assert engine_using_test_dataset.has_table('sample', 'test_pybigquery') is True
489+
assert engine_using_test_dataset.has_table('test_pybigquery.sample') is True
490+
491+
assert engine_using_test_dataset.has_table('sample_alt') is False
492+
493+
assert engine_using_test_dataset.has_table('sample_alt', 'test_pybigquery_alt') is True
494+
assert engine_using_test_dataset.has_table('test_pybigquery_alt.sample_alt') is True

0 commit comments

Comments
 (0)