Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,12 @@ private static Expression<?> coalesce(List<FunctionArgument> args, Root<?> root,
/**
* NULLIF function implementation.
*
* <p>JPA CriteriaBuilder.nullif(Expression, Y) only accepts a literal as second argument.
* When the second argument is a PropertyExpression (field reference), we cannot use the
* <p>JPA CriteriaBuilder.nullif(Expression, Y) only accepts a literal as second argument,
* where Y is the raw literal value: the Querity {@link Literal} wrapper must be unwrapped
* before passing it, otherwise the persistence provider tries to convert the wrapper itself
* to the expression type and fails.
*
* <p>When the second argument is a PropertyExpression (field reference), we cannot use the
* standard nullif() method.
*
* <p>Workaround: We emulate NULLIF(a, b) using CASE WHEN:
Expand All @@ -260,17 +264,16 @@ private static Expression<?> coalesce(List<FunctionArgument> args, Root<?> root,
*/
private static Expression<?> nullif(List<FunctionArgument> args, Root<?> root, CriteriaBuilder cb, Metamodel metamodel) {
Expression<Object> first = castExpression(toExpressionOrLiteral(args.get(0), root, cb, metamodel));
Object second = args.get(1);

if (second instanceof PropertyExpression pe) {
// PropertyExpression requires CASE WHEN workaround (see Javadoc above)
Expression<Object> secondExpr = castExpression(toExpressionOrLiteral(pe, root, cb, metamodel));
return cb.<Object>selectCase()
.when(cb.equal(first, secondExpr), cb.nullLiteral(Object.class))
.otherwise(first);
} else {
return cb.nullif(first, second);
FunctionArgument second = args.get(1);

if (second instanceof Literal lit) {
return cb.nullif(first, lit.getValue());
}
// PropertyExpression requires CASE WHEN workaround (see Javadoc above)
Expression<Object> secondExpr = castExpression(toExpressionOrLiteral(second, root, cb, metamodel));
return cb.<Object>selectCase()
.when(cb.equal(first, secondExpr), cb.nullLiteral(Object.class))
.otherwise(first);
}

// --- Aggregate functions ---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;
Expand Down Expand Up @@ -392,12 +393,15 @@ void givenCoalesceFunction_whenToExpression_thenReturnCoalesceExpression() {

@Test
@SuppressWarnings("unchecked")
void givenNullifFunctionWithLiteral_whenToExpression_thenReturnNullifExpression() {
void givenNullifFunctionWithLiteral_whenToExpression_thenReturnNullifExpressionWithUnwrappedLiteralValue() {
Expression<Object> nullifExpr = mock(Expression.class);
when(cb.nullif(any(Expression.class), any(Object.class))).thenReturn(nullifExpr);
FunctionCall fc = FunctionCall.of(Function.NULLIF, PropertyReference.of("status"), Literal.of("INACTIVE"));
Expression<?> result = JpaFunctionMapper.toExpression(fc, root, cb, metamodel);
assertThat(result).isEqualTo(nullifExpr);
// the literal wrapper must be unwrapped, otherwise the persistence provider
// cannot convert it to the expression type
verify(cb).nullif(any(Expression.class), eq("INACTIVE"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1485,6 +1485,25 @@ void givenSelectWithMultipleFunctions_whenFindAllProjected_thenReturnProjectedRe
assertThat(map).containsKey("lastNameLength");
});
}

@Test
void givenSelectWithNullifFunctionAndLiteral_whenFindAllProjected_thenReturnProjectedResults() {
if (!supportsFunctionExpressionsInProjections()) return;
// Select NULLIF(lastName, entity1's lastName) as nullifLastName
String targetLastName = entity1.getLastName();
AdvancedQuery query = Querity.advancedQuery()
.filter(filterBy(PROPERTY_LAST_NAME, IS_NOT_NULL))
.select(nullif(property(PROPERTY_LAST_NAME), lit(targetLastName)).as("nullifLastName"))
.build();
List<Map<String, Object>> result = querity.findAllProjected(getEntityClass(), query);
assertThat(result).isNotEmpty();
List<Object> expected = entities.stream()
.filter(p -> p.getLastName() != null)
.map(p -> p.getLastName().equals(targetLastName) ? null : (Object) p.getLastName())
.toList();
assertThat(result.stream().map(map -> map.get("nullifLastName")).toList())
.containsExactlyInAnyOrderElementsOf(expected);
}
}

protected abstract Class<T> getEntityClass();
Expand Down
Loading