realm-java icon indicating copy to clipboard operation
realm-java copied to clipboard

Add support for multi-field comparisons

Open ghost opened this issue 11 years ago • 25 comments

How to compare two fields in android realm.io ?

ghost avatar Oct 19 '15 04:10 ghost

We currently we don't support multi-field comparisons in the Java API, mostly because it's hard to make a nice API for it. We are thinking about some possible options.

emanuelez avatar Oct 19 '15 07:10 emanuelez

Just adding equalToField would probably be easy, but the list is also a bit bigger which complicates it:

  • notEqualTo
  • Compare integer fields: lessThan, biggerThan, etc.
  • Calculations on integer fields, eg. 2 * field1 > field2
  • does contains/startsWith/endsWith work on multiple fields?

cmelchior avatar Oct 19 '15 07:10 cmelchior

+1

Hello I need to use this solution to make this comparison : updateDate > syncDate (these two dates are in the database). I want to fetch only the updated entities or there is another solution for this?

brozot avatar Nov 26 '15 08:11 brozot

Please consider adding support for this. I've been running equalTo and other operators in a loop for multiple arguments :(

saket avatar Nov 26 '15 10:11 saket

Came here looking for solution to this problem (now missing feature) too. Looks like there is significant demand for it. I am hoping it will be in the form of: .equalTo(string, string), notEqualTo(string, string), etc - to compare compare two properties of the same entity.

eyalfein avatar Jan 26 '16 11:01 eyalfein

No workaround at the moment? I can't select comparing two columns in any way?

papagno avatar Apr 16 '16 09:04 papagno

One workaround is to add a third boolean which is the result of the comparison you wan to make. You would then need to update that whenever the properties you compare changes.

The Core does support this, so anyone with time can make a PR. The first task is to propose a proper API, then it is trivial to implement (I'll be happy to provide pointers to the core API).

bmunkholm avatar Apr 17 '16 11:04 bmunkholm

I would like to have this feature too!

I want to filter my RealmResults in my adapter when the user types in a search text view. I want to filter by all the fields in RealmResults.

Example: firstName -- lastName John -- Johnnes

constraint = "John J" mRealmObjectList.where().contains("firstName", String.valueOf(constraint), Case.INSENSITIVE).or().contains("lastName", String.valueOf(constraint), Case.INSENSITIVE)

This wouldn't receive any record.

My workaround is to get all the fields in a temporal string and do the contains() in there. But i wonder how much it will cost if my table has a lot of rows...

nachogarrone avatar May 05 '16 02:05 nachogarrone

+1 for this

letronje avatar Mar 25 '17 13:03 letronje

+1 for calculations on integer fields, eg. (qty / dailyAvg) < 3

acib708 avatar May 16 '17 17:05 acib708

+1

vostreltsov avatar Jun 23 '17 19:06 vostreltsov

+1

N-Olifer avatar Jun 23 '17 19:06 N-Olifer

+1 on this - it is possible in realm cocoa, seems like it should be here to!

samvanderhyden avatar Jul 14 '17 15:07 samvanderhyden

The most common cases could probably be solved by adding the following methods

equalToField(field1, field2);
greaterThanField(field1, field2);
greaterThanOrEqualToField(field1, field2);
lessThanField(field1, field2);
lessThanOrEqualToField(field1, field2);
notEqualToField(field1, field2);

cmelchior avatar Jul 17 '17 08:07 cmelchior

I'm not sure how common this actually is. A possible workaround for now of course is to store the actual boolean for the fields you are interested in, and update it in the two setters.

This feels like one of those things that would be better solved by exposing the query language that is already processed by partial sync preview api: https://github.com/realm/realm-java/issues/1573#issuecomment-292805550

Zhuinden avatar Oct 15 '17 11:10 Zhuinden

+1

tristangrichard avatar Jan 14 '18 19:01 tristangrichard

+1 Any updates?

abou7mied avatar Jan 25 '18 16:01 abou7mied

this issue has been open for couple years! any update?

w19787 avatar Oct 05 '18 14:10 w19787

I also run into this issue while making an Android app based on an already existing Objective-C codebase.

I find quite a few things in the java bindings that are supported in other platforms and the core but the Java team won't add support with the same API used on other platforms. Quite frustrating when used to the binding of another language. Realm is an awesome tech don't get me wrong but I would like all bindings teams to pull all API's together.

BosAtWork avatar Oct 23 '18 21:10 BosAtWork

Bump this. This is not just implementation issue but a performance issue. Currently users are relagated to do this to simluate the effect (kotlin). f1 and f2 are numeric.

r.where(table).filter { row.f1 < row.f2 }

The above looks simple enough but performance-wise we are doing filtering (with lots of native calls plus allocations) in jvm runtime rather than c++. With lots of rows this becomes a huge issue.

The current accepted work-around is the "additional field computed" comparison but this method is not sustainable in a normal dev env where logic change all the time. Imagine changing schema and/or adding migration every time this logic changes. Yikes.

Qubitium avatar Mar 05 '19 09:03 Qubitium