From 2ce7c0bc5e7ac2676fa6d47ed297781c34bc57e0 Mon Sep 17 00:00:00 2001 From: Layla Bristol Date: Wed, 10 Apr 2019 10:51:49 -0700 Subject: [PATCH 1/2] Added property clustering_fields to TableListItem along with test --- bigquery/google/cloud/bigquery/table.py | 17 +++++++++++++++++ bigquery/tests/unit/test_table.py | 1 + 2 files changed, 18 insertions(+) diff --git a/bigquery/google/cloud/bigquery/table.py b/bigquery/google/cloud/bigquery/table.py index bf5fbc65b5a3..e9f06998d2ce 100644 --- a/bigquery/google/cloud/bigquery/table.py +++ b/bigquery/google/cloud/bigquery/table.py @@ -1021,6 +1021,23 @@ def friendly_name(self): view_use_legacy_sql = property(_view_use_legacy_sql_getter) + @property + def clustering_fields(self): + """Union[List[str], None]: Fields defining clustering for the table + + (Defaults to :data:`None`). + + Clustering fields are immutable after table creation. + + .. note:: + + As of 2018-06-29, clustering fields cannot be set on a table + which does not also have time partioning defined. + """ + prop = self._properties.get("clustering") + if prop is not None: + return list(prop.get("fields", ())) + @classmethod def from_string(cls, full_table_id): """Construct a table from fully-qualified table ID. diff --git a/bigquery/tests/unit/test_table.py b/bigquery/tests/unit/test_table.py index 965dd5f0f195..1c4df7b61460 100644 --- a/bigquery/tests/unit/test_table.py +++ b/bigquery/tests/unit/test_table.py @@ -1170,6 +1170,7 @@ def test_ctor(self): self.assertEqual(table.time_partitioning.field, "mycolumn") self.assertEqual(table.labels["some-stuff"], "this-is-a-label") self.assertIsNone(table.view_use_legacy_sql) + self.assertIsNone(table.clustering_fields) with warnings.catch_warnings(record=True) as warned: self.assertEqual(table.partitioning_type, "DAY") From 880e32220f9657bdfbb8a765f7e145b23a2b7d6b Mon Sep 17 00:00:00 2001 From: Layla Bristol Date: Wed, 10 Apr 2019 12:33:36 -0700 Subject: [PATCH 2/2] Updated tests for clustering fields. --- bigquery/tests/unit/test_table.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/bigquery/tests/unit/test_table.py b/bigquery/tests/unit/test_table.py index 1c4df7b61460..557d57741382 100644 --- a/bigquery/tests/unit/test_table.py +++ b/bigquery/tests/unit/test_table.py @@ -1148,6 +1148,7 @@ def test_ctor(self): "expirationMs": "10000", }, "labels": {"some-stuff": "this-is-a-label"}, + "clustering": {"fields": ["string"]}, } table = self._make_one(resource) @@ -1170,7 +1171,7 @@ def test_ctor(self): self.assertEqual(table.time_partitioning.field, "mycolumn") self.assertEqual(table.labels["some-stuff"], "this-is-a-label") self.assertIsNone(table.view_use_legacy_sql) - self.assertIsNone(table.clustering_fields) + self.assertEqual(table.clustering_fields, ["string"]) with warnings.catch_warnings(record=True) as warned: self.assertEqual(table.partitioning_type, "DAY") @@ -1223,6 +1224,7 @@ def test_ctor_missing_properties(self): self.assertEqual(table.table_id, "testtable") self.assertIsNone(table.created) self.assertIsNone(table.expires) + self.assertIsNone(table.clustering_fields) self.assertIsNone(table.full_table_id) self.assertIsNone(table.friendly_name) self.assertIsNone(table.table_type)