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

Replace poor man's relation name quoting with implementation from sqlalchemy-cratedb #38

Merged
merged 1 commit into from
Aug 31, 2024

Conversation

amotl
Copy link
Member

@amotl amotl commented Aug 29, 2024

About

When needed, use quote_relation_name from sqlalchemy-cratedb package. Otherwise, don't quote at all, and push this responsibility to the caller. I think it is better doing it this way, than doing it wrong.

Instead of doing it insufficiently, let's pay the price on pulling in the sqlalchemy-cratedb package as a dependency, to be able to use the canonical implementation quote_relation_name without further ado.

@amotl amotl requested review from seut and surister August 29, 2024 21:44
@amotl amotl marked this pull request as ready for review August 29, 2024 21:44
try:
from sqlalchemy_cratedb.support import quote_relation_name
except ImportError: # pragma: no cover
quote_relation_name = None
Copy link
Member

Choose a reason for hiding this comment

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

Is this a good pattern as without quoting it can lead to unexpected behaviour (wrong names used, maybe even deleting wrong data/tables)?

Copy link
Member Author

@amotl amotl Aug 30, 2024

Choose a reason for hiding this comment

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

Initially, I didn't want to make sqlalchemy-cratedb, where the support function is now homed, a strict dependency of commons-codec, being originally conceived as a library with minimal dependencies. It is probably a wrong decision, and I will just add it.

We can't vendorize it in a standalone fashion, because it needs the dialect. In this spirit, decoupling them is probably just not viable, and, as said, just adding it, because the focus is about CrateDB anyway, is probably the right decision. Do you agree?

Copy link
Member

Choose a reason for hiding this comment

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

Yes I think we must depend on it, unexpected behaviour is worse than this dependency ;)

Copy link
Member Author

Choose a reason for hiding this comment

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

Let's do it. Thanks!

Copy link
Member Author

Choose a reason for hiding this comment

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

I've amended the patch according to you suggestions, it is much more straight-forward now. Thank you!

if name and '"' not in name:
name = f'"{name}"'
return name
identifier = f"{self.schema}.{self.table}"
Copy link
Member

Choose a reason for hiding this comment

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

As schema and table are already separated here, quoting them dedicated would be much safer.

Copy link
Member Author

Choose a reason for hiding this comment

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

Our quote_relation_name function currently only accepts a single identifier. Maybe add another one, which accepts the fragments separately?

Copy link
Member

Choose a reason for hiding this comment

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

Normally, a quoting function is called per identifier, so for both, schema and name separately. With quote_relation_name we already moved into the direction of supporting a full relation name FQN, that's why we named it like that if I remember correctly. We did this afaik as at some usages, schema and name weren't available separately. Yeah maybe leave it like that, just keep in mind that quoting single identifiers is less error prone than quoting an already FQN relation name.

Copy link
Member Author

@amotl amotl Aug 30, 2024

Choose a reason for hiding this comment

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

Yeah, I had it in mind. In this case, I was just trying to assemble the bricks we have to solve the problem at hand. Every other suggestion is super valuable to provide a rock solid reference implementation to be re-usable for different kinds of applications and use cases.

If you think it is acceptable for now, I will leave it for this iteration, and then, on another one, improve that at crate/sqlalchemy-cratedb#155 going forward. Thanks!

@@ -11,28 +12,25 @@
else:
from backports.strenum import StrEnum # pragma: no cover

try:
from sqlalchemy_cratedb.support import quote_relation_name
Copy link
Contributor

@surister surister Aug 30, 2024

Choose a reason for hiding this comment

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

Under what circumstances will this import cause an ImportError? if that happens, TableAdress.fqn() will cause a runtime error TypeError: 'NoneType' object is not callable wouldn't it?

Copy link
Member Author

@amotl amotl Aug 30, 2024

Choose a reason for hiding this comment

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

Yeah, unfortunate code stacking here, see #38 (comment). Let's get rid of code smells completely on behalf of another iteration. Thanks!

Copy link
Member Author

Choose a reason for hiding this comment

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

I've amended the patch according to you suggestions, it is much more straight-forward now. Thank you!

@amotl amotl changed the title Remove poor man's relation name quoting Replace poor man's relation name quoting with implementation from sqlalchemy-cratedb Aug 30, 2024
@amotl amotl requested review from seut and surister August 30, 2024 12:23
Copy link
Member

@seut seut left a comment

Choose a reason for hiding this comment

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

👍 thx!

@amotl amotl merged commit 664a5f7 into main Aug 31, 2024
15 checks passed
@amotl amotl deleted the better-tablename-quoting branch August 31, 2024 17:19
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