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

Adding property to get a table identifier for a query. #1254

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 2 additions & 2 deletions docs/bigquery-usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ Run a query which can be expected to complete within bounded time:
>>> from gcloud import bigquery
>>> client = bigquery.Client()
>>> query = """\
SELECT count(*) AS age_count FROM dataset_name.person_ages
SELECT count(*) AS age_count FROM [project-name:dataset_name.person_ages]

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

"""
>>> results = client.run_sync_query(query, timeout_ms=1000)
>>> retry_count = 100
Expand Down Expand Up @@ -311,7 +311,7 @@ Background a query, loading the results into a table:
>>> query = """\
SELECT firstname + ' ' + last_name AS full_name,
FLOOR(DATEDIFF(CURRENT_DATE(), birth_date) / 365) AS age
FROM dataset_name.persons
FROM [project-name:dataset_name.person_ages]
"""
>>> dataset = client.dataset('dataset_name')
>>> table = dataset.table(name='person_ages')
Expand Down
27 changes: 27 additions & 0 deletions gcloud/bigquery/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,33 @@ def schema(self, value):
raise ValueError('Schema items must be fields')
self._schema = tuple(value)

@property
def query_id(self):
"""The identifier for the table to be used in a query.

This is of the form

``"{project_name}:{dataset_name}.{table_name}"``

and brackets will be added if the project name includes a
dash. For reference, see:

https://cloud.google.com/bigquery/query-reference#from

.. note::

This is identical to the ID (``table_id``) returned from the
server, but that value from the server will not have the
square brackets added.

:rtype: string
:returns: The table identifier to be used in a query.
"""
query_parts = [self.project, ':', self.dataset_name, '.', self.name]
if '-' in self.project:
query_parts = ['['] + query_parts + [']']
return ''.join(query_parts)

@property
def created(self):
"""Datetime at which the table was created.
Expand Down
17 changes: 17 additions & 0 deletions gcloud/bigquery/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,23 @@ def test_ctor_w_schema(self):
schema=[full_name, age])
self.assertEqual(table.schema, [full_name, age])

def test_query_id_property(self):
client = _Client(self.PROJECT)
dataset = _Dataset(client)
table = self._makeOne(self.TABLE_NAME, dataset)
expected_result = ''.join([self.PROJECT, ':', self.DS_NAME,
'.', self.TABLE_NAME])

This comment was marked as spam.

This comment was marked as spam.

self.assertEqual(table.query_id, expected_result)

def test_query_id_property_with_hyphen(self):
curr_project = self.PROJECT + '-extra'
client = _Client(curr_project)
dataset = _Dataset(client)
table = self._makeOne(self.TABLE_NAME, dataset)
expected_result = ''.join(['[', curr_project, ':', self.DS_NAME,
'.', self.TABLE_NAME, ']'])
self.assertEqual(table.query_id, expected_result)

def test_num_bytes_getter(self):
client = _Client(self.PROJECT)
dataset = _Dataset(client)
Expand Down