use of com.blazebit.persistence.parser.predicate.IsNullPredicate in project blaze-persistence by Blazebit.
the class OrderByManager method buildImplicitGroupByClauses.
/**
* Builds the clauses needed for the group by clause for a query that uses aggregate functions to work.
*
* @return
*/
void buildImplicitGroupByClauses(GroupByManager groupByManager, boolean hasGroupBy, JoinVisitor joinVisitor) {
if (orderByInfos.isEmpty()) {
return;
}
SimpleQueryGenerator.BooleanLiteralRenderingContext oldBooleanLiteralRenderingContext = queryGenerator.setBooleanLiteralRenderingContext(SimpleQueryGenerator.BooleanLiteralRenderingContext.CASE_WHEN);
StringBuilder sb = new StringBuilder();
List<OrderByInfo> infos = orderByInfos;
boolean hasFullJoin = !jpaProvider.supportsNullPrecedenceExpression() && joinManager.hasFullJoin();
int size = infos.size();
for (int i = 0; i < size; i++) {
final OrderByInfo orderByInfo = infos.get(i);
String potentialSelectAlias = orderByInfo.getExpressionString();
AliasInfo aliasInfo = aliasManager.getAliasInfo(potentialSelectAlias);
Expression expr;
if (aliasInfo instanceof SelectInfo) {
SelectInfo selectInfo = (SelectInfo) aliasInfo;
expr = selectInfo.getExpression();
} else {
expr = orderByInfo.getExpression();
}
Set<Expression> extractedGroupByExpressions = groupByExpressionGatheringVisitor.extractGroupByExpressions(expr, getClauseType());
if (!extractedGroupByExpressions.isEmpty()) {
queryGenerator.setClauseType(ClauseType.GROUP_BY);
queryGenerator.setQueryBuffer(sb);
for (Expression extractedExpression : extractedGroupByExpressions) {
sb.setLength(0);
queryGenerator.generate(extractedExpression);
String expressionString = sb.toString();
if (!jpaProvider.supportsNullPrecedenceExpression()) {
boolean nullable = hasFullJoin || ExpressionUtils.isNullable(metamodel, functionalDependencyAnalyzerVisitor.getConstantifiedJoinNodeAttributeCollector(), extractedExpression);
Expression resultExpression;
Expression defaultExpression;
if (nullable) {
sb.insert(0, "CASE WHEN ");
sb.append(" IS NULL THEN ");
if (orderByInfo.nullFirst) {
resultExpression = new NumericLiteral("0", NumericType.INTEGER);
defaultExpression = new NumericLiteral("1", NumericType.INTEGER);
sb.append("0 ELSE 1 END");
} else {
resultExpression = new NumericLiteral("1", NumericType.INTEGER);
defaultExpression = new NumericLiteral("0", NumericType.INTEGER);
sb.append("1 ELSE 0 END");
}
List<WhenClauseExpression> whenClauses = new ArrayList<>(1);
whenClauses.add(new WhenClauseExpression(new IsNullPredicate(extractedExpression.copy(ExpressionCopyContext.CLONE)), resultExpression));
Expression nullEmulationExpression = new GeneralCaseExpression(whenClauses, defaultExpression);
String nullPrecedenceEmulationExpression = sb.toString();
groupByManager.collect(new ResolvedExpression(nullPrecedenceEmulationExpression, nullEmulationExpression), ClauseType.ORDER_BY, hasGroupBy, joinVisitor);
}
}
groupByManager.collect(new ResolvedExpression(expressionString, extractedExpression), ClauseType.ORDER_BY, hasGroupBy, joinVisitor);
}
queryGenerator.setClauseType(null);
}
}
queryGenerator.setBooleanLiteralRenderingContext(oldBooleanLiteralRenderingContext);
groupByExpressionGatheringVisitor.clear();
}
use of com.blazebit.persistence.parser.predicate.IsNullPredicate in project blaze-persistence by Blazebit.
the class GeneralParserTest method testIsNull.
@Test
public void testIsNull() {
GeneralCaseExpression result = (GeneralCaseExpression) parse("CASE WHEN a.x IS NULL THEN 0 ELSE 1 END");
GeneralCaseExpression expected = new GeneralCaseExpression(Arrays.asList(new WhenClauseExpression(new IsNullPredicate(path("a", "x")), _int("0"))), _int("1"));
assertEquals(expected, result);
}
use of com.blazebit.persistence.parser.predicate.IsNullPredicate in project blaze-persistence by Blazebit.
the class GeneralParserTest method testIsNotNull.
@Test
public void testIsNotNull() {
GeneralCaseExpression result = (GeneralCaseExpression) parse("CASE WHEN a.x IS NOT NULL THEN 0 ELSE 1 END");
GeneralCaseExpression expected = new GeneralCaseExpression(Arrays.asList(new WhenClauseExpression(new IsNullPredicate(path("a", "x"), true), _int("0"))), _int("1"));
assertEquals(expected, result);
}
use of com.blazebit.persistence.parser.predicate.IsNullPredicate in project blaze-persistence by Blazebit.
the class EqualityCheckingVisitor method visit.
@Override
public Boolean visit(IsNullPredicate predicate) {
if (referenceExpression.getClass() != predicate.getClass()) {
return Boolean.TRUE;
}
IsNullPredicate referencePredicate = (IsNullPredicate) referenceExpression;
if (referencePredicate.isNegated() != predicate.isNegated()) {
return Boolean.TRUE;
}
referenceExpression = referencePredicate.getExpression();
return predicate.getExpression().accept(this);
}
use of com.blazebit.persistence.parser.predicate.IsNullPredicate in project blaze-persistence by Blazebit.
the class GeneralParserTest method testTreatPredicate1.
@Test
public void testTreatPredicate1() {
Predicate result = parsePredicate("TREAT(d AS GoodDocument).name IS NOT NULL", false);
Predicate expected = new IsNullPredicate(path(treat(path("d"), "GoodDocument"), "name"), true);
assertEquals(expected, result);
}
Aggregations