Realm-java: RealmQuery between problems with double datatype

Created on 2 May 2018  路  9Comments  路  Source: realm/realm-java

I want to get data from Realm Cloud with between filter 2 numbers. Previously I used long type for date column and It will return value if I query this:

long start = calendarStart.getTimeInMillis() / 1000;
long end = calendarEnd.getTimeInMillis() / 1000;

result =  getRealmSyncInstance()
                .where(MyClass.class)
//                .greaterThanOrEqualTo("date", start)
//                .lessThanOrEqualTo("date", end)
                .between("date", start, end)
                .sort("date", Sort.ASCENDING)
                .findAllAsync();

Then I clear Realm Cloud and change local MyClass date, start, end from long to double. Then no result received.
I also use greaterThanOrEqualTo and lessThanOrEqualTo but no luck.

Seem like it only happens on realm cloud.

Version of Realm and tooling

Realm version(s): 5.0.0

Realm sync feature enabled: yes

Android Studio version: 3.1

Which Android version and device: Oreo

O-Community T-Bug

Most helpful comment

Hi @kainjinez I was able to reproduce your issue, it looks like the Sync query parser failed to parse the mathematical notation .lessThanOrEqualTo("value", 10_000_000d) which result in 1e+07 (full query value >= 0 and value <= 1e+07 SORT(timestamp DESC)). I'm investigating with the Core team to provide a fix

All 9 comments

Why are you replacing date with double?

I've seen Date many times as long, but never as double.

Sounds like bad idea.

Just to be clear. You are querying the dates that are now a double, but the result is empty? Have you verified in Realm Studio that the data is on the server? And that they have been synchronized to the device. You can use SyncManager.getSession(config).downloadAllServerChanges() for that.

@Zhuinden I often work with timeInMilliseconds. But when I tried to migrate with iOS, they use double to store timestamp, then I have to change long to double.
@cmelchior Yes. I tried some tests. I changed value from server to 1000 (double). Then I changed start and end from current time to start = 0; end = 2000; but when i try to query it again, i get realmresult size = 0 in RealmChangeListener. And I didn't use session yet.

Internally, Realm uses long to represent dates AFAIK, specifically UTC timezone longs.

Maybe iOS team should use dedicated Date type, or long Millis too?

@Zhuinden My date is just a fieldname. Recently, I modify SyncIntro project from Todo sample. Please see this code below:

public class Item extends RealmObject {
    @PrimaryKey
    @Required
    private String itemId;
    @Required
    private String body;
    @Required
    private Boolean isDone;
    @Required
    private Date timestamp;
    private double value;

ItemsActivity

private RealmResults<Item> setUpRealm() {
        SyncConfiguration configuration = new SyncConfiguration.Builder(
                SyncUser.current(),
                REALM_BASE_URL + "/" + SyncUser.current().getIdentity()).partialRealm()
                .build();
        realm = Realm.getInstance(configuration);

        return realm
                .where(Item.class)
                .greaterThanOrEqualTo("value", 0d)
                .lessThanOrEqualTo("value", 10_000_000d)
                .sort("timestamp", Sort.DESCENDING)
                .findAllAsync();
    }

Then I change value on Realm Studio https://i.imgur.com/U7YRgVD.jpg:
Below 1000d => shown
Over 1001d => no data
I just don't know what happened with this double or i was wrong some where?

Any updates?

Hi @kainjinez I was able to reproduce your issue, it looks like the Sync query parser failed to parse the mathematical notation .lessThanOrEqualTo("value", 10_000_000d) which result in 1e+07 (full query value >= 0 and value <= 1e+07 SORT(timestamp DESC)). I'm investigating with the Core team to provide a fix

closing please follow progress in realm/realm-core#3076

Was this page helpful?
0 / 5 - 0 ratings