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

Compiler: Add CrateIdentifierPreparer for properly quoting reserved words #21

Merged
merged 1 commit into from
Jun 24, 2024

Conversation

amotl
Copy link
Member

@amotl amotl commented Jan 15, 2024

About

By using this component of the SQLAlchemy dialect compiler, it can define CrateDB's reserved words to be quoted properly when building SQL statements.

References

In this case, OBJECT needs to be quoted when used as column name.

Backlog

  • Changelog item
  • Software tests

Comment on lines 323 to 328
class CrateIdentifierPreparer(sa.sql.compiler.IdentifierPreparer):
"""
Define CrateDB's reserved words to be quoted properly.
"""
# TODO: There are certainly more to add here than just `object`?
reserved_words = set(list(POSTGRESQL_RESERVED_WORDS) + ["object"])
Copy link
Member Author

Choose a reason for hiding this comment

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

This list clearly needs to be expanded. Suggestions are welcome. Probably, derive the list directly from CrateDB / crash / Admin UI? Can someone come up with suggestions for retrieving / generating an exhaustive list of reserved keywords for CrateDB?

/cc @seut, @matriv, @surister

Copy link
Contributor

Choose a reason for hiding this comment

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

In the past I extracted many into https://github.com/surister/cratedb-admin-alt/blob/master/src/store/crate_api/crate_lang.js but it is incomplete, perhaps we could maintain a crate-owned Gist with all of this properly done and complete. It could be javascript or python, dicts and lists are generally very copy-paste friendly.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, it would be sweet to maintain such a list on behalf of a Python generator, which derives them directly from the ANTLR grammar or so.

Copy link
Member Author

@amotl amotl Jun 5, 2024

Choose a reason for hiding this comment

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

Hi again. Do you think cratedb-sqlparse should be used here, or would it be too heavy? Can you think of any alternatives?

@amotl amotl force-pushed the amo/identifier-preparer branch 2 times, most recently from 33e74cf to 27f3e35 Compare June 22, 2024 21:42
@amotl amotl requested review from seut, matriv and surister June 22, 2024 21:43
@amotl amotl marked this pull request as ready for review June 22, 2024 21:44
@@ -25,6 +25,7 @@

import sqlalchemy as sa
from sqlalchemy.dialects.postgresql.base import PGCompiler
from sqlalchemy.dialects.postgresql.base import RESERVED_WORDS as POSTGRESQL_RESERVED_WORDS
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we have some deviations from the postgres reserved keywords, I think it's better if we used the python sqlparser to get the list for CrateDB programmatically.

Copy link
Member Author

Choose a reason for hiding this comment

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

We can do it on a later iteration if we agree. I am not sure if it would be too heavy, see #21 (comment).

Copy link
Contributor

Choose a reason for hiding this comment

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

yep, we can do in next step.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks! Maybe you want to submit a corresponding patch, @surister?

@amotl amotl requested a review from matriv June 24, 2024 08:10
@amotl amotl force-pushed the amo/identifier-preparer branch from 27f3e35 to 7970219 Compare June 24, 2024 10:10
Copy link
Contributor

@matriv matriv left a comment

Choose a reason for hiding this comment

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

LGTM, thx!

By using this component of the SQLAlchemy dialect compiler, it can
define CrateDB's reserved words to be quoted properly when building
SQL statements.

This allows to quote reserved words like `index` or `object` properly,
for example when used as column names.
@amotl amotl force-pushed the amo/identifier-preparer branch from 7970219 to f4b4ab3 Compare June 24, 2024 10:28
@amotl amotl merged commit 9a95be0 into main Jun 24, 2024
27 checks passed
@amotl amotl deleted the amo/identifier-preparer branch June 24, 2024 14:10
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