-
Notifications
You must be signed in to change notification settings - Fork 309
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: Extended DB API parameter syntax to optionally provide parameter types #626
Conversation
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.
Not a complete review by any means, just glanced over the PR and the regex pattern caught my eye, gave have two improvement suggestions.
@@ -26,9 +26,42 @@ | |||
|
|||
_NUMERIC_SERVER_MIN = decimal.Decimal("-9.9999999999999999999999999999999999999E+28") | |||
_NUMERIC_SERVER_MAX = decimal.Decimal("9.9999999999999999999999999999999999999E+28") | |||
_BIGQUERY_SCALAR_TYPES = { |
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.
Any chance we can use
class SqlParameterScalarTypes: |
_SQL_SCALAR_TYPES = frozenset( |
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.
Yes! I didn't know those existed.
So some questions:
-
SqlParameterScalarTypes
includes some aliases.
Is that something we want in this context? (If so, I'll lobby forINT
. ;)) -
SqlParameterScalarTypes
and_SQL_SCALAR_TYPES
lackDECIMAL
andBIGDECIMAL
.
https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#decimal_typesShould I add
DECIMAL
andBIGDECIMAL
?
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.
SqlParameterScalarTypes and _SQL_SCALAR_TYPES lack DECIMAL and BIGDECIMAL.
https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#decimal_types
Should I add DECIMAL and BIGDECIMAL?
Good catch! Filed #633 so that we can follow-up and add those.
SqlParameterScalarTypes includes some aliases.
Is that something we want in this context? (If so, I'll lobby for INT. ;))
Those aliases were intended to help folks who first learned BigQuery with "legacy" syntax. https://cloud.google.com/bigquery/data-types I think it's worth allowing the same aliases here in the DB-API.
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.
On second-thought, I don't know if we want to be adding DECIMAL
and BIGDECIMAL
as aliases. Let's leave them out of this implementation for now, and we can decide later if we want more aliases beyond those we have for Legacy SQL support.
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.
But they're documented in the standard sql docs.
self.execute(operation, parameters) | ||
if seq_of_parameters: | ||
formatted_operation, parameter_types = _format_operation( | ||
operation, seq_of_parameters[0] |
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.
Could you describe in a comment why we grab just the first one? I guess either we get the types correct on the first go, or we have a problem anyway?
Also it's a niche case, but it's possible that someone could pass in a generator or something for the sequence of parameters. In which case [0]
won't work. I think we need to either always loop like we had (and add some logic to only extract the types once) or do some funny business with the low-level iteration function.
I probably should have had a test case with this (generator as parameters) when I documented the allowed type as a Sequence.
Edit: Nevermind, Sequence supports integer-based item access https://docs.python.org/3/glossary.html#term-sequence It's "Iterable" that only supports iteration. We might actually want to support this use case, but probably best to wait until we actually get a customer request for that feature.
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.
Comment added.
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.
Looks good!
One optional suggestion, but I'm okay with solving the type aliases feature at a future date (if ever)
("values('%%(oof:INT64)s, %(foo)s, %(foo)s)", dict(foo="INT64")), | ||
), | ||
( | ||
"values(%(foo:INT64)s, %(foo:INT64)s)", |
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.
Can we add some tests that check our aliases, too? (INTEGER -> INT64, for example)
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.
I think there is a backend alias for INTEGER
, so it's not strictly necessary, but will be if we want to support aliases like DECIMAL
in the future.
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.
That's included in my latest push.
We do need it if we want to do client-side checks.
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.
Looks good, although I mostly focused on the params replacement logic and only glanced over the rest (but didn't see anything obvious to fix).
🤖 I have created a release \*beep\* \*boop\* --- ## [2.15.0](https://github.com/googleapis/python-bigquery/compare/v2.14.0...v2.15.0) (2021-04-29) ### Features * Extended DB API parameter syntax to optionally provide parameter types ([#626](https://github.com/googleapis/python-bigquery/issues/626)) ([8bcf397](https://github.com/googleapis/python-bigquery/commit/8bcf397fbe2527e06317741875a059b109cfcd9c)) ### Bug Fixes * add DECIMAL and BIGDECIMAL as aliases for NUMERIC and BIGNUMERIC ([#638](https://github.com/googleapis/python-bigquery/issues/638)) ([aa59023](https://github.com/googleapis/python-bigquery/commit/aa59023317b1c63720fb717b3544f755652da58d)) * The DB API Binary function accepts bytes data ([#630](https://github.com/googleapis/python-bigquery/issues/630)) ([4396e70](https://github.com/googleapis/python-bigquery/commit/4396e70771af6889d3242c37c5ff2e80241023a2)) --- This PR was generated with [Release Please](https://github.com/googleapis/release-please). See [documentation](https://github.com/googleapis/release-please#release-please).
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:
Fixes #609 🦕