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

Support inspection of Redshift datatypes #242

Merged

Conversation

Brooke-white
Copy link
Contributor

This PR modifies RedshiftDialectMixin to support database schema inspection of Amazon Redshift datatypes. It adds entries in ischema_names for these Redshift specific datatypes. It also makes a small change to the definition of Timetz and Timestamptz datatype definition for compatibility with SqlAlchemy insepctor.

Use case: Support for Redshift datatypes in Querybook Metastore

Issue: When inspect.get_columns(...) is called, NullType() is returned as the type for columns with type geometry and super, which causes issues in downstream tools like Querybook's metastore.

Repro:

from sqlalchemy.engine import reflection
from sqlalchemy import create_engine
engine = create_engine('redshift+redshift_connector://...')
inspect = reflection.Inspector.from_engine(engine)
x = inspect.get_columns(table_name='my_table', schema='public')

where my_table is defined as

create table public.my_table (c1 geometry, c2 super, c3 timetz, c4 timestamptz);

For columns with type timetz and timestamptz, sa.dialects.postgresql.TIME and sa.dialects.postgresql.TIMESTAMP are returned, which isn't ideal since we've defined TIMETZ and TIMESTAMPTZ datatypes within sqlalchemy-redshift.

sqlalchemy.engine.dialects.postgresql.base.PGDialect._get_column_info uses the ischema_names to map datatype string names to datatype classes.

screenshots below show the behavior before & after this change when the above use case is run:

note entries 11, 12, 15, 16

behavior before:

Screen Shot 2021-11-16 at 9 50 19 AM

behavior after:
Screen Shot 2021-11-16 at 9 48 01 AM

Todos

  • [ x ] MIT compatible
  • [ x ] Tests
  • [ x ] Documentation
  • [ x ] Updated CHANGES.rst

@Brooke-white Brooke-white marked this pull request as draft November 16, 2021 18:51
Copy link
Member

@jklukas jklukas left a comment

Choose a reason for hiding this comment

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

Left some early thoughts. Feel free to force push the content to the trigger-integration branch of the main repo when you're ready to have integration tests run.

sqlalchemy_redshift/dialect.py Show resolved Hide resolved
@@ -260,6 +260,13 @@ def process_bind_param(self, value, dialect):
return json.dumps(value)
return value

# Mapping for database schema inspection of Amazon Redshift datatypes
redshift_ischema_names = {
Copy link
Member

Choose a reason for hiding this comment

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

Can we define this constant in UPPER_CAMEL_CASE and move it to the top of the file with other constants?

@Brooke-white
Copy link
Contributor Author

I added a few more changes here as I noticed there were still some issues with type inspection

Use case:

from sqlalchemy_redshift.dialect import TIMETZ, SUPER, TIMESTAMPTZ, GEOMETRY

for x in (TIMETZ(), TIMESTAMPTZ(), SUPER(), GEOMETRY()):
    print(x)
TIMETZ
TIMESTAMPTZ
SUPER
GEOMETRY

previous behavior

sqlalchemy.exc.UnsupportedCompilationError: Compiler <sqlalchemy.sql.compiler.GenericTypeCompiler object at 0x7fdd6427e610> can't render element of type <class 'sqlalchemy_redshift.dialect.TIMETZ'> (Background on this error at: http://sqlalche.me/e/l7de)
  • Has RedshiftDialectMixin inherit from DefaultDialect so the default dialect initializer is called upon creation of a RedshiftDialectMixin object, so dialect attributes such as type_compiler are initialized. (the use case is the above mentioned type compilation)

  • Modifies Redshift data types to inherit from RedshiftTypeEngine so compilation of these types results in the RedshiftDialectMixin being utilized.

  • Adds docs for TIMETZ and TIMESTAMPTZ’s unused timezone parameter

  • Changes redshift_ischema_names -> REDSHIFT_ISCHEMA_NAMES

I don’t think we can move REDSHIFT_ISCHEMA_NAMES to be defined with the rest of the constants at the top of dialect.py as it makes reference to the Redshift data types (e.g. TIMETZ) which are defined after the constants. I’m open to any suggestions as to where else this constant could live

@Brooke-white Brooke-white changed the title fix(dialect, metadata): support inspection of Redshift datatypes Support inspection of Redshift datatypes Nov 19, 2021
@Brooke-white
Copy link
Contributor Author

Feel free to force push the content to the trigger-integration branch of the main repo when you're ready to have integration tests run.

Hmm, I pushed to trigger-integration but encountered some message saying build have been temporarily disabled. I'll pick this PR back up following Thanksgiving week. Enjoy the holiday! :)

@jklukas
Copy link
Member

jklukas commented Nov 19, 2021

but encountered some message saying build have been temporarily disabled

I'll contact Travis support to get this unstuck.

Enjoy the holiday! :)

Likewise!

@Brooke-white Brooke-white marked this pull request as ready for review December 1, 2021 13:16
@Brooke-white
Copy link
Contributor Author

Thanks for getting Travis going again, @jklukas! Integration tests are all passing. Am I ok to merge?

@jklukas
Copy link
Member

jklukas commented Dec 1, 2021

Looks good! Feel free to merge.

@Brooke-white Brooke-white merged commit 08eb502 into sqlalchemy-redshift:main Dec 1, 2021
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.

None yet

2 participants