Skip to content

[Issue 171] Improve sqlalchemy dialect backward compatibility with 1.3.24 #173

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

Merged
merged 11 commits into from
Jul 11, 2023

Conversation

susodapop
Copy link
Contributor

@susodapop susodapop commented Jul 10, 2023

Description

In #113 we relaxed the version requirement for SQLAlchemy. But some changes are needed to the client code to actually work with version 1.3. This wasn't caught in our e2e tests initially because our test infra installs the newest supported version of SQLAlchemy -- so the tests never actually ran under 1.3.24.

This pull request is split into easily understood commits that make sense to review one-by-one. Here are the changes needed to backport 1.3 support:

  • sqla 1.4 changed how connection strings are parsed (changelog). To use databricks-sql-connector with version 1.3 you must include http_path, catalog, and schema in your extra connect_args not in the connection string. This pull request reflects that change in examples/sqlalchemy.py and our test suite.
  • sqla 1.4 no longer automatically adds CHECK constraints for Boolean and Enum column types (changelog). To support version 1.3 in our code we have to explicitly disable this.
  • sqla 1.4 changed the dialect properties used to access the underlying dbapi connection (this is not mentioned in the changelog afaik), which we use when calling databricks-sql-connector's get_columns and get_tables methods.
  • sqla 1.4 changed the semantics when using the select() construct (changelog). That change is not backwards compatible. So I added conditional logic in our tests and example script.
  • Pandas requires sqlalchemy 1.4 so I add a skip condition for our pandas ingestion e2e test. It simply won't work for version 1.3
  • Issue Engine introspection is not working with SQLAlchemy==1.3.24 #171 tries to directly access the sqlalchemy Inspector object for reflection. I added a smoke test to ensure that this works in a basic use-case. This pull request does not implement a comprehensive suite of tests. More changes may be needed. We will expect further feedback from users on older versions of SQLAlchemy.

Related Tickets & Documents

Closes #171
Follow-up for #113

Tests

I ran the e2e tests with SQLAlchemy 1.4.48 and 1.3.24. All tests now pass. I also ran examples/sqlalchemy.py under both installations to confirm it works.

To test this yourself, check out this branch and run poetry install. Then run poetry run pip install sqlalchemy==1.3.24 to force the SQLAlchemy version back to 1.3. Then just run the sqlalchemy e2e tests.

You can also run pip install databricks-sql-connector==2.7.1.dev1 and run the examples/sqlalchemy.py script from a fresh virtualenv to ensure that it works for you.

1.3.24

CleanShot 2023-07-10 at 17 03 28@2x

1.4.48

CleanShot 2023-07-10 at 16 58 58@2x

Prior to this change, version 1.3.24 would error-out immediately.

Jesse Whitehouse added 10 commits July 10, 2023 16:39
catalog, and schema must instead be passed as connect_args.

This change allows our e2e tests to run. But they won't pass yet.

Signed-off-by: Jesse Whitehouse <[email protected]>
…that

Databricks doesn't support. This default is changed in versions > 1.4

https: //docs.sqlalchemy.org/en/20/changelog/migration_14.html#enum-and-boolean-datatypes-no-longer-default-to-create-constraint
Signed-off-by: Jesse Whitehouse <[email protected]>
different property names than SQLAlchemy > 1.4. So here I wrapped the logic
in a public method.

Signed-off-by: Jesse Whitehouse <[email protected]>
method

https: //docs.sqlalchemy.org/en/20/changelog/migration_14.html#orm-query-is-internally-unified-with-select-update-delete-2-0-style-execution-available
Signed-off-by: Jesse Whitehouse <[email protected]>
Use samples catalog to avoid flakiness on our internal infrastructure

Signed-off-by: Jesse Whitehouse <[email protected]>
self.catalog if the version is < 1.4. This means that you _must_ use
the new connection string format for >= 1.4

Signed-off-by: Jesse Whitehouse <[email protected]>
Signed-off-by: Jesse Whitehouse <[email protected]>
Signed-off-by: Jesse Whitehouse <[email protected]>
Signed-off-by: Jesse Whitehouse <[email protected]>
@@ -237,7 +238,11 @@ class SampleObject(base):
SampleObject.name.in_(["Bim Adewunmi", "Miki Meek"])
)

output = [i for i in session.scalars(stmt)]
if sqlalchemy_1_3():
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Whoops. This change belongs in a separate commit. Basically: version 1.3 doesn't have the .scalars method whereas 1.4 does.

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

@mattdeekay mattdeekay left a comment

Choose a reason for hiding this comment

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

Appreciate the detailed walkthrough with the commit messages!

@susodapop susodapop merged commit f45280d into main Jul 11, 2023
@susodapop susodapop deleted the issue-171 branch July 11, 2023 22:43
susodapop pushed a commit to unj1m/databricks-sql-python that referenced this pull request Sep 19, 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.

Engine introspection is not working with SQLAlchemy==1.3.24
3 participants