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

BigQuery: Use TableListItem for table listing. #4427

Merged
merged 6 commits into from
Nov 20, 2017
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
51 changes: 21 additions & 30 deletions bigquery/google/cloud/bigquery/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,25 @@
_MARKER = object()


def _view_use_legacy_sql_getter(table):
"""Specifies whether to execute the view with Legacy or Standard SQL.

If this table is not a view, None is returned.

Returns:
bool: True if the view is using legacy SQL, or None if not a view
"""
view = table._properties.get('view')
if view is not None:
# The server-side default for useLegacySql is True.
return view.get('useLegacySql', True)
# In some cases, such as in a table list no view object is present, but the
# resource still represents a view. Use the type as a fallback.
if table.table_type == 'VIEW':
# The server-side default for useLegacySql is True.
return True


class TableReference(object):
"""TableReferences are pointers to tables.

Expand Down Expand Up @@ -531,23 +550,7 @@ def view_query(self):
"""Delete SQL query defining the table as a view."""
self._properties.pop('view', None)

@property
def view_use_legacy_sql(self):
"""Specifies whether to execute the view with Legacy or Standard SQL.

The default is False for views (use Standard SQL).
If this table is not a view, None is returned.

:rtype: bool or ``NoneType``
:returns: The boolean for view.useLegacySql, or None if not a view.
"""
view = self._properties.get('view')
if view is not None:
# useLegacySql is never missing from the view dict if this table
# was created client-side, because the view_query setter populates
# it. So a missing or None can only come from the server, whose
# default is True.
return view.get('useLegacySql', True)
view_use_legacy_sql = property(_view_use_legacy_sql_getter)

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.


@view_use_legacy_sql.setter
def view_use_legacy_sql(self, value):
Expand Down Expand Up @@ -829,19 +832,7 @@ def friendly_name(self):
"""
return self._properties.get('friendlyName')

@property
def view_use_legacy_sql(self):
"""Specifies whether to execute the view with Legacy or Standard SQL.

If this table is not a view, None is returned.

Returns:
bool: True if the view is using legacy SQL, or None if not a view
"""
view = self._properties.get('view', {})
if self.table_type == 'VIEW':
# The server-side default for useLegacySql is True.
return view.get('useLegacySql', True)
view_use_legacy_sql = property(_view_use_legacy_sql_getter)


def _row_from_mapping(mapping, schema):
Expand Down
6 changes: 2 additions & 4 deletions bigquery/tests/unit/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -817,9 +817,6 @@ def test_ctor_view(self):
'tableId': table_id,
},
'type': 'VIEW',
'view': {
'useLegacySql': False,
},
}

table = self._make_one(resource)
Expand All @@ -833,7 +830,8 @@ def test_ctor_view(self):
self.assertEqual(table.reference.dataset_id, dataset_id)
self.assertEqual(table.reference.table_id, table_id)
self.assertEqual(table.table_type, 'VIEW')
self.assertFalse(table.view_use_legacy_sql)
# Server default for useLegacySql is True.
self.assertTrue(table.view_use_legacy_sql)


class TestRow(unittest.TestCase):
Expand Down