Skip to content

Commit 8c1450f

Browse files
authored
Merge pull request #62 from dimkonko/ISSUE-60_implement_get_view_names
ISSUE-60: Implement get_view_names()
2 parents 9f47d0b + e7135eb commit 8c1450f

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
@@ -293,6 +295,11 @@ def __init__(
293295
def dbapi(cls):
294296
return dbapi
295297

298+
@staticmethod
299+
def _build_formatted_table_id(table):
300+
"""Build '<dataset_id>.<table_id>' string using given table."""
301+
return "{}.{}".format(table.reference.dataset_id, table.table_id)
302+
296303
@staticmethod
297304
def _add_default_dataset_to_job_config(job_config, project_id, dataset_id):
298305
# If dataset_id is set, then we know the job_config isn't None
@@ -359,6 +366,26 @@ def _json_deserializer(self, row):
359366
"""
360367
return row
361368

369+
def _get_table_or_view_names(self, connection, table_type, schema=None):
370+
current_schema = schema or self.dataset_id
371+
get_table_name = self._build_formatted_table_id \
372+
if self.dataset_id is None else \
373+
operator.attrgetter("table_id")
374+
375+
client = connection.connection._client
376+
datasets = client.list_datasets()
377+
378+
result = []
379+
for dataset in datasets:
380+
if current_schema is not None and current_schema != dataset.dataset_id:
381+
continue
382+
383+
tables = client.list_tables(dataset.reference)
384+
for table in tables:
385+
if table_type == table.table_type:
386+
result.append(get_table_name(table))
387+
return result
388+
362389
@staticmethod
363390
def _split_table_name(full_table_name):
364391
# Split full_table_name to get project, dataset and table name
@@ -474,23 +501,13 @@ def get_table_names(self, connection, schema=None, **kw):
474501
if isinstance(connection, Engine):
475502
connection = connection.connect()
476503

477-
datasets = connection.connection._client.list_datasets()
478-
result = []
479-
for d in datasets:
480-
if schema is not None and d.dataset_id != schema:
481-
continue
504+
return self._get_table_or_view_names(connection, "TABLE", schema)
482505

483-
if self.dataset_id is not None and d.dataset_id != self.dataset_id:
484-
continue
506+
def get_view_names(self, connection, schema=None, **kw):
507+
if isinstance(connection, Engine):
508+
connection = connection.connect()
485509

486-
tables = connection.connection._client.list_tables(d.reference)
487-
for t in tables:
488-
if self.dataset_id is None:
489-
table_name = d.dataset_id + '.' + t.table_id
490-
else:
491-
table_name = t.table_id
492-
result.append(table_name)
493-
return result
510+
return self._get_table_or_view_names(connection, "VIEW", schema)
494511

495512
def do_rollback(self, dbapi_connection):
496513
# 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
@@ -287,11 +287,13 @@ def test_tables_list(engine, engine_using_test_dataset):
287287
assert 'test_pybigquery.sample' in tables
288288
assert 'test_pybigquery.sample_one_row' in tables
289289
assert 'test_pybigquery.sample_dml' in tables
290+
assert 'test_pybigquery.sample_view' not in tables
290291

291292
tables = engine_using_test_dataset.table_names()
292293
assert 'sample' in tables
293294
assert 'sample_one_row' in tables
294295
assert 'sample_dml' in tables
296+
assert 'sample_view' not in tables
295297

296298

297299
def test_group_by(session, table, session_using_test_dataset, table_using_test_dataset):
@@ -477,15 +479,27 @@ def test_table_names_in_schema(inspector, inspector_using_test_dataset):
477479
assert 'test_pybigquery.sample' in tables
478480
assert 'test_pybigquery.sample_one_row' in tables
479481
assert 'test_pybigquery.sample_dml' in tables
482+
assert 'test_pybigquery.sample_view' not in tables
480483
assert len(tables) == 3
481484

482485
tables = inspector_using_test_dataset.get_table_names()
483486
assert 'sample' in tables
484487
assert 'sample_one_row' in tables
485488
assert 'sample_dml' in tables
489+
assert 'sample_view' not in tables
486490
assert len(tables) == 3
487491

488492

493+
def test_view_names(inspector, inspector_using_test_dataset):
494+
view_names = inspector.get_view_names()
495+
assert "test_pybigquery.sample_view" in view_names
496+
assert "test_pybigquery.sample" not in view_names
497+
498+
view_names = inspector_using_test_dataset.get_view_names()
499+
assert "sample_view" in view_names
500+
assert "sample" not in view_names
501+
502+
489503
def test_get_indexes(inspector, inspector_using_test_dataset):
490504
for table in ['test_pybigquery.sample', 'test_pybigquery.sample_one_row']:
491505
indexes = inspector.get_indexes('test_pybigquery.sample')

0 commit comments

Comments
 (0)