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 in() returns an empty RealmResults #6522

Closed
mick1418 opened this issue May 20, 2019 · 17 comments
Closed

Query with in() returns an empty RealmResults #6522

mick1418 opened this issue May 20, 2019 · 17 comments
Assignees

Comments

@mick1418
Copy link

Goal

I want to query a table and retrieve objects by their IDS

Actual Results

The RealmResults returns an empty list, even if the objects are in the table (checked with Realm Studio).

Steps & Code to Reproduce

Requesting the table with multiples IDs always return an empty list, but querying the IDs separately returns a list with the object.

realm.where("mytable").equalTo("uid", "firstUID").findAll()-> returns a list with the object
realm.where("mytable").equalTo("uid", "secondUID").findAll()-> returns a list with the object

realm.where("mytable").beginGroup().equalTo("uid", "firstUID").or().equalTo("uid", "secondUID").endGroup().findAll()-> returns an empty list

realm.where("mytable").in("uid", new String[]{"firstUID", "secondUID"}).findAll()-> returns an empty list

Maybe it has something to do with the following PR: (Realm Core PR #3250).
Reverting to 5.9.1 solved the issue.

Version of Realm and tooling

Realm version(s): ? 5.11.0

Realm Sync feature enabled: No

Android Studio version: ? 3.4 stable

Android Build Tools version: ? 28.0.3

Gradle version: ? 5.1.1

Which Android version and device(s): ? Android API 28, Emulator

@Zhuinden
Copy link
Contributor

Are we talking about realm/realm-core#3250?

realm.where("mytable").beginGroup().equalTo("uid", "firstUID").or().equalTo("uid", "secondUID").endGroup().findAll()-> returns an empty list

This is supposed to work. 😞

@Zhuinden
Copy link
Contributor

Although it says:

This change does not try to optimise indexed columns which should be running O(log(N)*C). The benchmark with indexes turned on runs in 3.5 seconds.

If the uid column is Primary Key, then it is Indexed, and therefore should have been unaffected by this change.


I was wondering if realm/realm-core#3271 is related, but it is for int columns...

@mick1418
Copy link
Author

I should have mentioned my "uid" field is of type String, and not the Primary key in this given table

@Zhuinden
Copy link
Contributor

It is also not marked with @Index, correct?

@mick1418
Copy link
Author

Correct

@Zhuinden
Copy link
Contributor

@cmelchior

@bmunkholm
Copy link
Contributor

@mick1418 Could you please make a minimal self-contained repro case? Or a PR with a test-case that shows this?
(We do have extensive query tests here: https://github.com/realm/realm-java/blob/master/realm/realm-library/src/androidTest/java/io/realm/RealmQueryTests.java)

@BWMuller
Copy link

Getting this issue as well. In my case the field is marked with @Index but I am only getting 1 or no data back instead of around 20 items. Realm version 5.10.0 works fine still.

Haven't been able to determine a use-case to make available.
Can state though that the query has a equalsTo and a in which is likely the cause

@cmelchior
Copy link
Contributor

I cannot reproduce this with a unit test:

    @Test
    public void in_primaryKey() {
        realm.executeTransaction(r -> {
            for (int i = 0; i < 5; i++) {
                realm.createObject(AllJavaTypes.class, i);
            }
        });

        RealmResults<AllJavaTypes> results = realm.where(AllJavaTypes.class).in(AllJavaTypes.FIELD_ID, new Long[]{2L, 3L, 4L}).findAll();
        assertEquals(3, results.size());

        realm.executeTransaction(r -> {
            for (int i = 0; i < 5; i++) {
                realm.createObject(PrimaryKeyAsString.class, Integer.toString(i));
            }
        });

        RealmResults<PrimaryKeyAsString> stringResults = realm.where(PrimaryKeyAsString.class).in(PrimaryKeyAsString.FIELD_PRIMARY_KEY, new String[]{"2", "3", "4"}).findAll();
        assertEquals(3, stringResults.size());
    }

For neither integer nor String primary key

@Quarx2k
Copy link

Quarx2k commented Jun 21, 2019

I have near same problem. Where query return zero values, but simply loop show that values are here.
This is not 100% reproducible. Happens randomly for users (until they not clear app data). But probably happens after calling: `report.reportValues.deleteAllFromRealm();' on another 'report' entity which not linked with problematic entity. ReportValues also unique for each 'report' entity.

Realm version(s): 5.11.0, 5.12.0
Downgrade to 5.10.0 solved problem.

RealmResults<ReportValue> dbrvs = report.getReportValues().where().equalTo("category", TaskCategory.BEFORE_REPORT.ordinal()).findAll();
Log.e(LOG_TAG, "ReportValues: " + dbrvs.size());
for (ReportValue rv : report.getReportValues()) {
    if (rv.getCategory() == TaskCategory.BEFORE_REPORT.ordinal()) {
        Log.e(LOG_TAG,  rv.toString());
    }
 }

Result:

E/ReportHubFragment: ReportValues: 0   **(but should be 1)**
E/ReportHubFragment: ReportValue{attachement=null, id=-1711229038, subTaskId=null, name='null', description='null', priority='null', clientId='null', value='null', oldValue='null', hotline='null', oldHotline='null', posSpecific=true, requiresPhoto=false, completed=true, max=0, min=0, type=4, category=1, sku=null}
public class Report extends RealmObject {
    @PrimaryKey
    private String guid;
....
    private RealmList<ReportValue> reportValues;
}
public class ReportValue extends RealmObject {
    @Index
    private Long id;
........
    @Index
    private int category;

@Quarx2k
Copy link

Quarx2k commented Jun 21, 2019

@cmelchior
I created test project which can reproduce bug https://github.com/Quarx2k/Realm-issue-6522
5.11/5.12 Realm

2019-06-21 20:27:50.686 24924-24924/? E/MainActivity: Report 97 Values Size: 5 
2019-06-21 20:27:50.687 24924-24924/? E/MainActivity: Report 97 Values Query Size: 1 
2019-06-21 20:27:50.687 24924-24924/? E/MainActivity: The bug ;(
2019-06-21 20:27:50.687 24924-24924/? E/MainActivity: Report 98 Values Size: 5 
2019-06-21 20:27:50.687 24924-24924/? E/MainActivity: Report 98 Values Query Size: 1 
2019-06-21 20:27:50.687 24924-24924/? E/MainActivity: The bug ;(
2019-06-21 20:27:50.688 24924-24924/? E/MainActivity: Report 99 Values Size: 5 
2019-06-21 20:27:50.688 24924-24924/? E/MainActivity: Report 99 Values Query Size: 1 
2019-06-21 20:27:50.688 24924-24924/? E/MainActivity: The bug ;(
2019-06-21 20:27:50.689 24924-24924/? E/MainActivity: Report 100 Values Size: 5 
2019-06-21 20:27:50.689 24924-24924/? E/MainActivity: Report 100 Values Query Size: 5 

5.10 Realm

2019-06-21 20:31:34.112 25324-25324/? E/MainActivity: Report 97 Values Size: 5 
2019-06-21 20:31:34.112 25324-25324/? E/MainActivity: Report 97 Values Query Size: 5 
2019-06-21 20:31:34.113 25324-25324/? E/MainActivity: Report 98 Values Size: 5 
2019-06-21 20:31:34.113 25324-25324/? E/MainActivity: Report 98 Values Query Size: 5 
2019-06-21 20:31:34.113 25324-25324/? E/MainActivity: Report 99 Values Size: 5 
2019-06-21 20:31:34.113 25324-25324/? E/MainActivity: Report 99 Values Query Size: 5 
2019-06-21 20:31:34.114 25324-25324/? E/MainActivity: Report 100 Values Size: 5 
2019-06-21 20:31:34.114 25324-25324/? E/MainActivity: Report 100 Values Query Size: 5 

Hope it helps :)

@cmelchior
Copy link
Contributor

@Quarx2k Thank you very much for this 👏 . I can reproduce the behavior and is looking into it.

@cmelchior
Copy link
Contributor

It seems to be a bug with Indexes. The error goes away if you remove Indexes.

@Zhuinden
Copy link
Contributor

(you can't remove the index of a primary key field though)

@tgoyne
Copy link
Member

tgoyne commented Jul 9, 2019

realm/realm-core#3320 should fix this.

@Zhuinden
Copy link
Contributor

Realm Core 5.23.1 release should have the fix, now Realm-Java needs to update the Core version in next release 🤔

@cmelchior
Copy link
Contributor

Fixed in 5.13.1

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 15, 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

8 participants