Skip to content
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

feat: Support passing struct data to the DB API #718

Merged
merged 26 commits into from Jul 1, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
586a022
Added support for dbi struct parameters with explicit types
jimfulton Jun 22, 2021
13f9107
Make the dataset_id fixture a session fixture
jimfulton Jun 23, 2021
aea7bae
Make the dataset_id fixture a session fixture
jimfulton Jun 23, 2021
6f26130
system test of the struct machinery
jimfulton Jun 23, 2021
c386448
Verify that we can bind non-parameterized types
jimfulton Jun 23, 2021
71c8614
Parse and remove type parameters from explcit types.
jimfulton Jun 23, 2021
6f5b345
Document passing struct data.
jimfulton Jun 23, 2021
e08f6f6
Merge remote-tracking branch 'origin/master' into riversnake-dbi-stru…
jimfulton Jun 23, 2021
411c336
blacken
jimfulton Jun 23, 2021
ef2b323
using match.groups() throws off pytypes, also fix some type hints.
jimfulton Jun 23, 2021
654b108
🦉 Updates from OwlBot
gcf-owl-bot[bot] Jun 23, 2021
525b8fd
blacken
jimfulton Jun 23, 2021
462f2eb
remove type hints -- maybe they broke docs?
jimfulton Jun 23, 2021
904d2ce
merge upstream
jimfulton Jun 23, 2021
a6393e6
Revert "remove type hints -- maybe they broke docs?"
jimfulton Jun 23, 2021
e63e8b7
pin gcp-sphinx-docfx-yaml==0.2.0 so docfx doesn't fail.
jimfulton Jun 23, 2021
2f2bdcd
Merge remote-tracking branch 'origin/master' into riversnake-dbi-stru…
jimfulton Jun 24, 2021
91b0028
Review comments: examples, and guard against large number of fields
jimfulton Jun 24, 2021
35555aa
🦉 Updates from OwlBot
gcf-owl-bot[bot] Jun 24, 2021
0d81b80
Merge remote-tracking branch 'origin/master' into riversnake-dbi-stru…
jimfulton Jun 24, 2021
d3f959c
Merge branch 'riversnake-dbi-struct-types' of github.com:googleapis/p…
jimfulton Jun 24, 2021
5554301
Factored some repeated code in handling complex parameters
jimfulton Jun 25, 2021
8830113
Improved the error for dict (structish) parameter values without expl…
jimfulton Jun 25, 2021
a72cafb
blacken
jimfulton Jun 25, 2021
cba9697
removed repeated word
jimfulton Jun 25, 2021
12bd941
Update google/cloud/bigquery/dbapi/_helpers.py
jimfulton Jun 29, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
62 changes: 24 additions & 38 deletions google/cloud/bigquery/dbapi/_helpers.py
Expand Up @@ -302,6 +302,24 @@ def complex_query_parameter(
return param


def _dispatch_parameter(type_, value, name=None):
if type_ is not None and "<" in type_:
param = complex_query_parameter(name, value, type_)
elif isinstance(value, collections_abc.Mapping):
raise NotImplementedError(
f"STRUCT-like parameter values are not supported"
f"{' (parameter ' + name + ')' if name else ''},"
f" unless an explicit type is give in the parameter placeholder"
f" (e.g. '%({name if name else ''}:struct<...>)s')."
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much more informative, great!

elif array_like(value):
param = array_to_query_parameter(value, name, type_)
else:
param = scalar_to_query_parameter(value, name, type_)

return param


def to_query_parameters_list(parameters, parameter_types):
"""Converts a sequence of parameter values into query parameters.

Expand All @@ -315,21 +333,9 @@ def to_query_parameters_list(parameters, parameter_types):
List[google.cloud.bigquery.query._AbstractQueryParameter]:
A list of query parameters.
"""
result = []

for value, type_ in zip(parameters, parameter_types):
if type_ is not None and "<" in type_:
param = complex_query_parameter(None, value, type_)
elif isinstance(value, collections_abc.Mapping):
raise NotImplementedError("STRUCT-like parameter values are not supported.")
elif array_like(value):
param = array_to_query_parameter(value, None, type_)
else:
param = scalar_to_query_parameter(value, None, type_)

result.append(param)

return result
return [_dispatch_parameter(type_, value)
for value, type_ in zip(parameters, parameter_types)
]


def to_query_parameters_dict(parameters, query_parameter_types):
Expand All @@ -345,29 +351,9 @@ def to_query_parameters_dict(parameters, query_parameter_types):
List[google.cloud.bigquery.query._AbstractQueryParameter]:
A list of named query parameters.
"""
result = []

for name, value in parameters.items():
query_parameter_type = query_parameter_types.get(name)
if query_parameter_type is not None and "<" in query_parameter_type:
param = complex_query_parameter(name, value, query_parameter_type)
elif isinstance(value, collections_abc.Mapping):
raise NotImplementedError(
"STRUCT-like parameter values are not supported "
"(parameter {}).".format(name)
)
elif array_like(value):
param = array_to_query_parameter(
value, name=name, query_parameter_type=query_parameter_type
)
else:
param = scalar_to_query_parameter(
value, name=name, query_parameter_type=query_parameter_type,
)

result.append(param)

return result
return [_dispatch_parameter(query_parameter_types.get(name), value, name)
for name, value in parameters.items()
]


def to_query_parameters(parameters, parameter_types):
Expand Down
29 changes: 27 additions & 2 deletions tests/unit/test_dbapi__helpers.py
Expand Up @@ -563,6 +563,10 @@ def test_complex_query_parameter_type(type_, value, expect):
assert param == expect


def _expected_error_match(expect):
return "^" + re.escape(expect) + "$"


@pytest.mark.parametrize(
"value,type_,expect",
[
Expand Down Expand Up @@ -599,11 +603,10 @@ def test_complex_query_parameter_type_errors(type_, value, expect):
from google.cloud.bigquery.dbapi import exceptions

with pytest.raises(
exceptions.ProgrammingError, match="^" + re.escape(expect) + "$",
exceptions.ProgrammingError, match=_expected_error_match(expect),
):
complex_query_parameter("test", value, type_)


@pytest.mark.parametrize(
"parameters,parameter_types,expect",
[
Expand Down Expand Up @@ -666,3 +669,25 @@ def test_to_query_parameters_complex_types(parameters, parameter_types, expect):

result = [p.to_api_repr() for p in to_query_parameters(parameters, parameter_types)]
assert result == expect

def test_to_query_parameters_struct_error():
from google.cloud.bigquery.dbapi._helpers import to_query_parameters

with pytest.raises(
NotImplementedError,
match=_expected_error_match(
"STRUCT-like parameter values are not supported, "
"unless an explicit type is give in the parameter placeholder "
"(e.g. '%(:struct<...>)s').")
):
to_query_parameters([dict(x=1)], [None])

with pytest.raises(
NotImplementedError,
match=_expected_error_match(
"STRUCT-like parameter values are not supported (parameter foo), "
"unless an explicit type is give in the parameter placeholder "
"(e.g. '%(foo:struct<...>)s').")
):
to_query_parameters(dict(foo=dict(x=1)), {})