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

[PECO-1109] Parameterized Query: add suport for inferring decimal types #228

Merged
merged 9 commits into from
Sep 26, 2023

Conversation

susodapop
Copy link
Contributor

@susodapop susodapop commented Sep 23, 2023

Description

This connector has strong support for Python Decimal types. But the parameterized query implementation merged last week is missing the ability to infer them. This pull request implements type inference for decimals with e2e tests for four different scenarios:

  • A parameter is set equal to a primitive Decimal() object (type must be inferred)
  • A Decimal() parameter value is wrapped in DbsqlParameter with the type set to None (type must be inferred)
  • A Decimal() parameter value is wrapped in DbsqlParameter and the type is set to DbsqlType.DECIMAL (type is not inferred but cast expression must be calculated)
  • A Decimal() parameter value is wrapped in DbsqlParameter and the type is set to a custom implementation (type is not inferred and cast expression is accepted from the user as-is)

In the first three scenarios, pysql will inspect the passed Decimal and provide the appropriate Databricks SQL cast expression to contain that decimal (such as DECIMAL(18,2) or DECIMAL(38,5))

I also split out the infer_types tests so we can make special assertions about decimals and ran isort over a couple of files.

I'll open a subsequent PR with a full example of how users can work with these APIs.

@susodapop susodapop changed the title [PECO-1109] Paramterized Query: add suport for inferring decimal types [PECO-1109] Parameterized Query: add suport for inferring decimal types Sep 25, 2023
Jesse Whitehouse added 3 commits September 25, 2023 14:00
STRING = "STRING"
DATE = "DATE"
TIMESTAMP = "TIMESTAMP"
FLOAT = "FLOAT"
DECIMAL = "DECIMAL"
DECIMAL = "DECIMAL(6,2)"
Copy link
Contributor Author

@susodapop susodapop Sep 25, 2023

Choose a reason for hiding this comment

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

If we keep this as just DECIMAL then DBR will create a decimal with zero precision after the decimal point because in Databricks SQL

SELECT CAST("1234.56" as DECIMAL)

returns 1235

Whereas

SELECT CAST("1234.56" as DECIMAL(6,2))

returns 1234.56

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As of 1565700 I've implemented dynamic casting and removed the DECIMAL(6,2) default

Jesse Whitehouse added 2 commits September 25, 2023 15:58
Signed-off-by: Jesse Whitehouse <[email protected]>
Copy link
Collaborator

@benc-db benc-db left a comment

Choose a reason for hiding this comment

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

LGTM

This lets us make an easier to understand series of assertions about Decimal inference

Note that `test_infer_types_decimal` tests a slightly different thing than its predecessor:
the predecessor would pass a Python float into the inference but force its DbsqlParameter type to DECIMAL
This causes the float value to be coerced. But this is an antipattern in Python.

If the user explicitly sets a DbsqlParameter object to type DECIMAL then
the user should also pass it a Python Decimal() object as its value, not a float

Signed-off-by: Jesse Whitehouse <[email protected]>
Copy link
Contributor

@nithinkdb nithinkdb left a comment

Choose a reason for hiding this comment

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

LGTM!

Jesse Whitehouse added 2 commits September 25, 2023 17:22
Signed-off-by: Jesse Whitehouse <[email protected]>
@susodapop
Copy link
Contributor Author

The python 3.8 lint check is hanging. Merging without this check passing as it passes for numerous other versions and appears to just be flaky.

@susodapop
Copy link
Contributor Author

Also, we need to update the changelog to discuss the new parameterisation features. But we can punt this to a subsequent PR as there is more work to do before this releases.

@susodapop susodapop merged commit 1239bff into main Sep 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants