use of com.blazebit.persistence.parser.expression.GeneralCaseExpression 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.expression.GeneralCaseExpression in project blaze-persistence by Blazebit.
the class TypeEqTest method testTypeEqPath2.
@Test
public void testTypeEqPath2() {
entityTypes.put(Entity.class.getName(), Entity.class);
entityTypes.put(Entity.class.getSimpleName(), Entity.class);
GeneralCaseExpression actual = (GeneralCaseExpression) parse("CASE WHEN TYPE(a.b.c) = " + Entity.class.getName() + " THEN 0 ELSE 1 END");
GeneralCaseExpression expected = new GeneralCaseExpression(Arrays.asList(new WhenClauseExpression(new EqPredicate(typeFunction(path("a", "b", "c")), _entity(Entity.class)), _int("0"))), _int("1"));
assertEquals(expected, actual);
}
use of com.blazebit.persistence.parser.expression.GeneralCaseExpression in project blaze-persistence by Blazebit.
the class TypeEqTest method testPathEqType1.
@Test
public void testPathEqType1() {
entityTypes.put(Entity.class.getName(), Entity.class);
entityTypes.put(Entity.class.getSimpleName(), Entity.class);
GeneralCaseExpression actual = (GeneralCaseExpression) parse("CASE WHEN Entity = TYPE(d.e.f) THEN 0 ELSE 1 END");
GeneralCaseExpression expected = new GeneralCaseExpression(Arrays.asList(new WhenClauseExpression(new EqPredicate(_entity(Entity.class), typeFunction(path("d", "e", "f"))), _int("0"))), _int("1"));
assertEquals(expected, actual);
}
use of com.blazebit.persistence.parser.expression.GeneralCaseExpression in project blaze-persistence by Blazebit.
the class TypeEqTest method testTypeEqPath1.
@Test
public void testTypeEqPath1() {
entityTypes.put(Entity.class.getName(), Entity.class);
entityTypes.put(Entity.class.getSimpleName(), Entity.class);
GeneralCaseExpression actual = (GeneralCaseExpression) parse("CASE WHEN TYPE(a.b.c) = Entity THEN 0 ELSE 1 END");
GeneralCaseExpression expected = new GeneralCaseExpression(Arrays.asList(new WhenClauseExpression(new EqPredicate(typeFunction(path("a", "b", "c")), _entity(Entity.class)), _int("0"))), _int("1"));
assertEquals(expected, actual);
}
use of com.blazebit.persistence.parser.expression.GeneralCaseExpression in project blaze-persistence by Blazebit.
the class LogicOptimizationTest method testOptimizeNegationStructure4.
@Test
public void testOptimizeNegationStructure4() {
Expression result = parseOptimized("CASE WHEN NOT(NOT(a > b)) THEN 1 ELSE 0 END");
GeneralCaseExpression expected = new GeneralCaseExpression(Arrays.asList(new WhenClauseExpression(new GtPredicate(path("a"), path("b")), _int("1"))), _int("0"));
assertEquals(expected, result);
}
Aggregations