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

SQLAlchemy DQL: Use CrateDB's native ILIKE operator #564

Merged
merged 2 commits into from
Jul 17, 2023

Conversation

amotl
Copy link
Member

@amotl amotl commented Jul 6, 2023

About

At crate/sqlalchemy-cratedb#98, @hlcianfagna suggested to use CrateDB's native ILIKE operator instead of using SA's generic implementation lower() LIKE lower().

Details

This patch brings in the corresponding visitor methods from the PGDialect to the CrateDialect implementation.

Comment on lines 259 to 265
return "%s ILIKE %s" % (
self.process(binary.left, **kw),
self.process(binary.right, **kw),
) + (
" ESCAPE " + self.render_literal_value(escape, sqltypes.STRINGTYPE)
if escape
else ""
)
Copy link
Member Author

@amotl amotl Jul 6, 2023

Choose a reason for hiding this comment

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

Does CrateDB support ESCAPE in this context? Do you suggest to remove this code, if not?

Copy link
Contributor

Choose a reason for hiding this comment

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

No,

UnsupportedFeatureException[ESCAPE is not supported.]

Maybe we should raise a feature request?

How were % and _ handled previously in the lower() like lower() implementation?

Copy link
Contributor

Choose a reason for hiding this comment

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

Escape character for _ and % in CrateDB is \, so \%' or _`:

cr> create table t(s string);
CREATE OK, 1 row affected  (0.385 sec)
cr> insert into t values('f%oo');
INSERT OK, 1 row affected  (0.062 sec)
cr> refresh table t;
REFRESH OK, 1 row affected  (0.005 sec)
cr> select * from t where s ilike 'f\%O%';
+------+
| s    |
+------+
| f%oo |
+------+
SELECT 1 row in set (0.003 sec)

I don't get what's this ESCAPE here, can you please explain?

Copy link
Member Author

@amotl amotl Jul 7, 2023

Choose a reason for hiding this comment

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

I was hoping that you can explain, and/or follow up with proper guidance. ;]

Copy link
Member Author

@amotl amotl Jul 7, 2023

Choose a reason for hiding this comment

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

Shall we just remove the corresponding code, in order to be able to bring in the patch, and create an issue at crate/crate to track concerns of the ESCAPE operator/keyword? I guess it can also be used on statements/clauses other than this specific spot?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes please, it seems that postgres supports defining a custom escape char seq:
https://www.postgresql.org/docs/15/functions-matching.html
https://stackoverflow.com/questions/196626/escaping-in-a-ilike-query
which is not the case for CrateDB.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thank you. I've just removed corresponding code. Please let me know if there should be an issue on the crate/crate tracker, in order to track any missing compatibility features.

@amotl amotl requested review from matriv and hlcianfagna July 6, 2023 23:35
@amotl amotl marked this pull request as ready for review July 6, 2023 23:35
@amotl amotl requested a review from seut July 7, 2023 07:14
Copy link
Contributor

@hlcianfagna hlcianfagna left a comment

Choose a reason for hiding this comment

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

See #564 (comment) about the handling of % and _ in the matching string

@amotl amotl force-pushed the amo/sqlalchemy-native-ilike branch 2 times, most recently from da77c3a to 689493b Compare July 7, 2023 12:31
@amotl amotl requested a review from hlcianfagna July 7, 2023 14:01
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.

Thx!

Copy link
Contributor

@hlcianfagna hlcianfagna left a comment

Choose a reason for hiding this comment

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

I confirmed that ilike accepts an escape parameter and if that is used today it renders as the SQL ESCAPE clause which results on an UnsupportedFeatureException being returned from CrateDB, if the change goes in as is there would be no error but we would be risking incorrect results, maybe we should raise some exception if the escape parameter is used?

@matriv
Copy link
Contributor

matriv commented Jul 7, 2023

I confirmed that ilike accepts an escape parameter and if that is used today it renders as the SQL ESCAPE clause which results on an UnsupportedFeatureException being returned from CrateDB, if the change goes in as is there would be no error but we would be risking incorrect results, maybe we should raise some exception if the escape parameter is used?

Very true, thx! I agree to throw an exception if escape is used.

@amotl
Copy link
Member Author

amotl commented Jul 7, 2023

@hlcianfagna: Well spotted, thank you very much. I agree to catch the escape argument, but not ignore it, and raise a synthetic exception instead, mimicking the one which is currently raised by CrateDB when actually sending the ESCAPE keyword.

@amotl amotl force-pushed the amo/sqlalchemy-native-ilike branch from 689493b to 279938a Compare July 7, 2023 18:42
@amotl amotl requested review from hlcianfagna and matriv July 7, 2023 18:43
@amotl amotl force-pushed the amo/sqlalchemy-native-ilike branch 2 times, most recently from 1e361d5 to 73b5ab9 Compare July 7, 2023 20:09
@amotl
Copy link
Member Author

amotl commented Jul 7, 2023

Can we find out since which version CrateDB supports the ILIKE operator, and if it would be advisable that we need to fall back to the generic SQLAlchemy implementation, when operating on a lower version of CrateDB, like, not breaking support for CrateDB 3.x?

@hlcianfagna
Copy link
Contributor

Can we find out since which version CrateDB supports the ILIKE operator, and if it would be advisable that we need to fall back to the generic SQLAlchemy implementation, when operating on a lower version of CrateDB, like, not breaking support for CrateDB 3.x?

Support for ILIKE was added in 4.1.0

Instead of using SA's generic implementation `lower() LIKE lower()`, use
CrateDB's native one.
@amotl amotl force-pushed the amo/sqlalchemy-native-ilike branch from 73b5ab9 to 596f5e0 Compare July 17, 2023 16:47
@amotl
Copy link
Member Author

amotl commented Jul 17, 2023

Support for ILIKE was added in 4.1.0.

Thank you @hlcianfagna. I've just added b93007d, in order to implement corresponding code branching based on the CrateDB version in use.

May I ask for another review from you and @matriv?

@amotl amotl requested review from matriv and hlcianfagna July 17, 2023 16:50
@amotl amotl force-pushed the amo/sqlalchemy-native-ilike branch from 596f5e0 to b93007d Compare July 17, 2023 16:57
@amotl amotl mentioned this pull request Jul 17, 2023
@amotl amotl merged commit 17e6ebb into master Jul 17, 2023
29 checks passed
@amotl amotl deleted the amo/sqlalchemy-native-ilike branch July 17, 2023 17:13
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