GeezerVison is showing github.com — feat: add support for INTERVAL data type to `list_rows` (#840) · techsoft29/python-bigquery@e37380a
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: docs/conf.py +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 366 | 366 | "grpc": ("https://grpc.github.io/grpc/python/", None), | 367 | 367 | "proto-plus": ("https://proto-plus-python.readthedocs.io/en/latest/", None), | 368 | 368 | "protobuf": ("https://googleapis.dev/python/protobuf/latest/", None), | 369 | | - "pandas": ("http://pandas.pydata.org/pandas-docs/stable/", None), | | 369 | + "dateutil": ("https://dateutil.readthedocs.io/en/latest/", None), | 370 | 370 | "geopandas": ("https://geopandas.org/", None), | | 371 | + "pandas": ("https://pandas.pydata.org/pandas-docs/dev", None), | 371 | 372 | } | 372 | 373 |
| 373 | 374 |
| |
Collapse file Copy file name to clipboardExpand all lines: google/cloud/bigquery/_helpers.py +46-1Lines changed: 46 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 19 | 19 | import decimal | 20 | 20 | import math | 21 | 21 | import re | 22 | | -from typing import Any, Union | | 22 | +from typing import Any, Optional, Union | 23 | 23 |
| | 24 | +from dateutil import relativedelta | 24 | 25 | from google.cloud._helpers import UTC | 25 | 26 | from google.cloud._helpers import _date_from_iso8601_date | 26 | 27 | from google.cloud._helpers import _datetime_from_microseconds | | 45 | 46 | re.VERBOSE, | 46 | 47 | ) | 47 | 48 |
| | 49 | +# BigQuery sends INTERVAL data in "canonical format" | | 50 | +# https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#interval_type | | 51 | +_INTERVAL_PATTERN = re.compile( | | 52 | + r"(?P<calendar_sign>-?)(?P<years>\d+)-(?P<months>\d+) " | | 53 | + r"(?P<days>-?\d+) " | | 54 | + r"(?P<time_sign>-?)(?P<hours>\d+):(?P<minutes>\d+):(?P<seconds>\d+)\.?(?P<fraction>\d*)?$" | | 55 | +) | | 56 | + | 48 | 57 | _MIN_PYARROW_VERSION = packaging.version.Version("3.0.0") | 49 | 58 | _MIN_BQ_STORAGE_VERSION = packaging.version.Version("2.0.0") | 50 | 59 | _BQ_STORAGE_OPTIONAL_READ_SESSION_VERSION = packaging.version.Version("2.6.0") | @@ -191,6 +200,41 @@ def _int_from_json(value, field): | 191 | 200 | return int(value) | 192 | 201 |
| 193 | 202 |
| | 203 | +def _interval_from_json( | | 204 | + value: Optional[str], field | | 205 | +) -> Optional[relativedelta.relativedelta]: | | 206 | + """Coerce 'value' to an interval, if set or not nullable.""" | | 207 | + if not _not_null(value, field): | | 208 | + return None | | 209 | + if value is None: | | 210 | + raise TypeError(f"got {value} for REQUIRED field: {repr(field)}") | | 211 | + | | 212 | + parsed = _INTERVAL_PATTERN.match(value) | | 213 | + if parsed is None: | | 214 | + raise ValueError(f"got interval: '{value}' with unexpected format") | | 215 | + | | 216 | + calendar_sign = -1 if parsed.group("calendar_sign") == "-" else 1 | | 217 | + years = calendar_sign * int(parsed.group("years")) | | 218 | + months = calendar_sign * int(parsed.group("months")) | | 219 | + days = int(parsed.group("days")) | | 220 | + time_sign = -1 if parsed.group("time_sign") == "-" else 1 | | 221 | + hours = time_sign * int(parsed.group("hours")) | | 222 | + minutes = time_sign * int(parsed.group("minutes")) | | 223 | + seconds = time_sign * int(parsed.group("seconds")) | | 224 | + fraction = parsed.group("fraction") | | 225 | + microseconds = time_sign * int(fraction.ljust(6, "0")[:6]) if fraction else 0 | | 226 | + | | 227 | + return relativedelta.relativedelta( | | 228 | + years=years, | | 229 | + months=months, | | 230 | + days=days, | | 231 | + hours=hours, | | 232 | + minutes=minutes, | | 233 | + seconds=seconds, | | 234 | + microseconds=microseconds, | | 235 | + ) | | 236 | + | | 237 | + | 194 | 238 | def _float_from_json(value, field): | 195 | 239 | """Coerce 'value' to a float, if set or not nullable.""" | 196 | 240 | if _not_null(value, field): | @@ -327,6 +371,7 @@ def _record_from_json(value, field): | 327 | 371 | _CELLDATA_FROM_JSON = { | 328 | 372 | "INTEGER": _int_from_json, | 329 | 373 | "INT64": _int_from_json, | | 374 | + "INTERVAL": _interval_from_json, | 330 | 375 | "FLOAT": _float_from_json, | 331 | 376 | "FLOAT64": _float_from_json, | 332 | 377 | "NUMERIC": _decimal_from_json, | |
Collapse file Copy file name to clipboardExpand all lines: google/cloud/bigquery/enums.py +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -254,6 +254,7 @@ class SqlTypeNames(str, enum.Enum): | 254 | 254 | DATE = "DATE" | 255 | 255 | TIME = "TIME" | 256 | 256 | DATETIME = "DATETIME" | | 257 | + INTERVAL = "INTERVAL" # NOTE: not available in legacy types | 257 | 258 |
| 258 | 259 |
| 259 | 260 | class SqlParameterScalarTypes: | |
Collapse file Copy file name to clipboardExpand all lines: owlbot.py +2-5Lines changed: 2 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 98 | 98 | microgenerator=True, | 99 | 99 | split_system_tests=True, | 100 | 100 | intersphinx_dependencies={ | 101 | | - "pandas": "http://pandas.pydata.org/pandas-docs/stable/", | | 101 | + "dateutil": "https://dateutil.readthedocs.io/en/latest/", | 102 | 102 | "geopandas": "https://geopandas.org/", | | 103 | + "pandas": "https://pandas.pydata.org/pandas-docs/dev", | 103 | 104 | }, | 104 | 105 | ) | 105 | 106 |
| | 115 | 116 | # Include custom SNIPPETS_TESTS job for performance. | 116 | 117 | # https://github.com/googleapis/python-bigquery/issues/191 | 117 | 118 | ".kokoro/presubmit/presubmit.cfg", | 118 | | - # Group all renovate PRs together. If this works well, remove this and | 119 | | - # update the shared templates (possibly with configuration option to | 120 | | - # py_library.) | 121 | | - "renovate.json", | 122 | 119 | ], | 123 | 120 | ) | 124 | 121 |
| |
Collapse file Copy file name to clipboardExpand all lines: renovate.json +4-1Lines changed: 4 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | { | 2 | 2 | "extends": [ | 3 | | - "config:base", "group:all", ":preserveSemverRanges" | | 3 | + "config:base", | | 4 | + "group:all", | | 5 | + ":preserveSemverRanges", | | 6 | + ":disableDependencyDashboard" | 4 | 7 | ], | 5 | 8 | "ignorePaths": [".pre-commit-config.yaml"], | 6 | 9 | "pip_requirements": { | |
Collapse file Copy file name to clipboardExpand all lines: setup.py +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 42 | 42 | "google-resumable-media >= 0.6.0, < 3.0dev", | 43 | 43 | "packaging >= 14.3", | 44 | 44 | "protobuf >= 3.12.0", | | 45 | + "python-dateutil >= 2.7.2, <3.0dev", | 45 | 46 | "requests >= 2.18.0, < 3.0.0dev", | 46 | 47 | ] | 47 | 48 | extras = { | |
Collapse file Copy file name to clipboardExpand all lines: testing/constraints-3.6.txt +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -18,6 +18,7 @@ pandas==0.24.2 | 18 | 18 | proto-plus==1.10.0 | 19 | 19 | protobuf==3.12.0 | 20 | 20 | pyarrow==3.0.0 | | 21 | +python-dateutil==2.7.2 | 21 | 22 | requests==2.18.0 | 22 | 23 | Shapely==1.6.0 | 23 | 24 | six==1.13.0 | |
Collapse file Copy file name to clipboardExpand all lines: tests/system/test_client.py -5Lines changed: 0 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 37 | 37 | except ImportError: # pragma: NO COVER | 38 | 38 | bigquery_storage = None | 39 | 39 |
| 40 | | -try: | 41 | | - import fastavro # to parse BQ storage client results | 42 | | -except ImportError: # pragma: NO COVER | 43 | | - fastavro = None | 44 | | - | 45 | 40 | try: | 46 | 41 | import pyarrow | 47 | 42 | import pyarrow.types | |
Collapse file Copy file name to clipboardExpand all lines: tests/system/test_list_rows.py +8Lines changed: 8 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 15 | 15 | import datetime | 16 | 16 | import decimal | 17 | 17 |
| | 18 | +from dateutil import relativedelta | | 19 | + | 18 | 20 | from google.cloud import bigquery | 19 | 21 | from google.cloud.bigquery import enums | 20 | 22 |
| @@ -64,6 +66,9 @@ def test_list_rows_scalars(bigquery_client: bigquery.Client, scalars_table: str) | 64 | 66 | assert row["datetime_col"] == datetime.datetime(2021, 7, 21, 11, 39, 45) | 65 | 67 | assert row["geography_col"] == "POINT(-122.0838511 37.3860517)" | 66 | 68 | assert row["int64_col"] == 123456789 | | 69 | + assert row["interval_col"] == relativedelta.relativedelta( | | 70 | + years=7, months=11, days=9, hours=4, minutes=15, seconds=37, microseconds=123456 | | 71 | + ) | 67 | 72 | assert row["numeric_col"] == decimal.Decimal("1.23456789") | 68 | 73 | assert row["bignumeric_col"] == decimal.Decimal("10.111213141516171819") | 69 | 74 | assert row["float64_col"] == 1.25 | @@ -95,6 +100,9 @@ def test_list_rows_scalars_extreme( | 95 | 100 | assert row["datetime_col"] == datetime.datetime(9999, 12, 31, 23, 59, 59, 999999) | 96 | 101 | assert row["geography_col"] == "POINT(-135 90)" | 97 | 102 | assert row["int64_col"] == 9223372036854775807 | | 103 | + assert row["interval_col"] == relativedelta.relativedelta( | | 104 | + years=-10000, days=-3660000, hours=-87840000 | | 105 | + ) | 98 | 106 | assert row["numeric_col"] == decimal.Decimal(f"9.{'9' * 37}E+28") | 99 | 107 | assert row["bignumeric_col"] == decimal.Decimal(f"9.{'9' * 75}E+37") | 100 | 108 | assert row["float64_col"] == float("Inf") | |
Collapse file Copy file name to clipboard +157Lines changed: 157 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +# Copyright 2021 Google LLC | | 2 | +# | | 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); | | 4 | +# you may not use this file except in compliance with the License. | | 5 | +# You may obtain a copy of the License at | | 6 | +# | | 7 | +# http://www.apache.org/licenses/LICENSE-2.0 | | 8 | +# | | 9 | +# Unless required by applicable law or agreed to in writing, software | | 10 | +# distributed under the License is distributed on an "AS IS" BASIS, | | 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | | 12 | +# See the License for the specific language governing permissions and | | 13 | +# limitations under the License. | | 14 | + | | 15 | +from dateutil.relativedelta import relativedelta | | 16 | +import pytest | | 17 | + | | 18 | +from google.cloud.bigquery.schema import SchemaField | | 19 | + | | 20 | + | | 21 | +def create_field(mode="NULLABLE", type_="IGNORED"): | | 22 | + return SchemaField("test_field", type_, mode=mode) | | 23 | + | | 24 | + | | 25 | +@pytest.fixture | | 26 | +def mut(): | | 27 | + from google.cloud.bigquery import _helpers | | 28 | + | | 29 | + return _helpers | | 30 | + | | 31 | + | | 32 | +def test_interval_from_json_w_none_nullable(mut): | | 33 | + got = mut._interval_from_json(None, create_field()) | | 34 | + assert got is None | | 35 | + | | 36 | + | | 37 | +def test_interval_from_json_w_none_required(mut): | | 38 | + with pytest.raises(TypeError): | | 39 | + mut._interval_from_json(None, create_field(mode="REQUIRED")) | | 40 | + | | 41 | + | | 42 | +def test_interval_from_json_w_invalid_format(mut): | | 43 | + with pytest.raises(ValueError, match="NOT_AN_INTERVAL"): | | 44 | + mut._interval_from_json("NOT_AN_INTERVAL", create_field()) | | 45 | + | | 46 | + | | 47 | +@pytest.mark.parametrize( | | 48 | + ("value", "expected"), | | 49 | + ( | | 50 | + ("0-0 0 0:0:0", relativedelta()), | | 51 | + # SELECT INTERVAL X YEAR | | 52 | + ("-10000-0 0 0:0:0", relativedelta(years=-10000)), | | 53 | + ("-1-0 0 0:0:0", relativedelta(years=-1)), | | 54 | + ("1-0 0 0:0:0", relativedelta(years=1)), | | 55 | + ("10000-0 0 0:0:0", relativedelta(years=10000)), | | 56 | + # SELECT INTERVAL X MONTH | | 57 | + ("-0-11 0 0:0:0", relativedelta(months=-11)), | | 58 | + ("-0-1 0 0:0:0", relativedelta(months=-1)), | | 59 | + ("0-1 0 0:0:0", relativedelta(months=1)), | | 60 | + ("0-11 0 0:0:0", relativedelta(months=11)), | | 61 | + # SELECT INTERVAL X DAY | | 62 | + ("0-0 -3660000 0:0:0", relativedelta(days=-3660000)), | | 63 | + ("0-0 -1 0:0:0", relativedelta(days=-1)), | | 64 | + ("0-0 1 0:0:0", relativedelta(days=1)), | | 65 | + ("0-0 3660000 0:0:0", relativedelta(days=3660000)), | | 66 | + # SELECT INTERVAL X HOUR | | 67 | + ("0-0 0 -87840000:0:0", relativedelta(hours=-87840000)), | | 68 | + ("0-0 0 -1:0:0", relativedelta(hours=-1)), | | 69 | + ("0-0 0 1:0:0", relativedelta(hours=1)), | | 70 | + ("0-0 0 87840000:0:0", relativedelta(hours=87840000)), | | 71 | + # SELECT INTERVAL X MINUTE | | 72 | + ("0-0 0 -0:59:0", relativedelta(minutes=-59)), | | 73 | + ("0-0 0 -0:1:0", relativedelta(minutes=-1)), | | 74 | + ("0-0 0 0:1:0", relativedelta(minutes=1)), | | 75 | + ("0-0 0 0:59:0", relativedelta(minutes=59)), | | 76 | + # SELECT INTERVAL X SECOND | | 77 | + ("0-0 0 -0:0:59", relativedelta(seconds=-59)), | | 78 | + ("0-0 0 -0:0:1", relativedelta(seconds=-1)), | | 79 | + ("0-0 0 0:0:1", relativedelta(seconds=1)), | | 80 | + ("0-0 0 0:0:59", relativedelta(seconds=59)), | | 81 | + # SELECT (INTERVAL -1 SECOND) / 1000000 | | 82 | + ("0-0 0 -0:0:0.000001", relativedelta(microseconds=-1)), | | 83 | + ("0-0 0 -0:0:59.999999", relativedelta(seconds=-59, microseconds=-999999)), | | 84 | + ("0-0 0 -0:0:59.999", relativedelta(seconds=-59, microseconds=-999000)), | | 85 | + ("0-0 0 0:0:59.999", relativedelta(seconds=59, microseconds=999000)), | | 86 | + ("0-0 0 0:0:59.999999", relativedelta(seconds=59, microseconds=999999)), | | 87 | + # Test with multiple digits in each section. | | 88 | + ( | | 89 | + "32-11 45 67:16:23.987654", | | 90 | + relativedelta( | | 91 | + years=32, | | 92 | + months=11, | | 93 | + days=45, | | 94 | + hours=67, | | 95 | + minutes=16, | | 96 | + seconds=23, | | 97 | + microseconds=987654, | | 98 | + ), | | 99 | + ), | | 100 | + ( | | 101 | + "-32-11 -45 -67:16:23.987654", | | 102 | + relativedelta( | | 103 | + years=-32, | | 104 | + months=-11, | | 105 | + days=-45, | | 106 | + hours=-67, | | 107 | + minutes=-16, | | 108 | + seconds=-23, | | 109 | + microseconds=-987654, | | 110 | + ), | | 111 | + ), | | 112 | + # Test with mixed +/- sections. | | 113 | + ( | | 114 | + "9999-9 -999999 9999999:59:59.999999", | | 115 | + relativedelta( | | 116 | + years=9999, | | 117 | + months=9, | | 118 | + days=-999999, | | 119 | + hours=9999999, | | 120 | + minutes=59, | | 121 | + seconds=59, | | 122 | + microseconds=999999, | | 123 | + ), | | 124 | + ), | | 125 | + # Test with fraction that is not microseconds. | | 126 | + ("0-0 0 0:0:42.", relativedelta(seconds=42)), | | 127 | + ("0-0 0 0:0:59.1", relativedelta(seconds=59, microseconds=100000)), | | 128 | + ("0-0 0 0:0:0.12", relativedelta(microseconds=120000)), | | 129 | + ("0-0 0 0:0:0.123", relativedelta(microseconds=123000)), | | 130 | + ("0-0 0 0:0:0.1234", relativedelta(microseconds=123400)), | | 131 | + # Fractional seconds can cause rounding problems if cast to float. See: | | 132 | + # https://github.com/googleapis/python-db-dtypes-pandas/issues/18 | | 133 | + ("0-0 0 0:0:59.876543", relativedelta(seconds=59, microseconds=876543)), | | 134 | + ( | | 135 | + "0-0 0 01:01:01.010101", | | 136 | + relativedelta(hours=1, minutes=1, seconds=1, microseconds=10101), | | 137 | + ), | | 138 | + ( | | 139 | + "0-0 0 09:09:09.090909", | | 140 | + relativedelta(hours=9, minutes=9, seconds=9, microseconds=90909), | | 141 | + ), | | 142 | + ( | | 143 | + "0-0 0 11:11:11.111111", | | 144 | + relativedelta(hours=11, minutes=11, seconds=11, microseconds=111111), | | 145 | + ), | | 146 | + ( | | 147 | + "0-0 0 19:16:23.987654", | | 148 | + relativedelta(hours=19, minutes=16, seconds=23, microseconds=987654), | | 149 | + ), | | 150 | + # Nanoseconds are not expected, but should not cause error. | | 151 | + ("0-0 0 0:0:00.123456789", relativedelta(microseconds=123456)), | | 152 | + ("0-0 0 0:0:59.87654321", relativedelta(seconds=59, microseconds=876543)), | | 153 | + ), | | 154 | +) | | 155 | +def test_w_string_values(mut, value, expected): | | 156 | + got = mut._interval_from_json(value, create_field()) | | 157 | + assert got == expected |
|