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

Query with parameterized expression only considers last element in array #6222

Closed
yakusho opened this issue Oct 31, 2023 · 3 comments
Closed

Comments

@yakusho
Copy link

yakusho commented Oct 31, 2023

How frequently does the bug occur?

Always

Description

I'm trying to use parameterized expressions in React Native, and I noticed that whenever I pass an array, it only considers the last element in the array.

The docs for the Realm Query Language shows this example:

const ids = [
  new BSON.ObjectId("631a072f75120729dc9223d9"),
  new BSON.ObjectId("631a0737c98f89f5b81cd24d"),
  new BSON.ObjectId("631a073c833a34ade21db2b2"),
];
const parameterizedQuery = realm.objects("Item").filtered("id IN $0", ids);

My use case is slightly different and would be better expressed like this:

const targetTypes = ["House", "Flat"];
const parameterizedQuery = useQuery(Target, res => res.filtered("targetType IN $0", targetTypes));

However, it's only ever returning records where targetType is "Flat" in that example, if you put "House" as the last element, it starts showing only those with targetType "House", if I use a random value that has 0 results ("Test" for example), it crashes the app but I'm unable to verify if that happens because of the query or just my application

Stacktrace & log output

No response

Can you reproduce the bug?

Always

Reproduction Steps

No response

Version

11.10.2

What services are you using?

Local Database only

Are you using encryption?

No

Platform OS and version(s)

iOS 16.4

Build environment

"@realm/react": "^0.6.0",
"realm": "^11.10.2",
"react-native": "0.72.6",

Cocoapods version

~> 1.12

@kneth
Copy link
Contributor

kneth commented Nov 2, 2023

In v12.0.0-rc.0 we fixed a bug related to IN. The changelog entry is Fixed an error where performing a query like "{1, 2, 3, ...} IN list" where the array is longer than 8 and all elements are smaller than some values in list, the program would crash.. I don't know if it applies to your case.

You are using @realm/react, and does you only observe it with useQuery() or do you see a similar behavior when querying directly?

We have tests covering the IN operator. In order for us to reproduce it, we would like to know how your case differs from our tests.

@sync-by-unito sync-by-unito bot added the Waiting-For-Reporter Waiting for more information from the reporter before we can proceed label Nov 2, 2023
@yakusho
Copy link
Author

yakusho commented Nov 3, 2023

In v12.0.0-rc.0 we fixed a bug related to IN. The changelog entry is Fixed an error where performing a query like "{1, 2, 3, ...} IN list" where the array is longer than 8 and all elements are smaller than some values in list, the program would crash.. I don't know if it applies to your case.

You are using @realm/react, and does you only observe it with useQuery() or do you see a similar behavior when querying directly?

We have tests covering the IN operator. In order for us to reproduce it, we would like to know how your case differs from our tests.

I'll have to verify if upgrading to v12 will fix the bug where it crashes.

As for the useQuery, I could reproduce it both with useQuery or querying directly using objects(type).filtered

I created a project to reproduce the problem I'm having here, I tested it using an array of ObjectIds and in that case, the IN operator does work, but if it's a string, then it doesn't work anymore, which is weird considering that I believe this test should probably fail in that scenario:

const many = persons.filtered("name in $0", ["Alice", "Dennis", "Bob"]);
expect(many.length).equal(2);

@github-actions github-actions bot added Needs-Attention Reporter has responded. Review comment. and removed Waiting-For-Reporter Waiting for more information from the reporter before we can proceed labels Nov 3, 2023
@nirinchev
Copy link
Member

This is fixed in Realm 12.

@sync-by-unito sync-by-unito bot removed the Needs-Attention Reporter has responded. Review comment. label Mar 20, 2024
@sync-by-unito sync-by-unito bot changed the title Query with parameterized expression only considers last element in array Query with parameterized expression only considers last element in array Mar 20, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 19, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants