Skip to content

Commit f2f3752

Browse files
committed
ISSUE-60: Implement get_view_names()
1 parent 2a41ed7 commit f2f3752

File tree

3 files changed

+49
-15
lines changed

3 files changed

+49
-15
lines changed

pybigquery/sqlalchemy_bigquery.py

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
from __future__ import absolute_import
44
from __future__ import unicode_literals
55

6+
import operator
7+
68
from google import auth
79
from google.cloud import bigquery
810
from google.cloud.bigquery import dbapi, QueryJobConfig
@@ -283,6 +285,11 @@ def __init__(
283285
def dbapi(cls):
284286
return dbapi
285287

288+
@staticmethod
289+
def _build_formatted_table_id(table):
290+
"""Build '<dataset_id>.<table_id>' string using given table."""
291+
return "{}.{}".format(table.reference.dataset_id, table.table_id)
292+
286293
@staticmethod
287294
def _add_default_dataset_to_job_config(job_config, project_id, dataset_id):
288295
# If dataset_id is set, then we know the job_config isn't None
@@ -349,6 +356,26 @@ def _json_deserializer(self, row):
349356
"""
350357
return row
351358

359+
def _get_table_or_view_names(self, connection, table_type, schema=None):
360+
current_schema = schema or self.dataset_id
361+
get_table_name = self._build_formatted_table_id \
362+
if self.dataset_id is None else \
363+
operator.attrgetter("table_id")
364+
365+
client = connection.connection._client
366+
datasets = client.list_datasets()
367+
368+
result = []
369+
for dataset in datasets:
370+
if current_schema is not None and current_schema != dataset.dataset_id:
371+
continue
372+
373+
tables = client.list_tables(dataset.reference)
374+
for table in tables:
375+
if table_type == table.table_type:
376+
result.append(get_table_name(table))
377+
return result
378+
352379
@staticmethod
353380
def _split_table_name(full_table_name):
354381
# Split full_table_name to get project, dataset and table name
@@ -464,23 +491,13 @@ def get_table_names(self, connection, schema=None, **kw):
464491
if isinstance(connection, Engine):
465492
connection = connection.connect()
466493

467-
datasets = connection.connection._client.list_datasets()
468-
result = []
469-
for d in datasets:
470-
if schema is not None and d.dataset_id != schema:
471-
continue
494+
return self._get_table_or_view_names(connection, "TABLE", schema)
472495

473-
if self.dataset_id is not None and d.dataset_id != self.dataset_id:
474-
continue
496+
def get_view_names(self, connection, schema=None, **kw):
497+
if isinstance(connection, Engine):
498+
connection = connection.connect()
475499

476-
tables = connection.connection._client.list_tables(d.reference)
477-
for t in tables:
478-
if self.dataset_id is None:
479-
table_name = d.dataset_id + '.' + t.table_id
480-
else:
481-
table_name = t.table_id
482-
result.append(table_name)
483-
return result
500+
return self._get_table_or_view_names(connection, "VIEW", schema)
484501

485502
def do_rollback(self, dbapi_connection):
486503
# BigQuery has no support for transactions.

scripts/load_test_data.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ bq rm -f -t test_pybigquery.sample
66
bq rm -f -t test_pybigquery_alt.sample_alt
77
bq rm -f -t test_pybigquery.sample_one_row
88
bq rm -f -t test_pybigquery.sample_dml
9+
bq rm -f -t test_pybigquery.sample_view
910
bq rm -f -t test_pybigquery_location.sample_one_row
1011

1112
bq mk --table --schema=$(dirname $0)/schema.json --time_partitioning_field timestamp --clustering_fields integer,string test_pybigquery.sample
@@ -17,3 +18,5 @@ bq load --source_format=NEWLINE_DELIMITED_JSON --schema=$(dirname $0)/schema.jso
1718

1819
bq --location=asia-northeast1 load --source_format=NEWLINE_DELIMITED_JSON --schema=$(dirname $0)/schema.json test_pybigquery_location.sample_one_row $(dirname $0)/sample_one_row.json
1920
bq mk --schema=$(dirname $0)/schema.json -t test_pybigquery.sample_dml
21+
22+
bq mk --use_legacy_sql=false --view 'SELECT string FROM test_pybigquery.sample' test_pybigquery.sample_view

test/test_sqlalchemy_bigquery.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,11 +283,13 @@ def test_tables_list(engine, engine_using_test_dataset):
283283
assert 'test_pybigquery.sample' in tables
284284
assert 'test_pybigquery.sample_one_row' in tables
285285
assert 'test_pybigquery.sample_dml' in tables
286+
assert 'test_pybigquery.sample_view' not in tables
286287

287288
tables = engine_using_test_dataset.table_names()
288289
assert 'sample' in tables
289290
assert 'sample_one_row' in tables
290291
assert 'sample_dml' in tables
292+
assert 'sample_view' not in tables
291293

292294

293295
def test_group_by(session, table, session_using_test_dataset, table_using_test_dataset):
@@ -436,15 +438,27 @@ def test_table_names_in_schema(inspector, inspector_using_test_dataset):
436438
assert 'test_pybigquery.sample' in tables
437439
assert 'test_pybigquery.sample_one_row' in tables
438440
assert 'test_pybigquery.sample_dml' in tables
441+
assert 'test_pybigquery.sample_view' not in tables
439442
assert len(tables) == 3
440443

441444
tables = inspector_using_test_dataset.get_table_names()
442445
assert 'sample' in tables
443446
assert 'sample_one_row' in tables
444447
assert 'sample_dml' in tables
448+
assert 'sample_view' not in tables
445449
assert len(tables) == 3
446450

447451

452+
def test_view_names(inspector, inspector_using_test_dataset):
453+
view_names = inspector.get_view_names()
454+
assert "test_pybigquery.sample_view" in view_names
455+
assert "test_pybigquery.sample" not in view_names
456+
457+
view_names = inspector_using_test_dataset.get_view_names()
458+
assert "sample_view" in view_names
459+
assert "sample" not in view_names
460+
461+
448462
def test_get_indexes(inspector, inspector_using_test_dataset):
449463
for table in ['test_pybigquery.sample', 'test_pybigquery.sample_one_row']:
450464
indexes = inspector.get_indexes('test_pybigquery.sample')

0 commit comments

Comments
 (0)