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

Mongodb regex query not working with IN operation #41840

Open
minhaz1217 opened this issue Jul 11, 2024 · 11 comments
Open

Mongodb regex query not working with IN operation #41840

minhaz1217 opened this issue Jul 11, 2024 · 11 comments
Labels
area/mongodb env/windows Impacts Windows machines kind/bug Something isn't working

Comments

@minhaz1217
Copy link

Describe the bug

When I try to query the db using an IN operation with panache query it doesn't work. It doesn't return results.

Expected behavior

The IN query should work

Actual behavior

The IN query doesn't generate a response.

How to Reproduce?

Steps to reproduce

  1. Query the db using find("emails IN ?1", "/something/").list() in any field it doesn't work

Output of uname -a or ver

Microsoft Windows [Version 10.0.19045.4529]

Output of java -version

java version "21.0.2" 2024-01-16 LTS Java(TM) SE Runtime Environment (build 21.0.2+13-LTS-58) Java HotSpot(TM) 64-Bit Server VM (build 21.0.2+13-LTS-58, mixed mode, sharing)

Quarkus version or git rev

3.12.1

Build tool (ie. output of mvnw --version or gradlew --version)

Apache Maven 3.9.7 (8b094c9513efc1b9ce2d952b3b9c8eaedaf8cbf0) Maven home: C:\Users\HA HA.m2\wrapper\dists\apache-maven-3.9.7-bin\33482774\apache-maven-3.9.7 Java version: 21.0.2, vendor: Oracle Corporation, runtime: C:\Program Files\Java\jdk-21 Default locale: en_IN, platform encoding: UTF-8 OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"

Additional information

I've created a sample project to regenerate the issue.

It can be found here

https://github.com/minhaz1217/java-quarkus/tree/master/mongodb-regex-in-list-query-panache

@minhaz1217 minhaz1217 added the kind/bug Something isn't working label Jul 11, 2024
@quarkus-bot quarkus-bot bot added area/mongodb env/windows Impacts Windows machines labels Jul 11, 2024
@quarkus-bot
Copy link

quarkus-bot bot commented Jul 11, 2024

/cc @loicmathieu (mongodb)

@minhaz1217
Copy link
Author

The issue is happening because panache is wrapping my /@email.com/ in quote(''). So mongodb is treating it as string search even though the search is regex search indicated by the initial /

This is the query panache is generating
{'emails':{'$in':['/@email.com/']}}
This is how I queried
find("emails in ?1", "/" + email + "/").list()
The expected query is this
{'emails':{'$in':[/@email.com/]}}

@minhaz1217
Copy link
Author

I've also debugged the issue a little bit, it seems that this line is putting the quotation around the value even though it shouldn't

String var10000 = value.toString().replace("\\", "\\\\");
return "'" + var10000.replace("'", "\\'") + "'";

The line can be found in the CommonQueryBinder.class in the io.quarkus.mongodb.panache.common.binder package.

@minhaz1217
Copy link
Author

Should I create a PR that checks if the string var10000 is prefixed by / and if it is then not wrap it in quote?

@loicmathieu
Copy link
Contributor

@minhaz1217 yes please, open a PR

@minhaz1217
Copy link
Author

@loicmathieu I'm on it.

@minhaz1217
Copy link
Author

@loicmathieu Should I consider something like /anything here the first / wasn't closed with similar / so we can say that this is normal string search.

I can do this by matching / from the end and checking that the first / wasn't the only one. That way even if the search is like this
/something/i (case insensitive search) it will be safe.

@gsmet
Copy link
Member

gsmet commented Jul 13, 2024

I don’t think testing if the string starts with / will fly.
What if you actually have a string starting with /?
I think you need either another method or a wrapper to mark that this thing is a regexp.

@minhaz1217
Copy link
Author

We can use the LIKE keyword and if the passed value is of collection or array type then we can just not wrap the value with '

@loicmathieu
Copy link
Contributor

Regex should use LIKE, we may need to support LIKE IN in PanacheQL for that, which may not be that easy.

@minhaz1217
Copy link
Author

I've tried to debug and see if I can modify the normal LIKE keyword and make it work, but it seems the visitLikePredicate in MongoParserVisitor only receives the first item in the list even though the list has more than one keyword.

So modifying LIKE or LIKE IN is out of my league.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/mongodb env/windows Impacts Windows machines kind/bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants