use of com.blazebit.persistence.parser.expression.AggregateExpression in project blaze-persistence by Blazebit.
the class SizeTransformationVisitor method getSizeExpression.
private Expression getSizeExpression(ExpressionModifier parentModifier, PathExpression sizeArg) {
JoinNode sizeArgJoin = (JoinNode) sizeArg.getBaseNode();
String property = sizeArg.getPathReference().getField();
final Type<?> nodeType = ((JoinNode) sizeArg.getBaseNode()).getNodeType();
if (!(nodeType instanceof EntityType<?>)) {
throw new IllegalArgumentException("Size on a collection owned by a non-entity type is not supported yet: " + sizeArg);
}
final EntityType<?> startType = (EntityType<?>) nodeType;
AttributeHolder result = JpaUtils.getAttributeForJoining(metamodel, sizeArg);
PluralAttribute<?, ?, ?> targetAttribute = (PluralAttribute<?, ?, ?>) result.getAttribute();
if (targetAttribute == null) {
throw new RuntimeException("Attribute [" + property + "] not found on class " + startType.getJavaType().getName());
}
final PluralAttribute.CollectionType collectionType = targetAttribute.getCollectionType();
final boolean isElementCollection = jpaProvider.getJpaMetamodelAccessor().isElementCollection(targetAttribute);
boolean subqueryRequired;
if (isElementCollection) {
subqueryRequired = false;
} else {
ManagedType<?> managedTargetType = (ManagedType<?>) result.getAttributeType();
if (managedTargetType instanceof EntityType<?>) {
// we could also generate counts for collections with embeddable id but we do not implement this for now
subqueryRequired = ((EntityType<?>) managedTargetType).getIdType().getPersistenceType() == PersistenceType.EMBEDDABLE;
} else {
throw new RuntimeException("Path [" + sizeArg.toString() + "] does not refer to a collection");
}
}
// build group by id clause
List<PathExpression> groupByExprs = new ArrayList<>();
for (SingularAttribute<?, ?> idAttribute : JpaMetamodelUtils.getIdAttributes(startType)) {
List<PathElementExpression> pathElementExpr = new ArrayList<>(2);
pathElementExpr.add(new PropertyExpression(sizeArgJoin.getAlias()));
pathElementExpr.add(new PropertyExpression(idAttribute.getName()));
PathExpression groupByExpr = new PathExpression(pathElementExpr);
groupByExprs.add(groupByExpr);
}
subqueryRequired = subqueryRequired || // we could also generate counts for collections with IdClass attributes but we do not implement this for now
!startType.hasSingleIdAttribute() || joinManager.getRoots().size() > 1 || clause == ClauseType.JOIN || !isCountTransformationEnabled() || // for now, we always generate a subquery when a bag is encountered
jpaProvider.isBag((EntityType<?>) targetAttribute.getDeclaringType(), targetAttribute.getName()) || requiresBlacklistedNode(sizeArg) || aggregateFunctionContext;
if (subqueryRequired) {
return wrapSubqueryConditionally(generateSubquery(sizeArg), aggregateFunctionContext);
} else {
if (currentJoinNode != null && (!currentJoinNode.equals(sizeArgJoin))) {
int currentJoinDepth = currentJoinNode.getJoinDepth();
int sizeArgJoinDepth = sizeArgJoin.getJoinDepth();
if (currentJoinDepth > sizeArgJoinDepth) {
return wrapSubqueryConditionally(generateSubquery(sizeArg), aggregateFunctionContext);
} else {
// we have to change all transformed expressions to subqueries
for (TransformedExpressionEntry transformedExpressionEntry : transformedExpressions) {
PathExpression originalSizeArg = transformedExpressionEntry.getOriginalSizeArg();
Expression subquery = wrapSubqueryConditionally(generateSubquery(originalSizeArg), transformedExpressionEntry.isAggregateFunctionContext());
transformedExpressionEntry.getParentModifier().set(subquery);
}
transformedExpressions.clear();
requiredGroupBys.clear();
lateJoins.clear();
distinctRequired = false;
if (currentJoinDepth == sizeArgJoinDepth) {
return wrapSubqueryConditionally(generateSubquery(sizeArg), aggregateFunctionContext);
}
}
}
for (PathExpression groupByExpr : groupByExprs) {
joinManager.implicitJoin(groupByExpr, true, true, true, null, null, new HashSet<String>(), false, false, false, false);
}
PathExpression originalSizeArg = sizeArg.copy(ExpressionCopyContext.EMPTY);
originalSizeArg.setPathReference(sizeArg.getPathReference());
sizeArg.setUsedInCollectionFunction(false);
List<Expression> countArguments = new ArrayList<>();
String joinLookupKey = getJoinLookupKey(sizeArg);
LateJoinEntry lateJoin = lateJoins.get(joinLookupKey);
if (lateJoin == null) {
lateJoin = new LateJoinEntry();
lateJoins.put(joinLookupKey, lateJoin);
}
lateJoin.getExpressionsToJoin().add(sizeArg);
lateJoin.getClauseDependencies().add(clause);
if ((isElementCollection && collectionType != PluralAttribute.CollectionType.MAP) || collectionType == PluralAttribute.CollectionType.SET) {
if (IDENTIFIABLE_PERSISTENCE_TYPES.contains(targetAttribute.getElementType().getPersistenceType()) && targetAttribute.isCollection()) {
// append id attribute name of joinable size argument
PluralAttribute<?, ?, ?> sizeArgTargetAttribute = (PluralAttribute<?, ?, ?>) JpaMetamodelUtils.getAttribute(startType, sizeArg.getPathReference().getField());
for (Attribute<?, ?> idAttribute : JpaMetamodelUtils.getIdAttributes(((IdentifiableType<?>) sizeArgTargetAttribute.getElementType()))) {
List<PathElementExpression> pathElementExpressions = new ArrayList<>(sizeArg.getExpressions().size() + 1);
pathElementExpressions.addAll(sizeArg.getExpressions());
pathElementExpressions.add(new PropertyExpression(idAttribute.getName()));
PathExpression pathExpression = new PathExpression(pathElementExpressions);
countArguments.add(pathExpression);
lateJoin.getExpressionsToJoin().add(pathExpression);
}
} else {
countArguments.add(sizeArg);
}
} else {
sizeArg.setCollectionQualifiedPath(true);
if (collectionType == PluralAttribute.CollectionType.LIST) {
countArguments.add(new ListIndexExpression(sizeArg));
} else {
countArguments.add(new MapKeyExpression(sizeArg));
}
}
AggregateExpression countExpr = createCountFunction(distinctRequired, countArguments);
transformedExpressions.add(new TransformedExpressionEntry(countExpr, originalSizeArg, parentModifier, aggregateFunctionContext));
currentJoinNode = (JoinNode) originalSizeArg.getBaseNode();
if (!distinctRequired) {
if (lateJoins.size() + joinManager.getCollectionJoins().size() > 1) {
distinctRequired = true;
/*
* As soon as we encounter another collection join, set previously
* performed transformations to distinct.
*/
for (TransformedExpressionEntry transformedExpressionEntry : transformedExpressions) {
AggregateExpression transformedExpr = transformedExpressionEntry.getTransformedExpression();
if (ExpressionUtils.isCustomFunctionInvocation(transformedExpr) && AbstractCountFunction.FUNCTION_NAME.equalsIgnoreCase(((StringLiteral) transformedExpr.getExpressions().get(0)).getValue())) {
Expression possibleDistinct = transformedExpr.getExpressions().get(1);
if (!(possibleDistinct instanceof StringLiteral) || !AbstractCountFunction.DISTINCT_QUALIFIER.equals(((StringLiteral) possibleDistinct).getValue())) {
transformedExpr.getExpressions().add(1, new StringLiteral(AbstractCountFunction.DISTINCT_QUALIFIER));
}
} else {
transformedExpr.setDistinct(true);
}
}
}
}
for (Expression groupByExpr : groupByExprs) {
String groupByExprString = groupByExpr.toString();
ResolvedExpression resolvedExpression = new ResolvedExpression(groupByExprString, groupByExpr);
Set<ClauseType> clauseTypes = requiredGroupBys.get(resolvedExpression);
if (clauseTypes == null) {
requiredGroupBys.put(resolvedExpression, EnumSet.of(clause));
} else {
clauseTypes.add(clause);
}
}
return countExpr;
}
}
use of com.blazebit.persistence.parser.expression.AggregateExpression in project blaze-persistence by Blazebit.
the class SimpleQueryGenerator method visit.
@Override
public void visit(FunctionExpression expression) {
if (expression.getRealArgument() != null) {
expression.getRealArgument().accept(this);
return;
}
BooleanLiteralRenderingContext oldBooleanLiteralRenderingContext = setBooleanLiteralRenderingContext(BooleanLiteralRenderingContext.PLAIN);
ParameterRenderingMode oldParameterRenderingMode = setParameterRenderingMode(ParameterRenderingMode.PLACEHOLDER);
List<Expression> expressions = expression.getExpressions();
int size = expressions.size();
boolean hasExpressions = size != 0;
String functionName = expression.getFunctionName();
WindowDefinition windowDefinition = expression.getWindowDefinition();
if (expression instanceof AggregateExpression && windowDefinition != null && windowDefinition.isFilterOnly()) {
sb.append(functionName, "window_".length(), functionName.length());
} else {
sb.append(functionName);
}
// @formatter:off
if (!"CURRENT_TIME".equalsIgnoreCase(functionName) && !"CURRENT_DATE".equalsIgnoreCase(functionName) && !"CURRENT_TIMESTAMP".equalsIgnoreCase(functionName)) {
// @formatter:on
sb.append('(');
if (expression instanceof AggregateExpression) {
AggregateExpression aggregateExpression = (AggregateExpression) expression;
if (aggregateExpression.isDistinct()) {
sb.append("DISTINCT ");
}
if (!hasExpressions && "COUNT".equalsIgnoreCase(aggregateExpression.getFunctionName())) {
sb.append('*');
}
}
if (hasExpressions) {
expressions.get(0).accept(this);
for (int i = 1; i < size; i++) {
sb.append(",");
expressions.get(i).accept(this);
}
}
sb.append(')');
List<OrderByItem> withinGroup = expression.getWithinGroup();
if (withinGroup != null && !withinGroup.isEmpty()) {
sb.append(" WITHIN GROUP (");
for (int i = 0; i < withinGroup.size(); i++) {
visit(withinGroup.get(i));
sb.append(", ");
}
sb.setLength(sb.length() - 1);
sb.setCharAt(sb.length() - 1, ')');
}
visitWindowDefinition(windowDefinition);
}
setBooleanLiteralRenderingContext(oldBooleanLiteralRenderingContext);
setParameterRenderingMode(oldParameterRenderingMode);
}
use of com.blazebit.persistence.parser.expression.AggregateExpression in project blaze-persistence by Blazebit.
the class GroupByExpressionGatheringVisitor method extractGroupByExpressions.
public Set<Expression> extractGroupByExpressions(Expression expression, ClauseType clauseType) {
// Also, grouping by just a parameter does not make sense as the value is constant
if (expression instanceof LiteralExpression || expression instanceof ParameterExpression) {
return Collections.emptySet();
}
clear();
try {
// When having a predicate at the top level, we have to collect
collect = expression instanceof Predicate;
boolean expressionWasSplit = expression.accept(this);
// In the HAVING clause we additionally add the path expressions to the expression set for DBMS like MySQL that's don't support partial expression structure matching
if (clauseType == ClauseType.HAVING && !dbmsDialect.supportsGroupByExpressionInHavingMatching()) {
expression.accept(new VisitorAdapter() {
@Override
public void visit(FunctionExpression expression) {
// Skip aggregate expressions
if (expression instanceof AggregateExpression || (treatSizeAsAggregate && com.blazebit.persistence.parser.util.ExpressionUtils.isSizeFunction(expression))) {
return;
}
super.visit(expression);
}
@Override
public void visit(SubqueryExpression expression) {
GroupByExpressionGatheringVisitor.this.visit(expression);
}
@Override
public void visit(PathExpression expression) {
if (expression.getBaseNode() == null) {
((SelectInfo) aliasManager.getAliasInfo(expression.toString())).getExpression().accept(this);
} else {
expressions.add(expression);
}
}
@Override
public void visit(TreatExpression expression) {
expressions.add(expression);
}
@Override
public void visit(PropertyExpression expression) {
expressions.add(expression);
}
@Override
public void visit(ListIndexExpression expression) {
expressions.add(expression);
}
@Override
public void visit(MapEntryExpression expression) {
expressions.add(expression);
}
@Override
public void visit(MapKeyExpression expression) {
expressions.add(expression);
}
@Override
public void visit(MapValueExpression expression) {
expressions.add(expression);
}
});
return expressions;
}
if (expressionWasSplit) {
return expressions;
}
} catch (IllegalParameterException ex) {
throw new IllegalArgumentException("Can't use the expression '" + expression + "' as an implicit group by clause, because the parameter '" + ex.parameterExpression + "' with the value '" + ex.value + "' can't be rendered as literal which is required!", ex);
}
return Collections.singleton(expression);
}
Aggregations