Program: 168906
Project: 168906
Buggy source:
@Override public Expression visit(FunctionExpression expression){
  if (com.blazebit.persistence.impl.util.ExpressionUtils.isSizeFunction(expression) && clause != ClauseType.WHERE) {
    PathExpression sizeArg=(PathExpression)expression.getExpressions().get(0);
    String property=sizeArg.getPathReference().getField();
    Class<?> startClass=((JoinNode)sizeArg.getBaseNode()).getPropertyClass();
    ManagedType<?> managedTargetType=MetamodelUtils.resolveManagedTargetType(metamodel,startClass,property);
    PersistenceType collectionIdType;
    if (managedTargetType instanceof EntityType<?>) {
      collectionIdType=((EntityType<?>)managedTargetType).getIdType().getPersistenceType();
    }
 else {
      throw new RuntimeException("String_Node_Str" + sizeArg.toString() + "String_Node_Str");
    }
    if (collectionIdType == PersistenceType.EMBEDDABLE || !metamodel.entity(startClass).hasSingleIdAttribute() || joinManager.getRoots().size() > 1 || clause == ClauseType.JOIN || !isCountTransformationEnabled() || (hasComplexGroupBySelects && !dbmsDialect.supportsComplexGroupBy()) || (hasGroupBySelects && !isImplicitGroupByFromSelectEnabled()) || jpaProvider.hasJoinTable(startClass,property)) {
      return generateSubquery(sizeArg,startClass);
    }
 else {
      List<PathElementExpression> pathElementExpr=new ArrayList<PathElementExpression>();
      List<JoinNode> roots=joinManager.getRoots();
      String rootAlias=roots.get(0).getAliasInfo().getAlias();
      String rootId=JpaUtils.getIdAttribute(metamodel.entity(roots.get(0).getPropertyClass())).getName();
      pathElementExpr.add(new PropertyExpression(rootAlias));
      pathElementExpr.add(new PropertyExpression(rootId));
      PathExpression groupByExpr=new PathExpression(pathElementExpr);
      joinManager.implicitJoin(groupByExpr,true,null,null,false,false,false);
      if (groupByManager.hasGroupBys() && !groupByManager.existsGroupBy(groupByExpr)) {
        return generateSubquery(sizeArg,startClass);
      }
      sizeArg.setUsedInCollectionFunction(false);
      AggregateExpression countExpr=new AggregateExpression(distinctRequired,"String_Node_Str",expression.getExpressions());
      transformedExpressions.add(countExpr);
      JoinNode originalNode=(JoinNode)sizeArg.getBaseNode();
      String nodeLookupKey=originalNode.getAliasInfo().getAbsolutePath() + "String_Node_Str" + sizeArg.getField();
      PathReference generatedJoin=generatedJoins.get(nodeLookupKey);
      if (generatedJoin == null) {
        joinManager.implicitJoin(sizeArg,true,null,clause,false,false,true);
        generatedJoin=sizeArg.getPathReference();
        generatedJoins.put(((JoinNode)generatedJoin.getBaseNode()).getAliasInfo().getAbsolutePath(),generatedJoin);
      }
 else {
        sizeArg.setPathReference(new SimplePathReference(generatedJoin.getBaseNode(),generatedJoin.getField(),null));
      }
      if (distinctRequired == false) {
        if (joinManager.getCollectionJoins().size() > 1) {
          distinctRequired=true;
          for (          AggregateExpression aggregateExpr : transformedExpressions) {
            aggregateExpr.setDistinct(true);
          }
        }
      }
      groupByManager.groupBy(groupByExpr);
      super.visit(expression);
      return countExpr;
    }
  }
 else {
    super.visit(expression);
  }
  return expression;
}

Fixed version (ground truth):
@Override public Expression visit(FunctionExpression expression){
  if (com.blazebit.persistence.impl.util.ExpressionUtils.isSizeFunction(expression) && clause != ClauseType.WHERE) {
    PathExpression sizeArg=(PathExpression)expression.getExpressions().get(0);
    String property=sizeArg.getPathReference().getField();
    Class<?> startClass=((JoinNode)sizeArg.getBaseNode()).getPropertyClass();
    ManagedType<?> managedTargetType=MetamodelUtils.resolveManagedTargetType(metamodel,startClass,property);
    PersistenceType collectionIdType;
    if (managedTargetType instanceof EntityType<?>) {
      collectionIdType=((EntityType<?>)managedTargetType).getIdType().getPersistenceType();
    }
 else {
      throw new RuntimeException("String_Node_Str" + sizeArg.toString() + "String_Node_Str");
    }
    if (collectionIdType == PersistenceType.EMBEDDABLE || !metamodel.entity(startClass).hasSingleIdAttribute() || joinManager.getRoots().size() > 1 || clause == ClauseType.JOIN || !isCountTransformationEnabled() || (hasComplexGroupBySelects && !dbmsDialect.supportsComplexGroupBy()) || (hasGroupBySelects && !isImplicitGroupByFromSelectEnabled())) {
      return generateSubquery(sizeArg,startClass);
    }
 else {
      List<PathElementExpression> pathElementExpr=new ArrayList<PathElementExpression>();
      List<JoinNode> roots=joinManager.getRoots();
      String rootAlias=roots.get(0).getAliasInfo().getAlias();
      String rootId=JpaUtils.getIdAttribute(metamodel.entity(roots.get(0).getPropertyClass())).getName();
      pathElementExpr.add(new PropertyExpression(rootAlias));
      pathElementExpr.add(new PropertyExpression(rootId));
      PathExpression groupByExpr=new PathExpression(pathElementExpr);
      joinManager.implicitJoin(groupByExpr,true,null,null,false,false,false);
      if (groupByManager.hasGroupBys() && !groupByManager.existsGroupBy(groupByExpr)) {
        return generateSubquery(sizeArg,startClass);
      }
      sizeArg.setUsedInCollectionFunction(false);
      AggregateExpression countExpr=new AggregateExpression(distinctRequired,"String_Node_Str",expression.getExpressions());
      transformedExpressions.add(countExpr);
      JoinNode originalNode=(JoinNode)sizeArg.getBaseNode();
      String nodeLookupKey=originalNode.getAliasInfo().getAbsolutePath() + "String_Node_Str" + sizeArg.getField();
      PathReference generatedJoin=generatedJoins.get(nodeLookupKey);
      if (generatedJoin == null) {
        joinManager.implicitJoin(sizeArg,true,null,clause,false,false,true);
        generatedJoin=sizeArg.getPathReference();
        generatedJoins.put(((JoinNode)generatedJoin.getBaseNode()).getAliasInfo().getAbsolutePath(),generatedJoin);
      }
 else {
        sizeArg.setPathReference(new SimplePathReference(generatedJoin.getBaseNode(),generatedJoin.getField(),null));
      }
      if (distinctRequired == false) {
        if (joinManager.getCollectionJoins().size() > 1) {
          distinctRequired=true;
          for (          AggregateExpression aggregateExpr : transformedExpressions) {
            aggregateExpr.setDistinct(true);
          }
        }
      }
      groupByManager.groupBy(groupByExpr);
      super.visit(expression);
      return countExpr;
    }
  }
 else {
    super.visit(expression);
  }
  return expression;
}