Skip to content

Commit 93d15e2

Browse files
authored
feat: support ScalarQueryParameterType for type_ argument in ScalarQueryParameter constructor (#850)
Follow-up to https://github.com/googleapis/python-bigquery/pull/840/files#r679880582 Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly: - [ ] Make sure to open an issue as a [bug/issue](https://github.com/googleapis/python-bigquery/issues/new/choose) before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea - [ ] Ensure the tests and linter pass - [ ] Code coverage does not decrease (if any source code was changed) - [ ] Appropriate docs were updated (if necessary)
1 parent c1a3d44 commit 93d15e2

File tree

5 files changed

+57
-24
lines changed

5 files changed

+57
-24
lines changed

docs/conf.py

+1
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@
110110
# directories to ignore when looking for source files.
111111
exclude_patterns = [
112112
"_build",
113+
"**/.nox/**/*",
113114
"samples/AUTHORING_GUIDE.md",
114115
"samples/CONTRIBUTING.md",
115116
"samples/snippets/README.rst",

docs/reference.rst

+1
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ Query
138138

139139
query.ArrayQueryParameter
140140
query.ScalarQueryParameter
141+
query.ScalarQueryParameterType
141142
query.StructQueryParameter
142143
query.UDFResource
143144

google/cloud/bigquery/enums.py

+12-12
Original file line numberDiff line numberDiff line change
@@ -259,23 +259,23 @@ class SqlTypeNames(str, enum.Enum):
259259
class SqlParameterScalarTypes:
260260
"""Supported scalar SQL query parameter types as type objects."""
261261

262-
STRING = ScalarQueryParameterType("STRING")
262+
BOOL = ScalarQueryParameterType("BOOL")
263+
BOOLEAN = ScalarQueryParameterType("BOOL")
264+
BIGDECIMAL = ScalarQueryParameterType("BIGNUMERIC")
265+
BIGNUMERIC = ScalarQueryParameterType("BIGNUMERIC")
263266
BYTES = ScalarQueryParameterType("BYTES")
264-
INTEGER = ScalarQueryParameterType("INT64")
265-
INT64 = ScalarQueryParameterType("INT64")
267+
DATE = ScalarQueryParameterType("DATE")
268+
DATETIME = ScalarQueryParameterType("DATETIME")
269+
DECIMAL = ScalarQueryParameterType("NUMERIC")
266270
FLOAT = ScalarQueryParameterType("FLOAT64")
267271
FLOAT64 = ScalarQueryParameterType("FLOAT64")
268-
NUMERIC = ScalarQueryParameterType("NUMERIC")
269-
BIGNUMERIC = ScalarQueryParameterType("BIGNUMERIC")
270-
DECIMAL = ScalarQueryParameterType("NUMERIC")
271-
BIGDECIMAL = ScalarQueryParameterType("BIGNUMERIC")
272-
BOOLEAN = ScalarQueryParameterType("BOOL")
273-
BOOL = ScalarQueryParameterType("BOOL")
274272
GEOGRAPHY = ScalarQueryParameterType("GEOGRAPHY")
275-
TIMESTAMP = ScalarQueryParameterType("TIMESTAMP")
276-
DATE = ScalarQueryParameterType("DATE")
273+
INT64 = ScalarQueryParameterType("INT64")
274+
INTEGER = ScalarQueryParameterType("INT64")
275+
NUMERIC = ScalarQueryParameterType("NUMERIC")
276+
STRING = ScalarQueryParameterType("STRING")
277277
TIME = ScalarQueryParameterType("TIME")
278-
DATETIME = ScalarQueryParameterType("DATETIME")
278+
TIMESTAMP = ScalarQueryParameterType("TIMESTAMP")
279279

280280

281281
class WriteDisposition(object):

google/cloud/bigquery/query.py

+30-12
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,21 @@
1616

1717
from collections import OrderedDict
1818
import copy
19-
from typing import Union
19+
import datetime
20+
import decimal
21+
from typing import Optional, Union
2022

2123
from google.cloud.bigquery.table import _parse_schema_resource
2224
from google.cloud.bigquery._helpers import _rows_from_json
2325
from google.cloud.bigquery._helpers import _QUERY_PARAMS_FROM_JSON
2426
from google.cloud.bigquery._helpers import _SCALAR_VALUE_TO_JSON_PARAM
2527

2628

29+
_SCALAR_VALUE_TYPE = Optional[
30+
Union[str, int, float, decimal.Decimal, bool, datetime.datetime, datetime.date]
31+
]
32+
33+
2734
class UDFResource(object):
2835
"""Describe a single user-defined function (UDF) resource.
2936
@@ -325,35 +332,46 @@ class ScalarQueryParameter(_AbstractQueryParameter):
325332
"""Named / positional query parameters for scalar values.
326333
327334
Args:
328-
name (Optional[str]):
335+
name:
329336
Parameter name, used via ``@foo`` syntax. If None, the
330337
parameter can only be addressed via position (``?``).
331338
332-
type_ (str):
333-
Name of parameter type. One of 'STRING', 'INT64',
334-
'FLOAT64', 'NUMERIC', 'BIGNUMERIC', 'BOOL', 'TIMESTAMP', 'DATETIME', or
335-
'DATE'.
339+
type_:
340+
Name of parameter type. See
341+
:class:`google.cloud.bigquery.enums.SqlTypeNames` and
342+
:class:`google.cloud.bigquery.enums.SqlParameterScalarTypes` for
343+
supported types.
336344
337-
value (Union[str, int, float, decimal.Decimal, bool, datetime.datetime, datetime.date]):
345+
value:
338346
The scalar parameter value.
339347
"""
340348

341-
def __init__(self, name, type_, value):
349+
def __init__(
350+
self,
351+
name: Optional[str],
352+
type_: Optional[Union[str, ScalarQueryParameterType]],
353+
value: _SCALAR_VALUE_TYPE,
354+
):
342355
self.name = name
343-
self.type_ = type_
356+
if isinstance(type_, ScalarQueryParameterType):
357+
self.type_ = type_._type
358+
else:
359+
self.type_ = type_
344360
self.value = value
345361

346362
@classmethod
347-
def positional(cls, type_: str, value) -> "ScalarQueryParameter":
363+
def positional(
364+
cls, type_: Union[str, ScalarQueryParameterType], value: _SCALAR_VALUE_TYPE
365+
) -> "ScalarQueryParameter":
348366
"""Factory for positional paramater.
349367
350368
Args:
351-
type_ (str):
369+
type_:
352370
Name of parameter type. One of 'STRING', 'INT64',
353371
'FLOAT64', 'NUMERIC', 'BIGNUMERIC', 'BOOL', 'TIMESTAMP', 'DATETIME', or
354372
'DATE'.
355373
356-
value (Union[str, int, float, decimal.Decimal, bool, datetime.datetime, datetime.date]):
374+
value:
357375
The scalar parameter value.
358376
359377
Returns:

tests/unit/test_query.py

+13
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414

1515
import datetime
16+
import decimal
1617
import unittest
1718

1819
import mock
@@ -430,6 +431,18 @@ def test_positional(self):
430431
self.assertEqual(param.type_, "INT64")
431432
self.assertEqual(param.value, 123)
432433

434+
def test_ctor_w_scalar_query_parameter_type(self):
435+
from google.cloud.bigquery import enums
436+
437+
param = self._make_one(
438+
name="foo",
439+
type_=enums.SqlParameterScalarTypes.BIGNUMERIC,
440+
value=decimal.Decimal("123.456"),
441+
)
442+
self.assertEqual(param.name, "foo")
443+
self.assertEqual(param.type_, "BIGNUMERIC")
444+
self.assertEqual(param.value, decimal.Decimal("123.456"))
445+
433446
def test_from_api_repr_w_name(self):
434447
RESOURCE = {
435448
"name": "foo",

0 commit comments

Comments
 (0)