Skip to content

Compiler: Add CrateIdentifierPreparer for properly quoting reserved words - #21

Merged
amotl merged 1 commit into
mainfrom
amo/identifier-preparer
Jun 24, 2024
Merged

Compiler: Add CrateIdentifierPreparer for properly quoting reserved words#21
amotl merged 1 commit into
mainfrom
amo/identifier-preparer

Conversation

@amotl

@amotl amotl commented Jan 15, 2024

Copy link
Copy Markdown
Contributor

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 thread src/sqlalchemy_cratedb/compiler.py Outdated
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
Copy Markdown
Contributor 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
Copy Markdown
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
Copy Markdown
Contributor 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.

@amotl amotl Jun 5, 2024

Copy link
Copy Markdown
Contributor Author

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?

Comment thread src/sqlalchemy_cratedb/compiler.py Outdated
@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 matriv, seut and surister June 22, 2024 21:43
@amotl
amotl marked this pull request as ready for review June 22, 2024 21:44
Comment thread src/sqlalchemy_cratedb/compiler.py Outdated

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
Copy Markdown
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
Copy Markdown
Contributor 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
Copy Markdown
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
Copy Markdown
Contributor 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
Comment thread src/sqlalchemy_cratedb/compiler.py

@matriv matriv left a comment

Copy link
Copy Markdown
Contributor

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
@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