Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ISSUE-60: Implement get_view_names() #62

Merged
merged 2 commits into from
Nov 18, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
47 changes: 32 additions & 15 deletions pybigquery/sqlalchemy_bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from __future__ import absolute_import
from __future__ import unicode_literals

import operator

from google import auth
from google.cloud import bigquery
from google.cloud.bigquery import dbapi, QueryJobConfig
Expand Down Expand Up @@ -283,6 +285,11 @@ def __init__(
def dbapi(cls):
return dbapi

@staticmethod
def _build_formatted_table_id(table):
"""Build '<dataset_id>.<table_id>' string using given table."""
return "{}.{}".format(table.reference.dataset_id, table.table_id)

@staticmethod
def _add_default_dataset_to_job_config(job_config, project_id, dataset_id):
# If dataset_id is set, then we know the job_config isn't None
Expand Down Expand Up @@ -349,6 +356,26 @@ def _json_deserializer(self, row):
"""
return row

def _get_table_or_view_names(self, connection, table_type, schema=None):
current_schema = schema or self.dataset_id
get_table_name = self._build_formatted_table_id \
if self.dataset_id is None else \
operator.attrgetter("table_id")

client = connection.connection._client
datasets = client.list_datasets()

result = []
for dataset in datasets:
if current_schema is not None and current_schema != dataset.dataset_id:
Copy link
Collaborator

@tswast tswast Nov 18, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any reason to call list_datasets at all if current_schema is set?

Edit: I see this was already-existing logic, just moved. Won't block the PR on this.

continue

tables = client.list_tables(dataset.reference)
for table in tables:
if table_type == table.table_type:
result.append(get_table_name(table))
return result

@staticmethod
def _split_table_name(full_table_name):
# Split full_table_name to get project, dataset and table name
Expand Down Expand Up @@ -464,23 +491,13 @@ def get_table_names(self, connection, schema=None, **kw):
if isinstance(connection, Engine):
connection = connection.connect()

datasets = connection.connection._client.list_datasets()
result = []
for d in datasets:
if schema is not None and d.dataset_id != schema:
continue
return self._get_table_or_view_names(connection, "TABLE", schema)

if self.dataset_id is not None and d.dataset_id != self.dataset_id:
continue
def get_view_names(self, connection, schema=None, **kw):
if isinstance(connection, Engine):
connection = connection.connect()

tables = connection.connection._client.list_tables(d.reference)
for t in tables:
if self.dataset_id is None:
table_name = d.dataset_id + '.' + t.table_id
else:
table_name = t.table_id
result.append(table_name)
return result
return self._get_table_or_view_names(connection, "VIEW", schema)

def do_rollback(self, dbapi_connection):
# BigQuery has no support for transactions.
Expand Down
3 changes: 3 additions & 0 deletions scripts/load_test_data.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ bq rm -f -t test_pybigquery.sample
bq rm -f -t test_pybigquery_alt.sample_alt
bq rm -f -t test_pybigquery.sample_one_row
bq rm -f -t test_pybigquery.sample_dml
bq rm -f -t test_pybigquery.sample_view
bq rm -f -t test_pybigquery_location.sample_one_row

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

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
bq mk --schema=$(dirname $0)/schema.json -t test_pybigquery.sample_dml

bq mk --use_legacy_sql=false --view 'SELECT string FROM test_pybigquery.sample' test_pybigquery.sample_view
14 changes: 14 additions & 0 deletions test/test_sqlalchemy_bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,11 +283,13 @@ def test_tables_list(engine, engine_using_test_dataset):
assert 'test_pybigquery.sample' in tables
assert 'test_pybigquery.sample_one_row' in tables
assert 'test_pybigquery.sample_dml' in tables
assert 'test_pybigquery.sample_view' not in tables

tables = engine_using_test_dataset.table_names()
assert 'sample' in tables
assert 'sample_one_row' in tables
assert 'sample_dml' in tables
assert 'sample_view' not in tables


def test_group_by(session, table, session_using_test_dataset, table_using_test_dataset):
Expand Down Expand Up @@ -436,15 +438,27 @@ def test_table_names_in_schema(inspector, inspector_using_test_dataset):
assert 'test_pybigquery.sample' in tables
assert 'test_pybigquery.sample_one_row' in tables
assert 'test_pybigquery.sample_dml' in tables
assert 'test_pybigquery.sample_view' not in tables
assert len(tables) == 3

tables = inspector_using_test_dataset.get_table_names()
assert 'sample' in tables
assert 'sample_one_row' in tables
assert 'sample_dml' in tables
assert 'sample_view' not in tables
assert len(tables) == 3


def test_view_names(inspector, inspector_using_test_dataset):
view_names = inspector.get_view_names()
assert "test_pybigquery.sample_view" in view_names
assert "test_pybigquery.sample" not in view_names

view_names = inspector_using_test_dataset.get_view_names()
assert "sample_view" in view_names
assert "sample" not in view_names


def test_get_indexes(inspector, inspector_using_test_dataset):
for table in ['test_pybigquery.sample', 'test_pybigquery.sample_one_row']:
indexes = inspector.get_indexes('test_pybigquery.sample')
Expand Down