querydsl / querydsl

Unified Queries for Java
https://querydsl.com
Apache License 2.0
4.74k stars 873 forks source link

Performing coalesce on NumberPath in from or where clause #1376

Closed robertandrewbain closed 3 years ago

robertandrewbain commented 9 years ago

ComparableExpressionBase's public final Coalesce<T> coalesce(Expression<?>...exprs) returns a Coalesce<T>. Coalesce does not have SimpleExpression's public BooleanExpression eq(T right) method, so I assume the idea is to use Coalesce's public NumberExpression<?> asNumber(). However if you try to E.G: (I know the query is nonsense)

query()
.from(someRelationalPathBase)
.innerJoin(someOtherRelationalPathBase)
.on(someRelationalPathBase.id.coalesce(someOtherRelationalPathBase.id).asNumber().eq(someRelationalPathBase.id));

you receive the compiler error The method eq(capture#2-of ?) in the type SimpleExpression<capture#2-of ?> is not applicable for the arguments (NumberPath<Long>). I'm working around this with the following code:

 @SuppressWarnings("unchecked")
 public static <T extends Number & Comparable<T>> NumberOperation<T> coalesceNumbers(NumberPath<T> arg0, NumberPath<T> arg1) {
   return ((NumberOperation<T>) arg0.coalesce(arg1).asNumber());
 }
cenobyte321 commented 8 years ago

Is there a workaround for this particular issue?

jwgmeligmeyling commented 3 years ago

The current workaround is a cast: (NumberExpression<Long>) someRelationalPathBase.id.coalesce(someOtherRelationalPathBase.id).asNumber(). Alternatively: Expressions.asNumber(someRelationalPathBase.id.coalesce(someOtherRelationalPathBase.id)).