GeezerVison is showing github.com — fix: add warning when encountering unknown field types (#1989) · chenhance/python-bigquery@8f5a41d
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: google/cloud/bigquery/_helpers.py +28-10Lines changed: 28 additions & 10 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 21 | 21 | import math | 22 | 22 | import re | 23 | 23 | import os | | 24 | +import warnings | 24 | 25 | from typing import Optional, Union | 25 | 26 |
| 26 | 27 | from dateutil import relativedelta | @@ -297,12 +298,7 @@ def _record_from_json(value, field): | 297 | 298 | record = {} | 298 | 299 | record_iter = zip(field.fields, value["f"]) | 299 | 300 | for subfield, cell in record_iter: | 300 | | - converter = _CELLDATA_FROM_JSON[subfield.field_type] | 301 | | - if subfield.mode == "REPEATED": | 302 | | - value = [converter(item["v"], subfield) for item in cell["v"]] | 303 | | - else: | 304 | | - value = converter(cell["v"], subfield) | 305 | | - record[subfield.name] = value | | 301 | + record[subfield.name] = _field_from_json(cell["v"], subfield) | 306 | 302 | return record | 307 | 303 |
| 308 | 304 |
| @@ -382,7 +378,11 @@ def _field_to_index_mapping(schema): | 382 | 378 |
| 383 | 379 |
| 384 | 380 | def _field_from_json(resource, field): | 385 | | - converter = _CELLDATA_FROM_JSON.get(field.field_type, lambda value, _: value) | | 381 | + def default_converter(value, field): | | 382 | + _warn_unknown_field_type(field) | | 383 | + return value | | 384 | + | | 385 | + converter = _CELLDATA_FROM_JSON.get(field.field_type, default_converter) | 386 | 386 | if field.mode == "REPEATED": | 387 | 387 | return [converter(item["v"], field) for item in resource] | 388 | 388 | else: | @@ -484,6 +484,11 @@ def _json_to_json(value): | 484 | 484 | return json.dumps(value) | 485 | 485 |
| 486 | 486 |
| | 487 | +def _string_to_json(value): | | 488 | + """NOOP string -> string coercion""" | | 489 | + return value | | 490 | + | | 491 | + | 487 | 492 | def _timestamp_to_json_parameter(value): | 488 | 493 | """Coerce 'value' to an JSON-compatible representation. | 489 | 494 |
| @@ -596,6 +601,7 @@ def _range_field_to_json(range_element_type, value): | 596 | 601 | "DATE": _date_to_json, | 597 | 602 | "TIME": _time_to_json, | 598 | 603 | "JSON": _json_to_json, | | 604 | + "STRING": _string_to_json, | 599 | 605 | # Make sure DECIMAL and BIGDECIMAL are handled, even though | 600 | 606 | # requests for them should be converted to NUMERIC. Better safe | 601 | 607 | # than sorry. | @@ -609,6 +615,15 @@ def _range_field_to_json(range_element_type, value): | 609 | 615 | _SCALAR_VALUE_TO_JSON_PARAM["TIMESTAMP"] = _timestamp_to_json_parameter | 610 | 616 |
| 611 | 617 |
| | 618 | +def _warn_unknown_field_type(field): | | 619 | + warnings.warn( | | 620 | + "Unknown type '{}' for field '{}'. Behavior reading and writing this type is not officially supported and may change in the future.".format( | | 621 | + field.field_type, field.name | | 622 | + ), | | 623 | + FutureWarning, | | 624 | + ) | | 625 | + | | 626 | + | 612 | 627 | def _scalar_field_to_json(field, row_value): | 613 | 628 | """Maps a field and value to a JSON-safe value. | 614 | 629 |
| @@ -621,9 +636,12 @@ def _scalar_field_to_json(field, row_value): | 621 | 636 | Returns: | 622 | 637 | Any: A JSON-serializable object. | 623 | 638 | """ | 624 | | - converter = _SCALAR_VALUE_TO_JSON_ROW.get(field.field_type) | 625 | | - if converter is None: # STRING doesn't need converting | 626 | | - return row_value | | 639 | + | | 640 | + def default_converter(value): | | 641 | + _warn_unknown_field_type(field) | | 642 | + return value | | 643 | + | | 644 | + converter = _SCALAR_VALUE_TO_JSON_ROW.get(field.field_type, default_converter) | 627 | 645 | return converter(row_value) | 628 | 646 |
| 629 | 647 |
| |
Collapse file Copy file name to clipboardExpand all lines: google/cloud/bigquery/_pandas_helpers.py +3-1Lines changed: 3 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -204,7 +204,9 @@ def bq_to_arrow_field(bq_field, array_type=None): | 204 | 204 | metadata=metadata, | 205 | 205 | ) | 206 | 206 |
| 207 | | - warnings.warn("Unable to determine type for field '{}'.".format(bq_field.name)) | | 207 | + warnings.warn( | | 208 | + "Unable to determine Arrow type for field '{}'.".format(bq_field.name) | | 209 | + ) | 208 | 210 | return None | 209 | 211 |
| 210 | 212 |
| |
Collapse file Copy file name to clipboardExpand all lines: google/cloud/bigquery/query.py +9-11Lines changed: 9 additions & 11 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -591,9 +591,8 @@ def to_api_repr(self) -> dict: | 591 | 591 | Dict: JSON mapping | 592 | 592 | """ | 593 | 593 | value = self.value | 594 | | - converter = _SCALAR_VALUE_TO_JSON_PARAM.get(self.type_) | 595 | | - if converter is not None: | 596 | | - value = converter(value) # type: ignore | | 594 | + converter = _SCALAR_VALUE_TO_JSON_PARAM.get(self.type_, lambda value: value) | | 595 | + value = converter(value) # type: ignore | 597 | 596 | resource: Dict[str, Any] = { | 598 | 597 | "parameterType": {"type": self.type_}, | 599 | 598 | "parameterValue": {"value": value}, | @@ -748,9 +747,10 @@ def to_api_repr(self) -> dict: | 748 | 747 | else: | 749 | 748 | a_type = self.array_type.to_api_repr() | 750 | 749 |
| 751 | | - converter = _SCALAR_VALUE_TO_JSON_PARAM.get(a_type["type"]) | 752 | | - if converter is not None: | 753 | | - values = [converter(value) for value in values] # type: ignore | | 750 | + converter = _SCALAR_VALUE_TO_JSON_PARAM.get( | | 751 | + a_type["type"], lambda value: value | | 752 | + ) | | 753 | + values = [converter(value) for value in values] # type: ignore | 754 | 754 | a_values = [{"value": value} for value in values] | 755 | 755 |
| 756 | 756 | resource = { | @@ -792,7 +792,7 @@ def __repr__(self): | 792 | 792 |
| 793 | 793 |
| 794 | 794 | class StructQueryParameter(_AbstractQueryParameter): | 795 | | - """Named / positional query parameters for struct values. | | 795 | + """Name / positional query parameters for struct values. | 796 | 796 |
| 797 | 797 | Args: | 798 | 798 | name (Optional[str]): | @@ -897,10 +897,8 @@ def to_api_repr(self) -> dict: | 897 | 897 | values[name] = repr_["parameterValue"] | 898 | 898 | else: | 899 | 899 | s_types[name] = {"name": name, "type": {"type": type_}} | 900 | | - converter = _SCALAR_VALUE_TO_JSON_PARAM.get(type_) | 901 | | - if converter is not None: | 902 | | - value = converter(value) # type: ignore | 903 | | - values[name] = {"value": value} | | 900 | + converter = _SCALAR_VALUE_TO_JSON_PARAM.get(type_, lambda value: value) | | 901 | + values[name] = {"value": converter(value)} | 904 | 902 |
| 905 | 903 | resource = { | 906 | 904 | "parameterType": { | |
Collapse file Copy file name to clipboardExpand all lines: tests/unit/test__helpers.py +61-1Lines changed: 61 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 17 | 17 | import decimal | 18 | 18 | import json | 19 | 19 | import os | | 20 | +import warnings | 20 | 21 | import pytest | 21 | 22 | import packaging | 22 | 23 | import unittest | @@ -640,6 +641,17 @@ def test_w_single_scalar_column(self): | 640 | 641 | row = {"f": [{"v": "1"}]} | 641 | 642 | self.assertEqual(self._call_fut(row, schema=[col]), (1,)) | 642 | 643 |
| | 644 | + def test_w_unknown_type(self): | | 645 | + # SELECT 1 AS col | | 646 | + col = _Field("REQUIRED", "col", "UNKNOWN") | | 647 | + row = {"f": [{"v": "1"}]} | | 648 | + with warnings.catch_warnings(record=True) as warned: | | 649 | + self.assertEqual(self._call_fut(row, schema=[col]), ("1",)) | | 650 | + self.assertEqual(len(warned), 1) | | 651 | + warning = warned[0] | | 652 | + self.assertTrue("UNKNOWN" in str(warning)) | | 653 | + self.assertTrue("col" in str(warning)) | | 654 | + | 643 | 655 | def test_w_single_scalar_geography_column(self): | 644 | 656 | # SELECT 1 AS col | 645 | 657 | col = _Field("REQUIRED", "geo", "GEOGRAPHY") | @@ -660,6 +672,17 @@ def test_w_single_array_column(self): | 660 | 672 | row = {"f": [{"v": [{"v": "1"}, {"v": "2"}, {"v": "3"}]}]} | 661 | 673 | self.assertEqual(self._call_fut(row, schema=[col]), ([1, 2, 3],)) | 662 | 674 |
| | 675 | + def test_w_unknown_type_repeated(self): | | 676 | + # SELECT 1 AS col | | 677 | + col = _Field("REPEATED", "col", "UNKNOWN") | | 678 | + row = {"f": [{"v": [{"v": "1"}, {"v": "2"}, {"v": "3"}]}]} | | 679 | + with warnings.catch_warnings(record=True) as warned: | | 680 | + self.assertEqual(self._call_fut(row, schema=[col]), (["1", "2", "3"],)) | | 681 | + self.assertEqual(len(warned), 1) | | 682 | + warning = warned[0] | | 683 | + self.assertTrue("UNKNOWN" in str(warning)) | | 684 | + self.assertTrue("col" in str(warning)) | | 685 | + | 663 | 686 | def test_w_struct_w_nested_array_column(self): | 664 | 687 | # SELECT ([1, 2], 3, [4, 5]) as col | 665 | 688 | first = _Field("REPEATED", "first", "INTEGER") | @@ -684,6 +707,39 @@ def test_w_struct_w_nested_array_column(self): | 684 | 707 | ({"first": [1, 2], "second": 3, "third": [4, 5]},), | 685 | 708 | ) | 686 | 709 |
| | 710 | + def test_w_unknown_type_subfield(self): | | 711 | + # SELECT [(1, 2, 3), (4, 5, 6)] as col | | 712 | + first = _Field("REPEATED", "first", "UNKNOWN1") | | 713 | + second = _Field("REQUIRED", "second", "UNKNOWN2") | | 714 | + third = _Field("REPEATED", "third", "INTEGER") | | 715 | + col = _Field("REQUIRED", "col", "RECORD", fields=[first, second, third]) | | 716 | + row = { | | 717 | + "f": [ | | 718 | + { | | 719 | + "v": { | | 720 | + "f": [ | | 721 | + {"v": [{"v": "1"}, {"v": "2"}]}, | | 722 | + {"v": "3"}, | | 723 | + {"v": [{"v": "4"}, {"v": "5"}]}, | | 724 | + ] | | 725 | + } | | 726 | + } | | 727 | + ] | | 728 | + } | | 729 | + with warnings.catch_warnings(record=True) as warned: | | 730 | + self.assertEqual( | | 731 | + self._call_fut(row, schema=[col]), | | 732 | + ({"first": ["1", "2"], "second": "3", "third": [4, 5]},), | | 733 | + ) | | 734 | + self.assertEqual(len(warned), 2) # 1 warning per unknown field. | | 735 | + warned = [str(warning) for warning in warned] | | 736 | + self.assertTrue( | | 737 | + any(["first" in warning and "UNKNOWN1" in warning for warning in warned]) | | 738 | + ) | | 739 | + self.assertTrue( | | 740 | + any(["second" in warning and "UNKNOWN2" in warning for warning in warned]) | | 741 | + ) | | 742 | + | 687 | 743 | def test_w_array_of_struct(self): | 688 | 744 | # SELECT [(1, 2, 3), (4, 5, 6)] as col | 689 | 745 | first = _Field("REQUIRED", "first", "INTEGER") | @@ -1076,8 +1132,12 @@ def _call_fut(self, field, value): | 1076 | 1132 | def test_w_unknown_field_type(self): | 1077 | 1133 | field = _make_field("UNKNOWN") | 1078 | 1134 | original = object() | 1079 | | - converted = self._call_fut(field, original) | | 1135 | + with warnings.catch_warnings(record=True) as warned: | | 1136 | + converted = self._call_fut(field, original) | 1080 | 1137 | self.assertIs(converted, original) | | 1138 | + self.assertEqual(len(warned), 1) | | 1139 | + warning = warned[0] | | 1140 | + self.assertTrue("UNKNOWN" in str(warning)) | 1081 | 1141 |
| 1082 | 1142 | def test_w_known_field_type(self): | 1083 | 1143 | field = _make_field("INT64") | |
Collapse file Copy file name to clipboardExpand all lines: tests/unit/test_query.py +19Lines changed: 19 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1780,6 +1780,25 @@ def test_to_api_repr_w_nested_struct(self): | 1780 | 1780 | param = self._make_one("foo", scalar_1, sub) | 1781 | 1781 | self.assertEqual(param.to_api_repr(), EXPECTED) | 1782 | 1782 |
| | 1783 | + def test_to_api_repr_w_unknown_type(self): | | 1784 | + EXPECTED = { | | 1785 | + "name": "foo", | | 1786 | + "parameterType": { | | 1787 | + "type": "STRUCT", | | 1788 | + "structTypes": [ | | 1789 | + {"name": "bar", "type": {"type": "INT64"}}, | | 1790 | + {"name": "baz", "type": {"type": "UNKNOWN_TYPE"}}, | | 1791 | + ], | | 1792 | + }, | | 1793 | + "parameterValue": { | | 1794 | + "structValues": {"bar": {"value": "123"}, "baz": {"value": "abc"}} | | 1795 | + }, | | 1796 | + } | | 1797 | + sub_1 = _make_subparam("bar", "INT64", 123) | | 1798 | + sub_2 = _make_subparam("baz", "UNKNOWN_TYPE", "abc") | | 1799 | + param = self._make_one("foo", sub_1, sub_2) | | 1800 | + self.assertEqual(param.to_api_repr(), EXPECTED) | | 1801 | + | 1783 | 1802 | def test___eq___wrong_type(self): | 1784 | 1803 | field = self._make_one("test", _make_subparam("bar", "STRING", "abc")) | 1785 | 1804 | other = object() | |
Collapse file Copy file name to clipboardExpand all lines: tests/unit/test_table.py +3-3Lines changed: 3 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2751,9 +2751,9 @@ def test_to_arrow_w_unknown_type(self): | 2751 | 2751 | self.assertEqual(ages, [33, 29]) | 2752 | 2752 | self.assertEqual(sports, ["volleyball", "basketball"]) | 2753 | 2753 |
| 2754 | | - self.assertEqual(len(warned), 1) | 2755 | | - warning = warned[0] | 2756 | | - self.assertTrue("sport" in str(warning)) | | 2754 | + # Expect warning from both the arrow conversion, and the json deserialization. | | 2755 | + self.assertEqual(len(warned), 2) | | 2756 | + self.assertTrue(all("sport" in str(warning) for warning in warned)) | 2757 | 2757 |
| 2758 | 2758 | def test_to_arrow_w_empty_table(self): | 2759 | 2759 | pyarrow = pytest.importorskip( | |
|