-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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: Add StandardSqlDataTypes enum to BigQuery #8782
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
cf64140
Add StandardSqlDataTypes enum to BigQuery
plamut 84f167e
Move StandardSqlDataTypes tests under enums/ dir
plamut 70292d8
Treat GEOGRAPHY as scalar SQL type in enum
plamut 4da6e78
Use more descriptive name in generator expression
plamut 95c13df
Replace enum out of sync warning with a loud test
plamut 4ebdd91
Add Enums section to BigQuery API reference docs
plamut File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# Copyright 2019 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import re | ||
|
||
import enum | ||
import six | ||
|
||
from google.cloud.bigquery_v2.gapic import enums as gapic_enums | ||
|
||
|
||
_SQL_SCALAR_TYPES = frozenset( | ||
( | ||
"INT64", | ||
"BOOL", | ||
"FLOAT64", | ||
"STRING", | ||
"BYTES", | ||
"TIMESTAMP", | ||
"DATE", | ||
"TIME", | ||
"DATETIME", | ||
"GEOGRAPHY", | ||
"NUMERIC", | ||
) | ||
) | ||
|
||
_SQL_NONSCALAR_TYPES = frozenset(("TYPE_KIND_UNSPECIFIED", "ARRAY", "STRUCT")) | ||
|
||
|
||
def _make_sql_scalars_enum(): | ||
"""Create an enum based on a gapic enum containing only SQL scalar types.""" | ||
|
||
new_enum = enum.Enum( | ||
"StandardSqlDataTypes", | ||
( | ||
(member.name, member.value) | ||
for member in gapic_enums.StandardSqlDataType.TypeKind | ||
if member.name in _SQL_SCALAR_TYPES | ||
), | ||
) | ||
|
||
# make sure the docstring for the new enum is also correct | ||
orig_doc = gapic_enums.StandardSqlDataType.TypeKind.__doc__ | ||
skip_pattern = re.compile( | ||
"|".join(_SQL_NONSCALAR_TYPES) | ||
+ "|because a JSON object" # the second description line of STRUCT member | ||
tswast marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
|
||
new_doc = "\n".join( | ||
six.moves.filterfalse(skip_pattern.search, orig_doc.splitlines()) | ||
) | ||
new_enum.__doc__ = "An Enum of scalar SQL types.\n" + new_doc | ||
|
||
return new_enum | ||
|
||
|
||
StandardSqlDataTypes = _make_sql_scalars_enum() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Copyright 2019, Google LLC All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# Copyright 2019 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import pytest | ||
|
||
|
||
@pytest.fixture | ||
def module_under_test(): | ||
from google.cloud.bigquery import enums | ||
|
||
return enums | ||
|
||
|
||
@pytest.fixture | ||
def enum_under_test(): | ||
from google.cloud.bigquery.enums import StandardSqlDataTypes | ||
|
||
return StandardSqlDataTypes | ||
|
||
|
||
@pytest.fixture | ||
def gapic_enum(): | ||
"""The referential autogenerated enum the enum under test is based on.""" | ||
from google.cloud.bigquery_v2.gapic.enums import StandardSqlDataType | ||
|
||
return StandardSqlDataType.TypeKind | ||
|
||
|
||
def test_all_gapic_enum_members_are_known(module_under_test, gapic_enum): | ||
gapic_names = set(type_.name for type_ in gapic_enum) | ||
anticipated_names = ( | ||
module_under_test._SQL_SCALAR_TYPES | module_under_test._SQL_NONSCALAR_TYPES | ||
) | ||
assert not (gapic_names - anticipated_names) # no unhandled names | ||
|
||
|
||
def test_standard_sql_types_enum_members(enum_under_test, gapic_enum): | ||
# check the presence of a few typical SQL types | ||
for name in ("INT64", "FLOAT64", "DATE", "BOOL", "GEOGRAPHY"): | ||
assert name in enum_under_test.__members__ | ||
|
||
# the enum members must match those in the original gapic enum | ||
for member in enum_under_test: | ||
assert member.name in gapic_enum.__members__ | ||
assert member.value == gapic_enum[member.name].value | ||
|
||
# check a few members that should *not* be copied over from the gapic enum | ||
for name in ("STRUCT", "ARRAY"): | ||
assert name in gapic_enum.__members__ | ||
assert name not in enum_under_test.__members__ | ||
|
||
|
||
def test_standard_sql_types_enum_docstring(enum_under_test, gapic_enum): | ||
assert "STRUCT (int):" not in enum_under_test.__doc__ | ||
assert "BOOL (int):" in enum_under_test.__doc__ | ||
assert "TIME (int):" in enum_under_test.__doc__ | ||
|
||
# All lines in the docstring should actually come from the original docstring, | ||
# except for the header. | ||
assert "An Enum of scalar SQL types." in enum_under_test.__doc__ | ||
doc_lines = enum_under_test.__doc__.splitlines() | ||
assert set(doc_lines[1:]) <= set(gapic_enum.__doc__.splitlines()) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This reminds me: let's add an Enums section to the API reference docs, too.
https://github.com/googleapis/google-cloud-python/blob/master/bigquery/docs/reference.rst
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added. Also had to inject an additional header line to the new enum's docstring, because Sphinx complained about a duplicate object description (took the first line - the INT64 member - which is of course directly copied from the gapic enum).