Repository navigation
Auto-infer BigQuery schema from schema'd PCollections for FILE_LOADS #39662
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
Open
nikitagrover19
wants to merge
6
commits into
apache:master
Choose a base branch
from
nikitagrover19:file-loads-schema-inference-28162
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4f476c2
Auto-infer BigQuery schema from schema'd PCollections for FILE_LOADS
nikitagrover19 04e5466
Merge branch 'apache:master' into file-loads-schema-inference-28162
nikitagrover19 aef5019
Re-running tests
nikitagrover19 1da59bf
Merge branch 'master' into file-loads-schema-inference-28162
nikitagrover19 edfcc6a
Merge branch 'master' into file-loads-schema-inference-28162
jrmccluskey 2ba57ad
Merge branch 'master' into file-loads-schema-inference-28162
nikitagrover19 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 hidden or 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 |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| { | ||
| "comment": "Modify this file in a trivial way to cause this test suite to run.", | ||
| "pr": "40241", | ||
| "modification": 63 | ||
| "pr": "39662", | ||
| "modification": 12 | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -512,6 +512,151 @@ def test_type_overrides_can_override_default_types(self): | |
| bq_field_to_type("GEOGRAPHY", "REQUIRED", overrides), bytes) | ||
|
|
||
|
|
||
| class TestBeamSchemaToBqTableSchema(unittest.TestCase): | ||
| """Tests for beam_schema_to_bq_table_schema, the reverse of | ||
| generate_user_type_from_bq_schema, used to auto-infer a destination | ||
| table's schema from a schema'd PCollection (e.g. for FILE_LOADS).""" | ||
| def test_atomic_types(self): | ||
| import decimal | ||
|
|
||
| class MyRow(typing.NamedTuple): | ||
| name: str | ||
| age: np.int64 | ||
| score: float | ||
| active: bool | ||
| raw: bytes | ||
| amount: decimal.Decimal | ||
|
|
||
| schema = beam.typehints.schemas.schema_from_element_type(MyRow) | ||
| bq_schema = bigquery_schema_tools.beam_schema_to_bq_table_schema(schema) | ||
|
|
||
| self.assertEqual( | ||
| bq_schema, | ||
| { | ||
| 'fields': [ | ||
| { | ||
| 'name': 'name', 'type': 'STRING', 'mode': 'REQUIRED' | ||
| }, | ||
| { | ||
| 'name': 'age', 'type': 'INT64', 'mode': 'REQUIRED' | ||
| }, | ||
| { | ||
| 'name': 'score', 'type': 'FLOAT64', 'mode': 'REQUIRED' | ||
| }, | ||
| { | ||
| 'name': 'active', 'type': 'BOOL', 'mode': 'REQUIRED' | ||
| }, | ||
| { | ||
| 'name': 'raw', 'type': 'BYTES', 'mode': 'REQUIRED' | ||
| }, | ||
| { | ||
| 'name': 'amount', 'type': 'NUMERIC', 'mode': 'REQUIRED' | ||
| }, | ||
| ] | ||
| }) | ||
|
|
||
| def test_timestamp_type(self): | ||
| from apache_beam.utils.timestamp import Timestamp | ||
|
|
||
| class MyRow(typing.NamedTuple): | ||
| when: Timestamp | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we also test datetime.date and datetime.time here? |
||
|
|
||
| schema = beam.typehints.schemas.schema_from_element_type(MyRow) | ||
| bq_schema = bigquery_schema_tools.beam_schema_to_bq_table_schema(schema) | ||
|
|
||
| self.assertEqual( | ||
| bq_schema, | ||
| {'fields': [{ | ||
| 'name': 'when', 'type': 'TIMESTAMP', 'mode': 'REQUIRED' | ||
| }]}) | ||
|
|
||
| def test_nullable_field(self): | ||
| class MyRow(typing.NamedTuple): | ||
| name: typing.Optional[str] | ||
|
|
||
| schema = beam.typehints.schemas.schema_from_element_type(MyRow) | ||
| bq_schema = bigquery_schema_tools.beam_schema_to_bq_table_schema(schema) | ||
|
|
||
| self.assertEqual( | ||
| bq_schema, | ||
| {'fields': [{ | ||
| 'name': 'name', 'type': 'STRING', 'mode': 'NULLABLE' | ||
| }]}) | ||
|
|
||
| def test_repeated_field(self): | ||
| class MyRow(typing.NamedTuple): | ||
| tags: typing.Sequence[str] | ||
|
|
||
| schema = beam.typehints.schemas.schema_from_element_type(MyRow) | ||
| bq_schema = bigquery_schema_tools.beam_schema_to_bq_table_schema(schema) | ||
|
|
||
| self.assertEqual( | ||
| bq_schema, | ||
| {'fields': [{ | ||
| 'name': 'tags', 'type': 'STRING', 'mode': 'REPEATED' | ||
| }]}) | ||
|
|
||
| def test_nested_record(self): | ||
| class Nested(typing.NamedTuple): | ||
| x: np.int64 | ||
| y: typing.Optional[str] | ||
|
|
||
| class MyRow(typing.NamedTuple): | ||
| nested: Nested | ||
|
|
||
| schema = beam.typehints.schemas.schema_from_element_type(MyRow) | ||
| bq_schema = bigquery_schema_tools.beam_schema_to_bq_table_schema(schema) | ||
|
|
||
| self.assertEqual( | ||
| bq_schema, | ||
| { | ||
| 'fields': [{ | ||
| 'name': 'nested', | ||
| 'type': 'STRUCT', | ||
| 'mode': 'REQUIRED', | ||
| 'fields': [ | ||
| { | ||
| 'name': 'x', 'type': 'INT64', 'mode': 'REQUIRED' | ||
| }, | ||
| { | ||
| 'name': 'y', 'type': 'STRING', 'mode': 'NULLABLE' | ||
| }, | ||
| ] | ||
| }] | ||
| }) | ||
|
|
||
| def test_repeated_record(self): | ||
| class Nested(typing.NamedTuple): | ||
| x: np.int64 | ||
|
|
||
| class MyRow(typing.NamedTuple): | ||
| nested_list: typing.Sequence[Nested] | ||
|
|
||
| schema = beam.typehints.schemas.schema_from_element_type(MyRow) | ||
| bq_schema = bigquery_schema_tools.beam_schema_to_bq_table_schema(schema) | ||
|
|
||
| self.assertEqual( | ||
| bq_schema, | ||
| { | ||
| 'fields': [{ | ||
| 'name': 'nested_list', | ||
| 'type': 'STRUCT', | ||
| 'mode': 'REPEATED', | ||
| 'fields': [{ | ||
| 'name': 'x', 'type': 'INT64', 'mode': 'REQUIRED' | ||
| }] | ||
| }] | ||
| }) | ||
|
|
||
| def test_map_type_raises(self): | ||
| class MyRow(typing.NamedTuple): | ||
| attrs: typing.Mapping[str, str] | ||
|
|
||
| schema = beam.typehints.schemas.schema_from_element_type(MyRow) | ||
| with self.assertRaises(ValueError): | ||
| bigquery_schema_tools.beam_schema_to_bq_table_schema(schema) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| logging.getLogger().setLevel(logging.INFO) | ||
| unittest.main() | ||
Oops, something went wrong.
Oops, something went wrong.
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.
There are also two new Tuple logical types added recently in
beam/sdks/python/apache_beam/typehints/schemas.py
Lines 40 to 41 in 5394822
Could we also support and test them?