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

DISTKEY and SORTKEY should be in quotations #74

Merged
merged 1 commit into from Apr 20, 2016

Conversation

solackerman
Copy link
Contributor

Redshift column names can contain spaces. A column name like Foo Bar will become a sql statement looking like this

SORTKEY (Foo Bar)

which will fail. This PR quotify's DISTKEY and SORTKEY, so they will appear like this:

SORTKEY ("Foo Bar")

@graingert
Copy link
Member

@solackerman can you create a model + reflection tests with a column name with spaces in?
eg:

class SortKeyDistKeyWithSpaces(Base):
    __tablename__ = 'sort_key_with_spaces'
    col1 = sa.Column('col with spaces', sa.Integer(), primary_key=True)
    __table_args__ = {
        'redshift_diststyle': 'EVEN',
        'redshift_sortkey': 'col with spaces',
        'redshift_distkey': 'col with spaces',
    }

@solackerman
Copy link
Contributor Author

done.

@graingert
Copy link
Member

@jklukas any thoughts?

@@ -255,7 +255,8 @@ def post_create_table(self, table):
for key in keys]
if interleaved_sortkey:
text += " INTERLEAVED"
text += " SORTKEY ({0})".format(", ".join(keys))
text += " SORTKEY ({0})".format(
", ".join('"' + key + '"' for key in keys))
Copy link
Member

Choose a reason for hiding this comment

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

I think we can have our cake and eat it too. SQLAlchemy's IdentifierPreparer.quote method exists to conditionally quote identifiers. I think we should use that here rather than putting in explicit quotes. It would be safer (there might be some edge cases we aren't considering if column names contain quotes themselves) and it means quotes aren't added to identifiers unless necessary, which would make output more natural.

So I think we could use something like:

sortkey_string = ", ".join(self.preparer.quote(key) for key in keys)
text += " SORTKEY ({0})".format(sortkey_string)

@jklukas
Copy link
Member

jklukas commented Jan 22, 2016

Thanks for finding this bug and putting together the PR, @solackerman. Would you agree that conditional quoting is the way to go?

@solackerman
Copy link
Contributor Author

Totally agree, that's much more elegant. I'll update the PR shortly.

@solackerman
Copy link
Contributor Author

done.

@jklukas
Copy link
Member

jklukas commented Jan 25, 2016

This looks great! +1

@@ -44,7 +44,7 @@ class ReflectionSortKey(Base):
col2 = sa.Column(sa.Integer())
__table_args__ = (
{'redshift_diststyle': 'EVEN',
'redshift_sortkey': ('col1, col2')}
'redshift_sortkey': ('col1', 'col2')}
Copy link
Member

Choose a reason for hiding this comment

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

Is there any chance of this being a breaking change?

Copy link
Member

Choose a reason for hiding this comment

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

Might be worth documenting the fact you can no longer include two columns in one string in the sortkey

Copy link
Member

Choose a reason for hiding this comment

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

I'd agree that this is a breaking change that should be included in the CHANGES.rst

@jklukas
Copy link
Member

jklukas commented Jan 25, 2016

@graingert - Do you want to give a final +1 before we merge?

I just remembered that we should probably have a note in the changelog about this as well. We should probably have a CONTRIBUTING.md to write a checklist for PRs.

@graingert
Copy link
Member

👍

@graingert
Copy link
Member

@solackerman can you update the CHANGELOG because this is a breaking change.

@solackerman
Copy link
Contributor Author

Done.

@jklukas
Copy link
Member

jklukas commented Apr 20, 2016

+1

@graingert
Copy link
Member

@solackerman can you squash these together into one, please.

@solackerman
Copy link
Contributor Author

done.

@graingert graingert merged commit 3fa5f3d into sqlalchemy-redshift:master Apr 20, 2016
@solackerman
Copy link
Contributor Author

will there be a new release coming soon?

@graingert
Copy link
Member

@solackerman 0.5.0 is out now

@solackerman
Copy link
Contributor Author

Amazing! Thanks :)

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

3 participants